script.c 351 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. //#define DEBUG_DUMP_STACK
  8. #include "../common/cbasetypes.h"
  9. #include "../common/malloc.h"
  10. #include "../common/md5calc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/strlib.h"
  15. #include "../common/timer.h"
  16. #include "../common/utils.h"
  17. #include "map.h"
  18. #include "path.h"
  19. #include "clif.h"
  20. #include "chrif.h"
  21. #include "itemdb.h"
  22. #include "pc.h"
  23. #include "status.h"
  24. #include "storage.h"
  25. #include "mob.h"
  26. #include "npc.h"
  27. #include "pet.h"
  28. #include "mapreg.h"
  29. #include "homunculus.h"
  30. #include "instance.h"
  31. #include "mercenary.h"
  32. #include "intif.h"
  33. #include "skill.h"
  34. #include "status.h"
  35. #include "chat.h"
  36. #include "battle.h"
  37. #include "battleground.h"
  38. #include "party.h"
  39. #include "guild.h"
  40. #include "atcommand.h"
  41. #include "log.h"
  42. #include "unit.h"
  43. #include "pet.h"
  44. #include "mail.h"
  45. #include "script.h"
  46. #include "quest.h"
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <math.h>
  51. #ifndef WIN32
  52. #include <sys/time.h>
  53. #endif
  54. #include <time.h>
  55. #include <setjmp.h>
  56. #include <errno.h>
  57. ///////////////////////////////////////////////////////////////////////////////
  58. //## TODO possible enhancements: [FlavioJS]
  59. // - 'callfunc' supporting labels in the current npc "::LabelName"
  60. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  61. // - 'function FuncName;' function declarations reverting to global functions
  62. // if local label isn't found
  63. // - join callfunc and callsub's functionality
  64. // - use script_op2name in the DEBUG_DISASM block
  65. // - remove dynamic allocation in add_word()
  66. // - remove GETVALUE / SETVALUE
  67. // - clean up the set_reg / set_val / setd_sub mess
  68. //
  69. // struct script_state* st;
  70. //
  71. /// Returns the script_data at the target index
  72. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  73. /// Returns if the stack contains data at the target index
  74. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  75. /// Returns the index of the last data in the stack
  76. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  77. /// Pushes an int into the stack
  78. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  79. /// Pushes a string into the stack (script engine frees it automatically)
  80. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  81. /// Pushes a copy of a string into the stack
  82. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  83. /// Pushes a constant string into the stack (must never change or be freed)
  84. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  85. /// Pushes a nil into the stack
  86. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  87. /// Pushes a copy of the data in the target index
  88. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  89. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  90. #define script_isint(st,i) data_isint(script_getdata(st,i))
  91. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  92. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  93. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  94. // Note: "top" functions/defines use indexes relative to the top of the stack
  95. // -1 is the index of the data at the top
  96. /// Returns the script_data at the target index relative to the top of the stack
  97. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  98. /// Pushes a copy of the data in the target index relative to the top of the stack
  99. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  100. /// Removes the range of values [start,end[ relative to the top of the stack
  101. #define script_removetop(st,start,end) ( pop_stack((st), ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  102. //
  103. // struct script_data* data;
  104. //
  105. /// Returns if the script data is a string
  106. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  107. /// Returns if the script data is an int
  108. #define data_isint(data) ( (data)->type == C_INT )
  109. /// Returns if the script data is a reference
  110. #define data_isreference(data) ( (data)->type == C_NAME )
  111. /// Returns if the script data is a label
  112. #define data_islabel(data) ( (data)->type == C_POS )
  113. /// Returns if the script data is an internal script function label
  114. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  115. /// Returns if this is a reference to a constant
  116. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  117. /// Returns if this a reference to a param
  118. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  119. /// Returns if this a reference to a variable
  120. //##TODO confirm it's C_NAME [FlavioJS]
  121. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  122. /// Returns the unique id of the reference (id and index)
  123. #define reference_getuid(data) ( (data)->u.num )
  124. /// Returns the id of the reference
  125. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  126. /// Returns the array index of the reference
  127. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  128. /// Returns the name of the reference
  129. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  130. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  131. #define reference_getref(data) ( (data)->ref )
  132. /// Returns the value of the constant
  133. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  134. /// Returns the type of param
  135. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  136. /// Composes the uid of a reference from the id and the index
  137. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  138. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.' && (prefix) != '\'')
  139. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' && (prefix) != '\'' )
  140. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  141. #define FETCH(n, t) \
  142. if( script_hasdata(st,n) ) \
  143. (t)=script_getnum(st,n);
  144. #define SCRIPT_BLOCK_SIZE 512
  145. enum { LABEL_NEXTLINE=1,LABEL_START };
  146. /// temporary buffer for passing around compiled bytecode
  147. /// @see add_scriptb, set_label, parse_script
  148. static unsigned char* script_buf = NULL;
  149. static int script_pos = 0, script_size = 0;
  150. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  151. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  152. // String buffer structures.
  153. // str_data stores string information
  154. static struct str_data_struct {
  155. enum c_op type;
  156. int str;
  157. int backpatch;
  158. int label;
  159. int (*func)(struct script_state *st);
  160. int val;
  161. int next;
  162. } *str_data = NULL;
  163. static int str_data_size = 0; // size of the data
  164. static int str_num = LABEL_START; // next id to be assigned
  165. // str_buf holds the strings themselves
  166. static char *str_buf;
  167. static int str_size = 0; // size of the buffer
  168. static int str_pos = 0; // next position to be assigned
  169. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  170. #define SCRIPT_HASH_SIZE 1021
  171. int str_hash[SCRIPT_HASH_SIZE];
  172. // Specifies which string hashing method to use
  173. //#define SCRIPT_HASH_DJB2
  174. //#define SCRIPT_HASH_SDBM
  175. #define SCRIPT_HASH_ELF
  176. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  177. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  178. static int parse_options=0;
  179. DBMap* script_get_label_db(){ return scriptlabel_db; }
  180. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  181. struct Script_Config script_config = {
  182. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  183. 0, INT_MAX, // input_min_value/input_max_value
  184. "OnPCDieEvent", //die_event_name
  185. "OnPCKillEvent", //kill_pc_event_name
  186. "OnNPCKillEvent", //kill_mob_event_name
  187. "OnPCLoginEvent", //login_event_name
  188. "OnPCLogoutEvent", //logout_event_name
  189. "OnPCLoadMapEvent", //loadmap_event_name
  190. "OnPCBaseLvUpEvent", //baselvup_event_name
  191. "OnPCJobLvUpEvent", //joblvup_event_name
  192. "OnTouch", //Official version of OnTouch
  193. "OnTouch2", //Official version of OnTouch2
  194. };
  195. static jmp_buf error_jump;
  196. static char* error_msg;
  197. static const char* error_pos;
  198. static int error_report; // if the error should produce output
  199. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  200. // [Eoe / jA 1080, 1081, 1094, 1164]
  201. enum curly_type {
  202. TYPE_NULL = 0,
  203. TYPE_IF,
  204. TYPE_SWITCH,
  205. TYPE_WHILE,
  206. TYPE_FOR,
  207. TYPE_DO,
  208. TYPE_USERFUNC,
  209. TYPE_ARGLIST // function argument list
  210. };
  211. #define ARGLIST_UNDEFINED 0
  212. #define ARGLIST_NO_PAREN 1
  213. #define ARGLIST_PAREN 2
  214. static struct {
  215. struct {
  216. enum curly_type type;
  217. int index;
  218. int count;
  219. int flag;
  220. struct linkdb_node *case_label;
  221. } curly[256]; // 右カッコの情報
  222. int curly_count; // 右カッコの数
  223. int index; // スクリプト内で使用した構文の数
  224. } syntax;
  225. const char* parse_curly_close(const char* p);
  226. const char* parse_syntax_close(const char* p);
  227. const char* parse_syntax_close_sub(const char* p,int* flag);
  228. const char* parse_syntax(const char* p);
  229. static int parse_syntax_for_flag = 0;
  230. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  231. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  232. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  233. int potion_target=0;
  234. c_op get_com(unsigned char *script,int *pos);
  235. int get_num(unsigned char *script,int *pos);
  236. typedef struct script_function {
  237. int (*func)(struct script_state *st);
  238. const char *name;
  239. const char *arg;
  240. } script_function;
  241. extern script_function buildin_func[];
  242. static struct linkdb_node* sleep_db;// int oid -> struct script_state*
  243. uint32 crctab[256];
  244. /*==========================================
  245. * ローカルプロトタイプ宣言 (必要な物のみ)
  246. *------------------------------------------*/
  247. const char* parse_subexpr(const char* p,int limit);
  248. void push_val(struct script_stack *stack,int type,int val);
  249. int run_func(struct script_state *st);
  250. enum {
  251. MF_NOMEMO, //0
  252. MF_NOTELEPORT,
  253. MF_NOSAVE,
  254. MF_NOBRANCH,
  255. MF_NOPENALTY,
  256. MF_NOZENYPENALTY,
  257. MF_PVP,
  258. MF_PVP_NOPARTY,
  259. MF_PVP_NOGUILD,
  260. MF_GVG,
  261. MF_GVG_NOPARTY, //10
  262. MF_NOTRADE,
  263. MF_NOSKILL,
  264. MF_NOWARP,
  265. MF_PARTYLOCK,
  266. MF_NOICEWALL,
  267. MF_SNOW,
  268. MF_FOG,
  269. MF_SAKURA,
  270. MF_LEAVES,
  271. MF_RAIN, //20
  272. MF_INDOORS,
  273. MF_NOGO,
  274. MF_CLOUDS,
  275. MF_CLOUDS2,
  276. MF_FIREWORKS,
  277. MF_GVG_CASTLE,
  278. MF_GVG_DUNGEON,
  279. MF_NIGHTENABLED,
  280. MF_NOBASEEXP,
  281. MF_NOJOBEXP, //30
  282. MF_NOMOBLOOT,
  283. MF_NOMVPLOOT,
  284. MF_NORETURN,
  285. MF_NOWARPTO,
  286. MF_NIGHTMAREDROP,
  287. MF_RESTRICTED,
  288. MF_NOCOMMAND,
  289. MF_NODROP,
  290. MF_JEXP,
  291. MF_BEXP, //40
  292. MF_NOVENDING,
  293. MF_LOADEVENT,
  294. MF_NOCHAT,
  295. MF_NOEXPPENALTY,
  296. MF_GUILDLOCK
  297. };
  298. const char* script_op2name(int op)
  299. {
  300. #define RETURN_OP_NAME(type) case type: return #type
  301. switch( op )
  302. {
  303. RETURN_OP_NAME(C_NOP);
  304. RETURN_OP_NAME(C_POS);
  305. RETURN_OP_NAME(C_INT);
  306. RETURN_OP_NAME(C_PARAM);
  307. RETURN_OP_NAME(C_FUNC);
  308. RETURN_OP_NAME(C_STR);
  309. RETURN_OP_NAME(C_CONSTSTR);
  310. RETURN_OP_NAME(C_ARG);
  311. RETURN_OP_NAME(C_NAME);
  312. RETURN_OP_NAME(C_EOL);
  313. RETURN_OP_NAME(C_RETINFO);
  314. RETURN_OP_NAME(C_USERFUNC);
  315. RETURN_OP_NAME(C_USERFUNC_POS);
  316. // operators
  317. RETURN_OP_NAME(C_OP3);
  318. RETURN_OP_NAME(C_LOR);
  319. RETURN_OP_NAME(C_LAND);
  320. RETURN_OP_NAME(C_LE);
  321. RETURN_OP_NAME(C_LT);
  322. RETURN_OP_NAME(C_GE);
  323. RETURN_OP_NAME(C_GT);
  324. RETURN_OP_NAME(C_EQ);
  325. RETURN_OP_NAME(C_NE);
  326. RETURN_OP_NAME(C_XOR);
  327. RETURN_OP_NAME(C_OR);
  328. RETURN_OP_NAME(C_AND);
  329. RETURN_OP_NAME(C_ADD);
  330. RETURN_OP_NAME(C_SUB);
  331. RETURN_OP_NAME(C_MUL);
  332. RETURN_OP_NAME(C_DIV);
  333. RETURN_OP_NAME(C_MOD);
  334. RETURN_OP_NAME(C_NEG);
  335. RETURN_OP_NAME(C_LNOT);
  336. RETURN_OP_NAME(C_NOT);
  337. RETURN_OP_NAME(C_R_SHIFT);
  338. RETURN_OP_NAME(C_L_SHIFT);
  339. default:
  340. ShowDebug("script_op2name: unexpected op=%d\n", op);
  341. return "???";
  342. }
  343. #undef RETURN_OP_NAME
  344. }
  345. #ifdef DEBUG_DUMP_STACK
  346. static void script_dump_stack(struct script_state* st)
  347. {
  348. int i;
  349. ShowMessage("\tstart = %d\n", st->start);
  350. ShowMessage("\tend = %d\n", st->end);
  351. ShowMessage("\tdefsp = %d\n", st->stack->defsp);
  352. ShowMessage("\tsp = %d\n", st->stack->sp);
  353. for( i = 0; i < st->stack->sp; ++i )
  354. {
  355. struct script_data* data = &st->stack->stack_data[i];
  356. ShowMessage("\t[%d] %s", i, script_op2name(data->type));
  357. switch( data->type )
  358. {
  359. case C_INT:
  360. case C_POS:
  361. ShowMessage(" %d\n", data->u.num);
  362. break;
  363. case C_STR:
  364. case C_CONSTSTR:
  365. ShowMessage(" \"%s\"\n", data->u.str);
  366. break;
  367. case C_NAME:
  368. 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));
  369. break;
  370. case C_RETINFO:
  371. {
  372. struct script_retinfo* ri = data->u.ri;
  373. ShowMessage(" %p {var_function=%p, script=%p, pos=%d, nargs=%d, defsp=%d}\n", ri, ri->var_function, ri->script, ri->pos, ri->nargs, ri->defsp);
  374. }
  375. break;
  376. default:
  377. ShowMessage("\n");
  378. break;
  379. }
  380. }
  381. }
  382. #endif
  383. /// Reports on the console the src of a script error.
  384. static void script_reportsrc(struct script_state *st)
  385. {
  386. struct block_list* bl;
  387. if( st->oid == 0 )
  388. return; //Can't report source.
  389. bl = map_id2bl(st->oid);
  390. if( bl == NULL )
  391. return;
  392. switch( bl->type )
  393. {
  394. case BL_NPC:
  395. if( bl->m >= 0 )
  396. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  397. else
  398. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  399. break;
  400. default:
  401. if( bl->m >= 0 )
  402. 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);
  403. else
  404. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  405. break;
  406. }
  407. }
  408. /// Reports on the console information about the script data.
  409. static void script_reportdata(struct script_data* data)
  410. {
  411. if( data == NULL )
  412. return;
  413. switch( data->type )
  414. {
  415. case C_NOP:// no value
  416. ShowDebug("Data: nothing (nil)\n");
  417. break;
  418. case C_INT:// number
  419. ShowDebug("Data: number value=%d\n", data->u.num);
  420. break;
  421. case C_STR:
  422. case C_CONSTSTR:// string
  423. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  424. break;
  425. case C_NAME:// reference
  426. if( reference_tovariable(data) )
  427. {// variable
  428. const char* name = reference_getname(data);
  429. if( not_array_variable(*name) )
  430. ShowDebug("Data: variable name='%s'\n", name);
  431. else
  432. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  433. }
  434. else if( reference_toconstant(data) )
  435. {// constant
  436. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  437. }
  438. else if( reference_toparam(data) )
  439. {// param
  440. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  441. }
  442. else
  443. {// ???
  444. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  445. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  446. }
  447. break;
  448. case C_POS:// label
  449. ShowDebug("Data: label pos=%d\n", data->u.num);
  450. break;
  451. default:
  452. ShowDebug("Data: %s\n", script_op2name(data->type));
  453. break;
  454. }
  455. }
  456. /*==========================================
  457. * エラーメッセージ出力
  458. *------------------------------------------*/
  459. static void disp_error_message2(const char *mes,const char *pos,int report)
  460. {
  461. error_msg = aStrdup(mes);
  462. error_pos = pos;
  463. error_report = report;
  464. longjmp( error_jump, 1 );
  465. }
  466. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  467. /// Checks event parameter validity
  468. static void check_event(struct script_state *st, const char *evt)
  469. {
  470. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  471. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  472. script_reportsrc(st);
  473. }
  474. }
  475. /*==========================================
  476. * Hashes the input string
  477. *------------------------------------------*/
  478. static unsigned int calc_hash(const char* p)
  479. {
  480. unsigned int h;
  481. #if defined(SCRIPT_HASH_DJB2)
  482. h = 5381;
  483. while( *p ) // hash*33 + c
  484. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  485. #elif defined(SCRIPT_HASH_SDBM)
  486. h = 0;
  487. while( *p ) // hash*65599 + c
  488. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  489. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  490. h = 0;
  491. while( *p ){
  492. unsigned int g;
  493. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  494. g = h & 0xF0000000;
  495. if( g )
  496. {
  497. h ^= g >> 24;
  498. h &= ~g;
  499. }
  500. }
  501. #else // athena hash
  502. h = 0;
  503. while( *p )
  504. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  505. #endif
  506. return h % SCRIPT_HASH_SIZE;
  507. }
  508. /*==========================================
  509. * str_data manipulation functions
  510. *------------------------------------------*/
  511. /// Looks up string using the provided id.
  512. const char* get_str(int id)
  513. {
  514. Assert( id >= LABEL_START && id < str_size );
  515. return str_buf+str_data[id].str;
  516. }
  517. /// Returns the uid of the string, or -1.
  518. static int search_str(const char* p)
  519. {
  520. int i;
  521. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  522. if( strcasecmp(get_str(i),p) == 0 )
  523. return i;
  524. return -1;
  525. }
  526. /// Stores a copy of the string and returns its id.
  527. /// If an identical string is already present, returns its id instead.
  528. int add_str(const char* p)
  529. {
  530. int i, h;
  531. int len;
  532. h = calc_hash(p);
  533. if( str_hash[h] == 0 )
  534. {// empty bucket, add new node here
  535. str_hash[h] = str_num;
  536. }
  537. else
  538. {// scan for end of list, or occurence of identical string
  539. for( i = str_hash[h]; ; i = str_data[i].next )
  540. {
  541. if( strcasecmp(get_str(i),p) == 0 )
  542. return i; // string already in list
  543. if( str_data[i].next == 0 )
  544. break; // reached the end
  545. }
  546. // append node to end of list
  547. str_data[i].next = str_num;
  548. }
  549. // grow list if neccessary
  550. if( str_num >= str_data_size )
  551. {
  552. str_data_size += 128;
  553. RECREATE(str_data,struct str_data_struct,str_data_size);
  554. memset(str_data + (str_data_size - 128), '\0', 128);
  555. }
  556. len=(int)strlen(p);
  557. // grow string buffer if neccessary
  558. while( str_pos+len+1 >= str_size )
  559. {
  560. str_size += 256;
  561. RECREATE(str_buf,char,str_size);
  562. memset(str_buf + (str_size - 256), '\0', 256);
  563. }
  564. safestrncpy(str_buf+str_pos, p, len+1);
  565. str_data[str_num].type = C_NOP;
  566. str_data[str_num].str = str_pos;
  567. str_data[str_num].next = 0;
  568. str_data[str_num].func = NULL;
  569. str_data[str_num].backpatch = -1;
  570. str_data[str_num].label = -1;
  571. str_pos += len+1;
  572. return str_num++;
  573. }
  574. /// Appends 1 byte to the script buffer.
  575. static void add_scriptb(int a)
  576. {
  577. if( script_pos+1 >= script_size )
  578. {
  579. script_size += SCRIPT_BLOCK_SIZE;
  580. RECREATE(script_buf,unsigned char,script_size);
  581. }
  582. script_buf[script_pos++] = (uint8)(a);
  583. }
  584. /// Appends a c_op value to the script buffer.
  585. /// The value is variable-length encoded into 8-bit blocks.
  586. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  587. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  588. static void add_scriptc(int a)
  589. {
  590. while( a >= 0x40 )
  591. {
  592. add_scriptb((a&0x3f)|0x40);
  593. a = (a - 0x40) >> 6;
  594. }
  595. add_scriptb(a);
  596. }
  597. /// Appends an integer value to the script buffer.
  598. /// The value is variable-length encoded into 8-bit blocks.
  599. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  600. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  601. static void add_scripti(int a)
  602. {
  603. while( a >= 0x40 )
  604. {
  605. add_scriptb((a&0x3f)|0xc0);
  606. a = (a - 0x40) >> 6;
  607. }
  608. add_scriptb(a|0x80);
  609. }
  610. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  611. ///
  612. /// @param l The id of the str_data entry
  613. // 最大16Mまで
  614. static void add_scriptl(int l)
  615. {
  616. int backpatch = str_data[l].backpatch;
  617. switch(str_data[l].type){
  618. case C_POS:
  619. case C_USERFUNC_POS:
  620. add_scriptc(C_POS);
  621. add_scriptb(str_data[l].label);
  622. add_scriptb(str_data[l].label>>8);
  623. add_scriptb(str_data[l].label>>16);
  624. break;
  625. case C_NOP:
  626. case C_USERFUNC:
  627. // ラベルの可能性があるのでbackpatch用データ埋め込み
  628. add_scriptc(C_NAME);
  629. str_data[l].backpatch = script_pos;
  630. add_scriptb(backpatch);
  631. add_scriptb(backpatch>>8);
  632. add_scriptb(backpatch>>16);
  633. break;
  634. case C_INT:
  635. add_scripti(abs(str_data[l].val));
  636. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  637. add_scriptc(C_NEG);
  638. break;
  639. default: // assume C_NAME
  640. add_scriptc(C_NAME);
  641. add_scriptb(l);
  642. add_scriptb(l>>8);
  643. add_scriptb(l>>16);
  644. break;
  645. }
  646. }
  647. /*==========================================
  648. * ラベルを解決する
  649. *------------------------------------------*/
  650. void set_label(int l,int pos, const char* script_pos)
  651. {
  652. int i,next;
  653. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  654. { //Prevent overwriting constants values and parameters [Skotlex]
  655. disp_error_message("set_label: invalid label name",script_pos);
  656. return;
  657. }
  658. if(str_data[l].label!=-1){
  659. disp_error_message("set_label: dup label ",script_pos);
  660. return;
  661. }
  662. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  663. str_data[l].label=pos;
  664. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  665. next=GETVALUE(script_buf,i);
  666. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  667. SETVALUE(script_buf,i,pos);
  668. i=next;
  669. }
  670. }
  671. /// Skips spaces and/or comments.
  672. const char* skip_space(const char* p)
  673. {
  674. if( p == NULL )
  675. return NULL;
  676. for(;;)
  677. {
  678. while( ISSPACE(*p) )
  679. ++p;
  680. if( *p == '/' && p[1] == '/' )
  681. {// line comment
  682. while(*p && *p!='\n')
  683. ++p;
  684. }
  685. else if( *p == '/' && p[1] == '*' )
  686. {// block comment
  687. p += 2;
  688. for(;;)
  689. {
  690. if( *p == '\0' )
  691. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  692. if( *p == '*' && p[1] == '/' )
  693. {// end of block comment
  694. p += 2;
  695. break;
  696. }
  697. ++p;
  698. }
  699. }
  700. else
  701. break;
  702. }
  703. return p;
  704. }
  705. /// Skips a word.
  706. /// A word consists of undercores and/or alfanumeric characters,
  707. /// and valid variable prefixes/postfixes.
  708. static
  709. const char* skip_word(const char* p)
  710. {
  711. // prefix
  712. switch( *p )
  713. {
  714. case '@':// temporary char variable
  715. ++p; break;
  716. case '#':// account variable
  717. p += ( p[1] == '#' ? 2 : 1 ); break;
  718. case '\'':// instance variable
  719. ++p; break;
  720. case '.':// npc variable
  721. p += ( p[1] == '@' ? 2 : 1 ); break;
  722. case '$':// global variable
  723. p += ( p[1] == '@' ? 2 : 1 ); break;
  724. }
  725. while( ISALNUM(*p) || *p == '_' )
  726. ++p;
  727. // postfix
  728. if( *p == '$' )// string
  729. p++;
  730. return p;
  731. }
  732. /// Adds a word to str_data.
  733. /// @see skip_word
  734. /// @see add_str
  735. static
  736. int add_word(const char* p)
  737. {
  738. char* word;
  739. int len;
  740. int i;
  741. // Check for a word
  742. len = skip_word(p) - p;
  743. if( len == 0 )
  744. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  745. // Duplicate the word
  746. CREATE(word, char, len+1);
  747. memcpy(word, p, len);
  748. word[len] = 0;
  749. // add the word
  750. i = add_str(word);
  751. aFree(word);
  752. return i;
  753. }
  754. /// Parses a function call.
  755. /// The argument list can have parenthesis or not.
  756. /// The number of arguments is checked.
  757. static
  758. const char* parse_callfunc(const char* p, int require_paren)
  759. {
  760. const char* p2;
  761. const char* arg=NULL;
  762. int func;
  763. func = add_word(p);
  764. if( str_data[func].type == C_FUNC ){
  765. // buildin function
  766. add_scriptl(func);
  767. add_scriptc(C_ARG);
  768. arg = buildin_func[str_data[func].val].arg;
  769. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  770. // script defined function
  771. int callsub = search_str("callsub");
  772. add_scriptl(callsub);
  773. add_scriptc(C_ARG);
  774. add_scriptl(func);
  775. arg = buildin_func[str_data[callsub].val].arg;
  776. if( *arg == 0 )
  777. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  778. if( *arg != '*' )
  779. ++arg; // count func as argument
  780. } else
  781. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  782. p = skip_word(p);
  783. p = skip_space(p);
  784. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  785. syntax.curly[syntax.curly_count].count = 0;
  786. if( *p == ';' )
  787. {// <func name> ';'
  788. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  789. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  790. {// <func name> '(' ')'
  791. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  792. p = p2;
  793. /*
  794. } else if( 0 && require_paren && *p != '(' )
  795. {// <func name>
  796. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  797. */
  798. } else
  799. {// <func name> <arg list>
  800. if( require_paren ){
  801. if( *p != '(' )
  802. disp_error_message("need '('",p);
  803. ++p; // skip '('
  804. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  805. } else if( *p == '(' ){
  806. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  807. } else {
  808. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  809. }
  810. ++syntax.curly_count;
  811. while( *arg ) {
  812. p2=parse_subexpr(p,-1);
  813. if( p == p2 )
  814. break; // not an argument
  815. if( *arg != '*' )
  816. ++arg; // next argument
  817. p=skip_space(p2);
  818. if( *arg == 0 || *p != ',' )
  819. break; // no more arguments
  820. ++p; // skip comma
  821. }
  822. --syntax.curly_count;
  823. }
  824. if( *arg && *arg != '?' && *arg != '*' )
  825. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  826. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  827. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  828. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  829. if( *p != ')' )
  830. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  831. ++p;
  832. }
  833. add_scriptc(C_FUNC);
  834. return p;
  835. }
  836. /*==========================================
  837. * 項の解析
  838. *------------------------------------------*/
  839. const char* parse_simpleexpr(const char *p)
  840. {
  841. int i;
  842. p=skip_space(p);
  843. if(*p==';' || *p==',')
  844. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  845. if(*p=='('){
  846. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  847. ++syntax.curly[i].count;
  848. p=parse_subexpr(p+1,-1);
  849. p=skip_space(p);
  850. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  851. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  852. ){
  853. if( *p == ',' ){
  854. syntax.curly[i].flag = ARGLIST_PAREN;
  855. return p;
  856. } else
  857. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  858. }
  859. if( *p != ')' )
  860. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  861. ++p;
  862. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  863. char *np;
  864. i=strtoul(p,&np,0);
  865. add_scripti(i);
  866. p=np;
  867. } else if(*p=='"'){
  868. add_scriptc(C_STR);
  869. p++;
  870. while( *p && *p != '"' ){
  871. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  872. {
  873. char buf[8];
  874. size_t len = skip_escaped_c(p) - p;
  875. size_t n = sv_unescape_c(buf, p, len);
  876. if( n != 1 )
  877. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  878. p += len;
  879. add_scriptb(*buf);
  880. continue;
  881. }
  882. else if( *p == '\n' )
  883. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  884. add_scriptb(*p++);
  885. }
  886. if(!*p)
  887. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  888. add_scriptb(0);
  889. p++; //'"'
  890. } else {
  891. int l;
  892. // label , register , function etc
  893. if(skip_word(p)==p)
  894. disp_error_message("parse_simpleexpr: unexpected character",p);
  895. l=add_word(p);
  896. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  897. return parse_callfunc(p,1);
  898. p=skip_word(p);
  899. if( *p == '[' ){
  900. // array(name[i] => getelementofarray(name,i) )
  901. add_scriptl(search_str("getelementofarray"));
  902. add_scriptc(C_ARG);
  903. add_scriptl(l);
  904. p=parse_subexpr(p+1,-1);
  905. p=skip_space(p);
  906. if( *p != ']' )
  907. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  908. ++p;
  909. add_scriptc(C_FUNC);
  910. }else
  911. add_scriptl(l);
  912. }
  913. return p;
  914. }
  915. /*==========================================
  916. * 式の解析
  917. *------------------------------------------*/
  918. const char* parse_subexpr(const char* p,int limit)
  919. {
  920. int op,opl,len;
  921. const char* tmpp;
  922. p=skip_space(p);
  923. if(*p=='-'){
  924. tmpp=skip_space(p+1);
  925. if(*tmpp==';' || *tmpp==','){
  926. add_scriptl(LABEL_NEXTLINE);
  927. p++;
  928. return p;
  929. }
  930. }
  931. tmpp=p;
  932. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  933. p=parse_subexpr(p+1,10);
  934. add_scriptc(op);
  935. } else
  936. p=parse_simpleexpr(p);
  937. p=skip_space(p);
  938. while((
  939. (op=C_OP3,opl=0,len=1,*p=='?') ||
  940. (op=C_ADD,opl=8,len=1,*p=='+') ||
  941. (op=C_SUB,opl=8,len=1,*p=='-') ||
  942. (op=C_MUL,opl=9,len=1,*p=='*') ||
  943. (op=C_DIV,opl=9,len=1,*p=='/') ||
  944. (op=C_MOD,opl=9,len=1,*p=='%') ||
  945. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  946. (op=C_AND,opl=6,len=1,*p=='&') ||
  947. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  948. (op=C_OR,opl=5,len=1,*p=='|') ||
  949. (op=C_XOR,opl=4,len=1,*p=='^') ||
  950. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  951. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  952. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  953. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  954. (op=C_GT,opl=3,len=1,*p=='>') ||
  955. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  956. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  957. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  958. p+=len;
  959. if(op == C_OP3) {
  960. p=parse_subexpr(p,-1);
  961. p=skip_space(p);
  962. if( *(p++) != ':')
  963. disp_error_message("parse_subexpr: need ':'", p-1);
  964. p=parse_subexpr(p,-1);
  965. } else {
  966. p=parse_subexpr(p,opl);
  967. }
  968. add_scriptc(op);
  969. p=skip_space(p);
  970. }
  971. return p; /* return first untreated operator */
  972. }
  973. /*==========================================
  974. * 式の評価
  975. *------------------------------------------*/
  976. const char* parse_expr(const char *p)
  977. {
  978. switch(*p){
  979. case ')': case ';': case ':': case '[': case ']':
  980. case '}':
  981. disp_error_message("parse_expr: unexpected char",p);
  982. }
  983. p=parse_subexpr(p,-1);
  984. return p;
  985. }
  986. /*==========================================
  987. * 行の解析
  988. *------------------------------------------*/
  989. const char* parse_line(const char* p)
  990. {
  991. const char* p2;
  992. p=skip_space(p);
  993. if(*p==';') {
  994. // if(); for(); while(); のために閉じ判定
  995. p = parse_syntax_close(p + 1);
  996. return p;
  997. }
  998. if(*p==')' && parse_syntax_for_flag)
  999. return p+1;
  1000. p = skip_space(p);
  1001. if(p[0] == '{') {
  1002. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1003. syntax.curly[syntax.curly_count].count = -1;
  1004. syntax.curly[syntax.curly_count].index = -1;
  1005. syntax.curly_count++;
  1006. return p + 1;
  1007. } else if(p[0] == '}') {
  1008. return parse_curly_close(p);
  1009. }
  1010. // 構文関連の処理
  1011. p2 = parse_syntax(p);
  1012. if(p2 != NULL)
  1013. return p2;
  1014. p = parse_callfunc(p,0);
  1015. p = skip_space(p);
  1016. if(parse_syntax_for_flag) {
  1017. if( *p != ')' )
  1018. disp_error_message("parse_line: need ')'",p);
  1019. } else {
  1020. if( *p != ';' )
  1021. disp_error_message("parse_line: need ';'",p);
  1022. }
  1023. // if, for , while の閉じ判定
  1024. p = parse_syntax_close(p+1);
  1025. return p;
  1026. }
  1027. // { ... } の閉じ処理
  1028. const char* parse_curly_close(const char* p)
  1029. {
  1030. if(syntax.curly_count <= 0) {
  1031. disp_error_message("parse_curly_close: unexpected string",p);
  1032. return p + 1;
  1033. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1034. syntax.curly_count--;
  1035. // if, for , while の閉じ判定
  1036. p = parse_syntax_close(p + 1);
  1037. return p;
  1038. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1039. // switch() 閉じ判定
  1040. int pos = syntax.curly_count-1;
  1041. char label[256];
  1042. int l;
  1043. // 一時変数を消す
  1044. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1045. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1046. parse_line(label);
  1047. syntax.curly_count--;
  1048. // 無条件で終了ポインタに移動
  1049. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1050. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1051. parse_line(label);
  1052. syntax.curly_count--;
  1053. // 現在地のラベルを付ける
  1054. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1055. l=add_str(label);
  1056. set_label(l,script_pos, p);
  1057. if(syntax.curly[pos].flag) {
  1058. // default が存在する
  1059. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1060. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1061. parse_line(label);
  1062. syntax.curly_count--;
  1063. }
  1064. // 終了ラベルを付ける
  1065. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1066. l=add_str(label);
  1067. set_label(l,script_pos, p);
  1068. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1069. syntax.curly_count--;
  1070. return p+1;
  1071. } else {
  1072. disp_error_message("parse_curly_close: unexpected string",p);
  1073. return p + 1;
  1074. }
  1075. }
  1076. // 構文関連の処理
  1077. // break, case, continue, default, do, for, function,
  1078. // if, switch, while をこの内部で処理します。
  1079. const char* parse_syntax(const char* p)
  1080. {
  1081. const char *p2 = skip_word(p);
  1082. switch(*p) {
  1083. case 'B':
  1084. case 'b':
  1085. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1086. // break の処理
  1087. char label[256];
  1088. int pos = syntax.curly_count - 1;
  1089. while(pos >= 0) {
  1090. if(syntax.curly[pos].type == TYPE_DO) {
  1091. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1092. break;
  1093. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1094. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1095. break;
  1096. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1097. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1098. break;
  1099. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1100. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1101. break;
  1102. }
  1103. pos--;
  1104. }
  1105. if(pos < 0) {
  1106. disp_error_message("parse_syntax: unexpected 'break'",p);
  1107. } else {
  1108. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1109. parse_line(label);
  1110. syntax.curly_count--;
  1111. }
  1112. p = skip_space(p2);
  1113. if(*p != ';')
  1114. disp_error_message("parse_syntax: need ';'",p);
  1115. // if, for , while の閉じ判定
  1116. p = parse_syntax_close(p + 1);
  1117. return p;
  1118. }
  1119. break;
  1120. case 'c':
  1121. case 'C':
  1122. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1123. // case の処理
  1124. int pos = syntax.curly_count-1;
  1125. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1126. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1127. return p+1;
  1128. } else {
  1129. char label[256];
  1130. int l,v;
  1131. char *np;
  1132. if(syntax.curly[pos].count != 1) {
  1133. // FALLTHRU 用のジャンプ
  1134. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1135. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1136. parse_line(label);
  1137. syntax.curly_count--;
  1138. // 現在地のラベルを付ける
  1139. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1140. l=add_str(label);
  1141. set_label(l,script_pos, p);
  1142. }
  1143. // switch 判定文
  1144. p = skip_space(p2);
  1145. if(p == p2) {
  1146. disp_error_message("parse_syntax: expect space ' '",p);
  1147. }
  1148. // check whether case label is integer or not
  1149. v = strtol(p,&np,0);
  1150. if(np == p) { //Check for constants
  1151. p2 = skip_word(p);
  1152. v = p2-p; // length of word at p2
  1153. memcpy(label,p,v);
  1154. label[v]='\0';
  1155. v = search_str(label);
  1156. if (v < 0 || str_data[v].type != C_INT)
  1157. disp_error_message("parse_syntax: 'case' label not integer",p);
  1158. v = str_data[v].val;
  1159. p = skip_word(p);
  1160. } else { //Numeric value
  1161. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1162. p++;
  1163. p = skip_word(p);
  1164. if(np != p)
  1165. disp_error_message("parse_syntax: 'case' label not integer",np);
  1166. }
  1167. p = skip_space(p);
  1168. if(*p != ':')
  1169. disp_error_message("parse_syntax: expect ':'",p);
  1170. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1171. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1172. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1173. // 2回parse しないとダメ
  1174. p2 = parse_line(label);
  1175. parse_line(p2);
  1176. syntax.curly_count--;
  1177. if(syntax.curly[pos].count != 1) {
  1178. // FALLTHRU 終了後のラベル
  1179. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1180. l=add_str(label);
  1181. set_label(l,script_pos,p);
  1182. }
  1183. // check duplication of case label [Rayce]
  1184. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1185. disp_error_message("parse_syntax: dup 'case'",p);
  1186. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1187. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1188. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1189. parse_line(label);
  1190. syntax.curly_count--;
  1191. syntax.curly[pos].count++;
  1192. }
  1193. return p + 1;
  1194. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1195. // continue の処理
  1196. char label[256];
  1197. int pos = syntax.curly_count - 1;
  1198. while(pos >= 0) {
  1199. if(syntax.curly[pos].type == TYPE_DO) {
  1200. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1201. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1202. break;
  1203. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1204. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1205. break;
  1206. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1207. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1208. break;
  1209. }
  1210. pos--;
  1211. }
  1212. if(pos < 0) {
  1213. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1214. } else {
  1215. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1216. parse_line(label);
  1217. syntax.curly_count--;
  1218. }
  1219. p = skip_space(p2);
  1220. if(*p != ';')
  1221. disp_error_message("parse_syntax: need ';'",p);
  1222. // if, for , while の閉じ判定
  1223. p = parse_syntax_close(p + 1);
  1224. return p;
  1225. }
  1226. break;
  1227. case 'd':
  1228. case 'D':
  1229. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1230. // switch - default の処理
  1231. int pos = syntax.curly_count-1;
  1232. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1233. disp_error_message("parse_syntax: unexpected 'default'",p);
  1234. } else if(syntax.curly[pos].flag) {
  1235. disp_error_message("parse_syntax: dup 'default'",p);
  1236. } else {
  1237. char label[256];
  1238. int l;
  1239. // 現在地のラベルを付ける
  1240. p = skip_space(p2);
  1241. if(*p != ':') {
  1242. disp_error_message("parse_syntax: need ':'",p);
  1243. }
  1244. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1245. l=add_str(label);
  1246. set_label(l,script_pos,p);
  1247. // 無条件で次のリンクに飛ばす
  1248. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1249. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1250. parse_line(label);
  1251. syntax.curly_count--;
  1252. // default のラベルを付ける
  1253. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1254. l=add_str(label);
  1255. set_label(l,script_pos,p);
  1256. syntax.curly[syntax.curly_count - 1].flag = 1;
  1257. syntax.curly[pos].count++;
  1258. }
  1259. return p + 1;
  1260. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1261. int l;
  1262. char label[256];
  1263. p=skip_space(p2);
  1264. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1265. syntax.curly[syntax.curly_count].count = 1;
  1266. syntax.curly[syntax.curly_count].index = syntax.index++;
  1267. syntax.curly[syntax.curly_count].flag = 0;
  1268. // 現在地のラベル形成する
  1269. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1270. l=add_str(label);
  1271. set_label(l,script_pos,p);
  1272. syntax.curly_count++;
  1273. return p;
  1274. }
  1275. break;
  1276. case 'f':
  1277. case 'F':
  1278. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1279. int l;
  1280. char label[256];
  1281. int pos = syntax.curly_count;
  1282. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1283. syntax.curly[syntax.curly_count].count = 1;
  1284. syntax.curly[syntax.curly_count].index = syntax.index++;
  1285. syntax.curly[syntax.curly_count].flag = 0;
  1286. syntax.curly_count++;
  1287. p=skip_space(p2);
  1288. if(*p != '(')
  1289. disp_error_message("parse_syntax: need '('",p);
  1290. p++;
  1291. // 初期化文を実行する
  1292. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1293. p=parse_line(p);
  1294. syntax.curly_count--;
  1295. // 条件判断開始のラベル形成する
  1296. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1297. l=add_str(label);
  1298. set_label(l,script_pos,p);
  1299. p=skip_space(p);
  1300. if(*p == ';') {
  1301. // for(;;) のパターンなので必ず真
  1302. ;
  1303. } else {
  1304. // 条件が偽なら終了地点に飛ばす
  1305. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1306. add_scriptl(add_str("jump_zero"));
  1307. add_scriptc(C_ARG);
  1308. p=parse_expr(p);
  1309. p=skip_space(p);
  1310. add_scriptl(add_str(label));
  1311. add_scriptc(C_FUNC);
  1312. }
  1313. if(*p != ';')
  1314. disp_error_message("parse_syntax: need ';'",p);
  1315. p++;
  1316. // ループ開始に飛ばす
  1317. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1318. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1319. parse_line(label);
  1320. syntax.curly_count--;
  1321. // 次のループへのラベル形成する
  1322. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1323. l=add_str(label);
  1324. set_label(l,script_pos,p);
  1325. // 次のループに入る時の処理
  1326. // for 最後の ')' を ';' として扱うフラグ
  1327. parse_syntax_for_flag = 1;
  1328. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1329. p=parse_line(p);
  1330. syntax.curly_count--;
  1331. parse_syntax_for_flag = 0;
  1332. // 条件判定処理に飛ばす
  1333. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1334. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1335. parse_line(label);
  1336. syntax.curly_count--;
  1337. // ループ開始のラベル付け
  1338. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1339. l=add_str(label);
  1340. set_label(l,script_pos,p);
  1341. return p;
  1342. }
  1343. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1344. {// internal script function
  1345. const char *func_name;
  1346. func_name = skip_space(p2);
  1347. p = skip_word(func_name);
  1348. if( p == func_name )
  1349. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1350. p2 = skip_space(p);
  1351. if( *p2 == ';' )
  1352. {// function <name> ;
  1353. // function declaration - just register the name
  1354. int l;
  1355. l = add_word(func_name);
  1356. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1357. str_data[l].type = C_USERFUNC;
  1358. // if, for , while の閉じ判定
  1359. p = parse_syntax_close(p2 + 1);
  1360. return p; }
  1361. else if(*p2 == '{')
  1362. {// function <name> <line/block of code>
  1363. char label[256];
  1364. int l;
  1365. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1366. syntax.curly[syntax.curly_count].count = 1;
  1367. syntax.curly[syntax.curly_count].index = syntax.index++;
  1368. syntax.curly[syntax.curly_count].flag = 0;
  1369. ++syntax.curly_count;
  1370. // Jump over the function code
  1371. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1372. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1373. ++syntax.curly_count;
  1374. parse_line(label);
  1375. --syntax.curly_count;
  1376. // Set the position of the function (label)
  1377. l=add_word(func_name);
  1378. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1379. str_data[l].type = C_USERFUNC;
  1380. set_label(l, script_pos, p);
  1381. if( parse_options&SCRIPT_USE_LABEL_DB )
  1382. strdb_put(scriptlabel_db, get_str(l), (void*)script_pos);
  1383. return skip_space(p);
  1384. }
  1385. else
  1386. {
  1387. disp_error_message("expect ';' or '{' at function syntax",p);
  1388. }
  1389. }
  1390. break;
  1391. case 'i':
  1392. case 'I':
  1393. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1394. // if() の処理
  1395. char label[256];
  1396. p=skip_space(p2);
  1397. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1398. disp_error_message("need '('",p);
  1399. }
  1400. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1401. syntax.curly[syntax.curly_count].count = 1;
  1402. syntax.curly[syntax.curly_count].index = syntax.index++;
  1403. syntax.curly[syntax.curly_count].flag = 0;
  1404. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1405. syntax.curly_count++;
  1406. add_scriptl(add_str("jump_zero"));
  1407. add_scriptc(C_ARG);
  1408. p=parse_expr(p);
  1409. p=skip_space(p);
  1410. add_scriptl(add_str(label));
  1411. add_scriptc(C_FUNC);
  1412. return p;
  1413. }
  1414. break;
  1415. case 's':
  1416. case 'S':
  1417. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1418. // switch() の処理
  1419. char label[256];
  1420. p=skip_space(p2);
  1421. if(*p != '(') {
  1422. disp_error_message("need '('",p);
  1423. }
  1424. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1425. syntax.curly[syntax.curly_count].count = 1;
  1426. syntax.curly[syntax.curly_count].index = syntax.index++;
  1427. syntax.curly[syntax.curly_count].flag = 0;
  1428. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1429. syntax.curly_count++;
  1430. add_scriptl(add_str("set"));
  1431. add_scriptc(C_ARG);
  1432. add_scriptl(add_str(label));
  1433. p=parse_expr(p);
  1434. p=skip_space(p);
  1435. if(*p != '{') {
  1436. disp_error_message("parse_syntax: need '{'",p);
  1437. }
  1438. add_scriptc(C_FUNC);
  1439. return p + 1;
  1440. }
  1441. break;
  1442. case 'w':
  1443. case 'W':
  1444. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1445. int l;
  1446. char label[256];
  1447. p=skip_space(p2);
  1448. if(*p != '(') {
  1449. disp_error_message("need '('",p);
  1450. }
  1451. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1452. syntax.curly[syntax.curly_count].count = 1;
  1453. syntax.curly[syntax.curly_count].index = syntax.index++;
  1454. syntax.curly[syntax.curly_count].flag = 0;
  1455. // 条件判断開始のラベル形成する
  1456. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1457. l=add_str(label);
  1458. set_label(l,script_pos,p);
  1459. // 条件が偽なら終了地点に飛ばす
  1460. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1461. syntax.curly_count++;
  1462. add_scriptl(add_str("jump_zero"));
  1463. add_scriptc(C_ARG);
  1464. p=parse_expr(p);
  1465. p=skip_space(p);
  1466. add_scriptl(add_str(label));
  1467. add_scriptc(C_FUNC);
  1468. return p;
  1469. }
  1470. break;
  1471. }
  1472. return NULL;
  1473. }
  1474. const char* parse_syntax_close(const char *p) {
  1475. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1476. int flag;
  1477. do {
  1478. p = parse_syntax_close_sub(p,&flag);
  1479. } while(flag);
  1480. return p;
  1481. }
  1482. // if, for , while , do の閉じ判定
  1483. // flag == 1 : 閉じられた
  1484. // flag == 0 : 閉じられない
  1485. const char* parse_syntax_close_sub(const char* p,int* flag)
  1486. {
  1487. char label[256];
  1488. int pos = syntax.curly_count - 1;
  1489. int l;
  1490. *flag = 1;
  1491. if(syntax.curly_count <= 0) {
  1492. *flag = 0;
  1493. return p;
  1494. } else if(syntax.curly[pos].type == TYPE_IF) {
  1495. const char *bp = p;
  1496. const char *p2;
  1497. // if 最終場所へ飛ばす
  1498. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1499. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1500. parse_line(label);
  1501. syntax.curly_count--;
  1502. // 現在地のラベルを付ける
  1503. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1504. l=add_str(label);
  1505. set_label(l,script_pos,p);
  1506. syntax.curly[pos].count++;
  1507. p = skip_space(p);
  1508. p2 = skip_word(p);
  1509. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1510. // else or else - if
  1511. p = skip_space(p2);
  1512. p2 = skip_word(p);
  1513. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1514. // else - if
  1515. p=skip_space(p2);
  1516. if(*p != '(') {
  1517. disp_error_message("need '('",p);
  1518. }
  1519. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1520. add_scriptl(add_str("jump_zero"));
  1521. add_scriptc(C_ARG);
  1522. p=parse_expr(p);
  1523. p=skip_space(p);
  1524. add_scriptl(add_str(label));
  1525. add_scriptc(C_FUNC);
  1526. *flag = 0;
  1527. return p;
  1528. } else {
  1529. // else
  1530. if(!syntax.curly[pos].flag) {
  1531. syntax.curly[pos].flag = 1;
  1532. *flag = 0;
  1533. return p;
  1534. }
  1535. }
  1536. }
  1537. // if 閉じ
  1538. syntax.curly_count--;
  1539. // 最終地のラベルを付ける
  1540. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1541. l=add_str(label);
  1542. set_label(l,script_pos,p);
  1543. if(syntax.curly[pos].flag == 1) {
  1544. // このifに対するelseじゃないのでポインタの位置は同じ
  1545. return bp;
  1546. }
  1547. return p;
  1548. } else if(syntax.curly[pos].type == TYPE_DO) {
  1549. int l;
  1550. char label[256];
  1551. const char *p2;
  1552. if(syntax.curly[pos].flag) {
  1553. // 現在地のラベル形成する(continue でここに来る)
  1554. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1555. l=add_str(label);
  1556. set_label(l,script_pos,p);
  1557. }
  1558. // 条件が偽なら終了地点に飛ばす
  1559. p = skip_space(p);
  1560. p2 = skip_word(p);
  1561. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1562. disp_error_message("parse_syntax: need 'while'",p);
  1563. p = skip_space(p2);
  1564. if(*p != '(') {
  1565. disp_error_message("need '('",p);
  1566. }
  1567. sprintf(label,"__DO%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. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1576. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1577. parse_line(label);
  1578. syntax.curly_count--;
  1579. // 条件終了地点のラベル形成する
  1580. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1581. l=add_str(label);
  1582. set_label(l,script_pos,p);
  1583. p = skip_space(p);
  1584. if(*p != ';') {
  1585. disp_error_message("parse_syntax: need ';'",p);
  1586. return p+1;
  1587. }
  1588. p++;
  1589. syntax.curly_count--;
  1590. return p;
  1591. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1592. // 次のループに飛ばす
  1593. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1594. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1595. parse_line(label);
  1596. syntax.curly_count--;
  1597. // for 終了のラベル付け
  1598. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1599. l=add_str(label);
  1600. set_label(l,script_pos,p);
  1601. syntax.curly_count--;
  1602. return p;
  1603. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1604. // while 条件判断へ飛ばす
  1605. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1606. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1607. parse_line(label);
  1608. syntax.curly_count--;
  1609. // while 終了のラベル付け
  1610. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1611. l=add_str(label);
  1612. set_label(l,script_pos,p);
  1613. syntax.curly_count--;
  1614. return p;
  1615. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1616. int pos = syntax.curly_count-1;
  1617. char label[256];
  1618. int l;
  1619. // 戻す
  1620. sprintf(label,"return;");
  1621. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1622. parse_line(label);
  1623. syntax.curly_count--;
  1624. // 現在地のラベルを付ける
  1625. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1626. l=add_str(label);
  1627. set_label(l,script_pos,p);
  1628. syntax.curly_count--;
  1629. return p;
  1630. } else {
  1631. *flag = 0;
  1632. return p;
  1633. }
  1634. }
  1635. /*==========================================
  1636. * 組み込み関数の追加
  1637. *------------------------------------------*/
  1638. static void add_buildin_func(void)
  1639. {
  1640. int i,n;
  1641. const char* p;
  1642. for( i = 0; buildin_func[i].func; i++ )
  1643. {
  1644. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1645. // 'v' - value (either string or int or reference)
  1646. // 's' - string
  1647. // 'i' - int
  1648. // 'r' - reference (of a variable)
  1649. // 'l' - label
  1650. // '?' - one optional parameter
  1651. // '*' - unknown number of optional parameters
  1652. p = buildin_func[i].arg;
  1653. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1654. while( *p == '?' ) ++p;
  1655. if( *p == '*' ) ++p;
  1656. if( *p != 0){
  1657. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1658. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1659. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1660. } else {
  1661. n = add_str(buildin_func[i].name);
  1662. str_data[n].type = C_FUNC;
  1663. str_data[n].val = i;
  1664. str_data[n].func = buildin_func[i].func;
  1665. }
  1666. }
  1667. }
  1668. /*==========================================
  1669. * 定数データベースの読み込み
  1670. *------------------------------------------*/
  1671. static void read_constdb(void)
  1672. {
  1673. FILE *fp;
  1674. char line[1024],name[1024],val[1024];
  1675. int n,type;
  1676. sprintf(line, "%s/const.txt", db_path);
  1677. fp=fopen(line, "r");
  1678. if(fp==NULL){
  1679. ShowError("can't read %s\n", line);
  1680. return ;
  1681. }
  1682. while(fgets(line, sizeof(line), fp))
  1683. {
  1684. if(line[0]=='/' && line[1]=='/')
  1685. continue;
  1686. type=0;
  1687. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1688. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1689. n=add_str(name);
  1690. if(type==0)
  1691. str_data[n].type=C_INT;
  1692. else
  1693. str_data[n].type=C_PARAM;
  1694. str_data[n].val= (int)strtol(val,NULL,0);
  1695. }
  1696. }
  1697. fclose(fp);
  1698. }
  1699. /*==========================================
  1700. * エラー表示
  1701. *------------------------------------------*/
  1702. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1703. {
  1704. int i;
  1705. if( p == NULL || !p[0] ) return NULL;
  1706. if( line < 0 )
  1707. StringBuf_Printf(buf, "*% 5d : ", -line);
  1708. else
  1709. StringBuf_Printf(buf, " % 5d : ", line);
  1710. for(i=0;p[i] && p[i] != '\n';i++){
  1711. if(p + i != mark)
  1712. StringBuf_Printf(buf, "%c", p[i]);
  1713. else
  1714. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1715. }
  1716. StringBuf_AppendStr(buf, "\n");
  1717. return p+i+(p[i] == '\n' ? 1 : 0);
  1718. }
  1719. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1720. {
  1721. // エラーが発生した行を求める
  1722. int j;
  1723. int line = start_line;
  1724. const char *p;
  1725. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1726. StringBuf buf;
  1727. for(p=src;p && *p;line++){
  1728. const char *lineend=strchr(p,'\n');
  1729. if(lineend==NULL || error_pos<lineend){
  1730. break;
  1731. }
  1732. for( j = 0; j < 4; j++ ) {
  1733. linestart[j] = linestart[j+1];
  1734. }
  1735. linestart[4] = p;
  1736. p=lineend+1;
  1737. }
  1738. StringBuf_Init(&buf);
  1739. StringBuf_AppendStr(&buf, "\a\n");
  1740. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1741. StringBuf_Printf(&buf, " %s\n", error_msg);
  1742. for(j = 0; j < 5; j++ ) {
  1743. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1744. }
  1745. p = script_print_line(&buf, p, error_pos, -line);
  1746. for(j = 0; j < 5; j++) {
  1747. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1748. }
  1749. ShowError("%s", StringBuf_Value(&buf));
  1750. StringBuf_Destroy(&buf);
  1751. }
  1752. /*==========================================
  1753. * スクリプトの解析
  1754. *------------------------------------------*/
  1755. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1756. {
  1757. const char *p,*tmpp;
  1758. int i;
  1759. struct script_code* code = NULL;
  1760. static int first=1;
  1761. char end;
  1762. if( src == NULL )
  1763. return NULL;// empty script
  1764. memset(&syntax,0,sizeof(syntax));
  1765. if(first){
  1766. add_buildin_func();
  1767. read_constdb();
  1768. first=0;
  1769. }
  1770. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1771. script_pos=0;
  1772. script_size=SCRIPT_BLOCK_SIZE;
  1773. str_data[LABEL_NEXTLINE].type=C_NOP;
  1774. str_data[LABEL_NEXTLINE].backpatch=-1;
  1775. str_data[LABEL_NEXTLINE].label=-1;
  1776. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1777. if( options&SCRIPT_USE_LABEL_DB )
  1778. scriptlabel_db->clear(scriptlabel_db, NULL);
  1779. parse_options = options;
  1780. if( setjmp( error_jump ) != 0 ) {
  1781. //Restore program state when script has problems. [from jA]
  1782. int i;
  1783. const int size = ARRAYLENGTH(syntax.curly);
  1784. if( error_report )
  1785. script_error(src,file,line,error_msg,error_pos);
  1786. aFree( error_msg );
  1787. aFree( script_buf );
  1788. script_pos = 0;
  1789. script_size = 0;
  1790. script_buf = NULL;
  1791. for(i=LABEL_START;i<str_num;i++)
  1792. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1793. for(i=0; i<size; i++)
  1794. linkdb_final(&syntax.curly[i].case_label);
  1795. return NULL;
  1796. }
  1797. parse_syntax_for_flag=0;
  1798. p=src;
  1799. p=skip_space(p);
  1800. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1801. {// does not require brackets around the script
  1802. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1803. {// empty script and can return NULL
  1804. aFree( script_buf );
  1805. script_pos = 0;
  1806. script_size = 0;
  1807. script_buf = NULL;
  1808. return NULL;
  1809. }
  1810. end = '\0';
  1811. }
  1812. else
  1813. {// requires brackets around the script
  1814. if( *p != '{' )
  1815. disp_error_message("not found '{'",p);
  1816. p = skip_space(p+1);
  1817. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1818. {// empty script and can return NULL
  1819. aFree( script_buf );
  1820. script_pos = 0;
  1821. script_size = 0;
  1822. script_buf = NULL;
  1823. return NULL;
  1824. }
  1825. end = '}';
  1826. }
  1827. // clear references of labels, variables and internal functions
  1828. for(i=LABEL_START;i<str_num;i++){
  1829. if(
  1830. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1831. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1832. ){
  1833. str_data[i].type=C_NOP;
  1834. str_data[i].backpatch=-1;
  1835. str_data[i].label=-1;
  1836. }
  1837. }
  1838. while( syntax.curly_count != 0 || *p != end )
  1839. {
  1840. if( *p == '\0' )
  1841. disp_error_message("unexpected end of script",p);
  1842. // labelだけ特殊処理
  1843. tmpp=skip_space(skip_word(p));
  1844. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1845. i=add_word(p);
  1846. set_label(i,script_pos,p);
  1847. if( parse_options&SCRIPT_USE_LABEL_DB )
  1848. strdb_put(scriptlabel_db, get_str(i), (void*)script_pos);
  1849. p=tmpp+1;
  1850. p=skip_space(p);
  1851. continue;
  1852. }
  1853. // 他は全部一緒くた
  1854. p=parse_line(p);
  1855. p=skip_space(p);
  1856. add_scriptc(C_EOL);
  1857. set_label(LABEL_NEXTLINE,script_pos,p);
  1858. str_data[LABEL_NEXTLINE].type=C_NOP;
  1859. str_data[LABEL_NEXTLINE].backpatch=-1;
  1860. str_data[LABEL_NEXTLINE].label=-1;
  1861. }
  1862. add_scriptc(C_NOP);
  1863. // trim code to size
  1864. script_size = script_pos;
  1865. RECREATE(script_buf,unsigned char,script_pos);
  1866. // default unknown references to variables
  1867. for(i=LABEL_START;i<str_num;i++){
  1868. if(str_data[i].type==C_NOP){
  1869. int j,next;
  1870. str_data[i].type=C_NAME;
  1871. str_data[i].label=i;
  1872. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1873. next=GETVALUE(script_buf,j);
  1874. SETVALUE(script_buf,j,i);
  1875. j=next;
  1876. }
  1877. }
  1878. }
  1879. #ifdef DEBUG_DISP
  1880. for(i=0;i<script_pos;i++){
  1881. if((i&15)==0) printf("%04x : ",i);
  1882. ShowMessage("%02x ",script_buf[i]);
  1883. if((i&15)==15) printf("\n");
  1884. }
  1885. ShowMessage("\n");
  1886. #endif
  1887. #ifdef DEBUG_DISASM
  1888. {
  1889. int i = 0,j;
  1890. while(i < script_pos) {
  1891. ShowMessage("%06x ",i);
  1892. j = i;
  1893. switch(get_com(script_buf,&i)) {
  1894. case C_EOL: printf("C_EOL"); break;
  1895. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1896. case C_POS:
  1897. ShowMessage("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1898. i += 3;
  1899. break;
  1900. case C_NAME:
  1901. j = (*(int*)(script_buf+i)&0xffffff);
  1902. ShowMessage("C_NAME %s",j == 0xffffff ? "?? unknown ??" : get_str(j));
  1903. i += 3;
  1904. break;
  1905. case C_ARG: ShowMessage("C_ARG"); break;
  1906. case C_FUNC: ShowMessage("C_FUNC"); break;
  1907. case C_ADD: ShowMessage("C_ADD"); break;
  1908. case C_SUB: ShowMessage("C_SUB"); break;
  1909. case C_MUL: ShowMessage("C_MUL"); break;
  1910. case C_DIV: ShowMessage("C_DIV"); break;
  1911. case C_MOD: ShowMessage("C_MOD"); break;
  1912. case C_EQ: ShowMessage("C_EQ"); break;
  1913. case C_NE: ShowMessage("C_NE"); break;
  1914. case C_GT: ShowMessage("C_GT"); break;
  1915. case C_GE: ShowMessage("C_GE"); break;
  1916. case C_LT: ShowMessage("C_LT"); break;
  1917. case C_LE: ShowMessage("C_LE"); break;
  1918. case C_AND: ShowMessage("C_AND"); break;
  1919. case C_OR: ShowMessage("C_OR"); break;
  1920. case C_XOR: ShowMessage("C_XOR"); break;
  1921. case C_LAND: ShowMessage("C_LAND"); break;
  1922. case C_LOR: ShowMessage("C_LOR"); break;
  1923. case C_R_SHIFT: ShowMessage("C_R_SHIFT"); break;
  1924. case C_L_SHIFT: ShowMessage("C_L_SHIFT"); break;
  1925. case C_NEG: ShowMessage("C_NEG"); break;
  1926. case C_NOT: ShowMessage("C_NOT"); break;
  1927. case C_LNOT: ShowMessage("C_LNOT"); break;
  1928. case C_NOP: ShowMessage("C_NOP"); break;
  1929. case C_OP3: ShowMessage("C_OP3"); break;
  1930. case C_STR:
  1931. j = strlen(script_buf + i);
  1932. ShowMessage("C_STR %s",script_buf + i);
  1933. i+= j+1;
  1934. break;
  1935. default:
  1936. ShowMessage("unknown");
  1937. }
  1938. ShowMessage(CL_CLL "\n");
  1939. }
  1940. }
  1941. #endif
  1942. CREATE(code,struct script_code,1);
  1943. code->script_buf = script_buf;
  1944. code->script_size = script_size;
  1945. code->script_vars = NULL;
  1946. return code;
  1947. }
  1948. /// Returns the player attached to this script, identified by the rid.
  1949. /// If there is no player attached, the script is terminated.
  1950. TBL_PC *script_rid2sd(struct script_state *st)
  1951. {
  1952. TBL_PC *sd=map_id2sd(st->rid);
  1953. if(!sd){
  1954. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1955. script_reportsrc(st);
  1956. st->state = END;
  1957. }
  1958. return sd;
  1959. }
  1960. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1961. ///
  1962. /// @param st Script state
  1963. /// @param data Variable/constant
  1964. void get_val(struct script_state* st, struct script_data* data)
  1965. {
  1966. const char* name;
  1967. char prefix;
  1968. char postfix;
  1969. TBL_PC* sd = NULL;
  1970. if( !data_isreference(data) )
  1971. return;// not a variable/constant
  1972. name = reference_getname(data);
  1973. prefix = name[0];
  1974. postfix = name[strlen(name) - 1];
  1975. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1976. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1977. {
  1978. sd = script_rid2sd(st);
  1979. if( sd == NULL )
  1980. {// needs player attached
  1981. if( postfix == '$' )
  1982. {// string variable
  1983. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1984. data->type = C_CONSTSTR;
  1985. data->u.str = "";
  1986. }
  1987. else
  1988. {// integer variable
  1989. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1990. data->type = C_INT;
  1991. data->u.num = 0;
  1992. }
  1993. return;
  1994. }
  1995. }
  1996. if( postfix == '$' )
  1997. {// string variable
  1998. switch( prefix )
  1999. {
  2000. case '@':
  2001. data->u.str = pc_readregstr(sd, data->u.num);
  2002. break;
  2003. case '$':
  2004. data->u.str = mapreg_readregstr(data->u.num);
  2005. break;
  2006. case '#':
  2007. if( name[1] == '#' )
  2008. data->u.str = pc_readaccountreg2str(sd, name);// global
  2009. else
  2010. data->u.str = pc_readaccountregstr(sd, name);// local
  2011. break;
  2012. case '.':
  2013. {
  2014. struct linkdb_node** n =
  2015. data->ref ? data->ref:
  2016. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2017. &st->script->script_vars;// npc variable
  2018. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2019. }
  2020. break;
  2021. case '\'':
  2022. {
  2023. struct linkdb_node** n = NULL;
  2024. if( st->instance_id )
  2025. n = &instance[st->instance_id].svar;
  2026. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2027. }
  2028. break;
  2029. default:
  2030. data->u.str = pc_readglobalreg_str(sd, name);
  2031. break;
  2032. }
  2033. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2034. {// empty string
  2035. data->type = C_CONSTSTR;
  2036. data->u.str = "";
  2037. }
  2038. else
  2039. {// duplicate string
  2040. data->type = C_STR;
  2041. data->u.str = aStrdup(data->u.str);
  2042. }
  2043. }
  2044. else
  2045. {// integer variable
  2046. data->type = C_INT;
  2047. if( reference_toconstant(data) )
  2048. {
  2049. data->u.num = reference_getconstant(data);
  2050. }
  2051. else if( reference_toparam(data) )
  2052. {
  2053. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2054. }
  2055. else
  2056. switch( prefix )
  2057. {
  2058. case '@':
  2059. data->u.num = pc_readreg(sd, data->u.num);
  2060. break;
  2061. case '$':
  2062. data->u.num = mapreg_readreg(data->u.num);
  2063. break;
  2064. case '#':
  2065. if( name[1] == '#' )
  2066. data->u.num = pc_readaccountreg2(sd, name);// global
  2067. else
  2068. data->u.num = pc_readaccountreg(sd, name);// local
  2069. break;
  2070. case '.':
  2071. {
  2072. struct linkdb_node** n =
  2073. data->ref ? data->ref:
  2074. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2075. &st->script->script_vars;// npc variable
  2076. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2077. }
  2078. break;
  2079. case '\'':
  2080. {
  2081. struct linkdb_node** n = NULL;
  2082. if( st->instance_id )
  2083. n = &instance[st->instance_id].ivar;
  2084. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2085. }
  2086. break;
  2087. default:
  2088. data->u.num = pc_readglobalreg(sd, name);
  2089. break;
  2090. }
  2091. }
  2092. return;
  2093. }
  2094. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2095. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2096. /// The value is left in the top of the stack and needs to be removed manually.
  2097. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2098. {
  2099. struct script_data* data;
  2100. push_val2(st->stack, C_NAME, uid, ref);
  2101. data = script_getdatatop(st, -1);
  2102. get_val(st, data);
  2103. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2104. }
  2105. /*==========================================
  2106. * Stores the value of a script variable
  2107. * Return value is 0 on fail, 1 on success.
  2108. *------------------------------------------*/
  2109. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2110. {
  2111. char prefix = name[0];
  2112. if( is_string_variable(name) )
  2113. {// string variable
  2114. const char* str = (const char*)value;
  2115. switch (prefix) {
  2116. case '@':
  2117. return pc_setregstr(sd, num, str);
  2118. case '$':
  2119. return mapreg_setregstr(num, str);
  2120. case '#':
  2121. return (name[1] == '#') ?
  2122. pc_setaccountreg2str(sd, name, str) :
  2123. pc_setaccountregstr(sd, name, str);
  2124. case '.': {
  2125. char* p;
  2126. struct linkdb_node** n;
  2127. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2128. p = (char*)linkdb_erase(n, (void*)num);
  2129. if (p) aFree(p);
  2130. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2131. }
  2132. return 1;
  2133. case '\'': {
  2134. char *p;
  2135. struct linkdb_node** n = NULL;
  2136. if( st->instance_id )
  2137. n = &instance[st->instance_id].svar;
  2138. p = (char*)linkdb_erase(n, (void*)num);
  2139. if (p) aFree(p);
  2140. if( str[0] ) linkdb_insert(n, (void*)num, aStrdup(str));
  2141. }
  2142. return 1;
  2143. default:
  2144. return pc_setglobalreg_str(sd, name, str);
  2145. }
  2146. }
  2147. else
  2148. {// integer variable
  2149. int val = (int)value;
  2150. if(str_data[num&0x00ffffff].type == C_PARAM)
  2151. {
  2152. if( pc_setparam(sd, str_data[num&0x00ffffff].val, val) == 0 )
  2153. {
  2154. if( st != NULL )
  2155. {
  2156. ShowError("script:set_reg: failed to set param '%s' to %d.\n", name, val);
  2157. script_reportsrc(st);
  2158. st->state = END;
  2159. }
  2160. return 0;
  2161. }
  2162. return 1;
  2163. }
  2164. switch (prefix) {
  2165. case '@':
  2166. return pc_setreg(sd, num, val);
  2167. case '$':
  2168. return mapreg_setreg(num, val);
  2169. case '#':
  2170. return (name[1] == '#') ?
  2171. pc_setaccountreg2(sd, name, val) :
  2172. pc_setaccountreg(sd, name, val);
  2173. case '.': {
  2174. struct linkdb_node** n;
  2175. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2176. if (val == 0)
  2177. linkdb_erase(n, (void*)num);
  2178. else
  2179. linkdb_replace(n, (void*)num, (void*)val);
  2180. }
  2181. return 1;
  2182. case '\'':
  2183. {
  2184. struct linkdb_node** n = NULL;
  2185. if( st->instance_id )
  2186. n = &instance[st->instance_id].ivar;
  2187. if( val == 0 )
  2188. linkdb_erase(n, (void*)num);
  2189. else
  2190. linkdb_replace(n, (void*)num, (void*)val);
  2191. return 1;
  2192. }
  2193. default:
  2194. return pc_setglobalreg(sd, name, val);
  2195. }
  2196. }
  2197. }
  2198. int set_var(TBL_PC* sd, char* name, void* val)
  2199. {
  2200. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2201. }
  2202. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2203. {
  2204. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2205. }
  2206. /// Converts the data to a string
  2207. const char* conv_str(struct script_state* st, struct script_data* data)
  2208. {
  2209. char* p;
  2210. get_val(st, data);
  2211. if( data_isstring(data) )
  2212. {// nothing to convert
  2213. }
  2214. else if( data_isint(data) )
  2215. {// int -> string
  2216. CREATE(p, char, ITEM_NAME_LENGTH);
  2217. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2218. p[ITEM_NAME_LENGTH-1] = '\0';
  2219. data->type = C_STR;
  2220. data->u.str = p;
  2221. }
  2222. else if( data_isreference(data) )
  2223. {// reference -> string
  2224. //##TODO when does this happen (check get_val) [FlavioJS]
  2225. data->type = C_CONSTSTR;
  2226. data->u.str = reference_getname(data);
  2227. }
  2228. else
  2229. {// unsupported data type
  2230. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2231. script_reportdata(data);
  2232. script_reportsrc(st);
  2233. data->type = C_CONSTSTR;
  2234. data->u.str = "";
  2235. }
  2236. return data->u.str;
  2237. }
  2238. /// Converts the data to an int
  2239. int conv_num(struct script_state* st, struct script_data* data)
  2240. {
  2241. char* p;
  2242. long num;
  2243. get_val(st, data);
  2244. if( data_isint(data) )
  2245. {// nothing to convert
  2246. }
  2247. else if( data_isstring(data) )
  2248. {// string -> int
  2249. // the result does not overflow or underflow, it is capped instead
  2250. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2251. p = data->u.str;
  2252. errno = 0;
  2253. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2254. if( errno == ERANGE
  2255. #if LONG_MAX > INT_MAX
  2256. || num < INT_MIN || num > INT_MAX
  2257. #endif
  2258. )
  2259. {
  2260. if( num <= INT_MIN )
  2261. {
  2262. num = INT_MIN;
  2263. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2264. }
  2265. else//if( num >= INT_MAX )
  2266. {
  2267. num = INT_MAX;
  2268. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2269. }
  2270. script_reportdata(data);
  2271. script_reportsrc(st);
  2272. }
  2273. if( data->type == C_STR )
  2274. aFree(p);
  2275. data->type = C_INT;
  2276. data->u.num = (int)num;
  2277. }
  2278. #if 0
  2279. // FIXME this function is being used to retrieve the position of labels and
  2280. // probably other stuff [FlavioJS]
  2281. else
  2282. {// unsupported data type
  2283. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2284. script_reportdata(data);
  2285. script_reportsrc(st);
  2286. data->type = C_INT;
  2287. data->u.num = 0;
  2288. }
  2289. #endif
  2290. return data->u.num;
  2291. }
  2292. //
  2293. // Stack operations
  2294. //
  2295. /// Increases the size of the stack
  2296. void stack_expand(struct script_stack* stack)
  2297. {
  2298. stack->sp_max += 64;
  2299. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2300. stack->sp_max * sizeof(stack->stack_data[0]) );
  2301. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2302. 64 * sizeof(stack->stack_data[0]) );
  2303. }
  2304. /// Pushes a value into the stack
  2305. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2306. /// Pushes a value into the stack (with reference)
  2307. struct script_data* push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2308. {
  2309. if( stack->sp >= stack->sp_max )
  2310. stack_expand(stack);
  2311. stack->stack_data[stack->sp].type = type;
  2312. stack->stack_data[stack->sp].u.num = val;
  2313. stack->stack_data[stack->sp].ref = ref;
  2314. stack->sp++;
  2315. return &stack->stack_data[stack->sp-1];
  2316. }
  2317. /// Pushes a string into the stack
  2318. struct script_data* push_str(struct script_stack* stack, enum c_op type, char* str)
  2319. {
  2320. if( stack->sp >= stack->sp_max )
  2321. stack_expand(stack);
  2322. stack->stack_data[stack->sp].type = type;
  2323. stack->stack_data[stack->sp].u.str = str;
  2324. stack->stack_data[stack->sp].ref = NULL;
  2325. stack->sp++;
  2326. return &stack->stack_data[stack->sp-1];
  2327. }
  2328. /// Pushes a retinfo into the stack
  2329. struct script_data* push_retinfo(struct script_stack* stack, struct script_retinfo* ri)
  2330. {
  2331. if( stack->sp >= stack->sp_max )
  2332. stack_expand(stack);
  2333. stack->stack_data[stack->sp].type = C_RETINFO;
  2334. stack->stack_data[stack->sp].u.ri = ri;
  2335. stack->stack_data[stack->sp].ref = NULL;
  2336. stack->sp++;
  2337. return &stack->stack_data[stack->sp-1];
  2338. }
  2339. /// Pushes a copy of the target position into the stack
  2340. struct script_data* push_copy(struct script_stack* stack, int pos)
  2341. {
  2342. switch( stack->stack_data[pos].type )
  2343. {
  2344. case C_CONSTSTR:
  2345. return push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2346. break;
  2347. case C_STR:
  2348. return push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2349. break;
  2350. case C_RETINFO:
  2351. ShowFatalError("script:push_copy: can't create copies of C_RETINFO. Exiting...\n");
  2352. exit(1);
  2353. break;
  2354. default:
  2355. return push_val2(
  2356. stack,stack->stack_data[pos].type,
  2357. stack->stack_data[pos].u.num,
  2358. stack->stack_data[pos].ref
  2359. );
  2360. break;
  2361. }
  2362. }
  2363. /// Removes the values in indexes [start,end[ from the stack.
  2364. /// Adjusts all stack pointers.
  2365. void pop_stack(struct script_state* st, int start, int end)
  2366. {
  2367. struct script_stack* stack = st->stack;
  2368. struct script_data* data;
  2369. int i;
  2370. if( start < 0 )
  2371. start = 0;
  2372. if( end > stack->sp )
  2373. end = stack->sp;
  2374. if( start >= end )
  2375. return;// nothing to pop
  2376. // free stack elements
  2377. for( i = start; i < end; i++ )
  2378. {
  2379. data = &stack->stack_data[i];
  2380. if( data->type == C_STR )
  2381. aFree(data->u.str);
  2382. if( data->type == C_RETINFO )
  2383. {
  2384. struct script_retinfo* ri = data->u.ri;
  2385. if( ri->var_function )
  2386. {
  2387. script_free_vars(ri->var_function);
  2388. aFree(ri->var_function);
  2389. }
  2390. aFree(ri);
  2391. }
  2392. data->type = C_NOP;
  2393. }
  2394. // move the rest of the elements
  2395. if( stack->sp > end )
  2396. {
  2397. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2398. for( i = start + stack->sp - end; i < stack->sp; ++i )
  2399. stack->stack_data[i].type = C_NOP;
  2400. }
  2401. // adjust stack pointers
  2402. if( st->start > end ) st->start -= end - start;
  2403. else if( st->start > start ) st->start = start;
  2404. if( st->end > end ) st->end -= end - start;
  2405. else if( st->end > start ) st->end = start;
  2406. if( stack->defsp > end ) stack->defsp -= end - start;
  2407. else if( stack->defsp > start ) stack->defsp = start;
  2408. stack->sp -= end - start;
  2409. }
  2410. ///
  2411. ///
  2412. ///
  2413. /*==========================================
  2414. * スクリプト依存変数、関数依存変数の解放
  2415. *------------------------------------------*/
  2416. void script_free_vars(struct linkdb_node **node)
  2417. {
  2418. struct linkdb_node* n = *node;
  2419. while( n != NULL)
  2420. {
  2421. const char* name = get_str((int)(n->key)&0x00ffffff);
  2422. if( is_string_variable(name) )
  2423. aFree(n->data); // 文字型変数なので、データ削除
  2424. n = n->next;
  2425. }
  2426. linkdb_final( node );
  2427. }
  2428. void script_free_code(struct script_code* code)
  2429. {
  2430. script_free_vars( &code->script_vars );
  2431. aFree( code->script_buf );
  2432. aFree( code );
  2433. }
  2434. /// Creates a new script state.
  2435. ///
  2436. /// @param script Script code
  2437. /// @param pos Position in the code
  2438. /// @param rid Who is running the script (attached player)
  2439. /// @param oid Where the code is being run (npc 'object')
  2440. /// @return Script state
  2441. struct script_state* script_alloc_state(struct script_code* script, int pos, int rid, int oid)
  2442. {
  2443. struct script_state* st;
  2444. CREATE(st, struct script_state, 1);
  2445. st->stack = (struct script_stack*)aMalloc(sizeof(struct script_stack));
  2446. st->stack->sp = 0;
  2447. st->stack->sp_max = 64;
  2448. CREATE(st->stack->stack_data, struct script_data, st->stack->sp_max);
  2449. st->stack->defsp = st->stack->sp;
  2450. CREATE(st->stack->var_function, struct linkdb_node*, 1);
  2451. st->state = RUN;
  2452. st->script = script;
  2453. //st->scriptroot = script;
  2454. st->pos = pos;
  2455. st->rid = rid;
  2456. st->oid = oid;
  2457. st->sleep.timer = INVALID_TIMER;
  2458. return st;
  2459. }
  2460. /// Frees a script state.
  2461. ///
  2462. /// @param st Script state
  2463. void script_free_state(struct script_state* st)
  2464. {
  2465. if( st->sleep.timer != INVALID_TIMER )
  2466. delete_timer(st->sleep.timer, run_script_timer);
  2467. script_free_vars(st->stack->var_function);
  2468. aFree(st->stack->var_function);
  2469. pop_stack(st, 0, st->stack->sp);
  2470. aFree(st->stack->stack_data);
  2471. aFree(st->stack);
  2472. st->pos = -1;
  2473. aFree(st);
  2474. }
  2475. //
  2476. // 実行部main
  2477. //
  2478. /*==========================================
  2479. * コマンドの読み取り
  2480. *------------------------------------------*/
  2481. c_op get_com(unsigned char *script,int *pos)
  2482. {
  2483. int i = 0, j = 0;
  2484. if(script[*pos]>=0x80){
  2485. return C_INT;
  2486. }
  2487. while(script[*pos]>=0x40){
  2488. i=script[(*pos)++]<<j;
  2489. j+=6;
  2490. }
  2491. return (c_op)(i+(script[(*pos)++]<<j));
  2492. }
  2493. /*==========================================
  2494. * 数値の所得
  2495. *------------------------------------------*/
  2496. int get_num(unsigned char *script,int *pos)
  2497. {
  2498. int i,j;
  2499. i=0; j=0;
  2500. while(script[*pos]>=0xc0){
  2501. i+=(script[(*pos)++]&0x7f)<<j;
  2502. j+=6;
  2503. }
  2504. return i+((script[(*pos)++]&0x7f)<<j);
  2505. }
  2506. /*==========================================
  2507. * スタックから値を取り出す
  2508. *------------------------------------------*/
  2509. int pop_val(struct script_state* st)
  2510. {
  2511. if(st->stack->sp<=0)
  2512. return 0;
  2513. st->stack->sp--;
  2514. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2515. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2516. return st->stack->stack_data[st->stack->sp].u.num;
  2517. return 0;
  2518. }
  2519. /// Ternary operators
  2520. /// test ? if_true : if_false
  2521. void op_3(struct script_state* st, int op)
  2522. {
  2523. struct script_data* data;
  2524. int flag = 0;
  2525. data = script_getdatatop(st, -3);
  2526. get_val(st, data);
  2527. if( data_isstring(data) )
  2528. flag = data->u.str[0];// "" -> false
  2529. else if( data_isint(data) )
  2530. flag = data->u.num;// 0 -> false
  2531. else
  2532. {
  2533. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2534. script_reportdata(data);
  2535. script_reportsrc(st);
  2536. script_removetop(st, -3, 0);
  2537. script_pushnil(st);
  2538. return;
  2539. }
  2540. if( flag )
  2541. script_pushcopytop(st, -2);
  2542. else
  2543. script_pushcopytop(st, -1);
  2544. script_removetop(st, -4, -1);
  2545. }
  2546. /// Binary string operators
  2547. /// s1 EQ s2 -> i
  2548. /// s1 NE s2 -> i
  2549. /// s1 GT s2 -> i
  2550. /// s1 GE s2 -> i
  2551. /// s1 LT s2 -> i
  2552. /// s1 LE s2 -> i
  2553. /// s1 ADD s2 -> s
  2554. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2555. {
  2556. int a = 0;
  2557. switch(op){
  2558. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2559. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2560. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2561. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2562. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2563. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2564. case C_ADD:
  2565. {
  2566. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2567. strcpy(buf, s1);
  2568. strcat(buf, s2);
  2569. script_pushstr(st, buf);
  2570. return;
  2571. }
  2572. default:
  2573. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2574. script_reportsrc(st);
  2575. script_pushnil(st);
  2576. st->state = END;
  2577. return;
  2578. }
  2579. script_pushint(st,a);
  2580. }
  2581. /// Binary number operators
  2582. /// i OP i -> i
  2583. void op_2num(struct script_state* st, int op, int i1, int i2)
  2584. {
  2585. int ret;
  2586. double ret_double;
  2587. switch( op )
  2588. {
  2589. case C_AND: ret = i1 & i2; break;
  2590. case C_OR: ret = i1 | i2; break;
  2591. case C_XOR: ret = i1 ^ i2; break;
  2592. case C_LAND: ret = (i1 && i2); break;
  2593. case C_LOR: ret = (i1 || i2); break;
  2594. case C_EQ: ret = (i1 == i2); break;
  2595. case C_NE: ret = (i1 != i2); break;
  2596. case C_GT: ret = (i1 > i2); break;
  2597. case C_GE: ret = (i1 >= i2); break;
  2598. case C_LT: ret = (i1 < i2); break;
  2599. case C_LE: ret = (i1 <= i2); break;
  2600. case C_R_SHIFT: ret = i1>>i2; break;
  2601. case C_L_SHIFT: ret = i1<<i2; break;
  2602. case C_DIV:
  2603. case C_MOD:
  2604. if( i2 == 0 )
  2605. {
  2606. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2607. script_reportsrc(st);
  2608. script_pushnil(st);
  2609. st->state = END;
  2610. return;
  2611. }
  2612. else if( op == C_DIV )
  2613. ret = i1 / i2;
  2614. else//if( op == C_MOD )
  2615. ret = i1 % i2;
  2616. break;
  2617. default:
  2618. switch( op )
  2619. {// operators that can overflow/underflow
  2620. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2621. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2622. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2623. default:
  2624. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2625. script_reportsrc(st);
  2626. script_pushnil(st);
  2627. return;
  2628. }
  2629. if( ret_double < (double)INT_MIN )
  2630. {
  2631. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2632. script_reportsrc(st);
  2633. ret = INT_MIN;
  2634. }
  2635. else if( ret_double > (double)INT_MAX )
  2636. {
  2637. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2638. script_reportsrc(st);
  2639. ret = INT_MAX;
  2640. }
  2641. }
  2642. script_pushint(st, ret);
  2643. }
  2644. /// Binary operators
  2645. void op_2(struct script_state *st, int op)
  2646. {
  2647. struct script_data* left;
  2648. struct script_data* right;
  2649. left = script_getdatatop(st, -2);
  2650. right = script_getdatatop(st, -1);
  2651. get_val(st, left);
  2652. get_val(st, right);
  2653. // automatic conversions
  2654. switch( op )
  2655. {
  2656. case C_ADD:
  2657. if( data_isint(left) && data_isstring(right) )
  2658. {// convert int-string to string-string
  2659. conv_str(st, left);
  2660. }
  2661. else if( data_isstring(left) && data_isint(right) )
  2662. {// convert string-int to string-string
  2663. conv_str(st, right);
  2664. }
  2665. break;
  2666. }
  2667. if( data_isstring(left) && data_isstring(right) )
  2668. {// ss => op_2str
  2669. op_2str(st, op, left->u.str, right->u.str);
  2670. script_removetop(st, -3, -1);// pop the two values before the top one
  2671. }
  2672. else if( data_isint(left) && data_isint(right) )
  2673. {// ii => op_2num
  2674. int i1 = left->u.num;
  2675. int i2 = right->u.num;
  2676. script_removetop(st, -2, 0);
  2677. op_2num(st, op, i1, i2);
  2678. }
  2679. else
  2680. {// invalid argument
  2681. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2682. script_reportdata(left);
  2683. script_reportdata(right);
  2684. script_reportsrc(st);
  2685. script_removetop(st, -2, 0);
  2686. script_pushnil(st);
  2687. st->state = END;
  2688. }
  2689. }
  2690. /// Unary operators
  2691. /// NEG i -> i
  2692. /// NOT i -> i
  2693. /// LNOT i -> i
  2694. void op_1(struct script_state* st, int op)
  2695. {
  2696. struct script_data* data;
  2697. int i1;
  2698. data = script_getdatatop(st, -1);
  2699. get_val(st, data);
  2700. if( !data_isint(data) )
  2701. {// not a number
  2702. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2703. script_reportdata(data);
  2704. script_reportsrc(st);
  2705. script_pushnil(st);
  2706. st->state = END;
  2707. return;
  2708. }
  2709. i1 = data->u.num;
  2710. script_removetop(st, -1, 0);
  2711. switch( op )
  2712. {
  2713. case C_NEG: i1 = -i1; break;
  2714. case C_NOT: i1 = ~i1; break;
  2715. case C_LNOT: i1 = !i1; break;
  2716. default:
  2717. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2718. script_reportsrc(st);
  2719. script_pushnil(st);
  2720. st->state = END;
  2721. return;
  2722. }
  2723. script_pushint(st, i1);
  2724. }
  2725. /// Executes a buildin command.
  2726. /// Stack: C_NAME(<command>) C_ARG <arg0> <arg1> ... <argN>
  2727. int run_func(struct script_state *st)
  2728. {
  2729. struct script_data* data;
  2730. int i,start_sp,end_sp,func;
  2731. end_sp = st->stack->sp;// position after the last argument
  2732. for( i = end_sp-1; i > 0 ; --i )
  2733. if( st->stack->stack_data[i].type == C_ARG )
  2734. break;
  2735. if( i == 0 )
  2736. {
  2737. ShowError("script:run_func: C_ARG not found. please report this!!!\n");
  2738. st->state = END;
  2739. script_reportsrc(st);
  2740. return 1;
  2741. }
  2742. start_sp = i-1;// C_NAME of the command
  2743. st->start = start_sp;
  2744. st->end = end_sp;
  2745. data = &st->stack->stack_data[st->start];
  2746. if( data->type == C_NAME && str_data[data->u.num].type == C_FUNC )
  2747. func = data->u.num;
  2748. else
  2749. {
  2750. ShowError("script:run_func: not a buildin command.\n");
  2751. script_reportdata(data);
  2752. script_reportsrc(st);
  2753. st->state = END;
  2754. return 1;
  2755. }
  2756. if(str_data[func].func){
  2757. if (str_data[func].func(st)) //Report error
  2758. script_reportsrc(st);
  2759. } else {
  2760. 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));
  2761. script_reportsrc(st);
  2762. st->state = END;
  2763. }
  2764. // Stack's datum are used when re-running functions [Eoe]
  2765. if( st->state == RERUNLINE )
  2766. return 0;
  2767. pop_stack(st, st->start, st->end);
  2768. if( st->state == RETFUNC )
  2769. {// return from a user-defined function
  2770. struct script_retinfo* ri;
  2771. int olddefsp = st->stack->defsp;
  2772. int nargs;
  2773. pop_stack(st, st->stack->defsp, st->start);// pop distractions from the stack
  2774. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp-1].type != C_RETINFO )
  2775. {
  2776. ShowWarning("script:run_func: return without callfunc or callsub!\n");
  2777. script_reportsrc(st);
  2778. st->state = END;
  2779. return 1;
  2780. }
  2781. script_free_vars( st->stack->var_function );
  2782. aFree(st->stack->var_function);
  2783. ri = st->stack->stack_data[st->stack->defsp-1].u.ri;
  2784. nargs = ri->nargs;
  2785. st->pos = ri->pos;
  2786. st->script = ri->script;
  2787. st->stack->var_function = ri->var_function;
  2788. st->stack->defsp = ri->defsp;
  2789. memset(ri, 0, sizeof(struct script_retinfo));
  2790. pop_stack(st, olddefsp-nargs-1, olddefsp);// pop arguments and retinfo
  2791. st->state = GOTO;
  2792. }
  2793. return 0;
  2794. }
  2795. /*==========================================
  2796. * script execution
  2797. *------------------------------------------*/
  2798. void run_script_main(struct script_state *st);
  2799. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2800. {
  2801. struct script_state *st;
  2802. if( rootscript == NULL || pos < 0 )
  2803. return;
  2804. // TODO In jAthena, this function can take over the pending script in the player. [FlavioJS]
  2805. // It is unclear how that can be triggered, so it needs the be traced/checked in more detail.
  2806. // NOTE At the time of this change, this function wasn't capable of taking over the script state because st->scriptroot was never set.
  2807. st = script_alloc_state(rootscript, pos, rid, oid);
  2808. run_script_main(st);
  2809. }
  2810. void script_stop_sleeptimers(int id)
  2811. {
  2812. struct script_state* st;
  2813. for(;;)
  2814. {
  2815. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2816. if( st == NULL )
  2817. break; // no more sleep timers
  2818. script_free_state(st);
  2819. }
  2820. }
  2821. /*==========================================
  2822. * 指定ノードをsleep_dbから削除
  2823. *------------------------------------------*/
  2824. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2825. {
  2826. struct linkdb_node *retnode;
  2827. if( n == NULL)
  2828. return NULL;
  2829. if( n->prev == NULL )
  2830. sleep_db = n->next;
  2831. else
  2832. n->prev->next = n->next;
  2833. if( n->next )
  2834. n->next->prev = n->prev;
  2835. retnode = n->next;
  2836. aFree( n );
  2837. return retnode; // 次のノードを返す
  2838. }
  2839. /*==========================================
  2840. * sleep用タイマー関数
  2841. *------------------------------------------*/
  2842. int run_script_timer(int tid, unsigned int tick, int id, intptr data)
  2843. {
  2844. struct script_state *st = (struct script_state *)data;
  2845. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2846. TBL_PC *sd = map_id2sd(st->rid);
  2847. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2848. { //Character mismatch. Cancel execution.
  2849. st->rid = 0;
  2850. st->state = END;
  2851. }
  2852. while( node && st->sleep.timer != -1 ) {
  2853. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2854. script_erase_sleepdb(node);
  2855. st->sleep.timer = INVALID_TIMER;
  2856. break;
  2857. }
  2858. node = node->next;
  2859. }
  2860. if(st->state != RERUNLINE)
  2861. st->sleep.tick = 0;
  2862. run_script_main(st);
  2863. return 0;
  2864. }
  2865. /*==========================================
  2866. * スクリプトの実行メイン部分
  2867. *------------------------------------------*/
  2868. void run_script_main(struct script_state *st)
  2869. {
  2870. int cmdcount=script_config.check_cmdcount;
  2871. int gotocount=script_config.check_gotocount;
  2872. TBL_PC *sd;
  2873. //For backing up purposes
  2874. struct script_state *bk_st = NULL;
  2875. int bk_npcid = 0;
  2876. struct script_stack *stack=st->stack;
  2877. struct npc_data *nd;
  2878. sd = map_id2sd(st->rid);
  2879. if(sd){
  2880. if(sd->st != st){
  2881. bk_st = sd->st;
  2882. bk_npcid = sd->npc_id;
  2883. }
  2884. sd->st = st;
  2885. sd->npc_id = st->oid;
  2886. }
  2887. nd = map_id2nd(st->oid);
  2888. if( nd && map[nd->bl.m].instance_id > 0 )
  2889. st->instance_id = map[nd->bl.m].instance_id;
  2890. if(st->state == RERUNLINE) {
  2891. run_func(st);
  2892. if(st->state == GOTO)
  2893. st->state = RUN;
  2894. } else if(st->state != END)
  2895. st->state = RUN;
  2896. while(st->state == RUN)
  2897. {
  2898. enum c_op c = get_com(st->script->script_buf,&st->pos);
  2899. switch(c){
  2900. case C_EOL:
  2901. if( stack->defsp > stack->sp )
  2902. ShowError("script:run_script_main: unexpected stack position (defsp=%d sp=%d). please report this!!!\n", stack->defsp, stack->sp);
  2903. else
  2904. pop_stack(st, stack->defsp, stack->sp);// pop unused stack data. (unused return value)
  2905. break;
  2906. case C_INT:
  2907. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2908. break;
  2909. case C_POS:
  2910. case C_NAME:
  2911. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2912. st->pos+=3;
  2913. break;
  2914. case C_ARG:
  2915. push_val(stack,c,0);
  2916. break;
  2917. case C_STR:
  2918. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2919. while(st->script->script_buf[st->pos++]);
  2920. break;
  2921. case C_FUNC:
  2922. run_func(st);
  2923. if(st->state==GOTO){
  2924. st->state = RUN;
  2925. if( gotocount>0 && (--gotocount)<=0 ){
  2926. ShowError("run_script: infinity loop !\n");
  2927. script_reportsrc(st);
  2928. st->state=END;
  2929. }
  2930. }
  2931. break;
  2932. case C_NEG:
  2933. case C_NOT:
  2934. case C_LNOT:
  2935. op_1(st ,c);
  2936. break;
  2937. case C_ADD:
  2938. case C_SUB:
  2939. case C_MUL:
  2940. case C_DIV:
  2941. case C_MOD:
  2942. case C_EQ:
  2943. case C_NE:
  2944. case C_GT:
  2945. case C_GE:
  2946. case C_LT:
  2947. case C_LE:
  2948. case C_AND:
  2949. case C_OR:
  2950. case C_XOR:
  2951. case C_LAND:
  2952. case C_LOR:
  2953. case C_R_SHIFT:
  2954. case C_L_SHIFT:
  2955. op_2(st, c);
  2956. break;
  2957. case C_OP3:
  2958. op_3(st, c);
  2959. break;
  2960. case C_NOP:
  2961. st->state=END;
  2962. break;
  2963. default:
  2964. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2965. st->state=END;
  2966. break;
  2967. }
  2968. if( cmdcount>0 && (--cmdcount)<=0 ){
  2969. ShowError("run_script: infinity loop !\n");
  2970. script_reportsrc(st);
  2971. st->state=END;
  2972. }
  2973. }
  2974. if(st->sleep.tick > 0) {
  2975. //Restore previous script
  2976. if (sd) {
  2977. sd->st = bk_st;
  2978. sd->npc_id = bk_npcid;
  2979. bk_st = NULL; //Remove tag for removal.
  2980. }
  2981. //Delay execution
  2982. sd = map_id2sd(st->rid); // Refresh sd since script might have attached someone while running. [Inkfish]
  2983. st->sleep.charid = sd?sd->status.char_id:0;
  2984. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2985. run_script_timer, st->sleep.charid, (intptr)st);
  2986. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2987. }
  2988. else if(st->state != END && sd){
  2989. //Resume later (st is already attached to player).
  2990. if(bk_st) {
  2991. ShowWarning("Unable to restore stack! Double continuation!\n");
  2992. //Report BOTH scripts to see if that can help somehow.
  2993. ShowDebug("Previous script (lost):\n");
  2994. script_reportsrc(bk_st);
  2995. ShowDebug("Current script:\n");
  2996. script_reportsrc(st);
  2997. }
  2998. } else {
  2999. //Dispose of script.
  3000. if (sd)
  3001. { //Restore previous stack and save char.
  3002. if(sd->state.using_fake_npc){
  3003. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  3004. sd->state.using_fake_npc = 0;
  3005. }
  3006. //Restore previous script if any.
  3007. sd->st = bk_st;
  3008. sd->npc_id = bk_npcid;
  3009. if (!bk_st)
  3010. npc_event_dequeue(sd);
  3011. else
  3012. bk_st = NULL; //Remove tag for removal.
  3013. if (sd->state.reg_dirty&2)
  3014. intif_saveregistry(sd,2);
  3015. if (sd->state.reg_dirty&1)
  3016. intif_saveregistry(sd,1);
  3017. }
  3018. script_free_state(st);
  3019. st = NULL;
  3020. }
  3021. if (bk_st)
  3022. { //Remove previous script
  3023. script_free_state(bk_st);
  3024. bk_st = NULL;
  3025. }
  3026. }
  3027. int script_config_read(char *cfgName)
  3028. {
  3029. int i;
  3030. char line[1024],w1[1024],w2[1024];
  3031. FILE *fp;
  3032. fp=fopen(cfgName,"r");
  3033. if(fp==NULL){
  3034. ShowError("file not found: [%s]\n", cfgName);
  3035. return 1;
  3036. }
  3037. while(fgets(line, sizeof(line), fp))
  3038. {
  3039. if(line[0] == '/' && line[1] == '/')
  3040. continue;
  3041. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3042. if(i!=2)
  3043. continue;
  3044. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3045. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3046. }
  3047. else if(strcmpi(w1,"check_cmdcount")==0) {
  3048. script_config.check_cmdcount = config_switch(w2);
  3049. }
  3050. else if(strcmpi(w1,"check_gotocount")==0) {
  3051. script_config.check_gotocount = config_switch(w2);
  3052. }
  3053. else if(strcmpi(w1,"input_min_value")==0) {
  3054. script_config.input_min_value = config_switch(w2);
  3055. }
  3056. else if(strcmpi(w1,"input_max_value")==0) {
  3057. script_config.input_max_value = config_switch(w2);
  3058. }
  3059. else if(strcmpi(w1, "OnTouch_Label") == 0) {
  3060. safestrncpy(script_config.ontouch_name, w2, NAME_LENGTH);
  3061. }
  3062. else if(strcmpi(w1, "OnTouch2_Label") == 0) {
  3063. safestrncpy(script_config.ontouch2_name, w2, NAME_LENGTH);
  3064. }
  3065. else if(strcmpi(w1,"import")==0){
  3066. script_config_read(w2);
  3067. }
  3068. }
  3069. fclose(fp);
  3070. return 0;
  3071. }
  3072. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3073. {
  3074. struct script_code *code = (struct script_code *)data;
  3075. if(code){
  3076. script_free_vars( &code->script_vars );
  3077. aFree( code->script_buf );
  3078. aFree( code );
  3079. }
  3080. return 0;
  3081. }
  3082. /*==========================================
  3083. * 終了
  3084. *------------------------------------------*/
  3085. int do_final_script()
  3086. {
  3087. #ifdef DEBUG_HASH
  3088. if (battle_config.etc_log)
  3089. {
  3090. FILE *fp = fopen("hash_dump.txt","wt");
  3091. if(fp) {
  3092. int i,count[SCRIPT_HASH_SIZE];
  3093. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3094. int n=0;
  3095. int min=INT_MAX,max=0,zero=0;
  3096. double mean=0.0f;
  3097. double median=0.0f;
  3098. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3099. memset(count, 0, sizeof(count));
  3100. fprintf(fp,"num : hash : data_name\n");
  3101. fprintf(fp,"---------------------------------------------------------------\n");
  3102. for(i=LABEL_START; i<str_num; i++) {
  3103. unsigned int h = calc_hash(get_str(i));
  3104. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  3105. ++count[h];
  3106. }
  3107. fprintf(fp,"--------------------\n\n");
  3108. memset(count2, 0, sizeof(count2));
  3109. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3110. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3111. if(min > count[i])
  3112. min = count[i]; // minimun count of collision
  3113. if(max < count[i])
  3114. max = count[i]; // maximun count of collision
  3115. if(count[i] == 0)
  3116. zero++;
  3117. ++count2[count[i]];
  3118. }
  3119. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3120. for( i=min; i <= max; ++i ){
  3121. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3122. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3123. }
  3124. for( i=min; i <= max; ++i ){
  3125. n += count2[i];
  3126. if( n*2 >= SCRIPT_HASH_SIZE )
  3127. {
  3128. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3129. median = (i+i+1)/2.0f;
  3130. else
  3131. median = i;
  3132. break;
  3133. }
  3134. }
  3135. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3136. fclose(fp);
  3137. }
  3138. }
  3139. #endif
  3140. mapreg_final();
  3141. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3142. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3143. if(sleep_db) {
  3144. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3145. while(n) {
  3146. struct script_state *st = (struct script_state *)n->data;
  3147. script_free_state(st);
  3148. n = n->next;
  3149. }
  3150. linkdb_final(&sleep_db);
  3151. }
  3152. if (str_data)
  3153. aFree(str_data);
  3154. if (str_buf)
  3155. aFree(str_buf);
  3156. return 0;
  3157. }
  3158. /*==========================================
  3159. * 初期化
  3160. *------------------------------------------*/
  3161. int do_init_script()
  3162. {
  3163. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3164. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3165. mapreg_init();
  3166. return 0;
  3167. }
  3168. int script_reload()
  3169. {
  3170. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3171. scriptlabel_db->clear(scriptlabel_db, NULL);
  3172. if(sleep_db) {
  3173. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3174. while(n) {
  3175. struct script_state *st = (struct script_state *)n->data;
  3176. script_free_state(st);
  3177. n = n->next;
  3178. }
  3179. linkdb_final(&sleep_db);
  3180. }
  3181. mapreg_reload();
  3182. return 0;
  3183. }
  3184. //-----------------------------------------------------------------------------
  3185. // buildin functions
  3186. //
  3187. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3188. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3189. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3190. /////////////////////////////////////////////////////////////////////
  3191. // NPC interaction
  3192. //
  3193. /// Appends a message to the npc dialog.
  3194. /// If a dialog doesn't exist yet, one is created.
  3195. ///
  3196. /// mes "<message>";
  3197. BUILDIN_FUNC(mes)
  3198. {
  3199. TBL_PC* sd = script_rid2sd(st);
  3200. if( sd == NULL )
  3201. return 0;
  3202. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3203. return 0;
  3204. }
  3205. /// Displays the button 'next' in the npc dialog.
  3206. /// The dialog text is cleared and the script continues when the button is pressed.
  3207. ///
  3208. /// next;
  3209. BUILDIN_FUNC(next)
  3210. {
  3211. TBL_PC* sd;
  3212. sd = script_rid2sd(st);
  3213. if( sd == NULL )
  3214. return 0;
  3215. st->state = STOP;
  3216. clif_scriptnext(sd, st->oid);
  3217. return 0;
  3218. }
  3219. /// Ends the script and displays the button 'close' on the npc dialog.
  3220. /// The dialog is closed when the button is pressed.
  3221. ///
  3222. /// close;
  3223. BUILDIN_FUNC(close)
  3224. {
  3225. TBL_PC* sd;
  3226. sd = script_rid2sd(st);
  3227. if( sd == NULL )
  3228. return 0;
  3229. st->state = END;
  3230. clif_scriptclose(sd, st->oid);
  3231. return 0;
  3232. }
  3233. /// Displays the button 'close' on the npc dialog.
  3234. /// The dialog is closed and the script continues when the button is pressed.
  3235. ///
  3236. /// close2;
  3237. BUILDIN_FUNC(close2)
  3238. {
  3239. TBL_PC* sd;
  3240. sd = script_rid2sd(st);
  3241. if( sd == NULL )
  3242. return 0;
  3243. st->state = STOP;
  3244. clif_scriptclose(sd, st->oid);
  3245. return 0;
  3246. }
  3247. /// Counts the number of valid and total number of options in 'str'
  3248. /// If max_count > 0 the counting stops when that valid option is reached
  3249. /// total is incremented for each option (NULL is supported)
  3250. static int menu_countoptions(const char* str, int max_count, int* total)
  3251. {
  3252. int count = 0;
  3253. int bogus_total;
  3254. if( total == NULL )
  3255. total = &bogus_total;
  3256. ++(*total);
  3257. // initial empty options
  3258. while( *str == ':' )
  3259. {
  3260. ++str;
  3261. ++(*total);
  3262. }
  3263. // count menu options
  3264. while( *str != '\0' )
  3265. {
  3266. ++count;
  3267. --max_count;
  3268. if( max_count == 0 )
  3269. break;
  3270. while( *str != ':' && *str != '\0' )
  3271. ++str;
  3272. while( *str == ':' )
  3273. {
  3274. ++str;
  3275. ++(*total);
  3276. }
  3277. }
  3278. return count;
  3279. }
  3280. /// Displays a menu with options and goes to the target label.
  3281. /// The script is stopped if cancel is pressed.
  3282. /// Options with no text are not displayed in the client.
  3283. ///
  3284. /// Options can be grouped together, separated by the character ':' in the text:
  3285. /// ex: menu "A:B:C",L_target;
  3286. /// All these options go to the specified target label.
  3287. ///
  3288. /// The index of the selected option is put in the variable @menu.
  3289. /// Indexes start with 1 and are consistent with grouped and empty options.
  3290. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3291. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3292. ///
  3293. /// NOTE: the client closes the npc dialog when cancel is pressed
  3294. ///
  3295. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3296. BUILDIN_FUNC(menu)
  3297. {
  3298. int i;
  3299. const char* text;
  3300. TBL_PC* sd;
  3301. sd = script_rid2sd(st);
  3302. if( sd == NULL )
  3303. return 0;
  3304. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3305. if( sd->state.menu_or_input == 0 )
  3306. {
  3307. struct StringBuf buf;
  3308. struct script_data* data;
  3309. if( script_lastdata(st) % 2 == 0 )
  3310. {// argument count is not even (1st argument is at index 2)
  3311. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3312. st->state = END;
  3313. return 1;
  3314. }
  3315. StringBuf_Init(&buf);
  3316. sd->npc_menu = 0;
  3317. for( i = 2; i < script_lastdata(st); i += 2 )
  3318. {
  3319. // menu options
  3320. text = script_getstr(st, i);
  3321. // target label
  3322. data = script_getdata(st, i+1);
  3323. if( !data_islabel(data) )
  3324. {// not a label
  3325. StringBuf_Destroy(&buf);
  3326. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3327. script_reportdata(data);
  3328. st->state = END;
  3329. return 1;
  3330. }
  3331. // append option(s)
  3332. if( text[0] == '\0' )
  3333. continue;// empty string, ignore
  3334. if( sd->npc_menu > 0 )
  3335. StringBuf_AppendStr(&buf, ":");
  3336. StringBuf_AppendStr(&buf, text);
  3337. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3338. }
  3339. st->state = RERUNLINE;
  3340. sd->state.menu_or_input = 1;
  3341. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3342. StringBuf_Destroy(&buf);
  3343. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3344. }
  3345. else if( sd->npc_menu == 0xff )
  3346. {// Cancel was pressed
  3347. sd->state.menu_or_input = 0;
  3348. st->state = END;
  3349. }
  3350. else
  3351. {// goto target label
  3352. int menu = 0;
  3353. sd->state.menu_or_input = 0;
  3354. if( sd->npc_menu <= 0 )
  3355. {
  3356. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3357. st->state = END;
  3358. return 1;
  3359. }
  3360. // get target label
  3361. for( i = 2; i < script_lastdata(st); i += 2 )
  3362. {
  3363. text = script_getstr(st, i);
  3364. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3365. if( sd->npc_menu <= 0 )
  3366. break;// entry found
  3367. }
  3368. if( sd->npc_menu > 0 )
  3369. {// Invalid selection
  3370. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3371. st->state = END;
  3372. return 1;
  3373. }
  3374. if( !data_islabel(script_getdata(st, i + 1)) )
  3375. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3376. ShowError("script:menu: unexpected data in label argument\n");
  3377. script_reportdata(script_getdata(st, i + 1));
  3378. st->state = END;
  3379. return 1;
  3380. }
  3381. pc_setreg(sd, add_str("@menu"), menu);
  3382. st->pos = script_getnum(st, i + 1);
  3383. st->state = GOTO;
  3384. }
  3385. return 0;
  3386. }
  3387. /// Displays a menu with options and returns the selected option.
  3388. /// Behaves like 'menu' without the target labels.
  3389. ///
  3390. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3391. ///
  3392. /// @see menu
  3393. BUILDIN_FUNC(select)
  3394. {
  3395. int i;
  3396. const char* text;
  3397. TBL_PC* sd;
  3398. sd = script_rid2sd(st);
  3399. if( sd == NULL )
  3400. return 0;
  3401. if( sd->state.menu_or_input == 0 )
  3402. {
  3403. struct StringBuf buf;
  3404. StringBuf_Init(&buf);
  3405. sd->npc_menu = 0;
  3406. for( i = 2; i <= script_lastdata(st); ++i )
  3407. {
  3408. text = script_getstr(st, i);
  3409. if( sd->npc_menu > 0 )
  3410. StringBuf_AppendStr(&buf, ":");
  3411. StringBuf_AppendStr(&buf, text);
  3412. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3413. }
  3414. st->state = RERUNLINE;
  3415. sd->state.menu_or_input = 1;
  3416. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3417. StringBuf_Destroy(&buf);
  3418. }
  3419. else if( sd->npc_menu == 0xff )
  3420. {// Cancel was pressed
  3421. sd->state.menu_or_input = 0;
  3422. st->state = END;
  3423. }
  3424. else
  3425. {// return selected option
  3426. int menu = 0;
  3427. sd->state.menu_or_input = 0;
  3428. for( i = 2; i <= script_lastdata(st); ++i )
  3429. {
  3430. text = script_getstr(st, i);
  3431. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3432. if( sd->npc_menu <= 0 )
  3433. break;// entry found
  3434. }
  3435. pc_setreg(sd, add_str("@menu"), menu);
  3436. script_pushint(st, menu);
  3437. st->state = RUN;
  3438. }
  3439. return 0;
  3440. }
  3441. /// Displays a menu with options and returns the selected option.
  3442. /// Behaves like 'menu' without the target labels, except when cancel is
  3443. /// pressed.
  3444. /// When cancel is pressed, the script continues and 255 is returned.
  3445. ///
  3446. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3447. ///
  3448. /// @see menu
  3449. BUILDIN_FUNC(prompt)
  3450. {
  3451. int i;
  3452. const char *text;
  3453. TBL_PC* sd;
  3454. sd = script_rid2sd(st);
  3455. if( sd == NULL )
  3456. return 0;
  3457. if( sd->state.menu_or_input == 0 )
  3458. {
  3459. struct StringBuf buf;
  3460. StringBuf_Init(&buf);
  3461. sd->npc_menu = 0;
  3462. for( i = 2; i <= script_lastdata(st); ++i )
  3463. {
  3464. text = script_getstr(st, i);
  3465. if( sd->npc_menu > 0 )
  3466. StringBuf_AppendStr(&buf, ":");
  3467. StringBuf_AppendStr(&buf, text);
  3468. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3469. }
  3470. st->state = RERUNLINE;
  3471. sd->state.menu_or_input = 1;
  3472. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3473. StringBuf_Destroy(&buf);
  3474. }
  3475. else if( sd->npc_menu == 0xff )
  3476. {// Cancel was pressed
  3477. sd->state.menu_or_input = 0;
  3478. pc_setreg(sd, add_str("@menu"), 0xff);
  3479. script_pushint(st, 0xff);
  3480. st->state = RUN;
  3481. }
  3482. else
  3483. {// return selected option
  3484. int menu = 0;
  3485. sd->state.menu_or_input = 0;
  3486. for( i = 2; i <= script_lastdata(st); ++i )
  3487. {
  3488. text = script_getstr(st, i);
  3489. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3490. if( sd->npc_menu <= 0 )
  3491. break;// entry found
  3492. }
  3493. pc_setreg(sd, add_str("@menu"), menu);
  3494. script_pushint(st, menu);
  3495. st->state = RUN;
  3496. }
  3497. return 0;
  3498. }
  3499. /////////////////////////////////////////////////////////////////////
  3500. // ...
  3501. //
  3502. /// Jumps to the target script label.
  3503. ///
  3504. /// goto <label>;
  3505. BUILDIN_FUNC(goto)
  3506. {
  3507. if( !data_islabel(script_getdata(st,2)) )
  3508. {
  3509. ShowError("script:goto: not a label\n");
  3510. script_reportdata(script_getdata(st,2));
  3511. st->state = END;
  3512. return 1;
  3513. }
  3514. st->pos = script_getnum(st,2);
  3515. st->state = GOTO;
  3516. return 0;
  3517. }
  3518. /*==========================================
  3519. * user-defined function call
  3520. *------------------------------------------*/
  3521. BUILDIN_FUNC(callfunc)
  3522. {
  3523. int i, j;
  3524. struct script_retinfo* ri;
  3525. struct script_code* scr;
  3526. const char* str = script_getstr(st,2);
  3527. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3528. if( !scr )
  3529. {
  3530. ShowError("script:callfunc: function not found! [%s]\n", str);
  3531. st->state = END;
  3532. return 1;
  3533. }
  3534. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3535. {
  3536. struct script_data* data = push_copy(st->stack,i);
  3537. if( data_isreference(data) && !data->ref )
  3538. {
  3539. const char* name = reference_getname(data);
  3540. if( name[0] == '.' && name[1] == '@' )
  3541. data->ref = st->stack->var_function;
  3542. else if( name[0] == '.' )
  3543. data->ref = &st->script->script_vars;
  3544. }
  3545. }
  3546. CREATE(ri, struct script_retinfo, 1);
  3547. ri->script = st->script;// script code
  3548. ri->var_function = st->stack->var_function;// scope variables
  3549. ri->pos = st->pos;// script location
  3550. ri->nargs = j;// argument count
  3551. ri->defsp = st->stack->defsp;// default stack pointer
  3552. push_retinfo(st->stack, ri);
  3553. st->pos = 0;
  3554. st->script = scr;
  3555. st->stack->defsp = st->stack->sp;
  3556. st->state = GOTO;
  3557. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3558. return 0;
  3559. }
  3560. /*==========================================
  3561. * subroutine call
  3562. *------------------------------------------*/
  3563. BUILDIN_FUNC(callsub)
  3564. {
  3565. int i,j;
  3566. struct script_retinfo* ri;
  3567. int pos = script_getnum(st,2);
  3568. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3569. {
  3570. ShowError("script:callsub: argument is not a label\n");
  3571. script_reportdata(script_getdata(st,2));
  3572. st->state = END;
  3573. return 1;
  3574. }
  3575. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3576. {
  3577. struct script_data* data = push_copy(st->stack,i);
  3578. if( data_isreference(data) && !data->ref )
  3579. {
  3580. const char* name = reference_getname(data);
  3581. if( name[0] == '.' && name[1] == '@' )
  3582. data->ref = st->stack->var_function;
  3583. }
  3584. }
  3585. CREATE(ri, struct script_retinfo, 1);
  3586. ri->script = st->script;// script code
  3587. ri->var_function = st->stack->var_function;// scope variables
  3588. ri->pos = st->pos;// script location
  3589. ri->nargs = j;// argument count
  3590. ri->defsp = st->stack->defsp;// default stack pointer
  3591. push_retinfo(st->stack, ri);
  3592. st->pos = pos;
  3593. st->stack->defsp = st->stack->sp;
  3594. st->state = GOTO;
  3595. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3596. return 0;
  3597. }
  3598. /// Retrieves an argument provided to callfunc/callsub.
  3599. /// If the argument doesn't exist
  3600. ///
  3601. /// getarg(<index>{,<default_value>}) -> <value>
  3602. BUILDIN_FUNC(getarg)
  3603. {
  3604. struct script_retinfo* ri;
  3605. int idx;
  3606. if( st->stack->defsp < 1 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3607. {
  3608. ShowError("script:getarg: no callfunc or callsub!\n");
  3609. st->state = END;
  3610. return 1;
  3611. }
  3612. ri = st->stack->stack_data[st->stack->defsp - 1].u.ri;
  3613. idx = script_getnum(st,2);
  3614. if( idx >= 0 && idx < ri->nargs )
  3615. push_copy(st->stack, st->stack->defsp - 1 - ri->nargs + idx);
  3616. else if( script_hasdata(st,3) )
  3617. script_pushcopy(st, 3);
  3618. else
  3619. {
  3620. ShowError("script:getarg: index (idx=%d) out of range (nargs=%d) and no default value found\n", idx, ri->nargs);
  3621. st->state = END;
  3622. return 1;
  3623. }
  3624. return 0;
  3625. }
  3626. /// Returns from the current function, optionaly returning a value from the functions.
  3627. /// Don't use outside script functions.
  3628. ///
  3629. /// return;
  3630. /// return <value>;
  3631. BUILDIN_FUNC(return)
  3632. {
  3633. if( script_hasdata(st,2) )
  3634. {// return value
  3635. struct script_data* data;
  3636. script_pushcopy(st, 2);
  3637. data = script_getdatatop(st, -1);
  3638. if( data_isreference(data) )
  3639. {
  3640. const char* name = reference_getname(data);
  3641. if( name[0] == '.' && name[1] == '@' )
  3642. {// scope variable
  3643. if( !data->ref || data->ref == st->stack->var_function )
  3644. get_val(st, data);// current scope, convert to value
  3645. }
  3646. else if( name[0] == '.' && !data->ref )
  3647. {// script variable, link to current script
  3648. data->ref = &st->script->script_vars;
  3649. }
  3650. }
  3651. }
  3652. else
  3653. {// no return value
  3654. script_pushnil(st);
  3655. }
  3656. st->state = RETFUNC;
  3657. return 0;
  3658. }
  3659. /// Returns a random number from 0 to <range>-1.
  3660. /// Or returns a random number from <min> to <max>.
  3661. /// If <min> is greater than <max>, their numbers are switched.
  3662. /// rand(<range>) -> <int>
  3663. /// rand(<min>,<max>) -> <int>
  3664. BUILDIN_FUNC(rand)
  3665. {
  3666. int range;
  3667. int min;
  3668. int max;
  3669. if( script_hasdata(st,3) )
  3670. {// min,max
  3671. min = script_getnum(st,2);
  3672. max = script_getnum(st,3);
  3673. if( max < min )
  3674. swap(min, max);
  3675. range = max - min + 1;
  3676. }
  3677. else
  3678. {// range
  3679. min = 0;
  3680. range = script_getnum(st,2);
  3681. }
  3682. if( range <= 1 )
  3683. script_pushint(st, min);
  3684. else
  3685. script_pushint(st, rand()%range + min);
  3686. return 0;
  3687. }
  3688. /*==========================================
  3689. *
  3690. *------------------------------------------*/
  3691. BUILDIN_FUNC(warp)
  3692. {
  3693. int ret;
  3694. int x,y;
  3695. const char* str;
  3696. TBL_PC* sd;
  3697. sd = script_rid2sd(st);
  3698. if( sd == NULL )
  3699. return 0;
  3700. str = script_getstr(st,2);
  3701. x = script_getnum(st,3);
  3702. y = script_getnum(st,4);
  3703. if(strcmp(str,"Random")==0)
  3704. ret = pc_randomwarp(sd,3);
  3705. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3706. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3707. else
  3708. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3709. if( ret ) {
  3710. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3711. script_reportsrc(st);
  3712. }
  3713. return 0;
  3714. }
  3715. /*==========================================
  3716. * エリア指定ワープ
  3717. *------------------------------------------*/
  3718. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3719. {
  3720. int x,y;
  3721. unsigned int map;
  3722. map=va_arg(ap, unsigned int);
  3723. x=va_arg(ap,int);
  3724. y=va_arg(ap,int);
  3725. if(map == 0)
  3726. pc_randomwarp((TBL_PC *)bl,3);
  3727. else
  3728. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3729. return 0;
  3730. }
  3731. BUILDIN_FUNC(areawarp)
  3732. {
  3733. int x,y,m;
  3734. unsigned int index;
  3735. const char *str;
  3736. const char *mapname;
  3737. int x0,y0,x1,y1;
  3738. mapname=script_getstr(st,2);
  3739. x0=script_getnum(st,3);
  3740. y0=script_getnum(st,4);
  3741. x1=script_getnum(st,5);
  3742. y1=script_getnum(st,6);
  3743. str=script_getstr(st,7);
  3744. x=script_getnum(st,8);
  3745. y=script_getnum(st,9);
  3746. if( (m=map_mapname2mapid(mapname))< 0)
  3747. return 0;
  3748. if(strcmp(str,"Random")==0)
  3749. index = 0;
  3750. else if(!(index=mapindex_name2id(str)))
  3751. return 0;
  3752. map_forsomeinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,0,BL_PC, index,x,y);
  3753. return 0;
  3754. }
  3755. /*==========================================
  3756. * areapercentheal <map>,<x1>,<y1>,<x2>,<y2>,<hp>,<sp>
  3757. *------------------------------------------*/
  3758. static int buildin_areapercentheal_sub(struct block_list *bl,va_list ap)
  3759. {
  3760. int hp, sp;
  3761. hp = va_arg(ap, int);
  3762. sp = va_arg(ap, int);
  3763. pc_percentheal((TBL_PC *)bl,hp,sp);
  3764. return 0;
  3765. }
  3766. BUILDIN_FUNC(areapercentheal)
  3767. {
  3768. int hp,sp,m;
  3769. const char *mapname;
  3770. int x0,y0,x1,y1;
  3771. mapname=script_getstr(st,2);
  3772. x0=script_getnum(st,3);
  3773. y0=script_getnum(st,4);
  3774. x1=script_getnum(st,5);
  3775. y1=script_getnum(st,6);
  3776. hp=script_getnum(st,7);
  3777. sp=script_getnum(st,8);
  3778. if( (m=map_mapname2mapid(mapname))< 0)
  3779. return 0;
  3780. map_forsomeinarea(buildin_areapercentheal_sub,m,x0,y0,x1,y1,0,BL_PC,hp,sp);
  3781. return 0;
  3782. }
  3783. /*==========================================
  3784. * warpchar [LuzZza]
  3785. * Useful for warp one player from
  3786. * another player npc-session.
  3787. * Using: warpchar "mapname",x,y,Char_ID;
  3788. *------------------------------------------*/
  3789. BUILDIN_FUNC(warpchar)
  3790. {
  3791. int x,y,a;
  3792. const char *str;
  3793. TBL_PC *sd;
  3794. str=script_getstr(st,2);
  3795. x=script_getnum(st,3);
  3796. y=script_getnum(st,4);
  3797. a=script_getnum(st,5);
  3798. sd = map_charid2sd(a);
  3799. if( sd == NULL )
  3800. return 0;
  3801. if(strcmp(str, "Random") == 0)
  3802. pc_randomwarp(sd, 3);
  3803. else
  3804. if(strcmp(str, "SavePoint") == 0)
  3805. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3806. else
  3807. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3808. return 0;
  3809. }
  3810. /*==========================================
  3811. * Warpparty - [Fredzilla]
  3812. * Syntax: warpparty "mapname",x,y,Party_ID;
  3813. *------------------------------------------*/
  3814. BUILDIN_FUNC(warpparty)
  3815. {
  3816. int x,y;
  3817. const char *str;
  3818. int p_id;
  3819. int i;
  3820. unsigned short mapindex;
  3821. TBL_PC *pl_sd;
  3822. struct party_data *p=NULL;
  3823. str=script_getstr(st,2);
  3824. x=script_getnum(st,3);
  3825. y=script_getnum(st,4);
  3826. p_id=script_getnum(st,5);
  3827. if(p_id < 1)
  3828. return 0;
  3829. p = party_search(p_id);
  3830. if (!p)
  3831. return 0;
  3832. if(strcmp(str,"Random")==0)
  3833. {
  3834. for (i = 0; i < MAX_PARTY; i++)
  3835. {
  3836. if ((pl_sd = p->data[i].sd))
  3837. {
  3838. if(map[pl_sd->bl.m].flag.nowarp)
  3839. continue;
  3840. pc_randomwarp(pl_sd,3);
  3841. }
  3842. }
  3843. }
  3844. else if(strcmp(str,"SavePointAll")==0)
  3845. {
  3846. for (i = 0; i < MAX_PARTY; i++)
  3847. {
  3848. if ((pl_sd = p->data[i].sd))
  3849. {
  3850. if(map[pl_sd->bl.m].flag.noreturn)
  3851. continue;
  3852. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3853. }
  3854. }
  3855. }
  3856. else if(strcmp(str,"SavePoint")==0)
  3857. {
  3858. pl_sd=script_rid2sd(st);
  3859. if (!pl_sd) return 0;
  3860. mapindex=pl_sd->status.save_point.map;
  3861. x=pl_sd->status.save_point.x;
  3862. y=pl_sd->status.save_point.y;
  3863. for (i = 0; i < MAX_PARTY; i++)
  3864. {
  3865. if ((pl_sd = p->data[i].sd))
  3866. {
  3867. if(map[pl_sd->bl.m].flag.noreturn)
  3868. continue;
  3869. pc_setpos(pl_sd,mapindex,x,y,3);
  3870. }
  3871. }
  3872. }
  3873. else if(strcmp(str,"Leader")==0)
  3874. {
  3875. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  3876. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  3877. return 0;
  3878. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  3879. return 0;
  3880. mapindex = p->data[i].sd->mapindex;
  3881. x = p->data[i].sd->bl.x;
  3882. y = p->data[i].sd->bl.y;
  3883. for (i = 0; i < MAX_PARTY; i++)
  3884. {
  3885. pl_sd = p->data[i].sd;
  3886. if (!pl_sd)
  3887. continue;
  3888. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3889. continue;
  3890. pc_setpos(pl_sd,mapindex,x,y,3);
  3891. }
  3892. }
  3893. else
  3894. {
  3895. mapindex = mapindex_name2id(str);
  3896. if (!mapindex) //Show source of npc error.
  3897. return 1;
  3898. for (i = 0; i < MAX_PARTY; i++)
  3899. {
  3900. if ((pl_sd = p->data[i].sd))
  3901. {
  3902. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3903. continue;
  3904. pc_setpos(pl_sd,mapindex,x,y,3);
  3905. }
  3906. }
  3907. }
  3908. return 0;
  3909. }
  3910. /*==========================================
  3911. * Warpguild - [Fredzilla]
  3912. * Syntax: warpguild "mapname",x,y,Guild_ID;
  3913. *------------------------------------------*/
  3914. BUILDIN_FUNC(warpguild)
  3915. {
  3916. TBL_PC *sd;
  3917. TBL_PC *pl_sd;
  3918. struct guild* g;
  3919. struct s_mapiterator* iter;
  3920. int type;
  3921. const char* str = script_getstr(st,2);
  3922. int x = script_getnum(st,3);
  3923. int y = script_getnum(st,4);
  3924. int gid = script_getnum(st,5);
  3925. sd=script_rid2sd(st);
  3926. if( sd == NULL )
  3927. return 0;
  3928. g = guild_search(gid);
  3929. if( g == NULL )
  3930. return 0;
  3931. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  3932. return 0;
  3933. type = ( strcmp(str,"Random")==0 ) ? 0
  3934. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  3935. : ( strcmp(str,"SavePoint")==0 ) ? 2
  3936. : 3;
  3937. iter = mapit_getallusers();
  3938. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  3939. {
  3940. if( pl_sd->status.guild_id != gid )
  3941. continue;
  3942. switch( type )
  3943. {
  3944. case 0: // Random
  3945. if(!map[pl_sd->bl.m].flag.nowarp)
  3946. pc_randomwarp(pl_sd,3);
  3947. break;
  3948. case 1: // SavePointAll
  3949. if(!map[pl_sd->bl.m].flag.noreturn)
  3950. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3951. break;
  3952. case 2: // SavePoint
  3953. if(!map[pl_sd->bl.m].flag.noreturn)
  3954. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3955. break;
  3956. case 3: // m,x,y
  3957. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  3958. pc_setpos(pl_sd,mapindex_name2id(str),x,y,3);
  3959. break;
  3960. }
  3961. }
  3962. mapit_free(iter);
  3963. return 0;
  3964. }
  3965. /*==========================================
  3966. *
  3967. *------------------------------------------*/
  3968. BUILDIN_FUNC(heal)
  3969. {
  3970. TBL_PC *sd;
  3971. int hp,sp;
  3972. sd = script_rid2sd(st);
  3973. if (!sd) return 0;
  3974. hp=script_getnum(st,2);
  3975. sp=script_getnum(st,3);
  3976. status_heal(&sd->bl, hp, sp, 1);
  3977. return 0;
  3978. }
  3979. /*==========================================
  3980. *
  3981. *------------------------------------------*/
  3982. BUILDIN_FUNC(itemheal)
  3983. {
  3984. TBL_PC *sd;
  3985. int hp,sp;
  3986. hp=script_getnum(st,2);
  3987. sp=script_getnum(st,3);
  3988. if(potion_flag==1) {
  3989. potion_hp = hp;
  3990. potion_sp = sp;
  3991. return 0;
  3992. }
  3993. sd = script_rid2sd(st);
  3994. if (!sd) return 0;
  3995. pc_itemheal(sd,sd->itemid,hp,sp);
  3996. return 0;
  3997. }
  3998. /*==========================================
  3999. *
  4000. *------------------------------------------*/
  4001. BUILDIN_FUNC(percentheal)
  4002. {
  4003. int hp,sp;
  4004. TBL_PC* sd;
  4005. hp=script_getnum(st,2);
  4006. sp=script_getnum(st,3);
  4007. if(potion_flag==1) {
  4008. potion_per_hp = hp;
  4009. potion_per_sp = sp;
  4010. return 0;
  4011. }
  4012. sd = script_rid2sd(st);
  4013. if( sd == NULL )
  4014. return 0;
  4015. pc_percentheal(sd,hp,sp);
  4016. return 0;
  4017. }
  4018. /*==========================================
  4019. *
  4020. *------------------------------------------*/
  4021. BUILDIN_FUNC(jobchange)
  4022. {
  4023. int job, upper=-1;
  4024. job=script_getnum(st,2);
  4025. if( script_hasdata(st,3) )
  4026. upper=script_getnum(st,3);
  4027. if (pcdb_checkid(job))
  4028. {
  4029. TBL_PC* sd;
  4030. sd = script_rid2sd(st);
  4031. if( sd == NULL )
  4032. return 0;
  4033. pc_jobchange(sd, job, upper);
  4034. }
  4035. return 0;
  4036. }
  4037. /*==========================================
  4038. *
  4039. *------------------------------------------*/
  4040. BUILDIN_FUNC(jobname)
  4041. {
  4042. int class_=script_getnum(st,2);
  4043. script_pushconststr(st,job_name(class_));
  4044. return 0;
  4045. }
  4046. /// Get input from the player.
  4047. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4048. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4049. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4050. /// shorter than 'min' and 0 otherwise.
  4051. ///
  4052. /// input(<var>{,<min>{,<max>}}) -> <int>
  4053. BUILDIN_FUNC(input)
  4054. {
  4055. TBL_PC* sd;
  4056. struct script_data* data;
  4057. int uid;
  4058. const char* name;
  4059. int min;
  4060. int max;
  4061. sd = script_rid2sd(st);
  4062. if( sd == NULL )
  4063. return 0;
  4064. data = script_getdata(st,2);
  4065. if( !data_isreference(data) ){
  4066. ShowError("script:input: not a variable\n");
  4067. script_reportdata(data);
  4068. st->state = END;
  4069. return 1;
  4070. }
  4071. uid = reference_getuid(data);
  4072. name = reference_getname(data);
  4073. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4074. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4075. if( !sd->state.menu_or_input )
  4076. { // first invocation, display npc input box
  4077. sd->state.menu_or_input = 1;
  4078. st->state = RERUNLINE;
  4079. if( is_string_variable(name) )
  4080. clif_scriptinputstr(sd,st->oid);
  4081. else
  4082. clif_scriptinput(sd,st->oid);
  4083. }
  4084. else
  4085. { // take received text/value and store it in the designated variable
  4086. sd->state.menu_or_input = 0;
  4087. if( is_string_variable(name) )
  4088. {
  4089. int len = (int)strlen(sd->npc_str);
  4090. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4091. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4092. }
  4093. else
  4094. {
  4095. int amount = sd->npc_amount;
  4096. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4097. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4098. }
  4099. st->state = RUN;
  4100. }
  4101. return 0;
  4102. }
  4103. /// Sets the value of a variable.
  4104. /// The value is converted to the type of the variable.
  4105. ///
  4106. /// set(<variable>,<value>) -> <variable>
  4107. BUILDIN_FUNC(set)
  4108. {
  4109. TBL_PC* sd = NULL;
  4110. struct script_data* data;
  4111. int num;
  4112. const char* name;
  4113. char prefix;
  4114. data = script_getdata(st,2);
  4115. if( !data_isreference(data) )
  4116. {
  4117. ShowError("script:set: not a variable\n");
  4118. script_reportdata(script_getdata(st,2));
  4119. st->state = END;
  4120. return 1;
  4121. }
  4122. num = reference_getuid(data);
  4123. name = reference_getname(data);
  4124. prefix = *name;
  4125. if( not_server_variable(prefix) )
  4126. {
  4127. sd = script_rid2sd(st);
  4128. if( sd == NULL )
  4129. {
  4130. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4131. return 0;
  4132. }
  4133. }
  4134. if( is_string_variable(name) )
  4135. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4136. else
  4137. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4138. // return a copy of the variable reference
  4139. script_pushcopy(st,2);
  4140. return 0;
  4141. }
  4142. /////////////////////////////////////////////////////////////////////
  4143. /// Array variables
  4144. ///
  4145. /// Returns the size of the specified array
  4146. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4147. {
  4148. int32 ret = idx;
  4149. if( isstring )
  4150. {
  4151. for( ; idx < 128; ++idx )
  4152. {
  4153. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4154. if( str && *str )
  4155. ret = idx + 1;
  4156. script_removetop(st, -1, 0);
  4157. }
  4158. }
  4159. else
  4160. {
  4161. for( ; idx < 128; ++idx )
  4162. {
  4163. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4164. if( num )
  4165. ret = idx + 1;
  4166. script_removetop(st, -1, 0);
  4167. }
  4168. }
  4169. return ret;
  4170. }
  4171. /// Sets values of an array, from the starting index.
  4172. /// ex: setarray arr[1],1,2,3;
  4173. ///
  4174. /// setarray <array variable>,<value1>{,<value2>...};
  4175. BUILDIN_FUNC(setarray)
  4176. {
  4177. struct script_data* data;
  4178. const char* name;
  4179. int32 start;
  4180. int32 end;
  4181. int32 id;
  4182. int32 i;
  4183. TBL_PC* sd = NULL;
  4184. data = script_getdata(st, 2);
  4185. if( !data_isreference(data) )
  4186. {
  4187. ShowError("script:setarray: not a variable\n");
  4188. script_reportdata(data);
  4189. st->state = END;
  4190. return 1;// not a variable
  4191. }
  4192. id = reference_getid(data);
  4193. start = reference_getindex(data);
  4194. name = reference_getname(data);
  4195. if( not_array_variable(*name) )
  4196. {
  4197. ShowError("script:setarray: illegal scope\n");
  4198. script_reportdata(data);
  4199. st->state = END;
  4200. return 1;// not supported
  4201. }
  4202. if( not_server_variable(*name) )
  4203. {
  4204. sd = script_rid2sd(st);
  4205. if( sd == NULL )
  4206. return 0;// no player attached
  4207. }
  4208. end = start + script_lastdata(st) - 2;
  4209. if( end > 127 )
  4210. end = 127;
  4211. if( is_string_variable(name) )
  4212. {// string array
  4213. for( i = 3; start < end; ++start, ++i )
  4214. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4215. }
  4216. else
  4217. {// int array
  4218. for( i = 3; start < end; ++start, ++i )
  4219. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4220. }
  4221. return 0;
  4222. }
  4223. /// Sets count values of an array, from the starting index.
  4224. /// ex: cleararray arr[0],0,1;
  4225. ///
  4226. /// cleararray <array variable>,<value>,<count>;
  4227. BUILDIN_FUNC(cleararray)
  4228. {
  4229. struct script_data* data;
  4230. const char* name;
  4231. int32 start;
  4232. int32 end;
  4233. int32 id;
  4234. void* v;
  4235. TBL_PC* sd = NULL;
  4236. data = script_getdata(st, 2);
  4237. if( !data_isreference(data) )
  4238. {
  4239. ShowError("script:cleararray: not a variable\n");
  4240. script_reportdata(data);
  4241. st->state = END;
  4242. return 1;// not a variable
  4243. }
  4244. id = reference_getid(data);
  4245. start = reference_getindex(data);
  4246. name = reference_getname(data);
  4247. if( not_array_variable(*name) )
  4248. {
  4249. ShowError("script:cleararray: illegal scope\n");
  4250. script_reportdata(data);
  4251. st->state = END;
  4252. return 1;// not supported
  4253. }
  4254. if( not_server_variable(*name) )
  4255. {
  4256. sd = script_rid2sd(st);
  4257. if( sd == NULL )
  4258. return 0;// no player attached
  4259. }
  4260. if( is_string_variable(name) )
  4261. v = (void*)script_getstr(st, 3);
  4262. else
  4263. v = (void*)script_getnum(st, 3);
  4264. end = start + script_getnum(st, 4);
  4265. if( end > 127 )
  4266. end = 127;
  4267. for( ; start <= end; ++start )
  4268. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4269. return 0;
  4270. }
  4271. /// Copies data from one array to another.
  4272. /// ex: copyarray arr[0],arr[2],2;
  4273. ///
  4274. /// copyarray <destination array variable>,<source array variable>,<count>;
  4275. BUILDIN_FUNC(copyarray)
  4276. {
  4277. struct script_data* data1;
  4278. struct script_data* data2;
  4279. const char* name1;
  4280. const char* name2;
  4281. int32 idx1;
  4282. int32 idx2;
  4283. int32 id1;
  4284. int32 id2;
  4285. void* v;
  4286. int32 i;
  4287. int32 count;
  4288. TBL_PC* sd = NULL;
  4289. data1 = script_getdata(st, 2);
  4290. data2 = script_getdata(st, 3);
  4291. if( !data_isreference(data1) || !data_isreference(data2) )
  4292. {
  4293. ShowError("script:copyarray: not a variable\n");
  4294. script_reportdata(data1);
  4295. script_reportdata(data2);
  4296. st->state = END;
  4297. return 1;// not a variable
  4298. }
  4299. id1 = reference_getid(data1);
  4300. id2 = reference_getid(data2);
  4301. idx1 = reference_getindex(data1);
  4302. idx2 = reference_getindex(data2);
  4303. name1 = reference_getname(data1);
  4304. name2 = reference_getname(data2);
  4305. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4306. {
  4307. ShowError("script:copyarray: illegal scope\n");
  4308. script_reportdata(data1);
  4309. script_reportdata(data2);
  4310. st->state = END;
  4311. return 1;// not supported
  4312. }
  4313. if( is_string_variable(name1) != is_string_variable(name2) )
  4314. {
  4315. ShowError("script:copyarray: type mismatch\n");
  4316. script_reportdata(data1);
  4317. script_reportdata(data2);
  4318. st->state = END;
  4319. return 1;// data type mismatch
  4320. }
  4321. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4322. {
  4323. sd = script_rid2sd(st);
  4324. if( sd == NULL )
  4325. return 0;// no player attached
  4326. }
  4327. count = script_getnum(st, 4);
  4328. if( count > 128 - idx1 )
  4329. count = 128 - idx1;
  4330. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4331. return 0;// nothing to copy
  4332. if( id1 == id2 && idx1 > idx2 )
  4333. {// destination might be overlapping the source - copy in reverse order
  4334. for( i = count - 1; i >= 0; --i )
  4335. {
  4336. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4337. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4338. script_removetop(st, -1, 0);
  4339. }
  4340. }
  4341. else
  4342. {// normal copy
  4343. for( i = 0; i < count; ++i )
  4344. {
  4345. if( idx2 + i < 128 )
  4346. {
  4347. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4348. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4349. script_removetop(st, -1, 0);
  4350. }
  4351. else// out of range - assume ""/0
  4352. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4353. }
  4354. }
  4355. return 0;
  4356. }
  4357. /// Returns the size of the array.
  4358. /// Assumes that everything before the starting index exists.
  4359. /// ex: getarraysize(arr[3])
  4360. ///
  4361. /// getarraysize(<array variable>) -> <int>
  4362. BUILDIN_FUNC(getarraysize)
  4363. {
  4364. struct script_data* data;
  4365. const char* name;
  4366. data = script_getdata(st, 2);
  4367. if( !data_isreference(data) )
  4368. {
  4369. ShowError("script:getarraysize: not a variable\n");
  4370. script_reportdata(data);
  4371. script_pushnil(st);
  4372. st->state = END;
  4373. return 1;// not a variable
  4374. }
  4375. name = reference_getname(data);
  4376. if( not_array_variable(*name) )
  4377. {
  4378. ShowError("script:getarraysize: illegal scope\n");
  4379. script_reportdata(data);
  4380. script_pushnil(st);
  4381. st->state = END;
  4382. return 1;// not supported
  4383. }
  4384. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4385. return 0;
  4386. }
  4387. /// Deletes count or all the elements in an array, from the starting index.
  4388. /// ex: deletearray arr[4],2;
  4389. ///
  4390. /// deletearray <array variable>;
  4391. /// deletearray <array variable>,<count>;
  4392. BUILDIN_FUNC(deletearray)
  4393. {
  4394. struct script_data* data;
  4395. const char* name;
  4396. int start;
  4397. int end;
  4398. int id;
  4399. TBL_PC *sd = NULL;
  4400. data = script_getdata(st, 2);
  4401. if( !data_isreference(data) )
  4402. {
  4403. ShowError("script:deletearray: not a variable\n");
  4404. script_reportdata(data);
  4405. st->state = END;
  4406. return 1;// not a variable
  4407. }
  4408. id = reference_getid(data);
  4409. start = reference_getindex(data);
  4410. name = reference_getname(data);
  4411. if( not_array_variable(*name) )
  4412. {
  4413. ShowError("script:deletearray: illegal scope\n");
  4414. script_reportdata(data);
  4415. st->state = END;
  4416. return 1;// not supported
  4417. }
  4418. if( not_server_variable(*name) )
  4419. {
  4420. sd = script_rid2sd(st);
  4421. if( sd == NULL )
  4422. return 0;// no player attached
  4423. }
  4424. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4425. if( start >= end )
  4426. return 0;// nothing to free
  4427. if( script_hasdata(st,3) )
  4428. {
  4429. int count = script_getnum(st, 3);
  4430. if( count > end - start )
  4431. count = end - start;
  4432. if( count <= 0 )
  4433. return 0;// nothing to free
  4434. // move rest of the elements backward
  4435. for( ; start + count < end; ++start )
  4436. {
  4437. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4438. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4439. script_removetop(st, -1, 0);
  4440. }
  4441. }
  4442. // clear the rest of the array
  4443. if( is_string_variable(name) )
  4444. {
  4445. for( ; start < end; ++start )
  4446. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4447. }
  4448. else
  4449. {
  4450. for( ; start < end; ++start )
  4451. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4452. }
  4453. return 0;
  4454. }
  4455. /// Returns a reference to the target index of the array variable.
  4456. /// Equivalent to var[index].
  4457. ///
  4458. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4459. BUILDIN_FUNC(getelementofarray)
  4460. {
  4461. struct script_data* data;
  4462. const char* name;
  4463. int32 id;
  4464. int i;
  4465. data = script_getdata(st, 2);
  4466. if( !data_isreference(data) )
  4467. {
  4468. ShowError("script:getelementofarray: not a variable\n");
  4469. script_reportdata(data);
  4470. script_pushnil(st);
  4471. st->state = END;
  4472. return 1;// not a variable
  4473. }
  4474. id = reference_getid(data);
  4475. name = reference_getname(data);
  4476. if( not_array_variable(*name) )
  4477. {
  4478. ShowError("script:getelementofarray: illegal scope\n");
  4479. script_reportdata(data);
  4480. script_pushnil(st);
  4481. st->state = END;
  4482. return 1;// not supported
  4483. }
  4484. i = script_getnum(st, 3);
  4485. if( i < 0 || i >= 128 )
  4486. {
  4487. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4488. script_reportdata(data);
  4489. script_pushnil(st);
  4490. st->state = END;
  4491. return 1;// out of range
  4492. }
  4493. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4494. return 0;
  4495. }
  4496. /////////////////////////////////////////////////////////////////////
  4497. /// ...
  4498. ///
  4499. /*==========================================
  4500. *
  4501. *------------------------------------------*/
  4502. BUILDIN_FUNC(setlook)
  4503. {
  4504. int type,val;
  4505. TBL_PC* sd;
  4506. type=script_getnum(st,2);
  4507. val=script_getnum(st,3);
  4508. sd = script_rid2sd(st);
  4509. if( sd == NULL )
  4510. return 0;
  4511. pc_changelook(sd,type,val);
  4512. return 0;
  4513. }
  4514. BUILDIN_FUNC(changelook)
  4515. { // As setlook but only client side
  4516. int type,val;
  4517. TBL_PC* sd;
  4518. type=script_getnum(st,2);
  4519. val=script_getnum(st,3);
  4520. sd = script_rid2sd(st);
  4521. if( sd == NULL )
  4522. return 0;
  4523. clif_changelook(&sd->bl,type,val);
  4524. return 0;
  4525. }
  4526. /*==========================================
  4527. *
  4528. *------------------------------------------*/
  4529. BUILDIN_FUNC(cutin)
  4530. {
  4531. TBL_PC* sd;
  4532. sd = script_rid2sd(st);
  4533. if( sd == NULL )
  4534. return 0;
  4535. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4536. return 0;
  4537. }
  4538. /*==========================================
  4539. *
  4540. *------------------------------------------*/
  4541. BUILDIN_FUNC(viewpoint)
  4542. {
  4543. int type,x,y,id,color;
  4544. TBL_PC* sd;
  4545. type=script_getnum(st,2);
  4546. x=script_getnum(st,3);
  4547. y=script_getnum(st,4);
  4548. id=script_getnum(st,5);
  4549. color=script_getnum(st,6);
  4550. sd = script_rid2sd(st);
  4551. if( sd == NULL )
  4552. return 0;
  4553. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4554. return 0;
  4555. }
  4556. /*==========================================
  4557. *
  4558. *------------------------------------------*/
  4559. BUILDIN_FUNC(countitem)
  4560. {
  4561. int nameid, i;
  4562. int count = 0;
  4563. struct script_data* data;
  4564. TBL_PC* sd = script_rid2sd(st);
  4565. if (!sd) {
  4566. script_pushint(st,0);
  4567. return 0;
  4568. }
  4569. data = script_getdata(st,2);
  4570. get_val(st,data);
  4571. if( data_isstring(data) ) {
  4572. const char* name = conv_str(st,data);
  4573. struct item_data* item_data;
  4574. if((item_data = itemdb_searchname(name)) != NULL)
  4575. nameid = item_data->nameid;
  4576. else
  4577. nameid = 0;
  4578. } else
  4579. nameid = conv_num(st,data);
  4580. if (nameid < 500) {
  4581. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4582. script_reportsrc(st);
  4583. script_pushint(st,0);
  4584. return 1;
  4585. }
  4586. for(i = 0; i < MAX_INVENTORY; i++)
  4587. if(sd->status.inventory[i].nameid == nameid)
  4588. count += sd->status.inventory[i].amount;
  4589. script_pushint(st,count);
  4590. return 0;
  4591. }
  4592. /*==========================================
  4593. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4594. * returns number of items that meet the conditions
  4595. *------------------------------------------*/
  4596. BUILDIN_FUNC(countitem2)
  4597. {
  4598. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4599. int count = 0;
  4600. int i;
  4601. struct script_data* data;
  4602. TBL_PC* sd = script_rid2sd(st);
  4603. if (!sd) {
  4604. script_pushint(st,0);
  4605. return 0;
  4606. }
  4607. data = script_getdata(st,2);
  4608. get_val(st,data);
  4609. if( data_isstring(data) ) {
  4610. const char* name = conv_str(st,data);
  4611. struct item_data* item_data;
  4612. if((item_data = itemdb_searchname(name)) != NULL)
  4613. nameid = item_data->nameid;
  4614. else
  4615. nameid = 0;
  4616. } else
  4617. nameid = conv_num(st,data);
  4618. iden = script_getnum(st,3);
  4619. ref = script_getnum(st,4);
  4620. attr = script_getnum(st,5);
  4621. c1 = (short)script_getnum(st,6);
  4622. c2 = (short)script_getnum(st,7);
  4623. c3 = (short)script_getnum(st,8);
  4624. c4 = (short)script_getnum(st,9);
  4625. if (nameid < 500) {
  4626. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4627. script_pushint(st,0);
  4628. return 1;
  4629. }
  4630. for(i = 0; i < MAX_INVENTORY; i++)
  4631. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4632. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4633. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4634. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4635. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4636. sd->status.inventory[i].card[3] == c4
  4637. )
  4638. count += sd->status.inventory[i].amount;
  4639. script_pushint(st,count);
  4640. return 0;
  4641. }
  4642. /*==========================================
  4643. * 重量チェック
  4644. *------------------------------------------*/
  4645. BUILDIN_FUNC(checkweight)
  4646. {
  4647. int nameid=0,amount,i;
  4648. unsigned long weight;
  4649. TBL_PC *sd;
  4650. struct script_data *data;
  4651. sd = script_rid2sd(st);
  4652. if( sd == NULL )
  4653. return 0;
  4654. data=script_getdata(st,2);
  4655. get_val(st,data);
  4656. if( data_isstring(data) ){
  4657. const char *name=conv_str(st,data);
  4658. struct item_data *item_data = itemdb_searchname(name);
  4659. if( item_data )
  4660. nameid=item_data->nameid;
  4661. }else
  4662. nameid=conv_num(st,data);
  4663. amount=script_getnum(st,3);
  4664. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4665. script_pushint(st,0);
  4666. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4667. return 1;
  4668. }
  4669. weight = itemdb_weight(nameid)*amount;
  4670. if( amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight )
  4671. script_pushint(st,0);
  4672. else if( itemdb_isstackable(nameid) )
  4673. {
  4674. if( (i = pc_search_inventory(sd,nameid)) >= 0 )
  4675. script_pushint(st,amount + sd->status.inventory[i].amount > MAX_AMOUNT ? 0 : 1);
  4676. else
  4677. script_pushint(st,pc_search_inventory(sd,0) >= 0 ? 1 : 0);
  4678. }
  4679. else
  4680. {
  4681. for( i = 0; i < MAX_INVENTORY && amount; ++i )
  4682. if( sd->status.inventory[i].nameid == 0 )
  4683. amount--;
  4684. script_pushint(st,amount ? 0 : 1);
  4685. }
  4686. return 0;
  4687. }
  4688. /*==========================================
  4689. * getitem <item id>,<amount>{,<character ID>};
  4690. * getitem "<item name>",<amount>{,<character ID>};
  4691. *------------------------------------------*/
  4692. BUILDIN_FUNC(getitem)
  4693. {
  4694. int nameid,amount,get_count,i,flag = 0;
  4695. struct item it;
  4696. TBL_PC *sd;
  4697. struct script_data *data;
  4698. data=script_getdata(st,2);
  4699. get_val(st,data);
  4700. if( data_isstring(data) )
  4701. {// "<item name>"
  4702. const char *name=conv_str(st,data);
  4703. struct item_data *item_data = itemdb_searchname(name);
  4704. if( item_data == NULL ){
  4705. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4706. return 1; //No item created.
  4707. }
  4708. nameid=item_data->nameid;
  4709. } else if( data_isint(data) )
  4710. {// <item id>
  4711. nameid=conv_num(st,data);
  4712. //Violet Box, Blue Box, etc - random item pick
  4713. if( nameid < 0 ) {
  4714. nameid=itemdb_searchrandomid(-nameid);
  4715. flag = 1;
  4716. }
  4717. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4718. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4719. return 1; //No item created.
  4720. }
  4721. } else {
  4722. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4723. return 1;
  4724. }
  4725. // <amount>
  4726. if( (amount=script_getnum(st,3)) <= 0)
  4727. return 0; //return if amount <=0, skip the useles iteration
  4728. memset(&it,0,sizeof(it));
  4729. it.nameid=nameid;
  4730. if(!flag)
  4731. it.identify=1;
  4732. else
  4733. it.identify=itemdb_isidentified(nameid);
  4734. if( script_hasdata(st,4) )
  4735. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4736. else
  4737. sd=script_rid2sd(st); // Attached player
  4738. if( sd == NULL ) // no target
  4739. return 0;
  4740. //Check if it's stackable.
  4741. if (!itemdb_isstackable(nameid))
  4742. get_count = 1;
  4743. else
  4744. get_count = amount;
  4745. for (i = 0; i < amount; i += get_count)
  4746. {
  4747. // if not pet egg
  4748. if (!pet_create_egg(sd, nameid))
  4749. {
  4750. if ((flag = pc_additem(sd, &it, get_count)))
  4751. {
  4752. clif_additem(sd, 0, 0, flag);
  4753. if( pc_candrop(sd,&it) )
  4754. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4755. }
  4756. }
  4757. }
  4758. //Logs items, got from (N)PC scripts [Lupus]
  4759. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4760. log_pick_pc(sd, "N", nameid, amount, NULL);
  4761. return 0;
  4762. }
  4763. /*==========================================
  4764. *
  4765. *------------------------------------------*/
  4766. BUILDIN_FUNC(getitem2)
  4767. {
  4768. int nameid,amount,get_count,i,flag = 0;
  4769. int iden,ref,attr,c1,c2,c3,c4;
  4770. struct item_data *item_data;
  4771. struct item item_tmp;
  4772. TBL_PC *sd;
  4773. struct script_data *data;
  4774. if( script_hasdata(st,11) )
  4775. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4776. else
  4777. sd=script_rid2sd(st); // Attached player
  4778. if( sd == NULL ) // no target
  4779. return 0;
  4780. data=script_getdata(st,2);
  4781. get_val(st,data);
  4782. if( data_isstring(data) ){
  4783. const char *name=conv_str(st,data);
  4784. struct item_data *item_data = itemdb_searchname(name);
  4785. if( item_data )
  4786. nameid=item_data->nameid;
  4787. else
  4788. nameid=UNKNOWN_ITEM_ID;
  4789. }else
  4790. nameid=conv_num(st,data);
  4791. amount=script_getnum(st,3);
  4792. iden=script_getnum(st,4);
  4793. ref=script_getnum(st,5);
  4794. attr=script_getnum(st,6);
  4795. c1=(short)script_getnum(st,7);
  4796. c2=(short)script_getnum(st,8);
  4797. c3=(short)script_getnum(st,9);
  4798. c4=(short)script_getnum(st,10);
  4799. if(nameid<0) { // ランダム
  4800. nameid=itemdb_searchrandomid(-nameid);
  4801. flag = 1;
  4802. }
  4803. if(nameid > 0) {
  4804. memset(&item_tmp,0,sizeof(item_tmp));
  4805. item_data=itemdb_exists(nameid);
  4806. if (item_data == NULL)
  4807. return -1;
  4808. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4809. if(ref > 10) ref = 10;
  4810. }
  4811. else if(item_data->type==IT_PETEGG) {
  4812. iden = 1;
  4813. ref = 0;
  4814. }
  4815. else {
  4816. iden = 1;
  4817. ref = attr = 0;
  4818. }
  4819. item_tmp.nameid=nameid;
  4820. if(!flag)
  4821. item_tmp.identify=iden;
  4822. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4823. item_tmp.identify=0;
  4824. item_tmp.refine=ref;
  4825. item_tmp.attribute=attr;
  4826. item_tmp.card[0]=(short)c1;
  4827. item_tmp.card[1]=(short)c2;
  4828. item_tmp.card[2]=(short)c3;
  4829. item_tmp.card[3]=(short)c4;
  4830. //Check if it's stackable.
  4831. if (!itemdb_isstackable(nameid))
  4832. get_count = 1;
  4833. else
  4834. get_count = amount;
  4835. for (i = 0; i < amount; i += get_count)
  4836. {
  4837. // if not pet egg
  4838. if (!pet_create_egg(sd, nameid))
  4839. {
  4840. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  4841. {
  4842. clif_additem(sd, 0, 0, flag);
  4843. if( pc_candrop(sd,&item_tmp) )
  4844. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4845. }
  4846. }
  4847. }
  4848. //Logs items, got from (N)PC scripts [Lupus]
  4849. if(log_config.enable_logs&0x40)
  4850. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4851. }
  4852. return 0;
  4853. }
  4854. /*==========================================
  4855. * rentitem <item id>
  4856. * rentitem "<item name>"
  4857. *------------------------------------------*/
  4858. BUILDIN_FUNC(rentitem)
  4859. {
  4860. struct map_session_data *sd;
  4861. struct script_data *data;
  4862. struct item it;
  4863. int seconds;
  4864. int nameid = 0, flag;
  4865. data = script_getdata(st,2);
  4866. get_val(st,data);
  4867. if( (sd = script_rid2sd(st)) == NULL )
  4868. return 0;
  4869. if( data_isstring(data) )
  4870. {
  4871. const char *name = conv_str(st,data);
  4872. struct item_data *itd = itemdb_searchname(name);
  4873. if( itd == NULL )
  4874. {
  4875. ShowError("buildin_rentitem: Nonexistant item %s requested.\n", name);
  4876. return 1;
  4877. }
  4878. nameid = itd->nameid;
  4879. }
  4880. else if( data_isint(data) )
  4881. {
  4882. nameid = conv_num(st,data);
  4883. if( nameid <= 0 || !itemdb_exists(nameid) )
  4884. {
  4885. ShowError("buildin_rentitem: Nonexistant item %d requested.\n", nameid);
  4886. return 1;
  4887. }
  4888. }
  4889. else
  4890. {
  4891. ShowError("buildin_rentitem: invalid data type for argument #1 (%d).\n", data->type);
  4892. return 1;
  4893. }
  4894. seconds = script_getnum(st,3);
  4895. memset(&it, 0, sizeof(it));
  4896. it.nameid = nameid;
  4897. it.identify = 1;
  4898. it.expire_time = (unsigned int)(time(NULL) + seconds);
  4899. if( (flag = pc_additem(sd, &it, 1)) )
  4900. {
  4901. clif_additem(sd, 0, 0, flag);
  4902. return 1;
  4903. }
  4904. clif_rental_time(sd->fd, nameid, seconds);
  4905. pc_inventory_rental_add(sd, seconds);
  4906. if( log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS )
  4907. log_pick_pc(sd, "N", nameid, 1, NULL);
  4908. return 0;
  4909. }
  4910. /*==========================================
  4911. * gets an item with someone's name inscribed [Skotlex]
  4912. * getinscribeditem item_num, character_name
  4913. * Returned Qty is always 1, only works on equip-able
  4914. * equipment
  4915. *------------------------------------------*/
  4916. BUILDIN_FUNC(getnameditem)
  4917. {
  4918. int nameid;
  4919. struct item item_tmp;
  4920. TBL_PC *sd, *tsd;
  4921. struct script_data *data;
  4922. sd = script_rid2sd(st);
  4923. if (sd == NULL)
  4924. { //Player not attached!
  4925. script_pushint(st,0);
  4926. return 0;
  4927. }
  4928. data=script_getdata(st,2);
  4929. get_val(st,data);
  4930. if( data_isstring(data) ){
  4931. const char *name=conv_str(st,data);
  4932. struct item_data *item_data = itemdb_searchname(name);
  4933. if( item_data == NULL)
  4934. { //Failed
  4935. script_pushint(st,0);
  4936. return 0;
  4937. }
  4938. nameid = item_data->nameid;
  4939. }else
  4940. nameid = conv_num(st,data);
  4941. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  4942. { //Even though named stackable items "could" be risky, they are required for certain quests.
  4943. script_pushint(st,0);
  4944. return 0;
  4945. }
  4946. data=script_getdata(st,3);
  4947. get_val(st,data);
  4948. if( data_isstring(data) ) //Char Name
  4949. tsd=map_nick2sd(conv_str(st,data));
  4950. else //Char Id was given
  4951. tsd=map_charid2sd(conv_num(st,data));
  4952. if( tsd == NULL )
  4953. { //Failed
  4954. script_pushint(st,0);
  4955. return 0;
  4956. }
  4957. memset(&item_tmp,0,sizeof(item_tmp));
  4958. item_tmp.nameid=nameid;
  4959. item_tmp.amount=1;
  4960. item_tmp.identify=1;
  4961. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  4962. item_tmp.card[2]=tsd->status.char_id;
  4963. item_tmp.card[3]=tsd->status.char_id >> 16;
  4964. if(pc_additem(sd,&item_tmp,1)) {
  4965. script_pushint(st,0);
  4966. return 0; //Failed to add item, we will not drop if they don't fit
  4967. }
  4968. //Logs items, got from (N)PC scripts [Lupus]
  4969. if(log_config.enable_logs&0x40)
  4970. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  4971. script_pushint(st,1);
  4972. return 0;
  4973. }
  4974. /*==========================================
  4975. * gets a random item ID from an item group [Skotlex]
  4976. * groupranditem group_num
  4977. *------------------------------------------*/
  4978. BUILDIN_FUNC(grouprandomitem)
  4979. {
  4980. int group;
  4981. group = script_getnum(st,2);
  4982. script_pushint(st,itemdb_searchrandomid(group));
  4983. return 0;
  4984. }
  4985. /*==========================================
  4986. *
  4987. *------------------------------------------*/
  4988. BUILDIN_FUNC(makeitem)
  4989. {
  4990. int nameid,amount,flag = 0;
  4991. int x,y,m;
  4992. const char *mapname;
  4993. struct item item_tmp;
  4994. struct script_data *data;
  4995. data=script_getdata(st,2);
  4996. get_val(st,data);
  4997. if( data_isstring(data) ){
  4998. const char *name=conv_str(st,data);
  4999. struct item_data *item_data = itemdb_searchname(name);
  5000. if( item_data )
  5001. nameid=item_data->nameid;
  5002. else
  5003. nameid=UNKNOWN_ITEM_ID;
  5004. }else
  5005. nameid=conv_num(st,data);
  5006. amount=script_getnum(st,3);
  5007. mapname =script_getstr(st,4);
  5008. x =script_getnum(st,5);
  5009. y =script_getnum(st,6);
  5010. if(strcmp(mapname,"this")==0)
  5011. {
  5012. TBL_PC *sd;
  5013. sd = script_rid2sd(st);
  5014. if (!sd) return 0; //Failed...
  5015. m=sd->bl.m;
  5016. } else
  5017. m=map_mapname2mapid(mapname);
  5018. if(nameid<0) { // ランダム
  5019. nameid=itemdb_searchrandomid(-nameid);
  5020. flag = 1;
  5021. }
  5022. if(nameid > 0) {
  5023. memset(&item_tmp,0,sizeof(item_tmp));
  5024. item_tmp.nameid=nameid;
  5025. if(!flag)
  5026. item_tmp.identify=1;
  5027. else
  5028. item_tmp.identify=itemdb_isidentified(nameid);
  5029. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5030. }
  5031. return 0;
  5032. }
  5033. /// Deletes items from the target/attached player.
  5034. /// Prioritizes ordinary items.
  5035. ///
  5036. /// delitem <item id>,<amount>{,<account id>}
  5037. /// delitem "<item name>",<amount>{,<account id>}
  5038. BUILDIN_FUNC(delitem)
  5039. {
  5040. int nameid=0,amount,i,important_item=0;
  5041. TBL_PC *sd;
  5042. struct script_data *data;
  5043. if( script_hasdata(st,4) )
  5044. {
  5045. int account_id = script_getnum(st,4);
  5046. sd = map_id2sd(account_id); // <account id>
  5047. if( sd == NULL )
  5048. {
  5049. ShowError("script:delitem: player not found (AID=%d).\n", account_id);
  5050. st->state = END;
  5051. return 1;
  5052. }
  5053. }
  5054. else
  5055. {
  5056. sd = script_rid2sd(st);// attached player
  5057. if( sd == NULL )
  5058. return 0;
  5059. }
  5060. data = script_getdata(st,2);
  5061. get_val(st,data);
  5062. if( data_isstring(data) )
  5063. {
  5064. const char* item_name = conv_str(st,data);
  5065. struct item_data* id = itemdb_searchname(item_name);
  5066. if( id == NULL )
  5067. {
  5068. ShowError("script:delitem: unknown item \"%s\".\n", item_name);
  5069. st->state = END;
  5070. return 1;
  5071. }
  5072. nameid = id->nameid;// "<item name>"
  5073. }
  5074. else
  5075. nameid = conv_num(st,data);// <item id>
  5076. amount=script_getnum(st,3);
  5077. if( amount <= 0 )
  5078. return 0;// nothing to do
  5079. //1st pass
  5080. //here we won't delete items with CARDS, named items but we count them
  5081. for(i=0;i<MAX_INVENTORY;i++){
  5082. //we don't delete wrong item or equipped item
  5083. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5084. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5085. continue;
  5086. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5087. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5088. sd->status.inventory[i].card[0] == CARD0_PET)
  5089. {
  5090. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5091. continue; //pet couldn't be sent for deletion.
  5092. } else
  5093. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5094. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5095. sd->status.inventory[i].card[0] ||
  5096. sd->status.inventory[i].refine) {
  5097. //this is important item, count it (except for pet eggs)
  5098. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5099. important_item++;
  5100. continue;
  5101. }
  5102. if(sd->status.inventory[i].amount>=amount){
  5103. //Logs items, got from (N)PC scripts [Lupus]
  5104. if(log_config.enable_logs&0x40)
  5105. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5106. pc_delitem(sd,i,amount,0);
  5107. return 0; //we deleted exact amount of items. now exit
  5108. } else {
  5109. amount-=sd->status.inventory[i].amount;
  5110. //Logs items, got from (N)PC scripts [Lupus]
  5111. if(log_config.enable_logs&0x40) {
  5112. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5113. }
  5114. //Logs
  5115. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5116. }
  5117. }
  5118. //2nd pass
  5119. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5120. if (important_item>0 && amount>0)
  5121. for(i=0;i<MAX_INVENTORY;i++){
  5122. //we don't delete wrong item
  5123. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5124. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5125. continue;
  5126. if(sd->status.inventory[i].amount>=amount){
  5127. //Logs items, got from (N)PC scripts [Lupus]
  5128. if(log_config.enable_logs&0x40)
  5129. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5130. pc_delitem(sd,i,amount,0);
  5131. return 0; //we deleted exact amount of items. now exit
  5132. } else {
  5133. amount-=sd->status.inventory[i].amount;
  5134. //Logs items, got from (N)PC scripts [Lupus]
  5135. if(log_config.enable_logs&0x40)
  5136. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5137. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5138. }
  5139. }
  5140. ShowError("script:delitem: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5141. st->state = END;
  5142. return 1;
  5143. }
  5144. /// Deletes items from the target/attached player.
  5145. ///
  5146. /// delitem2 <item id>,<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5147. /// delitem2 "<Item name>",<amount>,<identify>,<refine>,<attribute>,<card1>,<card2>,<card3>,<card4>{,<account ID>}
  5148. BUILDIN_FUNC(delitem2)
  5149. {
  5150. int nameid=0,amount,i=0;
  5151. int iden,ref,attr,c1,c2,c3,c4;
  5152. TBL_PC *sd;
  5153. struct script_data *data;
  5154. if( script_hasdata(st,11) )
  5155. {
  5156. int account_id = script_getnum(st,11);
  5157. sd = map_id2sd(account_id); // <account id>
  5158. if( sd == NULL )
  5159. {
  5160. ShowError("script:delitem2: player not found (AID=%d).\n", account_id);
  5161. st->state = END;
  5162. return 1;
  5163. }
  5164. }
  5165. else
  5166. {
  5167. sd = script_rid2sd(st);// attached player
  5168. if( sd == NULL )
  5169. return 0;
  5170. }
  5171. data = script_getdata(st,2);
  5172. get_val(st,data);
  5173. if( data_isstring(data) )
  5174. {
  5175. const char* item_name = conv_str(st,data);
  5176. struct item_data* id = itemdb_searchname(item_name);
  5177. if( id == NULL )
  5178. {
  5179. ShowError("script:delitem2: unknown item \"%s\".\n", item_name);
  5180. st->state = END;
  5181. return 1;
  5182. }
  5183. nameid = id->nameid;// "<item name>"
  5184. }
  5185. else
  5186. nameid = conv_num(st,data);// <item id>
  5187. amount=script_getnum(st,3);
  5188. iden=script_getnum(st,4);
  5189. ref=script_getnum(st,5);
  5190. attr=script_getnum(st,6);
  5191. c1=(short)script_getnum(st,7);
  5192. c2=(short)script_getnum(st,8);
  5193. c3=(short)script_getnum(st,9);
  5194. c4=(short)script_getnum(st,10);
  5195. if( amount <= 0 )
  5196. return 0;// nothing to do
  5197. for(i=0;i<MAX_INVENTORY;i++){
  5198. //we don't delete wrong item or equipped item
  5199. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5200. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5201. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5202. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5203. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5204. sd->status.inventory[i].card[3]!=c4)
  5205. continue;
  5206. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5207. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5208. {
  5209. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5210. continue; //Failed to send delete the pet.
  5211. }
  5212. if(sd->status.inventory[i].amount>=amount){
  5213. //Logs items, got from (N)PC scripts [Lupus]
  5214. if(log_config.enable_logs&0x40)
  5215. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5216. pc_delitem(sd,i,amount,0);
  5217. return 0; //we deleted exact amount of items. now exit
  5218. } else {
  5219. amount-=sd->status.inventory[i].amount;
  5220. //Logs items, got from (N)PC scripts [Lupus]
  5221. if(log_config.enable_logs&0x40)
  5222. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5223. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5224. }
  5225. }
  5226. ShowError("script:delitem2: failed to delete %d items (AID=%d item_id=%d).\n", amount, sd->status.account_id, nameid);
  5227. st->state = END;
  5228. return 1;
  5229. }
  5230. /*==========================================
  5231. * Enables/Disables use of items while in an NPC [Skotlex]
  5232. *------------------------------------------*/
  5233. BUILDIN_FUNC(enableitemuse)
  5234. {
  5235. TBL_PC *sd;
  5236. sd=script_rid2sd(st);
  5237. if (sd)
  5238. sd->npc_item_flag = st->oid;
  5239. return 0;
  5240. }
  5241. BUILDIN_FUNC(disableitemuse)
  5242. {
  5243. TBL_PC *sd;
  5244. sd=script_rid2sd(st);
  5245. if (sd)
  5246. sd->npc_item_flag = 0;
  5247. return 0;
  5248. }
  5249. /*==========================================
  5250. *キャラ関係のパラメータ取得
  5251. *------------------------------------------*/
  5252. BUILDIN_FUNC(readparam)
  5253. {
  5254. int type;
  5255. TBL_PC *sd;
  5256. type=script_getnum(st,2);
  5257. if( script_hasdata(st,3) )
  5258. sd=map_nick2sd(script_getstr(st,3));
  5259. else
  5260. sd=script_rid2sd(st);
  5261. if(sd==NULL){
  5262. script_pushint(st,-1);
  5263. return 0;
  5264. }
  5265. script_pushint(st,pc_readparam(sd,type));
  5266. return 0;
  5267. }
  5268. /*==========================================
  5269. *キャラ関係のID取得
  5270. *------------------------------------------*/
  5271. BUILDIN_FUNC(getcharid)
  5272. {
  5273. int num;
  5274. TBL_PC *sd;
  5275. num = script_getnum(st,2);
  5276. if( script_hasdata(st,3) )
  5277. sd=map_nick2sd(script_getstr(st,3));
  5278. else
  5279. sd=script_rid2sd(st);
  5280. if(sd==NULL){
  5281. script_pushint(st,0); //return 0, according docs
  5282. return 0;
  5283. }
  5284. switch( num ) {
  5285. case 0: script_pushint(st,sd->status.char_id); break;
  5286. case 1: script_pushint(st,sd->status.party_id); break;
  5287. case 2: script_pushint(st,sd->status.guild_id); break;
  5288. case 3: script_pushint(st,sd->status.account_id); break;
  5289. case 4: script_pushint(st,sd->state.bg_id); break;
  5290. default:
  5291. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5292. script_pushint(st,0);
  5293. break;
  5294. }
  5295. return 0;
  5296. }
  5297. /*==========================================
  5298. *指定IDのPT名取得
  5299. *------------------------------------------*/
  5300. char *buildin_getpartyname_sub(int party_id)
  5301. {
  5302. struct party_data *p;
  5303. p=party_search(party_id);
  5304. if(p!=NULL){
  5305. char *buf;
  5306. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5307. memcpy(buf, p->party.name, NAME_LENGTH);
  5308. buf[NAME_LENGTH-1] = '\0';
  5309. return buf;
  5310. }
  5311. return 0;
  5312. }
  5313. BUILDIN_FUNC(getpartyname)
  5314. {
  5315. char *name;
  5316. int party_id;
  5317. party_id=script_getnum(st,2);
  5318. name=buildin_getpartyname_sub(party_id);
  5319. if(name != NULL)
  5320. script_pushstr(st,name);
  5321. else
  5322. script_pushconststr(st,"null");
  5323. return 0;
  5324. }
  5325. /*==========================================
  5326. *指定IDのPT人数とメンバーID取得
  5327. *------------------------------------------*/
  5328. BUILDIN_FUNC(getpartymember)
  5329. {
  5330. struct party_data *p;
  5331. int i,j=0,type=0;
  5332. p=party_search(script_getnum(st,2));
  5333. if( script_hasdata(st,3) )
  5334. type=script_getnum(st,3);
  5335. if(p!=NULL){
  5336. for(i=0;i<MAX_PARTY;i++){
  5337. if(p->party.member[i].account_id){
  5338. switch (type) {
  5339. case 2:
  5340. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5341. break;
  5342. case 1:
  5343. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5344. break;
  5345. default:
  5346. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5347. }
  5348. j++;
  5349. }
  5350. }
  5351. }
  5352. mapreg_setreg(add_str("$@partymembercount"),j);
  5353. return 0;
  5354. }
  5355. /*==========================================
  5356. * Retrieves party leader. if flag is specified,
  5357. * return some of the leader data. Otherwise, return name.
  5358. *------------------------------------------*/
  5359. BUILDIN_FUNC(getpartyleader)
  5360. {
  5361. int party_id, type = 0, i=0;
  5362. struct party_data *p;
  5363. party_id=script_getnum(st,2);
  5364. if( script_hasdata(st,3) )
  5365. type=script_getnum(st,3);
  5366. p=party_search(party_id);
  5367. if (p) //Search leader
  5368. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5369. if (!p || i == MAX_PARTY) { //leader not found
  5370. if (type)
  5371. script_pushint(st,-1);
  5372. else
  5373. script_pushconststr(st,"null");
  5374. return 0;
  5375. }
  5376. switch (type) {
  5377. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5378. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5379. case 3: script_pushint(st,p->party.member[i].class_); break;
  5380. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5381. case 5: script_pushint(st,p->party.member[i].lv); break;
  5382. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5383. }
  5384. return 0;
  5385. }
  5386. /*==========================================
  5387. *指定IDのギルド名取得
  5388. *------------------------------------------*/
  5389. char *buildin_getguildname_sub(int guild_id)
  5390. {
  5391. struct guild *g=NULL;
  5392. g=guild_search(guild_id);
  5393. if(g!=NULL){
  5394. char *buf;
  5395. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5396. memcpy(buf, g->name, NAME_LENGTH);
  5397. buf[NAME_LENGTH-1] = '\0';
  5398. return buf;
  5399. }
  5400. return NULL;
  5401. }
  5402. BUILDIN_FUNC(getguildname)
  5403. {
  5404. char *name;
  5405. int guild_id=script_getnum(st,2);
  5406. name=buildin_getguildname_sub(guild_id);
  5407. if(name != NULL)
  5408. script_pushstr(st,name);
  5409. else
  5410. script_pushconststr(st,"null");
  5411. return 0;
  5412. }
  5413. /*==========================================
  5414. *指定IDのGuildMaster名取得
  5415. *------------------------------------------*/
  5416. char *buildin_getguildmaster_sub(int guild_id)
  5417. {
  5418. struct guild *g=NULL;
  5419. g=guild_search(guild_id);
  5420. if(g!=NULL){
  5421. char *buf;
  5422. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5423. memcpy(buf, g->master, NAME_LENGTH);
  5424. buf[NAME_LENGTH-1] = '\0';
  5425. return buf;
  5426. }
  5427. return 0;
  5428. }
  5429. BUILDIN_FUNC(getguildmaster)
  5430. {
  5431. char *master;
  5432. int guild_id=script_getnum(st,2);
  5433. master=buildin_getguildmaster_sub(guild_id);
  5434. if(master!=0)
  5435. script_pushstr(st,master);
  5436. else
  5437. script_pushconststr(st,"null");
  5438. return 0;
  5439. }
  5440. BUILDIN_FUNC(getguildmasterid)
  5441. {
  5442. char *master;
  5443. TBL_PC *sd=NULL;
  5444. int guild_id=script_getnum(st,2);
  5445. master=buildin_getguildmaster_sub(guild_id);
  5446. if(master!=0){
  5447. if((sd=map_nick2sd(master)) == NULL){
  5448. script_pushint(st,0);
  5449. return 0;
  5450. }
  5451. script_pushint(st,sd->status.char_id);
  5452. }else{
  5453. script_pushint(st,0);
  5454. }
  5455. return 0;
  5456. }
  5457. /*==========================================
  5458. * キャラクタの名前
  5459. *------------------------------------------*/
  5460. BUILDIN_FUNC(strcharinfo)
  5461. {
  5462. TBL_PC *sd;
  5463. int num;
  5464. char *buf;
  5465. sd=script_rid2sd(st);
  5466. if (!sd) { //Avoid crashing....
  5467. script_pushconststr(st,"");
  5468. return 0;
  5469. }
  5470. num=script_getnum(st,2);
  5471. switch(num){
  5472. case 0:
  5473. script_pushstrcopy(st,sd->status.name);
  5474. break;
  5475. case 1:
  5476. buf=buildin_getpartyname_sub(sd->status.party_id);
  5477. if(buf!=0)
  5478. script_pushstr(st,buf);
  5479. else
  5480. script_pushconststr(st,"");
  5481. break;
  5482. case 2:
  5483. buf=buildin_getguildname_sub(sd->status.guild_id);
  5484. if(buf != NULL)
  5485. script_pushstr(st,buf);
  5486. else
  5487. script_pushconststr(st,"");
  5488. break;
  5489. default:
  5490. ShowWarning("buildin_strcharinfo: unknown parameter.\n");
  5491. script_pushconststr(st,"");
  5492. break;
  5493. }
  5494. return 0;
  5495. }
  5496. /*==========================================
  5497. * 呼び出し元のNPC情報を取得する
  5498. *------------------------------------------*/
  5499. BUILDIN_FUNC(strnpcinfo)
  5500. {
  5501. TBL_NPC* nd;
  5502. int num;
  5503. char *buf,*name=NULL;
  5504. nd = map_id2nd(st->oid);
  5505. if (!nd) {
  5506. script_pushconststr(st, "");
  5507. return 0;
  5508. }
  5509. num = script_getnum(st,2);
  5510. switch(num){
  5511. case 0: // display name
  5512. name = aStrdup(nd->name);
  5513. break;
  5514. case 1: // visible part of display name name
  5515. if((buf = strchr(nd->name,'#')) != NULL)
  5516. {
  5517. name = aStrdup(nd->name);
  5518. name[buf - nd->name] = 0;
  5519. }
  5520. break;
  5521. case 2: // # fragment
  5522. if((buf = strchr(nd->name,'#')) != NULL)
  5523. name = aStrdup(buf+1);
  5524. break;
  5525. case 3: // unique name
  5526. name = aStrdup(nd->exname);
  5527. break;
  5528. }
  5529. if(name)
  5530. script_pushstr(st, name);
  5531. else
  5532. script_pushconststr(st, "");
  5533. return 0;
  5534. }
  5535. // aegis->athena slot position conversion table
  5536. static unsigned int equip[] = {EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_GARMENT,EQP_SHOES,EQP_ACC_L,EQP_ACC_R,EQP_HEAD_MID,EQP_HEAD_LOW};
  5537. /*==========================================
  5538. * GetEquipID(Pos); Pos: 1-10
  5539. *------------------------------------------*/
  5540. BUILDIN_FUNC(getequipid)
  5541. {
  5542. int i, num;
  5543. TBL_PC* sd;
  5544. struct item_data* item;
  5545. sd = script_rid2sd(st);
  5546. if( sd == NULL )
  5547. return 0;
  5548. num = script_getnum(st,2) - 1;
  5549. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5550. {
  5551. script_pushint(st,-1);
  5552. return 0;
  5553. }
  5554. // get inventory position of item
  5555. i = pc_checkequip(sd,equip[num]);
  5556. if( i < 0 )
  5557. {
  5558. script_pushint(st,-1);
  5559. return 0;
  5560. }
  5561. item = sd->inventory_data[i];
  5562. if( item != 0 )
  5563. script_pushint(st,item->nameid);
  5564. else
  5565. script_pushint(st,0);
  5566. return 0;
  5567. }
  5568. /*==========================================
  5569. * 装備名文字列(精錬メニュー用)
  5570. *------------------------------------------*/
  5571. BUILDIN_FUNC(getequipname)
  5572. {
  5573. int i, num;
  5574. TBL_PC* sd;
  5575. struct item_data* item;
  5576. sd = script_rid2sd(st);
  5577. if( sd == NULL )
  5578. return 0;
  5579. num = script_getnum(st,2) - 1;
  5580. if( num < 0 || num >= ARRAYLENGTH(equip) )
  5581. {
  5582. script_pushconststr(st,"");
  5583. return 0;
  5584. }
  5585. // get inventory position of item
  5586. i = pc_checkequip(sd,equip[num]);
  5587. if( i < 0 )
  5588. {
  5589. script_pushint(st,-1);
  5590. return 0;
  5591. }
  5592. item = sd->inventory_data[i];
  5593. if( item != 0 )
  5594. script_pushstrcopy(st,item->jname);
  5595. else
  5596. script_pushconststr(st,"");
  5597. return 0;
  5598. }
  5599. /*==========================================
  5600. * getbrokenid [Valaris]
  5601. *------------------------------------------*/
  5602. BUILDIN_FUNC(getbrokenid)
  5603. {
  5604. int i,num,id=0,brokencounter=0;
  5605. TBL_PC *sd;
  5606. sd = script_rid2sd(st);
  5607. if( sd == NULL )
  5608. return 0;
  5609. num=script_getnum(st,2);
  5610. for(i=0; i<MAX_INVENTORY; i++) {
  5611. if(sd->status.inventory[i].attribute==1){
  5612. brokencounter++;
  5613. if(num==brokencounter){
  5614. id=sd->status.inventory[i].nameid;
  5615. break;
  5616. }
  5617. }
  5618. }
  5619. script_pushint(st,id);
  5620. return 0;
  5621. }
  5622. /*==========================================
  5623. * repair [Valaris]
  5624. *------------------------------------------*/
  5625. BUILDIN_FUNC(repair)
  5626. {
  5627. int i,num;
  5628. int repaircounter=0;
  5629. TBL_PC *sd;
  5630. sd = script_rid2sd(st);
  5631. if( sd == NULL )
  5632. return 0;
  5633. num=script_getnum(st,2);
  5634. for(i=0; i<MAX_INVENTORY; i++) {
  5635. if(sd->status.inventory[i].attribute==1){
  5636. repaircounter++;
  5637. if(num==repaircounter){
  5638. sd->status.inventory[i].attribute=0;
  5639. clif_equiplist(sd);
  5640. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5641. clif_misceffect(&sd->bl, 3);
  5642. break;
  5643. }
  5644. }
  5645. }
  5646. return 0;
  5647. }
  5648. /*==========================================
  5649. * 装備チェック
  5650. *------------------------------------------*/
  5651. BUILDIN_FUNC(getequipisequiped)
  5652. {
  5653. int i=-1,num;
  5654. TBL_PC *sd;
  5655. num=script_getnum(st,2);
  5656. sd = script_rid2sd(st);
  5657. if( sd == NULL )
  5658. return 0;
  5659. if (num > 0 && num <= ARRAYLENGTH(equip))
  5660. i=pc_checkequip(sd,equip[num-1]);
  5661. if(i >= 0)
  5662. script_pushint(st,1);
  5663. else
  5664. script_pushint(st,0);
  5665. return 0;
  5666. }
  5667. /*==========================================
  5668. * 装備品精錬可能チェック
  5669. *------------------------------------------*/
  5670. BUILDIN_FUNC(getequipisenableref)
  5671. {
  5672. int i=-1,num;
  5673. TBL_PC *sd;
  5674. num=script_getnum(st,2);
  5675. sd = script_rid2sd(st);
  5676. if( sd == NULL )
  5677. return 0;
  5678. if( num > 0 && num <= ARRAYLENGTH(equip) )
  5679. i = pc_checkequip(sd,equip[num-1]);
  5680. if( i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine && !sd->status.inventory[i].expire_time )
  5681. script_pushint(st,1);
  5682. else
  5683. script_pushint(st,0);
  5684. return 0;
  5685. }
  5686. /*==========================================
  5687. * 装備品鑑定チェック
  5688. *------------------------------------------*/
  5689. BUILDIN_FUNC(getequipisidentify)
  5690. {
  5691. int i=-1,num;
  5692. TBL_PC *sd;
  5693. num=script_getnum(st,2);
  5694. sd = script_rid2sd(st);
  5695. if( sd == NULL )
  5696. return 0;
  5697. if (num > 0 && num <= ARRAYLENGTH(equip))
  5698. i=pc_checkequip(sd,equip[num-1]);
  5699. if(i >= 0)
  5700. script_pushint(st,sd->status.inventory[i].identify);
  5701. else
  5702. script_pushint(st,0);
  5703. return 0;
  5704. }
  5705. /*==========================================
  5706. * 装備品精錬度
  5707. *------------------------------------------*/
  5708. BUILDIN_FUNC(getequiprefinerycnt)
  5709. {
  5710. int i=-1,num;
  5711. TBL_PC *sd;
  5712. num=script_getnum(st,2);
  5713. sd = script_rid2sd(st);
  5714. if( sd == NULL )
  5715. return 0;
  5716. if (num > 0 && num <= ARRAYLENGTH(equip))
  5717. i=pc_checkequip(sd,equip[num-1]);
  5718. if(i >= 0)
  5719. script_pushint(st,sd->status.inventory[i].refine);
  5720. else
  5721. script_pushint(st,0);
  5722. return 0;
  5723. }
  5724. /*==========================================
  5725. * 装備品武器LV
  5726. *------------------------------------------*/
  5727. BUILDIN_FUNC(getequipweaponlv)
  5728. {
  5729. int i=-1,num;
  5730. TBL_PC *sd;
  5731. num=script_getnum(st,2);
  5732. sd = script_rid2sd(st);
  5733. if( sd == NULL )
  5734. return 0;
  5735. if (num > 0 && num <= ARRAYLENGTH(equip))
  5736. i=pc_checkequip(sd,equip[num-1]);
  5737. if(i >= 0 && sd->inventory_data[i])
  5738. script_pushint(st,sd->inventory_data[i]->wlv);
  5739. else
  5740. script_pushint(st,0);
  5741. return 0;
  5742. }
  5743. /*==========================================
  5744. * 装備品精錬成功率
  5745. *------------------------------------------*/
  5746. BUILDIN_FUNC(getequippercentrefinery)
  5747. {
  5748. int i=-1,num;
  5749. TBL_PC *sd;
  5750. num=script_getnum(st,2);
  5751. sd = script_rid2sd(st);
  5752. if( sd == NULL )
  5753. return 0;
  5754. if (num > 0 && num <= ARRAYLENGTH(equip))
  5755. i=pc_checkequip(sd,equip[num-1]);
  5756. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5757. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5758. else
  5759. script_pushint(st,0);
  5760. return 0;
  5761. }
  5762. /*==========================================
  5763. * 精錬成功
  5764. *------------------------------------------*/
  5765. BUILDIN_FUNC(successrefitem)
  5766. {
  5767. int i=-1,num,ep;
  5768. TBL_PC *sd;
  5769. num=script_getnum(st,2);
  5770. sd = script_rid2sd(st);
  5771. if( sd == NULL )
  5772. return 0;
  5773. if (num > 0 && num <= ARRAYLENGTH(equip))
  5774. i=pc_checkequip(sd,equip[num-1]);
  5775. if(i >= 0) {
  5776. ep=sd->status.inventory[i].equip;
  5777. //Logs items, got from (N)PC scripts [Lupus]
  5778. if(log_config.enable_logs&0x40)
  5779. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5780. sd->status.inventory[i].refine++;
  5781. pc_unequipitem(sd,i,2); // status calc will happen in pc_equipitem() below
  5782. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5783. clif_delitem(sd,i,1);
  5784. //Logs items, got from (N)PC scripts [Lupus]
  5785. if(log_config.enable_logs&0x40)
  5786. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5787. clif_additem(sd,i,1,0);
  5788. pc_equipitem(sd,i,ep);
  5789. clif_misceffect(&sd->bl,3);
  5790. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5791. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5792. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5793. ){ // Fame point system [DracoRPG]
  5794. switch (sd->inventory_data[i]->wlv){
  5795. case 1:
  5796. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5797. break;
  5798. case 2:
  5799. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5800. break;
  5801. case 3:
  5802. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5803. break;
  5804. }
  5805. }
  5806. }
  5807. return 0;
  5808. }
  5809. /*==========================================
  5810. * 精錬失敗
  5811. *------------------------------------------*/
  5812. BUILDIN_FUNC(failedrefitem)
  5813. {
  5814. int i=-1,num;
  5815. TBL_PC *sd;
  5816. num=script_getnum(st,2);
  5817. sd = script_rid2sd(st);
  5818. if( sd == NULL )
  5819. return 0;
  5820. if (num > 0 && num <= ARRAYLENGTH(equip))
  5821. i=pc_checkequip(sd,equip[num-1]);
  5822. if(i >= 0) {
  5823. //Logs items, got from (N)PC scripts [Lupus]
  5824. if(log_config.enable_logs&0x40)
  5825. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5826. sd->status.inventory[i].refine = 0;
  5827. pc_unequipitem(sd,i,3);
  5828. // 精錬失敗エフェクトのパケット
  5829. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5830. pc_delitem(sd,i,1,0);
  5831. // 他の人にも失敗を通知
  5832. clif_misceffect(&sd->bl,2);
  5833. }
  5834. return 0;
  5835. }
  5836. /*==========================================
  5837. *
  5838. *------------------------------------------*/
  5839. BUILDIN_FUNC(statusup)
  5840. {
  5841. int type;
  5842. TBL_PC *sd;
  5843. type=script_getnum(st,2);
  5844. sd = script_rid2sd(st);
  5845. if( sd == NULL )
  5846. return 0;
  5847. pc_statusup(sd,type);
  5848. return 0;
  5849. }
  5850. /*==========================================
  5851. *
  5852. *------------------------------------------*/
  5853. BUILDIN_FUNC(statusup2)
  5854. {
  5855. int type,val;
  5856. TBL_PC *sd;
  5857. type=script_getnum(st,2);
  5858. val=script_getnum(st,3);
  5859. sd = script_rid2sd(st);
  5860. if( sd == NULL )
  5861. return 0;
  5862. pc_statusup2(sd,type,val);
  5863. return 0;
  5864. }
  5865. /// See 'doc/item_bonus.txt'
  5866. ///
  5867. /// bonus <bonus type>,<val1>;
  5868. /// bonus2 <bonus type>,<val1>,<val2>;
  5869. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5870. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5871. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5872. BUILDIN_FUNC(bonus)
  5873. {
  5874. int type;
  5875. int val1;
  5876. int val2 = 0;
  5877. int val3 = 0;
  5878. int val4 = 0;
  5879. int val5 = 0;
  5880. TBL_PC* sd;
  5881. sd = script_rid2sd(st);
  5882. if( sd == NULL )
  5883. return 0; // no player attached
  5884. type = script_getnum(st,2);
  5885. switch( type )
  5886. {
  5887. case SP_AUTOSPELL:
  5888. case SP_AUTOSPELL_WHENHIT:
  5889. case SP_AUTOSPELL_ONSKILL:
  5890. case SP_SKILL_ATK:
  5891. case SP_SKILL_HEAL:
  5892. case SP_SKILL_HEAL2:
  5893. case SP_ADD_SKILL_BLOW:
  5894. case SP_CASTRATE:
  5895. case SP_ADDEFF_ONSKILL:
  5896. // these bonuses support skill names
  5897. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  5898. break;
  5899. default:
  5900. val1 = script_getnum(st,3);
  5901. break;
  5902. }
  5903. switch( script_lastdata(st)-2 )
  5904. {
  5905. case 1:
  5906. pc_bonus(sd, type, val1);
  5907. break;
  5908. case 2:
  5909. val2 = script_getnum(st,4);
  5910. pc_bonus2(sd, type, val1, val2);
  5911. break;
  5912. case 3:
  5913. val2 = script_getnum(st,4);
  5914. val3 = script_getnum(st,5);
  5915. pc_bonus3(sd, type, val1, val2, val3);
  5916. break;
  5917. case 4:
  5918. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  5919. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  5920. else
  5921. val2 = script_getnum(st,4);
  5922. val3 = script_getnum(st,5);
  5923. val4 = script_getnum(st,6);
  5924. pc_bonus4(sd, type, val1, val2, val3, val4);
  5925. break;
  5926. case 5:
  5927. if( type == SP_AUTOSPELL_ONSKILL && script_isstring(st,4) )
  5928. val2 = skill_name2id(script_getstr(st,4)); // 2nd value can be skill name
  5929. else
  5930. val2 = script_getnum(st,4);
  5931. val3 = script_getnum(st,5);
  5932. val4 = script_getnum(st,6);
  5933. val5 = script_getnum(st,7);
  5934. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  5935. break;
  5936. default:
  5937. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  5938. break;
  5939. }
  5940. return 0;
  5941. }
  5942. BUILDIN_FUNC(autobonus)
  5943. {
  5944. unsigned int dur;
  5945. short rate;
  5946. short atk_type = 0;
  5947. TBL_PC* sd;
  5948. struct script_code *bonus_script;
  5949. struct script_code *other_script = NULL;
  5950. sd = script_rid2sd(st);
  5951. if( sd == NULL )
  5952. return 0; // no player attached
  5953. if( sd->state.autocast )
  5954. return 0;
  5955. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  5956. return 0;
  5957. bonus_script = parse_script(script_getstr(st,2), "autobonus bonus", 0, 0);
  5958. rate = script_getnum(st,3);
  5959. dur = script_getnum(st,4);
  5960. if( !bonus_script || !rate || !dur )
  5961. return 0;
  5962. if( script_hasdata(st,5) )
  5963. atk_type = script_getnum(st,5);
  5964. if( script_hasdata(st,6) )
  5965. other_script = parse_script(script_getstr(st,6), "autobonus other", 0, 0);
  5966. if( !pc_addautobonus(sd->autobonus,ARRAYLENGTH(sd->autobonus),bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  5967. {
  5968. if( bonus_script )
  5969. script_free_code(bonus_script);
  5970. if( other_script )
  5971. script_free_code(other_script);
  5972. }
  5973. return 0;
  5974. }
  5975. BUILDIN_FUNC(autobonus2)
  5976. {
  5977. unsigned int dur;
  5978. short rate;
  5979. short atk_type = 0;
  5980. TBL_PC* sd;
  5981. struct script_code *bonus_script;
  5982. struct script_code *other_script = NULL;
  5983. sd = script_rid2sd(st);
  5984. if( sd == NULL )
  5985. return 0; // no player attached
  5986. if( sd->state.autocast )
  5987. return 0;
  5988. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  5989. return 0;
  5990. bonus_script = parse_script(script_getstr(st,2), "autobonus bonus", 0, 0);
  5991. rate = script_getnum(st,3);
  5992. dur = script_getnum(st,4);
  5993. if( !bonus_script || !rate || !dur )
  5994. return 0;
  5995. if( script_hasdata(st,5) )
  5996. atk_type = script_getnum(st,5);
  5997. if( script_hasdata(st,6) )
  5998. other_script = parse_script(script_getstr(st,6), "autobonus2 other", 0, 0);
  5999. if( !pc_addautobonus(sd->autobonus2,ARRAYLENGTH(sd->autobonus2),bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,false) )
  6000. {
  6001. if( bonus_script )
  6002. script_free_code(bonus_script);
  6003. if( other_script )
  6004. script_free_code(other_script);
  6005. }
  6006. return 0;
  6007. }
  6008. BUILDIN_FUNC(autobonus3)
  6009. {
  6010. unsigned int dur;
  6011. short rate,atk_type;
  6012. TBL_PC* sd;
  6013. struct script_code *bonus_script;
  6014. struct script_code *other_script = NULL;
  6015. sd = script_rid2sd(st);
  6016. if( sd == NULL )
  6017. return 0; // no player attached
  6018. if( sd->state.autocast )
  6019. return 0;
  6020. if( sd->state.autobonus&sd->status.inventory[current_equip_item_index].equip )
  6021. return 0;
  6022. bonus_script = parse_script(script_getstr(st,2), "autobonus bonus", 0, 0);
  6023. rate = script_getnum(st,3);
  6024. dur = script_getnum(st,4);
  6025. atk_type = ( script_isstring(st,5) ? skill_name2id(script_getstr(st,5)) : script_getnum(st,5) );
  6026. if( !bonus_script || !rate || !dur || !atk_type )
  6027. return 0;
  6028. if( script_hasdata(st,6) )
  6029. other_script = parse_script(script_getstr(st,6), "autobonus3 other", 0, 0);
  6030. if( !pc_addautobonus(sd->autobonus3,ARRAYLENGTH(sd->autobonus3),bonus_script,rate,dur,atk_type,other_script,sd->status.inventory[current_equip_item_index].equip,true) )
  6031. {
  6032. if( bonus_script )
  6033. script_free_code(bonus_script);
  6034. if( other_script )
  6035. script_free_code(other_script);
  6036. }
  6037. return 0;
  6038. }
  6039. /// Changes the level of a player skill.
  6040. /// <flag> defaults to 1
  6041. /// <flag>=0 : set the level of the skill
  6042. /// <flag>=1 : set the temporary level of the skill
  6043. /// <flag>=2 : add to the level of the skill
  6044. ///
  6045. /// skill <skill id>,<level>,<flag>
  6046. /// skill <skill id>,<level>
  6047. /// skill "<skill name>",<level>,<flag>
  6048. /// skill "<skill name>",<level>
  6049. BUILDIN_FUNC(skill)
  6050. {
  6051. int id;
  6052. int level;
  6053. int flag = 1;
  6054. TBL_PC* sd;
  6055. sd = script_rid2sd(st);
  6056. if( sd == NULL )
  6057. return 0;// no player attached, report source
  6058. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6059. level = script_getnum(st,3);
  6060. if( script_hasdata(st,4) )
  6061. flag = script_getnum(st,4);
  6062. pc_skill(sd, id, level, flag);
  6063. return 0;
  6064. }
  6065. /// Changes the level of a player skill.
  6066. /// like skill, but <flag> defaults to 2
  6067. ///
  6068. /// addtoskill <skill id>,<amount>,<flag>
  6069. /// addtoskill <skill id>,<amount>
  6070. /// addtoskill "<skill name>",<amount>,<flag>
  6071. /// addtoskill "<skill name>",<amount>
  6072. ///
  6073. /// @see skill
  6074. BUILDIN_FUNC(addtoskill)
  6075. {
  6076. int id;
  6077. int level;
  6078. int flag = 2;
  6079. TBL_PC* sd;
  6080. sd = script_rid2sd(st);
  6081. if( sd == NULL )
  6082. return 0;// no player attached, report source
  6083. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6084. level = script_getnum(st,3);
  6085. if( script_hasdata(st,4) )
  6086. flag = script_getnum(st,4);
  6087. pc_skill(sd, id, level, flag);
  6088. return 0;
  6089. }
  6090. /// Increases the level of a guild skill.
  6091. ///
  6092. /// guildskill <skill id>,<amount>;
  6093. /// guildskill "<skill name>",<amount>;
  6094. BUILDIN_FUNC(guildskill)
  6095. {
  6096. int id;
  6097. int level;
  6098. TBL_PC* sd;
  6099. int i;
  6100. sd = script_rid2sd(st);
  6101. if( sd == NULL )
  6102. return 0;// no player attached, report source
  6103. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6104. level = script_getnum(st,3);
  6105. for( i=0; i < level; i++ )
  6106. guild_skillup(sd, id);
  6107. return 0;
  6108. }
  6109. /// Returns the level of the player skill.
  6110. ///
  6111. /// getskilllv(<skill id>) -> <level>
  6112. /// getskilllv("<skill name>") -> <level>
  6113. BUILDIN_FUNC(getskilllv)
  6114. {
  6115. int id;
  6116. TBL_PC* sd;
  6117. sd = script_rid2sd(st);
  6118. if( sd == NULL )
  6119. return 0;// no player attached, report source
  6120. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6121. script_pushint(st, pc_checkskill(sd,id));
  6122. return 0;
  6123. }
  6124. /// Returns the level of the guild skill.
  6125. ///
  6126. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6127. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  6128. BUILDIN_FUNC(getgdskilllv)
  6129. {
  6130. int guild_id;
  6131. int skill_id;
  6132. struct guild* g;
  6133. guild_id = script_getnum(st,2);
  6134. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  6135. g = guild_search(guild_id);
  6136. if( g == NULL )
  6137. script_pushint(st, -1);
  6138. else
  6139. script_pushint(st, guild_checkskill(g,skill_id));
  6140. return 0;
  6141. }
  6142. /// Returns the 'basic_skill_check' setting.
  6143. /// This config determines if the server checks the skill level of NV_BASIC
  6144. /// before allowing the basic actions.
  6145. ///
  6146. /// basicskillcheck() -> <bool>
  6147. BUILDIN_FUNC(basicskillcheck)
  6148. {
  6149. script_pushint(st, battle_config.basic_skill_check);
  6150. return 0;
  6151. }
  6152. /// Returns the GM level of the player.
  6153. ///
  6154. /// getgmlevel() -> <level>
  6155. BUILDIN_FUNC(getgmlevel)
  6156. {
  6157. TBL_PC* sd;
  6158. sd = script_rid2sd(st);
  6159. if( sd == NULL )
  6160. return 0;// no player attached, report source
  6161. script_pushint(st, pc_isGM(sd));
  6162. return 0;
  6163. }
  6164. /// Terminates the execution of this script instance.
  6165. ///
  6166. /// end
  6167. BUILDIN_FUNC(end)
  6168. {
  6169. st->state = END;
  6170. return 0;
  6171. }
  6172. /// Checks if the player has that effect state (option).
  6173. ///
  6174. /// checkoption(<option>) -> <bool>
  6175. BUILDIN_FUNC(checkoption)
  6176. {
  6177. int option;
  6178. TBL_PC* sd;
  6179. sd = script_rid2sd(st);
  6180. if( sd == NULL )
  6181. return 0;// no player attached, report source
  6182. option = script_getnum(st,2);
  6183. if( sd->sc.option&option )
  6184. script_pushint(st, 1);
  6185. else
  6186. script_pushint(st, 0);
  6187. return 0;
  6188. }
  6189. /// Checks if the player is in that body state (opt1).
  6190. ///
  6191. /// checkoption1(<opt1>) -> <bool>
  6192. BUILDIN_FUNC(checkoption1)
  6193. {
  6194. int opt1;
  6195. TBL_PC* sd;
  6196. sd = script_rid2sd(st);
  6197. if( sd == NULL )
  6198. return 0;// no player attached, report source
  6199. opt1 = script_getnum(st,2);
  6200. if( sd->sc.opt1 == opt1 )
  6201. script_pushint(st, 1);
  6202. else
  6203. script_pushint(st, 0);
  6204. return 0;
  6205. }
  6206. /// Checks if the player has that health state (opt2).
  6207. ///
  6208. /// checkoption2(<opt2>) -> <bool>
  6209. BUILDIN_FUNC(checkoption2)
  6210. {
  6211. int opt2;
  6212. TBL_PC* sd;
  6213. sd = script_rid2sd(st);
  6214. if( sd == NULL )
  6215. return 0;// no player attached, report source
  6216. opt2 = script_getnum(st,2);
  6217. if( sd->sc.opt2&opt2 )
  6218. script_pushint(st, 1);
  6219. else
  6220. script_pushint(st, 0);
  6221. return 0;
  6222. }
  6223. /// Changes the effect state (option) of the player.
  6224. /// <flag> defaults to 1
  6225. /// <flag>=0 : removes the option
  6226. /// <flag>=other : adds the option
  6227. ///
  6228. /// setoption <option>,<flag>;
  6229. /// setoption <option>;
  6230. BUILDIN_FUNC(setoption)
  6231. {
  6232. int option;
  6233. int flag = 1;
  6234. TBL_PC* sd;
  6235. sd = script_rid2sd(st);
  6236. if( sd == NULL )
  6237. return 0;// no player attached, report source
  6238. option = script_getnum(st,2);
  6239. if( script_hasdata(st,3) )
  6240. flag = script_getnum(st,3);
  6241. else if( !option ){// Request to remove everything.
  6242. flag = 0;
  6243. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6244. }
  6245. if( flag ){// Add option
  6246. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6247. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6248. pc_setoption(sd, sd->sc.option|option);
  6249. } else// Remove option
  6250. pc_setoption(sd, sd->sc.option&~option);
  6251. return 0;
  6252. }
  6253. /// Returns if the player has a cart.
  6254. ///
  6255. /// checkcart() -> <bool>
  6256. ///
  6257. /// @author Valaris
  6258. BUILDIN_FUNC(checkcart)
  6259. {
  6260. TBL_PC* sd;
  6261. sd = script_rid2sd(st);
  6262. if( sd == NULL )
  6263. return 0;// no player attached, report source
  6264. if( pc_iscarton(sd) )
  6265. script_pushint(st, 1);
  6266. else
  6267. script_pushint(st, 0);
  6268. return 0;
  6269. }
  6270. /// Sets the cart of the player.
  6271. /// <type> defaults to 1
  6272. /// <type>=0 : removes the cart
  6273. /// <type>=1 : Normal cart
  6274. /// <type>=2 : Wooden cart
  6275. /// <type>=3 : Covered cart with flowers and ferns
  6276. /// <type>=4 : Wooden cart with a Panda doll on the back
  6277. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6278. ///
  6279. /// setcart <type>;
  6280. /// setcart;
  6281. BUILDIN_FUNC(setcart)
  6282. {
  6283. int type = 1;
  6284. TBL_PC* sd;
  6285. sd = script_rid2sd(st);
  6286. if( sd == NULL )
  6287. return 0;// no player attached, report source
  6288. if( script_hasdata(st,2) )
  6289. type = script_getnum(st,2);
  6290. pc_setcart(sd, type);
  6291. return 0;
  6292. }
  6293. /// Returns if the player has a falcon.
  6294. ///
  6295. /// checkfalcon() -> <bool>
  6296. ///
  6297. /// @author Valaris
  6298. BUILDIN_FUNC(checkfalcon)
  6299. {
  6300. TBL_PC* sd;
  6301. sd = script_rid2sd(st);
  6302. if( sd == NULL )
  6303. return 0;// no player attached, report source
  6304. if( pc_isfalcon(sd) )
  6305. script_pushint(st, 1);
  6306. else
  6307. script_pushint(st, 0);
  6308. return 0;
  6309. }
  6310. /// Sets if the player has a falcon or not.
  6311. /// <flag> defaults to 1
  6312. ///
  6313. /// setfalcon <flag>;
  6314. /// setfalcon;
  6315. BUILDIN_FUNC(setfalcon)
  6316. {
  6317. int flag = 1;
  6318. TBL_PC* sd;
  6319. sd = script_rid2sd(st);
  6320. if( sd == NULL )
  6321. return 0;// no player attached, report source
  6322. if( script_hasdata(st,2) )
  6323. flag = script_getnum(st,2);
  6324. pc_setfalcon(sd, flag);
  6325. return 0;
  6326. }
  6327. /// Returns if the player is riding.
  6328. ///
  6329. /// checkriding() -> <bool>
  6330. ///
  6331. /// @author Valaris
  6332. BUILDIN_FUNC(checkriding)
  6333. {
  6334. TBL_PC* sd;
  6335. sd = script_rid2sd(st);
  6336. if( sd == NULL )
  6337. return 0;// no player attached, report source
  6338. if( pc_isriding(sd) )
  6339. script_pushint(st, 1);
  6340. else
  6341. script_pushint(st, 0);
  6342. return 0;
  6343. }
  6344. /// Sets if the player is riding.
  6345. /// <flag> defaults to 1
  6346. ///
  6347. /// setriding <flag>;
  6348. /// setriding;
  6349. BUILDIN_FUNC(setriding)
  6350. {
  6351. int flag = 1;
  6352. TBL_PC* sd;
  6353. sd = script_rid2sd(st);
  6354. if( sd == NULL )
  6355. return 0;// no player attached, report source
  6356. if( script_hasdata(st,2) )
  6357. flag = script_getnum(st,2);
  6358. pc_setriding(sd, flag);
  6359. return 0;
  6360. }
  6361. /// Sets the save point of the player.
  6362. ///
  6363. /// save "<map name>",<x>,<y>
  6364. /// savepoint "<map name>",<x>,<y>
  6365. BUILDIN_FUNC(savepoint)
  6366. {
  6367. int x;
  6368. int y;
  6369. short map;
  6370. const char* str;
  6371. TBL_PC* sd;
  6372. sd = script_rid2sd(st);
  6373. if( sd == NULL )
  6374. return 0;// no player attached, report source
  6375. str = script_getstr(st, 2);
  6376. x = script_getnum(st,3);
  6377. y = script_getnum(st,4);
  6378. map = mapindex_name2id(str);
  6379. if( map )
  6380. pc_setsavepoint(sd, map, x, y);
  6381. return 0;
  6382. }
  6383. /*==========================================
  6384. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6385. *------------------------------------------*/
  6386. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6387. {
  6388. int type;
  6389. time_t timer;
  6390. struct tm *t;
  6391. type=script_getnum(st,2);
  6392. switch(type){
  6393. case 2:
  6394. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6395. // from the system clock.)
  6396. script_pushint(st,(int)time(NULL));
  6397. break;
  6398. case 1:
  6399. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6400. time(&timer);
  6401. t=localtime(&timer);
  6402. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6403. break;
  6404. case 0:
  6405. default:
  6406. //type 0:(System Ticks)
  6407. script_pushint(st,gettick());
  6408. break;
  6409. }
  6410. return 0;
  6411. }
  6412. /*==========================================
  6413. * GetTime(Type);
  6414. * 1: Sec 2: Min 3: Hour
  6415. * 4: WeekDay 5: MonthDay 6: Month
  6416. * 7: Year
  6417. *------------------------------------------*/
  6418. BUILDIN_FUNC(gettime) /* Asgard Version */
  6419. {
  6420. int type;
  6421. time_t timer;
  6422. struct tm *t;
  6423. type=script_getnum(st,2);
  6424. time(&timer);
  6425. t=localtime(&timer);
  6426. switch(type){
  6427. case 1://Sec(0~59)
  6428. script_pushint(st,t->tm_sec);
  6429. break;
  6430. case 2://Min(0~59)
  6431. script_pushint(st,t->tm_min);
  6432. break;
  6433. case 3://Hour(0~23)
  6434. script_pushint(st,t->tm_hour);
  6435. break;
  6436. case 4://WeekDay(0~6)
  6437. script_pushint(st,t->tm_wday);
  6438. break;
  6439. case 5://MonthDay(01~31)
  6440. script_pushint(st,t->tm_mday);
  6441. break;
  6442. case 6://Month(01~12)
  6443. script_pushint(st,t->tm_mon+1);
  6444. break;
  6445. case 7://Year(20xx)
  6446. script_pushint(st,t->tm_year+1900);
  6447. break;
  6448. case 8://Year Day(01~366)
  6449. script_pushint(st,t->tm_yday+1);
  6450. break;
  6451. default://(format error)
  6452. script_pushint(st,-1);
  6453. break;
  6454. }
  6455. return 0;
  6456. }
  6457. /*==========================================
  6458. * GetTimeStr("TimeFMT", Length);
  6459. *------------------------------------------*/
  6460. BUILDIN_FUNC(gettimestr)
  6461. {
  6462. char *tmpstr;
  6463. const char *fmtstr;
  6464. int maxlen;
  6465. time_t now = time(NULL);
  6466. fmtstr=script_getstr(st,2);
  6467. maxlen=script_getnum(st,3);
  6468. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6469. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6470. tmpstr[maxlen]='\0';
  6471. script_pushstr(st,tmpstr);
  6472. return 0;
  6473. }
  6474. /*==========================================
  6475. * カプラ倉庫を開く
  6476. *------------------------------------------*/
  6477. BUILDIN_FUNC(openstorage)
  6478. {
  6479. TBL_PC* sd;
  6480. sd = script_rid2sd(st);
  6481. if( sd == NULL )
  6482. return 0;
  6483. storage_storageopen(sd);
  6484. return 0;
  6485. }
  6486. BUILDIN_FUNC(guildopenstorage)
  6487. {
  6488. TBL_PC* sd;
  6489. int ret;
  6490. sd = script_rid2sd(st);
  6491. if( sd == NULL )
  6492. return 0;
  6493. ret = storage_guild_storageopen(sd);
  6494. script_pushint(st,ret);
  6495. return 0;
  6496. }
  6497. /*==========================================
  6498. * アイテムによるスキル発動
  6499. *------------------------------------------*/
  6500. /// itemskill <skill id>,<level>
  6501. /// itemskill "<skill name>",<level>
  6502. BUILDIN_FUNC(itemskill)
  6503. {
  6504. int id;
  6505. int lv;
  6506. TBL_PC* sd;
  6507. sd = script_rid2sd(st);
  6508. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6509. return 0;
  6510. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6511. lv = script_getnum(st,3);
  6512. sd->skillitem=id;
  6513. sd->skillitemlv=lv;
  6514. clif_item_skill(sd,id,lv);
  6515. return 0;
  6516. }
  6517. /*==========================================
  6518. * アイテム作成
  6519. *------------------------------------------*/
  6520. BUILDIN_FUNC(produce)
  6521. {
  6522. int trigger;
  6523. TBL_PC* sd;
  6524. sd = script_rid2sd(st);
  6525. if( sd == NULL )
  6526. return 0;
  6527. trigger=script_getnum(st,2);
  6528. clif_skill_produce_mix_list(sd, trigger);
  6529. return 0;
  6530. }
  6531. /*==========================================
  6532. *
  6533. *------------------------------------------*/
  6534. BUILDIN_FUNC(cooking)
  6535. {
  6536. int trigger;
  6537. TBL_PC* sd;
  6538. sd = script_rid2sd(st);
  6539. if( sd == NULL )
  6540. return 0;
  6541. trigger=script_getnum(st,2);
  6542. clif_cooking_list(sd, trigger);
  6543. return 0;
  6544. }
  6545. /*==========================================
  6546. * NPCでペット作る
  6547. *------------------------------------------*/
  6548. BUILDIN_FUNC(makepet)
  6549. {
  6550. TBL_PC* sd;
  6551. int id,pet_id;
  6552. id=script_getnum(st,2);
  6553. sd = script_rid2sd(st);
  6554. if( sd == NULL )
  6555. return 0;
  6556. pet_id = search_petDB_index(id, PET_CLASS);
  6557. if (pet_id < 0)
  6558. pet_id = search_petDB_index(id, PET_EGG);
  6559. if (pet_id >= 0 && sd) {
  6560. sd->catch_target_class = pet_db[pet_id].class_;
  6561. intif_create_pet(
  6562. sd->status.account_id, sd->status.char_id,
  6563. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6564. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6565. 100, 0, 1, pet_db[pet_id].jname);
  6566. }
  6567. return 0;
  6568. }
  6569. /*==========================================
  6570. * NPCで経験値上げる
  6571. *------------------------------------------*/
  6572. BUILDIN_FUNC(getexp)
  6573. {
  6574. TBL_PC* sd;
  6575. int base=0,job=0;
  6576. double bonus;
  6577. sd = script_rid2sd(st);
  6578. if( sd == NULL )
  6579. return 0;
  6580. base=script_getnum(st,2);
  6581. job =script_getnum(st,3);
  6582. if(base<0 || job<0)
  6583. return 0;
  6584. // bonus for npc-given exp
  6585. bonus = battle_config.quest_exp_rate / 100.;
  6586. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6587. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6588. pc_gainexp(sd, NULL, base, job);
  6589. return 0;
  6590. }
  6591. /*==========================================
  6592. * Gain guild exp [Celest]
  6593. *------------------------------------------*/
  6594. BUILDIN_FUNC(guildgetexp)
  6595. {
  6596. TBL_PC* sd;
  6597. int exp;
  6598. sd = script_rid2sd(st);
  6599. if( sd == NULL )
  6600. return 0;
  6601. exp = script_getnum(st,2);
  6602. if(exp < 0)
  6603. return 0;
  6604. if(sd && sd->status.guild_id > 0)
  6605. guild_getexp (sd, exp);
  6606. return 0;
  6607. }
  6608. /*==========================================
  6609. * Changes the guild master of a guild [Skotlex]
  6610. *------------------------------------------*/
  6611. BUILDIN_FUNC(guildchangegm)
  6612. {
  6613. TBL_PC *sd;
  6614. int guild_id;
  6615. const char *name;
  6616. guild_id = script_getnum(st,2);
  6617. name = script_getstr(st,3);
  6618. sd=map_nick2sd(name);
  6619. if (!sd)
  6620. script_pushint(st,0);
  6621. else
  6622. script_pushint(st,guild_gm_change(guild_id, sd));
  6623. return 0;
  6624. }
  6625. /*==========================================
  6626. * モンスター発生
  6627. *------------------------------------------*/
  6628. BUILDIN_FUNC(monster)
  6629. {
  6630. const char* mapn = script_getstr(st,2);
  6631. int x = script_getnum(st,3);
  6632. int y = script_getnum(st,4);
  6633. const char* str = script_getstr(st,5);
  6634. int class_ = script_getnum(st,6);
  6635. int amount = script_getnum(st,7);
  6636. const char* event = "";
  6637. struct map_session_data* sd;
  6638. int m;
  6639. if( script_hasdata(st,8) )
  6640. {
  6641. event = script_getstr(st,8);
  6642. check_event(st, event);
  6643. }
  6644. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6645. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6646. return 1;
  6647. }
  6648. sd = map_id2sd(st->rid);
  6649. if( sd && strcmp(mapn,"this") == 0 )
  6650. m = sd->bl.m;
  6651. else
  6652. {
  6653. m = map_mapname2mapid(mapn);
  6654. if( map[m].flag.src4instance && st->instance_id )
  6655. { // Try to redirect to the instance map, not the src map
  6656. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6657. {
  6658. ShowError("buildin_monster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  6659. return 1;
  6660. }
  6661. }
  6662. }
  6663. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6664. return 0;
  6665. }
  6666. /*==========================================
  6667. * Request List of Monster Drops
  6668. *------------------------------------------*/
  6669. BUILDIN_FUNC(getmobdrops)
  6670. {
  6671. int class_ = script_getnum(st,2);
  6672. int i, j = 0;
  6673. struct item_data *i_data;
  6674. struct mob_db *mob;
  6675. if( !mobdb_checkid(class_) )
  6676. {
  6677. script_pushint(st, 0);
  6678. return 0;
  6679. }
  6680. mob = mob_db(class_);
  6681. for( i = 0; i < MAX_MOB_DROP; i++ )
  6682. {
  6683. if( mob->dropitem[i].nameid < 1 )
  6684. continue;
  6685. if( (i_data = itemdb_exists(mob->dropitem[i].nameid)) == NULL )
  6686. continue;
  6687. mapreg_setreg(add_str("$@MobDrop_item") + (j<<24), mob->dropitem[i].nameid);
  6688. mapreg_setreg(add_str("$@MobDrop_rate") + (j<<24), mob->dropitem[i].p);
  6689. j++;
  6690. }
  6691. mapreg_setreg(add_str("$@MobDrop_count"), j);
  6692. script_pushint(st, 1);
  6693. return 0;
  6694. }
  6695. /*==========================================
  6696. * モンスター発生
  6697. *------------------------------------------*/
  6698. BUILDIN_FUNC(areamonster)
  6699. {
  6700. const char* mapn = script_getstr(st,2);
  6701. int x0 = script_getnum(st,3);
  6702. int y0 = script_getnum(st,4);
  6703. int x1 = script_getnum(st,5);
  6704. int y1 = script_getnum(st,6);
  6705. const char* str = script_getstr(st,7);
  6706. int class_ = script_getnum(st,8);
  6707. int amount = script_getnum(st,9);
  6708. const char* event = "";
  6709. struct map_session_data* sd;
  6710. int m;
  6711. if( script_hasdata(st,10) )
  6712. {
  6713. event = script_getstr(st,10);
  6714. check_event(st, event);
  6715. }
  6716. sd = map_id2sd(st->rid);
  6717. if( sd && strcmp(mapn,"this") == 0 )
  6718. m = sd->bl.m;
  6719. else
  6720. {
  6721. m = map_mapname2mapid(mapn);
  6722. if( map[m].flag.src4instance && st->instance_id )
  6723. { // Try to redirect to the instance map, not the src map
  6724. if( (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6725. {
  6726. ShowError("buildin_areamonster: Trying to spawn monster (%d) on instance map (%s) without instance attached.\n", class_, mapn);
  6727. return 1;
  6728. }
  6729. }
  6730. }
  6731. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6732. return 0;
  6733. }
  6734. /*==========================================
  6735. * モンスター削除
  6736. *------------------------------------------*/
  6737. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  6738. { //same fix but with killmonster instead - stripping events from mobs.
  6739. TBL_MOB* md = (TBL_MOB*)bl;
  6740. char *event=va_arg(ap,char *);
  6741. int allflag=va_arg(ap,int);
  6742. md->state.npc_killmonster = 1;
  6743. if(!allflag){
  6744. if(strcmp(event,md->npc_event)==0)
  6745. status_kill(bl);
  6746. }else{
  6747. if(!md->spawn)
  6748. status_kill(bl);
  6749. }
  6750. md->state.npc_killmonster = 0;
  6751. return 0;
  6752. }
  6753. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6754. {
  6755. TBL_MOB* md = (TBL_MOB*)bl;
  6756. char *event=va_arg(ap,char *);
  6757. int allflag=va_arg(ap,int);
  6758. if(!allflag){
  6759. if(strcmp(event,md->npc_event)==0)
  6760. status_kill(bl);
  6761. }else{
  6762. if(!md->spawn)
  6763. status_kill(bl);
  6764. }
  6765. return 0;
  6766. }
  6767. BUILDIN_FUNC(killmonster)
  6768. {
  6769. const char *mapname,*event;
  6770. int m,allflag=0;
  6771. mapname=script_getstr(st,2);
  6772. event=script_getstr(st,3);
  6773. if(strcmp(event,"All")==0)
  6774. allflag = 1;
  6775. else
  6776. check_event(st, event);
  6777. if( (m=map_mapname2mapid(mapname))<0 )
  6778. return 0;
  6779. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6780. return 0;
  6781. if( script_hasdata(st,4) ) {
  6782. if ( script_getnum(st,4) == 1 ) {
  6783. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6784. return 0;
  6785. }
  6786. }
  6787. map_freeblock_lock();
  6788. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  6789. map_freeblock_unlock();
  6790. return 0;
  6791. }
  6792. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  6793. { //Strips the event from the mob if it's killed the old method.
  6794. struct mob_data *md;
  6795. md = BL_CAST(BL_MOB, bl);
  6796. if (md->npc_event[0])
  6797. md->npc_event[0] = 0;
  6798. status_kill(bl);
  6799. return 0;
  6800. }
  6801. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6802. {
  6803. status_kill(bl);
  6804. return 0;
  6805. }
  6806. BUILDIN_FUNC(killmonsterall)
  6807. {
  6808. const char *mapname;
  6809. int m;
  6810. mapname=script_getstr(st,2);
  6811. if( (m = map_mapname2mapid(mapname))<0 )
  6812. return 0;
  6813. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  6814. return 0;
  6815. if( script_hasdata(st,3) ) {
  6816. if ( script_getnum(st,3) == 1 ) {
  6817. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  6818. return 0;
  6819. }
  6820. }
  6821. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  6822. return 0;
  6823. }
  6824. /*==========================================
  6825. * Creates a clone of a player.
  6826. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6827. *------------------------------------------*/
  6828. BUILDIN_FUNC(clone)
  6829. {
  6830. TBL_PC *sd, *msd=NULL;
  6831. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6832. unsigned int duration = 0;
  6833. const char *map,*event="";
  6834. map=script_getstr(st,2);
  6835. x=script_getnum(st,3);
  6836. y=script_getnum(st,4);
  6837. event=script_getstr(st,5);
  6838. char_id=script_getnum(st,6);
  6839. if( script_hasdata(st,7) )
  6840. master_id=script_getnum(st,7);
  6841. if( script_hasdata(st,8) )
  6842. mode=script_getnum(st,8);
  6843. if( script_hasdata(st,9) )
  6844. flag=script_getnum(st,9);
  6845. if( script_hasdata(st,10) )
  6846. duration=script_getnum(st,10);
  6847. check_event(st, event);
  6848. m = map_mapname2mapid(map);
  6849. if (m < 0) return 0;
  6850. sd = map_charid2sd(char_id);
  6851. if (master_id) {
  6852. msd = map_charid2sd(master_id);
  6853. if (msd)
  6854. master_id = msd->bl.id;
  6855. else
  6856. master_id = 0;
  6857. }
  6858. if (sd) //Return ID of newly crafted clone.
  6859. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6860. else //Failed to create clone.
  6861. script_pushint(st,0);
  6862. return 0;
  6863. }
  6864. /*==========================================
  6865. * イベント実行
  6866. *------------------------------------------*/
  6867. BUILDIN_FUNC(doevent)
  6868. {
  6869. const char* event = script_getstr(st,2);
  6870. check_event(st, event);
  6871. npc_event(map_id2sd(st->rid),event,0);
  6872. return 0;
  6873. }
  6874. /*==========================================
  6875. * NPC主体イベント実行
  6876. *------------------------------------------*/
  6877. BUILDIN_FUNC(donpcevent)
  6878. {
  6879. const char* event = script_getstr(st,2);
  6880. check_event(st, event);
  6881. npc_event_do(event);
  6882. return 0;
  6883. }
  6884. /// for Aegis compatibility
  6885. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6886. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6887. {
  6888. const char* npc = script_getstr(st,2);
  6889. const char* command = script_getstr(st,3);
  6890. char event[51];
  6891. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6892. check_event(st, event);
  6893. npc_event_do(event);
  6894. return 0;
  6895. }
  6896. /*==========================================
  6897. * イベントタイマー追加
  6898. *------------------------------------------*/
  6899. BUILDIN_FUNC(addtimer)
  6900. {
  6901. int tick = script_getnum(st,2);
  6902. const char* event = script_getstr(st, 3);
  6903. TBL_PC* sd;
  6904. check_event(st, event);
  6905. sd = script_rid2sd(st);
  6906. if( sd == NULL )
  6907. return 0;
  6908. pc_addeventtimer(sd,tick,event);
  6909. return 0;
  6910. }
  6911. /*==========================================
  6912. * イベントタイマー削除
  6913. *------------------------------------------*/
  6914. BUILDIN_FUNC(deltimer)
  6915. {
  6916. const char *event;
  6917. TBL_PC* sd;
  6918. event=script_getstr(st, 2);
  6919. sd = script_rid2sd(st);
  6920. if( sd == NULL )
  6921. return 0;
  6922. check_event(st, event);
  6923. pc_deleventtimer(sd,event);
  6924. return 0;
  6925. }
  6926. /*==========================================
  6927. * イベントタイマーのカウント値追加
  6928. *------------------------------------------*/
  6929. BUILDIN_FUNC(addtimercount)
  6930. {
  6931. const char *event;
  6932. int tick;
  6933. TBL_PC* sd;
  6934. event=script_getstr(st, 2);
  6935. tick=script_getnum(st,3);
  6936. sd = script_rid2sd(st);
  6937. if( sd == NULL )
  6938. return 0;
  6939. check_event(st, event);
  6940. pc_addeventtimercount(sd,event,tick);
  6941. return 0;
  6942. }
  6943. /*==========================================
  6944. * NPCタイマー初期化
  6945. *------------------------------------------*/
  6946. BUILDIN_FUNC(initnpctimer)
  6947. {
  6948. struct npc_data *nd;
  6949. int flag = 0;
  6950. if( script_hasdata(st,3) )
  6951. { //Two arguments: NPC name and attach flag.
  6952. nd = npc_name2id(script_getstr(st, 2));
  6953. flag = script_getnum(st,3);
  6954. }
  6955. else if( script_hasdata(st,2) )
  6956. { //Check if argument is numeric (flag) or string (npc name)
  6957. struct script_data *data;
  6958. data = script_getdata(st,2);
  6959. get_val(st,data);
  6960. if( data_isstring(data) ) //NPC name
  6961. nd = npc_name2id(conv_str(st, data));
  6962. else if( data_isint(data) ) //Flag
  6963. {
  6964. nd = (struct npc_data *)map_id2bl(st->oid);
  6965. flag = conv_num(st,data);
  6966. }
  6967. else
  6968. {
  6969. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6970. return 1;
  6971. }
  6972. }
  6973. else
  6974. nd = (struct npc_data *)map_id2bl(st->oid);
  6975. if( !nd )
  6976. return 0;
  6977. if( flag ) //Attach
  6978. {
  6979. TBL_PC* sd = script_rid2sd(st);
  6980. if( sd == NULL )
  6981. return 0;
  6982. nd->u.scr.rid = sd->bl.id;
  6983. }
  6984. npc_settimerevent_tick(nd,0);
  6985. npc_timerevent_start(nd, st->rid);
  6986. return 0;
  6987. }
  6988. /*==========================================
  6989. * NPCタイマー開始
  6990. *------------------------------------------*/
  6991. BUILDIN_FUNC(startnpctimer)
  6992. {
  6993. struct npc_data *nd;
  6994. int flag = 0;
  6995. if( script_hasdata(st,3) )
  6996. { //Two arguments: NPC name and attach flag.
  6997. nd = npc_name2id(script_getstr(st, 2));
  6998. flag = script_getnum(st,3);
  6999. }
  7000. else if( script_hasdata(st,2) )
  7001. { //Check if argument is numeric (flag) or string (npc name)
  7002. struct script_data *data;
  7003. data = script_getdata(st,2);
  7004. get_val(st,data);
  7005. if( data_isstring(data) ) //NPC name
  7006. nd = npc_name2id(conv_str(st, data));
  7007. else if( data_isint(data) ) //Flag
  7008. {
  7009. nd = (struct npc_data *)map_id2bl(st->oid);
  7010. flag = conv_num(st,data);
  7011. }
  7012. else
  7013. {
  7014. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7015. return 1;
  7016. }
  7017. }
  7018. else
  7019. nd=(struct npc_data *)map_id2bl(st->oid);
  7020. if( !nd )
  7021. return 0;
  7022. if( flag ) //Attach
  7023. {
  7024. TBL_PC* sd = script_rid2sd(st);
  7025. if( sd == NULL )
  7026. return 0;
  7027. nd->u.scr.rid = sd->bl.id;
  7028. }
  7029. npc_timerevent_start(nd, st->rid);
  7030. return 0;
  7031. }
  7032. /*==========================================
  7033. * NPCタイマー停止
  7034. *------------------------------------------*/
  7035. BUILDIN_FUNC(stopnpctimer)
  7036. {
  7037. struct npc_data *nd;
  7038. int flag = 0;
  7039. if( script_hasdata(st,3) )
  7040. { //Two arguments: NPC name and attach flag.
  7041. nd = npc_name2id(script_getstr(st, 2));
  7042. flag = script_getnum(st,3);
  7043. }
  7044. else if( script_hasdata(st,2) )
  7045. { //Check if argument is numeric (flag) or string (npc name)
  7046. struct script_data *data;
  7047. data = script_getdata(st,2);
  7048. get_val(st,data);
  7049. if( data_isstring(data) ) //NPC name
  7050. nd = npc_name2id(conv_str(st, data));
  7051. else if( data_isint(data) ) //Flag
  7052. {
  7053. nd = (struct npc_data *)map_id2bl(st->oid);
  7054. flag = conv_num(st,data);
  7055. }
  7056. else
  7057. {
  7058. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7059. return 1;
  7060. }
  7061. }
  7062. else
  7063. nd=(struct npc_data *)map_id2bl(st->oid);
  7064. if( !nd )
  7065. return 0;
  7066. if( flag ) //Detach
  7067. nd->u.scr.rid = 0;
  7068. npc_timerevent_stop(nd);
  7069. return 0;
  7070. }
  7071. /*==========================================
  7072. * NPCタイマー情報所得
  7073. *------------------------------------------*/
  7074. BUILDIN_FUNC(getnpctimer)
  7075. {
  7076. struct npc_data *nd;
  7077. TBL_PC *sd;
  7078. int type = script_getnum(st,2);
  7079. int val = 0;
  7080. if( script_hasdata(st,3) )
  7081. nd = npc_name2id(script_getstr(st,3));
  7082. else
  7083. nd = (struct npc_data *)map_id2bl(st->oid);
  7084. if( !nd || nd->bl.type != BL_NPC )
  7085. {
  7086. script_pushint(st,0);
  7087. ShowError("getnpctimer: Invalid NPC.\n");
  7088. return 1;
  7089. }
  7090. switch( type )
  7091. {
  7092. case 0: val = npc_gettimerevent_tick(nd); break;
  7093. case 1:
  7094. if( nd->u.scr.rid )
  7095. {
  7096. sd = map_id2sd(nd->u.scr.rid);
  7097. if( !sd )
  7098. {
  7099. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7100. break;
  7101. }
  7102. val = (sd->npc_timer_id != -1);
  7103. }
  7104. else
  7105. val = (nd->u.scr.timerid !=-1);
  7106. break;
  7107. case 2: val = nd->u.scr.timeramount; break;
  7108. }
  7109. script_pushint(st,val);
  7110. return 0;
  7111. }
  7112. /*==========================================
  7113. * NPCタイマー値設定
  7114. *------------------------------------------*/
  7115. BUILDIN_FUNC(setnpctimer)
  7116. {
  7117. int tick;
  7118. struct npc_data *nd;
  7119. tick = script_getnum(st,2);
  7120. if( script_hasdata(st,3) )
  7121. nd = npc_name2id(script_getstr(st,3));
  7122. else
  7123. nd = (struct npc_data *)map_id2bl(st->oid);
  7124. if( !nd || nd->bl.type != BL_NPC )
  7125. {
  7126. script_pushint(st,1);
  7127. ShowError("setnpctimer: Invalid NPC.\n");
  7128. return 1;
  7129. }
  7130. npc_settimerevent_tick(nd,tick);
  7131. script_pushint(st,0);
  7132. return 0;
  7133. }
  7134. /*==========================================
  7135. * attaches the player rid to the timer [Celest]
  7136. *------------------------------------------*/
  7137. BUILDIN_FUNC(attachnpctimer)
  7138. {
  7139. TBL_PC *sd;
  7140. struct npc_data *nd = (struct npc_data *)map_id2bl(st->oid);
  7141. if( !nd || nd->bl.type != BL_NPC )
  7142. {
  7143. script_pushint(st,1);
  7144. ShowError("setnpctimer: Invalid NPC.\n");
  7145. return 1;
  7146. }
  7147. if( script_hasdata(st,2) )
  7148. sd = map_nick2sd(script_getstr(st,2));
  7149. else
  7150. sd = script_rid2sd(st);
  7151. if( !sd )
  7152. {
  7153. script_pushint(st,1);
  7154. ShowWarning("attachnpctimer: Invalid player.\n");
  7155. return 1;
  7156. }
  7157. nd->u.scr.rid = sd->bl.id;
  7158. script_pushint(st,0);
  7159. return 0;
  7160. }
  7161. /*==========================================
  7162. * detaches a player rid from the timer [Celest]
  7163. *------------------------------------------*/
  7164. BUILDIN_FUNC(detachnpctimer)
  7165. {
  7166. struct npc_data *nd;
  7167. if( script_hasdata(st,2) )
  7168. nd = npc_name2id(script_getstr(st,2));
  7169. else
  7170. nd = (struct npc_data *)map_id2bl(st->oid);
  7171. if( !nd || nd->bl.type != BL_NPC )
  7172. {
  7173. script_pushint(st,1);
  7174. ShowError("detachnpctimer: Invalid NPC.\n");
  7175. return 1;
  7176. }
  7177. nd->u.scr.rid = 0;
  7178. script_pushint(st,0);
  7179. return 0;
  7180. }
  7181. /*==========================================
  7182. * To avoid "player not attached" script errors, this function is provided,
  7183. * it checks if there is a player attached to the current script. [Skotlex]
  7184. * If no, returns 0, if yes, returns the account_id of the attached player.
  7185. *------------------------------------------*/
  7186. BUILDIN_FUNC(playerattached)
  7187. {
  7188. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7189. script_pushint(st,0);
  7190. else
  7191. script_pushint(st,st->rid);
  7192. return 0;
  7193. }
  7194. /*==========================================
  7195. * 天の声アナウンス
  7196. *------------------------------------------*/
  7197. BUILDIN_FUNC(announce)
  7198. {
  7199. const char *str, *color=NULL;
  7200. int flag;
  7201. str=script_getstr(st,2);
  7202. flag=script_getnum(st,3);
  7203. if (script_hasdata(st,4))
  7204. color=script_getstr(st,4);
  7205. if(flag&0x0f){
  7206. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  7207. (struct block_list *)script_rid2sd(st);
  7208. if( bl == NULL )
  7209. return 0;
  7210. if (color)
  7211. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  7212. else
  7213. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  7214. }else {
  7215. if (color)
  7216. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  7217. else
  7218. intif_GMmessage(str,(int)strlen(str)+1,flag);
  7219. }
  7220. return 0;
  7221. }
  7222. /*==========================================
  7223. * 天の声アナウンス(特定マップ)
  7224. *------------------------------------------*/
  7225. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  7226. {
  7227. char *str, *color;
  7228. int len,flag;
  7229. str=va_arg(ap,char *);
  7230. len=va_arg(ap,int);
  7231. flag=va_arg(ap,int);
  7232. color=va_arg(ap,char *);
  7233. if (color)
  7234. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  7235. else
  7236. clif_GMmessage(bl,str,len,flag|3);
  7237. return 0;
  7238. }
  7239. BUILDIN_FUNC(mapannounce)
  7240. {
  7241. const char *mapname,*str, *color=NULL;
  7242. int flag,m;
  7243. mapname=script_getstr(st,2);
  7244. str=script_getstr(st,3);
  7245. flag=script_getnum(st,4);
  7246. if (script_hasdata(st,5))
  7247. color=script_getstr(st,5);
  7248. if( (m=map_mapname2mapid(mapname))<0 )
  7249. return 0;
  7250. map_foreachinmap(buildin_mapannounce_sub,
  7251. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7252. return 0;
  7253. }
  7254. /*==========================================
  7255. * 天の声アナウンス(特定エリア)
  7256. *------------------------------------------*/
  7257. BUILDIN_FUNC(areaannounce)
  7258. {
  7259. const char *map,*str,*color=NULL;
  7260. int flag,m;
  7261. int x0,y0,x1,y1;
  7262. map=script_getstr(st,2);
  7263. x0=script_getnum(st,3);
  7264. y0=script_getnum(st,4);
  7265. x1=script_getnum(st,5);
  7266. y1=script_getnum(st,6);
  7267. str=script_getstr(st,7);
  7268. flag=script_getnum(st,8);
  7269. if (script_hasdata(st,9))
  7270. color=script_getstr(st,9);
  7271. if( (m=map_mapname2mapid(map))<0 )
  7272. return 0;
  7273. map_forsomeinarea(buildin_mapannounce_sub,
  7274. m,x0,y0,x1,y1,0,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7275. return 0;
  7276. }
  7277. /*==========================================
  7278. * ユーザー数所得
  7279. *------------------------------------------*/
  7280. BUILDIN_FUNC(getusers)
  7281. {
  7282. int flag=script_getnum(st,2);
  7283. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7284. int val=0;
  7285. switch(flag&0x07){
  7286. case 0: val=map[bl->m].users; break;
  7287. case 1: val=map_getusers(); break;
  7288. }
  7289. script_pushint(st,val);
  7290. return 0;
  7291. }
  7292. /*==========================================
  7293. * Works like @WHO - displays all online users names in window
  7294. *------------------------------------------*/
  7295. BUILDIN_FUNC(getusersname)
  7296. {
  7297. TBL_PC *sd, *pl_sd;
  7298. int disp_num=1;
  7299. struct s_mapiterator* iter;
  7300. sd = script_rid2sd(st);
  7301. if (!sd) return 0;
  7302. iter = mapit_getallusers();
  7303. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7304. {
  7305. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7306. continue; // skip hidden GMs
  7307. if((disp_num++)%10==0)
  7308. clif_scriptnext(sd,st->oid);
  7309. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7310. }
  7311. mapit_free(iter);
  7312. return 0;
  7313. }
  7314. /*==========================================
  7315. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7316. *------------------------------------------*/
  7317. BUILDIN_FUNC(getmapguildusers)
  7318. {
  7319. const char *str;
  7320. int m, gid;
  7321. int i=0,c=0;
  7322. struct guild *g = NULL;
  7323. str=script_getstr(st,2);
  7324. gid=script_getnum(st,3);
  7325. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7326. script_pushint(st,-1);
  7327. return 0;
  7328. }
  7329. g = guild_search(gid);
  7330. if (g){
  7331. for(i = 0; i < g->max_member; i++)
  7332. {
  7333. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7334. c++;
  7335. }
  7336. }
  7337. script_pushint(st,c);
  7338. return 0;
  7339. }
  7340. /*==========================================
  7341. * マップ指定ユーザー数所得
  7342. *------------------------------------------*/
  7343. BUILDIN_FUNC(getmapusers)
  7344. {
  7345. const char *str;
  7346. int m;
  7347. str=script_getstr(st,2);
  7348. if( (m=map_mapname2mapid(str))< 0){
  7349. script_pushint(st,-1);
  7350. return 0;
  7351. }
  7352. script_pushint(st,map[m].users);
  7353. return 0;
  7354. }
  7355. /*==========================================
  7356. * エリア指定ユーザー数所得
  7357. *------------------------------------------*/
  7358. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7359. {
  7360. int *users=va_arg(ap,int *);
  7361. (*users)++;
  7362. return 0;
  7363. }
  7364. BUILDIN_FUNC(getareausers)
  7365. {
  7366. const char *str;
  7367. int m,x0,y0,x1,y1,users=0;
  7368. str=script_getstr(st,2);
  7369. x0=script_getnum(st,3);
  7370. y0=script_getnum(st,4);
  7371. x1=script_getnum(st,5);
  7372. y1=script_getnum(st,6);
  7373. if( (m=map_mapname2mapid(str))< 0){
  7374. script_pushint(st,-1);
  7375. return 0;
  7376. }
  7377. map_forsomeinarea(buildin_getareausers_sub,
  7378. m,x0,y0,x1,y1,0,BL_PC,&users);
  7379. script_pushint(st,users);
  7380. return 0;
  7381. }
  7382. /*==========================================
  7383. * エリア指定ドロップアイテム数所得
  7384. *------------------------------------------*/
  7385. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7386. {
  7387. int item=va_arg(ap,int);
  7388. int *amount=va_arg(ap,int *);
  7389. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7390. if(drop->item_data.nameid==item)
  7391. (*amount)+=drop->item_data.amount;
  7392. return 0;
  7393. }
  7394. BUILDIN_FUNC(getareadropitem)
  7395. {
  7396. const char *str;
  7397. int m,x0,y0,x1,y1,item,amount=0;
  7398. struct script_data *data;
  7399. str=script_getstr(st,2);
  7400. x0=script_getnum(st,3);
  7401. y0=script_getnum(st,4);
  7402. x1=script_getnum(st,5);
  7403. y1=script_getnum(st,6);
  7404. data=script_getdata(st,7);
  7405. get_val(st,data);
  7406. if( data_isstring(data) ){
  7407. const char *name=conv_str(st,data);
  7408. struct item_data *item_data = itemdb_searchname(name);
  7409. item=UNKNOWN_ITEM_ID;
  7410. if( item_data )
  7411. item=item_data->nameid;
  7412. }else
  7413. item=conv_num(st,data);
  7414. if( (m=map_mapname2mapid(str))< 0){
  7415. script_pushint(st,-1);
  7416. return 0;
  7417. }
  7418. map_forsomeinarea(buildin_getareadropitem_sub,
  7419. m,x0,y0,x1,y1,0,BL_ITEM,item,&amount);
  7420. script_pushint(st,amount);
  7421. return 0;
  7422. }
  7423. /*==========================================
  7424. * NPCの有効化
  7425. *------------------------------------------*/
  7426. BUILDIN_FUNC(enablenpc)
  7427. {
  7428. const char *str;
  7429. str=script_getstr(st,2);
  7430. npc_enable(str,1);
  7431. return 0;
  7432. }
  7433. /*==========================================
  7434. * NPCの無効化
  7435. *------------------------------------------*/
  7436. BUILDIN_FUNC(disablenpc)
  7437. {
  7438. const char *str;
  7439. str=script_getstr(st,2);
  7440. npc_enable(str,0);
  7441. return 0;
  7442. }
  7443. /*==========================================
  7444. * 隠れているNPCの表示
  7445. *------------------------------------------*/
  7446. BUILDIN_FUNC(hideoffnpc)
  7447. {
  7448. const char *str;
  7449. str=script_getstr(st,2);
  7450. npc_enable(str,2);
  7451. return 0;
  7452. }
  7453. /*==========================================
  7454. * NPCをハイディング
  7455. *------------------------------------------*/
  7456. BUILDIN_FUNC(hideonnpc)
  7457. {
  7458. const char *str;
  7459. str=script_getstr(st,2);
  7460. npc_enable(str,4);
  7461. return 0;
  7462. }
  7463. /// Starts a status effect on the target unit or on the attached player.
  7464. ///
  7465. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7466. BUILDIN_FUNC(sc_start)
  7467. {
  7468. struct block_list* bl;
  7469. enum sc_type type;
  7470. int tick;
  7471. int val1;
  7472. int val4 = 0;
  7473. type = (sc_type)script_getnum(st,2);
  7474. tick = script_getnum(st,3);
  7475. val1 = script_getnum(st,4);
  7476. if( script_hasdata(st,5) )
  7477. bl = map_id2bl(script_getnum(st,5));
  7478. else
  7479. bl = map_id2bl(st->rid);
  7480. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7481. {// When there isn't a duration specified, try to get it from the skill_db
  7482. tick = skill_get_time(status_sc2skill(type), val1);
  7483. }
  7484. if( potion_flag == 1 && potion_target )
  7485. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7486. bl = map_id2bl(potion_target);
  7487. tick /= 2;// Thrown potions only last half.
  7488. val4 = 1;// Mark that this was a thrown sc_effect
  7489. }
  7490. if( bl )
  7491. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7492. return 0;
  7493. }
  7494. /// Starts a status effect on the target unit or on the attached player.
  7495. ///
  7496. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7497. BUILDIN_FUNC(sc_start2)
  7498. {
  7499. struct block_list* bl;
  7500. enum sc_type type;
  7501. int tick;
  7502. int val1;
  7503. int val4 = 0;
  7504. int rate;
  7505. type = (sc_type)script_getnum(st,2);
  7506. tick = script_getnum(st,3);
  7507. val1 = script_getnum(st,4);
  7508. rate = script_getnum(st,5);
  7509. if( script_hasdata(st,6) )
  7510. bl = map_id2bl(script_getnum(st,6));
  7511. else
  7512. bl = map_id2bl(st->rid);
  7513. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7514. {// When there isn't a duration specified, try to get it from the skill_db
  7515. tick = skill_get_time(status_sc2skill(type), val1);
  7516. }
  7517. if( potion_flag == 1 && potion_target )
  7518. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7519. bl = map_id2bl(potion_target);
  7520. tick /= 2;// Thrown potions only last half.
  7521. val4 = 1;// Mark that this was a thrown sc_effect
  7522. }
  7523. if( bl )
  7524. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7525. return 0;
  7526. }
  7527. /// Starts a status effect on the target unit or on the attached player.
  7528. ///
  7529. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7530. BUILDIN_FUNC(sc_start4)
  7531. {
  7532. struct block_list* bl;
  7533. enum sc_type type;
  7534. int tick;
  7535. int val1;
  7536. int val2;
  7537. int val3;
  7538. int val4;
  7539. type = (sc_type)script_getnum(st,2);
  7540. tick = script_getnum(st,3);
  7541. val1 = script_getnum(st,4);
  7542. val2 = script_getnum(st,5);
  7543. val3 = script_getnum(st,6);
  7544. val4 = script_getnum(st,7);
  7545. if( script_hasdata(st,8) )
  7546. bl = map_id2bl(script_getnum(st,8));
  7547. else
  7548. bl = map_id2bl(st->rid);
  7549. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7550. {// When there isn't a duration specified, try to get it from the skill_db
  7551. tick = skill_get_time(status_sc2skill(type), val1);
  7552. }
  7553. if( potion_flag == 1 && potion_target )
  7554. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7555. bl = map_id2bl(potion_target);
  7556. tick /= 2;// Thrown potions only last half.
  7557. }
  7558. if( bl )
  7559. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7560. return 0;
  7561. }
  7562. /// Ends one or all status effects on the target unit or on the attached player.
  7563. ///
  7564. /// sc_end <effect_id>{,<unit_id>};
  7565. BUILDIN_FUNC(sc_end)
  7566. {
  7567. struct block_list* bl;
  7568. int type;
  7569. type = script_getnum(st,2);
  7570. if( script_hasdata(st,3) )
  7571. bl = map_id2bl(script_getnum(st,3));
  7572. else
  7573. bl = map_id2bl(st->rid);
  7574. if( potion_flag==1 && potion_target )
  7575. {//##TODO how does this work [FlavioJS]
  7576. bl = map_id2bl(potion_target);
  7577. }
  7578. if( !bl ) return 0;
  7579. if( type >= 0 && type < SC_MAX )
  7580. {
  7581. struct status_change *sc = status_get_sc(bl);
  7582. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7583. if (!sce) return 0;
  7584. //This should help status_change_end force disabling the SC in case it has no limit.
  7585. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7586. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7587. } else
  7588. status_change_clear(bl, 2);// remove all effects
  7589. return 0;
  7590. }
  7591. /*==========================================
  7592. * 状態異常耐性を計算した確率を返す
  7593. *------------------------------------------*/
  7594. BUILDIN_FUNC(getscrate)
  7595. {
  7596. struct block_list *bl;
  7597. int type,rate;
  7598. type=script_getnum(st,2);
  7599. rate=script_getnum(st,3);
  7600. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7601. bl = map_id2bl(script_getnum(st,4));
  7602. else
  7603. bl = map_id2bl(st->rid);
  7604. if (bl)
  7605. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7606. script_pushint(st,rate);
  7607. return 0;
  7608. }
  7609. /*==========================================
  7610. *
  7611. *------------------------------------------*/
  7612. BUILDIN_FUNC(debugmes)
  7613. {
  7614. const char *str;
  7615. str=script_getstr(st,2);
  7616. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7617. return 0;
  7618. }
  7619. /*==========================================
  7620. *捕獲アイテム使用
  7621. *------------------------------------------*/
  7622. BUILDIN_FUNC(catchpet)
  7623. {
  7624. int pet_id;
  7625. TBL_PC *sd;
  7626. pet_id= script_getnum(st,2);
  7627. sd=script_rid2sd(st);
  7628. if( sd == NULL )
  7629. return 0;
  7630. pet_catch_process1(sd,pet_id);
  7631. return 0;
  7632. }
  7633. /*==========================================
  7634. * [orn]
  7635. *------------------------------------------*/
  7636. BUILDIN_FUNC(homunculus_evolution)
  7637. {
  7638. TBL_PC *sd;
  7639. sd=script_rid2sd(st);
  7640. if( sd == NULL )
  7641. return 0;
  7642. if(merc_is_hom_active(sd->hd))
  7643. {
  7644. if (sd->hd->homunculus.intimacy > 91000)
  7645. merc_hom_evolution(sd->hd);
  7646. else
  7647. clif_emotion(&sd->hd->bl, 4) ; //swt
  7648. }
  7649. return 0;
  7650. }
  7651. // [Zephyrus]
  7652. BUILDIN_FUNC(homunculus_shuffle)
  7653. {
  7654. TBL_PC *sd;
  7655. sd=script_rid2sd(st);
  7656. if( sd == NULL )
  7657. return 0;
  7658. if(merc_is_hom_active(sd->hd))
  7659. merc_hom_shuffle(sd->hd);
  7660. return 0;
  7661. }
  7662. //These two functions bring the eA MAPID_* class functionality to scripts.
  7663. BUILDIN_FUNC(eaclass)
  7664. {
  7665. int class_;
  7666. if( script_hasdata(st,2) )
  7667. class_ = script_getnum(st,2);
  7668. else {
  7669. TBL_PC *sd;
  7670. sd=script_rid2sd(st);
  7671. if (!sd) {
  7672. script_pushint(st,-1);
  7673. return 0;
  7674. }
  7675. class_ = sd->status.class_;
  7676. }
  7677. script_pushint(st,pc_jobid2mapid(class_));
  7678. return 0;
  7679. }
  7680. BUILDIN_FUNC(roclass)
  7681. {
  7682. int class_ =script_getnum(st,2);
  7683. int sex;
  7684. if( script_hasdata(st,3) )
  7685. sex = script_getnum(st,3);
  7686. else {
  7687. TBL_PC *sd;
  7688. if (st->rid && (sd=script_rid2sd(st)))
  7689. sex = sd->status.sex;
  7690. else
  7691. sex = 1; //Just use male when not found.
  7692. }
  7693. script_pushint(st,pc_mapid2jobid(class_, sex));
  7694. return 0;
  7695. }
  7696. /*==========================================
  7697. *携帯卵孵化機使用
  7698. *------------------------------------------*/
  7699. BUILDIN_FUNC(birthpet)
  7700. {
  7701. TBL_PC *sd;
  7702. sd=script_rid2sd(st);
  7703. if( sd == NULL )
  7704. return 0;
  7705. clif_sendegg(sd);
  7706. return 0;
  7707. }
  7708. /*==========================================
  7709. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7710. *------------------------------------------*/
  7711. BUILDIN_FUNC(resetlvl)
  7712. {
  7713. TBL_PC *sd;
  7714. int type=script_getnum(st,2);
  7715. sd=script_rid2sd(st);
  7716. if( sd == NULL )
  7717. return 0;
  7718. pc_resetlvl(sd,type);
  7719. return 0;
  7720. }
  7721. /*==========================================
  7722. * ステータスリセット
  7723. *------------------------------------------*/
  7724. BUILDIN_FUNC(resetstatus)
  7725. {
  7726. TBL_PC *sd;
  7727. sd=script_rid2sd(st);
  7728. pc_resetstate(sd);
  7729. return 0;
  7730. }
  7731. /*==========================================
  7732. * script command resetskill
  7733. *------------------------------------------*/
  7734. BUILDIN_FUNC(resetskill)
  7735. {
  7736. TBL_PC *sd;
  7737. sd=script_rid2sd(st);
  7738. pc_resetskill(sd,1);
  7739. return 0;
  7740. }
  7741. /*==========================================
  7742. * Counts total amount of skill points.
  7743. *------------------------------------------*/
  7744. BUILDIN_FUNC(skillpointcount)
  7745. {
  7746. TBL_PC *sd;
  7747. sd=script_rid2sd(st);
  7748. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7749. return 0;
  7750. }
  7751. /*==========================================
  7752. *
  7753. *------------------------------------------*/
  7754. BUILDIN_FUNC(changebase)
  7755. {
  7756. TBL_PC *sd=NULL;
  7757. int vclass;
  7758. if( script_hasdata(st,3) )
  7759. sd=map_id2sd(script_getnum(st,3));
  7760. else
  7761. sd=script_rid2sd(st);
  7762. if(sd == NULL)
  7763. return 0;
  7764. vclass = script_getnum(st,2);
  7765. if(vclass == JOB_WEDDING)
  7766. {
  7767. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7768. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7769. )
  7770. return 0;
  7771. }
  7772. if(!sd->disguise && vclass != sd->vd.class_) {
  7773. status_set_viewdata(&sd->bl, vclass);
  7774. //Updated client view. Base, Weapon and Cloth Colors.
  7775. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7776. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7777. if (sd->vd.cloth_color)
  7778. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7779. clif_skillinfoblock(sd);
  7780. }
  7781. return 0;
  7782. }
  7783. /*==========================================
  7784. * 性別変換
  7785. *------------------------------------------*/
  7786. BUILDIN_FUNC(changesex)
  7787. {
  7788. TBL_PC *sd = NULL;
  7789. sd = script_rid2sd(st);
  7790. chrif_changesex(sd);
  7791. return 0;
  7792. }
  7793. /*==========================================
  7794. * Works like 'announce' but outputs in the common chat window
  7795. *------------------------------------------*/
  7796. BUILDIN_FUNC(globalmes)
  7797. {
  7798. struct block_list *bl = map_id2bl(st->oid);
  7799. struct npc_data *nd = (struct npc_data *)bl;
  7800. const char *name=NULL,*mes;
  7801. mes=script_getstr(st,2); // メッセージの取得
  7802. if(mes==NULL) return 0;
  7803. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7804. name=script_getstr(st,3);
  7805. } else {
  7806. name=nd->name;
  7807. }
  7808. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7809. return 0;
  7810. }
  7811. /////////////////////////////////////////////////////////////////////
  7812. // NPC waiting room (chat room)
  7813. //
  7814. /// Creates a waiting room (chat room) for this npc.
  7815. ///
  7816. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7817. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7818. /// waitingroom "<title>",<limit>,"<event>";
  7819. /// waitingroom "<title>",<limit>;
  7820. BUILDIN_FUNC(waitingroom)
  7821. {
  7822. struct npc_data* nd;
  7823. const char* title;
  7824. const char* ev = "";
  7825. int limit;
  7826. int trigger = 0;
  7827. int pub = 1;
  7828. title = script_getstr(st, 2);
  7829. limit = script_getnum(st, 3);
  7830. if( script_hasdata(st,5) )
  7831. {
  7832. struct script_data* last = script_getdata(st, 5);
  7833. get_val(st, last);
  7834. if( data_isstring(last) )
  7835. {// ,<trigger>,"<event>"
  7836. trigger = script_getnum(st, 4);
  7837. ev = script_getstr(st, 5);
  7838. }
  7839. else
  7840. {// ,"<event>",<trigger>
  7841. ev = script_getstr(st, 4);
  7842. trigger = script_getnum(st,5);
  7843. }
  7844. }
  7845. else if( script_hasdata(st,4) )
  7846. {// ,"<event>"
  7847. ev = script_getstr(st, 4);
  7848. trigger = limit;
  7849. }
  7850. nd = (struct npc_data *)map_id2bl(st->oid);
  7851. if( nd != NULL )
  7852. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7853. return 0;
  7854. }
  7855. /// Removes the waiting room of the current or target npc.
  7856. ///
  7857. /// delwaitingroom "<npc_name>";
  7858. /// delwaitingroom;
  7859. BUILDIN_FUNC(delwaitingroom)
  7860. {
  7861. struct npc_data* nd;
  7862. if( script_hasdata(st,2) )
  7863. nd = npc_name2id(script_getstr(st, 2));
  7864. else
  7865. nd = (struct npc_data *)map_id2bl(st->oid);
  7866. if( nd != NULL )
  7867. chat_deletenpcchat(nd);
  7868. return 0;
  7869. }
  7870. /// Kicks all the players from the waiting room of the current or target npc.
  7871. ///
  7872. /// kickwaitingroomall "<npc_name>";
  7873. /// kickwaitingroomall;
  7874. BUILDIN_FUNC(waitingroomkickall)
  7875. {
  7876. struct npc_data* nd;
  7877. struct chat_data* cd;
  7878. if( script_hasdata(st,2) )
  7879. nd = npc_name2id(script_getstr(st,2));
  7880. else
  7881. nd = (struct npc_data *)map_id2bl(st->oid);
  7882. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7883. chat_npckickall(cd);
  7884. return 0;
  7885. }
  7886. /// Enables the waiting room event of the current or target npc.
  7887. ///
  7888. /// enablewaitingroomevent "<npc_name>";
  7889. /// enablewaitingroomevent;
  7890. BUILDIN_FUNC(enablewaitingroomevent)
  7891. {
  7892. struct npc_data* nd;
  7893. struct chat_data* cd;
  7894. if( script_hasdata(st,2) )
  7895. nd = npc_name2id(script_getstr(st, 2));
  7896. else
  7897. nd = (struct npc_data *)map_id2bl(st->oid);
  7898. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7899. chat_enableevent(cd);
  7900. return 0;
  7901. }
  7902. /// Disables the waiting room event of the current or target npc.
  7903. ///
  7904. /// disablewaitingroomevent "<npc_name>";
  7905. /// disablewaitingroomevent;
  7906. BUILDIN_FUNC(disablewaitingroomevent)
  7907. {
  7908. struct npc_data *nd;
  7909. struct chat_data *cd;
  7910. if( script_hasdata(st,2) )
  7911. nd = npc_name2id(script_getstr(st, 2));
  7912. else
  7913. nd = (struct npc_data *)map_id2bl(st->oid);
  7914. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7915. chat_disableevent(cd);
  7916. return 0;
  7917. }
  7918. /// Returns info on the waiting room of the current or target npc.
  7919. /// Returns -1 if the type unknown
  7920. /// <type>=0 : current number of users
  7921. /// <type>=1 : maximum number of users allowed
  7922. /// <type>=2 : the number of users that trigger the event
  7923. /// <type>=3 : if the trigger is disabled
  7924. /// <type>=4 : the title of the waiting room
  7925. /// <type>=5 : the password of the waiting room
  7926. /// <type>=16 : the name of the waiting room event
  7927. /// <type>=32 : if the waiting room is full
  7928. /// <type>=33 : if there are enough users to trigger the event
  7929. ///
  7930. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7931. /// getwaitingroomstate(<type>) -> <info>
  7932. BUILDIN_FUNC(getwaitingroomstate)
  7933. {
  7934. struct npc_data *nd;
  7935. struct chat_data *cd;
  7936. int type;
  7937. type = script_getnum(st,2);
  7938. if( script_hasdata(st,3) )
  7939. nd = npc_name2id(script_getstr(st, 3));
  7940. else
  7941. nd = (struct npc_data *)map_id2bl(st->oid);
  7942. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7943. {
  7944. script_pushint(st, -1);
  7945. return 0;
  7946. }
  7947. switch(type)
  7948. {
  7949. case 0: script_pushint(st, cd->users); break;
  7950. case 1: script_pushint(st, cd->limit); break;
  7951. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7952. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7953. case 4: script_pushstrcopy(st, cd->title); break;
  7954. case 5: script_pushstrcopy(st, cd->pass); break;
  7955. case 16: script_pushstrcopy(st, cd->npc_event);break;
  7956. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7957. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7958. default: script_pushint(st, -1); break;
  7959. }
  7960. return 0;
  7961. }
  7962. /// Warps the trigger or target amount of players to the target map and position.
  7963. /// Players are automatically removed from the waiting room.
  7964. /// Those waiting the longest will get warped first.
  7965. /// The target map can be "Random" for a random position in the current map,
  7966. /// and "SavePoint" for the savepoint map+position.
  7967. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7968. ///
  7969. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7970. /// The total number of teleported players is put into $@warpwaitingpcnum
  7971. ///
  7972. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7973. /// warpwaitingpc "<map name>",<x>,<y>;
  7974. BUILDIN_FUNC(warpwaitingpc)
  7975. {
  7976. int x;
  7977. int y;
  7978. int i;
  7979. int n;
  7980. const char* map_name;
  7981. struct npc_data* nd;
  7982. struct chat_data* cd;
  7983. TBL_PC* sd;
  7984. nd = (struct npc_data *)map_id2bl(st->oid);
  7985. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7986. return 0;
  7987. map_name = script_getstr(st,2);
  7988. x = script_getnum(st,3);
  7989. y = script_getnum(st,4);
  7990. n = cd->trigger&0x7f;
  7991. if( script_hasdata(st,5) )
  7992. n = script_getnum(st,5);
  7993. for( i = 0; i < n && cd->users > 0; i++ )
  7994. {
  7995. sd = cd->usersd[0];
  7996. if( sd == NULL )
  7997. {
  7998. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7999. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  8000. continue;// Broken npc chat room?
  8001. }
  8002. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  8003. if( strcmp(map_name,"Random") == 0 )
  8004. pc_randomwarp(sd,3);
  8005. else if( strcmp(map_name,"SavePoint") == 0 )
  8006. {
  8007. if( map[sd->bl.m].flag.noteleport )
  8008. return 0;// can't teleport on this map
  8009. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  8010. }
  8011. else
  8012. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  8013. }
  8014. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8015. return 0;
  8016. }
  8017. /////////////////////////////////////////////////////////////////////
  8018. // ...
  8019. //
  8020. /*==========================================
  8021. * RIDのアタッチ
  8022. *------------------------------------------*/
  8023. BUILDIN_FUNC(attachrid)
  8024. {
  8025. st->rid=script_getnum(st,2);
  8026. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  8027. return 0;
  8028. }
  8029. /*==========================================
  8030. * RIDのデタッチ
  8031. *------------------------------------------*/
  8032. BUILDIN_FUNC(detachrid)
  8033. {
  8034. st->rid=0;
  8035. return 0;
  8036. }
  8037. /*==========================================
  8038. * 存在チェック
  8039. *------------------------------------------*/
  8040. BUILDIN_FUNC(isloggedin)
  8041. {
  8042. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8043. if (script_hasdata(st,3) && sd &&
  8044. sd->status.char_id != script_getnum(st,3))
  8045. sd = NULL;
  8046. push_val(st->stack,C_INT,sd!=NULL);
  8047. return 0;
  8048. }
  8049. /*==========================================
  8050. *
  8051. *------------------------------------------*/
  8052. BUILDIN_FUNC(setmapflagnosave)
  8053. {
  8054. int m,x,y;
  8055. unsigned short mapindex;
  8056. const char *str,*str2;
  8057. str=script_getstr(st,2);
  8058. str2=script_getstr(st,3);
  8059. x=script_getnum(st,4);
  8060. y=script_getnum(st,5);
  8061. m = map_mapname2mapid(str);
  8062. mapindex = mapindex_name2id(str2);
  8063. if(m >= 0 && mapindex) {
  8064. map[m].flag.nosave=1;
  8065. map[m].save.map=mapindex;
  8066. map[m].save.x=x;
  8067. map[m].save.y=y;
  8068. }
  8069. return 0;
  8070. }
  8071. BUILDIN_FUNC(getmapflag)
  8072. {
  8073. int m,i;
  8074. const char *str;
  8075. str=script_getstr(st,2);
  8076. i=script_getnum(st,3);
  8077. m = map_mapname2mapid(str);
  8078. if(m >= 0) {
  8079. switch(i) {
  8080. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  8081. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  8082. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  8083. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  8084. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  8085. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  8086. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  8087. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  8088. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  8089. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  8090. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  8091. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  8092. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  8093. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  8094. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  8095. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  8096. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  8097. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  8098. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  8099. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  8100. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  8101. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  8102. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  8103. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  8104. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  8105. case MF_INDOORS: script_pushint(st,map[m].flag.indoors); break;
  8106. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  8107. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  8108. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  8109. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  8110. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  8111. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  8112. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  8113. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  8114. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  8115. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  8116. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  8117. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  8118. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  8119. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  8120. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  8121. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  8122. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  8123. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  8124. }
  8125. }
  8126. return 0;
  8127. }
  8128. BUILDIN_FUNC(setmapflag)
  8129. {
  8130. int m,i;
  8131. const char *str;
  8132. const char *val=NULL;
  8133. str=script_getstr(st,2);
  8134. i=script_getnum(st,3);
  8135. if(script_hasdata(st,4)){
  8136. val=script_getstr(st,4);
  8137. }
  8138. m = map_mapname2mapid(str);
  8139. if(m >= 0) {
  8140. switch(i) {
  8141. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  8142. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  8143. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  8144. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  8145. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  8146. case MF_PVP: map[m].flag.pvp=1; break;
  8147. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  8148. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  8149. case MF_GVG: map[m].flag.gvg=1; break;
  8150. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  8151. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  8152. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  8153. case MF_NOTRADE: map[m].flag.notrade=1; break;
  8154. case MF_NODROP: map[m].flag.nodrop=1; break;
  8155. case MF_NOSKILL: map[m].flag.noskill=1; break;
  8156. case MF_NOWARP: map[m].flag.nowarp=1; break;
  8157. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  8158. case MF_SNOW: map[m].flag.snow=1; break;
  8159. case MF_CLOUDS: map[m].flag.clouds=1; break;
  8160. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  8161. case MF_FOG: map[m].flag.fog=1; break;
  8162. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  8163. case MF_SAKURA: map[m].flag.sakura=1; break;
  8164. case MF_LEAVES: map[m].flag.leaves=1; break;
  8165. case MF_RAIN: map[m].flag.rain=1; break;
  8166. case MF_INDOORS: map[m].flag.indoors=1; break;
  8167. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  8168. case MF_NOGO: map[m].flag.nogo=1; break;
  8169. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  8170. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  8171. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  8172. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  8173. case MF_NORETURN: map[m].flag.noreturn=1; break;
  8174. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  8175. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  8176. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  8177. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8178. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8179. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8180. case MF_NOVENDING: map[m].flag.novending=1; break;
  8181. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  8182. case MF_NOCHAT: map[m].flag.nochat=1; break;
  8183. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  8184. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  8185. }
  8186. }
  8187. return 0;
  8188. }
  8189. BUILDIN_FUNC(removemapflag)
  8190. {
  8191. int m,i;
  8192. const char *str;
  8193. str=script_getstr(st,2);
  8194. i=script_getnum(st,3);
  8195. m = map_mapname2mapid(str);
  8196. if(m >= 0) {
  8197. switch(i) {
  8198. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  8199. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  8200. case MF_NOSAVE: map[m].flag.nosave=0; break;
  8201. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  8202. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  8203. case MF_PVP: map[m].flag.pvp=0; break;
  8204. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  8205. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  8206. case MF_GVG: map[m].flag.gvg=0; break;
  8207. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  8208. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  8209. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8210. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8211. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8212. case MF_NODROP: map[m].flag.nodrop=0; break;
  8213. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8214. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8215. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8216. case MF_SNOW: map[m].flag.snow=0; break;
  8217. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8218. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8219. case MF_FOG: map[m].flag.fog=0; break;
  8220. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8221. case MF_SAKURA: map[m].flag.sakura=0; break;
  8222. case MF_LEAVES: map[m].flag.leaves=0; break;
  8223. case MF_RAIN: map[m].flag.rain=0; break;
  8224. case MF_INDOORS: map[m].flag.indoors=0; break;
  8225. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8226. case MF_NOGO: map[m].flag.nogo=0; break;
  8227. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8228. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8229. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8230. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8231. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8232. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8233. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8234. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8235. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8236. case MF_JEXP: map[m].jexp=100; break;
  8237. case MF_BEXP: map[m].bexp=100; break;
  8238. case MF_NOVENDING: map[m].flag.novending=0; break;
  8239. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8240. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8241. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8242. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8243. }
  8244. }
  8245. return 0;
  8246. }
  8247. BUILDIN_FUNC(pvpon)
  8248. {
  8249. int m;
  8250. const char *str;
  8251. TBL_PC* sd = NULL;
  8252. struct s_mapiterator* iter;
  8253. str = script_getstr(st,2);
  8254. m = map_mapname2mapid(str);
  8255. if( m < 0 || map[m].flag.pvp )
  8256. return 0; // nothing to do
  8257. map[m].flag.pvp = 1;
  8258. clif_send0199(m,1);
  8259. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8260. return 0;
  8261. iter = mapit_getallusers();
  8262. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8263. {
  8264. if( sd->bl.m != m || sd->pvp_timer != -1 )
  8265. continue; // not applicable
  8266. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8267. sd->pvp_rank = 0;
  8268. sd->pvp_lastusers = 0;
  8269. sd->pvp_point = 5;
  8270. sd->pvp_won = 0;
  8271. sd->pvp_lost = 0;
  8272. }
  8273. mapit_free(iter);
  8274. return 0;
  8275. }
  8276. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8277. {
  8278. TBL_PC* sd = (TBL_PC*)bl;
  8279. clif_pvpset(sd, 0, 0, 2);
  8280. if (sd->pvp_timer != -1) {
  8281. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8282. sd->pvp_timer = INVALID_TIMER;
  8283. }
  8284. return 0;
  8285. }
  8286. BUILDIN_FUNC(pvpoff)
  8287. {
  8288. int m;
  8289. const char *str;
  8290. str=script_getstr(st,2);
  8291. m = map_mapname2mapid(str);
  8292. if(m < 0 || !map[m].flag.pvp)
  8293. return 0; //fixed Lupus
  8294. map[m].flag.pvp = 0;
  8295. clif_send0199(m,0);
  8296. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8297. return 0;
  8298. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8299. return 0;
  8300. }
  8301. BUILDIN_FUNC(gvgon)
  8302. {
  8303. int m;
  8304. const char *str;
  8305. str=script_getstr(st,2);
  8306. m = map_mapname2mapid(str);
  8307. if(m >= 0 && !map[m].flag.gvg) {
  8308. map[m].flag.gvg = 1;
  8309. clif_send0199(m,3);
  8310. }
  8311. return 0;
  8312. }
  8313. BUILDIN_FUNC(gvgoff)
  8314. {
  8315. int m;
  8316. const char *str;
  8317. str=script_getstr(st,2);
  8318. m = map_mapname2mapid(str);
  8319. if(m >= 0 && map[m].flag.gvg) {
  8320. map[m].flag.gvg = 0;
  8321. clif_send0199(m,0);
  8322. }
  8323. return 0;
  8324. }
  8325. /*==========================================
  8326. * Shows an emoticon on top of the player/npc
  8327. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8328. *------------------------------------------*/
  8329. //Optional second parameter added by [Skotlex]
  8330. BUILDIN_FUNC(emotion)
  8331. {
  8332. int type;
  8333. int player=0;
  8334. type=script_getnum(st,2);
  8335. if(type < 0 || type > 100)
  8336. return 0;
  8337. if( script_hasdata(st,3) )
  8338. player=script_getnum(st,3);
  8339. if (player) {
  8340. TBL_PC *sd = script_rid2sd(st);
  8341. if (sd)
  8342. clif_emotion(&sd->bl,type);
  8343. } else
  8344. clif_emotion(map_id2bl(st->oid),type);
  8345. return 0;
  8346. }
  8347. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8348. {
  8349. int m=va_arg(ap,int);
  8350. int g_id=va_arg(ap,int);
  8351. int flag=va_arg(ap,int);
  8352. if(!sd || sd->bl.m != m)
  8353. return 0;
  8354. if(
  8355. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8356. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8357. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8358. )
  8359. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8360. return 1;
  8361. }
  8362. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8363. {
  8364. struct mob_data *md=(struct mob_data *)bl;
  8365. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8366. status_kill(bl);
  8367. return 0;
  8368. }
  8369. BUILDIN_FUNC(maprespawnguildid)
  8370. {
  8371. const char *mapname=script_getstr(st,2);
  8372. int g_id=script_getnum(st,3);
  8373. int flag=script_getnum(st,4);
  8374. int m=map_mapname2mapid(mapname);
  8375. if(m == -1)
  8376. return 0;
  8377. //Catch ALL players (in case some are 'between maps' on execution time)
  8378. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8379. if (flag&4) //Remove script mobs.
  8380. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8381. return 0;
  8382. }
  8383. BUILDIN_FUNC(agitstart)
  8384. {
  8385. if(agit_flag==1) return 0; // Agit already Start.
  8386. agit_flag=1;
  8387. guild_agit_start();
  8388. return 0;
  8389. }
  8390. BUILDIN_FUNC(agitend)
  8391. {
  8392. if(agit_flag==0) return 0; // Agit already End.
  8393. agit_flag=0;
  8394. guild_agit_end();
  8395. return 0;
  8396. }
  8397. BUILDIN_FUNC(agitstart2)
  8398. {
  8399. if(agit2_flag==1) return 0; // Agit2 already Start.
  8400. agit2_flag=1;
  8401. guild_agit2_start();
  8402. return 0;
  8403. }
  8404. BUILDIN_FUNC(agitend2)
  8405. {
  8406. if(agit2_flag==0) return 0; // Agit2 already End.
  8407. agit2_flag=0;
  8408. guild_agit2_end();
  8409. return 0;
  8410. }
  8411. /*==========================================
  8412. * Returns whether woe is on or off. // choice script
  8413. *------------------------------------------*/
  8414. BUILDIN_FUNC(agitcheck)
  8415. {
  8416. script_pushint(st,agit_flag);
  8417. return 0;
  8418. }
  8419. /*==========================================
  8420. * Returns whether woese is on or off. // choice script
  8421. *------------------------------------------*/
  8422. BUILDIN_FUNC(agitcheck2)
  8423. {
  8424. script_pushint(st,agit2_flag);
  8425. return 0;
  8426. }
  8427. /// Sets the guild_id of this npc.
  8428. ///
  8429. /// flagemblem <guild_id>;
  8430. BUILDIN_FUNC(flagemblem)
  8431. {
  8432. TBL_NPC* nd;
  8433. int g_id=script_getnum(st,2);
  8434. if(g_id < 0) return 0;
  8435. nd = (TBL_NPC*)map_id2nd(st->oid);
  8436. if( nd == NULL )
  8437. {
  8438. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8439. }
  8440. else if( nd->subtype != SCRIPT )
  8441. {
  8442. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8443. }
  8444. else
  8445. {
  8446. nd->u.scr.guild_id = g_id;
  8447. clif_guild_emblem_area(&nd->bl);
  8448. }
  8449. return 0;
  8450. }
  8451. BUILDIN_FUNC(getcastlename)
  8452. {
  8453. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8454. struct guild_castle* gc = guild_mapname2gc(mapname);
  8455. const char* name = (gc) ? gc->castle_name : "";
  8456. script_pushstrcopy(st,name);
  8457. return 0;
  8458. }
  8459. BUILDIN_FUNC(getcastledata)
  8460. {
  8461. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8462. int index = script_getnum(st,3);
  8463. struct guild_castle* gc = guild_mapname2gc(mapname);
  8464. if(script_hasdata(st,4) && index==0 && gc) {
  8465. const char* event = script_getstr(st,4);
  8466. check_event(st, event);
  8467. guild_addcastleinfoevent(gc->castle_id,17,event);
  8468. }
  8469. if(gc){
  8470. switch(index){
  8471. case 0: {
  8472. int i;
  8473. for(i=1;i<18;i++) // Initialize[AgitInit]
  8474. guild_castledataload(gc->castle_id,i);
  8475. } break;
  8476. case 1:
  8477. script_pushint(st,gc->guild_id); break;
  8478. case 2:
  8479. script_pushint(st,gc->economy); break;
  8480. case 3:
  8481. script_pushint(st,gc->defense); break;
  8482. case 4:
  8483. script_pushint(st,gc->triggerE); break;
  8484. case 5:
  8485. script_pushint(st,gc->triggerD); break;
  8486. case 6:
  8487. script_pushint(st,gc->nextTime); break;
  8488. case 7:
  8489. script_pushint(st,gc->payTime); break;
  8490. case 8:
  8491. script_pushint(st,gc->createTime); break;
  8492. case 9:
  8493. script_pushint(st,gc->visibleC); break;
  8494. case 10:
  8495. case 11:
  8496. case 12:
  8497. case 13:
  8498. case 14:
  8499. case 15:
  8500. case 16:
  8501. case 17:
  8502. script_pushint(st,gc->guardian[index-10].visible); break;
  8503. default:
  8504. script_pushint(st,0); break;
  8505. }
  8506. return 0;
  8507. }
  8508. script_pushint(st,0);
  8509. return 0;
  8510. }
  8511. BUILDIN_FUNC(setcastledata)
  8512. {
  8513. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8514. int index = script_getnum(st,3);
  8515. int value = script_getnum(st,4);
  8516. struct guild_castle* gc = guild_mapname2gc(mapname);
  8517. if(gc) {
  8518. // Save Data byself First
  8519. switch(index){
  8520. case 1:
  8521. gc->guild_id = value; break;
  8522. case 2:
  8523. gc->economy = value; break;
  8524. case 3:
  8525. gc->defense = value; break;
  8526. case 4:
  8527. gc->triggerE = value; break;
  8528. case 5:
  8529. gc->triggerD = value; break;
  8530. case 6:
  8531. gc->nextTime = value; break;
  8532. case 7:
  8533. gc->payTime = value; break;
  8534. case 8:
  8535. gc->createTime = value; break;
  8536. case 9:
  8537. gc->visibleC = value; break;
  8538. case 10:
  8539. case 11:
  8540. case 12:
  8541. case 13:
  8542. case 14:
  8543. case 15:
  8544. case 16:
  8545. case 17:
  8546. gc->guardian[index-10].visible = value; break;
  8547. default:
  8548. return 0;
  8549. }
  8550. guild_castledatasave(gc->castle_id,index,value);
  8551. }
  8552. return 0;
  8553. }
  8554. /* =====================================================================
  8555. * ギルド情報を要求する
  8556. * ---------------------------------------------------------------------*/
  8557. BUILDIN_FUNC(requestguildinfo)
  8558. {
  8559. int guild_id=script_getnum(st,2);
  8560. const char *event=NULL;
  8561. if( script_hasdata(st,3) ){
  8562. event=script_getstr(st,3);
  8563. check_event(st, event);
  8564. }
  8565. if(guild_id>0)
  8566. guild_npc_request_info(guild_id,event);
  8567. return 0;
  8568. }
  8569. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8570. /// getequipcardcnt(<equipment slot>);
  8571. BUILDIN_FUNC(getequipcardcnt)
  8572. {
  8573. int i=-1,j,num;
  8574. TBL_PC *sd;
  8575. int count;
  8576. num=script_getnum(st,2);
  8577. sd=script_rid2sd(st);
  8578. if (num > 0 && num <= ARRAYLENGTH(equip))
  8579. i=pc_checkequip(sd,equip[num-1]);
  8580. if (i < 0 || !sd->inventory_data[i]) {
  8581. script_pushint(st,0);
  8582. return 0;
  8583. }
  8584. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8585. {
  8586. script_pushint(st,0);
  8587. return 0;
  8588. }
  8589. count = 0;
  8590. for( j = 0; j < sd->inventory_data[i]->slot; j++ )
  8591. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8592. count++;
  8593. script_pushint(st,count);
  8594. return 0;
  8595. }
  8596. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8597. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8598. /// successremovecards <slot>;
  8599. BUILDIN_FUNC(successremovecards)
  8600. {
  8601. int i=-1,j,c,cardflag=0;
  8602. TBL_PC* sd = script_rid2sd(st);
  8603. int num = script_getnum(st,2);
  8604. if (num > 0 && num <= ARRAYLENGTH(equip))
  8605. i=pc_checkequip(sd,equip[num-1]);
  8606. if (i < 0 || !sd->inventory_data[i]) {
  8607. return 0;
  8608. }
  8609. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8610. return 0;
  8611. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  8612. {
  8613. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8614. {// extract this card from the item
  8615. int flag;
  8616. struct item item_tmp;
  8617. cardflag = 1;
  8618. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8619. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8620. item_tmp.attribute=0,item_tmp.expire_time=0;
  8621. for (j = 0; j < MAX_SLOTS; j++)
  8622. item_tmp.card[j]=0;
  8623. //Logs items, got from (N)PC scripts [Lupus]
  8624. if(log_config.enable_logs&0x40)
  8625. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8626. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8627. clif_additem(sd,0,0,flag);
  8628. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8629. }
  8630. }
  8631. }
  8632. if(cardflag == 1)
  8633. { // カードを取り除いたアイテム所得
  8634. int flag;
  8635. struct item item_tmp;
  8636. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8637. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8638. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  8639. for (j = 0; j < MAX_SLOTS; j++)
  8640. item_tmp.card[j]=0;
  8641. //Logs items, got from (N)PC scripts [Lupus]
  8642. if(log_config.enable_logs&0x40)
  8643. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8644. pc_delitem(sd,i,1,0);
  8645. //Logs items, got from (N)PC scripts [Lupus]
  8646. if(log_config.enable_logs&0x40)
  8647. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8648. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8649. clif_additem(sd,0,0,flag);
  8650. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8651. }
  8652. clif_misceffect(&sd->bl,3);
  8653. }
  8654. return 0;
  8655. }
  8656. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8657. /// failedremovecards <slot>, <type>;
  8658. /// <type>=0 : will destroy both the item and the cards.
  8659. /// <type>=1 : will keep the item, but destroy the cards.
  8660. /// <type>=2 : will keep the cards, but destroy the item.
  8661. /// <type>=? : will just display the failure effect.
  8662. BUILDIN_FUNC(failedremovecards)
  8663. {
  8664. int i=-1,j,c,cardflag=0;
  8665. TBL_PC* sd = script_rid2sd(st);
  8666. int num = script_getnum(st,2);
  8667. int typefail = script_getnum(st,3);
  8668. if (num > 0 && num <= ARRAYLENGTH(equip))
  8669. i=pc_checkequip(sd,equip[num-1]);
  8670. if (i < 0 || !sd->inventory_data[i])
  8671. return 0;
  8672. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8673. return 0;
  8674. for( c = sd->inventory_data[i]->slot - 1; c >= 0; --c )
  8675. {
  8676. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8677. {
  8678. cardflag = 1;
  8679. if(typefail == 2)
  8680. {// add cards to inventory, clear
  8681. int flag;
  8682. struct item item_tmp;
  8683. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8684. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8685. item_tmp.attribute=0,item_tmp.expire_time=0;
  8686. for (j = 0; j < MAX_SLOTS; j++)
  8687. item_tmp.card[j]=0;
  8688. //Logs items, got from (N)PC scripts [Lupus]
  8689. if(log_config.enable_logs&0x40)
  8690. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8691. if((flag=pc_additem(sd,&item_tmp,1))){
  8692. clif_additem(sd,0,0,flag);
  8693. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8694. }
  8695. }
  8696. }
  8697. }
  8698. if(cardflag == 1)
  8699. {
  8700. if(typefail == 0 || typefail == 2){ // 武具損失
  8701. //Logs items, got from (N)PC scripts [Lupus]
  8702. if(log_config.enable_logs&0x40)
  8703. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8704. pc_delitem(sd,i,1,0);
  8705. }
  8706. if(typefail == 1){ // カードのみ損失(武具を返す)
  8707. int flag;
  8708. struct item item_tmp;
  8709. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8710. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8711. item_tmp.attribute=sd->status.inventory[i].attribute,item_tmp.expire_time=sd->status.inventory[i].expire_time;
  8712. //Logs items, got from (N)PC scripts [Lupus]
  8713. if(log_config.enable_logs&0x40)
  8714. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8715. for (j = 0; j < MAX_SLOTS; j++)
  8716. item_tmp.card[j]=0;
  8717. pc_delitem(sd,i,1,0);
  8718. //Logs items, got from (N)PC scripts [Lupus]
  8719. if(log_config.enable_logs&0x40)
  8720. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8721. if((flag=pc_additem(sd,&item_tmp,1))){
  8722. clif_additem(sd,0,0,flag);
  8723. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8724. }
  8725. }
  8726. clif_misceffect(&sd->bl,2);
  8727. }
  8728. return 0;
  8729. }
  8730. /* ================================================================
  8731. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8732. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8733. * improved by [Lance]
  8734. * ================================================================*/
  8735. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8736. {
  8737. int x,y,m,check_val=0,check_ID=0,i=0;
  8738. struct guild *g = NULL;
  8739. struct party_data *p = NULL;
  8740. const char *str;
  8741. const char *mapname;
  8742. unsigned int index;
  8743. mapname=script_getstr(st,2);
  8744. str=script_getstr(st,3);
  8745. x=script_getnum(st,4);
  8746. y=script_getnum(st,5);
  8747. if(script_hasdata(st,7)){
  8748. check_val=script_getnum(st,6);
  8749. check_ID=script_getnum(st,7);
  8750. }
  8751. if((m=map_mapname2mapid(mapname))< 0)
  8752. return 0;
  8753. if(!(index=mapindex_name2id(str)))
  8754. return 0;
  8755. switch(check_val){
  8756. case 1:
  8757. g = guild_search(check_ID);
  8758. if (g){
  8759. for( i=0; i < g->max_member; i++)
  8760. {
  8761. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8762. pc_setpos(g->member[i].sd,index,x,y,3);
  8763. }
  8764. }
  8765. }
  8766. break;
  8767. case 2:
  8768. p = party_search(check_ID);
  8769. if(p){
  8770. for(i=0;i<MAX_PARTY; i++){
  8771. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8772. pc_setpos(p->data[i].sd,index,x,y,3);
  8773. }
  8774. }
  8775. }
  8776. break;
  8777. default:
  8778. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8779. break;
  8780. }
  8781. return 0;
  8782. }
  8783. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8784. {
  8785. char *event=va_arg(ap,char *);
  8786. struct mob_data *md = ((struct mob_data *)bl);
  8787. if(strcmp(event,md->npc_event)==0 && md->status.hp > 0)
  8788. return 1;
  8789. return 0;
  8790. }
  8791. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8792. {
  8793. const char *mapname,*event;
  8794. int m;
  8795. mapname=script_getstr(st,2);
  8796. event=script_getstr(st,3);
  8797. check_event(st, event);
  8798. if( (m = map_mapname2mapid(mapname)) < 0 ) {
  8799. script_pushint(st,-1);
  8800. return 0;
  8801. }
  8802. if( map[m].flag.src4instance && map[m].instance_id == 0 && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  8803. {
  8804. script_pushint(st,-1);
  8805. return 0;
  8806. }
  8807. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8808. return 0;
  8809. }
  8810. BUILDIN_FUNC(marriage)
  8811. {
  8812. const char *partner=script_getstr(st,2);
  8813. TBL_PC *sd=script_rid2sd(st);
  8814. TBL_PC *p_sd=map_nick2sd(partner);
  8815. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8816. script_pushint(st,0);
  8817. return 0;
  8818. }
  8819. script_pushint(st,1);
  8820. return 0;
  8821. }
  8822. BUILDIN_FUNC(wedding_effect)
  8823. {
  8824. TBL_PC *sd=script_rid2sd(st);
  8825. struct block_list *bl;
  8826. if(sd==NULL) {
  8827. bl=map_id2bl(st->oid);
  8828. } else
  8829. bl=&sd->bl;
  8830. clif_wedding_effect(bl);
  8831. return 0;
  8832. }
  8833. BUILDIN_FUNC(divorce)
  8834. {
  8835. TBL_PC *sd=script_rid2sd(st);
  8836. if(sd==NULL || pc_divorce(sd) < 0){
  8837. script_pushint(st,0);
  8838. return 0;
  8839. }
  8840. script_pushint(st,1);
  8841. return 0;
  8842. }
  8843. BUILDIN_FUNC(ispartneron)
  8844. {
  8845. TBL_PC *sd=script_rid2sd(st);
  8846. TBL_PC *p_sd=NULL;
  8847. if(sd==NULL || !pc_ismarried(sd) ||
  8848. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8849. script_pushint(st,0);
  8850. return 0;
  8851. }
  8852. script_pushint(st,1);
  8853. return 0;
  8854. }
  8855. BUILDIN_FUNC(getpartnerid)
  8856. {
  8857. TBL_PC *sd=script_rid2sd(st);
  8858. if (sd == NULL) {
  8859. script_pushint(st,0);
  8860. return 0;
  8861. }
  8862. script_pushint(st,sd->status.partner_id);
  8863. return 0;
  8864. }
  8865. BUILDIN_FUNC(getchildid)
  8866. {
  8867. TBL_PC *sd=script_rid2sd(st);
  8868. if (sd == NULL) {
  8869. script_pushint(st,0);
  8870. return 0;
  8871. }
  8872. script_pushint(st,sd->status.child);
  8873. return 0;
  8874. }
  8875. BUILDIN_FUNC(getmotherid)
  8876. {
  8877. TBL_PC *sd=script_rid2sd(st);
  8878. if (sd == NULL) {
  8879. script_pushint(st,0);
  8880. return 0;
  8881. }
  8882. script_pushint(st,sd->status.mother);
  8883. return 0;
  8884. }
  8885. BUILDIN_FUNC(getfatherid)
  8886. {
  8887. TBL_PC *sd=script_rid2sd(st);
  8888. if (sd == NULL) {
  8889. script_pushint(st,0);
  8890. return 0;
  8891. }
  8892. script_pushint(st,sd->status.father);
  8893. return 0;
  8894. }
  8895. BUILDIN_FUNC(warppartner)
  8896. {
  8897. int x,y;
  8898. unsigned short mapindex;
  8899. const char *str;
  8900. TBL_PC *sd=script_rid2sd(st);
  8901. TBL_PC *p_sd=NULL;
  8902. if(sd==NULL || !pc_ismarried(sd) ||
  8903. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8904. script_pushint(st,0);
  8905. return 0;
  8906. }
  8907. str=script_getstr(st,2);
  8908. x=script_getnum(st,3);
  8909. y=script_getnum(st,4);
  8910. mapindex = mapindex_name2id(str);
  8911. if (mapindex) {
  8912. pc_setpos(p_sd,mapindex,x,y,0);
  8913. script_pushint(st,1);
  8914. } else
  8915. script_pushint(st,0);
  8916. return 0;
  8917. }
  8918. /*================================================
  8919. * Script for Displaying MOB Information [Valaris]
  8920. *------------------------------------------------*/
  8921. BUILDIN_FUNC(strmobinfo)
  8922. {
  8923. int num=script_getnum(st,2);
  8924. int class_=script_getnum(st,3);
  8925. if(!mobdb_checkid(class_))
  8926. {
  8927. script_pushint(st,0);
  8928. return 0;
  8929. }
  8930. switch (num) {
  8931. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  8932. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  8933. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8934. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8935. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8936. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8937. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8938. default:
  8939. script_pushint(st,0);
  8940. break;
  8941. }
  8942. return 0;
  8943. }
  8944. /*==========================================
  8945. * Summon guardians [Valaris]
  8946. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  8947. *------------------------------------------*/
  8948. BUILDIN_FUNC(guardian)
  8949. {
  8950. int class_=0,x=0,y=0,guardian=0;
  8951. const char *str,*map,*evt="";
  8952. struct script_data *data;
  8953. bool has_index = false;
  8954. map =script_getstr(st,2);
  8955. x =script_getnum(st,3);
  8956. y =script_getnum(st,4);
  8957. str =script_getstr(st,5);
  8958. class_=script_getnum(st,6);
  8959. if( script_hasdata(st,8) )
  8960. {// "<event label>",<guardian index>
  8961. evt=script_getstr(st,7);
  8962. guardian=script_getnum(st,8);
  8963. has_index = true;
  8964. } else if( script_hasdata(st,7) ){
  8965. data=script_getdata(st,7);
  8966. get_val(st,data);
  8967. if( data_isstring(data) )
  8968. {// "<event label>"
  8969. evt=script_getstr(st,7);
  8970. } else if( data_isint(data) )
  8971. {// <guardian index>
  8972. guardian=script_getnum(st,7);
  8973. has_index = true;
  8974. } else {
  8975. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8976. script_reportdata(data);
  8977. return 1;
  8978. }
  8979. }
  8980. check_event(st, evt);
  8981. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  8982. return 0;
  8983. }
  8984. /*==========================================
  8985. * Invisible Walls [Zephyrus]
  8986. *------------------------------------------*/
  8987. BUILDIN_FUNC(setwall)
  8988. {
  8989. const char *map, *name;
  8990. int x, y, m, size, dir;
  8991. bool shootable;
  8992. map = script_getstr(st,2);
  8993. x = script_getnum(st,3);
  8994. y = script_getnum(st,4);
  8995. size = script_getnum(st,5);
  8996. dir = script_getnum(st,6);
  8997. shootable = script_getnum(st,7);
  8998. name = script_getstr(st,8);
  8999. if( (m = map_mapname2mapid(map)) < 0 )
  9000. return 0; // Invalid Map
  9001. map_iwall_set(m, x, y, size, dir, shootable, name);
  9002. return 0;
  9003. }
  9004. BUILDIN_FUNC(delwall)
  9005. {
  9006. const char *name = script_getstr(st,2);
  9007. map_iwall_remove(name);
  9008. return 0;
  9009. }
  9010. /// Retrieves various information about the specified guardian.
  9011. ///
  9012. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  9013. /// type: 0 - whether it is deployed or not
  9014. /// 1 - maximum hp
  9015. /// 2 - current hp
  9016. ///
  9017. BUILDIN_FUNC(guardianinfo)
  9018. {
  9019. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  9020. int id = script_getnum(st,3);
  9021. int type = script_getnum(st,4);
  9022. struct guild_castle* gc = guild_mapname2gc(mapname);
  9023. struct mob_data* gd;
  9024. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  9025. {
  9026. script_pushint(st,-1);
  9027. return 0;
  9028. }
  9029. if( type == 0 )
  9030. script_pushint(st, gc->guardian[id].visible);
  9031. else
  9032. if( !gc->guardian[id].visible )
  9033. script_pushint(st,-1);
  9034. else
  9035. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  9036. script_pushint(st,-1);
  9037. else
  9038. {
  9039. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  9040. else if( type == 2 ) script_pushint(st,gd->status.hp);
  9041. else
  9042. script_pushint(st,-1);
  9043. }
  9044. return 0;
  9045. }
  9046. /*==========================================
  9047. * IDからItem名
  9048. *------------------------------------------*/
  9049. BUILDIN_FUNC(getitemname)
  9050. {
  9051. int item_id=0;
  9052. struct item_data *i_data;
  9053. char *item_name;
  9054. struct script_data *data;
  9055. data=script_getdata(st,2);
  9056. get_val(st,data);
  9057. if( data_isstring(data) ){
  9058. const char *name=conv_str(st,data);
  9059. struct item_data *item_data = itemdb_searchname(name);
  9060. if( item_data )
  9061. item_id=item_data->nameid;
  9062. }else
  9063. item_id=conv_num(st,data);
  9064. i_data = itemdb_exists(item_id);
  9065. if (i_data == NULL)
  9066. {
  9067. script_pushconststr(st,"null");
  9068. return 0;
  9069. }
  9070. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9071. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9072. script_pushstr(st,item_name);
  9073. return 0;
  9074. }
  9075. /*==========================================
  9076. * Returns number of slots an item has. [Skotlex]
  9077. *------------------------------------------*/
  9078. BUILDIN_FUNC(getitemslots)
  9079. {
  9080. int item_id;
  9081. struct item_data *i_data;
  9082. item_id=script_getnum(st,2);
  9083. i_data = itemdb_exists(item_id);
  9084. if (i_data)
  9085. script_pushint(st,i_data->slot);
  9086. else
  9087. script_pushint(st,-1);
  9088. return 0;
  9089. }
  9090. /*==========================================
  9091. * Returns some values of an item [Lupus]
  9092. * Price, Weight, etc...
  9093. getiteminfo(itemID,n), where n
  9094. 0 value_buy;
  9095. 1 value_sell;
  9096. 2 type;
  9097. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9098. if = 0, then monsters don't drop it at all (rare or a quest item)
  9099. if = 10000, then this item is sold in NPC shops only
  9100. 4 sex;
  9101. 5 equip;
  9102. 6 weight;
  9103. 7 atk;
  9104. 8 def;
  9105. 9 range;
  9106. 10 slot;
  9107. 11 look;
  9108. 12 elv;
  9109. 13 wlv;
  9110. 14 view id
  9111. *------------------------------------------*/
  9112. BUILDIN_FUNC(getiteminfo)
  9113. {
  9114. int item_id,n;
  9115. int *item_arr;
  9116. struct item_data *i_data;
  9117. item_id = script_getnum(st,2);
  9118. n = script_getnum(st,3);
  9119. i_data = itemdb_exists(item_id);
  9120. if (i_data && n>=0 && n<14) {
  9121. item_arr = (int*)&i_data->value_buy;
  9122. script_pushint(st,item_arr[n]);
  9123. } else
  9124. script_pushint(st,-1);
  9125. return 0;
  9126. }
  9127. /*==========================================
  9128. * Set some values of an item [Lupus]
  9129. * Price, Weight, etc...
  9130. setiteminfo(itemID,n,Value), where n
  9131. 0 value_buy;
  9132. 1 value_sell;
  9133. 2 type;
  9134. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9135. if = 0, then monsters don't drop it at all (rare or a quest item)
  9136. if = 10000, then this item is sold in NPC shops only
  9137. 4 sex;
  9138. 5 equip;
  9139. 6 weight;
  9140. 7 atk;
  9141. 8 def;
  9142. 9 range;
  9143. 10 slot;
  9144. 11 look;
  9145. 12 elv;
  9146. 13 wlv;
  9147. 14 view id
  9148. * Returns Value or -1 if the wrong field's been set
  9149. *------------------------------------------*/
  9150. BUILDIN_FUNC(setiteminfo)
  9151. {
  9152. int item_id,n,value;
  9153. int *item_arr;
  9154. struct item_data *i_data;
  9155. item_id = script_getnum(st,2);
  9156. n = script_getnum(st,3);
  9157. value = script_getnum(st,4);
  9158. i_data = itemdb_exists(item_id);
  9159. if (i_data && n>=0 && n<14) {
  9160. item_arr = (int*)&i_data->value_buy;
  9161. item_arr[n] = value;
  9162. script_pushint(st,value);
  9163. } else
  9164. script_pushint(st,-1);
  9165. return 0;
  9166. }
  9167. /*==========================================
  9168. * Returns value from equipped item slot n [Lupus]
  9169. getequipcardid(num,slot)
  9170. where
  9171. num = eqip position slot
  9172. slot = 0,1,2,3 (Card Slot N)
  9173. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9174. it's useful when you want to check item cards or if it's signed
  9175. Useful for such quests as "Sign this refined item with players name" etc
  9176. Hat[0] +4 -> Player's Hat[0] +4
  9177. *------------------------------------------*/
  9178. BUILDIN_FUNC(getequipcardid)
  9179. {
  9180. int i=-1,num,slot;
  9181. TBL_PC *sd;
  9182. num=script_getnum(st,2);
  9183. slot=script_getnum(st,3);
  9184. sd=script_rid2sd(st);
  9185. if (num > 0 && num <= ARRAYLENGTH(equip))
  9186. i=pc_checkequip(sd,equip[num-1]);
  9187. if(i >= 0 && slot>=0 && slot<4)
  9188. script_pushint(st,sd->status.inventory[i].card[slot]);
  9189. else
  9190. script_pushint(st,0);
  9191. return 0;
  9192. }
  9193. /*==========================================
  9194. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9195. *------------------------------------------*/
  9196. BUILDIN_FUNC(petskillbonus)
  9197. {
  9198. struct pet_data *pd;
  9199. TBL_PC *sd=script_rid2sd(st);
  9200. if(sd==NULL || sd->pd==NULL)
  9201. return 0;
  9202. pd=sd->pd;
  9203. if (pd->bonus)
  9204. { //Clear previous bonus
  9205. if (pd->bonus->timer != -1)
  9206. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9207. } else //init
  9208. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9209. pd->bonus->type=script_getnum(st,2);
  9210. pd->bonus->val=script_getnum(st,3);
  9211. pd->bonus->duration=script_getnum(st,4);
  9212. pd->bonus->delay=script_getnum(st,5);
  9213. if (pd->state.skillbonus == 1)
  9214. pd->state.skillbonus=0; // waiting state
  9215. // wait for timer to start
  9216. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9217. pd->bonus->timer = INVALID_TIMER;
  9218. else
  9219. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9220. return 0;
  9221. }
  9222. /*==========================================
  9223. * pet looting [Valaris] //Rewritten by [Skotlex]
  9224. *------------------------------------------*/
  9225. BUILDIN_FUNC(petloot)
  9226. {
  9227. int max;
  9228. struct pet_data *pd;
  9229. TBL_PC *sd=script_rid2sd(st);
  9230. if(sd==NULL || sd->pd==NULL)
  9231. return 0;
  9232. max=script_getnum(st,2);
  9233. if(max < 1)
  9234. max = 1; //Let'em loot at least 1 item.
  9235. else if (max > MAX_PETLOOT_SIZE)
  9236. max = MAX_PETLOOT_SIZE;
  9237. pd = sd->pd;
  9238. if (pd->loot != NULL)
  9239. { //Release whatever was there already and reallocate memory
  9240. pet_lootitem_drop(pd, pd->msd);
  9241. aFree(pd->loot->item);
  9242. }
  9243. else
  9244. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9245. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9246. pd->loot->max=max;
  9247. pd->loot->count = 0;
  9248. pd->loot->weight = 0;
  9249. return 0;
  9250. }
  9251. /*==========================================
  9252. * PCの所持品情報読み取り
  9253. *------------------------------------------*/
  9254. BUILDIN_FUNC(getinventorylist)
  9255. {
  9256. TBL_PC *sd=script_rid2sd(st);
  9257. char card_var[NAME_LENGTH];
  9258. int i,j=0,k;
  9259. if(!sd) return 0;
  9260. for(i=0;i<MAX_INVENTORY;i++){
  9261. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9262. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9263. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9264. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9265. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9266. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9267. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9268. for (k = 0; k < MAX_SLOTS; k++)
  9269. {
  9270. sprintf(card_var, "@inventorylist_card%d",k+1);
  9271. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9272. }
  9273. j++;
  9274. }
  9275. }
  9276. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9277. return 0;
  9278. }
  9279. BUILDIN_FUNC(getskilllist)
  9280. {
  9281. TBL_PC *sd=script_rid2sd(st);
  9282. int i,j=0;
  9283. if(!sd) return 0;
  9284. for(i=0;i<MAX_SKILL;i++){
  9285. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9286. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9287. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9288. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9289. j++;
  9290. }
  9291. }
  9292. pc_setreg(sd,add_str("@skilllist_count"),j);
  9293. return 0;
  9294. }
  9295. BUILDIN_FUNC(clearitem)
  9296. {
  9297. TBL_PC *sd=script_rid2sd(st);
  9298. int i;
  9299. if(sd==NULL) return 0;
  9300. for (i=0; i<MAX_INVENTORY; i++) {
  9301. if (sd->status.inventory[i].amount) {
  9302. //Logs items, got from (N)PC scripts [Lupus]
  9303. if(log_config.enable_logs&0x40)
  9304. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9305. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9306. }
  9307. }
  9308. return 0;
  9309. }
  9310. /*==========================================
  9311. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9312. *------------------------------------------*/
  9313. BUILDIN_FUNC(disguise)
  9314. {
  9315. int id;
  9316. TBL_PC* sd = script_rid2sd(st);
  9317. if (sd == NULL) return 0;
  9318. id = script_getnum(st,2);
  9319. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9320. pc_disguise(sd, id);
  9321. script_pushint(st,id);
  9322. } else
  9323. script_pushint(st,0);
  9324. return 0;
  9325. }
  9326. /*==========================================
  9327. * Undisguise Player (returns 1 if success, 0 on fail)
  9328. *------------------------------------------*/
  9329. BUILDIN_FUNC(undisguise)
  9330. {
  9331. TBL_PC* sd = script_rid2sd(st);
  9332. if (sd == NULL) return 0;
  9333. if (sd->disguise) {
  9334. pc_disguise(sd, 0);
  9335. script_pushint(st,0);
  9336. } else {
  9337. script_pushint(st,1);
  9338. }
  9339. return 0;
  9340. }
  9341. /*==========================================
  9342. * NPCクラスチェンジ
  9343. * classは変わりたいclass
  9344. * typeは通常0なのかな?
  9345. *------------------------------------------*/
  9346. BUILDIN_FUNC(classchange)
  9347. {
  9348. int _class,type;
  9349. struct block_list *bl=map_id2bl(st->oid);
  9350. if(bl==NULL) return 0;
  9351. _class=script_getnum(st,2);
  9352. type=script_getnum(st,3);
  9353. clif_class_change(bl,_class,type);
  9354. return 0;
  9355. }
  9356. /*==========================================
  9357. * NPCから発生するエフェクト
  9358. *------------------------------------------*/
  9359. BUILDIN_FUNC(misceffect)
  9360. {
  9361. int type;
  9362. type=script_getnum(st,2);
  9363. if(st->oid && st->oid != fake_nd->bl.id) {
  9364. struct block_list *bl = map_id2bl(st->oid);
  9365. if (bl)
  9366. clif_misceffect2(bl,type);
  9367. } else{
  9368. TBL_PC *sd=script_rid2sd(st);
  9369. if(sd)
  9370. clif_misceffect2(&sd->bl,type);
  9371. }
  9372. return 0;
  9373. }
  9374. /*==========================================
  9375. * サウンドエフェクト
  9376. *------------------------------------------*/
  9377. BUILDIN_FUNC(soundeffect)
  9378. {
  9379. TBL_PC* sd = script_rid2sd(st);
  9380. const char* name = script_getstr(st,2);
  9381. int type = script_getnum(st,3);
  9382. if(sd)
  9383. {
  9384. if(!st->rid)
  9385. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9386. else
  9387. clif_soundeffect(sd,&sd->bl,name,type);
  9388. }
  9389. return 0;
  9390. }
  9391. int soundeffect_sub(struct block_list* bl,va_list ap)
  9392. {
  9393. char* name = va_arg(ap,char*);
  9394. int type = va_arg(ap,int);
  9395. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9396. return 0;
  9397. }
  9398. /*==========================================
  9399. * Play a sound effect (.wav) on multiple clients
  9400. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9401. *------------------------------------------*/
  9402. BUILDIN_FUNC(soundeffectall)
  9403. {
  9404. struct block_list* bl;
  9405. const char* name;
  9406. int type;
  9407. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9408. if (!bl)
  9409. return 0;
  9410. name = script_getstr(st,2);
  9411. type = script_getnum(st,3);
  9412. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9413. if(!script_hasdata(st,4))
  9414. { // area around
  9415. clif_soundeffectall(bl, name, type, AREA);
  9416. }
  9417. else
  9418. if(!script_hasdata(st,5))
  9419. { // entire map
  9420. const char* map = script_getstr(st,4);
  9421. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9422. }
  9423. else
  9424. if(script_hasdata(st,8))
  9425. { // specified part of map
  9426. const char* map = script_getstr(st,4);
  9427. int x0 = script_getnum(st,5);
  9428. int y0 = script_getnum(st,6);
  9429. int x1 = script_getnum(st,7);
  9430. int y1 = script_getnum(st,8);
  9431. map_forsomeinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, 0, BL_PC, name, type);
  9432. }
  9433. else
  9434. {
  9435. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9436. }
  9437. return 0;
  9438. }
  9439. /*==========================================
  9440. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9441. *------------------------------------------*/
  9442. BUILDIN_FUNC(petrecovery)
  9443. {
  9444. struct pet_data *pd;
  9445. TBL_PC *sd=script_rid2sd(st);
  9446. if(sd==NULL || sd->pd==NULL)
  9447. return 0;
  9448. pd=sd->pd;
  9449. if (pd->recovery)
  9450. { //Halt previous bonus
  9451. if (pd->recovery->timer != -1)
  9452. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9453. } else //Init
  9454. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9455. pd->recovery->type = (sc_type)script_getnum(st,2);
  9456. pd->recovery->delay = script_getnum(st,3);
  9457. pd->recovery->timer = INVALID_TIMER;
  9458. return 0;
  9459. }
  9460. /*==========================================
  9461. * pet healing [Valaris] //Rewritten by [Skotlex]
  9462. *------------------------------------------*/
  9463. BUILDIN_FUNC(petheal)
  9464. {
  9465. struct pet_data *pd;
  9466. TBL_PC *sd=script_rid2sd(st);
  9467. if(sd==NULL || sd->pd==NULL)
  9468. return 0;
  9469. pd=sd->pd;
  9470. if (pd->s_skill)
  9471. { //Clear previous skill
  9472. if (pd->s_skill->timer != -1)
  9473. {
  9474. if (pd->s_skill->id)
  9475. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9476. else
  9477. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9478. }
  9479. } else //init memory
  9480. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9481. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9482. //Use the lv as the amount to heal
  9483. pd->s_skill->lv=script_getnum(st,2);
  9484. pd->s_skill->delay=script_getnum(st,3);
  9485. pd->s_skill->hp=script_getnum(st,4);
  9486. pd->s_skill->sp=script_getnum(st,5);
  9487. //Use delay as initial offset to avoid skill/heal exploits
  9488. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9489. pd->s_skill->timer = INVALID_TIMER;
  9490. else
  9491. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9492. return 0;
  9493. }
  9494. /*==========================================
  9495. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9496. *------------------------------------------*/
  9497. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  9498. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  9499. BUILDIN_FUNC(petskillattack)
  9500. {
  9501. struct pet_data *pd;
  9502. TBL_PC *sd=script_rid2sd(st);
  9503. if(sd==NULL || sd->pd==NULL)
  9504. return 0;
  9505. pd=sd->pd;
  9506. if (pd->a_skill == NULL)
  9507. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9508. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9509. pd->a_skill->lv=script_getnum(st,3);
  9510. pd->a_skill->div_ = 0;
  9511. pd->a_skill->rate=script_getnum(st,4);
  9512. pd->a_skill->bonusrate=script_getnum(st,5);
  9513. return 0;
  9514. }
  9515. /*==========================================
  9516. * pet attack skills [Valaris]
  9517. *------------------------------------------*/
  9518. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  9519. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  9520. BUILDIN_FUNC(petskillattack2)
  9521. {
  9522. struct pet_data *pd;
  9523. TBL_PC *sd=script_rid2sd(st);
  9524. if(sd==NULL || sd->pd==NULL)
  9525. return 0;
  9526. pd=sd->pd;
  9527. if (pd->a_skill == NULL)
  9528. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9529. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9530. pd->a_skill->lv=script_getnum(st,3);
  9531. pd->a_skill->div_ = script_getnum(st,4);
  9532. pd->a_skill->rate=script_getnum(st,5);
  9533. pd->a_skill->bonusrate=script_getnum(st,6);
  9534. return 0;
  9535. }
  9536. /*==========================================
  9537. * pet support skills [Skotlex]
  9538. *------------------------------------------*/
  9539. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  9540. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  9541. BUILDIN_FUNC(petskillsupport)
  9542. {
  9543. struct pet_data *pd;
  9544. TBL_PC *sd=script_rid2sd(st);
  9545. if(sd==NULL || sd->pd==NULL)
  9546. return 0;
  9547. pd=sd->pd;
  9548. if (pd->s_skill)
  9549. { //Clear previous skill
  9550. if (pd->s_skill->timer != -1)
  9551. {
  9552. if (pd->s_skill->id)
  9553. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9554. else
  9555. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9556. }
  9557. } else //init memory
  9558. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9559. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9560. pd->s_skill->lv=script_getnum(st,3);
  9561. pd->s_skill->delay=script_getnum(st,4);
  9562. pd->s_skill->hp=script_getnum(st,5);
  9563. pd->s_skill->sp=script_getnum(st,6);
  9564. //Use delay as initial offset to avoid skill/heal exploits
  9565. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9566. pd->s_skill->timer = INVALID_TIMER;
  9567. else
  9568. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9569. return 0;
  9570. }
  9571. /*==========================================
  9572. * Scripted skill effects [Celest]
  9573. *------------------------------------------*/
  9574. /// skilleffect <skill id>,<level>
  9575. /// skilleffect "<skill name>",<level>
  9576. BUILDIN_FUNC(skilleffect)
  9577. {
  9578. TBL_PC *sd;
  9579. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9580. int skilllv=script_getnum(st,3);
  9581. sd=script_rid2sd(st);
  9582. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9583. return 0;
  9584. }
  9585. /*==========================================
  9586. * NPC skill effects [Valaris]
  9587. *------------------------------------------*/
  9588. /// npcskilleffect <skill id>,<level>,<x>,<y>
  9589. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  9590. BUILDIN_FUNC(npcskilleffect)
  9591. {
  9592. struct block_list *bl= map_id2bl(st->oid);
  9593. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9594. int skilllv=script_getnum(st,3);
  9595. int x=script_getnum(st,4);
  9596. int y=script_getnum(st,5);
  9597. if (bl)
  9598. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9599. return 0;
  9600. }
  9601. /*==========================================
  9602. * Special effects [Valaris]
  9603. *------------------------------------------*/
  9604. BUILDIN_FUNC(specialeffect)
  9605. {
  9606. struct block_list *bl=map_id2bl(st->oid);
  9607. int type = script_getnum(st,2);
  9608. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9609. if(bl==NULL)
  9610. return 0;
  9611. clif_specialeffect(bl, type, target);
  9612. return 0;
  9613. }
  9614. BUILDIN_FUNC(specialeffect2)
  9615. {
  9616. TBL_PC *sd=script_rid2sd(st);
  9617. int type = script_getnum(st,2);
  9618. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9619. if(sd==NULL)
  9620. return 0;
  9621. clif_specialeffect(&sd->bl, type, target);
  9622. return 0;
  9623. }
  9624. /*==========================================
  9625. * Nude [Valaris]
  9626. *------------------------------------------*/
  9627. BUILDIN_FUNC(nude)
  9628. {
  9629. TBL_PC *sd=script_rid2sd(st);
  9630. int i,calcflag=0;
  9631. if(sd==NULL)
  9632. return 0;
  9633. for(i=0;i<11;i++)
  9634. if(sd->equip_index[i] >= 0) {
  9635. if(!calcflag)
  9636. calcflag=1;
  9637. pc_unequipitem(sd,sd->equip_index[i],2);
  9638. }
  9639. if(calcflag)
  9640. status_calc_pc(sd,0);
  9641. return 0;
  9642. }
  9643. /*==========================================
  9644. * gmcommand [MouseJstr]
  9645. *
  9646. * suggested on the forums...
  9647. * splitted into atcommand & charcommand by [Skotlex]
  9648. *------------------------------------------*/
  9649. BUILDIN_FUNC(atcommand)
  9650. {
  9651. TBL_PC dummy_sd;
  9652. TBL_PC* sd;
  9653. int fd;
  9654. const char* cmd;
  9655. cmd = script_getstr(st,2);
  9656. if (st->rid) {
  9657. sd = script_rid2sd(st);
  9658. fd = sd->fd;
  9659. } else { //Use a dummy character.
  9660. sd = &dummy_sd;
  9661. fd = 0;
  9662. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9663. if (st->oid)
  9664. {
  9665. struct block_list* bl = map_id2bl(st->oid);
  9666. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9667. if (bl->type == BL_NPC)
  9668. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9669. }
  9670. }
  9671. // compatibility with previous implementation (deprecated!)
  9672. if(cmd[0] != atcommand_symbol)
  9673. {
  9674. cmd += strlen(sd->status.name);
  9675. while(*cmd != atcommand_symbol && *cmd != 0)
  9676. cmd++;
  9677. }
  9678. is_atcommand(fd, sd, cmd, 0);
  9679. return 0;
  9680. }
  9681. BUILDIN_FUNC(charcommand)
  9682. {
  9683. TBL_PC dummy_sd;
  9684. TBL_PC* sd;
  9685. int fd;
  9686. const char* cmd;
  9687. cmd = script_getstr(st,2);
  9688. if (st->rid) {
  9689. sd = script_rid2sd(st);
  9690. fd = sd->fd;
  9691. } else { //Use a dummy character.
  9692. sd = &dummy_sd;
  9693. fd = 0;
  9694. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9695. if (st->oid)
  9696. {
  9697. struct block_list* bl = map_id2bl(st->oid);
  9698. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9699. if (bl->type == BL_NPC)
  9700. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9701. }
  9702. }
  9703. if (*cmd != charcommand_symbol) {
  9704. ShowWarning("script: buildin_charcommand: No '#' symbol!\n");
  9705. script_reportsrc(st);
  9706. return 1;
  9707. }
  9708. is_atcommand(fd, sd, cmd, 0);
  9709. return 0;
  9710. }
  9711. /*==========================================
  9712. * Displays a message for the player only (like system messages like "you got an apple" )
  9713. *------------------------------------------*/
  9714. BUILDIN_FUNC(dispbottom)
  9715. {
  9716. TBL_PC *sd=script_rid2sd(st);
  9717. const char *message;
  9718. message=script_getstr(st,2);
  9719. if(sd)
  9720. clif_disp_onlyself(sd,message,(int)strlen(message));
  9721. return 0;
  9722. }
  9723. /*==========================================
  9724. * All The Players Full Recovery
  9725. * (HP/SP full restore and resurrect if need)
  9726. *------------------------------------------*/
  9727. BUILDIN_FUNC(recovery)
  9728. {
  9729. TBL_PC* sd;
  9730. struct s_mapiterator* iter;
  9731. iter = mapit_getallusers();
  9732. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9733. {
  9734. if(pc_isdead(sd))
  9735. status_revive(&sd->bl, 100, 100);
  9736. else
  9737. status_percent_heal(&sd->bl, 100, 100);
  9738. clif_displaymessage(sd->fd,"You have been recovered!");
  9739. }
  9740. mapit_free(iter);
  9741. return 0;
  9742. }
  9743. /*==========================================
  9744. * Get your pet info: getpetinfo(n)
  9745. * n -> 0:pet_id 1:pet_class 2:pet_name
  9746. * 3:friendly 4:hungry, 5: rename flag.
  9747. *------------------------------------------*/
  9748. BUILDIN_FUNC(getpetinfo)
  9749. {
  9750. TBL_PC *sd=script_rid2sd(st);
  9751. TBL_PET *pd;
  9752. int type=script_getnum(st,2);
  9753. if(!sd || !sd->pd) {
  9754. if (type == 2)
  9755. script_pushconststr(st,"null");
  9756. else
  9757. script_pushint(st,0);
  9758. return 0;
  9759. }
  9760. pd = sd->pd;
  9761. switch(type){
  9762. case 0: script_pushint(st,pd->pet.pet_id); break;
  9763. case 1: script_pushint(st,pd->pet.class_); break;
  9764. case 2: script_pushstrcopy(st,pd->pet.name); break;
  9765. case 3: script_pushint(st,pd->pet.intimate); break;
  9766. case 4: script_pushint(st,pd->pet.hungry); break;
  9767. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9768. default:
  9769. script_pushint(st,0);
  9770. break;
  9771. }
  9772. return 0;
  9773. }
  9774. /*==========================================
  9775. * Get your homunculus info: gethominfo(n)
  9776. * n -> 0:hom_id 1:class 2:name
  9777. * 3:friendly 4:hungry, 5: rename flag.
  9778. * 6: level
  9779. *------------------------------------------*/
  9780. BUILDIN_FUNC(gethominfo)
  9781. {
  9782. TBL_PC *sd=script_rid2sd(st);
  9783. TBL_HOM *hd;
  9784. int type=script_getnum(st,2);
  9785. hd = sd?sd->hd:NULL;
  9786. if(!merc_is_hom_active(hd))
  9787. {
  9788. if (type == 2)
  9789. script_pushconststr(st,"null");
  9790. else
  9791. script_pushint(st,0);
  9792. return 0;
  9793. }
  9794. switch(type){
  9795. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9796. case 1: script_pushint(st,hd->homunculus.class_); break;
  9797. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  9798. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9799. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9800. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9801. case 6: script_pushint(st,hd->homunculus.level); break;
  9802. default:
  9803. script_pushint(st,0);
  9804. break;
  9805. }
  9806. return 0;
  9807. }
  9808. /*==========================================
  9809. * Shows wether your inventory(and equips) contain
  9810. selected card or not.
  9811. checkequipedcard(4001);
  9812. *------------------------------------------*/
  9813. BUILDIN_FUNC(checkequipedcard)
  9814. {
  9815. TBL_PC *sd=script_rid2sd(st);
  9816. int n,i,c=0;
  9817. c=script_getnum(st,2);
  9818. if(sd){
  9819. for(i=0;i<MAX_INVENTORY;i++){
  9820. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount && sd->inventory_data[i]){
  9821. for(n=0;n<sd->inventory_data[i]->slot;n++){
  9822. if(sd->status.inventory[i].card[n]==c){
  9823. script_pushint(st,1);
  9824. return 0;
  9825. }
  9826. }
  9827. }
  9828. }
  9829. }
  9830. script_pushint(st,0);
  9831. return 0;
  9832. }
  9833. BUILDIN_FUNC(jump_zero)
  9834. {
  9835. int sel;
  9836. sel=script_getnum(st,2);
  9837. if(!sel) {
  9838. int pos;
  9839. if( !data_islabel(script_getdata(st,3)) ){
  9840. ShowError("script: jump_zero: not label !\n");
  9841. st->state=END;
  9842. return 1;
  9843. }
  9844. pos=script_getnum(st,3);
  9845. st->pos=pos;
  9846. st->state=GOTO;
  9847. // printf("script: jump_zero: jumpto : %d\n",pos);
  9848. } else {
  9849. // printf("script: jump_zero: fail\n");
  9850. }
  9851. return 0;
  9852. }
  9853. /*==========================================
  9854. * GetMapMobs
  9855. returns mob counts on a set map:
  9856. e.g. GetMapMobs("prontera")
  9857. use "this" - for player's map
  9858. *------------------------------------------*/
  9859. BUILDIN_FUNC(getmapmobs)
  9860. {
  9861. const char *str=NULL;
  9862. int m=-1,bx,by;
  9863. int count=0;
  9864. struct block_list *bl;
  9865. str=script_getstr(st,2);
  9866. if(strcmp(str,"this")==0){
  9867. TBL_PC *sd=script_rid2sd(st);
  9868. if(sd)
  9869. m=sd->bl.m;
  9870. else{
  9871. script_pushint(st,-1);
  9872. return 0;
  9873. }
  9874. }else
  9875. m=map_mapname2mapid(str);
  9876. if(m < 0){
  9877. script_pushint(st,-1);
  9878. return 0;
  9879. }
  9880. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  9881. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  9882. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  9883. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9884. count++;
  9885. script_pushint(st,count);
  9886. return 0;
  9887. }
  9888. /*==========================================
  9889. * movenpc [MouseJstr]
  9890. *------------------------------------------*/
  9891. BUILDIN_FUNC(movenpc)
  9892. {
  9893. TBL_NPC *nd = NULL;
  9894. const char *npc;
  9895. int x,y;
  9896. npc = script_getstr(st,2);
  9897. x = script_getnum(st,3);
  9898. y = script_getnum(st,4);
  9899. if ((nd = npc_name2id(npc)) == NULL)
  9900. return -1;
  9901. npc_movenpc(nd, x, y);
  9902. return 0;
  9903. }
  9904. /*==========================================
  9905. * message [MouseJstr]
  9906. *------------------------------------------*/
  9907. BUILDIN_FUNC(message)
  9908. {
  9909. TBL_PC *sd;
  9910. const char *msg,*player;
  9911. TBL_PC *pl_sd = NULL;
  9912. sd = script_rid2sd(st);
  9913. player = script_getstr(st,2);
  9914. msg = script_getstr(st,3);
  9915. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9916. return 0;
  9917. clif_displaymessage(pl_sd->fd, msg);
  9918. return 0;
  9919. }
  9920. /*==========================================
  9921. * npctalk (sends message to surrounding area)
  9922. *------------------------------------------*/
  9923. BUILDIN_FUNC(npctalk)
  9924. {
  9925. const char* str;
  9926. char message[255];
  9927. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9928. str = script_getstr(st,2);
  9929. if(nd) {
  9930. memcpy(message, nd->name, NAME_LENGTH);
  9931. strtok(message, "#"); // discard extra name identifier if present
  9932. strcat(message, " : ");
  9933. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9934. clif_message(&(nd->bl), message);
  9935. }
  9936. return 0;
  9937. }
  9938. // change npc walkspeed [Valaris]
  9939. BUILDIN_FUNC(npcspeed)
  9940. {
  9941. struct npc_data* nd;
  9942. int speed;
  9943. speed = script_getnum(st,2);
  9944. nd =(struct npc_data *)map_id2bl(st->oid);
  9945. if( nd )
  9946. {
  9947. nd->speed = speed;
  9948. nd->ud.state.speed_changed = 1;
  9949. }
  9950. return 0;
  9951. }
  9952. // make an npc walk to a position [Valaris]
  9953. BUILDIN_FUNC(npcwalkto)
  9954. {
  9955. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9956. int x=0,y=0;
  9957. x=script_getnum(st,2);
  9958. y=script_getnum(st,3);
  9959. if(nd) {
  9960. unit_walktoxy(&nd->bl,x,y,0);
  9961. }
  9962. return 0;
  9963. }
  9964. // stop an npc's movement [Valaris]
  9965. BUILDIN_FUNC(npcstop)
  9966. {
  9967. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9968. if(nd) {
  9969. unit_stop_walking(&nd->bl,1|4);
  9970. }
  9971. return 0;
  9972. }
  9973. /*==========================================
  9974. * getlook char info. getlook(arg)
  9975. *------------------------------------------*/
  9976. BUILDIN_FUNC(getlook)
  9977. {
  9978. int type,val;
  9979. TBL_PC *sd;
  9980. sd=script_rid2sd(st);
  9981. type=script_getnum(st,2);
  9982. val=-1;
  9983. switch(type) {
  9984. case LOOK_HAIR: val=sd->status.hair; break; //1
  9985. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  9986. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  9987. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  9988. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  9989. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  9990. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  9991. case LOOK_SHIELD: val=sd->status.shield; break; //8
  9992. case LOOK_SHOES: break; //9
  9993. }
  9994. script_pushint(st,val);
  9995. return 0;
  9996. }
  9997. /*==========================================
  9998. * get char save point. argument: 0- map name, 1- x, 2- y
  9999. *------------------------------------------*/
  10000. BUILDIN_FUNC(getsavepoint)
  10001. {
  10002. TBL_PC* sd;
  10003. int type;
  10004. sd = script_rid2sd(st);
  10005. if (sd == NULL) {
  10006. script_pushint(st,0);
  10007. return 0;
  10008. }
  10009. type = script_getnum(st,2);
  10010. switch(type) {
  10011. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  10012. case 1: script_pushint(st,sd->status.save_point.x); break;
  10013. case 2: script_pushint(st,sd->status.save_point.y); break;
  10014. default:
  10015. script_pushint(st,0);
  10016. break;
  10017. }
  10018. return 0;
  10019. }
  10020. /*==========================================
  10021. * Get position for char/npc/pet/mob objects. Added by Lorky
  10022. *
  10023. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  10024. * where type:
  10025. * MapName$ - String variable for output map name
  10026. * MapX - Integer variable for output coord X
  10027. * MapY - Integer variable for output coord Y
  10028. * type - type of object
  10029. * 0 - Character coord
  10030. * 1 - NPC coord
  10031. * 2 - Pet coord
  10032. * 3 - Mob coord (not released)
  10033. * 4 - Homun coord
  10034. * CharName$ - Name object. If miss or "this" the current object
  10035. *
  10036. * Return:
  10037. * 0 - success
  10038. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10039. *------------------------------------------*/
  10040. BUILDIN_FUNC(getmapxy)
  10041. {
  10042. struct block_list *bl = NULL;
  10043. TBL_PC *sd=NULL;
  10044. int num;
  10045. const char *name;
  10046. char prefix;
  10047. int x,y,type;
  10048. char mapname[MAP_NAME_LENGTH];
  10049. if( !data_isreference(script_getdata(st,2)) ){
  10050. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10051. script_pushint(st,-1);
  10052. return 1;
  10053. }
  10054. if( !data_isreference(script_getdata(st,3)) ){
  10055. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10056. script_pushint(st,-1);
  10057. return 1;
  10058. }
  10059. if( !data_isreference(script_getdata(st,4)) ){
  10060. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10061. script_pushint(st,-1);
  10062. return 1;
  10063. }
  10064. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10065. type=script_getnum(st,5);
  10066. switch (type){
  10067. case 0: //Get Character Position
  10068. if( script_hasdata(st,6) )
  10069. sd=map_nick2sd(script_getstr(st,6));
  10070. else
  10071. sd=script_rid2sd(st);
  10072. if (sd)
  10073. bl = &sd->bl;
  10074. break;
  10075. case 1: //Get NPC Position
  10076. if( script_hasdata(st,6) )
  10077. {
  10078. struct npc_data *nd;
  10079. nd=npc_name2id(script_getstr(st,6));
  10080. if (nd)
  10081. bl = &nd->bl;
  10082. } else //In case the origin is not an npc?
  10083. bl=map_id2bl(st->oid);
  10084. break;
  10085. case 2: //Get Pet Position
  10086. if(script_hasdata(st,6))
  10087. sd=map_nick2sd(script_getstr(st,6));
  10088. else
  10089. sd=script_rid2sd(st);
  10090. if (sd && sd->pd)
  10091. bl = &sd->pd->bl;
  10092. break;
  10093. case 3: //Get Mob Position
  10094. break; //Not supported?
  10095. case 4: //Get Homun Position
  10096. if(script_hasdata(st,6))
  10097. sd=map_nick2sd(script_getstr(st,6));
  10098. else
  10099. sd=script_rid2sd(st);
  10100. if (sd && sd->hd)
  10101. bl = &sd->hd->bl;
  10102. break;
  10103. }
  10104. if (!bl) { //No object found.
  10105. script_pushint(st,-1);
  10106. return 0;
  10107. }
  10108. x= bl->x;
  10109. y= bl->y;
  10110. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10111. //Set MapName$
  10112. num=st->stack->stack_data[st->start+2].u.num;
  10113. name=get_str(num&0x00ffffff);
  10114. prefix=*name;
  10115. if(not_server_variable(prefix))
  10116. sd=script_rid2sd(st);
  10117. else
  10118. sd=NULL;
  10119. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10120. //Set MapX
  10121. num=st->stack->stack_data[st->start+3].u.num;
  10122. name=get_str(num&0x00ffffff);
  10123. prefix=*name;
  10124. if(not_server_variable(prefix))
  10125. sd=script_rid2sd(st);
  10126. else
  10127. sd=NULL;
  10128. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10129. //Set MapY
  10130. num=st->stack->stack_data[st->start+4].u.num;
  10131. name=get_str(num&0x00ffffff);
  10132. prefix=*name;
  10133. if(not_server_variable(prefix))
  10134. sd=script_rid2sd(st);
  10135. else
  10136. sd=NULL;
  10137. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10138. //Return Success value
  10139. script_pushint(st,0);
  10140. return 0;
  10141. }
  10142. /*==========================================
  10143. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10144. *------------------------------------------*/
  10145. BUILDIN_FUNC(logmes)
  10146. {
  10147. const char *str;
  10148. TBL_PC* sd;
  10149. if( log_config.npc <= 0 )
  10150. return 0;
  10151. sd = script_rid2sd(st);
  10152. if( sd == NULL )
  10153. return 1;
  10154. str = script_getstr(st,2);
  10155. log_npc(sd,str);
  10156. return 0;
  10157. }
  10158. BUILDIN_FUNC(summon)
  10159. {
  10160. int _class, timeout=0;
  10161. const char *str,*event="";
  10162. TBL_PC *sd;
  10163. struct mob_data *md;
  10164. int tick = gettick();
  10165. sd=script_rid2sd(st);
  10166. if (!sd) return 0;
  10167. str =script_getstr(st,2);
  10168. _class=script_getnum(st,3);
  10169. if( script_hasdata(st,4) )
  10170. timeout=script_getnum(st,4);
  10171. if( script_hasdata(st,5) ){
  10172. event=script_getstr(st,5);
  10173. check_event(st, event);
  10174. }
  10175. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10176. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10177. if (md) {
  10178. md->master_id=sd->bl.id;
  10179. md->special_state.ai=1;
  10180. if( md->deletetimer != INVALID_TIMER )
  10181. delete_timer(md->deletetimer, mob_timer_delete);
  10182. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10183. mob_spawn (md); //Now it is ready for spawning.
  10184. clif_misceffect2(&md->bl,344);
  10185. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10186. }
  10187. return 0;
  10188. }
  10189. /*==========================================
  10190. * Checks whether it is daytime/nighttime
  10191. *------------------------------------------*/
  10192. BUILDIN_FUNC(isnight)
  10193. {
  10194. script_pushint(st,(night_flag == 1));
  10195. return 0;
  10196. }
  10197. BUILDIN_FUNC(isday)
  10198. {
  10199. script_pushint(st,(night_flag == 0));
  10200. return 0;
  10201. }
  10202. /*================================================
  10203. * Check whether another item/card has been
  10204. * equipped - used for 2/15's cards patch [celest]
  10205. *------------------------------------------------*/
  10206. // leave this here, just in case
  10207. #if 0
  10208. BUILDIN_FUNC(isequipped)
  10209. {
  10210. TBL_PC *sd;
  10211. int i, j, k, id = 1;
  10212. int ret = -1;
  10213. sd = script_rid2sd(st);
  10214. for (i=0; id!=0; i++) {
  10215. int flag = 0;
  10216. FETCH (i+2, id) else id = 0;
  10217. if (id <= 0)
  10218. continue;
  10219. for (j=0; j<EQI_MAX; j++) {
  10220. int index;
  10221. index = sd->equip_index[j];
  10222. if(index < 0) continue;
  10223. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10224. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10225. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10226. if(!sd->inventory_data[index])
  10227. continue;
  10228. if(itemdb_type(id) != IT_CARD) { //Non card
  10229. if (sd->inventory_data[index]->nameid != id)
  10230. continue;
  10231. flag = 1;
  10232. break;
  10233. } else { //Card
  10234. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10235. continue;
  10236. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10237. if (sd->status.inventory[index].card[k] == id)
  10238. {
  10239. flag = 1;
  10240. break;
  10241. }
  10242. }
  10243. }
  10244. if (flag) break;
  10245. }
  10246. if (ret == -1)
  10247. ret = flag;
  10248. else
  10249. ret &= flag;
  10250. if (!ret) break;
  10251. }
  10252. script_pushint(st,ret);
  10253. return 0;
  10254. }
  10255. #endif
  10256. /*================================================
  10257. * Check how many items/cards in the list are
  10258. * equipped - used for 2/15's cards patch [celest]
  10259. *------------------------------------------------*/
  10260. BUILDIN_FUNC(isequippedcnt)
  10261. {
  10262. TBL_PC *sd;
  10263. int i, j, k, id = 1;
  10264. int ret = 0;
  10265. sd = script_rid2sd(st);
  10266. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10267. script_pushint(st,0);
  10268. return 0;
  10269. }
  10270. for (i=0; id!=0; i++) {
  10271. FETCH (i+2, id) else id = 0;
  10272. if (id <= 0)
  10273. continue;
  10274. for (j=0; j<EQI_MAX; j++) {
  10275. int index;
  10276. index = sd->equip_index[j];
  10277. if(index < 0) continue;
  10278. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10279. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10280. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10281. if(!sd->inventory_data[index])
  10282. continue;
  10283. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10284. if (sd->inventory_data[index]->nameid == id)
  10285. ret+= sd->status.inventory[index].amount;
  10286. } else { //Count cards.
  10287. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10288. continue; //No cards
  10289. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10290. if (sd->status.inventory[index].card[k] == id)
  10291. ret++; //[Lupus]
  10292. }
  10293. }
  10294. }
  10295. }
  10296. script_pushint(st,ret);
  10297. return 0;
  10298. }
  10299. /*================================================
  10300. * Check whether another card has been
  10301. * equipped - used for 2/15's cards patch [celest]
  10302. * -- Items checked cannot be reused in another
  10303. * card set to prevent exploits
  10304. *------------------------------------------------*/
  10305. BUILDIN_FUNC(isequipped)
  10306. {
  10307. TBL_PC *sd;
  10308. int i, j, k, id = 1;
  10309. int index, flag;
  10310. int ret = -1;
  10311. //Original hash to reverse it when full check fails.
  10312. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10313. sd = script_rid2sd(st);
  10314. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10315. script_pushint(st,0);
  10316. return 0;
  10317. }
  10318. setitem_hash = sd->setitem_hash;
  10319. setitem_hash2 = sd->setitem_hash2;
  10320. for (i=0; id!=0; i++)
  10321. {
  10322. FETCH (i+2, id) else id = 0;
  10323. if (id <= 0)
  10324. continue;
  10325. flag = 0;
  10326. for (j=0; j<EQI_MAX; j++)
  10327. {
  10328. index = sd->equip_index[j];
  10329. if(index < 0) continue;
  10330. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10331. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10332. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10333. if(!sd->inventory_data[index])
  10334. continue;
  10335. if (itemdb_type(id) != IT_CARD) {
  10336. if (sd->inventory_data[index]->nameid != id)
  10337. continue;
  10338. flag = 1;
  10339. break;
  10340. } else { //Cards
  10341. if (sd->inventory_data[index]->slot == 0 ||
  10342. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10343. continue;
  10344. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10345. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10346. unsigned int hash = 0;
  10347. if (sd->status.inventory[index].card[k] != id)
  10348. continue;
  10349. hash = 1<<((j<5?j:j-5)*4 + k);
  10350. // check if card is already used by another set
  10351. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10352. continue;
  10353. // We have found a match
  10354. flag = 1;
  10355. // Set hash so this card cannot be used by another
  10356. if (j<5)
  10357. sd->setitem_hash |= hash;
  10358. else
  10359. sd->setitem_hash2 |= hash;
  10360. break;
  10361. }
  10362. }
  10363. if (flag) break; //Card found
  10364. }
  10365. if (ret == -1)
  10366. ret = flag;
  10367. else
  10368. ret &= flag;
  10369. if (!ret) break;
  10370. }
  10371. if (!ret)
  10372. { //When check fails, restore original hash values. [Skotlex]
  10373. sd->setitem_hash = setitem_hash;
  10374. sd->setitem_hash2 = setitem_hash2;
  10375. }
  10376. script_pushint(st,ret);
  10377. return 0;
  10378. }
  10379. /*================================================
  10380. * Check how many given inserted cards in the CURRENT
  10381. * weapon - used for 2/15's cards patch [Lupus]
  10382. *------------------------------------------------*/
  10383. BUILDIN_FUNC(cardscnt)
  10384. {
  10385. TBL_PC *sd;
  10386. int i, k, id = 1;
  10387. int ret = 0;
  10388. int index;
  10389. sd = script_rid2sd(st);
  10390. for (i=0; id!=0; i++) {
  10391. FETCH (i+2, id) else id = 0;
  10392. if (id <= 0)
  10393. continue;
  10394. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10395. if(index < 0) continue;
  10396. if(!sd->inventory_data[index])
  10397. continue;
  10398. if(itemdb_type(id) != IT_CARD) {
  10399. if (sd->inventory_data[index]->nameid == id)
  10400. ret+= sd->status.inventory[index].amount;
  10401. } else {
  10402. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10403. continue;
  10404. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10405. if (sd->status.inventory[index].card[k] == id)
  10406. ret++;
  10407. }
  10408. }
  10409. }
  10410. script_pushint(st,ret);
  10411. // script_pushint(st,current_equip_item_index);
  10412. return 0;
  10413. }
  10414. /*=======================================================
  10415. * Returns the refined number of the current item, or an
  10416. * item with inventory index specified
  10417. *-------------------------------------------------------*/
  10418. BUILDIN_FUNC(getrefine)
  10419. {
  10420. TBL_PC *sd;
  10421. if ((sd = script_rid2sd(st))!= NULL)
  10422. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10423. else
  10424. script_pushint(st,0);
  10425. return 0;
  10426. }
  10427. /*=======================================================
  10428. * Day/Night controls
  10429. *-------------------------------------------------------*/
  10430. BUILDIN_FUNC(night)
  10431. {
  10432. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10433. return 0;
  10434. }
  10435. BUILDIN_FUNC(day)
  10436. {
  10437. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10438. return 0;
  10439. }
  10440. //=======================================================
  10441. // Unequip [Spectre]
  10442. //-------------------------------------------------------
  10443. BUILDIN_FUNC(unequip)
  10444. {
  10445. int i;
  10446. size_t num;
  10447. TBL_PC *sd;
  10448. num = script_getnum(st,2);
  10449. sd = script_rid2sd(st);
  10450. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10451. {
  10452. i = pc_checkequip(sd,equip[num-1]);
  10453. if (i >= 0)
  10454. pc_unequipitem(sd,i,1|2);
  10455. }
  10456. return 0;
  10457. }
  10458. BUILDIN_FUNC(equip)
  10459. {
  10460. int nameid=0,i;
  10461. TBL_PC *sd;
  10462. struct item_data *item_data;
  10463. sd = script_rid2sd(st);
  10464. nameid=script_getnum(st,2);
  10465. if((item_data = itemdb_exists(nameid)) == NULL)
  10466. {
  10467. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10468. return 1;
  10469. }
  10470. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10471. if( i < MAX_INVENTORY )
  10472. pc_equipitem(sd,i,item_data->equip);
  10473. return 0;
  10474. }
  10475. BUILDIN_FUNC(autoequip)
  10476. {
  10477. int nameid, flag;
  10478. struct item_data *item_data;
  10479. nameid=script_getnum(st,2);
  10480. flag=script_getnum(st,3);
  10481. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10482. item_data->flag.autoequip = flag>0?1:0;
  10483. }
  10484. return 0;
  10485. }
  10486. BUILDIN_FUNC(setbattleflag)
  10487. {
  10488. const char *flag, *value;
  10489. flag = script_getstr(st,2);
  10490. value = script_getstr(st,3);
  10491. if (battle_set_value(flag, value) == 0)
  10492. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10493. else
  10494. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10495. return 0;
  10496. }
  10497. BUILDIN_FUNC(getbattleflag)
  10498. {
  10499. const char *flag;
  10500. flag = script_getstr(st,2);
  10501. script_pushint(st,battle_get_value(flag));
  10502. return 0;
  10503. }
  10504. //=======================================================
  10505. // strlen [Valaris]
  10506. //-------------------------------------------------------
  10507. BUILDIN_FUNC(getstrlen)
  10508. {
  10509. const char *str = script_getstr(st,2);
  10510. int len = (str) ? (int)strlen(str) : 0;
  10511. script_pushint(st,len);
  10512. return 0;
  10513. }
  10514. //=======================================================
  10515. // isalpha [Valaris]
  10516. //-------------------------------------------------------
  10517. BUILDIN_FUNC(charisalpha)
  10518. {
  10519. const char *str=script_getstr(st,2);
  10520. int pos=script_getnum(st,3);
  10521. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10522. script_pushint(st,val);
  10523. return 0;
  10524. }
  10525. /// Changes the display name and/or display class of the npc.
  10526. /// Returns 0 is successful, 1 if the npc does not exist.
  10527. ///
  10528. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  10529. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10530. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10531. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10532. BUILDIN_FUNC(setnpcdisplay)
  10533. {
  10534. const char* name;
  10535. const char* newname = NULL;
  10536. int class_ = -1, size = -1;
  10537. struct script_data* data;
  10538. struct npc_data* nd;
  10539. name = script_getstr(st,2);
  10540. data = script_getdata(st,3);
  10541. if( script_hasdata(st,4) )
  10542. class_ = script_getnum(st,4);
  10543. if( script_hasdata(st,5) )
  10544. size = script_getnum(st,5);
  10545. get_val(st, data);
  10546. if( data_isstring(data) )
  10547. newname = conv_str(st,data);
  10548. else if( data_isint(data) )
  10549. class_ = conv_num(st,data);
  10550. else
  10551. {
  10552. ShowError("script:setnpcdisplay: expected a string or number\n");
  10553. script_reportdata(data);
  10554. return 1;
  10555. }
  10556. nd = npc_name2id(name);
  10557. if( nd == NULL )
  10558. {// not found
  10559. script_pushint(st,1);
  10560. return 0;
  10561. }
  10562. // update npc
  10563. if( newname )
  10564. npc_setdisplayname(nd, newname);
  10565. if( size != -1 && size != nd->size )
  10566. nd->size = size;
  10567. else
  10568. size = -1;
  10569. if( class_ != -1 && nd->class_ != class_ )
  10570. npc_setclass(nd, class_);
  10571. else if( size != -1 )
  10572. { // Required to update the visual size
  10573. clif_clearunit_area(&nd->bl, 0);
  10574. clif_spawn(&nd->bl);
  10575. }
  10576. script_pushint(st,0);
  10577. return 0;
  10578. }
  10579. BUILDIN_FUNC(atoi)
  10580. {
  10581. const char *value;
  10582. value = script_getstr(st,2);
  10583. script_pushint(st,atoi(value));
  10584. return 0;
  10585. }
  10586. // case-insensitive substring search [lordalfa]
  10587. BUILDIN_FUNC(compare)
  10588. {
  10589. const char *message;
  10590. const char *cmpstring;
  10591. message = script_getstr(st,2);
  10592. cmpstring = script_getstr(st,3);
  10593. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10594. return 0;
  10595. }
  10596. // [zBuffer] List of mathematics commands --->
  10597. BUILDIN_FUNC(sqrt)
  10598. {
  10599. double i, a;
  10600. i = script_getnum(st,2);
  10601. a = sqrt(i);
  10602. script_pushint(st,(int)a);
  10603. return 0;
  10604. }
  10605. BUILDIN_FUNC(pow)
  10606. {
  10607. double i, a, b;
  10608. a = script_getnum(st,2);
  10609. b = script_getnum(st,3);
  10610. i = pow(a,b);
  10611. script_pushint(st,(int)i);
  10612. return 0;
  10613. }
  10614. BUILDIN_FUNC(distance)
  10615. {
  10616. int x0, y0, x1, y1;
  10617. x0 = script_getnum(st,2);
  10618. y0 = script_getnum(st,3);
  10619. x1 = script_getnum(st,4);
  10620. y1 = script_getnum(st,5);
  10621. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10622. return 0;
  10623. }
  10624. // <--- [zBuffer] List of mathematics commands
  10625. BUILDIN_FUNC(md5)
  10626. {
  10627. const char *tmpstr;
  10628. char *md5str;
  10629. tmpstr = script_getstr(st,2);
  10630. md5str = (char *)aMallocA((32+1)*sizeof(char));
  10631. MD5_String(tmpstr, md5str);
  10632. script_pushstr(st, md5str);
  10633. return 0;
  10634. }
  10635. // [zBuffer] List of dynamic var commands --->
  10636. BUILDIN_FUNC(setd)
  10637. {
  10638. TBL_PC *sd=NULL;
  10639. char varname[100];
  10640. const char *value, *buffer;
  10641. int elem;
  10642. buffer = script_getstr(st, 2);
  10643. value = script_getstr(st, 3);
  10644. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10645. elem = 0;
  10646. if( not_server_variable(*varname) )
  10647. {
  10648. sd = script_rid2sd(st);
  10649. if( sd == NULL )
  10650. {
  10651. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10652. return 0;
  10653. }
  10654. }
  10655. if(varname[strlen(varname)-1] != '$') {
  10656. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10657. } else {
  10658. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10659. }
  10660. return 0;
  10661. }
  10662. #ifndef TXT_ONLY
  10663. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10664. {
  10665. int i, j;
  10666. TBL_PC* sd = NULL;
  10667. const char* query;
  10668. struct script_data* data;
  10669. const char* name;
  10670. int max_rows = 128;// maximum number of rows
  10671. int num_vars;
  10672. int num_cols;
  10673. // check target variables
  10674. for( i = 3; script_hasdata(st,i); ++i )
  10675. {
  10676. data = script_getdata(st, i);
  10677. if( data_isreference(data) && reference_tovariable(data) )
  10678. {// it's a variable
  10679. name = reference_getname(data);
  10680. if( not_server_variable(*name) && sd == NULL )
  10681. {// requires a player
  10682. sd = script_rid2sd(st);
  10683. if( sd == NULL )
  10684. {// no player attached
  10685. script_reportdata(data);
  10686. st->state = END;
  10687. return 1;
  10688. }
  10689. }
  10690. if( not_array_variable(*name) )
  10691. max_rows = 1;// not an array, limit to one row
  10692. }
  10693. else
  10694. {
  10695. ShowError("script:query_sql: not a variable\n");
  10696. script_reportdata(data);
  10697. st->state = END;
  10698. return 1;
  10699. }
  10700. }
  10701. num_vars = i - 3;
  10702. // Execute the query
  10703. query = script_getstr(st,2);
  10704. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10705. {
  10706. Sql_ShowDebug(handle);
  10707. script_pushint(st, 0);
  10708. return 1;
  10709. }
  10710. if( Sql_NumRows(handle) == 0 )
  10711. {// No data received
  10712. Sql_FreeResult(handle);
  10713. script_pushint(st, 0);
  10714. return 0;
  10715. }
  10716. // Count the number of columns to store
  10717. num_cols = Sql_NumColumns(handle);
  10718. if( num_vars < num_cols )
  10719. {
  10720. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10721. script_reportsrc(st);
  10722. }
  10723. else if( num_vars > num_cols )
  10724. {
  10725. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10726. script_reportsrc(st);
  10727. }
  10728. // Store data
  10729. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10730. {
  10731. for( j = 0; j < num_vars; ++j )
  10732. {
  10733. char* str = NULL;
  10734. if( j < num_cols )
  10735. Sql_GetData(handle, j, &str, NULL);
  10736. data = script_getdata(st, j+3);
  10737. name = reference_getname(data);
  10738. if( is_string_variable(name) )
  10739. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10740. else
  10741. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10742. }
  10743. }
  10744. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10745. {
  10746. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10747. script_reportsrc(st);
  10748. }
  10749. // Free data
  10750. Sql_FreeResult(handle);
  10751. script_pushint(st, i);
  10752. return 0;
  10753. }
  10754. #endif
  10755. BUILDIN_FUNC(query_sql)
  10756. {
  10757. #ifndef TXT_ONLY
  10758. return buildin_query_sql_sub(st, mmysql_handle);
  10759. #else
  10760. //for TXT version, we always return -1
  10761. script_pushint(st,-1);
  10762. return 0;
  10763. #endif
  10764. }
  10765. BUILDIN_FUNC(query_logsql)
  10766. {
  10767. #ifndef TXT_ONLY
  10768. return buildin_query_sql_sub(st, logmysql_handle);
  10769. #else
  10770. //for TXT version, we always return -1
  10771. script_pushint(st,-1);
  10772. return 0;
  10773. #endif
  10774. }
  10775. //Allows escaping of a given string.
  10776. BUILDIN_FUNC(escape_sql)
  10777. {
  10778. const char *str;
  10779. char *esc_str;
  10780. size_t len;
  10781. str = script_getstr(st,2);
  10782. len = strlen(str);
  10783. esc_str = (char*)aMallocA(len*2+1);
  10784. #if defined(TXT_ONLY)
  10785. jstrescapecpy(esc_str, str);
  10786. #else
  10787. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10788. #endif
  10789. script_pushstr(st, esc_str);
  10790. return 0;
  10791. }
  10792. BUILDIN_FUNC(getd)
  10793. {
  10794. char varname[100];
  10795. const char *buffer;
  10796. int elem;
  10797. buffer = script_getstr(st, 2);
  10798. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10799. elem = 0;
  10800. // Push the 'pointer' so it's more flexible [Lance]
  10801. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10802. return 0;
  10803. }
  10804. // <--- [zBuffer] List of dynamic var commands
  10805. // Pet stat [Lance]
  10806. BUILDIN_FUNC(petstat)
  10807. {
  10808. TBL_PC *sd = NULL;
  10809. struct pet_data *pd;
  10810. int flag = script_getnum(st,2);
  10811. sd = script_rid2sd(st);
  10812. if(!sd || !sd->status.pet_id || !sd->pd){
  10813. if(flag == 2)
  10814. script_pushconststr(st, "");
  10815. else
  10816. script_pushint(st,0);
  10817. return 0;
  10818. }
  10819. pd = sd->pd;
  10820. switch(flag){
  10821. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10822. case 2: script_pushstrcopy(st, pd->pet.name); break;
  10823. case 3: script_pushint(st,(int)pd->pet.level); break;
  10824. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10825. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10826. default:
  10827. script_pushint(st,0);
  10828. break;
  10829. }
  10830. return 0;
  10831. }
  10832. BUILDIN_FUNC(callshop)
  10833. {
  10834. TBL_PC *sd = NULL;
  10835. struct npc_data *nd;
  10836. const char *shopname;
  10837. int flag = 0;
  10838. sd = script_rid2sd(st);
  10839. if (!sd) {
  10840. script_pushint(st,0);
  10841. return 0;
  10842. }
  10843. shopname = script_getstr(st, 2);
  10844. if( script_hasdata(st,3) )
  10845. flag = script_getnum(st,3);
  10846. nd = npc_name2id(shopname);
  10847. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  10848. {
  10849. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  10850. script_pushint(st,0);
  10851. return 1;
  10852. }
  10853. if( nd->subtype == SHOP )
  10854. {
  10855. switch( flag )
  10856. {
  10857. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  10858. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  10859. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  10860. }
  10861. }
  10862. else
  10863. clif_cashshop_show(sd, nd);
  10864. sd->npc_shopid = nd->bl.id;
  10865. script_pushint(st,1);
  10866. return 0;
  10867. }
  10868. BUILDIN_FUNC(npcshopitem)
  10869. {
  10870. const char* npcname = script_getstr(st, 2);
  10871. struct npc_data* nd = npc_name2id(npcname);
  10872. int n, i;
  10873. int amount;
  10874. if( !nd || nd->subtype != SHOP )
  10875. { //Not found.
  10876. script_pushint(st,0);
  10877. return 0;
  10878. }
  10879. // get the count of new entries
  10880. amount = (script_lastdata(st)-2)/2;
  10881. // generate new shop item list
  10882. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  10883. for( n = 0, i = 3; n < amount; n++, i+=2 )
  10884. {
  10885. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10886. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10887. }
  10888. nd->u.shop.count = n;
  10889. script_pushint(st,1);
  10890. return 0;
  10891. }
  10892. BUILDIN_FUNC(npcshopadditem)
  10893. {
  10894. const char* npcname = script_getstr(st,2);
  10895. struct npc_data* nd = npc_name2id(npcname);
  10896. int n, i;
  10897. int amount;
  10898. if( !nd || nd->subtype != SHOP )
  10899. { //Not found.
  10900. script_pushint(st,0);
  10901. return 0;
  10902. }
  10903. // get the count of new entries
  10904. amount = (script_lastdata(st)-2)/2;
  10905. // append new items to existing shop item list
  10906. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  10907. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  10908. {
  10909. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10910. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10911. }
  10912. nd->u.shop.count = n;
  10913. script_pushint(st,1);
  10914. return 0;
  10915. }
  10916. BUILDIN_FUNC(npcshopdelitem)
  10917. {
  10918. const char* npcname = script_getstr(st,2);
  10919. struct npc_data* nd = npc_name2id(npcname);
  10920. int n, i;
  10921. int amount;
  10922. int size;
  10923. if( !nd || nd->subtype != SHOP )
  10924. { //Not found.
  10925. script_pushint(st,0);
  10926. return 0;
  10927. }
  10928. amount = script_lastdata(st)-2;
  10929. size = nd->u.shop.count;
  10930. // remove specified items from the shop item list
  10931. for( i = 3; i < 3 + amount; i++ )
  10932. {
  10933. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
  10934. if( n < size )
  10935. {
  10936. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  10937. size--;
  10938. }
  10939. }
  10940. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  10941. nd->u.shop.count = size;
  10942. script_pushint(st,1);
  10943. return 0;
  10944. }
  10945. //Sets a script to attach to a shop npc.
  10946. BUILDIN_FUNC(npcshopattach)
  10947. {
  10948. const char* npcname = script_getstr(st,2);
  10949. struct npc_data* nd = npc_name2id(npcname);
  10950. int flag = 1;
  10951. if( script_hasdata(st,3) )
  10952. flag = script_getnum(st,3);
  10953. if( !nd || nd->subtype != SHOP )
  10954. { //Not found.
  10955. script_pushint(st,0);
  10956. return 0;
  10957. }
  10958. if (flag)
  10959. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10960. else
  10961. nd->master_nd = NULL;
  10962. script_pushint(st,1);
  10963. return 0;
  10964. }
  10965. /*==========================================
  10966. * Returns some values of an item [Lupus]
  10967. * Price, Weight, etc...
  10968. setitemscript(itemID,"{new item bonus script}",[n]);
  10969. Where n:
  10970. 0 - script
  10971. 1 - Equip script
  10972. 2 - Unequip script
  10973. *------------------------------------------*/
  10974. BUILDIN_FUNC(setitemscript)
  10975. {
  10976. int item_id,n=0;
  10977. const char *script;
  10978. struct item_data *i_data;
  10979. struct script_code **dstscript;
  10980. item_id = script_getnum(st,2);
  10981. script = script_getstr(st,3);
  10982. if( script_hasdata(st,4) )
  10983. n=script_getnum(st,4);
  10984. i_data = itemdb_exists(item_id);
  10985. if (!i_data || script==NULL || script[0]!='{') {
  10986. script_pushint(st,0);
  10987. return 0;
  10988. }
  10989. switch (n) {
  10990. case 2:
  10991. dstscript = &i_data->unequip_script;
  10992. break;
  10993. case 1:
  10994. dstscript = &i_data->equip_script;
  10995. break;
  10996. default:
  10997. dstscript = &i_data->script;
  10998. break;
  10999. }
  11000. if(*dstscript)
  11001. script_free_code(*dstscript);
  11002. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  11003. script_pushint(st,1);
  11004. return 0;
  11005. }
  11006. /* Work In Progress [Lupus]
  11007. BUILDIN_FUNC(addmonsterdrop)
  11008. {
  11009. int class_,item_id,chance;
  11010. class_=script_getnum(st,2);
  11011. item_id=script_getnum(st,3);
  11012. chance=script_getnum(st,4);
  11013. if(class_>1000 && item_id>500 && chance>0) {
  11014. script_pushint(st,1);
  11015. } else {
  11016. script_pushint(st,0);
  11017. }
  11018. }
  11019. BUILDIN_FUNC(delmonsterdrop)
  11020. {
  11021. int class_,item_id;
  11022. class_=script_getnum(st,2);
  11023. item_id=script_getnum(st,3);
  11024. if(class_>1000 && item_id>500) {
  11025. script_pushint(st,1);
  11026. } else {
  11027. script_pushint(st,0);
  11028. }
  11029. }
  11030. */
  11031. /*==========================================
  11032. * Returns some values of a monster [Lupus]
  11033. * Name, Level, race, size, etc...
  11034. getmonsterinfo(monsterID,queryIndex);
  11035. *------------------------------------------*/
  11036. BUILDIN_FUNC(getmonsterinfo)
  11037. {
  11038. struct mob_db *mob;
  11039. int mob_id;
  11040. mob_id = script_getnum(st,2);
  11041. if (!mobdb_checkid(mob_id)) {
  11042. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  11043. if ( !script_getnum(st,3) ) //requested a string
  11044. script_pushconststr(st,"null");
  11045. else
  11046. script_pushint(st,-1);
  11047. return -1;
  11048. }
  11049. mob = mob_db(mob_id);
  11050. switch ( script_getnum(st,3) ) {
  11051. case 0: script_pushstrcopy(st,mob->jname); break;
  11052. case 1: script_pushint(st,mob->lv); break;
  11053. case 2: script_pushint(st,mob->status.max_hp); break;
  11054. case 3: script_pushint(st,mob->base_exp); break;
  11055. case 4: script_pushint(st,mob->job_exp); break;
  11056. case 5: script_pushint(st,mob->status.rhw.atk); break;
  11057. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  11058. case 7: script_pushint(st,mob->status.def); break;
  11059. case 8: script_pushint(st,mob->status.mdef); break;
  11060. case 9: script_pushint(st,mob->status.str); break;
  11061. case 10: script_pushint(st,mob->status.agi); break;
  11062. case 11: script_pushint(st,mob->status.vit); break;
  11063. case 12: script_pushint(st,mob->status.int_); break;
  11064. case 13: script_pushint(st,mob->status.dex); break;
  11065. case 14: script_pushint(st,mob->status.luk); break;
  11066. case 15: script_pushint(st,mob->status.rhw.range); break;
  11067. case 16: script_pushint(st,mob->range2); break;
  11068. case 17: script_pushint(st,mob->range3); break;
  11069. case 18: script_pushint(st,mob->status.size); break;
  11070. case 19: script_pushint(st,mob->status.race); break;
  11071. case 20: script_pushint(st,mob->status.def_ele); break;
  11072. case 21: script_pushint(st,mob->status.mode); break;
  11073. default: script_pushint(st,-1); //wrong Index
  11074. }
  11075. return 0;
  11076. }
  11077. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  11078. {
  11079. TBL_PC *sd = NULL;
  11080. if(script_hasdata(st,2))
  11081. sd = map_nick2sd(script_getstr(st,2));
  11082. else
  11083. sd = script_rid2sd(st);
  11084. if(sd)
  11085. script_pushint(st,(sd->vender_id != 0));
  11086. else
  11087. script_pushint(st,0);
  11088. return 0;
  11089. }
  11090. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  11091. {
  11092. TBL_PC *sd = NULL;
  11093. if(script_hasdata(st,2))
  11094. sd = map_nick2sd(script_getstr(st,2));
  11095. else
  11096. sd = script_rid2sd(st);
  11097. if(sd)
  11098. script_pushint(st,(sd->chatID != 0));
  11099. else
  11100. script_pushint(st,0);
  11101. return 0;
  11102. }
  11103. int axtoi(const char *hexStg)
  11104. {
  11105. int n = 0; // position in string
  11106. int m = 0; // position in digit[] to shift
  11107. int count; // loop index
  11108. int intValue = 0; // integer value of hex string
  11109. int digit[11]; // hold values to convert
  11110. while (n < 10) {
  11111. if (hexStg[n]=='\0')
  11112. break;
  11113. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11114. digit[n] = hexStg[n] & 0x0f; //convert to int
  11115. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11116. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11117. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  11118. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11119. else break;
  11120. n++;
  11121. }
  11122. count = n;
  11123. m = n - 1;
  11124. n = 0;
  11125. while(n < count) {
  11126. // digit[n] is value of hex digit at position n
  11127. // (m << 2) is the number of positions to shift
  11128. // OR the bits into return value
  11129. intValue = intValue | (digit[n] << (m << 2));
  11130. m--; // adjust the position to set
  11131. n++; // next digit to process
  11132. }
  11133. return (intValue);
  11134. }
  11135. // [Lance] Hex string to integer converter
  11136. BUILDIN_FUNC(axtoi)
  11137. {
  11138. const char *hex = script_getstr(st,2);
  11139. script_pushint(st,axtoi(hex));
  11140. return 0;
  11141. }
  11142. // [zBuffer] List of player cont commands --->
  11143. BUILDIN_FUNC(rid2name)
  11144. {
  11145. struct block_list *bl = NULL;
  11146. int rid = script_getnum(st,2);
  11147. if((bl = map_id2bl(rid)))
  11148. {
  11149. switch(bl->type) {
  11150. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  11151. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  11152. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  11153. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  11154. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  11155. case BL_MER: script_pushstrcopy(st,((TBL_MER*)bl)->db->name); break;
  11156. default:
  11157. ShowError("buildin_rid2name: BL type unknown.\n");
  11158. script_pushconststr(st,"");
  11159. break;
  11160. }
  11161. } else {
  11162. ShowError("buildin_rid2name: invalid RID\n");
  11163. script_pushconststr(st,"(null)");
  11164. }
  11165. return 0;
  11166. }
  11167. BUILDIN_FUNC(pcblockmove)
  11168. {
  11169. int id, flag;
  11170. TBL_PC *sd = NULL;
  11171. id = script_getnum(st,2);
  11172. flag = script_getnum(st,3);
  11173. if(id)
  11174. sd = map_id2sd(id);
  11175. else
  11176. sd = script_rid2sd(st);
  11177. if(sd)
  11178. sd->state.blockedmove = flag > 0;
  11179. return 0;
  11180. }
  11181. BUILDIN_FUNC(pcfollow)
  11182. {
  11183. int id, targetid;
  11184. TBL_PC *sd = NULL;
  11185. id = script_getnum(st,2);
  11186. targetid = script_getnum(st,3);
  11187. if(id)
  11188. sd = map_id2sd(id);
  11189. else
  11190. sd = script_rid2sd(st);
  11191. if(sd)
  11192. pc_follow(sd, targetid);
  11193. return 0;
  11194. }
  11195. BUILDIN_FUNC(pcstopfollow)
  11196. {
  11197. int id;
  11198. TBL_PC *sd = NULL;
  11199. id = script_getnum(st,2);
  11200. if(id)
  11201. sd = map_id2sd(id);
  11202. else
  11203. sd = script_rid2sd(st);
  11204. if(sd)
  11205. pc_stop_following(sd);
  11206. return 0;
  11207. }
  11208. // <--- [zBuffer] List of player cont commands
  11209. // [zBuffer] List of mob control commands --->
  11210. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11211. /// Makes the unit walk to target position or map
  11212. /// Returns if it was successfull
  11213. ///
  11214. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11215. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11216. BUILDIN_FUNC(unitwalk)
  11217. {
  11218. struct block_list* bl;
  11219. bl = map_id2bl(script_getnum(st,2));
  11220. if( bl == NULL )
  11221. {
  11222. script_pushint(st, 0);
  11223. }
  11224. else if( script_hasdata(st,4) )
  11225. {
  11226. int x = script_getnum(st,3);
  11227. int y = script_getnum(st,4);
  11228. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11229. }
  11230. else
  11231. {
  11232. int map_id = script_getnum(st,3);
  11233. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11234. }
  11235. return 0;
  11236. }
  11237. /// Kills the unit
  11238. ///
  11239. /// unitkill <unit_id>;
  11240. BUILDIN_FUNC(unitkill)
  11241. {
  11242. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11243. if( bl != NULL )
  11244. status_kill(bl);
  11245. return 0;
  11246. }
  11247. /// Warps the unit to the target position in the target map
  11248. /// Returns if it was successfull
  11249. ///
  11250. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11251. BUILDIN_FUNC(unitwarp)
  11252. {
  11253. int unit_id;
  11254. int map;
  11255. short x;
  11256. short y;
  11257. struct block_list* bl;
  11258. unit_id = script_getnum(st,2);
  11259. map = map_mapname2mapid(script_getstr(st, 3));
  11260. x = (short)script_getnum(st,4);
  11261. y = (short)script_getnum(st,5);
  11262. bl = map_id2bl(unit_id);
  11263. if( map >= 0 && bl != NULL )
  11264. script_pushint(st, unit_warp(bl,map,x,y,0));
  11265. else
  11266. script_pushint(st, 0);
  11267. return 0;
  11268. }
  11269. /// Makes the unit attack the target.
  11270. /// If the unit is a player and <action type> is not 0, it does a continuous
  11271. /// attack instead of a single attack.
  11272. /// Returns if the request was successfull.
  11273. ///
  11274. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11275. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11276. BUILDIN_FUNC(unitattack)
  11277. {
  11278. struct block_list* unit_bl;
  11279. struct block_list* target_bl = NULL;
  11280. struct script_data* data;
  11281. int actiontype = 0;
  11282. // get unit
  11283. unit_bl = map_id2bl(script_getnum(st,2));
  11284. if( unit_bl == NULL ) {
  11285. script_pushint(st, 0);
  11286. return 0;
  11287. }
  11288. data = script_getdata(st, 3);
  11289. get_val(st, data);
  11290. if( data_isstring(data) )
  11291. {
  11292. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11293. if( sd != NULL )
  11294. target_bl = &sd->bl;
  11295. } else
  11296. target_bl = map_id2bl(conv_num(st, data));
  11297. // request the attack
  11298. if( target_bl == NULL )
  11299. {
  11300. script_pushint(st, 0);
  11301. return 0;
  11302. }
  11303. // get actiontype
  11304. if( script_hasdata(st,4) )
  11305. actiontype = script_getnum(st,4);
  11306. switch( unit_bl->type )
  11307. {
  11308. case BL_PC:
  11309. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11310. script_pushint(st, 1);
  11311. return 0;
  11312. case BL_MOB:
  11313. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11314. break;
  11315. case BL_PET:
  11316. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11317. break;
  11318. default:
  11319. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11320. script_pushint(st, 0);
  11321. return 1;
  11322. }
  11323. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11324. return 0;
  11325. }
  11326. /// Makes the unit stop attacking and moving
  11327. ///
  11328. /// unitstop <unit_id>;
  11329. BUILDIN_FUNC(unitstop)
  11330. {
  11331. int unit_id;
  11332. struct block_list* bl;
  11333. unit_id = script_getnum(st,2);
  11334. bl = map_id2bl(unit_id);
  11335. if( bl != NULL )
  11336. {
  11337. unit_stop_attack(bl);
  11338. unit_stop_walking(bl,4);
  11339. if( bl->type == BL_MOB )
  11340. ((TBL_MOB*)bl)->target_id = 0;
  11341. }
  11342. return 0;
  11343. }
  11344. /// Makes the unit say the message
  11345. ///
  11346. /// unittalk <unit_id>,"<message>";
  11347. BUILDIN_FUNC(unittalk)
  11348. {
  11349. int unit_id;
  11350. const char* message;
  11351. struct block_list* bl;
  11352. unit_id = script_getnum(st,2);
  11353. message = script_getstr(st, 3);
  11354. bl = map_id2bl(unit_id);
  11355. if( bl != NULL )
  11356. {
  11357. struct StringBuf sbuf;
  11358. StringBuf_Init(&sbuf);
  11359. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11360. clif_message(bl, StringBuf_Value(&sbuf));
  11361. if( bl->type == BL_PC )
  11362. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11363. StringBuf_Destroy(&sbuf);
  11364. }
  11365. return 0;
  11366. }
  11367. /// Makes the unit do an emotion
  11368. ///
  11369. /// unitemote <unit_id>,<emotion>;
  11370. ///
  11371. /// @see e_* in const.txt
  11372. BUILDIN_FUNC(unitemote)
  11373. {
  11374. int unit_id;
  11375. int emotion;
  11376. struct block_list* bl;
  11377. unit_id = script_getnum(st,2);
  11378. emotion = script_getnum(st,3);
  11379. bl = map_id2bl(unit_id);
  11380. if( bl != NULL )
  11381. clif_emotion(bl, emotion);
  11382. return 0;
  11383. }
  11384. /// Makes the unit cast the skill on the target or self if no target is specified
  11385. ///
  11386. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11387. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  11388. BUILDIN_FUNC(unitskilluseid)
  11389. {
  11390. int unit_id;
  11391. int skill_id;
  11392. int skill_lv;
  11393. int target_id;
  11394. struct block_list* bl;
  11395. unit_id = script_getnum(st,2);
  11396. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11397. skill_lv = script_getnum(st,4);
  11398. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11399. bl = map_id2bl(unit_id);
  11400. if( bl != NULL )
  11401. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11402. return 0;
  11403. }
  11404. /// Makes the unit cast the skill on the target position.
  11405. ///
  11406. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11407. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  11408. BUILDIN_FUNC(unitskillusepos)
  11409. {
  11410. int unit_id;
  11411. int skill_id;
  11412. int skill_lv;
  11413. int skill_x;
  11414. int skill_y;
  11415. struct block_list* bl;
  11416. unit_id = script_getnum(st,2);
  11417. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11418. skill_lv = script_getnum(st,4);
  11419. skill_x = script_getnum(st,5);
  11420. skill_y = script_getnum(st,6);
  11421. bl = map_id2bl(unit_id);
  11422. if( bl != NULL )
  11423. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11424. return 0;
  11425. }
  11426. // <--- [zBuffer] List of mob control commands
  11427. /// Pauses the execution of the script, detaching the player
  11428. ///
  11429. /// sleep <mili seconds>;
  11430. BUILDIN_FUNC(sleep)
  11431. {
  11432. int ticks;
  11433. TBL_PC* sd;
  11434. ticks = script_getnum(st,2);
  11435. sd = map_id2sd(st->rid);
  11436. // detach the player
  11437. if( sd && sd->npc_id == st->oid )
  11438. {
  11439. sd->npc_id = 0;
  11440. }
  11441. st->rid = 0;
  11442. if( ticks <= 0 )
  11443. {// do nothing
  11444. }
  11445. else if( st->sleep.tick == 0 )
  11446. {// sleep for the target amount of time
  11447. st->state = RERUNLINE;
  11448. st->sleep.tick = ticks;
  11449. }
  11450. else
  11451. {// sleep time is over
  11452. st->state = RUN;
  11453. st->sleep.tick = 0;
  11454. }
  11455. return 0;
  11456. }
  11457. /// Pauses the execution of the script, keeping the player attached
  11458. /// Returns if a player is still attached
  11459. ///
  11460. /// sleep2(<mili secconds>) -> <bool>
  11461. BUILDIN_FUNC(sleep2)
  11462. {
  11463. int ticks;
  11464. ticks = script_getnum(st,2);
  11465. if( ticks <= 0 )
  11466. {// do nothing
  11467. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11468. }
  11469. else if( !st->sleep.tick )
  11470. {// sleep for the target amount of time
  11471. st->state = RERUNLINE;
  11472. st->sleep.tick = ticks;
  11473. }
  11474. else
  11475. {// sleep time is over
  11476. st->state = RUN;
  11477. st->sleep.tick = 0;
  11478. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11479. }
  11480. return 0;
  11481. }
  11482. /// Awakes all the sleep timers of the target npc
  11483. ///
  11484. /// awake "<npc name>";
  11485. BUILDIN_FUNC(awake)
  11486. {
  11487. struct npc_data* nd;
  11488. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11489. nd = npc_name2id(script_getstr(st, 2));
  11490. if( nd == NULL )
  11491. return 0;
  11492. while( node )
  11493. {
  11494. if( (int)node->key == nd->bl.id )
  11495. {// sleep timer for the npc
  11496. struct script_state* tst = (struct script_state*)node->data;
  11497. TBL_PC* sd = map_id2sd(tst->rid);
  11498. if( tst->sleep.timer == -1 )
  11499. {// already awake ???
  11500. node = node->next;
  11501. continue;
  11502. }
  11503. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11504. {// char not online anymore / another char of the same account is online - Cancel execution
  11505. tst->state = END;
  11506. tst->rid = 0;
  11507. }
  11508. delete_timer(tst->sleep.timer, run_script_timer);
  11509. node = script_erase_sleepdb(node);
  11510. tst->sleep.timer = INVALID_TIMER;
  11511. //tst->sleep.tick = 0;
  11512. run_script_main(tst);
  11513. }
  11514. else
  11515. {
  11516. node = node->next;
  11517. }
  11518. }
  11519. return 0;
  11520. }
  11521. /// Returns a reference to a variable of the target NPC.
  11522. /// Returns 0 if an error occurs.
  11523. ///
  11524. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11525. BUILDIN_FUNC(getvariableofnpc)
  11526. {
  11527. struct script_data* data;
  11528. const char* name;
  11529. struct npc_data* nd;
  11530. data = script_getdata(st,2);
  11531. if( !data_isreference(data) )
  11532. {// Not a reference (aka varaible name)
  11533. ShowError("script:getvariableofnpc: not a variable\n");
  11534. script_reportdata(data);
  11535. script_pushnil(st);
  11536. st->state = END;
  11537. return 1;
  11538. }
  11539. name = reference_getname(data);
  11540. if( *name != '.' || name[1] == '@' )
  11541. {// not a npc variable
  11542. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11543. script_reportdata(data);
  11544. script_pushnil(st);
  11545. st->state = END;
  11546. return 1;
  11547. }
  11548. nd = npc_name2id(script_getstr(st,3));
  11549. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11550. {// NPC not found or has no script
  11551. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11552. script_pushnil(st);
  11553. st->state = END;
  11554. return 1;
  11555. }
  11556. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11557. return 0;
  11558. }
  11559. /// Opens a warp portal.
  11560. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11561. ///
  11562. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11563. ///
  11564. /// @author blackhole89
  11565. BUILDIN_FUNC(warpportal)
  11566. {
  11567. int spx;
  11568. int spy;
  11569. unsigned short mapindex;
  11570. int tpx;
  11571. int tpy;
  11572. struct skill_unit_group* group;
  11573. struct block_list* bl;
  11574. bl = map_id2bl(st->oid);
  11575. if( bl == NULL )
  11576. {
  11577. ShowError("script:warpportal: npc is needed\n");
  11578. return 1;
  11579. }
  11580. spx = script_getnum(st,2);
  11581. spy = script_getnum(st,3);
  11582. mapindex = mapindex_name2id(script_getstr(st, 4));
  11583. tpx = script_getnum(st,5);
  11584. tpy = script_getnum(st,6);
  11585. if( mapindex == 0 )
  11586. return 0;// map not found
  11587. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11588. if( group == NULL )
  11589. return 0;// failed
  11590. group->val2 = (tpx<<16) | tpy;
  11591. group->val3 = mapindex;
  11592. return 0;
  11593. }
  11594. BUILDIN_FUNC(openmail)
  11595. {
  11596. TBL_PC* sd;
  11597. sd = script_rid2sd(st);
  11598. if( sd == NULL )
  11599. return 0;
  11600. #ifndef TXT_ONLY
  11601. mail_openmail(sd);
  11602. #endif
  11603. return 0;
  11604. }
  11605. BUILDIN_FUNC(openauction)
  11606. {
  11607. TBL_PC* sd;
  11608. sd = script_rid2sd(st);
  11609. if( sd == NULL )
  11610. return 0;
  11611. #ifndef TXT_ONLY
  11612. clif_Auction_openwindow(sd);
  11613. #endif
  11614. return 0;
  11615. }
  11616. /// Retrieves the value of the specified flag of the specified cell.
  11617. ///
  11618. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  11619. ///
  11620. /// @see cell_chk* constants in const.txt for the types
  11621. BUILDIN_FUNC(checkcell)
  11622. {
  11623. int m = map_mapname2mapid(script_getstr(st,2));
  11624. int x = script_getnum(st,3);
  11625. int y = script_getnum(st,4);
  11626. cell_chk type = (cell_chk)script_getnum(st,5);
  11627. script_pushint(st, map_getcell(m, x, y, type));
  11628. return 0;
  11629. }
  11630. /// Modifies flags of cells in the specified area.
  11631. ///
  11632. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11633. ///
  11634. /// @see cell_* constants in const.txt for the types
  11635. BUILDIN_FUNC(setcell)
  11636. {
  11637. int m = map_mapname2mapid(script_getstr(st,2));
  11638. int x1 = script_getnum(st,3);
  11639. int y1 = script_getnum(st,4);
  11640. int x2 = script_getnum(st,5);
  11641. int y2 = script_getnum(st,6);
  11642. cell_t type = (cell_t)script_getnum(st,7);
  11643. bool flag = (bool)script_getnum(st,8);
  11644. int x,y;
  11645. if( x1 > x2 ) swap(x1,x2);
  11646. if( y1 > y2 ) swap(y1,y2);
  11647. for( y = y1; y <= y2; ++y )
  11648. for( x = x1; x <= x2; ++x )
  11649. map_setcell(m, x, y, type, flag);
  11650. return 0;
  11651. }
  11652. /*==========================================
  11653. * Mercenary Commands
  11654. *------------------------------------------*/
  11655. BUILDIN_FUNC(mercenary_create)
  11656. {
  11657. struct map_session_data *sd;
  11658. int class_, contract_time;
  11659. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  11660. return 0;
  11661. class_ = script_getnum(st,2);
  11662. if( !merc_class(class_) )
  11663. return 0;
  11664. contract_time = script_getnum(st,3);
  11665. merc_create(sd, class_, contract_time);
  11666. return 0;
  11667. }
  11668. BUILDIN_FUNC(mercenary_heal)
  11669. {
  11670. struct map_session_data *sd = script_rid2sd(st);
  11671. int hp, sp;
  11672. if( sd == NULL || sd->md == NULL )
  11673. return 0;
  11674. hp = script_getnum(st,2);
  11675. sp = script_getnum(st,3);
  11676. status_heal(&sd->md->bl, hp, sp, 0);
  11677. return 0;
  11678. }
  11679. BUILDIN_FUNC(mercenary_sc_start)
  11680. {
  11681. struct map_session_data *sd = script_rid2sd(st);
  11682. enum sc_type type;
  11683. int tick, val1;
  11684. if( sd == NULL || sd->md == NULL )
  11685. return 0;
  11686. type = (sc_type)script_getnum(st,2);
  11687. tick = script_getnum(st,3);
  11688. val1 = script_getnum(st,4);
  11689. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  11690. return 0;
  11691. }
  11692. BUILDIN_FUNC(mercenary_get_calls)
  11693. {
  11694. struct map_session_data *sd = script_rid2sd(st);
  11695. int guild;
  11696. if( sd == NULL )
  11697. return 0;
  11698. guild = script_getnum(st,2);
  11699. switch( guild )
  11700. {
  11701. case ARCH_MERC_GUILD:
  11702. script_pushint(st,sd->status.arch_calls);
  11703. break;
  11704. case SPEAR_MERC_GUILD:
  11705. script_pushint(st,sd->status.spear_calls);
  11706. break;
  11707. case SWORD_MERC_GUILD:
  11708. script_pushint(st,sd->status.sword_calls);
  11709. break;
  11710. default:
  11711. script_pushint(st,0);
  11712. break;
  11713. }
  11714. return 0;
  11715. }
  11716. BUILDIN_FUNC(mercenary_set_calls)
  11717. {
  11718. struct map_session_data *sd = script_rid2sd(st);
  11719. int guild, value, *calls;
  11720. if( sd == NULL )
  11721. return 0;
  11722. guild = script_getnum(st,2);
  11723. value = script_getnum(st,3);
  11724. switch( guild )
  11725. {
  11726. case ARCH_MERC_GUILD:
  11727. calls = &sd->status.arch_calls;
  11728. break;
  11729. case SPEAR_MERC_GUILD:
  11730. calls = &sd->status.spear_calls;
  11731. break;
  11732. case SWORD_MERC_GUILD:
  11733. calls = &sd->status.sword_calls;
  11734. break;
  11735. default:
  11736. return 0; // Invalid Guild
  11737. }
  11738. *calls += value;
  11739. *calls = cap_value(*calls, 0, INT_MAX);
  11740. return 0;
  11741. }
  11742. BUILDIN_FUNC(mercenary_get_faith)
  11743. {
  11744. struct map_session_data *sd = script_rid2sd(st);
  11745. int guild;
  11746. if( sd == NULL )
  11747. return 0;
  11748. guild = script_getnum(st,2);
  11749. switch( guild )
  11750. {
  11751. case ARCH_MERC_GUILD:
  11752. script_pushint(st,sd->status.arch_faith);
  11753. break;
  11754. case SPEAR_MERC_GUILD:
  11755. script_pushint(st,sd->status.spear_faith);
  11756. break;
  11757. case SWORD_MERC_GUILD:
  11758. script_pushint(st,sd->status.sword_faith);
  11759. break;
  11760. default:
  11761. script_pushint(st,0);
  11762. break;
  11763. }
  11764. return 0;
  11765. }
  11766. BUILDIN_FUNC(mercenary_set_faith)
  11767. {
  11768. struct map_session_data *sd = script_rid2sd(st);
  11769. int guild, value, *calls;
  11770. if( sd == NULL )
  11771. return 0;
  11772. guild = script_getnum(st,2);
  11773. value = script_getnum(st,3);
  11774. switch( guild )
  11775. {
  11776. case ARCH_MERC_GUILD:
  11777. calls = &sd->status.arch_faith;
  11778. break;
  11779. case SPEAR_MERC_GUILD:
  11780. calls = &sd->status.spear_faith;
  11781. break;
  11782. case SWORD_MERC_GUILD:
  11783. calls = &sd->status.sword_faith;
  11784. break;
  11785. default:
  11786. return 0; // Invalid Guild
  11787. }
  11788. *calls += value;
  11789. *calls = cap_value(*calls, 0, INT_MAX);
  11790. if( mercenary_get_guild(sd->md) == guild )
  11791. clif_mercenary_updatestatus(sd,SP_MERCFAITH);
  11792. return 0;
  11793. }
  11794. /*------------------------------------------
  11795. * Book Reading
  11796. *------------------------------------------*/
  11797. BUILDIN_FUNC(readbook)
  11798. {
  11799. struct map_session_data *sd;
  11800. int book_id, page;
  11801. if( (sd = script_rid2sd(st)) == NULL )
  11802. return 0;
  11803. book_id = script_getnum(st,2);
  11804. page = script_getnum(st,3);
  11805. clif_readbook(sd->fd, book_id, page);
  11806. return 0;
  11807. }
  11808. /******************
  11809. Questlog script commands
  11810. *******************/
  11811. BUILDIN_FUNC(setquest)
  11812. {
  11813. TBL_PC * sd = script_rid2sd(st);
  11814. quest_add(sd, script_getnum(st, 2));
  11815. return 0;
  11816. }
  11817. BUILDIN_FUNC(erasequest)
  11818. {
  11819. TBL_PC * sd = script_rid2sd(st);
  11820. quest_delete(sd, script_getnum(st, 2));
  11821. return 0;
  11822. }
  11823. BUILDIN_FUNC(completequest)
  11824. {
  11825. TBL_PC * sd = script_rid2sd(st);
  11826. quest_update_status(sd, script_getnum(st, 2), Q_COMPLETE);
  11827. return 0;
  11828. }
  11829. BUILDIN_FUNC(changequest)
  11830. {
  11831. TBL_PC * sd = script_rid2sd(st);
  11832. quest_change(sd, script_getnum(st, 2),script_getnum(st, 3));
  11833. return 0;
  11834. }
  11835. BUILDIN_FUNC(checkquest)
  11836. {
  11837. TBL_PC * sd = script_rid2sd(st);
  11838. quest_check_type type = HAVEQUEST;
  11839. if( script_hasdata(st, 3) )
  11840. type = (quest_check_type)script_getnum(st, 3);
  11841. script_pushint(st, quest_check(sd, script_getnum(st, 2), type));
  11842. return 0;
  11843. }
  11844. /*==========================================
  11845. * BattleGround System
  11846. *------------------------------------------*/
  11847. BUILDIN_FUNC(waitingroom2bg)
  11848. {
  11849. struct npc_data *nd;
  11850. struct chat_data *cd;
  11851. const char *map_name, *ev = "", *dev = "";
  11852. int x, y, i, mapindex = 0, bg_id, n;
  11853. struct map_session_data *sd;
  11854. if( script_hasdata(st,7) )
  11855. nd = npc_name2id(script_getstr(st,7));
  11856. else
  11857. nd = (struct npc_data *)map_id2bl(st->oid);
  11858. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  11859. {
  11860. script_pushint(st,0);
  11861. return 0;
  11862. }
  11863. map_name = script_getstr(st,2);
  11864. if( strcmp(map_name,"-") != 0 )
  11865. {
  11866. mapindex = mapindex_name2id(map_name);
  11867. if( mapindex == 0 )
  11868. { // Invalid Map
  11869. script_pushint(st,0);
  11870. return 0;
  11871. }
  11872. }
  11873. x = script_getnum(st,3);
  11874. y = script_getnum(st,4);
  11875. ev = script_getstr(st,5); // Logout Event
  11876. dev = script_getstr(st,6); // Die Event
  11877. if( (bg_id = bg_create(mapindex, x, y, ev, dev)) == 0 )
  11878. { // Creation failed
  11879. script_pushint(st,0);
  11880. return 0;
  11881. }
  11882. n = cd->users;
  11883. for( i = 0; i < n && i < MAX_BG_MEMBERS; i++ )
  11884. {
  11885. if( (sd = cd->usersd[i]) != NULL && bg_team_join(bg_id, sd) )
  11886. mapreg_setreg(add_str("$@arenamembers") + (i<<24), sd->bl.id);
  11887. else
  11888. mapreg_setreg(add_str("$@arenamembers") + (i<<24), 0);
  11889. }
  11890. mapreg_setreg(add_str("$@arenamembersnum"), i);
  11891. script_pushint(st,bg_id);
  11892. return 0;
  11893. }
  11894. BUILDIN_FUNC(waitingroom2bg_single)
  11895. {
  11896. const char* map_name;
  11897. struct npc_data *nd;
  11898. struct chat_data *cd;
  11899. struct map_session_data *sd;
  11900. int x, y, mapindex, bg_id;
  11901. bg_id = script_getnum(st,2);
  11902. map_name = script_getstr(st,3);
  11903. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  11904. return 0; // Invalid Map
  11905. x = script_getnum(st,4);
  11906. y = script_getnum(st,5);
  11907. nd = npc_name2id(script_getstr(st,6));
  11908. if( nd == NULL || (cd = (struct chat_data *)map_id2bl(nd->chat_id)) == NULL || cd->users <= 0 )
  11909. return 0;
  11910. if( (sd = cd->usersd[0]) == NULL )
  11911. return 0;
  11912. if( bg_team_join(bg_id, sd) )
  11913. {
  11914. pc_setpos(sd, mapindex, x, y, 3);
  11915. script_pushint(st,1);
  11916. }
  11917. else
  11918. script_pushint(st,0);
  11919. return 0;
  11920. }
  11921. BUILDIN_FUNC(bg_team_setxy)
  11922. {
  11923. struct battleground_data *bg;
  11924. int bg_id;
  11925. bg_id = script_getnum(st,2);
  11926. if( (bg = bg_team_search(bg_id)) == NULL )
  11927. return 0;
  11928. bg->x = script_getnum(st,3);
  11929. bg->y = script_getnum(st,4);
  11930. return 0;
  11931. }
  11932. BUILDIN_FUNC(bg_warp)
  11933. {
  11934. int x, y, mapindex, bg_id;
  11935. const char* map_name;
  11936. bg_id = script_getnum(st,2);
  11937. map_name = script_getstr(st,3);
  11938. if( (mapindex = mapindex_name2id(map_name)) == 0 )
  11939. return 0; // Invalid Map
  11940. x = script_getnum(st,4);
  11941. y = script_getnum(st,5);
  11942. bg_team_warp(bg_id, mapindex, x, y);
  11943. return 0;
  11944. }
  11945. BUILDIN_FUNC(bg_monster)
  11946. {
  11947. int class_ = 0, x = 0, y = 0, bg_id = 0;
  11948. const char *str,*map, *evt="";
  11949. bg_id = script_getnum(st,2);
  11950. map = script_getstr(st,3);
  11951. x = script_getnum(st,4);
  11952. y = script_getnum(st,5);
  11953. str = script_getstr(st,6);
  11954. class_ = script_getnum(st,7);
  11955. if( script_hasdata(st,8) ) evt = script_getstr(st,8);
  11956. check_event(st, evt);
  11957. script_pushint(st, mob_spawn_bg(map,x,y,str,class_,evt,bg_id));
  11958. return 0;
  11959. }
  11960. BUILDIN_FUNC(bg_monster_set_team)
  11961. {
  11962. struct mob_data *md;
  11963. struct block_list *mbl;
  11964. int id = script_getnum(st,2),
  11965. bg_id = script_getnum(st,3);
  11966. if( (mbl = map_id2bl(id)) == NULL || mbl->type != BL_MOB )
  11967. return 0;
  11968. md = (TBL_MOB *)mbl;
  11969. md->state.bg_id = bg_id;
  11970. mob_stop_attack(md);
  11971. mob_stop_walking(md, 0);
  11972. md->target_id = md->attacked_id = 0;
  11973. clif_charnameack(0, &md->bl);
  11974. return 0;
  11975. }
  11976. BUILDIN_FUNC(bg_leave)
  11977. {
  11978. struct map_session_data *sd = script_rid2sd(st);
  11979. if( sd == NULL || !sd->state.bg_id )
  11980. return 0;
  11981. bg_team_leave(sd,0);
  11982. return 0;
  11983. }
  11984. BUILDIN_FUNC(bg_destroy)
  11985. {
  11986. int bg_id = script_getnum(st,2);
  11987. bg_team_delete(bg_id);
  11988. return 0;
  11989. }
  11990. BUILDIN_FUNC(bg_getareausers)
  11991. {
  11992. const char *str;
  11993. int m, x0, y0, x1, y1, bg_id;
  11994. int i = 0, c = 0;
  11995. struct battleground_data *bg = NULL;
  11996. struct map_session_data *sd;
  11997. bg_id = script_getnum(st,2);
  11998. str = script_getstr(st,3);
  11999. if( (bg = bg_team_search(bg_id)) == NULL || (m = map_mapname2mapid(str)) < 0 )
  12000. {
  12001. script_pushint(st,0);
  12002. return 0;
  12003. }
  12004. x0 = script_getnum(st,4);
  12005. y0 = script_getnum(st,5);
  12006. x1 = script_getnum(st,6);
  12007. y1 = script_getnum(st,7);
  12008. for( i = 0; i < MAX_BG_MEMBERS; i++ )
  12009. {
  12010. if( (sd = bg->members[i].sd) == NULL )
  12011. continue;
  12012. if( sd->bl.m != m || sd->bl.x < x0 || sd->bl.y < y0 || sd->bl.x > x1 || sd->bl.y > y1 )
  12013. continue;
  12014. c++;
  12015. }
  12016. script_pushint(st,c);
  12017. return 0;
  12018. }
  12019. BUILDIN_FUNC(bg_updatescore)
  12020. {
  12021. const char *str;
  12022. int m;
  12023. str = script_getstr(st,2);
  12024. if( (m = map_mapname2mapid(str)) < 0 )
  12025. return 0;
  12026. map[m].bgscore_lion = script_getnum(st,3);
  12027. map[m].bgscore_eagle = script_getnum(st,4);
  12028. clif_bg_updatescore(m);
  12029. return 0;
  12030. }
  12031. BUILDIN_FUNC(bg_get_data)
  12032. {
  12033. struct battleground_data *bg;
  12034. int bg_id = script_getnum(st,2),
  12035. type = script_getnum(st,3);
  12036. if( (bg = bg_team_search(bg_id)) == NULL )
  12037. {
  12038. script_pushint(st,0);
  12039. return 0;
  12040. }
  12041. switch( type )
  12042. {
  12043. case 0: script_pushint(st, bg->count); break;
  12044. default:
  12045. ShowError("script:bg_get_data: unknown data identifier %d\n", type);
  12046. break;
  12047. }
  12048. return 0;
  12049. }
  12050. /*==========================================
  12051. * Instancing Script Commands
  12052. *------------------------------------------*/
  12053. BUILDIN_FUNC(instance_create)
  12054. {
  12055. const char *name;
  12056. int party_id, res;
  12057. name = script_getstr(st, 2);
  12058. party_id = script_getnum(st, 3);
  12059. res = instance_create(party_id, name);
  12060. if( res == -4 ) // Already exists
  12061. {
  12062. script_pushint(st, -1);
  12063. return 0;
  12064. }
  12065. else if( res < 0 )
  12066. {
  12067. char *err;
  12068. switch(res)
  12069. {
  12070. case -3: err = "No free instances"; break;
  12071. case -2: err = "Missing parameter"; break;
  12072. case -1: err = "Invalid type"; break;
  12073. default: err = "Unknown"; break;
  12074. }
  12075. ShowError("buildin_instance_create: %s [%d].\n", err, res);
  12076. script_pushint(st, -2);
  12077. return 0;
  12078. }
  12079. script_pushint(st, res);
  12080. return 0;
  12081. }
  12082. BUILDIN_FUNC(instance_destroy)
  12083. {
  12084. int instance_id;
  12085. struct map_session_data *sd;
  12086. struct party_data *p;
  12087. if( script_hasdata(st, 2) )
  12088. instance_id = script_getnum(st, 2);
  12089. else if( st->instance_id )
  12090. instance_id = st->instance_id;
  12091. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12092. instance_id = p->instance_id;
  12093. else return 0;
  12094. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12095. {
  12096. ShowError("buildin_instance_destroy: Trying to destroy invalid instance %d.\n", instance_id);
  12097. return 0;
  12098. }
  12099. instance_destroy(instance_id);
  12100. return 0;
  12101. }
  12102. BUILDIN_FUNC(instance_attachmap)
  12103. {
  12104. const char *name;
  12105. int m;
  12106. int instance_id;
  12107. bool usebasename = false;
  12108. name = script_getstr(st,2);
  12109. instance_id = script_getnum(st,3);
  12110. if( script_hasdata(st,4) && script_getnum(st,4) > 0)
  12111. usebasename = true;
  12112. if( (m = instance_add_map(name, instance_id, usebasename)) < 0 ) // [Saithis]
  12113. {
  12114. ShowError("buildin_instance_attachmap: instance creation failed (%s): %d\n", name, m);
  12115. script_pushconststr(st, "");
  12116. return 0;
  12117. }
  12118. script_pushconststr(st, map[m].name);
  12119. return 0;
  12120. }
  12121. BUILDIN_FUNC(instance_detachmap)
  12122. {
  12123. struct map_session_data *sd;
  12124. struct party_data *p;
  12125. const char *str;
  12126. int m, instance_id;
  12127. str = script_getstr(st, 2);
  12128. if( script_hasdata(st, 3) )
  12129. instance_id = script_getnum(st, 3);
  12130. else if( st->instance_id )
  12131. instance_id = st->instance_id;
  12132. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12133. instance_id = p->instance_id;
  12134. else return 0;
  12135. if( (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m,instance_id)) < 0 )
  12136. {
  12137. ShowError("buildin_instance_detachmap: Trying to detach invalid map %s\n", str);
  12138. return 0;
  12139. }
  12140. instance_del_map(m);
  12141. return 0;
  12142. }
  12143. BUILDIN_FUNC(instance_attach)
  12144. {
  12145. int instance_id;
  12146. instance_id = script_getnum(st, 2);
  12147. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12148. return 0;
  12149. st->instance_id = instance_id;
  12150. return 0;
  12151. }
  12152. BUILDIN_FUNC(instance_id)
  12153. {
  12154. int type, instance_id;
  12155. struct map_session_data *sd;
  12156. struct party_data *p;
  12157. if( script_hasdata(st, 2) )
  12158. {
  12159. type = script_getnum(st, 2);
  12160. if( type == 0 )
  12161. instance_id = st->instance_id;
  12162. else if( type == 1 && (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL )
  12163. instance_id = p->instance_id;
  12164. else
  12165. instance_id = 0;
  12166. }
  12167. else
  12168. instance_id = st->instance_id;
  12169. script_pushint(st, instance_id);
  12170. return 0;
  12171. }
  12172. BUILDIN_FUNC(instance_set_timeout)
  12173. {
  12174. int progress_timeout, idle_timeout;
  12175. int instance_id;
  12176. struct map_session_data *sd;
  12177. struct party_data *p;
  12178. progress_timeout = script_getnum(st, 2);
  12179. idle_timeout = script_getnum(st, 3);
  12180. if( script_hasdata(st, 4) )
  12181. instance_id = script_getnum(st, 4);
  12182. else if( st->instance_id )
  12183. instance_id = st->instance_id;
  12184. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12185. instance_id = p->instance_id;
  12186. else return 0;
  12187. if( instance_id > 0 )
  12188. instance_set_timeout(instance_id, progress_timeout, idle_timeout);
  12189. return 0;
  12190. }
  12191. BUILDIN_FUNC(instance_init)
  12192. {
  12193. int instance_id = script_getnum(st, 2);
  12194. instance_init(instance_id);
  12195. return 0;
  12196. }
  12197. BUILDIN_FUNC(instance_announce)
  12198. {
  12199. const char *str, *color=NULL;
  12200. int flag,instance_id,i;
  12201. struct map_session_data *sd;
  12202. struct party_data *p;
  12203. instance_id = script_getnum(st,2);
  12204. str = script_getstr(st,3);
  12205. flag = script_getnum(st,4);
  12206. if( script_hasdata(st,5) )
  12207. color = script_getstr(st,5);
  12208. if( instance_id == 0 )
  12209. {
  12210. if( st->instance_id )
  12211. instance_id = st->instance_id;
  12212. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12213. instance_id = p->instance_id;
  12214. else return 0;
  12215. }
  12216. if( instance_id <= 0 || instance_id >= MAX_INSTANCE )
  12217. return 0;
  12218. for( i = 0; i < instance[instance_id].num_map; i++ )
  12219. map_foreachinmap(buildin_mapannounce_sub, instance[instance_id].map[i], BL_PC, str, strlen(str) + 1, flag&0x10, color);
  12220. return 0;
  12221. }
  12222. BUILDIN_FUNC(instance_npcname)
  12223. {
  12224. const char *str;
  12225. int instance_id = 0;
  12226. struct map_session_data *sd;
  12227. struct party_data *p;
  12228. struct npc_data *nd;
  12229. str = script_getstr(st, 2);
  12230. if( script_hasdata(st, 3) )
  12231. instance_id = script_getnum(st, 3);
  12232. else if( st->instance_id )
  12233. instance_id = st->instance_id;
  12234. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12235. instance_id = p->instance_id;
  12236. if( instance_id && (nd = npc_name2id(str)) != NULL )
  12237. {
  12238. static char npcname[NAME_LENGTH];
  12239. snprintf(npcname, sizeof(npcname), "dup_%d_%d", instance_id, nd->bl.id);
  12240. script_pushconststr(st,npcname);
  12241. }
  12242. else
  12243. script_pushconststr(st,"");
  12244. return 0;
  12245. }
  12246. BUILDIN_FUNC(has_instance)
  12247. {
  12248. struct map_session_data *sd;
  12249. struct party_data *p;
  12250. const char *str;
  12251. int m, instance_id = 0;
  12252. str = script_getstr(st, 2);
  12253. if( script_hasdata(st, 3) )
  12254. instance_id = script_getnum(st, 3);
  12255. else if( st->instance_id )
  12256. instance_id = st->instance_id;
  12257. else if( (sd = script_rid2sd(st)) != NULL && sd->status.party_id && (p = party_search(sd->status.party_id)) != NULL && p->instance_id )
  12258. instance_id = p->instance_id;
  12259. if( !instance_id || (m = map_mapname2mapid(str)) < 0 || (m = instance_map2imap(m, instance_id)) < 0 )
  12260. {
  12261. script_pushconststr(st, "");
  12262. return 0;
  12263. }
  12264. script_pushconststr(st, map[m].name);
  12265. return 0;
  12266. }
  12267. BUILDIN_FUNC(instance_warpall)
  12268. {
  12269. struct map_session_data *pl_sd;
  12270. int m, i, instance_id;
  12271. const char *mapn;
  12272. int x, y;
  12273. unsigned short mapindex;
  12274. struct party_data *p = NULL;
  12275. mapn = script_getstr(st,2);
  12276. x = script_getnum(st,3);
  12277. y = script_getnum(st,4);
  12278. if( script_hasdata(st,5) )
  12279. instance_id = script_getnum(st,5);
  12280. else if( st->instance_id )
  12281. instance_id = st->instance_id;
  12282. else if( (pl_sd = script_rid2sd(st)) != NULL && pl_sd->status.party_id && (p = party_search(pl_sd->status.party_id)) != NULL && p->instance_id )
  12283. instance_id = p->instance_id;
  12284. else return 0;
  12285. if( (m = map_mapname2mapid(mapn)) < 0 || (map[m].flag.src4instance && (m = instance_mapid2imapid(m, instance_id)) < 0) )
  12286. return 0;
  12287. if( !(p = party_search(instance[instance_id].party_id)) )
  12288. return 0;
  12289. mapindex = map_id2index(m);
  12290. for( i = 0; i < MAX_PARTY; i++ )
  12291. if( (pl_sd = p->data[i].sd) && map[pl_sd->bl.m].instance_id == st->instance_id ) pc_setpos(pl_sd,mapindex,x,y,3);
  12292. return 0;
  12293. }
  12294. /*==========================================
  12295. * Custom Fonts
  12296. *------------------------------------------*/
  12297. BUILDIN_FUNC(setfont)
  12298. {
  12299. struct map_session_data *sd = script_rid2sd(st);
  12300. int font = script_getnum(st,2);
  12301. if( sd == NULL )
  12302. return 0;
  12303. if( sd->state.user_font != font )
  12304. sd->state.user_font = font;
  12305. else
  12306. sd->state.user_font = 0;
  12307. clif_font_area(sd);
  12308. return 0;
  12309. }
  12310. static int buildin_mobuseskill_sub(struct block_list *bl,va_list ap)
  12311. {
  12312. TBL_MOB* md = (TBL_MOB*)bl;
  12313. struct block_list *tbl;
  12314. int mobid = va_arg(ap,int);
  12315. int skillid = va_arg(ap,int);
  12316. int skilllv = va_arg(ap,int);
  12317. int casttime = va_arg(ap,int);
  12318. int cancel = va_arg(ap,int);
  12319. int emotion = va_arg(ap,int);
  12320. int target = va_arg(ap,int);
  12321. if( md->class_ != mobid )
  12322. return 0;
  12323. if( md->ud.skilltimer != -1 ) // Cancel the casting skill.
  12324. unit_skillcastcancel(bl,0);
  12325. // 0:self, 1:target, 2:master, default:random
  12326. switch( target )
  12327. {
  12328. case 0: tbl = map_id2bl(md->bl.id); break;
  12329. case 1: tbl = map_id2bl(md->target_id); break;
  12330. case 2: tbl = map_id2bl(md->master_id); break;
  12331. default:tbl = battle_getenemy(&md->bl, DEFAULT_ENEMY_TYPE(md),skill_get_range2(&md->bl, skillid, skilllv)); break;
  12332. }
  12333. if( !tbl )
  12334. return 0;
  12335. if( skill_get_casttype(skillid) == CAST_GROUND )
  12336. unit_skilluse_pos2(&md->bl, tbl->x, tbl->y, skillid, skilllv, casttime, cancel);
  12337. else
  12338. unit_skilluse_id2(&md->bl, tbl->id, skillid, skilllv, casttime, cancel);
  12339. clif_emotion(&md->bl, emotion);
  12340. return 0;
  12341. }
  12342. /*==========================================
  12343. * areamobuseskill "Map Name",<x>,<y>,<range>,<Mob ID>,"Skill Name"/<Skill ID>,<Skill Lv>,<Cast Time>,<Cancelable>,<Emotion>,<Target Type>;
  12344. *------------------------------------------*/
  12345. BUILDIN_FUNC(areamobuseskill)
  12346. {
  12347. struct block_list center;
  12348. int m,range,mobid,skillid,skilllv,casttime,emotion,target,cancel;
  12349. if( (m = map_mapname2mapid(script_getstr(st,2))) < 0 )
  12350. {
  12351. ShowError("areamobuseskill: invalid map name.\n");
  12352. return 0;
  12353. }
  12354. if( map[m].flag.src4instance && st->instance_id && (m = instance_mapid2imapid(m, st->instance_id)) < 0 )
  12355. return 0;
  12356. center.m = m;
  12357. center.x = script_getnum(st,3);
  12358. center.y = script_getnum(st,4);
  12359. range = script_getnum(st,5);
  12360. mobid = script_getnum(st,6);
  12361. skillid = ( script_isstring(st,7) ? skill_name2id(script_getstr(st,7)) : script_getnum(st,7) );
  12362. if( (skilllv = script_getnum(st,8)) > battle_config.mob_max_skilllvl )
  12363. skilllv = battle_config.mob_max_skilllvl;
  12364. casttime = script_getnum(st,9);
  12365. cancel = script_getnum(st,10);
  12366. emotion = script_getnum(st,11);
  12367. target = script_getnum(st,12);
  12368. map_foreachinrange(buildin_mobuseskill_sub, &center, range, BL_MOB, mobid, skillid, skilllv, casttime, cancel, emotion, target);
  12369. return 0;
  12370. }
  12371. BUILDIN_FUNC(progressbar)
  12372. {
  12373. #if PACKETVER >= 20080318
  12374. struct map_session_data * sd = script_rid2sd(st);
  12375. const char * color;
  12376. unsigned int second;
  12377. if( !st || !sd )
  12378. return 0;
  12379. st->state = STOP;
  12380. color = script_getstr(st,2);
  12381. second = script_getnum(st,3);
  12382. sd->progressbar.npc_id = st->oid;
  12383. sd->progressbar.timeout = gettick() + second*1000;
  12384. clif_progressbar(sd, strtol(color, (char **)NULL, 0), second);
  12385. #endif
  12386. return 0;
  12387. }
  12388. // declarations that were supposed to be exported from npc_chat.c
  12389. #ifdef PCRE_SUPPORT
  12390. BUILDIN_FUNC(defpattern);
  12391. BUILDIN_FUNC(activatepset);
  12392. BUILDIN_FUNC(deactivatepset);
  12393. BUILDIN_FUNC(deletepset);
  12394. #endif
  12395. struct script_function buildin_func[] = {
  12396. // NPC interaction
  12397. BUILDIN_DEF(mes,"s"),
  12398. BUILDIN_DEF(next,""),
  12399. BUILDIN_DEF(close,""),
  12400. BUILDIN_DEF(close2,""),
  12401. BUILDIN_DEF(menu,"sl*"),
  12402. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  12403. BUILDIN_DEF(prompt,"s*"),
  12404. //
  12405. BUILDIN_DEF(goto,"l"),
  12406. BUILDIN_DEF(callsub,"i*"),
  12407. BUILDIN_DEF(callfunc,"s*"),
  12408. BUILDIN_DEF(return,"?"),
  12409. BUILDIN_DEF(getarg,"i?"),
  12410. BUILDIN_DEF(jobchange,"i*"),
  12411. BUILDIN_DEF(jobname,"i"),
  12412. BUILDIN_DEF(input,"v??"),
  12413. BUILDIN_DEF(warp,"sii"),
  12414. BUILDIN_DEF(areawarp,"siiiisii"),
  12415. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  12416. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  12417. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  12418. BUILDIN_DEF(setlook,"ii"),
  12419. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  12420. BUILDIN_DEF(set,"ii"),
  12421. BUILDIN_DEF(setarray,"rv*"),
  12422. BUILDIN_DEF(cleararray,"rvi"),
  12423. BUILDIN_DEF(copyarray,"rri"),
  12424. BUILDIN_DEF(getarraysize,"r"),
  12425. BUILDIN_DEF(deletearray,"r?"),
  12426. BUILDIN_DEF(getelementofarray,"ri"),
  12427. BUILDIN_DEF(getitem,"vi?"),
  12428. BUILDIN_DEF(rentitem,"vi"),
  12429. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  12430. BUILDIN_DEF(getnameditem,"is"),
  12431. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  12432. BUILDIN_DEF(makeitem,"iisii"),
  12433. BUILDIN_DEF(delitem,"ii?"),
  12434. BUILDIN_DEF(delitem2,"iiiiiiiii?"),
  12435. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  12436. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  12437. BUILDIN_DEF(cutin,"si"),
  12438. BUILDIN_DEF(viewpoint,"iiiii"),
  12439. BUILDIN_DEF(heal,"ii"),
  12440. BUILDIN_DEF(itemheal,"ii"),
  12441. BUILDIN_DEF(percentheal,"ii"),
  12442. BUILDIN_DEF(rand,"i?"),
  12443. BUILDIN_DEF(countitem,"i"),
  12444. BUILDIN_DEF(countitem2,"iiiiiiii"),
  12445. BUILDIN_DEF(checkweight,"ii"),
  12446. BUILDIN_DEF(readparam,"i*"),
  12447. BUILDIN_DEF(getcharid,"i*"),
  12448. BUILDIN_DEF(getpartyname,"i"),
  12449. BUILDIN_DEF(getpartymember,"i*"),
  12450. BUILDIN_DEF(getpartyleader,"i?"),
  12451. BUILDIN_DEF(getguildname,"i"),
  12452. BUILDIN_DEF(getguildmaster,"i"),
  12453. BUILDIN_DEF(getguildmasterid,"i"),
  12454. BUILDIN_DEF(strcharinfo,"i"),
  12455. BUILDIN_DEF(strnpcinfo,"i"),
  12456. BUILDIN_DEF(getequipid,"i"),
  12457. BUILDIN_DEF(getequipname,"i"),
  12458. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  12459. BUILDIN_DEF(repair,"i"), // [Valaris]
  12460. BUILDIN_DEF(getequipisequiped,"i"),
  12461. BUILDIN_DEF(getequipisenableref,"i"),
  12462. BUILDIN_DEF(getequipisidentify,"i"),
  12463. BUILDIN_DEF(getequiprefinerycnt,"i"),
  12464. BUILDIN_DEF(getequipweaponlv,"i"),
  12465. BUILDIN_DEF(getequippercentrefinery,"i"),
  12466. BUILDIN_DEF(successrefitem,"i"),
  12467. BUILDIN_DEF(failedrefitem,"i"),
  12468. BUILDIN_DEF(statusup,"i"),
  12469. BUILDIN_DEF(statusup2,"ii"),
  12470. BUILDIN_DEF(bonus,"iv"),
  12471. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  12472. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  12473. BUILDIN_DEF2(bonus,"bonus4","ivvii"),
  12474. BUILDIN_DEF2(bonus,"bonus5","ivviii"),
  12475. BUILDIN_DEF(autobonus,"sii??"),
  12476. BUILDIN_DEF(autobonus2,"sii??"),
  12477. BUILDIN_DEF(autobonus3,"siiv?"),
  12478. BUILDIN_DEF(skill,"vi?"),
  12479. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  12480. BUILDIN_DEF(guildskill,"vi"),
  12481. BUILDIN_DEF(getskilllv,"v"),
  12482. BUILDIN_DEF(getgdskilllv,"iv"),
  12483. BUILDIN_DEF(basicskillcheck,""),
  12484. BUILDIN_DEF(getgmlevel,""),
  12485. BUILDIN_DEF(end,""),
  12486. BUILDIN_DEF(checkoption,"i"),
  12487. BUILDIN_DEF(setoption,"i?"),
  12488. BUILDIN_DEF(setcart,"?"),
  12489. BUILDIN_DEF(checkcart,""),
  12490. BUILDIN_DEF(setfalcon,"?"),
  12491. BUILDIN_DEF(checkfalcon,""),
  12492. BUILDIN_DEF(setriding,"?"),
  12493. BUILDIN_DEF(checkriding,""),
  12494. BUILDIN_DEF2(savepoint,"save","sii"),
  12495. BUILDIN_DEF(savepoint,"sii"),
  12496. BUILDIN_DEF(gettimetick,"i"),
  12497. BUILDIN_DEF(gettime,"i"),
  12498. BUILDIN_DEF(gettimestr,"si"),
  12499. BUILDIN_DEF(openstorage,""),
  12500. BUILDIN_DEF(guildopenstorage,"*"),
  12501. BUILDIN_DEF(itemskill,"vi"),
  12502. BUILDIN_DEF(produce,"i"),
  12503. BUILDIN_DEF(cooking,"i"),
  12504. BUILDIN_DEF(monster,"siisii*"),
  12505. BUILDIN_DEF(getmobdrops,"i"),
  12506. BUILDIN_DEF(areamonster,"siiiisii*"),
  12507. BUILDIN_DEF(killmonster,"ss?"),
  12508. BUILDIN_DEF(killmonsterall,"s?"),
  12509. BUILDIN_DEF(clone,"siisi*"),
  12510. BUILDIN_DEF(doevent,"s"),
  12511. BUILDIN_DEF(donpcevent,"s"),
  12512. BUILDIN_DEF(cmdothernpc,"ss"),
  12513. BUILDIN_DEF(addtimer,"is"),
  12514. BUILDIN_DEF(deltimer,"s"),
  12515. BUILDIN_DEF(addtimercount,"si"),
  12516. BUILDIN_DEF(initnpctimer,"??"),
  12517. BUILDIN_DEF(stopnpctimer,"??"),
  12518. BUILDIN_DEF(startnpctimer,"??"),
  12519. BUILDIN_DEF(setnpctimer,"i?"),
  12520. BUILDIN_DEF(getnpctimer,"i?"),
  12521. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  12522. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  12523. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  12524. BUILDIN_DEF(announce,"si*"),
  12525. BUILDIN_DEF(mapannounce,"ssi*"),
  12526. BUILDIN_DEF(areaannounce,"siiiisi*"),
  12527. BUILDIN_DEF(getusers,"i"),
  12528. BUILDIN_DEF(getmapguildusers,"si"),
  12529. BUILDIN_DEF(getmapusers,"s"),
  12530. BUILDIN_DEF(getareausers,"siiii"),
  12531. BUILDIN_DEF(getareadropitem,"siiiii"),
  12532. BUILDIN_DEF(enablenpc,"s"),
  12533. BUILDIN_DEF(disablenpc,"s"),
  12534. BUILDIN_DEF(hideoffnpc,"s"),
  12535. BUILDIN_DEF(hideonnpc,"s"),
  12536. BUILDIN_DEF(sc_start,"iii?"),
  12537. BUILDIN_DEF(sc_start2,"iiii?"),
  12538. BUILDIN_DEF(sc_start4,"iiiiii?"),
  12539. BUILDIN_DEF(sc_end,"i?"),
  12540. BUILDIN_DEF(getscrate,"ii*"),
  12541. BUILDIN_DEF(debugmes,"s"),
  12542. BUILDIN_DEF2(catchpet,"pet","i"),
  12543. BUILDIN_DEF2(birthpet,"bpet",""),
  12544. BUILDIN_DEF(resetlvl,"i"),
  12545. BUILDIN_DEF(resetstatus,""),
  12546. BUILDIN_DEF(resetskill,""),
  12547. BUILDIN_DEF(skillpointcount,""),
  12548. BUILDIN_DEF(changebase,"i"),
  12549. BUILDIN_DEF(changesex,""),
  12550. BUILDIN_DEF(waitingroom,"si??"),
  12551. BUILDIN_DEF(delwaitingroom,"?"),
  12552. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  12553. BUILDIN_DEF(enablewaitingroomevent,"?"),
  12554. BUILDIN_DEF(disablewaitingroomevent,"?"),
  12555. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  12556. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  12557. BUILDIN_DEF(getwaitingroomstate,"i?"),
  12558. BUILDIN_DEF(warpwaitingpc,"sii?"),
  12559. BUILDIN_DEF(attachrid,"i"),
  12560. BUILDIN_DEF(detachrid,""),
  12561. BUILDIN_DEF(isloggedin,"i?"),
  12562. BUILDIN_DEF(setmapflagnosave,"ssii"),
  12563. BUILDIN_DEF(getmapflag,"si"),
  12564. BUILDIN_DEF(setmapflag,"si*"),
  12565. BUILDIN_DEF(removemapflag,"si"),
  12566. BUILDIN_DEF(pvpon,"s"),
  12567. BUILDIN_DEF(pvpoff,"s"),
  12568. BUILDIN_DEF(gvgon,"s"),
  12569. BUILDIN_DEF(gvgoff,"s"),
  12570. BUILDIN_DEF(emotion,"i*"),
  12571. BUILDIN_DEF(maprespawnguildid,"sii"),
  12572. BUILDIN_DEF(agitstart,""), // <Agit>
  12573. BUILDIN_DEF(agitend,""),
  12574. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  12575. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  12576. BUILDIN_DEF(getcastlename,"s"),
  12577. BUILDIN_DEF(getcastledata,"si*"),
  12578. BUILDIN_DEF(setcastledata,"sii"),
  12579. BUILDIN_DEF(requestguildinfo,"i*"),
  12580. BUILDIN_DEF(getequipcardcnt,"i"),
  12581. BUILDIN_DEF(successremovecards,"i"),
  12582. BUILDIN_DEF(failedremovecards,"ii"),
  12583. BUILDIN_DEF(marriage,"s"),
  12584. BUILDIN_DEF2(wedding_effect,"wedding",""),
  12585. BUILDIN_DEF(divorce,""),
  12586. BUILDIN_DEF(ispartneron,""),
  12587. BUILDIN_DEF(getpartnerid,""),
  12588. BUILDIN_DEF(getchildid,""),
  12589. BUILDIN_DEF(getmotherid,""),
  12590. BUILDIN_DEF(getfatherid,""),
  12591. BUILDIN_DEF(warppartner,"sii"),
  12592. BUILDIN_DEF(getitemname,"i"),
  12593. BUILDIN_DEF(getitemslots,"i"),
  12594. BUILDIN_DEF(makepet,"i"),
  12595. BUILDIN_DEF(getexp,"ii"),
  12596. BUILDIN_DEF(getinventorylist,""),
  12597. BUILDIN_DEF(getskilllist,""),
  12598. BUILDIN_DEF(clearitem,""),
  12599. BUILDIN_DEF(classchange,"ii"),
  12600. BUILDIN_DEF(misceffect,"i"),
  12601. BUILDIN_DEF(soundeffect,"si"),
  12602. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  12603. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  12604. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  12605. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  12606. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  12607. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  12608. BUILDIN_DEF(petloot,"i"), // [Valaris]
  12609. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  12610. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  12611. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  12612. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  12613. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  12614. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  12615. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  12616. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  12617. BUILDIN_DEF(nude,""), // nude command [Valaris]
  12618. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  12619. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  12620. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  12621. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  12622. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  12623. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  12624. BUILDIN_DEF(mobcount,"ss"),
  12625. BUILDIN_DEF(getlook,"i"),
  12626. BUILDIN_DEF(getsavepoint,"i"),
  12627. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  12628. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  12629. BUILDIN_DEF(npcstop,""), // [Valaris]
  12630. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  12631. BUILDIN_DEF(checkoption1,"i"),
  12632. BUILDIN_DEF(checkoption2,"i"),
  12633. BUILDIN_DEF(guildgetexp,"i"),
  12634. BUILDIN_DEF(guildchangegm,"is"),
  12635. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  12636. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  12637. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  12638. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  12639. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  12640. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  12641. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  12642. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  12643. BUILDIN_DEF(night,""), // sets the server to night time
  12644. BUILDIN_DEF(day,""), // sets the server to day time
  12645. #ifdef PCRE_SUPPORT
  12646. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  12647. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  12648. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  12649. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  12650. #endif
  12651. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  12652. BUILDIN_DEF(getusersname,"*"),
  12653. BUILDIN_DEF(recovery,""),
  12654. BUILDIN_DEF(getpetinfo,"i"),
  12655. BUILDIN_DEF(gethominfo,"i"),
  12656. BUILDIN_DEF(checkequipedcard,"i"),
  12657. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  12658. BUILDIN_DEF(globalmes,"s*"),
  12659. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  12660. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  12661. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  12662. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  12663. BUILDIN_DEF(setnpcdisplay,"sv??"),
  12664. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  12665. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  12666. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  12667. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  12668. // [zBuffer] List of mathematics commands --->
  12669. BUILDIN_DEF(sqrt,"i"),
  12670. BUILDIN_DEF(pow,"ii"),
  12671. BUILDIN_DEF(distance,"iiii"),
  12672. // <--- [zBuffer] List of mathematics commands
  12673. BUILDIN_DEF(md5,"s"),
  12674. // [zBuffer] List of dynamic var commands --->
  12675. BUILDIN_DEF(getd,"*"),
  12676. BUILDIN_DEF(setd,"*"),
  12677. // <--- [zBuffer] List of dynamic var commands
  12678. BUILDIN_DEF(petstat,"i"),
  12679. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  12680. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  12681. BUILDIN_DEF(npcshopadditem,"sii*"),
  12682. BUILDIN_DEF(npcshopdelitem,"si*"),
  12683. BUILDIN_DEF(npcshopattach,"s?"),
  12684. BUILDIN_DEF(equip,"i"),
  12685. BUILDIN_DEF(autoequip,"ii"),
  12686. BUILDIN_DEF(setbattleflag,"ss"),
  12687. BUILDIN_DEF(getbattleflag,"s"),
  12688. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  12689. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  12690. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  12691. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  12692. BUILDIN_DEF(axtoi,"s"),
  12693. BUILDIN_DEF(query_sql,"s*"),
  12694. BUILDIN_DEF(query_logsql,"s*"),
  12695. BUILDIN_DEF(escape_sql,"s"),
  12696. BUILDIN_DEF(atoi,"s"),
  12697. // [zBuffer] List of player cont commands --->
  12698. BUILDIN_DEF(rid2name,"i"),
  12699. BUILDIN_DEF(pcfollow,"ii"),
  12700. BUILDIN_DEF(pcstopfollow,"i"),
  12701. BUILDIN_DEF(pcblockmove,"ii"),
  12702. // <--- [zBuffer] List of player cont commands
  12703. // [zBuffer] List of mob control commands --->
  12704. BUILDIN_DEF(unitwalk,"ii?"),
  12705. BUILDIN_DEF(unitkill,"i"),
  12706. BUILDIN_DEF(unitwarp,"isii"),
  12707. BUILDIN_DEF(unitattack,"iv?"),
  12708. BUILDIN_DEF(unitstop,"i"),
  12709. BUILDIN_DEF(unittalk,"is"),
  12710. BUILDIN_DEF(unitemote,"ii"),
  12711. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  12712. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  12713. // <--- [zBuffer] List of mob control commands
  12714. BUILDIN_DEF(sleep,"i"),
  12715. BUILDIN_DEF(sleep2,"i"),
  12716. BUILDIN_DEF(awake,"s"),
  12717. BUILDIN_DEF(getvariableofnpc,"rs"),
  12718. BUILDIN_DEF(warpportal,"iisii"),
  12719. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  12720. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  12721. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  12722. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  12723. BUILDIN_DEF(checkvending,"*"),
  12724. BUILDIN_DEF(checkchatting,"*"),
  12725. BUILDIN_DEF(openmail,""),
  12726. BUILDIN_DEF(openauction,""),
  12727. BUILDIN_DEF(checkcell,"siii"),
  12728. BUILDIN_DEF(setcell,"siiiiii"),
  12729. BUILDIN_DEF(setwall,"siiiiis"),
  12730. BUILDIN_DEF(delwall,"s"),
  12731. BUILDIN_DEF(mercenary_create,"ii"),
  12732. BUILDIN_DEF(mercenary_heal,"ii"),
  12733. BUILDIN_DEF(mercenary_sc_start,"iii"),
  12734. BUILDIN_DEF(mercenary_get_calls,"i"),
  12735. BUILDIN_DEF(mercenary_get_faith,"i"),
  12736. BUILDIN_DEF(mercenary_set_calls,"ii"),
  12737. BUILDIN_DEF(mercenary_set_faith,"ii"),
  12738. BUILDIN_DEF(readbook,"ii"),
  12739. BUILDIN_DEF(setfont,"i"),
  12740. BUILDIN_DEF(areamobuseskill,"siiiiviiiii"),
  12741. BUILDIN_DEF(progressbar, "si"),
  12742. // WoE SE
  12743. BUILDIN_DEF(agitstart2,""),
  12744. BUILDIN_DEF(agitend2,""),
  12745. BUILDIN_DEF(agitcheck2,""),
  12746. // BattleGround
  12747. BUILDIN_DEF(waitingroom2bg,"siiss?"),
  12748. BUILDIN_DEF(waitingroom2bg_single,"isiis"),
  12749. BUILDIN_DEF(bg_team_setxy,"iii"),
  12750. BUILDIN_DEF(bg_warp,"isii"),
  12751. BUILDIN_DEF(bg_monster,"isiisi*"),
  12752. BUILDIN_DEF(bg_monster_set_team,"ii"),
  12753. BUILDIN_DEF(bg_leave,""),
  12754. BUILDIN_DEF(bg_destroy,"i"),
  12755. BUILDIN_DEF(areapercentheal,"siiiiii"),
  12756. BUILDIN_DEF(bg_get_data,"ii"),
  12757. BUILDIN_DEF(bg_getareausers,"isiiii"),
  12758. BUILDIN_DEF(bg_updatescore,"sii"),
  12759. // Instancing
  12760. BUILDIN_DEF(instance_create,"si"),
  12761. BUILDIN_DEF(instance_destroy,"?"),
  12762. BUILDIN_DEF(instance_attachmap,"si?"),
  12763. BUILDIN_DEF(instance_detachmap,"s?"),
  12764. BUILDIN_DEF(instance_attach,"i"),
  12765. BUILDIN_DEF(instance_id,"?"),
  12766. BUILDIN_DEF(instance_set_timeout,"ii?"),
  12767. BUILDIN_DEF(instance_init,"i"),
  12768. BUILDIN_DEF(instance_announce,"isi*"),
  12769. BUILDIN_DEF(instance_npcname,"s?"),
  12770. BUILDIN_DEF(has_instance,"s?"),
  12771. BUILDIN_DEF(instance_warpall,"sii?"),
  12772. //Quest Log System [Inkfish]
  12773. BUILDIN_DEF(setquest, "i"),
  12774. BUILDIN_DEF(erasequest, "i"),
  12775. BUILDIN_DEF(completequest, "i"),
  12776. BUILDIN_DEF(checkquest, "i*"),
  12777. BUILDIN_DEF(changequest, "ii"),
  12778. {NULL,NULL,NULL},
  12779. };