script.c 578 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. //#define DEBUG_DUMP_STACK
  8. #ifdef PCRE_SUPPORT
  9. #include "../../3rdparty/pcre/include/pcre.h" // preg_match
  10. #endif
  11. #include "../common/cbasetypes.h"
  12. #include "../common/malloc.h"
  13. #include "../common/md5calc.h"
  14. #include "../common/nullpo.h"
  15. #include "../common/random.h"
  16. #include "../common/showmsg.h"
  17. #include "../common/socket.h"
  18. #include "../common/strlib.h"
  19. #include "../common/timer.h"
  20. #include "../common/utils.h"
  21. #ifdef BETA_THREAD_TEST
  22. #include "../common/atomic.h"
  23. #include "../common/spinlock.h"
  24. #include "../common/thread.h"
  25. #include "../common/mutex.h"
  26. #endif
  27. #include "map.h"
  28. #include "path.h"
  29. #include "clif.h"
  30. #include "chrif.h"
  31. #include "itemdb.h"
  32. #include "pc.h"
  33. #include "storage.h"
  34. #include "pet.h"
  35. #include "mapreg.h"
  36. #include "homunculus.h"
  37. #include "instance.h"
  38. #include "mercenary.h"
  39. #include "intif.h"
  40. #include "chat.h"
  41. #include "battleground.h"
  42. #include "party.h"
  43. #include "mail.h"
  44. #include "script.h"
  45. #include "quest.h"
  46. #include "elemental.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <math.h>
  51. #ifndef WIN32
  52. #endif
  53. #include <setjmp.h>
  54. #include <errno.h>
  55. TBL_PC *script_rid2sd(struct script_state *st);
  56. /**
  57. * Get `sd` from a account id in `loc` param instead of attached rid
  58. * @param st Script
  59. * @param loc Location to look account id in script parameter
  60. * @param sd Variable that will be assigned
  61. * @return True if `sd` is assigned, false otherwise
  62. **/
  63. static bool script_accid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  64. if (script_hasdata(st, loc)) {
  65. int id_ = script_getnum(st, loc);
  66. if (!(*sd = map_id2sd(id_)))
  67. ShowError("%s: Player with account id '%d' is not found.\n", func, id_);
  68. }
  69. else
  70. *sd = script_rid2sd(st);
  71. return (*sd) ? true : false;
  72. }
  73. /**
  74. * Get `sd` from a char id in `loc` param instead of attached rid
  75. * @param st Script
  76. * @param loc Location to look char id in script parameter
  77. * @param sd Variable that will be assigned
  78. * @return True if `sd` is assigned, false otherwise
  79. **/
  80. static bool script_charid2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  81. if (script_hasdata(st, loc)) {
  82. int id_ = script_getnum(st, loc);
  83. if (!(*sd = map_charid2sd(id_)))
  84. ShowError("%s: Player with char id '%d' is not found.\n", func, id_);
  85. }
  86. else
  87. *sd = script_rid2sd(st);
  88. return (*sd) ? true : false;
  89. }
  90. /**
  91. * Get `sd` from a nick in `loc` param instead of attached rid
  92. * @param st Script
  93. * @param loc Location to look nick in script parameter
  94. * @param sd Variable that will be assigned
  95. * @return True if `sd` is assigned, false otherwise
  96. **/
  97. static bool script_nick2sd_(struct script_state *st, uint8 loc, struct map_session_data **sd, const char *func) {
  98. if (script_hasdata(st, loc)) {
  99. const char *name_ = script_getstr(st, loc);
  100. if (!(*sd = map_nick2sd(name_)))
  101. ShowError("%s: Player with nick '%s' is not found.\n", func, name_);
  102. }
  103. else
  104. *sd = script_rid2sd(st);
  105. return (*sd) ? true : false;
  106. }
  107. #define script_accid2sd(loc,sd) script_accid2sd_(st,(loc),&(sd),__FUNCTION__)
  108. #define script_charid2sd(loc,sd) script_charid2sd_(st,(loc),&(sd),__FUNCTION__)
  109. #define script_nick2sd(loc,sd) script_nick2sd_(st,(loc),&(sd),__FUNCTION__)
  110. /// temporary buffer for passing around compiled bytecode
  111. /// @see add_scriptb, set_label, parse_script
  112. static unsigned char* script_buf = NULL;
  113. static int script_pos = 0, script_size = 0;
  114. static inline int GETVALUE(const unsigned char* buf, int i)
  115. {
  116. return (int)MakeDWord(MakeWord(buf[i], buf[i+1]), MakeWord(buf[i+2], 0));
  117. }
  118. static inline void SETVALUE(unsigned char* buf, int i, int n)
  119. {
  120. buf[i] = GetByte(n, 0);
  121. buf[i+1] = GetByte(n, 1);
  122. buf[i+2] = GetByte(n, 2);
  123. }
  124. // String buffer structures.
  125. // str_data stores string information
  126. static struct str_data_struct {
  127. enum c_op type;
  128. int str;
  129. int backpatch;
  130. int label;
  131. int (*func)(struct script_state *st);
  132. int val;
  133. int next;
  134. } *str_data = NULL;
  135. static int str_data_size = 0; // size of the data
  136. static int str_num = LABEL_START; // next id to be assigned
  137. // str_buf holds the strings themselves
  138. static char *str_buf;
  139. static int str_size = 0; // size of the buffer
  140. static int str_pos = 0; // next position to be assigned
  141. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  142. #define SCRIPT_HASH_SIZE 1021
  143. int str_hash[SCRIPT_HASH_SIZE];
  144. // Specifies which string hashing method to use
  145. //#define SCRIPT_HASH_DJB2
  146. //#define SCRIPT_HASH_SDBM
  147. #define SCRIPT_HASH_ELF
  148. static DBMap* scriptlabel_db = NULL; // const char* label_name -> int script_pos
  149. static DBMap* userfunc_db = NULL; // const char* func_name -> struct script_code*
  150. static int parse_options = 0;
  151. DBMap* script_get_label_db(void) { return scriptlabel_db; }
  152. DBMap* script_get_userfunc_db(void) { return userfunc_db; }
  153. // important buildin function references for usage in scripts
  154. static int buildin_set_ref = 0;
  155. static int buildin_callsub_ref = 0;
  156. static int buildin_callfunc_ref = 0;
  157. static int buildin_getelementofarray_ref = 0;
  158. // Caches compiled autoscript item code.
  159. // Note: This is not cleared when reloading itemdb.
  160. static DBMap* autobonus_db = NULL; // char* script -> char* bytecode
  161. struct Script_Config script_config = {
  162. 1, // warn_func_mismatch_argtypes
  163. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  164. 0, INT_MAX, // input_min_value/input_max_value
  165. "OnPCDieEvent", //die_event_name
  166. "OnPCKillEvent", //kill_pc_event_name
  167. "OnNPCKillEvent", //kill_mob_event_name
  168. "OnPCLoginEvent", //login_event_name
  169. "OnPCLogoutEvent", //logout_event_name
  170. "OnPCLoadMapEvent", //loadmap_event_name
  171. "OnPCBaseLvUpEvent", //baselvup_event_name
  172. "OnPCJobLvUpEvent", //joblvup_event_name
  173. "OnPCStatCalcEvent", //stat_calc_event_name
  174. "OnTouch_", //ontouch_name (runs on first visible char to enter area, picks another char if the first char leaves)
  175. "OnTouch", //ontouch2_name (run whenever a char walks into the OnTouch area)
  176. };
  177. static jmp_buf error_jump;
  178. static char* error_msg;
  179. static const char* error_pos;
  180. static int error_report; // if the error should produce output
  181. // Used by disp_warning_message
  182. static const char* parser_current_src;
  183. static const char* parser_current_file;
  184. static int parser_current_line;
  185. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  186. // [Eoe / jA 1080, 1081, 1094, 1164]
  187. enum curly_type {
  188. TYPE_NULL = 0,
  189. TYPE_IF,
  190. TYPE_SWITCH,
  191. TYPE_WHILE,
  192. TYPE_FOR,
  193. TYPE_DO,
  194. TYPE_USERFUNC,
  195. TYPE_ARGLIST // function argument list
  196. };
  197. enum e_arglist
  198. {
  199. ARGLIST_UNDEFINED = 0,
  200. ARGLIST_NO_PAREN = 1,
  201. ARGLIST_PAREN = 2,
  202. };
  203. static struct {
  204. struct {
  205. enum curly_type type;
  206. int index;
  207. int count;
  208. int flag;
  209. struct linkdb_node *case_label;
  210. } curly[256]; // Information right parenthesis
  211. int curly_count; // The number of right brackets
  212. int index; // Number of the syntax used in the script
  213. } syntax;
  214. const char* parse_curly_close(const char* p);
  215. const char* parse_syntax_close(const char* p);
  216. const char* parse_syntax_close_sub(const char* p,int* flag);
  217. const char* parse_syntax(const char* p);
  218. static int parse_syntax_for_flag = 0;
  219. extern short current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  220. extern unsigned int current_equip_combo_pos;
  221. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  222. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  223. int potion_target = 0;
  224. unsigned int *generic_ui_array = NULL;
  225. unsigned int generic_ui_array_size = 0;
  226. c_op get_com(unsigned char *script,int *pos);
  227. int get_num(unsigned char *script,int *pos);
  228. typedef struct script_function {
  229. int (*func)(struct script_state *st);
  230. const char *name;
  231. const char *arg;
  232. } script_function;
  233. extern script_function buildin_func[];
  234. #ifdef BETA_THREAD_TEST
  235. /**
  236. * MySQL Query Slave
  237. **/
  238. static SPIN_LOCK queryThreadLock;
  239. static rAthread queryThread = NULL;
  240. static ramutex queryThreadMutex = NULL;
  241. static racond queryThreadCond = NULL;
  242. static volatile int32 queryThreadTerminate = 0;
  243. struct queryThreadEntry {
  244. bool ok;
  245. bool type; /* main db or log db? */
  246. struct script_state *st;
  247. };
  248. /* Ladies and Gentleman the Manager! */
  249. struct {
  250. struct queryThreadEntry **entry;/* array of structs */
  251. int count;
  252. int timer;/* used to receive processed entries */
  253. } queryThreadData;
  254. #endif
  255. /*==========================================
  256. * (Only those needed) local declaration prototype
  257. *------------------------------------------*/
  258. const char* parse_subexpr(const char* p,int limit);
  259. int run_func(struct script_state *st);
  260. int script_instancegetid(struct script_state *st);
  261. enum {
  262. MF_NOMEMO, //0
  263. MF_NOTELEPORT,
  264. MF_NOSAVE,
  265. MF_NOBRANCH,
  266. MF_NOPENALTY,
  267. MF_NOZENYPENALTY,
  268. MF_PVP,
  269. MF_PVP_NOPARTY,
  270. MF_PVP_NOGUILD,
  271. MF_GVG,
  272. MF_GVG_NOPARTY, //10
  273. MF_NOTRADE,
  274. MF_NOSKILL,
  275. MF_NOWARP,
  276. MF_PARTYLOCK,
  277. MF_NOICEWALL,
  278. MF_SNOW,
  279. MF_FOG,
  280. MF_SAKURA,
  281. MF_LEAVES,
  282. /**
  283. * No longer available, keeping here just in case it's back someday. [Ind]
  284. **/
  285. //MF_RAIN, //20
  286. // 21 free
  287. MF_NOGO = 22,
  288. MF_CLOUDS,
  289. MF_CLOUDS2,
  290. MF_FIREWORKS,
  291. MF_GVG_CASTLE,
  292. MF_GVG_DUNGEON,
  293. MF_NIGHTENABLED,
  294. MF_NOBASEEXP,
  295. MF_NOJOBEXP, //30
  296. MF_NOMOBLOOT,
  297. MF_NOMVPLOOT,
  298. MF_NORETURN,
  299. MF_NOWARPTO,
  300. MF_NIGHTMAREDROP,
  301. MF_RESTRICTED,
  302. MF_NOCOMMAND,
  303. MF_NODROP,
  304. MF_JEXP,
  305. MF_BEXP, //40
  306. MF_NOVENDING,
  307. MF_LOADEVENT,
  308. MF_NOCHAT,
  309. MF_NOEXPPENALTY,
  310. MF_GUILDLOCK,
  311. MF_TOWN,
  312. MF_AUTOTRADE,
  313. MF_ALLOWKS,
  314. MF_MONSTER_NOTELEPORT,
  315. MF_PVP_NOCALCRANK, //50
  316. MF_BATTLEGROUND,
  317. MF_RESET,
  318. MF_CHANNELAUTOJOIN,
  319. MF_NOUSECART,
  320. MF_NOITEMCONSUMPTION,
  321. MF_SUMSTARTMIRACLE,
  322. MF_NOMINEEFFECT,
  323. MF_NOLOCKON,
  324. MF_NOTOMB,
  325. MF_SKILL_DAMAGE, //60
  326. MF_GVG_TE_CASTLE,
  327. MF_GVG_TE,
  328. };
  329. const char* script_op2name(int op)
  330. {
  331. #define RETURN_OP_NAME(type) case type: return #type
  332. switch( op )
  333. {
  334. RETURN_OP_NAME(C_NOP);
  335. RETURN_OP_NAME(C_POS);
  336. RETURN_OP_NAME(C_INT);
  337. RETURN_OP_NAME(C_PARAM);
  338. RETURN_OP_NAME(C_FUNC);
  339. RETURN_OP_NAME(C_STR);
  340. RETURN_OP_NAME(C_CONSTSTR);
  341. RETURN_OP_NAME(C_ARG);
  342. RETURN_OP_NAME(C_NAME);
  343. RETURN_OP_NAME(C_EOL);
  344. RETURN_OP_NAME(C_RETINFO);
  345. RETURN_OP_NAME(C_USERFUNC);
  346. RETURN_OP_NAME(C_USERFUNC_POS);
  347. // operators
  348. RETURN_OP_NAME(C_OP3);
  349. RETURN_OP_NAME(C_LOR);
  350. RETURN_OP_NAME(C_LAND);
  351. RETURN_OP_NAME(C_LE);
  352. RETURN_OP_NAME(C_LT);
  353. RETURN_OP_NAME(C_GE);
  354. RETURN_OP_NAME(C_GT);
  355. RETURN_OP_NAME(C_EQ);
  356. RETURN_OP_NAME(C_NE);
  357. RETURN_OP_NAME(C_XOR);
  358. RETURN_OP_NAME(C_OR);
  359. RETURN_OP_NAME(C_AND);
  360. RETURN_OP_NAME(C_ADD);
  361. RETURN_OP_NAME(C_SUB);
  362. RETURN_OP_NAME(C_MUL);
  363. RETURN_OP_NAME(C_DIV);
  364. RETURN_OP_NAME(C_MOD);
  365. RETURN_OP_NAME(C_NEG);
  366. RETURN_OP_NAME(C_LNOT);
  367. RETURN_OP_NAME(C_NOT);
  368. RETURN_OP_NAME(C_R_SHIFT);
  369. RETURN_OP_NAME(C_L_SHIFT);
  370. default:
  371. ShowDebug("script_op2name: unexpected op=%d\n", op);
  372. return "???";
  373. }
  374. #undef RETURN_OP_NAME
  375. }
  376. #ifdef DEBUG_DUMP_STACK
  377. static void script_dump_stack(struct script_state* st)
  378. {
  379. int i;
  380. ShowMessage("\tstart = %d\n", st->start);
  381. ShowMessage("\tend = %d\n", st->end);
  382. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  383. ShowMessage("\tsp = %d\n", st->stack->sp);
  384. for( i = 0; i < st->stack->sp; ++i )
  385. {
  386. struct script_data* data = &st->stack->stack_data[i];
  387. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  388. switch( data->type )
  389. {
  390. case C_INT:
  391. case C_POS:
  392. ShowMessage(" %d\n", data->u.num);
  393. break;
  394. case C_STR:
  395. case C_CONSTSTR:
  396. ShowMessage(" \"%s\"\n", data->u.str);
  397. break;
  398. case C_NAME:
  399. ShowMessage(" \"%s\" (id=%d ref=%p subtype=%s)\n", reference_getname(data), data->u.num, data->ref, script_op2name(str_data[data->u.num].type));
  400. break;
  401. case C_RETINFO:
  402. {
  403. struct script_retinfo* ri = data->u.ri;
  404. ShowMessage(" %p {scope.vars=%p, scope.arrays=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->scope.vars, ri->scope.arrays, ri->script, ri->pos, ri->nargs, ri->defsp);
  405. }
  406. break;
  407. default:
  408. ShowMessage("\n");
  409. break;
  410. }
  411. }
  412. }
  413. #endif
  414. /// Reports on the console the src of a script error.
  415. static void script_reportsrc(struct script_state *st)
  416. {
  417. struct block_list* bl;
  418. if( st->oid == 0 )
  419. return; //Can't report source.
  420. bl = map_id2bl(st->oid);
  421. if( bl == NULL )
  422. return;
  423. switch( bl->type ) {
  424. case BL_NPC:
  425. if( bl->m >= 0 )
  426. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  427. else
  428. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  429. break;
  430. default:
  431. if( bl->m >= 0 )
  432. 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);
  433. else
  434. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  435. break;
  436. }
  437. }
  438. /// Reports on the console information about the script data.
  439. static void script_reportdata(struct script_data* data)
  440. {
  441. if( data == NULL )
  442. return;
  443. switch( data->type ) {
  444. case C_NOP:// no value
  445. ShowDebug("Data: nothing (nil)\n");
  446. break;
  447. case C_INT:// number
  448. ShowDebug("Data: number value=%"PRId64"\n", data->u.num);
  449. break;
  450. case C_STR:
  451. case C_CONSTSTR:// string
  452. if( data->u.str ) {
  453. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  454. } else {
  455. ShowDebug("Data: string value=NULL\n");
  456. }
  457. break;
  458. case C_NAME:// reference
  459. if( reference_tovariable(data) ) {// variable
  460. const char* name = reference_getname(data);
  461. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  462. } else if( reference_toconstant(data) ) {// constant
  463. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  464. } else if( reference_toparam(data) ) {// param
  465. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  466. } else {// ???
  467. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  468. ShowDebug("Please report this!!! - script->str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  469. }
  470. break;
  471. case C_POS:// label
  472. ShowDebug("Data: label pos=%"PRId64"\n", data->u.num);
  473. break;
  474. default:
  475. ShowDebug("Data: %s\n", script_op2name(data->type));
  476. break;
  477. }
  478. }
  479. /// Reports on the console information about the current built-in function.
  480. static void script_reportfunc(struct script_state* st)
  481. {
  482. int params, id;
  483. struct script_data* data;
  484. if( !script_hasdata(st,0) )
  485. {// no stack
  486. return;
  487. }
  488. data = script_getdata(st,0);
  489. if( !data_isreference(data) || str_data[reference_getid(data)].type != C_FUNC )
  490. {// script currently not executing a built-in function or corrupt stack
  491. return;
  492. }
  493. id = reference_getid(data);
  494. params = script_lastdata(st)-1;
  495. if( params > 0 )
  496. {
  497. int i;
  498. ShowDebug("Function: %s (%d parameter%s):\n", get_str(id), params, ( params == 1 ) ? "" : "s");
  499. for( i = 2; i <= script_lastdata(st); i++ )
  500. {
  501. script_reportdata(script_getdata(st,i));
  502. }
  503. }
  504. else
  505. {
  506. ShowDebug("Function: %s (no parameters)\n", get_str(id));
  507. }
  508. }
  509. /*==========================================
  510. * Output error message
  511. *------------------------------------------*/
  512. static void disp_error_message2(const char *mes,const char *pos,int report)
  513. {
  514. error_msg = aStrdup(mes);
  515. error_pos = pos;
  516. error_report = report;
  517. longjmp( error_jump, 1 );
  518. }
  519. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  520. static void disp_warning_message(const char *mes, const char *pos) {
  521. script_warning(parser_current_src,parser_current_file,parser_current_line,mes,pos);
  522. }
  523. /// Checks event parameter validity
  524. static void check_event(struct script_state *st, const char *evt)
  525. {
  526. if( evt && evt[0] && !stristr(evt, "::On") )
  527. {
  528. ShowWarning("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n", evt);
  529. script_reportsrc(st);
  530. }
  531. }
  532. /*==========================================
  533. * Hashes the input string
  534. *------------------------------------------*/
  535. static unsigned int calc_hash(const char* p)
  536. {
  537. unsigned int h;
  538. #if defined(SCRIPT_HASH_DJB2)
  539. h = 5381;
  540. while( *p ) // hash*33 + c
  541. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  542. #elif defined(SCRIPT_HASH_SDBM)
  543. h = 0;
  544. while( *p ) // hash*65599 + c
  545. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  546. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  547. h = 0;
  548. while( *p ){
  549. unsigned int g;
  550. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  551. g = h & 0xF0000000;
  552. if( g )
  553. {
  554. h ^= g >> 24;
  555. h &= ~g;
  556. }
  557. }
  558. #else // athena hash
  559. h = 0;
  560. while( *p )
  561. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  562. #endif
  563. return h % SCRIPT_HASH_SIZE;
  564. }
  565. /*==========================================
  566. * str_data manipulation functions
  567. *------------------------------------------*/
  568. /// Looks up string using the provided id.
  569. const char* get_str(int id)
  570. {
  571. Assert( id >= LABEL_START && id < str_size );
  572. return str_buf+str_data[id].str;
  573. }
  574. /// Returns the uid of the string, or -1.
  575. static int search_str(const char* p)
  576. {
  577. int i;
  578. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  579. if( strcasecmp(get_str(i),p) == 0 )
  580. return i;
  581. return -1;
  582. }
  583. /// Stores a copy of the string and returns its id.
  584. /// If an identical string is already present, returns its id instead.
  585. int add_str(const char* p)
  586. {
  587. int h;
  588. int len;
  589. h = calc_hash(p);
  590. if( str_hash[h] == 0 )
  591. {// empty bucket, add new node here
  592. str_hash[h] = str_num;
  593. }
  594. else
  595. {// scan for end of list, or occurence of identical string
  596. int i;
  597. for( i = str_hash[h]; ; i = str_data[i].next )
  598. {
  599. if( strcasecmp(get_str(i),p) == 0 )
  600. return i; // string already in list
  601. if( str_data[i].next == 0 )
  602. break; // reached the end
  603. }
  604. // append node to end of list
  605. str_data[i].next = str_num;
  606. }
  607. // grow list if neccessary
  608. if( str_num >= str_data_size )
  609. {
  610. str_data_size += 128;
  611. RECREATE(str_data,struct str_data_struct,str_data_size);
  612. memset(str_data + (str_data_size - 128), '\0', 128);
  613. }
  614. len=(int)strlen(p);
  615. // grow string buffer if neccessary
  616. while( str_pos+len+1 >= str_size )
  617. {
  618. str_size += 256;
  619. RECREATE(str_buf,char,str_size);
  620. memset(str_buf + (str_size - 256), '\0', 256);
  621. }
  622. safestrncpy(str_buf+str_pos, p, len+1);
  623. str_data[str_num].type = C_NOP;
  624. str_data[str_num].str = str_pos;
  625. str_data[str_num].next = 0;
  626. str_data[str_num].func = NULL;
  627. str_data[str_num].backpatch = -1;
  628. str_data[str_num].label = -1;
  629. str_pos += len+1;
  630. return str_num++;
  631. }
  632. /// Appends 1 byte to the script buffer.
  633. static void add_scriptb(int a)
  634. {
  635. if( script_pos+1 >= script_size )
  636. {
  637. script_size += SCRIPT_BLOCK_SIZE;
  638. RECREATE(script_buf,unsigned char,script_size);
  639. }
  640. script_buf[script_pos++] = (uint8)(a);
  641. }
  642. /// Appends a c_op value to the script buffer.
  643. /// The value is variable-length encoded into 8-bit blocks.
  644. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  645. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  646. static void add_scriptc(int a)
  647. {
  648. while( a >= 0x40 )
  649. {
  650. add_scriptb((a&0x3f)|0x40);
  651. a = (a - 0x40) >> 6;
  652. }
  653. add_scriptb(a);
  654. }
  655. /// Appends an integer value to the script buffer.
  656. /// The value is variable-length encoded into 8-bit blocks.
  657. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  658. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  659. static void add_scripti(int a)
  660. {
  661. while( a >= 0x40 )
  662. {
  663. add_scriptb((a&0x3f)|0xc0);
  664. a = (a - 0x40) >> 6;
  665. }
  666. add_scriptb(a|0x80);
  667. }
  668. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  669. ///
  670. /// @param l The id of the str_data entry
  671. // Maximum up to 16M
  672. static void add_scriptl(int l)
  673. {
  674. int backpatch = str_data[l].backpatch;
  675. switch(str_data[l].type){
  676. case C_POS:
  677. case C_USERFUNC_POS:
  678. add_scriptc(C_POS);
  679. add_scriptb(str_data[l].label);
  680. add_scriptb(str_data[l].label>>8);
  681. add_scriptb(str_data[l].label>>16);
  682. break;
  683. case C_NOP:
  684. case C_USERFUNC:
  685. // Embedded data backpatch there is a possibility of label
  686. add_scriptc(C_NAME);
  687. str_data[l].backpatch = script_pos;
  688. add_scriptb(backpatch);
  689. add_scriptb(backpatch>>8);
  690. add_scriptb(backpatch>>16);
  691. break;
  692. case C_INT:
  693. add_scripti(abs(str_data[l].val));
  694. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  695. add_scriptc(C_NEG);
  696. break;
  697. default: // assume C_NAME
  698. add_scriptc(C_NAME);
  699. add_scriptb(l);
  700. add_scriptb(l>>8);
  701. add_scriptb(l>>16);
  702. break;
  703. }
  704. }
  705. /*==========================================
  706. * Resolve the label
  707. *------------------------------------------*/
  708. void set_label(int l,int pos, const char* script_pos_cur)
  709. {
  710. int i;
  711. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM || str_data[l].type==C_FUNC)
  712. { //Prevent overwriting constants values, parameters and built-in functions [Skotlex]
  713. disp_error_message("set_label: invalid label name",script_pos_cur);
  714. return;
  715. }
  716. if(str_data[l].label!=-1){
  717. disp_error_message("set_label: dup label ",script_pos_cur);
  718. return;
  719. }
  720. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  721. str_data[l].label=pos;
  722. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  723. int next=GETVALUE(script_buf,i);
  724. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  725. SETVALUE(script_buf,i,pos);
  726. i=next;
  727. }
  728. }
  729. /// Skips spaces and/or comments.
  730. const char* skip_space(const char* p)
  731. {
  732. if( p == NULL )
  733. return NULL;
  734. for(;;)
  735. {
  736. while( ISSPACE(*p) )
  737. ++p;
  738. if( *p == '/' && p[1] == '/' )
  739. {// line comment
  740. while(*p && *p!='\n')
  741. ++p;
  742. }
  743. else if( *p == '/' && p[1] == '*' )
  744. {// block comment
  745. p += 2;
  746. for(;;)
  747. {
  748. if( *p == '\0' ) {
  749. disp_warning_message("script:script->skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  750. return p;
  751. }
  752. if( *p == '*' && p[1] == '/' )
  753. {// end of block comment
  754. p += 2;
  755. break;
  756. }
  757. ++p;
  758. }
  759. }
  760. else
  761. break;
  762. }
  763. return p;
  764. }
  765. /// Skips a word.
  766. /// A word consists of undercores and/or alphanumeric characters,
  767. /// and valid variable prefixes/postfixes.
  768. static const char* skip_word(const char* p)
  769. {
  770. // prefix
  771. switch( *p ) {
  772. case '@':// temporary char variable
  773. ++p; break;
  774. case '#':// account variable
  775. p += ( p[1] == '#' ? 2 : 1 ); break;
  776. case '\'':// instance variable
  777. ++p; break;
  778. case '.':// npc variable
  779. p += ( p[1] == '@' ? 2 : 1 ); break;
  780. case '$':// global variable
  781. p += ( p[1] == '@' ? 2 : 1 ); break;
  782. }
  783. while( ISALNUM(*p) || *p == '_' )
  784. ++p;
  785. // postfix
  786. if( *p == '$' )// string
  787. p++;
  788. return p;
  789. }
  790. /// Adds a word to str_data.
  791. /// @see skip_word
  792. /// @see add_str
  793. static int add_word(const char* p)
  794. {
  795. char* word;
  796. int len;
  797. int i;
  798. // Check for a word
  799. len = skip_word(p) - p;
  800. if( len == 0 )
  801. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alphanumeric characters, and valid variable prefixes/postfixes.", p);
  802. // Duplicate the word
  803. word = (char*)aMalloc(len+1);
  804. memcpy(word, p, len);
  805. word[len] = 0;
  806. // add the word
  807. i = add_str(word);
  808. aFree(word);
  809. return i;
  810. }
  811. /// Parses a function call.
  812. /// The argument list can have parenthesis or not.
  813. /// The number of arguments is checked.
  814. static
  815. const char* parse_callfunc(const char* p, int require_paren, int is_custom)
  816. {
  817. const char* p2;
  818. const char* arg=NULL;
  819. int func;
  820. func = add_word(p);
  821. if( str_data[func].type == C_FUNC ){
  822. // buildin function
  823. add_scriptl(func);
  824. add_scriptc(C_ARG);
  825. arg = buildin_func[str_data[func].val].arg;
  826. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  827. // script defined function
  828. add_scriptl(buildin_callsub_ref);
  829. add_scriptc(C_ARG);
  830. add_scriptl(func);
  831. arg = buildin_func[str_data[buildin_callsub_ref].val].arg;
  832. if( *arg == 0 )
  833. disp_error_message("parse_callfunc: callsub has no arguments, please review its definition",p);
  834. if( *arg != '*' )
  835. ++arg; // count func as argument
  836. } else {
  837. #ifdef SCRIPT_CALLFUNC_CHECK
  838. const char* name = get_str(func);
  839. if( !is_custom && strdb_get(userfunc_db, name) == NULL ) {
  840. #endif
  841. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  842. #ifdef SCRIPT_CALLFUNC_CHECK
  843. } else {;
  844. add_scriptl(buildin_callfunc_ref);
  845. add_scriptc(C_ARG);
  846. add_scriptc(C_STR);
  847. while( *name ) add_scriptb(*name ++);
  848. add_scriptb(0);
  849. arg = buildin_func[str_data[buildin_callfunc_ref].val].arg;
  850. if( *arg != '*' ) ++ arg;
  851. }
  852. #endif
  853. }
  854. p = skip_word(p);
  855. p = skip_space(p);
  856. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  857. syntax.curly[syntax.curly_count].count = 0;
  858. if( *p == ';' )
  859. {// <func name> ';'
  860. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  861. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  862. {// <func name> '(' ')'
  863. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  864. p = p2;
  865. /*
  866. } else if( 0 && require_paren && *p != '(' )
  867. {// <func name>
  868. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  869. */
  870. } else
  871. {// <func name> <arg list>
  872. if( require_paren ){
  873. if( *p != '(' )
  874. disp_error_message("need '('",p);
  875. ++p; // skip '('
  876. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  877. } else if( *p == '(' ){
  878. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  879. } else {
  880. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  881. }
  882. ++syntax.curly_count;
  883. while( *arg ) {
  884. p2=parse_subexpr(p,-1);
  885. if( p == p2 )
  886. break; // not an argument
  887. if( *arg != '*' )
  888. ++arg; // next argument
  889. p=skip_space(p2);
  890. if( *arg == 0 || *p != ',' )
  891. break; // no more arguments
  892. ++p; // skip comma
  893. }
  894. --syntax.curly_count;
  895. }
  896. if( *arg && *arg != '?' && *arg != '*' )
  897. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  898. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  899. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  900. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  901. if( *p != ')' )
  902. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  903. ++p;
  904. }
  905. add_scriptc(C_FUNC);
  906. return p;
  907. }
  908. /// Processes end of logical script line.
  909. /// @param first When true, only fix up scheduling data is initialized
  910. /// @param p Script position for error reporting in set_label
  911. static void parse_nextline(bool first, const char* p)
  912. {
  913. if( !first )
  914. {
  915. add_scriptc(C_EOL); // mark end of line for stack cleanup
  916. set_label(LABEL_NEXTLINE, script_pos, p); // fix up '-' labels
  917. }
  918. // initialize data for new '-' label fix up scheduling
  919. str_data[LABEL_NEXTLINE].type = C_NOP;
  920. str_data[LABEL_NEXTLINE].backpatch = -1;
  921. str_data[LABEL_NEXTLINE].label = -1;
  922. }
  923. /// Parse a variable assignment using the direct equals operator
  924. /// @param p script position where the function should run from
  925. /// @return NULL if not a variable assignment, the new position otherwise
  926. const char* parse_variable(const char* p) {
  927. int word;
  928. c_op type = C_NOP;
  929. const char *p2 = NULL;
  930. const char *var = p;
  931. // skip the variable where applicable
  932. p = skip_word(p);
  933. p = skip_space(p);
  934. if( p == NULL ) {// end of the line or invalid buffer
  935. return NULL;
  936. }
  937. if( *p == '[' ) {// array variable so process the array as appropriate
  938. int i,j;
  939. for( p2 = p, i = 0, j = 1; p; ++ i ) {
  940. if( *p ++ == ']' && --(j) == 0 ) break;
  941. if( *p == '[' ) ++ j;
  942. }
  943. if( !(p = skip_space(p)) ) {// end of line or invalid characters remaining
  944. disp_error_message("Missing right expression or closing bracket for variable.", p);
  945. }
  946. }
  947. if( type == C_NOP &&
  948. !( ( p[0] == '=' && p[1] != '=' && (type = C_EQ) ) // =
  949. || ( p[0] == '+' && p[1] == '=' && (type = C_ADD) ) // +=
  950. || ( p[0] == '-' && p[1] == '=' && (type = C_SUB) ) // -=
  951. || ( p[0] == '^' && p[1] == '=' && (type = C_XOR) ) // ^=
  952. || ( p[0] == '|' && p[1] == '=' && (type = C_OR ) ) // |=
  953. || ( p[0] == '&' && p[1] == '=' && (type = C_AND) ) // &=
  954. || ( p[0] == '*' && p[1] == '=' && (type = C_MUL) ) // *=
  955. || ( p[0] == '/' && p[1] == '=' && (type = C_DIV) ) // /=
  956. || ( p[0] == '%' && p[1] == '=' && (type = C_MOD) ) // %=
  957. || ( p[0] == '~' && p[1] == '=' && (type = C_NOT) ) // ~=
  958. || ( p[0] == '+' && p[1] == '+' && (type = C_ADD_PP) ) // ++
  959. || ( p[0] == '-' && p[1] == '-' && (type = C_SUB_PP) ) // --
  960. || ( p[0] == '<' && p[1] == '<' && p[2] == '=' && (type = C_L_SHIFT) ) // <<=
  961. || ( p[0] == '>' && p[1] == '>' && p[2] == '=' && (type = C_R_SHIFT) ) // >>=
  962. ) )
  963. {// failed to find a matching operator combination so invalid
  964. return NULL;
  965. }
  966. switch( type ) {
  967. case C_EQ: {// incremental modifier
  968. p = skip_space( &p[1] );
  969. }
  970. break;
  971. case C_L_SHIFT:
  972. case C_R_SHIFT: {// left or right shift modifier
  973. p = skip_space( &p[3] );
  974. }
  975. break;
  976. default: {// normal incremental command
  977. p = skip_space( &p[2] );
  978. }
  979. }
  980. if( p == NULL ) {// end of line or invalid buffer
  981. return NULL;
  982. }
  983. // push the set function onto the stack
  984. add_scriptl(buildin_set_ref);
  985. add_scriptc(C_ARG);
  986. // always append parenthesis to avoid errors
  987. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  988. syntax.curly[syntax.curly_count].count = 0;
  989. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  990. // increment the total curly count for the position in the script
  991. ++ syntax.curly_count;
  992. // parse the variable currently being modified
  993. word = add_word(var);
  994. if( str_data[word].type == C_FUNC || str_data[word].type == C_USERFUNC || str_data[word].type == C_USERFUNC_POS )
  995. {// cannot assign a variable which exists as a function or label
  996. disp_error_message("Cannot modify a variable which has the same name as a function or label.", p);
  997. }
  998. if( p2 ) {// process the variable index
  999. const char* p3 = NULL;
  1000. // push the getelementofarray method into the stack
  1001. add_scriptl(buildin_getelementofarray_ref);
  1002. add_scriptc(C_ARG);
  1003. add_scriptl(word);
  1004. // process the sub-expression for this assignment
  1005. p3 = parse_subexpr(p2 + 1, 1);
  1006. p3 = skip_space(p3);
  1007. if( *p3 != ']' ) {// closing parenthesis is required for this script
  1008. disp_error_message("Missing closing ']' parenthesis for the variable assignment.", p3);
  1009. }
  1010. // push the closing function stack operator onto the stack
  1011. add_scriptc(C_FUNC);
  1012. p3 ++;
  1013. } else {// simply push the variable or value onto the stack
  1014. add_scriptl(word);
  1015. }
  1016. if( type != C_EQ )
  1017. add_scriptc(C_REF);
  1018. if( type == C_ADD_PP || type == C_SUB_PP ) {// incremental operator for the method
  1019. add_scripti(1);
  1020. add_scriptc(type == C_ADD_PP ? C_ADD : C_SUB);
  1021. } else {// process the value as an expression
  1022. p = parse_subexpr(p, -1);
  1023. if( type != C_EQ )
  1024. {// push the type of modifier onto the stack
  1025. add_scriptc(type);
  1026. }
  1027. }
  1028. // decrement the curly count for the position within the script
  1029. -- syntax.curly_count;
  1030. // close the script by appending the function operator
  1031. add_scriptc(C_FUNC);
  1032. // push the buffer from the method
  1033. return p;
  1034. }
  1035. /*
  1036. * Checks whether the gives string is a number literal
  1037. *
  1038. * Mainly necessary to differentiate between number literals and NPC name
  1039. * constants, since several of those start with a digit.
  1040. *
  1041. * All this does is to check if the string begins with an optional + or - sign,
  1042. * followed by a hexadecimal or decimal number literal literal and is NOT
  1043. * followed by a underscore or letter.
  1044. *
  1045. * @author : Hercules.ws
  1046. * @param p Pointer to the string to check
  1047. * @return Whether the string is a number literal
  1048. */
  1049. bool is_number(const char *p) {
  1050. const char *np;
  1051. if (!p)
  1052. return false;
  1053. if (*p == '-' || *p == '+')
  1054. p++;
  1055. np = p;
  1056. if (*p == '0' && p[1] == 'x') {
  1057. p+=2;
  1058. np = p;
  1059. // Hexadecimal
  1060. while (ISXDIGIT(*np))
  1061. np++;
  1062. } else {
  1063. // Decimal
  1064. while (ISDIGIT(*np))
  1065. np++;
  1066. }
  1067. if (p != np && *np != '_' && !ISALPHA(*np)) // At least one digit, and next isn't a letter or _
  1068. return true;
  1069. return false;
  1070. }
  1071. /*==========================================
  1072. * Analysis section
  1073. *------------------------------------------*/
  1074. const char* parse_simpleexpr(const char *p)
  1075. {
  1076. long long i;
  1077. p=skip_space(p);
  1078. if(*p==';' || *p==',')
  1079. disp_error_message("parse_simpleexpr: unexpected end of expression",p);
  1080. if(*p=='('){
  1081. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  1082. ++syntax.curly[i].count;
  1083. p=parse_subexpr(p+1,-1);
  1084. p=skip_space(p);
  1085. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  1086. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  1087. ){
  1088. if( *p == ',' ){
  1089. syntax.curly[i].flag = ARGLIST_PAREN;
  1090. return p;
  1091. } else
  1092. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  1093. }
  1094. if( *p != ')' )
  1095. disp_error_message("parse_simpleexpr: unmatched ')'",p);
  1096. ++p;
  1097. } else if(is_number(p)) {
  1098. char *np;
  1099. while(*p == '0' && ISDIGIT(p[1])) p++;
  1100. i=strtoll(p,&np,0);
  1101. if( i < INT_MIN ) {
  1102. i = INT_MIN;
  1103. disp_warning_message("parse_simpleexpr: underflow detected, capping value to INT_MIN",p);
  1104. } else if( i > INT_MAX ) {
  1105. i = INT_MAX;
  1106. disp_warning_message("parse_simpleexpr: overflow detected, capping value to INT_MAX",p);
  1107. }
  1108. add_scripti((int)i);
  1109. p=np;
  1110. } else if(*p=='"'){
  1111. add_scriptc(C_STR);
  1112. p++;
  1113. while( *p && *p != '"' ){
  1114. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  1115. {
  1116. char buf[8];
  1117. size_t len = skip_escaped_c(p) - p;
  1118. size_t n = sv_unescape_c(buf, p, len);
  1119. if( n != 1 )
  1120. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  1121. p += len;
  1122. add_scriptb(*buf);
  1123. continue;
  1124. }
  1125. else if( *p == '\n' )
  1126. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  1127. add_scriptb(*p++);
  1128. }
  1129. if(!*p)
  1130. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  1131. add_scriptb(0);
  1132. p++; //'"'
  1133. } else {
  1134. int l;
  1135. const char* pv;
  1136. // label , register , function etc
  1137. if(skip_word(p)==p)
  1138. disp_error_message("parse_simpleexpr: unexpected character",p);
  1139. l=add_word(p);
  1140. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  1141. return parse_callfunc(p,1,0);
  1142. #ifdef SCRIPT_CALLFUNC_CHECK
  1143. else {
  1144. const char* name = get_str(l);
  1145. if( strdb_get(userfunc_db,name) != NULL ) {
  1146. return parse_callfunc(p,1,1);
  1147. }
  1148. }
  1149. #endif
  1150. if( (pv = parse_variable(p)) )
  1151. {// successfully processed a variable assignment
  1152. return pv;
  1153. }
  1154. p=skip_word(p);
  1155. if( *p == '[' ){
  1156. // array(name[i] => getelementofarray(name,i) )
  1157. add_scriptl(buildin_getelementofarray_ref);
  1158. add_scriptc(C_ARG);
  1159. add_scriptl(l);
  1160. p=parse_subexpr(p+1,-1);
  1161. p=skip_space(p);
  1162. if( *p != ']' )
  1163. disp_error_message("parse_simpleexpr: unmatched ']'",p);
  1164. ++p;
  1165. add_scriptc(C_FUNC);
  1166. }else
  1167. add_scriptl(l);
  1168. }
  1169. return p;
  1170. }
  1171. /*==========================================
  1172. * Analysis of the expression
  1173. *------------------------------------------*/
  1174. const char* parse_subexpr(const char* p,int limit)
  1175. {
  1176. int op,opl,len;
  1177. p=skip_space(p);
  1178. if( *p == '-' ){
  1179. const char* tmpp = skip_space(p+1);
  1180. if( *tmpp == ';' || *tmpp == ',' ){
  1181. add_scriptl(LABEL_NEXTLINE);
  1182. p++;
  1183. return p;
  1184. }
  1185. }
  1186. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  1187. p=parse_subexpr(p+1,10);
  1188. add_scriptc(op);
  1189. } else
  1190. p=parse_simpleexpr(p);
  1191. p=skip_space(p);
  1192. while((
  1193. (op=C_OP3,opl=0,len=1,*p=='?') ||
  1194. (op=C_ADD,opl=8,len=1,*p=='+') ||
  1195. (op=C_SUB,opl=8,len=1,*p=='-') ||
  1196. (op=C_MUL,opl=9,len=1,*p=='*') ||
  1197. (op=C_DIV,opl=9,len=1,*p=='/') ||
  1198. (op=C_MOD,opl=9,len=1,*p=='%') ||
  1199. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  1200. (op=C_AND,opl=6,len=1,*p=='&') ||
  1201. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  1202. (op=C_OR,opl=5,len=1,*p=='|') ||
  1203. (op=C_XOR,opl=4,len=1,*p=='^') ||
  1204. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  1205. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  1206. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  1207. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  1208. (op=C_GT,opl=3,len=1,*p=='>') ||
  1209. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  1210. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  1211. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  1212. p+=len;
  1213. if(op == C_OP3) {
  1214. p=parse_subexpr(p,-1);
  1215. p=skip_space(p);
  1216. if( *(p++) != ':')
  1217. disp_error_message("parse_subexpr: expected ':'", p-1);
  1218. p=parse_subexpr(p,-1);
  1219. } else {
  1220. p=parse_subexpr(p,opl);
  1221. }
  1222. add_scriptc(op);
  1223. p=skip_space(p);
  1224. }
  1225. return p; /* return first untreated operator */
  1226. }
  1227. /*==========================================
  1228. * Evaluation of the expression
  1229. *------------------------------------------*/
  1230. const char* parse_expr(const char *p)
  1231. {
  1232. switch(*p){
  1233. case ')': case ';': case ':': case '[': case ']':
  1234. case '}':
  1235. disp_error_message("parse_expr: unexpected character",p);
  1236. }
  1237. p=parse_subexpr(p,-1);
  1238. return p;
  1239. }
  1240. /*==========================================
  1241. * Analysis of the line
  1242. *------------------------------------------*/
  1243. const char* parse_line(const char* p)
  1244. {
  1245. const char* p2;
  1246. p=skip_space(p);
  1247. if(*p==';') {
  1248. //Close decision for if(); for(); while();
  1249. p = parse_syntax_close(p + 1);
  1250. return p;
  1251. }
  1252. if(*p==')' && parse_syntax_for_flag)
  1253. return p+1;
  1254. p = skip_space(p);
  1255. if(p[0] == '{') {
  1256. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1257. syntax.curly[syntax.curly_count].count = -1;
  1258. syntax.curly[syntax.curly_count].index = -1;
  1259. syntax.curly_count++;
  1260. return p + 1;
  1261. } else if(p[0] == '}') {
  1262. return parse_curly_close(p);
  1263. }
  1264. // Syntax-related processing
  1265. p2 = parse_syntax(p);
  1266. if(p2 != NULL)
  1267. return p2;
  1268. // attempt to process a variable assignment
  1269. p2 = parse_variable(p);
  1270. if( p2 != NULL )
  1271. {// variable assignment processed so leave the method
  1272. return parse_syntax_close(p2 + 1);
  1273. }
  1274. p = parse_callfunc(p,0,0);
  1275. p = skip_space(p);
  1276. if(parse_syntax_for_flag) {
  1277. if( *p != ')' )
  1278. disp_error_message("parse_line: expected ')'",p);
  1279. } else {
  1280. if( *p != ';' )
  1281. disp_error_message("parse_line: expected ';'",p);
  1282. }
  1283. //Binding decision for if(), for(), while()
  1284. p = parse_syntax_close(p+1);
  1285. return p;
  1286. }
  1287. // { ... } Closing process
  1288. const char* parse_curly_close(const char* p)
  1289. {
  1290. if(syntax.curly_count <= 0) {
  1291. disp_error_message("parse_curly_close: unexpected string",p);
  1292. return p + 1;
  1293. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1294. syntax.curly_count--;
  1295. //Close decision if, for , while
  1296. p = parse_syntax_close(p + 1);
  1297. return p;
  1298. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1299. //Closing switch()
  1300. int pos = syntax.curly_count-1;
  1301. char label[256];
  1302. int l;
  1303. // Remove temporary variables
  1304. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1305. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1306. parse_line(label);
  1307. syntax.curly_count--;
  1308. // Go to the end pointer unconditionally
  1309. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1310. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1311. parse_line(label);
  1312. syntax.curly_count--;
  1313. // You are here labeled
  1314. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1315. l=add_str(label);
  1316. set_label(l,script_pos, p);
  1317. if(syntax.curly[pos].flag) {
  1318. //Exists default
  1319. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1320. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1321. parse_line(label);
  1322. syntax.curly_count--;
  1323. }
  1324. // Label end
  1325. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1326. l=add_str(label);
  1327. set_label(l,script_pos, p);
  1328. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1329. syntax.curly_count--;
  1330. //Closing decision if, for , while
  1331. p = parse_syntax_close(p + 1);
  1332. return p;
  1333. } else {
  1334. disp_error_message("parse_curly_close: unexpected string",p);
  1335. return p + 1;
  1336. }
  1337. }
  1338. // Syntax-related processing
  1339. // break, case, continue, default, do, for, function,
  1340. // if, switch, while ? will handle this internally.
  1341. const char* parse_syntax(const char* p)
  1342. {
  1343. const char *p2 = skip_word(p);
  1344. switch(*p) {
  1345. case 'B':
  1346. case 'b':
  1347. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1348. // break Processing
  1349. char label[256];
  1350. int pos = syntax.curly_count - 1;
  1351. while(pos >= 0) {
  1352. if(syntax.curly[pos].type == TYPE_DO) {
  1353. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1354. break;
  1355. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1356. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1357. break;
  1358. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1359. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1360. break;
  1361. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1362. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1363. break;
  1364. }
  1365. pos--;
  1366. }
  1367. if(pos < 0) {
  1368. disp_error_message("parse_syntax: unexpected 'break'",p);
  1369. } else {
  1370. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1371. parse_line(label);
  1372. syntax.curly_count--;
  1373. }
  1374. p = skip_space(p2);
  1375. if(*p != ';')
  1376. disp_error_message("parse_syntax: expected ';'",p);
  1377. // Closing decision if, for , while
  1378. p = parse_syntax_close(p + 1);
  1379. return p;
  1380. }
  1381. break;
  1382. case 'c':
  1383. case 'C':
  1384. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1385. //Processing case
  1386. int pos = syntax.curly_count-1;
  1387. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1388. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1389. return p+1;
  1390. } else {
  1391. char label[256];
  1392. int l,v;
  1393. char *np;
  1394. if(syntax.curly[pos].count != 1) {
  1395. //Jump for FALLTHRU
  1396. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1397. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1398. parse_line(label);
  1399. syntax.curly_count--;
  1400. // You are here labeled
  1401. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1402. l=add_str(label);
  1403. set_label(l,script_pos, p);
  1404. }
  1405. //Decision statement switch
  1406. p = skip_space(p2);
  1407. if(p == p2) {
  1408. disp_error_message("parse_syntax: expected a space ' '",p);
  1409. }
  1410. // check whether case label is integer or not
  1411. if(is_number(p)) {
  1412. //Numeric value
  1413. v = (int)strtol(p,&np,0);
  1414. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1415. p++;
  1416. p = skip_word(p);
  1417. if(np != p)
  1418. disp_error_message("parse_syntax: 'case' label is not an integer",np);
  1419. } else {
  1420. //Check for constants
  1421. p2 = skip_word(p);
  1422. v = (int)(size_t) (p2-p); // length of word at p2
  1423. memcpy(label,p,v);
  1424. label[v]='\0';
  1425. if( !script_get_constant(label, &v) )
  1426. disp_error_message("parse_syntax: 'case' label is not an integer",p);
  1427. p = skip_word(p);
  1428. }
  1429. p = skip_space(p);
  1430. if(*p != ':')
  1431. disp_error_message("parse_syntax: expect ':'",p);
  1432. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1433. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1434. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1435. // Bad I do not parse twice
  1436. p2 = parse_line(label);
  1437. parse_line(p2);
  1438. syntax.curly_count--;
  1439. if(syntax.curly[pos].count != 1) {
  1440. // Label after the completion of FALLTHRU
  1441. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1442. l=add_str(label);
  1443. set_label(l,script_pos,p);
  1444. }
  1445. // check duplication of case label [Rayce]
  1446. if(linkdb_search(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v)) != NULL)
  1447. disp_error_message("parse_syntax: dup 'case'",p);
  1448. linkdb_insert(&syntax.curly[pos].case_label, (void*)__64BPRTSIZE(v), (void*)1);
  1449. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1450. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1451. parse_line(label);
  1452. syntax.curly_count--;
  1453. syntax.curly[pos].count++;
  1454. }
  1455. return p + 1;
  1456. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1457. // Processing continue
  1458. char label[256];
  1459. int pos = syntax.curly_count - 1;
  1460. while(pos >= 0) {
  1461. if(syntax.curly[pos].type == TYPE_DO) {
  1462. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1463. syntax.curly[pos].flag = 1; //Flag put the link for continue
  1464. break;
  1465. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1466. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1467. break;
  1468. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1469. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1470. break;
  1471. }
  1472. pos--;
  1473. }
  1474. if(pos < 0) {
  1475. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1476. } else {
  1477. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1478. parse_line(label);
  1479. syntax.curly_count--;
  1480. }
  1481. p = skip_space(p2);
  1482. if(*p != ';')
  1483. disp_error_message("parse_syntax: expected ';'",p);
  1484. //Closing decision if, for , while
  1485. p = parse_syntax_close(p + 1);
  1486. return p;
  1487. }
  1488. break;
  1489. case 'd':
  1490. case 'D':
  1491. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1492. // Switch - default processing
  1493. int pos = syntax.curly_count-1;
  1494. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1495. disp_error_message("parse_syntax: unexpected 'default'",p);
  1496. } else if(syntax.curly[pos].flag) {
  1497. disp_error_message("parse_syntax: dup 'default'",p);
  1498. } else {
  1499. char label[256];
  1500. int l;
  1501. // Put the label location
  1502. p = skip_space(p2);
  1503. if(*p != ':') {
  1504. disp_error_message("parse_syntax: expected ':'",p);
  1505. }
  1506. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1507. l=add_str(label);
  1508. set_label(l,script_pos,p);
  1509. // Skip to the next link w/o condition
  1510. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1511. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1512. parse_line(label);
  1513. syntax.curly_count--;
  1514. // The default label
  1515. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1516. l=add_str(label);
  1517. set_label(l,script_pos,p);
  1518. syntax.curly[syntax.curly_count - 1].flag = 1;
  1519. syntax.curly[pos].count++;
  1520. }
  1521. return p + 1;
  1522. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1523. int l;
  1524. char label[256];
  1525. p=skip_space(p2);
  1526. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1527. syntax.curly[syntax.curly_count].count = 1;
  1528. syntax.curly[syntax.curly_count].index = syntax.index++;
  1529. syntax.curly[syntax.curly_count].flag = 0;
  1530. // Label of the (do) form here
  1531. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1532. l=add_str(label);
  1533. set_label(l,script_pos,p);
  1534. syntax.curly_count++;
  1535. return p;
  1536. }
  1537. break;
  1538. case 'f':
  1539. case 'F':
  1540. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1541. int l;
  1542. char label[256];
  1543. int pos = syntax.curly_count;
  1544. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1545. syntax.curly[syntax.curly_count].count = 1;
  1546. syntax.curly[syntax.curly_count].index = syntax.index++;
  1547. syntax.curly[syntax.curly_count].flag = 0;
  1548. syntax.curly_count++;
  1549. p=skip_space(p2);
  1550. if(*p != '(')
  1551. disp_error_message("parse_syntax: expected '('",p);
  1552. p++;
  1553. // Execute the initialization statement
  1554. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1555. p=parse_line(p);
  1556. syntax.curly_count--;
  1557. // Form the start of label decision
  1558. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1559. l=add_str(label);
  1560. set_label(l,script_pos,p);
  1561. p=skip_space(p);
  1562. if(*p == ';') {
  1563. // For (; Because the pattern of always true ;)
  1564. ;
  1565. } else {
  1566. // Skip to the end point if the condition is false
  1567. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1568. add_scriptl(add_str("jump_zero"));
  1569. add_scriptc(C_ARG);
  1570. p=parse_expr(p);
  1571. p=skip_space(p);
  1572. add_scriptl(add_str(label));
  1573. add_scriptc(C_FUNC);
  1574. }
  1575. if(*p != ';')
  1576. disp_error_message("parse_syntax: expected ';'",p);
  1577. p++;
  1578. // Skip to the beginning of the loop
  1579. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1580. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1581. parse_line(label);
  1582. syntax.curly_count--;
  1583. // Labels to form the next loop
  1584. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1585. l=add_str(label);
  1586. set_label(l,script_pos,p);
  1587. // Process the next time you enter the loop
  1588. // A ')' last for; flag to be treated as'
  1589. parse_syntax_for_flag = 1;
  1590. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1591. p=parse_line(p);
  1592. syntax.curly_count--;
  1593. parse_syntax_for_flag = 0;
  1594. // Skip to the determination process conditions
  1595. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1596. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1597. parse_line(label);
  1598. syntax.curly_count--;
  1599. // Loop start labeling
  1600. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1601. l=add_str(label);
  1602. set_label(l,script_pos,p);
  1603. return p;
  1604. }
  1605. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1606. {// internal script function
  1607. const char *func_name;
  1608. func_name = skip_space(p2);
  1609. p = skip_word(func_name);
  1610. if( p == func_name )
  1611. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1612. p2 = skip_space(p);
  1613. if( *p2 == ';' )
  1614. {// function <name> ;
  1615. // function declaration - just register the name
  1616. int l;
  1617. l = add_word(func_name);
  1618. if( str_data[l].type == C_NOP )// register only, if the name was not used by something else
  1619. str_data[l].type = C_USERFUNC;
  1620. else if( str_data[l].type == C_USERFUNC )
  1621. ; // already registered
  1622. else
  1623. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1624. // Close condition of if, for, while
  1625. p = parse_syntax_close(p2 + 1);
  1626. return p;
  1627. }
  1628. else if(*p2 == '{')
  1629. {// function <name> <line/block of code>
  1630. char label[256];
  1631. int l;
  1632. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1633. syntax.curly[syntax.curly_count].count = 1;
  1634. syntax.curly[syntax.curly_count].index = syntax.index++;
  1635. syntax.curly[syntax.curly_count].flag = 0;
  1636. ++syntax.curly_count;
  1637. // Jump over the function code
  1638. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1639. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1640. ++syntax.curly_count;
  1641. parse_line(label);
  1642. --syntax.curly_count;
  1643. // Set the position of the function (label)
  1644. l=add_word(func_name);
  1645. if( str_data[l].type == C_NOP || str_data[l].type == C_USERFUNC )// register only, if the name was not used by something else
  1646. {
  1647. str_data[l].type = C_USERFUNC;
  1648. set_label(l, script_pos, p);
  1649. if( parse_options&SCRIPT_USE_LABEL_DB )
  1650. strdb_iput(scriptlabel_db, get_str(l), script_pos);
  1651. }
  1652. else
  1653. disp_error_message("parse_syntax:function: function name is invalid", func_name);
  1654. return skip_space(p);
  1655. }
  1656. else
  1657. {
  1658. disp_error_message("expect ';' or '{' at function syntax",p);
  1659. }
  1660. }
  1661. break;
  1662. case 'i':
  1663. case 'I':
  1664. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1665. // If process
  1666. char label[256];
  1667. p=skip_space(p2);
  1668. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1669. disp_error_message("need '('",p);
  1670. }
  1671. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1672. syntax.curly[syntax.curly_count].count = 1;
  1673. syntax.curly[syntax.curly_count].index = syntax.index++;
  1674. syntax.curly[syntax.curly_count].flag = 0;
  1675. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1676. syntax.curly_count++;
  1677. add_scriptl(add_str("jump_zero"));
  1678. add_scriptc(C_ARG);
  1679. p=parse_expr(p);
  1680. p=skip_space(p);
  1681. add_scriptl(add_str(label));
  1682. add_scriptc(C_FUNC);
  1683. return p;
  1684. }
  1685. break;
  1686. case 's':
  1687. case 'S':
  1688. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1689. // Processing of switch ()
  1690. char label[256];
  1691. p=skip_space(p2);
  1692. if(*p != '(') {
  1693. disp_error_message("need '('",p);
  1694. }
  1695. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1696. syntax.curly[syntax.curly_count].count = 1;
  1697. syntax.curly[syntax.curly_count].index = syntax.index++;
  1698. syntax.curly[syntax.curly_count].flag = 0;
  1699. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1700. syntax.curly_count++;
  1701. add_scriptl(add_str("set"));
  1702. add_scriptc(C_ARG);
  1703. add_scriptl(add_str(label));
  1704. p=parse_expr(p);
  1705. p=skip_space(p);
  1706. if(*p != '{') {
  1707. disp_error_message("parse_syntax: expected '{'",p);
  1708. }
  1709. add_scriptc(C_FUNC);
  1710. return p + 1;
  1711. }
  1712. break;
  1713. case 'w':
  1714. case 'W':
  1715. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1716. int l;
  1717. char label[256];
  1718. p=skip_space(p2);
  1719. if(*p != '(') {
  1720. disp_error_message("need '('",p);
  1721. }
  1722. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1723. syntax.curly[syntax.curly_count].count = 1;
  1724. syntax.curly[syntax.curly_count].index = syntax.index++;
  1725. syntax.curly[syntax.curly_count].flag = 0;
  1726. // Form the start of label decision
  1727. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1728. l=add_str(label);
  1729. set_label(l,script_pos,p);
  1730. // Skip to the end point if the condition is false
  1731. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1732. syntax.curly_count++;
  1733. add_scriptl(add_str("jump_zero"));
  1734. add_scriptc(C_ARG);
  1735. p=parse_expr(p);
  1736. p=skip_space(p);
  1737. add_scriptl(add_str(label));
  1738. add_scriptc(C_FUNC);
  1739. return p;
  1740. }
  1741. break;
  1742. }
  1743. return NULL;
  1744. }
  1745. const char* parse_syntax_close(const char *p) {
  1746. // If (...) for (...) hoge (); as to make sure closed closed once again
  1747. int flag;
  1748. do {
  1749. p = parse_syntax_close_sub(p,&flag);
  1750. } while(flag);
  1751. return p;
  1752. }
  1753. // Close judgment if, for, while, of do
  1754. // flag == 1 : closed
  1755. // flag == 0 : not closed
  1756. const char* parse_syntax_close_sub(const char* p,int* flag)
  1757. {
  1758. char label[256];
  1759. int pos = syntax.curly_count - 1;
  1760. int l;
  1761. *flag = 1;
  1762. if(syntax.curly_count <= 0) {
  1763. *flag = 0;
  1764. return p;
  1765. } else if(syntax.curly[pos].type == TYPE_IF) {
  1766. const char *bp = p;
  1767. const char *p2;
  1768. // if-block and else-block end is a new line
  1769. parse_nextline(false, p);
  1770. // Skip to the last location if
  1771. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1772. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1773. parse_line(label);
  1774. syntax.curly_count--;
  1775. // Put the label of the location
  1776. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1777. l=add_str(label);
  1778. set_label(l,script_pos,p);
  1779. syntax.curly[pos].count++;
  1780. p = skip_space(p);
  1781. p2 = skip_word(p);
  1782. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1783. // else or else - if
  1784. p = skip_space(p2);
  1785. p2 = skip_word(p);
  1786. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1787. // else - if
  1788. p=skip_space(p2);
  1789. if(*p != '(') {
  1790. disp_error_message("need '('",p);
  1791. }
  1792. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1793. add_scriptl(add_str("jump_zero"));
  1794. add_scriptc(C_ARG);
  1795. p=parse_expr(p);
  1796. p=skip_space(p);
  1797. add_scriptl(add_str(label));
  1798. add_scriptc(C_FUNC);
  1799. *flag = 0;
  1800. return p;
  1801. } else {
  1802. // else
  1803. if(!syntax.curly[pos].flag) {
  1804. syntax.curly[pos].flag = 1;
  1805. *flag = 0;
  1806. return p;
  1807. }
  1808. }
  1809. }
  1810. // Close if
  1811. syntax.curly_count--;
  1812. // Put the label of the final location
  1813. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1814. l=add_str(label);
  1815. set_label(l,script_pos,p);
  1816. if(syntax.curly[pos].flag == 1) {
  1817. // Because the position of the pointer is the same if not else for this
  1818. return bp;
  1819. }
  1820. return p;
  1821. } else if(syntax.curly[pos].type == TYPE_DO) {
  1822. int l2;
  1823. char label2[256];
  1824. const char *p2;
  1825. if(syntax.curly[pos].flag) {
  1826. // (Come here continue) to form the label here
  1827. sprintf(label2,"__DO%x_NXT",syntax.curly[pos].index);
  1828. l2=add_str(label2);
  1829. set_label(l2,script_pos,p);
  1830. }
  1831. // Skip to the end point if the condition is false
  1832. p = skip_space(p);
  1833. p2 = skip_word(p);
  1834. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1835. disp_error_message("parse_syntax: expected 'while'",p);
  1836. p = skip_space(p2);
  1837. if(*p != '(') {
  1838. disp_error_message("need '('",p);
  1839. }
  1840. // do-block end is a new line
  1841. parse_nextline(false, p);
  1842. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1843. add_scriptl(add_str("jump_zero"));
  1844. add_scriptc(C_ARG);
  1845. p=parse_expr(p);
  1846. p=skip_space(p);
  1847. add_scriptl(add_str(label2));
  1848. add_scriptc(C_FUNC);
  1849. // Skip to the starting point
  1850. sprintf(label2,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1851. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1852. parse_line(label2);
  1853. syntax.curly_count--;
  1854. // Form label of the end point conditions
  1855. sprintf(label2,"__DO%x_FIN",syntax.curly[pos].index);
  1856. l2=add_str(label2);
  1857. set_label(l2,script_pos,p);
  1858. p = skip_space(p);
  1859. if(*p != ';') {
  1860. disp_error_message("parse_syntax: expected ';'",p);
  1861. return p+1;
  1862. }
  1863. p++;
  1864. syntax.curly_count--;
  1865. return p;
  1866. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1867. // for-block end is a new line
  1868. parse_nextline(false, p);
  1869. // Skip to the next loop
  1870. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1871. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1872. parse_line(label);
  1873. syntax.curly_count--;
  1874. // End for labeling
  1875. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1876. l=add_str(label);
  1877. set_label(l,script_pos,p);
  1878. syntax.curly_count--;
  1879. return p;
  1880. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1881. // while-block end is a new line
  1882. parse_nextline(false, p);
  1883. // Skip to the decision while
  1884. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1885. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1886. parse_line(label);
  1887. syntax.curly_count--;
  1888. // End while labeling
  1889. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1890. l=add_str(label);
  1891. set_label(l,script_pos,p);
  1892. syntax.curly_count--;
  1893. return p;
  1894. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1895. int pos2 = syntax.curly_count-1;
  1896. char label2[256];
  1897. int l2;
  1898. // Back
  1899. sprintf(label2,"return;");
  1900. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1901. parse_line(label2);
  1902. syntax.curly_count--;
  1903. // Put the label of the location
  1904. sprintf(label2,"__FN%x_FIN",syntax.curly[pos2].index);
  1905. l2=add_str(label2);
  1906. set_label(l2,script_pos,p);
  1907. syntax.curly_count--;
  1908. return p;
  1909. } else {
  1910. *flag = 0;
  1911. return p;
  1912. }
  1913. }
  1914. /*==========================================
  1915. * Added built-in functions
  1916. *------------------------------------------*/
  1917. static void add_buildin_func(void)
  1918. {
  1919. int i;
  1920. for( i = 0; buildin_func[i].func; i++ )
  1921. {
  1922. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1923. // 'v' - value (either string or int or reference)
  1924. // 's' - string
  1925. // 'i' - int
  1926. // 'r' - reference (of a variable)
  1927. // 'l' - label
  1928. // '?' - one optional parameter
  1929. // '*' - unknown number of optional parameters
  1930. const char* p = buildin_func[i].arg;
  1931. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1932. while( *p == '?' ) ++p;
  1933. if( *p == '*' ) ++p;
  1934. if( *p != 0){
  1935. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1936. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1937. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1938. } else {
  1939. int n = add_str(buildin_func[i].name);
  1940. str_data[n].type = C_FUNC;
  1941. str_data[n].val = i;
  1942. str_data[n].func = buildin_func[i].func;
  1943. if (!strcmp(buildin_func[i].name, "set")) buildin_set_ref = n;
  1944. else if (!strcmp(buildin_func[i].name, "callsub")) buildin_callsub_ref = n;
  1945. else if (!strcmp(buildin_func[i].name, "callfunc")) buildin_callfunc_ref = n;
  1946. else if( !strcmp(buildin_func[i].name, "getelementofarray") ) buildin_getelementofarray_ref = n;
  1947. }
  1948. }
  1949. }
  1950. /// Retrieves the value of a constant.
  1951. bool script_get_constant(const char* name, int* value)
  1952. {
  1953. int n = search_str(name);
  1954. if( n == -1 || str_data[n].type != C_INT )
  1955. {// not found or not a constant
  1956. return false;
  1957. }
  1958. value[0] = str_data[n].val;
  1959. return true;
  1960. }
  1961. /// Creates new constant or parameter with given value.
  1962. void script_set_constant(const char* name, int value, bool isparameter)
  1963. {
  1964. int n = add_str(name);
  1965. if( str_data[n].type == C_NOP )
  1966. {// new
  1967. str_data[n].type = isparameter ? C_PARAM : C_INT;
  1968. str_data[n].val = value;
  1969. }
  1970. else if( str_data[n].type == C_PARAM || str_data[n].type == C_INT )
  1971. {// existing parameter or constant
  1972. ShowError("script_set_constant: Attempted to overwrite existing %s '%s' (old value=%d, new value=%d).\n", ( str_data[n].type == C_PARAM ) ? "parameter" : "constant", name, str_data[n].val, value);
  1973. }
  1974. else
  1975. {// existing name
  1976. ShowError("script_set_constant: Invalid name for %s '%s' (already defined as %s).\n", isparameter ? "parameter" : "constant", name, script_op2name(str_data[n].type));
  1977. }
  1978. }
  1979. /*==========================================
  1980. * Reading constant databases
  1981. * const.txt
  1982. *------------------------------------------*/
  1983. static void read_constdb(void)
  1984. {
  1985. FILE *fp;
  1986. char line[1024],name[1024],val[1024];
  1987. int type;
  1988. int entries=0, skipped=0, linenum=0;
  1989. sprintf(line, "%s/const.txt", db_path);
  1990. fp=fopen(line, "r");
  1991. if(fp==NULL){
  1992. ShowError("can't read %s\n", line);
  1993. return ;
  1994. }
  1995. while(fgets(line, sizeof(line), fp))
  1996. {
  1997. linenum++;
  1998. if( line[0] == '\0' || line[0] == '\n' || line[0] == '\r') //ignore empty line
  1999. continue;
  2000. if(line[0]=='/' && line[1]=='/') //ignore commented line
  2001. continue;
  2002. type=0;
  2003. if(sscanf(line,"%1023[A-Za-z0-9/_],%1023[A-Za-z0-9/_-],%11d",name,val,&type)>=2 ||
  2004. sscanf(line,"%1023[A-Za-z0-9/_] %1023[A-Za-z0-9/_-] %11d",name,val,&type)>=2){
  2005. entries++;
  2006. script_set_constant(name, (int)strtol(val, NULL, 0), (bool)type);
  2007. }
  2008. else {
  2009. skipped++;
  2010. ShowWarning("Skipping line '"CL_WHITE"%d"CL_RESET"', invalide constant definition\n",linenum);
  2011. }
  2012. }
  2013. fclose(fp);
  2014. ShowStatus("Done reading '"CL_WHITE"%d"CL_RESET"' entries in '"CL_WHITE"%s/const.txt"CL_RESET"'.\n", entries, db_path);
  2015. if(skipped){
  2016. ShowWarning("Skipped '"CL_WHITE"%d"CL_RESET"', entries\n",skipped);
  2017. }
  2018. }
  2019. /**
  2020. * Sets source-end constants for NPC scripts to access.
  2021. **/
  2022. void script_hardcoded_constants(void) {
  2023. #include "script_constants.h"
  2024. }
  2025. /*==========================================
  2026. * Display emplacement line of script
  2027. *------------------------------------------*/
  2028. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  2029. {
  2030. int i;
  2031. if( p == NULL || !p[0] ) return NULL;
  2032. if( line < 0 )
  2033. StringBuf_Printf(buf, "*% 5d : ", -line);
  2034. else
  2035. StringBuf_Printf(buf, " % 5d : ", line);
  2036. for(i=0;p[i] && p[i] != '\n';i++){
  2037. if(p + i != mark)
  2038. StringBuf_Printf(buf, "%c", p[i]);
  2039. else
  2040. StringBuf_Printf(buf, "\'%c\'", p[i]);
  2041. }
  2042. StringBuf_AppendStr(buf, "\n");
  2043. return p+i+(p[i] == '\n' ? 1 : 0);
  2044. }
  2045. void script_errorwarning_sub(StringBuf *buf, const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2046. // Find the line where the error occurred
  2047. int j;
  2048. int line = start_line;
  2049. const char *p;
  2050. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  2051. for(p=src;p && *p;line++){
  2052. const char *lineend=strchr(p,'\n');
  2053. if(lineend==NULL || error_pos_cur<lineend){
  2054. break;
  2055. }
  2056. for( j = 0; j < 4; j++ ) {
  2057. linestart[j] = linestart[j+1];
  2058. }
  2059. linestart[4] = p;
  2060. p=lineend+1;
  2061. }
  2062. StringBuf_Printf(buf, "script error on %s line %d\n", file, line);
  2063. StringBuf_Printf(buf, " %s\n", error_msg_cur);
  2064. for(j = 0; j < 5; j++ ) {
  2065. script_print_line(buf, linestart[j], NULL, line + j - 5);
  2066. }
  2067. p = script_print_line(buf, p, error_pos_cur, -line);
  2068. for(j = 0; j < 5; j++) {
  2069. p = script_print_line(buf, p, NULL, line + j + 1 );
  2070. }
  2071. }
  2072. void script_error(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2073. StringBuf buf;
  2074. StringBuf_Init(&buf);
  2075. StringBuf_AppendStr(&buf, "\a\n");
  2076. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2077. ShowError("%s", StringBuf_Value(&buf));
  2078. StringBuf_Destroy(&buf);
  2079. }
  2080. void script_warning(const char* src, const char* file, int start_line, const char* error_msg_cur, const char* error_pos_cur) {
  2081. StringBuf buf;
  2082. StringBuf_Init(&buf);
  2083. script_errorwarning_sub(&buf, src, file, start_line, error_msg_cur, error_pos_cur);
  2084. ShowWarning("%s", StringBuf_Value(&buf));
  2085. StringBuf_Destroy(&buf);
  2086. }
  2087. /*==========================================
  2088. * Analysis of the script
  2089. *------------------------------------------*/
  2090. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  2091. {
  2092. const char *p,*tmpp;
  2093. int i;
  2094. struct script_code* code = NULL;
  2095. static int first=1;
  2096. char end;
  2097. bool unresolved_names = false;
  2098. parser_current_src = src;
  2099. parser_current_file = file;
  2100. parser_current_line = line;
  2101. if( src == NULL )
  2102. return NULL;// empty script
  2103. memset(&syntax,0,sizeof(syntax));
  2104. if(first){
  2105. add_buildin_func();
  2106. read_constdb();
  2107. script_hardcoded_constants();
  2108. first=0;
  2109. }
  2110. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  2111. script_pos=0;
  2112. script_size=SCRIPT_BLOCK_SIZE;
  2113. parse_nextline(true, NULL);
  2114. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  2115. if( options&SCRIPT_USE_LABEL_DB )
  2116. db_clear(scriptlabel_db);
  2117. parse_options = options;
  2118. if( setjmp( error_jump ) != 0 ) {
  2119. //Restore program state when script has problems. [from jA]
  2120. int j;
  2121. const int size = ARRAYLENGTH(syntax.curly);
  2122. if( error_report )
  2123. script_error(src,file,line,error_msg,error_pos);
  2124. aFree( error_msg );
  2125. aFree( script_buf );
  2126. script_pos = 0;
  2127. script_size = 0;
  2128. script_buf = NULL;
  2129. for(j=LABEL_START;j<str_num;j++)
  2130. if(str_data[j].type == C_NOP) str_data[j].type = C_NAME;
  2131. for(j=0; j<size; j++)
  2132. linkdb_final(&syntax.curly[j].case_label);
  2133. return NULL;
  2134. }
  2135. parse_syntax_for_flag=0;
  2136. p=src;
  2137. p=skip_space(p);
  2138. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  2139. {// does not require brackets around the script
  2140. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2141. {// empty script and can return NULL
  2142. aFree( script_buf );
  2143. script_pos = 0;
  2144. script_size = 0;
  2145. script_buf = NULL;
  2146. return NULL;
  2147. }
  2148. end = '\0';
  2149. }
  2150. else
  2151. {// requires brackets around the script
  2152. if( *p != '{' )
  2153. disp_error_message("not found '{'",p);
  2154. p = skip_space(p+1);
  2155. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  2156. {// empty script and can return NULL
  2157. aFree( script_buf );
  2158. script_pos = 0;
  2159. script_size = 0;
  2160. script_buf = NULL;
  2161. return NULL;
  2162. }
  2163. end = '}';
  2164. }
  2165. // clear references of labels, variables and internal functions
  2166. for(i=LABEL_START;i<str_num;i++){
  2167. if(
  2168. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  2169. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  2170. ){
  2171. str_data[i].type=C_NOP;
  2172. str_data[i].backpatch=-1;
  2173. str_data[i].label=-1;
  2174. }
  2175. }
  2176. while( syntax.curly_count != 0 || *p != end )
  2177. {
  2178. if( *p == '\0' )
  2179. disp_error_message("unexpected end of script",p);
  2180. // Special handling only label
  2181. tmpp=skip_space(skip_word(p));
  2182. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  2183. i=add_word(p);
  2184. set_label(i,script_pos,p);
  2185. if( parse_options&SCRIPT_USE_LABEL_DB )
  2186. strdb_iput(scriptlabel_db, get_str(i), script_pos);
  2187. p=tmpp+1;
  2188. p=skip_space(p);
  2189. continue;
  2190. }
  2191. // All other lumped
  2192. p=parse_line(p);
  2193. p=skip_space(p);
  2194. parse_nextline(false, p);
  2195. }
  2196. add_scriptc(C_NOP);
  2197. // trim code to size
  2198. script_size = script_pos;
  2199. RECREATE(script_buf,unsigned char,script_pos);
  2200. // default unknown references to variables
  2201. for(i=LABEL_START;i<str_num;i++){
  2202. if(str_data[i].type==C_NOP){
  2203. int j;
  2204. str_data[i].type=C_NAME;
  2205. str_data[i].label=i;
  2206. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  2207. int next=GETVALUE(script_buf,j);
  2208. SETVALUE(script_buf,j,i);
  2209. j=next;
  2210. }
  2211. }
  2212. else if( str_data[i].type == C_USERFUNC )
  2213. {// 'function name;' without follow-up code
  2214. ShowError("parse_script: function '%s' declared but not defined.\n", str_buf+str_data[i].str);
  2215. unresolved_names = true;
  2216. }
  2217. }
  2218. if( unresolved_names )
  2219. {
  2220. disp_error_message("parse_script: unresolved function references", p);
  2221. }
  2222. #ifdef DEBUG_DISP
  2223. for(i=0;i<script_pos;i++){
  2224. if((i&15)==0) ShowMessage("%04x : ",i);
  2225. ShowMessage("%02x ",script_buf[i]);
  2226. if((i&15)==15) ShowMessage("\n");
  2227. }
  2228. ShowMessage("\n");
  2229. #endif
  2230. #ifdef DEBUG_DISASM
  2231. {
  2232. int i = 0,j;
  2233. while(i < script_pos) {
  2234. c_op op = get_com(script_buf,&i);
  2235. ShowMessage("%06x %s", i, script_op2name(op));
  2236. j = i;
  2237. switch(op) {
  2238. case C_INT:
  2239. ShowMessage(" %d", get_num(script_buf,&i));
  2240. break;
  2241. case C_POS:
  2242. ShowMessage(" 0x%06x", *(int*)(script_buf+i)&0xffffff);
  2243. i += 3;
  2244. break;
  2245. case C_NAME:
  2246. j = (*(int*)(script_buf+i)&0xffffff);
  2247. ShowMessage(" %s", ( j == 0xffffff ) ? "?? unknown ??" : get_str(j));
  2248. i += 3;
  2249. break;
  2250. case C_STR:
  2251. j = strlen(script_buf + i);
  2252. ShowMessage(" %s", script_buf + i);
  2253. i += j+1;
  2254. break;
  2255. }
  2256. ShowMessage(CL_CLL"\n");
  2257. }
  2258. }
  2259. #endif
  2260. CREATE(code,struct script_code,1);
  2261. code->script_buf = script_buf;
  2262. code->script_size = script_size;
  2263. code->local.vars = NULL;
  2264. code->local.arrays = NULL;
  2265. return code;
  2266. }
  2267. /// Returns the player attached to this script, identified by the rid.
  2268. /// If there is no player attached, the script is terminated.
  2269. TBL_PC *script_rid2sd(struct script_state *st)
  2270. {
  2271. TBL_PC *sd=map_id2sd(st->rid);
  2272. if(!sd){
  2273. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  2274. script_reportfunc(st);
  2275. script_reportsrc(st);
  2276. st->state = END;
  2277. }
  2278. return sd;
  2279. }
  2280. /**
  2281. * Dereferences a variable/constant, replacing it with a copy of the value.
  2282. * @param st Script state
  2283. * @param data Variable/constant
  2284. * @param sd If NULL, will try to use sd from st->rid (for player's variables)
  2285. */
  2286. void get_val_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2287. {
  2288. const char* name;
  2289. char prefix;
  2290. char postfix;
  2291. if( !data_isreference(data) )
  2292. return;// not a variable/constant
  2293. name = reference_getname(data);
  2294. prefix = name[0];
  2295. postfix = name[strlen(name) - 1];
  2296. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  2297. if( !reference_toconstant(data) && not_server_variable(prefix) ) {
  2298. if (sd == NULL)
  2299. sd = script_rid2sd(st);
  2300. if( sd == NULL ) {// needs player attached
  2301. if( postfix == '$' ) {// string variable
  2302. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  2303. data->type = C_CONSTSTR;
  2304. data->u.str = "";
  2305. } else {// integer variable
  2306. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  2307. data->type = C_INT;
  2308. data->u.num = 0;
  2309. }
  2310. return;
  2311. }
  2312. }
  2313. if( postfix == '$' ) {// string variable
  2314. switch( prefix ) {
  2315. case '@':
  2316. data->u.str = pc_readregstr(sd, data->u.num);
  2317. break;
  2318. case '$':
  2319. data->u.str = mapreg_readregstr(data->u.num);
  2320. break;
  2321. case '#':
  2322. if( name[1] == '#' )
  2323. data->u.str = pc_readaccountreg2str(sd, data->u.num);// global
  2324. else
  2325. data->u.str = pc_readaccountregstr(sd, data->u.num);// local
  2326. break;
  2327. case '.':
  2328. {
  2329. struct DBMap* n = data->ref ?
  2330. data->ref->vars : name[1] == '@' ?
  2331. st->stack->scope.vars : // instance/scope variable
  2332. st->script->local.vars; // npc variable
  2333. if( n )
  2334. data->u.str = (char*)i64db_get(n,reference_getuid(data));
  2335. else
  2336. data->u.str = NULL;
  2337. }
  2338. break;
  2339. case '\'':
  2340. {
  2341. int instance_id = script_instancegetid(st);
  2342. if( instance_id )
  2343. data->u.str = (char*)i64db_get(instance_data[instance_id].regs.vars,reference_getuid(data));
  2344. else {
  2345. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to \"\"\n", name);
  2346. data->u.str = NULL;
  2347. }
  2348. }
  2349. break;
  2350. default:
  2351. data->u.str = pc_readglobalreg_str(sd, data->u.num);
  2352. break;
  2353. }
  2354. if( data->u.str == NULL || data->u.str[0] == '\0' ) {// empty string
  2355. data->type = C_CONSTSTR;
  2356. data->u.str = "";
  2357. } else {// duplicate string
  2358. data->type = C_STR;
  2359. data->u.str = aStrdup(data->u.str);
  2360. }
  2361. } else {// integer variable
  2362. data->type = C_INT;
  2363. if( reference_toconstant(data) ) {
  2364. data->u.num = reference_getconstant(data);
  2365. } else if( reference_toparam(data) ) {
  2366. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2367. } else
  2368. switch( prefix ) {
  2369. case '@':
  2370. data->u.num = pc_readreg(sd, data->u.num);
  2371. break;
  2372. case '$':
  2373. data->u.num = mapreg_readreg(data->u.num);
  2374. break;
  2375. case '#':
  2376. if( name[1] == '#' )
  2377. data->u.num = pc_readaccountreg2(sd, data->u.num);// global
  2378. else
  2379. data->u.num = pc_readaccountreg(sd, data->u.num);// local
  2380. break;
  2381. case '.':
  2382. {
  2383. struct DBMap* n = data->ref ?
  2384. data->ref->vars : name[1] == '@' ?
  2385. st->stack->scope.vars : // instance/scope variable
  2386. st->script->local.vars; // npc variable
  2387. if( n )
  2388. data->u.num = (int)i64db_iget(n,reference_getuid(data));
  2389. else
  2390. data->u.num = 0;
  2391. }
  2392. break;
  2393. case '\'':
  2394. {
  2395. int instance_id = script_instancegetid(st);
  2396. if( instance_id )
  2397. data->u.num = (int)i64db_iget(instance_data[instance_id].regs.vars,reference_getuid(data));
  2398. else {
  2399. ShowWarning("script:get_val: cannot access instance variable '%s', defaulting to 0\n", name);
  2400. data->u.num = 0;
  2401. }
  2402. }
  2403. break;
  2404. default:
  2405. data->u.num = pc_readglobalreg(sd, data->u.num);
  2406. break;
  2407. }
  2408. }
  2409. data->ref = NULL;
  2410. return;
  2411. }
  2412. void get_val(struct script_state* st, struct script_data* data)
  2413. {
  2414. get_val_(st,data,NULL);
  2415. }
  2416. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db* ref);
  2417. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2418. /// The value is left in the top of the stack and needs to be removed manually.
  2419. /// @param st[in]: script state.
  2420. /// @param uid[in]: reference identifier.
  2421. /// @param ref[in]: the container to look up the reference into.
  2422. /// @return: the retrieved value of the reference.
  2423. void* get_val2(struct script_state* st, int64 uid, struct reg_db *ref)
  2424. {
  2425. struct script_data* data;
  2426. push_val2(st->stack, C_NAME, uid, ref);
  2427. data = script_getdatatop(st, -1);
  2428. get_val(st, data);
  2429. //! TODO: Support data->u.num as int64 instead cast it to int? (in get_val, it was casted to int).
  2430. return (data->type == C_INT ? (void*)__64BPRTSIZE((int)data->u.num) : (void*)__64BPRTSIZE(data->u.str));
  2431. }
  2432. /**
  2433. * Because, currently, array members with key 0 are indifferenciable from normal variables, we should ensure its actually in
  2434. * Will be gone as soon as undefined var feature is implemented
  2435. **/
  2436. void script_array_ensure_zero(struct script_state *st, struct map_session_data *sd, int64 uid, struct reg_db *ref)
  2437. {
  2438. const char *name = get_str(script_getvarid(uid));
  2439. // is here st can be null pointer and st->rid is wrong?
  2440. struct reg_db *src = script_array_src(st, sd ? sd : st->rid ? map_id2sd(st->rid) : NULL, name, ref);
  2441. bool insert = false;
  2442. if (sd && !st) {
  2443. // when sd comes, st isn't available
  2444. insert = true;
  2445. } else {
  2446. if( is_string_variable(name) ) {
  2447. char* str = (char*)get_val2(st, uid, ref);
  2448. if( str && *str )
  2449. insert = true;
  2450. script_removetop(st, -1, 0);
  2451. } else {
  2452. int32 num = (int32)__64BPRTSIZE(get_val2(st, uid, ref));
  2453. if( num )
  2454. insert = true;
  2455. script_removetop(st, -1, 0);
  2456. }
  2457. }
  2458. if (src && src->arrays) {
  2459. struct script_array *sa = idb_get(src->arrays, script_getvarid(uid));
  2460. if (sa) {
  2461. unsigned int i;
  2462. ARR_FIND(0, sa->size, i, sa->members[i] == 0);
  2463. if( i != sa->size ) {
  2464. if( !insert )
  2465. script_array_remove_member(src,sa,i);
  2466. return;
  2467. }
  2468. script_array_add_member(sa,0);
  2469. } else if (insert) {
  2470. script_array_update(src,reference_uid(script_getvarid(uid), 0),false);
  2471. }
  2472. }
  2473. }
  2474. /**
  2475. * Returns array size by ID
  2476. **/
  2477. unsigned int script_array_size(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2478. {
  2479. struct script_array *sa = NULL;
  2480. struct reg_db *src = script_array_src(st, sd, name, ref);
  2481. if (src && src->arrays)
  2482. sa = idb_get(src->arrays, search_str(name));
  2483. return sa ? sa->size : 0;
  2484. }
  2485. /**
  2486. * Returns array's highest key (for that awful getarraysize implementation that doesn't really gets the array size)
  2487. **/
  2488. unsigned int script_array_highest_key(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2489. {
  2490. struct script_array *sa = NULL;
  2491. struct reg_db *src = script_array_src(st, sd, name, ref);
  2492. if (src && src->arrays) {
  2493. int key = add_word(name);
  2494. script_array_ensure_zero(st,sd,reference_uid(key, 0), ref);
  2495. if( ( sa = idb_get(src->arrays, key) ) ) {
  2496. unsigned int i, highest_key = 0;
  2497. for(i = 0; i < sa->size; i++) {
  2498. if( sa->members[i] > highest_key )
  2499. highest_key = sa->members[i];
  2500. }
  2501. return sa->size ? highest_key + 1 : 0;
  2502. }
  2503. }
  2504. return SCRIPT_CMD_SUCCESS;
  2505. }
  2506. int script_free_array_db(DBKey key, DBData *data, va_list ap)
  2507. {
  2508. struct script_array *sa = db_data2ptr(data);
  2509. aFree(sa->members);
  2510. ers_free(array_ers, sa);
  2511. return SCRIPT_CMD_SUCCESS;
  2512. }
  2513. /**
  2514. * Clears script_array and removes it from script->array_db
  2515. **/
  2516. void script_array_delete(struct reg_db *src, struct script_array *sa)
  2517. {
  2518. aFree(sa->members);
  2519. idb_remove(src->arrays, sa->id);
  2520. ers_free(array_ers, sa);
  2521. }
  2522. /**
  2523. * Removes a member from a script_array list
  2524. *
  2525. * @param idx the index of the member in script_array struct list, not of the actual array member
  2526. **/
  2527. void script_array_remove_member(struct reg_db *src, struct script_array *sa, unsigned int idx)
  2528. {
  2529. unsigned int i, cursor;
  2530. // it's the only member left, no need to do anything other than delete the array data
  2531. if( sa->size == 1 ) {
  2532. script_array_delete(src,sa);
  2533. return;
  2534. }
  2535. sa->members[idx] = UINT_MAX;
  2536. for(i = 0, cursor = 0; i < sa->size; i++) {
  2537. if( sa->members[i] == UINT_MAX )
  2538. continue;
  2539. if( i != cursor )
  2540. sa->members[cursor] = sa->members[i];
  2541. cursor++;
  2542. }
  2543. sa->size = cursor;
  2544. }
  2545. /**
  2546. * Appends a new array index to the list in script_array
  2547. *
  2548. * @param idx the index of the array member being inserted
  2549. **/
  2550. void script_array_add_member(struct script_array *sa, unsigned int idx)
  2551. {
  2552. RECREATE(sa->members, unsigned int, ++sa->size);
  2553. sa->members[sa->size - 1] = idx;
  2554. }
  2555. /**
  2556. * Obtains the source of the array database for this type and scenario
  2557. * Initializes such database when not yet initialized.
  2558. **/
  2559. struct reg_db *script_array_src(struct script_state *st, struct map_session_data *sd, const char *name, struct reg_db *ref)
  2560. {
  2561. struct reg_db *src = NULL;
  2562. switch( name[0] ) {
  2563. // from player
  2564. default: // char reg
  2565. case '@': // temp char reg
  2566. case '#': // account reg
  2567. src = &sd->regs;
  2568. break;
  2569. case '$': // map reg
  2570. src = &regs;
  2571. break;
  2572. case '.': // npc/script
  2573. if (ref)
  2574. src = ref;
  2575. else
  2576. src = (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2577. break;
  2578. case '\'': // instance
  2579. {
  2580. int instance_id = script_instancegetid(st);
  2581. if( instance_id ) {
  2582. src = &instance_data[instance_id].regs;
  2583. }
  2584. break;
  2585. }
  2586. }
  2587. if( src ) {
  2588. if( !src->arrays )
  2589. src->arrays = idb_alloc(DB_OPT_BASE);
  2590. return src;
  2591. }
  2592. return NULL;
  2593. }
  2594. /**
  2595. * Processes a array member modification, and update data accordingly
  2596. *
  2597. * @param src[in,out] Variable source database. If the array database doesn't exist, it is created.
  2598. * @param num[in] Variable ID
  2599. * @param empty[in] Whether the modified member is empty (needs to be removed)
  2600. **/
  2601. void script_array_update(struct reg_db *src, int64 num, bool empty)
  2602. {
  2603. struct script_array *sa = NULL;
  2604. int id = script_getvarid(num);
  2605. unsigned int index = script_getvaridx(num);
  2606. if (!src->arrays) {
  2607. src->arrays = idb_alloc(DB_OPT_BASE);
  2608. } else {
  2609. sa = idb_get(src->arrays, id);
  2610. }
  2611. if( sa ) {
  2612. unsigned int i;
  2613. // search
  2614. for(i = 0; i < sa->size; i++) {
  2615. if( sa->members[i] == index )
  2616. break;
  2617. }
  2618. // if existent
  2619. if( i != sa->size ) {
  2620. // if empty, we gotta remove it
  2621. if( empty ) {
  2622. script_array_remove_member(src, sa, i);
  2623. }
  2624. } else if( !empty ) { /* new entry */
  2625. script_array_add_member(sa,index);
  2626. // we do nothing if its empty, no point in modifying array data for a new empty member
  2627. }
  2628. } else if ( !empty ) { // we only move to create if not empty
  2629. sa = ers_alloc(array_ers, struct script_array);
  2630. sa->id = id;
  2631. sa->members = NULL;
  2632. sa->size = 0;
  2633. script_array_add_member(sa,index);
  2634. idb_put(src->arrays, id, sa);
  2635. }
  2636. }
  2637. /**
  2638. * Stores the value of a script variable
  2639. *
  2640. * @param st: current script state.
  2641. * @param sd: current character data.
  2642. * @param num: variable identifier.
  2643. * @param name: variable name.
  2644. * @param value: new value.
  2645. * @param ref: variable container, in case of a npc/scope variable reference outside the current scope.
  2646. * @return: 0 failure, 1 success.
  2647. *
  2648. * TODO: return values are screwed up, have been for some time (reaad: years), e.g. some functions return 1 failure and success.
  2649. *------------------------------------------*/
  2650. int set_reg(struct script_state* st, TBL_PC* sd, int64 num, const char* name, const void* value, struct reg_db *ref)
  2651. {
  2652. char prefix = name[0];
  2653. if( is_string_variable(name) ) {// string variable
  2654. const char *str = (const char*)value;
  2655. switch (prefix) {
  2656. case '@':
  2657. pc_setregstr(sd, num, str);
  2658. return 1;
  2659. case '$':
  2660. return mapreg_setregstr(num, str);
  2661. case '#':
  2662. return (name[1] == '#') ?
  2663. pc_setaccountreg2str(sd, num, str) :
  2664. pc_setaccountregstr(sd, num, str);
  2665. case '.':
  2666. {
  2667. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2668. if( n ) {
  2669. if (str[0]) {
  2670. i64db_put(n->vars, num, aStrdup(str));
  2671. if( script_getvaridx(num) )
  2672. script_array_update(n, num, false);
  2673. } else {
  2674. i64db_remove(n->vars, num);
  2675. if( script_getvaridx(num) )
  2676. script_array_update(n, num, true);
  2677. }
  2678. }
  2679. }
  2680. return 1;
  2681. case '\'':
  2682. {
  2683. int instance_id = script_instancegetid(st);
  2684. if( instance_id ) {
  2685. if( str[0] ) {
  2686. i64db_put(instance_data[instance_id].regs.vars, num, aStrdup(str));
  2687. if( script_getvaridx(num) )
  2688. script_array_update(&instance_data[instance_id].regs, num, false);
  2689. } else {
  2690. i64db_remove(instance_data[instance_id].regs.vars, num);
  2691. if (script_getvaridx(num))
  2692. script_array_update(&instance_data[instance_id].regs, num, true);
  2693. }
  2694. } else {
  2695. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2696. script_reportsrc(st);
  2697. }
  2698. return 1;
  2699. }
  2700. default:
  2701. return pc_setglobalreg_str(sd, num, str);
  2702. }
  2703. } else {// integer variable
  2704. int val = (int)__64BPRTSIZE(value);
  2705. if(str_data[script_getvarid(num)].type == C_PARAM) {
  2706. if( pc_setparam(sd, str_data[script_getvarid(num)].val, val) == 0 ) {
  2707. if( st != NULL ) {
  2708. ShowError("script_set_reg: failed to set param '%s' to %d.\n", name, val);
  2709. script_reportsrc(st);
  2710. st->state = END;
  2711. }
  2712. return 0;
  2713. }
  2714. return 1;
  2715. }
  2716. switch (prefix) {
  2717. case '@':
  2718. pc_setreg(sd, num, val);
  2719. return 1;
  2720. case '$':
  2721. return mapreg_setreg(num, val);
  2722. case '#':
  2723. return (name[1] == '#') ?
  2724. pc_setaccountreg2(sd, num, val) :
  2725. pc_setaccountreg(sd, num, val);
  2726. case '.':
  2727. {
  2728. struct reg_db *n = (ref) ? ref : (name[1] == '@') ? &st->stack->scope : &st->script->local;
  2729. if( n ) {
  2730. if( val != 0 ) {
  2731. i64db_iput(n->vars, num, val);
  2732. if( script_getvaridx(num) )
  2733. script_array_update(n, num, false);
  2734. } else {
  2735. i64db_remove(n->vars, num);
  2736. if( script_getvaridx(num) )
  2737. script_array_update(n, num, true);
  2738. }
  2739. }
  2740. }
  2741. return 1;
  2742. case '\'':
  2743. {
  2744. int instance_id = script_instancegetid(st);
  2745. if( instance_id ) {
  2746. if( val != 0 ) {
  2747. i64db_iput(instance_data[instance_id].regs.vars, num, val);
  2748. if( script_getvaridx(num) )
  2749. script_array_update(&instance_data[instance_id].regs, num, false);
  2750. } else {
  2751. i64db_remove(instance_data[instance_id].regs.vars, num);
  2752. if (script_getvaridx(num))
  2753. script_array_update(&instance_data[instance_id].regs, num, true);
  2754. }
  2755. } else {
  2756. ShowError("script_set_reg: cannot write instance variable '%s', NPC not in a instance!\n", name);
  2757. script_reportsrc(st);
  2758. }
  2759. return 1;
  2760. }
  2761. default:
  2762. return pc_setglobalreg(sd, num, val);
  2763. }
  2764. }
  2765. }
  2766. int set_var(TBL_PC* sd, char* name, void* val)
  2767. {
  2768. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2769. }
  2770. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct reg_db *ref)
  2771. {
  2772. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2773. }
  2774. /**
  2775. * Converts the data to a string
  2776. * @param st
  2777. * @param data
  2778. * @param sd
  2779. */
  2780. const char* conv_str_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2781. {
  2782. char* p;
  2783. get_val_(st, data, sd);
  2784. if( data_isstring(data) )
  2785. {// nothing to convert
  2786. }
  2787. else if( data_isint(data) )
  2788. {// int -> string
  2789. CREATE(p, char, ITEM_NAME_LENGTH);
  2790. snprintf(p, ITEM_NAME_LENGTH, "%"PRId64"", data->u.num);
  2791. p[ITEM_NAME_LENGTH-1] = '\0';
  2792. data->type = C_STR;
  2793. data->u.str = p;
  2794. }
  2795. else if( data_isreference(data) )
  2796. {// reference -> string
  2797. //##TODO when does this happen (check get_val) [FlavioJS]
  2798. data->type = C_CONSTSTR;
  2799. data->u.str = reference_getname(data);
  2800. }
  2801. else
  2802. {// unsupported data type
  2803. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2804. script_reportdata(data);
  2805. script_reportsrc(st);
  2806. data->type = C_CONSTSTR;
  2807. data->u.str = "";
  2808. }
  2809. return data->u.str;
  2810. }
  2811. const char* conv_str(struct script_state* st, struct script_data* data)
  2812. {
  2813. return conv_str_(st, data, NULL);
  2814. }
  2815. /**
  2816. * Converts the data to an int
  2817. * @param st
  2818. * @param data
  2819. * @param sd
  2820. */
  2821. int conv_num_(struct script_state* st, struct script_data* data, struct map_session_data *sd)
  2822. {
  2823. get_val_(st, data, sd);
  2824. if( data_isint(data) )
  2825. {// nothing to convert
  2826. }
  2827. else if( data_isstring(data) )
  2828. {// string -> int
  2829. // the result does not overflow or underflow, it is capped instead
  2830. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2831. char* p = data->u.str;
  2832. long num;
  2833. errno = 0;
  2834. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2835. if( errno == ERANGE
  2836. #if LONG_MAX > INT_MAX
  2837. || num < INT_MIN || num > INT_MAX
  2838. #endif
  2839. )
  2840. {
  2841. if( num <= INT_MIN )
  2842. {
  2843. num = INT_MIN;
  2844. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2845. }
  2846. else//if( num >= INT_MAX )
  2847. {
  2848. num = INT_MAX;
  2849. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2850. }
  2851. script_reportdata(data);
  2852. script_reportsrc(st);
  2853. }
  2854. if( data->type == C_STR )
  2855. aFree(p);
  2856. data->type = C_INT;
  2857. data->u.num = (int)num;
  2858. }
  2859. #if 0
  2860. // FIXME this function is being used to retrieve the position of labels and
  2861. // probably other stuff [FlavioJS]
  2862. else
  2863. {// unsupported data type
  2864. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2865. script_reportdata(data);
  2866. script_reportsrc(st);
  2867. data->type = C_INT;
  2868. data->u.num = 0;
  2869. }
  2870. #endif
  2871. return (int)data->u.num;
  2872. }
  2873. int conv_num(struct script_state* st, struct script_data* data)
  2874. {
  2875. return conv_num_(st, data, NULL);
  2876. }
  2877. //
  2878. // Stack operations
  2879. //
  2880. /// Increases the size of the stack
  2881. void stack_expand(struct script_stack* stack)
  2882. {
  2883. stack->sp_max += 64;
  2884. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2885. stack->sp_max * sizeof(stack->stack_data[0]) );
  2886. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2887. 64 * sizeof(stack->stack_data[0]) );
  2888. }
  2889. /// Pushes a value into the stack
  2890. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2891. /// Pushes a value into the stack (with reference)
  2892. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int64 val, struct reg_db *ref)
  2893. {
  2894. if( stack->sp >= stack->sp_max )
  2895. stack_expand(stack);
  2896. stack->stack_data[stack->sp].type = type;
  2897. stack->stack_data[stack->sp].u.num = val;
  2898. stack->stack_data[stack->sp].ref = ref;
  2899. stack->sp++;
  2900. return &stack->stack_data[stack->sp-1];
  2901. }
  2902. /// Pushes a string into the stack
  2903. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2904. {
  2905. if( stack->sp >= stack->sp_max )
  2906. stack_expand(stack);
  2907. stack->stack_data[stack->sp].type = type;
  2908. stack->stack_data[stack->sp].u.str = str;
  2909. stack->stack_data[stack->sp].ref = NULL;
  2910. stack->sp++;
  2911. return &stack->stack_data[stack->sp-1];
  2912. }
  2913. /// Pushes a retinfo into the stack
  2914. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri, struct reg_db *ref)
  2915. {
  2916. if( stack->sp >= stack->sp_max )
  2917. stack_expand(stack);
  2918. stack->stack_data[stack->sp].type = C_RETINFO;
  2919. stack->stack_data[stack->sp].u.ri = ri;
  2920. stack->stack_data[stack->sp].ref = ref;
  2921. stack->sp++;
  2922. return &stack->stack_data[stack->sp-1];
  2923. }
  2924. /// Pushes a copy of the target position into the stack
  2925. struct script_data* push_copy(struct script_stack* stack, int pos)
  2926. {
  2927. switch( stack->stack_data[pos].type ) {
  2928. case C_CONSTSTR:
  2929. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2930. break;
  2931. case C_STR:
  2932. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2933. break;
  2934. case C_RETINFO:
  2935. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2936. exit(1);
  2937. break;
  2938. default:
  2939. return push_val2(
  2940. stack,stack->stack_data[pos].type,
  2941. stack->stack_data[pos].u.num,
  2942. stack->stack_data[pos].ref
  2943. );
  2944. break;
  2945. }
  2946. }
  2947. /// Removes the values in indexes [start,end] from the stack.
  2948. /// Adjusts all stack pointers.
  2949. void pop_stack(struct script_state* st, int start, int end)
  2950. {
  2951. struct script_stack* stack = st->stack;
  2952. struct script_data* data;
  2953. int i;
  2954. if( start < 0 )
  2955. start = 0;
  2956. if( end > stack->sp )
  2957. end = stack->sp;
  2958. if( start >= end )
  2959. return;// nothing to pop
  2960. // free stack elements
  2961. for( i = start; i < end; i++ )
  2962. {
  2963. data = &stack->stack_data[i];
  2964. if( data->type == C_STR )
  2965. aFree(data->u.str);
  2966. if( data->type == C_RETINFO ) {
  2967. struct script_retinfo* ri = data->u.ri;
  2968. if (ri->scope.vars) {
  2969. script_free_vars(ri->scope.vars);
  2970. ri->scope.vars = NULL;
  2971. }
  2972. if (ri->scope.arrays) {
  2973. ri->scope.arrays->destroy(ri->scope.arrays, script_free_array_db);
  2974. ri->scope.arrays = NULL;
  2975. }
  2976. if( data->ref )
  2977. aFree(data->ref);
  2978. aFree(ri);
  2979. }
  2980. data->type = C_NOP;
  2981. }
  2982. // move the rest of the elements
  2983. if( stack->sp > end )
  2984. {
  2985. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2986. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2987. stack->stack_data[i].type = C_NOP;
  2988. }
  2989. // adjust stack pointers
  2990. if( st->start > end ) st->start -= end - start;
  2991. else if( st->start > start ) st->start = start;
  2992. if( st->end > end ) st->end -= end - start;
  2993. else if( st->end > start ) st->end = start;
  2994. if( stack->defsp > end ) stack->defsp -= end - start;
  2995. else if( stack->defsp > start ) stack->defsp = start;
  2996. stack->sp -= end - start;
  2997. }
  2998. ///
  2999. ///
  3000. ///
  3001. /*==========================================
  3002. * Release script dependent variable, dependent variable of function
  3003. *------------------------------------------*/
  3004. void script_free_vars(struct DBMap* storage)
  3005. {
  3006. if( storage ) {
  3007. // destroy the storage construct containing the variables
  3008. db_destroy(storage);
  3009. }
  3010. }
  3011. void script_free_code(struct script_code* code)
  3012. {
  3013. nullpo_retv(code);
  3014. if (code->instances)
  3015. script_stop_instances(code);
  3016. script_free_vars(code->local.vars);
  3017. if (code->local.arrays)
  3018. code->local.arrays->destroy(code->local.arrays, script_free_array_db);
  3019. aFree(code->script_buf);
  3020. aFree(code);
  3021. }
  3022. /// Creates a new script state.
  3023. ///
  3024. /// @param script Script code
  3025. /// @param pos Position in the code
  3026. /// @param rid Who is running the script (attached player)
  3027. /// @param oid Where the code is being run (npc 'object')
  3028. /// @return Script state
  3029. struct script_state* script_alloc_state(struct script_code* rootscript, int pos, int rid, int oid)
  3030. {
  3031. struct script_state* st;
  3032. st = ers_alloc(st_ers, struct script_state);
  3033. st->stack = ers_alloc(stack_ers, struct script_stack);
  3034. st->stack->sp = 0;
  3035. st->stack->sp_max = 64;
  3036. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  3037. st->stack->defsp = st->stack->sp;
  3038. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3039. st->stack->scope.arrays = NULL;
  3040. st->state = RUN;
  3041. st->script = rootscript;
  3042. st->pos = pos;
  3043. st->rid = rid;
  3044. st->oid = oid;
  3045. st->sleep.timer = INVALID_TIMER;
  3046. st->npc_item_flag = battle_config.item_enabled_npc;
  3047. if( st->script->instances != USHRT_MAX )
  3048. st->script->instances++;
  3049. else {
  3050. struct npc_data *nd = map_id2nd(oid);
  3051. ShowError("Over 65k instances of '%s' script are being run!\n",nd ? nd->name : "unknown");
  3052. }
  3053. if (!st->script->local.vars)
  3054. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  3055. st->id = next_id++;
  3056. active_scripts++;
  3057. idb_put(st_db, st->id, st);
  3058. return st;
  3059. }
  3060. /// Frees a script state.
  3061. ///
  3062. /// @param st Script state
  3063. void script_free_state(struct script_state* st)
  3064. {
  3065. if (idb_exists(st_db, st->id)) {
  3066. struct map_session_data *sd = st->rid ? map_id2sd(st->rid) : NULL;
  3067. if (st->bk_st) // backup was not restored
  3068. ShowDebug("script_free_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3069. if (sd && sd->st == st) { // Current script is aborted.
  3070. if(sd->state.using_fake_npc) {
  3071. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3072. sd->state.using_fake_npc = 0;
  3073. }
  3074. sd->st = NULL;
  3075. sd->npc_id = 0;
  3076. }
  3077. if (st->sleep.timer != INVALID_TIMER)
  3078. delete_timer(st->sleep.timer, run_script_timer);
  3079. if (st->stack) {
  3080. script_free_vars(st->stack->scope.vars);
  3081. if (st->stack->scope.arrays)
  3082. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3083. pop_stack(st, 0, st->stack->sp);
  3084. aFree(st->stack->stack_data);
  3085. ers_free(stack_ers, st->stack);
  3086. st->stack = NULL;
  3087. }
  3088. if (st->script && st->script->instances != USHRT_MAX && --st->script->instances == 0) {
  3089. if (st->script->local.vars && !db_size(st->script->local.vars)) {
  3090. script_free_vars(st->script->local.vars);
  3091. st->script->local.vars = NULL;
  3092. }
  3093. if (st->script->local.arrays && !db_size(st->script->local.arrays)) {
  3094. st->script->local.arrays->destroy(st->script->local.arrays, script_free_array_db);
  3095. st->script->local.arrays = NULL;
  3096. }
  3097. }
  3098. st->pos = -1;
  3099. idb_remove(st_db, st->id);
  3100. ers_free(st_ers, st);
  3101. if (--active_scripts == 0)
  3102. next_id = 0;
  3103. }
  3104. }
  3105. //
  3106. // Main execution unit
  3107. //
  3108. /*==========================================
  3109. * Read command
  3110. *------------------------------------------*/
  3111. c_op get_com(unsigned char *script,int *pos)
  3112. {
  3113. int i = 0, j = 0;
  3114. if(script[*pos]>=0x80){
  3115. return C_INT;
  3116. }
  3117. while(script[*pos]>=0x40){
  3118. i=script[(*pos)++]<<j;
  3119. j+=6;
  3120. }
  3121. return (c_op)(i+(script[(*pos)++]<<j));
  3122. }
  3123. /*==========================================
  3124. * Income figures
  3125. *------------------------------------------*/
  3126. int get_num(unsigned char *script,int *pos)
  3127. {
  3128. int i,j;
  3129. i=0; j=0;
  3130. while(script[*pos]>=0xc0){
  3131. i+=(script[(*pos)++]&0x7f)<<j;
  3132. j+=6;
  3133. }
  3134. return i+((script[(*pos)++]&0x7f)<<j);
  3135. }
  3136. /// Ternary operators
  3137. /// test ? if_true : if_false
  3138. void op_3(struct script_state* st, int op)
  3139. {
  3140. struct script_data* data;
  3141. int flag = 0;
  3142. data = script_getdatatop(st, -3);
  3143. get_val(st, data);
  3144. if( data_isstring(data) )
  3145. flag = data->u.str[0];// "" -> false
  3146. else if( data_isint(data) )
  3147. flag = data->u.num == 0 ? 0 : 1;// 0 -> false
  3148. else
  3149. {
  3150. ShowError("script:op_3: invalid data for the ternary operator test\n");
  3151. script_reportdata(data);
  3152. script_reportsrc(st);
  3153. script_removetop(st, -3, 0);
  3154. script_pushnil(st);
  3155. return;
  3156. }
  3157. if( flag )
  3158. script_pushcopytop(st, -2);
  3159. else
  3160. script_pushcopytop(st, -1);
  3161. script_removetop(st, -4, -1);
  3162. }
  3163. /// Binary string operators
  3164. /// s1 EQ s2 -> i
  3165. /// s1 NE s2 -> i
  3166. /// s1 GT s2 -> i
  3167. /// s1 GE s2 -> i
  3168. /// s1 LT s2 -> i
  3169. /// s1 LE s2 -> i
  3170. /// s1 ADD s2 -> s
  3171. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  3172. {
  3173. int a = 0;
  3174. switch(op){
  3175. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  3176. case C_NE: a = (strcmp(s1,s2) != 0); break;
  3177. case C_GT: a = (strcmp(s1,s2) > 0); break;
  3178. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  3179. case C_LT: a = (strcmp(s1,s2) < 0); break;
  3180. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  3181. case C_ADD:
  3182. {
  3183. char* buf = (char *)aMalloc((strlen(s1)+strlen(s2)+1)*sizeof(char));
  3184. strcpy(buf, s1);
  3185. strcat(buf, s2);
  3186. script_pushstr(st, buf);
  3187. return;
  3188. }
  3189. default:
  3190. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  3191. script_reportsrc(st);
  3192. script_pushnil(st);
  3193. st->state = END;
  3194. return;
  3195. }
  3196. script_pushint(st,a);
  3197. }
  3198. /// Binary number operators
  3199. /// i OP i -> i
  3200. void op_2num(struct script_state* st, int op, int i1, int i2)
  3201. {
  3202. int ret;
  3203. double ret_double;
  3204. switch( op ) {
  3205. case C_AND: ret = i1 & i2; break;
  3206. case C_OR: ret = i1 | i2; break;
  3207. case C_XOR: ret = i1 ^ i2; break;
  3208. case C_LAND: ret = (i1 && i2); break;
  3209. case C_LOR: ret = (i1 || i2); break;
  3210. case C_EQ: ret = (i1 == i2); break;
  3211. case C_NE: ret = (i1 != i2); break;
  3212. case C_GT: ret = (i1 > i2); break;
  3213. case C_GE: ret = (i1 >= i2); break;
  3214. case C_LT: ret = (i1 < i2); break;
  3215. case C_LE: ret = (i1 <= i2); break;
  3216. case C_R_SHIFT: ret = i1>>i2; break;
  3217. case C_L_SHIFT: ret = i1<<i2; break;
  3218. case C_DIV:
  3219. case C_MOD:
  3220. if( i2 == 0 )
  3221. {
  3222. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3223. script_reportsrc(st);
  3224. script_pushnil(st);
  3225. st->state = END;
  3226. return;
  3227. }
  3228. else if( op == C_DIV )
  3229. ret = i1 / i2;
  3230. else//if( op == C_MOD )
  3231. ret = i1 % i2;
  3232. break;
  3233. default:
  3234. switch( op ) {// operators that can overflow/underflow
  3235. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  3236. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  3237. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  3238. default:
  3239. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3240. script_reportsrc(st);
  3241. script_pushnil(st);
  3242. return;
  3243. }
  3244. if( ret_double < (double)INT_MIN ) {
  3245. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3246. script_reportsrc(st);
  3247. ret = INT_MIN;
  3248. }
  3249. else if( ret_double > (double)INT_MAX ) {
  3250. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  3251. script_reportsrc(st);
  3252. ret = INT_MAX;
  3253. }
  3254. }
  3255. script_pushint(st, ret);
  3256. }
  3257. /// Binary operators
  3258. void op_2(struct script_state *st, int op)
  3259. {
  3260. struct script_data* left, leftref;
  3261. struct script_data* right;
  3262. leftref.type = C_NOP;
  3263. left = script_getdatatop(st, -2);
  3264. right = script_getdatatop(st, -1);
  3265. if (st->op2ref) {
  3266. if (data_isreference(left)) {
  3267. leftref = *left;
  3268. }
  3269. st->op2ref = 0;
  3270. }
  3271. get_val(st, left);
  3272. get_val(st, right);
  3273. // automatic conversions
  3274. switch( op )
  3275. {
  3276. case C_ADD:
  3277. if( data_isint(left) && data_isstring(right) )
  3278. {// convert int-string to string-string
  3279. conv_str(st, left);
  3280. }
  3281. else if( data_isstring(left) && data_isint(right) )
  3282. {// convert string-int to string-string
  3283. conv_str(st, right);
  3284. }
  3285. break;
  3286. }
  3287. if( data_isstring(left) && data_isstring(right) )
  3288. {// ss => op_2str
  3289. op_2str(st, op, left->u.str, right->u.str);
  3290. script_removetop(st, leftref.type == C_NOP ? -3 : -2, -1);// pop the two values before the top one
  3291. if (leftref.type != C_NOP)
  3292. {
  3293. if (left->type == C_STR) // don't free C_CONSTSTR
  3294. aFree(left->u.str);
  3295. *left = leftref;
  3296. }
  3297. }
  3298. else if( data_isint(left) && data_isint(right) )
  3299. {// ii => op_2num
  3300. int i1 = (int)left->u.num;
  3301. int i2 = (int)right->u.num;
  3302. script_removetop(st, leftref.type == C_NOP ? -2 : -1, 0);
  3303. op_2num(st, op, i1, i2);
  3304. if (leftref.type != C_NOP)
  3305. *left = leftref;
  3306. }
  3307. else
  3308. {// invalid argument
  3309. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  3310. script_reportdata(left);
  3311. script_reportdata(right);
  3312. script_reportsrc(st);
  3313. script_removetop(st, -2, 0);
  3314. script_pushnil(st);
  3315. st->state = END;
  3316. }
  3317. }
  3318. /// Unary operators
  3319. /// NEG i -> i
  3320. /// NOT i -> i
  3321. /// LNOT i -> i
  3322. void op_1(struct script_state* st, int op)
  3323. {
  3324. struct script_data* data;
  3325. int i1;
  3326. data = script_getdatatop(st, -1);
  3327. get_val(st, data);
  3328. if( !data_isint(data) )
  3329. {// not a number
  3330. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  3331. script_reportdata(data);
  3332. script_reportsrc(st);
  3333. script_pushnil(st);
  3334. st->state = END;
  3335. return;
  3336. }
  3337. i1 = (int)data->u.num;
  3338. script_removetop(st, -1, 0);
  3339. switch( op )
  3340. {
  3341. case C_NEG: i1 = -i1; break;
  3342. case C_NOT: i1 = ~i1; break;
  3343. case C_LNOT: i1 = !i1; break;
  3344. default:
  3345. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  3346. script_reportsrc(st);
  3347. script_pushnil(st);
  3348. st->state = END;
  3349. return;
  3350. }
  3351. script_pushint(st, i1);
  3352. }
  3353. /// Checks the type of all arguments passed to a built-in function.
  3354. ///
  3355. /// @param st Script state whose stack arguments should be inspected.
  3356. /// @param func Built-in function for which the arguments are intended.
  3357. static void script_check_buildin_argtype(struct script_state* st, int func)
  3358. {
  3359. int idx, invalid = 0;
  3360. for( idx = 2; script_hasdata(st, idx); idx++ )
  3361. {
  3362. struct script_data* data = script_getdata(st, idx);
  3363. script_function* sf = &buildin_func[str_data[func].val];
  3364. char type = sf->arg[idx-2];
  3365. if( type == '?' || type == '*' )
  3366. {// optional argument or unknown number of optional parameters ( no types are after this )
  3367. break;
  3368. }
  3369. else if( type == 0 )
  3370. {// more arguments than necessary ( should not happen, as it is checked before )
  3371. ShowWarning("Found more arguments than necessary. unexpected arg type %s\n",script_op2name(data->type));
  3372. invalid++;
  3373. break;
  3374. }
  3375. else
  3376. {
  3377. const char* name = NULL;
  3378. if( data_isreference(data) )
  3379. {// get name for variables to determine the type they refer to
  3380. name = reference_getname(data);
  3381. }
  3382. switch( type )
  3383. {
  3384. case 'v':
  3385. if( !data_isstring(data) && !data_isint(data) && !data_isreference(data) )
  3386. {// variant
  3387. ShowWarning("Unexpected type for argument %d. Expected string, number or variable.\n", idx-1);
  3388. script_reportdata(data);
  3389. invalid++;
  3390. }
  3391. break;
  3392. case 's':
  3393. if( !data_isstring(data) && !( data_isreference(data) && is_string_variable(name) ) )
  3394. {// string
  3395. ShowWarning("Unexpected type for argument %d. Expected string.\n", idx-1);
  3396. script_reportdata(data);
  3397. invalid++;
  3398. }
  3399. break;
  3400. case 'i':
  3401. if( !data_isint(data) && !( data_isreference(data) && ( reference_toparam(data) || reference_toconstant(data) || !is_string_variable(name) ) ) )
  3402. {// int ( params and constants are always int )
  3403. ShowWarning("Unexpected type for argument %d. Expected number.\n", idx-1);
  3404. script_reportdata(data);
  3405. invalid++;
  3406. }
  3407. break;
  3408. case 'r':
  3409. if( !data_isreference(data) )
  3410. {// variables
  3411. ShowWarning("Unexpected type for argument %d. Expected variable, got %s.\n", idx-1,script_op2name(data->type));
  3412. script_reportdata(data);
  3413. invalid++;
  3414. }
  3415. break;
  3416. case 'l':
  3417. if( !data_islabel(data) && !data_isfunclabel(data) )
  3418. {// label
  3419. ShowWarning("Unexpected type for argument %d. Expected label, got %s\n", idx-1,script_op2name(data->type));
  3420. script_reportdata(data);
  3421. invalid++;
  3422. }
  3423. break;
  3424. }
  3425. }
  3426. }
  3427. if(invalid)
  3428. {
  3429. ShowDebug("Function: %s\n", get_str(func));
  3430. script_reportsrc(st);
  3431. }
  3432. }
  3433. /// Executes a buildin command.
  3434. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  3435. int run_func(struct script_state *st)
  3436. {
  3437. struct script_data* data;
  3438. int i,start_sp,end_sp,func;
  3439. end_sp = st->stack->sp;// position after the last argument
  3440. for( i = end_sp-1; i > 0 ; --i )
  3441. if( st->stack->stack_data[i].type == C_ARG )
  3442. break;
  3443. if( i == 0 ) {
  3444. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  3445. st->state = END;
  3446. script_reportsrc(st);
  3447. return 1;
  3448. }
  3449. start_sp = i-1;// C_NAME of the command
  3450. st->start = start_sp;
  3451. st->end = end_sp;
  3452. data = &st->stack->stack_data[st->start];
  3453. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC ) {
  3454. func = (int)data->u.num;
  3455. st->funcname = reference_getname(data);
  3456. } else {
  3457. ShowError("script:run_func: not a buildin command.\n");
  3458. script_reportdata(data);
  3459. script_reportsrc(st);
  3460. st->state = END;
  3461. return 1;
  3462. }
  3463. if( script_config.warn_func_mismatch_argtypes ) {
  3464. script_check_buildin_argtype(st, func);
  3465. }
  3466. if(str_data[func].func) {
  3467. if (str_data[func].func(st) == SCRIPT_CMD_FAILURE) //Report error
  3468. script_reportsrc(st);
  3469. } else {
  3470. ShowError("script:run_func: '%s' (id=%d type=%s) has no C function. please report this!!!\n", get_str(func), func, script_op2name(str_data[func].type));
  3471. script_reportsrc(st);
  3472. st->state = END;
  3473. }
  3474. // Stack's datum are used when re-running functions [Eoe]
  3475. if( st->state == RERUNLINE )
  3476. return 0;
  3477. pop_stack(st, st->start, st->end);
  3478. if( st->state == RETFUNC ) {// return from a user-defined function
  3479. struct script_retinfo* ri;
  3480. int olddefsp = st->stack->defsp;
  3481. int nargs;
  3482. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  3483. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  3484. {
  3485. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  3486. script_reportsrc(st);
  3487. st->state = END;
  3488. return 1;
  3489. }
  3490. script_free_vars(st->stack->scope.vars);
  3491. st->stack->scope.arrays->destroy(st->stack->scope.arrays, script_free_array_db);
  3492. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  3493. nargs = ri->nargs;
  3494. st->pos = ri->pos;
  3495. st->script = ri->script;
  3496. st->stack->scope.vars = ri->scope.vars;
  3497. st->stack->scope.arrays = ri->scope.arrays;
  3498. st->stack->defsp = ri->defsp;
  3499. memset(ri, 0, sizeof(struct script_retinfo));
  3500. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  3501. st->state = GOTO;
  3502. }
  3503. return 0;
  3504. }
  3505. /*==========================================
  3506. * script execution
  3507. *------------------------------------------*/
  3508. void run_script(struct script_code *rootscript, int pos, int rid, int oid)
  3509. {
  3510. struct script_state *st;
  3511. if( rootscript == NULL || pos < 0 )
  3512. return;
  3513. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  3514. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  3515. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  3516. st = script_alloc_state(rootscript, pos, rid, oid);
  3517. run_script_main(st);
  3518. }
  3519. void script_stop_instances(struct script_code *code) {
  3520. DBIterator *iter;
  3521. struct script_state* st;
  3522. if( !active_scripts )
  3523. return; // Don't even bother.
  3524. iter = db_iterator(st_db);
  3525. for( st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter) ) {
  3526. if( st->script == code )
  3527. script_free_state(st);
  3528. }
  3529. dbi_destroy(iter);
  3530. }
  3531. /*==========================================
  3532. * Timer function for sleep
  3533. *------------------------------------------*/
  3534. int run_script_timer(int tid, unsigned int tick, int id, intptr_t data)
  3535. {
  3536. struct script_state *st = (struct script_state *)data;
  3537. TBL_PC *sd = map_id2sd(st->rid);
  3538. if ((sd && sd->status.char_id != id) || (st->rid && !sd)) { // Character mismatch. Cancel execution.
  3539. st->rid = 0;
  3540. st->state = END;
  3541. }
  3542. st->sleep.timer = INVALID_TIMER;
  3543. if(st->state != RERUNLINE)
  3544. st->sleep.tick = 0;
  3545. run_script_main(st);
  3546. return 0;
  3547. }
  3548. /// Detaches script state from possibly attached character and restores it's previous script if any.
  3549. ///
  3550. /// @param st Script state to detach.
  3551. /// @param dequeue_event Whether to schedule any queued events, when there was no previous script.
  3552. static void script_detach_state(struct script_state* st, bool dequeue_event)
  3553. {
  3554. struct map_session_data* sd;
  3555. if(st->rid && (sd = map_id2sd(st->rid))!=NULL) {
  3556. sd->st = st->bk_st;
  3557. sd->npc_id = st->bk_npcid;
  3558. sd->state.disable_atcommand_on_npc = 0;
  3559. if(st->bk_st) {
  3560. //Remove tag for removal.
  3561. st->bk_st = NULL;
  3562. st->bk_npcid = 0;
  3563. } else if(dequeue_event) {
  3564. #ifdef SECURE_NPCTIMEOUT
  3565. /**
  3566. * We're done with this NPC session, so we cancel the timer (if existent) and move on
  3567. **/
  3568. if( sd->npc_idle_timer != INVALID_TIMER ) {
  3569. delete_timer(sd->npc_idle_timer,npc_rr_secure_timeout_timer);
  3570. sd->npc_idle_timer = INVALID_TIMER;
  3571. }
  3572. #endif
  3573. npc_event_dequeue(sd);
  3574. }
  3575. }
  3576. else if(st->bk_st)
  3577. {// rid was set to 0, before detaching the script state
  3578. ShowError("script_detach_state: Found previous script state without attached player (rid=%d, oid=%d, state=%d, bk_npcid=%d)\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3579. script_reportsrc(st->bk_st);
  3580. script_free_state(st->bk_st);
  3581. st->bk_st = NULL;
  3582. }
  3583. }
  3584. /// Attaches script state to possibly attached character and backups it's previous script, if any.
  3585. ///
  3586. /// @param st Script state to attach.
  3587. static void script_attach_state(struct script_state* st)
  3588. {
  3589. struct map_session_data* sd;
  3590. if(st->rid && (sd = map_id2sd(st->rid))!=NULL)
  3591. {
  3592. if(st!=sd->st)
  3593. {
  3594. if(st->bk_st)
  3595. {// there is already a backup
  3596. ShowDebug("script_attach_state: Previous script state lost (rid=%d, oid=%d, state=%d, bk_npcid=%d).\n", st->bk_st->rid, st->bk_st->oid, st->bk_st->state, st->bk_npcid);
  3597. }
  3598. st->bk_st = sd->st;
  3599. st->bk_npcid = sd->npc_id;
  3600. }
  3601. sd->st = st;
  3602. sd->npc_id = st->oid;
  3603. sd->npc_item_flag = st->npc_item_flag; // load default.
  3604. sd->state.disable_atcommand_on_npc = (!pc_has_permission(sd, PC_PERM_ENABLE_COMMAND));
  3605. #ifdef SECURE_NPCTIMEOUT
  3606. if( sd->npc_idle_timer == INVALID_TIMER )
  3607. sd->npc_idle_timer = add_timer(gettick() + (SECURE_NPCTIMEOUT_INTERVAL*1000),npc_rr_secure_timeout_timer,sd->bl.id,0);
  3608. sd->npc_idle_tick = gettick();
  3609. #endif
  3610. }
  3611. }
  3612. /*==========================================
  3613. * The main part of the script execution
  3614. *------------------------------------------*/
  3615. void run_script_main(struct script_state *st)
  3616. {
  3617. int cmdcount = script_config.check_cmdcount;
  3618. int gotocount = script_config.check_gotocount;
  3619. TBL_PC *sd;
  3620. struct script_stack *stack = st->stack;
  3621. script_attach_state(st);
  3622. if(st->state == RERUNLINE) {
  3623. run_func(st);
  3624. if(st->state == GOTO)
  3625. st->state = RUN;
  3626. } else if(st->state != END)
  3627. st->state = RUN;
  3628. while(st->state == RUN) {
  3629. enum c_op c = get_com(st->script->script_buf,&st->pos);
  3630. switch(c){
  3631. case C_EOL:
  3632. if( stack->defsp > stack->sp )
  3633. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  3634. else
  3635. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  3636. break;
  3637. case C_INT:
  3638. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  3639. break;
  3640. case C_POS:
  3641. case C_NAME:
  3642. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  3643. st->pos+=3;
  3644. break;
  3645. case C_ARG:
  3646. push_val(stack,c,0);
  3647. break;
  3648. case C_STR:
  3649. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  3650. while(st->script->script_buf[st->pos++]);
  3651. break;
  3652. case C_FUNC:
  3653. run_func(st);
  3654. if(st->state==GOTO){
  3655. st->state = RUN;
  3656. if( !st->freeloop && gotocount>0 && (--gotocount)<=0 ){
  3657. ShowError("script:run_script_main: infinity loop !\n");
  3658. script_reportsrc(st);
  3659. st->state=END;
  3660. }
  3661. }
  3662. break;
  3663. case C_REF:
  3664. st->op2ref = 1;
  3665. break;
  3666. case C_NEG:
  3667. case C_NOT:
  3668. case C_LNOT:
  3669. op_1(st ,c);
  3670. break;
  3671. case C_ADD:
  3672. case C_SUB:
  3673. case C_MUL:
  3674. case C_DIV:
  3675. case C_MOD:
  3676. case C_EQ:
  3677. case C_NE:
  3678. case C_GT:
  3679. case C_GE:
  3680. case C_LT:
  3681. case C_LE:
  3682. case C_AND:
  3683. case C_OR:
  3684. case C_XOR:
  3685. case C_LAND:
  3686. case C_LOR:
  3687. case C_R_SHIFT:
  3688. case C_L_SHIFT:
  3689. op_2(st, c);
  3690. break;
  3691. case C_OP3:
  3692. op_3(st, c);
  3693. break;
  3694. case C_NOP:
  3695. st->state=END;
  3696. break;
  3697. default:
  3698. ShowError("script:run_script_main:unknown command : %d @ %d\n",c,st->pos);
  3699. st->state=END;
  3700. break;
  3701. }
  3702. if( !st->freeloop && cmdcount>0 && (--cmdcount)<=0 ){
  3703. ShowError("script:run_script_main: infinity loop !\n");
  3704. script_reportsrc(st);
  3705. st->state=END;
  3706. }
  3707. }
  3708. if(st->sleep.tick > 0) {
  3709. //Restore previous script
  3710. script_detach_state(st, false);
  3711. //Delay execution
  3712. sd = map_id2sd(st->rid); // Get sd since script might have attached someone while running. [Inkfish]
  3713. st->sleep.charid = sd?sd->status.char_id:0;
  3714. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  3715. run_script_timer, st->sleep.charid, (intptr_t)st);
  3716. } else if(st->state != END && st->rid) {
  3717. //Resume later (st is already attached to player).
  3718. if(st->bk_st) {
  3719. ShowWarning("Unable to restore stack! Double continuation!\n");
  3720. //Report BOTH scripts to see if that can help somehow.
  3721. ShowDebug("Previous script (lost):\n");
  3722. script_reportsrc(st->bk_st);
  3723. ShowDebug("Current script:\n");
  3724. script_reportsrc(st);
  3725. script_free_state(st->bk_st);
  3726. st->bk_st = NULL;
  3727. }
  3728. } else {
  3729. //Dispose of script.
  3730. if ((sd = map_id2sd(st->rid))!=NULL)
  3731. { //Restore previous stack and save char.
  3732. if(sd->state.using_fake_npc){
  3733. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  3734. sd->state.using_fake_npc = 0;
  3735. }
  3736. //Restore previous script if any.
  3737. script_detach_state(st, true);
  3738. if (sd->vars_dirty)
  3739. intif_saveregistry(sd);
  3740. }
  3741. script_free_state(st);
  3742. }
  3743. }
  3744. int script_config_read(char *cfgName)
  3745. {
  3746. int i;
  3747. char line[1024],w1[1024],w2[1024];
  3748. FILE *fp;
  3749. fp=fopen(cfgName,"r");
  3750. if(fp==NULL){
  3751. ShowError("File not found: %s\n", cfgName);
  3752. return 1;
  3753. }
  3754. while(fgets(line, sizeof(line), fp))
  3755. {
  3756. if(line[0] == '/' && line[1] == '/')
  3757. continue;
  3758. i=sscanf(line,"%1023[^:]: %1023[^\r\n]",w1,w2);
  3759. if(i!=2)
  3760. continue;
  3761. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3762. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3763. }
  3764. else if(strcmpi(w1,"check_cmdcount")==0) {
  3765. script_config.check_cmdcount = config_switch(w2);
  3766. }
  3767. else if(strcmpi(w1,"check_gotocount")==0) {
  3768. script_config.check_gotocount = config_switch(w2);
  3769. }
  3770. else if(strcmpi(w1,"input_min_value")==0) {
  3771. script_config.input_min_value = config_switch(w2);
  3772. }
  3773. else if(strcmpi(w1,"input_max_value")==0) {
  3774. script_config.input_max_value = config_switch(w2);
  3775. }
  3776. else if(strcmpi(w1,"warn_func_mismatch_argtypes")==0) {
  3777. script_config.warn_func_mismatch_argtypes = config_switch(w2);
  3778. }
  3779. else if(strcmpi(w1,"import")==0){
  3780. script_config_read(w2);
  3781. }
  3782. else {
  3783. ShowWarning("Unknown setting '%s' in file %s\n", w1, cfgName);
  3784. }
  3785. }
  3786. fclose(fp);
  3787. return 0;
  3788. }
  3789. /**
  3790. * @see DBApply
  3791. */
  3792. static int db_script_free_code_sub(DBKey key, DBData *data, va_list ap)
  3793. {
  3794. struct script_code *code = db_data2ptr(data);
  3795. if (code)
  3796. script_free_code(code);
  3797. return 0;
  3798. }
  3799. void script_run_autobonus(const char *autobonus, struct map_session_data *sd, unsigned int pos)
  3800. {
  3801. struct script_code *script = (struct script_code *)strdb_get(autobonus_db, autobonus);
  3802. if( script )
  3803. {
  3804. int j;
  3805. ARR_FIND( 0, EQI_MAX, j, sd->equip_index[j] >= 0 && sd->status.inventory[sd->equip_index[j]].equip == pos );
  3806. if( j < EQI_MAX ) {
  3807. //Single item autobonus
  3808. current_equip_item_index = sd->equip_index[j];
  3809. current_equip_combo_pos = 0;
  3810. } else {
  3811. //Combo autobonus
  3812. current_equip_item_index = -1;
  3813. current_equip_combo_pos = pos;
  3814. }
  3815. run_script(script,0,sd->bl.id,0);
  3816. }
  3817. }
  3818. void script_add_autobonus(const char *autobonus)
  3819. {
  3820. if( strdb_get(autobonus_db, autobonus) == NULL )
  3821. {
  3822. struct script_code *script = parse_script(autobonus, "autobonus", 0, 0);
  3823. if( script )
  3824. strdb_put(autobonus_db, autobonus, script);
  3825. }
  3826. }
  3827. /// resets a temporary character array variable to given value
  3828. void script_cleararray_pc(struct map_session_data* sd, const char* varname, void* value)
  3829. {
  3830. struct script_array *sa = NULL;
  3831. struct reg_db *src = NULL;
  3832. unsigned int i, *list = NULL, size = 0;
  3833. int key;
  3834. key = add_str(varname);
  3835. if( !(src = script_array_src(NULL,sd,varname,NULL) ) )
  3836. return;
  3837. if( value )
  3838. script_array_ensure_zero(NULL,sd,reference_uid(key,0), NULL);
  3839. if( !(sa = idb_get(src->arrays, key)) ) /* non-existent array, nothing to empty */
  3840. return;
  3841. size = sa->size;
  3842. list = script_array_cpy_list(sa);
  3843. for(i = 0; i < size; i++) {
  3844. set_reg(NULL,sd,reference_uid(key, list[i]),varname,value,NULL);
  3845. }
  3846. }
  3847. /// sets a temporary character array variable element idx to given value
  3848. /// @param refcache Pointer to an int variable, which keeps a copy of the reference to varname and must be initialized to 0. Can be NULL if only one element is set.
  3849. void script_setarray_pc(struct map_session_data* sd, const char* varname, uint32 idx, void* value, int* refcache)
  3850. {
  3851. int key;
  3852. if( idx >= SCRIPT_MAX_ARRAYSIZE ) {
  3853. ShowError("script_setarray_pc: Variable '%s' has invalid index '%u' (char_id=%d).\n", varname, idx, sd->status.char_id);
  3854. return;
  3855. }
  3856. key = ( refcache && refcache[0] ) ? refcache[0] : add_str(varname);
  3857. set_reg(NULL,sd,reference_uid(key, idx),varname,value,NULL);
  3858. if( refcache ) {
  3859. // save to avoid repeated script->add_str calls
  3860. refcache[0] = key;
  3861. }
  3862. }
  3863. /**
  3864. * Clears persistent variables from memory
  3865. **/
  3866. int script_reg_destroy(DBKey key, DBData *data, va_list ap)
  3867. {
  3868. struct script_reg_state *src;
  3869. if( data->type != DB_DATA_PTR ) // got no need for those!
  3870. return 0;
  3871. src = db_data2ptr(data);
  3872. if( src->type ) {
  3873. struct script_reg_str *p = (struct script_reg_str *)src;
  3874. if( p->value )
  3875. aFree(p->value);
  3876. ers_free(str_reg_ers,p);
  3877. } else {
  3878. ers_free(num_reg_ers,(struct script_reg_num*)src);
  3879. }
  3880. return 0;
  3881. }
  3882. /**
  3883. * Clears a single persistent variable
  3884. **/
  3885. void script_reg_destroy_single(struct map_session_data *sd, int64 reg, struct script_reg_state *data)
  3886. {
  3887. i64db_remove(sd->regs.vars, reg);
  3888. if( data->type ) {
  3889. struct script_reg_str *p = (struct script_reg_str*)data;
  3890. if( p->value )
  3891. aFree(p->value);
  3892. ers_free(str_reg_ers,p);
  3893. } else {
  3894. ers_free(num_reg_ers,(struct script_reg_num*)data);
  3895. }
  3896. }
  3897. unsigned int *script_array_cpy_list(struct script_array *sa)
  3898. {
  3899. if( sa->size > generic_ui_array_size )
  3900. script_generic_ui_array_expand(sa->size);
  3901. memcpy(generic_ui_array, sa->members, sizeof(unsigned int)*sa->size);
  3902. return generic_ui_array;
  3903. }
  3904. void script_generic_ui_array_expand (unsigned int plus)
  3905. {
  3906. generic_ui_array_size += plus + 100;
  3907. RECREATE(generic_ui_array, unsigned int, generic_ui_array_size);
  3908. }
  3909. int buildin_query_sql_sub(struct script_state *st, Sql *handle);
  3910. #ifdef BETA_THREAD_TEST
  3911. /* used to receive items the queryThread has already processed */
  3912. int queryThread_timer(int tid, unsigned int tick, int id, intptr_t data) {
  3913. int i, cursor = 0;
  3914. bool allOk = true;
  3915. EnterSpinLock(&queryThreadLock);
  3916. for( i = 0; i < queryThreadData.count; i++ ) {
  3917. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3918. if( !entry->ok ) {
  3919. allOk = false;
  3920. continue;
  3921. }
  3922. run_script_main(entry->st);
  3923. entry->st = NULL;/* empty entries */
  3924. aFree(entry);
  3925. queryThreadData.entry[i] = NULL;
  3926. }
  3927. if( allOk ) {
  3928. /* cancel the repeating timer -- it'll re-create itself when necessary, dont need to remain looping */
  3929. delete_timer(queryThreadData.timer, queryThread_timer);
  3930. queryThreadData.timer = INVALID_TIMER;
  3931. }
  3932. /* now lets clear the mess. */
  3933. for( i = 0; i < queryThreadData.count; i++ ) {
  3934. struct queryThreadEntry *entry = queryThreadData.entry[i];
  3935. if( entry == NULL )
  3936. continue;/* entry on hold */
  3937. /* move */
  3938. memmove(&queryThreadData.entry[cursor], &queryThreadData.entry[i], sizeof(struct queryThreadEntry*));
  3939. cursor++;
  3940. }
  3941. queryThreadData.count = cursor;
  3942. LeaveSpinLock(&queryThreadLock);
  3943. return 0;
  3944. }
  3945. void queryThread_add(struct script_state *st, bool type) {
  3946. int idx = 0;
  3947. struct queryThreadEntry* entry = NULL;
  3948. EnterSpinLock(&queryThreadLock);
  3949. if( queryThreadData.count++ != 0 )
  3950. RECREATE(queryThreadData.entry, struct queryThreadEntry* , queryThreadData.count);
  3951. idx = queryThreadData.count-1;
  3952. CREATE(queryThreadData.entry[idx],struct queryThreadEntry,1);
  3953. entry = queryThreadData.entry[idx];
  3954. entry->st = st;
  3955. entry->ok = false;
  3956. entry->type = type;
  3957. if( queryThreadData.timer == INVALID_TIMER ) { /* start the receiver timer */
  3958. queryThreadData.timer = add_timer_interval(gettick() + 100, queryThread_timer, 0, 0, 100);
  3959. }
  3960. LeaveSpinLock(&queryThreadLock);
  3961. /* unlock the queryThread */
  3962. racond_signal(queryThreadCond);
  3963. }
  3964. /* adds a new log to the queue */
  3965. void queryThread_log(char * entry, int length) {
  3966. int idx = logThreadData.count;
  3967. EnterSpinLock(&queryThreadLock);
  3968. if( logThreadData.count++ != 0 )
  3969. RECREATE(logThreadData.entry, char* , logThreadData.count);
  3970. CREATE(logThreadData.entry[idx], char, length + 1 );
  3971. safestrncpy(logThreadData.entry[idx], entry, length + 1 );
  3972. LeaveSpinLock(&queryThreadLock);
  3973. /* unlock the queryThread */
  3974. racond_signal(queryThreadCond);
  3975. }
  3976. /* queryThread_main */
  3977. static void *queryThread_main(void *x) {
  3978. Sql *queryThread_handle = Sql_Malloc();
  3979. int i;
  3980. if ( SQL_ERROR == Sql_Connect(queryThread_handle, map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db) ){
  3981. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3982. map_server_id, map_server_pw, map_server_ip, map_server_port, map_server_db);
  3983. Sql_ShowDebug(queryThread_handle);
  3984. Sql_Free(queryThread_handle);
  3985. exit(EXIT_FAILURE);
  3986. }
  3987. if( strlen(default_codepage) > 0 )
  3988. if ( SQL_ERROR == Sql_SetEncoding(queryThread_handle, default_codepage) )
  3989. Sql_ShowDebug(queryThread_handle);
  3990. if( log_config.sql_logs ) {
  3991. logmysql_handle = Sql_Malloc();
  3992. if ( SQL_ERROR == Sql_Connect(logmysql_handle, log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db) ){
  3993. ShowError("Couldn't connect with uname='%s',passwd='%s',host='%s',port='%d',database='%s'\n",
  3994. log_db_id, log_db_pw, log_db_ip, log_db_port, log_db_db);
  3995. Sql_ShowDebug(logmysql_handle);
  3996. Sql_Free(logmysql_handle);
  3997. exit(EXIT_FAILURE);
  3998. }
  3999. if( strlen(default_codepage) > 0 )
  4000. if ( SQL_ERROR == Sql_SetEncoding(logmysql_handle, default_codepage) )
  4001. Sql_ShowDebug(logmysql_handle);
  4002. }
  4003. while( 1 ) {
  4004. if(queryThreadTerminate > 0)
  4005. break;
  4006. EnterSpinLock(&queryThreadLock);
  4007. /* mess with queryThreadData within the lock */
  4008. for( i = 0; i < queryThreadData.count; i++ ) {
  4009. struct queryThreadEntry *entry = queryThreadData.entry[i];
  4010. if( entry->ok )
  4011. continue;
  4012. else if ( !entry->st || !entry->st->stack ) {
  4013. entry->ok = true;/* dispose */
  4014. continue;
  4015. }
  4016. buildin_query_sql_sub(entry->st, entry->type ? logmysql_handle : queryThread_handle);
  4017. entry->ok = true;/* we're done with this */
  4018. }
  4019. /* also check for any logs in need to be sent */
  4020. if( log_config.sql_logs ) {
  4021. for( i = 0; i < logThreadData.count; i++ ) {
  4022. if( SQL_ERROR == Sql_Query(logmysql_handle, logThreadData.entry[i]) )
  4023. Sql_ShowDebug(logmysql_handle);
  4024. aFree(logThreadData.entry[i]);
  4025. }
  4026. logThreadData.count = 0;
  4027. }
  4028. LeaveSpinLock(&queryThreadLock);
  4029. ramutex_lock( queryThreadMutex );
  4030. racond_wait( queryThreadCond, queryThreadMutex, -1 );
  4031. ramutex_unlock( queryThreadMutex );
  4032. }
  4033. Sql_Free(queryThread_handle);
  4034. if( log_config.sql_logs ) {
  4035. Sql_Free(logmysql_handle);
  4036. }
  4037. return NULL;
  4038. }
  4039. #endif
  4040. /*==========================================
  4041. * Destructor
  4042. *------------------------------------------*/
  4043. void do_final_script() {
  4044. int i;
  4045. DBIterator *iter;
  4046. struct script_state *st;
  4047. #ifdef DEBUG_HASH
  4048. if (battle_config.etc_log)
  4049. {
  4050. FILE *fp = fopen("hash_dump.txt","wt");
  4051. if(fp) {
  4052. int count[SCRIPT_HASH_SIZE];
  4053. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  4054. int n=0;
  4055. int min=INT_MAX,max=0,zero=0;
  4056. double mean=0.0f;
  4057. double median=0.0f;
  4058. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  4059. memset(count, 0, sizeof(count));
  4060. fprintf(fp,"num : hash : data_name\n");
  4061. fprintf(fp,"---------------------------------------------------------------\n");
  4062. for(i=LABEL_START; i<str_num; i++) {
  4063. unsigned int h = calc_hash(get_str(i));
  4064. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  4065. ++count[h];
  4066. }
  4067. fprintf(fp,"--------------------\n\n");
  4068. memset(count2, 0, sizeof(count2));
  4069. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  4070. fprintf(fp," hash %3d = %d\n",i,count[i]);
  4071. if(min > count[i])
  4072. min = count[i]; // minimun count of collision
  4073. if(max < count[i])
  4074. max = count[i]; // maximun count of collision
  4075. if(count[i] == 0)
  4076. zero++;
  4077. ++count2[count[i]];
  4078. }
  4079. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  4080. for( i=min; i <= max; ++i ){
  4081. fprintf(fp," %5d : %7d\n",i,count2[i]);
  4082. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  4083. }
  4084. for( i=min; i <= max; ++i ){
  4085. n += count2[i];
  4086. if( n*2 >= SCRIPT_HASH_SIZE )
  4087. {
  4088. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  4089. median = (i+i+1)/2.0f;
  4090. else
  4091. median = i;
  4092. break;
  4093. }
  4094. }
  4095. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  4096. fclose(fp);
  4097. }
  4098. }
  4099. #endif
  4100. mapreg_final();
  4101. db_destroy(scriptlabel_db);
  4102. userfunc_db->destroy(userfunc_db, db_script_free_code_sub);
  4103. autobonus_db->destroy(autobonus_db, db_script_free_code_sub);
  4104. ers_destroy(array_ers);
  4105. if (generic_ui_array)
  4106. aFree(generic_ui_array);
  4107. iter = db_iterator(st_db);
  4108. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4109. script_free_state(st);
  4110. dbi_destroy(iter);
  4111. if (str_data)
  4112. aFree(str_data);
  4113. if (str_buf)
  4114. aFree(str_buf);
  4115. for( i = 0; i < atcmd_binding_count; i++ ) {
  4116. aFree(atcmd_binding[i]);
  4117. }
  4118. if( atcmd_binding_count != 0 )
  4119. aFree(atcmd_binding);
  4120. ers_destroy(st_ers);
  4121. ers_destroy(stack_ers);
  4122. db_destroy(st_db);
  4123. #ifdef BETA_THREAD_TEST
  4124. /* QueryThread */
  4125. InterlockedIncrement(&queryThreadTerminate);
  4126. racond_signal(queryThreadCond);
  4127. rathread_wait(queryThread, NULL);
  4128. // Destroy cond var and mutex.
  4129. racond_destroy( queryThreadCond );
  4130. ramutex_destroy( queryThreadMutex );
  4131. /* Clear missing vars */
  4132. for( i = 0; i < queryThreadData.count; i++ ) {
  4133. aFree(queryThreadData.entry[i]);
  4134. }
  4135. aFree(queryThreadData.entry);
  4136. for( i = 0; i < logThreadData.count; i++ ) {
  4137. aFree(logThreadData.entry[i]);
  4138. }
  4139. aFree(logThreadData.entry);
  4140. #endif
  4141. }
  4142. /*==========================================
  4143. * Initialization
  4144. *------------------------------------------*/
  4145. void do_init_script(void) {
  4146. st_db = idb_alloc(DB_OPT_BASE);
  4147. userfunc_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4148. scriptlabel_db = strdb_alloc(DB_OPT_DUP_KEY,50);
  4149. autobonus_db = strdb_alloc(DB_OPT_DUP_KEY,0);
  4150. st_ers = ers_new(sizeof(struct script_state), "script.c::st_ers", ERS_CACHE_OPTIONS);
  4151. stack_ers = ers_new(sizeof(struct script_stack), "script.c::script_stack", ERS_OPT_FLEX_CHUNK);
  4152. array_ers = ers_new(sizeof(struct script_array), "script.c:array_ers", ERS_OPT_CLEAN|ERS_OPT_CLEAR); // TODO: This value is not yet in the enum
  4153. ers_chunk_size(st_ers, 10);
  4154. ers_chunk_size(stack_ers, 10);
  4155. active_scripts = 0;
  4156. next_id = 0;
  4157. mapreg_init();
  4158. #ifdef BETA_THREAD_TEST
  4159. CREATE(queryThreadData.entry, struct queryThreadEntry*, 1);
  4160. queryThreadData.count = 0;
  4161. CREATE(logThreadData.entry, char *, 1);
  4162. logThreadData.count = 0;
  4163. /* QueryThread Start */
  4164. InitializeSpinLock(&queryThreadLock);
  4165. queryThreadData.timer = INVALID_TIMER;
  4166. queryThreadTerminate = 0;
  4167. queryThreadMutex = ramutex_create();
  4168. queryThreadCond = racond_create();
  4169. queryThread = rathread_create(queryThread_main, NULL);
  4170. if(queryThread == NULL){
  4171. ShowFatalError("do_init_script: cannot spawn Query Thread.\n");
  4172. exit(EXIT_FAILURE);
  4173. }
  4174. add_timer_func_list(queryThread_timer, "queryThread_timer");
  4175. #endif
  4176. }
  4177. void script_reload(void) {
  4178. int i;
  4179. DBIterator *iter;
  4180. struct script_state *st;
  4181. #ifdef BETA_THREAD_TEST
  4182. /* we're reloading so any queries undergoing should be...exterminated. */
  4183. EnterSpinLock(&queryThreadLock);
  4184. for( i = 0; i < queryThreadData.count; i++ ) {
  4185. aFree(queryThreadData.entry[i]);
  4186. }
  4187. queryThreadData.count = 0;
  4188. if( queryThreadData.timer != INVALID_TIMER ) {
  4189. delete_timer(queryThreadData.timer, queryThread_timer);
  4190. queryThreadData.timer = INVALID_TIMER;
  4191. }
  4192. LeaveSpinLock(&queryThreadLock);
  4193. #endif
  4194. userfunc_db->clear(userfunc_db, db_script_free_code_sub);
  4195. db_clear(scriptlabel_db);
  4196. // @commands (script based)
  4197. // Clear bindings
  4198. for( i = 0; i < atcmd_binding_count; i++ ) {
  4199. aFree(atcmd_binding[i]);
  4200. }
  4201. if( atcmd_binding_count != 0 )
  4202. aFree(atcmd_binding);
  4203. atcmd_binding_count = 0;
  4204. iter = db_iterator(st_db);
  4205. for(st = dbi_first(iter); dbi_exists(iter); st = dbi_next(iter))
  4206. script_free_state(st);
  4207. dbi_destroy(iter);
  4208. db_clear(st_db);
  4209. mapreg_reload();
  4210. }
  4211. //-----------------------------------------------------------------------------
  4212. // buildin functions
  4213. //
  4214. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  4215. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  4216. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  4217. /////////////////////////////////////////////////////////////////////
  4218. // NPC interaction
  4219. //
  4220. /// Appends a message to the npc dialog.
  4221. /// If a dialog doesn't exist yet, one is created.
  4222. ///
  4223. /// mes "<message>";
  4224. BUILDIN_FUNC(mes)
  4225. {
  4226. TBL_PC* sd = script_rid2sd(st);
  4227. if( sd == NULL )
  4228. return SCRIPT_CMD_SUCCESS;
  4229. if( !script_hasdata(st, 3) )
  4230. {// only a single line detected in the script
  4231. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4232. }
  4233. else
  4234. {// parse multiple lines as they exist
  4235. int i;
  4236. for( i = 2; script_hasdata(st, i); i++ )
  4237. {
  4238. // send the message to the client
  4239. clif_scriptmes(sd, st->oid, script_getstr(st, i));
  4240. }
  4241. }
  4242. st->mes_active = 1; // Invoking character has a NPC dialog box open.
  4243. return SCRIPT_CMD_SUCCESS;
  4244. }
  4245. /// Displays the button 'next' in the npc dialog.
  4246. /// The dialog text is cleared and the script continues when the button is pressed.
  4247. ///
  4248. /// next;
  4249. BUILDIN_FUNC(next)
  4250. {
  4251. TBL_PC* sd;
  4252. sd = script_rid2sd(st);
  4253. if( sd == NULL )
  4254. return SCRIPT_CMD_SUCCESS;
  4255. #ifdef SECURE_NPCTIMEOUT
  4256. sd->npc_idle_type = NPCT_WAIT;
  4257. #endif
  4258. st->state = STOP;
  4259. clif_scriptnext(sd, st->oid);
  4260. return SCRIPT_CMD_SUCCESS;
  4261. }
  4262. /// Ends the script and displays the button 'close' on the npc dialog.
  4263. /// The dialog is closed when the button is pressed.
  4264. ///
  4265. /// close;
  4266. BUILDIN_FUNC(close)
  4267. {
  4268. TBL_PC* sd;
  4269. sd = script_rid2sd(st);
  4270. if( sd == NULL )
  4271. return SCRIPT_CMD_SUCCESS;
  4272. if( !st->mes_active ) {
  4273. TBL_NPC* nd = map_id2nd(st->oid);
  4274. st->state = END; // Keep backwards compatibility.
  4275. ShowWarning("Incorrect use of 'close' command! (source:%s / path:%s)\n",nd?nd->name:"Unknown",nd?nd->path:"Unknown");
  4276. } else {
  4277. st->state = CLOSE;
  4278. st->mes_active = 0;
  4279. }
  4280. clif_scriptclose(sd, st->oid);
  4281. return SCRIPT_CMD_SUCCESS;
  4282. }
  4283. /// Displays the button 'close' on the npc dialog.
  4284. /// The dialog is closed and the script continues when the button is pressed.
  4285. ///
  4286. /// close2;
  4287. BUILDIN_FUNC(close2)
  4288. {
  4289. TBL_PC* sd;
  4290. sd = script_rid2sd(st);
  4291. if( sd == NULL )
  4292. return SCRIPT_CMD_SUCCESS;
  4293. st->state = STOP;
  4294. if( st->mes_active )
  4295. st->mes_active = 0;
  4296. clif_scriptclose(sd, st->oid);
  4297. return SCRIPT_CMD_SUCCESS;
  4298. }
  4299. /// Counts the number of valid and total number of options in 'str'
  4300. /// If max_count > 0 the counting stops when that valid option is reached
  4301. /// total is incremented for each option (NULL is supported)
  4302. static int menu_countoptions(const char* str, int max_count, int* total)
  4303. {
  4304. int count = 0;
  4305. int bogus_total;
  4306. if( total == NULL )
  4307. total = &bogus_total;
  4308. ++(*total);
  4309. // initial empty options
  4310. while( *str == ':' )
  4311. {
  4312. ++str;
  4313. ++(*total);
  4314. }
  4315. // count menu options
  4316. while( *str != '\0' )
  4317. {
  4318. ++count;
  4319. --max_count;
  4320. if( max_count == 0 )
  4321. break;
  4322. while( *str != ':' && *str != '\0' )
  4323. ++str;
  4324. while( *str == ':' )
  4325. {
  4326. ++str;
  4327. ++(*total);
  4328. }
  4329. }
  4330. return count;
  4331. }
  4332. /// Displays a menu with options and goes to the target label.
  4333. /// The script is stopped if cancel is pressed.
  4334. /// Options with no text are not displayed in the client.
  4335. ///
  4336. /// Options can be grouped together, separated by the character ':' in the text:
  4337. /// ex: menu "A:B:C",L_target;
  4338. /// All these options go to the specified target label.
  4339. ///
  4340. /// The index of the selected option is put in the variable @menu.
  4341. /// Indexes start with 1 and are consistent with grouped and empty options.
  4342. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4343. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4344. ///
  4345. /// NOTE: the client closes the npc dialog when cancel is pressed
  4346. ///
  4347. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4348. BUILDIN_FUNC(menu)
  4349. {
  4350. int i;
  4351. const char* text;
  4352. TBL_PC* sd;
  4353. sd = script_rid2sd(st);
  4354. if( sd == NULL )
  4355. return SCRIPT_CMD_SUCCESS;
  4356. #ifdef SECURE_NPCTIMEOUT
  4357. sd->npc_idle_type = NPCT_MENU;
  4358. #endif
  4359. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4360. if( sd->state.menu_or_input == 0 )
  4361. {
  4362. struct StringBuf buf;
  4363. if( script_lastdata(st) % 2 == 0 )
  4364. {// argument count is not even (1st argument is at index 2)
  4365. ShowError("buildin_menu: Illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4366. st->state = END;
  4367. return SCRIPT_CMD_FAILURE;
  4368. }
  4369. StringBuf_Init(&buf);
  4370. sd->npc_menu = 0;
  4371. for( i = 2; i < script_lastdata(st); i += 2 )
  4372. {
  4373. struct script_data* data;
  4374. // menu options
  4375. text = script_getstr(st, i);
  4376. // target label
  4377. data = script_getdata(st, i+1);
  4378. if( !data_islabel(data) )
  4379. {// not a label
  4380. StringBuf_Destroy(&buf);
  4381. ShowError("buildin_menu: Argument #%d (from 1) is not a label or label not found.\n", i);
  4382. script_reportdata(data);
  4383. st->state = END;
  4384. return SCRIPT_CMD_FAILURE;
  4385. }
  4386. // append option(s)
  4387. if( text[0] == '\0' )
  4388. continue;// empty string, ignore
  4389. if( sd->npc_menu > 0 )
  4390. StringBuf_AppendStr(&buf, ":");
  4391. StringBuf_AppendStr(&buf, text);
  4392. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4393. }
  4394. st->state = RERUNLINE;
  4395. sd->state.menu_or_input = 1;
  4396. /**
  4397. * menus beyond this length crash the client (see bugreport:6402)
  4398. **/
  4399. if( StringBuf_Length(&buf) >= 2047 ) {
  4400. struct npc_data * nd = map_id2nd(st->oid);
  4401. char* menu;
  4402. CREATE(menu, char, 2048);
  4403. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4404. ShowWarning("buildin_menu: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4405. clif_scriptmenu(sd, st->oid, menu);
  4406. aFree(menu);
  4407. } else
  4408. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4409. StringBuf_Destroy(&buf);
  4410. if( sd->npc_menu >= 0xff )
  4411. {// client supports only up to 254 entries; 0 is not used and 255 is reserved for cancel; excess entries are displayed but cause 'uint8' overflow
  4412. ShowWarning("buildin_menu: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4413. script_reportsrc(st);
  4414. }
  4415. }
  4416. else if( sd->npc_menu == 0xff )
  4417. {// Cancel was pressed
  4418. sd->state.menu_or_input = 0;
  4419. st->state = END;
  4420. }
  4421. else
  4422. {// goto target label
  4423. int menu = 0;
  4424. sd->state.menu_or_input = 0;
  4425. if( sd->npc_menu <= 0 )
  4426. {
  4427. ShowDebug("buildin_menu: Unexpected selection (%d)\n", sd->npc_menu);
  4428. st->state = END;
  4429. return SCRIPT_CMD_FAILURE;
  4430. }
  4431. // get target label
  4432. for( i = 2; i < script_lastdata(st); i += 2 )
  4433. {
  4434. text = script_getstr(st, i);
  4435. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4436. if( sd->npc_menu <= 0 )
  4437. break;// entry found
  4438. }
  4439. if( sd->npc_menu > 0 )
  4440. {// Invalid selection
  4441. ShowDebug("buildin_menu: Selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4442. st->state = END;
  4443. return SCRIPT_CMD_FAILURE;
  4444. }
  4445. if( !data_islabel(script_getdata(st, i + 1)) )
  4446. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4447. ShowError("buildin_menu: Unexpected data in label argument\n");
  4448. script_reportdata(script_getdata(st, i + 1));
  4449. st->state = END;
  4450. return SCRIPT_CMD_FAILURE;
  4451. }
  4452. pc_setreg(sd, add_str("@menu"), menu);
  4453. st->pos = script_getnum(st, i + 1);
  4454. st->state = GOTO;
  4455. }
  4456. return SCRIPT_CMD_SUCCESS;
  4457. }
  4458. /// Displays a menu with options and returns the selected option.
  4459. /// Behaves like 'menu' without the target labels.
  4460. ///
  4461. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4462. ///
  4463. /// @see menu
  4464. BUILDIN_FUNC(select)
  4465. {
  4466. int i;
  4467. const char* text;
  4468. TBL_PC* sd;
  4469. sd = script_rid2sd(st);
  4470. if( sd == NULL )
  4471. return SCRIPT_CMD_SUCCESS;
  4472. #ifdef SECURE_NPCTIMEOUT
  4473. sd->npc_idle_type = NPCT_MENU;
  4474. #endif
  4475. if( sd->state.menu_or_input == 0 ) {
  4476. struct StringBuf buf;
  4477. StringBuf_Init(&buf);
  4478. sd->npc_menu = 0;
  4479. for( i = 2; i <= script_lastdata(st); ++i ) {
  4480. text = script_getstr(st, i);
  4481. if( sd->npc_menu > 0 )
  4482. StringBuf_AppendStr(&buf, ":");
  4483. StringBuf_AppendStr(&buf, text);
  4484. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4485. }
  4486. st->state = RERUNLINE;
  4487. sd->state.menu_or_input = 1;
  4488. /**
  4489. * menus beyond this length crash the client (see bugreport:6402)
  4490. **/
  4491. if( StringBuf_Length(&buf) >= 2047 ) {
  4492. struct npc_data * nd = map_id2nd(st->oid);
  4493. char* menu;
  4494. CREATE(menu, char, 2048);
  4495. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4496. ShowWarning("buildin_select: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4497. clif_scriptmenu(sd, st->oid, menu);
  4498. aFree(menu);
  4499. } else
  4500. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4501. StringBuf_Destroy(&buf);
  4502. if( sd->npc_menu >= 0xff ) {
  4503. ShowWarning("buildin_select: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4504. script_reportsrc(st);
  4505. }
  4506. } else if( sd->npc_menu == 0xff ) {// Cancel was pressed
  4507. sd->state.menu_or_input = 0;
  4508. st->state = END;
  4509. } else {// return selected option
  4510. int menu = 0;
  4511. sd->state.menu_or_input = 0;
  4512. for( i = 2; i <= script_lastdata(st); ++i ) {
  4513. text = script_getstr(st, i);
  4514. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4515. if( sd->npc_menu <= 0 )
  4516. break;// entry found
  4517. }
  4518. pc_setreg(sd, add_str("@menu"), menu);
  4519. script_pushint(st, menu);
  4520. st->state = RUN;
  4521. }
  4522. return SCRIPT_CMD_SUCCESS;
  4523. }
  4524. /// Displays a menu with options and returns the selected option.
  4525. /// Behaves like 'menu' without the target labels, except when cancel is
  4526. /// pressed.
  4527. /// When cancel is pressed, the script continues and 255 is returned.
  4528. ///
  4529. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4530. ///
  4531. /// @see menu
  4532. BUILDIN_FUNC(prompt)
  4533. {
  4534. int i;
  4535. const char *text;
  4536. TBL_PC* sd;
  4537. sd = script_rid2sd(st);
  4538. if( sd == NULL )
  4539. return SCRIPT_CMD_SUCCESS;
  4540. #ifdef SECURE_NPCTIMEOUT
  4541. sd->npc_idle_type = NPCT_MENU;
  4542. #endif
  4543. if( sd->state.menu_or_input == 0 )
  4544. {
  4545. struct StringBuf buf;
  4546. StringBuf_Init(&buf);
  4547. sd->npc_menu = 0;
  4548. for( i = 2; i <= script_lastdata(st); ++i )
  4549. {
  4550. text = script_getstr(st, i);
  4551. if( sd->npc_menu > 0 )
  4552. StringBuf_AppendStr(&buf, ":");
  4553. StringBuf_AppendStr(&buf, text);
  4554. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4555. }
  4556. st->state = RERUNLINE;
  4557. sd->state.menu_or_input = 1;
  4558. /**
  4559. * menus beyond this length crash the client (see bugreport:6402)
  4560. **/
  4561. if( StringBuf_Length(&buf) >= 2047 ) {
  4562. struct npc_data * nd = map_id2nd(st->oid);
  4563. char* menu;
  4564. CREATE(menu, char, 2048);
  4565. safestrncpy(menu, StringBuf_Value(&buf), 2047);
  4566. ShowWarning("buildin_prompt: NPC Menu too long! (source:%s / length:%d)\n",nd?nd->name:"Unknown",StringBuf_Length(&buf));
  4567. clif_scriptmenu(sd, st->oid, menu);
  4568. aFree(menu);
  4569. } else
  4570. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4571. StringBuf_Destroy(&buf);
  4572. if( sd->npc_menu >= 0xff )
  4573. {
  4574. ShowWarning("buildin_prompt: Too many options specified (current=%d, max=254).\n", sd->npc_menu);
  4575. script_reportsrc(st);
  4576. }
  4577. }
  4578. else if( sd->npc_menu == 0xff )
  4579. {// Cancel was pressed
  4580. sd->state.menu_or_input = 0;
  4581. pc_setreg(sd, add_str("@menu"), 0xff);
  4582. script_pushint(st, 0xff);
  4583. st->state = RUN;
  4584. }
  4585. else
  4586. {// return selected option
  4587. int menu = 0;
  4588. sd->state.menu_or_input = 0;
  4589. for( i = 2; i <= script_lastdata(st); ++i )
  4590. {
  4591. text = script_getstr(st, i);
  4592. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4593. if( sd->npc_menu <= 0 )
  4594. break;// entry found
  4595. }
  4596. pc_setreg(sd, add_str("@menu"), menu);
  4597. script_pushint(st, menu);
  4598. st->state = RUN;
  4599. }
  4600. return SCRIPT_CMD_SUCCESS;
  4601. }
  4602. /////////////////////////////////////////////////////////////////////
  4603. // ...
  4604. //
  4605. /// Jumps to the target script label.
  4606. ///
  4607. /// goto <label>;
  4608. BUILDIN_FUNC(goto)
  4609. {
  4610. if( !data_islabel(script_getdata(st,2)) )
  4611. {
  4612. ShowError("buildin_goto: Not a label\n");
  4613. script_reportdata(script_getdata(st,2));
  4614. st->state = END;
  4615. return SCRIPT_CMD_FAILURE;
  4616. }
  4617. st->pos = script_getnum(st,2);
  4618. st->state = GOTO;
  4619. return SCRIPT_CMD_SUCCESS;
  4620. }
  4621. /*==========================================
  4622. * user-defined function call
  4623. *------------------------------------------*/
  4624. BUILDIN_FUNC(callfunc)
  4625. {
  4626. int i, j;
  4627. struct script_retinfo* ri;
  4628. struct script_code* scr;
  4629. const char* str = script_getstr(st,2);
  4630. struct reg_db *ref = NULL;
  4631. scr = (struct script_code*)strdb_get(userfunc_db, str);
  4632. if(!scr) {
  4633. ShowError("buildin_callfunc: Function not found! [%s]\n", str);
  4634. st->state = END;
  4635. return SCRIPT_CMD_FAILURE;
  4636. }
  4637. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 2);
  4638. ref[0].vars = st->stack->scope.vars;
  4639. if (!st->stack->scope.arrays)
  4640. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4641. ref[0].arrays = st->stack->scope.arrays;
  4642. ref[1].vars = st->script->local.vars;
  4643. if (!st->script->local.arrays)
  4644. st->script->local.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4645. ref[1].arrays = st->script->local.arrays;
  4646. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4647. struct script_data* data = push_copy(st->stack,i);
  4648. if (data_isreference(data) && !data->ref) {
  4649. const char* name = reference_getname(data);
  4650. if (name[0] == '.')
  4651. data->ref = (name[1] == '@' ? &ref[0] : &ref[1]);
  4652. }
  4653. }
  4654. CREATE(ri, struct script_retinfo, 1);
  4655. ri->script = st->script; // script code
  4656. ri->scope.vars = st->stack->scope.vars; // scope variables
  4657. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4658. ri->pos = st->pos; // script location
  4659. ri->nargs = j; // argument count
  4660. ri->defsp = st->stack->defsp; // default stack pointer
  4661. push_retinfo(st->stack, ri, ref);
  4662. st->pos = 0;
  4663. st->script = scr;
  4664. st->stack->defsp = st->stack->sp;
  4665. st->state = GOTO;
  4666. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4667. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4668. if (!st->script->local.vars)
  4669. st->script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4670. return SCRIPT_CMD_SUCCESS;
  4671. }
  4672. /*==========================================
  4673. * subroutine call
  4674. *------------------------------------------*/
  4675. BUILDIN_FUNC(callsub)
  4676. {
  4677. int i,j;
  4678. struct script_retinfo* ri;
  4679. int pos = script_getnum(st,2);
  4680. struct reg_db *ref = NULL;
  4681. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) ) {
  4682. ShowError("buildin_callsub: Argument is not a label\n");
  4683. script_reportdata(script_getdata(st,2));
  4684. st->state = END;
  4685. return SCRIPT_CMD_FAILURE;
  4686. }
  4687. ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4688. ref[0].vars = st->stack->scope.vars;
  4689. if (!st->stack->scope.arrays)
  4690. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE); // TODO: Can this happen? when?
  4691. ref[0].arrays = st->stack->scope.arrays;
  4692. for(i = st->start+3, j = 0; i < st->end; i++, j++) {
  4693. struct script_data* data = push_copy(st->stack,i);
  4694. if (data_isreference(data) && !data->ref) {
  4695. const char* name = reference_getname(data);
  4696. if (name[0] == '.' && name[1] == '@')
  4697. data->ref = &ref[0];
  4698. }
  4699. }
  4700. CREATE(ri, struct script_retinfo, 1);
  4701. ri->script = st->script; // script code
  4702. ri->scope.vars = st->stack->scope.vars; // scope variables
  4703. ri->scope.arrays = st->stack->scope.arrays; // scope arrays
  4704. ri->pos = st->pos; // script location
  4705. ri->nargs = j; // argument count
  4706. ri->defsp = st->stack->defsp; // default stack pointer
  4707. push_retinfo(st->stack, ri, ref);
  4708. st->pos = pos;
  4709. st->stack->defsp = st->stack->sp;
  4710. st->state = GOTO;
  4711. st->stack->scope.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  4712. st->stack->scope.arrays = idb_alloc(DB_OPT_BASE);
  4713. return SCRIPT_CMD_SUCCESS;
  4714. }
  4715. /// Retrieves an argument provided to callfunc/callsub.
  4716. /// If the argument doesn't exist
  4717. ///
  4718. /// getarg(<index>{,<default_value>}) -> <value>
  4719. BUILDIN_FUNC(getarg)
  4720. {
  4721. struct script_retinfo* ri;
  4722. int idx;
  4723. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4724. {
  4725. ShowError("buildin_getarg: No callfunc or callsub!\n");
  4726. st->state = END;
  4727. return SCRIPT_CMD_FAILURE;
  4728. }
  4729. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  4730. idx = script_getnum(st,2);
  4731. if( idx >= 0 && idx < ri->nargs )
  4732. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  4733. else if( script_hasdata(st,3) )
  4734. script_pushcopy(st, 3);
  4735. else
  4736. {
  4737. ShowError("buildin_getarg: Index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  4738. st->state = END;
  4739. return SCRIPT_CMD_FAILURE;
  4740. }
  4741. return SCRIPT_CMD_SUCCESS;
  4742. }
  4743. /// Returns from the current function, optionaly returning a value from the functions.
  4744. /// Don't use outside script functions.
  4745. ///
  4746. /// return;
  4747. /// return <value>;
  4748. BUILDIN_FUNC(return)
  4749. {
  4750. if( script_hasdata(st,2) )
  4751. {// return value
  4752. struct script_data* data;
  4753. script_pushcopy(st, 2);
  4754. data = script_getdatatop(st, -1);
  4755. if( data_isreference(data) ) {
  4756. const char* name = reference_getname(data);
  4757. if( name[0] == '.' && name[1] == '@' ) { // scope variable
  4758. if( !data->ref || data->ref->vars == st->stack->scope.vars )
  4759. get_val(st, data); // current scope, convert to value
  4760. if( data->ref && data->ref->vars == st->stack->stack_data[st->stack->defsp-1].u.ri->scope.vars )
  4761. data->ref = NULL; // Reference to the parent scope, remove reference pointer
  4762. } else if( name[0] == '.' && !data->ref ) { // script variable, link to current script
  4763. data->ref = (struct reg_db *)aCalloc(sizeof(struct reg_db), 1);
  4764. data->ref->vars = st->script->local.vars;
  4765. if (!st->script->local.arrays)
  4766. st->script->local.arrays = idb_alloc(DB_OPT_BASE);
  4767. data->ref->arrays = st->script->local.arrays;
  4768. }
  4769. }
  4770. }
  4771. else
  4772. {// no return value
  4773. script_pushnil(st);
  4774. }
  4775. st->state = RETFUNC;
  4776. return SCRIPT_CMD_SUCCESS;
  4777. }
  4778. /// Returns a random number from 0 to <range>-1.
  4779. /// Or returns a random number from <min> to <max>.
  4780. /// If <min> is greater than <max>, their numbers are switched.
  4781. /// rand(<range>) -> <int>
  4782. /// rand(<min>,<max>) -> <int>
  4783. BUILDIN_FUNC(rand)
  4784. {
  4785. int range;
  4786. int min;
  4787. if( script_hasdata(st,3) )
  4788. {// min,max
  4789. int max = script_getnum(st,3);
  4790. min = script_getnum(st,2);
  4791. if( max < min )
  4792. swap(min, max);
  4793. range = max - min + 1;
  4794. }
  4795. else
  4796. {// range
  4797. min = 0;
  4798. range = script_getnum(st,2);
  4799. }
  4800. if( range <= 1 )
  4801. script_pushint(st, min);
  4802. else
  4803. script_pushint(st, rnd()%range + min);
  4804. return SCRIPT_CMD_SUCCESS;
  4805. }
  4806. /*==========================================
  4807. * Warp sd to str,x,y or Random or SavePoint/Save
  4808. *------------------------------------------*/
  4809. BUILDIN_FUNC(warp)
  4810. {
  4811. int ret;
  4812. int x,y;
  4813. const char* str;
  4814. TBL_PC* sd;
  4815. sd = script_rid2sd(st);
  4816. if( sd == NULL )
  4817. return SCRIPT_CMD_SUCCESS;
  4818. str = script_getstr(st,2);
  4819. x = script_getnum(st,3);
  4820. y = script_getnum(st,4);
  4821. if(strcmp(str,"Random")==0)
  4822. ret = pc_randomwarp(sd,CLR_TELEPORT);
  4823. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  4824. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  4825. else
  4826. ret = pc_setpos(sd,mapindex_name2id(str),x,y,CLR_OUTSIGHT);
  4827. if( ret ) {
  4828. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  4829. return SCRIPT_CMD_FAILURE;
  4830. }
  4831. return SCRIPT_CMD_SUCCESS;
  4832. }
  4833. /*==========================================
  4834. * Warp a specified area
  4835. *------------------------------------------*/
  4836. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4837. {
  4838. int x2,y2,x3,y3;
  4839. unsigned int index;
  4840. index = va_arg(ap,unsigned int);
  4841. x2 = va_arg(ap,int);
  4842. y2 = va_arg(ap,int);
  4843. x3 = va_arg(ap,int);
  4844. y3 = va_arg(ap,int);
  4845. if(index == 0)
  4846. pc_randomwarp((TBL_PC *)bl,CLR_TELEPORT);
  4847. else if(x3 && y3) {
  4848. int max, tx, ty, j = 0;
  4849. // choose a suitable max number of attempts
  4850. if( (max = (y3-y2+1)*(x3-x2+1)*3) > 1000 )
  4851. max = 1000;
  4852. // find a suitable map cell
  4853. do {
  4854. tx = rnd()%(x3-x2+1)+x2;
  4855. ty = rnd()%(y3-y2+1)+y2;
  4856. j++;
  4857. } while( map_getcell(index,tx,ty,CELL_CHKNOPASS) && j < max );
  4858. pc_setpos((TBL_PC *)bl,index,tx,ty,CLR_OUTSIGHT);
  4859. }
  4860. else
  4861. pc_setpos((TBL_PC *)bl,index,x2,y2,CLR_OUTSIGHT);
  4862. return 0;
  4863. }
  4864. BUILDIN_FUNC(areawarp)
  4865. {
  4866. int16 m, x0,y0,x1,y1, x2,y2,x3=0,y3=0;
  4867. unsigned int index;
  4868. const char *str;
  4869. const char *mapname;
  4870. mapname = script_getstr(st,2);
  4871. x0 = script_getnum(st,3);
  4872. y0 = script_getnum(st,4);
  4873. x1 = script_getnum(st,5);
  4874. y1 = script_getnum(st,6);
  4875. str = script_getstr(st,7);
  4876. x2 = script_getnum(st,8);
  4877. y2 = script_getnum(st,9);
  4878. if( script_hasdata(st,10) && script_hasdata(st,11) ) { // Warp area to area
  4879. if( (x3 = script_getnum(st,10)) < 0 || (y3 = script_getnum(st,11)) < 0 ){
  4880. x3 = 0;
  4881. y3 = 0;
  4882. } else if( x3 && y3 ) {
  4883. // normalize x3/y3 coordinates
  4884. if( x3 < x2 ) swap(x3,x2);
  4885. if( y3 < y2 ) swap(y3,y2);
  4886. }
  4887. }
  4888. if( (m = map_mapname2mapid(mapname)) < 0 )
  4889. return SCRIPT_CMD_FAILURE;
  4890. if( strcmp(str,"Random") == 0 )
  4891. index = 0;
  4892. else if( !(index=mapindex_name2id(str)) )
  4893. return SCRIPT_CMD_FAILURE;
  4894. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1, BL_PC, index,x2,y2,x3,y3);
  4895. return SCRIPT_CMD_SUCCESS;
  4896. }
  4897. /*==========================================
  4898. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  4899. *------------------------------------------*/
  4900. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  4901. {
  4902. int hp, sp;
  4903. hp = va_arg(ap, int);
  4904. sp = va_arg(ap, int);
  4905. pc_percentheal((TBL_PC *)bl,hp,sp);
  4906. return 0;
  4907. }
  4908. BUILDIN_FUNC(areapercentheal)
  4909. {
  4910. int hp,sp,m;
  4911. const char *mapname;
  4912. int x0,y0,x1,y1;
  4913. mapname=script_getstr(st,2);
  4914. x0=script_getnum(st,3);
  4915. y0=script_getnum(st,4);
  4916. x1=script_getnum(st,5);
  4917. y1=script_getnum(st,6);
  4918. hp=script_getnum(st,7);
  4919. sp=script_getnum(st,8);
  4920. if( (m=map_mapname2mapid(mapname))< 0)
  4921. return SCRIPT_CMD_FAILURE;
  4922. map_foreachinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,BL_PC,hp,sp);
  4923. return SCRIPT_CMD_SUCCESS;
  4924. }
  4925. /*==========================================
  4926. * warpchar [LuzZza]
  4927. * Useful for warp one player from
  4928. * another player npc-session.
  4929. * Using: warpchar "mapname",x,y,Char_ID;
  4930. *------------------------------------------*/
  4931. BUILDIN_FUNC(warpchar)
  4932. {
  4933. int x,y,a;
  4934. const char *str;
  4935. TBL_PC *sd;
  4936. str=script_getstr(st,2);
  4937. x=script_getnum(st,3);
  4938. y=script_getnum(st,4);
  4939. a=script_getnum(st,5);
  4940. sd = map_charid2sd(a);
  4941. if( sd == NULL )
  4942. return SCRIPT_CMD_SUCCESS;
  4943. if(strcmp(str, "Random") == 0)
  4944. pc_randomwarp(sd, CLR_TELEPORT);
  4945. else
  4946. if(strcmp(str, "SavePoint") == 0)
  4947. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  4948. else
  4949. pc_setpos(sd, mapindex_name2id(str), x, y, CLR_TELEPORT);
  4950. return SCRIPT_CMD_SUCCESS;
  4951. }
  4952. /*==========================================
  4953. * Warpparty - [Fredzilla] [Paradox924X]
  4954. * Syntax: warpparty "to_mapname",x,y,Party_ID,{"from_mapname"};
  4955. * If 'from_mapname' is specified, only the party members on that map will be warped
  4956. *------------------------------------------*/
  4957. BUILDIN_FUNC(warpparty)
  4958. {
  4959. TBL_PC *sd = NULL;
  4960. TBL_PC *pl_sd;
  4961. struct party_data* p;
  4962. int type;
  4963. int mapindex;
  4964. int i;
  4965. const char* str = script_getstr(st,2);
  4966. int x = script_getnum(st,3);
  4967. int y = script_getnum(st,4);
  4968. int p_id = script_getnum(st,5);
  4969. const char* str2 = NULL;
  4970. if ( script_hasdata(st,6) )
  4971. str2 = script_getstr(st,6);
  4972. p = party_search(p_id);
  4973. if(!p)
  4974. return SCRIPT_CMD_SUCCESS;
  4975. type = ( strcmp(str,"Random")==0 ) ? 0
  4976. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4977. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4978. : ( strcmp(str,"Leader")==0 ) ? 3
  4979. : 4;
  4980. switch (type)
  4981. {
  4982. case 3:
  4983. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4984. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4985. return SCRIPT_CMD_FAILURE;
  4986. pl_sd = p->data[i].sd;
  4987. mapindex = pl_sd->mapindex;
  4988. x = pl_sd->bl.x;
  4989. y = pl_sd->bl.y;
  4990. break;
  4991. case 4:
  4992. mapindex = mapindex_name2id(str);
  4993. break;
  4994. case 2:
  4995. //"SavePoint" uses save point of the currently attached player
  4996. if (( sd = script_rid2sd(st) ) == NULL )
  4997. return SCRIPT_CMD_SUCCESS;
  4998. default:
  4999. mapindex = 0;
  5000. break;
  5001. }
  5002. for (i = 0; i < MAX_PARTY; i++)
  5003. {
  5004. if( !(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id )
  5005. continue;
  5006. if( str2 && strcmp(str2, map[pl_sd->bl.m].name) != 0 )
  5007. continue;
  5008. if( pc_isdead(pl_sd) )
  5009. continue;
  5010. switch( type )
  5011. {
  5012. case 0: // Random
  5013. if(!map[pl_sd->bl.m].flag.nowarp)
  5014. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5015. break;
  5016. case 1: // SavePointAll
  5017. if(!map[pl_sd->bl.m].flag.noreturn)
  5018. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5019. break;
  5020. case 2: // SavePoint
  5021. if(!map[pl_sd->bl.m].flag.noreturn)
  5022. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5023. break;
  5024. case 3: // Leader
  5025. case 4: // m,x,y
  5026. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  5027. pc_setpos(pl_sd,mapindex,x,y,CLR_TELEPORT);
  5028. break;
  5029. }
  5030. }
  5031. return SCRIPT_CMD_SUCCESS;
  5032. }
  5033. /*==========================================
  5034. * Warpguild - [Fredzilla]
  5035. * Syntax: warpguild "mapname",x,y,Guild_ID;
  5036. *------------------------------------------*/
  5037. BUILDIN_FUNC(warpguild)
  5038. {
  5039. TBL_PC *sd = NULL;
  5040. TBL_PC *pl_sd;
  5041. struct guild* g;
  5042. struct s_mapiterator* iter;
  5043. int type;
  5044. const char* str = script_getstr(st,2);
  5045. int x = script_getnum(st,3);
  5046. int y = script_getnum(st,4);
  5047. int gid = script_getnum(st,5);
  5048. g = guild_search(gid);
  5049. if( g == NULL )
  5050. return SCRIPT_CMD_SUCCESS;
  5051. type = ( strcmp(str,"Random")==0 ) ? 0
  5052. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  5053. : ( strcmp(str,"SavePoint")==0 ) ? 2
  5054. : 3;
  5055. if( type == 2 && ( sd = script_rid2sd(st) ) == NULL )
  5056. {// "SavePoint" uses save point of the currently attached player
  5057. return SCRIPT_CMD_SUCCESS;
  5058. }
  5059. iter = mapit_getallusers();
  5060. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  5061. {
  5062. if( pl_sd->status.guild_id != gid )
  5063. continue;
  5064. switch( type )
  5065. {
  5066. case 0: // Random
  5067. if(!map[pl_sd->bl.m].flag.nowarp)
  5068. pc_randomwarp(pl_sd,CLR_TELEPORT);
  5069. break;
  5070. case 1: // SavePointAll
  5071. if(!map[pl_sd->bl.m].flag.noreturn)
  5072. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,CLR_TELEPORT);
  5073. break;
  5074. case 2: // SavePoint
  5075. if(!map[pl_sd->bl.m].flag.noreturn)
  5076. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  5077. break;
  5078. case 3: // m,x,y
  5079. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  5080. pc_setpos(pl_sd,mapindex_name2id(str),x,y,CLR_TELEPORT);
  5081. break;
  5082. }
  5083. }
  5084. mapit_free(iter);
  5085. return SCRIPT_CMD_SUCCESS;
  5086. }
  5087. /*==========================================
  5088. * Force Heal a player (hp and sp)
  5089. *------------------------------------------*/
  5090. BUILDIN_FUNC(heal)
  5091. {
  5092. TBL_PC *sd;
  5093. int hp,sp;
  5094. if (!script_charid2sd(4,sd))
  5095. return SCRIPT_CMD_SUCCESS;
  5096. hp=script_getnum(st,2);
  5097. sp=script_getnum(st,3);
  5098. status_heal(&sd->bl, hp, sp, 1);
  5099. return SCRIPT_CMD_SUCCESS;
  5100. }
  5101. /*==========================================
  5102. * Heal a player by item (get vit bonus etc)
  5103. *------------------------------------------*/
  5104. BUILDIN_FUNC(itemheal)
  5105. {
  5106. TBL_PC *sd;
  5107. int hp,sp;
  5108. hp=script_getnum(st,2);
  5109. sp=script_getnum(st,3);
  5110. if(potion_flag==1) {
  5111. potion_hp = hp;
  5112. potion_sp = sp;
  5113. return SCRIPT_CMD_SUCCESS;
  5114. }
  5115. if (!script_charid2sd(4,sd))
  5116. return SCRIPT_CMD_SUCCESS;
  5117. pc_itemheal(sd,sd->itemid,hp,sp);
  5118. return SCRIPT_CMD_SUCCESS;
  5119. }
  5120. /*==========================================
  5121. *
  5122. *------------------------------------------*/
  5123. BUILDIN_FUNC(percentheal)
  5124. {
  5125. int hp,sp;
  5126. TBL_PC* sd;
  5127. hp=script_getnum(st,2);
  5128. sp=script_getnum(st,3);
  5129. if(potion_flag==1) {
  5130. potion_per_hp = hp;
  5131. potion_per_sp = sp;
  5132. return SCRIPT_CMD_SUCCESS;
  5133. }
  5134. if (!script_charid2sd(4,sd))
  5135. return SCRIPT_CMD_SUCCESS;
  5136. #ifdef RENEWAL
  5137. if( sd->sc.data[SC_EXTREMITYFIST2] )
  5138. sp = 0;
  5139. #endif
  5140. if (sd->sc.data[SC_NORECOVER_STATE]) {
  5141. hp = 0;
  5142. sp = 0;
  5143. }
  5144. pc_percentheal(sd,hp,sp);
  5145. return SCRIPT_CMD_SUCCESS;
  5146. }
  5147. /*==========================================
  5148. *
  5149. *------------------------------------------*/
  5150. BUILDIN_FUNC(jobchange)
  5151. {
  5152. int job, upper=-1;
  5153. job=script_getnum(st,2);
  5154. if( script_hasdata(st,3) )
  5155. upper=script_getnum(st,3);
  5156. if (pcdb_checkid(job))
  5157. {
  5158. TBL_PC* sd;
  5159. if (!script_charid2sd(4,sd))
  5160. return SCRIPT_CMD_SUCCESS;
  5161. pc_jobchange(sd, job, upper);
  5162. }
  5163. return SCRIPT_CMD_SUCCESS;
  5164. }
  5165. /*==========================================
  5166. *
  5167. *------------------------------------------*/
  5168. BUILDIN_FUNC(jobname)
  5169. {
  5170. int class_=script_getnum(st,2);
  5171. script_pushconststr(st, (char*)job_name(class_));
  5172. return SCRIPT_CMD_SUCCESS;
  5173. }
  5174. /// Get input from the player.
  5175. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  5176. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  5177. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  5178. /// shorter than 'min' and 0 otherwise.
  5179. ///
  5180. /// input(<var>{,<min>{,<max>}}) -> <int>
  5181. BUILDIN_FUNC(input)
  5182. {
  5183. TBL_PC* sd;
  5184. struct script_data* data;
  5185. int64 uid;
  5186. const char* name;
  5187. int min;
  5188. int max;
  5189. sd = script_rid2sd(st);
  5190. if( sd == NULL )
  5191. return SCRIPT_CMD_SUCCESS;
  5192. data = script_getdata(st,2);
  5193. if( !data_isreference(data) ){
  5194. ShowError("script:input: not a variable\n");
  5195. script_reportdata(data);
  5196. st->state = END;
  5197. return SCRIPT_CMD_FAILURE;
  5198. }
  5199. uid = reference_getuid(data);
  5200. name = reference_getname(data);
  5201. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  5202. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  5203. #ifdef SECURE_NPCTIMEOUT
  5204. sd->npc_idle_type = NPCT_WAIT;
  5205. #endif
  5206. if( !sd->state.menu_or_input )
  5207. { // first invocation, display npc input box
  5208. sd->state.menu_or_input = 1;
  5209. st->state = RERUNLINE;
  5210. if( is_string_variable(name) )
  5211. clif_scriptinputstr(sd,st->oid);
  5212. else
  5213. clif_scriptinput(sd,st->oid);
  5214. }
  5215. else
  5216. { // take received text/value and store it in the designated variable
  5217. sd->state.menu_or_input = 0;
  5218. if( is_string_variable(name) )
  5219. {
  5220. int len = (int)strlen(sd->npc_str);
  5221. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  5222. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  5223. }
  5224. else
  5225. {
  5226. int amount = sd->npc_amount;
  5227. set_reg(st, sd, uid, name, (void*)__64BPRTSIZE(cap_value(amount,min,max)), script_getref(st,2));
  5228. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  5229. }
  5230. st->state = RUN;
  5231. }
  5232. return SCRIPT_CMD_SUCCESS;
  5233. }
  5234. // declare the copyarray method here for future reference
  5235. BUILDIN_FUNC(copyarray);
  5236. /// Sets the value of a variable.
  5237. /// The value is converted to the type of the variable.
  5238. ///
  5239. /// set(<variable>,<value>{,<charid>})
  5240. BUILDIN_FUNC(set)
  5241. {
  5242. TBL_PC* sd = NULL;
  5243. struct script_data* data;
  5244. //struct script_data* datavalue;
  5245. int64 num;
  5246. const char* name;
  5247. char prefix;
  5248. data = script_getdata(st,2);
  5249. //datavalue = script_getdata(st,3);
  5250. if( !data_isreference(data) )
  5251. {
  5252. ShowError("script:set: not a variable\n");
  5253. script_reportdata(script_getdata(st,2));
  5254. st->state = END;
  5255. return SCRIPT_CMD_FAILURE;
  5256. }
  5257. num = reference_getuid(data);
  5258. name = reference_getname(data);
  5259. prefix = *name;
  5260. if (not_server_variable(prefix) && !script_charid2sd(4,sd)) {
  5261. ShowError("buildin_set: No player attached for player variable '%s'\n", name);
  5262. return SCRIPT_CMD_FAILURE;
  5263. }
  5264. #if 0
  5265. if( data_isreference(datavalue) )
  5266. {// the value being referenced is a variable
  5267. const char* namevalue = reference_getname(datavalue);
  5268. if( !not_array_variable(*namevalue) )
  5269. {// array variable being copied into another array variable
  5270. if( sd == NULL && not_server_variable(*namevalue) && !(sd = script_rid2sd(st)) )
  5271. {// player must be attached in order to copy a player variable
  5272. ShowError("script:set: no player attached for player variable '%s'\n", namevalue);
  5273. return SCRIPT_CMD_SUCCESS;
  5274. }
  5275. if( is_string_variable(namevalue) != is_string_variable(name) )
  5276. {// non-matching array value types
  5277. ShowWarning("script:set: two array variables do not match in type.\n");
  5278. return SCRIPT_CMD_SUCCESS;
  5279. }
  5280. // push the maximum number of array values to the stack
  5281. push_val(st->stack, C_INT, SCRIPT_MAX_ARRAYSIZE);
  5282. // call the copy array method directly
  5283. return buildin_copyarray(st);
  5284. }
  5285. }
  5286. #endif
  5287. if( is_string_variable(name) )
  5288. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  5289. else
  5290. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(script_getnum(st,3)),script_getref(st,2));
  5291. // return a copy of the variable reference
  5292. script_pushcopy(st,2);
  5293. return SCRIPT_CMD_SUCCESS;
  5294. }
  5295. /////////////////////////////////////////////////////////////////////
  5296. /// Array variables
  5297. ///
  5298. /// Sets values of an array, from the starting index.
  5299. /// ex: setarray arr[1],1,2,3;
  5300. ///
  5301. /// setarray <array variable>,<value1>{,<value2>...};
  5302. BUILDIN_FUNC(setarray)
  5303. {
  5304. struct script_data* data;
  5305. const char* name;
  5306. uint32 start;
  5307. uint32 end;
  5308. int32 id;
  5309. int32 i;
  5310. TBL_PC* sd = NULL;
  5311. data = script_getdata(st, 2);
  5312. if( !data_isreference(data) )
  5313. {
  5314. ShowError("script:setarray: not a variable\n");
  5315. script_reportdata(data);
  5316. st->state = END;
  5317. return SCRIPT_CMD_FAILURE;// not a variable
  5318. }
  5319. id = reference_getid(data);
  5320. start = reference_getindex(data);
  5321. name = reference_getname(data);
  5322. if( not_server_variable(*name) )
  5323. {
  5324. sd = script_rid2sd(st);
  5325. if( sd == NULL )
  5326. return SCRIPT_CMD_SUCCESS;// no player attached
  5327. }
  5328. end = start + script_lastdata(st) - 2;
  5329. if( end > SCRIPT_MAX_ARRAYSIZE )
  5330. end = SCRIPT_MAX_ARRAYSIZE;
  5331. if( is_string_variable(name) )
  5332. {// string array
  5333. for( i = 3; start < end; ++start, ++i )
  5334. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  5335. }
  5336. else
  5337. {// int array
  5338. for( i = 3; start < end; ++start, ++i )
  5339. set_reg(st, sd, reference_uid(id, start), name, (void*)__64BPRTSIZE(script_getnum(st,i)), reference_getref(data));
  5340. }
  5341. return SCRIPT_CMD_SUCCESS;
  5342. }
  5343. /// Sets count values of an array, from the starting index.
  5344. /// ex: cleararray arr[0],0,1;
  5345. ///
  5346. /// cleararray <array variable>,<value>,<count>;
  5347. BUILDIN_FUNC(cleararray)
  5348. {
  5349. struct script_data* data;
  5350. const char* name;
  5351. uint32 start;
  5352. uint32 end;
  5353. int32 id;
  5354. void* v;
  5355. TBL_PC* sd = NULL;
  5356. data = script_getdata(st, 2);
  5357. if( !data_isreference(data) )
  5358. {
  5359. ShowError("script:cleararray: not a variable\n");
  5360. script_reportdata(data);
  5361. st->state = END;
  5362. return SCRIPT_CMD_FAILURE;// not a variable
  5363. }
  5364. id = reference_getid(data);
  5365. start = reference_getindex(data);
  5366. name = reference_getname(data);
  5367. if( not_server_variable(*name) )
  5368. {
  5369. sd = script_rid2sd(st);
  5370. if( sd == NULL )
  5371. return SCRIPT_CMD_SUCCESS;// no player attached
  5372. }
  5373. if( is_string_variable(name) )
  5374. v = (void*)script_getstr(st, 3);
  5375. else
  5376. v = (void*)__64BPRTSIZE(script_getnum(st, 3));
  5377. end = start + script_getnum(st, 4);
  5378. if( end > SCRIPT_MAX_ARRAYSIZE )
  5379. end = SCRIPT_MAX_ARRAYSIZE;
  5380. for( ; start < end; ++start )
  5381. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5382. return SCRIPT_CMD_SUCCESS;
  5383. }
  5384. /// Copies data from one array to another.
  5385. /// ex: copyarray arr[0],arr[2],2;
  5386. ///
  5387. /// copyarray <destination array variable>,<source array variable>,<count>;
  5388. BUILDIN_FUNC(copyarray)
  5389. {
  5390. struct script_data* data1;
  5391. struct script_data* data2;
  5392. const char* name1;
  5393. const char* name2;
  5394. int32 idx1;
  5395. int32 idx2;
  5396. int32 id1;
  5397. int32 id2;
  5398. void* v;
  5399. int32 i;
  5400. uint32 count;
  5401. TBL_PC* sd = NULL;
  5402. data1 = script_getdata(st, 2);
  5403. data2 = script_getdata(st, 3);
  5404. if( !data_isreference(data1) || !data_isreference(data2) )
  5405. {
  5406. ShowError("script:copyarray: not a variable\n");
  5407. script_reportdata(data1);
  5408. script_reportdata(data2);
  5409. st->state = END;
  5410. return SCRIPT_CMD_FAILURE;// not a variable
  5411. }
  5412. id1 = reference_getid(data1);
  5413. id2 = reference_getid(data2);
  5414. idx1 = reference_getindex(data1);
  5415. idx2 = reference_getindex(data2);
  5416. name1 = reference_getname(data1);
  5417. name2 = reference_getname(data2);
  5418. if( is_string_variable(name1) != is_string_variable(name2) )
  5419. {
  5420. ShowError("script:copyarray: type mismatch\n");
  5421. script_reportdata(data1);
  5422. script_reportdata(data2);
  5423. st->state = END;
  5424. return SCRIPT_CMD_FAILURE;// data type mismatch
  5425. }
  5426. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5427. {
  5428. sd = script_rid2sd(st);
  5429. if( sd == NULL )
  5430. return SCRIPT_CMD_SUCCESS;// no player attached
  5431. }
  5432. count = script_getnum(st, 4);
  5433. if( count > SCRIPT_MAX_ARRAYSIZE - idx1 )
  5434. count = SCRIPT_MAX_ARRAYSIZE - idx1;
  5435. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5436. return SCRIPT_CMD_SUCCESS;// nothing to copy
  5437. if( id1 == id2 && idx1 > idx2 )
  5438. {// destination might be overlapping the source - copy in reverse order
  5439. for( i = count - 1; i >= 0; --i )
  5440. {
  5441. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5442. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5443. script_removetop(st, -1, 0);
  5444. }
  5445. }
  5446. else
  5447. {// normal copy
  5448. for( i = 0; i < count; ++i )
  5449. {
  5450. if( idx2 + i < SCRIPT_MAX_ARRAYSIZE )
  5451. {
  5452. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5453. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5454. script_removetop(st, -1, 0);
  5455. }
  5456. else// out of range - assume ""/0
  5457. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  5458. }
  5459. }
  5460. return SCRIPT_CMD_SUCCESS;
  5461. }
  5462. /// Returns the size of the array.
  5463. /// Assumes that everything before the starting index exists.
  5464. /// ex: getarraysize(arr[3])
  5465. ///
  5466. /// getarraysize(<array variable>) -> <int>
  5467. BUILDIN_FUNC(getarraysize)
  5468. {
  5469. struct script_data* data;
  5470. data = script_getdata(st, 2);
  5471. if( !data_isreference(data) )
  5472. {
  5473. ShowError("script:getarraysize: not a variable\n");
  5474. script_reportdata(data);
  5475. script_pushnil(st);
  5476. st->state = END;
  5477. return SCRIPT_CMD_FAILURE;// not a variable
  5478. }
  5479. script_pushint(st, script_array_highest_key(st, st->rid ? script_rid2sd(st) : NULL, reference_getname(data), reference_getref(data)));
  5480. return SCRIPT_CMD_SUCCESS;
  5481. }
  5482. int script_array_index_cmp(const void *a, const void *b) {
  5483. return ( *(unsigned int*)a - *(unsigned int*)b );
  5484. }
  5485. /// Deletes count or all the elements in an array, from the starting index.
  5486. /// ex: deletearray arr[4],2;
  5487. ///
  5488. /// deletearray <array variable>;
  5489. /// deletearray <array variable>,<count>;
  5490. BUILDIN_FUNC(deletearray)
  5491. {
  5492. struct script_data* data;
  5493. const char* name;
  5494. unsigned int start, end, i;
  5495. int id;
  5496. TBL_PC *sd = NULL;
  5497. struct script_array *sa = NULL;
  5498. struct reg_db *src = NULL;
  5499. void *value;
  5500. data = script_getdata(st, 2);
  5501. if( !data_isreference(data) ) {
  5502. ShowError("script:deletearray: not a variable\n");
  5503. script_reportdata(data);
  5504. st->state = END;
  5505. return SCRIPT_CMD_FAILURE;// not a variable
  5506. }
  5507. id = reference_getid(data);
  5508. start = reference_getindex(data);
  5509. name = reference_getname(data);
  5510. if( not_server_variable(*name) ) {
  5511. sd = script_rid2sd(st);
  5512. if( sd == NULL )
  5513. return SCRIPT_CMD_SUCCESS;// no player attached
  5514. }
  5515. if (!(src = script_array_src(st, sd, name, reference_getref(data)))) {
  5516. ShowError("script:deletearray: not a array\n");
  5517. script_reportdata(data);
  5518. st->state = END;
  5519. return SCRIPT_CMD_FAILURE;// not a variable
  5520. }
  5521. script_array_ensure_zero(st, NULL, data->u.num, reference_getref(data));
  5522. if ( !(sa = idb_get(src->arrays, id)) ) { // non-existent array, nothing to empty
  5523. return SCRIPT_CMD_SUCCESS;// not a variable
  5524. }
  5525. end = script_array_highest_key(st, sd, name, reference_getref(data));
  5526. if( start >= end )
  5527. return SCRIPT_CMD_SUCCESS;// nothing to free
  5528. if( is_string_variable(name) )
  5529. value = (void *)"";
  5530. else
  5531. value = (void *)0;
  5532. if( script_hasdata(st,3) ) {
  5533. unsigned int count = script_getnum(st, 3);
  5534. if( count > end - start )
  5535. count = end - start;
  5536. if( count <= 0 )
  5537. return SCRIPT_CMD_SUCCESS;// nothing to free
  5538. if( end - start < sa->size ) {
  5539. // Better to iterate directly on the array, no speed-up from using sa
  5540. for( ; start + count < end; ++start ) {
  5541. // Compact and overwrite
  5542. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5543. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5544. script_removetop(st, -1, 0);
  5545. }
  5546. for( ; start < end; start++ ) {
  5547. // Clean up any leftovers that weren't overwritten
  5548. set_reg(st, sd, reference_uid(id, start), name, value, reference_getref(data));
  5549. }
  5550. } else {
  5551. // using sa to speed up
  5552. unsigned int *list = NULL, size = 0;
  5553. list = script_array_cpy_list(sa);
  5554. size = sa->size;
  5555. qsort(list, size, sizeof(unsigned int), script_array_index_cmp);
  5556. ARR_FIND(0, size, i, list[i] >= start);
  5557. for( ; i < size && list[i] < start + count; i++ ) {
  5558. // Clear any entries between start and start+count, if they exist
  5559. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5560. }
  5561. for( ; i < size && list[i] < end; i++ ) {
  5562. // Move back count positions any entries between start+count to fill the gaps
  5563. void* v = get_val2(st, reference_uid(id, list[i]), reference_getref(data));
  5564. set_reg(st, sd, reference_uid(id, list[i]-count), name, v, reference_getref(data));
  5565. script_removetop(st, -1, 0);
  5566. // Clear their originals
  5567. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5568. }
  5569. }
  5570. } else {
  5571. unsigned int *list = NULL, size = 0;
  5572. list = script_array_cpy_list(sa);
  5573. size = sa->size;
  5574. for(i = 0; i < size; i++) {
  5575. if( list[i] >= start ) // Less expensive than sorting it, most likely
  5576. set_reg(st, sd, reference_uid(id, list[i]), name, value, reference_getref(data));
  5577. }
  5578. }
  5579. return SCRIPT_CMD_SUCCESS;
  5580. }
  5581. /// Returns a reference to the target index of the array variable.
  5582. /// Equivalent to var[index].
  5583. ///
  5584. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5585. BUILDIN_FUNC(getelementofarray)
  5586. {
  5587. struct script_data* data;
  5588. int32 id;
  5589. int64 i;
  5590. data = script_getdata(st, 2);
  5591. if( !data_isreference(data) )
  5592. {
  5593. ShowError("script:getelementofarray: not a variable\n");
  5594. script_reportdata(data);
  5595. script_pushnil(st);
  5596. st->state = END;
  5597. return SCRIPT_CMD_SUCCESS;// not a variable
  5598. }
  5599. id = reference_getid(data);
  5600. i = script_getnum(st, 3);
  5601. if (i < 0 || i >= SCRIPT_MAX_ARRAYSIZE) {
  5602. ShowWarning("script:getelementofarray: index out of range (%"PRId64")\n", i);
  5603. script_reportdata(data);
  5604. script_pushnil(st);
  5605. st->state = END;
  5606. return SCRIPT_CMD_FAILURE;// out of range
  5607. }
  5608. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5609. return SCRIPT_CMD_SUCCESS;
  5610. }
  5611. /////////////////////////////////////////////////////////////////////
  5612. /// ...
  5613. ///
  5614. /*==========================================
  5615. *
  5616. *------------------------------------------*/
  5617. BUILDIN_FUNC(setlook)
  5618. {
  5619. int type,val;
  5620. TBL_PC* sd;
  5621. type = script_getnum(st,2);
  5622. val = script_getnum(st,3);
  5623. if (!script_charid2sd(4,sd))
  5624. return SCRIPT_CMD_SUCCESS;
  5625. pc_changelook(sd,type,val);
  5626. return SCRIPT_CMD_SUCCESS;
  5627. }
  5628. BUILDIN_FUNC(changelook)
  5629. { // As setlook but only client side
  5630. int type,val;
  5631. TBL_PC* sd;
  5632. type = script_getnum(st,2);
  5633. val = script_getnum(st,3);
  5634. if (!script_charid2sd(4,sd))
  5635. return SCRIPT_CMD_SUCCESS;
  5636. clif_changelook(&sd->bl,type,val);
  5637. return SCRIPT_CMD_SUCCESS;
  5638. }
  5639. /*==========================================
  5640. *
  5641. *------------------------------------------*/
  5642. BUILDIN_FUNC(cutin)
  5643. {
  5644. TBL_PC* sd;
  5645. sd = script_rid2sd(st);
  5646. if( sd == NULL )
  5647. return SCRIPT_CMD_SUCCESS;
  5648. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  5649. return SCRIPT_CMD_SUCCESS;
  5650. }
  5651. /*==========================================
  5652. *
  5653. *------------------------------------------*/
  5654. BUILDIN_FUNC(viewpoint)
  5655. {
  5656. int type,x,y,id,color;
  5657. TBL_PC* sd;
  5658. type=script_getnum(st,2);
  5659. x=script_getnum(st,3);
  5660. y=script_getnum(st,4);
  5661. id=script_getnum(st,5);
  5662. color=script_getnum(st,6);
  5663. sd = script_rid2sd(st);
  5664. if( sd == NULL )
  5665. return SCRIPT_CMD_SUCCESS;
  5666. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  5667. return SCRIPT_CMD_SUCCESS;
  5668. }
  5669. /// Returns number of items in inventory/cart/storage
  5670. /// countitem <nameID>{,<accountID>});
  5671. /// countitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>}) [Lupus]
  5672. /// cartcountitem <nameID>{,<accountID>});
  5673. /// cartcountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5674. /// storagecountitem <nameID>{,<accountID>});
  5675. /// storagecountitem2 <nameID>,<Identified>,<Refine>,<Attribute>,<Card0>,<Card1>,<Card2>,<Card3>{,<accountID>})
  5676. BUILDIN_FUNC(countitem)
  5677. {
  5678. int i = 0, count = 0, aid = 3;
  5679. struct item_data* id = NULL;
  5680. struct script_data* data;
  5681. char *command = (char *)script_getfuncname(st);
  5682. uint8 loc = 0;
  5683. uint16 size;
  5684. struct item *items;
  5685. TBL_PC *sd = NULL;
  5686. if( command[strlen(command)-1] == '2' ) {
  5687. i = 1;
  5688. aid = 10;
  5689. }
  5690. if( script_hasdata(st,aid) ) {
  5691. if( !(sd = map_id2sd( (aid = script_getnum(st, aid)) )) ) {
  5692. ShowError("buildin_%s: player not found (AID=%d).\n", command, aid);
  5693. st->state = END;
  5694. return SCRIPT_CMD_FAILURE;
  5695. }
  5696. }
  5697. else {
  5698. if( !(sd = script_rid2sd(st)) )
  5699. return SCRIPT_CMD_SUCCESS;
  5700. }
  5701. if( !strncmp(command, "cart", 4) ) {
  5702. loc = 1;
  5703. size = MAX_CART;
  5704. items = sd->status.cart;
  5705. }
  5706. else if( !strncmp(command, "storage", 7) ) {
  5707. loc = 2;
  5708. size = MAX_STORAGE;
  5709. items = sd->status.storage.items;
  5710. }
  5711. //TODO: 3 - Guild Storage
  5712. else {
  5713. size = MAX_INVENTORY;
  5714. items = sd->status.inventory;
  5715. }
  5716. if( loc == 1 && !pc_iscarton(sd) ) {
  5717. ShowError("buildin_%s: Player doesn't have cart (CID:%d).\n", command, sd->status.char_id);
  5718. script_pushint(st,-1);
  5719. return SCRIPT_CMD_SUCCESS;
  5720. }
  5721. data = script_getdata(st, 2);
  5722. get_val(st, data); // Convert into value in case of a variable
  5723. if( data_isstring(data) ) // item name
  5724. id = itemdb_searchname(conv_str(st, data));
  5725. else // item id
  5726. id = itemdb_exists(conv_num(st, data));
  5727. if( id == NULL ) {
  5728. ShowError("buildin_%s: Invalid item '%s'.\n", command, script_getstr(st,2)); // returns string, regardless of what it was
  5729. script_pushint(st,0);
  5730. return SCRIPT_CMD_FAILURE;
  5731. }
  5732. if( !i ) { // For count/cart/storagecountitem function
  5733. unsigned short nameid = id->nameid;
  5734. for( i = 0; i < size; i++ )
  5735. if( &items[i] && items[i].nameid == nameid )
  5736. count += items[i].amount;
  5737. }
  5738. else { // For count/cart/storagecountitem2 function
  5739. unsigned short nameid;
  5740. int iden, ref, attr, c1, c2, c3, c4;
  5741. nameid = id->nameid;
  5742. iden = script_getnum(st,3);
  5743. ref = script_getnum(st,4);
  5744. attr = script_getnum(st,5);
  5745. c1 = script_getnum(st,6);
  5746. c2 = script_getnum(st,7);
  5747. c3 = script_getnum(st,8);
  5748. c4 = script_getnum(st,9);
  5749. for( i = 0; i < size; i++ )
  5750. if( &items[i] && items[i].nameid > 0 && items[i].nameid == nameid &&
  5751. items[i].amount > 0 && items[i].identify == iden &&
  5752. items[i].refine == ref && items[i].attribute == attr &&
  5753. items[i].card[0] == c1 && items[i].card[1] == c2 &&
  5754. items[i].card[2] == c3 && items[i].card[3] == c4 )
  5755. {
  5756. count += items[i].amount;
  5757. }
  5758. }
  5759. script_pushint(st, count);
  5760. return SCRIPT_CMD_SUCCESS;
  5761. }
  5762. /*==========================================
  5763. * Check if item with this amount can fit in inventory
  5764. * Checking : weight, stack amount >(MAX_AMOUNT), slots amount >(MAX_INVENTORY)
  5765. * Return
  5766. * 0 : fail
  5767. * 1 : success (npc side only)
  5768. *------------------------------------------*/
  5769. BUILDIN_FUNC(checkweight)
  5770. {
  5771. int slots, amount2 = 0;
  5772. unsigned int weight = 0, i, nbargs;
  5773. struct item_data* id = NULL;
  5774. struct map_session_data* sd;
  5775. if( ( sd = script_rid2sd(st) ) == NULL )
  5776. return SCRIPT_CMD_SUCCESS;
  5777. nbargs = script_lastdata(st)+1;
  5778. if(nbargs%2) {
  5779. ShowError("buildin_checkweight: Invalid nb of args should be a multiple of 2.\n");
  5780. script_pushint(st,0);
  5781. return SCRIPT_CMD_FAILURE;
  5782. }
  5783. slots = pc_inventoryblank(sd); //nb of empty slot
  5784. for (i = 2; i < nbargs; i += 2) {
  5785. unsigned short nameid, amount;
  5786. struct script_data* data = script_getdata(st,i);
  5787. get_val(st, data); // Convert into value in case of a variable
  5788. if( data_isstring(data) ) // item name
  5789. id = itemdb_searchname(conv_str(st, data));
  5790. else // item id
  5791. id = itemdb_exists(conv_num(st, data));
  5792. if( id == NULL ) {
  5793. ShowError("buildin_checkweight: Invalid item '%s'.\n", script_getstr(st,i)); // returns string, regardless of what it was
  5794. script_pushint(st,0);
  5795. return SCRIPT_CMD_FAILURE;
  5796. }
  5797. nameid = id->nameid;
  5798. amount = script_getnum(st,i+1);
  5799. if( amount < 1 ) {
  5800. ShowError("buildin_checkweight: Invalid amount '%d'.\n", amount);
  5801. script_pushint(st,0);
  5802. return SCRIPT_CMD_FAILURE;
  5803. }
  5804. weight += itemdb_weight(nameid)*amount; //total weight for all chk
  5805. if( weight + sd->weight > sd->max_weight )
  5806. {// too heavy
  5807. script_pushint(st,0);
  5808. return SCRIPT_CMD_SUCCESS;
  5809. }
  5810. switch( pc_checkadditem(sd, nameid, amount) ) {
  5811. case CHKADDITEM_EXIST:
  5812. // item is already in inventory, but there is still space for the requested amount
  5813. break;
  5814. case CHKADDITEM_NEW:
  5815. if( itemdb_isstackable(nameid) ) {
  5816. // stackable
  5817. amount2++;
  5818. if( slots < amount2 ) {
  5819. script_pushint(st,0);
  5820. return SCRIPT_CMD_SUCCESS;
  5821. }
  5822. } else {
  5823. // non-stackable
  5824. amount2 += amount;
  5825. if( slots < amount2) {
  5826. script_pushint(st,0);
  5827. return SCRIPT_CMD_SUCCESS;
  5828. }
  5829. }
  5830. break;
  5831. case CHKADDITEM_OVERAMOUNT:
  5832. script_pushint(st,0);
  5833. return SCRIPT_CMD_SUCCESS;
  5834. }
  5835. }
  5836. script_pushint(st,1);
  5837. return SCRIPT_CMD_SUCCESS;
  5838. }
  5839. BUILDIN_FUNC(checkweight2)
  5840. {
  5841. //variable sub checkweight
  5842. int i = 0, amount2 = 0, slots = 0, weight = 0;
  5843. short fail = 0;
  5844. //variable for array parsing
  5845. struct script_data* data_it;
  5846. struct script_data* data_nb;
  5847. const char* name_it;
  5848. const char* name_nb;
  5849. int32 id_it, id_nb;
  5850. int32 idx_it, idx_nb;
  5851. int nb_it, nb_nb; //array size
  5852. TBL_PC *sd = script_rid2sd(st);
  5853. nullpo_retr(1,sd);
  5854. data_it = script_getdata(st, 2);
  5855. data_nb = script_getdata(st, 3);
  5856. if( !data_isreference(data_it) || !data_isreference(data_nb)) {
  5857. ShowError("buildin_checkweight2: parameter not a variable\n");
  5858. script_pushint(st,0);
  5859. return SCRIPT_CMD_FAILURE;// not a variable
  5860. }
  5861. id_it = reference_getid(data_it);
  5862. id_nb = reference_getid(data_nb);
  5863. idx_it = reference_getindex(data_it);
  5864. idx_nb = reference_getindex(data_nb);
  5865. name_it = reference_getname(data_it);
  5866. name_nb = reference_getname(data_nb);
  5867. if(is_string_variable(name_it) || is_string_variable(name_nb)) {
  5868. ShowError("buildin_checkweight2: illegal type, need int\n");
  5869. script_pushint(st,0);
  5870. return SCRIPT_CMD_FAILURE;// not supported
  5871. }
  5872. nb_it = script_array_highest_key(st, sd, reference_getname(data_it), reference_getref(data_it));
  5873. nb_nb = script_array_highest_key(st, sd, reference_getname(data_nb), reference_getref(data_nb));
  5874. if(nb_it != nb_nb) {
  5875. ShowError("buildin_checkweight2: Size mistmatch: nb_it=%d, nb_nb=%d\n",nb_it,nb_nb);
  5876. fail = 1;
  5877. }
  5878. slots = pc_inventoryblank(sd);
  5879. for(i=0; i<nb_it; i++) {
  5880. unsigned short amount, nameid = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_it,idx_it+i),reference_getref(data_it)));
  5881. script_removetop(st, -1, 0);
  5882. amount = (int32)__64BPRTSIZE(get_val2(st,reference_uid(id_nb,idx_nb+i),reference_getref(data_nb)));
  5883. script_removetop(st, -1, 0);
  5884. if(fail)
  5885. continue; //cpntonie to depop rest
  5886. if(itemdb_exists(nameid) == NULL ) {
  5887. ShowError("buildin_checkweight2: Invalid item '%d'.\n", nameid);
  5888. fail=1;
  5889. continue;
  5890. }
  5891. if(amount < 0 ) {
  5892. ShowError("buildin_checkweight2: Invalid amount '%d'.\n", amount);
  5893. fail = 1;
  5894. continue;
  5895. }
  5896. weight += itemdb_weight(nameid)*amount;
  5897. if( weight + sd->weight > sd->max_weight ) {
  5898. fail = 1;
  5899. continue;
  5900. }
  5901. switch( pc_checkadditem(sd, nameid, amount) ) {
  5902. case CHKADDITEM_EXIST:
  5903. // item is already in inventory, but there is still space for the requested amount
  5904. break;
  5905. case CHKADDITEM_NEW:
  5906. if( itemdb_isstackable(nameid) ) {// stackable
  5907. amount2++;
  5908. if( slots < amount2 )
  5909. fail = 1;
  5910. } else {// non-stackable
  5911. amount2 += amount;
  5912. if( slots < amount2 ) {
  5913. fail = 1;
  5914. }
  5915. }
  5916. break;
  5917. case CHKADDITEM_OVERAMOUNT:
  5918. fail = 1;
  5919. } //end switch
  5920. } //end loop DO NOT break it prematurly we need to depop all stack
  5921. fail ? script_pushint(st,0) : script_pushint(st,1);
  5922. return SCRIPT_CMD_SUCCESS;
  5923. }
  5924. /*==========================================
  5925. * getitem <item id>,<amount>{,<account ID>};
  5926. * getitem "<item name>",<amount>{,<account ID>};
  5927. *
  5928. * getitembound <item id>,<amount>,<type>{,<account ID>};
  5929. * getitembound "<item id>",<amount>,<type>{,<account ID>};
  5930. * Type:
  5931. * 0 - No bound
  5932. * 1 - Account Bound
  5933. * 2 - Guild Bound
  5934. * 3 - Party Bound
  5935. * 4 - Character Bound
  5936. *------------------------------------------*/
  5937. BUILDIN_FUNC(getitem)
  5938. {
  5939. int amount, get_count, i;
  5940. unsigned short nameid;
  5941. struct item it;
  5942. TBL_PC *sd;
  5943. struct script_data *data;
  5944. unsigned char flag = 0;
  5945. const char* command = script_getfuncname(st);
  5946. struct item_data *id = NULL;
  5947. data = script_getdata(st,2);
  5948. get_val(st,data);
  5949. if( data_isstring(data) ) {// "<item name>"
  5950. const char *name = conv_str(st,data);
  5951. id = itemdb_searchname(name);
  5952. if( id == NULL ){
  5953. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5954. return SCRIPT_CMD_FAILURE; //No item created.
  5955. }
  5956. nameid = id->nameid;
  5957. } else if( data_isint(data) ) {// <item id>
  5958. nameid = conv_num(st,data);
  5959. if( !(id = itemdb_exists(nameid)) ){
  5960. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5961. return SCRIPT_CMD_FAILURE; //No item created.
  5962. }
  5963. } else {
  5964. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5965. return SCRIPT_CMD_FAILURE;
  5966. }
  5967. // <amount>
  5968. if( (amount = script_getnum(st,3)) <= 0)
  5969. return SCRIPT_CMD_SUCCESS; //return if amount <=0, skip the useles iteration
  5970. memset(&it,0,sizeof(it));
  5971. it.nameid = nameid;
  5972. it.identify = 1;
  5973. it.bound = BOUND_NONE;
  5974. if( !strcmp(command,"getitembound") ) {
  5975. char bound = script_getnum(st,4);
  5976. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  5977. ShowError("script_getitembound: Not a correct bound type! Type=%d\n",bound);
  5978. return SCRIPT_CMD_FAILURE;
  5979. }
  5980. if( script_hasdata(st,5) )
  5981. sd = map_id2sd(script_getnum(st,5));
  5982. else
  5983. sd = script_rid2sd(st); // Attached player
  5984. it.bound = bound;
  5985. } else if( script_hasdata(st,4) )
  5986. sd = map_id2sd(script_getnum(st,4)); // <Account ID>
  5987. else
  5988. sd = script_rid2sd(st); // Attached player
  5989. if( sd == NULL ) // no target
  5990. return SCRIPT_CMD_SUCCESS;
  5991. //Check if it's stackable.
  5992. if (!itemdb_isstackable2(id))
  5993. get_count = 1;
  5994. else
  5995. get_count = amount;
  5996. for (i = 0; i < amount; i += get_count)
  5997. {
  5998. // if not pet egg
  5999. if (!pet_create_egg(sd, nameid))
  6000. {
  6001. if ((flag = pc_additem(sd, &it, get_count, LOG_TYPE_SCRIPT)))
  6002. {
  6003. clif_additem(sd, 0, 0, flag);
  6004. if( pc_candrop(sd,&it) )
  6005. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6006. }
  6007. }
  6008. }
  6009. return SCRIPT_CMD_SUCCESS;
  6010. }
  6011. /*==========================================
  6012. * getitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6013. * getitem2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>};
  6014. *
  6015. * getitembound2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6016. * getitembound2 "<item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>,<bound type>{,<account ID>};
  6017. * Type:
  6018. * 0 - No bound
  6019. * 1 - Account Bound
  6020. * 2 - Guild Bound
  6021. * 3 - Party Bound
  6022. * 4 - Character Bound
  6023. *------------------------------------------*/
  6024. BUILDIN_FUNC(getitem2)
  6025. {
  6026. int amount;
  6027. unsigned short nameid;
  6028. int iden, ref, attr;
  6029. unsigned short c1, c2, c3, c4;
  6030. char bound = BOUND_NONE;
  6031. struct item_data *item_data = NULL;
  6032. struct item item_tmp;
  6033. TBL_PC *sd;
  6034. struct script_data *data;
  6035. const char* command = script_getfuncname(st);
  6036. if( !strcmp(command,"getitembound2") ) {
  6037. bound = script_getnum(st,11);
  6038. if( bound < BOUND_NONE || bound >= BOUND_MAX ) {
  6039. ShowError("script_getitembound2: Not a correct bound type! Type=%d\n",bound);
  6040. return SCRIPT_CMD_FAILURE;
  6041. }
  6042. if( script_hasdata(st,12) )
  6043. sd = map_id2sd(script_getnum(st,12));
  6044. else
  6045. sd = script_rid2sd(st); // Attached player
  6046. } else if( script_hasdata(st,11) )
  6047. sd = map_id2sd(script_getnum(st,11)); // <Account ID>
  6048. else
  6049. sd = script_rid2sd(st); // Attached player
  6050. if( sd == NULL ) // no target
  6051. return SCRIPT_CMD_SUCCESS;
  6052. data = script_getdata(st,2);
  6053. get_val(st,data);
  6054. if( data_isstring(data) ) {
  6055. const char *name = conv_str(st,data);
  6056. if( (item_data = itemdb_searchname(name)) == NULL ){
  6057. ShowError("buildin_getitem2: Nonexistant item %s requested (by conv_str).\n", name);
  6058. return SCRIPT_CMD_FAILURE; //No item created.
  6059. }
  6060. nameid = item_data->nameid;
  6061. } else {
  6062. nameid = conv_num(st,data);
  6063. if( (item_data = itemdb_exists(nameid)) == NULL ){
  6064. ShowError("buildin_getitem2: Nonexistant item %d requested (by conv_num).\n", nameid);
  6065. return SCRIPT_CMD_FAILURE; //No item created.
  6066. }
  6067. }
  6068. amount = script_getnum(st,3);
  6069. iden = script_getnum(st,4);
  6070. ref = script_getnum(st,5);
  6071. attr = script_getnum(st,6);
  6072. c1 = (unsigned short)script_getnum(st,7);
  6073. c2 = (unsigned short)script_getnum(st,8);
  6074. c3 = (unsigned short)script_getnum(st,9);
  6075. c4 = (unsigned short)script_getnum(st,10);
  6076. if( item_data ) {
  6077. int get_count = 0, i;
  6078. memset(&item_tmp,0,sizeof(item_tmp));
  6079. if( item_data->type == IT_WEAPON || item_data->type == IT_ARMOR || item_data->type == IT_SHADOWGEAR ) {
  6080. if(ref > MAX_REFINE)
  6081. ref = MAX_REFINE;
  6082. }
  6083. else if( item_data->type == IT_PETEGG ) {
  6084. iden = 1;
  6085. ref = 0;
  6086. }
  6087. else {
  6088. iden = 1;
  6089. ref = attr = 0;
  6090. }
  6091. item_tmp.nameid = nameid;
  6092. item_tmp.identify = iden;
  6093. item_tmp.refine = ref;
  6094. item_tmp.attribute = attr;
  6095. item_tmp.card[0] = c1;
  6096. item_tmp.card[1] = c2;
  6097. item_tmp.card[2] = c3;
  6098. item_tmp.card[3] = c4;
  6099. item_tmp.bound = bound;
  6100. //Check if it's stackable.
  6101. if (!itemdb_isstackable2(item_data))
  6102. get_count = 1;
  6103. else
  6104. get_count = amount;
  6105. for (i = 0; i < amount; i += get_count)
  6106. {
  6107. // if not pet egg
  6108. if (!pet_create_egg(sd, nameid))
  6109. {
  6110. unsigned char flag = 0;
  6111. if ((flag = pc_additem(sd, &item_tmp, get_count, LOG_TYPE_SCRIPT)))
  6112. {
  6113. clif_additem(sd, 0, 0, flag);
  6114. if( pc_candrop(sd,&item_tmp) )
  6115. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  6116. }
  6117. }
  6118. }
  6119. }
  6120. return SCRIPT_CMD_SUCCESS;
  6121. }
  6122. /** Gives rental item to player
  6123. * rentitem <item id>,<seconds>{,<account_id>}
  6124. * rentitem "<item name>",<seconds>{,<account_id>}
  6125. */
  6126. BUILDIN_FUNC(rentitem) {
  6127. struct map_session_data *sd;
  6128. struct script_data *data;
  6129. struct item it;
  6130. int seconds;
  6131. unsigned short nameid = 0;
  6132. unsigned char flag = 0;
  6133. data = script_getdata(st,2);
  6134. get_val(st,data);
  6135. if (!script_accid2sd(4,sd))
  6136. return SCRIPT_CMD_FAILURE;
  6137. if( data_isstring(data) )
  6138. {
  6139. const char *name = conv_str(st,data);
  6140. struct item_data *itd = itemdb_searchname(name);
  6141. if( itd == NULL )
  6142. {
  6143. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  6144. return SCRIPT_CMD_FAILURE;
  6145. }
  6146. nameid = itd->nameid;
  6147. }
  6148. else if( data_isint(data) )
  6149. {
  6150. nameid = conv_num(st,data);
  6151. if( nameid == 0 || !itemdb_exists(nameid) )
  6152. {
  6153. ShowError("buildin_rentitem: Nonexistant item %hu requested.\n", nameid);
  6154. return SCRIPT_CMD_FAILURE;
  6155. }
  6156. }
  6157. else
  6158. {
  6159. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  6160. return SCRIPT_CMD_FAILURE;
  6161. }
  6162. seconds = script_getnum(st,3);
  6163. memset(&it, 0, sizeof(it));
  6164. it.nameid = nameid;
  6165. it.identify = 1;
  6166. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6167. it.bound = BOUND_NONE;
  6168. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) )
  6169. {
  6170. clif_additem(sd, 0, 0, flag);
  6171. return SCRIPT_CMD_FAILURE;
  6172. }
  6173. return SCRIPT_CMD_SUCCESS;
  6174. }
  6175. /** Gives rental item to player with advanced option
  6176. * rentitem2 <item id>,<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6177. * rentitem2 "<item name>",<time>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account_id>};
  6178. */
  6179. BUILDIN_FUNC(rentitem2) {
  6180. struct map_session_data *sd;
  6181. struct script_data *data;
  6182. struct item it;
  6183. struct item_data *id;
  6184. int seconds;
  6185. unsigned short nameid = 0;
  6186. unsigned char flag = 0;
  6187. int iden,ref,attr,c1,c2,c3,c4;
  6188. data = script_getdata(st,2);
  6189. get_val(st,data);
  6190. if (!script_accid2sd(11,sd))
  6191. return SCRIPT_CMD_FAILURE;
  6192. if( data_isstring(data) ) {
  6193. const char *name = conv_str(st,data);
  6194. id = itemdb_searchname(name);
  6195. if( id == NULL ) {
  6196. ShowError("buildin_rentitem2: Nonexistant item %s requested.\n", name);
  6197. return SCRIPT_CMD_FAILURE;
  6198. }
  6199. nameid = id->nameid;
  6200. }
  6201. else if( data_isint(data) ) {
  6202. nameid = conv_num(st,data);
  6203. if( !(id = itemdb_search(nameid))) {
  6204. ShowError("buildin_rentitem2: Nonexistant item %hu requested.\n", nameid);
  6205. return SCRIPT_CMD_FAILURE;
  6206. }
  6207. }
  6208. else {
  6209. ShowError("buildin_rentitem2: invalid data type for argument #1 (%d).\n", data->type);
  6210. return SCRIPT_CMD_FAILURE;
  6211. }
  6212. seconds = script_getnum(st,3);
  6213. iden = script_getnum(st,4);
  6214. ref = script_getnum(st,5);
  6215. attr = script_getnum(st,6);
  6216. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6217. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6218. }
  6219. else if (id->type==IT_PETEGG) {
  6220. iden = 1;
  6221. ref = 0;
  6222. }
  6223. else {
  6224. iden = 1;
  6225. ref = attr = 0;
  6226. }
  6227. c1 = (short)script_getnum(st,7);
  6228. c2 = (short)script_getnum(st,8);
  6229. c3 = (short)script_getnum(st,9);
  6230. c4 = (short)script_getnum(st,10);
  6231. memset(&it, 0, sizeof(it));
  6232. it.nameid = nameid;
  6233. it.identify = iden;
  6234. it.refine = ref;
  6235. it.attribute = attr;
  6236. it.card[0] = (short)c1;
  6237. it.card[1] = (short)c2;
  6238. it.card[2] = (short)c3;
  6239. it.card[3] = (short)c4;
  6240. it.expire_time = (unsigned int)(time(NULL) + seconds);
  6241. if( (flag = pc_additem(sd, &it, 1, LOG_TYPE_SCRIPT)) ) {
  6242. clif_additem(sd, 0, 0, flag);
  6243. return SCRIPT_CMD_FAILURE;
  6244. }
  6245. return SCRIPT_CMD_SUCCESS;
  6246. }
  6247. /*==========================================
  6248. * gets an item with someone's name inscribed [Skotlex]
  6249. * getinscribeditem item_num, character_name
  6250. * Returned Qty is always 1, only works on equip-able
  6251. * equipment
  6252. *------------------------------------------*/
  6253. BUILDIN_FUNC(getnameditem)
  6254. {
  6255. unsigned short nameid;
  6256. struct item item_tmp;
  6257. TBL_PC *sd, *tsd;
  6258. struct script_data *data;
  6259. sd = script_rid2sd(st);
  6260. if (sd == NULL)
  6261. { //Player not attached!
  6262. script_pushint(st,0);
  6263. return SCRIPT_CMD_SUCCESS;
  6264. }
  6265. data=script_getdata(st,2);
  6266. get_val(st,data);
  6267. if( data_isstring(data) ){
  6268. const char *name=conv_str(st,data);
  6269. struct item_data *item_data = itemdb_searchname(name);
  6270. if( item_data == NULL)
  6271. { //Failed
  6272. script_pushint(st,0);
  6273. return SCRIPT_CMD_SUCCESS;
  6274. }
  6275. nameid = item_data->nameid;
  6276. }else
  6277. nameid = conv_num(st,data);
  6278. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  6279. { //Even though named stackable items "could" be risky, they are required for certain quests.
  6280. script_pushint(st,0);
  6281. return SCRIPT_CMD_SUCCESS;
  6282. }
  6283. data=script_getdata(st,3);
  6284. get_val(st,data);
  6285. if( data_isstring(data) ) //Char Name
  6286. tsd=map_nick2sd(conv_str(st,data));
  6287. else //Char Id was given
  6288. tsd=map_charid2sd(conv_num(st,data));
  6289. if( tsd == NULL )
  6290. { //Failed
  6291. script_pushint(st,0);
  6292. return SCRIPT_CMD_SUCCESS;
  6293. }
  6294. memset(&item_tmp,0,sizeof(item_tmp));
  6295. item_tmp.nameid=nameid;
  6296. item_tmp.amount=1;
  6297. item_tmp.identify=1;
  6298. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  6299. item_tmp.card[2]=tsd->status.char_id;
  6300. item_tmp.card[3]=tsd->status.char_id >> 16;
  6301. if(pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT)) {
  6302. script_pushint(st,0);
  6303. return SCRIPT_CMD_SUCCESS; //Failed to add item, we will not drop if they don't fit
  6304. }
  6305. script_pushint(st,1);
  6306. return SCRIPT_CMD_SUCCESS;
  6307. }
  6308. /*==========================================
  6309. * gets a random item ID from an item group [Skotlex]
  6310. * groupranditem <group_num>{,<sub_group>};
  6311. *------------------------------------------*/
  6312. BUILDIN_FUNC(grouprandomitem) {
  6313. struct s_item_group_entry *entry = NULL;
  6314. int sub_group = 1;
  6315. FETCH(3, sub_group);
  6316. entry = itemdb_get_randgroupitem(script_getnum(st,2),sub_group);
  6317. if (!entry) {
  6318. ShowError("buildin_grouprandomitem: Invalid item group with group_id '%d', sub_group '%d'.\n", script_getnum(st,2), sub_group);
  6319. script_pushint(st,UNKNOWN_ITEM_ID);
  6320. return SCRIPT_CMD_FAILURE;
  6321. }
  6322. script_pushint(st,entry->nameid);
  6323. return SCRIPT_CMD_SUCCESS;
  6324. }
  6325. /**
  6326. * makeitem <item id>,<amount>,"<map name>",<X>,<Y>;
  6327. * makeitem "<item name>",<amount>,"<map name>",<X>,<Y>;
  6328. */
  6329. BUILDIN_FUNC(makeitem) {
  6330. int16 nameid;
  6331. uint16 amount, flag = 0, x, y;
  6332. const char *mapname;
  6333. int m;
  6334. struct item item_tmp;
  6335. struct script_data *data;
  6336. data = script_getdata(st,2);
  6337. get_val(st,data);
  6338. if( data_isstring(data) ){
  6339. const char *name = conv_str(st,data);
  6340. struct item_data *item_data = itemdb_searchname(name);
  6341. if( item_data )
  6342. nameid = item_data->nameid;
  6343. else
  6344. nameid = UNKNOWN_ITEM_ID;
  6345. }
  6346. else
  6347. nameid = conv_num(st,data);
  6348. amount = script_getnum(st,3);
  6349. mapname = script_getstr(st,4);
  6350. x = script_getnum(st,5);
  6351. y = script_getnum(st,6);
  6352. if(strcmp(mapname,"this")==0) {
  6353. TBL_PC *sd;
  6354. sd = script_rid2sd(st);
  6355. if (!sd)
  6356. return SCRIPT_CMD_SUCCESS; //Failed...
  6357. m = sd->bl.m;
  6358. } else
  6359. m = map_mapname2mapid(mapname);
  6360. if(nameid<0) {
  6361. nameid = -nameid;
  6362. flag = 1;
  6363. }
  6364. if(nameid > 0) {
  6365. memset(&item_tmp,0,sizeof(item_tmp));
  6366. item_tmp.nameid = nameid;
  6367. if(!flag)
  6368. item_tmp.identify = 1;
  6369. else
  6370. item_tmp.identify = itemdb_isidentified(nameid);
  6371. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6372. }
  6373. return SCRIPT_CMD_SUCCESS;
  6374. }
  6375. /**
  6376. * makeitem2 <item id>,<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6377. * makeitem2 "<item name>",<amount>,"<map name>",<X>,<Y>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>;
  6378. */
  6379. BUILDIN_FUNC(makeitem2) {
  6380. uint16 nameid, amount, x, y;
  6381. const char *mapname;
  6382. int m;
  6383. struct item item_tmp;
  6384. struct script_data *data;
  6385. struct item_data *id;
  6386. data = script_getdata(st,2);
  6387. get_val(st,data);
  6388. if( data_isstring(data) ){
  6389. const char *name = conv_str(st,data);
  6390. struct item_data *item_data = itemdb_searchname(name);
  6391. if( item_data )
  6392. nameid = item_data->nameid;
  6393. else
  6394. nameid = UNKNOWN_ITEM_ID;
  6395. }
  6396. else
  6397. nameid = conv_num(st,data);
  6398. amount = script_getnum(st,3);
  6399. mapname = script_getstr(st,4);
  6400. x = script_getnum(st,5);
  6401. y = script_getnum(st,6);
  6402. if (strcmp(mapname,"this")==0) {
  6403. TBL_PC *sd;
  6404. sd = script_rid2sd(st);
  6405. if (!sd)
  6406. return SCRIPT_CMD_SUCCESS; //Failed...
  6407. m = sd->bl.m;
  6408. }
  6409. else
  6410. m = map_mapname2mapid(mapname);
  6411. if ((id = itemdb_search(nameid))) {
  6412. char iden, ref, attr;
  6413. memset(&item_tmp,0,sizeof(item_tmp));
  6414. item_tmp.nameid = nameid;
  6415. iden = (char)script_getnum(st,7);
  6416. ref = (char)script_getnum(st,8);
  6417. attr = (char)script_getnum(st,9);
  6418. if (id->type==IT_WEAPON || id->type==IT_ARMOR || id->type==IT_SHADOWGEAR) {
  6419. if(ref > MAX_REFINE) ref = MAX_REFINE;
  6420. }
  6421. else if (id->type==IT_PETEGG) {
  6422. iden = 1;
  6423. ref = 0;
  6424. }
  6425. else {
  6426. iden = 1;
  6427. ref = attr = 0;
  6428. }
  6429. item_tmp.identify = iden;
  6430. item_tmp.refine = ref;
  6431. item_tmp.attribute = attr;
  6432. item_tmp.card[0] = script_getnum(st,10);
  6433. item_tmp.card[1] = script_getnum(st,11);
  6434. item_tmp.card[2] = script_getnum(st,12);
  6435. item_tmp.card[3] = script_getnum(st,13);
  6436. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,4,0);
  6437. }
  6438. else
  6439. return SCRIPT_CMD_FAILURE;
  6440. return SCRIPT_CMD_SUCCESS;
  6441. }
  6442. /// Counts / deletes the current item given by idx.
  6443. /// Used by buildin_delitem_search
  6444. /// Relies on all input data being already fully valid.
  6445. static void buildin_delitem_delete(struct map_session_data* sd, int idx, int* amount, uint8 loc, bool delete_items)
  6446. {
  6447. int delamount;
  6448. struct item *itm = NULL;
  6449. switch(loc) {
  6450. case 1: // cart
  6451. itm = &sd->status.cart[idx];
  6452. break;
  6453. case 2: // storage
  6454. itm = &sd->status.storage.items[idx];
  6455. break;
  6456. default: //inventory
  6457. itm = &sd->status.inventory[idx];
  6458. break;
  6459. }
  6460. delamount = ( amount[0] < itm->amount ) ? amount[0] : itm->amount;
  6461. if( delete_items )
  6462. {
  6463. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET )
  6464. {// delete associated pet
  6465. intif_delete_petdata(MakeDWord(itm->card[1], itm->card[2]));
  6466. }
  6467. switch(loc) {
  6468. case 1:
  6469. pc_cart_delitem(sd,idx,delamount,0,LOG_TYPE_SCRIPT);
  6470. break;
  6471. case 2:
  6472. storage_delitem(sd,idx,delamount);
  6473. log_pick_pc(sd,LOG_TYPE_SCRIPT,-delamount,itm);
  6474. break;
  6475. default:
  6476. pc_delitem(sd, idx, delamount, 0, 0, LOG_TYPE_SCRIPT);
  6477. break;
  6478. }
  6479. }
  6480. amount[0]-= delamount;
  6481. }
  6482. /// Searches for item(s) and checks, if there is enough of them.
  6483. /// Used by delitem and delitem2
  6484. /// Relies on all input data being already fully valid.
  6485. /// @param exact_match will also match item attributes and cards, not just name id
  6486. /// @return true when all items could be deleted, false when there were not enough items to delete
  6487. static bool buildin_delitem_search(struct map_session_data* sd, struct item* it, bool exact_match, uint8 loc)
  6488. {
  6489. bool delete_items = false;
  6490. int i, amount, size;
  6491. struct item *items;
  6492. // prefer always non-equipped items
  6493. it->equip = 0;
  6494. // when searching for nameid only, prefer additionally
  6495. if( !exact_match )
  6496. {
  6497. // non-refined items
  6498. it->refine = 0;
  6499. // card-less items
  6500. memset(it->card, 0, sizeof(it->card));
  6501. }
  6502. switch(loc) {
  6503. case 1: // cart
  6504. size = MAX_CART;
  6505. items = sd->status.cart;
  6506. break;
  6507. case 2: // storage
  6508. size = MAX_STORAGE;
  6509. items = sd->status.storage.items;
  6510. break;
  6511. default: //inventory
  6512. size = MAX_INVENTORY;
  6513. items = sd->status.inventory;
  6514. break;
  6515. }
  6516. for(;;)
  6517. {
  6518. unsigned short important = 0;
  6519. amount = it->amount;
  6520. // 1st pass -- less important items / exact match
  6521. for( i = 0; amount && i < size; i++ )
  6522. {
  6523. struct item *itm = NULL;
  6524. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6525. {// wrong/invalid item
  6526. continue;
  6527. }
  6528. if( itm->equip != it->equip || itm->refine != it->refine )
  6529. {// not matching attributes
  6530. important++;
  6531. continue;
  6532. }
  6533. if( exact_match )
  6534. {
  6535. if( itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6536. {// not matching exact attributes
  6537. continue;
  6538. }
  6539. }
  6540. else
  6541. {
  6542. if( itemdb_type(itm->nameid) == IT_PETEGG )
  6543. {
  6544. if( itm->card[0] == CARD0_PET && CheckForCharServer() )
  6545. {// pet which cannot be deleted
  6546. continue;
  6547. }
  6548. }
  6549. else if( memcmp(itm->card, it->card, sizeof(itm->card)) )
  6550. {// named/carded item
  6551. important++;
  6552. continue;
  6553. }
  6554. }
  6555. // count / delete item
  6556. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6557. }
  6558. // 2nd pass -- any matching item
  6559. if( amount == 0 || important == 0 )
  6560. {// either everything was already consumed or no items were skipped
  6561. ;
  6562. }
  6563. else for( i = 0; amount && i < size; i++ )
  6564. {
  6565. struct item *itm = NULL;
  6566. if( !&items[i] || !(itm = &items[i])->nameid || itm->nameid != it->nameid )
  6567. {// wrong/invalid item
  6568. continue;
  6569. }
  6570. if( itemdb_type(itm->nameid) == IT_PETEGG && itm->card[0] == CARD0_PET && CheckForCharServer() )
  6571. {// pet which cannot be deleted
  6572. continue;
  6573. }
  6574. if( exact_match )
  6575. {
  6576. if( itm->refine != it->refine || itm->identify != it->identify || itm->attribute != it->attribute || memcmp(itm->card, it->card, sizeof(itm->card)) )
  6577. {// not matching attributes
  6578. continue;
  6579. }
  6580. }
  6581. // count / delete item
  6582. buildin_delitem_delete(sd, i, &amount, loc, delete_items);
  6583. }
  6584. if( amount )
  6585. {// not enough items
  6586. return false;
  6587. }
  6588. else if( delete_items )
  6589. {// we are done with the work
  6590. return true;
  6591. }
  6592. else
  6593. {// get rid of the items now
  6594. delete_items = true;
  6595. }
  6596. }
  6597. }
  6598. /// Deletes items from the target/attached player.
  6599. /// Prioritizes ordinary items.
  6600. ///
  6601. /// delitem <item id>,<amount>{,<account id>}
  6602. /// delitem "<item name>",<amount>{,<account id>}
  6603. /// cartdelitem <item id>,<amount>{,<account id>}
  6604. /// cartdelitem "<item name>",<amount>{,<account id>}
  6605. /// storagedelitem <item id>,<amount>{,<account id>}
  6606. /// storagedelitem "<item name>",<amount>{,<account id>}
  6607. BUILDIN_FUNC(delitem)
  6608. {
  6609. TBL_PC *sd;
  6610. struct item it;
  6611. struct script_data *data;
  6612. uint8 loc = 0;
  6613. char* command = (char*)script_getfuncname(st);
  6614. if(!strncmp(command, "cart", 4))
  6615. loc = 1;
  6616. else if(!strncmp(command, "storage", 7))
  6617. loc = 2;
  6618. //TODO: 3 - Guild Storage
  6619. if( script_hasdata(st,4) )
  6620. {
  6621. uint32 account_id = script_getnum(st,4);
  6622. sd = map_id2sd(account_id); // <account id>
  6623. if( sd == NULL )
  6624. {
  6625. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6626. st->state = END;
  6627. return SCRIPT_CMD_FAILURE;
  6628. }
  6629. }
  6630. else
  6631. {
  6632. sd = script_rid2sd(st);// attached player
  6633. if( sd == NULL )
  6634. return SCRIPT_CMD_SUCCESS;
  6635. }
  6636. if (loc == 1 && !pc_iscarton(sd)) {
  6637. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6638. return SCRIPT_CMD_FAILURE;
  6639. }
  6640. data = script_getdata(st,2);
  6641. get_val(st,data);
  6642. if( data_isstring(data) )
  6643. {
  6644. const char* item_name = conv_str(st,data);
  6645. struct item_data* id = itemdb_searchname(item_name);
  6646. if( id == NULL )
  6647. {
  6648. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6649. st->state = END;
  6650. return SCRIPT_CMD_FAILURE;
  6651. }
  6652. it.nameid = id->nameid;// "<item name>"
  6653. }
  6654. else
  6655. {
  6656. it.nameid = conv_num(st,data);// <item id>
  6657. if( !itemdb_exists( it.nameid ) )
  6658. {
  6659. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6660. st->state = END;
  6661. return SCRIPT_CMD_FAILURE;
  6662. }
  6663. }
  6664. it.amount=script_getnum(st,3);
  6665. if( it.amount <= 0 )
  6666. return SCRIPT_CMD_SUCCESS;// nothing to do
  6667. if( buildin_delitem_search(sd, &it, false, loc) )
  6668. {// success
  6669. return SCRIPT_CMD_SUCCESS;
  6670. }
  6671. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6672. st->state = END;
  6673. st->mes_active = 0;
  6674. clif_scriptclose(sd, st->oid);
  6675. return SCRIPT_CMD_FAILURE;
  6676. }
  6677. /// Deletes items from the target/attached player.
  6678. ///
  6679. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6680. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6681. /// cartdelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6682. /// cartdelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6683. /// storagedelitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6684. /// storagedelitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  6685. BUILDIN_FUNC(delitem2)
  6686. {
  6687. TBL_PC *sd;
  6688. struct item it;
  6689. struct script_data *data;
  6690. uint8 loc = 0;
  6691. char* command = (char*)script_getfuncname(st);
  6692. if(!strncmp(command, "cart", 4))
  6693. loc = 1;
  6694. else if(!strncmp(command, "storage", 7))
  6695. loc = 2;
  6696. //TODO: 3 - Guild Storage
  6697. if( script_hasdata(st,11) )
  6698. {
  6699. uint32 account_id = script_getnum(st,11);
  6700. sd = map_id2sd(account_id); // <account id>
  6701. if( sd == NULL )
  6702. {
  6703. ShowError("buildin_%s: player not found (AID=%d).\n", command, account_id);
  6704. st->state = END;
  6705. return SCRIPT_CMD_FAILURE;
  6706. }
  6707. }
  6708. else
  6709. {
  6710. sd = script_rid2sd(st);// attached player
  6711. if( sd == NULL )
  6712. return SCRIPT_CMD_SUCCESS;
  6713. }
  6714. if (loc == 1 && !pc_iscarton(sd)) {
  6715. ShowError("buildin_cartdelitem: player doesn't have cart (CID=%d).\n", sd->status.char_id);
  6716. script_pushint(st,-1);
  6717. return SCRIPT_CMD_FAILURE;
  6718. }
  6719. data = script_getdata(st,2);
  6720. get_val(st,data);
  6721. if( data_isstring(data) )
  6722. {
  6723. const char* item_name = conv_str(st,data);
  6724. struct item_data* id = itemdb_searchname(item_name);
  6725. if( id == NULL )
  6726. {
  6727. ShowError("buildin_%s: unknown item \"%s\".\n", command, item_name);
  6728. st->state = END;
  6729. return SCRIPT_CMD_FAILURE;
  6730. }
  6731. it.nameid = id->nameid;// "<item name>"
  6732. }
  6733. else
  6734. {
  6735. it.nameid = conv_num(st,data);// <item id>
  6736. if( !itemdb_exists( it.nameid ) )
  6737. {
  6738. ShowError("buildin_%s: unknown item \"%hu\".\n", command, it.nameid);
  6739. st->state = END;
  6740. return SCRIPT_CMD_FAILURE;
  6741. }
  6742. }
  6743. it.amount=script_getnum(st,3);
  6744. it.identify=script_getnum(st,4);
  6745. it.refine=script_getnum(st,5);
  6746. it.attribute=script_getnum(st,6);
  6747. it.card[0]=(short)script_getnum(st,7);
  6748. it.card[1]=(short)script_getnum(st,8);
  6749. it.card[2]=(short)script_getnum(st,9);
  6750. it.card[3]=(short)script_getnum(st,10);
  6751. if( it.amount <= 0 )
  6752. return SCRIPT_CMD_SUCCESS;// nothing to do
  6753. if( buildin_delitem_search(sd, &it, true, loc) )
  6754. {// success
  6755. return SCRIPT_CMD_SUCCESS;
  6756. }
  6757. ShowError("buildin_%s: failed to delete %d items (AID=%d item_id=%hu).\n", command, it.amount, sd->status.account_id, it.nameid);
  6758. st->state = END;
  6759. st->mes_active = 0;
  6760. clif_scriptclose(sd, st->oid);
  6761. return SCRIPT_CMD_FAILURE;
  6762. }
  6763. /*==========================================
  6764. * Enables/Disables use of items while in an NPC [Skotlex]
  6765. *------------------------------------------*/
  6766. BUILDIN_FUNC(enableitemuse)
  6767. {
  6768. TBL_PC *sd;
  6769. sd=script_rid2sd(st);
  6770. if (sd)
  6771. st->npc_item_flag = sd->npc_item_flag = 1;
  6772. return SCRIPT_CMD_SUCCESS;
  6773. }
  6774. BUILDIN_FUNC(disableitemuse)
  6775. {
  6776. TBL_PC *sd;
  6777. sd=script_rid2sd(st);
  6778. if (sd)
  6779. st->npc_item_flag = sd->npc_item_flag = 0;
  6780. return SCRIPT_CMD_SUCCESS;
  6781. }
  6782. /*==========================================
  6783. * Returns a character's specified stat.
  6784. * Check pc_readparam for available options.
  6785. * readparam <param>{,"<nick>"}
  6786. *------------------------------------------*/
  6787. BUILDIN_FUNC(readparam)
  6788. {
  6789. int type;
  6790. struct script_data *data = script_getdata(st, 2);
  6791. TBL_PC *sd;
  6792. if (!script_nick2sd(3,sd)) {
  6793. script_pushint(st,-1);
  6794. return SCRIPT_CMD_FAILURE;
  6795. }
  6796. if (data->type == C_NAME) { // If using constant name, just get the val
  6797. get_val_(st, data, sd);
  6798. script_pushint(st, (int)data->u.num);
  6799. return SCRIPT_CMD_SUCCESS;
  6800. }
  6801. type = script_getnum(st, 2);
  6802. script_pushint(st,pc_readparam(sd,type));
  6803. return SCRIPT_CMD_SUCCESS;
  6804. }
  6805. /*==========================================
  6806. * Return charid identification
  6807. * return by @num :
  6808. * 0 : char_id
  6809. * 1 : party_id
  6810. * 2 : guild_id
  6811. * 3 : account_id
  6812. * 4 : bg_id
  6813. *------------------------------------------*/
  6814. BUILDIN_FUNC(getcharid)
  6815. {
  6816. int num;
  6817. TBL_PC *sd;
  6818. num = script_getnum(st,2);
  6819. if( script_hasdata(st,3) )
  6820. sd=map_nick2sd(script_getstr(st,3));
  6821. else
  6822. sd=script_rid2sd(st);
  6823. if(sd==NULL){
  6824. script_pushint(st,0); //return 0, according docs
  6825. return SCRIPT_CMD_SUCCESS;
  6826. }
  6827. switch( num ) {
  6828. case 0: script_pushint(st,sd->status.char_id); break;
  6829. case 1: script_pushint(st,sd->status.party_id); break;
  6830. case 2: script_pushint(st,sd->status.guild_id); break;
  6831. case 3: script_pushint(st,sd->status.account_id); break;
  6832. case 4: script_pushint(st,sd->bg_id); break;
  6833. default:
  6834. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  6835. script_pushint(st,0);
  6836. break;
  6837. }
  6838. return SCRIPT_CMD_SUCCESS;
  6839. }
  6840. /*==========================================
  6841. * returns the GID of an NPC
  6842. *------------------------------------------*/
  6843. BUILDIN_FUNC(getnpcid)
  6844. {
  6845. int num = script_getnum(st,2);
  6846. struct npc_data* nd = NULL;
  6847. if( script_hasdata(st,3) )
  6848. {// unique npc name
  6849. if( ( nd = npc_name2id(script_getstr(st,3)) ) == NULL )
  6850. {
  6851. ShowError("buildin_getnpcid: No such NPC '%s'.\n", script_getstr(st,3));
  6852. script_pushint(st,0);
  6853. return SCRIPT_CMD_FAILURE;
  6854. }
  6855. }
  6856. switch (num) {
  6857. case 0:
  6858. script_pushint(st,nd ? nd->bl.id : st->oid);
  6859. break;
  6860. default:
  6861. ShowError("buildin_getnpcid: invalid parameter (%d).\n", num);
  6862. script_pushint(st,0);
  6863. return SCRIPT_CMD_FAILURE;
  6864. }
  6865. return SCRIPT_CMD_SUCCESS;
  6866. }
  6867. /*==========================================
  6868. * Return the name of the party_id
  6869. * null if not found
  6870. *------------------------------------------*/
  6871. BUILDIN_FUNC(getpartyname)
  6872. {
  6873. int party_id;
  6874. struct party_data* p;
  6875. party_id = script_getnum(st,2);
  6876. if( ( p = party_search(party_id) ) != NULL )
  6877. {
  6878. script_pushstrcopy(st,p->party.name);
  6879. }
  6880. else
  6881. {
  6882. script_pushconststr(st,"null");
  6883. }
  6884. return SCRIPT_CMD_SUCCESS;
  6885. }
  6886. /*==========================================
  6887. * Get the information of the members of a party by type
  6888. * @party_id, @type
  6889. * return by @type :
  6890. * - : nom des membres
  6891. * 1 : char_id des membres
  6892. * 2 : account_id des membres
  6893. *------------------------------------------*/
  6894. BUILDIN_FUNC(getpartymember)
  6895. {
  6896. struct party_data *p;
  6897. uint8 j = 0;
  6898. p = party_search(script_getnum(st,2));
  6899. if (p != NULL) {
  6900. uint8 i, type = 0;
  6901. struct script_data *data = NULL;
  6902. char *varname = NULL;
  6903. if (script_hasdata(st,3))
  6904. type = script_getnum(st,3);
  6905. if (script_hasdata(st,4)) {
  6906. data = script_getdata(st, 4);
  6907. if (!data_isreference(data)) {
  6908. ShowError("buildin_getpartymember: Error in argument! Please give a variable to store values in.\n");
  6909. return SCRIPT_CMD_FAILURE;
  6910. }
  6911. varname = reference_getname(data);
  6912. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  6913. ShowError("buildin_getpartymember: The array %s is not string type.\n", varname);
  6914. return SCRIPT_CMD_FAILURE;
  6915. }
  6916. }
  6917. for (i = 0; i < MAX_PARTY; i++) {
  6918. if (p->party.member[i].account_id) {
  6919. switch (type) {
  6920. case 2:
  6921. if (data)
  6922. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].account_id), data->ref);
  6923. else
  6924. mapreg_setreg(reference_uid(add_str("$@partymemberaid"), j),p->party.member[i].account_id);
  6925. break;
  6926. case 1:
  6927. if (data)
  6928. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].char_id), data->ref);
  6929. else
  6930. mapreg_setreg(reference_uid(add_str("$@partymembercid"), j),p->party.member[i].char_id);
  6931. break;
  6932. default:
  6933. if (data)
  6934. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(p->party.member[i].name), data->ref);
  6935. else
  6936. mapreg_setregstr(reference_uid(add_str("$@partymembername$"), j),p->party.member[i].name);
  6937. break;
  6938. }
  6939. j++;
  6940. }
  6941. }
  6942. }
  6943. mapreg_setreg(add_str("$@partymembercount"),j);
  6944. script_pushint(st, j);
  6945. return SCRIPT_CMD_SUCCESS;
  6946. }
  6947. /*==========================================
  6948. * Retrieves party leader. if flag is specified,
  6949. * return some of the leader data. Otherwise, return name.
  6950. *------------------------------------------*/
  6951. BUILDIN_FUNC(getpartyleader)
  6952. {
  6953. int party_id, type = 0, i=0;
  6954. struct party_data *p;
  6955. party_id=script_getnum(st,2);
  6956. if( script_hasdata(st,3) )
  6957. type=script_getnum(st,3);
  6958. p=party_search(party_id);
  6959. if (p) //Search leader
  6960. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6961. if (!p || i == MAX_PARTY) { //leader not found
  6962. if (type)
  6963. script_pushint(st,-1);
  6964. else
  6965. script_pushconststr(st,"null");
  6966. return SCRIPT_CMD_SUCCESS;
  6967. }
  6968. switch (type) {
  6969. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6970. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6971. case 3: script_pushint(st,p->party.member[i].class_); break;
  6972. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  6973. case 5: script_pushint(st,p->party.member[i].lv); break;
  6974. default: script_pushstrcopy(st,p->party.member[i].name); break;
  6975. }
  6976. return SCRIPT_CMD_SUCCESS;
  6977. }
  6978. /*==========================================
  6979. * Return the name of the @guild_id
  6980. * null if not found
  6981. *------------------------------------------*/
  6982. BUILDIN_FUNC(getguildname)
  6983. {
  6984. int guild_id;
  6985. struct guild* g;
  6986. guild_id = script_getnum(st,2);
  6987. if( ( g = guild_search(guild_id) ) != NULL )
  6988. script_pushstrcopy(st,g->name);
  6989. else
  6990. script_pushconststr(st,"null");
  6991. return SCRIPT_CMD_SUCCESS;
  6992. }
  6993. /*==========================================
  6994. * Return the name of the guild master of @guild_id
  6995. * null if not found
  6996. *------------------------------------------*/
  6997. BUILDIN_FUNC(getguildmaster)
  6998. {
  6999. int guild_id;
  7000. struct guild* g;
  7001. guild_id = script_getnum(st,2);
  7002. if( ( g = guild_search(guild_id) ) != NULL )
  7003. script_pushstrcopy(st,g->member[0].name);
  7004. else
  7005. script_pushconststr(st,"null");
  7006. return SCRIPT_CMD_SUCCESS;
  7007. }
  7008. BUILDIN_FUNC(getguildmasterid)
  7009. {
  7010. int guild_id;
  7011. struct guild* g;
  7012. guild_id = script_getnum(st,2);
  7013. if( ( g = guild_search(guild_id) ) != NULL )
  7014. script_pushint(st,g->member[0].char_id);
  7015. else
  7016. script_pushint(st,0);
  7017. return SCRIPT_CMD_SUCCESS;
  7018. }
  7019. /*==========================================
  7020. * Get char string information by type :
  7021. * Return by @type :
  7022. * 0 : char_name
  7023. * 1 : party_name or ""
  7024. * 2 : guild_name or ""
  7025. * 3 : map_name
  7026. * - : ""
  7027. * strcharinfo(<type>{,<char_id>})
  7028. *------------------------------------------*/
  7029. BUILDIN_FUNC(strcharinfo)
  7030. {
  7031. TBL_PC *sd;
  7032. int num;
  7033. struct guild* g;
  7034. struct party_data* p;
  7035. if (!script_charid2sd(3,sd)) {
  7036. script_pushconststr(st,"");
  7037. return SCRIPT_CMD_FAILURE;
  7038. }
  7039. num=script_getnum(st,2);
  7040. switch(num){
  7041. case 0:
  7042. script_pushstrcopy(st,sd->status.name);
  7043. break;
  7044. case 1:
  7045. if( ( p = party_search(sd->status.party_id) ) != NULL ) {
  7046. script_pushstrcopy(st,p->party.name);
  7047. } else {
  7048. script_pushconststr(st,"");
  7049. }
  7050. break;
  7051. case 2:
  7052. if( ( g = sd->guild ) != NULL ) {
  7053. script_pushstrcopy(st,g->name);
  7054. } else {
  7055. script_pushconststr(st,"");
  7056. }
  7057. break;
  7058. case 3:
  7059. script_pushconststr(st,map[sd->bl.m].name);
  7060. break;
  7061. default:
  7062. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  7063. script_pushconststr(st,"");
  7064. break;
  7065. }
  7066. return SCRIPT_CMD_SUCCESS;
  7067. }
  7068. /*==========================================
  7069. * Get npc string information by type
  7070. * return by @type:
  7071. * 0 : name
  7072. * 1 : str#
  7073. * 2 : #str
  7074. * 3 : ::str
  7075. * 4 : map name
  7076. *------------------------------------------*/
  7077. BUILDIN_FUNC(strnpcinfo)
  7078. {
  7079. TBL_NPC* nd;
  7080. int num;
  7081. char *buf,*name=NULL;
  7082. nd = map_id2nd(st->oid);
  7083. if (!nd) {
  7084. script_pushconststr(st, "");
  7085. return SCRIPT_CMD_SUCCESS;
  7086. }
  7087. num = script_getnum(st,2);
  7088. switch(num){
  7089. case 0: // display name
  7090. name = aStrdup(nd->name);
  7091. break;
  7092. case 1: // visible part of display name
  7093. if((buf = strchr(nd->name,'#')) != NULL)
  7094. {
  7095. name = aStrdup(nd->name);
  7096. name[buf - nd->name] = 0;
  7097. } else // Return the name, there is no '#' present
  7098. name = aStrdup(nd->name);
  7099. break;
  7100. case 2: // # fragment
  7101. if((buf = strchr(nd->name,'#')) != NULL)
  7102. name = aStrdup(buf+1);
  7103. break;
  7104. case 3: // unique name
  7105. name = aStrdup(nd->exname);
  7106. break;
  7107. case 4: // map name
  7108. name = aStrdup(map[nd->bl.m].name);
  7109. break;
  7110. }
  7111. if(name)
  7112. script_pushstr(st, name);
  7113. else
  7114. script_pushconststr(st, "");
  7115. return SCRIPT_CMD_SUCCESS;
  7116. }
  7117. /// aegis->athena slot position conversion table
  7118. /// Index is used by EQI_ in db/const.txt
  7119. static unsigned int equip[] = {
  7120. EQP_HEAD_TOP,
  7121. EQP_ARMOR,
  7122. EQP_HAND_L,
  7123. EQP_HAND_R,
  7124. EQP_GARMENT,
  7125. EQP_SHOES,
  7126. EQP_ACC_L,
  7127. EQP_ACC_R,
  7128. EQP_HEAD_MID,
  7129. EQP_HEAD_LOW,
  7130. EQP_COSTUME_HEAD_LOW,
  7131. EQP_COSTUME_HEAD_MID,
  7132. EQP_COSTUME_HEAD_TOP,
  7133. EQP_COSTUME_GARMENT,
  7134. EQP_AMMO,
  7135. EQP_SHADOW_ARMOR,
  7136. EQP_SHADOW_WEAPON,
  7137. EQP_SHADOW_SHIELD,
  7138. EQP_SHADOW_SHOES,
  7139. EQP_SHADOW_ACC_R,
  7140. EQP_SHADOW_ACC_L
  7141. };
  7142. /**
  7143. * getequipid(<equipment slot>{,<char_id>})
  7144. **/
  7145. BUILDIN_FUNC(getequipid)
  7146. {
  7147. int i, num;
  7148. TBL_PC* sd;
  7149. struct item_data* item;
  7150. if (!script_charid2sd(3, sd)) {
  7151. script_pushint(st,-1);
  7152. return SCRIPT_CMD_FAILURE;
  7153. }
  7154. num = script_getnum(st,2) - 1;
  7155. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7156. {
  7157. script_pushint(st,-1);
  7158. return SCRIPT_CMD_FAILURE;
  7159. }
  7160. // get inventory position of item
  7161. i = pc_checkequip(sd,equip[num]);
  7162. if( i < 0 )
  7163. {
  7164. script_pushint(st,-1);
  7165. return SCRIPT_CMD_SUCCESS;
  7166. }
  7167. item = sd->inventory_data[i];
  7168. if( item != 0 )
  7169. script_pushint(st,item->nameid);
  7170. else
  7171. script_pushint(st,0);
  7172. return SCRIPT_CMD_SUCCESS;
  7173. }
  7174. /*==========================================
  7175. * getequipuniqueid(<equipment slot>{,<char_id>})
  7176. *------------------------------------------*/
  7177. BUILDIN_FUNC(getequipuniqueid)
  7178. {
  7179. int i, num;
  7180. TBL_PC* sd;
  7181. struct item* item;
  7182. if (!script_charid2sd(3, sd)) {
  7183. script_pushconststr(st, "");
  7184. return SCRIPT_CMD_FAILURE;
  7185. }
  7186. num = script_getnum(st,2) - 1;
  7187. if (num < 0 || num >= ARRAYLENGTH(equip)) {
  7188. script_pushconststr(st, "");
  7189. return SCRIPT_CMD_FAILURE;
  7190. }
  7191. // get inventory position of item
  7192. i = pc_checkequip(sd,equip[num]);
  7193. if (i < 0) {
  7194. script_pushconststr(st, "");
  7195. return SCRIPT_CMD_FAILURE;
  7196. }
  7197. item = &sd->status.inventory[i];
  7198. if (item != 0) {
  7199. int maxlen = 256;
  7200. char *buf = (char *)aMalloc(maxlen*sizeof(char));
  7201. memset(buf, 0, maxlen);
  7202. snprintf(buf, maxlen-1, "%llu", (unsigned long long)item->unique_id);
  7203. script_pushstr(st, buf);
  7204. } else
  7205. script_pushconststr(st, "");
  7206. return SCRIPT_CMD_SUCCESS;
  7207. }
  7208. /*==========================================
  7209. * Get the equipement name at pos
  7210. * return item jname or ""
  7211. * getequipname(<equipment slot>{,<char_id>})
  7212. *------------------------------------------*/
  7213. BUILDIN_FUNC(getequipname)
  7214. {
  7215. int i, num;
  7216. TBL_PC* sd;
  7217. struct item_data* item;
  7218. if (!script_charid2sd(3, sd)) {
  7219. script_pushconststr(st, "");
  7220. return SCRIPT_CMD_FAILURE;
  7221. }
  7222. num = script_getnum(st,2) - 1;
  7223. if( num < 0 || num >= ARRAYLENGTH(equip) )
  7224. {
  7225. script_pushconststr(st,"");
  7226. return SCRIPT_CMD_SUCCESS;
  7227. }
  7228. // get inventory position of item
  7229. i = pc_checkequip(sd,equip[num]);
  7230. if( i < 0 )
  7231. {
  7232. script_pushconststr(st,"");
  7233. return SCRIPT_CMD_SUCCESS;
  7234. }
  7235. item = sd->inventory_data[i];
  7236. if( item != 0 )
  7237. script_pushstrcopy(st,item->jname);
  7238. else
  7239. script_pushconststr(st,"");
  7240. return SCRIPT_CMD_SUCCESS;
  7241. }
  7242. /*==========================================
  7243. * getbrokenid [Valaris]
  7244. * getbrokenid(<number>{,<char_id>})
  7245. *------------------------------------------*/
  7246. BUILDIN_FUNC(getbrokenid)
  7247. {
  7248. int i,num,id=0,brokencounter=0;
  7249. TBL_PC *sd;
  7250. if (!script_charid2sd(3, sd)) {
  7251. script_pushint(st,0);
  7252. return SCRIPT_CMD_FAILURE;
  7253. }
  7254. num=script_getnum(st,2);
  7255. for(i=0; i<MAX_INVENTORY; i++) {
  7256. if(sd->status.inventory[i].attribute){
  7257. brokencounter++;
  7258. if(num==brokencounter){
  7259. id=sd->status.inventory[i].nameid;
  7260. break;
  7261. }
  7262. }
  7263. }
  7264. script_pushint(st,id);
  7265. return SCRIPT_CMD_SUCCESS;
  7266. }
  7267. /*==========================================
  7268. * repair [Valaris]
  7269. * repair <num>{,<char_id>};
  7270. *------------------------------------------*/
  7271. BUILDIN_FUNC(repair)
  7272. {
  7273. int i,num;
  7274. int repaircounter=0;
  7275. TBL_PC *sd;
  7276. if (!script_charid2sd(3,sd))
  7277. return SCRIPT_CMD_FAILURE;
  7278. num=script_getnum(st,2);
  7279. for(i=0; i<MAX_INVENTORY; i++) {
  7280. if(sd->status.inventory[i].attribute){
  7281. repaircounter++;
  7282. if(num==repaircounter){
  7283. sd->status.inventory[i].attribute=0;
  7284. clif_equiplist(sd);
  7285. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  7286. clif_misceffect(&sd->bl, 3);
  7287. break;
  7288. }
  7289. }
  7290. }
  7291. return SCRIPT_CMD_SUCCESS;
  7292. }
  7293. /*==========================================
  7294. * repairall {<char_id>}
  7295. *------------------------------------------*/
  7296. BUILDIN_FUNC(repairall)
  7297. {
  7298. int i, repaircounter = 0;
  7299. TBL_PC *sd;
  7300. if (!script_charid2sd(2,sd))
  7301. return SCRIPT_CMD_FAILURE;
  7302. for(i = 0; i < MAX_INVENTORY; i++)
  7303. {
  7304. if(sd->status.inventory[i].nameid && sd->status.inventory[i].attribute)
  7305. {
  7306. sd->status.inventory[i].attribute = 0;
  7307. clif_produceeffect(sd,0,sd->status.inventory[i].nameid);
  7308. repaircounter++;
  7309. }
  7310. }
  7311. if(repaircounter)
  7312. {
  7313. clif_misceffect(&sd->bl, 3);
  7314. clif_equiplist(sd);
  7315. }
  7316. return SCRIPT_CMD_SUCCESS;
  7317. }
  7318. /*==========================================
  7319. * Chk if player have something equiped at pos
  7320. * getequipisequiped <pos>{,<char_id>}
  7321. *------------------------------------------*/
  7322. BUILDIN_FUNC(getequipisequiped)
  7323. {
  7324. int i = -1,num;
  7325. TBL_PC *sd;
  7326. num = script_getnum(st,2);
  7327. if (!script_charid2sd(3,sd))
  7328. return SCRIPT_CMD_FAILURE;
  7329. if (num > 0 && num <= ARRAYLENGTH(equip))
  7330. i=pc_checkequip(sd,equip[num-1]);
  7331. if(i >= 0)
  7332. script_pushint(st,1);
  7333. else
  7334. script_pushint(st,0);
  7335. return SCRIPT_CMD_SUCCESS;
  7336. }
  7337. /*==========================================
  7338. * Chk if the player have something equiped at pos
  7339. * if so chk if this item ain't marked not refinable or rental
  7340. * return (npc)
  7341. * 1 : true
  7342. * 0 : false
  7343. * getequipisenableref(<equipment slot>{,<char_id>})
  7344. *------------------------------------------*/
  7345. BUILDIN_FUNC(getequipisenableref)
  7346. {
  7347. int i = -1,num;
  7348. TBL_PC *sd;
  7349. num = script_getnum(st,2);
  7350. if (!script_charid2sd(3, sd)) {
  7351. script_pushint(st,0);
  7352. return SCRIPT_CMD_FAILURE;
  7353. }
  7354. if( num > 0 && num <= ARRAYLENGTH(equip) )
  7355. i = pc_checkequip(sd,equip[num-1]);
  7356. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  7357. script_pushint(st,1);
  7358. else
  7359. script_pushint(st,0);
  7360. return SCRIPT_CMD_SUCCESS;
  7361. }
  7362. /*==========================================
  7363. * Get the item refined value at pos
  7364. * return (npc)
  7365. * x : refine amount
  7366. * 0 : false (not refined)
  7367. * getequiprefinerycnt(<equipment slot>{,<char_id>})
  7368. *------------------------------------------*/
  7369. BUILDIN_FUNC(getequiprefinerycnt)
  7370. {
  7371. int i = -1,num;
  7372. TBL_PC *sd;
  7373. num = script_getnum(st,2);
  7374. if (!script_charid2sd(3, sd)) {
  7375. script_pushint(st,0);
  7376. return SCRIPT_CMD_FAILURE;
  7377. }
  7378. if (num > 0 && num <= ARRAYLENGTH(equip))
  7379. i=pc_checkequip(sd,equip[num-1]);
  7380. if(i >= 0)
  7381. script_pushint(st,sd->status.inventory[i].refine);
  7382. else
  7383. script_pushint(st,0);
  7384. return SCRIPT_CMD_SUCCESS;
  7385. }
  7386. /*==========================================
  7387. * Get the weapon level value at pos
  7388. * (pos should normally only be EQI_HAND_L or EQI_HAND_R)
  7389. * return (npc)
  7390. * x : weapon level
  7391. * 0 : false
  7392. * getequipweaponlv(<equipment slot>{,<char_id>})
  7393. *------------------------------------------*/
  7394. BUILDIN_FUNC(getequipweaponlv)
  7395. {
  7396. int i = -1,num;
  7397. TBL_PC *sd;
  7398. num = script_getnum(st,2);
  7399. if (!script_charid2sd(3, sd)) {
  7400. script_pushint(st,0);
  7401. return SCRIPT_CMD_FAILURE;
  7402. }
  7403. if (num > 0 && num <= ARRAYLENGTH(equip))
  7404. i=pc_checkequip(sd,equip[num-1]);
  7405. if(i >= 0 && sd->inventory_data[i])
  7406. script_pushint(st,sd->inventory_data[i]->wlv);
  7407. else
  7408. script_pushint(st,0);
  7409. return SCRIPT_CMD_SUCCESS;
  7410. }
  7411. /*==========================================
  7412. * Get the item refine chance (from refine.txt) for item at pos
  7413. * return (npc)
  7414. * x : refine chance
  7415. * 0 : false (max refine level or unequip..)
  7416. * getequippercentrefinery(<equipment slot>{,<char_id>})
  7417. *------------------------------------------*/
  7418. BUILDIN_FUNC(getequippercentrefinery)
  7419. {
  7420. int i = -1,num;
  7421. TBL_PC *sd;
  7422. num = script_getnum(st,2);
  7423. if (!script_charid2sd(3, sd)) {
  7424. script_pushint(st,0);
  7425. return SCRIPT_CMD_FAILURE;
  7426. }
  7427. if (num > 0 && num <= ARRAYLENGTH(equip))
  7428. i = pc_checkequip(sd,equip[num-1]);
  7429. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  7430. script_pushint(st,status_get_refine_chance((enum refine_type)itemdb_wlv(sd->status.inventory[i].nameid), (int)sd->status.inventory[i].refine));
  7431. else
  7432. script_pushint(st,0);
  7433. return SCRIPT_CMD_SUCCESS;
  7434. }
  7435. /*==========================================
  7436. * Refine +1 item at pos and log and display refine
  7437. * successrefitem <equipment slot>{,<count>{,<char_id>}}
  7438. *------------------------------------------*/
  7439. BUILDIN_FUNC(successrefitem) {
  7440. short i = -1, up = 1;
  7441. int pos;
  7442. TBL_PC *sd;
  7443. pos = script_getnum(st,2);
  7444. if (!script_charid2sd(4, sd)) {
  7445. script_pushint(st, -1);
  7446. return SCRIPT_CMD_FAILURE;
  7447. }
  7448. if (script_hasdata(st, 3))
  7449. up = script_getnum(st, 3);
  7450. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7451. i = pc_checkequip(sd,equip[pos-1]);
  7452. if (i >= 0) {
  7453. unsigned int ep = sd->status.inventory[i].equip;
  7454. //Logs items, got from (N)PC scripts [Lupus]
  7455. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7456. if (sd->status.inventory[i].refine >= MAX_REFINE) {
  7457. script_pushint(st, MAX_REFINE);
  7458. return SCRIPT_CMD_SUCCESS;
  7459. }
  7460. sd->status.inventory[i].refine += up;
  7461. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7462. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7463. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  7464. clif_delitem(sd,i,1,3);
  7465. //Logs items, got from (N)PC scripts [Lupus]
  7466. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7467. clif_additem(sd,i,1,0);
  7468. pc_equipitem(sd,i,ep);
  7469. clif_misceffect(&sd->bl,3);
  7470. if (sd->status.inventory[i].refine == MAX_REFINE &&
  7471. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  7472. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3]))
  7473. { // Fame point system [DracoRPG]
  7474. switch (sd->inventory_data[i]->wlv){
  7475. case 1:
  7476. pc_addfame(sd, battle_config.fame_refine_lv1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  7477. break;
  7478. case 2:
  7479. pc_addfame(sd, battle_config.fame_refine_lv2); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  7480. break;
  7481. case 3:
  7482. pc_addfame(sd, battle_config.fame_refine_lv3); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  7483. break;
  7484. }
  7485. }
  7486. script_pushint(st, sd->status.inventory[i].refine);
  7487. return SCRIPT_CMD_SUCCESS;
  7488. }
  7489. ShowError("buildin_successrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7490. script_pushint(st, -1);
  7491. return SCRIPT_CMD_FAILURE;
  7492. }
  7493. /*==========================================
  7494. * Show a failed Refine +1 attempt
  7495. * failedrefitem <equipment slot>{,<char_id>}
  7496. *------------------------------------------*/
  7497. BUILDIN_FUNC(failedrefitem) {
  7498. short i = -1;
  7499. int pos;
  7500. TBL_PC *sd;
  7501. pos = script_getnum(st,2);
  7502. if (!script_charid2sd(3, sd)) {
  7503. script_pushint(st, 0);
  7504. return SCRIPT_CMD_FAILURE;
  7505. }
  7506. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7507. i = pc_checkequip(sd,equip[pos-1]);
  7508. if (i >= 0) {
  7509. sd->status.inventory[i].refine = 0;
  7510. pc_unequipitem(sd,i,3); //recalculate bonus
  7511. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine); //notify client of failure
  7512. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  7513. clif_misceffect(&sd->bl,2); // display failure effect
  7514. script_pushint(st, 1);
  7515. return SCRIPT_CMD_SUCCESS;
  7516. }
  7517. ShowError("buildin_failedrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7518. script_pushint(st, 0);
  7519. return SCRIPT_CMD_FAILURE;
  7520. }
  7521. /*==========================================
  7522. * Downgrades an Equipment Part by -1 . [Masao]
  7523. * downrefitem <equipment slot>{,<count>{,<char_id>}}
  7524. *------------------------------------------*/
  7525. BUILDIN_FUNC(downrefitem) {
  7526. short i = -1, down = 1;
  7527. int pos;
  7528. TBL_PC *sd;
  7529. if (!script_charid2sd(4, sd)) {
  7530. script_pushint(st, -1);
  7531. return SCRIPT_CMD_FAILURE;
  7532. }
  7533. pos = script_getnum(st,2);
  7534. if (script_hasdata(st, 3))
  7535. down = script_getnum(st, 3);
  7536. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7537. i = pc_checkequip(sd,equip[pos-1]);
  7538. if (i >= 0) {
  7539. unsigned int ep = sd->status.inventory[i].equip;
  7540. //Logs items, got from (N)PC scripts [Lupus]
  7541. log_pick_pc(sd, LOG_TYPE_SCRIPT, -1, &sd->status.inventory[i]);
  7542. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  7543. sd->status.inventory[i].refine -= down;
  7544. sd->status.inventory[i].refine = cap_value( sd->status.inventory[i].refine, 0, MAX_REFINE);
  7545. clif_refine(sd->fd,2,i,sd->status.inventory[i].refine);
  7546. clif_delitem(sd,i,1,3);
  7547. //Logs items, got from (N)PC scripts [Lupus]
  7548. log_pick_pc(sd, LOG_TYPE_SCRIPT, 1, &sd->status.inventory[i]);
  7549. clif_additem(sd,i,1,0);
  7550. pc_equipitem(sd,i,ep);
  7551. clif_misceffect(&sd->bl,2);
  7552. script_pushint(st, sd->status.inventory[i].refine);
  7553. return SCRIPT_CMD_SUCCESS;
  7554. }
  7555. ShowError("buildin_downrefitem: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7556. script_pushint(st, -1);
  7557. return SCRIPT_CMD_FAILURE;
  7558. }
  7559. /**
  7560. * Delete the item equipped at pos.
  7561. * delequip <equipment slot>{,<char_id>};
  7562. **/
  7563. BUILDIN_FUNC(delequip) {
  7564. short i = -1;
  7565. int pos;
  7566. int8 ret;
  7567. TBL_PC *sd;
  7568. pos = script_getnum(st,2);
  7569. if (!script_charid2sd(3,sd)) {
  7570. st->state = END;
  7571. return SCRIPT_CMD_FAILURE;
  7572. }
  7573. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7574. i = pc_checkequip(sd,equip[pos-1]);
  7575. if (i >= 0) {
  7576. pc_unequipitem(sd,i,3); //recalculate bonus
  7577. ret = !(pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT));
  7578. }
  7579. else {
  7580. ShowError("buildin_delequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7581. st->state = END;
  7582. return SCRIPT_CMD_FAILURE;
  7583. }
  7584. script_pushint(st,ret);
  7585. return SCRIPT_CMD_SUCCESS;
  7586. }
  7587. /**
  7588. * Break the item equipped at pos.
  7589. * breakequip <equipment slot>{,<char_id>};
  7590. **/
  7591. BUILDIN_FUNC(breakequip) {
  7592. short i = -1;
  7593. int pos;
  7594. TBL_PC *sd;
  7595. pos = script_getnum(st,2);
  7596. if (!script_charid2sd(3,sd))
  7597. return SCRIPT_CMD_FAILURE;
  7598. if (pos > 0 && pos <= ARRAYLENGTH(equip))
  7599. i = pc_checkequip(sd,equip[pos-1]);
  7600. if (i >= 0) {
  7601. sd->status.inventory[i].attribute = 1;
  7602. pc_unequipitem(sd,i,3);
  7603. clif_equiplist(sd);
  7604. script_pushint(st,1);
  7605. return SCRIPT_CMD_SUCCESS;
  7606. }
  7607. ShowError("buildin_breakequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  7608. script_pushint(st,0);
  7609. return SCRIPT_CMD_FAILURE;
  7610. }
  7611. /**
  7612. * statusup <stat>{,<char_id>};
  7613. **/
  7614. BUILDIN_FUNC(statusup)
  7615. {
  7616. int type;
  7617. TBL_PC *sd;
  7618. type = script_getnum(st,2);
  7619. if (!script_charid2sd(3, sd))
  7620. return SCRIPT_CMD_FAILURE;
  7621. pc_statusup(sd, type, 1);
  7622. return SCRIPT_CMD_SUCCESS;
  7623. }
  7624. /**
  7625. * statusup2 <stat>,<amount>{,<char_id>};
  7626. **/
  7627. BUILDIN_FUNC(statusup2)
  7628. {
  7629. int type,val;
  7630. TBL_PC *sd;
  7631. type=script_getnum(st,2);
  7632. val=script_getnum(st,3);
  7633. if (!script_charid2sd(4, sd))
  7634. return SCRIPT_CMD_FAILURE;
  7635. pc_statusup2(sd,type,val);
  7636. return SCRIPT_CMD_SUCCESS;
  7637. }
  7638. /// See 'doc/item_bonus.txt'
  7639. ///
  7640. /// bonus <bonus type>,<val1>;
  7641. /// bonus2 <bonus type>,<val1>,<val2>;
  7642. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  7643. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  7644. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  7645. BUILDIN_FUNC(bonus)
  7646. {
  7647. int type;
  7648. int val1;
  7649. int val2 = 0;
  7650. int val3 = 0;
  7651. int val4 = 0;
  7652. int val5 = 0;
  7653. TBL_PC* sd;
  7654. struct script_data *data;
  7655. sd = script_rid2sd(st);
  7656. if( sd == NULL )
  7657. return SCRIPT_CMD_SUCCESS; // no player attached
  7658. type = script_getnum(st,2);
  7659. switch( type ) {
  7660. case SP_AUTOSPELL:
  7661. case SP_AUTOSPELL_WHENHIT:
  7662. case SP_AUTOSPELL_ONSKILL:
  7663. case SP_SKILL_ATK:
  7664. case SP_SKILL_HEAL:
  7665. case SP_SKILL_HEAL2:
  7666. case SP_ADD_SKILL_BLOW:
  7667. case SP_CASTRATE:
  7668. case SP_ADDEFF_ONSKILL:
  7669. case SP_SKILL_USE_SP_RATE:
  7670. case SP_SKILL_COOLDOWN:
  7671. case SP_SKILL_FIXEDCAST:
  7672. case SP_SKILL_VARIABLECAST:
  7673. case SP_VARCASTRATE:
  7674. case SP_FIXCASTRATE:
  7675. case SP_SKILL_USE_SP:
  7676. case SP_SUB_SKILL:
  7677. // these bonuses support skill names
  7678. data = script_getdata(st, 3);
  7679. get_val(st, data); // Convert into value in case of a variable
  7680. val1 = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7681. break;
  7682. default:
  7683. val1 = script_getnum(st,3);
  7684. break;
  7685. }
  7686. switch( script_lastdata(st)-2 ) {
  7687. case 1:
  7688. pc_bonus(sd, type, val1);
  7689. break;
  7690. case 2:
  7691. val2 = script_getnum(st,4);
  7692. pc_bonus2(sd, type, val1, val2);
  7693. break;
  7694. case 3:
  7695. val2 = script_getnum(st,4);
  7696. val3 = script_getnum(st,5);
  7697. pc_bonus3(sd, type, val1, val2, val3);
  7698. break;
  7699. case 4:
  7700. data = script_getdata(st, 4);
  7701. get_val(st, data); // Convert into value in case of a variable
  7702. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7703. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7704. else
  7705. val2 = script_getnum(st,4);
  7706. val3 = script_getnum(st,5);
  7707. val4 = script_getnum(st,6);
  7708. pc_bonus4(sd, type, val1, val2, val3, val4);
  7709. break;
  7710. case 5:
  7711. data = script_getdata(st, 4);
  7712. get_val(st, data); // Convert into value in case of a variable
  7713. if( type == SP_AUTOSPELL_ONSKILL && data_isstring(data) )
  7714. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  7715. else
  7716. val2 = script_getnum(st,4);
  7717. val3 = script_getnum(st,5);
  7718. val4 = script_getnum(st,6);
  7719. val5 = script_getnum(st,7);
  7720. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  7721. break;
  7722. default:
  7723. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  7724. break;
  7725. }
  7726. return SCRIPT_CMD_SUCCESS;
  7727. }
  7728. BUILDIN_FUNC(autobonus)
  7729. {
  7730. unsigned int dur, pos;
  7731. short rate;
  7732. short atk_type = 0;
  7733. TBL_PC* sd;
  7734. const char *bonus_script, *other_script = NULL;
  7735. sd = script_rid2sd(st);
  7736. if( sd == NULL )
  7737. return SCRIPT_CMD_SUCCESS; // no player attached
  7738. if (current_equip_combo_pos)
  7739. pos = current_equip_combo_pos;
  7740. else
  7741. pos = sd->status.inventory[current_equip_item_index].equip;
  7742. if((sd->state.autobonus&pos) == pos)
  7743. return SCRIPT_CMD_SUCCESS;
  7744. rate = script_getnum(st,3);
  7745. dur = script_getnum(st,4);
  7746. bonus_script = script_getstr(st,2);
  7747. if( !rate || !dur || !pos || !bonus_script )
  7748. return SCRIPT_CMD_SUCCESS;
  7749. if( script_hasdata(st,5) )
  7750. atk_type = script_getnum(st,5);
  7751. if( script_hasdata(st,6) )
  7752. other_script = script_getstr(st,6);
  7753. if( pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),
  7754. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7755. {
  7756. script_add_autobonus(bonus_script);
  7757. if( other_script )
  7758. script_add_autobonus(other_script);
  7759. }
  7760. return SCRIPT_CMD_SUCCESS;
  7761. }
  7762. BUILDIN_FUNC(autobonus2)
  7763. {
  7764. unsigned int dur, pos;
  7765. short rate;
  7766. short atk_type = 0;
  7767. TBL_PC* sd;
  7768. const char *bonus_script, *other_script = NULL;
  7769. sd = script_rid2sd(st);
  7770. if( sd == NULL )
  7771. return SCRIPT_CMD_SUCCESS; // no player attached
  7772. if (current_equip_combo_pos)
  7773. pos = current_equip_combo_pos;
  7774. else
  7775. pos = sd->status.inventory[current_equip_item_index].equip;
  7776. if((sd->state.autobonus&pos) == pos)
  7777. return SCRIPT_CMD_SUCCESS;
  7778. rate = script_getnum(st,3);
  7779. dur = script_getnum(st,4);
  7780. bonus_script = script_getstr(st,2);
  7781. if( !rate || !dur || !pos || !bonus_script )
  7782. return SCRIPT_CMD_SUCCESS;
  7783. if( script_hasdata(st,5) )
  7784. atk_type = script_getnum(st,5);
  7785. if( script_hasdata(st,6) )
  7786. other_script = script_getstr(st,6);
  7787. if( pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),
  7788. bonus_script,rate,dur,atk_type,other_script,pos,false) )
  7789. {
  7790. script_add_autobonus(bonus_script);
  7791. if( other_script )
  7792. script_add_autobonus(other_script);
  7793. }
  7794. return SCRIPT_CMD_SUCCESS;
  7795. }
  7796. BUILDIN_FUNC(autobonus3)
  7797. {
  7798. unsigned int dur, pos;
  7799. short rate,atk_type;
  7800. TBL_PC* sd;
  7801. const char *bonus_script, *other_script = NULL;
  7802. struct script_data *data;
  7803. sd = script_rid2sd(st);
  7804. if( sd == NULL )
  7805. return SCRIPT_CMD_SUCCESS; // no player attached
  7806. if (current_equip_combo_pos)
  7807. pos = current_equip_combo_pos;
  7808. else
  7809. pos = sd->status.inventory[current_equip_item_index].equip;
  7810. if((sd->state.autobonus&pos) == pos)
  7811. return SCRIPT_CMD_SUCCESS;
  7812. rate = script_getnum(st,3);
  7813. dur = script_getnum(st,4);
  7814. data = script_getdata(st, 5);
  7815. get_val(st, data); // Convert into value in case of a variable
  7816. atk_type = ( data_isstring(data) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  7817. bonus_script = script_getstr(st,2);
  7818. if( !rate || !dur || !pos || !atk_type || !bonus_script )
  7819. return SCRIPT_CMD_SUCCESS;
  7820. if( script_hasdata(st,6) )
  7821. other_script = script_getstr(st,6);
  7822. if( pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),
  7823. bonus_script,rate,dur,atk_type,other_script,pos,true) )
  7824. {
  7825. script_add_autobonus(bonus_script);
  7826. if( other_script )
  7827. script_add_autobonus(other_script);
  7828. }
  7829. return SCRIPT_CMD_SUCCESS;
  7830. }
  7831. /// Changes the level of a player skill.
  7832. /// <flag> defaults to 1
  7833. /// <flag>=0 : set the level of the skill
  7834. /// <flag>=1 : set the temporary level of the skill
  7835. /// <flag>=2 : add to the level of the skill
  7836. ///
  7837. /// skill <skill id>,<level>,<flag>
  7838. /// skill <skill id>,<level>
  7839. /// skill "<skill name>",<level>,<flag>
  7840. /// skill "<skill name>",<level>
  7841. BUILDIN_FUNC(skill)
  7842. {
  7843. int id;
  7844. int level;
  7845. int flag = ADDSKILL_TEMP;
  7846. TBL_PC* sd;
  7847. struct script_data *data;
  7848. const char* command = script_getfuncname(st);
  7849. sd = script_rid2sd(st);
  7850. if( sd == NULL )
  7851. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7852. if (strcmpi(command, "addtoskill") == 0)
  7853. flag = ADDSKILL_TEMP_ADDLEVEL;
  7854. data = script_getdata(st, 2);
  7855. get_val(st, data); // Convert into value in case of a variable
  7856. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7857. level = script_getnum(st,3);
  7858. if( script_hasdata(st,4) )
  7859. flag = script_getnum(st,4);
  7860. pc_skill(sd, id, level, (enum e_addskill_type)flag);
  7861. return SCRIPT_CMD_SUCCESS;
  7862. }
  7863. /// Increases the level of a guild skill.
  7864. ///
  7865. /// guildskill <skill id>,<amount>;
  7866. /// guildskill "<skill name>",<amount>;
  7867. BUILDIN_FUNC(guildskill)
  7868. {
  7869. int id;
  7870. int level;
  7871. TBL_PC* sd;
  7872. int i;
  7873. struct script_data *data;
  7874. sd = script_rid2sd(st);
  7875. if( sd == NULL )
  7876. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7877. data = script_getdata(st, 2);
  7878. get_val(st, data); // Convert into value in case of a variable
  7879. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7880. level = script_getnum(st,3);
  7881. for( i=0; i < level; i++ )
  7882. guild_skillup(sd, id);
  7883. return SCRIPT_CMD_SUCCESS;
  7884. }
  7885. /// Returns the level of the player skill.
  7886. ///
  7887. /// getskilllv(<skill id>) -> <level>
  7888. /// getskilllv("<skill name>") -> <level>
  7889. BUILDIN_FUNC(getskilllv)
  7890. {
  7891. int id;
  7892. TBL_PC* sd;
  7893. struct script_data *data;
  7894. sd = script_rid2sd(st);
  7895. if( sd == NULL )
  7896. return SCRIPT_CMD_SUCCESS;// no player attached, report source
  7897. data = script_getdata(st, 2);
  7898. get_val(st, data); // Convert into value in case of a variable
  7899. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  7900. script_pushint(st, pc_checkskill(sd,id));
  7901. return SCRIPT_CMD_SUCCESS;
  7902. }
  7903. /// Returns the level of the guild skill.
  7904. ///
  7905. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  7906. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  7907. BUILDIN_FUNC(getgdskilllv)
  7908. {
  7909. int guild_id;
  7910. uint16 skill_id;
  7911. struct guild* g;
  7912. struct script_data *data;
  7913. guild_id = script_getnum(st,2);
  7914. data = script_getdata(st, 3);
  7915. get_val(st, data); // Convert into value in case of a variable
  7916. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  7917. g = guild_search(guild_id);
  7918. if( g == NULL )
  7919. script_pushint(st, -1);
  7920. else
  7921. script_pushint(st, guild_checkskill(g,skill_id));
  7922. return SCRIPT_CMD_SUCCESS;
  7923. }
  7924. /// Returns the 'basic_skill_check' setting.
  7925. /// This config determines if the server checks the skill level of NV_BASIC
  7926. /// before allowing the basic actions.
  7927. ///
  7928. /// basicskillcheck() -> <bool>
  7929. BUILDIN_FUNC(basicskillcheck)
  7930. {
  7931. script_pushint(st, battle_config.basic_skill_check);
  7932. return SCRIPT_CMD_SUCCESS;
  7933. }
  7934. /// Returns the GM level of the player.
  7935. ///
  7936. /// getgmlevel({<char_id>}) -> <level>
  7937. BUILDIN_FUNC(getgmlevel)
  7938. {
  7939. TBL_PC* sd;
  7940. if (!script_charid2sd(2,sd))
  7941. return SCRIPT_CMD_FAILURE;
  7942. script_pushint(st, pc_get_group_level(sd));
  7943. return SCRIPT_CMD_SUCCESS;
  7944. }
  7945. /// Returns the group ID of the player.
  7946. ///
  7947. /// getgroupid({<char_id>}) -> <int>
  7948. BUILDIN_FUNC(getgroupid)
  7949. {
  7950. TBL_PC* sd;
  7951. if (!script_charid2sd(2,sd))
  7952. return SCRIPT_CMD_FAILURE;
  7953. script_pushint(st, pc_get_group_id(sd));
  7954. return SCRIPT_CMD_SUCCESS;
  7955. }
  7956. /// Terminates the execution of this script instance.
  7957. ///
  7958. /// end
  7959. BUILDIN_FUNC(end)
  7960. {
  7961. TBL_PC* sd;
  7962. sd = map_id2sd(st->rid);
  7963. st->state = END;
  7964. if (st->stack->defsp >= 1 && st->stack->stack_data[st->stack->defsp-1].type == C_RETINFO) {
  7965. int i;
  7966. for(i = 0; i < st->stack->sp; i++) {
  7967. if (st->stack->stack_data[i].type == C_RETINFO) { // Grab the first, aka the original
  7968. struct script_retinfo *ri = st->stack->stack_data[i].u.ri;
  7969. st->script = ri->script;
  7970. break;
  7971. }
  7972. }
  7973. }
  7974. if( st->mes_active )
  7975. st->mes_active = 0;
  7976. if (sd){
  7977. if (sd->state.callshop == 0)
  7978. clif_scriptclose(sd, st->oid); // If a menu/select/prompt is active, close it.
  7979. else
  7980. sd->state.callshop = 0;
  7981. }
  7982. return SCRIPT_CMD_SUCCESS;
  7983. }
  7984. /// Checks if the player has that effect state (option).
  7985. ///
  7986. /// checkoption(<option>{,<char_id>}) -> <bool>
  7987. BUILDIN_FUNC(checkoption)
  7988. {
  7989. int option;
  7990. TBL_PC* sd;
  7991. if (!script_charid2sd(3,sd))
  7992. return SCRIPT_CMD_FAILURE;
  7993. option = script_getnum(st,2);
  7994. if( sd->sc.option&option )
  7995. script_pushint(st, 1);
  7996. else
  7997. script_pushint(st, 0);
  7998. return SCRIPT_CMD_SUCCESS;
  7999. }
  8000. /// Checks if the player is in that body state (opt1).
  8001. ///
  8002. /// checkoption1(<opt1>{,<char_id>}) -> <bool>
  8003. BUILDIN_FUNC(checkoption1)
  8004. {
  8005. int opt1;
  8006. TBL_PC* sd;
  8007. if (!script_charid2sd(3,sd))
  8008. return SCRIPT_CMD_FAILURE;
  8009. opt1 = script_getnum(st,2);
  8010. if( sd->sc.opt1 == opt1 )
  8011. script_pushint(st, 1);
  8012. else
  8013. script_pushint(st, 0);
  8014. return SCRIPT_CMD_SUCCESS;
  8015. }
  8016. /// Checks if the player has that health state (opt2).
  8017. ///
  8018. /// checkoption2(<opt2>{,<char_id>}) -> <bool>
  8019. BUILDIN_FUNC(checkoption2)
  8020. {
  8021. int opt2;
  8022. TBL_PC* sd;
  8023. if (!script_charid2sd(3,sd))
  8024. return SCRIPT_CMD_FAILURE;
  8025. opt2 = script_getnum(st,2);
  8026. if( sd->sc.opt2&opt2 )
  8027. script_pushint(st, 1);
  8028. else
  8029. script_pushint(st, 0);
  8030. return SCRIPT_CMD_SUCCESS;
  8031. }
  8032. /// Changes the effect state (option) of the player.
  8033. /// <flag> defaults to 1
  8034. /// <flag>=0 : removes the option
  8035. /// <flag>=other : adds the option
  8036. ///
  8037. /// setoption <option>{,<flag>{,<char_id>}};
  8038. BUILDIN_FUNC(setoption)
  8039. {
  8040. int option;
  8041. int flag = 1;
  8042. TBL_PC* sd;
  8043. if (!script_charid2sd(4,sd))
  8044. return SCRIPT_CMD_FAILURE;
  8045. option = script_getnum(st,2);
  8046. if( script_hasdata(st,3) )
  8047. flag = script_getnum(st,3);
  8048. else if( !option ){// Request to remove everything.
  8049. flag = 0;
  8050. option = OPTION_FALCON|OPTION_RIDING;
  8051. #ifndef NEW_CARTS
  8052. option |= OPTION_CART;
  8053. #endif
  8054. }
  8055. if( flag ){// Add option
  8056. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  8057. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  8058. pc_setoption(sd, sd->sc.option|option);
  8059. } else// Remove option
  8060. pc_setoption(sd, sd->sc.option&~option);
  8061. return SCRIPT_CMD_SUCCESS;
  8062. }
  8063. /// Returns if the player has a cart.
  8064. ///
  8065. /// checkcart({char_id}) -> <bool>
  8066. ///
  8067. /// @author Valaris
  8068. BUILDIN_FUNC(checkcart)
  8069. {
  8070. TBL_PC* sd;
  8071. if (!script_charid2sd(2,sd))
  8072. return SCRIPT_CMD_FAILURE;
  8073. if( pc_iscarton(sd) )
  8074. script_pushint(st, 1);
  8075. else
  8076. script_pushint(st, 0);
  8077. return SCRIPT_CMD_SUCCESS;
  8078. }
  8079. /// Sets the cart of the player.
  8080. /// <type> defaults to 1
  8081. /// <type>=0 : removes the cart
  8082. /// <type>=1 : Normal cart
  8083. /// <type>=2 : Wooden cart
  8084. /// <type>=3 : Covered cart with flowers and ferns
  8085. /// <type>=4 : Wooden cart with a Panda doll on the back
  8086. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  8087. ///
  8088. /// setcart {<type>{,<char_id>}};
  8089. BUILDIN_FUNC(setcart)
  8090. {
  8091. int type = 1;
  8092. TBL_PC* sd;
  8093. if (!script_charid2sd(3,sd))
  8094. return SCRIPT_CMD_FAILURE;
  8095. if( script_hasdata(st,2) )
  8096. type = script_getnum(st,2);
  8097. pc_setcart(sd, type);
  8098. return SCRIPT_CMD_SUCCESS;
  8099. }
  8100. /// Returns if the player has a falcon.
  8101. ///
  8102. /// checkfalcon({char_id}) -> <bool>
  8103. ///
  8104. /// @author Valaris
  8105. BUILDIN_FUNC(checkfalcon)
  8106. {
  8107. TBL_PC* sd;
  8108. if (!script_charid2sd(2,sd))
  8109. return SCRIPT_CMD_FAILURE;
  8110. if( pc_isfalcon(sd) )
  8111. script_pushint(st, 1);
  8112. else
  8113. script_pushint(st, 0);
  8114. return SCRIPT_CMD_SUCCESS;
  8115. }
  8116. /// Sets if the player has a falcon or not.
  8117. /// <flag> defaults to 1
  8118. ///
  8119. /// setfalcon {<flag>{,<char_id>}};
  8120. BUILDIN_FUNC(setfalcon)
  8121. {
  8122. int flag = 1;
  8123. TBL_PC* sd;
  8124. if (!script_charid2sd(3,sd))
  8125. return SCRIPT_CMD_FAILURE;
  8126. if( script_hasdata(st,2) )
  8127. flag = script_getnum(st,2);
  8128. pc_setfalcon(sd, flag);
  8129. return SCRIPT_CMD_SUCCESS;
  8130. }
  8131. /// Returns if the player is riding.
  8132. ///
  8133. /// checkriding({char_id}) -> <bool>
  8134. ///
  8135. /// @author Valaris
  8136. BUILDIN_FUNC(checkriding)
  8137. {
  8138. TBL_PC* sd;
  8139. if (!script_charid2sd(2,sd))
  8140. return SCRIPT_CMD_FAILURE;
  8141. if( pc_isriding(sd) )
  8142. script_pushint(st, 1);
  8143. else
  8144. script_pushint(st, 0);
  8145. return SCRIPT_CMD_SUCCESS;
  8146. }
  8147. /// Sets if the player is riding.
  8148. /// <flag> defaults to 1
  8149. ///
  8150. /// setriding {<flag>{,<char_id>}};
  8151. BUILDIN_FUNC(setriding)
  8152. {
  8153. int flag = 1;
  8154. TBL_PC* sd;
  8155. if (!script_charid2sd(3,sd))
  8156. return SCRIPT_CMD_FAILURE;
  8157. if( script_hasdata(st,2) )
  8158. flag = script_getnum(st,2);
  8159. pc_setriding(sd, flag);
  8160. return SCRIPT_CMD_SUCCESS;
  8161. }
  8162. /// Returns if the player has a warg.
  8163. ///
  8164. /// checkwug({char_id}) -> <bool>
  8165. ///
  8166. BUILDIN_FUNC(checkwug)
  8167. {
  8168. TBL_PC* sd;
  8169. if (!script_charid2sd(2,sd))
  8170. return SCRIPT_CMD_FAILURE;
  8171. if( pc_iswug(sd) || pc_isridingwug(sd) )
  8172. script_pushint(st, 1);
  8173. else
  8174. script_pushint(st, 0);
  8175. return SCRIPT_CMD_SUCCESS;
  8176. }
  8177. /// Returns if the player is wearing MADO Gear.
  8178. ///
  8179. /// checkmadogear({<char_id>}) -> <bool>
  8180. ///
  8181. BUILDIN_FUNC(checkmadogear)
  8182. {
  8183. TBL_PC* sd;
  8184. if (!script_charid2sd(2,sd))
  8185. return SCRIPT_CMD_FAILURE;
  8186. if( pc_ismadogear(sd) )
  8187. script_pushint(st, 1);
  8188. else
  8189. script_pushint(st, 0);
  8190. return SCRIPT_CMD_SUCCESS;
  8191. }
  8192. /// Sets if the player is riding MADO Gear.
  8193. /// <flag> defaults to 1
  8194. ///
  8195. /// setmadogear {<flag>{,<char_id>}};
  8196. BUILDIN_FUNC(setmadogear)
  8197. {
  8198. int flag = 1;
  8199. TBL_PC* sd;
  8200. if (!script_charid2sd(3,sd))
  8201. return SCRIPT_CMD_FAILURE;
  8202. if( script_hasdata(st,2) )
  8203. flag = script_getnum(st,2);
  8204. pc_setmadogear(sd, flag);
  8205. return SCRIPT_CMD_SUCCESS;
  8206. }
  8207. /// Sets the save point of the player.
  8208. ///
  8209. /// save "<map name>",<x>,<y>{,<char_id>}
  8210. /// savepoint "<map name>",<x>,<y>{,<char_id>}
  8211. BUILDIN_FUNC(savepoint)
  8212. {
  8213. int x;
  8214. int y;
  8215. short map_idx;
  8216. const char* str;
  8217. TBL_PC* sd;
  8218. if (!script_charid2sd(5,sd))
  8219. return SCRIPT_CMD_FAILURE;// no player attached, report source
  8220. str = script_getstr(st, 2);
  8221. x = script_getnum(st,3);
  8222. y = script_getnum(st,4);
  8223. map_idx = mapindex_name2id(str);
  8224. if( map_idx )
  8225. pc_setsavepoint(sd, map_idx, x, y);
  8226. return SCRIPT_CMD_SUCCESS;
  8227. }
  8228. /*==========================================
  8229. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  8230. *------------------------------------------*/
  8231. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  8232. {
  8233. int type;
  8234. time_t timer;
  8235. struct tm *t;
  8236. type=script_getnum(st,2);
  8237. switch(type){
  8238. case 2:
  8239. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  8240. // from the system clock.)
  8241. script_pushint(st,(int)time(NULL));
  8242. break;
  8243. case 1:
  8244. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  8245. time(&timer);
  8246. t=localtime(&timer);
  8247. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  8248. break;
  8249. case 0:
  8250. default:
  8251. //type 0:(System Ticks)
  8252. script_pushint(st,gettick());
  8253. break;
  8254. }
  8255. return SCRIPT_CMD_SUCCESS;
  8256. }
  8257. /*==========================================
  8258. * GetTime(Type);
  8259. * 1: Sec 2: Min 3: Hour
  8260. * 4: WeekDay 5: MonthDay 6: Month
  8261. * 7: Year
  8262. *------------------------------------------*/
  8263. BUILDIN_FUNC(gettime) /* Asgard Version */
  8264. {
  8265. int type;
  8266. time_t timer;
  8267. struct tm *t;
  8268. type=script_getnum(st,2);
  8269. time(&timer);
  8270. t=localtime(&timer);
  8271. switch(type){
  8272. case 1://Sec(0~59)
  8273. script_pushint(st,t->tm_sec);
  8274. break;
  8275. case 2://Min(0~59)
  8276. script_pushint(st,t->tm_min);
  8277. break;
  8278. case 3://Hour(0~23)
  8279. script_pushint(st,t->tm_hour);
  8280. break;
  8281. case 4://WeekDay(0~6)
  8282. script_pushint(st,t->tm_wday);
  8283. break;
  8284. case 5://MonthDay(01~31)
  8285. script_pushint(st,t->tm_mday);
  8286. break;
  8287. case 6://Month(01~12)
  8288. script_pushint(st,t->tm_mon+1);
  8289. break;
  8290. case 7://Year(20xx)
  8291. script_pushint(st,t->tm_year+1900);
  8292. break;
  8293. case 8://Year Day(01~366)
  8294. script_pushint(st,t->tm_yday+1);
  8295. break;
  8296. default://(format error)
  8297. script_pushint(st,-1);
  8298. break;
  8299. }
  8300. return SCRIPT_CMD_SUCCESS;
  8301. }
  8302. /*==========================================
  8303. * GetTimeStr("TimeFMT", Length);
  8304. *------------------------------------------*/
  8305. BUILDIN_FUNC(gettimestr)
  8306. {
  8307. char *tmpstr;
  8308. const char *fmtstr;
  8309. int maxlen;
  8310. time_t now = time(NULL);
  8311. fmtstr=script_getstr(st,2);
  8312. maxlen=script_getnum(st,3);
  8313. tmpstr=(char *)aMalloc((maxlen+1)*sizeof(char));
  8314. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  8315. tmpstr[maxlen]='\0';
  8316. script_pushstr(st,tmpstr);
  8317. return SCRIPT_CMD_SUCCESS;
  8318. }
  8319. /*==========================================
  8320. * Open player storage
  8321. *------------------------------------------*/
  8322. BUILDIN_FUNC(openstorage)
  8323. {
  8324. TBL_PC* sd;
  8325. sd = script_rid2sd(st);
  8326. if( sd == NULL )
  8327. return SCRIPT_CMD_SUCCESS;
  8328. storage_storageopen(sd);
  8329. return SCRIPT_CMD_SUCCESS;
  8330. }
  8331. BUILDIN_FUNC(guildopenstorage)
  8332. {
  8333. TBL_PC* sd;
  8334. int ret;
  8335. sd = script_rid2sd(st);
  8336. if( sd == NULL )
  8337. return SCRIPT_CMD_SUCCESS;
  8338. ret = gstorage_storageopen(sd);
  8339. script_pushint(st,ret);
  8340. return SCRIPT_CMD_SUCCESS;
  8341. }
  8342. /*==========================================
  8343. * Make player use a skill trought item usage
  8344. *------------------------------------------*/
  8345. /// itemskill <skill id>,<level>
  8346. /// itemskill "<skill name>",<level>
  8347. BUILDIN_FUNC(itemskill)
  8348. {
  8349. int id;
  8350. int lv;
  8351. TBL_PC* sd;
  8352. struct script_data *data;
  8353. sd = script_rid2sd(st);
  8354. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  8355. return SCRIPT_CMD_SUCCESS;
  8356. data = script_getdata(st, 2);
  8357. get_val(st, data); // Convert into value in case of a variable
  8358. id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  8359. lv = script_getnum(st,3);
  8360. sd->skillitem=id;
  8361. sd->skillitemlv=lv;
  8362. clif_item_skill(sd,id,lv);
  8363. return SCRIPT_CMD_SUCCESS;
  8364. }
  8365. /*==========================================
  8366. * Attempt to create an item
  8367. *------------------------------------------*/
  8368. BUILDIN_FUNC(produce)
  8369. {
  8370. int trigger;
  8371. TBL_PC* sd;
  8372. sd = script_rid2sd(st);
  8373. if( sd == NULL )
  8374. return SCRIPT_CMD_SUCCESS;
  8375. trigger=script_getnum(st,2);
  8376. clif_skill_produce_mix_list(sd, -1, trigger);
  8377. return SCRIPT_CMD_SUCCESS;
  8378. }
  8379. /*==========================================
  8380. *
  8381. *------------------------------------------*/
  8382. BUILDIN_FUNC(cooking)
  8383. {
  8384. int trigger;
  8385. TBL_PC* sd;
  8386. sd = script_rid2sd(st);
  8387. if( sd == NULL )
  8388. return SCRIPT_CMD_SUCCESS;
  8389. trigger=script_getnum(st,2);
  8390. clif_cooking_list(sd, trigger, AM_PHARMACY, 1, 1);
  8391. return SCRIPT_CMD_SUCCESS;
  8392. }
  8393. /*==========================================
  8394. * Create a pet
  8395. *------------------------------------------*/
  8396. BUILDIN_FUNC(makepet)
  8397. {
  8398. TBL_PC* sd;
  8399. int id,pet_id;
  8400. id=script_getnum(st,2);
  8401. sd = script_rid2sd(st);
  8402. if( sd == NULL )
  8403. return SCRIPT_CMD_SUCCESS;
  8404. pet_id = search_petDB_index(id, PET_CLASS);
  8405. if (pet_id < 0)
  8406. pet_id = search_petDB_index(id, PET_EGG);
  8407. if (pet_id >= 0 && sd) {
  8408. sd->catch_target_class = pet_db[pet_id].class_;
  8409. intif_create_pet(
  8410. sd->status.account_id, sd->status.char_id,
  8411. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  8412. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  8413. 100, 0, 1, pet_db[pet_id].jname);
  8414. }
  8415. return SCRIPT_CMD_SUCCESS;
  8416. }
  8417. /**
  8418. * Give player exp base,job * quest_exp_rate/100
  8419. * getexp <base xp>,<job xp>{,<char_id>};
  8420. **/
  8421. BUILDIN_FUNC(getexp)
  8422. {
  8423. TBL_PC* sd;
  8424. int base=0,job=0;
  8425. double bonus;
  8426. if (!script_charid2sd(4,sd))
  8427. return SCRIPT_CMD_FAILURE;
  8428. base=script_getnum(st,2);
  8429. job =script_getnum(st,3);
  8430. if(base<0 || job<0)
  8431. return SCRIPT_CMD_SUCCESS;
  8432. // bonus for npc-given exp
  8433. bonus = battle_config.quest_exp_rate / 100.;
  8434. if (base)
  8435. base = (int) cap_value(base * bonus, 0, INT_MAX);
  8436. if (job)
  8437. job = (int) cap_value(job * bonus, 0, INT_MAX);
  8438. pc_gainexp(sd, NULL, base, job, true);
  8439. return SCRIPT_CMD_SUCCESS;
  8440. }
  8441. /*==========================================
  8442. * Gain guild exp [Celest]
  8443. *------------------------------------------*/
  8444. BUILDIN_FUNC(guildgetexp)
  8445. {
  8446. TBL_PC* sd;
  8447. int exp;
  8448. sd = script_rid2sd(st);
  8449. if( sd == NULL )
  8450. return SCRIPT_CMD_SUCCESS;
  8451. exp = script_getnum(st,2);
  8452. if(exp < 0)
  8453. return SCRIPT_CMD_SUCCESS;
  8454. if(sd && sd->status.guild_id > 0)
  8455. guild_getexp (sd, exp);
  8456. return SCRIPT_CMD_SUCCESS;
  8457. }
  8458. /*==========================================
  8459. * Changes the guild master of a guild [Skotlex]
  8460. *------------------------------------------*/
  8461. BUILDIN_FUNC(guildchangegm)
  8462. {
  8463. TBL_PC *sd;
  8464. int guild_id;
  8465. const char *name;
  8466. guild_id = script_getnum(st,2);
  8467. name = script_getstr(st,3);
  8468. sd=map_nick2sd(name);
  8469. if (!sd)
  8470. script_pushint(st,0);
  8471. else
  8472. script_pushint(st,guild_gm_change(guild_id, sd));
  8473. return SCRIPT_CMD_SUCCESS;
  8474. }
  8475. /*==========================================
  8476. * Spawn a monster:
  8477. * *monster "<map name>",<x>,<y>,"<name to show>",<mob id>,<amount>{,"<event label>",<size>,<ai>};
  8478. *------------------------------------------*/
  8479. BUILDIN_FUNC(monster)
  8480. {
  8481. const char* mapn = script_getstr(st,2);
  8482. int x = script_getnum(st,3);
  8483. int y = script_getnum(st,4);
  8484. const char* str = script_getstr(st,5);
  8485. int class_ = script_getnum(st,6);
  8486. int amount = script_getnum(st,7);
  8487. const char* event = "";
  8488. unsigned int size = SZ_SMALL;
  8489. unsigned int ai = AI_NONE;
  8490. struct map_session_data* sd;
  8491. int16 m;
  8492. int i;
  8493. if (script_hasdata(st, 8)) {
  8494. event = script_getstr(st, 8);
  8495. check_event(st, event);
  8496. }
  8497. if (script_hasdata(st, 9)) {
  8498. size = script_getnum(st, 9);
  8499. if (size > 3) {
  8500. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8501. return SCRIPT_CMD_FAILURE;
  8502. }
  8503. }
  8504. if (script_hasdata(st, 10)) {
  8505. ai = script_getnum(st, 10);
  8506. if (ai >= AI_MAX) {
  8507. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8508. return SCRIPT_CMD_FAILURE;
  8509. }
  8510. }
  8511. if (class_ >= 0 && !mobdb_checkid(class_)) {
  8512. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  8513. return SCRIPT_CMD_FAILURE;
  8514. }
  8515. sd = map_id2sd(st->rid);
  8516. if (sd && strcmp(mapn, "this") == 0)
  8517. m = sd->bl.m;
  8518. else
  8519. m = map_mapname2mapid(mapn);
  8520. for(i = 0; i < amount; i++) { //not optimised
  8521. int mobid = mob_once_spawn(sd, m, x, y, str, class_, 1, event, size, ai);
  8522. if (mobid)
  8523. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8524. }
  8525. return SCRIPT_CMD_SUCCESS;
  8526. }
  8527. /*==========================================
  8528. * Request List of Monster Drops
  8529. *------------------------------------------*/
  8530. BUILDIN_FUNC(getmobdrops)
  8531. {
  8532. int class_ = script_getnum(st,2);
  8533. int i, j = 0;
  8534. struct mob_db *mob;
  8535. if( !mobdb_checkid(class_) )
  8536. {
  8537. script_pushint(st, 0);
  8538. return SCRIPT_CMD_SUCCESS;
  8539. }
  8540. mob = mob_db(class_);
  8541. for( i = 0; i < MAX_MOB_DROP; i++ )
  8542. {
  8543. if( mob->dropitem[i].nameid < 1 )
  8544. continue;
  8545. if( itemdb_exists(mob->dropitem[i].nameid) == NULL )
  8546. continue;
  8547. mapreg_setreg(reference_uid(add_str("$@MobDrop_item"), j), mob->dropitem[i].nameid);
  8548. mapreg_setreg(reference_uid(add_str("$@MobDrop_rate"), j), mob->dropitem[i].p);
  8549. j++;
  8550. }
  8551. mapreg_setreg(add_str("$@MobDrop_count"), j);
  8552. script_pushint(st, 1);
  8553. return SCRIPT_CMD_SUCCESS;
  8554. }
  8555. /*==========================================
  8556. * Spawn a monster in a random location
  8557. * in x0,x1,y0,y1 area.
  8558. *------------------------------------------*/
  8559. BUILDIN_FUNC(areamonster)
  8560. {
  8561. const char* mapn = script_getstr(st,2);
  8562. int x0 = script_getnum(st,3);
  8563. int y0 = script_getnum(st,4);
  8564. int x1 = script_getnum(st,5);
  8565. int y1 = script_getnum(st,6);
  8566. const char* str = script_getstr(st,7);
  8567. int class_ = script_getnum(st,8);
  8568. int amount = script_getnum(st,9);
  8569. const char* event = "";
  8570. unsigned int size = SZ_SMALL;
  8571. unsigned int ai = AI_NONE;
  8572. struct map_session_data* sd;
  8573. int16 m;
  8574. int i;
  8575. if (script_hasdata(st,10)) {
  8576. event = script_getstr(st, 10);
  8577. check_event(st, event);
  8578. }
  8579. if (script_hasdata(st, 11)) {
  8580. size = script_getnum(st, 11);
  8581. if (size > 3) {
  8582. ShowWarning("buildin_monster: Attempted to spawn non-existing size %d for monster class %d\n", size, class_);
  8583. return SCRIPT_CMD_FAILURE;
  8584. }
  8585. }
  8586. if (script_hasdata(st, 12)) {
  8587. ai = script_getnum(st, 12);
  8588. if (ai >= AI_MAX) {
  8589. ShowWarning("buildin_monster: Attempted to spawn non-existing ai %d for monster class %d\n", ai, class_);
  8590. return SCRIPT_CMD_FAILURE;
  8591. }
  8592. }
  8593. sd = map_id2sd(st->rid);
  8594. if (sd && strcmp(mapn, "this") == 0)
  8595. m = sd->bl.m;
  8596. else
  8597. m = map_mapname2mapid(mapn);
  8598. for(i = 0; i < amount; i++) { //not optimised
  8599. int mobid = mob_once_spawn_area(sd, m, x0, y0, x1, y1, str, class_, 1, event, size, ai);
  8600. if (mobid)
  8601. mapreg_setreg(reference_uid(add_str("$@mobid"), i), mobid);
  8602. }
  8603. return SCRIPT_CMD_SUCCESS;
  8604. }
  8605. /*==========================================
  8606. * KillMonster subcheck, verify if mob to kill ain't got an even to handle, could be force kill by allflag
  8607. *------------------------------------------*/
  8608. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  8609. { //same fix but with killmonster instead - stripping events from mobs.
  8610. TBL_MOB* md = (TBL_MOB*)bl;
  8611. char *event=va_arg(ap,char *);
  8612. int allflag=va_arg(ap,int);
  8613. md->state.npc_killmonster = 1;
  8614. if(!allflag){
  8615. if(strcmp(event,md->npc_event)==0)
  8616. status_kill(bl);
  8617. }else{
  8618. if(!md->spawn)
  8619. status_kill(bl);
  8620. }
  8621. md->state.npc_killmonster = 0;
  8622. return SCRIPT_CMD_SUCCESS;
  8623. }
  8624. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  8625. {
  8626. TBL_MOB* md = (TBL_MOB*)bl;
  8627. char *event=va_arg(ap,char *);
  8628. int allflag=va_arg(ap,int);
  8629. if(!allflag){
  8630. if(strcmp(event,md->npc_event)==0)
  8631. status_kill(bl);
  8632. }else{
  8633. if(!md->spawn)
  8634. status_kill(bl);
  8635. }
  8636. return SCRIPT_CMD_SUCCESS;
  8637. }
  8638. BUILDIN_FUNC(killmonster)
  8639. {
  8640. const char *mapname,*event;
  8641. int16 m,allflag=0;
  8642. mapname=script_getstr(st,2);
  8643. event=script_getstr(st,3);
  8644. if(strcmp(event,"All")==0)
  8645. allflag = 1;
  8646. else
  8647. check_event(st, event);
  8648. if( (m=map_mapname2mapid(mapname))<0 )
  8649. return SCRIPT_CMD_SUCCESS;
  8650. if( script_hasdata(st,4) ) {
  8651. if ( script_getnum(st,4) == 1 ) {
  8652. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  8653. return SCRIPT_CMD_SUCCESS;
  8654. }
  8655. }
  8656. map_freeblock_lock();
  8657. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  8658. map_freeblock_unlock();
  8659. return SCRIPT_CMD_SUCCESS;
  8660. }
  8661. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  8662. { //Strips the event from the mob if it's killed the old method.
  8663. struct mob_data *md;
  8664. md = BL_CAST(BL_MOB, bl);
  8665. if (md->npc_event[0])
  8666. md->npc_event[0] = 0;
  8667. status_kill(bl);
  8668. return 0;
  8669. }
  8670. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  8671. {
  8672. status_kill(bl);
  8673. return 0;
  8674. }
  8675. BUILDIN_FUNC(killmonsterall)
  8676. {
  8677. const char *mapname;
  8678. int16 m;
  8679. mapname=script_getstr(st,2);
  8680. if( (m = map_mapname2mapid(mapname))<0 )
  8681. return SCRIPT_CMD_SUCCESS;
  8682. if( script_hasdata(st,3) ) {
  8683. if ( script_getnum(st,3) == 1 ) {
  8684. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  8685. return SCRIPT_CMD_SUCCESS;
  8686. }
  8687. }
  8688. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  8689. return SCRIPT_CMD_SUCCESS;
  8690. }
  8691. /*==========================================
  8692. * Creates a clone of a player.
  8693. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  8694. *------------------------------------------*/
  8695. BUILDIN_FUNC(clone)
  8696. {
  8697. TBL_PC *sd, *msd=NULL;
  8698. uint32 char_id;
  8699. int master_id=0,x,y, mode = 0, flag = 0, m;
  8700. unsigned int duration = 0;
  8701. const char *mapname,*event;
  8702. mapname=script_getstr(st,2);
  8703. x=script_getnum(st,3);
  8704. y=script_getnum(st,4);
  8705. event=script_getstr(st,5);
  8706. char_id=script_getnum(st,6);
  8707. if( script_hasdata(st,7) )
  8708. master_id=script_getnum(st,7);
  8709. if( script_hasdata(st,8) )
  8710. mode=script_getnum(st,8);
  8711. if( script_hasdata(st,9) )
  8712. flag=script_getnum(st,9);
  8713. if( script_hasdata(st,10) )
  8714. duration=script_getnum(st,10);
  8715. check_event(st, event);
  8716. m = map_mapname2mapid(mapname);
  8717. if (m < 0)
  8718. return SCRIPT_CMD_SUCCESS;
  8719. sd = map_charid2sd(char_id);
  8720. if (master_id) {
  8721. msd = map_charid2sd(master_id);
  8722. if (msd)
  8723. master_id = msd->bl.id;
  8724. else
  8725. master_id = 0;
  8726. }
  8727. if (sd) //Return ID of newly crafted clone.
  8728. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  8729. else //Failed to create clone.
  8730. script_pushint(st,0);
  8731. return SCRIPT_CMD_SUCCESS;
  8732. }
  8733. /*==========================================
  8734. *------------------------------------------*/
  8735. BUILDIN_FUNC(doevent)
  8736. {
  8737. const char* event = script_getstr(st,2);
  8738. struct map_session_data* sd;
  8739. if( ( sd = script_rid2sd(st) ) == NULL )
  8740. {
  8741. return SCRIPT_CMD_SUCCESS;
  8742. }
  8743. check_event(st, event);
  8744. npc_event(sd, event, 0);
  8745. return SCRIPT_CMD_SUCCESS;
  8746. }
  8747. /*==========================================
  8748. *------------------------------------------*/
  8749. BUILDIN_FUNC(donpcevent)
  8750. {
  8751. const char* event = script_getstr(st,2);
  8752. check_event(st, event);
  8753. if( !npc_event_do(event) ) {
  8754. struct npc_data * nd = map_id2nd(st->oid);
  8755. ShowDebug("NPCEvent '%s' not found! (source: %s)\n",event,nd?nd->name:"Unknown");
  8756. script_pushint(st, 0);
  8757. } else
  8758. script_pushint(st, 1);
  8759. return SCRIPT_CMD_SUCCESS;
  8760. }
  8761. /// for Aegis compatibility
  8762. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  8763. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  8764. {
  8765. const char* npc = script_getstr(st,2);
  8766. const char* command = script_getstr(st,3);
  8767. char event[EVENT_NAME_LENGTH];
  8768. snprintf(event, sizeof(event), "%s::OnCommand%s", npc, command);
  8769. check_event(st, event);
  8770. npc_event_do(event);
  8771. return SCRIPT_CMD_SUCCESS;
  8772. }
  8773. /*==========================================
  8774. *------------------------------------------*/
  8775. BUILDIN_FUNC(addtimer)
  8776. {
  8777. int tick = script_getnum(st,2);
  8778. const char* event = script_getstr(st, 3);
  8779. TBL_PC* sd;
  8780. check_event(st, event);
  8781. sd = script_rid2sd(st);
  8782. if( sd == NULL )
  8783. return SCRIPT_CMD_SUCCESS;
  8784. if (!pc_addeventtimer(sd,tick,event)) {
  8785. ShowWarning("buildin_addtimer: Event timer is full, can't add new event timer. (cid:%d timer:%s)\n",sd->status.char_id,event);
  8786. return SCRIPT_CMD_FAILURE;
  8787. }
  8788. return SCRIPT_CMD_SUCCESS;
  8789. }
  8790. /*==========================================
  8791. *------------------------------------------*/
  8792. BUILDIN_FUNC(deltimer)
  8793. {
  8794. const char *event;
  8795. TBL_PC* sd;
  8796. event=script_getstr(st, 2);
  8797. sd = script_rid2sd(st);
  8798. if( sd == NULL )
  8799. return SCRIPT_CMD_SUCCESS;
  8800. check_event(st, event);
  8801. pc_deleventtimer(sd,event);
  8802. return SCRIPT_CMD_SUCCESS;
  8803. }
  8804. /*==========================================
  8805. *------------------------------------------*/
  8806. BUILDIN_FUNC(addtimercount)
  8807. {
  8808. const char *event;
  8809. int tick;
  8810. TBL_PC* sd;
  8811. tick=script_getnum(st,2);
  8812. event=script_getstr(st,3);
  8813. sd = script_rid2sd(st);
  8814. if( sd == NULL )
  8815. return SCRIPT_CMD_SUCCESS;
  8816. check_event(st, event);
  8817. pc_addeventtimercount(sd,event,tick);
  8818. return SCRIPT_CMD_SUCCESS;
  8819. }
  8820. /*==========================================
  8821. *------------------------------------------*/
  8822. BUILDIN_FUNC(initnpctimer)
  8823. {
  8824. struct npc_data *nd;
  8825. int flag = 0;
  8826. if( script_hasdata(st,3) )
  8827. { //Two arguments: NPC name and attach flag.
  8828. nd = npc_name2id(script_getstr(st, 2));
  8829. flag = script_getnum(st,3);
  8830. }
  8831. else if( script_hasdata(st,2) )
  8832. { //Check if argument is numeric (flag) or string (npc name)
  8833. struct script_data *data;
  8834. data = script_getdata(st,2);
  8835. get_val(st,data);
  8836. if( data_isstring(data) ) //NPC name
  8837. nd = npc_name2id(conv_str(st, data));
  8838. else if( data_isint(data) ) //Flag
  8839. {
  8840. nd = (struct npc_data *)map_id2bl(st->oid);
  8841. flag = conv_num(st,data);
  8842. }
  8843. else
  8844. {
  8845. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8846. return SCRIPT_CMD_FAILURE;
  8847. }
  8848. }
  8849. else
  8850. nd = (struct npc_data *)map_id2bl(st->oid);
  8851. if( !nd )
  8852. return SCRIPT_CMD_SUCCESS;
  8853. if( flag ) //Attach
  8854. {
  8855. TBL_PC* sd = script_rid2sd(st);
  8856. if( sd == NULL )
  8857. return SCRIPT_CMD_SUCCESS;
  8858. nd->u.scr.rid = sd->bl.id;
  8859. }
  8860. nd->u.scr.timertick = 0;
  8861. npc_settimerevent_tick(nd,0);
  8862. npc_timerevent_start(nd, st->rid);
  8863. return SCRIPT_CMD_SUCCESS;
  8864. }
  8865. /*==========================================
  8866. *------------------------------------------*/
  8867. BUILDIN_FUNC(startnpctimer)
  8868. {
  8869. struct npc_data *nd;
  8870. int flag = 0;
  8871. if( script_hasdata(st,3) )
  8872. { //Two arguments: NPC name and attach flag.
  8873. nd = npc_name2id(script_getstr(st, 2));
  8874. flag = script_getnum(st,3);
  8875. }
  8876. else if( script_hasdata(st,2) )
  8877. { //Check if argument is numeric (flag) or string (npc name)
  8878. struct script_data *data;
  8879. data = script_getdata(st,2);
  8880. get_val(st,data);
  8881. if( data_isstring(data) ) //NPC name
  8882. nd = npc_name2id(conv_str(st, data));
  8883. else if( data_isint(data) ) //Flag
  8884. {
  8885. nd = (struct npc_data *)map_id2bl(st->oid);
  8886. flag = conv_num(st,data);
  8887. }
  8888. else
  8889. {
  8890. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8891. return SCRIPT_CMD_FAILURE;
  8892. }
  8893. }
  8894. else
  8895. nd=(struct npc_data *)map_id2bl(st->oid);
  8896. if( !nd )
  8897. return SCRIPT_CMD_SUCCESS;
  8898. if( flag ) //Attach
  8899. {
  8900. TBL_PC* sd = script_rid2sd(st);
  8901. if( sd == NULL )
  8902. return SCRIPT_CMD_SUCCESS;
  8903. nd->u.scr.rid = sd->bl.id;
  8904. }
  8905. npc_timerevent_start(nd, st->rid);
  8906. return SCRIPT_CMD_SUCCESS;
  8907. }
  8908. /*==========================================
  8909. *------------------------------------------*/
  8910. BUILDIN_FUNC(stopnpctimer)
  8911. {
  8912. struct npc_data *nd;
  8913. int flag = 0;
  8914. if( script_hasdata(st,3) )
  8915. { //Two arguments: NPC name and attach flag.
  8916. nd = npc_name2id(script_getstr(st, 2));
  8917. flag = script_getnum(st,3);
  8918. }
  8919. else if( script_hasdata(st,2) )
  8920. { //Check if argument is numeric (flag) or string (npc name)
  8921. struct script_data *data;
  8922. data = script_getdata(st,2);
  8923. get_val(st,data);
  8924. if( data_isstring(data) ) //NPC name
  8925. nd = npc_name2id(conv_str(st, data));
  8926. else if( data_isint(data) ) //Flag
  8927. {
  8928. nd = (struct npc_data *)map_id2bl(st->oid);
  8929. flag = conv_num(st,data);
  8930. }
  8931. else
  8932. {
  8933. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  8934. return SCRIPT_CMD_FAILURE;
  8935. }
  8936. }
  8937. else
  8938. nd=(struct npc_data *)map_id2bl(st->oid);
  8939. if( !nd )
  8940. return SCRIPT_CMD_SUCCESS;
  8941. if( flag ) //Detach
  8942. nd->u.scr.rid = 0;
  8943. npc_timerevent_stop(nd);
  8944. return SCRIPT_CMD_SUCCESS;
  8945. }
  8946. /*==========================================
  8947. *------------------------------------------*/
  8948. BUILDIN_FUNC(getnpctimer)
  8949. {
  8950. struct npc_data *nd;
  8951. TBL_PC *sd;
  8952. int type = script_getnum(st,2);
  8953. int val = 0;
  8954. if( script_hasdata(st,3) )
  8955. nd = npc_name2id(script_getstr(st,3));
  8956. else
  8957. nd = (struct npc_data *)map_id2bl(st->oid);
  8958. if( !nd || nd->bl.type != BL_NPC )
  8959. {
  8960. script_pushint(st,0);
  8961. ShowError("getnpctimer: Invalid NPC.\n");
  8962. return SCRIPT_CMD_FAILURE;
  8963. }
  8964. switch( type )
  8965. {
  8966. case 0: val = npc_gettimerevent_tick(nd); break;
  8967. case 1:
  8968. if( nd->u.scr.rid )
  8969. {
  8970. sd = map_id2sd(nd->u.scr.rid);
  8971. if( !sd )
  8972. {
  8973. ShowError("buildin_getnpctimer: Attached player not found!\n");
  8974. break;
  8975. }
  8976. val = (sd->npc_timer_id != INVALID_TIMER);
  8977. }
  8978. else
  8979. val = (nd->u.scr.timerid != INVALID_TIMER);
  8980. break;
  8981. case 2: val = nd->u.scr.timeramount; break;
  8982. }
  8983. script_pushint(st,val);
  8984. return SCRIPT_CMD_SUCCESS;
  8985. }
  8986. /*==========================================
  8987. *------------------------------------------*/
  8988. BUILDIN_FUNC(setnpctimer)
  8989. {
  8990. int tick;
  8991. struct npc_data *nd;
  8992. tick = script_getnum(st,2);
  8993. if( script_hasdata(st,3) )
  8994. nd = npc_name2id(script_getstr(st,3));
  8995. else
  8996. nd = (struct npc_data *)map_id2bl(st->oid);
  8997. if( !nd || nd->bl.type != BL_NPC )
  8998. {
  8999. script_pushint(st,1);
  9000. ShowError("setnpctimer: Invalid NPC.\n");
  9001. return SCRIPT_CMD_FAILURE;
  9002. }
  9003. npc_settimerevent_tick(nd,tick);
  9004. script_pushint(st,0);
  9005. return SCRIPT_CMD_SUCCESS;
  9006. }
  9007. /*==========================================
  9008. * attaches the player rid to the timer [Celest]
  9009. *------------------------------------------*/
  9010. BUILDIN_FUNC(attachnpctimer)
  9011. {
  9012. TBL_PC *sd;
  9013. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  9014. if( !nd || nd->bl.type != BL_NPC )
  9015. {
  9016. script_pushint(st,1);
  9017. ShowError("setnpctimer: Invalid NPC.\n");
  9018. return SCRIPT_CMD_FAILURE;
  9019. }
  9020. if( script_hasdata(st,2) )
  9021. sd = map_nick2sd(script_getstr(st,2));
  9022. else
  9023. sd = script_rid2sd(st);
  9024. if( !sd )
  9025. {
  9026. script_pushint(st,1);
  9027. ShowWarning("attachnpctimer: Invalid player.\n");
  9028. return SCRIPT_CMD_FAILURE;
  9029. }
  9030. nd->u.scr.rid = sd->bl.id;
  9031. script_pushint(st,0);
  9032. return SCRIPT_CMD_SUCCESS;
  9033. }
  9034. /*==========================================
  9035. * detaches a player rid from the timer [Celest]
  9036. *------------------------------------------*/
  9037. BUILDIN_FUNC(detachnpctimer)
  9038. {
  9039. struct npc_data *nd;
  9040. if( script_hasdata(st,2) )
  9041. nd = npc_name2id(script_getstr(st,2));
  9042. else
  9043. nd = (struct npc_data *)map_id2bl(st->oid);
  9044. if( !nd || nd->bl.type != BL_NPC )
  9045. {
  9046. script_pushint(st,1);
  9047. ShowError("detachnpctimer: Invalid NPC.\n");
  9048. return SCRIPT_CMD_FAILURE;
  9049. }
  9050. nd->u.scr.rid = 0;
  9051. script_pushint(st,0);
  9052. return SCRIPT_CMD_SUCCESS;
  9053. }
  9054. /*==========================================
  9055. * To avoid "player not attached" script errors, this function is provided,
  9056. * it checks if there is a player attached to the current script. [Skotlex]
  9057. * If no, returns 0, if yes, returns the account_id of the attached player.
  9058. *------------------------------------------*/
  9059. BUILDIN_FUNC(playerattached)
  9060. {
  9061. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  9062. script_pushint(st,0);
  9063. else
  9064. script_pushint(st,st->rid);
  9065. return SCRIPT_CMD_SUCCESS;
  9066. }
  9067. /*==========================================
  9068. *------------------------------------------*/
  9069. BUILDIN_FUNC(announce)
  9070. {
  9071. const char *mes = script_getstr(st,2);
  9072. int flag = script_getnum(st,3);
  9073. const char *fontColor = script_hasdata(st,4) ? script_getstr(st,4) : NULL;
  9074. int fontType = script_hasdata(st,5) ? script_getnum(st,5) : FW_NORMAL; // default fontType
  9075. int fontSize = script_hasdata(st,6) ? script_getnum(st,6) : 12; // default fontSize
  9076. int fontAlign = script_hasdata(st,7) ? script_getnum(st,7) : 0; // default fontAlign
  9077. int fontY = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontY
  9078. if (flag&(BC_TARGET_MASK|BC_SOURCE_MASK)) // Broadcast source or broadcast region defined
  9079. {
  9080. send_target target;
  9081. struct block_list *bl = (flag&BC_NPC) ? map_id2bl(st->oid) : (struct block_list *)script_rid2sd(st); // If bc_npc flag is set, use NPC as broadcast source
  9082. if (bl == NULL)
  9083. return SCRIPT_CMD_SUCCESS;
  9084. switch (flag&BC_TARGET_MASK) {
  9085. case BC_MAP: target = ALL_SAMEMAP; break;
  9086. case BC_AREA: target = AREA; break;
  9087. case BC_SELF: target = SELF; break;
  9088. default: target = ALL_CLIENT; break; // BC_ALL
  9089. }
  9090. if (fontColor)
  9091. clif_broadcast2(bl, mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, target);
  9092. else
  9093. clif_broadcast(bl, mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK, target);
  9094. }
  9095. else
  9096. {
  9097. if (fontColor)
  9098. intif_broadcast2(mes, (int)strlen(mes)+1, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY);
  9099. else
  9100. intif_broadcast(mes, (int)strlen(mes)+1, flag&BC_COLOR_MASK);
  9101. }
  9102. return SCRIPT_CMD_SUCCESS;
  9103. }
  9104. /*==========================================
  9105. *------------------------------------------*/
  9106. static int buildin_announce_sub(struct block_list *bl, va_list ap)
  9107. {
  9108. char *mes = va_arg(ap, char *);
  9109. int len = va_arg(ap, int);
  9110. int type = va_arg(ap, int);
  9111. char *fontColor = va_arg(ap, char *);
  9112. short fontType = (short)va_arg(ap, int);
  9113. short fontSize = (short)va_arg(ap, int);
  9114. short fontAlign = (short)va_arg(ap, int);
  9115. short fontY = (short)va_arg(ap, int);
  9116. if (fontColor)
  9117. clif_broadcast2(bl, mes, len, strtol(fontColor, (char **)NULL, 0), fontType, fontSize, fontAlign, fontY, SELF);
  9118. else
  9119. clif_broadcast(bl, mes, len, type, SELF);
  9120. return SCRIPT_CMD_SUCCESS;
  9121. }
  9122. BUILDIN_FUNC(mapannounce)
  9123. {
  9124. const char *mapname = script_getstr(st,2);
  9125. const char *mes = script_getstr(st,3);
  9126. int flag = script_getnum(st,4);
  9127. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  9128. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  9129. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  9130. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  9131. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  9132. int16 m;
  9133. if ((m = map_mapname2mapid(mapname)) < 0)
  9134. return SCRIPT_CMD_SUCCESS;
  9135. map_foreachinmap(buildin_announce_sub, m, BL_PC,
  9136. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9137. return SCRIPT_CMD_SUCCESS;
  9138. }
  9139. /*==========================================
  9140. *------------------------------------------*/
  9141. BUILDIN_FUNC(areaannounce)
  9142. {
  9143. const char *mapname = script_getstr(st,2);
  9144. int x0 = script_getnum(st,3);
  9145. int y0 = script_getnum(st,4);
  9146. int x1 = script_getnum(st,5);
  9147. int y1 = script_getnum(st,6);
  9148. const char *mes = script_getstr(st,7);
  9149. int flag = script_getnum(st,8);
  9150. const char *fontColor = script_hasdata(st,9) ? script_getstr(st,9) : NULL;
  9151. int fontType = script_hasdata(st,10) ? script_getnum(st,10) : FW_NORMAL; // default fontType
  9152. int fontSize = script_hasdata(st,11) ? script_getnum(st,11) : 12; // default fontSize
  9153. int fontAlign = script_hasdata(st,12) ? script_getnum(st,12) : 0; // default fontAlign
  9154. int fontY = script_hasdata(st,13) ? script_getnum(st,13) : 0; // default fontY
  9155. int16 m;
  9156. if ((m = map_mapname2mapid(mapname)) < 0)
  9157. return SCRIPT_CMD_SUCCESS;
  9158. map_foreachinarea(buildin_announce_sub, m, x0, y0, x1, y1, BL_PC,
  9159. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  9160. return SCRIPT_CMD_SUCCESS;
  9161. }
  9162. /*==========================================
  9163. *------------------------------------------*/
  9164. BUILDIN_FUNC(getusers)
  9165. {
  9166. int flag, val = 0;
  9167. struct map_session_data* sd;
  9168. struct block_list* bl = NULL;
  9169. flag = script_getnum(st,2);
  9170. switch(flag&0x07)
  9171. {
  9172. case 0:
  9173. if(flag&0x8)
  9174. {// npc
  9175. bl = map_id2bl(st->oid);
  9176. }
  9177. else if((sd = script_rid2sd(st))!=NULL)
  9178. {// pc
  9179. bl = &sd->bl;
  9180. }
  9181. if(bl)
  9182. {
  9183. val = map[bl->m].users;
  9184. }
  9185. break;
  9186. case 1:
  9187. val = map_getusers();
  9188. break;
  9189. default:
  9190. ShowWarning("buildin_getusers: Unknown type %d.\n", flag);
  9191. script_pushint(st,0);
  9192. return SCRIPT_CMD_FAILURE;
  9193. }
  9194. script_pushint(st,val);
  9195. return SCRIPT_CMD_SUCCESS;
  9196. }
  9197. /*==========================================
  9198. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  9199. *------------------------------------------*/
  9200. BUILDIN_FUNC(getmapguildusers)
  9201. {
  9202. const char *str;
  9203. int16 m;
  9204. int gid;
  9205. int c=0;
  9206. struct guild *g = NULL;
  9207. str=script_getstr(st,2);
  9208. gid=script_getnum(st,3);
  9209. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  9210. script_pushint(st,-1);
  9211. return SCRIPT_CMD_SUCCESS;
  9212. }
  9213. g = guild_search(gid);
  9214. if (g){
  9215. unsigned short i;
  9216. for(i = 0; i < g->max_member; i++)
  9217. {
  9218. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  9219. c++;
  9220. }
  9221. }
  9222. script_pushint(st,c);
  9223. return SCRIPT_CMD_SUCCESS;
  9224. }
  9225. /*==========================================
  9226. *------------------------------------------*/
  9227. BUILDIN_FUNC(getmapusers)
  9228. {
  9229. const char *str;
  9230. int16 m;
  9231. str=script_getstr(st,2);
  9232. if( (m=map_mapname2mapid(str))< 0){
  9233. script_pushint(st,-1);
  9234. return SCRIPT_CMD_SUCCESS;
  9235. }
  9236. script_pushint(st,map[m].users);
  9237. return SCRIPT_CMD_SUCCESS;
  9238. }
  9239. /*==========================================
  9240. *------------------------------------------*/
  9241. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  9242. {
  9243. int *users=va_arg(ap,int *);
  9244. (*users)++;
  9245. return SCRIPT_CMD_SUCCESS;
  9246. }
  9247. BUILDIN_FUNC(getareausers)
  9248. {
  9249. const char *str;
  9250. int16 m,x0,y0,x1,y1,users=0; //doubt we can have more then 32k users on
  9251. str=script_getstr(st,2);
  9252. x0=script_getnum(st,3);
  9253. y0=script_getnum(st,4);
  9254. x1=script_getnum(st,5);
  9255. y1=script_getnum(st,6);
  9256. if( (m=map_mapname2mapid(str))< 0){
  9257. script_pushint(st,-1);
  9258. return SCRIPT_CMD_SUCCESS;
  9259. }
  9260. map_foreachinarea(buildin_getareausers_sub,
  9261. m,x0,y0,x1,y1,BL_PC,&users);
  9262. script_pushint(st,users);
  9263. return SCRIPT_CMD_SUCCESS;
  9264. }
  9265. /*==========================================
  9266. *------------------------------------------*/
  9267. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  9268. {
  9269. int nameid=va_arg(ap,int);
  9270. int *amount=va_arg(ap,int *);
  9271. struct flooritem_data *drop=(struct flooritem_data *)bl;
  9272. if(drop->item.nameid==nameid)
  9273. (*amount)+=drop->item.amount;
  9274. return SCRIPT_CMD_SUCCESS;
  9275. }
  9276. BUILDIN_FUNC(getareadropitem)
  9277. {
  9278. const char *str;
  9279. int16 m,x0,y0,x1,y1;
  9280. int nameid,amount=0;
  9281. struct script_data *data;
  9282. str=script_getstr(st,2);
  9283. x0=script_getnum(st,3);
  9284. y0=script_getnum(st,4);
  9285. x1=script_getnum(st,5);
  9286. y1=script_getnum(st,6);
  9287. data=script_getdata(st,7);
  9288. get_val(st,data);
  9289. if( data_isstring(data) ){
  9290. const char *name=conv_str(st,data);
  9291. struct item_data *item_data = itemdb_searchname(name);
  9292. nameid=UNKNOWN_ITEM_ID;
  9293. if( item_data )
  9294. nameid=item_data->nameid;
  9295. }else
  9296. nameid=conv_num(st,data);
  9297. if( (m=map_mapname2mapid(str))< 0){
  9298. script_pushint(st,-1);
  9299. return SCRIPT_CMD_SUCCESS;
  9300. }
  9301. map_foreachinarea(buildin_getareadropitem_sub,
  9302. m,x0,y0,x1,y1,BL_ITEM,nameid,&amount);
  9303. script_pushint(st,amount);
  9304. return SCRIPT_CMD_SUCCESS;
  9305. }
  9306. /*==========================================
  9307. *------------------------------------------*/
  9308. BUILDIN_FUNC(enablenpc)
  9309. {
  9310. const char *str;
  9311. str=script_getstr(st,2);
  9312. npc_enable(str,1);
  9313. return SCRIPT_CMD_SUCCESS;
  9314. }
  9315. /*==========================================
  9316. *------------------------------------------*/
  9317. BUILDIN_FUNC(disablenpc)
  9318. {
  9319. const char *str;
  9320. str=script_getstr(st,2);
  9321. npc_enable(str,0);
  9322. return SCRIPT_CMD_SUCCESS;
  9323. }
  9324. /*==========================================
  9325. *------------------------------------------*/
  9326. BUILDIN_FUNC(hideoffnpc)
  9327. {
  9328. const char *str;
  9329. str=script_getstr(st,2);
  9330. npc_enable(str,2);
  9331. return SCRIPT_CMD_SUCCESS;
  9332. }
  9333. /*==========================================
  9334. *------------------------------------------*/
  9335. BUILDIN_FUNC(hideonnpc)
  9336. {
  9337. const char *str;
  9338. str=script_getstr(st,2);
  9339. npc_enable(str,4);
  9340. return SCRIPT_CMD_SUCCESS;
  9341. }
  9342. /* Starts a status effect on the target unit or on the attached player.
  9343. *
  9344. * sc_start <effect_id>,<duration>,<val1>{,<rate>,<flag>,{<unit_id>}};
  9345. * sc_start2 <effect_id>,<duration>,<val1>,<val2>{,<rate,<flag>,{<unit_id>}};
  9346. * sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<rate,<flag>,{<unit_id>}};
  9347. * <flag>: enum e_status_change_start_flags
  9348. */
  9349. BUILDIN_FUNC(sc_start)
  9350. {
  9351. TBL_NPC * nd = map_id2nd(st->oid);
  9352. struct block_list* bl;
  9353. enum sc_type type;
  9354. int tick, val1, val2, val3, val4=0, rate, flag;
  9355. char start_type;
  9356. const char* command = script_getfuncname(st);
  9357. if(strstr(command, "4"))
  9358. start_type = 4;
  9359. else if(strstr(command, "2"))
  9360. start_type = 2;
  9361. else
  9362. start_type = 1;
  9363. type = (sc_type)script_getnum(st,2);
  9364. tick = script_getnum(st,3);
  9365. val1 = script_getnum(st,4);
  9366. //If from NPC we make default flag 1 to be unavoidable
  9367. if(nd && nd->bl.id == fake_nd->bl.id)
  9368. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOTICKDEF;
  9369. else
  9370. flag = script_hasdata(st,5+start_type)?script_getnum(st,5+start_type):SCSTART_NOAVOID;
  9371. rate = script_hasdata(st,4+start_type)?min(script_getnum(st,4+start_type),10000):10000;
  9372. if(script_hasdata(st,(6+start_type)))
  9373. bl = map_id2bl(script_getnum(st,(6+start_type)));
  9374. else
  9375. bl = map_id2bl(st->rid);
  9376. if(tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0)
  9377. {// When there isn't a duration specified, try to get it from the skill_db
  9378. tick = skill_get_time(status_sc2skill(type), val1);
  9379. }
  9380. if(potion_flag == 1 && potion_target) { //skill.c set the flags before running the script, this is a potion-pitched effect.
  9381. bl = map_id2bl(potion_target);
  9382. tick /= 2;// Thrown potions only last half.
  9383. val4 = 1;// Mark that this was a thrown sc_effect
  9384. }
  9385. if(!bl)
  9386. return SCRIPT_CMD_SUCCESS;
  9387. switch(start_type) {
  9388. case 1:
  9389. status_change_start(bl, bl, type, rate, val1, 0, 0, val4, tick, flag);
  9390. break;
  9391. case 2:
  9392. val2 = script_getnum(st,5);
  9393. status_change_start(bl, bl, type, rate, val1, val2, 0, val4, tick, flag);
  9394. break;
  9395. case 4:
  9396. val2 = script_getnum(st,5);
  9397. val3 = script_getnum(st,6);
  9398. val4 = script_getnum(st,7);
  9399. status_change_start(bl, bl, type, rate, val1, val2, val3, val4, tick, flag);
  9400. break;
  9401. }
  9402. return SCRIPT_CMD_SUCCESS;
  9403. }
  9404. /// Ends one or all status effects on the target unit or on the attached player.
  9405. ///
  9406. /// sc_end <effect_id>{,<unit_id>};
  9407. BUILDIN_FUNC(sc_end)
  9408. {
  9409. struct block_list* bl;
  9410. int type;
  9411. type = script_getnum(st, 2);
  9412. if (script_hasdata(st, 3))
  9413. bl = map_id2bl(script_getnum(st, 3));
  9414. else
  9415. bl = map_id2bl(st->rid);
  9416. if (potion_flag == 1 && potion_target) //##TODO how does this work [FlavioJS]
  9417. bl = map_id2bl(potion_target);
  9418. if (!bl)
  9419. return SCRIPT_CMD_SUCCESS;
  9420. if (type >= 0 && type < SC_MAX) {
  9421. struct status_change *sc = status_get_sc(bl);
  9422. struct status_change_entry *sce = sc ? sc->data[type] : NULL;
  9423. if (!sce)
  9424. return SCRIPT_CMD_SUCCESS;
  9425. switch (type) {
  9426. case SC_WEIGHT50:
  9427. case SC_WEIGHT90:
  9428. case SC_NOCHAT:
  9429. case SC_PUSH_CART:
  9430. case SC_ALL_RIDING:
  9431. case SC_STYLE_CHANGE:
  9432. case SC_MONSTER_TRANSFORM:
  9433. case SC_MTF_ASPD:
  9434. case SC_MTF_RANGEATK:
  9435. case SC_MTF_MATK:
  9436. case SC_MTF_MLEATKED:
  9437. case SC_MTF_CRIDAMAGE:
  9438. case SC_MTF_ASPD2:
  9439. case SC_MTF_RANGEATK2:
  9440. case SC_MTF_MATK2:
  9441. case SC_MTF_MHP:
  9442. case SC_MTF_MSP:
  9443. case SC_MTF_PUMPKIN:
  9444. case SC_MTF_HITFLEE:
  9445. case SC_ATTHASTE_CASH:
  9446. case SC_REUSE_LIMIT_A: case SC_REUSE_LIMIT_B: case SC_REUSE_LIMIT_C:
  9447. case SC_REUSE_LIMIT_D: case SC_REUSE_LIMIT_E: case SC_REUSE_LIMIT_F:
  9448. case SC_REUSE_LIMIT_G: case SC_REUSE_LIMIT_H: case SC_REUSE_LIMIT_MTF:
  9449. case SC_REUSE_LIMIT_ASPD_POTION: case SC_REUSE_MILLENNIUMSHIELD: case SC_REUSE_CRUSHSTRIKE:
  9450. case SC_REUSE_STORMBLAST: case SC_ALL_RIDING_REUSE_LIMIT: case SC_REUSE_REFRESH:
  9451. case SC_REUSE_LIMIT_ECL: case SC_REUSE_LIMIT_RECALL:
  9452. return SCRIPT_CMD_SUCCESS;
  9453. default:
  9454. break;
  9455. }
  9456. //This should help status_change_end force disabling the SC in case it has no limit.
  9457. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  9458. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  9459. } else
  9460. status_change_clear(bl, 3); // remove all effects
  9461. return SCRIPT_CMD_SUCCESS;
  9462. }
  9463. /*==========================================
  9464. * @FIXME atm will return reduced tick, 0 immune, 1 no tick
  9465. *------------------------------------------*/
  9466. BUILDIN_FUNC(getscrate)
  9467. {
  9468. struct block_list *bl;
  9469. int type,rate;
  9470. type=script_getnum(st,2);
  9471. rate=script_getnum(st,3);
  9472. if( script_hasdata(st,4) ) //get for the bl assigned
  9473. bl = map_id2bl(script_getnum(st,4));
  9474. else
  9475. bl = map_id2bl(st->rid);
  9476. if (bl)
  9477. rate = status_get_sc_def(NULL,bl, (sc_type)type, 10000, 10000, SCSTART_NONE);
  9478. script_pushint(st,rate);
  9479. return SCRIPT_CMD_SUCCESS;
  9480. }
  9481. /**
  9482. * getstatus(<effect type>{,<type>{,<char_id>}});
  9483. **/
  9484. BUILDIN_FUNC(getstatus)
  9485. {
  9486. int id, type;
  9487. struct map_session_data* sd;
  9488. if (!script_charid2sd(4,sd))
  9489. return SCRIPT_CMD_FAILURE;
  9490. id = script_getnum(st, 2);
  9491. type = script_hasdata(st, 3) ? script_getnum(st, 3) : 0;
  9492. if( id <= SC_NONE || id >= SC_MAX )
  9493. {// invalid status type given
  9494. ShowWarning("script.c:getstatus: Invalid status type given (%d).\n", id);
  9495. return SCRIPT_CMD_SUCCESS;
  9496. }
  9497. if( sd->sc.count == 0 || !sd->sc.data[id] )
  9498. {// no status is active
  9499. script_pushint(st, 0);
  9500. return SCRIPT_CMD_SUCCESS;
  9501. }
  9502. switch( type )
  9503. {
  9504. case 1: script_pushint(st, sd->sc.data[id]->val1); break;
  9505. case 2: script_pushint(st, sd->sc.data[id]->val2); break;
  9506. case 3: script_pushint(st, sd->sc.data[id]->val3); break;
  9507. case 4: script_pushint(st, sd->sc.data[id]->val4); break;
  9508. case 5:
  9509. {
  9510. struct TimerData* timer = (struct TimerData*)get_timer(sd->sc.data[id]->timer);
  9511. if( timer )
  9512. {// return the amount of time remaining
  9513. script_pushint(st, timer->tick - gettick());
  9514. } else {
  9515. script_pushint(st, -1);
  9516. }
  9517. }
  9518. break;
  9519. default: script_pushint(st, 1); break;
  9520. }
  9521. return SCRIPT_CMD_SUCCESS;
  9522. }
  9523. /*==========================================
  9524. *
  9525. *------------------------------------------*/
  9526. BUILDIN_FUNC(debugmes)
  9527. {
  9528. const char *str;
  9529. str=script_getstr(st,2);
  9530. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  9531. return SCRIPT_CMD_SUCCESS;
  9532. }
  9533. /*==========================================
  9534. *------------------------------------------*/
  9535. BUILDIN_FUNC(catchpet)
  9536. {
  9537. int pet_id;
  9538. TBL_PC *sd;
  9539. pet_id= script_getnum(st,2);
  9540. sd=script_rid2sd(st);
  9541. if( sd == NULL )
  9542. return SCRIPT_CMD_SUCCESS;
  9543. pet_catch_process1(sd,pet_id);
  9544. return SCRIPT_CMD_SUCCESS;
  9545. }
  9546. /*==========================================
  9547. * [orn]
  9548. *------------------------------------------*/
  9549. BUILDIN_FUNC(homunculus_evolution)
  9550. {
  9551. TBL_PC *sd;
  9552. sd=script_rid2sd(st);
  9553. if( sd == NULL )
  9554. return SCRIPT_CMD_SUCCESS;
  9555. if(hom_is_active(sd->hd))
  9556. {
  9557. if (sd->hd->homunculus.intimacy >= battle_config.homunculus_evo_intimacy_need)
  9558. hom_evolution(sd->hd);
  9559. else
  9560. clif_emotion(&sd->hd->bl, E_SWT);
  9561. }
  9562. return SCRIPT_CMD_SUCCESS;
  9563. }
  9564. /*==========================================
  9565. * Checks for vaporized morph state
  9566. * and deletes ITEMID_STRANGE_EMBRYO.
  9567. *------------------------------------------*/
  9568. BUILDIN_FUNC(homunculus_mutate)
  9569. {
  9570. int homun_id;
  9571. TBL_PC *sd;
  9572. sd = script_rid2sd(st);
  9573. if( sd == NULL || sd->hd == NULL )
  9574. return SCRIPT_CMD_SUCCESS;
  9575. if(script_hasdata(st,2))
  9576. homun_id = script_getnum(st,2);
  9577. else
  9578. homun_id = 6048 + (rnd() % 4);
  9579. if( sd->hd->homunculus.vaporize == HOM_ST_MORPH ) {
  9580. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9581. int m_id = hom_class2mapid(homun_id);
  9582. short i = pc_search_inventory(sd, ITEMID_STRANGE_EMBRYO);
  9583. if ( m_class != -1 && m_id != -1 && m_class&HOM_EVO && m_id&HOM_S && sd->hd->homunculus.level >= 99 && i >= 0 ) {
  9584. sd->hd->homunculus.vaporize = HOM_ST_REST; // Remove morph state.
  9585. hom_call(sd); // Respawn homunculus.
  9586. hom_mutate(sd->hd, homun_id);
  9587. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_SCRIPT);
  9588. script_pushint(st, 1);
  9589. return SCRIPT_CMD_SUCCESS;
  9590. } else
  9591. clif_emotion(&sd->bl, E_SWT);
  9592. } else
  9593. clif_emotion(&sd->bl, E_SWT);
  9594. script_pushint(st, 0);
  9595. return SCRIPT_CMD_SUCCESS;
  9596. }
  9597. /*==========================================
  9598. * Puts homunculus into morph state
  9599. * and gives ITEMID_STRANGE_EMBRYO.
  9600. *------------------------------------------*/
  9601. BUILDIN_FUNC(morphembryo)
  9602. {
  9603. struct item item_tmp;
  9604. TBL_PC *sd;
  9605. sd = script_rid2sd(st);
  9606. if( sd == NULL || sd->hd == NULL )
  9607. return SCRIPT_CMD_SUCCESS;
  9608. if( hom_is_active(sd->hd) ) {
  9609. int m_class = hom_class2mapid(sd->hd->homunculus.class_);
  9610. if ( m_class != -1 && m_class&HOM_EVO && sd->hd->homunculus.level >= 99 ) {
  9611. char i;
  9612. memset(&item_tmp, 0, sizeof(item_tmp));
  9613. item_tmp.nameid = ITEMID_STRANGE_EMBRYO;
  9614. item_tmp.identify = 1;
  9615. if( (i = pc_additem(sd, &item_tmp, 1, LOG_TYPE_SCRIPT)) ) {
  9616. clif_additem(sd, 0, 0, i);
  9617. clif_emotion(&sd->bl, E_SWT); // Fail to avoid item drop exploit.
  9618. } else {
  9619. hom_vaporize(sd, HOM_ST_MORPH);
  9620. script_pushint(st, 1);
  9621. return SCRIPT_CMD_SUCCESS;
  9622. }
  9623. } else
  9624. clif_emotion(&sd->hd->bl, E_SWT);
  9625. } else
  9626. clif_emotion(&sd->bl, E_SWT);
  9627. script_pushint(st, 0);
  9628. return SCRIPT_CMD_SUCCESS;
  9629. }
  9630. // [Zephyrus]
  9631. BUILDIN_FUNC(homunculus_shuffle)
  9632. {
  9633. TBL_PC *sd;
  9634. sd=script_rid2sd(st);
  9635. if( sd == NULL )
  9636. return SCRIPT_CMD_SUCCESS;
  9637. if(hom_is_active(sd->hd))
  9638. hom_shuffle(sd->hd);
  9639. return SCRIPT_CMD_SUCCESS;
  9640. }
  9641. /*==========================================
  9642. * Check for homunculus state.
  9643. * Return: -1 = No homunculus
  9644. * 0 = Homunculus is active
  9645. * 1 = Homunculus is vaporized (rest)
  9646. * 2 = Homunculus is in morph state
  9647. *------------------------------------------*/
  9648. BUILDIN_FUNC(checkhomcall)
  9649. {
  9650. TBL_PC *sd = script_rid2sd(st);
  9651. TBL_HOM *hd;
  9652. if( sd == NULL )
  9653. return SCRIPT_CMD_SUCCESS;
  9654. hd = sd->hd;
  9655. if( !hd )
  9656. script_pushint(st, -1);
  9657. else
  9658. script_pushint(st, hd->homunculus.vaporize);
  9659. return SCRIPT_CMD_SUCCESS;
  9660. }
  9661. //These two functions bring the eA MAPID_* class functionality to scripts.
  9662. BUILDIN_FUNC(eaclass)
  9663. {
  9664. int class_;
  9665. if( script_hasdata(st,2) )
  9666. class_ = script_getnum(st,2);
  9667. else {
  9668. TBL_PC *sd;
  9669. if (!script_charid2sd(3,sd)) {
  9670. script_pushint(st,-1);
  9671. return SCRIPT_CMD_SUCCESS;
  9672. }
  9673. class_ = sd->status.class_;
  9674. }
  9675. script_pushint(st,pc_jobid2mapid(class_));
  9676. return SCRIPT_CMD_SUCCESS;
  9677. }
  9678. BUILDIN_FUNC(roclass)
  9679. {
  9680. int class_ =script_getnum(st,2);
  9681. int sex;
  9682. if( script_hasdata(st,3) )
  9683. sex = script_getnum(st,3);
  9684. else {
  9685. TBL_PC *sd;
  9686. if (st->rid && (sd=script_rid2sd(st)))
  9687. sex = sd->status.sex;
  9688. else
  9689. sex = 1; //Just use male when not found.
  9690. }
  9691. script_pushint(st,pc_mapid2jobid(class_, sex));
  9692. return SCRIPT_CMD_SUCCESS;
  9693. }
  9694. /*==========================================
  9695. * Tells client to open a hatching window, used for pet incubator
  9696. *------------------------------------------*/
  9697. BUILDIN_FUNC(birthpet)
  9698. {
  9699. TBL_PC *sd;
  9700. sd=script_rid2sd(st);
  9701. if( sd == NULL )
  9702. return SCRIPT_CMD_SUCCESS;
  9703. if( sd->status.pet_id )
  9704. {// do not send egg list, when you already have a pet
  9705. return SCRIPT_CMD_SUCCESS;
  9706. }
  9707. clif_sendegg(sd);
  9708. return SCRIPT_CMD_SUCCESS;
  9709. }
  9710. /**
  9711. * resetlvl <action type>{,<char_id>};
  9712. * @param action_type:
  9713. * 1 : make like after rebirth
  9714. * 2 : blvl,jlvl=1, skillpoint=0
  9715. * 3 : don't reset skill, blvl=1
  9716. * 4 : jlvl=0
  9717. * @author AppleGirl
  9718. **/
  9719. BUILDIN_FUNC(resetlvl)
  9720. {
  9721. TBL_PC *sd;
  9722. int type=script_getnum(st,2);
  9723. if (!script_charid2sd(3,sd))
  9724. return SCRIPT_CMD_FAILURE;
  9725. pc_resetlvl(sd,type);
  9726. return SCRIPT_CMD_SUCCESS;
  9727. }
  9728. /**
  9729. * Reset a player status point
  9730. * resetstatus({<char_id>});
  9731. **/
  9732. BUILDIN_FUNC(resetstatus)
  9733. {
  9734. TBL_PC *sd;
  9735. if (!script_charid2sd(2,sd))
  9736. return SCRIPT_CMD_FAILURE;
  9737. pc_resetstate(sd);
  9738. return SCRIPT_CMD_SUCCESS;
  9739. }
  9740. /**
  9741. * Reset player's skill
  9742. * resetskill({<char_id>});
  9743. **/
  9744. BUILDIN_FUNC(resetskill)
  9745. {
  9746. TBL_PC *sd;
  9747. if (!script_charid2sd(2,sd))
  9748. return SCRIPT_CMD_FAILURE;
  9749. pc_resetskill(sd,1);
  9750. return SCRIPT_CMD_SUCCESS;
  9751. }
  9752. /**
  9753. * Counts total amount of skill points.
  9754. * skillpointcount({<char_id>})
  9755. **/
  9756. BUILDIN_FUNC(skillpointcount)
  9757. {
  9758. TBL_PC *sd;
  9759. if (!script_charid2sd(2,sd))
  9760. return SCRIPT_CMD_FAILURE;
  9761. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  9762. return SCRIPT_CMD_SUCCESS;
  9763. }
  9764. /*==========================================
  9765. *
  9766. *------------------------------------------*/
  9767. BUILDIN_FUNC(changebase)
  9768. {
  9769. TBL_PC *sd=NULL;
  9770. int vclass;
  9771. if( script_hasdata(st,3) )
  9772. sd=map_id2sd(script_getnum(st,3));
  9773. else
  9774. sd=script_rid2sd(st);
  9775. if(sd == NULL)
  9776. return SCRIPT_CMD_SUCCESS;
  9777. vclass = script_getnum(st,2);
  9778. if(vclass == JOB_WEDDING)
  9779. {
  9780. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  9781. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  9782. )
  9783. return SCRIPT_CMD_SUCCESS;
  9784. }
  9785. if(!sd->disguise && vclass != sd->vd.class_) {
  9786. status_set_viewdata(&sd->bl, vclass);
  9787. //Updated client view. Base, Weapon and Cloth Colors.
  9788. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  9789. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  9790. if (sd->vd.cloth_color)
  9791. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9792. if (sd->vd.body_style)
  9793. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9794. clif_skillinfoblock(sd);
  9795. }
  9796. return SCRIPT_CMD_SUCCESS;
  9797. }
  9798. /**
  9799. * Change account sex and unequip all item and request for a changesex to char-serv
  9800. * changesex({<char_id>});
  9801. */
  9802. BUILDIN_FUNC(changesex)
  9803. {
  9804. int i;
  9805. TBL_PC *sd = NULL;
  9806. if (!script_charid2sd(2,sd))
  9807. return SCRIPT_CMD_FAILURE;
  9808. pc_resetskill(sd,4);
  9809. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9810. for(i = 0; i < EQI_MAX; i++) {
  9811. if (sd->equip_index[i] >= 0)
  9812. pc_unequipitem(sd, sd->equip_index[i], 3);
  9813. }
  9814. chrif_changesex(sd, true);
  9815. return SCRIPT_CMD_SUCCESS;
  9816. }
  9817. /**
  9818. * Change character's sex and unequip all item and request for a changesex to char-serv
  9819. * changecharsex({<char_id>});
  9820. */
  9821. BUILDIN_FUNC(changecharsex)
  9822. {
  9823. #if PACKETVER >= 20141016
  9824. int i;
  9825. TBL_PC *sd = NULL;
  9826. if (!script_charid2sd(2,sd))
  9827. return SCRIPT_CMD_FAILURE;
  9828. pc_resetskill(sd,4);
  9829. // to avoid any problem with equipment and invalid sex, equipment is unequiped.
  9830. for (i = 0; i < EQI_MAX; i++) {
  9831. if (sd->equip_index[i] >= 0)
  9832. pc_unequipitem(sd, sd->equip_index[i], 3);
  9833. }
  9834. chrif_changesex(sd, false);
  9835. return SCRIPT_CMD_SUCCESS;
  9836. #else
  9837. return SCRIPT_CMD_FAILURE;
  9838. #endif
  9839. }
  9840. /*==========================================
  9841. * Works like 'announce' but outputs in the common chat window
  9842. *------------------------------------------*/
  9843. BUILDIN_FUNC(globalmes)
  9844. {
  9845. struct block_list *bl = map_id2bl(st->oid);
  9846. struct npc_data *nd = (struct npc_data *)bl;
  9847. const char *name=NULL,*mes;
  9848. mes=script_getstr(st,2);
  9849. if(mes==NULL)
  9850. return SCRIPT_CMD_SUCCESS;
  9851. if(script_hasdata(st,3)){ // npc name to display
  9852. name=script_getstr(st,3);
  9853. } else {
  9854. name=nd->name; //use current npc name
  9855. }
  9856. npc_globalmessage(name,mes); // broadcast to all players connected
  9857. return SCRIPT_CMD_SUCCESS;
  9858. }
  9859. /////////////////////////////////////////////////////////////////////
  9860. // NPC waiting room (chat room)
  9861. //
  9862. /// Creates a waiting room (chat room) for this npc.
  9863. ///
  9864. /// waitingroom "<title>",<limit>{,"<event>"{,<trigger>{,<zeny>{,<minlvl>{,<maxlvl>}}}}};
  9865. BUILDIN_FUNC(waitingroom)
  9866. {
  9867. struct npc_data* nd;
  9868. const char* title = script_getstr(st, 2);
  9869. int limit = script_getnum(st, 3);
  9870. const char* ev = script_hasdata(st,4) ? script_getstr(st,4) : "";
  9871. int trigger = script_hasdata(st,5) ? script_getnum(st,5) : limit;
  9872. int zeny = script_hasdata(st,6) ? script_getnum(st,6) : 0;
  9873. int minLvl = script_hasdata(st,7) ? script_getnum(st,7) : 1;
  9874. int maxLvl = script_hasdata(st,8) ? script_getnum(st,8) : MAX_LEVEL;
  9875. nd = (struct npc_data *)map_id2bl(st->oid);
  9876. if( nd != NULL )
  9877. chat_createnpcchat(nd, title, limit, 1, trigger, ev, zeny, minLvl, maxLvl);
  9878. return SCRIPT_CMD_SUCCESS;
  9879. }
  9880. /// Removes the waiting room of the current or target npc.
  9881. ///
  9882. /// delwaitingroom "<npc_name>";
  9883. /// delwaitingroom;
  9884. BUILDIN_FUNC(delwaitingroom)
  9885. {
  9886. struct npc_data* nd;
  9887. if( script_hasdata(st,2) )
  9888. nd = npc_name2id(script_getstr(st, 2));
  9889. else
  9890. nd = (struct npc_data *)map_id2bl(st->oid);
  9891. if( nd != NULL )
  9892. chat_deletenpcchat(nd);
  9893. return SCRIPT_CMD_SUCCESS;
  9894. }
  9895. /// Kick the specified player from the waiting room of the target npc.
  9896. ///
  9897. /// waitingroomkick "<npc_name>", <kickusername>;
  9898. BUILDIN_FUNC(waitingroomkick)
  9899. {
  9900. struct npc_data* nd;
  9901. struct chat_data* cd;
  9902. const char* kickusername;
  9903. nd = npc_name2id(script_getstr(st,2));
  9904. kickusername = script_getstr(st,3);
  9905. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9906. chat_npckickchat(cd, kickusername);
  9907. return SCRIPT_CMD_SUCCESS;
  9908. }
  9909. /// Get Users in waiting room and stores gids in .@waitingroom_users[]
  9910. /// Num users stored in .@waitingroom_usercount
  9911. ///
  9912. /// getwaitingroomusers "<npc_name>";
  9913. BUILDIN_FUNC(getwaitingroomusers)
  9914. {
  9915. struct npc_data* nd;
  9916. struct chat_data* cd;
  9917. int i, j=0;
  9918. if( script_hasdata(st,2) )
  9919. nd = npc_name2id(script_getstr(st, 2));
  9920. else
  9921. nd = (struct npc_data *)map_id2bl(st->oid);
  9922. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL ) {
  9923. for(i = 0; i < cd->users; ++i) {
  9924. setd_sub(st, NULL, ".@waitingroom_users", j, (void *)__64BPRTSIZE(cd->usersd[i]->status.account_id), NULL);
  9925. j++;
  9926. }
  9927. setd_sub(st, NULL, ".@waitingroom_usercount", 0, (void *)__64BPRTSIZE(j), NULL);
  9928. }
  9929. return SCRIPT_CMD_SUCCESS;
  9930. }
  9931. /// Kicks all the players from the waiting room of the current or target npc.
  9932. ///
  9933. /// kickwaitingroomall "<npc_name>";
  9934. /// kickwaitingroomall;
  9935. BUILDIN_FUNC(waitingroomkickall)
  9936. {
  9937. struct npc_data* nd;
  9938. struct chat_data* cd;
  9939. if( script_hasdata(st,2) )
  9940. nd = npc_name2id(script_getstr(st,2));
  9941. else
  9942. nd = (struct npc_data *)map_id2bl(st->oid);
  9943. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9944. chat_npckickall(cd);
  9945. return SCRIPT_CMD_SUCCESS;
  9946. }
  9947. /// Enables the waiting room event of the current or target npc.
  9948. ///
  9949. /// enablewaitingroomevent "<npc_name>";
  9950. /// enablewaitingroomevent;
  9951. BUILDIN_FUNC(enablewaitingroomevent)
  9952. {
  9953. struct npc_data* nd;
  9954. struct chat_data* cd;
  9955. if( script_hasdata(st,2) )
  9956. nd = npc_name2id(script_getstr(st, 2));
  9957. else
  9958. nd = (struct npc_data *)map_id2bl(st->oid);
  9959. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9960. chat_enableevent(cd);
  9961. return SCRIPT_CMD_SUCCESS;
  9962. }
  9963. /// Disables the waiting room event of the current or target npc.
  9964. ///
  9965. /// disablewaitingroomevent "<npc_name>";
  9966. /// disablewaitingroomevent;
  9967. BUILDIN_FUNC(disablewaitingroomevent)
  9968. {
  9969. struct npc_data *nd;
  9970. struct chat_data *cd;
  9971. if( script_hasdata(st,2) )
  9972. nd = npc_name2id(script_getstr(st, 2));
  9973. else
  9974. nd = (struct npc_data *)map_id2bl(st->oid);
  9975. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  9976. chat_disableevent(cd);
  9977. return SCRIPT_CMD_SUCCESS;
  9978. }
  9979. /// Returns info on the waiting room of the current or target npc.
  9980. /// Returns -1 if the type unknown
  9981. /// <type>=0 : current number of users
  9982. /// <type>=1 : maximum number of users allowed
  9983. /// <type>=2 : the number of users that trigger the event
  9984. /// <type>=3 : if the trigger is disabled
  9985. /// <type>=4 : the title of the waiting room
  9986. /// <type>=5 : the password of the waiting room
  9987. /// <type>=16 : the name of the waiting room event
  9988. /// <type>=32 : if the waiting room is full
  9989. /// <type>=33 : if there are enough users to trigger the event
  9990. ///
  9991. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  9992. /// getwaitingroomstate(<type>) -> <info>
  9993. BUILDIN_FUNC(getwaitingroomstate)
  9994. {
  9995. struct npc_data *nd;
  9996. struct chat_data *cd;
  9997. int type;
  9998. type = script_getnum(st,2);
  9999. if( script_hasdata(st,3) )
  10000. nd = npc_name2id(script_getstr(st, 3));
  10001. else
  10002. nd = (struct npc_data *)map_id2bl(st->oid);
  10003. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10004. {
  10005. script_pushint(st, -1);
  10006. return SCRIPT_CMD_SUCCESS;
  10007. }
  10008. switch(type)
  10009. {
  10010. case 0: script_pushint(st, cd->users); break;
  10011. case 1: script_pushint(st, cd->limit); break;
  10012. case 2: script_pushint(st, cd->trigger&0x7f); break;
  10013. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  10014. case 4: script_pushstrcopy(st, cd->title); break;
  10015. case 5: script_pushstrcopy(st, cd->pass); break;
  10016. case 16: script_pushstrcopy(st, cd->npc_event);break;
  10017. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  10018. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  10019. default: script_pushint(st, -1); break;
  10020. }
  10021. return SCRIPT_CMD_SUCCESS;
  10022. }
  10023. /// Warps the trigger or target amount of players to the target map and position.
  10024. /// Players are automatically removed from the waiting room.
  10025. /// Those waiting the longest will get warped first.
  10026. /// The target map can be "Random" for a random position in the current map,
  10027. /// and "SavePoint" for the savepoint map+position.
  10028. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  10029. ///
  10030. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  10031. /// The total number of teleported players is put into $@warpwaitingpcnum
  10032. ///
  10033. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  10034. /// warpwaitingpc "<map name>",<x>,<y>;
  10035. BUILDIN_FUNC(warpwaitingpc)
  10036. {
  10037. int x;
  10038. int y;
  10039. int i;
  10040. int n;
  10041. const char* map_name;
  10042. struct npc_data* nd;
  10043. struct chat_data* cd;
  10044. nd = (struct npc_data *)map_id2bl(st->oid);
  10045. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  10046. return SCRIPT_CMD_SUCCESS;
  10047. map_name = script_getstr(st,2);
  10048. x = script_getnum(st,3);
  10049. y = script_getnum(st,4);
  10050. n = cd->trigger&0x7f;
  10051. if( script_hasdata(st,5) )
  10052. n = script_getnum(st,5);
  10053. for( i = 0; i < n && cd->users > 0; i++ )
  10054. {
  10055. TBL_PC* sd = cd->usersd[0];
  10056. if( strcmp(map_name,"SavePoint") == 0 && map[sd->bl.m].flag.noteleport )
  10057. {// can't teleport on this map
  10058. break;
  10059. }
  10060. if( cd->zeny )
  10061. {// fee set
  10062. if( (uint32)sd->status.zeny < cd->zeny )
  10063. {// no zeny to cover set fee
  10064. break;
  10065. }
  10066. pc_payzeny(sd, cd->zeny, LOG_TYPE_NPC, NULL);
  10067. }
  10068. mapreg_setreg(reference_uid(add_str("$@warpwaitingpc"), i), sd->bl.id);
  10069. if( strcmp(map_name,"Random") == 0 )
  10070. pc_randomwarp(sd,CLR_TELEPORT);
  10071. else if( strcmp(map_name,"SavePoint") == 0 )
  10072. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, CLR_TELEPORT);
  10073. else
  10074. pc_setpos(sd, mapindex_name2id(map_name), x, y, CLR_OUTSIGHT);
  10075. }
  10076. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  10077. return SCRIPT_CMD_SUCCESS;
  10078. }
  10079. /////////////////////////////////////////////////////////////////////
  10080. // ...
  10081. //
  10082. /// Detaches a character from a script.
  10083. ///
  10084. /// @param st Script state to detach the character from.
  10085. static void script_detach_rid(struct script_state* st)
  10086. {
  10087. if(st->rid)
  10088. {
  10089. script_detach_state(st, false);
  10090. st->rid = 0;
  10091. }
  10092. }
  10093. /*=========================================================================
  10094. * Attaches a set of RIDs to the current script. [digitalhamster]
  10095. * addrid(<type>{,<flag>{,<parameters>}});
  10096. * <type>:
  10097. * 0 : All players in the server.
  10098. * 1 : All players in the map of the invoking player, or the invoking NPC if no player is attached.
  10099. * 2 : Party members of a specified party ID.
  10100. * [ Parameters: <party id> ]
  10101. * 3 : Guild members of a specified guild ID.
  10102. * [ Parameters: <guild id> ]
  10103. * 4 : All players in a specified area of the map of the invoking player (or NPC).
  10104. * [ Parameters: <x0>,<y0>,<x1>,<y1> ]
  10105. * Account ID: The specified account ID.
  10106. * <flag>:
  10107. * 0 : Players are always attached. (default)
  10108. * 1 : Players currently running another script will not be attached.
  10109. *-------------------------------------------------------------------------*/
  10110. static int buildin_addrid_sub(struct block_list *bl,va_list ap)
  10111. {
  10112. int forceflag;
  10113. struct map_session_data *sd = (TBL_PC *)bl;
  10114. struct script_state* st;
  10115. st = va_arg(ap,struct script_state*);
  10116. forceflag = va_arg(ap,int);
  10117. if(!forceflag || !sd->st)
  10118. if(sd->status.account_id != st->rid)
  10119. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10120. return 0;
  10121. }
  10122. BUILDIN_FUNC(addrid)
  10123. {
  10124. struct s_mapiterator* iter;
  10125. struct block_list *bl;
  10126. TBL_PC *sd;
  10127. if(st->rid < 1) {
  10128. st->state = END;
  10129. bl = map_id2bl(st->oid);
  10130. } else
  10131. bl = map_id2bl(st->rid); //if run without rid it'd error,also oid if npc, else rid for map
  10132. iter = mapit_getallusers();
  10133. switch(script_getnum(st,2)) {
  10134. case 0:
  10135. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10136. if(!script_getnum(st,3) || !sd->st)
  10137. if(sd->status.account_id != st->rid) //attached player already runs.
  10138. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10139. }
  10140. break;
  10141. case 1:
  10142. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10143. if(!script_getnum(st,3) || !sd->st)
  10144. if((sd->bl.m == bl->m) && (sd->status.account_id != st->rid))
  10145. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10146. }
  10147. break;
  10148. case 2:
  10149. if(script_getnum(st,4) == 0) {
  10150. script_pushint(st,0);
  10151. return SCRIPT_CMD_SUCCESS;
  10152. }
  10153. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10154. if(!script_getnum(st,3) || !sd->st)
  10155. if((sd->status.account_id != st->rid) && (sd->status.party_id == script_getnum(st,4))) //attached player already runs.
  10156. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10157. }
  10158. break;
  10159. case 3:
  10160. if(script_getnum(st,4) == 0) {
  10161. script_pushint(st,0);
  10162. return SCRIPT_CMD_SUCCESS;
  10163. }
  10164. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  10165. if(!script_getnum(st,3) || !sd->st)
  10166. if((sd->status.account_id != st->rid) && (sd->status.guild_id == script_getnum(st,4))) //attached player already runs.
  10167. run_script(st->script,st->pos,sd->status.account_id,st->oid);
  10168. }
  10169. break;
  10170. case 4:
  10171. map_foreachinarea(buildin_addrid_sub,
  10172. bl->m,script_getnum(st,4),script_getnum(st,5),script_getnum(st,6),script_getnum(st,7),BL_PC,
  10173. st,script_getnum(st,3));//4-x0 , 5-y0 , 6-x1, 7-y1
  10174. break;
  10175. default:
  10176. if((map_id2sd(script_getnum(st,2))) == NULL) { // Player not found.
  10177. script_pushint(st,0);
  10178. return SCRIPT_CMD_SUCCESS;
  10179. }
  10180. if(!script_getnum(st,3) || !map_id2sd(script_getnum(st,2))->st) {
  10181. run_script(st->script,st->pos,script_getnum(st,2),st->oid);
  10182. script_pushint(st,1);
  10183. }
  10184. return SCRIPT_CMD_SUCCESS;
  10185. }
  10186. mapit_free(iter);
  10187. script_pushint(st,1);
  10188. return SCRIPT_CMD_SUCCESS;
  10189. }
  10190. /*==========================================
  10191. * Attach sd char id to script and detach current one if any
  10192. *------------------------------------------*/
  10193. BUILDIN_FUNC(attachrid)
  10194. {
  10195. int rid = script_getnum(st,2);
  10196. if (map_id2sd(rid) != NULL) {
  10197. script_detach_rid(st);
  10198. st->rid = rid;
  10199. script_attach_state(st);
  10200. script_pushint(st,1);
  10201. } else
  10202. script_pushint(st,0);
  10203. return SCRIPT_CMD_SUCCESS;
  10204. }
  10205. /*==========================================
  10206. * Detach script to rid
  10207. *------------------------------------------*/
  10208. BUILDIN_FUNC(detachrid)
  10209. {
  10210. script_detach_rid(st);
  10211. return SCRIPT_CMD_SUCCESS;
  10212. }
  10213. /*==========================================
  10214. * Chk if account connected, (and charid from account if specified)
  10215. *------------------------------------------*/
  10216. BUILDIN_FUNC(isloggedin)
  10217. {
  10218. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  10219. if (script_hasdata(st,3) && sd &&
  10220. sd->status.char_id != script_getnum(st,3))
  10221. sd = NULL;
  10222. push_val(st->stack,C_INT,sd!=NULL);
  10223. return SCRIPT_CMD_SUCCESS;
  10224. }
  10225. /*==========================================
  10226. *
  10227. *------------------------------------------*/
  10228. BUILDIN_FUNC(setmapflagnosave)
  10229. {
  10230. int16 m,x,y;
  10231. unsigned short mapindex;
  10232. const char *str,*str2;
  10233. str=script_getstr(st,2);
  10234. str2=script_getstr(st,3);
  10235. x=script_getnum(st,4);
  10236. y=script_getnum(st,5);
  10237. m = map_mapname2mapid(str);
  10238. mapindex = mapindex_name2id(str2);
  10239. if(m >= 0 && mapindex) {
  10240. map[m].flag.nosave=1;
  10241. map[m].save.map=mapindex;
  10242. map[m].save.x=x;
  10243. map[m].save.y=y;
  10244. }
  10245. return SCRIPT_CMD_SUCCESS;
  10246. }
  10247. BUILDIN_FUNC(getmapflag)
  10248. {
  10249. int16 m,i;
  10250. const char *str;
  10251. str=script_getstr(st,2);
  10252. i=script_getnum(st,3);
  10253. m = map_mapname2mapid(str);
  10254. if(m >= 0) {
  10255. switch(i) {
  10256. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  10257. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  10258. case MF_NOSAVE: script_pushint(st,map[m].flag.nosave); break;
  10259. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  10260. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  10261. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  10262. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  10263. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  10264. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  10265. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  10266. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  10267. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  10268. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  10269. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  10270. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  10271. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  10272. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  10273. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  10274. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  10275. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  10276. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  10277. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  10278. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  10279. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  10280. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  10281. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  10282. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  10283. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  10284. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  10285. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  10286. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  10287. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  10288. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  10289. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  10290. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  10291. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  10292. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  10293. case MF_JEXP: script_pushint(st,map[m].adjust.jexp); break;
  10294. case MF_BEXP: script_pushint(st,map[m].adjust.bexp); break;
  10295. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  10296. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  10297. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  10298. case MF_NOEXPPENALTY: script_pushint(st,map[m].flag.noexppenalty ); break;
  10299. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  10300. case MF_TOWN: script_pushint(st,map[m].flag.town); break;
  10301. case MF_AUTOTRADE: script_pushint(st,map[m].flag.autotrade); break;
  10302. case MF_ALLOWKS: script_pushint(st,map[m].flag.allowks); break;
  10303. case MF_MONSTER_NOTELEPORT: script_pushint(st,map[m].flag.monster_noteleport); break;
  10304. case MF_PVP_NOCALCRANK: script_pushint(st,map[m].flag.pvp_nocalcrank); break;
  10305. case MF_BATTLEGROUND: script_pushint(st,map[m].flag.battleground); break;
  10306. case MF_RESET: script_pushint(st,map[m].flag.reset); break;
  10307. case MF_CHANNELAUTOJOIN: script_pushint(st,map[m].flag.chmautojoin); break;
  10308. case MF_NOUSECART: script_pushint(st,map[m].flag.nousecart); break;
  10309. case MF_NOITEMCONSUMPTION: script_pushint(st,map[m].flag.noitemconsumption); break;
  10310. case MF_SUMSTARTMIRACLE: script_pushint(st,map[m].flag.nosumstarmiracle); break;
  10311. case MF_NOMINEEFFECT: script_pushint(st,map[m].flag.nomineeffect); break;
  10312. case MF_NOLOCKON: script_pushint(st,map[m].flag.nolockon); break;
  10313. case MF_NOTOMB: script_pushint(st,map[m].flag.notomb); break;
  10314. case MF_GVG_TE_CASTLE: script_pushint(st,map[m].flag.gvg_te_castle); break;
  10315. case MF_GVG_TE: script_pushint(st,map[m].flag.gvg_te); break;
  10316. #ifdef ADJUST_SKILL_DAMAGE
  10317. case MF_SKILL_DAMAGE:
  10318. {
  10319. int ret_val=0, type=0;
  10320. FETCH(4,type);
  10321. switch (type) {
  10322. case 1: ret_val = map[m].adjust.damage.pc; break;
  10323. case 2: ret_val = map[m].adjust.damage.mob; break;
  10324. case 3: ret_val = map[m].adjust.damage.boss; break;
  10325. case 4: ret_val = map[m].adjust.damage.other; break;
  10326. case 5: ret_val = map[m].adjust.damage.caster; break;
  10327. default: ret_val = map[m].flag.skill_damage; break;
  10328. }
  10329. script_pushint(st,ret_val); break;
  10330. } break;
  10331. #endif
  10332. }
  10333. }
  10334. return SCRIPT_CMD_SUCCESS;
  10335. }
  10336. /* pvp timer handling */
  10337. static int script_mapflag_pvp_sub(struct block_list *bl,va_list ap) {
  10338. TBL_PC* sd = (TBL_PC*)bl;
  10339. if (sd->pvp_timer == INVALID_TIMER) {
  10340. sd->pvp_timer = add_timer(gettick() + 200, pc_calc_pvprank_timer, sd->bl.id, 0);
  10341. sd->pvp_rank = 0;
  10342. sd->pvp_lastusers = 0;
  10343. sd->pvp_point = 5;
  10344. sd->pvp_won = 0;
  10345. sd->pvp_lost = 0;
  10346. }
  10347. clif_map_property(sd, MAPPROPERTY_FREEPVPZONE);
  10348. clif_maptypeproperty2(&sd->bl,SELF);
  10349. return 0;
  10350. }
  10351. BUILDIN_FUNC(setmapflag)
  10352. {
  10353. int16 m,i;
  10354. const char *str;
  10355. int val=0;
  10356. str=script_getstr(st,2);
  10357. i=script_getnum(st,3);
  10358. FETCH(4,val);
  10359. m = map_mapname2mapid(str);
  10360. if(m >= 0) {
  10361. switch(i) {
  10362. case MF_NOMEMO: map[m].flag.nomemo = 1; break;
  10363. case MF_NOTELEPORT: map[m].flag.noteleport = 1; break;
  10364. case MF_NOSAVE: map[m].flag.nosave = 1; break;
  10365. case MF_NOBRANCH: map[m].flag.nobranch = 1; break;
  10366. case MF_NOPENALTY: map[m].flag.noexppenalty = 1; map[m].flag.nozenypenalty = 1; break;
  10367. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 1; break;
  10368. case MF_PVP:
  10369. map[m].flag.pvp = 1;
  10370. if( !battle_config.pk_mode ) {
  10371. map_foreachinmap(script_mapflag_pvp_sub,m,BL_PC);
  10372. }
  10373. break;
  10374. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 1; break;
  10375. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 1; break;
  10376. case MF_GVG: {
  10377. struct block_list bl;
  10378. map[m].flag.gvg = 1;
  10379. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10380. bl.type = BL_NUL;
  10381. bl.m = m;
  10382. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10383. }
  10384. break;
  10385. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 1; break;
  10386. case MF_NOTRADE: map[m].flag.notrade = 1; break;
  10387. case MF_NOSKILL: map[m].flag.noskill = 1; break;
  10388. case MF_NOWARP: map[m].flag.nowarp = 1; break;
  10389. case MF_PARTYLOCK: map[m].flag.partylock = 1; break;
  10390. case MF_NOICEWALL: map[m].flag.noicewall = 1; break;
  10391. case MF_SNOW: map[m].flag.snow = 1; break;
  10392. case MF_FOG: map[m].flag.fog = 1; break;
  10393. case MF_SAKURA: map[m].flag.sakura = 1; break;
  10394. case MF_LEAVES: map[m].flag.leaves = 1; break;
  10395. case MF_NOGO: map[m].flag.nogo = 1; break;
  10396. case MF_CLOUDS: map[m].flag.clouds = 1; break;
  10397. case MF_CLOUDS2: map[m].flag.clouds2 = 1; break;
  10398. case MF_FIREWORKS: map[m].flag.fireworks = 1; break;
  10399. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 1; break;
  10400. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 1; break;
  10401. case MF_NIGHTENABLED: map[m].flag.nightenabled = 1; break;
  10402. case MF_NOBASEEXP: map[m].flag.nobaseexp = 1; break;
  10403. case MF_NOJOBEXP: map[m].flag.nojobexp = 1; break;
  10404. case MF_NOMOBLOOT: map[m].flag.nomobloot = 1; break;
  10405. case MF_NOMVPLOOT: map[m].flag.nomvploot = 1; break;
  10406. case MF_NORETURN: map[m].flag.noreturn = 1; break;
  10407. case MF_NOWARPTO: map[m].flag.nowarpto = 1; break;
  10408. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 1; break;
  10409. case MF_RESTRICTED:
  10410. map[m].zone |= 1<<(val+1);
  10411. map[m].flag.restricted=1;
  10412. break;
  10413. case MF_NOCOMMAND: map[m].nocommand = (val <= 0) ? 100 : val; break;
  10414. case MF_NODROP: map[m].flag.nodrop = 1; break;
  10415. case MF_JEXP: map[m].adjust.jexp = (val <= 0) ? 100 : val; break;
  10416. case MF_BEXP: map[m].adjust.bexp = (val <= 0) ? 100 : val; break;
  10417. case MF_NOVENDING: map[m].flag.novending = 1; break;
  10418. case MF_LOADEVENT: map[m].flag.loadevent = 1; break;
  10419. case MF_NOCHAT: map[m].flag.nochat = 1; break;
  10420. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 1; break;
  10421. case MF_GUILDLOCK: map[m].flag.guildlock = 1; break;
  10422. case MF_TOWN: map[m].flag.town = 1; break;
  10423. case MF_AUTOTRADE: map[m].flag.autotrade = 1; break;
  10424. case MF_ALLOWKS: map[m].flag.allowks = 1; break;
  10425. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 1; break;
  10426. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 1; break;
  10427. case MF_BATTLEGROUND: map[m].flag.battleground = (val <= 0 || val > 2) ? 1 : val; break;
  10428. case MF_RESET: map[m].flag.reset = 1; break;
  10429. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 1 ; break;
  10430. case MF_NOUSECART: map[m].flag.nousecart = 1 ; break;
  10431. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 1 ; break;
  10432. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 1 ; break;
  10433. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 1 ; break;
  10434. case MF_NOLOCKON: map[m].flag.nolockon = 1 ; break;
  10435. case MF_NOTOMB: map[m].flag.notomb = 1; break;
  10436. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 1; break;
  10437. case MF_GVG_TE:
  10438. {
  10439. struct block_list bl;
  10440. map[m].flag.gvg_te = 1;
  10441. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10442. bl.type = BL_NUL;
  10443. bl.m = m;
  10444. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10445. }
  10446. break;
  10447. #ifdef ADJUST_SKILL_DAMAGE
  10448. case MF_SKILL_DAMAGE:
  10449. {
  10450. int type=0;
  10451. FETCH(5,type);
  10452. switch (type) {
  10453. case 1: map[m].adjust.damage.pc = val; break;
  10454. case 2: map[m].adjust.damage.mob = val; break;
  10455. case 3: map[m].adjust.damage.boss = val; break;
  10456. case 4: map[m].adjust.damage.other = val; break;
  10457. case 5: map[m].adjust.damage.caster = val; break;
  10458. }
  10459. map[m].flag.skill_damage = 1;
  10460. } break;
  10461. #endif
  10462. }
  10463. }
  10464. return SCRIPT_CMD_SUCCESS;
  10465. }
  10466. BUILDIN_FUNC(removemapflag)
  10467. {
  10468. int16 m,i;
  10469. const char *str;
  10470. int val=0;
  10471. str=script_getstr(st,2);
  10472. i=script_getnum(st,3);
  10473. FETCH(4,val);
  10474. m = map_mapname2mapid(str);
  10475. if(m >= 0) {
  10476. switch(i) {
  10477. case MF_NOMEMO: map[m].flag.nomemo = 0; break;
  10478. case MF_NOTELEPORT: map[m].flag.noteleport = 0; break;
  10479. case MF_NOSAVE: map[m].flag.nosave = 0; break;
  10480. case MF_NOBRANCH: map[m].flag.nobranch = 0; break;
  10481. case MF_NOPENALTY: map[m].flag.noexppenalty = 0; map[m].flag.nozenypenalty = 0; break;
  10482. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty = 0; break;
  10483. case MF_PVP: {
  10484. struct block_list bl;
  10485. bl.type = BL_NUL;
  10486. bl.m = m;
  10487. map[m].flag.pvp = 0;
  10488. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10489. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10490. }
  10491. break;
  10492. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty = 0; break;
  10493. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild = 0; break;
  10494. case MF_GVG: {
  10495. struct block_list bl;
  10496. bl.type = BL_NUL;
  10497. bl.m = m;
  10498. map[m].flag.gvg = 0;
  10499. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10500. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10501. }
  10502. break;
  10503. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty = 0; break;
  10504. case MF_NOTRADE: map[m].flag.notrade = 0; break;
  10505. case MF_NOSKILL: map[m].flag.noskill = 0; break;
  10506. case MF_NOWARP: map[m].flag.nowarp = 0; break;
  10507. case MF_PARTYLOCK: map[m].flag.partylock = 0; break;
  10508. case MF_NOICEWALL: map[m].flag.noicewall = 0; break;
  10509. case MF_SNOW: map[m].flag.snow = 0; break;
  10510. case MF_FOG: map[m].flag.fog = 0; break;
  10511. case MF_SAKURA: map[m].flag.sakura = 0; break;
  10512. case MF_LEAVES: map[m].flag.leaves = 0; break;
  10513. case MF_NOGO: map[m].flag.nogo = 0; break;
  10514. case MF_CLOUDS: map[m].flag.clouds = 0; break;
  10515. case MF_CLOUDS2: map[m].flag.clouds2 = 0; break;
  10516. case MF_FIREWORKS: map[m].flag.fireworks = 0; break;
  10517. case MF_GVG_CASTLE: map[m].flag.gvg_castle = 0; break;
  10518. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon = 0; break;
  10519. case MF_NIGHTENABLED: map[m].flag.nightenabled = 0; break;
  10520. case MF_NOBASEEXP: map[m].flag.nobaseexp = 0; break;
  10521. case MF_NOJOBEXP: map[m].flag.nojobexp = 0; break;
  10522. case MF_NOMOBLOOT: map[m].flag.nomobloot = 0; break;
  10523. case MF_NOMVPLOOT: map[m].flag.nomvploot = 0; break;
  10524. case MF_NORETURN: map[m].flag.noreturn = 0; break;
  10525. case MF_NOWARPTO: map[m].flag.nowarpto = 0; break;
  10526. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop = 0; break;
  10527. case MF_RESTRICTED:
  10528. map[m].zone ^= 1<<(val+1);
  10529. if (map[m].zone == 0){
  10530. map[m].flag.restricted=0;
  10531. }
  10532. break;
  10533. case MF_NOCOMMAND: map[m].nocommand = 0; break;
  10534. case MF_NODROP: map[m].flag.nodrop = 0; break;
  10535. case MF_JEXP: map[m].adjust.jexp = 0; break;
  10536. case MF_BEXP: map[m].adjust.bexp = 0; break;
  10537. case MF_NOVENDING: map[m].flag.novending = 0; break;
  10538. case MF_LOADEVENT: map[m].flag.loadevent = 0; break;
  10539. case MF_NOCHAT: map[m].flag.nochat = 0; break;
  10540. case MF_NOEXPPENALTY: map[m].flag.noexppenalty = 0; break;
  10541. case MF_GUILDLOCK: map[m].flag.guildlock = 0; break;
  10542. case MF_TOWN: map[m].flag.town = 0; break;
  10543. case MF_AUTOTRADE: map[m].flag.autotrade = 0; break;
  10544. case MF_ALLOWKS: map[m].flag.allowks = 0; break;
  10545. case MF_MONSTER_NOTELEPORT: map[m].flag.monster_noteleport = 0; break;
  10546. case MF_PVP_NOCALCRANK: map[m].flag.pvp_nocalcrank = 0; break;
  10547. case MF_BATTLEGROUND: map[m].flag.battleground = 0; break;
  10548. case MF_RESET: map[m].flag.reset = 0; break;
  10549. case MF_CHANNELAUTOJOIN: map[m].flag.chmautojoin = 0 ; break;
  10550. case MF_NOUSECART: map[m].flag.nousecart = 0 ; break;
  10551. case MF_NOITEMCONSUMPTION: map[m].flag.noitemconsumption = 0 ; break;
  10552. case MF_SUMSTARTMIRACLE: map[m].flag.nosumstarmiracle = 0 ; break;
  10553. case MF_NOMINEEFFECT: map[m].flag.nomineeffect = 0 ; break;
  10554. case MF_NOLOCKON: map[m].flag.nolockon = 0 ; break;
  10555. case MF_NOTOMB: map[m].flag.notomb = 0; break;
  10556. case MF_GVG_TE_CASTLE: map[m].flag.gvg_te_castle = 0; break;
  10557. case MF_GVG_TE:
  10558. {
  10559. struct block_list bl;
  10560. bl.type = BL_NUL;
  10561. bl.m = m;
  10562. map[m].flag.gvg_te = 0;
  10563. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10564. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10565. }
  10566. break;
  10567. #ifdef ADJUST_SKILL_DAMAGE
  10568. case MF_SKILL_DAMAGE:
  10569. {
  10570. map[m].flag.skill_damage = 0;
  10571. memset(&map[m].adjust.damage, 0, sizeof(map[m].adjust.damage));
  10572. if (map[m].skill_damage.count)
  10573. map_skill_damage_free(&map[m]);
  10574. } break;
  10575. #endif
  10576. }
  10577. }
  10578. return SCRIPT_CMD_SUCCESS;
  10579. }
  10580. BUILDIN_FUNC(pvpon)
  10581. {
  10582. int16 m;
  10583. const char *str;
  10584. TBL_PC* sd = NULL;
  10585. struct s_mapiterator* iter;
  10586. struct block_list bl;
  10587. str = script_getstr(st,2);
  10588. m = map_mapname2mapid(str);
  10589. if( m < 0 || map[m].flag.pvp )
  10590. return SCRIPT_CMD_SUCCESS; // nothing to do
  10591. map[m].flag.pvp = 1;
  10592. clif_map_property_mapall(m, MAPPROPERTY_FREEPVPZONE);
  10593. bl.type = BL_NUL;
  10594. bl.m = m;
  10595. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10596. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  10597. return SCRIPT_CMD_SUCCESS;
  10598. iter = mapit_getallusers();
  10599. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10600. {
  10601. if( sd->bl.m != m || sd->pvp_timer != INVALID_TIMER )
  10602. continue; // not applicable
  10603. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  10604. sd->pvp_rank = 0;
  10605. sd->pvp_lastusers = 0;
  10606. sd->pvp_point = 5;
  10607. sd->pvp_won = 0;
  10608. sd->pvp_lost = 0;
  10609. }
  10610. mapit_free(iter);
  10611. return SCRIPT_CMD_SUCCESS;
  10612. }
  10613. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  10614. {
  10615. TBL_PC* sd = (TBL_PC*)bl;
  10616. clif_pvpset(sd, 0, 0, 2);
  10617. if (sd->pvp_timer != INVALID_TIMER) {
  10618. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  10619. sd->pvp_timer = INVALID_TIMER;
  10620. }
  10621. return 0;
  10622. }
  10623. BUILDIN_FUNC(pvpoff)
  10624. {
  10625. int16 m;
  10626. const char *str;
  10627. struct block_list bl;
  10628. str=script_getstr(st,2);
  10629. m = map_mapname2mapid(str);
  10630. if(m < 0 || !map[m].flag.pvp)
  10631. return SCRIPT_CMD_SUCCESS; //fixed Lupus
  10632. map[m].flag.pvp = 0;
  10633. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10634. bl.type = BL_NUL;
  10635. bl.m = m;
  10636. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10637. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  10638. return SCRIPT_CMD_SUCCESS;
  10639. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  10640. return SCRIPT_CMD_SUCCESS;
  10641. }
  10642. BUILDIN_FUNC(gvgon)
  10643. {
  10644. int16 m;
  10645. const char *str;
  10646. struct block_list bl;
  10647. str=script_getstr(st,2);
  10648. m = map_mapname2mapid(str);
  10649. if(m >= 0 && !map[m].flag.gvg) {
  10650. map[m].flag.gvg = 1;
  10651. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10652. bl.type = BL_NUL;
  10653. bl.m = m;
  10654. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10655. }
  10656. return SCRIPT_CMD_SUCCESS;
  10657. }
  10658. BUILDIN_FUNC(gvgoff)
  10659. {
  10660. int16 m;
  10661. const char *str;
  10662. str=script_getstr(st,2);
  10663. m = map_mapname2mapid(str);
  10664. if(m >= 0 && map[m].flag.gvg) {
  10665. struct block_list bl;
  10666. map[m].flag.gvg = 0;
  10667. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10668. bl.type = BL_NUL;
  10669. bl.m = m;
  10670. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10671. }
  10672. return SCRIPT_CMD_SUCCESS;
  10673. }
  10674. BUILDIN_FUNC(gvgon3)
  10675. {
  10676. int16 m;
  10677. const char *str;
  10678. struct block_list bl;
  10679. str = script_getstr(st,2);
  10680. m = map_mapname2mapid(str);
  10681. if (m >= 0 && !map[m].flag.gvg_te) {
  10682. map[m].flag.gvg_te = 1;
  10683. clif_map_property_mapall(m, MAPPROPERTY_AGITZONE);
  10684. bl.type = BL_NUL;
  10685. bl.m = m;
  10686. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10687. }
  10688. return SCRIPT_CMD_SUCCESS;
  10689. }
  10690. BUILDIN_FUNC(gvgoff3)
  10691. {
  10692. int16 m;
  10693. const char *str;
  10694. str = script_getstr(st,2);
  10695. m = map_mapname2mapid(str);
  10696. if (m >= 0 && map[m].flag.gvg_te) {
  10697. struct block_list bl;
  10698. map[m].flag.gvg_te = 0;
  10699. clif_map_property_mapall(m, MAPPROPERTY_NOTHING);
  10700. bl.type = BL_NUL;
  10701. bl.m = m;
  10702. clif_maptypeproperty2(&bl,ALL_SAMEMAP);
  10703. }
  10704. return SCRIPT_CMD_SUCCESS;
  10705. }
  10706. /*==========================================
  10707. * Shows an emoticon on top of the player/npc
  10708. * emotion emotion#, <target: 0 - NPC, 1 - PC>, <NPC/PC name>
  10709. *------------------------------------------*/
  10710. //Optional second parameter added by [Skotlex]
  10711. BUILDIN_FUNC(emotion)
  10712. {
  10713. int type;
  10714. int player=0;
  10715. type=script_getnum(st,2);
  10716. if(type < 0 || type > 100)
  10717. return SCRIPT_CMD_SUCCESS;
  10718. if( script_hasdata(st,3) )
  10719. player=script_getnum(st,3);
  10720. if (player) {
  10721. TBL_PC *sd = NULL;
  10722. if( script_hasdata(st,4) )
  10723. sd = map_nick2sd(script_getstr(st,4));
  10724. else
  10725. sd = script_rid2sd(st);
  10726. if (sd)
  10727. clif_emotion(&sd->bl,type);
  10728. } else
  10729. if( script_hasdata(st,4) )
  10730. {
  10731. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  10732. if(nd)
  10733. clif_emotion(&nd->bl,type);
  10734. }
  10735. else
  10736. clif_emotion(map_id2bl(st->oid),type);
  10737. return SCRIPT_CMD_SUCCESS;
  10738. }
  10739. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  10740. {
  10741. int16 m=va_arg(ap,int);
  10742. int g_id=va_arg(ap,int);
  10743. int flag=va_arg(ap,int);
  10744. if(!sd || sd->bl.m != m)
  10745. return 0;
  10746. if(
  10747. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  10748. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  10749. (sd->status.guild_id == 0 && flag&2) // Warp out players not in guild
  10750. )
  10751. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,CLR_TELEPORT);
  10752. return 1;
  10753. }
  10754. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  10755. {
  10756. struct mob_data *md=(struct mob_data *)bl;
  10757. if(!md->guardian_data && md->mob_id != MOBID_EMPERIUM)
  10758. status_kill(bl);
  10759. return 0;
  10760. }
  10761. /*
  10762. * Function to kick guild members out of a map and to their save points.
  10763. * m : mapid
  10764. * g_id : owner guild id
  10765. * flag & 1 : Warp out owners
  10766. * flag & 2 : Warp out outsiders
  10767. * flag & 4 : reserved for mobs
  10768. * */
  10769. BUILDIN_FUNC(maprespawnguildid)
  10770. {
  10771. const char *mapname=script_getstr(st,2);
  10772. int g_id=script_getnum(st,3);
  10773. int flag=script_getnum(st,4);
  10774. int16 m=map_mapname2mapid(mapname);
  10775. if(m == -1)
  10776. return SCRIPT_CMD_SUCCESS;
  10777. //Catch ALL players (in case some are 'between maps' on execution time)
  10778. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  10779. if (flag&4) //Remove script mobs.
  10780. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  10781. return SCRIPT_CMD_SUCCESS;
  10782. }
  10783. /// Siege commands
  10784. /**
  10785. * Start WoE:FE
  10786. * agitstart();
  10787. */
  10788. BUILDIN_FUNC(agitstart)
  10789. {
  10790. if (agit_flag)
  10791. return SCRIPT_CMD_SUCCESS;// Agit already Started.
  10792. agit_flag = true;
  10793. guild_agit_start();
  10794. return SCRIPT_CMD_SUCCESS;
  10795. }
  10796. /**
  10797. * End WoE:FE
  10798. * agitend();
  10799. */
  10800. BUILDIN_FUNC(agitend)
  10801. {
  10802. if (!agit_flag)
  10803. return SCRIPT_CMD_SUCCESS;// Agit already Ended.
  10804. agit_flag = false;
  10805. guild_agit_end();
  10806. return SCRIPT_CMD_SUCCESS;
  10807. }
  10808. /**
  10809. * Start WoE:SE
  10810. * agitstart2();
  10811. */
  10812. BUILDIN_FUNC(agitstart2)
  10813. {
  10814. if (agit2_flag)
  10815. return SCRIPT_CMD_SUCCESS;// Agit2 already Started.
  10816. agit2_flag = true;
  10817. guild_agit2_start();
  10818. return SCRIPT_CMD_SUCCESS;
  10819. }
  10820. /**
  10821. * End WoE:SE
  10822. * agitend();
  10823. */
  10824. BUILDIN_FUNC(agitend2)
  10825. {
  10826. if (!agit2_flag)
  10827. return SCRIPT_CMD_SUCCESS;// Agit2 already Ended.
  10828. agit2_flag = false;
  10829. guild_agit2_end();
  10830. return SCRIPT_CMD_SUCCESS;
  10831. }
  10832. /**
  10833. * Start WoE:TE
  10834. * agitstart3();
  10835. */
  10836. BUILDIN_FUNC(agitstart3)
  10837. {
  10838. if (agit3_flag)
  10839. return SCRIPT_CMD_SUCCESS;// AgitTE already Started.
  10840. agit3_flag = true;
  10841. guild_agit3_start();
  10842. return SCRIPT_CMD_SUCCESS;
  10843. }
  10844. /**
  10845. * End WoE:TE
  10846. * agitend3();
  10847. */
  10848. BUILDIN_FUNC(agitend3)
  10849. {
  10850. if (!agit3_flag)
  10851. return SCRIPT_CMD_SUCCESS;// AgitTE already Ended.
  10852. agit3_flag = false;
  10853. guild_agit3_end();
  10854. return SCRIPT_CMD_SUCCESS;
  10855. }
  10856. /**
  10857. * Returns whether WoE:FE is on or off.
  10858. * agitcheck();
  10859. */
  10860. BUILDIN_FUNC(agitcheck)
  10861. {
  10862. script_pushint(st, agit_flag);
  10863. return SCRIPT_CMD_SUCCESS;
  10864. }
  10865. /**
  10866. * Returns whether WoE:SE is on or off.
  10867. * agitcheck2();
  10868. */
  10869. BUILDIN_FUNC(agitcheck2)
  10870. {
  10871. script_pushint(st, agit2_flag);
  10872. return SCRIPT_CMD_SUCCESS;
  10873. }
  10874. /**
  10875. * Returns whether WoE:TE is on or off.
  10876. * agitcheck3();
  10877. */
  10878. BUILDIN_FUNC(agitcheck3)
  10879. {
  10880. script_pushint(st, agit3_flag);
  10881. return SCRIPT_CMD_SUCCESS;
  10882. }
  10883. /// Sets the guild_id of this npc.
  10884. ///
  10885. /// flagemblem <guild_id>;
  10886. BUILDIN_FUNC(flagemblem)
  10887. {
  10888. TBL_NPC* nd;
  10889. int g_id = script_getnum(st,2);
  10890. if(g_id < 0)
  10891. return SCRIPT_CMD_SUCCESS;
  10892. nd = (TBL_NPC*)map_id2nd(st->oid);
  10893. if( nd == NULL ) {
  10894. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  10895. } else if( nd->subtype != NPCTYPE_SCRIPT ) {
  10896. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  10897. } else {
  10898. bool changed = ( nd->u.scr.guild_id != g_id )?true:false;
  10899. nd->u.scr.guild_id = g_id;
  10900. clif_guild_emblem_area(&nd->bl);
  10901. /* guild flag caching */
  10902. if( g_id ) /* adding a id */
  10903. guild_flag_add(nd);
  10904. else if( changed ) /* removing a flag */
  10905. guild_flag_remove(nd);
  10906. }
  10907. return SCRIPT_CMD_SUCCESS;
  10908. }
  10909. BUILDIN_FUNC(getcastlename)
  10910. {
  10911. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10912. struct guild_castle* gc = guild_mapname2gc(mapname);
  10913. const char* name = (gc) ? gc->castle_name : "";
  10914. script_pushstrcopy(st,name);
  10915. return SCRIPT_CMD_SUCCESS;
  10916. }
  10917. BUILDIN_FUNC(getcastledata)
  10918. {
  10919. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10920. int index = script_getnum(st,3);
  10921. struct guild_castle *gc = guild_mapname2gc(mapname);
  10922. if (gc == NULL) {
  10923. script_pushint(st,0);
  10924. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10925. return SCRIPT_CMD_FAILURE;
  10926. }
  10927. switch (index) {
  10928. case 1:
  10929. script_pushint(st,gc->guild_id); break;
  10930. case 2:
  10931. script_pushint(st,gc->economy); break;
  10932. case 3:
  10933. script_pushint(st,gc->defense); break;
  10934. case 4:
  10935. script_pushint(st,gc->triggerE); break;
  10936. case 5:
  10937. script_pushint(st,gc->triggerD); break;
  10938. case 6:
  10939. script_pushint(st,gc->nextTime); break;
  10940. case 7:
  10941. script_pushint(st,gc->payTime); break;
  10942. case 8:
  10943. script_pushint(st,gc->createTime); break;
  10944. case 9:
  10945. script_pushint(st,gc->visibleC); break;
  10946. default:
  10947. if (index > 9 && index <= 9+MAX_GUARDIANS) {
  10948. script_pushint(st,gc->guardian[index-10].visible);
  10949. break;
  10950. }
  10951. script_pushint(st,0);
  10952. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10953. return SCRIPT_CMD_FAILURE;
  10954. }
  10955. return SCRIPT_CMD_SUCCESS;
  10956. }
  10957. BUILDIN_FUNC(setcastledata)
  10958. {
  10959. const char *mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  10960. int index = script_getnum(st,3);
  10961. int value = script_getnum(st,4);
  10962. struct guild_castle *gc = guild_mapname2gc(mapname);
  10963. if (gc == NULL) {
  10964. ShowWarning("buildin_setcastledata: guild castle for map '%s' not found\n", mapname);
  10965. return SCRIPT_CMD_FAILURE;
  10966. }
  10967. if (index <= 0 || index > 9+MAX_GUARDIANS) {
  10968. ShowWarning("buildin_setcastledata: index = '%d' is out of allowed range\n", index);
  10969. return SCRIPT_CMD_FAILURE;
  10970. }
  10971. guild_castledatasave(gc->castle_id, index, value);
  10972. return SCRIPT_CMD_SUCCESS;
  10973. }
  10974. /* =====================================================================
  10975. * ---------------------------------------------------------------------*/
  10976. BUILDIN_FUNC(requestguildinfo)
  10977. {
  10978. int guild_id=script_getnum(st,2);
  10979. const char *event=NULL;
  10980. if( script_hasdata(st,3) ){
  10981. event=script_getstr(st,3);
  10982. check_event(st, event);
  10983. }
  10984. if(guild_id>0)
  10985. guild_npc_request_info(guild_id,event);
  10986. return SCRIPT_CMD_SUCCESS;
  10987. }
  10988. /// Returns the number of cards that have been compounded onto the specified equipped item.
  10989. /// getequipcardcnt(<equipment slot>);
  10990. BUILDIN_FUNC(getequipcardcnt)
  10991. {
  10992. int i=-1,j,num;
  10993. TBL_PC *sd;
  10994. int count;
  10995. num=script_getnum(st,2);
  10996. sd=script_rid2sd(st);
  10997. if (num > 0 && num <= ARRAYLENGTH(equip))
  10998. i=pc_checkequip(sd,equip[num-1]);
  10999. if (i < 0 || !sd->inventory_data[i]) {
  11000. script_pushint(st,0);
  11001. return SCRIPT_CMD_SUCCESS;
  11002. }
  11003. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  11004. {
  11005. script_pushint(st,0);
  11006. return SCRIPT_CMD_SUCCESS;
  11007. }
  11008. count = 0;
  11009. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  11010. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  11011. count++;
  11012. script_pushint(st,count);
  11013. return SCRIPT_CMD_SUCCESS;
  11014. }
  11015. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  11016. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  11017. /// successremovecards <slot>;
  11018. BUILDIN_FUNC(successremovecards) {
  11019. int i=-1,c,cardflag=0;
  11020. TBL_PC* sd = script_rid2sd(st);
  11021. int num = script_getnum(st,2);
  11022. if (num > 0 && num <= ARRAYLENGTH(equip))
  11023. i=pc_checkequip(sd,equip[num-1]);
  11024. if (i < 0 || !sd->inventory_data[i]) {
  11025. return SCRIPT_CMD_SUCCESS;
  11026. }
  11027. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  11028. return SCRIPT_CMD_SUCCESS;
  11029. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11030. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {// extract this card from the item
  11031. unsigned char flag = 0;
  11032. struct item item_tmp;
  11033. memset(&item_tmp,0,sizeof(item_tmp));
  11034. cardflag = 1;
  11035. item_tmp.nameid = sd->status.inventory[i].card[c];
  11036. item_tmp.identify = 1;
  11037. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ // get back the cart in inventory
  11038. clif_additem(sd,0,0,flag);
  11039. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11040. }
  11041. }
  11042. }
  11043. if(cardflag == 1) {//if card was remove remplace item with no card
  11044. unsigned char flag = 0, j;
  11045. struct item item_tmp;
  11046. memset(&item_tmp,0,sizeof(item_tmp));
  11047. item_tmp.nameid = sd->status.inventory[i].nameid;
  11048. item_tmp.identify = 1;
  11049. item_tmp.refine = sd->status.inventory[i].refine;
  11050. item_tmp.attribute = sd->status.inventory[i].attribute;
  11051. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  11052. item_tmp.bound = sd->status.inventory[i].bound;
  11053. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11054. item_tmp.card[j]=sd->status.inventory[i].card[j];
  11055. pc_delitem(sd,i,1,0,3,LOG_TYPE_SCRIPT);
  11056. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){ //chk if can be spawn in inventory otherwise put on floor
  11057. clif_additem(sd,0,0,flag);
  11058. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11059. }
  11060. clif_misceffect(&sd->bl,3);
  11061. }
  11062. return SCRIPT_CMD_SUCCESS;
  11063. }
  11064. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  11065. /// failedremovecards <slot>, <type>;
  11066. /// <type>=0 : will destroy both the item and the cards.
  11067. /// <type>=1 : will keep the item, but destroy the cards.
  11068. /// <type>=2 : will keep the cards, but destroy the item.
  11069. /// <type>=? : will just display the failure effect.
  11070. BUILDIN_FUNC(failedremovecards) {
  11071. int i=-1,c,cardflag=0;
  11072. TBL_PC* sd = script_rid2sd(st);
  11073. int num = script_getnum(st,2);
  11074. int typefail = script_getnum(st,3);
  11075. if (num > 0 && num <= ARRAYLENGTH(equip))
  11076. i=pc_checkequip(sd,equip[num-1]);
  11077. if (i < 0 || !sd->inventory_data[i])
  11078. return SCRIPT_CMD_SUCCESS;
  11079. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  11080. return SCRIPT_CMD_SUCCESS;
  11081. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c ) {
  11082. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD ) {
  11083. cardflag = 1;
  11084. if(typefail == 2) {// add cards to inventory, clear
  11085. unsigned char flag = 0;
  11086. struct item item_tmp;
  11087. memset(&item_tmp,0,sizeof(item_tmp));
  11088. item_tmp.nameid = sd->status.inventory[i].card[c];
  11089. item_tmp.identify = 1;
  11090. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11091. clif_additem(sd,0,0,flag);
  11092. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11093. }
  11094. }
  11095. }
  11096. }
  11097. if(cardflag == 1) {
  11098. if(typefail == 0 || typefail == 2){ // destroy the item
  11099. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11100. }
  11101. if(typefail == 1){ // destroy the card
  11102. unsigned char flag = 0, j;
  11103. struct item item_tmp;
  11104. memset(&item_tmp,0,sizeof(item_tmp));
  11105. item_tmp.nameid = sd->status.inventory[i].nameid;
  11106. item_tmp.identify = 1;
  11107. item_tmp.refine = sd->status.inventory[i].refine;
  11108. item_tmp.attribute = sd->status.inventory[i].attribute;
  11109. item_tmp.expire_time = sd->status.inventory[i].expire_time;
  11110. item_tmp.bound = sd->status.inventory[i].bound;
  11111. for (j = sd->inventory_data[i]->slot; j < MAX_SLOTS; j++)
  11112. item_tmp.card[j]=sd->status.inventory[i].card[j];
  11113. pc_delitem(sd,i,1,0,2,LOG_TYPE_SCRIPT);
  11114. if((flag=pc_additem(sd,&item_tmp,1,LOG_TYPE_SCRIPT))){
  11115. clif_additem(sd,0,0,flag);
  11116. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  11117. }
  11118. }
  11119. clif_misceffect(&sd->bl,2);
  11120. }
  11121. return SCRIPT_CMD_SUCCESS;
  11122. }
  11123. /* ================================================================
  11124. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  11125. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  11126. * improved by [Lance]
  11127. * ================================================================*/
  11128. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  11129. {
  11130. int x,y,m,check_val=0,check_ID=0,i=0;
  11131. struct guild *g = NULL;
  11132. struct party_data *p = NULL;
  11133. const char *str;
  11134. const char *mapname;
  11135. unsigned int index;
  11136. mapname=script_getstr(st,2);
  11137. str=script_getstr(st,3);
  11138. x=script_getnum(st,4);
  11139. y=script_getnum(st,5);
  11140. if(script_hasdata(st,7)){
  11141. check_val=script_getnum(st,6);
  11142. check_ID=script_getnum(st,7);
  11143. }
  11144. if((m=map_mapname2mapid(mapname))< 0)
  11145. return SCRIPT_CMD_SUCCESS;
  11146. if(!(index=mapindex_name2id(str)))
  11147. return SCRIPT_CMD_SUCCESS;
  11148. switch(check_val){
  11149. case 1:
  11150. g = guild_search(check_ID);
  11151. if (g){
  11152. for( i=0; i < g->max_member; i++)
  11153. {
  11154. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  11155. pc_setpos(g->member[i].sd,index,x,y,CLR_TELEPORT);
  11156. }
  11157. }
  11158. }
  11159. break;
  11160. case 2:
  11161. p = party_search(check_ID);
  11162. if(p){
  11163. for(i=0;i<MAX_PARTY; i++){
  11164. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  11165. pc_setpos(p->data[i].sd,index,x,y,CLR_TELEPORT);
  11166. }
  11167. }
  11168. }
  11169. break;
  11170. default:
  11171. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y,0,0);
  11172. break;
  11173. }
  11174. return SCRIPT_CMD_SUCCESS;
  11175. }
  11176. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  11177. {
  11178. char *event=va_arg(ap,char *);
  11179. struct mob_data *md = ((struct mob_data *)bl);
  11180. if( md->status.hp > 0 && (!event || strcmp(event,md->npc_event) == 0) )
  11181. return 1;
  11182. return SCRIPT_CMD_SUCCESS;
  11183. }
  11184. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  11185. {
  11186. const char *mapname,*event;
  11187. int16 m;
  11188. mapname=script_getstr(st,2);
  11189. event=script_getstr(st,3);
  11190. if( strcmp(event, "all") == 0 )
  11191. event = NULL;
  11192. else
  11193. check_event(st, event);
  11194. if( strcmp(mapname, "this") == 0 ) {
  11195. struct map_session_data *sd = script_rid2sd(st);
  11196. if( sd )
  11197. m = sd->bl.m;
  11198. else {
  11199. script_pushint(st,-1);
  11200. return SCRIPT_CMD_SUCCESS;
  11201. }
  11202. }
  11203. else if( (m = map_mapname2mapid(mapname)) < 0 ) {
  11204. script_pushint(st,-1);
  11205. return SCRIPT_CMD_SUCCESS;
  11206. }
  11207. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  11208. return SCRIPT_CMD_SUCCESS;
  11209. }
  11210. BUILDIN_FUNC(marriage)
  11211. {
  11212. const char *partner=script_getstr(st,2);
  11213. TBL_PC *sd=script_rid2sd(st);
  11214. TBL_PC *p_sd=map_nick2sd(partner);
  11215. if(!sd || !p_sd || !pc_marriage(sd,p_sd)){
  11216. script_pushint(st,0);
  11217. return SCRIPT_CMD_SUCCESS;
  11218. }
  11219. script_pushint(st,1);
  11220. return SCRIPT_CMD_SUCCESS;
  11221. }
  11222. BUILDIN_FUNC(wedding_effect)
  11223. {
  11224. TBL_PC *sd=script_rid2sd(st);
  11225. struct block_list *bl;
  11226. if(sd==NULL) {
  11227. bl=map_id2bl(st->oid);
  11228. } else
  11229. bl=&sd->bl;
  11230. clif_wedding_effect(bl);
  11231. return SCRIPT_CMD_SUCCESS;
  11232. }
  11233. /**
  11234. * divorce({<char_id>})
  11235. **/
  11236. BUILDIN_FUNC(divorce)
  11237. {
  11238. TBL_PC *sd;
  11239. if (!script_charid2sd(2,sd) || !pc_divorce(sd)) {
  11240. script_pushint(st,0);
  11241. return SCRIPT_CMD_FAILURE;
  11242. }
  11243. script_pushint(st,1);
  11244. return SCRIPT_CMD_SUCCESS;
  11245. }
  11246. /**
  11247. * ispartneron({<char_id>})
  11248. **/
  11249. BUILDIN_FUNC(ispartneron)
  11250. {
  11251. TBL_PC *sd;
  11252. if (!script_charid2sd(2,sd) || !pc_ismarried(sd) ||
  11253. map_charid2sd(sd->status.partner_id) == NULL)
  11254. {
  11255. script_pushint(st,0);
  11256. return SCRIPT_CMD_FAILURE;
  11257. }
  11258. script_pushint(st,1);
  11259. return SCRIPT_CMD_SUCCESS;
  11260. }
  11261. /**
  11262. * getpartnerid({<char_id>})
  11263. **/
  11264. BUILDIN_FUNC(getpartnerid)
  11265. {
  11266. TBL_PC *sd;
  11267. if (!script_charid2sd(2,sd)) {
  11268. script_pushint(st,0);
  11269. return SCRIPT_CMD_FAILURE;
  11270. }
  11271. script_pushint(st,sd->status.partner_id);
  11272. return SCRIPT_CMD_SUCCESS;
  11273. }
  11274. /**
  11275. * getchildid({<char_id>})
  11276. **/
  11277. BUILDIN_FUNC(getchildid)
  11278. {
  11279. TBL_PC *sd;
  11280. if (!script_charid2sd(2,sd)) {
  11281. script_pushint(st,0);
  11282. return SCRIPT_CMD_FAILURE;
  11283. }
  11284. script_pushint(st,sd->status.child);
  11285. return SCRIPT_CMD_SUCCESS;
  11286. }
  11287. /**
  11288. * getmotherid({<char_id>})
  11289. **/
  11290. BUILDIN_FUNC(getmotherid)
  11291. {
  11292. TBL_PC *sd;
  11293. if (!script_charid2sd(2,sd)) {
  11294. script_pushint(st,0);
  11295. return SCRIPT_CMD_FAILURE;
  11296. }
  11297. script_pushint(st,sd->status.mother);
  11298. return SCRIPT_CMD_SUCCESS;
  11299. }
  11300. /**
  11301. * getfatherid({<char_id>})
  11302. **/
  11303. BUILDIN_FUNC(getfatherid)
  11304. {
  11305. TBL_PC *sd;
  11306. if (!script_charid2sd(2,sd)) {
  11307. script_pushint(st,0);
  11308. return SCRIPT_CMD_FAILURE;
  11309. }
  11310. script_pushint(st,sd->status.father);
  11311. return SCRIPT_CMD_SUCCESS;
  11312. }
  11313. BUILDIN_FUNC(warppartner)
  11314. {
  11315. int x,y;
  11316. unsigned short mapindex;
  11317. const char *str;
  11318. TBL_PC *sd=script_rid2sd(st);
  11319. TBL_PC *p_sd=NULL;
  11320. if(sd==NULL || !pc_ismarried(sd) ||
  11321. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  11322. script_pushint(st,0);
  11323. return SCRIPT_CMD_SUCCESS;
  11324. }
  11325. str=script_getstr(st,2);
  11326. x=script_getnum(st,3);
  11327. y=script_getnum(st,4);
  11328. mapindex = mapindex_name2id(str);
  11329. if (mapindex) {
  11330. pc_setpos(p_sd,mapindex,x,y,CLR_OUTSIGHT);
  11331. script_pushint(st,1);
  11332. } else
  11333. script_pushint(st,0);
  11334. return SCRIPT_CMD_SUCCESS;
  11335. }
  11336. /*================================================
  11337. * Script for Displaying MOB Information [Valaris]
  11338. *------------------------------------------------*/
  11339. BUILDIN_FUNC(strmobinfo)
  11340. {
  11341. int num=script_getnum(st,2);
  11342. int class_=script_getnum(st,3);
  11343. if(!mobdb_checkid(class_))
  11344. {
  11345. if (num < 3) //requested a string
  11346. script_pushconststr(st,"");
  11347. else
  11348. script_pushint(st,0);
  11349. return SCRIPT_CMD_SUCCESS;
  11350. }
  11351. switch (num) {
  11352. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  11353. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  11354. case 3: script_pushint(st,mob_db(class_)->lv); break;
  11355. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  11356. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  11357. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  11358. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  11359. default:
  11360. script_pushint(st,0);
  11361. break;
  11362. }
  11363. return SCRIPT_CMD_SUCCESS;
  11364. }
  11365. /*==========================================
  11366. * Summon guardians [Valaris]
  11367. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  11368. *------------------------------------------*/
  11369. BUILDIN_FUNC(guardian)
  11370. {
  11371. int class_=0,x=0,y=0,guardian=0;
  11372. const char *str,*mapname,*evt="";
  11373. bool has_index = false;
  11374. mapname =script_getstr(st,2);
  11375. x =script_getnum(st,3);
  11376. y =script_getnum(st,4);
  11377. str =script_getstr(st,5);
  11378. class_=script_getnum(st,6);
  11379. if( script_hasdata(st,8) )
  11380. {// "<event label>",<guardian index>
  11381. evt=script_getstr(st,7);
  11382. guardian=script_getnum(st,8);
  11383. has_index = true;
  11384. } else if( script_hasdata(st,7) ){
  11385. struct script_data *data = script_getdata(st,7);
  11386. get_val(st,data);
  11387. if( data_isstring(data) )
  11388. {// "<event label>"
  11389. evt=script_getstr(st,7);
  11390. } else if( data_isint(data) )
  11391. {// <guardian index>
  11392. guardian=script_getnum(st,7);
  11393. has_index = true;
  11394. } else {
  11395. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  11396. script_reportdata(data);
  11397. return SCRIPT_CMD_FAILURE;
  11398. }
  11399. }
  11400. check_event(st, evt);
  11401. script_pushint(st, mob_spawn_guardian(mapname,x,y,str,class_,evt,guardian,has_index));
  11402. return SCRIPT_CMD_SUCCESS;
  11403. }
  11404. /*==========================================
  11405. * Invisible Walls [Zephyrus]
  11406. *------------------------------------------*/
  11407. BUILDIN_FUNC(setwall)
  11408. {
  11409. const char *mapname, *name;
  11410. int x, y, m, size, dir;
  11411. bool shootable;
  11412. mapname = script_getstr(st,2);
  11413. x = script_getnum(st,3);
  11414. y = script_getnum(st,4);
  11415. size = script_getnum(st,5);
  11416. dir = script_getnum(st,6);
  11417. shootable = script_getnum(st,7);
  11418. name = script_getstr(st,8);
  11419. if( (m = map_mapname2mapid(mapname)) < 0 )
  11420. return SCRIPT_CMD_SUCCESS; // Invalid Map
  11421. map_iwall_set(m, x, y, size, dir, shootable, name);
  11422. return SCRIPT_CMD_SUCCESS;
  11423. }
  11424. BUILDIN_FUNC(delwall)
  11425. {
  11426. const char *name = script_getstr(st,2);
  11427. map_iwall_remove(name);
  11428. return SCRIPT_CMD_SUCCESS;
  11429. }
  11430. /// Retrieves various information about the specified guardian.
  11431. ///
  11432. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  11433. /// type: 0 - whether it is deployed or not
  11434. /// 1 - maximum hp
  11435. /// 2 - current hp
  11436. ///
  11437. BUILDIN_FUNC(guardianinfo)
  11438. {
  11439. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  11440. int id = script_getnum(st,3);
  11441. int type = script_getnum(st,4);
  11442. struct guild_castle* gc = guild_mapname2gc(mapname);
  11443. struct mob_data* gd;
  11444. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  11445. {
  11446. script_pushint(st,-1);
  11447. return SCRIPT_CMD_SUCCESS;
  11448. }
  11449. if( type == 0 )
  11450. script_pushint(st, gc->guardian[id].visible);
  11451. else
  11452. if( !gc->guardian[id].visible )
  11453. script_pushint(st,-1);
  11454. else
  11455. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  11456. script_pushint(st,-1);
  11457. else
  11458. {
  11459. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  11460. else if( type == 2 ) script_pushint(st,gd->status.hp);
  11461. else
  11462. script_pushint(st,-1);
  11463. }
  11464. return SCRIPT_CMD_SUCCESS;
  11465. }
  11466. /*==========================================
  11467. * Get the item name by item_id or null
  11468. *------------------------------------------*/
  11469. BUILDIN_FUNC(getitemname)
  11470. {
  11471. unsigned short item_id = 0;
  11472. struct item_data *i_data;
  11473. char *item_name;
  11474. struct script_data *data;
  11475. data=script_getdata(st,2);
  11476. get_val(st,data);
  11477. if( data_isstring(data) ){
  11478. const char *name=conv_str(st,data);
  11479. struct item_data *item_data = itemdb_searchname(name);
  11480. if( item_data )
  11481. item_id=item_data->nameid;
  11482. }else
  11483. item_id=conv_num(st,data);
  11484. i_data = itemdb_exists(item_id);
  11485. if (i_data == NULL)
  11486. {
  11487. script_pushconststr(st,"null");
  11488. return SCRIPT_CMD_SUCCESS;
  11489. }
  11490. item_name=(char *)aMalloc(ITEM_NAME_LENGTH*sizeof(char));
  11491. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  11492. script_pushstr(st,item_name);
  11493. return SCRIPT_CMD_SUCCESS;
  11494. }
  11495. /*==========================================
  11496. * Returns number of slots an item has. [Skotlex]
  11497. *------------------------------------------*/
  11498. BUILDIN_FUNC(getitemslots)
  11499. {
  11500. unsigned short item_id;
  11501. struct item_data *i_data;
  11502. item_id=script_getnum(st,2);
  11503. i_data = itemdb_exists(item_id);
  11504. if (i_data)
  11505. script_pushint(st,i_data->slot);
  11506. else
  11507. script_pushint(st,-1);
  11508. return SCRIPT_CMD_SUCCESS;
  11509. }
  11510. // TODO: add matk here if needed/once we get rid of RENEWAL
  11511. /*==========================================
  11512. * Returns some values of an item [Lupus]
  11513. * Price, Weight, etc...
  11514. getiteminfo(itemID,n), where n
  11515. 0 value_buy;
  11516. 1 value_sell;
  11517. 2 type;
  11518. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11519. if = 0, then monsters don't drop it at all (rare or a quest item)
  11520. if = -1, then this item is sold in NPC shops only
  11521. 4 sex;
  11522. 5 equip;
  11523. 6 weight;
  11524. 7 atk;
  11525. 8 def;
  11526. 9 range;
  11527. 10 slot;
  11528. 11 look;
  11529. 12 elv;
  11530. 13 wlv;
  11531. 14 view id
  11532. 15 eLvmax
  11533. 16 matk (renewal)
  11534. *------------------------------------------*/
  11535. BUILDIN_FUNC(getiteminfo)
  11536. {
  11537. unsigned short item_id,n;
  11538. struct item_data *i_data;
  11539. item_id = script_getnum(st,2);
  11540. n = script_getnum(st,3);
  11541. i_data = itemdb_exists(item_id);
  11542. if (i_data && n <= 16) {
  11543. int *item_arr = (int*)&i_data->value_buy;
  11544. #ifndef RENEWAL
  11545. if (n == 16)
  11546. script_pushint(st,0);
  11547. else
  11548. #endif
  11549. script_pushint(st,item_arr[n]);
  11550. } else
  11551. script_pushint(st,-1);
  11552. return SCRIPT_CMD_SUCCESS;
  11553. }
  11554. /*==========================================
  11555. * Set some values of an item [Lupus]
  11556. * Price, Weight, etc...
  11557. setiteminfo(itemID,n,Value), where n
  11558. 0 value_buy;
  11559. 1 value_sell;
  11560. 2 type;
  11561. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  11562. if = 0, then monsters don't drop it at all (rare or a quest item)
  11563. if = -1, then this item is sold in NPC shops only
  11564. 4 sex;
  11565. 5 equip;
  11566. 6 weight;
  11567. 7 atk;
  11568. 8 def;
  11569. 9 range;
  11570. 10 slot;
  11571. 11 look;
  11572. 12 elv;
  11573. 13 wlv;
  11574. 14 view id
  11575. * Returns Value or -1 if the wrong field's been set
  11576. *------------------------------------------*/
  11577. BUILDIN_FUNC(setiteminfo)
  11578. {
  11579. unsigned short item_id;
  11580. int n,value;
  11581. struct item_data *i_data;
  11582. item_id = script_getnum(st,2);
  11583. n = script_getnum(st,3);
  11584. value = script_getnum(st,4);
  11585. i_data = itemdb_exists(item_id);
  11586. if (i_data && n>=0 && n<=14) {
  11587. int *item_arr = (int*)&i_data->value_buy;
  11588. item_arr[n] = value;
  11589. script_pushint(st,value);
  11590. } else
  11591. script_pushint(st,-1);
  11592. return SCRIPT_CMD_SUCCESS;
  11593. }
  11594. /*==========================================
  11595. * Returns value from equipped item slot n [Lupus]
  11596. getequipcardid(num,slot)
  11597. where
  11598. num = eqip position slot
  11599. slot = 0,1,2,3 (Card Slot N)
  11600. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  11601. it's useful when you want to check item cards or if it's signed
  11602. Useful for such quests as "Sign this refined item with players name" etc
  11603. Hat[0] +4 -> Player's Hat[0] +4
  11604. *------------------------------------------*/
  11605. BUILDIN_FUNC(getequipcardid)
  11606. {
  11607. int i=-1,num,slot;
  11608. TBL_PC *sd;
  11609. num=script_getnum(st,2);
  11610. slot=script_getnum(st,3);
  11611. sd=script_rid2sd(st);
  11612. if (num > 0 && num <= ARRAYLENGTH(equip))
  11613. i=pc_checkequip(sd,equip[num-1]);
  11614. if(i >= 0 && slot>=0 && slot<4)
  11615. script_pushint(st,sd->status.inventory[i].card[slot]);
  11616. else
  11617. script_pushint(st,0);
  11618. return SCRIPT_CMD_SUCCESS;
  11619. }
  11620. /*==========================================
  11621. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  11622. *------------------------------------------*/
  11623. BUILDIN_FUNC(petskillbonus)
  11624. {
  11625. struct pet_data *pd;
  11626. TBL_PC *sd = script_rid2sd(st);
  11627. if(sd == NULL || sd->pd == NULL)
  11628. return SCRIPT_CMD_FAILURE;
  11629. pd = sd->pd;
  11630. if (pd->bonus)
  11631. { //Clear previous bonus
  11632. if (pd->bonus->timer != INVALID_TIMER)
  11633. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  11634. } else //init
  11635. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  11636. pd->bonus->type = script_getnum(st,2);
  11637. pd->bonus->val = script_getnum(st,3);
  11638. pd->bonus->duration = script_getnum(st,4);
  11639. pd->bonus->delay = script_getnum(st,5);
  11640. if (pd->state.skillbonus == 1)
  11641. pd->state.skillbonus = 0; // waiting state
  11642. // wait for timer to start
  11643. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11644. pd->bonus->timer = INVALID_TIMER;
  11645. else
  11646. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  11647. return SCRIPT_CMD_SUCCESS;
  11648. }
  11649. /*==========================================
  11650. * pet looting [Valaris] //Rewritten by [Skotlex]
  11651. *------------------------------------------*/
  11652. BUILDIN_FUNC(petloot)
  11653. {
  11654. int max;
  11655. struct pet_data *pd;
  11656. TBL_PC *sd=script_rid2sd(st);
  11657. if(sd==NULL || sd->pd==NULL)
  11658. return SCRIPT_CMD_SUCCESS;
  11659. max=script_getnum(st,2);
  11660. if(max < 1)
  11661. max = 1; //Let'em loot at least 1 item.
  11662. else if (max > MAX_PETLOOT_SIZE)
  11663. max = MAX_PETLOOT_SIZE;
  11664. pd = sd->pd;
  11665. if (pd->loot != NULL)
  11666. { //Release whatever was there already and reallocate memory
  11667. pet_lootitem_drop(pd, pd->master);
  11668. aFree(pd->loot->item);
  11669. }
  11670. else
  11671. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  11672. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  11673. pd->loot->max=max;
  11674. pd->loot->count = 0;
  11675. pd->loot->weight = 0;
  11676. return SCRIPT_CMD_SUCCESS;
  11677. }
  11678. /*==========================================
  11679. * Set arrays with info of all sd inventory :
  11680. * @inventorylist_id, @inventorylist_amount, @inventorylist_equip,
  11681. * @inventorylist_refine, @inventorylist_identify, @inventorylist_attribute,
  11682. * @inventorylist_card(0..3), @inventorylist_expire
  11683. * @inventorylist_count = scalar
  11684. *------------------------------------------*/
  11685. BUILDIN_FUNC(getinventorylist)
  11686. {
  11687. TBL_PC *sd;
  11688. char card_var[NAME_LENGTH];
  11689. int i,j=0,k;
  11690. if (!script_charid2sd(2,sd))
  11691. return SCRIPT_CMD_FAILURE;
  11692. for(i=0;i<MAX_INVENTORY;i++){
  11693. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  11694. pc_setreg(sd,reference_uid(add_str("@inventorylist_id"), j),sd->status.inventory[i].nameid);
  11695. pc_setreg(sd,reference_uid(add_str("@inventorylist_amount"), j),sd->status.inventory[i].amount);
  11696. pc_setreg(sd,reference_uid(add_str("@inventorylist_equip"), j),sd->status.inventory[i].equip);
  11697. pc_setreg(sd,reference_uid(add_str("@inventorylist_refine"), j),sd->status.inventory[i].refine);
  11698. pc_setreg(sd,reference_uid(add_str("@inventorylist_identify"), j),sd->status.inventory[i].identify);
  11699. pc_setreg(sd,reference_uid(add_str("@inventorylist_attribute"), j),sd->status.inventory[i].attribute);
  11700. for (k = 0; k < MAX_SLOTS; k++)
  11701. {
  11702. sprintf(card_var, "@inventorylist_card%d",k+1);
  11703. pc_setreg(sd,reference_uid(add_str(card_var), j),sd->status.inventory[i].card[k]);
  11704. }
  11705. pc_setreg(sd,reference_uid(add_str("@inventorylist_expire"), j),sd->status.inventory[i].expire_time);
  11706. pc_setreg(sd,reference_uid(add_str("@inventorylist_bound"), j),sd->status.inventory[i].bound);
  11707. j++;
  11708. }
  11709. }
  11710. pc_setreg(sd,add_str("@inventorylist_count"),j);
  11711. return SCRIPT_CMD_SUCCESS;
  11712. }
  11713. /**
  11714. * getskilllist ({<char_id>});
  11715. **/
  11716. BUILDIN_FUNC(getskilllist)
  11717. {
  11718. TBL_PC *sd;
  11719. int i,j=0;
  11720. if (!script_charid2sd(2,sd))
  11721. return SCRIPT_CMD_FAILURE;
  11722. for(i=0;i<MAX_SKILL;i++){
  11723. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  11724. pc_setreg(sd,reference_uid(add_str("@skilllist_id"), j),sd->status.skill[i].id);
  11725. pc_setreg(sd,reference_uid(add_str("@skilllist_lv"), j),sd->status.skill[i].lv);
  11726. pc_setreg(sd,reference_uid(add_str("@skilllist_flag"), j),sd->status.skill[i].flag);
  11727. j++;
  11728. }
  11729. }
  11730. pc_setreg(sd,add_str("@skilllist_count"),j);
  11731. return SCRIPT_CMD_SUCCESS;
  11732. }
  11733. /**
  11734. * clearitem ({<char_id>});
  11735. **/
  11736. BUILDIN_FUNC(clearitem)
  11737. {
  11738. TBL_PC *sd;
  11739. int i;
  11740. if (!script_charid2sd(2,sd))
  11741. return SCRIPT_CMD_FAILURE;
  11742. for (i=0; i<MAX_INVENTORY; i++) {
  11743. if (sd->status.inventory[i].amount) {
  11744. pc_delitem(sd, i, sd->status.inventory[i].amount, 0, 0, LOG_TYPE_SCRIPT);
  11745. }
  11746. }
  11747. return SCRIPT_CMD_SUCCESS;
  11748. }
  11749. /**
  11750. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  11751. * disguise <Monster ID>{,<char_id>};
  11752. **/
  11753. BUILDIN_FUNC(disguise)
  11754. {
  11755. int id;
  11756. TBL_PC* sd;
  11757. if (!script_charid2sd(3,sd))
  11758. return SCRIPT_CMD_FAILURE;
  11759. id = script_getnum(st,2);
  11760. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  11761. pc_disguise(sd, id);
  11762. script_pushint(st,id);
  11763. } else
  11764. script_pushint(st,0);
  11765. return SCRIPT_CMD_SUCCESS;
  11766. }
  11767. /**
  11768. * Undisguise Player (returns 1 if success, 0 on fail)
  11769. * undisguise {<char_id>};
  11770. **/
  11771. BUILDIN_FUNC(undisguise)
  11772. {
  11773. TBL_PC* sd;
  11774. if (!script_charid2sd(2,sd))
  11775. return SCRIPT_CMD_FAILURE;
  11776. if (sd->disguise) {
  11777. pc_disguise(sd, 0);
  11778. script_pushint(st,0);
  11779. } else {
  11780. script_pushint(st,1);
  11781. }
  11782. return SCRIPT_CMD_SUCCESS;
  11783. }
  11784. /*==========================================
  11785. * Transform a bl to another _class,
  11786. * @type unused
  11787. *------------------------------------------*/
  11788. BUILDIN_FUNC(classchange)
  11789. {
  11790. int _class,type;
  11791. struct block_list *bl=map_id2bl(st->oid);
  11792. if (bl==NULL)
  11793. return SCRIPT_CMD_FAILURE;
  11794. _class=script_getnum(st,2);
  11795. type=script_getnum(st,3);
  11796. clif_class_change(bl,_class,type);
  11797. return SCRIPT_CMD_SUCCESS;
  11798. }
  11799. /*==========================================
  11800. * Display an effect
  11801. *------------------------------------------*/
  11802. BUILDIN_FUNC(misceffect)
  11803. {
  11804. int type;
  11805. type=script_getnum(st,2);
  11806. if(st->oid && st->oid != fake_nd->bl.id) {
  11807. struct block_list *bl = map_id2bl(st->oid);
  11808. if (bl)
  11809. clif_specialeffect(bl,type,AREA);
  11810. } else{
  11811. TBL_PC *sd=script_rid2sd(st);
  11812. if(sd)
  11813. clif_specialeffect(&sd->bl,type,AREA);
  11814. }
  11815. return SCRIPT_CMD_SUCCESS;
  11816. }
  11817. /*==========================================
  11818. * Play a BGM on a single client [Rikter/Yommy]
  11819. *------------------------------------------*/
  11820. BUILDIN_FUNC(playBGM)
  11821. {
  11822. struct map_session_data* sd;
  11823. if( ( sd = script_rid2sd(st) ) != NULL ) {
  11824. clif_playBGM(sd, script_getstr(st,2));
  11825. }
  11826. return SCRIPT_CMD_SUCCESS;
  11827. }
  11828. static int playBGM_sub(struct block_list* bl,va_list ap)
  11829. {
  11830. const char* name = va_arg(ap,const char*);
  11831. clif_playBGM(BL_CAST(BL_PC, bl), name);
  11832. return 0;
  11833. }
  11834. static int playBGM_foreachpc_sub(struct map_session_data* sd, va_list args)
  11835. {
  11836. const char* name = va_arg(args, const char*);
  11837. clif_playBGM(sd, name);
  11838. return 0;
  11839. }
  11840. /*==========================================
  11841. * Play a BGM on multiple client [Rikter/Yommy]
  11842. *------------------------------------------*/
  11843. BUILDIN_FUNC(playBGMall)
  11844. {
  11845. const char* name;
  11846. name = script_getstr(st,2);
  11847. if( script_hasdata(st,7) ) {// specified part of map
  11848. const char* mapname = script_getstr(st,3);
  11849. int x0 = script_getnum(st,4);
  11850. int y0 = script_getnum(st,5);
  11851. int x1 = script_getnum(st,6);
  11852. int y1 = script_getnum(st,7);
  11853. map_foreachinarea(playBGM_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name);
  11854. }
  11855. else if( script_hasdata(st,3) ) {// entire map
  11856. const char* mapname = script_getstr(st,3);
  11857. map_foreachinmap(playBGM_sub, map_mapname2mapid(mapname), BL_PC, name);
  11858. }
  11859. else {// entire server
  11860. map_foreachpc(&playBGM_foreachpc_sub, name);
  11861. }
  11862. return SCRIPT_CMD_SUCCESS;
  11863. }
  11864. /*==========================================
  11865. * Play a .wav sound for sd
  11866. *------------------------------------------*/
  11867. BUILDIN_FUNC(soundeffect)
  11868. {
  11869. TBL_PC* sd = script_rid2sd(st);
  11870. const char* name = script_getstr(st,2);
  11871. int type = script_getnum(st,3);
  11872. if(sd) {
  11873. clif_soundeffect(sd,&sd->bl,name,type);
  11874. }
  11875. return SCRIPT_CMD_SUCCESS;
  11876. }
  11877. int soundeffect_sub(struct block_list* bl,va_list ap)
  11878. {
  11879. char* name = va_arg(ap,char*);
  11880. int type = va_arg(ap,int);
  11881. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  11882. return 0;
  11883. }
  11884. /*==========================================
  11885. * Play a sound effect (.wav) on multiple clients
  11886. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  11887. *------------------------------------------*/
  11888. BUILDIN_FUNC(soundeffectall)
  11889. {
  11890. struct block_list* bl;
  11891. const char* name;
  11892. int type;
  11893. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  11894. if (!bl)
  11895. return SCRIPT_CMD_SUCCESS;
  11896. name = script_getstr(st,2);
  11897. type = script_getnum(st,3);
  11898. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  11899. if(!script_hasdata(st,4))
  11900. { // area around
  11901. clif_soundeffectall(bl, name, type, AREA);
  11902. }
  11903. else
  11904. if(!script_hasdata(st,5))
  11905. { // entire map
  11906. const char* mapname = script_getstr(st,4);
  11907. map_foreachinmap(soundeffect_sub, map_mapname2mapid(mapname), BL_PC, name, type);
  11908. }
  11909. else
  11910. if(script_hasdata(st,8))
  11911. { // specified part of map
  11912. const char* mapname = script_getstr(st,4);
  11913. int x0 = script_getnum(st,5);
  11914. int y0 = script_getnum(st,6);
  11915. int x1 = script_getnum(st,7);
  11916. int y1 = script_getnum(st,8);
  11917. map_foreachinarea(soundeffect_sub, map_mapname2mapid(mapname), x0, y0, x1, y1, BL_PC, name, type);
  11918. }
  11919. else
  11920. {
  11921. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  11922. }
  11923. return SCRIPT_CMD_SUCCESS;
  11924. }
  11925. /*==========================================
  11926. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  11927. *------------------------------------------*/
  11928. BUILDIN_FUNC(petrecovery)
  11929. {
  11930. struct pet_data *pd;
  11931. TBL_PC *sd=script_rid2sd(st);
  11932. int sc;
  11933. if(sd == NULL || sd->pd == NULL)
  11934. return SCRIPT_CMD_FAILURE;
  11935. sc = script_getnum(st,2);
  11936. if (sc <= SC_NONE || sc >= SC_MAX) {
  11937. ShowError("buildin_petrecovery: Invalid SC type: %d\n", sc);
  11938. return SCRIPT_CMD_FAILURE;
  11939. }
  11940. pd = sd->pd;
  11941. if (pd->recovery)
  11942. { //Halt previous bonus
  11943. if (pd->recovery->timer != INVALID_TIMER)
  11944. delete_timer(pd->recovery->timer, pet_recovery_timer);
  11945. } else //Init
  11946. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  11947. pd->recovery->type = (sc_type)sc;
  11948. pd->recovery->delay = script_getnum(st,3);
  11949. pd->recovery->timer = INVALID_TIMER;
  11950. return SCRIPT_CMD_SUCCESS;
  11951. }
  11952. /*==========================================
  11953. * pet healing [Valaris] //Rewritten by [Skotlex]
  11954. *------------------------------------------*/
  11955. BUILDIN_FUNC(petheal)
  11956. {
  11957. struct pet_data *pd;
  11958. TBL_PC *sd=script_rid2sd(st);
  11959. if(sd==NULL || sd->pd==NULL)
  11960. return SCRIPT_CMD_SUCCESS;
  11961. pd=sd->pd;
  11962. if (pd->s_skill)
  11963. { //Clear previous skill
  11964. if (pd->s_skill->timer != INVALID_TIMER)
  11965. {
  11966. if (pd->s_skill->id)
  11967. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  11968. else
  11969. delete_timer(pd->s_skill->timer, pet_heal_timer);
  11970. }
  11971. } else //init memory
  11972. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  11973. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  11974. //Use the lv as the amount to heal
  11975. pd->s_skill->lv=script_getnum(st,2);
  11976. pd->s_skill->delay=script_getnum(st,3);
  11977. pd->s_skill->hp=script_getnum(st,4);
  11978. pd->s_skill->sp=script_getnum(st,5);
  11979. //Use delay as initial offset to avoid skill/heal exploits
  11980. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  11981. pd->s_skill->timer = INVALID_TIMER;
  11982. else
  11983. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  11984. return SCRIPT_CMD_SUCCESS;
  11985. }
  11986. /*==========================================
  11987. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  11988. *------------------------------------------*/
  11989. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  11990. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  11991. BUILDIN_FUNC(petskillattack)
  11992. {
  11993. struct pet_data *pd;
  11994. struct script_data *data;
  11995. TBL_PC *sd = script_rid2sd(st);
  11996. int id = 0;
  11997. if(sd == NULL || sd->pd == NULL)
  11998. return SCRIPT_CMD_FAILURE;
  11999. data = script_getdata(st, 2);
  12000. get_val(st, data);
  12001. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12002. if (!id) {
  12003. ShowError("buildin_petskillattack: Invalid skill defined!\n");
  12004. return SCRIPT_CMD_FAILURE;
  12005. }
  12006. pd = sd->pd;
  12007. if (pd->a_skill == NULL)
  12008. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12009. pd->a_skill->id = id;
  12010. pd->a_skill->damage = 0;
  12011. pd->a_skill->lv = (unsigned short)min(script_getnum(st,3), skill_get_max(pd->a_skill->id));
  12012. pd->a_skill->div_ = 0;
  12013. pd->a_skill->rate = script_getnum(st,4);
  12014. pd->a_skill->bonusrate = script_getnum(st,5);
  12015. return SCRIPT_CMD_SUCCESS;
  12016. }
  12017. /*==========================================
  12018. * pet attack skills [Valaris]
  12019. *------------------------------------------*/
  12020. /// petskillattack2 <skill id>,<damage>,<div>,<rate>,<bonusrate>
  12021. /// petskillattack2 "<skill name>",<damage>,<div>,<rate>,<bonusrate>
  12022. BUILDIN_FUNC(petskillattack2)
  12023. {
  12024. struct pet_data *pd;
  12025. struct script_data *data;
  12026. TBL_PC *sd = script_rid2sd(st);
  12027. int id = 0;
  12028. if(sd == NULL || sd->pd == NULL)
  12029. return SCRIPT_CMD_FAILURE;
  12030. data = script_getdata(st, 2);
  12031. get_val(st, data);
  12032. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12033. if (!id) {
  12034. ShowError("buildin_petskillattack2: Invalid skill defined!\n");
  12035. return SCRIPT_CMD_FAILURE;
  12036. }
  12037. pd = sd->pd;
  12038. if (pd->a_skill == NULL)
  12039. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  12040. pd->a_skill->id = id;
  12041. pd->a_skill->damage = script_getnum(st,3); // Fixed damage
  12042. pd->a_skill->lv = (unsigned short)skill_get_max(pd->a_skill->id); // Adjust to max skill level
  12043. pd->a_skill->div_ = script_getnum(st,4);
  12044. pd->a_skill->rate = script_getnum(st,5);
  12045. pd->a_skill->bonusrate = script_getnum(st,6);
  12046. return SCRIPT_CMD_SUCCESS;
  12047. }
  12048. /*==========================================
  12049. * pet support skills [Skotlex]
  12050. *------------------------------------------*/
  12051. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  12052. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  12053. BUILDIN_FUNC(petskillsupport)
  12054. {
  12055. struct pet_data *pd;
  12056. struct script_data *data;
  12057. TBL_PC *sd = script_rid2sd(st);
  12058. int id = 0;
  12059. if(sd == NULL || sd->pd == NULL)
  12060. return SCRIPT_CMD_FAILURE;
  12061. data = script_getdata(st, 2);
  12062. get_val(st, data);
  12063. id = (data_isstring(data) ? skill_name2id(script_getstr(st,2)) : skill_get_index(script_getnum(st,2)));
  12064. if (!id) {
  12065. ShowError("buildin_petskillsupport: Invalid skill defined!\n");
  12066. return SCRIPT_CMD_FAILURE;
  12067. }
  12068. pd = sd->pd;
  12069. if (pd->s_skill)
  12070. { //Clear previous skill
  12071. if (pd->s_skill->timer != INVALID_TIMER)
  12072. {
  12073. if (pd->s_skill->id)
  12074. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  12075. else
  12076. delete_timer(pd->s_skill->timer, pet_heal_timer);
  12077. }
  12078. } else //init memory
  12079. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  12080. pd->s_skill->id = id;
  12081. pd->s_skill->lv = script_getnum(st,3);
  12082. pd->s_skill->delay = script_getnum(st,4);
  12083. pd->s_skill->hp = script_getnum(st,5);
  12084. pd->s_skill->sp = script_getnum(st,6);
  12085. //Use delay as initial offset to avoid skill/heal exploits
  12086. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  12087. pd->s_skill->timer = INVALID_TIMER;
  12088. else
  12089. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  12090. return SCRIPT_CMD_SUCCESS;
  12091. }
  12092. /*==========================================
  12093. * Scripted skill effects [Celest]
  12094. *------------------------------------------*/
  12095. /// skilleffect <skill id>,<level>
  12096. /// skilleffect "<skill name>",<level>
  12097. BUILDIN_FUNC(skilleffect)
  12098. {
  12099. TBL_PC *sd;
  12100. uint16 skill_id, skill_lv;
  12101. struct script_data *data = script_getdata(st, 2);
  12102. get_val(st, data); // Convert into value in case of a variable
  12103. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12104. skill_lv = script_getnum(st,3);
  12105. sd = script_rid2sd(st);
  12106. /* Ensure we're standing because the following packet causes the client to virtually set the char to stand,
  12107. * which leaves the server thinking it still is sitting. */
  12108. if( pc_issit(sd) && pc_setstand(sd, false) ) {
  12109. skill_sit(sd, 0);
  12110. clif_standing(&sd->bl);
  12111. }
  12112. clif_skill_nodamage(&sd->bl,&sd->bl,skill_id,skill_lv,1);
  12113. return SCRIPT_CMD_SUCCESS;
  12114. }
  12115. /*==========================================
  12116. * NPC skill effects [Valaris]
  12117. *------------------------------------------*/
  12118. /// npcskilleffect <skill id>,<level>,<x>,<y>
  12119. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  12120. BUILDIN_FUNC(npcskilleffect)
  12121. {
  12122. struct block_list *bl= map_id2bl(st->oid);
  12123. uint16 skill_id, skill_lv;
  12124. int x, y;
  12125. struct script_data *data = script_getdata(st, 2);
  12126. get_val(st, data); // Convert into value in case of a variable
  12127. skill_id=( data_isstring(data) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  12128. skill_lv=script_getnum(st,3);
  12129. x=script_getnum(st,4);
  12130. y=script_getnum(st,5);
  12131. if (bl)
  12132. clif_skill_poseffect(bl,skill_id,skill_lv,x,y,gettick());
  12133. return SCRIPT_CMD_SUCCESS;
  12134. }
  12135. /*==========================================
  12136. * Special effects [Valaris]
  12137. *------------------------------------------*/
  12138. BUILDIN_FUNC(specialeffect)
  12139. {
  12140. struct block_list *bl=map_id2bl(st->oid);
  12141. int type = script_getnum(st,2);
  12142. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12143. if(bl==NULL)
  12144. return SCRIPT_CMD_SUCCESS;
  12145. if( script_hasdata(st,4) )
  12146. {
  12147. TBL_NPC *nd = npc_name2id(script_getstr(st,4));
  12148. if(nd)
  12149. clif_specialeffect(&nd->bl, type, target);
  12150. }
  12151. else
  12152. {
  12153. if (target == SELF) {
  12154. TBL_PC *sd=script_rid2sd(st);
  12155. if (sd)
  12156. clif_specialeffect_single(bl,type,sd->fd);
  12157. } else {
  12158. clif_specialeffect(bl, type, target);
  12159. }
  12160. }
  12161. return SCRIPT_CMD_SUCCESS;
  12162. }
  12163. BUILDIN_FUNC(specialeffect2)
  12164. {
  12165. TBL_PC *sd=script_rid2sd(st);
  12166. int type = script_getnum(st,2);
  12167. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  12168. if( script_hasdata(st,4) )
  12169. sd = map_nick2sd(script_getstr(st,4));
  12170. if (sd)
  12171. clif_specialeffect(&sd->bl, type, target);
  12172. return SCRIPT_CMD_SUCCESS;
  12173. }
  12174. /**
  12175. * nude({<char_id>});
  12176. * @author [Valaris]
  12177. **/
  12178. BUILDIN_FUNC(nude)
  12179. {
  12180. TBL_PC *sd;
  12181. int i, calcflag = 0;
  12182. if (!script_charid2sd(2,sd))
  12183. return SCRIPT_CMD_FAILURE;
  12184. for( i = 0 ; i < EQI_MAX; i++ ) {
  12185. if( sd->equip_index[ i ] >= 0 ) {
  12186. if( !calcflag )
  12187. calcflag = 1;
  12188. pc_unequipitem( sd , sd->equip_index[ i ] , 2);
  12189. }
  12190. }
  12191. if( calcflag )
  12192. status_calc_pc(sd,SCO_NONE);
  12193. return SCRIPT_CMD_SUCCESS;
  12194. }
  12195. int atcommand_sub(struct script_state* st,int type) {
  12196. TBL_PC *sd, dummy_sd;
  12197. int fd;
  12198. const char *cmd;
  12199. cmd = script_getstr(st,2);
  12200. if (st->rid) {
  12201. sd = script_rid2sd(st);
  12202. fd = sd->fd;
  12203. } else { //Use a dummy character.
  12204. sd = &dummy_sd;
  12205. fd = 0;
  12206. memset(&dummy_sd, 0, sizeof(TBL_PC));
  12207. if (st->oid) {
  12208. struct block_list* bl = map_id2bl(st->oid);
  12209. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  12210. if (bl->type == BL_NPC)
  12211. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  12212. sd->mapindex = (bl->m > 0) ? bl->m : mapindex_name2id(map_default.mapname);
  12213. }
  12214. // Init Group ID, Level, & permissions
  12215. sd->group_id = sd->group_level = 99;
  12216. sd->permissions |= PC_PERM_ALLPERMISSION;
  12217. }
  12218. if (!is_atcommand(fd, sd, cmd, type)) {
  12219. ShowWarning("buildin_atcommand: failed to execute command '%s'\n", cmd);
  12220. script_reportsrc(st);
  12221. return SCRIPT_CMD_FAILURE;
  12222. }
  12223. return SCRIPT_CMD_SUCCESS;
  12224. }
  12225. /*==========================================
  12226. * gmcommand [MouseJstr]
  12227. *------------------------------------------*/
  12228. BUILDIN_FUNC(atcommand) {
  12229. return atcommand_sub(st,0);
  12230. }
  12231. /** Displays a message for the player only (like system messages like "you got an apple" )
  12232. * dispbottom("<message>"{,<color>{,<char_id>}})
  12233. * @param message
  12234. * @param color Hex color default (Green)
  12235. */
  12236. BUILDIN_FUNC(dispbottom)
  12237. {
  12238. TBL_PC *sd;
  12239. int color = 0;
  12240. const char *message;
  12241. if (!script_charid2sd(4,sd))
  12242. return SCRIPT_CMD_FAILURE;
  12243. message = script_getstr(st,2);
  12244. if (script_hasdata(st,3))
  12245. color = script_getnum(st,3); // <color>
  12246. if(sd) {
  12247. if (script_hasdata(st,3))
  12248. clif_messagecolor2(sd, color, message); // [Napster]
  12249. else
  12250. clif_disp_onlyself(sd, message, (int)strlen(message));
  12251. }
  12252. return SCRIPT_CMD_SUCCESS;
  12253. }
  12254. /*===================================
  12255. * Heal portion of recovery command
  12256. *-----------------------------------*/
  12257. int recovery_sub(struct map_session_data* sd, int revive)
  12258. {
  12259. if(revive&(1|4) && pc_isdead(sd)) {
  12260. status_revive(&sd->bl, 100, 100);
  12261. clif_displaymessage(sd->fd,msg_txt(sd,16)); // You've been revived!
  12262. clif_specialeffect(&sd->bl, 77, AREA);
  12263. } else if(revive&(1|2) && !pc_isdead(sd)) {
  12264. status_percent_heal(&sd->bl, 100, 100);
  12265. clif_displaymessage(sd->fd,msg_txt(sd,680)); // You have been recovered!
  12266. }
  12267. return SCRIPT_CMD_SUCCESS;
  12268. }
  12269. /*=========================================================================
  12270. * Fully Recover a Character's HP/SP - [Capuche] & [Akinari]
  12271. * recovery <type>{,<option>,<revive_flag>{,<map name>}};
  12272. * <type> determines <option>:
  12273. * 0 : char_id
  12274. * 1 : party_id
  12275. * 2 : guild_id
  12276. * 3 : map_name
  12277. * 4 : all characters
  12278. * <revive_flag>:
  12279. * 1 : Revive and heal all players (default)
  12280. * 2 : Heal living players only
  12281. * 4 : Revive dead players only
  12282. * <map name>:
  12283. * for types 1-2 : map_name (null = all maps)
  12284. *-------------------------------------------------------------------------*/
  12285. BUILDIN_FUNC(recovery)
  12286. {
  12287. TBL_PC *sd;
  12288. int map_idx = 0, type = 0, revive = 1;
  12289. type = script_getnum(st,2);
  12290. if(script_hasdata(st,4))
  12291. revive = script_getnum(st,4);
  12292. switch(type) {
  12293. case 0:
  12294. if(script_hasdata(st,3))
  12295. sd=map_charid2sd(script_getnum(st,3));
  12296. else if((sd = script_rid2sd(st)) == NULL)
  12297. return SCRIPT_CMD_SUCCESS; //If we don't have sd by now, bail out
  12298. recovery_sub(sd, revive);
  12299. break;
  12300. case 1:
  12301. {
  12302. struct party_data* p;
  12303. //When no party given, we use invoker party
  12304. int p_id = 0, i;
  12305. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12306. map_idx = map_mapname2mapid(script_getstr(st,5));
  12307. if(map_idx < 1) { //But we'll check anyways
  12308. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12309. return SCRIPT_CMD_FAILURE;
  12310. }
  12311. }
  12312. if(script_hasdata(st,3))
  12313. p_id = script_getnum(st,3);
  12314. else if((sd = script_rid2sd(st)))
  12315. p_id = sd->status.party_id;
  12316. p = party_search(p_id);
  12317. if(p == NULL)
  12318. return SCRIPT_CMD_SUCCESS;
  12319. for (i = 0; i < MAX_PARTY; i++) {
  12320. struct map_session_data* pl_sd;
  12321. if((!(pl_sd = p->data[i].sd) || pl_sd->status.party_id != p_id)
  12322. || (map_idx && pl_sd->bl.m != map_idx))
  12323. continue;
  12324. recovery_sub(pl_sd, revive);
  12325. }
  12326. break;
  12327. }
  12328. case 2:
  12329. {
  12330. struct guild* g;
  12331. //When no guild given, we use invoker guild
  12332. int g_id = 0, i;
  12333. if(script_hasdata(st,5)) {//Bad maps shouldn't cause issues
  12334. map_idx = map_mapname2mapid(script_getstr(st,5));
  12335. if(map_idx < 1) { //But we'll check anyways
  12336. ShowDebug("recovery: bad map name given (%s)\n", script_getstr(st,5));
  12337. return SCRIPT_CMD_FAILURE;
  12338. }
  12339. }
  12340. if(script_hasdata(st,3))
  12341. g_id = script_getnum(st,3);
  12342. else if((sd = script_rid2sd(st)))
  12343. g_id = sd->status.guild_id;
  12344. g = guild_search(g_id);
  12345. if(g == NULL)
  12346. return SCRIPT_CMD_SUCCESS;
  12347. for (i = 0; i < MAX_GUILD; i++) {
  12348. struct map_session_data* pl_sd;
  12349. if((!(pl_sd = g->member[i].sd) || pl_sd->status.guild_id != g_id)
  12350. || (map_idx && pl_sd->bl.m != map_idx))
  12351. continue;
  12352. recovery_sub(pl_sd, revive);
  12353. }
  12354. break;
  12355. }
  12356. case 3:
  12357. if(script_hasdata(st,3))
  12358. map_idx = map_mapname2mapid(script_getstr(st,3));
  12359. else if((sd = script_rid2sd(st)))
  12360. map_idx = sd->bl.m;
  12361. if(map_idx < 1)
  12362. return SCRIPT_CMD_FAILURE; //No sd and no map given - return
  12363. case 4:
  12364. {
  12365. struct s_mapiterator *iter;
  12366. struct script_data *data = script_getdata(st, 3);
  12367. get_val(st, data); // Convert into value in case of a variable
  12368. if(script_hasdata(st,3) && !data_isstring(data))
  12369. revive = script_getnum(st,3); // recovery 4,<revive_flag>;
  12370. iter = mapit_getallusers();
  12371. for (sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter)) {
  12372. if(type == 3 && sd->bl.m != map_idx)
  12373. continue;
  12374. recovery_sub(sd, revive);
  12375. }
  12376. mapit_free(iter);
  12377. break;
  12378. }
  12379. default:
  12380. ShowWarning("script: buildin_recovery: Invalid type %d\n", type);
  12381. script_pushint(st,-1);
  12382. return SCRIPT_CMD_FAILURE;
  12383. }
  12384. script_pushint(st,1); //Successfully executed without errors
  12385. return SCRIPT_CMD_SUCCESS;
  12386. }
  12387. /*==========================================
  12388. * Get your pet info: getpetinfo <type>{,<char_id>}
  12389. * n -> 0:pet_id 1:pet_class 2:pet_name
  12390. * 3:friendly 4:hungry, 5: rename flag.6:level,
  12391. * 7:game id
  12392. *------------------------------------------*/
  12393. BUILDIN_FUNC(getpetinfo)
  12394. {
  12395. TBL_PC *sd=script_rid2sd(st);
  12396. TBL_PET *pd;
  12397. int type = script_getnum(st,2);
  12398. if (!script_charid2sd(3, sd) || !(pd = sd->pd)) {
  12399. if (type == 2)
  12400. script_pushconststr(st,"null");
  12401. else
  12402. script_pushint(st,0);
  12403. return SCRIPT_CMD_SUCCESS;
  12404. }
  12405. switch(type){
  12406. case PETINFO_ID: script_pushint(st,pd->pet.pet_id); break;
  12407. case PETINFO_CLASS: script_pushint(st,pd->pet.class_); break;
  12408. case PETINFO_NAME: script_pushstrcopy(st,pd->pet.name); break;
  12409. case PETINFO_INTIMATE: script_pushint(st,pd->pet.intimate); break;
  12410. case PETINFO_HUNGRY: script_pushint(st,pd->pet.hungry); break;
  12411. case PETINFO_RENAMED: script_pushint(st,pd->pet.rename_flag); break;
  12412. case PETINFO_LEVEL: script_pushint(st,(int)pd->pet.level); break;
  12413. case PETINFO_BLOCKID: script_pushint(st,pd->bl.id); break;
  12414. default:
  12415. script_pushint(st,0);
  12416. break;
  12417. }
  12418. return SCRIPT_CMD_SUCCESS;
  12419. }
  12420. /*==========================================
  12421. * Get your homunculus info: gethominfo <type>{,<char_id>};
  12422. * type -> 0:hom_id 1:class 2:name
  12423. * 3:friendly 4:hungry, 5: rename flag.
  12424. * 6: level, 7: game id
  12425. *------------------------------------------*/
  12426. BUILDIN_FUNC(gethominfo)
  12427. {
  12428. TBL_PC *sd=script_rid2sd(st);
  12429. TBL_HOM *hd;
  12430. int type=script_getnum(st,2);
  12431. if (!script_charid2sd(3, sd) || !(hd = sd->hd)) {
  12432. if (type == 2)
  12433. script_pushconststr(st,"null");
  12434. else
  12435. script_pushint(st,0);
  12436. return SCRIPT_CMD_SUCCESS;
  12437. }
  12438. switch(type){
  12439. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  12440. case 1: script_pushint(st,hd->homunculus.class_); break;
  12441. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  12442. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  12443. case 4: script_pushint(st,hd->homunculus.hunger); break;
  12444. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  12445. case 6: script_pushint(st,hd->homunculus.level); break;
  12446. case 7: script_pushint(st,hd->bl.id); break;
  12447. default:
  12448. script_pushint(st,0);
  12449. break;
  12450. }
  12451. return SCRIPT_CMD_SUCCESS;
  12452. }
  12453. /// Retrieves information about character's mercenary
  12454. /// getmercinfo <type>[,<char id>];
  12455. BUILDIN_FUNC(getmercinfo)
  12456. {
  12457. int type;
  12458. struct map_session_data* sd;
  12459. struct mercenary_data* md;
  12460. type = script_getnum(st,2);
  12461. if( script_hasdata(st,3) )
  12462. {
  12463. int char_id = script_getnum(st,3);
  12464. if( ( sd = map_charid2sd(char_id) ) == NULL )
  12465. {
  12466. ShowError("buildin_getmercinfo: No such character (char_id=%d).\n", char_id);
  12467. script_pushnil(st);
  12468. return SCRIPT_CMD_FAILURE;
  12469. }
  12470. }
  12471. else
  12472. {
  12473. if( ( sd = script_rid2sd(st) ) == NULL )
  12474. {
  12475. script_pushnil(st);
  12476. return SCRIPT_CMD_SUCCESS;
  12477. }
  12478. }
  12479. md = ( sd->status.mer_id && sd->md ) ? sd->md : NULL;
  12480. switch( type )
  12481. {
  12482. case 0: script_pushint(st,md ? md->mercenary.mercenary_id : 0); break;
  12483. case 1: script_pushint(st,md ? md->mercenary.class_ : 0); break;
  12484. case 2:
  12485. if( md )
  12486. script_pushstrcopy(st,md->db->name);
  12487. else
  12488. script_pushconststr(st,"");
  12489. break;
  12490. case 3: script_pushint(st,md ? mercenary_get_faith(md) : 0); break;
  12491. case 4: script_pushint(st,md ? mercenary_get_calls(md) : 0); break;
  12492. case 5: script_pushint(st,md ? md->mercenary.kill_count : 0); break;
  12493. case 6: script_pushint(st,md ? mercenary_get_lifetime(md) : 0); break;
  12494. case 7: script_pushint(st,md ? md->db->lv : 0); break;
  12495. case 8: script_pushint(st,md ? md->bl.id : 0); break;
  12496. default:
  12497. ShowError("buildin_getmercinfo: Invalid type %d (char_id=%d).\n", type, sd->status.char_id);
  12498. script_pushnil(st);
  12499. return SCRIPT_CMD_FAILURE;
  12500. }
  12501. return SCRIPT_CMD_SUCCESS;
  12502. }
  12503. /*==========================================
  12504. * Shows wether your inventory(and equips) contain
  12505. selected card or not.
  12506. checkequipedcard(4001);
  12507. *------------------------------------------*/
  12508. BUILDIN_FUNC(checkequipedcard)
  12509. {
  12510. TBL_PC *sd=script_rid2sd(st);
  12511. if(sd){
  12512. int n,i,c=0;
  12513. c=script_getnum(st,2);
  12514. for(i=0;i<MAX_INVENTORY;i++){
  12515. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  12516. if (itemdb_isspecial(sd->status.inventory[i].card[0]))
  12517. continue;
  12518. for(n=0;n<sd->inventory_data[i]->slot;n++){
  12519. if(sd->status.inventory[i].card[n]==c){
  12520. script_pushint(st,1);
  12521. return SCRIPT_CMD_SUCCESS;
  12522. }
  12523. }
  12524. }
  12525. }
  12526. }
  12527. script_pushint(st,0);
  12528. return SCRIPT_CMD_SUCCESS;
  12529. }
  12530. BUILDIN_FUNC(jump_zero)
  12531. {
  12532. int sel;
  12533. sel=script_getnum(st,2);
  12534. if(!sel) {
  12535. int pos;
  12536. if( !data_islabel(script_getdata(st,3)) ){
  12537. ShowError("script: jump_zero: not label !\n");
  12538. st->state=END;
  12539. return SCRIPT_CMD_FAILURE;
  12540. }
  12541. pos=script_getnum(st,3);
  12542. st->pos=pos;
  12543. st->state=GOTO;
  12544. }
  12545. return SCRIPT_CMD_SUCCESS;
  12546. }
  12547. /*==========================================
  12548. * movenpc [MouseJstr]
  12549. *------------------------------------------*/
  12550. BUILDIN_FUNC(movenpc)
  12551. {
  12552. TBL_NPC *nd = NULL;
  12553. const char *npc;
  12554. int x,y;
  12555. npc = script_getstr(st,2);
  12556. x = script_getnum(st,3);
  12557. y = script_getnum(st,4);
  12558. if ((nd = npc_name2id(npc)) == NULL){
  12559. ShowError("script: movenpc: NPC with ID '%s' was not found!\n", npc );
  12560. return -1;
  12561. }
  12562. if (script_hasdata(st,5))
  12563. nd->ud.dir = script_getnum(st,5) % 8;
  12564. npc_movenpc(nd, x, y);
  12565. return SCRIPT_CMD_SUCCESS;
  12566. }
  12567. /*==========================================
  12568. * message [MouseJstr]
  12569. *------------------------------------------*/
  12570. BUILDIN_FUNC(message)
  12571. {
  12572. const char *msg,*player;
  12573. TBL_PC *pl_sd = NULL;
  12574. player = script_getstr(st,2);
  12575. msg = script_getstr(st,3);
  12576. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  12577. return SCRIPT_CMD_SUCCESS;
  12578. clif_displaymessage(pl_sd->fd, msg);
  12579. return SCRIPT_CMD_SUCCESS;
  12580. }
  12581. /*==========================================
  12582. * npctalk (sends message to surrounding area)
  12583. *------------------------------------------*/
  12584. BUILDIN_FUNC(npctalk)
  12585. {
  12586. const char* str;
  12587. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  12588. str = script_getstr(st,2);
  12589. if(nd)
  12590. {
  12591. char name[NAME_LENGTH], message[256];
  12592. safestrncpy(name, nd->name, sizeof(name));
  12593. strtok(name, "#"); // discard extra name identifier if present
  12594. safesnprintf(message, sizeof(message), "%s : %s", name, str);
  12595. clif_disp_overhead(&nd->bl, message);
  12596. }
  12597. return SCRIPT_CMD_SUCCESS;
  12598. }
  12599. // change npc walkspeed [Valaris]
  12600. BUILDIN_FUNC(npcspeed)
  12601. {
  12602. struct npc_data* nd;
  12603. int speed;
  12604. speed = script_getnum(st,2);
  12605. nd =(struct npc_data *)map_id2bl(st->oid);
  12606. if( nd ) {
  12607. nd->speed = speed;
  12608. nd->ud.state.speed_changed = 1;
  12609. }
  12610. return SCRIPT_CMD_SUCCESS;
  12611. }
  12612. // make an npc walk to a position [Valaris]
  12613. BUILDIN_FUNC(npcwalkto)
  12614. {
  12615. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12616. int x=0,y=0;
  12617. x=script_getnum(st,2);
  12618. y=script_getnum(st,3);
  12619. if(nd) {
  12620. if (!nd->status.hp)
  12621. status_calc_npc(nd, SCO_FIRST);
  12622. else
  12623. status_calc_npc(nd, SCO_NONE);
  12624. unit_walktoxy(&nd->bl,x,y,0);
  12625. }
  12626. return SCRIPT_CMD_SUCCESS;
  12627. }
  12628. // stop an npc's movement [Valaris]
  12629. BUILDIN_FUNC(npcstop)
  12630. {
  12631. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  12632. if(nd) {
  12633. unit_stop_walking(&nd->bl,1|4);
  12634. }
  12635. return SCRIPT_CMD_SUCCESS;
  12636. }
  12637. /**
  12638. * getlook(<type>{,<char_id>})
  12639. **/
  12640. BUILDIN_FUNC(getlook)
  12641. {
  12642. int type,val;
  12643. TBL_PC *sd;
  12644. if (!script_charid2sd(3,sd)) {
  12645. script_pushint(st,-1);
  12646. return SCRIPT_CMD_FAILURE;
  12647. }
  12648. type=script_getnum(st,2);
  12649. val=-1;
  12650. switch(type) {
  12651. // TODO: implement LOOK_BASE as stated in script doc
  12652. case LOOK_HAIR: val=sd->status.hair; break; //1
  12653. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  12654. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  12655. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  12656. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  12657. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  12658. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  12659. case LOOK_SHIELD: val=sd->status.shield; break; //8
  12660. case LOOK_SHOES: break; //9
  12661. case LOOK_ROBE: val=sd->status.robe; break; //12
  12662. case LOOK_BODY2: val=sd->status.body; break; //13
  12663. }
  12664. script_pushint(st,val);
  12665. return SCRIPT_CMD_SUCCESS;
  12666. }
  12667. /**
  12668. * getsavepoint(<information type>{,<char_id>})
  12669. * @param type 0- map name, 1- x, 2- y
  12670. **/
  12671. BUILDIN_FUNC(getsavepoint)
  12672. {
  12673. TBL_PC* sd;
  12674. int type;
  12675. if (!script_charid2sd(3,sd)) {
  12676. script_pushint(st,0);
  12677. return SCRIPT_CMD_SUCCESS;
  12678. }
  12679. type = script_getnum(st,2);
  12680. switch(type) {
  12681. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  12682. case 1: script_pushint(st,sd->status.save_point.x); break;
  12683. case 2: script_pushint(st,sd->status.save_point.y); break;
  12684. default:
  12685. script_pushint(st,0);
  12686. break;
  12687. }
  12688. return SCRIPT_CMD_SUCCESS;
  12689. }
  12690. /*==========================================
  12691. * Get position for char/NPC/pet/hom/merc/elem objects. Added by Lorky
  12692. *
  12693. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  12694. * where type:
  12695. * MapName$ - String variable for output map name
  12696. * MapX - Integer variable for output coord X
  12697. * MapY - Integer variable for output coord Y
  12698. * type - type of object
  12699. * 0 - Character coord
  12700. * 1 - NPC coord
  12701. * 2 - Pet coord
  12702. * 3 - Mob coord (see 'getunitdata')
  12703. * 4 - Homun coord
  12704. * 5 - Mercenary coord
  12705. * 6 - Elemental coord
  12706. * CharName$ - Name object. If miss or "this" the current object
  12707. *
  12708. * Return:
  12709. * 0 - success
  12710. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  12711. *------------------------------------------*/
  12712. BUILDIN_FUNC(getmapxy)
  12713. {
  12714. struct block_list *bl = NULL;
  12715. TBL_PC *sd=NULL;
  12716. int64 num;
  12717. const char *name;
  12718. char prefix;
  12719. int x,y,type;
  12720. char mapname[MAP_NAME_LENGTH];
  12721. if( !data_isreference(script_getdata(st,2)) ) {
  12722. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  12723. script_pushint(st,-1);
  12724. return SCRIPT_CMD_FAILURE;
  12725. }
  12726. if( !data_isreference(script_getdata(st,3)) ) {
  12727. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  12728. script_pushint(st,-1);
  12729. return SCRIPT_CMD_FAILURE;
  12730. }
  12731. if( !data_isreference(script_getdata(st,4)) ) {
  12732. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  12733. script_pushint(st,-1);
  12734. return SCRIPT_CMD_FAILURE;
  12735. }
  12736. if( !is_string_variable(reference_getname(script_getdata(st, 2))) ) {
  12737. ShowWarning("script: buildin_getmapxy: %s is not a string variable\n",reference_getname(script_getdata(st, 2)));
  12738. script_pushint(st,-1);
  12739. return SCRIPT_CMD_FAILURE;
  12740. }
  12741. if( is_string_variable(reference_getname(script_getdata(st, 3))) ) {
  12742. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be int\n",reference_getname(script_getdata(st, 3)));
  12743. script_pushint(st,-1);
  12744. return SCRIPT_CMD_FAILURE;
  12745. }
  12746. if( is_string_variable(reference_getname(script_getdata(st, 4))) ) {
  12747. ShowWarning("script: buildin_getmapxy: %s is a string variable, should be int\n",reference_getname(script_getdata(st, 4)));
  12748. script_pushint(st,-1);
  12749. return SCRIPT_CMD_FAILURE;
  12750. }
  12751. // Possible needly check function parameters on C_STR,C_INT,C_INT
  12752. type=script_getnum(st,5);
  12753. switch (type) {
  12754. case 0: //Get Character Position
  12755. if( script_hasdata(st,6) )
  12756. sd=map_nick2sd(script_getstr(st,6));
  12757. else
  12758. sd=script_rid2sd(st);
  12759. if (sd)
  12760. bl = &sd->bl;
  12761. break;
  12762. case 1: //Get NPC Position
  12763. if( script_hasdata(st,6) )
  12764. {
  12765. struct npc_data *nd;
  12766. nd=npc_name2id(script_getstr(st,6));
  12767. if (nd)
  12768. bl = &nd->bl;
  12769. } else //In case the origin is not an npc?
  12770. bl=map_id2bl(st->oid);
  12771. break;
  12772. case 2: //Get Pet Position
  12773. if(script_hasdata(st,6))
  12774. sd=map_nick2sd(script_getstr(st,6));
  12775. else
  12776. sd=script_rid2sd(st);
  12777. if (sd && sd->pd)
  12778. bl = &sd->pd->bl;
  12779. break;
  12780. case 3: //Get Mob Position
  12781. break; //see 'getunitdata'
  12782. case 4: //Get Homun Position
  12783. if(script_hasdata(st,6))
  12784. sd=map_nick2sd(script_getstr(st,6));
  12785. else
  12786. sd=script_rid2sd(st);
  12787. if (sd && sd->hd)
  12788. bl = &sd->hd->bl;
  12789. break;
  12790. case 5: //Get Mercenary Position
  12791. if(script_hasdata(st,6))
  12792. sd=map_nick2sd(script_getstr(st,6));
  12793. else
  12794. sd=script_rid2sd(st);
  12795. if (sd && sd->md)
  12796. bl = &sd->md->bl;
  12797. break;
  12798. case 6: //Get Elemental Position
  12799. if(script_hasdata(st,6))
  12800. sd=map_nick2sd(script_getstr(st,6));
  12801. else
  12802. sd=script_rid2sd(st);
  12803. if (sd && sd->ed)
  12804. bl = &sd->ed->bl;
  12805. break;
  12806. default:
  12807. ShowWarning("script: buildin_getmapxy: Invalid type %d\n", type);
  12808. script_pushint(st,-1);
  12809. return SCRIPT_CMD_FAILURE;
  12810. }
  12811. if (!bl) { //No object found.
  12812. script_pushint(st,-1);
  12813. return SCRIPT_CMD_SUCCESS;
  12814. }
  12815. x= bl->x;
  12816. y= bl->y;
  12817. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  12818. //Set MapName$
  12819. num=st->stack->stack_data[st->start+2].u.num;
  12820. name=get_str(script_getvarid(num));
  12821. prefix=*name;
  12822. if(not_server_variable(prefix))
  12823. sd=script_rid2sd(st);
  12824. else
  12825. sd=NULL;
  12826. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  12827. //Set MapX
  12828. num=st->stack->stack_data[st->start+3].u.num;
  12829. name=get_str(script_getvarid(num));
  12830. prefix=*name;
  12831. if(not_server_variable(prefix))
  12832. sd=script_rid2sd(st);
  12833. else
  12834. sd=NULL;
  12835. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(x),script_getref(st,3));
  12836. //Set MapY
  12837. num=st->stack->stack_data[st->start+4].u.num;
  12838. name=get_str(script_getvarid(num));
  12839. prefix=*name;
  12840. if(not_server_variable(prefix))
  12841. sd=script_rid2sd(st);
  12842. else
  12843. sd=NULL;
  12844. set_reg(st,sd,num,name,(void*)__64BPRTSIZE(y),script_getref(st,4));
  12845. //Return Success value
  12846. script_pushint(st,0);
  12847. return SCRIPT_CMD_SUCCESS;
  12848. }
  12849. /// Returns the map name of given map ID.
  12850. ///
  12851. /// mapid2name <map ID>;
  12852. BUILDIN_FUNC(mapid2name)
  12853. {
  12854. uint16 m = script_getnum(st, 2);
  12855. if (m < 0 || m >= MAX_MAP_PER_SERVER) {
  12856. script_pushstr(st, "");
  12857. return SCRIPT_CMD_FAILURE;
  12858. }
  12859. script_pushstrcopy(st, map_mapid2mapname(m));
  12860. return SCRIPT_CMD_SUCCESS;
  12861. }
  12862. /*==========================================
  12863. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  12864. *------------------------------------------*/
  12865. BUILDIN_FUNC(logmes)
  12866. {
  12867. const char *str;
  12868. TBL_PC* sd;
  12869. sd = script_rid2sd(st);
  12870. if( sd == NULL )
  12871. return SCRIPT_CMD_FAILURE;
  12872. str = script_getstr(st,2);
  12873. log_npc(sd,str);
  12874. return SCRIPT_CMD_SUCCESS;
  12875. }
  12876. BUILDIN_FUNC(summon)
  12877. {
  12878. int _class, timeout=0;
  12879. const char *str,*event="";
  12880. TBL_PC *sd;
  12881. struct mob_data *md;
  12882. int tick = gettick();
  12883. sd=script_rid2sd(st);
  12884. if (!sd)
  12885. return SCRIPT_CMD_SUCCESS;
  12886. str =script_getstr(st,2);
  12887. _class=script_getnum(st,3);
  12888. if( script_hasdata(st,4) )
  12889. timeout=script_getnum(st,4);
  12890. if( script_hasdata(st,5) ){
  12891. event=script_getstr(st,5);
  12892. check_event(st, event);
  12893. }
  12894. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  12895. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event, SZ_SMALL, AI_NONE);
  12896. if (md) {
  12897. md->master_id=sd->bl.id;
  12898. md->special_state.ai = AI_ATTACK;
  12899. if( md->deletetimer != INVALID_TIMER )
  12900. delete_timer(md->deletetimer, mob_timer_delete);
  12901. md->deletetimer = add_timer(tick+(timeout>0?timeout:60000),mob_timer_delete,md->bl.id,0);
  12902. mob_spawn (md); //Now it is ready for spawning.
  12903. clif_specialeffect(&md->bl,344,AREA);
  12904. sc_start4(NULL,&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  12905. }
  12906. script_pushint(st, md->bl.id);
  12907. return SCRIPT_CMD_SUCCESS;
  12908. }
  12909. /*==========================================
  12910. * Checks whether it is daytime/nighttime
  12911. *------------------------------------------*/
  12912. BUILDIN_FUNC(isnight)
  12913. {
  12914. script_pushint(st,(night_flag == 1));
  12915. return SCRIPT_CMD_SUCCESS;
  12916. }
  12917. BUILDIN_FUNC(isday)
  12918. {
  12919. script_pushint(st,(night_flag == 0));
  12920. return SCRIPT_CMD_SUCCESS;
  12921. }
  12922. /*================================================
  12923. * Check how many items/cards in the list are
  12924. * equipped - used for 2/15's cards patch [celest]
  12925. *------------------------------------------------*/
  12926. BUILDIN_FUNC(isequippedcnt)
  12927. {
  12928. TBL_PC *sd;
  12929. int i, id = 1;
  12930. int ret = 0;
  12931. sd = script_rid2sd(st);
  12932. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12933. script_pushint(st,0);
  12934. return SCRIPT_CMD_SUCCESS;
  12935. }
  12936. for (i=0; id!=0; i++) {
  12937. short j;
  12938. FETCH (i+2, id) else id = 0;
  12939. if (id <= 0)
  12940. continue;
  12941. for (j=0; j<EQI_MAX; j++) {
  12942. short index = sd->equip_index[j];
  12943. if(index < 0)
  12944. continue;
  12945. if (pc_is_same_equip_index((enum equip_index)j, sd->equip_index, index))
  12946. continue;
  12947. if(!sd->inventory_data[index])
  12948. continue;
  12949. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  12950. if (sd->inventory_data[index]->nameid == id)
  12951. ret+= sd->status.inventory[index].amount;
  12952. } else { //Count cards.
  12953. short k;
  12954. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  12955. continue; //No cards
  12956. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  12957. if (sd->status.inventory[index].card[k] == id)
  12958. ret++; //[Lupus]
  12959. }
  12960. }
  12961. }
  12962. }
  12963. script_pushint(st,ret);
  12964. return SCRIPT_CMD_SUCCESS;
  12965. }
  12966. /*================================================
  12967. * Check whether another card has been
  12968. * equipped - used for 2/15's cards patch [celest]
  12969. * -- Items checked cannot be reused in another
  12970. * card set to prevent exploits
  12971. *------------------------------------------------*/
  12972. BUILDIN_FUNC(isequipped)
  12973. {
  12974. TBL_PC *sd;
  12975. int i, id = 1;
  12976. int ret = -1;
  12977. //Original hash to reverse it when full check fails.
  12978. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  12979. sd = script_rid2sd(st);
  12980. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  12981. script_pushint(st,0);
  12982. return SCRIPT_CMD_SUCCESS;
  12983. }
  12984. setitem_hash = sd->bonus.setitem_hash;
  12985. setitem_hash2 = sd->bonus.setitem_hash2;
  12986. for (i=0; id!=0; i++) {
  12987. int flag = 0;
  12988. short j;
  12989. FETCH (i+2, id) else id = 0;
  12990. if (id <= 0)
  12991. continue;
  12992. for (j=0; j<EQI_MAX; j++) {
  12993. short index = sd->equip_index[j];
  12994. if(index < 0)
  12995. continue;
  12996. if (pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index))
  12997. continue;
  12998. if(!sd->inventory_data[index])
  12999. continue;
  13000. if (itemdb_type(id) != IT_CARD) {
  13001. if (sd->inventory_data[index]->nameid != id)
  13002. continue;
  13003. flag = 1;
  13004. break;
  13005. } else { //Cards
  13006. short k;
  13007. if (sd->inventory_data[index]->slot == 0 ||
  13008. itemdb_isspecial(sd->status.inventory[index].card[0]))
  13009. continue;
  13010. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  13011. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  13012. unsigned int hash = 0;
  13013. if (sd->status.inventory[index].card[k] != id)
  13014. continue;
  13015. hash = 1<<((j<5?j:j-5)*4 + k);
  13016. // check if card is already used by another set
  13017. if ( ( j < 5 ? sd->bonus.setitem_hash : sd->bonus.setitem_hash2 ) & hash)
  13018. continue;
  13019. // We have found a match
  13020. flag = 1;
  13021. // Set hash so this card cannot be used by another
  13022. if (j<5)
  13023. sd->bonus.setitem_hash |= hash;
  13024. else
  13025. sd->bonus.setitem_hash2 |= hash;
  13026. break;
  13027. }
  13028. }
  13029. if (flag) break; //Card found
  13030. }
  13031. if (ret == -1)
  13032. ret = flag;
  13033. else
  13034. ret &= flag;
  13035. if (!ret) break;
  13036. }
  13037. if (!ret) {//When check fails, restore original hash values. [Skotlex]
  13038. sd->bonus.setitem_hash = setitem_hash;
  13039. sd->bonus.setitem_hash2 = setitem_hash2;
  13040. }
  13041. script_pushint(st,ret);
  13042. return SCRIPT_CMD_SUCCESS;
  13043. }
  13044. /*================================================
  13045. * Check how many given inserted cards in the CURRENT
  13046. * weapon - used for 2/15's cards patch [Lupus]
  13047. *------------------------------------------------*/
  13048. BUILDIN_FUNC(cardscnt)
  13049. {
  13050. TBL_PC *sd;
  13051. int i, k, id = 1;
  13052. int ret = 0;
  13053. int index;
  13054. sd = script_rid2sd(st);
  13055. for (i=0; id!=0; i++) {
  13056. FETCH (i+2, id) else id = 0;
  13057. if (id <= 0)
  13058. continue;
  13059. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  13060. if(index < 0) continue;
  13061. if(!sd->inventory_data[index])
  13062. continue;
  13063. if(itemdb_type(id) != IT_CARD) {
  13064. if (sd->inventory_data[index]->nameid == id)
  13065. ret+= sd->status.inventory[index].amount;
  13066. } else {
  13067. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  13068. continue;
  13069. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  13070. if (sd->status.inventory[index].card[k] == id)
  13071. ret++;
  13072. }
  13073. }
  13074. }
  13075. script_pushint(st,ret);
  13076. // script_pushint(st,current_equip_item_index);
  13077. return SCRIPT_CMD_SUCCESS;
  13078. }
  13079. /*=======================================================
  13080. * Returns the refined number of the current item, or an
  13081. * item with inventory index specified
  13082. *-------------------------------------------------------*/
  13083. BUILDIN_FUNC(getrefine)
  13084. {
  13085. TBL_PC *sd;
  13086. if ((sd = script_rid2sd(st))!= NULL)
  13087. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  13088. else
  13089. script_pushint(st,0);
  13090. return SCRIPT_CMD_SUCCESS;
  13091. }
  13092. /*=======================================================
  13093. * Day/Night controls
  13094. *-------------------------------------------------------*/
  13095. BUILDIN_FUNC(night)
  13096. {
  13097. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  13098. return SCRIPT_CMD_SUCCESS;
  13099. }
  13100. BUILDIN_FUNC(day)
  13101. {
  13102. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  13103. return SCRIPT_CMD_SUCCESS;
  13104. }
  13105. /**
  13106. * unequip <equipment slot>{,<char_id>};
  13107. * @author [Spectre]
  13108. **/
  13109. BUILDIN_FUNC(unequip) {
  13110. int pos;
  13111. TBL_PC *sd;
  13112. if (!script_charid2sd(3,sd))
  13113. return SCRIPT_CMD_FAILURE;
  13114. pos = script_getnum(st,2);
  13115. if (pos >= 1 && pos <= ARRAYLENGTH(equip)) {
  13116. short i = pc_checkequip(sd,equip[pos-1]);
  13117. if (i >= 0) {
  13118. pc_unequipitem(sd,i,1|2);
  13119. script_pushint(st, 1);
  13120. return SCRIPT_CMD_SUCCESS;
  13121. }
  13122. }
  13123. ShowError("buildin_unequip: No item equipped at pos %d (CID=%d/AID=%d).\n", pos, sd->status.char_id, sd->status.account_id);
  13124. script_pushint(st, 0);
  13125. return SCRIPT_CMD_FAILURE;
  13126. }
  13127. /**
  13128. * equip <item id>{,<char_id>};
  13129. **/
  13130. BUILDIN_FUNC(equip) {
  13131. unsigned short nameid = 0;
  13132. TBL_PC *sd;
  13133. struct item_data *item_data;
  13134. if (!script_charid2sd(3,sd))
  13135. return SCRIPT_CMD_FAILURE;
  13136. nameid = script_getnum(st,2);
  13137. if ((item_data = itemdb_exists(nameid))) {
  13138. int i;
  13139. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  13140. if (i < MAX_INVENTORY) {
  13141. pc_equipitem(sd,i,item_data->equip);
  13142. script_pushint(st,1);
  13143. return SCRIPT_CMD_SUCCESS;
  13144. }
  13145. }
  13146. ShowError("buildin_equip: Item %hu cannot be equipped\n",nameid);
  13147. script_pushint(st,0);
  13148. return SCRIPT_CMD_FAILURE;
  13149. }
  13150. BUILDIN_FUNC(autoequip)
  13151. {
  13152. unsigned short nameid;
  13153. int flag;
  13154. struct item_data *item_data;
  13155. nameid=script_getnum(st,2);
  13156. flag=script_getnum(st,3);
  13157. if( ( item_data = itemdb_exists(nameid) ) == NULL )
  13158. {
  13159. ShowError("buildin_autoequip: Invalid item '%hu'.\n", nameid);
  13160. return SCRIPT_CMD_FAILURE;
  13161. }
  13162. if( !itemdb_isequip2(item_data) )
  13163. {
  13164. ShowError("buildin_autoequip: Item '%hu' cannot be equipped.\n", nameid);
  13165. return SCRIPT_CMD_FAILURE;
  13166. }
  13167. item_data->flag.autoequip = flag>0?1:0;
  13168. return SCRIPT_CMD_SUCCESS;
  13169. }
  13170. BUILDIN_FUNC(setbattleflag)
  13171. {
  13172. const char *flag, *value;
  13173. flag = script_getstr(st,2);
  13174. value = script_getstr(st,3); // HACK: Retrieve number as string (auto-converted) for battle_set_value
  13175. if (battle_set_value(flag, value) == 0)
  13176. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  13177. else
  13178. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  13179. return SCRIPT_CMD_SUCCESS;
  13180. }
  13181. BUILDIN_FUNC(getbattleflag)
  13182. {
  13183. const char *flag;
  13184. flag = script_getstr(st,2);
  13185. script_pushint(st,battle_get_value(flag));
  13186. return SCRIPT_CMD_SUCCESS;
  13187. }
  13188. //=======================================================
  13189. // strlen [Valaris]
  13190. //-------------------------------------------------------
  13191. BUILDIN_FUNC(getstrlen)
  13192. {
  13193. const char *str = script_getstr(st,2);
  13194. int len = (str) ? (int)strlen(str) : 0;
  13195. script_pushint(st,len);
  13196. return SCRIPT_CMD_SUCCESS;
  13197. }
  13198. //=======================================================
  13199. // isalpha [Valaris]
  13200. //-------------------------------------------------------
  13201. BUILDIN_FUNC(charisalpha)
  13202. {
  13203. const char *str=script_getstr(st,2);
  13204. int pos=script_getnum(st,3);
  13205. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) != 0 : 0;
  13206. script_pushint(st,val);
  13207. return SCRIPT_CMD_SUCCESS;
  13208. }
  13209. //=======================================================
  13210. // charisupper <str>, <index>
  13211. //-------------------------------------------------------
  13212. BUILDIN_FUNC(charisupper)
  13213. {
  13214. const char *str = script_getstr(st,2);
  13215. int pos = script_getnum(st,3);
  13216. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISUPPER( str[pos] ) : 0;
  13217. script_pushint(st,val);
  13218. return SCRIPT_CMD_SUCCESS;
  13219. }
  13220. //=======================================================
  13221. // charislower <str>, <index>
  13222. //-------------------------------------------------------
  13223. BUILDIN_FUNC(charislower)
  13224. {
  13225. const char *str = script_getstr(st,2);
  13226. int pos = script_getnum(st,3);
  13227. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISLOWER( str[pos] ) : 0;
  13228. script_pushint(st,val);
  13229. return SCRIPT_CMD_SUCCESS;
  13230. }
  13231. //=======================================================
  13232. // charat <str>, <index>
  13233. //-------------------------------------------------------
  13234. BUILDIN_FUNC(charat) {
  13235. const char *str = script_getstr(st,2);
  13236. int pos = script_getnum(st,3);
  13237. if( pos >= 0 && (unsigned int)pos < strlen(str) ) {
  13238. char output[2];
  13239. output[0] = str[pos];
  13240. output[1] = '\0';
  13241. script_pushstrcopy(st, output);
  13242. } else
  13243. script_pushconststr(st, "");
  13244. return SCRIPT_CMD_SUCCESS;
  13245. }
  13246. //=======================================================
  13247. // setchar <string>, <char>, <index>
  13248. //-------------------------------------------------------
  13249. BUILDIN_FUNC(setchar)
  13250. {
  13251. const char *str = script_getstr(st,2);
  13252. const char *c = script_getstr(st,3);
  13253. int index = script_getnum(st,4);
  13254. char *output = aStrdup(str);
  13255. if(index >= 0 && index < strlen(output))
  13256. output[index] = *c;
  13257. script_pushstr(st, output);
  13258. return SCRIPT_CMD_SUCCESS;
  13259. }
  13260. //=======================================================
  13261. // insertchar <string>, <char>, <index>
  13262. //-------------------------------------------------------
  13263. BUILDIN_FUNC(insertchar)
  13264. {
  13265. const char *str = script_getstr(st,2);
  13266. const char *c = script_getstr(st,3);
  13267. int index = script_getnum(st,4);
  13268. char *output;
  13269. size_t len = strlen(str);
  13270. if(index < 0)
  13271. index = 0;
  13272. else if(index > len)
  13273. index = len;
  13274. output = (char*)aMalloc(len + 2);
  13275. memcpy(output, str, index);
  13276. output[index] = c[0];
  13277. memcpy(&output[index+1], &str[index], len - index);
  13278. output[len+1] = '\0';
  13279. script_pushstr(st, output);
  13280. return SCRIPT_CMD_SUCCESS;
  13281. }
  13282. //=======================================================
  13283. // delchar <string>, <index>
  13284. //-------------------------------------------------------
  13285. BUILDIN_FUNC(delchar)
  13286. {
  13287. const char *str = script_getstr(st,2);
  13288. int index = script_getnum(st,3);
  13289. char *output;
  13290. size_t len = strlen(str);
  13291. if(index < 0 || index > len) {
  13292. //return original
  13293. output = aStrdup(str);
  13294. script_pushstr(st, output);
  13295. return SCRIPT_CMD_SUCCESS;
  13296. }
  13297. output = (char*)aMalloc(len);
  13298. memcpy(output, str, index);
  13299. memcpy(&output[index], &str[index+1], len - index);
  13300. script_pushstr(st, output);
  13301. return SCRIPT_CMD_SUCCESS;
  13302. }
  13303. //=======================================================
  13304. // strtoupper <str>
  13305. //-------------------------------------------------------
  13306. BUILDIN_FUNC(strtoupper)
  13307. {
  13308. const char *str = script_getstr(st,2);
  13309. char *output = aStrdup(str);
  13310. char *cursor = output;
  13311. while (*cursor != '\0') {
  13312. *cursor = TOUPPER(*cursor);
  13313. cursor++;
  13314. }
  13315. script_pushstr(st, output);
  13316. return SCRIPT_CMD_SUCCESS;
  13317. }
  13318. //=======================================================
  13319. // strtolower <str>
  13320. //-------------------------------------------------------
  13321. BUILDIN_FUNC(strtolower)
  13322. {
  13323. const char *str = script_getstr(st,2);
  13324. char *output = aStrdup(str);
  13325. char *cursor = output;
  13326. while (*cursor != '\0') {
  13327. *cursor = TOLOWER(*cursor);
  13328. cursor++;
  13329. }
  13330. script_pushstr(st, output);
  13331. return SCRIPT_CMD_SUCCESS;
  13332. }
  13333. //=======================================================
  13334. // substr <str>, <start>, <end>
  13335. //-------------------------------------------------------
  13336. BUILDIN_FUNC(substr)
  13337. {
  13338. const char *str = script_getstr(st,2);
  13339. char *output;
  13340. int start = script_getnum(st,3);
  13341. int end = script_getnum(st,4);
  13342. int len = 0;
  13343. if(start >= 0 && end < strlen(str) && start <= end) {
  13344. len = end - start + 1;
  13345. output = (char*)aMalloc(len + 1);
  13346. memcpy(output, &str[start], len);
  13347. } else
  13348. output = (char*)aMalloc(1);
  13349. output[len] = '\0';
  13350. script_pushstr(st, output);
  13351. return SCRIPT_CMD_SUCCESS;
  13352. }
  13353. //=======================================================
  13354. // explode <dest_string_array>, <str>, <delimiter>
  13355. // Note: delimiter is limited to 1 char
  13356. //-------------------------------------------------------
  13357. BUILDIN_FUNC(explode)
  13358. {
  13359. struct script_data* data = script_getdata(st, 2);
  13360. const char *str = script_getstr(st,3);
  13361. const char delimiter = script_getstr(st, 4)[0];
  13362. int32 id;
  13363. size_t len = strlen(str);
  13364. int i = 0, j = 0;
  13365. int start;
  13366. char *temp;
  13367. const char* name;
  13368. TBL_PC* sd = NULL;
  13369. temp = (char*)aMalloc(len + 1);
  13370. if( !data_isreference(data) ) {
  13371. ShowError("script:explode: not a variable\n");
  13372. script_reportdata(data);
  13373. st->state = END;
  13374. return SCRIPT_CMD_FAILURE;// not a variable
  13375. }
  13376. id = reference_getid(data);
  13377. start = reference_getindex(data);
  13378. name = reference_getname(data);
  13379. if( !is_string_variable(name) ) {
  13380. ShowError("script:explode: not string array\n");
  13381. script_reportdata(data);
  13382. st->state = END;
  13383. return SCRIPT_CMD_FAILURE;// data type mismatch
  13384. }
  13385. if( not_server_variable(*name) ) {
  13386. sd = script_rid2sd(st);
  13387. if( sd == NULL )
  13388. return SCRIPT_CMD_SUCCESS;// no player attached
  13389. }
  13390. while(str[i] != '\0') {
  13391. if(str[i] == delimiter && start < SCRIPT_MAX_ARRAYSIZE-1) { //break at delimiter but ignore after reaching last array index
  13392. temp[j] = '\0';
  13393. set_reg(st, sd, reference_uid(id, start++), name, (void*)temp, reference_getref(data));
  13394. j = 0;
  13395. ++i;
  13396. } else {
  13397. temp[j++] = str[i++];
  13398. }
  13399. }
  13400. //set last string
  13401. temp[j] = '\0';
  13402. set_reg(st, sd, reference_uid(id, start), name, (void*)temp, reference_getref(data));
  13403. aFree(temp);
  13404. return SCRIPT_CMD_SUCCESS;
  13405. }
  13406. //=======================================================
  13407. // implode <string_array>
  13408. // implode <string_array>, <glue>
  13409. //-------------------------------------------------------
  13410. BUILDIN_FUNC(implode)
  13411. {
  13412. struct script_data* data = script_getdata(st, 2);
  13413. const char *name;
  13414. uint32 glue_len = 0, array_size, id;
  13415. char *output;
  13416. TBL_PC* sd = NULL;
  13417. if( !data_isreference(data) ) {
  13418. ShowError("script:implode: not a variable\n");
  13419. script_reportdata(data);
  13420. st->state = END;
  13421. return SCRIPT_CMD_FAILURE;// not a variable
  13422. }
  13423. id = reference_getid(data);
  13424. name = reference_getname(data);
  13425. if( !is_string_variable(name) ) {
  13426. ShowError("script:implode: not string array\n");
  13427. script_reportdata(data);
  13428. st->state = END;
  13429. return SCRIPT_CMD_FAILURE;// data type mismatch
  13430. }
  13431. if( not_server_variable(*name) ) {
  13432. sd = script_rid2sd(st);
  13433. if( sd == NULL )
  13434. return SCRIPT_CMD_SUCCESS;// no player attached
  13435. }
  13436. //count chars
  13437. array_size = script_array_highest_key(st, sd, name, reference_getref(data)) - 1;
  13438. if(array_size == -1) { //empty array check (AmsTaff)
  13439. ShowWarning("script:implode: array length = 0\n");
  13440. output = (char*)aMalloc(sizeof(char)*5);
  13441. sprintf(output,"%s","NULL");
  13442. } else {
  13443. const char *glue = NULL, *temp;
  13444. size_t len = 0;
  13445. int i, k = 0;
  13446. for(i = 0; i <= array_size; ++i) {
  13447. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13448. len += strlen(temp);
  13449. script_removetop(st, -1, 0);
  13450. }
  13451. //allocate mem
  13452. if( script_hasdata(st,3) ) {
  13453. glue = script_getstr(st,3);
  13454. glue_len = strlen(glue);
  13455. len += glue_len * (array_size);
  13456. }
  13457. output = (char*)aMalloc(len + 1);
  13458. //build output
  13459. for(i = 0; i < array_size; ++i) {
  13460. temp = (char*) get_val2(st, reference_uid(id, i), reference_getref(data));
  13461. len = strlen(temp);
  13462. memcpy(&output[k], temp, len);
  13463. k += len;
  13464. if(glue_len != 0) {
  13465. memcpy(&output[k], glue, glue_len);
  13466. k += glue_len;
  13467. }
  13468. script_removetop(st, -1, 0);
  13469. }
  13470. temp = (char*) get_val2(st, reference_uid(id, array_size), reference_getref(data));
  13471. len = strlen(temp);
  13472. memcpy(&output[k], temp, len);
  13473. k += len;
  13474. script_removetop(st, -1, 0);
  13475. output[k] = '\0';
  13476. }
  13477. script_pushstr(st, output);
  13478. return SCRIPT_CMD_SUCCESS;
  13479. }
  13480. //=======================================================
  13481. // sprintf(<format>, ...);
  13482. // Implements C sprintf, except format %n. The resulting string is
  13483. // returned, instead of being saved in variable by reference.
  13484. //-------------------------------------------------------
  13485. BUILDIN_FUNC(sprintf)
  13486. {
  13487. unsigned int len, argc = 0, arg = 0, buf2_len = 0;
  13488. const char* format;
  13489. char* p;
  13490. char* q;
  13491. char* buf = NULL;
  13492. char* buf2 = NULL;
  13493. struct script_data* data;
  13494. StringBuf final_buf;
  13495. // Fetch init data
  13496. format = script_getstr(st, 2);
  13497. argc = script_lastdata(st)-2;
  13498. len = strlen(format);
  13499. // Skip parsing, where no parsing is required.
  13500. if(len == 0) {
  13501. script_pushconststr(st,"");
  13502. return SCRIPT_CMD_SUCCESS;
  13503. }
  13504. // Pessimistic alloc
  13505. CREATE(buf, char, len+1);
  13506. // Need not be parsed, just solve stuff like %%.
  13507. if(argc == 0) {
  13508. memcpy(buf,format,len+1);
  13509. script_pushstrcopy(st, buf);
  13510. aFree(buf);
  13511. return SCRIPT_CMD_SUCCESS;
  13512. }
  13513. safestrncpy(buf, format, len+1);
  13514. // Issue sprintf for each parameter
  13515. StringBuf_Init(&final_buf);
  13516. q = buf;
  13517. while((p = strchr(q, '%')) != NULL) {
  13518. if(p != q) {
  13519. len = p - q + 1;
  13520. if(buf2_len < len) {
  13521. RECREATE(buf2, char, len);
  13522. buf2_len = len;
  13523. }
  13524. safestrncpy(buf2, q, len);
  13525. StringBuf_AppendStr(&final_buf, buf2);
  13526. q = p;
  13527. }
  13528. p = q + 1;
  13529. if(*p == '%') { // %%
  13530. StringBuf_AppendStr(&final_buf, "%");
  13531. q+=2;
  13532. continue;
  13533. }
  13534. if(*p == 'n') { // %n
  13535. ShowWarning("buildin_sprintf: Format %%n not supported! Skipping...\n");
  13536. script_reportsrc(st);
  13537. q+=2;
  13538. continue;
  13539. }
  13540. if(arg >= argc) {
  13541. ShowError("buildin_sprintf: Not enough arguments passed!\n");
  13542. if(buf) aFree(buf);
  13543. if(buf2) aFree(buf2);
  13544. StringBuf_Destroy(&final_buf);
  13545. script_pushconststr(st,"");
  13546. return SCRIPT_CMD_FAILURE;
  13547. }
  13548. if((p = strchr(q+1, '%')) == NULL)
  13549. p = strchr(q, 0); // EOS
  13550. len = p - q + 1;
  13551. if(buf2_len < len) {
  13552. RECREATE(buf2, char, len);
  13553. buf2_len = len;
  13554. }
  13555. safestrncpy(buf2, q, len);
  13556. q = p;
  13557. // Note: This assumes the passed value being the correct
  13558. // type to the current format specifier. If not, the server
  13559. // probably crashes or returns anything else, than expected,
  13560. // but it would behave in normal code the same way so it's
  13561. // the scripter's responsibility.
  13562. data = script_getdata(st, arg+3);
  13563. if(data_isstring(data)) // String
  13564. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13565. else if(data_isint(data)) // Number
  13566. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13567. else if(data_isreference(data)) { // Variable
  13568. char* name = reference_getname(data);
  13569. if(name[strlen(name)-1]=='$') // var Str
  13570. StringBuf_Printf(&final_buf, buf2, script_getstr(st, arg+3));
  13571. else // var Int
  13572. StringBuf_Printf(&final_buf, buf2, script_getnum(st, arg+3));
  13573. } else { // Unsupported type
  13574. ShowError("buildin_sprintf: Unknown argument type!\n");
  13575. if(buf) aFree(buf);
  13576. if(buf2) aFree(buf2);
  13577. StringBuf_Destroy(&final_buf);
  13578. script_pushconststr(st,"");
  13579. return SCRIPT_CMD_FAILURE;
  13580. }
  13581. arg++;
  13582. }
  13583. // Append anything left
  13584. if(*q)
  13585. StringBuf_AppendStr(&final_buf, q);
  13586. // Passed more, than needed
  13587. if(arg < argc) {
  13588. ShowWarning("buildin_sprintf: Unused arguments passed.\n");
  13589. script_reportsrc(st);
  13590. }
  13591. script_pushstrcopy(st, StringBuf_Value(&final_buf));
  13592. if(buf) aFree(buf);
  13593. if(buf2) aFree(buf2);
  13594. StringBuf_Destroy(&final_buf);
  13595. return SCRIPT_CMD_SUCCESS;
  13596. }
  13597. //=======================================================
  13598. // sscanf(<str>, <format>, ...);
  13599. // Implements C sscanf.
  13600. //-------------------------------------------------------
  13601. BUILDIN_FUNC(sscanf){
  13602. unsigned int argc, arg = 0, len;
  13603. struct script_data* data;
  13604. struct map_session_data* sd = NULL;
  13605. const char* str;
  13606. const char* format;
  13607. const char* p;
  13608. const char* q;
  13609. char* buf = NULL;
  13610. char* buf_p;
  13611. char* ref_str = NULL;
  13612. int ref_int;
  13613. // Get data
  13614. str = script_getstr(st, 2);
  13615. format = script_getstr(st, 3);
  13616. argc = script_lastdata(st)-3;
  13617. len = strlen(format);
  13618. if (len != 0 && strlen(str) == 0) {
  13619. // If the source string is empty but the format string is not, we return -1
  13620. // according to the C specs. (if the format string is also empty, we shall
  13621. // continue and return 0: 0 conversions took place out of the 0 attempted.)
  13622. script_pushint(st, -1);
  13623. return SCRIPT_CMD_SUCCESS;
  13624. }
  13625. CREATE(buf, char, len*2+1);
  13626. // Issue sscanf for each parameter
  13627. *buf = 0;
  13628. q = format;
  13629. while((p = strchr(q, '%'))){
  13630. if(p!=q){
  13631. strncat(buf, q, (size_t)(p-q));
  13632. q = p;
  13633. }
  13634. p = q+1;
  13635. if(*p=='*' || *p=='%'){ // Skip
  13636. strncat(buf, q, 2);
  13637. q+=2;
  13638. continue;
  13639. }
  13640. if(arg>=argc){
  13641. ShowError("buildin_sscanf: Not enough arguments passed!\n");
  13642. script_pushint(st, -1);
  13643. if(buf) aFree(buf);
  13644. if(ref_str) aFree(ref_str);
  13645. return SCRIPT_CMD_FAILURE;
  13646. }
  13647. if((p = strchr(q+1, '%'))==NULL){
  13648. p = strchr(q, 0); // EOS
  13649. }
  13650. len = p-q;
  13651. strncat(buf, q, len);
  13652. q = p;
  13653. // Validate output
  13654. data = script_getdata(st, arg+4);
  13655. if(!data_isreference(data) || !reference_tovariable(data)){
  13656. ShowError("buildin_sscanf: Target argument is not a variable!\n");
  13657. script_pushint(st, -1);
  13658. if(buf) aFree(buf);
  13659. if(ref_str) aFree(ref_str);
  13660. return SCRIPT_CMD_FAILURE;
  13661. }
  13662. buf_p = reference_getname(data);
  13663. if(not_server_variable(*buf_p) && (sd = script_rid2sd(st))==NULL){
  13664. script_pushint(st, -1);
  13665. if(buf) aFree(buf);
  13666. if(ref_str) aFree(ref_str);
  13667. return SCRIPT_CMD_SUCCESS;
  13668. }
  13669. // Save value if any
  13670. if(buf_p[strlen(buf_p)-1]=='$'){ // String
  13671. if(ref_str==NULL){
  13672. CREATE(ref_str, char, strlen(str)+1);
  13673. }
  13674. if(sscanf(str, buf, ref_str)==0){
  13675. break;
  13676. }
  13677. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)(ref_str), reference_getref(data));
  13678. } else { // Number
  13679. if(sscanf(str, buf, &ref_int)==0){
  13680. break;
  13681. }
  13682. set_reg(st, sd, reference_uid( reference_getid(data), reference_getindex(data) ), buf_p, (void *)__64BPRTSIZE(ref_int), reference_getref(data));
  13683. }
  13684. arg++;
  13685. // Disable used format (%... -> %*...)
  13686. buf_p = strchr(buf, 0);
  13687. memmove(buf_p-len+2, buf_p-len+1, len);
  13688. *(buf_p-len+1) = '*';
  13689. }
  13690. script_pushint(st, arg);
  13691. if(buf) aFree(buf);
  13692. if(ref_str) aFree(ref_str);
  13693. return SCRIPT_CMD_SUCCESS;
  13694. }
  13695. //=======================================================
  13696. // strpos(<haystack>, <needle>)
  13697. // strpos(<haystack>, <needle>, <offset>)
  13698. //
  13699. // Implements PHP style strpos. Adapted from code from
  13700. // http://www.daniweb.com/code/snippet313.html, Dave Sinkula
  13701. //-------------------------------------------------------
  13702. BUILDIN_FUNC(strpos) {
  13703. const char *haystack = script_getstr(st,2);
  13704. const char *needle = script_getstr(st,3);
  13705. int i;
  13706. size_t len;
  13707. if( script_hasdata(st,4) )
  13708. i = script_getnum(st,4);
  13709. else
  13710. i = 0;
  13711. if (needle[0] == '\0') {
  13712. script_pushint(st, -1);
  13713. return SCRIPT_CMD_SUCCESS;
  13714. }
  13715. len = strlen(haystack);
  13716. for ( ; i < len; ++i ) {
  13717. if ( haystack[i] == *needle ) {
  13718. // matched starting char -- loop through remaining chars
  13719. const char *h, *n;
  13720. for ( h = &haystack[i], n = needle; *h && *n; ++h, ++n ) {
  13721. if ( *h != *n ) {
  13722. break;
  13723. }
  13724. }
  13725. if ( !*n ) { // matched all of 'needle' to null termination
  13726. script_pushint(st, i);
  13727. return SCRIPT_CMD_SUCCESS;
  13728. }
  13729. }
  13730. }
  13731. script_pushint(st, -1);
  13732. return SCRIPT_CMD_SUCCESS;
  13733. }
  13734. //===============================================================
  13735. // replacestr <input>, <search>, <replace>{, <usecase>{, <count>}}
  13736. //
  13737. // Note: Finds all instances of <search> in <input> and replaces
  13738. // with <replace>. If specified will only replace as many
  13739. // instances as specified in <count>. By default will be case
  13740. // sensitive.
  13741. //---------------------------------------------------------------
  13742. BUILDIN_FUNC(replacestr)
  13743. {
  13744. const char *input = script_getstr(st, 2);
  13745. const char *find = script_getstr(st, 3);
  13746. const char *replace = script_getstr(st, 4);
  13747. size_t inputlen = strlen(input);
  13748. size_t findlen = strlen(find);
  13749. struct StringBuf output;
  13750. bool usecase = true;
  13751. int count = 0;
  13752. int numFinds = 0;
  13753. int i = 0, f = 0;
  13754. if(findlen == 0) {
  13755. ShowError("script:replacestr: Invalid search length.\n");
  13756. st->state = END;
  13757. return SCRIPT_CMD_FAILURE;
  13758. }
  13759. if(script_hasdata(st, 5)) {
  13760. struct script_data *data = script_getdata(st, 5);
  13761. get_val(st, data); // Convert into value in case of a variable
  13762. if( !data_isstring(data) )
  13763. usecase = script_getnum(st, 5) != 0;
  13764. else {
  13765. ShowError("script:replacestr: Invalid usecase value. Expected int got string\n");
  13766. st->state = END;
  13767. return SCRIPT_CMD_FAILURE;
  13768. }
  13769. }
  13770. if(script_hasdata(st, 6)) {
  13771. count = script_getnum(st, 6);
  13772. if(count == 0) {
  13773. ShowError("script:replacestr: Invalid count value. Expected int got string\n");
  13774. st->state = END;
  13775. return SCRIPT_CMD_FAILURE;
  13776. }
  13777. }
  13778. StringBuf_Init(&output);
  13779. for(; i < inputlen; i++) {
  13780. if(count && count == numFinds) { //found enough, stop looking
  13781. break;
  13782. }
  13783. for(f = 0; f <= findlen; f++) {
  13784. if(f == findlen) { //complete match
  13785. numFinds++;
  13786. StringBuf_AppendStr(&output, replace);
  13787. i += findlen - 1;
  13788. break;
  13789. } else {
  13790. if(usecase) {
  13791. if((i + f) > inputlen || input[i + f] != find[f]) {
  13792. StringBuf_Printf(&output, "%c", input[i]);
  13793. break;
  13794. }
  13795. } else {
  13796. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13797. StringBuf_Printf(&output, "%c", input[i]);
  13798. break;
  13799. }
  13800. }
  13801. }
  13802. }
  13803. }
  13804. //append excess after enough found
  13805. if(i < inputlen)
  13806. StringBuf_AppendStr(&output, &(input[i]));
  13807. script_pushstrcopy(st, StringBuf_Value(&output));
  13808. StringBuf_Destroy(&output);
  13809. return SCRIPT_CMD_SUCCESS;
  13810. }
  13811. //========================================================
  13812. // countstr <input>, <search>{, <usecase>}
  13813. //
  13814. // Note: Counts the number of times <search> occurs in
  13815. // <input>. By default will be case sensitive.
  13816. //--------------------------------------------------------
  13817. BUILDIN_FUNC(countstr)
  13818. {
  13819. const char *input = script_getstr(st, 2);
  13820. const char *find = script_getstr(st, 3);
  13821. size_t inputlen = strlen(input);
  13822. size_t findlen = strlen(find);
  13823. bool usecase = true;
  13824. int numFinds = 0;
  13825. int i = 0, f = 0;
  13826. if(findlen == 0) {
  13827. ShowError("script:countstr: Invalid search length.\n");
  13828. st->state = END;
  13829. return SCRIPT_CMD_FAILURE;
  13830. }
  13831. if(script_hasdata(st, 4)) {
  13832. struct script_data *data;
  13833. data = script_getdata(st, 4);
  13834. get_val(st, data); // Convert into value in case of a variable
  13835. if( !data_isstring(data) )
  13836. usecase = script_getnum(st, 4) != 0;
  13837. else {
  13838. ShowError("script:countstr: Invalid usecase value. Expected int got string\n");
  13839. st->state = END;
  13840. return SCRIPT_CMD_FAILURE;
  13841. }
  13842. }
  13843. for(; i < inputlen; i++) {
  13844. for(f = 0; f <= findlen; f++) {
  13845. if(f == findlen) { //complete match
  13846. numFinds++;
  13847. i += findlen - 1;
  13848. break;
  13849. } else {
  13850. if(usecase) {
  13851. if((i + f) > inputlen || input[i + f] != find[f]) {
  13852. break;
  13853. }
  13854. } else {
  13855. if(((i + f) > inputlen || input[i + f] != find[f]) && TOUPPER(input[i+f]) != TOUPPER(find[f])) {
  13856. break;
  13857. }
  13858. }
  13859. }
  13860. }
  13861. }
  13862. script_pushint(st, numFinds);
  13863. return SCRIPT_CMD_SUCCESS;
  13864. }
  13865. /// Changes the display name and/or display class of the npc.
  13866. /// Returns 0 is successful, 1 if the npc does not exist.
  13867. ///
  13868. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  13869. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  13870. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  13871. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  13872. BUILDIN_FUNC(setnpcdisplay)
  13873. {
  13874. const char* name;
  13875. const char* newname = NULL;
  13876. int class_ = -1, size = -1;
  13877. struct script_data* data;
  13878. struct npc_data* nd;
  13879. name = script_getstr(st,2);
  13880. data = script_getdata(st,3);
  13881. if( script_hasdata(st,4) )
  13882. class_ = script_getnum(st,4);
  13883. if( script_hasdata(st,5) )
  13884. size = script_getnum(st,5);
  13885. get_val(st, data);
  13886. if( data_isstring(data) )
  13887. newname = conv_str(st,data);
  13888. else if( data_isint(data) )
  13889. class_ = conv_num(st,data);
  13890. else
  13891. {
  13892. ShowError("script:setnpcdisplay: expected string or number\n");
  13893. script_reportdata(data);
  13894. return SCRIPT_CMD_FAILURE;
  13895. }
  13896. nd = npc_name2id(name);
  13897. if( nd == NULL )
  13898. {// not found
  13899. script_pushint(st,1);
  13900. return SCRIPT_CMD_SUCCESS;
  13901. }
  13902. // update npc
  13903. if( newname )
  13904. npc_setdisplayname(nd, newname);
  13905. if( size != -1 && size != (int)nd->size )
  13906. nd->size = size;
  13907. else
  13908. size = -1;
  13909. if( class_ != -1 && nd->class_ != class_ )
  13910. npc_setclass(nd, class_);
  13911. else if( size != -1 )
  13912. { // Required to update the visual size
  13913. clif_clearunit_area(&nd->bl, CLR_OUTSIGHT);
  13914. clif_spawn(&nd->bl);
  13915. }
  13916. script_pushint(st,0);
  13917. return SCRIPT_CMD_SUCCESS;
  13918. }
  13919. BUILDIN_FUNC(atoi)
  13920. {
  13921. const char *value;
  13922. value = script_getstr(st,2);
  13923. script_pushint(st,atoi(value));
  13924. return SCRIPT_CMD_SUCCESS;
  13925. }
  13926. BUILDIN_FUNC(axtoi)
  13927. {
  13928. const char *hex = script_getstr(st,2);
  13929. long value = strtol(hex, NULL, 16);
  13930. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13931. value = cap_value(value, INT_MIN, INT_MAX);
  13932. #endif
  13933. script_pushint(st, (int)value);
  13934. return SCRIPT_CMD_SUCCESS;
  13935. }
  13936. BUILDIN_FUNC(strtol)
  13937. {
  13938. const char *string = script_getstr(st, 2);
  13939. int base = script_getnum(st, 3);
  13940. long value = strtol(string, NULL, base);
  13941. #if LONG_MAX > INT_MAX || LONG_MIN < INT_MIN
  13942. value = cap_value(value, INT_MIN, INT_MAX);
  13943. #endif
  13944. script_pushint(st, (int)value);
  13945. return SCRIPT_CMD_SUCCESS;
  13946. }
  13947. // case-insensitive substring search [lordalfa]
  13948. BUILDIN_FUNC(compare)
  13949. {
  13950. const char *message;
  13951. const char *cmpstring;
  13952. message = script_getstr(st,2);
  13953. cmpstring = script_getstr(st,3);
  13954. script_pushint(st,(stristr(message,cmpstring) != NULL));
  13955. return SCRIPT_CMD_SUCCESS;
  13956. }
  13957. BUILDIN_FUNC(strcmp)
  13958. {
  13959. const char *str1;
  13960. const char *str2;
  13961. str1 = script_getstr(st,2);
  13962. str2 = script_getstr(st,3);
  13963. script_pushint(st,strcmp(str1, str2));
  13964. return SCRIPT_CMD_SUCCESS;
  13965. }
  13966. // [zBuffer] List of mathematics commands --->
  13967. BUILDIN_FUNC(sqrt)
  13968. {
  13969. double i, a;
  13970. i = script_getnum(st,2);
  13971. a = sqrt(i);
  13972. script_pushint(st,(int)a);
  13973. return SCRIPT_CMD_SUCCESS;
  13974. }
  13975. BUILDIN_FUNC(pow)
  13976. {
  13977. double i, a, b;
  13978. a = script_getnum(st,2);
  13979. b = script_getnum(st,3);
  13980. i = pow(a,b);
  13981. script_pushint(st,(int)i);
  13982. return SCRIPT_CMD_SUCCESS;
  13983. }
  13984. BUILDIN_FUNC(distance)
  13985. {
  13986. int x0, y0, x1, y1;
  13987. x0 = script_getnum(st,2);
  13988. y0 = script_getnum(st,3);
  13989. x1 = script_getnum(st,4);
  13990. y1 = script_getnum(st,5);
  13991. script_pushint(st,distance_xy(x0,y0,x1,y1));
  13992. return SCRIPT_CMD_SUCCESS;
  13993. }
  13994. // <--- [zBuffer] List of mathematics commands
  13995. BUILDIN_FUNC(md5)
  13996. {
  13997. const char *tmpstr;
  13998. char *md5str;
  13999. tmpstr = script_getstr(st,2);
  14000. md5str = (char *)aMalloc((32+1)*sizeof(char));
  14001. MD5_String(tmpstr, md5str);
  14002. script_pushstr(st, md5str);
  14003. return SCRIPT_CMD_SUCCESS;
  14004. }
  14005. // [zBuffer] List of dynamic var commands --->
  14006. /**
  14007. * setd "<variable name>",<value>{,<char_id>};
  14008. **/
  14009. BUILDIN_FUNC(setd)
  14010. {
  14011. TBL_PC *sd = NULL;
  14012. char varname[100];
  14013. const char *buffer;
  14014. int elem;
  14015. buffer = script_getstr(st, 2);
  14016. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14017. elem = 0;
  14018. if( not_server_variable(*varname) ) {
  14019. if (!script_charid2sd(4,sd))
  14020. return SCRIPT_CMD_FAILURE;
  14021. }
  14022. if( is_string_variable(varname) ) {
  14023. setd_sub(st, sd, varname, elem, (void *)script_getstr(st, 3), NULL);
  14024. } else {
  14025. setd_sub(st, sd, varname, elem, (void *)__64BPRTSIZE(script_getnum(st, 3)), NULL);
  14026. }
  14027. return SCRIPT_CMD_SUCCESS;
  14028. }
  14029. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  14030. {
  14031. int i, j;
  14032. TBL_PC* sd = NULL;
  14033. const char* query;
  14034. struct script_data* data;
  14035. const char* name;
  14036. unsigned int max_rows = SCRIPT_MAX_ARRAYSIZE; // maximum number of rows
  14037. int num_vars;
  14038. int num_cols;
  14039. // check target variables
  14040. for( i = 3; script_hasdata(st,i); ++i ) {
  14041. data = script_getdata(st, i);
  14042. if( data_isreference(data) ) { // it's a variable
  14043. name = reference_getname(data);
  14044. if( not_server_variable(*name) && sd == NULL ) { // requires a player
  14045. sd = script_rid2sd(st);
  14046. if( sd == NULL ) { // no player attached
  14047. script_reportdata(data);
  14048. st->state = END;
  14049. return SCRIPT_CMD_FAILURE;
  14050. }
  14051. }
  14052. } else {
  14053. ShowError("script:query_sql: not a variable\n");
  14054. script_reportdata(data);
  14055. st->state = END;
  14056. return SCRIPT_CMD_FAILURE;
  14057. }
  14058. }
  14059. num_vars = i - 3;
  14060. // Execute the query
  14061. query = script_getstr(st,2);
  14062. if( SQL_ERROR == Sql_QueryStr(handle, query) ) {
  14063. Sql_ShowDebug(handle);
  14064. script_pushint(st, -1);
  14065. return SCRIPT_CMD_FAILURE;
  14066. }
  14067. if( Sql_NumRows(handle) == 0 ) { // No data received
  14068. Sql_FreeResult(handle);
  14069. script_pushint(st, -1);
  14070. return SCRIPT_CMD_SUCCESS;
  14071. }
  14072. // Count the number of columns to store
  14073. num_cols = Sql_NumColumns(handle);
  14074. if( num_vars < num_cols ) {
  14075. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  14076. script_reportsrc(st);
  14077. } else if( num_vars > num_cols ) {
  14078. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  14079. script_reportsrc(st);
  14080. }
  14081. // Store data
  14082. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i ) {
  14083. for( j = 0; j < num_vars; ++j ) {
  14084. char* str = NULL;
  14085. if( j < num_cols )
  14086. Sql_GetData(handle, j, &str, NULL);
  14087. data = script_getdata(st, j+3);
  14088. name = reference_getname(data);
  14089. if( is_string_variable(name) )
  14090. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  14091. else
  14092. setd_sub(st, sd, name, i, (void *)__64BPRTSIZE((str?atoi(str):0)), reference_getref(data));
  14093. }
  14094. }
  14095. if( i == max_rows && max_rows < Sql_NumRows(handle) ) {
  14096. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  14097. script_reportsrc(st);
  14098. }
  14099. // Free data
  14100. Sql_FreeResult(handle);
  14101. script_pushint(st, i);
  14102. return SCRIPT_CMD_SUCCESS;
  14103. }
  14104. BUILDIN_FUNC(query_sql) {
  14105. #ifdef BETA_THREAD_TEST
  14106. if( st->state != RERUNLINE ) {
  14107. queryThread_add(st,false);
  14108. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14109. } else
  14110. st->state = RUN;
  14111. return SCRIPT_CMD_SUCCESS;
  14112. #else
  14113. return buildin_query_sql_sub(st, qsmysql_handle);
  14114. #endif
  14115. }
  14116. BUILDIN_FUNC(query_logsql) {
  14117. if( !log_config.sql_logs ) {// logmysql_handle == NULL
  14118. ShowWarning("buildin_query_logsql: SQL logs are disabled, query '%s' will not be executed.\n", script_getstr(st,2));
  14119. script_pushint(st,-1);
  14120. return SCRIPT_CMD_FAILURE;
  14121. }
  14122. #ifdef BETA_THREAD_TEST
  14123. if( st->state != RERUNLINE ) {
  14124. queryThread_add(st,true);
  14125. st->state = RERUNLINE;/* will continue when the query is finished running. */
  14126. } else
  14127. st->state = RUN;
  14128. return SCRIPT_CMD_SUCCESS;
  14129. #else
  14130. return buildin_query_sql_sub(st, logmysql_handle);
  14131. #endif
  14132. }
  14133. //Allows escaping of a given string.
  14134. BUILDIN_FUNC(escape_sql)
  14135. {
  14136. const char *str;
  14137. char *esc_str;
  14138. size_t len;
  14139. str = script_getstr(st,2);
  14140. len = strlen(str);
  14141. esc_str = (char*)aMalloc(len*2+1);
  14142. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  14143. script_pushstr(st, esc_str);
  14144. return SCRIPT_CMD_SUCCESS;
  14145. }
  14146. BUILDIN_FUNC(getd)
  14147. {
  14148. char varname[100];
  14149. const char *buffer;
  14150. int elem;
  14151. buffer = script_getstr(st, 2);
  14152. if(sscanf(buffer, "%99[^[][%11d]", varname, &elem) < 2)
  14153. elem = 0;
  14154. // Push the 'pointer' so it's more flexible [Lance]
  14155. push_val(st->stack, C_NAME, reference_uid(add_str(varname), elem));
  14156. return SCRIPT_CMD_SUCCESS;
  14157. }
  14158. BUILDIN_FUNC(callshop)
  14159. {
  14160. TBL_PC *sd = NULL;
  14161. struct npc_data *nd;
  14162. const char *shopname;
  14163. int flag = 0;
  14164. sd = script_rid2sd(st);
  14165. if (!sd) {
  14166. script_pushint(st,0);
  14167. return SCRIPT_CMD_SUCCESS;
  14168. }
  14169. shopname = script_getstr(st, 2);
  14170. if (script_hasdata(st,3))
  14171. flag = script_getnum(st,3);
  14172. nd = npc_name2id(shopname);
  14173. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP) ) {
  14174. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  14175. script_pushint(st,0);
  14176. return SCRIPT_CMD_FAILURE;
  14177. }
  14178. if (nd->subtype == NPCTYPE_SHOP || nd->subtype == NPCTYPE_ITEMSHOP || nd->subtype == NPCTYPE_POINTSHOP) {
  14179. // flag the user as using a valid script call for opening the shop (for floating NPCs)
  14180. sd->state.callshop = 1;
  14181. switch (flag) {
  14182. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  14183. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  14184. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  14185. }
  14186. }
  14187. #if PACKETVER >= 20131223
  14188. else if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14189. unsigned short i;
  14190. for (i = 0; i < nd->u.shop.count; i++) {
  14191. if (nd->u.shop.shop_item[i].qty)
  14192. break;
  14193. }
  14194. if (i == nd->u.shop.count) {
  14195. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 534));
  14196. return SCRIPT_CMD_FAILURE;
  14197. }
  14198. sd->npc_shopid = nd->bl.id;
  14199. clif_npc_market_open(sd, nd);
  14200. script_pushint(st,1);
  14201. return SCRIPT_CMD_SUCCESS;
  14202. }
  14203. #endif
  14204. else
  14205. clif_cashshop_show(sd, nd);
  14206. sd->npc_shopid = nd->bl.id;
  14207. script_pushint(st,1);
  14208. return SCRIPT_CMD_SUCCESS;
  14209. }
  14210. BUILDIN_FUNC(npcshopitem)
  14211. {
  14212. const char* npcname = script_getstr(st, 2);
  14213. struct npc_data* nd = npc_name2id(npcname);
  14214. int n, i;
  14215. int amount;
  14216. uint16 offs = 2;
  14217. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  14218. script_pushint(st,0);
  14219. return SCRIPT_CMD_SUCCESS;
  14220. }
  14221. #if PACKETVER >= 20131223
  14222. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14223. offs = 3;
  14224. npc_market_delfromsql_(nd->exname, 0, true);
  14225. }
  14226. #endif
  14227. // get the count of new entries
  14228. amount = (script_lastdata(st)-2)/offs;
  14229. // generate new shop item list
  14230. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  14231. for (n = 0, i = 3; n < amount; n++, i+=offs) {
  14232. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14233. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14234. #if PACKETVER >= 20131223
  14235. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14236. nd->u.shop.shop_item[n].qty = script_getnum(st,i+2);
  14237. nd->u.shop.shop_item[n].flag = 1;
  14238. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[n]);
  14239. }
  14240. #endif
  14241. }
  14242. nd->u.shop.count = n;
  14243. script_pushint(st,1);
  14244. return SCRIPT_CMD_SUCCESS;
  14245. }
  14246. BUILDIN_FUNC(npcshopadditem)
  14247. {
  14248. const char* npcname = script_getstr(st,2);
  14249. struct npc_data* nd = npc_name2id(npcname);
  14250. int n, i;
  14251. int amount;
  14252. uint16 offs = 2;
  14253. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14254. script_pushint(st,0);
  14255. return SCRIPT_CMD_SUCCESS;
  14256. }
  14257. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14258. offs = 3;
  14259. // get the count of new entries
  14260. amount = (script_lastdata(st)-2)/offs;
  14261. #if PACKETVER >= 20131223
  14262. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  14263. for (n = 0, i = 3; n < amount; n++, i += offs) {
  14264. uint16 nameid = script_getnum(st,i), j;
  14265. // Check existing entries
  14266. ARR_FIND(0, nd->u.shop.count, j, nd->u.shop.shop_item[j].nameid == nameid);
  14267. if (j == nd->u.shop.count) {
  14268. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+1);
  14269. j = nd->u.shop.count;
  14270. nd->u.shop.shop_item[j].flag = 1;
  14271. nd->u.shop.count++;
  14272. }
  14273. nd->u.shop.shop_item[j].nameid = nameid;
  14274. nd->u.shop.shop_item[j].value = script_getnum(st,i+1);
  14275. nd->u.shop.shop_item[j].qty = script_getnum(st,i+2);
  14276. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[j]);
  14277. }
  14278. script_pushint(st,1);
  14279. return SCRIPT_CMD_SUCCESS;
  14280. }
  14281. #endif
  14282. // append new items to existing shop item list
  14283. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  14284. for (n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=offs)
  14285. {
  14286. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  14287. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  14288. }
  14289. nd->u.shop.count = n;
  14290. script_pushint(st,1);
  14291. return SCRIPT_CMD_SUCCESS;
  14292. }
  14293. BUILDIN_FUNC(npcshopdelitem)
  14294. {
  14295. const char* npcname = script_getstr(st,2);
  14296. struct npc_data* nd = npc_name2id(npcname);
  14297. int n, i;
  14298. int amount;
  14299. int size;
  14300. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14301. script_pushint(st,0);
  14302. return SCRIPT_CMD_SUCCESS;
  14303. }
  14304. amount = script_lastdata(st)-2;
  14305. size = nd->u.shop.count;
  14306. // remove specified items from the shop item list
  14307. for( i = 3; i < 3 + amount; i++ ) {
  14308. unsigned short nameid = script_getnum(st,i);
  14309. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == nameid );
  14310. if( n < size ) {
  14311. if (n+1 != size)
  14312. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  14313. #if PACKETVER >= 20131223
  14314. if (nd->subtype == NPCTYPE_MARKETSHOP)
  14315. npc_market_delfromsql_(nd->exname, nameid, false);
  14316. #endif
  14317. size--;
  14318. }
  14319. }
  14320. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  14321. nd->u.shop.count = size;
  14322. script_pushint(st,1);
  14323. return SCRIPT_CMD_SUCCESS;
  14324. }
  14325. /**
  14326. * Sets a script to attach to a shop npc.
  14327. * npcshopattach "<npc_name>";
  14328. **/
  14329. BUILDIN_FUNC(npcshopattach)
  14330. {
  14331. const char* npcname = script_getstr(st,2);
  14332. struct npc_data* nd = npc_name2id(npcname);
  14333. int flag = 1;
  14334. if( script_hasdata(st,3) )
  14335. flag = script_getnum(st,3);
  14336. if (!nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP)) { // Not found.
  14337. script_pushint(st,0);
  14338. return SCRIPT_CMD_SUCCESS;
  14339. }
  14340. if (flag)
  14341. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  14342. else
  14343. nd->master_nd = NULL;
  14344. script_pushint(st,1);
  14345. return SCRIPT_CMD_SUCCESS;
  14346. }
  14347. /*==========================================
  14348. * Returns some values of an item [Lupus]
  14349. * Price, Weight, etc...
  14350. setitemscript(itemID,"{new item bonus script}",[n]);
  14351. Where n:
  14352. 0 - script
  14353. 1 - Equip script
  14354. 2 - Unequip script
  14355. *------------------------------------------*/
  14356. BUILDIN_FUNC(setitemscript)
  14357. {
  14358. unsigned short item_id;
  14359. int n = 0;
  14360. const char *script;
  14361. struct item_data *i_data;
  14362. struct script_code **dstscript;
  14363. item_id = script_getnum(st,2);
  14364. script = script_getstr(st,3);
  14365. if( script_hasdata(st,4) )
  14366. n=script_getnum(st,4);
  14367. i_data = itemdb_exists(item_id);
  14368. if (!i_data || script==NULL || ( script[0] && script[0]!='{' )) {
  14369. script_pushint(st,0);
  14370. return SCRIPT_CMD_SUCCESS;
  14371. }
  14372. switch (n) {
  14373. case 2:
  14374. dstscript = &i_data->unequip_script;
  14375. break;
  14376. case 1:
  14377. dstscript = &i_data->equip_script;
  14378. break;
  14379. default:
  14380. dstscript = &i_data->script;
  14381. break;
  14382. }
  14383. if(*dstscript)
  14384. script_free_code(*dstscript);
  14385. *dstscript = script[0] ? parse_script(script, "script_setitemscript", 0, 0) : NULL;
  14386. script_pushint(st,1);
  14387. return SCRIPT_CMD_SUCCESS;
  14388. }
  14389. /*=======================================================
  14390. * Add or Update a mob drop temporarily [Akinari]
  14391. * Original Idea By: [Lupus]
  14392. *
  14393. * addmonsterdrop <mob_id or name>,<item_id>,<rate>;
  14394. *
  14395. * If given an item the mob already drops, the rate
  14396. * is updated to the new rate. Rate cannot exceed 10000
  14397. * Returns 1 if succeeded (added/updated a mob drop)
  14398. *-------------------------------------------------------*/
  14399. BUILDIN_FUNC(addmonsterdrop)
  14400. {
  14401. struct mob_db *mob;
  14402. struct script_data *data;
  14403. unsigned short item_id;
  14404. int rate;
  14405. data = script_getdata(st, 2);
  14406. get_val(st, data); // Convert into value in case of a variable
  14407. if(data_isstring(data))
  14408. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14409. else
  14410. mob = mob_db(script_getnum(st,2));
  14411. item_id=script_getnum(st,3);
  14412. rate=script_getnum(st,4);
  14413. if(!itemdb_exists(item_id)){
  14414. ShowError("addmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14415. return SCRIPT_CMD_FAILURE;
  14416. }
  14417. if(mob) { //We got a valid monster, check for available drop slot
  14418. unsigned char i, c = 0;
  14419. for(i = 0; i < MAX_MOB_DROP; i++) {
  14420. if(mob->dropitem[i].nameid) {
  14421. if(mob->dropitem[i].nameid == item_id) { //If it equals item_id we update that drop
  14422. c = i;
  14423. break;
  14424. }
  14425. continue;
  14426. }
  14427. if(!c) //Accept first available slot only
  14428. c = i;
  14429. }
  14430. if(c) { //Fill in the slot with the item and rate
  14431. mob->dropitem[c].nameid = item_id;
  14432. mob->dropitem[c].p = (rate > 10000)?10000:rate;
  14433. script_pushint(st,1);
  14434. } else //No place to put the new drop
  14435. script_pushint(st,0);
  14436. } else {
  14437. ShowWarning("addmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14438. return SCRIPT_CMD_FAILURE;
  14439. }
  14440. return SCRIPT_CMD_SUCCESS;
  14441. }
  14442. /*=======================================================
  14443. * Delete a mob drop temporarily [Akinari]
  14444. * Original Idea By: [Lupus]
  14445. *
  14446. * delmonsterdrop <mob_id or name>,<item_id>;
  14447. *
  14448. * Returns 1 if succeeded (deleted a mob drop)
  14449. *-------------------------------------------------------*/
  14450. BUILDIN_FUNC(delmonsterdrop)
  14451. {
  14452. struct mob_db *mob;
  14453. struct script_data *data;
  14454. unsigned short item_id;
  14455. data = script_getdata(st, 2);
  14456. get_val(st, data); // Convert into value in case of a variable
  14457. if(data_isstring(data))
  14458. mob = mob_db(mobdb_searchname(script_getstr(st,2)));
  14459. else
  14460. mob = mob_db(script_getnum(st,2));
  14461. item_id=script_getnum(st,3);
  14462. if(!itemdb_exists(item_id)){
  14463. ShowError("delmonsterdrop: Nonexistant item %hu requested.\n", item_id );
  14464. return SCRIPT_CMD_FAILURE;
  14465. }
  14466. if(mob) { //We got a valid monster, check for item drop on monster
  14467. unsigned char i;
  14468. for(i = 0; i < MAX_MOB_DROP; i++) {
  14469. if(mob->dropitem[i].nameid == item_id) {
  14470. mob->dropitem[i].nameid = 0;
  14471. mob->dropitem[i].p = 0;
  14472. script_pushint(st,1);
  14473. return SCRIPT_CMD_SUCCESS;
  14474. }
  14475. }
  14476. //No drop on that monster
  14477. script_pushint(st,0);
  14478. } else {
  14479. ShowWarning("delmonsterdrop: bad mob id given %d\n",script_getnum(st,2));
  14480. return SCRIPT_CMD_FAILURE;
  14481. }
  14482. return SCRIPT_CMD_SUCCESS;
  14483. }
  14484. /*==========================================
  14485. * Returns some values of a monster [Lupus]
  14486. * Name, Level, race, size, etc...
  14487. getmonsterinfo(monsterID,queryIndex);
  14488. *------------------------------------------*/
  14489. BUILDIN_FUNC(getmonsterinfo)
  14490. {
  14491. struct mob_db *mob;
  14492. int mob_id;
  14493. mob_id = script_getnum(st,2);
  14494. if (!mobdb_checkid(mob_id)) {
  14495. //ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  14496. if ( script_getnum(st,3) == MOB_NAME ) // requested the name
  14497. script_pushconststr(st,"null");
  14498. else
  14499. script_pushint(st,-1);
  14500. return SCRIPT_CMD_SUCCESS;
  14501. }
  14502. mob = mob_db(mob_id);
  14503. switch ( script_getnum(st,3) ) {
  14504. case MOB_NAME: script_pushstrcopy(st,mob->jname); break;
  14505. case MOB_LV: script_pushint(st,mob->lv); break;
  14506. case MOB_MAXHP: script_pushint(st,mob->status.max_hp); break;
  14507. case MOB_BASEEXP: script_pushint(st,mob->base_exp); break;
  14508. case MOB_JOBEXP: script_pushint(st,mob->job_exp); break;
  14509. case MOB_ATK1: script_pushint(st,mob->status.rhw.atk); break;
  14510. case MOB_ATK2: script_pushint(st,mob->status.rhw.atk2); break;
  14511. case MOB_DEF: script_pushint(st,mob->status.def); break;
  14512. case MOB_MDEF: script_pushint(st,mob->status.mdef); break;
  14513. case MOB_STR: script_pushint(st,mob->status.str); break;
  14514. case MOB_AGI: script_pushint(st,mob->status.agi); break;
  14515. case MOB_VIT: script_pushint(st,mob->status.vit); break;
  14516. case MOB_INT: script_pushint(st,mob->status.int_); break;
  14517. case MOB_DEX: script_pushint(st,mob->status.dex); break;
  14518. case MOB_LUK: script_pushint(st,mob->status.luk); break;
  14519. case MOB_RANGE: script_pushint(st,mob->status.rhw.range); break;
  14520. case MOB_RANGE2: script_pushint(st,mob->range2); break;
  14521. case MOB_RANGE3: script_pushint(st,mob->range3); break;
  14522. case MOB_SIZE: script_pushint(st,mob->status.size); break;
  14523. case MOB_RACE: script_pushint(st,mob->status.race); break;
  14524. case MOB_ELEMENT: script_pushint(st,mob->status.def_ele); break;
  14525. case MOB_MODE: script_pushint(st,mob->status.mode); break;
  14526. case MOB_MVPEXP: script_pushint(st,mob->mexp); break;
  14527. default: script_pushint(st,-1); //wrong Index
  14528. }
  14529. return SCRIPT_CMD_SUCCESS;
  14530. }
  14531. /**
  14532. * Check player's vending/buyingstore/autotrade state
  14533. * checkvending({"<Player Name>"})
  14534. * @author [Nab4]
  14535. */
  14536. BUILDIN_FUNC(checkvending) {
  14537. TBL_PC *sd = NULL;
  14538. if (script_hasdata(st,2)) {
  14539. if (!(sd = map_nick2sd(script_getstr(st,2)))) {
  14540. ShowError("buildin_checkvending: Player '%s' is not online!\n", script_getstr(st,2));
  14541. return SCRIPT_CMD_FAILURE;
  14542. }
  14543. }
  14544. else
  14545. sd = script_rid2sd(st);
  14546. if (!sd) {
  14547. script_pushint(st,0);
  14548. return SCRIPT_CMD_SUCCESS;
  14549. }
  14550. else {
  14551. int8 ret = 0;
  14552. if (sd->state.vending)
  14553. ret = 1;
  14554. else if (sd->state.buyingstore)
  14555. ret = 4;
  14556. if (sd->state.autotrade)
  14557. ret |= 2;
  14558. script_pushint(st, ret);
  14559. }
  14560. return SCRIPT_CMD_SUCCESS;
  14561. }
  14562. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  14563. {
  14564. TBL_PC *sd = NULL;
  14565. if(script_hasdata(st,2))
  14566. sd = map_nick2sd(script_getstr(st,2));
  14567. else
  14568. sd = script_rid2sd(st);
  14569. if(sd)
  14570. script_pushint(st,(sd->chatID != 0));
  14571. else
  14572. script_pushint(st,0);
  14573. return SCRIPT_CMD_SUCCESS;
  14574. }
  14575. BUILDIN_FUNC(checkidle)
  14576. {
  14577. TBL_PC *sd = NULL;
  14578. if (script_hasdata(st, 2))
  14579. sd = map_nick2sd(script_getstr(st, 2));
  14580. else
  14581. sd = script_rid2sd(st);
  14582. if (sd)
  14583. script_pushint(st, DIFF_TICK(last_tick, sd->idletime));
  14584. else
  14585. script_pushint(st, 0);
  14586. return SCRIPT_CMD_SUCCESS;
  14587. }
  14588. BUILDIN_FUNC(searchitem)
  14589. {
  14590. struct script_data* data = script_getdata(st, 2);
  14591. const char *itemname = script_getstr(st,3);
  14592. struct item_data *items[MAX_SEARCH];
  14593. int count;
  14594. char* name;
  14595. int32 start;
  14596. int32 id;
  14597. int32 i;
  14598. TBL_PC* sd = NULL;
  14599. if ((items[0] = itemdb_exists(atoi(itemname))))
  14600. count = 1;
  14601. else {
  14602. count = itemdb_searchname_array(items, ARRAYLENGTH(items), itemname);
  14603. if (count > MAX_SEARCH) count = MAX_SEARCH;
  14604. }
  14605. if (!count) {
  14606. script_pushint(st, 0);
  14607. return SCRIPT_CMD_SUCCESS;
  14608. }
  14609. if( !data_isreference(data) )
  14610. {
  14611. ShowError("script:searchitem: not a variable\n");
  14612. script_reportdata(data);
  14613. st->state = END;
  14614. return SCRIPT_CMD_FAILURE;// not a variable
  14615. }
  14616. id = reference_getid(data);
  14617. start = reference_getindex(data);
  14618. name = reference_getname(data);
  14619. if( not_server_variable(*name) )
  14620. {
  14621. sd = script_rid2sd(st);
  14622. if( sd == NULL )
  14623. return SCRIPT_CMD_SUCCESS;// no player attached
  14624. }
  14625. if( is_string_variable(name) )
  14626. {// string array
  14627. ShowError("script:searchitem: not an integer array reference\n");
  14628. script_reportdata(data);
  14629. st->state = END;
  14630. return SCRIPT_CMD_FAILURE;// not supported
  14631. }
  14632. for( i = 0; i < count; ++start, ++i )
  14633. {// Set array
  14634. void* v = (void*)__64BPRTSIZE((int)items[i]->nameid);
  14635. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  14636. }
  14637. script_pushint(st, count);
  14638. return SCRIPT_CMD_SUCCESS;
  14639. }
  14640. // [zBuffer] List of player cont commands --->
  14641. BUILDIN_FUNC(rid2name)
  14642. {
  14643. struct block_list *bl = NULL;
  14644. int rid = script_getnum(st,2);
  14645. if((bl = map_id2bl(rid)))
  14646. {
  14647. switch(bl->type) {
  14648. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  14649. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  14650. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  14651. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  14652. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  14653. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  14654. default:
  14655. ShowError("buildin_rid2name: BL type unknown.\n");
  14656. script_pushconststr(st,"");
  14657. break;
  14658. }
  14659. } else {
  14660. ShowError("buildin_rid2name: invalid RID\n");
  14661. script_pushconststr(st,"(null)");
  14662. }
  14663. return SCRIPT_CMD_SUCCESS;
  14664. }
  14665. BUILDIN_FUNC(pcblockmove)
  14666. {
  14667. struct block_list *bl = NULL;
  14668. if (script_getnum(st, 2))
  14669. bl = map_id2bl(script_getnum(st,2));
  14670. else
  14671. bl = map_id2bl(st->rid);
  14672. if (bl) {
  14673. struct unit_data *ud = unit_bl2ud(bl);
  14674. if (ud)
  14675. ud->state.blockedmove = script_getnum(st,3) > 0;
  14676. }
  14677. return SCRIPT_CMD_SUCCESS;
  14678. }
  14679. BUILDIN_FUNC(pcfollow)
  14680. {
  14681. int id, targetid;
  14682. TBL_PC *sd = NULL;
  14683. id = script_getnum(st,2);
  14684. targetid = script_getnum(st,3);
  14685. if(id)
  14686. sd = map_id2sd(id);
  14687. else
  14688. sd = script_rid2sd(st);
  14689. if(sd)
  14690. pc_follow(sd, targetid);
  14691. return SCRIPT_CMD_SUCCESS;
  14692. }
  14693. BUILDIN_FUNC(pcstopfollow)
  14694. {
  14695. int id;
  14696. TBL_PC *sd = NULL;
  14697. id = script_getnum(st,2);
  14698. if(id)
  14699. sd = map_id2sd(id);
  14700. else
  14701. sd = script_rid2sd(st);
  14702. if(sd)
  14703. pc_stop_following(sd);
  14704. return SCRIPT_CMD_SUCCESS;
  14705. }
  14706. // <--- [zBuffer] List of player cont commands
  14707. // [zBuffer] List of unit control commands --->
  14708. /// Gets the type of the given Game ID.
  14709. ///
  14710. /// getunittype <unit id>;
  14711. BUILDIN_FUNC(getunittype)
  14712. {
  14713. struct block_list* bl;
  14714. uint8 value = 0;
  14715. bl = map_id2bl(script_getnum(st, 2));
  14716. if (!bl) {
  14717. ShowWarning("buildin_getunittype: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14718. script_pushint(st, -1);
  14719. return SCRIPT_CMD_FAILURE;
  14720. }
  14721. switch (bl->type) {
  14722. case BL_MOB: value = 0; break;
  14723. case BL_HOM: value = 1; break;
  14724. case BL_PET: value = 2; break;
  14725. case BL_MER: value = 3; break;
  14726. case BL_ELEM: value = 4; break;
  14727. case BL_NPC: value = 5; break;
  14728. }
  14729. script_pushint(st, value);
  14730. return SCRIPT_CMD_SUCCESS;
  14731. }
  14732. /// Gets specific live information of a bl.
  14733. ///
  14734. /// getunitdata <unit id>,<arrayname>;
  14735. BUILDIN_FUNC(getunitdata)
  14736. {
  14737. TBL_PC *sd = st->rid ? map_id2sd(st->rid) : NULL;
  14738. struct block_list* bl;
  14739. TBL_MOB* md = NULL;
  14740. TBL_HOM* hd = NULL;
  14741. TBL_MER* mc = NULL;
  14742. TBL_PET* pd = NULL;
  14743. TBL_ELEM* ed = NULL;
  14744. TBL_NPC* nd = NULL;
  14745. char* name;
  14746. struct script_data *data = script_getdata(st, 3);
  14747. if (!data_isreference(data)) {
  14748. ShowWarning("buildin_getunitdata: Error in argument! Please give a variable to store values in.\n");
  14749. return SCRIPT_CMD_FAILURE;
  14750. }
  14751. bl = map_id2bl(script_getnum(st, 2));
  14752. if (!bl) {
  14753. ShowWarning("buildin_getunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  14754. return SCRIPT_CMD_FAILURE;
  14755. }
  14756. switch (bl->type) {
  14757. case BL_MOB: md = map_id2md(bl->id); break;
  14758. case BL_HOM: hd = map_id2hd(bl->id); break;
  14759. case BL_PET: pd = map_id2pd(bl->id); break;
  14760. case BL_MER: mc = map_id2mc(bl->id); break;
  14761. case BL_ELEM: ed = map_id2ed(bl->id); break;
  14762. case BL_NPC: nd = map_id2nd(bl->id); break;
  14763. }
  14764. name = reference_getname(data);
  14765. #define getunitdata_sub(idx__,var__) setd_sub(st,sd,name,(idx__),(void *)__64BPRTSIZE((int)(var__)),data->ref)
  14766. switch(bl->type) {
  14767. case BL_MOB:
  14768. if (!md) {
  14769. ShowWarning("buildin_getunitdata: Error in finding object BL_MOB!\n");
  14770. return SCRIPT_CMD_FAILURE;
  14771. }
  14772. getunitdata_sub(UMOB_SIZE, md->status.size);
  14773. getunitdata_sub(UMOB_LEVEL, md->level);
  14774. getunitdata_sub(UMOB_HP, md->status.hp);
  14775. getunitdata_sub(UMOB_MAXHP, md->status.max_hp);
  14776. getunitdata_sub(UMOB_MASTERAID, md->master_id);
  14777. getunitdata_sub(UMOB_MAPID, md->bl.m);
  14778. getunitdata_sub(UMOB_X, md->bl.x);
  14779. getunitdata_sub(UMOB_Y, md->bl.y);
  14780. getunitdata_sub(UMOB_SPEED, md->status.speed);
  14781. getunitdata_sub(UMOB_MODE, md->status.mode);
  14782. getunitdata_sub(UMOB_AI, md->special_state.ai);
  14783. getunitdata_sub(UMOB_SCOPTION, md->sc.option);
  14784. getunitdata_sub(UMOB_SEX, md->vd->sex);
  14785. getunitdata_sub(UMOB_CLASS, md->vd->class_);
  14786. getunitdata_sub(UMOB_HAIRSTYLE, md->vd->hair_style);
  14787. getunitdata_sub(UMOB_HAIRCOLOR, md->vd->hair_color);
  14788. getunitdata_sub(UMOB_HEADBOTTOM, md->vd->head_bottom);
  14789. getunitdata_sub(UMOB_HEADMIDDLE, md->vd->head_mid);
  14790. getunitdata_sub(UMOB_HEADTOP, md->vd->head_top);
  14791. getunitdata_sub(UMOB_CLOTHCOLOR, md->vd->cloth_color);
  14792. getunitdata_sub(UMOB_SHIELD, md->vd->shield);
  14793. getunitdata_sub(UMOB_WEAPON, md->vd->weapon);
  14794. getunitdata_sub(UMOB_LOOKDIR, md->ud.dir);
  14795. getunitdata_sub(UMOB_STR, md->status.str);
  14796. getunitdata_sub(UMOB_AGI, md->status.agi);
  14797. getunitdata_sub(UMOB_VIT, md->status.vit);
  14798. getunitdata_sub(UMOB_INT, md->status.int_);
  14799. getunitdata_sub(UMOB_DEX, md->status.dex);
  14800. getunitdata_sub(UMOB_LUK, md->status.luk);
  14801. getunitdata_sub(UMOB_SLAVECPYMSTRMD, md->state.copy_master_mode);
  14802. getunitdata_sub(UMOB_DMGIMMUNE, md->ud.immune_attack);
  14803. getunitdata_sub(UMOB_ATKRANGE, md->status.rhw.range);
  14804. getunitdata_sub(UMOB_ATKMIN, md->status.rhw.atk);
  14805. getunitdata_sub(UMOB_ATKMAX, md->status.rhw.atk2);
  14806. getunitdata_sub(UMOB_MATKMIN, md->status.matk_min);
  14807. getunitdata_sub(UMOB_MATKMAX, md->status.matk_max);
  14808. getunitdata_sub(UMOB_DEF, md->status.def);
  14809. getunitdata_sub(UMOB_MDEF, md->status.mdef);
  14810. getunitdata_sub(UMOB_HIT, md->status.hit);
  14811. getunitdata_sub(UMOB_FLEE, md->status.flee);
  14812. getunitdata_sub(UMOB_PDODGE, md->status.flee2);
  14813. getunitdata_sub(UMOB_CRIT, md->status.cri);
  14814. getunitdata_sub(UMOB_RACE, md->status.race);
  14815. getunitdata_sub(UMOB_ELETYPE, md->status.def_ele);
  14816. getunitdata_sub(UMOB_ELELEVEL, md->status.ele_lv);
  14817. getunitdata_sub(UMOB_AMOTION, md->status.amotion);
  14818. getunitdata_sub(UMOB_ADELAY, md->status.adelay);
  14819. getunitdata_sub(UMOB_DMOTION, md->status.dmotion);
  14820. break;
  14821. case BL_HOM:
  14822. if (!hd) {
  14823. ShowWarning("buildin_getunitdata: Error in finding object BL_HOM!\n");
  14824. return SCRIPT_CMD_FAILURE;
  14825. }
  14826. getunitdata_sub(UHOM_SIZE, hd->base_status.size);
  14827. getunitdata_sub(UHOM_LEVEL, hd->homunculus.level);
  14828. getunitdata_sub(UHOM_HP, hd->homunculus.hp);
  14829. getunitdata_sub(UHOM_MAXHP, hd->homunculus.max_hp);
  14830. getunitdata_sub(UHOM_SP, hd->homunculus.sp);
  14831. getunitdata_sub(UHOM_MAXSP, hd->homunculus.max_sp);
  14832. getunitdata_sub(UHOM_MASTERCID, hd->homunculus.char_id);
  14833. getunitdata_sub(UHOM_MAPID, hd->bl.m);
  14834. getunitdata_sub(UHOM_X, hd->bl.x);
  14835. getunitdata_sub(UHOM_Y, hd->bl.y);
  14836. getunitdata_sub(UHOM_HUNGER, hd->homunculus.hunger);
  14837. getunitdata_sub(UHOM_INTIMACY, hd->homunculus.intimacy);
  14838. getunitdata_sub(UHOM_SPEED, hd->base_status.speed);
  14839. getunitdata_sub(UHOM_LOOKDIR, hd->ud.dir);
  14840. getunitdata_sub(UHOM_CANMOVETICK, hd->ud.canmove_tick);
  14841. getunitdata_sub(UHOM_STR, hd->base_status.str);
  14842. getunitdata_sub(UHOM_AGI, hd->base_status.agi);
  14843. getunitdata_sub(UHOM_VIT, hd->base_status.vit);
  14844. getunitdata_sub(UHOM_INT, hd->base_status.int_);
  14845. getunitdata_sub(UHOM_DEX, hd->base_status.dex);
  14846. getunitdata_sub(UHOM_LUK, hd->base_status.luk);
  14847. getunitdata_sub(UHOM_DMGIMMUNE, hd->ud.immune_attack);
  14848. getunitdata_sub(UHOM_ATKRANGE, hd->battle_status.rhw.range);
  14849. getunitdata_sub(UHOM_ATKMIN, hd->base_status.rhw.atk);
  14850. getunitdata_sub(UHOM_ATKMAX, hd->base_status.rhw.atk2);
  14851. getunitdata_sub(UHOM_MATKMIN, hd->base_status.matk_min);
  14852. getunitdata_sub(UHOM_MATKMAX, hd->base_status.matk_max);
  14853. getunitdata_sub(UHOM_DEF, hd->battle_status.def);
  14854. getunitdata_sub(UHOM_MDEF, hd->battle_status.mdef);
  14855. getunitdata_sub(UHOM_HIT, hd->battle_status.hit);
  14856. getunitdata_sub(UHOM_FLEE, hd->battle_status.flee);
  14857. getunitdata_sub(UHOM_PDODGE, hd->battle_status.flee2);
  14858. getunitdata_sub(UHOM_CRIT, hd->battle_status.cri);
  14859. getunitdata_sub(UHOM_RACE, hd->battle_status.race);
  14860. getunitdata_sub(UHOM_ELETYPE, hd->battle_status.def_ele);
  14861. getunitdata_sub(UHOM_ELELEVEL, hd->battle_status.ele_lv);
  14862. getunitdata_sub(UHOM_AMOTION, hd->battle_status.amotion);
  14863. getunitdata_sub(UHOM_ADELAY, hd->battle_status.adelay);
  14864. getunitdata_sub(UHOM_DMOTION, hd->battle_status.dmotion);
  14865. break;
  14866. case BL_PET:
  14867. if (!pd) {
  14868. ShowWarning("buildin_getunitdata: Error in finding object BL_PET!\n");
  14869. return SCRIPT_CMD_FAILURE;
  14870. }
  14871. getunitdata_sub(UPET_SIZE, pd->status.size);
  14872. getunitdata_sub(UPET_LEVEL, pd->pet.level);
  14873. getunitdata_sub(UPET_HP, pd->status.hp);
  14874. getunitdata_sub(UPET_MAXHP, pd->status.max_hp);
  14875. getunitdata_sub(UPET_MASTERAID, pd->pet.account_id);
  14876. getunitdata_sub(UPET_MAPID, pd->bl.m);
  14877. getunitdata_sub(UPET_X, pd->bl.x);
  14878. getunitdata_sub(UPET_Y, pd->bl.y);
  14879. getunitdata_sub(UPET_HUNGER, pd->pet.hungry);
  14880. getunitdata_sub(UPET_INTIMACY, pd->pet.intimate);
  14881. getunitdata_sub(UPET_SPEED, pd->status.speed);
  14882. getunitdata_sub(UPET_LOOKDIR, pd->ud.dir);
  14883. getunitdata_sub(UPET_CANMOVETICK, pd->ud.canmove_tick);
  14884. getunitdata_sub(UPET_STR, pd->status.str);
  14885. getunitdata_sub(UPET_AGI, pd->status.agi);
  14886. getunitdata_sub(UPET_VIT, pd->status.vit);
  14887. getunitdata_sub(UPET_INT, pd->status.int_);
  14888. getunitdata_sub(UPET_DEX, pd->status.dex);
  14889. getunitdata_sub(UPET_LUK, pd->status.luk);
  14890. getunitdata_sub(UPET_DMGIMMUNE, pd->ud.immune_attack);
  14891. getunitdata_sub(UPET_ATKRANGE, pd->status.rhw.range);
  14892. getunitdata_sub(UPET_ATKMIN, pd->status.rhw.atk);
  14893. getunitdata_sub(UPET_ATKMAX, pd->status.rhw.atk2);
  14894. getunitdata_sub(UPET_MATKMIN, pd->status.matk_min);
  14895. getunitdata_sub(UPET_MATKMAX, pd->status.matk_max);
  14896. getunitdata_sub(UPET_DEF, pd->status.def);
  14897. getunitdata_sub(UPET_MDEF, pd->status.mdef);
  14898. getunitdata_sub(UPET_HIT, pd->status.hit);
  14899. getunitdata_sub(UPET_FLEE, pd->status.flee);
  14900. getunitdata_sub(UPET_PDODGE, pd->status.flee2);
  14901. getunitdata_sub(UPET_CRIT, pd->status.cri);
  14902. getunitdata_sub(UPET_RACE, pd->status.race);
  14903. getunitdata_sub(UPET_ELETYPE, pd->status.def_ele);
  14904. getunitdata_sub(UPET_ELELEVEL, pd->status.ele_lv);
  14905. getunitdata_sub(UPET_AMOTION, pd->status.amotion);
  14906. getunitdata_sub(UPET_ADELAY, pd->status.adelay);
  14907. getunitdata_sub(UPET_DMOTION, pd->status.dmotion);
  14908. break;
  14909. case BL_MER:
  14910. if (!mc) {
  14911. ShowWarning("buildin_getunitdata: Error in finding object BL_MER!\n");
  14912. return SCRIPT_CMD_FAILURE;
  14913. }
  14914. getunitdata_sub(UMER_SIZE, mc->base_status.size);
  14915. getunitdata_sub(UMER_HP, mc->base_status.hp);
  14916. getunitdata_sub(UMER_MAXHP, mc->base_status.max_hp);
  14917. getunitdata_sub(UMER_MASTERCID, mc->mercenary.char_id);
  14918. getunitdata_sub(UMER_MAPID, mc->bl.m);
  14919. getunitdata_sub(UMER_X, mc->bl.x);
  14920. getunitdata_sub(UMER_Y, mc->bl.y);
  14921. getunitdata_sub(UMER_KILLCOUNT, mc->mercenary.kill_count);
  14922. getunitdata_sub(UMER_LIFETIME, mc->mercenary.life_time);
  14923. getunitdata_sub(UMER_SPEED, mc->base_status.speed);
  14924. getunitdata_sub(UMER_LOOKDIR, mc->ud.dir);
  14925. getunitdata_sub(UMER_CANMOVETICK, mc->ud.canmove_tick);
  14926. getunitdata_sub(UMER_STR, mc->base_status.str);
  14927. getunitdata_sub(UMER_AGI, mc->base_status.agi);
  14928. getunitdata_sub(UMER_VIT, mc->base_status.vit);
  14929. getunitdata_sub(UMER_INT, mc->base_status.int_);
  14930. getunitdata_sub(UMER_DEX, mc->base_status.dex);
  14931. getunitdata_sub(UMER_LUK, mc->base_status.luk);
  14932. getunitdata_sub(UMER_DMGIMMUNE, mc->ud.immune_attack);
  14933. getunitdata_sub(UMER_ATKRANGE, mc->base_status.rhw.range);
  14934. getunitdata_sub(UMER_ATKMIN, mc->base_status.rhw.atk);
  14935. getunitdata_sub(UMER_ATKMAX, mc->base_status.rhw.atk2);
  14936. getunitdata_sub(UMER_MATKMIN, mc->base_status.matk_min);
  14937. getunitdata_sub(UMER_MATKMAX, mc->base_status.matk_max);
  14938. getunitdata_sub(UMER_DEF, mc->base_status.def);
  14939. getunitdata_sub(UMER_MDEF, mc->base_status.mdef);
  14940. getunitdata_sub(UMER_HIT, mc->base_status.hit);
  14941. getunitdata_sub(UMER_FLEE, mc->base_status.flee);
  14942. getunitdata_sub(UMER_PDODGE, mc->base_status.flee2);
  14943. getunitdata_sub(UMER_CRIT, mc->base_status.cri);
  14944. getunitdata_sub(UMER_RACE, mc->base_status.race);
  14945. getunitdata_sub(UMER_ELETYPE, mc->base_status.def_ele);
  14946. getunitdata_sub(UMER_ELELEVEL, mc->base_status.ele_lv);
  14947. getunitdata_sub(UMER_AMOTION, mc->base_status.amotion);
  14948. getunitdata_sub(UMER_ADELAY, mc->base_status.adelay);
  14949. getunitdata_sub(UMER_DMOTION, mc->base_status.dmotion);
  14950. break;
  14951. case BL_ELEM:
  14952. if (!ed) {
  14953. ShowWarning("buildin_getunitdata: Error in finding object BL_ELEM!\n");
  14954. return SCRIPT_CMD_FAILURE;
  14955. }
  14956. getunitdata_sub(UELE_SIZE, ed->base_status.size);
  14957. getunitdata_sub(UELE_HP, ed->elemental.hp);
  14958. getunitdata_sub(UELE_MAXHP, ed->elemental.max_hp);
  14959. getunitdata_sub(UELE_SP, ed->elemental.sp);
  14960. getunitdata_sub(UELE_MAXSP, ed->elemental.max_sp);
  14961. getunitdata_sub(UELE_MASTERCID, ed->elemental.char_id);
  14962. getunitdata_sub(UELE_MAPID, ed->bl.m);
  14963. getunitdata_sub(UELE_X, ed->bl.x);
  14964. getunitdata_sub(UELE_Y, ed->bl.y);
  14965. getunitdata_sub(UELE_LIFETIME, ed->elemental.life_time);
  14966. getunitdata_sub(UELE_MODE, ed->elemental.mode);
  14967. getunitdata_sub(UELE_SP, ed->base_status.speed);
  14968. getunitdata_sub(UELE_LOOKDIR, ed->ud.dir);
  14969. getunitdata_sub(UELE_CANMOVETICK, ed->ud.canmove_tick);
  14970. getunitdata_sub(UELE_STR, ed->base_status.str);
  14971. getunitdata_sub(UELE_AGI, ed->base_status.agi);
  14972. getunitdata_sub(UELE_VIT, ed->base_status.vit);
  14973. getunitdata_sub(UELE_INT, ed->base_status.int_);
  14974. getunitdata_sub(UELE_DEX, ed->base_status.dex);
  14975. getunitdata_sub(UELE_LUK, ed->base_status.luk);
  14976. getunitdata_sub(UELE_DMGIMMUNE, ed->ud.immune_attack);
  14977. getunitdata_sub(UELE_ATKRANGE, ed->base_status.rhw.range);
  14978. getunitdata_sub(UELE_ATKMIN, ed->base_status.rhw.atk);
  14979. getunitdata_sub(UELE_ATKMAX, ed->base_status.rhw.atk2);
  14980. getunitdata_sub(UELE_MATKMIN, ed->base_status.matk_min);
  14981. getunitdata_sub(UELE_MATKMAX, ed->base_status.matk_max);
  14982. getunitdata_sub(UELE_DEF, ed->base_status.def);
  14983. getunitdata_sub(UELE_MDEF, ed->base_status.mdef);
  14984. getunitdata_sub(UELE_HIT, ed->base_status.hit);
  14985. getunitdata_sub(UELE_FLEE, ed->base_status.flee);
  14986. getunitdata_sub(UELE_PDODGE, ed->base_status.flee2);
  14987. getunitdata_sub(UELE_CRIT, ed->base_status.cri);
  14988. getunitdata_sub(UELE_RACE, ed->base_status.race);
  14989. getunitdata_sub(UELE_ELETYPE, ed->base_status.def_ele);
  14990. getunitdata_sub(UELE_ELELEVEL, ed->base_status.ele_lv);
  14991. getunitdata_sub(UELE_AMOTION, ed->base_status.amotion);
  14992. getunitdata_sub(UELE_ADELAY, ed->base_status.adelay);
  14993. getunitdata_sub(UELE_DMOTION, ed->base_status.dmotion);
  14994. break;
  14995. case BL_NPC:
  14996. if (!nd) {
  14997. ShowWarning("buildin_getunitdata: Error in finding object BL_NPC!\n");
  14998. return SCRIPT_CMD_FAILURE;
  14999. }
  15000. getunitdata_sub(UNPC_DISPLAY, nd->class_);
  15001. getunitdata_sub(UNPC_LEVEL, nd->level);
  15002. getunitdata_sub(UNPC_HP, nd->status.hp);
  15003. getunitdata_sub(UNPC_MAXHP, nd->status.max_hp);
  15004. getunitdata_sub(UNPC_MAPID, nd->bl.m);
  15005. getunitdata_sub(UNPC_X, nd->bl.x);
  15006. getunitdata_sub(UNPC_Y, nd->bl.y);
  15007. getunitdata_sub(UNPC_LOOKDIR, nd->ud.dir);
  15008. getunitdata_sub(UNPC_STR, nd->status.str);
  15009. getunitdata_sub(UNPC_AGI, nd->status.agi);
  15010. getunitdata_sub(UNPC_VIT, nd->status.vit);
  15011. getunitdata_sub(UNPC_INT, nd->status.int_);
  15012. getunitdata_sub(UNPC_DEX, nd->status.dex);
  15013. getunitdata_sub(UNPC_LUK, nd->status.luk);
  15014. getunitdata_sub(UNPC_PLUSALLSTAT, nd->stat_point);
  15015. getunitdata_sub(UNPC_DMGIMMUNE, nd->ud.immune_attack);
  15016. getunitdata_sub(UNPC_ATKRANGE, nd->status.rhw.range);
  15017. getunitdata_sub(UNPC_ATKMIN, nd->status.rhw.atk);
  15018. getunitdata_sub(UNPC_ATKMAX, nd->status.rhw.atk2);
  15019. getunitdata_sub(UNPC_MATKMIN, nd->status.matk_min);
  15020. getunitdata_sub(UNPC_MATKMAX, nd->status.matk_max);
  15021. getunitdata_sub(UNPC_DEF, nd->status.def);
  15022. getunitdata_sub(UNPC_MDEF, nd->status.mdef);
  15023. getunitdata_sub(UNPC_HIT, nd->status.hit);
  15024. getunitdata_sub(UNPC_FLEE, nd->status.flee);
  15025. getunitdata_sub(UNPC_PDODGE, nd->status.flee2);
  15026. getunitdata_sub(UNPC_CRIT, nd->status.cri);
  15027. getunitdata_sub(UNPC_RACE, nd->status.race);
  15028. getunitdata_sub(UNPC_ELETYPE, nd->status.def_ele);
  15029. getunitdata_sub(UNPC_ELELEVEL, nd->status.ele_lv);
  15030. getunitdata_sub(UNPC_AMOTION, nd->status.amotion);
  15031. getunitdata_sub(UNPC_ADELAY, nd->status.adelay);
  15032. getunitdata_sub(UNPC_DMOTION, nd->status.dmotion);
  15033. break;
  15034. default:
  15035. ShowWarning("buildin_getunitdata: Unknown object type!\n");
  15036. return SCRIPT_CMD_FAILURE;
  15037. }
  15038. #undef getunitdata_sub
  15039. return SCRIPT_CMD_SUCCESS;
  15040. }
  15041. /// Changes the live data of a bl.
  15042. ///
  15043. /// setunitdata <unit id>,<type>,<value>;
  15044. BUILDIN_FUNC(setunitdata)
  15045. {
  15046. struct block_list* bl = NULL;
  15047. struct script_data* data;
  15048. const char *mapname = NULL;
  15049. TBL_MOB* md = NULL;
  15050. TBL_HOM* hd = NULL;
  15051. TBL_MER* mc = NULL;
  15052. TBL_PET* pd = NULL;
  15053. TBL_ELEM* ed = NULL;
  15054. TBL_NPC* nd = NULL;
  15055. int type, value = 0;
  15056. bl = map_id2bl(script_getnum(st, 2));
  15057. if (!bl) {
  15058. ShowWarning("buildin_setunitdata: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15059. return SCRIPT_CMD_FAILURE;
  15060. }
  15061. switch (bl->type) {
  15062. case BL_MOB: md = map_id2md(bl->id); break;
  15063. case BL_HOM: hd = map_id2hd(bl->id); break;
  15064. case BL_PET: pd = map_id2pd(bl->id); break;
  15065. case BL_MER: mc = map_id2mc(bl->id); break;
  15066. case BL_ELEM: ed = map_id2ed(bl->id); break;
  15067. case BL_NPC:
  15068. nd = map_id2nd(bl->id);
  15069. if (!nd->status.hp)
  15070. status_calc_npc(nd, SCO_FIRST);
  15071. else
  15072. status_calc_npc(nd, SCO_NONE);
  15073. break;
  15074. default:
  15075. ShowError("buildin_setunitdata: Invalid object!");
  15076. return SCRIPT_CMD_FAILURE;
  15077. }
  15078. type = script_getnum(st, 3);
  15079. data = script_getdata(st, 4);
  15080. get_val(st, data);
  15081. if (type == 5 && data_isstring(data))
  15082. mapname = conv_str(st, data);
  15083. else if (data_isint(data))
  15084. value = conv_num(st, data);
  15085. else {
  15086. ShowError("buildin_setunitdata: Invalid data type for argument #3 (%d).", data->type);
  15087. return SCRIPT_CMD_FAILURE;
  15088. }
  15089. switch (bl->type) {
  15090. case BL_MOB:
  15091. if (!md) {
  15092. ShowWarning("buildin_setunitdata: Error in finding object BL_MOB!\n");
  15093. return SCRIPT_CMD_FAILURE;
  15094. }
  15095. switch (type) {
  15096. case UMOB_SIZE: md->status.size = (unsigned char)value; break;
  15097. case UMOB_LEVEL: md->level = (unsigned short)value; break;
  15098. case UMOB_HP: status_set_hp(bl, (unsigned int)value, 0); clif_charnameack(0, &md->bl); break;
  15099. case UMOB_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); clif_charnameack(0, &md->bl); break;
  15100. case UMOB_MASTERAID: md->master_id = value; break;
  15101. case UMOB_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15102. case UMOB_X: if (!unit_walktoxy(bl, (short)value, md->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15103. case UMOB_Y: if (!unit_walktoxy(bl, md->bl.x, (short)value, 2)) unit_movepos(bl, md->bl.x, (short)value, 0, 0); break;
  15104. case UMOB_SPEED: md->status.speed = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15105. case UMOB_MODE: md->status.mode = (enum e_mode)value; break;
  15106. case UMOB_AI: md->special_state.ai = (enum mob_ai)value; break;
  15107. case UMOB_SCOPTION: md->sc.option = (unsigned short)value; break;
  15108. case UMOB_SEX: md->vd->sex = (char)value; break;
  15109. case UMOB_CLASS: status_set_viewdata(bl, (unsigned short)value); break;
  15110. case UMOB_HAIRSTYLE: clif_changelook(bl, LOOK_HAIR, (unsigned short)value); break;
  15111. case UMOB_HAIRCOLOR: clif_changelook(bl, LOOK_HAIR_COLOR, (unsigned short)value); break;
  15112. case UMOB_HEADBOTTOM: clif_changelook(bl, LOOK_HEAD_BOTTOM, (unsigned short)value); break;
  15113. case UMOB_HEADMIDDLE: clif_changelook(bl, LOOK_HEAD_MID, (unsigned short)value); break;
  15114. case UMOB_HEADTOP: clif_changelook(bl, LOOK_HEAD_TOP, (unsigned short)value); break;
  15115. case UMOB_CLOTHCOLOR: clif_changelook(bl, LOOK_CLOTHES_COLOR, (unsigned short)value); break;
  15116. case UMOB_SHIELD: clif_changelook(bl, LOOK_SHIELD, (unsigned short)value); break;
  15117. case UMOB_WEAPON: clif_changelook(bl, LOOK_WEAPON, (unsigned short)value); break;
  15118. case UMOB_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15119. case UMOB_STR: md->status.str = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15120. case UMOB_AGI: md->status.agi = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15121. case UMOB_VIT: md->status.vit = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15122. case UMOB_INT: md->status.int_ = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15123. case UMOB_DEX: md->status.dex = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15124. case UMOB_LUK: md->status.luk = (unsigned short)value; status_calc_misc(bl, &md->status, md->level); break;
  15125. case UMOB_SLAVECPYMSTRMD:
  15126. if (value > 0) {
  15127. TBL_MOB *md2 = NULL;
  15128. if (!md->master_id || !(md2 = map_id2md(md->master_id))) {
  15129. ShowWarning("buildin_setunitdata: Trying to set UMOB_SLAVECPYMSTRMD on mob without master!\n");
  15130. break;
  15131. }
  15132. md->status.mode = md2->status.mode;
  15133. md->state.copy_master_mode = 1;
  15134. } else
  15135. md->state.copy_master_mode = 0;
  15136. break;
  15137. case UMOB_DMGIMMUNE: md->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15138. case UMOB_ATKRANGE: md->status.rhw.range = (unsigned short)value; break;
  15139. case UMOB_ATKMIN: md->status.rhw.atk = (unsigned short)value; break;
  15140. case UMOB_ATKMAX: md->status.rhw.atk2 = (unsigned short)value; break;
  15141. case UMOB_MATKMIN: md->status.matk_min = (unsigned short)value; break;
  15142. case UMOB_MATKMAX: md->status.matk_max = (unsigned short)value; break;
  15143. case UMOB_DEF: md->status.def = (defType)value; break;
  15144. case UMOB_MDEF: md->status.mdef = (defType)value; break;
  15145. case UMOB_HIT: md->status.hit = (short)value; break;
  15146. case UMOB_FLEE: md->status.flee = (short)value; break;
  15147. case UMOB_PDODGE: md->status.flee2 = (short)value; break;
  15148. case UMOB_CRIT: md->status.cri = (short)value; break;
  15149. case UMOB_RACE: md->status.race = (unsigned char)value; break;
  15150. case UMOB_ELETYPE: md->status.def_ele = (unsigned char)value; break;
  15151. case UMOB_ELELEVEL: md->status.ele_lv = (unsigned char)value; break;
  15152. case UMOB_AMOTION: md->status.amotion = (short)value; break;
  15153. case UMOB_ADELAY: md->status.adelay = (short)value; break;
  15154. case UMOB_DMOTION: md->status.dmotion = (short)value; break;
  15155. default:
  15156. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MOB.\n", type);
  15157. return SCRIPT_CMD_FAILURE;
  15158. }
  15159. break;
  15160. case BL_HOM:
  15161. if (!hd) {
  15162. ShowWarning("buildin_setunitdata: Error in finding object BL_HOM!\n");
  15163. return SCRIPT_CMD_FAILURE;
  15164. }
  15165. switch (type) {
  15166. case UHOM_SIZE: hd->base_status.size = (unsigned char)value; break;
  15167. case UHOM_LEVEL: hd->homunculus.level = (unsigned short)value; break;
  15168. case UHOM_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15169. case UHOM_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15170. case UHOM_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15171. case UHOM_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15172. case UHOM_MASTERCID: hd->homunculus.char_id = (uint32)value; break;
  15173. case UHOM_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15174. case UHOM_X: if (!unit_walktoxy(bl, (short)value, hd->bl.y, 2)) unit_movepos(bl, (short)value, hd->bl.y, 0, 0); break;
  15175. case UHOM_Y: if (!unit_walktoxy(bl, hd->bl.x, (short)value, 2)) unit_movepos(bl, hd->bl.x, (short)value, 0, 0); break;
  15176. case UHOM_HUNGER: hd->homunculus.hunger = (short)value; clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_HUNGRY, hd->homunculus.hunger); break;
  15177. case UHOM_INTIMACY: hom_increase_intimacy(hd, (unsigned int)value); clif_send_homdata(map_charid2sd(hd->homunculus.char_id), SP_INTIMATE, hd->homunculus.intimacy / 100); break;
  15178. case UHOM_SPEED: hd->base_status.speed = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15179. case UHOM_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15180. case UHOM_CANMOVETICK: hd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15181. case UHOM_STR: hd->base_status.str = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15182. case UHOM_AGI: hd->base_status.agi = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15183. case UHOM_VIT: hd->base_status.vit = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15184. case UHOM_INT: hd->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15185. case UHOM_DEX: hd->base_status.dex = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15186. case UHOM_LUK: hd->base_status.luk = (unsigned short)value; status_calc_misc(bl, &hd->base_status, hd->homunculus.level); break;
  15187. case UHOM_DMGIMMUNE: hd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15188. case UHOM_ATKRANGE: hd->base_status.rhw.range = (unsigned short)value; break;
  15189. case UHOM_ATKMIN: hd->base_status.rhw.atk = (unsigned short)value; break;
  15190. case UHOM_ATKMAX: hd->base_status.rhw.atk2 = (unsigned short)value; break;
  15191. case UHOM_MATKMIN: hd->base_status.matk_min = (unsigned short)value; break;
  15192. case UHOM_MATKMAX: hd->base_status.matk_max = (unsigned short)value; break;
  15193. case UHOM_DEF: hd->base_status.def = (defType)value; break;
  15194. case UHOM_MDEF: hd->base_status.mdef = (defType)value; break;
  15195. case UHOM_HIT: hd->base_status.hit = (short)value; break;
  15196. case UHOM_FLEE: hd->base_status.flee = (short)value; break;
  15197. case UHOM_PDODGE: hd->base_status.flee2 = (short)value; break;
  15198. case UHOM_CRIT: hd->base_status.cri = (short)value; break;
  15199. case UHOM_RACE: hd->base_status.race = (unsigned char)value; break;
  15200. case UHOM_ELETYPE: hd->base_status.def_ele = (unsigned char)value; break;
  15201. case UHOM_ELELEVEL: hd->base_status.ele_lv = (unsigned char)value; break;
  15202. case UHOM_AMOTION: hd->base_status.amotion = (short)value; break;
  15203. case UHOM_ADELAY: hd->base_status.adelay = (short)value; break;
  15204. case UHOM_DMOTION: hd->base_status.dmotion = (short)value; break;
  15205. default:
  15206. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_HOM.\n", type);
  15207. return SCRIPT_CMD_FAILURE;
  15208. }
  15209. break;
  15210. case BL_PET:
  15211. if (!pd) {
  15212. ShowWarning("buildin_setunitdata: Error in finding object BL_PET!\n");
  15213. return SCRIPT_CMD_FAILURE;
  15214. }
  15215. switch (type) {
  15216. case UPET_SIZE: pd->status.size = (unsigned char)value; break;
  15217. case UPET_LEVEL: pd->pet.level = (unsigned short)value; break;
  15218. case UPET_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15219. case UPET_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15220. case UPET_MASTERAID: pd->pet.account_id = (unsigned int)value; break;
  15221. case UPET_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15222. case UPET_X: if (!unit_walktoxy(bl, (short)value, pd->bl.y, 2)) unit_movepos(bl, (short)value, md->bl.y, 0, 0); break;
  15223. case UPET_Y: if (!unit_walktoxy(bl, pd->bl.x, (short)value, 2)) unit_movepos(bl, pd->bl.x, (short)value, 0, 0); break;
  15224. case UPET_HUNGER: pd->pet.hungry = (short)value; clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 2, pd->pet.hungry); break;
  15225. case UPET_INTIMACY: pet_set_intimate(pd, (unsigned int)value); clif_send_petdata(map_id2sd(pd->pet.account_id), pd, 1, pd->pet.intimate); break;
  15226. case UPET_SPEED: pd->status.speed = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15227. case UPET_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15228. case UPET_CANMOVETICK: pd->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15229. case UPET_STR: pd->status.str = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15230. case UPET_AGI: pd->status.agi = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15231. case UPET_VIT: pd->status.vit = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15232. case UPET_INT: pd->status.int_ = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15233. case UPET_DEX: pd->status.dex = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15234. case UPET_LUK: pd->status.luk = (unsigned short)value; status_calc_misc(bl, &pd->status, pd->pet.level); break;
  15235. case UPET_DMGIMMUNE: pd->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15236. case UPET_ATKRANGE: pd->status.rhw.range = (unsigned short)value; break;
  15237. case UPET_ATKMIN: pd->status.rhw.atk = (unsigned short)value; break;
  15238. case UPET_ATKMAX: pd->status.rhw.atk2 = (unsigned short)value; break;
  15239. case UPET_MATKMIN: pd->status.matk_min = (unsigned short)value; break;
  15240. case UPET_MATKMAX: pd->status.matk_max = (unsigned short)value; break;
  15241. case UPET_DEF: pd->status.def = (defType)value; break;
  15242. case UPET_MDEF: pd->status.mdef = (defType)value; break;
  15243. case UPET_HIT: pd->status.hit = (short)value; break;
  15244. case UPET_FLEE: pd->status.flee = (short)value; break;
  15245. case UPET_PDODGE: pd->status.flee2 = (short)value; break;
  15246. case UPET_CRIT: pd->status.cri = (short)value; break;
  15247. case UPET_RACE: pd->status.race = (unsigned char)value; break;
  15248. case UPET_ELETYPE: pd->status.def_ele = (unsigned char)value; break;
  15249. case UPET_ELELEVEL: pd->status.ele_lv = (unsigned char)value; break;
  15250. case UPET_AMOTION: pd->status.amotion = (short)value; break;
  15251. case UPET_ADELAY: pd->status.adelay = (short)value; break;
  15252. case UPET_DMOTION: pd->status.dmotion = (short)value; break;
  15253. default:
  15254. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_PET.\n", type);
  15255. return SCRIPT_CMD_FAILURE;
  15256. }
  15257. break;
  15258. case BL_MER:
  15259. if (!mc) {
  15260. ShowWarning("buildin_setunitdata: Error in finding object BL_MER!\n");
  15261. return SCRIPT_CMD_FAILURE;
  15262. }
  15263. switch (type) {
  15264. case UMER_SIZE: mc->base_status.size = (unsigned char)value; break;
  15265. case UMER_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15266. case UMER_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15267. case UMER_MASTERCID: mc->mercenary.char_id = (uint32)value; break;
  15268. case UMER_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15269. case UMER_X: if (!unit_walktoxy(bl, (short)value, mc->bl.y, 2)) unit_movepos(bl, (short)value, mc->bl.y, 0, 0); break;
  15270. case UMER_Y: if (!unit_walktoxy(bl, mc->bl.x, (short)value, 2)) unit_movepos(bl, mc->bl.x, (short)value, 0, 0); break;
  15271. case UMER_KILLCOUNT: mc->mercenary.kill_count = (unsigned int)value; break;
  15272. case UMER_LIFETIME: mc->mercenary.life_time = (unsigned int)value; break;
  15273. case UMER_SPEED: mc->base_status.speed = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15274. case UMER_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15275. case UMER_CANMOVETICK: mc->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15276. case UMER_STR: mc->base_status.str = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15277. case UMER_AGI: mc->base_status.agi = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15278. case UMER_VIT: mc->base_status.vit = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15279. case UMER_INT: mc->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15280. case UMER_DEX: mc->base_status.dex = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15281. case UMER_LUK: mc->base_status.luk = (unsigned short)value; status_calc_misc(bl, &mc->base_status, mc->db->lv); break;
  15282. case UMER_DMGIMMUNE: mc->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15283. case UMER_ATKRANGE: mc->base_status.rhw.range = (unsigned short)value; break;
  15284. case UMER_ATKMIN: mc->base_status.rhw.atk = (unsigned short)value; break;
  15285. case UMER_ATKMAX: mc->base_status.rhw.atk2 = (unsigned short)value; break;
  15286. case UMER_MATKMIN: mc->base_status.matk_min = (unsigned short)value; break;
  15287. case UMER_MATKMAX: mc->base_status.matk_max = (unsigned short)value; break;
  15288. case UMER_DEF: mc->base_status.def = (defType)value; break;
  15289. case UMER_MDEF: mc->base_status.mdef = (defType)value; break;
  15290. case UMER_HIT: mc->base_status.hit = (short)value; break;
  15291. case UMER_FLEE: mc->base_status.flee = (short)value; break;
  15292. case UMER_PDODGE: mc->base_status.flee2 = (short)value; break;
  15293. case UMER_CRIT: mc->base_status.cri = (short)value; break;
  15294. case UMER_RACE: mc->base_status.race = (unsigned char)value; break;
  15295. case UMER_ELETYPE: mc->base_status.def_ele = (unsigned char)value; break;
  15296. case UMER_ELELEVEL: mc->base_status.ele_lv = (unsigned char)value; break;
  15297. case UMER_AMOTION: mc->base_status.amotion = (short)value; break;
  15298. case UMER_ADELAY: mc->base_status.adelay = (short)value; break;
  15299. case UMER_DMOTION: mc->base_status.dmotion = (short)value; break;
  15300. default:
  15301. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_MER.\n", type);
  15302. return SCRIPT_CMD_FAILURE;
  15303. }
  15304. break;
  15305. case BL_ELEM:
  15306. if (!ed) {
  15307. ShowWarning("buildin_setunitdata: Error in finding object BL_ELEM!\n");
  15308. return SCRIPT_CMD_FAILURE;
  15309. }
  15310. switch (type) {
  15311. case UELE_SIZE: ed->base_status.size = (unsigned char)value; break;
  15312. case UELE_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15313. case UELE_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15314. case UELE_SP: status_set_sp(bl, (unsigned int)value, 0); break;
  15315. case UELE_MAXSP: status_set_maxsp(bl, (unsigned int)value, 0); break;
  15316. case UELE_MASTERCID: ed->elemental.char_id = (uint32)value; break;
  15317. case UELE_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15318. case UELE_X: if (!unit_walktoxy(bl, (short)value, ed->bl.y, 2)) unit_movepos(bl, (short)value, ed->bl.y, 0, 0); break;
  15319. case UELE_Y: if (!unit_walktoxy(bl, ed->bl.x, (short)value, 2)) unit_movepos(bl, ed->bl.x, (short)value, 0, 0); break;
  15320. case UELE_LIFETIME: ed->elemental.life_time = (unsigned int)value; break;
  15321. case UELE_MODE: ed->elemental.mode = (enum e_mode)value; break;
  15322. case UELE_SPEED: ed->base_status.speed = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15323. case UELE_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15324. case UELE_CANMOVETICK: ed->ud.canmove_tick = value > 0 ? (unsigned int)value : 0; break;
  15325. case UELE_STR: ed->base_status.str = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15326. case UELE_AGI: ed->base_status.agi = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15327. case UELE_VIT: ed->base_status.vit = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15328. case UELE_INT: ed->base_status.int_ = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15329. case UELE_DEX: ed->base_status.dex = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15330. case UELE_LUK: ed->base_status.luk = (unsigned short)value; status_calc_misc(bl, &ed->base_status, ed->db->lv); break;
  15331. case UELE_DMGIMMUNE: ed->ud.immune_attack = (bool)value > 0 ? 1 : 0; break;
  15332. case UELE_ATKRANGE: ed->base_status.rhw.range = (unsigned short)value; break;
  15333. case UELE_ATKMIN: ed->base_status.rhw.atk = (unsigned short)value; break;
  15334. case UELE_ATKMAX: ed->base_status.rhw.atk2 = (unsigned short)value; break;
  15335. case UELE_MATKMIN: ed->base_status.matk_min = (unsigned short)value; break;
  15336. case UELE_MATKMAX: ed->base_status.matk_max = (unsigned short)value; break;
  15337. case UELE_DEF: ed->base_status.def = (defType)value; break;
  15338. case UELE_MDEF: ed->base_status.mdef = (defType)value; break;
  15339. case UELE_HIT: ed->base_status.hit = (short)value; break;
  15340. case UELE_FLEE: ed->base_status.flee = (short)value; break;
  15341. case UELE_PDODGE: ed->base_status.flee2 = (short)value; break;
  15342. case UELE_CRIT: ed->base_status.cri = (short)value; break;
  15343. case UELE_RACE: ed->base_status.race = (unsigned char)value; break;
  15344. case UELE_ELETYPE: ed->base_status.def_ele = (unsigned char)value; break;
  15345. case UELE_ELELEVEL: ed->base_status.ele_lv = (unsigned char)value; break;
  15346. case UELE_AMOTION: ed->base_status.amotion = (short)value; break;
  15347. case UELE_ADELAY: ed->base_status.adelay = (short)value; break;
  15348. case UELE_DMOTION: ed->base_status.dmotion = (short)value; break;
  15349. default:
  15350. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_ELEM.\n", type);
  15351. return SCRIPT_CMD_FAILURE;
  15352. }
  15353. break;
  15354. case BL_NPC:
  15355. if (!nd) {
  15356. ShowWarning("buildin_setunitdata: Error in finding object BL_NPC!\n");
  15357. return SCRIPT_CMD_FAILURE;
  15358. }
  15359. switch (type) {
  15360. case UNPC_DISPLAY: status_set_viewdata(bl, (unsigned short)value); break;
  15361. case UNPC_LEVEL: nd->level = (unsigned int)value; break;
  15362. case UNPC_HP: status_set_hp(bl, (unsigned int)value, 0); break;
  15363. case UNPC_MAXHP: status_set_maxhp(bl, (unsigned int)value, 0); break;
  15364. case UNPC_MAPID: if (mapname) value = map_mapname2mapid(mapname); unit_warp(bl, (short)value, 0, 0, CLR_TELEPORT); break;
  15365. case UNPC_X: if (!unit_walktoxy(bl, (short)value, nd->bl.y, 2)) unit_movepos(bl, (short)value, nd->bl.x, 0, 0); break;
  15366. case UNPC_Y: if (!unit_walktoxy(bl, nd->bl.x, (short)value, 2)) unit_movepos(bl, nd->bl.x, (short)value, 0, 0); break;
  15367. case UNPC_LOOKDIR: unit_setdir(bl, (uint8)value); break;
  15368. case UNPC_STR: nd->params.str = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15369. case UNPC_AGI: nd->params.agi = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15370. case UNPC_VIT: nd->params.vit = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15371. case UNPC_INT: nd->params.int_ = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15372. case UNPC_DEX: nd->params.dex = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15373. case UNPC_LUK: nd->params.luk = (unsigned short)value; status_calc_misc(bl, &nd->status, nd->level); break;
  15374. case UNPC_PLUSALLSTAT: nd->stat_point = (unsigned int)value; break;
  15375. case UNPC_ATKRANGE: nd->status.rhw.range = (unsigned short)value; break;
  15376. case UNPC_ATKMIN: nd->status.rhw.atk = (unsigned short)value; break;
  15377. case UNPC_ATKMAX: nd->status.rhw.atk2 = (unsigned short)value; break;
  15378. case UNPC_MATKMIN: nd->status.matk_min = (unsigned short)value; break;
  15379. case UNPC_MATKMAX: nd->status.matk_max = (unsigned short)value; break;
  15380. case UNPC_DEF: nd->status.def = (defType)value; break;
  15381. case UNPC_MDEF: nd->status.mdef = (defType)value; break;
  15382. case UNPC_HIT: nd->status.hit = (short)value; break;
  15383. case UNPC_FLEE: nd->status.flee = (short)value; break;
  15384. case UNPC_PDODGE: nd->status.flee2 = (short)value; break;
  15385. case UNPC_CRIT: nd->status.cri = (short)value; break;
  15386. case UNPC_RACE: nd->status.race = (unsigned char)value; break;
  15387. case UNPC_ELETYPE: nd->status.def_ele = (unsigned char)value; break;
  15388. case UNPC_ELELEVEL: nd->status.ele_lv = (unsigned char)value; break;
  15389. case UNPC_AMOTION: nd->status.amotion = (short)value; break;
  15390. case UNPC_ADELAY: nd->status.adelay = (short)value; break;
  15391. case UNPC_DMOTION: nd->status.dmotion = (short)value; break;
  15392. default:
  15393. ShowError("buildin_setunitdata: Unknown data identifier %d for BL_NPC.\n", type);
  15394. return SCRIPT_CMD_FAILURE;
  15395. }
  15396. break;
  15397. default:
  15398. ShowWarning("buildin_setunitdata: Unknown object type!\n");
  15399. return SCRIPT_CMD_FAILURE;
  15400. }
  15401. // Client information updates
  15402. switch (bl->type) {
  15403. case BL_HOM:
  15404. clif_send_homdata(hd->master, SP_ACK, 0);
  15405. break;
  15406. case BL_PET:
  15407. clif_send_petstatus(pd->master);
  15408. break;
  15409. case BL_MER:
  15410. clif_mercenary_info(map_charid2sd(md->master_id));
  15411. clif_mercenary_skillblock(map_charid2sd(md->master_id));
  15412. break;
  15413. case BL_ELEM:
  15414. clif_elemental_info(ed->master);
  15415. break;
  15416. }
  15417. return SCRIPT_CMD_SUCCESS;
  15418. }
  15419. /// Gets the name of a bl.
  15420. /// Supported types are [MOB|HOM|PET|NPC].
  15421. /// MER and ELEM don't support custom names.
  15422. ///
  15423. /// getunitname <unit id>;
  15424. BUILDIN_FUNC(getunitname)
  15425. {
  15426. struct block_list* bl = NULL;
  15427. bl = map_id2bl(script_getnum(st, 2));
  15428. if (!bl) {
  15429. ShowWarning("buildin_getunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15430. return SCRIPT_CMD_FAILURE;
  15431. }
  15432. script_pushstrcopy(st, status_get_name(bl));
  15433. return SCRIPT_CMD_SUCCESS;
  15434. }
  15435. /// Changes the name of a bl.
  15436. /// Supported types are [MOB|HOM|PET].
  15437. /// For NPC see 'setnpcdisplay', MER and ELEM don't support custom names.
  15438. ///
  15439. /// setunitname <unit id>,<name>;
  15440. BUILDIN_FUNC(setunitname)
  15441. {
  15442. struct block_list* bl = NULL;
  15443. TBL_MOB* md = NULL;
  15444. TBL_HOM* hd = NULL;
  15445. TBL_PET* pd = NULL;
  15446. bl = map_id2bl(script_getnum(st, 2));
  15447. if (!bl) {
  15448. ShowWarning("buildin_setunitname: Error in finding object with given game ID %d!\n", script_getnum(st, 2));
  15449. return SCRIPT_CMD_FAILURE;
  15450. }
  15451. switch (bl->type) {
  15452. case BL_MOB: md = map_id2md(bl->id); break;
  15453. case BL_HOM: hd = map_id2hd(bl->id); break;
  15454. case BL_PET: pd = map_id2pd(bl->id); break;
  15455. }
  15456. switch (bl->type) {
  15457. case BL_MOB:
  15458. if (!md) {
  15459. ShowWarning("buildin_setunitname: Error in finding object BL_MOB!\n");
  15460. return SCRIPT_CMD_FAILURE;
  15461. }
  15462. safestrncpy(md->name, script_getstr(st, 3), NAME_LENGTH);
  15463. break;
  15464. case BL_HOM:
  15465. if (!hd) {
  15466. ShowWarning("buildin_setunitname: Error in finding object BL_HOM!\n");
  15467. return SCRIPT_CMD_FAILURE;
  15468. }
  15469. safestrncpy(hd->homunculus.name, script_getstr(st, 3), NAME_LENGTH);
  15470. break;
  15471. case BL_PET:
  15472. if (!pd) {
  15473. ShowWarning("buildin_setunitname: Error in finding object BL_PET!\n");
  15474. return SCRIPT_CMD_FAILURE;
  15475. }
  15476. safestrncpy(pd->pet.name, script_getstr(st, 3), NAME_LENGTH);
  15477. break;
  15478. default:
  15479. ShowWarning("buildin_setunitname: Unknown object type!\n");
  15480. return SCRIPT_CMD_FAILURE;
  15481. }
  15482. clif_charnameack(0, bl); // Send update to client.
  15483. return SCRIPT_CMD_SUCCESS;
  15484. }
  15485. /// Makes the unit walk to target position or map.
  15486. /// Returns if it was successful.
  15487. ///
  15488. /// unitwalk(<unit_id>,<x>,<y>{,<event_label>}) -> <bool>
  15489. /// unitwalkto(<unit_id>,<target_id>{,<event_label>}) -> <bool>
  15490. BUILDIN_FUNC(unitwalk)
  15491. {
  15492. struct block_list* bl;
  15493. struct unit_data *ud = NULL;
  15494. const char *cmd = script_getfuncname(st), *done_label = "";
  15495. uint8 off = 5;
  15496. bl = map_id2bl(script_getnum(st,2));
  15497. if (!bl) {
  15498. ShowError("buildin_unitwalk: Invalid unit with ID '%d'.\n", script_getnum(st,2));
  15499. script_pushint(st, 0);
  15500. return SCRIPT_CMD_FAILURE;
  15501. }
  15502. ud = unit_bl2ud(bl);
  15503. if (!strcmp(cmd,"unitwalk")) {
  15504. int x = script_getnum(st,3);
  15505. int y = script_getnum(st,4);
  15506. if (script_pushint(st, unit_can_reach_pos(bl,x,y,0)))
  15507. add_timer(gettick()+50, unit_delay_walktoxy_timer, bl->id, (x<<16)|(y&0xFFFF)); // Need timer to avoid mismatches
  15508. } else {
  15509. struct block_list* tbl = map_id2bl(script_getnum(st,3));
  15510. if (!tbl) {
  15511. ShowError("buildin_unitwalk: Bad target destination.\n");
  15512. script_pushint(st, 0);
  15513. return SCRIPT_CMD_FAILURE;
  15514. } else if (script_pushint(st, unit_can_reach_bl(bl, tbl, distance_bl(bl, tbl)+1, 0, NULL, NULL)))
  15515. add_timer(gettick()+50, unit_delay_walktobl_timer, bl->id, tbl->id); // Need timer to avoid mismatches
  15516. off = 4;
  15517. }
  15518. if (ud && script_hasdata(st, off)) {
  15519. done_label = script_getstr(st, off);
  15520. check_event(st, done_label);
  15521. safestrncpy(ud->walk_done_event, done_label, sizeof(ud->walk_done_event));
  15522. }
  15523. return SCRIPT_CMD_SUCCESS;
  15524. }
  15525. /// Kills the unit.
  15526. ///
  15527. /// unitkill <unit_id>;
  15528. BUILDIN_FUNC(unitkill)
  15529. {
  15530. struct block_list* bl = map_id2bl(script_getnum(st,2));
  15531. if (bl != NULL)
  15532. status_kill(bl);
  15533. return SCRIPT_CMD_SUCCESS;
  15534. }
  15535. /// Warps the unit to the target position in the target map.
  15536. /// Returns if it was successful.
  15537. ///
  15538. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  15539. BUILDIN_FUNC(unitwarp)
  15540. {
  15541. int unit_id;
  15542. int map_idx;
  15543. short x;
  15544. short y;
  15545. struct block_list* bl;
  15546. const char *mapname;
  15547. unit_id = script_getnum(st,2);
  15548. mapname = script_getstr(st, 3);
  15549. x = (short)script_getnum(st,4);
  15550. y = (short)script_getnum(st,5);
  15551. if (!unit_id) //Warp the script's runner
  15552. bl = map_id2bl(st->rid);
  15553. else
  15554. bl = map_id2bl(unit_id);
  15555. if (!strcmp(mapname,"this"))
  15556. map_idx = bl?bl->m:-1;
  15557. else
  15558. map_idx = map_mapname2mapid(mapname);
  15559. if (map_idx >= 0 && bl != NULL)
  15560. script_pushint(st, unit_warp(bl,map_idx,x,y,CLR_OUTSIGHT));
  15561. else
  15562. script_pushint(st, 0);
  15563. return SCRIPT_CMD_SUCCESS;
  15564. }
  15565. /// Makes the unit attack the target.
  15566. /// If the unit is a player and <action type> is not 0, it does a continuous
  15567. /// attack instead of a single attack.
  15568. /// Returns if the request was successful.
  15569. ///
  15570. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  15571. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  15572. BUILDIN_FUNC(unitattack)
  15573. {
  15574. struct block_list* unit_bl;
  15575. struct block_list* target_bl = NULL;
  15576. struct script_data* data;
  15577. int actiontype = 0;
  15578. unit_bl = map_id2bl(script_getnum(st,2));
  15579. if (!unit_bl) {
  15580. script_pushint(st, 0);
  15581. return SCRIPT_CMD_FAILURE;
  15582. }
  15583. data = script_getdata(st, 3);
  15584. get_val(st, data);
  15585. if (data_isstring(data)) {
  15586. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  15587. if( sd != NULL )
  15588. target_bl = &sd->bl;
  15589. } else
  15590. target_bl = map_id2bl(conv_num(st, data));
  15591. if (!target_bl) {
  15592. script_pushint(st, 0);
  15593. return SCRIPT_CMD_FAILURE;
  15594. }
  15595. if (script_hasdata(st,4))
  15596. actiontype = script_getnum(st,4);
  15597. switch(unit_bl->type) {
  15598. case BL_PC:
  15599. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  15600. script_pushint(st, 1);
  15601. return SCRIPT_CMD_SUCCESS;
  15602. case BL_MOB:
  15603. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  15604. break;
  15605. case BL_PET:
  15606. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  15607. break;
  15608. default:
  15609. ShowError("buildin_unitattack: Unsupported source unit type %d.\n", unit_bl->type);
  15610. script_pushint(st, 0);
  15611. return SCRIPT_CMD_FAILURE;
  15612. }
  15613. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  15614. return SCRIPT_CMD_SUCCESS;
  15615. }
  15616. /// Makes the unit stop attacking.
  15617. ///
  15618. /// unitstopattack <unit_id>;
  15619. BUILDIN_FUNC(unitstopattack)
  15620. {
  15621. int unit_id;
  15622. struct block_list* bl;
  15623. unit_id = script_getnum(st,2);
  15624. bl = map_id2bl(unit_id);
  15625. if (bl != NULL) {
  15626. unit_stop_attack(bl);
  15627. if (bl->type == BL_MOB)
  15628. ((TBL_MOB*)bl)->target_id = 0;
  15629. }
  15630. return SCRIPT_CMD_SUCCESS;
  15631. }
  15632. /// Makes the unit stop walking.
  15633. ///
  15634. /// unitstopwalk <unit_id>;
  15635. BUILDIN_FUNC(unitstopwalk)
  15636. {
  15637. int unit_id;
  15638. struct block_list* bl;
  15639. unit_id = script_getnum(st,2);
  15640. bl = map_id2bl(unit_id);
  15641. if (bl != NULL)
  15642. unit_stop_walking(bl, 0);
  15643. return SCRIPT_CMD_SUCCESS;
  15644. }
  15645. /// Makes the unit say the given message.
  15646. ///
  15647. /// unittalk <unit_id>,"<message>";
  15648. BUILDIN_FUNC(unittalk)
  15649. {
  15650. int unit_id;
  15651. const char* message;
  15652. struct block_list* bl;
  15653. unit_id = script_getnum(st,2);
  15654. message = script_getstr(st, 3);
  15655. bl = map_id2bl(unit_id);
  15656. if (bl != NULL) {
  15657. struct StringBuf sbuf;
  15658. StringBuf_Init(&sbuf);
  15659. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  15660. clif_disp_overhead(bl, StringBuf_Value(&sbuf));
  15661. StringBuf_Destroy(&sbuf);
  15662. }
  15663. return SCRIPT_CMD_SUCCESS;
  15664. }
  15665. /// Makes the unit do an emotion.
  15666. ///
  15667. /// unitemote <unit_id>,<emotion>;
  15668. ///
  15669. /// @see e_* in db/const.txt
  15670. BUILDIN_FUNC(unitemote)
  15671. {
  15672. int unit_id;
  15673. int emotion;
  15674. struct block_list* bl;
  15675. unit_id = script_getnum(st,2);
  15676. emotion = script_getnum(st,3);
  15677. bl = map_id2bl(unit_id);
  15678. if (bl != NULL)
  15679. clif_emotion(bl, emotion);
  15680. return SCRIPT_CMD_SUCCESS;
  15681. }
  15682. /// Makes the unit cast the skill on the target or self if no target is specified.
  15683. ///
  15684. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>,<casttime>};
  15685. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>,<casttime>};
  15686. BUILDIN_FUNC(unitskilluseid)
  15687. {
  15688. int unit_id, target_id, casttime;
  15689. uint16 skill_id, skill_lv;
  15690. struct block_list* bl;
  15691. struct script_data *data;
  15692. unit_id = script_getnum(st,2);
  15693. data = script_getdata(st, 3);
  15694. get_val(st, data); // Convert into value in case of a variable
  15695. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15696. skill_lv = script_getnum(st,4);
  15697. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  15698. casttime = ( script_hasdata(st,6) ? script_getnum(st,6) : 0 );
  15699. bl = map_id2bl(unit_id);
  15700. if (bl != NULL) {
  15701. if (bl->type == BL_NPC) {
  15702. if (!((TBL_NPC*)bl)->status.hp)
  15703. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15704. else
  15705. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15706. }
  15707. unit_skilluse_id2(bl, target_id, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15708. }
  15709. return SCRIPT_CMD_SUCCESS;
  15710. }
  15711. /// Makes the unit cast the skill on the target position.
  15712. ///
  15713. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15714. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>{,<casttime>};
  15715. BUILDIN_FUNC(unitskillusepos)
  15716. {
  15717. int unit_id, skill_x, skill_y, casttime;
  15718. uint16 skill_id, skill_lv;
  15719. struct block_list* bl;
  15720. struct script_data *data;
  15721. unit_id = script_getnum(st,2);
  15722. data = script_getdata(st, 3);
  15723. get_val(st, data); // Convert into value in case of a variable
  15724. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  15725. skill_lv = script_getnum(st,4);
  15726. skill_x = script_getnum(st,5);
  15727. skill_y = script_getnum(st,6);
  15728. casttime = ( script_hasdata(st,7) ? script_getnum(st,7) : 0 );
  15729. bl = map_id2bl(unit_id);
  15730. if (bl != NULL) {
  15731. if (bl->type == BL_NPC) {
  15732. if (!((TBL_NPC*)bl)->status.hp)
  15733. status_calc_npc(((TBL_NPC*)bl), SCO_FIRST);
  15734. else
  15735. status_calc_npc(((TBL_NPC*)bl), SCO_NONE);
  15736. }
  15737. unit_skilluse_pos2(bl, skill_x, skill_y, skill_id, skill_lv, (casttime * 1000) + skill_castfix(bl, skill_id, skill_lv), skill_get_castcancel(skill_id));
  15738. }
  15739. return SCRIPT_CMD_SUCCESS;
  15740. }
  15741. // <--- [zBuffer] List of unit control commands
  15742. /// Pauses the execution of the script, detaching the player
  15743. ///
  15744. /// sleep <mili seconds>;
  15745. BUILDIN_FUNC(sleep)
  15746. {
  15747. int ticks;
  15748. ticks = script_getnum(st,2);
  15749. // detach the player
  15750. script_detach_rid(st);
  15751. if( ticks <= 0 )
  15752. {// do nothing
  15753. }
  15754. else if( st->sleep.tick == 0 )
  15755. {// sleep for the target amount of time
  15756. st->state = RERUNLINE;
  15757. st->sleep.tick = ticks;
  15758. }
  15759. else
  15760. {// sleep time is over
  15761. st->state = RUN;
  15762. st->sleep.tick = 0;
  15763. }
  15764. return SCRIPT_CMD_SUCCESS;
  15765. }
  15766. /// Pauses the execution of the script, keeping the player attached
  15767. /// Returns if a player is still attached
  15768. ///
  15769. /// sleep2(<mili secconds>) -> <bool>
  15770. BUILDIN_FUNC(sleep2)
  15771. {
  15772. int ticks;
  15773. ticks = script_getnum(st,2);
  15774. if( ticks <= 0 )
  15775. {// do nothing
  15776. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15777. }
  15778. else if( !st->sleep.tick )
  15779. {// sleep for the target amount of time
  15780. st->state = RERUNLINE;
  15781. st->sleep.tick = ticks;
  15782. }
  15783. else
  15784. {// sleep time is over
  15785. st->state = RUN;
  15786. st->sleep.tick = 0;
  15787. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  15788. }
  15789. return SCRIPT_CMD_SUCCESS;
  15790. }
  15791. /// Awakes all the sleep timers of the target npc
  15792. ///
  15793. /// awake "<npc name>";
  15794. BUILDIN_FUNC(awake)
  15795. {
  15796. DBIterator *iter;
  15797. struct script_state *tst;
  15798. struct npc_data* nd;
  15799. if ((nd = npc_name2id(script_getstr(st, 2))) == NULL) {
  15800. ShowError("buildin_awake: NPC \"%s\" not found\n", script_getstr(st, 2));
  15801. return SCRIPT_CMD_FAILURE;
  15802. }
  15803. iter = db_iterator(st_db);
  15804. for (tst = dbi_first(iter); dbi_exists(iter); tst = dbi_next(iter)) {
  15805. if (tst->oid == nd->bl.id) {
  15806. TBL_PC* sd = map_id2sd(tst->rid);
  15807. if (tst->sleep.timer == INVALID_TIMER) { // already awake ???
  15808. continue;
  15809. }
  15810. if ((sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd)) {
  15811. // char not online anymore / another char of the same account is online - Cancel execution
  15812. tst->state = END;
  15813. tst->rid = 0;
  15814. }
  15815. delete_timer(tst->sleep.timer, run_script_timer);
  15816. tst->sleep.timer = INVALID_TIMER;
  15817. if (tst->state != RERUNLINE)
  15818. tst->sleep.tick = 0;
  15819. run_script_main(tst);
  15820. }
  15821. }
  15822. dbi_destroy(iter);
  15823. return SCRIPT_CMD_SUCCESS;
  15824. }
  15825. /// Returns a reference to a variable of the target NPC.
  15826. /// Returns 0 if an error occurs.
  15827. ///
  15828. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  15829. BUILDIN_FUNC(getvariableofnpc)
  15830. {
  15831. struct script_data* data;
  15832. const char* name;
  15833. struct npc_data* nd;
  15834. data = script_getdata(st,2);
  15835. if( !data_isreference(data) )
  15836. {// Not a reference (aka varaible name)
  15837. ShowError("buildin_getvariableofnpc: not a variable\n");
  15838. script_reportdata(data);
  15839. script_pushnil(st);
  15840. st->state = END;
  15841. return SCRIPT_CMD_FAILURE;
  15842. }
  15843. name = reference_getname(data);
  15844. if( *name != '.' || name[1] == '@' )
  15845. {// not a npc variable
  15846. ShowError("buildin_getvariableofnpc: invalid scope (not npc variable)\n");
  15847. script_reportdata(data);
  15848. script_pushnil(st);
  15849. st->state = END;
  15850. return SCRIPT_CMD_FAILURE;
  15851. }
  15852. nd = npc_name2id(script_getstr(st,3));
  15853. if( nd == NULL || nd->subtype != NPCTYPE_SCRIPT || nd->u.scr.script == NULL )
  15854. {// NPC not found or has no script
  15855. ShowError("buildin_getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  15856. script_pushnil(st);
  15857. st->state = END;
  15858. return SCRIPT_CMD_FAILURE;
  15859. }
  15860. if (!nd->u.scr.script->local.vars)
  15861. nd->u.scr.script->local.vars = i64db_alloc(DB_OPT_RELEASE_DATA);
  15862. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->local);
  15863. return SCRIPT_CMD_SUCCESS;
  15864. }
  15865. /// Opens a warp portal.
  15866. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  15867. ///
  15868. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  15869. ///
  15870. /// @author blackhole89
  15871. BUILDIN_FUNC(warpportal)
  15872. {
  15873. int spx;
  15874. int spy;
  15875. unsigned short mapindex;
  15876. int tpx;
  15877. int tpy;
  15878. struct skill_unit_group* group;
  15879. struct block_list* bl;
  15880. bl = map_id2bl(st->oid);
  15881. if( bl == NULL ) {
  15882. ShowError("buildin_warpportal: NPC is needed\n");
  15883. return SCRIPT_CMD_FAILURE;
  15884. }
  15885. spx = script_getnum(st,2);
  15886. spy = script_getnum(st,3);
  15887. mapindex = mapindex_name2id(script_getstr(st, 4));
  15888. tpx = script_getnum(st,5);
  15889. tpy = script_getnum(st,6);
  15890. if( mapindex == 0 ) {
  15891. ShowError("buildin_warpportal: Target map not found %s.\n", script_getstr(st, 4));
  15892. return SCRIPT_CMD_FAILURE;
  15893. }
  15894. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  15895. if( group == NULL )
  15896. return SCRIPT_CMD_FAILURE;// failed
  15897. group->val1 = (group->val1<<16)|(short)0;
  15898. group->val2 = (tpx<<16) | tpy;
  15899. group->val3 = mapindex;
  15900. return SCRIPT_CMD_SUCCESS;
  15901. }
  15902. /**
  15903. * openmail({<char_id>});
  15904. **/
  15905. BUILDIN_FUNC(openmail)
  15906. {
  15907. TBL_PC* sd;
  15908. if (!script_charid2sd(2,sd))
  15909. return SCRIPT_CMD_FAILURE;
  15910. mail_openmail(sd);
  15911. return SCRIPT_CMD_SUCCESS;
  15912. }
  15913. /**
  15914. * openauction({<char_id>});
  15915. **/
  15916. BUILDIN_FUNC(openauction)
  15917. {
  15918. TBL_PC* sd;
  15919. if (!script_charid2sd(2,sd))
  15920. return SCRIPT_CMD_FAILURE;
  15921. if( !battle_config.feature_auction ) {
  15922. clif_colormes(sd->fd, color_table[COLOR_RED], msg_txt(sd, 517));
  15923. return SCRIPT_CMD_SUCCESS;
  15924. }
  15925. clif_Auction_openwindow(sd);
  15926. return SCRIPT_CMD_SUCCESS;
  15927. }
  15928. /// Retrieves the value of the specified flag of the specified cell.
  15929. ///
  15930. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  15931. ///
  15932. /// @see cell_chk* constants in const.txt for the types
  15933. BUILDIN_FUNC(checkcell)
  15934. {
  15935. int16 m = map_mapname2mapid(script_getstr(st,2));
  15936. int16 x = script_getnum(st,3);
  15937. int16 y = script_getnum(st,4);
  15938. cell_chk type = (cell_chk)script_getnum(st,5);
  15939. script_pushint(st, map_getcell(m, x, y, type));
  15940. return SCRIPT_CMD_SUCCESS;
  15941. }
  15942. /// Modifies flags of cells in the specified area.
  15943. ///
  15944. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  15945. ///
  15946. /// @see cell_* constants in const.txt for the types
  15947. BUILDIN_FUNC(setcell)
  15948. {
  15949. int16 m = map_mapname2mapid(script_getstr(st,2));
  15950. int16 x1 = script_getnum(st,3);
  15951. int16 y1 = script_getnum(st,4);
  15952. int16 x2 = script_getnum(st,5);
  15953. int16 y2 = script_getnum(st,6);
  15954. cell_t type = (cell_t)script_getnum(st,7);
  15955. bool flag = (bool)script_getnum(st,8);
  15956. int x,y;
  15957. if( x1 > x2 ) swap(x1,x2);
  15958. if( y1 > y2 ) swap(y1,y2);
  15959. for( y = y1; y <= y2; ++y )
  15960. for( x = x1; x <= x2; ++x )
  15961. map_setcell(m, x, y, type, flag);
  15962. return SCRIPT_CMD_SUCCESS;
  15963. }
  15964. /*==========================================
  15965. * Mercenary Commands
  15966. *------------------------------------------*/
  15967. BUILDIN_FUNC(mercenary_create)
  15968. {
  15969. struct map_session_data *sd;
  15970. int class_, contract_time;
  15971. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  15972. return SCRIPT_CMD_SUCCESS;
  15973. class_ = script_getnum(st,2);
  15974. if( !mercenary_class(class_) )
  15975. return SCRIPT_CMD_SUCCESS;
  15976. contract_time = script_getnum(st,3);
  15977. mercenary_create(sd, class_, contract_time);
  15978. return SCRIPT_CMD_SUCCESS;
  15979. }
  15980. BUILDIN_FUNC(mercenary_heal)
  15981. {
  15982. struct map_session_data *sd = script_rid2sd(st);
  15983. int hp, sp;
  15984. if( sd == NULL || sd->md == NULL )
  15985. return SCRIPT_CMD_SUCCESS;
  15986. hp = script_getnum(st,2);
  15987. sp = script_getnum(st,3);
  15988. status_heal(&sd->md->bl, hp, sp, 0);
  15989. return SCRIPT_CMD_SUCCESS;
  15990. }
  15991. BUILDIN_FUNC(mercenary_sc_start)
  15992. {
  15993. struct map_session_data *sd = script_rid2sd(st);
  15994. enum sc_type type;
  15995. int tick, val1;
  15996. if( sd == NULL || sd->md == NULL )
  15997. return SCRIPT_CMD_SUCCESS;
  15998. type = (sc_type)script_getnum(st,2);
  15999. tick = script_getnum(st,3);
  16000. val1 = script_getnum(st,4);
  16001. status_change_start(NULL, &sd->md->bl, type, 10000, val1, 0, 0, 0, tick, SCSTART_NOTICKDEF);
  16002. return SCRIPT_CMD_SUCCESS;
  16003. }
  16004. BUILDIN_FUNC(mercenary_get_calls)
  16005. {
  16006. struct map_session_data *sd = script_rid2sd(st);
  16007. int guild;
  16008. if( sd == NULL )
  16009. return SCRIPT_CMD_SUCCESS;
  16010. guild = script_getnum(st,2);
  16011. switch( guild )
  16012. {
  16013. case ARCH_MERC_GUILD:
  16014. script_pushint(st,sd->status.arch_calls);
  16015. break;
  16016. case SPEAR_MERC_GUILD:
  16017. script_pushint(st,sd->status.spear_calls);
  16018. break;
  16019. case SWORD_MERC_GUILD:
  16020. script_pushint(st,sd->status.sword_calls);
  16021. break;
  16022. default:
  16023. script_pushint(st,0);
  16024. break;
  16025. }
  16026. return SCRIPT_CMD_SUCCESS;
  16027. }
  16028. BUILDIN_FUNC(mercenary_set_calls)
  16029. {
  16030. struct map_session_data *sd = script_rid2sd(st);
  16031. int guild, value, *calls;
  16032. if( sd == NULL )
  16033. return SCRIPT_CMD_SUCCESS;
  16034. guild = script_getnum(st,2);
  16035. value = script_getnum(st,3);
  16036. switch( guild )
  16037. {
  16038. case ARCH_MERC_GUILD:
  16039. calls = &sd->status.arch_calls;
  16040. break;
  16041. case SPEAR_MERC_GUILD:
  16042. calls = &sd->status.spear_calls;
  16043. break;
  16044. case SWORD_MERC_GUILD:
  16045. calls = &sd->status.sword_calls;
  16046. break;
  16047. default:
  16048. ShowError("buildin_mercenary_set_calls: Invalid guild '%d'.\n", guild);
  16049. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16050. }
  16051. *calls += value;
  16052. *calls = cap_value(*calls, 0, INT_MAX);
  16053. return SCRIPT_CMD_SUCCESS;
  16054. }
  16055. BUILDIN_FUNC(mercenary_get_faith)
  16056. {
  16057. struct map_session_data *sd = script_rid2sd(st);
  16058. int guild;
  16059. if( sd == NULL )
  16060. return SCRIPT_CMD_SUCCESS;
  16061. guild = script_getnum(st,2);
  16062. switch( guild )
  16063. {
  16064. case ARCH_MERC_GUILD:
  16065. script_pushint(st,sd->status.arch_faith);
  16066. break;
  16067. case SPEAR_MERC_GUILD:
  16068. script_pushint(st,sd->status.spear_faith);
  16069. break;
  16070. case SWORD_MERC_GUILD:
  16071. script_pushint(st,sd->status.sword_faith);
  16072. break;
  16073. default:
  16074. script_pushint(st,0);
  16075. break;
  16076. }
  16077. return SCRIPT_CMD_SUCCESS;
  16078. }
  16079. BUILDIN_FUNC(mercenary_set_faith)
  16080. {
  16081. struct map_session_data *sd = script_rid2sd(st);
  16082. int guild, value, *calls;
  16083. if( sd == NULL )
  16084. return SCRIPT_CMD_SUCCESS;
  16085. guild = script_getnum(st,2);
  16086. value = script_getnum(st,3);
  16087. switch( guild )
  16088. {
  16089. case ARCH_MERC_GUILD:
  16090. calls = &sd->status.arch_faith;
  16091. break;
  16092. case SPEAR_MERC_GUILD:
  16093. calls = &sd->status.spear_faith;
  16094. break;
  16095. case SWORD_MERC_GUILD:
  16096. calls = &sd->status.sword_faith;
  16097. break;
  16098. default:
  16099. ShowError("buildin_mercenary_set_faith: Invalid guild '%d'.\n", guild);
  16100. return SCRIPT_CMD_SUCCESS; // Invalid Guild
  16101. }
  16102. *calls += value;
  16103. *calls = cap_value(*calls, 0, INT_MAX);
  16104. if( mercenary_get_guild(sd->md) == guild )
  16105. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  16106. return SCRIPT_CMD_SUCCESS;
  16107. }
  16108. /*------------------------------------------
  16109. * Book Reading
  16110. *------------------------------------------*/
  16111. BUILDIN_FUNC(readbook)
  16112. {
  16113. struct map_session_data *sd;
  16114. int book_id, page;
  16115. if( (sd = script_rid2sd(st)) == NULL )
  16116. return SCRIPT_CMD_SUCCESS;
  16117. book_id = script_getnum(st,2);
  16118. page = script_getnum(st,3);
  16119. clif_readbook(sd->fd, book_id, page);
  16120. return SCRIPT_CMD_SUCCESS;
  16121. }
  16122. /******************
  16123. Questlog script commands
  16124. *******************/
  16125. /**
  16126. * Add job criteria to questinfo
  16127. * @param qi Quest Info
  16128. * @param job
  16129. * @author [Cydh]
  16130. **/
  16131. static void buildin_questinfo_setjob(struct questinfo *qi, int job) {
  16132. RECREATE(qi->jobid, unsigned short, qi->jobid_count+1);
  16133. qi->jobid[qi->jobid_count++] = job;
  16134. }
  16135. /**
  16136. * questinfo <Quest ID>,<Icon>{,<Map Mark Color>{,<Job Class>}};
  16137. **/
  16138. BUILDIN_FUNC(questinfo)
  16139. {
  16140. TBL_NPC* nd = map_id2nd(st->oid);
  16141. int quest_id, icon;
  16142. struct questinfo qi, *q2;
  16143. if( nd == NULL || nd->bl.m == -1 ) {
  16144. ShowError("buildin_questinfo: No NPC attached.\n");
  16145. return SCRIPT_CMD_FAILURE;
  16146. }
  16147. quest_id = script_getnum(st, 2);
  16148. icon = script_getnum(st, 3);
  16149. #if PACKETVER >= 20120410
  16150. switch(icon){
  16151. case QTYPE_QUEST:
  16152. case QTYPE_QUEST2:
  16153. case QTYPE_JOB:
  16154. case QTYPE_JOB2:
  16155. case QTYPE_EVENT:
  16156. case QTYPE_EVENT2:
  16157. case QTYPE_WARG:
  16158. case QTYPE_WARG2:
  16159. // Leave everything as it is
  16160. break;
  16161. case QTYPE_NONE:
  16162. default:
  16163. // Default to nothing if icon id is invalid.
  16164. icon = QTYPE_NONE;
  16165. break;
  16166. }
  16167. #else
  16168. if(icon < QTYPE_QUEST || icon > 7) // TODO: check why 7 and not QTYPE_WARG, might be related to icon + 1 below
  16169. icon = QTYPE_QUEST;
  16170. else
  16171. icon = icon + 1;
  16172. #endif
  16173. qi.quest_id = quest_id;
  16174. qi.icon = (unsigned char)icon;
  16175. qi.nd = nd;
  16176. if( script_hasdata(st, 4) ) {
  16177. int color = script_getnum(st, 4);
  16178. if( color < 0 || color > 3 ) {
  16179. ShowWarning("buildin_questinfo: invalid color '%d', changing to 0\n",color);
  16180. script_reportfunc(st);
  16181. color = 0;
  16182. }
  16183. qi.color = (unsigned char)color;
  16184. }
  16185. qi.min_level = 1;
  16186. qi.max_level = MAX_LEVEL;
  16187. q2 = map_add_questinfo(nd->bl.m, &qi);
  16188. q2->req = NULL;
  16189. q2->req_count = 0;
  16190. q2->jobid = NULL;
  16191. q2->jobid_count = 0;
  16192. if(script_hasdata(st, 5)) {
  16193. int job = script_getnum(st, 5);
  16194. if (!pcdb_checkid(job))
  16195. ShowError("buildin_questinfo: Nonexistant Job Class.\n");
  16196. else {
  16197. buildin_questinfo_setjob(q2, job);
  16198. }
  16199. }
  16200. return SCRIPT_CMD_SUCCESS;
  16201. }
  16202. /**
  16203. * setquest <ID>{,<char_id>};
  16204. **/
  16205. BUILDIN_FUNC(setquest)
  16206. {
  16207. struct map_session_data *sd;
  16208. int quest_id;
  16209. quest_id = script_getnum(st, 2);
  16210. if (!script_charid2sd(3,sd))
  16211. return SCRIPT_CMD_FAILURE;
  16212. if( quest_add(sd, quest_id) == -1 ){
  16213. script_reportsrc(st);
  16214. script_reportfunc(st);
  16215. }
  16216. //20120410 or 20090218 ? no reason that shouldn't work for 2009
  16217. pc_show_questinfo(sd);
  16218. return SCRIPT_CMD_SUCCESS;
  16219. }
  16220. /**
  16221. * erasequest <ID>{,<char_id>};
  16222. **/
  16223. BUILDIN_FUNC(erasequest)
  16224. {
  16225. struct map_session_data *sd;
  16226. if (!script_charid2sd(3,sd))
  16227. return SCRIPT_CMD_FAILURE;
  16228. if( quest_delete(sd, script_getnum(st, 2)) == -1 ){
  16229. script_reportsrc(st);
  16230. script_reportfunc(st);
  16231. }
  16232. return SCRIPT_CMD_SUCCESS;
  16233. }
  16234. /**
  16235. * completequest <ID>{,<char_id>};
  16236. **/
  16237. BUILDIN_FUNC(completequest)
  16238. {
  16239. struct map_session_data *sd;
  16240. if (!script_charid2sd(3,sd))
  16241. return SCRIPT_CMD_FAILURE;
  16242. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  16243. //20120410 or 20090218
  16244. pc_show_questinfo(sd);
  16245. return SCRIPT_CMD_SUCCESS;
  16246. }
  16247. /**
  16248. * changequest <ID>,<ID2>{,<char_id>};
  16249. **/
  16250. BUILDIN_FUNC(changequest)
  16251. {
  16252. struct map_session_data *sd;
  16253. if (!script_charid2sd(4,sd))
  16254. return SCRIPT_CMD_FAILURE;
  16255. if( quest_change(sd, script_getnum(st, 2),script_getnum(st, 3)) == -1 ){
  16256. script_reportsrc(st);
  16257. script_reportfunc(st);
  16258. }
  16259. //20120410 or 20090218
  16260. pc_show_questinfo(sd);
  16261. return SCRIPT_CMD_SUCCESS;
  16262. }
  16263. /**
  16264. * checkquest(<ID>{,PLAYTIME|HUNTING{,<char_id>}})
  16265. **/
  16266. BUILDIN_FUNC(checkquest)
  16267. {
  16268. struct map_session_data *sd;
  16269. enum quest_check_type type = HAVEQUEST;
  16270. if( script_hasdata(st, 3) )
  16271. type = (enum quest_check_type)script_getnum(st, 3);
  16272. if (!script_charid2sd(4,sd))
  16273. return SCRIPT_CMD_FAILURE;
  16274. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  16275. return SCRIPT_CMD_SUCCESS;
  16276. }
  16277. /**
  16278. * isbegin_quest(<ID>{,<char_id>})
  16279. **/
  16280. BUILDIN_FUNC(isbegin_quest)
  16281. {
  16282. struct map_session_data *sd;
  16283. int i;
  16284. if (!script_charid2sd(3,sd))
  16285. return SCRIPT_CMD_FAILURE;
  16286. i = quest_check(sd, script_getnum(st, 2), (enum quest_check_type) HAVEQUEST);
  16287. script_pushint(st, i + (i < 1));
  16288. return SCRIPT_CMD_SUCCESS;
  16289. }
  16290. /**
  16291. * showevent <icon>{,<mark color>{,<char_id>}}
  16292. **/
  16293. BUILDIN_FUNC(showevent)
  16294. {
  16295. TBL_PC *sd;
  16296. struct npc_data *nd = map_id2nd(st->oid);
  16297. int icon, color = 0;
  16298. if (!script_charid2sd(4,sd))
  16299. return SCRIPT_CMD_FAILURE;
  16300. if( sd == NULL || nd == NULL )
  16301. return SCRIPT_CMD_SUCCESS;
  16302. icon = script_getnum(st, 2);
  16303. if( script_hasdata(st, 3) ) {
  16304. color = script_getnum(st, 3);
  16305. if( color < 0 || color > 3 ) {
  16306. ShowWarning("buildin_showevent: invalid color '%d', changing to 0\n",color);
  16307. script_reportfunc(st);
  16308. color = 0;
  16309. }
  16310. }
  16311. #if PACKETVER >= 20120410
  16312. if(icon < 0 || (icon > 8 && icon != 9999) || icon == 7)
  16313. icon = 9999; // Default to nothing if icon id is invalid.
  16314. #else
  16315. if(icon < 0 || icon > 7)
  16316. icon = 0;
  16317. else
  16318. icon = icon + 1;
  16319. #endif
  16320. clif_quest_show_event(sd, &nd->bl, icon, color);
  16321. return SCRIPT_CMD_SUCCESS;
  16322. }
  16323. /*==========================================
  16324. * BattleGround System
  16325. *------------------------------------------*/
  16326. BUILDIN_FUNC(waitingroom2bg)
  16327. {
  16328. struct npc_data *nd;
  16329. struct chat_data *cd;
  16330. const char *map_name, *ev = "", *dev = "";
  16331. int x, y, mapindex = 0, bg_id;
  16332. unsigned char i,c=0;
  16333. if( script_hasdata(st,7) )
  16334. nd = npc_name2id(script_getstr(st,7));
  16335. else
  16336. nd = (struct npc_data *)map_id2bl(st->oid);
  16337. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  16338. {
  16339. script_pushint(st,0);
  16340. return SCRIPT_CMD_SUCCESS;
  16341. }
  16342. map_name = script_getstr(st,2);
  16343. if( strcmp(map_name,"-") != 0 )
  16344. {
  16345. mapindex = mapindex_name2id(map_name);
  16346. if( mapindex == 0 )
  16347. { // Invalid Map
  16348. script_pushint(st,0);
  16349. return SCRIPT_CMD_SUCCESS;
  16350. }
  16351. }
  16352. x = script_getnum(st,3);
  16353. y = script_getnum(st,4);
  16354. ev = script_getstr(st,5); // Logout Event
  16355. dev = script_getstr(st,6); // Die Event
  16356. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  16357. { // Creation failed
  16358. script_pushint(st,0);
  16359. return SCRIPT_CMD_SUCCESS;
  16360. }
  16361. for (i = 0; i < cd->users; i++) { // Only add those who are in the chat room
  16362. struct map_session_data *sd;
  16363. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) ){
  16364. mapreg_setreg(reference_uid(add_str("$@arenamembers"), c), sd->bl.id);
  16365. ++c;
  16366. }
  16367. }
  16368. mapreg_setreg(add_str("$@arenamembersnum"), c);
  16369. script_pushint(st,bg_id);
  16370. return SCRIPT_CMD_SUCCESS;
  16371. }
  16372. BUILDIN_FUNC(waitingroom2bg_single)
  16373. {
  16374. const char* map_name;
  16375. struct npc_data *nd;
  16376. struct chat_data *cd;
  16377. struct map_session_data *sd;
  16378. int x, y, mapindex, bg_id;
  16379. bg_id = script_getnum(st,2);
  16380. map_name = script_getstr(st,3);
  16381. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16382. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16383. x = script_getnum(st,4);
  16384. y = script_getnum(st,5);
  16385. nd = npc_name2id(script_getstr(st,6));
  16386. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  16387. return SCRIPT_CMD_SUCCESS;
  16388. if( (sd = cd->usersd[0]) == NULL )
  16389. return SCRIPT_CMD_SUCCESS;
  16390. if( bg_team_join(bg_id, sd) )
  16391. {
  16392. pc_setpos(sd, mapindex, x, y, CLR_TELEPORT);
  16393. script_pushint(st,1);
  16394. }
  16395. else
  16396. script_pushint(st,0);
  16397. return SCRIPT_CMD_SUCCESS;
  16398. }
  16399. BUILDIN_FUNC(bg_team_setxy)
  16400. {
  16401. struct battleground_data *bg;
  16402. int bg_id;
  16403. bg_id = script_getnum(st,2);
  16404. if( (bg = bg_team_search(bg_id)) == NULL )
  16405. return SCRIPT_CMD_SUCCESS;
  16406. bg->x = script_getnum(st,3);
  16407. bg->y = script_getnum(st,4);
  16408. return SCRIPT_CMD_SUCCESS;
  16409. }
  16410. BUILDIN_FUNC(bg_warp)
  16411. {
  16412. int x, y, mapindex, bg_id;
  16413. const char* map_name;
  16414. bg_id = script_getnum(st,2);
  16415. map_name = script_getstr(st,3);
  16416. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  16417. return SCRIPT_CMD_SUCCESS; // Invalid Map
  16418. x = script_getnum(st,4);
  16419. y = script_getnum(st,5);
  16420. bg_team_warp(bg_id, mapindex, x, y);
  16421. return SCRIPT_CMD_SUCCESS;
  16422. }
  16423. BUILDIN_FUNC(bg_monster)
  16424. {
  16425. int class_ = 0, x = 0, y = 0, bg_id = 0;
  16426. const char *str,*mapname, *evt="";
  16427. bg_id = script_getnum(st,2);
  16428. mapname = script_getstr(st,3);
  16429. x = script_getnum(st,4);
  16430. y = script_getnum(st,5);
  16431. str = script_getstr(st,6);
  16432. class_ = script_getnum(st,7);
  16433. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  16434. check_event(st, evt);
  16435. script_pushint(st, mob_spawn_bg(mapname,x,y,str,class_,evt,bg_id));
  16436. return SCRIPT_CMD_SUCCESS;
  16437. }
  16438. BUILDIN_FUNC(bg_monster_set_team)
  16439. {
  16440. struct mob_data *md;
  16441. struct block_list *mbl;
  16442. int id = script_getnum(st,2),
  16443. bg_id = script_getnum(st,3);
  16444. if( id == 0 || (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  16445. return SCRIPT_CMD_SUCCESS;
  16446. md = (TBL_MOB *)mbl;
  16447. md->bg_id = bg_id;
  16448. mob_stop_attack(md);
  16449. mob_stop_walking(md, 0);
  16450. md->target_id = md->attacked_id = 0;
  16451. clif_charnameack(0, &md->bl);
  16452. return SCRIPT_CMD_SUCCESS;
  16453. }
  16454. BUILDIN_FUNC(bg_leave)
  16455. {
  16456. struct map_session_data *sd = NULL;
  16457. if( !script_charid2sd(2,sd) || !sd->bg_id )
  16458. return SCRIPT_CMD_SUCCESS;
  16459. bg_team_leave(sd,0);
  16460. return SCRIPT_CMD_SUCCESS;
  16461. }
  16462. BUILDIN_FUNC(bg_destroy)
  16463. {
  16464. int bg_id = script_getnum(st,2);
  16465. bg_team_delete(bg_id);
  16466. return SCRIPT_CMD_SUCCESS;
  16467. }
  16468. BUILDIN_FUNC(bg_getareausers)
  16469. {
  16470. const char *str;
  16471. int16 m, x0, y0, x1, y1;
  16472. int bg_id;
  16473. int i = 0, c = 0;
  16474. struct battleground_data *bg = NULL;
  16475. bg_id = script_getnum(st,2);
  16476. str = script_getstr(st,3);
  16477. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  16478. {
  16479. script_pushint(st,0);
  16480. return SCRIPT_CMD_SUCCESS;
  16481. }
  16482. x0 = script_getnum(st,4);
  16483. y0 = script_getnum(st,5);
  16484. x1 = script_getnum(st,6);
  16485. y1 = script_getnum(st,7);
  16486. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  16487. {
  16488. struct map_session_data *sd;
  16489. if( (sd = bg->members[i].sd) == NULL )
  16490. continue;
  16491. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  16492. continue;
  16493. c++;
  16494. }
  16495. script_pushint(st,c);
  16496. return SCRIPT_CMD_SUCCESS;
  16497. }
  16498. BUILDIN_FUNC(bg_updatescore)
  16499. {
  16500. const char *str;
  16501. int16 m;
  16502. str = script_getstr(st,2);
  16503. if( (m = map_mapname2mapid(str)) < 0 )
  16504. return SCRIPT_CMD_SUCCESS;
  16505. map[m].bgscore_lion = script_getnum(st,3);
  16506. map[m].bgscore_eagle = script_getnum(st,4);
  16507. clif_bg_updatescore(m);
  16508. return SCRIPT_CMD_SUCCESS;
  16509. }
  16510. BUILDIN_FUNC(bg_get_data)
  16511. {
  16512. struct battleground_data *bg;
  16513. int bg_id = script_getnum(st,2),
  16514. type = script_getnum(st,3);
  16515. if( (bg = bg_team_search(bg_id)) == NULL )
  16516. {
  16517. script_pushint(st,0);
  16518. return SCRIPT_CMD_SUCCESS;
  16519. }
  16520. switch( type )
  16521. {
  16522. case 0: script_pushint(st, bg->count); break;
  16523. default:
  16524. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  16525. break;
  16526. }
  16527. return SCRIPT_CMD_SUCCESS;
  16528. }
  16529. /*==========================================
  16530. * Instancing System
  16531. *------------------------------------------*/
  16532. //Returns an Instance ID
  16533. //Checks NPC first, then if player is attached we check party
  16534. int script_instancegetid(struct script_state* st)
  16535. {
  16536. short instance_id = 0;
  16537. struct npc_data *nd;
  16538. if( (nd = map_id2nd(st->oid)) && nd->instance_id > 0 )
  16539. instance_id = nd->instance_id;
  16540. else {
  16541. struct map_session_data *sd;
  16542. struct party_data *p;
  16543. if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  16544. instance_id = p->instance_id;
  16545. }
  16546. return instance_id;
  16547. }
  16548. /*==========================================
  16549. * Creates the instance
  16550. * Returns Instance ID if created successfully
  16551. *------------------------------------------*/
  16552. BUILDIN_FUNC(instance_create)
  16553. {
  16554. struct map_session_data *sd;
  16555. if((sd = script_rid2sd(st)) == NULL)
  16556. return -1;
  16557. script_pushint(st,instance_create(sd->status.party_id, script_getstr(st, 2)));
  16558. return SCRIPT_CMD_SUCCESS;
  16559. }
  16560. /*==========================================
  16561. * Destroys an instance (unofficial)
  16562. * Officially instances are only destroyed by timeout
  16563. *
  16564. * instance_destroy {<instance_id>};
  16565. *------------------------------------------*/
  16566. BUILDIN_FUNC(instance_destroy)
  16567. {
  16568. short instance_id;
  16569. if( script_hasdata(st,2) )
  16570. instance_id = script_getnum(st,2);
  16571. else
  16572. instance_id = script_instancegetid(st);
  16573. if( instance_id <= 0 || instance_id >= MAX_MAP_PER_SERVER ) {
  16574. ShowError("script:instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  16575. return SCRIPT_CMD_SUCCESS;
  16576. }
  16577. instance_destroy(instance_id);
  16578. return SCRIPT_CMD_SUCCESS;
  16579. }
  16580. /*==========================================
  16581. * Warps player to instance
  16582. * Results:
  16583. * 0: Success
  16584. * 1: Character not in party
  16585. * 2: Party doesn't have instance
  16586. * 3: Other errors (instance not in DB, instance doesn't match with party, etc.)
  16587. *------------------------------------------*/
  16588. BUILDIN_FUNC(instance_enter)
  16589. {
  16590. struct map_session_data *sd = NULL;
  16591. int x = script_hasdata(st,3) ? script_getnum(st, 3) : -1;
  16592. int y = script_hasdata(st,4) ? script_getnum(st, 4) : -1;
  16593. if (!script_charid2sd(5,sd))
  16594. return SCRIPT_CMD_FAILURE;
  16595. if (x != -1 && y != -1)
  16596. script_pushint(st,instance_enter_position(sd,script_getstr(st, 2),x,y));
  16597. else
  16598. script_pushint(st,instance_enter(sd,script_getstr(st, 2)));
  16599. return SCRIPT_CMD_SUCCESS;
  16600. }
  16601. /*==========================================
  16602. * Returns the name of a duplicated NPC
  16603. *
  16604. * instance_npcname <npc_name>{,<instance_id>};
  16605. * <npc_name> is the full name of an NPC.
  16606. *------------------------------------------*/
  16607. BUILDIN_FUNC(instance_npcname)
  16608. {
  16609. const char *str;
  16610. short instance_id = 0;
  16611. struct npc_data *nd;
  16612. str = script_getstr(st,2);
  16613. if( script_hasdata(st,3) )
  16614. instance_id = script_getnum(st,3);
  16615. else
  16616. instance_id = script_instancegetid(st);
  16617. if( instance_id && (nd = npc_name2id(str)) != NULL ) {
  16618. static char npcname[NAME_LENGTH];
  16619. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  16620. script_pushconststr(st,npcname);
  16621. } else {
  16622. ShowError("script:instance_npcname: invalid instance NPC (instance_id: %d, NPC name: \"%s\".)\n", instance_id, str);
  16623. st->state = END;
  16624. return SCRIPT_CMD_FAILURE;
  16625. }
  16626. return SCRIPT_CMD_SUCCESS;
  16627. }
  16628. /*==========================================
  16629. * Returns the name of a duplicated map
  16630. *
  16631. * instance_mapname <map_name>{,<instance_id>};
  16632. *------------------------------------------*/
  16633. BUILDIN_FUNC(instance_mapname)
  16634. {
  16635. const char *str;
  16636. int16 m;
  16637. short instance_id = 0;
  16638. str = script_getstr(st,2);
  16639. if( script_hasdata(st,3) )
  16640. instance_id = script_getnum(st,3);
  16641. else
  16642. instance_id = script_instancegetid(st);
  16643. // Check that instance mapname is a valid map
  16644. if(!instance_id || (m = instance_mapname2mapid(str,instance_id)) < 0)
  16645. script_pushconststr(st, "");
  16646. else
  16647. script_pushconststr(st, map[m].name);
  16648. return SCRIPT_CMD_SUCCESS;
  16649. }
  16650. /*==========================================
  16651. * Returns an Instance ID
  16652. *------------------------------------------*/
  16653. BUILDIN_FUNC(instance_id)
  16654. {
  16655. short instance_id;
  16656. instance_id = script_instancegetid(st);
  16657. if(!instance_id) {
  16658. //ShowError("script:instance_id: No instance attached to NPC or player");
  16659. script_pushint(st, 0);
  16660. return SCRIPT_CMD_SUCCESS;
  16661. }
  16662. script_pushint(st, instance_id);
  16663. return SCRIPT_CMD_SUCCESS;
  16664. }
  16665. /*==========================================
  16666. * Warps all players inside an instance
  16667. *
  16668. * instance_warpall <map_name>,<x>,<y>{,<instance_id>};
  16669. *------------------------------------------*/
  16670. BUILDIN_FUNC(instance_warpall)
  16671. {
  16672. struct party_data *p;
  16673. int16 m, i;
  16674. short instance_id;
  16675. const char *mapn;
  16676. int x, y;
  16677. mapn = script_getstr(st,2);
  16678. x = script_getnum(st,3);
  16679. y = script_getnum(st,4);
  16680. if( script_hasdata(st,5) )
  16681. instance_id = script_getnum(st,5);
  16682. else
  16683. instance_id = script_instancegetid(st);
  16684. if( !instance_id || (m = map_mapname2mapid(mapn)) < 0 || (m = instance_mapname2mapid(map[m].name,instance_id)) < 0)
  16685. return SCRIPT_CMD_SUCCESS;
  16686. if( !(p = party_search(instance_data[instance_id].party_id)) )
  16687. return SCRIPT_CMD_SUCCESS;
  16688. for( i = 0; i < MAX_PARTY; i++ ) {
  16689. struct map_session_data *pl_sd;
  16690. if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == instance_id ) pc_setpos(pl_sd,map_id2index(m),x,y,CLR_TELEPORT);
  16691. }
  16692. return SCRIPT_CMD_SUCCESS;
  16693. }
  16694. /*==========================================
  16695. * Broadcasts to all maps inside an instance
  16696. *
  16697. * instance_announce <instance id>,"<text>",<flag>{,<fontColor>{,<fontType>{,<fontSize>{,<fontAlign>{,<fontY>}}}}};
  16698. * Using -1 for <instance id> will auto-detect the id.
  16699. *------------------------------------------*/
  16700. BUILDIN_FUNC(instance_announce) {
  16701. int instance_id = script_getnum(st,2);
  16702. const char *mes = script_getstr(st,3);
  16703. int flag = script_getnum(st,4);
  16704. const char *fontColor = script_hasdata(st,5) ? script_getstr(st,5) : NULL;
  16705. int fontType = script_hasdata(st,6) ? script_getnum(st,6) : FW_NORMAL; // default fontType
  16706. int fontSize = script_hasdata(st,7) ? script_getnum(st,7) : 12; // default fontSize
  16707. int fontAlign = script_hasdata(st,8) ? script_getnum(st,8) : 0; // default fontAlign
  16708. int fontY = script_hasdata(st,9) ? script_getnum(st,9) : 0; // default fontY
  16709. int i;
  16710. if( instance_id == -1 ) {
  16711. instance_id = script_instancegetid(st);
  16712. }
  16713. if( !instance_id && &instance_data[instance_id] != NULL) {
  16714. ShowError("buildin_instance_announce: Intance is not found.\n");
  16715. return SCRIPT_CMD_FAILURE;
  16716. }
  16717. for( i = 0; i < instance_data[instance_id].cnt_map; i++ )
  16718. map_foreachinmap(buildin_announce_sub, instance_data[instance_id].map[i].m, BL_PC,
  16719. mes, strlen(mes)+1, flag&BC_COLOR_MASK, fontColor, fontType, fontSize, fontAlign, fontY);
  16720. return SCRIPT_CMD_SUCCESS;
  16721. }
  16722. /*==========================================
  16723. * instance_check_party [malufett]
  16724. * Values:
  16725. * party_id : Party ID of the invoking character. [Required Parameter]
  16726. * amount : Amount of needed Partymembers for the Instance. [Optional Parameter]
  16727. * min : Minimum Level needed to join the Instance. [Optional Parameter]
  16728. * max : Maxium Level allowed to join the Instance. [Optional Parameter]
  16729. * Example: instance_check_party (getcharid(1){,amount}{,min}{,max});
  16730. * Example 2: instance_check_party (getcharid(1),1,1,99);
  16731. *------------------------------------------*/
  16732. BUILDIN_FUNC(instance_check_party)
  16733. {
  16734. int amount, min, max, i, party_id, c = 0;
  16735. struct party_data *p = NULL;
  16736. amount = script_hasdata(st,3) ? script_getnum(st,3) : 1; // Amount of needed Partymembers for the Instance.
  16737. min = script_hasdata(st,4) ? script_getnum(st,4) : 1; // Minimum Level needed to join the Instance.
  16738. max = script_hasdata(st,5) ? script_getnum(st,5) : MAX_LEVEL; // Maxium Level allowed to join the Instance.
  16739. if( min < 1 || min > MAX_LEVEL) {
  16740. ShowError("buildin_instance_check_party: Invalid min level, %d\n", min);
  16741. return SCRIPT_CMD_FAILURE;
  16742. } else if( max < 1 || max > MAX_LEVEL) {
  16743. ShowError("buildin_instance_check_party: Invalid max level, %d\n", max);
  16744. return SCRIPT_CMD_FAILURE;
  16745. }
  16746. if( script_hasdata(st,2) )
  16747. party_id = script_getnum(st,2);
  16748. else return SCRIPT_CMD_SUCCESS;
  16749. if( !(p = party_search(party_id)) ) {
  16750. script_pushint(st, 0); // Returns false if party does not exist.
  16751. return SCRIPT_CMD_SUCCESS;
  16752. }
  16753. for( i = 0; i < MAX_PARTY; i++ ) {
  16754. struct map_session_data *pl_sd;
  16755. if( (pl_sd = p->data[i].sd) )
  16756. if(map_id2bl(pl_sd->bl.id)) {
  16757. if(pl_sd->status.base_level < min) {
  16758. script_pushint(st, 0);
  16759. return SCRIPT_CMD_SUCCESS;
  16760. } else if(pl_sd->status.base_level > max) {
  16761. script_pushint(st, 0);
  16762. return SCRIPT_CMD_SUCCESS;
  16763. }
  16764. c++;
  16765. }
  16766. }
  16767. if(c < amount)
  16768. script_pushint(st, 0); // Not enough Members in the Party to join Instance.
  16769. else
  16770. script_pushint(st, 1);
  16771. return SCRIPT_CMD_SUCCESS;
  16772. }
  16773. /*==========================================
  16774. * Custom Fonts
  16775. *------------------------------------------*/
  16776. BUILDIN_FUNC(setfont)
  16777. {
  16778. struct map_session_data *sd = script_rid2sd(st);
  16779. int font = script_getnum(st,2);
  16780. if( sd == NULL )
  16781. return SCRIPT_CMD_SUCCESS;
  16782. if( sd->status.font != font )
  16783. sd->status.font = font;
  16784. else
  16785. sd->status.font = 0;
  16786. clif_font(sd);
  16787. return SCRIPT_CMD_SUCCESS;
  16788. }
  16789. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  16790. {
  16791. TBL_MOB* md = (TBL_MOB*)bl;
  16792. struct block_list *tbl;
  16793. int mobid = va_arg(ap,int);
  16794. uint16 skill_id = va_arg(ap,int);
  16795. uint16 skill_lv = va_arg(ap,int);
  16796. int casttime = va_arg(ap,int);
  16797. int cancel = va_arg(ap,int);
  16798. int emotion = va_arg(ap,int);
  16799. int target = va_arg(ap,int);
  16800. if( md->mob_id != mobid )
  16801. return 0;
  16802. // 0:self, 1:target, 2:master, default:random
  16803. switch( target ) {
  16804. case 0: tbl = map_id2bl(md->bl.id); break;
  16805. case 1: tbl = map_id2bl(md->target_id); break;
  16806. case 2: tbl = map_id2bl(md->master_id); break;
  16807. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skill_id, skill_lv)); break;
  16808. }
  16809. if( !tbl )
  16810. return 0;
  16811. if( md->ud.skilltimer != INVALID_TIMER ) // Cancel the casting skill.
  16812. unit_skillcastcancel(bl,0);
  16813. if( skill_get_casttype(skill_id) == CAST_GROUND )
  16814. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skill_id, skill_lv, casttime, cancel);
  16815. else
  16816. unit_skilluse_id2(&md->bl, tbl->id, skill_id, skill_lv, casttime, cancel);
  16817. clif_emotion(&md->bl, emotion);
  16818. return 1;
  16819. }
  16820. /*==========================================
  16821. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  16822. *------------------------------------------*/
  16823. BUILDIN_FUNC(areamobuseskill)
  16824. {
  16825. struct block_list center;
  16826. struct script_data *data;
  16827. int16 m;
  16828. int range,mobid,skill_id,skill_lv,casttime,emotion,target,cancel;
  16829. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 ) {
  16830. ShowError("areamobuseskill: invalid map name.\n");
  16831. return SCRIPT_CMD_SUCCESS;
  16832. }
  16833. center.m = m;
  16834. center.x = script_getnum(st,3);
  16835. center.y = script_getnum(st,4);
  16836. range = script_getnum(st,5);
  16837. mobid = script_getnum(st,6);
  16838. data = script_getdata(st, 7);
  16839. get_val(st, data); // Convert into value in case of a variable
  16840. skill_id = ( data_isstring(data) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  16841. if( (skill_lv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  16842. skill_lv = battle_config.mob_max_skilllvl;
  16843. casttime = script_getnum(st,9);
  16844. cancel = script_getnum(st,10);
  16845. emotion = script_getnum(st,11);
  16846. target = script_getnum(st,12);
  16847. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skill_id, skill_lv, casttime, cancel, emotion, target);
  16848. return SCRIPT_CMD_SUCCESS;
  16849. }
  16850. BUILDIN_FUNC(progressbar)
  16851. {
  16852. struct map_session_data * sd = script_rid2sd(st);
  16853. const char * color;
  16854. unsigned int second;
  16855. if( !st || !sd )
  16856. return SCRIPT_CMD_SUCCESS;
  16857. st->state = STOP;
  16858. color = script_getstr(st,2);
  16859. second = script_getnum(st,3);
  16860. sd->progressbar.npc_id = st->oid;
  16861. sd->progressbar.timeout = gettick() + second*1000;
  16862. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  16863. return SCRIPT_CMD_SUCCESS;
  16864. }
  16865. BUILDIN_FUNC(pushpc)
  16866. {
  16867. uint8 dir;
  16868. int cells, dx, dy;
  16869. struct map_session_data* sd;
  16870. if((sd = script_rid2sd(st))==NULL)
  16871. {
  16872. return SCRIPT_CMD_SUCCESS;
  16873. }
  16874. dir = script_getnum(st,2);
  16875. cells = script_getnum(st,3);
  16876. if(dir>7)
  16877. {
  16878. ShowWarning("buildin_pushpc: Invalid direction %d specified.\n", dir);
  16879. script_reportsrc(st);
  16880. dir%= 8; // trim spin-over
  16881. }
  16882. if(!cells)
  16883. {// zero distance
  16884. return SCRIPT_CMD_SUCCESS;
  16885. }
  16886. else if(cells<0)
  16887. {// pushing backwards
  16888. dir = (dir+4)%8; // turn around
  16889. cells = -cells;
  16890. }
  16891. dx = dirx[dir];
  16892. dy = diry[dir];
  16893. unit_blown(&sd->bl, dx, dy, cells, 0);
  16894. return SCRIPT_CMD_SUCCESS;
  16895. }
  16896. /// Invokes buying store preparation window
  16897. /// buyingstore <slots>;
  16898. BUILDIN_FUNC(buyingstore)
  16899. {
  16900. struct map_session_data* sd;
  16901. if( ( sd = script_rid2sd(st) ) == NULL )
  16902. {
  16903. return SCRIPT_CMD_SUCCESS;
  16904. }
  16905. if( npc_isnear(&sd->bl) ) {
  16906. char output[150];
  16907. sprintf(output, msg_txt(sd,662), battle_config.min_npc_vendchat_distance);
  16908. clif_displaymessage(sd->fd, output);
  16909. return SCRIPT_CMD_SUCCESS;
  16910. }
  16911. buyingstore_setup(sd, script_getnum(st,2));
  16912. return SCRIPT_CMD_SUCCESS;
  16913. }
  16914. /// Invokes search store info window
  16915. /// searchstores <uses>,<effect>;
  16916. BUILDIN_FUNC(searchstores)
  16917. {
  16918. unsigned short effect;
  16919. unsigned int uses;
  16920. struct map_session_data* sd;
  16921. if( ( sd = script_rid2sd(st) ) == NULL )
  16922. {
  16923. return SCRIPT_CMD_SUCCESS;
  16924. }
  16925. uses = script_getnum(st,2);
  16926. effect = script_getnum(st,3);
  16927. if( !uses )
  16928. {
  16929. ShowError("buildin_searchstores: Amount of uses cannot be zero.\n");
  16930. return SCRIPT_CMD_FAILURE;
  16931. }
  16932. if( effect > 1 )
  16933. {
  16934. ShowError("buildin_searchstores: Invalid effect id %hu, specified.\n", effect);
  16935. return SCRIPT_CMD_FAILURE;
  16936. }
  16937. searchstore_open(sd, uses, effect);
  16938. return SCRIPT_CMD_SUCCESS;
  16939. }
  16940. /// Displays a number as large digital clock.
  16941. /// showdigit <value>[,<type>];
  16942. BUILDIN_FUNC(showdigit)
  16943. {
  16944. unsigned int type = 0;
  16945. int value;
  16946. struct map_session_data* sd;
  16947. if( ( sd = script_rid2sd(st) ) == NULL )
  16948. {
  16949. return SCRIPT_CMD_SUCCESS;
  16950. }
  16951. value = script_getnum(st,2);
  16952. if( script_hasdata(st,3) )
  16953. {
  16954. type = script_getnum(st,3);
  16955. if( type > 3 )
  16956. {
  16957. ShowError("buildin_showdigit: Invalid type %u.\n", type);
  16958. return SCRIPT_CMD_FAILURE;
  16959. }
  16960. }
  16961. clif_showdigit(sd, (unsigned char)type, value);
  16962. return SCRIPT_CMD_SUCCESS;
  16963. }
  16964. /**
  16965. * Rune Knight
  16966. * makerune <% success bonus>{,<char_id>};
  16967. **/
  16968. BUILDIN_FUNC(makerune) {
  16969. TBL_PC* sd;
  16970. if (!script_charid2sd(3,sd))
  16971. return SCRIPT_CMD_FAILURE;
  16972. clif_skill_produce_mix_list(sd,RK_RUNEMASTERY,24);
  16973. sd->itemid = script_getnum(st,2);
  16974. return SCRIPT_CMD_SUCCESS;
  16975. }
  16976. /**
  16977. * checkdragon({<char_id>}) returns 1 if mounting a dragon or 0 otherwise.
  16978. **/
  16979. BUILDIN_FUNC(checkdragon) {
  16980. TBL_PC* sd;
  16981. if (!script_charid2sd(2,sd))
  16982. return SCRIPT_CMD_FAILURE;
  16983. if( pc_isridingdragon(sd) )
  16984. script_pushint(st,1);
  16985. else
  16986. script_pushint(st,0);
  16987. return SCRIPT_CMD_SUCCESS;
  16988. }
  16989. /**
  16990. * setdragon({optional Color{,<char_id>}}) returns 1 on success or 0 otherwise
  16991. * - Toggles the dragon on a RK if he can mount;
  16992. * @param Color - when not provided uses the green dragon;
  16993. * - 1 : Green Dragon
  16994. * - 2 : Brown Dragon
  16995. * - 3 : Gray Dragon
  16996. * - 4 : Blue Dragon
  16997. * - 5 : Red Dragon
  16998. **/
  16999. BUILDIN_FUNC(setdragon) {
  17000. TBL_PC* sd;
  17001. int color = script_hasdata(st,2) ? script_getnum(st,2) : 0;
  17002. if (!script_charid2sd(3,sd))
  17003. return SCRIPT_CMD_FAILURE;
  17004. if( !pc_checkskill(sd,RK_DRAGONTRAINING) || (sd->class_&MAPID_THIRDMASK) != MAPID_RUNE_KNIGHT )
  17005. script_pushint(st,0);//Doesn't have the skill or it's not a Rune Knight
  17006. else if ( pc_isridingdragon(sd) ) {//Is mounted; release
  17007. pc_setoption(sd, sd->sc.option&~OPTION_DRAGON);
  17008. script_pushint(st,1);
  17009. } else {//Not mounted; Mount now.
  17010. unsigned int option = OPTION_DRAGON1;
  17011. if( color ) {
  17012. option = ( color == 1 ? OPTION_DRAGON1 :
  17013. color == 2 ? OPTION_DRAGON2 :
  17014. color == 3 ? OPTION_DRAGON3 :
  17015. color == 4 ? OPTION_DRAGON4 :
  17016. color == 5 ? OPTION_DRAGON5 : 0);
  17017. if( !option ) {
  17018. ShowWarning("script_setdragon: Unknown Color %d used; changing to green (1)\n",color);
  17019. option = OPTION_DRAGON1;
  17020. }
  17021. }
  17022. pc_setoption(sd, sd->sc.option|option);
  17023. script_pushint(st,1);
  17024. }
  17025. return SCRIPT_CMD_SUCCESS;
  17026. }
  17027. /**
  17028. * ismounting({<char_id>}) returns 1 if mounting a new mount or 0 otherwise
  17029. **/
  17030. BUILDIN_FUNC(ismounting) {
  17031. TBL_PC* sd;
  17032. if (!script_charid2sd(2,sd))
  17033. return SCRIPT_CMD_FAILURE;
  17034. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17035. script_pushint(st,1);
  17036. else
  17037. script_pushint(st,0);
  17038. return SCRIPT_CMD_SUCCESS;
  17039. }
  17040. /**
  17041. * setmounting({<char_id>}) returns 1 on success or 0 otherwise
  17042. * - Toggles new mounts on a player when he can mount
  17043. * - Will fail if the player is mounting a non-new mount, e.g. dragon, peco, wug, etc.
  17044. * - Will unmount the player is he is already mounting
  17045. **/
  17046. BUILDIN_FUNC(setmounting) {
  17047. TBL_PC* sd;
  17048. if (!script_charid2sd(2,sd))
  17049. return SCRIPT_CMD_FAILURE;
  17050. if( &sd->sc && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR) ) {
  17051. clif_msg(sd, NEED_REINS_OF_MOUNT);
  17052. script_pushint(st,0); //can't mount with one of these
  17053. } else {
  17054. if( &sd->sc && sd->sc.data[SC_ALL_RIDING] )
  17055. status_change_end(&sd->bl, SC_ALL_RIDING, INVALID_TIMER); //release mount
  17056. else
  17057. sc_start(NULL, &sd->bl, SC_ALL_RIDING, 10000, 1, INVALID_TIMER); //mount
  17058. script_pushint(st,1);//in both cases, return 1.
  17059. }
  17060. return SCRIPT_CMD_SUCCESS;
  17061. }
  17062. /**
  17063. * Retrieves quantity of arguments provided to callfunc/callsub.
  17064. * getargcount() -> amount of arguments received in a function
  17065. **/
  17066. BUILDIN_FUNC(getargcount) {
  17067. struct script_retinfo* ri;
  17068. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO ) {
  17069. ShowError("script:getargcount: used out of function or callsub label!\n");
  17070. st->state = END;
  17071. return SCRIPT_CMD_FAILURE;
  17072. }
  17073. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  17074. script_pushint(st, ri->nargs);
  17075. return SCRIPT_CMD_SUCCESS;
  17076. }
  17077. /**
  17078. * getcharip(<account ID>/<character ID>/<character name>)
  17079. **/
  17080. BUILDIN_FUNC(getcharip)
  17081. {
  17082. struct map_session_data* sd = NULL;
  17083. /* check if a character name is specified */
  17084. if( script_hasdata(st, 2) )
  17085. {
  17086. struct script_data *data;
  17087. data = script_getdata(st, 2);
  17088. get_val(st, data); // Convert into value in case of a variable
  17089. if (data_isstring(data))
  17090. sd = map_nick2sd(script_getstr(st, 2));
  17091. else if (data_isint(data) || script_getnum(st, 2))
  17092. {
  17093. int id = 0;
  17094. id = script_getnum(st, 2);
  17095. sd = (map_id2sd(id) ? map_id2sd(id) : map_charid2sd(id));
  17096. }
  17097. }
  17098. else
  17099. sd = script_rid2sd(st);
  17100. /* check for sd and IP */
  17101. if (!sd || !session[sd->fd]->client_addr)
  17102. {
  17103. script_pushconststr(st, "");
  17104. return SCRIPT_CMD_SUCCESS;
  17105. }
  17106. /* return the client ip_addr converted for output */
  17107. if (sd && sd->fd && session[sd->fd])
  17108. {
  17109. /* initiliaze */
  17110. const char *ip_addr = NULL;
  17111. uint32 ip;
  17112. /* set ip, ip_addr and convert to ip and push str */
  17113. ip = session[sd->fd]->client_addr;
  17114. ip_addr = ip2str(ip, NULL);
  17115. script_pushstrcopy(st, ip_addr);
  17116. }
  17117. return SCRIPT_CMD_SUCCESS;
  17118. }
  17119. /**
  17120. * is_function(<function name>) -> 1 if function exists, 0 otherwise
  17121. **/
  17122. BUILDIN_FUNC(is_function) {
  17123. const char* str = script_getstr(st,2);
  17124. if( strdb_exists(userfunc_db, str) )
  17125. script_pushint(st,1);
  17126. else
  17127. script_pushint(st,0);
  17128. return SCRIPT_CMD_SUCCESS;
  17129. }
  17130. /**
  17131. * get_revision() -> retrieves the current svn revision (if available)
  17132. **/
  17133. BUILDIN_FUNC(get_revision) {
  17134. const char *svn = get_svn_revision();
  17135. if ( svn[0] != UNKNOWN_VERSION )
  17136. script_pushint(st,atoi(svn));
  17137. else
  17138. script_pushint(st,-1); //unknown
  17139. return SCRIPT_CMD_SUCCESS;
  17140. }
  17141. /* get_hash() -> retrieves the current git hash (if available)*/
  17142. BUILDIN_FUNC(get_githash) {
  17143. const char* git = get_git_hash();
  17144. char buf[CHAT_SIZE_MAX];
  17145. safestrncpy(buf,git,strlen(git)+1);
  17146. if ( git[0] != UNKNOWN_VERSION )
  17147. script_pushstr(st,buf);
  17148. else
  17149. script_pushstr(st,"Unknown"); //unknown
  17150. return SCRIPT_CMD_SUCCESS;
  17151. }
  17152. /**
  17153. * freeloop(<toggle>) -> toggles this script instance's looping-check ability
  17154. **/
  17155. BUILDIN_FUNC(freeloop) {
  17156. if( script_hasdata(st,2) ) {
  17157. if( script_getnum(st,2) )
  17158. st->freeloop = 1;
  17159. else
  17160. st->freeloop = 0;
  17161. }
  17162. script_pushint(st, st->freeloop);
  17163. return SCRIPT_CMD_SUCCESS;
  17164. }
  17165. /**
  17166. * @commands (script based)
  17167. **/
  17168. BUILDIN_FUNC(bindatcmd) {
  17169. const char* atcmd;
  17170. const char* eventName;
  17171. int i, level = 0, level2 = 0;
  17172. bool create = false;
  17173. atcmd = script_getstr(st,2);
  17174. eventName = script_getstr(st,3);
  17175. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17176. atcmd++;
  17177. if( script_hasdata(st,4) ) level = script_getnum(st,4);
  17178. if( script_hasdata(st,5) ) level2 = script_getnum(st,5);
  17179. if( atcmd_binding_count == 0 ) {
  17180. CREATE(atcmd_binding,struct atcmd_binding_data*,1);
  17181. create = true;
  17182. } else {
  17183. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command,atcmd) == 0);
  17184. if( i < atcmd_binding_count ) {/* update existent entry */
  17185. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17186. atcmd_binding[i]->level = level;
  17187. atcmd_binding[i]->level2 = level2;
  17188. } else
  17189. create = true;
  17190. }
  17191. if( create ) {
  17192. i = atcmd_binding_count;
  17193. if( atcmd_binding_count++ != 0 )
  17194. RECREATE(atcmd_binding,struct atcmd_binding_data*,atcmd_binding_count);
  17195. CREATE(atcmd_binding[i],struct atcmd_binding_data,1);
  17196. safestrncpy(atcmd_binding[i]->command, atcmd, 50);
  17197. safestrncpy(atcmd_binding[i]->npc_event, eventName, 50);
  17198. atcmd_binding[i]->level = level;
  17199. atcmd_binding[i]->level2 = level2;
  17200. }
  17201. return SCRIPT_CMD_SUCCESS;
  17202. }
  17203. BUILDIN_FUNC(unbindatcmd) {
  17204. const char* atcmd;
  17205. int i = 0;
  17206. atcmd = script_getstr(st, 2);
  17207. if( *atcmd == atcommand_symbol || *atcmd == charcommand_symbol )
  17208. atcmd++;
  17209. if( atcmd_binding_count == 0 ) {
  17210. script_pushint(st, 0);
  17211. return SCRIPT_CMD_SUCCESS;
  17212. }
  17213. ARR_FIND(0, atcmd_binding_count, i, strcmp(atcmd_binding[i]->command, atcmd) == 0);
  17214. if( i < atcmd_binding_count ) {
  17215. int cursor = 0;
  17216. aFree(atcmd_binding[i]);
  17217. atcmd_binding[i] = NULL;
  17218. /* compact the list now that we freed a slot somewhere */
  17219. for( i = 0, cursor = 0; i < atcmd_binding_count; i++ ) {
  17220. if( atcmd_binding[i] == NULL )
  17221. continue;
  17222. if( cursor != i ) {
  17223. memmove(&atcmd_binding[cursor], &atcmd_binding[i], sizeof(struct atcmd_binding_data*));
  17224. }
  17225. cursor++;
  17226. }
  17227. if( (atcmd_binding_count = cursor) == 0 )
  17228. aFree(atcmd_binding);
  17229. script_pushint(st, 1);
  17230. } else
  17231. script_pushint(st, 0);/* not found */
  17232. return SCRIPT_CMD_SUCCESS;
  17233. }
  17234. BUILDIN_FUNC(useatcmd) {
  17235. return atcommand_sub(st,3);
  17236. }
  17237. BUILDIN_FUNC(checkre)
  17238. {
  17239. int num;
  17240. num=script_getnum(st,2);
  17241. switch(num){
  17242. case 0:
  17243. #ifdef RENEWAL
  17244. script_pushint(st, 1);
  17245. #else
  17246. script_pushint(st, 0);
  17247. #endif
  17248. break;
  17249. case 1:
  17250. #ifdef RENEWAL_CAST
  17251. script_pushint(st, 1);
  17252. #else
  17253. script_pushint(st, 0);
  17254. #endif
  17255. break;
  17256. case 2:
  17257. #ifdef RENEWAL_DROP
  17258. script_pushint(st, 1);
  17259. #else
  17260. script_pushint(st, 0);
  17261. #endif
  17262. break;
  17263. case 3:
  17264. #ifdef RENEWAL_EXP
  17265. script_pushint(st, 1);
  17266. #else
  17267. script_pushint(st, 0);
  17268. #endif
  17269. break;
  17270. case 4:
  17271. #ifdef RENEWAL_LVDMG
  17272. script_pushint(st, 1);
  17273. #else
  17274. script_pushint(st, 0);
  17275. #endif
  17276. break;
  17277. case 5:
  17278. #ifdef RENEWAL_ASPD
  17279. script_pushint(st, 1);
  17280. #else
  17281. script_pushint(st, 0);
  17282. #endif
  17283. break;
  17284. default:
  17285. ShowWarning("buildin_checkre: unknown parameter.\n");
  17286. break;
  17287. }
  17288. return SCRIPT_CMD_SUCCESS;
  17289. }
  17290. /* getrandgroupitem <group_id>{,<quantity>{,<sub_group>}} */
  17291. BUILDIN_FUNC(getrandgroupitem) {
  17292. TBL_PC* sd;
  17293. int i, get_count = 0;
  17294. uint16 group = script_getnum(st,2), qty = 0;
  17295. uint8 sub_group = 1;
  17296. struct item item_tmp;
  17297. struct s_item_group_entry *entry = NULL;
  17298. if (!(sd = script_rid2sd(st)))
  17299. return SCRIPT_CMD_SUCCESS;
  17300. if (!group) {
  17301. ShowError("buildin_getrandgroupitem: Invalid group id (%d)!\n",script_getnum(st,2));
  17302. return SCRIPT_CMD_FAILURE;
  17303. }
  17304. FETCH(3, qty);
  17305. FETCH(4, sub_group);
  17306. entry = itemdb_get_randgroupitem(group,sub_group);
  17307. if (!entry)
  17308. return SCRIPT_CMD_FAILURE; //ensure valid itemid
  17309. memset(&item_tmp,0,sizeof(item_tmp));
  17310. item_tmp.nameid = entry->nameid;
  17311. item_tmp.identify = itemdb_isidentified(entry->nameid);
  17312. if (!qty)
  17313. qty = entry->amount;
  17314. //Check if it's stackable.
  17315. if (!itemdb_isstackable(entry->nameid)) {
  17316. item_tmp.amount = 1;
  17317. get_count = qty;
  17318. }
  17319. else {
  17320. item_tmp.amount = qty;
  17321. get_count = 1;
  17322. }
  17323. for (i = 0; i < get_count; i++) {
  17324. // if not pet egg
  17325. if (!pet_create_egg(sd, entry->nameid)) {
  17326. unsigned char flag = 0;
  17327. if ((flag = pc_additem(sd,&item_tmp,item_tmp.amount,LOG_TYPE_SCRIPT))) {
  17328. clif_additem(sd,0,0,flag);
  17329. if (pc_candrop(sd,&item_tmp))
  17330. map_addflooritem(&item_tmp,item_tmp.amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  17331. }
  17332. }
  17333. }
  17334. return SCRIPT_CMD_SUCCESS;
  17335. }
  17336. /* getgroupitem <group_id>{,<char_id>};
  17337. * Gives item(s) to the attached player based on item group contents
  17338. */
  17339. BUILDIN_FUNC(getgroupitem) {
  17340. TBL_PC *sd;
  17341. int group_id = script_getnum(st,2);
  17342. if (!script_charid2sd(3,sd))
  17343. return SCRIPT_CMD_SUCCESS;
  17344. if (itemdb_pc_get_itemgroup(group_id,sd)) {
  17345. ShowError("buildin_getgroupitem: Invalid group id '%d' specified.\n",group_id);
  17346. return SCRIPT_CMD_FAILURE;
  17347. }
  17348. return SCRIPT_CMD_SUCCESS;
  17349. }
  17350. /* cleanmap <map_name>;
  17351. * cleanarea <map_name>, <x0>, <y0>, <x1>, <y1>; */
  17352. static int atcommand_cleanfloor_sub(struct block_list *bl, va_list ap)
  17353. {
  17354. nullpo_ret(bl);
  17355. map_clearflooritem(bl);
  17356. return SCRIPT_CMD_SUCCESS;
  17357. }
  17358. BUILDIN_FUNC(cleanmap)
  17359. {
  17360. const char *mapname;
  17361. int16 m;
  17362. mapname = script_getstr(st, 2);
  17363. m = map_mapname2mapid(mapname);
  17364. if (!m)
  17365. return SCRIPT_CMD_FAILURE;
  17366. if ((script_lastdata(st) - 2) < 4) {
  17367. map_foreachinmap(atcommand_cleanfloor_sub, m, BL_ITEM);
  17368. } else {
  17369. int16 x0 = script_getnum(st, 3);
  17370. int16 y0 = script_getnum(st, 4);
  17371. int16 x1 = script_getnum(st, 5);
  17372. int16 y1 = script_getnum(st, 6);
  17373. if (x0 > 0 && y0 > 0 && x1 > 0 && y1 > 0) {
  17374. map_foreachinarea(atcommand_cleanfloor_sub, m, x0, y0, x1, y1, BL_ITEM);
  17375. } else {
  17376. ShowError("cleanarea: invalid coordinate defined!\n");
  17377. return SCRIPT_CMD_FAILURE;
  17378. }
  17379. }
  17380. return SCRIPT_CMD_SUCCESS;
  17381. }
  17382. /* Cast a skill on the attached player.
  17383. * npcskill <skill id>, <skill lvl>, <stat point>, <NPC level>;
  17384. * npcskill "<skill name>", <skill lvl>, <stat point>, <NPC level>; */
  17385. BUILDIN_FUNC(npcskill)
  17386. {
  17387. uint16 skill_id;
  17388. unsigned short skill_level;
  17389. unsigned int stat_point;
  17390. unsigned int npc_level;
  17391. struct npc_data *nd;
  17392. struct map_session_data *sd;
  17393. struct script_data *data;
  17394. data = script_getdata(st, 2);
  17395. get_val(st, data); // Convert into value in case of a variable
  17396. skill_id = data_isstring(data) ? skill_name2id(script_getstr(st, 2)) : script_getnum(st, 2);
  17397. skill_level = script_getnum(st, 3);
  17398. stat_point = script_getnum(st, 4);
  17399. npc_level = script_getnum(st, 5);
  17400. sd = script_rid2sd(st);
  17401. nd = (struct npc_data *)map_id2bl(sd->npc_id);
  17402. if (stat_point > battle_config.max_third_parameter) {
  17403. ShowError("npcskill: stat point exceeded maximum of %d.\n",battle_config.max_third_parameter );
  17404. return SCRIPT_CMD_FAILURE;
  17405. }
  17406. if (npc_level > MAX_LEVEL) {
  17407. ShowError("npcskill: level exceeded maximum of %d.\n", MAX_LEVEL);
  17408. return SCRIPT_CMD_FAILURE;
  17409. }
  17410. if (sd == NULL || nd == NULL) { //ain't possible, but I don't trust people.
  17411. return SCRIPT_CMD_FAILURE;
  17412. }
  17413. nd->level = npc_level;
  17414. nd->stat_point = stat_point;
  17415. if (!nd->status.hp)
  17416. status_calc_npc(nd, SCO_FIRST);
  17417. else
  17418. status_calc_npc(nd, SCO_NONE);
  17419. if (skill_get_inf(skill_id)&INF_GROUND_SKILL)
  17420. unit_skilluse_pos(&nd->bl, sd->bl.x, sd->bl.y, skill_id, skill_level);
  17421. else
  17422. unit_skilluse_id(&nd->bl, sd->bl.id, skill_id, skill_level);
  17423. return SCRIPT_CMD_SUCCESS;
  17424. }
  17425. /* Consumes an item.
  17426. * consumeitem <item id>{,<char_id>};
  17427. * consumeitem "<item name>"{,<char_id>};
  17428. * @param item: Item ID or name
  17429. */
  17430. BUILDIN_FUNC(consumeitem)
  17431. {
  17432. TBL_PC *sd;
  17433. struct script_data *data;
  17434. struct item_data *item_data;
  17435. if (!script_charid2sd(3, sd))
  17436. return SCRIPT_CMD_FAILURE;
  17437. data = script_getdata( st, 2 );
  17438. get_val( st, data );
  17439. if( data_isstring( data ) ){
  17440. const char *name = conv_str( st, data );
  17441. if( ( item_data = itemdb_searchname( name ) ) == NULL ){
  17442. ShowError( "buildin_consumeitem: Nonexistant item %s requested.\n", name );
  17443. return SCRIPT_CMD_FAILURE;
  17444. }
  17445. }else if( data_isint( data ) ){
  17446. unsigned short nameid = conv_num( st, data );
  17447. if( ( item_data = itemdb_exists( nameid ) ) == NULL ){
  17448. ShowError("buildin_consumeitem: Nonexistant item %hu requested.\n", nameid );
  17449. return SCRIPT_CMD_FAILURE;
  17450. }
  17451. }else{
  17452. ShowError("buildin_consumeitem: invalid data type for argument #1 (%d).\n", data->type );
  17453. return SCRIPT_CMD_FAILURE;
  17454. }
  17455. run_script( item_data->script, 0, sd->bl.id, 0 );
  17456. return SCRIPT_CMD_SUCCESS;
  17457. }
  17458. /** Makes a player sit/stand.
  17459. * sit {"<character name>"};
  17460. * stand {"<character name>"};
  17461. * Note: Use readparam(Sitting) which returns 1 or 0 (sitting or standing).
  17462. * @param name: Player name that will be invoked
  17463. */
  17464. BUILDIN_FUNC(sit)
  17465. {
  17466. TBL_PC *sd;
  17467. if( script_hasdata(st, 2) )
  17468. sd = map_nick2sd(script_getstr(st, 2));
  17469. else
  17470. sd = script_rid2sd(st);
  17471. if( sd == NULL)
  17472. return SCRIPT_CMD_FAILURE;
  17473. if( !pc_issit(sd) ) {
  17474. pc_setsit(sd);
  17475. skill_sit(sd, 1);
  17476. clif_sitting(&sd->bl);
  17477. }
  17478. return SCRIPT_CMD_SUCCESS;
  17479. }
  17480. /** Makes player to stand
  17481. * @param name: Player name that will be set
  17482. */
  17483. BUILDIN_FUNC(stand)
  17484. {
  17485. TBL_PC *sd;
  17486. if( script_hasdata(st, 2) )
  17487. sd = map_nick2sd(script_getstr(st, 2));
  17488. else
  17489. sd = script_rid2sd(st);
  17490. if( sd == NULL)
  17491. return SCRIPT_CMD_FAILURE;
  17492. if( pc_issit(sd) && pc_setstand(sd, false)) {
  17493. skill_sit(sd, 0);
  17494. clif_standing(&sd->bl);
  17495. }
  17496. return SCRIPT_CMD_SUCCESS;
  17497. }
  17498. /** Creates an array of bounded item IDs
  17499. * countbound {<type>{,<char_id>}};
  17500. * @param type: 0 - All bound items; 1 - Account Bound; 2 - Guild Bound; 3 - Party Bound
  17501. * @return amt: Amount of items found
  17502. */
  17503. BUILDIN_FUNC(countbound)
  17504. {
  17505. int i, type, j = 0, k = 0;
  17506. TBL_PC *sd;
  17507. if (!script_charid2sd(3,sd))
  17508. return SCRIPT_CMD_FAILURE;
  17509. type = script_getnum(st,2);
  17510. for( i = 0; i < MAX_INVENTORY; i ++ ) {
  17511. if( sd->status.inventory[i].nameid > 0 && (
  17512. (!type && sd->status.inventory[i].bound) || (type && sd->status.inventory[i].bound == type)
  17513. ))
  17514. {
  17515. pc_setreg(sd,reference_uid(add_str("@bound_items"), k),sd->status.inventory[i].nameid);
  17516. k++;
  17517. j += sd->status.inventory[i].amount;
  17518. }
  17519. }
  17520. script_pushint(st,j);
  17521. return SCRIPT_CMD_SUCCESS;
  17522. }
  17523. /** Creates new party
  17524. * party_create "<party name>"{,<char id>{,<item share>{,<item share type>}}};
  17525. * @param party_name: String of party name that will be created
  17526. * @param char_id: Chara that will be leader of this new party. If no char_id specified, the invoker will be party leader
  17527. * @param item_share: 0-Each Take. 1-Party Share
  17528. * @param item_share_type: 0-Each Take. 1-Even Share
  17529. * @return val: Result value
  17530. * -3 - party name is exist
  17531. * -2 - player is in party already
  17532. * -1 - player is not found
  17533. * 0 - unknown error
  17534. * 1 - success, will return party id $@party_create_id
  17535. */
  17536. BUILDIN_FUNC(party_create)
  17537. {
  17538. char party_name[NAME_LENGTH];
  17539. int item1 = 0, item2 = 0;
  17540. TBL_PC *sd;
  17541. if( (!script_hasdata(st,3) && !(sd = script_rid2sd(st))) || (script_hasdata(st,3) && !(sd = map_charid2sd(script_getnum(st,3)))) ) {
  17542. script_pushint(st,-1);
  17543. return SCRIPT_CMD_FAILURE;
  17544. }
  17545. if( sd->status.party_id ) {
  17546. script_pushint(st,-2);
  17547. return SCRIPT_CMD_FAILURE;
  17548. }
  17549. safestrncpy(party_name,script_getstr(st,2),NAME_LENGTH);
  17550. trim(party_name);
  17551. if( party_searchname(party_name) ) {
  17552. script_pushint(st,-3);
  17553. return SCRIPT_CMD_FAILURE;
  17554. }
  17555. if( script_getnum(st,4) )
  17556. item1 = 1;
  17557. if( script_getnum(st,5) )
  17558. item2 = 1;
  17559. party_create_byscript = 1;
  17560. script_pushint(st,party_create(sd,party_name,item1,item2));
  17561. return SCRIPT_CMD_SUCCESS;
  17562. }
  17563. /** Adds player to specified party
  17564. * party_addmember <party id>,<char id>;
  17565. * @param party_id: The party that will be entered by player
  17566. * @param char_id: Char id of player that will be joined to the party
  17567. * @return val: Result value
  17568. * -4 - party is full
  17569. * -3 - party is not found
  17570. * -2 - player is in party already
  17571. * -1 - player is not found
  17572. * 0 - unknown error
  17573. * 1 - success
  17574. */
  17575. BUILDIN_FUNC(party_addmember)
  17576. {
  17577. int party_id = script_getnum(st,2);
  17578. TBL_PC *sd;
  17579. struct party_data *party;
  17580. if( !(sd = map_charid2sd(script_getnum(st,3))) ) {
  17581. script_pushint(st,-1);
  17582. return SCRIPT_CMD_FAILURE;
  17583. }
  17584. if( sd->status.party_id ) {
  17585. script_pushint(st,-2);
  17586. return SCRIPT_CMD_FAILURE;
  17587. }
  17588. if( !(party = party_search(party_id)) ) {
  17589. script_pushint(st,-3);
  17590. return SCRIPT_CMD_FAILURE;
  17591. }
  17592. if( party->party.count >= MAX_PARTY ) {
  17593. script_pushint(st,-4);
  17594. return SCRIPT_CMD_FAILURE;
  17595. }
  17596. sd->party_invite = party_id;
  17597. script_pushint(st,party_add_member(party_id,sd));
  17598. return SCRIPT_CMD_SUCCESS;
  17599. }
  17600. /** Removes player from his/her party. If party_id and char_id is empty remove the invoker from his/her party
  17601. * party_delmember {<char id>,<party_id>};
  17602. * @param: char_id
  17603. * @param: party_id
  17604. * @return val: Result value
  17605. * -3 - player is not in party
  17606. * -2 - party is not found
  17607. * -1 - player is not found
  17608. * 0 - unknown error
  17609. * 1 - success
  17610. */
  17611. BUILDIN_FUNC(party_delmember)
  17612. {
  17613. TBL_PC *sd = NULL;
  17614. if( !script_hasdata(st,2) && !script_hasdata(st,3) && !(sd = script_rid2sd(st)) ) {
  17615. script_pushint(st,-1);
  17616. return SCRIPT_CMD_FAILURE;
  17617. }
  17618. if( sd || (script_getnum(st,2) && (sd = map_charid2sd(script_getnum(st,2)))) )
  17619. script_pushint(st,party_removemember2(sd,0,0));
  17620. else
  17621. script_pushint(st,party_removemember2(NULL,script_getnum(st,2),script_getnum(st,3)));
  17622. return SCRIPT_CMD_SUCCESS;
  17623. }
  17624. /** Changes party leader of specified party (even the leader is offline)
  17625. * party_changeleader <party id>,<char id>;
  17626. * @param party_id: ID of party
  17627. * @param char_id: Char ID of new leader
  17628. * @return val: Result value
  17629. * -4 - player is party leader already
  17630. * -3 - player is not in this party
  17631. * -2 - player is not found
  17632. * -1 - party is not found
  17633. * 0 - unknown error
  17634. * 1 - success */
  17635. BUILDIN_FUNC(party_changeleader)
  17636. {
  17637. int i, party_id = script_getnum(st,2);
  17638. TBL_PC *sd = NULL;
  17639. TBL_PC *tsd = NULL;
  17640. struct party_data *party = NULL;
  17641. if( !(party = party_search(party_id)) ) {
  17642. script_pushint(st,-1);
  17643. return SCRIPT_CMD_FAILURE;
  17644. }
  17645. if( !(tsd = map_charid2sd(script_getnum(st,3))) ) {
  17646. script_pushint(st,-2);
  17647. return SCRIPT_CMD_FAILURE;
  17648. }
  17649. if( tsd->status.party_id != party_id ) {
  17650. script_pushint(st,-3);
  17651. return SCRIPT_CMD_FAILURE;
  17652. }
  17653. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17654. if( i >= MAX_PARTY ) { //this is should impossible!
  17655. script_pushint(st,0);
  17656. return SCRIPT_CMD_FAILURE;
  17657. }
  17658. if( party->data[i].sd == tsd ) {
  17659. script_pushint(st,-4);
  17660. return SCRIPT_CMD_FAILURE;
  17661. }
  17662. script_pushint(st,party_changeleader(sd,tsd,party));
  17663. return SCRIPT_CMD_SUCCESS;
  17664. }
  17665. /** Changes party option
  17666. * party_changeoption <party id>,<option>,<flag>;
  17667. * @param party_id: ID of party that will be changed
  17668. * @param option: Type of option
  17669. * @return val: -1 - party is not found, 0 - invalid option, 1 - success
  17670. */
  17671. BUILDIN_FUNC(party_changeoption)
  17672. {
  17673. struct party_data *party;
  17674. if( !(party = party_search(script_getnum(st,2))) ) {
  17675. script_pushint(st,-1);
  17676. return SCRIPT_CMD_FAILURE;
  17677. }
  17678. script_pushint(st,party_setoption(party,script_getnum(st,3),script_getnum(st,4)));
  17679. return SCRIPT_CMD_SUCCESS;
  17680. }
  17681. /** Destroys party with party id.
  17682. * party_destroy <party id>;
  17683. * @param party_id: ID of party that will be destroyed
  17684. */
  17685. BUILDIN_FUNC(party_destroy)
  17686. {
  17687. int i;
  17688. struct party_data *party;
  17689. if( !(party = party_search(script_getnum(st,2))) ) {
  17690. script_pushint(st,0);
  17691. return SCRIPT_CMD_FAILURE;
  17692. }
  17693. ARR_FIND(0,MAX_PARTY,i,party->party.member[i].leader);
  17694. if( i >= MAX_PARTY || !party->data[i].sd ) { //leader not online
  17695. int j;
  17696. for( j = 0; j < MAX_PARTY; j++ ) {
  17697. TBL_PC *sd = party->data[j].sd;
  17698. if(sd)
  17699. party_member_withdraw(party->party.party_id,sd->status.account_id,sd->status.char_id);
  17700. else if( party->party.member[j].char_id )
  17701. intif_party_leave(party->party.party_id,party->party.member[j].account_id,party->party.member[j].char_id);
  17702. }
  17703. party_broken(party->party.party_id);
  17704. script_pushint(st,1);
  17705. }
  17706. else //leader leave = party broken
  17707. script_pushint(st,party_leave(party->data[i].sd));
  17708. return SCRIPT_CMD_SUCCESS;
  17709. }
  17710. /** Checks if a player's client version meets a required version or date.
  17711. * @param type: 0 - check by version number; 1 - check by date
  17712. * @param data: Input
  17713. */
  17714. BUILDIN_FUNC(is_clientver) {
  17715. TBL_PC *sd = NULL;
  17716. int type = script_getnum(st,2);
  17717. int data = script_getnum(st,3);
  17718. int ret = 0;
  17719. if (script_hasdata(st,4))
  17720. sd = map_charid2sd(script_getnum(st,4));
  17721. else
  17722. sd = script_rid2sd(st);
  17723. if (sd == NULL) {
  17724. script_pushint(st,0);
  17725. return SCRIPT_CMD_FAILURE;
  17726. }
  17727. switch(type){
  17728. case 0:
  17729. ret = (sd->packet_ver >= data)?1:0;
  17730. break;
  17731. case 1:
  17732. ret = (sd->packet_ver >= date2version(data))?1:0;
  17733. break;
  17734. }
  17735. script_pushint(st,ret);
  17736. return SCRIPT_CMD_SUCCESS;
  17737. }
  17738. /** Returns various information about a player's VIP status. Need to enable VIP system
  17739. * vip_status <type>,{"<character name>"};
  17740. * @param type: Info type, 1: VIP status, 2: Expired date, 3: Remaining time
  17741. * @param name: Character name (Optional)
  17742. */
  17743. BUILDIN_FUNC(vip_status) {
  17744. #ifdef VIP_ENABLE
  17745. TBL_PC *sd;
  17746. char vip_str[26];
  17747. time_t now = time(NULL);
  17748. int type = script_getnum(st, 2);
  17749. if (script_hasdata(st, 3))
  17750. sd = map_nick2sd(script_getstr(st, 3));
  17751. else
  17752. sd = script_rid2sd(st);
  17753. if (sd == NULL)
  17754. return SCRIPT_CMD_FAILURE;
  17755. switch(type) {
  17756. case 1: // Get VIP status.
  17757. script_pushint(st, pc_isvip(sd));
  17758. break;
  17759. case 2: // Get VIP expire date.
  17760. if (pc_isvip(sd)) {
  17761. time_t viptime = sd->vip.time;
  17762. strftime(vip_str, 24, "%Y-%m-%d %H:%M", localtime(&viptime));
  17763. vip_str[25] = '\0';
  17764. script_pushstrcopy(st, vip_str);
  17765. } else
  17766. script_pushint(st, 0);
  17767. break;
  17768. case 3: // Get remaining time.
  17769. if (pc_isvip(sd)) {
  17770. time_t viptime_remain = sd->vip.time - now;
  17771. int year=0,month=0,day=0,hour=0,min=0,sec=0;
  17772. split_time((int)viptime_remain,&year,&month,&day,&hour,&min,&sec);
  17773. safesnprintf(vip_str,sizeof(vip_str),"%d-%d-%d %d:%d",year,month,day,hour,min);
  17774. script_pushstrcopy(st, vip_str);
  17775. } else
  17776. script_pushint(st, 0);
  17777. break;
  17778. }
  17779. #else
  17780. script_pushint(st, 0);
  17781. #endif
  17782. return SCRIPT_CMD_SUCCESS;
  17783. }
  17784. /** Adds or removes VIP time in minutes. Need to enable VIP system
  17785. * vip_time <time in mn>,{"<character name>"};
  17786. * @param time: VIP duration in minutes. If time < 0 remove time, else add time.
  17787. * @param name: Character name (optional)
  17788. */
  17789. BUILDIN_FUNC(vip_time) {
  17790. #ifdef VIP_ENABLE //would be a pain for scripting npc otherwise
  17791. TBL_PC *sd;
  17792. int viptime = script_getnum(st, 2) * 60; // Convert since it's given in minutes.
  17793. if (script_hasdata(st, 3))
  17794. sd = map_nick2sd(script_getstr(st, 3));
  17795. else
  17796. sd = script_rid2sd(st);
  17797. if (sd == NULL)
  17798. return SCRIPT_CMD_FAILURE;
  17799. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, viptime, 7, 0);
  17800. #endif
  17801. return SCRIPT_CMD_SUCCESS;
  17802. }
  17803. /** Turns a player into a monster and grants SC attribute effect. [malufett/Hercules]
  17804. * montransform <monster name/ID>, <duration>, <sc type>, <val1>, <val2>, <val3>, <val4>;
  17805. * @param monster: Monster ID or name
  17806. * @param duration: Transform duration in millisecond (ms)
  17807. * @param sc_type: Type of SC that will be affected during the transformation
  17808. * @param val1: Value for SC
  17809. * @param val2: Value for SC
  17810. * @param val3: Value for SC
  17811. * @param val4: Value for SC
  17812. */
  17813. BUILDIN_FUNC(montransform) {
  17814. TBL_PC *sd;
  17815. enum sc_type type;
  17816. int tick, mob_id, val1, val2, val3, val4;
  17817. struct script_data *data;
  17818. val1 = val2 = val3 = val4 = 0;
  17819. if( (sd = script_rid2sd(st)) == NULL )
  17820. return SCRIPT_CMD_FAILURE;
  17821. data = script_getdata(st, 2);
  17822. get_val(st, data); // Convert into value in case of a variable
  17823. if( data_isstring(data) )
  17824. mob_id = mobdb_searchname(script_getstr(st, 2));
  17825. else
  17826. mob_id = mobdb_checkid(script_getnum(st, 2));
  17827. tick = script_getnum(st, 3);
  17828. if (script_hasdata(st, 4))
  17829. type = (sc_type)script_getnum(st, 4);
  17830. else
  17831. type = SC_NONE;
  17832. if (mob_id == 0) {
  17833. if( data_isstring(data) )
  17834. ShowWarning("buildin_montransform: Attempted to use non-existing monster '%s'.\n", script_getstr(st, 2));
  17835. else
  17836. ShowWarning("buildin_montransform: Attempted to use non-existing monster of ID '%d'.\n", script_getnum(st, 2));
  17837. return SCRIPT_CMD_FAILURE;
  17838. }
  17839. if (mob_id == MOBID_EMPERIUM) {
  17840. ShowWarning("buildin_montransform: Monster 'Emperium' cannot be used.\n");
  17841. return SCRIPT_CMD_FAILURE;
  17842. }
  17843. if (!(type >= SC_NONE && type < SC_MAX)) {
  17844. ShowWarning("buildin_montransform: Unsupported status change id %d\n", type);
  17845. return SCRIPT_CMD_FAILURE;
  17846. }
  17847. if (script_hasdata(st, 5))
  17848. val1 = script_getnum(st, 5);
  17849. if (script_hasdata(st, 6))
  17850. val2 = script_getnum(st, 6);
  17851. if (script_hasdata(st, 7))
  17852. val3 = script_getnum(st, 7);
  17853. if (script_hasdata(st, 8))
  17854. val4 = script_getnum(st, 8);
  17855. if (tick != 0) {
  17856. if (battle_config.mon_trans_disable_in_gvg && map_flag_gvg2(sd->bl.m)) {
  17857. clif_displaymessage(sd->fd, msg_txt(sd,731)); // Transforming into monster is not allowed in Guild Wars.
  17858. return SCRIPT_CMD_FAILURE;
  17859. }
  17860. if (sd->disguise){
  17861. clif_displaymessage(sd->fd, msg_txt(sd,729)); // Cannot transform into monster while in disguise.
  17862. return SCRIPT_CMD_FAILURE;
  17863. }
  17864. status_change_end(&sd->bl, SC_MONSTER_TRANSFORM, INVALID_TIMER); // Clear previous
  17865. sc_start2(NULL, &sd->bl, SC_MONSTER_TRANSFORM, 100, mob_id, type, tick);
  17866. if (type != SC_NONE)
  17867. sc_start4(NULL, &sd->bl, type, 100, val1, val2, val3, val4, tick);
  17868. }
  17869. return SCRIPT_CMD_SUCCESS;
  17870. }
  17871. /**
  17872. * Attach script to player for certain duration
  17873. * bonus_script "<script code>",<duration>{,<flag>{,<type>{,<status_icon>{,<char_id>}}}};
  17874. * @param "script code"
  17875. * @param duration
  17876. * @param flag
  17877. * @param icon
  17878. * @param char_id
  17879. * @author [Cydh]
  17880. **/
  17881. BUILDIN_FUNC(bonus_script) {
  17882. uint16 flag = 0;
  17883. int16 icon = SI_BLANK;
  17884. uint32 dur;
  17885. uint8 type = 0;
  17886. TBL_PC* sd;
  17887. const char *script_str = NULL;
  17888. struct s_bonus_script_entry *entry = NULL;
  17889. if (script_hasdata(st,7)) {
  17890. if (!(sd = map_charid2sd(script_getnum(st,7)))) {
  17891. ShowError("buildin_bonus_script: Player CID=%d is not found.\n", script_getnum(st,7));
  17892. return SCRIPT_CMD_FAILURE;
  17893. }
  17894. }
  17895. else
  17896. sd = script_rid2sd(st);
  17897. if (sd == NULL)
  17898. return SCRIPT_CMD_FAILURE;
  17899. script_str = script_getstr(st,2);
  17900. dur = 1000 * abs(script_getnum(st,3));
  17901. FETCH(4, flag);
  17902. FETCH(5, type);
  17903. FETCH(6, icon);
  17904. // No Script string, No Duration!
  17905. if (script_str[0] == '\0' || !dur) {
  17906. ShowError("buildin_bonus_script: Invalid! Script: \"%s\". Duration: %d\n", script_str, dur);
  17907. return SCRIPT_CMD_FAILURE;
  17908. }
  17909. if (strlen(script_str) >= MAX_BONUS_SCRIPT_LENGTH) {
  17910. ShowError("buildin_bonus_script: Script string to long: \"%s\".\n", script_str);
  17911. return SCRIPT_CMD_FAILURE;
  17912. }
  17913. if (icon <= SI_BLANK || icon >= SI_MAX)
  17914. icon = SI_BLANK;
  17915. if ((entry = pc_bonus_script_add(sd, script_str, dur, (enum si_type)icon, flag, type))) {
  17916. linkdb_insert(&sd->bonus_script.head, (void *)((intptr_t)entry), entry);
  17917. status_calc_pc(sd,SCO_NONE);
  17918. }
  17919. return SCRIPT_CMD_SUCCESS;
  17920. }
  17921. /**
  17922. * Removes all bonus script from player
  17923. * bonus_script_clear {<flag>,{<char_id>}};
  17924. * @param flag 0 - Except permanent bonus, 1 - With permanent bonus
  17925. * @param char_id Clear script from this character
  17926. * @author [Cydh]
  17927. */
  17928. BUILDIN_FUNC(bonus_script_clear) {
  17929. TBL_PC* sd;
  17930. bool flag = false;
  17931. if (script_hasdata(st,2))
  17932. flag = script_getnum(st,2);
  17933. if (script_hasdata(st,3))
  17934. sd = map_charid2sd(script_getnum(st,3));
  17935. else
  17936. sd = script_rid2sd(st);
  17937. if (sd == NULL)
  17938. return SCRIPT_CMD_FAILURE;
  17939. pc_bonus_script_clear(sd,(flag ? BSF_PERMANENT : BSF_REM_ALL));
  17940. return SCRIPT_CMD_SUCCESS;
  17941. }
  17942. /** Allows player to use atcommand while talking with NPC
  17943. * enable_command;
  17944. * @author [Cydh], [Kichi]
  17945. */
  17946. BUILDIN_FUNC(enable_command) {
  17947. TBL_PC* sd = script_rid2sd(st);
  17948. if (!sd)
  17949. return SCRIPT_CMD_FAILURE;
  17950. sd->state.disable_atcommand_on_npc = 0;
  17951. return SCRIPT_CMD_SUCCESS;
  17952. }
  17953. /** Prevents player to use atcommand while talking with NPC
  17954. * disable_command;
  17955. * @author [Cydh], [Kichi]
  17956. */
  17957. BUILDIN_FUNC(disable_command) {
  17958. TBL_PC* sd = script_rid2sd(st);
  17959. if (!sd)
  17960. return SCRIPT_CMD_FAILURE;
  17961. sd->state.disable_atcommand_on_npc = 1;
  17962. return SCRIPT_CMD_SUCCESS;
  17963. }
  17964. /** Get the information of the members of a guild by type.
  17965. * getguildmember <guild_id>{,<type>};
  17966. * @param guild_id: ID of guild
  17967. * @param type: Type of option (optional)
  17968. */
  17969. BUILDIN_FUNC(getguildmember)
  17970. {
  17971. struct guild *g = NULL;
  17972. uint8 j = 0;
  17973. g = guild_search(script_getnum(st,2));
  17974. if (g) {
  17975. uint8 i, type = 0;
  17976. struct script_data *data = NULL;
  17977. char *varname = NULL;
  17978. if (script_hasdata(st,3))
  17979. type = script_getnum(st,3);
  17980. if (script_hasdata(st,4)) {
  17981. data = script_getdata(st, 4);
  17982. if (!data_isreference(data)) {
  17983. ShowError("buildin_getguildmember: Error in argument! Please give a variable to store values in.\n");
  17984. return SCRIPT_CMD_FAILURE;
  17985. }
  17986. varname = reference_getname(data);
  17987. if (type <= 0 && varname[strlen(varname)-1] != '$') {
  17988. ShowError("buildin_getguildmember: The array %s is not string type.\n", varname);
  17989. return SCRIPT_CMD_FAILURE;
  17990. }
  17991. }
  17992. for (i = 0; i < MAX_GUILD; i++) {
  17993. if (g->member[i].account_id) {
  17994. switch (type) {
  17995. case 2:
  17996. if (data)
  17997. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].account_id), data->ref);
  17998. else
  17999. mapreg_setreg(reference_uid(add_str("$@guildmemberaid"), j),g->member[i].account_id);
  18000. break;
  18001. case 1:
  18002. if (data)
  18003. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].char_id), data->ref);
  18004. else
  18005. mapreg_setreg(reference_uid(add_str("$@guildmembercid"), j), g->member[i].char_id);
  18006. break;
  18007. default:
  18008. if (data)
  18009. setd_sub(st, NULL, varname, j, (void *)__64BPRTSIZE(g->member[i].name), data->ref);
  18010. else
  18011. mapreg_setregstr(reference_uid(add_str("$@guildmembername$"), j), g->member[i].name);
  18012. break;
  18013. }
  18014. j++;
  18015. }
  18016. }
  18017. }
  18018. mapreg_setreg(add_str("$@guildmembercount"), j);
  18019. script_pushint(st, j);
  18020. return SCRIPT_CMD_SUCCESS;
  18021. }
  18022. /** Adds spirit ball to player for 'duration' in milisecond
  18023. * addspiritball <count>,<duration>{,<char_id>};
  18024. * @param count How many spirit ball will be added
  18025. * @param duration How long spiritball is active until it disappears
  18026. * @param char_id Target player (Optional)
  18027. * @author [Cydh]
  18028. */
  18029. BUILDIN_FUNC(addspiritball) {
  18030. uint8 i, count = script_getnum(st,2);
  18031. uint16 duration = script_getnum(st,3);
  18032. struct map_session_data *sd = NULL;
  18033. if (count == 0)
  18034. return SCRIPT_CMD_SUCCESS;
  18035. if (script_hasdata(st,4)) {
  18036. if (!script_isstring(st,4))
  18037. sd = map_charid2sd(script_getnum(st,4));
  18038. else
  18039. sd = map_nick2sd(script_getstr(st,4));
  18040. }
  18041. else
  18042. sd = script_rid2sd(st);
  18043. if (!sd)
  18044. return SCRIPT_CMD_FAILURE;
  18045. for (i = 0; i < count; i++)
  18046. pc_addspiritball(sd,duration,10);
  18047. return SCRIPT_CMD_SUCCESS;
  18048. }
  18049. /** Deletes the spirit ball(s) from player
  18050. * delspiritball <count>{,<char_id>};
  18051. * @param count How many spirit ball will be deleted
  18052. * @param char_id Target player (Optional)
  18053. * @author [Cydh]
  18054. */
  18055. BUILDIN_FUNC(delspiritball) {
  18056. uint8 count = script_getnum(st,2);
  18057. struct map_session_data *sd = NULL;
  18058. if (count == 0)
  18059. count = 1;
  18060. if (script_hasdata(st,3)) {
  18061. if (!script_isstring(st,3))
  18062. sd = map_charid2sd(script_getnum(st,3));
  18063. else
  18064. sd = map_nick2sd(script_getstr(st,3));
  18065. }
  18066. else
  18067. sd = script_rid2sd(st);
  18068. if (!sd)
  18069. return SCRIPT_CMD_FAILURE;
  18070. pc_delspiritball(sd,count,0);
  18071. return SCRIPT_CMD_SUCCESS;
  18072. }
  18073. /** Counts the spirit ball that player has
  18074. * countspiritball {<char_id>};
  18075. * @param char_id Target player (Optional)
  18076. * @author [Cydh]
  18077. */
  18078. BUILDIN_FUNC(countspiritball) {
  18079. struct map_session_data *sd;
  18080. if (script_hasdata(st,2)) {
  18081. if (!script_isstring(st,2))
  18082. sd = map_charid2sd(script_getnum(st,2));
  18083. else
  18084. sd = map_nick2sd(script_getstr(st,2));
  18085. }
  18086. else
  18087. sd = script_rid2sd(st);
  18088. if (!sd)
  18089. return SCRIPT_CMD_FAILURE;
  18090. script_pushint(st,sd->spiritball);
  18091. return SCRIPT_CMD_SUCCESS;
  18092. }
  18093. /** Merges separated stackable items because of guid
  18094. * mergeitem {<char_id>};
  18095. * @param char_id Char ID (Optional)
  18096. * @author [Cydh]
  18097. */
  18098. BUILDIN_FUNC(mergeitem) {
  18099. struct map_session_data *sd;
  18100. if (!script_charid2sd(2, sd))
  18101. return SCRIPT_CMD_FAILURE;
  18102. clif_merge_item_open(sd);
  18103. return SCRIPT_CMD_SUCCESS;
  18104. }
  18105. /** Merges separated stackable items because of guid (Unofficial)
  18106. * mergeitem2 {<item_id>,{<char_id>}};
  18107. * mergeitem2 {"<item name>",{<char_id>}};
  18108. * @param item Item ID/Name for merging specific item (Optional)
  18109. * @author [Cydh]
  18110. */
  18111. BUILDIN_FUNC(mergeitem2) {
  18112. struct map_session_data *sd;
  18113. struct item *items = NULL;
  18114. uint16 i, count = 0;
  18115. int nameid = 0;
  18116. if (!script_charid2sd(3, sd))
  18117. return SCRIPT_CMD_FAILURE;
  18118. if (script_hasdata(st, 2)) {
  18119. struct script_data *data = script_getdata(st, 2);
  18120. get_val(st, data);
  18121. if (data_isstring(data)) {// "<item name>"
  18122. const char *name = conv_str(st,data);
  18123. struct item_data *id;
  18124. if (!(id = itemdb_searchname(name))) {
  18125. ShowError("buildin_mergeitem2: Nonexistant item %s requested.\n", name);
  18126. script_pushint(st, count);
  18127. return SCRIPT_CMD_FAILURE;
  18128. }
  18129. nameid = id->nameid;
  18130. }
  18131. else if (data_isint(data)) {// <item id>
  18132. nameid = conv_num(st,data);
  18133. if (!itemdb_exists(nameid)) {
  18134. ShowError("buildin_mergeitem: Nonexistant item %d requested.\n", nameid);
  18135. script_pushint(st, count);
  18136. return SCRIPT_CMD_FAILURE;
  18137. }
  18138. }
  18139. }
  18140. for (i = 0; i < MAX_INVENTORY; i++) {
  18141. struct item *it = &sd->status.inventory[i];
  18142. if (!it || !it->unique_id || it->expire_time || !itemdb_isstackable(it->nameid))
  18143. continue;
  18144. if ((!nameid || (nameid == it->nameid))) {
  18145. uint8 k;
  18146. if (!count) {
  18147. CREATE(items, struct item, 1);
  18148. memcpy(&items[count++], it, sizeof(struct item));
  18149. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18150. continue;
  18151. }
  18152. for (k = 0; k < count; k++) {
  18153. // Find Match
  18154. if (&items[k] && items[k].nameid == it->nameid && items[k].bound == it->bound && memcmp(items[k].card, it->card, sizeof(it->card)) == 0) {
  18155. items[k].amount += it->amount;
  18156. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18157. break;
  18158. }
  18159. }
  18160. if (k >= count) {
  18161. // New entry
  18162. RECREATE(items, struct item, count+1);
  18163. memcpy(&items[count++], it, sizeof(struct item));
  18164. pc_delitem(sd, i, it->amount, 0, 0, LOG_TYPE_NPC);
  18165. }
  18166. }
  18167. }
  18168. if (!items) // Nothing todo here
  18169. return SCRIPT_CMD_SUCCESS;
  18170. // Retrieve the items
  18171. for (i = 0; i < count; i++) {
  18172. uint8 flag = 0;
  18173. if (!&items[i])
  18174. continue;
  18175. items[i].id = 0;
  18176. items[i].unique_id = 0;
  18177. if ((flag = pc_additem(sd, &items[i], items[i].amount, LOG_TYPE_NPC)))
  18178. clif_additem(sd, i, items[i].amount, flag);
  18179. }
  18180. aFree(items);
  18181. script_pushint(st, count);
  18182. return SCRIPT_CMD_SUCCESS;
  18183. }
  18184. /**
  18185. * Update an entry from NPC shop.
  18186. * npcshopupdate "<name>",<item_id>,<price>{,<stock>}
  18187. **/
  18188. BUILDIN_FUNC(npcshopupdate) {
  18189. const char* npcname = script_getstr(st, 2);
  18190. struct npc_data* nd = npc_name2id(npcname);
  18191. uint16 nameid = script_getnum(st, 3);
  18192. int price = script_getnum(st, 4);
  18193. #if PACKETVER >= 20131223
  18194. uint16 stock = script_hasdata(st,5) ? script_getnum(st,5) : 0;
  18195. #endif
  18196. int i;
  18197. if( !nd || ( nd->subtype != NPCTYPE_SHOP && nd->subtype != NPCTYPE_CASHSHOP && nd->subtype != NPCTYPE_ITEMSHOP && nd->subtype != NPCTYPE_POINTSHOP && nd->subtype != NPCTYPE_MARKETSHOP ) ) { // Not found.
  18198. script_pushint(st,0);
  18199. return SCRIPT_CMD_FAILURE;
  18200. }
  18201. if (!nd->u.shop.count) {
  18202. ShowError("buildin_npcshopupdate: Attempt to update empty shop from '%s'.\n", nd->exname);
  18203. script_pushint(st,0);
  18204. return SCRIPT_CMD_FAILURE;
  18205. }
  18206. for (i = 0; i < nd->u.shop.count; i++) {
  18207. if (nd->u.shop.shop_item[i].nameid == nameid) {
  18208. if (price != 0)
  18209. nd->u.shop.shop_item[i].value = price;
  18210. #if PACKETVER >= 20131223
  18211. if (nd->subtype == NPCTYPE_MARKETSHOP) {
  18212. nd->u.shop.shop_item[i].qty = stock;
  18213. npc_market_tosql(nd->exname, &nd->u.shop.shop_item[i]);
  18214. }
  18215. #endif
  18216. }
  18217. }
  18218. script_pushint(st,1);
  18219. return SCRIPT_CMD_SUCCESS;
  18220. }
  18221. /**
  18222. * Get rid from running script.
  18223. * getattachedrid();
  18224. **/
  18225. BUILDIN_FUNC(getattachedrid) {
  18226. script_pushint(st, st->rid);
  18227. return SCRIPT_CMD_SUCCESS;
  18228. }
  18229. /**
  18230. * Get variable from a Player
  18231. * getvar <variable>,<char_id>;
  18232. */
  18233. BUILDIN_FUNC(getvar) {
  18234. int char_id = script_getnum(st, 3);
  18235. struct map_session_data *sd = map_charid2sd(char_id);
  18236. struct script_data *data = NULL;
  18237. const char *name = NULL;
  18238. if (!sd) {
  18239. ShowError("buildin_getvar: No player found with char id '%d'.\n", char_id);
  18240. return SCRIPT_CMD_FAILURE;
  18241. }
  18242. data = script_getdata(st, 2);
  18243. if (!data_isreference(data)) {
  18244. ShowError("buildin_getvar: Not a variable\n");
  18245. script_reportdata(data);
  18246. script_pushnil(st);
  18247. st->state = END;
  18248. return SCRIPT_CMD_FAILURE;
  18249. }
  18250. name = reference_getname(data);
  18251. if (name[0] == '.' || name[0] == '$' || name[0] == '\'') { // Not a PC variable
  18252. ShowError("buildin_getvar: Invalid scope (not PC variable)\n");
  18253. script_reportdata(data);
  18254. script_pushnil(st);
  18255. st->state = END;
  18256. return SCRIPT_CMD_FAILURE;
  18257. }
  18258. get_val_(st, data, sd);
  18259. if (data_isint(data))
  18260. script_pushint(st, conv_num_(st, data, sd));
  18261. else
  18262. script_pushstrcopy(st, conv_str_(st, data, sd));
  18263. push_val2(st->stack, C_NAME, reference_getuid(data), reference_getref(data));
  18264. return SCRIPT_CMD_SUCCESS;
  18265. }
  18266. /**
  18267. * Display script message
  18268. * showscript "<message>"{,<GID>};
  18269. **/
  18270. BUILDIN_FUNC(showscript) {
  18271. struct block_list *bl = NULL;
  18272. const char *msg = script_getstr(st,2);
  18273. int id = 0;
  18274. if (script_hasdata(st,3)) {
  18275. id = script_getnum(st,3);
  18276. bl = map_id2bl(id);
  18277. }
  18278. else {
  18279. bl = st->rid ? map_id2bl(st->rid) : map_id2bl(st->oid);
  18280. }
  18281. if (!bl) {
  18282. ShowError("buildin_showscript: Script not attached. (id=%d, rid=%d, oid=%d)\n", id, st->rid, st->oid);
  18283. script_pushint(st,0);
  18284. return SCRIPT_CMD_FAILURE;
  18285. }
  18286. clif_showscript(bl, msg);
  18287. script_pushint(st,1);
  18288. return SCRIPT_CMD_SUCCESS;
  18289. }
  18290. /**
  18291. * Ignore the SECURE_NPCTIMEOUT function.
  18292. * ignoretimeout <flag>{,<char_id>};
  18293. */
  18294. BUILDIN_FUNC(ignoretimeout)
  18295. {
  18296. #ifdef SECURE_NPCTIMEOUT
  18297. struct map_session_data *sd = NULL;
  18298. if (script_hasdata(st,3)) {
  18299. if (!script_isstring(st,3))
  18300. sd = map_charid2sd(script_getnum(st,3));
  18301. else
  18302. sd = map_nick2sd(script_getstr(st,3));
  18303. } else
  18304. sd = script_rid2sd(st);
  18305. if (!sd)
  18306. return SCRIPT_CMD_FAILURE;
  18307. sd->state.ignoretimeout = script_getnum(st,2) > 0;
  18308. #endif
  18309. return SCRIPT_CMD_SUCCESS;
  18310. }
  18311. /**
  18312. * geteleminfo <type>{,<char_id>};
  18313. **/
  18314. BUILDIN_FUNC(geteleminfo) {
  18315. TBL_ELEM *ed = NULL;
  18316. TBL_PC *sd = NULL;
  18317. int type = script_getnum(st,2);
  18318. if (!script_charid2sd(3, sd)) {
  18319. script_pushint(st, 0);
  18320. return SCRIPT_CMD_SUCCESS;
  18321. }
  18322. if (!(ed = sd->ed)) {
  18323. //ShowDebug("buildin_geteleminfo: Player doesn't have Elemental.\n");
  18324. script_pushint(st, 0);
  18325. return SCRIPT_CMD_SUCCESS;
  18326. }
  18327. switch (type) {
  18328. case 0: script_pushint(st, ed->elemental.elemental_id); break;
  18329. case 1: script_pushint(st, ed->bl.id); break;
  18330. default:
  18331. ShowError("buildin_geteleminfo: Invalid type '%d'.\n", type);
  18332. script_pushint(st, 0);
  18333. return SCRIPT_CMD_FAILURE;
  18334. }
  18335. return SCRIPT_CMD_SUCCESS;
  18336. }
  18337. /**
  18338. * Set the quest info of quest_id only showed on player in level range.
  18339. * setquestinfo_level <quest_id>,<min_level>,<max_level>
  18340. * @author [Cydh]
  18341. **/
  18342. BUILDIN_FUNC(setquestinfo_level) {
  18343. TBL_NPC* nd = map_id2nd(st->oid);
  18344. int quest_id = script_getnum(st, 2);
  18345. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18346. if (!qi) {
  18347. ShowError("buildin_setquestinfo_level: Quest with ID '%d' is not defined yet.\n", quest_id);
  18348. return SCRIPT_CMD_FAILURE;
  18349. }
  18350. qi->min_level = script_getnum(st, 3);
  18351. qi->max_level = script_getnum(st, 4);
  18352. if (!qi->max_level)
  18353. qi->max_level = MAX_LEVEL;
  18354. return SCRIPT_CMD_SUCCESS;
  18355. }
  18356. /**
  18357. * Set the quest info of quest_id only showed for player that has quest criteria
  18358. * setquestinfo_req <quest_id>,<quest_req_id>,<state>{,<quest_req_id>,<state>,...};
  18359. * @author [Cydh]
  18360. **/
  18361. BUILDIN_FUNC(setquestinfo_req) {
  18362. TBL_NPC* nd = map_id2nd(st->oid);
  18363. int quest_id = script_getnum(st, 2);
  18364. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18365. uint8 i = 0;
  18366. uint8 num = script_lastdata(st);
  18367. if (!qi) {
  18368. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not defined yet.\n", quest_id);
  18369. return SCRIPT_CMD_FAILURE;
  18370. }
  18371. if (quest_search(quest_id) == &quest_dummy) {
  18372. ShowError("buildin_setquestinfo_req: Quest with ID '%d' is not found in Quest DB.\n", quest_id);
  18373. return SCRIPT_CMD_FAILURE;
  18374. }
  18375. if (num%2) {
  18376. ShowError("buildin_setquestinfo_req: Odd number of parameters(%d) - pairs of requirements are expected.\n", num-2);
  18377. return SCRIPT_CMD_FAILURE;
  18378. }
  18379. for (i = 3; i <= num; i += 2) {
  18380. RECREATE(qi->req, struct questinfo_req, qi->req_count+1);
  18381. qi->req[qi->req_count].quest_id = script_getnum(st, i);
  18382. qi->req[qi->req_count].state = (script_getnum(st, i+1) >= 2) ? 2 : (script_getnum(st, i+1) <= 0) ? 0 : 1;
  18383. qi->req_count++;
  18384. }
  18385. return SCRIPT_CMD_SUCCESS;
  18386. }
  18387. /**
  18388. * Set the quest info of quest_id only showed for player that has specified Job
  18389. * setquestinfo_job <quest_id>,<job>{,<job>...};
  18390. * @author [Cydh]
  18391. **/
  18392. BUILDIN_FUNC(setquestinfo_job) {
  18393. TBL_NPC* nd = map_id2nd(st->oid);
  18394. int quest_id = script_getnum(st, 2);
  18395. struct questinfo *qi = map_has_questinfo(nd->bl.m, nd, quest_id);
  18396. int job_id = 0;
  18397. uint8 i = 0;
  18398. uint8 num = script_lastdata(st)+1;
  18399. if (!qi) {
  18400. ShowError("buildin_setquestinfo_job: Quest with ID '%d' is not defined yet.\n", quest_id);
  18401. return SCRIPT_CMD_FAILURE;
  18402. }
  18403. for (i = 3; i < num; i++) {
  18404. job_id = script_getnum(st, i);
  18405. if (!pcdb_checkid(job_id)) {
  18406. ShowError("buildin_setquestinfo_job: Invalid job id '%d' in Quest with ID %d.\n", job_id, quest_id);
  18407. continue;
  18408. }
  18409. buildin_questinfo_setjob(qi, job_id);
  18410. }
  18411. return SCRIPT_CMD_SUCCESS;
  18412. }
  18413. /**
  18414. * opendressroom(<flag>{,<char_id>});
  18415. */
  18416. BUILDIN_FUNC(opendressroom)
  18417. {
  18418. #if PACKETVER >= 20150513
  18419. int flag = 1;
  18420. TBL_PC* sd;
  18421. if( script_hasdata(st,2) )
  18422. flag = script_getnum(st,2);
  18423. if (!script_charid2sd(3, sd))
  18424. return SCRIPT_CMD_FAILURE;
  18425. clif_dressing_room(sd, flag);
  18426. return SCRIPT_CMD_SUCCESS;
  18427. #else
  18428. return SCRIPT_CMD_FAILURE;
  18429. #endif
  18430. }
  18431. /**
  18432. * Return alliance information between the two guilds.
  18433. * getguildalliance(<guild id1>,<guild id2>);
  18434. * Return values:
  18435. * -2 - Guild ID1 does not exist
  18436. * -1 - Guild ID2 does not exist
  18437. * 0 - Both guilds have no relation OR guild ID aren't given
  18438. * 1 - Both guilds are allies
  18439. * 2 - Both guilds are antagonists
  18440. */
  18441. BUILDIN_FUNC(getguildalliance)
  18442. {
  18443. struct guild *guild_data1, *guild_data2;
  18444. int guild_id1, guild_id2, i = 0;
  18445. guild_id1 = script_getnum(st,2);
  18446. guild_id2 = script_getnum(st,3);
  18447. if (guild_id1 < 1 || guild_id2 < 1) {
  18448. script_pushint(st, 0);
  18449. return SCRIPT_CMD_SUCCESS;
  18450. }
  18451. if (guild_id1 == guild_id2) {
  18452. script_pushint(st, 1);
  18453. return SCRIPT_CMD_SUCCESS;
  18454. }
  18455. guild_data1 = guild_search(guild_id1);
  18456. guild_data2 = guild_search(guild_id2);
  18457. if (guild_data1 == NULL) {
  18458. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID1 '%d'.\n", guild_id1);
  18459. script_pushint(st, -2);
  18460. return SCRIPT_CMD_FAILURE;
  18461. }
  18462. if (guild_data2 == NULL) {
  18463. ShowWarning("buildin_getguildalliance: Requesting non-existent GuildID2 '%d'.\n", guild_id2);
  18464. script_pushint(st, -1);
  18465. return SCRIPT_CMD_FAILURE;
  18466. }
  18467. ARR_FIND(0, MAX_GUILDALLIANCE, i, guild_data1->alliance[i].guild_id == guild_id2);
  18468. if (i == MAX_GUILDALLIANCE) {
  18469. script_pushint(st, 0);
  18470. return SCRIPT_CMD_SUCCESS;
  18471. }
  18472. if (guild_data1->alliance[i].opposition)
  18473. script_pushint(st, 2);
  18474. else
  18475. script_pushint(st, 1);
  18476. return SCRIPT_CMD_SUCCESS;
  18477. }
  18478. /**
  18479. * canParticipateSiegeTE([<char_id>]);
  18480. * Check if player can participate in WOE:TE
  18481. * @param char_id Player's CharID (optional)
  18482. **/
  18483. BUILDIN_FUNC(canParticipateSiegeTE) {
  18484. TBL_PC *sd = NULL;
  18485. if (!script_charid2sd(2, sd)) {
  18486. script_pushint(st, 0);
  18487. return SCRIPT_CMD_FAILURE;
  18488. }
  18489. script_pushint(st, pc_canParticipateSiegeTE(sd->class_) ? 1 : 0);
  18490. return SCRIPT_CMD_SUCCESS;
  18491. }
  18492. #include "../custom/script.inc"
  18493. // declarations that were supposed to be exported from npc_chat.c
  18494. #ifdef PCRE_SUPPORT
  18495. BUILDIN_FUNC(defpattern);
  18496. BUILDIN_FUNC(activatepset);
  18497. BUILDIN_FUNC(deactivatepset);
  18498. BUILDIN_FUNC(deletepset);
  18499. #endif
  18500. /** Regular expression matching
  18501. * preg_match(<pattern>,<string>{,<offset>})
  18502. */
  18503. BUILDIN_FUNC(preg_match) {
  18504. #ifdef PCRE_SUPPORT
  18505. pcre *re;
  18506. pcre_extra *pcreExtra;
  18507. const char *error;
  18508. int erroffset, r, offset = 0;
  18509. int subStrVec[30];
  18510. const char* pattern = script_getstr(st,2);
  18511. const char* subject = script_getstr(st,3);
  18512. if (script_hasdata(st,4))
  18513. offset = script_getnum(st,4);
  18514. re = pcre_compile(pattern, 0, &error, &erroffset, NULL);
  18515. pcreExtra = pcre_study(re, 0, &error);
  18516. r = pcre_exec(re, pcreExtra, subject, (int)strlen(subject), offset, 0, subStrVec, 30);
  18517. pcre_free(re);
  18518. if (pcreExtra != NULL)
  18519. pcre_free(pcreExtra);
  18520. if (r < 0)
  18521. script_pushint(st,0);
  18522. else
  18523. script_pushint(st,(r > 0) ? r : 30 / 3);
  18524. return SCRIPT_CMD_SUCCESS;
  18525. #else
  18526. ShowDebug("script:preg_match: cannot run without PCRE library enabled.\n");
  18527. script_pushint(st,0);
  18528. return SCRIPT_CMD_SUCCESS;
  18529. #endif
  18530. }
  18531. /// script command definitions
  18532. /// for an explanation on args, see add_buildin_func
  18533. struct script_function buildin_func[] = {
  18534. // NPC interaction
  18535. BUILDIN_DEF(mes,"s*"),
  18536. BUILDIN_DEF(next,""),
  18537. BUILDIN_DEF(close,""),
  18538. BUILDIN_DEF(close2,""),
  18539. BUILDIN_DEF(menu,"sl*"),
  18540. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  18541. BUILDIN_DEF(prompt,"s*"),
  18542. //
  18543. BUILDIN_DEF(goto,"l"),
  18544. BUILDIN_DEF(callsub,"l*"),
  18545. BUILDIN_DEF(callfunc,"s*"),
  18546. BUILDIN_DEF(return,"?"),
  18547. BUILDIN_DEF(getarg,"i?"),
  18548. BUILDIN_DEF(jobchange,"i??"),
  18549. BUILDIN_DEF(jobname,"i"),
  18550. BUILDIN_DEF(input,"r??"),
  18551. BUILDIN_DEF(warp,"sii"),
  18552. BUILDIN_DEF(areawarp,"siiiisii??"),
  18553. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  18554. BUILDIN_DEF(warpparty,"siii?"), // [Fredzilla] [Paradox924X]
  18555. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  18556. BUILDIN_DEF(setlook,"ii?"),
  18557. BUILDIN_DEF(changelook,"ii?"), // Simulates but don't Store it
  18558. BUILDIN_DEF(set,"rv?"),
  18559. BUILDIN_DEF(setarray,"rv*"),
  18560. BUILDIN_DEF(cleararray,"rvi"),
  18561. BUILDIN_DEF(copyarray,"rri"),
  18562. BUILDIN_DEF(getarraysize,"r"),
  18563. BUILDIN_DEF(deletearray,"r?"),
  18564. BUILDIN_DEF(getelementofarray,"ri"),
  18565. BUILDIN_DEF(getitem,"vi?"),
  18566. BUILDIN_DEF(rentitem,"vi?"),
  18567. BUILDIN_DEF(rentitem2,"viiiiiiii?"),
  18568. BUILDIN_DEF(getitem2,"viiiiiiii?"),
  18569. BUILDIN_DEF(getnameditem,"vv"),
  18570. BUILDIN_DEF2(grouprandomitem,"groupranditem","i?"),
  18571. BUILDIN_DEF(makeitem,"visii"),
  18572. BUILDIN_DEF(makeitem2,"visiiiiiiiii"),
  18573. BUILDIN_DEF(delitem,"vi?"),
  18574. BUILDIN_DEF2(delitem,"storagedelitem","vi?"),
  18575. BUILDIN_DEF2(delitem,"cartdelitem","vi?"),
  18576. BUILDIN_DEF(delitem2,"viiiiiiii?"),
  18577. BUILDIN_DEF2(delitem2,"storagedelitem2","viiiiiiii?"),
  18578. BUILDIN_DEF2(delitem2,"cartdelitem2","viiiiiiii?"),
  18579. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  18580. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  18581. BUILDIN_DEF(cutin,"si"),
  18582. BUILDIN_DEF(viewpoint,"iiiii"),
  18583. BUILDIN_DEF(heal,"ii?"),
  18584. BUILDIN_DEF(itemheal,"ii?"),
  18585. BUILDIN_DEF(percentheal,"ii?"),
  18586. BUILDIN_DEF(rand,"i?"),
  18587. BUILDIN_DEF(countitem,"v?"),
  18588. BUILDIN_DEF2(countitem,"storagecountitem","v?"),
  18589. BUILDIN_DEF2(countitem,"cartcountitem","v?"),
  18590. BUILDIN_DEF2(countitem,"countitem2","viiiiiii?"),
  18591. BUILDIN_DEF2(countitem,"storagecountitem2","viiiiiii?"),
  18592. BUILDIN_DEF2(countitem,"cartcountitem2","viiiiiii?"),
  18593. BUILDIN_DEF(checkweight,"vi*"),
  18594. BUILDIN_DEF(checkweight2,"rr"),
  18595. BUILDIN_DEF(readparam,"i?"),
  18596. BUILDIN_DEF(getcharid,"i?"),
  18597. BUILDIN_DEF(getnpcid,"i?"),
  18598. BUILDIN_DEF(getpartyname,"i"),
  18599. BUILDIN_DEF(getpartymember,"i??"),
  18600. BUILDIN_DEF(getpartyleader,"i?"),
  18601. BUILDIN_DEF(getguildname,"i"),
  18602. BUILDIN_DEF(getguildmaster,"i"),
  18603. BUILDIN_DEF(getguildmasterid,"i"),
  18604. BUILDIN_DEF(strcharinfo,"i?"),
  18605. BUILDIN_DEF(strnpcinfo,"i"),
  18606. BUILDIN_DEF(getequipid,"i?"),
  18607. BUILDIN_DEF(getequipuniqueid,"i?"),
  18608. BUILDIN_DEF(getequipname,"i?"),
  18609. BUILDIN_DEF(getbrokenid,"i?"), // [Valaris]
  18610. BUILDIN_DEF(repair,"i?"), // [Valaris]
  18611. BUILDIN_DEF(repairall,"?"),
  18612. BUILDIN_DEF(getequipisequiped,"i?"),
  18613. BUILDIN_DEF(getequipisenableref,"i?"),
  18614. BUILDIN_DEF(getequiprefinerycnt,"i?"),
  18615. BUILDIN_DEF(getequipweaponlv,"i?"),
  18616. BUILDIN_DEF(getequippercentrefinery,"i?"),
  18617. BUILDIN_DEF(successrefitem,"i??"),
  18618. BUILDIN_DEF(failedrefitem,"i?"),
  18619. BUILDIN_DEF(downrefitem,"i??"),
  18620. BUILDIN_DEF(statusup,"i?"),
  18621. BUILDIN_DEF(statusup2,"ii?"),
  18622. BUILDIN_DEF(bonus,"iv"),
  18623. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  18624. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  18625. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  18626. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  18627. BUILDIN_DEF(autobonus,"sii??"),
  18628. BUILDIN_DEF(autobonus2,"sii??"),
  18629. BUILDIN_DEF(autobonus3,"siiv?"),
  18630. BUILDIN_DEF(skill,"vi?"),
  18631. BUILDIN_DEF2(skill,"addtoskill","vi?"), // [Valaris]
  18632. BUILDIN_DEF(guildskill,"vi"),
  18633. BUILDIN_DEF(getskilllv,"v"),
  18634. BUILDIN_DEF(getgdskilllv,"iv"),
  18635. BUILDIN_DEF(basicskillcheck,""),
  18636. BUILDIN_DEF(getgmlevel,"?"),
  18637. BUILDIN_DEF(getgroupid,"?"),
  18638. BUILDIN_DEF(end,""),
  18639. BUILDIN_DEF(checkoption,"i?"),
  18640. BUILDIN_DEF(setoption,"i??"),
  18641. BUILDIN_DEF(setcart,"??"),
  18642. BUILDIN_DEF(checkcart,"?"),
  18643. BUILDIN_DEF(setfalcon,"??"),
  18644. BUILDIN_DEF(checkfalcon,"?"),
  18645. BUILDIN_DEF(setriding,"??"),
  18646. BUILDIN_DEF(checkriding,"?"),
  18647. BUILDIN_DEF(checkwug,"?"),
  18648. BUILDIN_DEF(checkmadogear,"?"),
  18649. BUILDIN_DEF(setmadogear,"??"),
  18650. BUILDIN_DEF2(savepoint,"save","sii?"),
  18651. BUILDIN_DEF(savepoint,"sii?"),
  18652. BUILDIN_DEF(gettimetick,"i"),
  18653. BUILDIN_DEF(gettime,"i"),
  18654. BUILDIN_DEF(gettimestr,"si"),
  18655. BUILDIN_DEF(openstorage,""),
  18656. BUILDIN_DEF(guildopenstorage,""),
  18657. BUILDIN_DEF(itemskill,"vi"),
  18658. BUILDIN_DEF(produce,"i"),
  18659. BUILDIN_DEF(cooking,"i"),
  18660. BUILDIN_DEF(monster,"siisii???"),
  18661. BUILDIN_DEF(getmobdrops,"i"),
  18662. BUILDIN_DEF(areamonster,"siiiisii???"),
  18663. BUILDIN_DEF(killmonster,"ss?"),
  18664. BUILDIN_DEF(killmonsterall,"s?"),
  18665. BUILDIN_DEF(clone,"siisi????"),
  18666. BUILDIN_DEF(doevent,"s"),
  18667. BUILDIN_DEF(donpcevent,"s"),
  18668. BUILDIN_DEF(cmdothernpc,"ss"),
  18669. BUILDIN_DEF(addtimer,"is"),
  18670. BUILDIN_DEF(deltimer,"s"),
  18671. BUILDIN_DEF(addtimercount,"is"),
  18672. BUILDIN_DEF(initnpctimer,"??"),
  18673. BUILDIN_DEF(stopnpctimer,"??"),
  18674. BUILDIN_DEF(startnpctimer,"??"),
  18675. BUILDIN_DEF(setnpctimer,"i?"),
  18676. BUILDIN_DEF(getnpctimer,"i?"),
  18677. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  18678. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  18679. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  18680. BUILDIN_DEF(announce,"si?????"),
  18681. BUILDIN_DEF(mapannounce,"ssi?????"),
  18682. BUILDIN_DEF(areaannounce,"siiiisi?????"),
  18683. BUILDIN_DEF(getusers,"i"),
  18684. BUILDIN_DEF(getmapguildusers,"si"),
  18685. BUILDIN_DEF(getmapusers,"s"),
  18686. BUILDIN_DEF(getareausers,"siiii"),
  18687. BUILDIN_DEF(getareadropitem,"siiiiv"),
  18688. BUILDIN_DEF(enablenpc,"s"),
  18689. BUILDIN_DEF(disablenpc,"s"),
  18690. BUILDIN_DEF(hideoffnpc,"s"),
  18691. BUILDIN_DEF(hideonnpc,"s"),
  18692. BUILDIN_DEF(sc_start,"iii???"),
  18693. BUILDIN_DEF2(sc_start,"sc_start2","iiii???"),
  18694. BUILDIN_DEF2(sc_start,"sc_start4","iiiiii???"),
  18695. BUILDIN_DEF(sc_end,"i?"),
  18696. BUILDIN_DEF(getstatus, "i??"),
  18697. BUILDIN_DEF(getscrate,"ii?"),
  18698. BUILDIN_DEF(debugmes,"s"),
  18699. BUILDIN_DEF2(catchpet,"pet","i"),
  18700. BUILDIN_DEF2(birthpet,"bpet",""),
  18701. BUILDIN_DEF(resetlvl,"i?"),
  18702. BUILDIN_DEF(resetstatus,"?"),
  18703. BUILDIN_DEF(resetskill,"?"),
  18704. BUILDIN_DEF(skillpointcount,"?"),
  18705. BUILDIN_DEF(changebase,"i?"),
  18706. BUILDIN_DEF(changesex,"?"),
  18707. BUILDIN_DEF(changecharsex,"?"),
  18708. BUILDIN_DEF(waitingroom,"si?????"),
  18709. BUILDIN_DEF(delwaitingroom,"?"),
  18710. BUILDIN_DEF(waitingroomkick,"ss"),
  18711. BUILDIN_DEF(getwaitingroomusers, "?"),
  18712. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  18713. BUILDIN_DEF(enablewaitingroomevent,"?"),
  18714. BUILDIN_DEF(disablewaitingroomevent,"?"),
  18715. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  18716. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  18717. BUILDIN_DEF(getwaitingroomstate,"i?"),
  18718. BUILDIN_DEF(warpwaitingpc,"sii?"),
  18719. BUILDIN_DEF(attachrid,"i"),
  18720. BUILDIN_DEF(addrid,"i?????"),
  18721. BUILDIN_DEF(detachrid,""),
  18722. BUILDIN_DEF(isloggedin,"i?"),
  18723. BUILDIN_DEF(setmapflagnosave,"ssii"),
  18724. BUILDIN_DEF(getmapflag,"si?"),
  18725. BUILDIN_DEF(setmapflag,"si??"),
  18726. BUILDIN_DEF(removemapflag,"si?"),
  18727. BUILDIN_DEF(pvpon,"s"),
  18728. BUILDIN_DEF(pvpoff,"s"),
  18729. BUILDIN_DEF(gvgon,"s"),
  18730. BUILDIN_DEF(gvgoff,"s"),
  18731. BUILDIN_DEF(emotion,"i??"),
  18732. BUILDIN_DEF(maprespawnguildid,"sii"),
  18733. BUILDIN_DEF(agitstart,""), // <Agit>
  18734. BUILDIN_DEF(agitend,""),
  18735. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  18736. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  18737. BUILDIN_DEF(getcastlename,"s"),
  18738. BUILDIN_DEF(getcastledata,"si"),
  18739. BUILDIN_DEF(setcastledata,"sii"),
  18740. BUILDIN_DEF(requestguildinfo,"i?"),
  18741. BUILDIN_DEF(getequipcardcnt,"i"),
  18742. BUILDIN_DEF(successremovecards,"i"),
  18743. BUILDIN_DEF(failedremovecards,"ii"),
  18744. BUILDIN_DEF(marriage,"s"),
  18745. BUILDIN_DEF2(wedding_effect,"wedding",""),
  18746. BUILDIN_DEF(divorce,"?"),
  18747. BUILDIN_DEF(ispartneron,"?"),
  18748. BUILDIN_DEF(getpartnerid,"?"),
  18749. BUILDIN_DEF(getchildid,"?"),
  18750. BUILDIN_DEF(getmotherid,"?"),
  18751. BUILDIN_DEF(getfatherid,"?"),
  18752. BUILDIN_DEF(warppartner,"sii"),
  18753. BUILDIN_DEF(getitemname,"v"),
  18754. BUILDIN_DEF(getitemslots,"i"),
  18755. BUILDIN_DEF(makepet,"i"),
  18756. BUILDIN_DEF(getexp,"ii?"),
  18757. BUILDIN_DEF(getinventorylist,"?"),
  18758. BUILDIN_DEF(getskilllist,"?"),
  18759. BUILDIN_DEF(clearitem,"?"),
  18760. BUILDIN_DEF(classchange,"ii"),
  18761. BUILDIN_DEF(misceffect,"i"),
  18762. BUILDIN_DEF(playBGM,"s"),
  18763. BUILDIN_DEF(playBGMall,"s?????"),
  18764. BUILDIN_DEF(soundeffect,"si"),
  18765. BUILDIN_DEF(soundeffectall,"si?????"), // SoundEffectAll [Codemaster]
  18766. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  18767. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  18768. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  18769. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  18770. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  18771. BUILDIN_DEF(petloot,"i"), // [Valaris]
  18772. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  18773. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  18774. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  18775. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  18776. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  18777. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  18778. BUILDIN_DEF(specialeffect,"i??"), // npc skill effect [Valaris]
  18779. BUILDIN_DEF(specialeffect2,"i??"), // skill effect on players[Valaris]
  18780. BUILDIN_DEF(nude,"?"), // nude command [Valaris]
  18781. BUILDIN_DEF(mapwarp,"ssii??"), // Added by RoVeRT
  18782. BUILDIN_DEF(atcommand,"s"), // [MouseJstr]
  18783. BUILDIN_DEF2(atcommand,"charcommand","s"), // [MouseJstr]
  18784. BUILDIN_DEF(movenpc,"sii?"), // [MouseJstr]
  18785. BUILDIN_DEF(message,"ss"), // [MouseJstr]
  18786. BUILDIN_DEF(npctalk,"s"), // [Valaris]
  18787. BUILDIN_DEF(mobcount,"ss"),
  18788. BUILDIN_DEF(getlook,"i?"),
  18789. BUILDIN_DEF(getsavepoint,"i?"),
  18790. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  18791. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  18792. BUILDIN_DEF(npcstop,""), // [Valaris]
  18793. BUILDIN_DEF(getmapxy,"rrri?"), //by Lorky [Lupus]
  18794. BUILDIN_DEF(mapid2name,"i"),
  18795. BUILDIN_DEF(checkoption1,"i?"),
  18796. BUILDIN_DEF(checkoption2,"i?"),
  18797. BUILDIN_DEF(guildgetexp,"i"),
  18798. BUILDIN_DEF(guildchangegm,"is"),
  18799. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  18800. BUILDIN_DEF(summon,"si??"), // summons a slave monster [Celest]
  18801. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  18802. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  18803. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  18804. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  18805. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  18806. BUILDIN_DEF(getrefine,""), // returns the refined number of the current item, or an item with index specified [celest]
  18807. BUILDIN_DEF(night,""), // sets the server to night time
  18808. BUILDIN_DEF(day,""), // sets the server to day time
  18809. #ifdef PCRE_SUPPORT
  18810. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  18811. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  18812. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  18813. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  18814. #endif
  18815. BUILDIN_DEF(preg_match,"ss?"),
  18816. BUILDIN_DEF(dispbottom,"s??"), //added from jA [Lupus]
  18817. BUILDIN_DEF(recovery,"i???"),
  18818. BUILDIN_DEF(getpetinfo,"i?"),
  18819. BUILDIN_DEF(gethominfo,"i?"),
  18820. BUILDIN_DEF(getmercinfo,"i?"),
  18821. BUILDIN_DEF(checkequipedcard,"i"),
  18822. BUILDIN_DEF(jump_zero,"il"), //for future jA script compatibility
  18823. BUILDIN_DEF(globalmes,"s?"), //end jA addition
  18824. BUILDIN_DEF(unequip,"i?"), // unequip command [Spectre]
  18825. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  18826. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  18827. BUILDIN_DEF(charat,"si"),
  18828. BUILDIN_DEF(setchar,"ssi"),
  18829. BUILDIN_DEF(insertchar,"ssi"),
  18830. BUILDIN_DEF(delchar,"si"),
  18831. BUILDIN_DEF(strtoupper,"s"),
  18832. BUILDIN_DEF(strtolower,"s"),
  18833. BUILDIN_DEF(charisupper, "si"),
  18834. BUILDIN_DEF(charislower, "si"),
  18835. BUILDIN_DEF(substr,"sii"),
  18836. BUILDIN_DEF(explode, "rss"),
  18837. BUILDIN_DEF(implode, "r?"),
  18838. BUILDIN_DEF(sprintf,"s*"), // [Mirei]
  18839. BUILDIN_DEF(sscanf,"ss*"), // [Mirei]
  18840. BUILDIN_DEF(strpos,"ss?"),
  18841. BUILDIN_DEF(replacestr,"sss??"),
  18842. BUILDIN_DEF(countstr,"ss?"),
  18843. BUILDIN_DEF(setnpcdisplay,"sv??"),
  18844. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  18845. BUILDIN_DEF(strcmp,"ss"),
  18846. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  18847. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  18848. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  18849. // [zBuffer] List of mathematics commands --->
  18850. BUILDIN_DEF(sqrt,"i"),
  18851. BUILDIN_DEF(pow,"ii"),
  18852. BUILDIN_DEF(distance,"iiii"),
  18853. // <--- [zBuffer] List of mathematics commands
  18854. BUILDIN_DEF(md5,"s"),
  18855. // [zBuffer] List of dynamic var commands --->
  18856. BUILDIN_DEF(getd,"s"),
  18857. BUILDIN_DEF(setd,"sv?"),
  18858. BUILDIN_DEF(callshop,"s?"), // [Skotlex]
  18859. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  18860. BUILDIN_DEF(npcshopadditem,"sii*"),
  18861. BUILDIN_DEF(npcshopdelitem,"si*"),
  18862. BUILDIN_DEF(npcshopattach,"s?"),
  18863. BUILDIN_DEF(equip,"i?"),
  18864. BUILDIN_DEF(autoequip,"ii"),
  18865. BUILDIN_DEF(setbattleflag,"si"),
  18866. BUILDIN_DEF(getbattleflag,"s"),
  18867. BUILDIN_DEF(setitemscript,"is?"), //Set NEW item bonus script. Lupus
  18868. BUILDIN_DEF(disguise,"i?"), //disguise player. Lupus
  18869. BUILDIN_DEF(undisguise,"?"), //undisguise player. Lupus
  18870. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  18871. BUILDIN_DEF(addmonsterdrop,"vii"), //Akinari [Lupus]
  18872. BUILDIN_DEF(delmonsterdrop,"vi"), //Akinari [Lupus]
  18873. BUILDIN_DEF(axtoi,"s"),
  18874. BUILDIN_DEF(query_sql,"s*"),
  18875. BUILDIN_DEF(query_logsql,"s*"),
  18876. BUILDIN_DEF(escape_sql,"v"),
  18877. BUILDIN_DEF(atoi,"s"),
  18878. BUILDIN_DEF(strtol,"si"),
  18879. // [zBuffer] List of player cont commands --->
  18880. BUILDIN_DEF(rid2name,"i"),
  18881. BUILDIN_DEF(pcfollow,"ii"),
  18882. BUILDIN_DEF(pcstopfollow,"i"),
  18883. BUILDIN_DEF(pcblockmove,"ii"),
  18884. // <--- [zBuffer] List of player cont commands
  18885. // [zBuffer] List of unit control commands --->
  18886. BUILDIN_DEF(getunittype,"i"),
  18887. BUILDIN_DEF(getunitname,"i"),
  18888. BUILDIN_DEF(setunitname,"is"),
  18889. BUILDIN_DEF(getunitdata,"i*"),
  18890. BUILDIN_DEF(setunitdata,"iii"),
  18891. BUILDIN_DEF(unitwalk,"iii?"),
  18892. BUILDIN_DEF2(unitwalk,"unitwalkto","ii?"),
  18893. BUILDIN_DEF(unitkill,"i"),
  18894. BUILDIN_DEF(unitwarp,"isii"),
  18895. BUILDIN_DEF(unitattack,"iv?"),
  18896. BUILDIN_DEF(unitstopattack,"i"),
  18897. BUILDIN_DEF(unitstopwalk,"i"),
  18898. BUILDIN_DEF2(pcblockmove,"unitblockmove","ii"),
  18899. BUILDIN_DEF(unittalk,"is"),
  18900. BUILDIN_DEF(unitemote,"ii"),
  18901. BUILDIN_DEF(unitskilluseid,"ivi??"), // originally by Qamera [Celest]
  18902. BUILDIN_DEF(unitskillusepos,"iviii?"), // [Celest]
  18903. // <--- [zBuffer] List of unit control commands
  18904. BUILDIN_DEF(sleep,"i"),
  18905. BUILDIN_DEF(sleep2,"i"),
  18906. BUILDIN_DEF(awake,"s"),
  18907. BUILDIN_DEF(getvariableofnpc,"rs"),
  18908. BUILDIN_DEF(warpportal,"iisii"),
  18909. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  18910. BUILDIN_DEF2(homunculus_mutate,"hommutate","?"),
  18911. BUILDIN_DEF(morphembryo,""),
  18912. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  18913. BUILDIN_DEF(checkhomcall,""),
  18914. BUILDIN_DEF(eaclass,"??"), //[Skotlex]
  18915. BUILDIN_DEF(roclass,"i?"), //[Skotlex]
  18916. BUILDIN_DEF(checkvending,"?"),
  18917. BUILDIN_DEF(checkchatting,"?"),
  18918. BUILDIN_DEF(checkidle,"?"),
  18919. BUILDIN_DEF(openmail,"?"),
  18920. BUILDIN_DEF(openauction,"?"),
  18921. BUILDIN_DEF(checkcell,"siii"),
  18922. BUILDIN_DEF(setcell,"siiiiii"),
  18923. BUILDIN_DEF(setwall,"siiiiis"),
  18924. BUILDIN_DEF(delwall,"s"),
  18925. BUILDIN_DEF(searchitem,"rs"),
  18926. BUILDIN_DEF(mercenary_create,"ii"),
  18927. BUILDIN_DEF(mercenary_heal,"ii"),
  18928. BUILDIN_DEF(mercenary_sc_start,"iii"),
  18929. BUILDIN_DEF(mercenary_get_calls,"i"),
  18930. BUILDIN_DEF(mercenary_get_faith,"i"),
  18931. BUILDIN_DEF(mercenary_set_calls,"ii"),
  18932. BUILDIN_DEF(mercenary_set_faith,"ii"),
  18933. BUILDIN_DEF(readbook,"ii"),
  18934. BUILDIN_DEF(setfont,"i"),
  18935. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  18936. BUILDIN_DEF(progressbar,"si"),
  18937. BUILDIN_DEF(pushpc,"ii"),
  18938. BUILDIN_DEF(buyingstore,"i"),
  18939. BUILDIN_DEF(searchstores,"ii"),
  18940. BUILDIN_DEF(showdigit,"i?"),
  18941. // WoE SE
  18942. BUILDIN_DEF(agitstart2,""),
  18943. BUILDIN_DEF(agitend2,""),
  18944. BUILDIN_DEF(agitcheck2,""),
  18945. // BattleGround
  18946. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  18947. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  18948. BUILDIN_DEF(bg_team_setxy,"iii"),
  18949. BUILDIN_DEF(bg_warp,"isii"),
  18950. BUILDIN_DEF(bg_monster,"isiisi?"),
  18951. BUILDIN_DEF(bg_monster_set_team,"ii"),
  18952. BUILDIN_DEF(bg_leave,"?"),
  18953. BUILDIN_DEF(bg_destroy,"i"),
  18954. BUILDIN_DEF(areapercentheal,"siiiiii"),
  18955. BUILDIN_DEF(bg_get_data,"ii"),
  18956. BUILDIN_DEF(bg_getareausers,"isiiii"),
  18957. BUILDIN_DEF(bg_updatescore,"sii"),
  18958. // Instancing
  18959. BUILDIN_DEF(instance_create,"s"),
  18960. BUILDIN_DEF(instance_destroy,"?"),
  18961. BUILDIN_DEF(instance_id,""),
  18962. BUILDIN_DEF(instance_enter,"s???"),
  18963. BUILDIN_DEF(instance_npcname,"s?"),
  18964. BUILDIN_DEF(instance_mapname,"s?"),
  18965. BUILDIN_DEF(instance_warpall,"sii?"),
  18966. BUILDIN_DEF(instance_announce,"isi?????"),
  18967. BUILDIN_DEF(instance_check_party,"i???"),
  18968. /**
  18969. * 3rd-related
  18970. **/
  18971. BUILDIN_DEF(makerune,"i?"),
  18972. BUILDIN_DEF(checkdragon,"?"),//[Ind]
  18973. BUILDIN_DEF(setdragon,"??"),//[Ind]
  18974. BUILDIN_DEF(ismounting,"?"),//[Ind]
  18975. BUILDIN_DEF(setmounting,"?"),//[Ind]
  18976. BUILDIN_DEF(checkre,"i"),
  18977. /**
  18978. * rAthena and beyond!
  18979. **/
  18980. BUILDIN_DEF(getargcount,""),
  18981. BUILDIN_DEF(getcharip,"?"),
  18982. BUILDIN_DEF(is_function,"s"),
  18983. BUILDIN_DEF(get_revision,""),
  18984. BUILDIN_DEF(get_githash,""),
  18985. BUILDIN_DEF(freeloop,"?"),
  18986. BUILDIN_DEF(getrandgroupitem,"i??"),
  18987. BUILDIN_DEF(cleanmap,"s"),
  18988. BUILDIN_DEF2(cleanmap,"cleanarea","siiii"),
  18989. BUILDIN_DEF(npcskill,"viii"),
  18990. BUILDIN_DEF(consumeitem,"v?"),
  18991. BUILDIN_DEF(delequip,"i?"),
  18992. BUILDIN_DEF(breakequip,"i?"),
  18993. BUILDIN_DEF(sit,"?"),
  18994. BUILDIN_DEF(stand,"?"),
  18995. //@commands (script based)
  18996. BUILDIN_DEF(bindatcmd, "ss??"),
  18997. BUILDIN_DEF(unbindatcmd, "s"),
  18998. BUILDIN_DEF(useatcmd, "s"),
  18999. //Quest Log System [Inkfish]
  19000. BUILDIN_DEF(questinfo, "ii??"),
  19001. BUILDIN_DEF(setquest, "i?"),
  19002. BUILDIN_DEF(erasequest, "i?"),
  19003. BUILDIN_DEF(completequest, "i?"),
  19004. BUILDIN_DEF(checkquest, "i??"),
  19005. BUILDIN_DEF(isbegin_quest,"i?"),
  19006. BUILDIN_DEF(changequest, "ii?"),
  19007. BUILDIN_DEF(showevent, "i??"),
  19008. //Bound items [Xantara] & [Akinari]
  19009. BUILDIN_DEF2(getitem,"getitembound","vii?"),
  19010. BUILDIN_DEF2(getitem2,"getitembound2","viiiiiiiii?"),
  19011. BUILDIN_DEF(countbound, "??"),
  19012. // Party related
  19013. BUILDIN_DEF(party_create,"s???"),
  19014. BUILDIN_DEF(party_addmember,"ii"),
  19015. BUILDIN_DEF(party_delmember,"??"),
  19016. BUILDIN_DEF(party_changeleader,"ii"),
  19017. BUILDIN_DEF(party_changeoption,"iii"),
  19018. BUILDIN_DEF(party_destroy,"i"),
  19019. BUILDIN_DEF(is_clientver,"ii?"),
  19020. BUILDIN_DEF2(montransform, "transform", "vi?????"), // Monster Transform [malufett/Hercules]
  19021. BUILDIN_DEF(vip_status,"i?"),
  19022. BUILDIN_DEF(vip_time,"i?"),
  19023. BUILDIN_DEF(bonus_script,"si????"),
  19024. BUILDIN_DEF(bonus_script_clear,"??"),
  19025. BUILDIN_DEF(getgroupitem,"i?"),
  19026. BUILDIN_DEF(enable_command,""),
  19027. BUILDIN_DEF(disable_command,""),
  19028. BUILDIN_DEF(getguildmember,"i??"),
  19029. BUILDIN_DEF(addspiritball,"ii?"),
  19030. BUILDIN_DEF(delspiritball,"i?"),
  19031. BUILDIN_DEF(countspiritball,"?"),
  19032. BUILDIN_DEF(mergeitem,"?"),
  19033. BUILDIN_DEF(mergeitem2,"??"),
  19034. BUILDIN_DEF(npcshopupdate,"sii?"),
  19035. BUILDIN_DEF(getattachedrid,""),
  19036. BUILDIN_DEF(getvar,"vi"),
  19037. BUILDIN_DEF(showscript,"s?"),
  19038. BUILDIN_DEF(ignoretimeout,"i?"),
  19039. BUILDIN_DEF(geteleminfo,"i?"),
  19040. BUILDIN_DEF(setquestinfo_level,"iii"),
  19041. BUILDIN_DEF(setquestinfo_req,"iii*"),
  19042. BUILDIN_DEF(setquestinfo_job,"ii*"),
  19043. BUILDIN_DEF(opendressroom,"i?"),
  19044. BUILDIN_DEF(getguildalliance,"ii"),
  19045. // WoE TE
  19046. BUILDIN_DEF(agitstart3,""),
  19047. BUILDIN_DEF(agitend3,""),
  19048. BUILDIN_DEF(agitcheck3,""),
  19049. BUILDIN_DEF(gvgon3,"s"),
  19050. BUILDIN_DEF(gvgoff3,"s"),
  19051. BUILDIN_DEF(canParticipateSiegeTE,"?"),
  19052. #include "../custom/script_def.inc"
  19053. {NULL,NULL,NULL},
  19054. };