script.c 322 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_DISP
  4. //#define DEBUG_DISASM
  5. //#define DEBUG_RUN
  6. //#define DEBUG_HASH
  7. #include "../common/cbasetypes.h"
  8. #include "../common/malloc.h"
  9. #include "../common/lock.h"
  10. #include "../common/nullpo.h"
  11. #include "../common/showmsg.h"
  12. #include "../common/strlib.h"
  13. #include "../common/timer.h"
  14. #include "../common/utils.h"
  15. #include "map.h"
  16. #include "path.h"
  17. #include "clif.h"
  18. #include "chrif.h"
  19. #include "itemdb.h"
  20. #include "pc.h"
  21. #include "status.h"
  22. #include "storage.h"
  23. #include "mob.h"
  24. #include "npc.h"
  25. #include "pet.h"
  26. #include "mapreg.h"
  27. #include "homunculus.h"
  28. #include "mercenary.h"
  29. #include "intif.h"
  30. #include "skill.h"
  31. #include "status.h"
  32. #include "chat.h"
  33. #include "battle.h"
  34. #include "party.h"
  35. #include "guild.h"
  36. #include "atcommand.h"
  37. #include "charcommand.h"
  38. #include "log.h"
  39. #include "unit.h"
  40. #include "pet.h"
  41. #include "mail.h"
  42. #include "script.h"
  43. #include "quest.h"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <math.h>
  48. #ifndef WIN32
  49. #include <sys/time.h>
  50. #endif
  51. #include <time.h>
  52. #include <setjmp.h>
  53. #include <errno.h>
  54. ///////////////////////////////////////////////////////////////////////////////
  55. //## TODO possible enhancements: [FlavioJS]
  56. // - 'callfunc' supporting labels in the current npc "::LabelName"
  57. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  58. // - 'function FuncName;' function declarations reverting to global functions
  59. // if local label isn't found
  60. // - join callfunc and callsub's functionality
  61. // - use script_op2name in the DEBUG_DISASM block
  62. // - remove dynamic allocation in add_word()
  63. // - remove GETVALUE / SETVALUE
  64. // - clean up the set_reg / set_val / setd_sub mess
  65. //
  66. // struct script_state* st;
  67. //
  68. /// Returns the script_data at the target index
  69. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  70. /// Returns if the stack contains data at the target index
  71. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  72. /// Returns the index of the last data in the stack
  73. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  74. /// Pushes an int into the stack
  75. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  76. /// Pushes a string into the stack (script engine frees it automatically)
  77. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  78. /// Pushes a copy of a string into the stack
  79. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  80. /// Pushes a constant string into the stack (must never change or be freed)
  81. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  82. /// Pushes a nil into the stack
  83. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  84. /// Pushes a copy of the data in the target index
  85. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  86. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  87. #define script_isint(st,i) data_isint(script_getdata(st,i))
  88. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  89. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  90. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  91. // Note: "top" functions/defines use indexes relative to the top of the stack
  92. // -1 is the index of the data at the top
  93. /// Returns the script_data at the target index relative to the top of the stack
  94. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  95. /// Pushes a copy of the data in the target index relative to the top of the stack
  96. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  97. /// Removes the range of values [start,end[ relative to the top of the stack
  98. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  99. //
  100. // struct script_data* data;
  101. //
  102. /// Returns if the script data is a string
  103. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  104. /// Returns if the script data is an int
  105. #define data_isint(data) ( (data)->type == C_INT )
  106. /// Returns if the script data is a reference
  107. #define data_isreference(data) ( (data)->type == C_NAME )
  108. /// Returns if the script data is a label
  109. #define data_islabel(data) ( (data)->type == C_POS )
  110. /// Returns if the script data is an internal script function label
  111. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  112. /// Returns if this is a reference to a constant
  113. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  114. /// Returns if this a reference to a param
  115. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  116. /// Returns if this a reference to a variable
  117. //##TODO confirm it's C_NAME [FlavioJS]
  118. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  119. /// Returns the unique id of the reference (id and index)
  120. #define reference_getuid(data) ( (data)->u.num )
  121. /// Returns the id of the reference
  122. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  123. /// Returns the array index of the reference
  124. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  125. /// Returns the name of the reference
  126. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  127. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  128. #define reference_getref(data) ( (data)->ref )
  129. /// Returns the value of the constant
  130. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  131. /// Returns the type of param
  132. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  133. /// Composes the uid of a reference from the id and the index
  134. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  135. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.')
  136. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' )
  137. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  138. #define FETCH(n, t) \
  139. if( script_hasdata(st,n) ) \
  140. (t)=script_getnum(st,n);
  141. #define SCRIPT_BLOCK_SIZE 512
  142. enum { LABEL_NEXTLINE=1,LABEL_START };
  143. /// temporary buffer for passing around compiled bytecode
  144. /// @see add_scriptb, set_label, parse_script
  145. static unsigned char* script_buf = NULL;
  146. static int script_pos = 0, script_size = 0;
  147. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  148. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  149. // String buffer structures.
  150. // str_data stores string information
  151. static struct str_data_struct {
  152. enum c_op type;
  153. int str;
  154. int backpatch;
  155. int label;
  156. int (*func)(struct script_state *st);
  157. int val;
  158. int next;
  159. } *str_data = NULL;
  160. static int str_data_size = 0; // size of the data
  161. static int str_num = LABEL_START; // next id to be assigned
  162. // str_buf holds the strings themselves
  163. static char *str_buf;
  164. static int str_size = 0; // size of the buffer
  165. static int str_pos = 0; // next position to be assigned
  166. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  167. #define SCRIPT_HASH_SIZE 1021
  168. int str_hash[SCRIPT_HASH_SIZE];
  169. // Specifies which string hashing method to use
  170. //#define SCRIPT_HASH_DJB2
  171. //#define SCRIPT_HASH_SDBM
  172. #define SCRIPT_HASH_ELF
  173. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  174. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  175. static int parse_options=0;
  176. DBMap* script_get_label_db(){ return scriptlabel_db; }
  177. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  178. struct Script_Config script_config = {
  179. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  180. 0, INT_MAX, // input_min_value/input_max_value
  181. "OnPCDieEvent", //die_event_name
  182. "OnPCKillEvent", //kill_pc_event_name
  183. "OnNPCKillEvent", //kill_mob_event_name
  184. "OnPCLoginEvent", //login_event_name
  185. "OnPCLogoutEvent", //logout_event_name
  186. "OnPCLoadMapEvent", //loadmap_event_name
  187. "OnPCBaseLvUpEvent", //baselvup_event_name
  188. "OnPCJobLvUpEvent" //joblvup_event_name
  189. };
  190. static jmp_buf error_jump;
  191. static char* error_msg;
  192. static const char* error_pos;
  193. static int error_report; // if the error should produce output
  194. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  195. // [Eoe / jA 1080, 1081, 1094, 1164]
  196. enum curly_type {
  197. TYPE_NULL = 0,
  198. TYPE_IF,
  199. TYPE_SWITCH,
  200. TYPE_WHILE,
  201. TYPE_FOR,
  202. TYPE_DO,
  203. TYPE_USERFUNC,
  204. TYPE_ARGLIST // function argument list
  205. };
  206. #define ARGLIST_UNDEFINED 0
  207. #define ARGLIST_NO_PAREN 1
  208. #define ARGLIST_PAREN 2
  209. static struct {
  210. struct {
  211. enum curly_type type;
  212. int index;
  213. int count;
  214. int flag;
  215. struct linkdb_node *case_label;
  216. } curly[256]; // 右カッコの情報
  217. int curly_count; // 右カッコの数
  218. int index; // スクリプト内で使用した構文の数
  219. } syntax;
  220. const char* parse_curly_close(const char* p);
  221. const char* parse_syntax_close(const char* p);
  222. const char* parse_syntax_close_sub(const char* p,int* flag);
  223. const char* parse_syntax(const char* p);
  224. static int parse_syntax_for_flag = 0;
  225. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  226. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  227. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  228. int potion_target=0;
  229. c_op get_com(unsigned char *script,int *pos);
  230. int get_num(unsigned char *script,int *pos);
  231. typedef struct script_function {
  232. int (*func)(struct script_state *st);
  233. const char *name;
  234. const char *arg;
  235. } script_function;
  236. extern script_function buildin_func[];
  237. static struct linkdb_node *sleep_db;
  238. /*==========================================
  239. * ローカルプロトタイプ宣言 (必要な物のみ)
  240. *------------------------------------------*/
  241. const char* parse_subexpr(const char* p,int limit);
  242. void push_val(struct script_stack *stack,int type,int val);
  243. int run_func(struct script_state *st);
  244. enum {
  245. MF_NOMEMO, //0
  246. MF_NOTELEPORT,
  247. MF_NOSAVE,
  248. MF_NOBRANCH,
  249. MF_NOPENALTY,
  250. MF_NOZENYPENALTY,
  251. MF_PVP,
  252. MF_PVP_NOPARTY,
  253. MF_PVP_NOGUILD,
  254. MF_GVG,
  255. MF_GVG_NOPARTY, //10
  256. MF_NOTRADE,
  257. MF_NOSKILL,
  258. MF_NOWARP,
  259. MF_PARTYLOCK,
  260. MF_NOICEWALL,
  261. MF_SNOW,
  262. MF_FOG,
  263. MF_SAKURA,
  264. MF_LEAVES,
  265. MF_RAIN, //20
  266. MF_INDOORS,
  267. MF_NOGO,
  268. MF_CLOUDS,
  269. MF_CLOUDS2,
  270. MF_FIREWORKS,
  271. MF_GVG_CASTLE,
  272. MF_GVG_DUNGEON,
  273. MF_NIGHTENABLED,
  274. MF_NOBASEEXP,
  275. MF_NOJOBEXP, //30
  276. MF_NOMOBLOOT,
  277. MF_NOMVPLOOT,
  278. MF_NORETURN,
  279. MF_NOWARPTO,
  280. MF_NIGHTMAREDROP,
  281. MF_RESTRICTED,
  282. MF_NOCOMMAND,
  283. MF_NODROP,
  284. MF_JEXP,
  285. MF_BEXP, //40
  286. MF_NOVENDING,
  287. MF_LOADEVENT,
  288. MF_NOCHAT,
  289. MF_NOEXPPENALTY,
  290. MF_GUILDLOCK
  291. };
  292. const char* script_op2name(int op)
  293. {
  294. #define RETURN_OP_NAME(type) case type: return #type
  295. switch( op )
  296. {
  297. RETURN_OP_NAME(C_NOP);
  298. RETURN_OP_NAME(C_POS);
  299. RETURN_OP_NAME(C_INT);
  300. RETURN_OP_NAME(C_PARAM);
  301. RETURN_OP_NAME(C_FUNC);
  302. RETURN_OP_NAME(C_STR);
  303. RETURN_OP_NAME(C_CONSTSTR);
  304. RETURN_OP_NAME(C_ARG);
  305. RETURN_OP_NAME(C_NAME);
  306. RETURN_OP_NAME(C_EOL);
  307. RETURN_OP_NAME(C_RETINFO);
  308. RETURN_OP_NAME(C_USERFUNC);
  309. RETURN_OP_NAME(C_USERFUNC_POS);
  310. // operators
  311. RETURN_OP_NAME(C_OP3);
  312. RETURN_OP_NAME(C_LOR);
  313. RETURN_OP_NAME(C_LAND);
  314. RETURN_OP_NAME(C_LE);
  315. RETURN_OP_NAME(C_LT);
  316. RETURN_OP_NAME(C_GE);
  317. RETURN_OP_NAME(C_GT);
  318. RETURN_OP_NAME(C_EQ);
  319. RETURN_OP_NAME(C_NE);
  320. RETURN_OP_NAME(C_XOR);
  321. RETURN_OP_NAME(C_OR);
  322. RETURN_OP_NAME(C_AND);
  323. RETURN_OP_NAME(C_ADD);
  324. RETURN_OP_NAME(C_SUB);
  325. RETURN_OP_NAME(C_MUL);
  326. RETURN_OP_NAME(C_DIV);
  327. RETURN_OP_NAME(C_MOD);
  328. RETURN_OP_NAME(C_NEG);
  329. RETURN_OP_NAME(C_LNOT);
  330. RETURN_OP_NAME(C_NOT);
  331. RETURN_OP_NAME(C_R_SHIFT);
  332. RETURN_OP_NAME(C_L_SHIFT);
  333. default:
  334. ShowDebug("script_op2name: unexpected op=%d\n", op);
  335. return "???";
  336. }
  337. #undef RETURN_OP_NAME
  338. }
  339. /// Reports on the console the src of a script error.
  340. static void script_reportsrc(struct script_state *st)
  341. {
  342. struct block_list* bl;
  343. if( st->oid == 0 )
  344. return; //Can't report source.
  345. bl = map_id2bl(st->oid);
  346. if( bl == NULL )
  347. return;
  348. switch( bl->type )
  349. {
  350. case BL_NPC:
  351. if( bl->m >= 0 )
  352. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  353. else
  354. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  355. break;
  356. default:
  357. if( bl->m >= 0 )
  358. 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);
  359. else
  360. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  361. break;
  362. }
  363. }
  364. /// Reports on the console information about the script data.
  365. static void script_reportdata(struct script_data* data)
  366. {
  367. if( data == NULL )
  368. return;
  369. switch( data->type )
  370. {
  371. case C_NOP:// no value
  372. ShowDebug("Data: nothing (nil)\n");
  373. break;
  374. case C_INT:// number
  375. ShowDebug("Data: number value=%d\n", data->u.num);
  376. break;
  377. case C_STR:
  378. case C_CONSTSTR:// string
  379. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  380. break;
  381. case C_NAME:// reference
  382. if( reference_tovariable(data) )
  383. {// variable
  384. const char* name = reference_getname(data);
  385. if( not_array_variable(*name) )
  386. ShowDebug("Data: variable name='%s'\n", name);
  387. else
  388. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  389. }
  390. else if( reference_toconstant(data) )
  391. {// constant
  392. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  393. }
  394. else if( reference_toparam(data) )
  395. {// param
  396. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  397. }
  398. else
  399. {// ???
  400. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  401. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  402. }
  403. break;
  404. case C_POS:// label
  405. ShowDebug("Data: label pos=%d\n", data->u.num);
  406. break;
  407. default:
  408. ShowDebug("Data: %s\n", script_op2name(data->type));
  409. break;
  410. }
  411. }
  412. /*==========================================
  413. * エラーメッセージ出力
  414. *------------------------------------------*/
  415. static void disp_error_message2(const char *mes,const char *pos,int report)
  416. {
  417. error_msg = aStrdup(mes);
  418. error_pos = pos;
  419. error_report = report;
  420. longjmp( error_jump, 1 );
  421. }
  422. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  423. /// Checks event parameter validity
  424. static void check_event(struct script_state *st, const char *evt)
  425. {
  426. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  427. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  428. script_reportsrc(st);
  429. }
  430. }
  431. /*==========================================
  432. * Hashes the input string
  433. *------------------------------------------*/
  434. static unsigned int calc_hash(const char* p)
  435. {
  436. unsigned int h;
  437. #if defined(SCRIPT_HASH_DJB2)
  438. h = 5381;
  439. while( *p ) // hash*33 + c
  440. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  441. #elif defined(SCRIPT_HASH_SDBM)
  442. h = 0;
  443. while( *p ) // hash*65599 + c
  444. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  445. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  446. h = 0;
  447. while( *p ){
  448. unsigned int g;
  449. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  450. g = h & 0xF0000000;
  451. if( g )
  452. {
  453. h ^= g >> 24;
  454. h &= ~g;
  455. }
  456. }
  457. #else // athena hash
  458. h = 0;
  459. while( *p )
  460. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  461. #endif
  462. return h % SCRIPT_HASH_SIZE;
  463. }
  464. /*==========================================
  465. * str_data manipulation functions
  466. *------------------------------------------*/
  467. /// Looks up string using the provided id.
  468. const char* get_str(int id)
  469. {
  470. Assert( id >= LABEL_START && id < str_size );
  471. return str_buf+str_data[id].str;
  472. }
  473. /// Returns the uid of the string, or -1.
  474. static int search_str(const char* p)
  475. {
  476. int i;
  477. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  478. if( strcasecmp(get_str(i),p) == 0 )
  479. return i;
  480. return -1;
  481. }
  482. /// Stores a copy of the string and returns its id.
  483. /// If an identical string is already present, returns its id instead.
  484. int add_str(const char* p)
  485. {
  486. int i, h;
  487. int len;
  488. h = calc_hash(p);
  489. if( str_hash[h] == 0 )
  490. {// empty bucket, add new node here
  491. str_hash[h] = str_num;
  492. }
  493. else
  494. {// scan for end of list, or occurence of identical string
  495. for( i = str_hash[h]; ; i = str_data[i].next )
  496. {
  497. if( strcasecmp(get_str(i),p) == 0 )
  498. return i; // string already in list
  499. if( str_data[i].next == 0 )
  500. break; // reached the end
  501. }
  502. // append node to end of list
  503. str_data[i].next = str_num;
  504. }
  505. // grow list if neccessary
  506. if( str_num >= str_data_size )
  507. {
  508. str_data_size += 128;
  509. RECREATE(str_data,struct str_data_struct,str_data_size);
  510. memset(str_data + (str_data_size - 128), '\0', 128);
  511. }
  512. len=(int)strlen(p);
  513. // grow string buffer if neccessary
  514. while( str_pos+len+1 >= str_size )
  515. {
  516. str_size += 256;
  517. RECREATE(str_buf,char,str_size);
  518. memset(str_buf + (str_size - 256), '\0', 256);
  519. }
  520. safestrncpy(str_buf+str_pos, p, len+1);
  521. str_data[str_num].type = C_NOP;
  522. str_data[str_num].str = str_pos;
  523. str_data[str_num].next = 0;
  524. str_data[str_num].func = NULL;
  525. str_data[str_num].backpatch = -1;
  526. str_data[str_num].label = -1;
  527. str_pos += len+1;
  528. return str_num++;
  529. }
  530. /// Appends 1 byte to the script buffer.
  531. static void add_scriptb(int a)
  532. {
  533. if( script_pos+1 >= script_size )
  534. {
  535. script_size += SCRIPT_BLOCK_SIZE;
  536. RECREATE(script_buf,unsigned char,script_size);
  537. }
  538. script_buf[script_pos++] = (uint8)(a);
  539. }
  540. /// Appends a c_op value to the script buffer.
  541. /// The value is variable-length encoded into 8-bit blocks.
  542. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  543. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  544. static void add_scriptc(int a)
  545. {
  546. while( a >= 0x40 )
  547. {
  548. add_scriptb((a&0x3f)|0x40);
  549. a = (a - 0x40) >> 6;
  550. }
  551. add_scriptb(a);
  552. }
  553. /// Appends an integer value to the script buffer.
  554. /// The value is variable-length encoded into 8-bit blocks.
  555. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  556. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  557. static void add_scripti(int a)
  558. {
  559. while( a >= 0x40 )
  560. {
  561. add_scriptb((a&0x3f)|0xc0);
  562. a = (a - 0x40) >> 6;
  563. }
  564. add_scriptb(a|0x80);
  565. }
  566. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  567. ///
  568. /// @param l The id of the str_data entry
  569. // 最大16Mまで
  570. static void add_scriptl(int l)
  571. {
  572. int backpatch = str_data[l].backpatch;
  573. switch(str_data[l].type){
  574. case C_POS:
  575. case C_USERFUNC_POS:
  576. add_scriptc(C_POS);
  577. add_scriptb(str_data[l].label);
  578. add_scriptb(str_data[l].label>>8);
  579. add_scriptb(str_data[l].label>>16);
  580. break;
  581. case C_NOP:
  582. case C_USERFUNC:
  583. // ラベルの可能性があるのでbackpatch用データ埋め込み
  584. add_scriptc(C_NAME);
  585. str_data[l].backpatch = script_pos;
  586. add_scriptb(backpatch);
  587. add_scriptb(backpatch>>8);
  588. add_scriptb(backpatch>>16);
  589. break;
  590. case C_INT:
  591. add_scripti(abs(str_data[l].val));
  592. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  593. add_scriptc(C_NEG);
  594. break;
  595. default: // assume C_NAME
  596. add_scriptc(C_NAME);
  597. add_scriptb(l);
  598. add_scriptb(l>>8);
  599. add_scriptb(l>>16);
  600. break;
  601. }
  602. }
  603. /*==========================================
  604. * ラベルを解決する
  605. *------------------------------------------*/
  606. void set_label(int l,int pos, const char* script_pos)
  607. {
  608. int i,next;
  609. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  610. { //Prevent overwriting constants values and parameters [Skotlex]
  611. disp_error_message("set_label: invalid label name",script_pos);
  612. return;
  613. }
  614. if(str_data[l].label!=-1){
  615. disp_error_message("set_label: dup label ",script_pos);
  616. return;
  617. }
  618. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  619. str_data[l].label=pos;
  620. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  621. next=GETVALUE(script_buf,i);
  622. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  623. SETVALUE(script_buf,i,pos);
  624. i=next;
  625. }
  626. }
  627. /// Skips spaces and/or comments.
  628. const char* skip_space(const char* p)
  629. {
  630. if( p == NULL )
  631. return NULL;
  632. for(;;)
  633. {
  634. while( ISSPACE(*p) )
  635. ++p;
  636. if( *p == '/' && p[1] == '/' )
  637. {// line comment
  638. while(*p && *p!='\n')
  639. ++p;
  640. }
  641. else if( *p == '/' && p[1] == '*' )
  642. {// block comment
  643. p += 2;
  644. for(;;)
  645. {
  646. if( *p == '\0' )
  647. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  648. if( *p == '*' && p[1] == '/' )
  649. {// end of block comment
  650. p += 2;
  651. break;
  652. }
  653. ++p;
  654. }
  655. }
  656. else
  657. break;
  658. }
  659. return p;
  660. }
  661. /// Skips a word.
  662. /// A word consists of undercores and/or alfanumeric characters,
  663. /// and valid variable prefixes/postfixes.
  664. static
  665. const char* skip_word(const char* p)
  666. {
  667. // prefix
  668. switch( *p )
  669. {
  670. case '@':// temporary char variable
  671. ++p; break;
  672. case '#':// account variable
  673. p += ( p[1] == '#' ? 2 : 1 ); break;
  674. case '.':// npc variable
  675. p += ( p[1] == '@' ? 2 : 1 ); break;
  676. case '$':// global variable
  677. p += ( p[1] == '@' ? 2 : 1 ); break;
  678. }
  679. while( ISALNUM(*p) || *p == '_' )
  680. ++p;
  681. // postfix
  682. if( *p == '$' )// string
  683. p++;
  684. return p;
  685. }
  686. /// Adds a word to str_data.
  687. /// @see skip_word
  688. /// @see add_str
  689. static
  690. int add_word(const char* p)
  691. {
  692. char* word;
  693. int len;
  694. int i;
  695. // Check for a word
  696. len = skip_word(p) - p;
  697. if( len == 0 )
  698. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  699. // Duplicate the word
  700. CREATE(word, char, len+1);
  701. memcpy(word, p, len);
  702. word[len] = 0;
  703. // add the word
  704. i = add_str(word);
  705. aFree(word);
  706. return i;
  707. }
  708. /// Parses a function call.
  709. /// The argument list can have parenthesis or not.
  710. /// The number of arguments is checked.
  711. static
  712. const char* parse_callfunc(const char* p, int require_paren)
  713. {
  714. const char* p2;
  715. const char* arg=NULL;
  716. int func;
  717. func = add_word(p);
  718. if( str_data[func].type == C_FUNC ){
  719. // buildin function
  720. add_scriptl(func);
  721. add_scriptc(C_ARG);
  722. arg = buildin_func[str_data[func].val].arg;
  723. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  724. // script defined function
  725. int callsub = search_str("callsub");
  726. add_scriptl(callsub);
  727. add_scriptc(C_ARG);
  728. add_scriptl(func);
  729. arg = buildin_func[str_data[callsub].val].arg;
  730. if( *arg == 0 )
  731. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  732. if( *arg != '*' )
  733. ++arg; // count func as argument
  734. } else
  735. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  736. p = skip_word(p);
  737. p = skip_space(p);
  738. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  739. syntax.curly[syntax.curly_count].count = 0;
  740. if( *p == ';' )
  741. {// <func name> ';'
  742. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  743. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  744. {// <func name> '(' ')'
  745. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  746. p = p2;
  747. /*
  748. } else if( 0 && require_paren && *p != '(' )
  749. {// <func name>
  750. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  751. */
  752. } else
  753. {// <func name> <arg list>
  754. if( require_paren ){
  755. if( *p != '(' )
  756. disp_error_message("need '('",p);
  757. ++p; // skip '('
  758. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  759. } else if( *p == '(' ){
  760. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  761. } else {
  762. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  763. }
  764. ++syntax.curly_count;
  765. while( *arg ) {
  766. p2=parse_subexpr(p,-1);
  767. if( p == p2 )
  768. break; // not an argument
  769. if( *arg != '*' )
  770. ++arg; // next argument
  771. p=skip_space(p2);
  772. if( *arg == 0 || *p != ',' )
  773. break; // no more arguments
  774. ++p; // skip comma
  775. }
  776. --syntax.curly_count;
  777. }
  778. if( *arg && *arg != '?' && *arg != '*' )
  779. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  780. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  781. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  782. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  783. if( *p != ')' )
  784. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  785. ++p;
  786. }
  787. add_scriptc(C_FUNC);
  788. return p;
  789. }
  790. /*==========================================
  791. * 項の解析
  792. *------------------------------------------*/
  793. const char* parse_simpleexpr(const char *p)
  794. {
  795. int i;
  796. p=skip_space(p);
  797. if(*p==';' || *p==',')
  798. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  799. if(*p=='('){
  800. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  801. ++syntax.curly[i].count;
  802. p=parse_subexpr(p+1,-1);
  803. p=skip_space(p);
  804. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  805. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  806. ){
  807. if( *p == ',' ){
  808. syntax.curly[i].flag = ARGLIST_PAREN;
  809. return p;
  810. } else
  811. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  812. }
  813. if( *p != ')' )
  814. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  815. ++p;
  816. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  817. char *np;
  818. i=strtoul(p,&np,0);
  819. add_scripti(i);
  820. p=np;
  821. } else if(*p=='"'){
  822. add_scriptc(C_STR);
  823. p++;
  824. while( *p && *p != '"' ){
  825. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  826. {
  827. char buf[8];
  828. size_t len = skip_escaped_c(p) - p;
  829. size_t n = sv_unescape_c(buf, p, len);
  830. if( n != 1 )
  831. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  832. p += len;
  833. add_scriptb(*buf);
  834. continue;
  835. }
  836. else if( *p == '\n' )
  837. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  838. add_scriptb(*p++);
  839. }
  840. if(!*p)
  841. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  842. add_scriptb(0);
  843. p++; //'"'
  844. } else {
  845. int l;
  846. // label , register , function etc
  847. if(skip_word(p)==p)
  848. disp_error_message("parse_simpleexpr: unexpected character",p);
  849. l=add_word(p);
  850. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  851. return parse_callfunc(p,1);
  852. p=skip_word(p);
  853. if( *p == '[' ){
  854. // array(name[i] => getelementofarray(name,i) )
  855. add_scriptl(search_str("getelementofarray"));
  856. add_scriptc(C_ARG);
  857. add_scriptl(l);
  858. p=parse_subexpr(p+1,-1);
  859. p=skip_space(p);
  860. if( *p != ']' )
  861. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  862. ++p;
  863. add_scriptc(C_FUNC);
  864. }else
  865. add_scriptl(l);
  866. }
  867. return p;
  868. }
  869. /*==========================================
  870. * 式の解析
  871. *------------------------------------------*/
  872. const char* parse_subexpr(const char* p,int limit)
  873. {
  874. int op,opl,len;
  875. const char* tmpp;
  876. p=skip_space(p);
  877. if(*p=='-'){
  878. tmpp=skip_space(p+1);
  879. if(*tmpp==';' || *tmpp==','){
  880. add_scriptl(LABEL_NEXTLINE);
  881. p++;
  882. return p;
  883. }
  884. }
  885. tmpp=p;
  886. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  887. p=parse_subexpr(p+1,10);
  888. add_scriptc(op);
  889. } else
  890. p=parse_simpleexpr(p);
  891. p=skip_space(p);
  892. while((
  893. (op=C_OP3,opl=0,len=1,*p=='?') ||
  894. (op=C_ADD,opl=8,len=1,*p=='+') ||
  895. (op=C_SUB,opl=8,len=1,*p=='-') ||
  896. (op=C_MUL,opl=9,len=1,*p=='*') ||
  897. (op=C_DIV,opl=9,len=1,*p=='/') ||
  898. (op=C_MOD,opl=9,len=1,*p=='%') ||
  899. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  900. (op=C_AND,opl=6,len=1,*p=='&') ||
  901. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  902. (op=C_OR,opl=5,len=1,*p=='|') ||
  903. (op=C_XOR,opl=4,len=1,*p=='^') ||
  904. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  905. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  906. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  907. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  908. (op=C_GT,opl=3,len=1,*p=='>') ||
  909. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  910. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  911. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  912. p+=len;
  913. if(op == C_OP3) {
  914. p=parse_subexpr(p,-1);
  915. p=skip_space(p);
  916. if( *(p++) != ':')
  917. disp_error_message("parse_subexpr: need ':'", p-1);
  918. p=parse_subexpr(p,-1);
  919. } else {
  920. p=parse_subexpr(p,opl);
  921. }
  922. add_scriptc(op);
  923. p=skip_space(p);
  924. }
  925. return p; /* return first untreated operator */
  926. }
  927. /*==========================================
  928. * 式の評価
  929. *------------------------------------------*/
  930. const char* parse_expr(const char *p)
  931. {
  932. switch(*p){
  933. case ')': case ';': case ':': case '[': case ']':
  934. case '}':
  935. disp_error_message("parse_expr: unexpected char",p);
  936. }
  937. p=parse_subexpr(p,-1);
  938. return p;
  939. }
  940. /*==========================================
  941. * 行の解析
  942. *------------------------------------------*/
  943. const char* parse_line(const char* p)
  944. {
  945. const char* p2;
  946. p=skip_space(p);
  947. if(*p==';') {
  948. // if(); for(); while(); のために閉じ判定
  949. p = parse_syntax_close(p + 1);
  950. return p;
  951. }
  952. if(*p==')' && parse_syntax_for_flag)
  953. return p+1;
  954. p = skip_space(p);
  955. if(p[0] == '{') {
  956. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  957. syntax.curly[syntax.curly_count].count = -1;
  958. syntax.curly[syntax.curly_count].index = -1;
  959. syntax.curly_count++;
  960. return p + 1;
  961. } else if(p[0] == '}') {
  962. return parse_curly_close(p);
  963. }
  964. // 構文関連の処理
  965. p2 = parse_syntax(p);
  966. if(p2 != NULL)
  967. return p2;
  968. p = parse_callfunc(p,0);
  969. p = skip_space(p);
  970. if(parse_syntax_for_flag) {
  971. if( *p != ')' )
  972. disp_error_message("parse_line: need ')'",p);
  973. } else {
  974. if( *p != ';' )
  975. disp_error_message("parse_line: need ';'",p);
  976. }
  977. // if, for , while の閉じ判定
  978. p = parse_syntax_close(p+1);
  979. return p;
  980. }
  981. // { ... } の閉じ処理
  982. const char* parse_curly_close(const char* p)
  983. {
  984. if(syntax.curly_count <= 0) {
  985. disp_error_message("parse_curly_close: unexpected string",p);
  986. return p + 1;
  987. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  988. syntax.curly_count--;
  989. // if, for , while の閉じ判定
  990. p = parse_syntax_close(p + 1);
  991. return p;
  992. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  993. // switch() 閉じ判定
  994. int pos = syntax.curly_count-1;
  995. char label[256];
  996. int l;
  997. // 一時変数を消す
  998. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  999. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1000. parse_line(label);
  1001. syntax.curly_count--;
  1002. // 無条件で終了ポインタに移動
  1003. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1004. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1005. parse_line(label);
  1006. syntax.curly_count--;
  1007. // 現在地のラベルを付ける
  1008. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1009. l=add_str(label);
  1010. set_label(l,script_pos, p);
  1011. if(syntax.curly[pos].flag) {
  1012. // default が存在する
  1013. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1014. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1015. parse_line(label);
  1016. syntax.curly_count--;
  1017. }
  1018. // 終了ラベルを付ける
  1019. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1020. l=add_str(label);
  1021. set_label(l,script_pos, p);
  1022. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1023. syntax.curly_count--;
  1024. return p+1;
  1025. } else {
  1026. disp_error_message("parse_curly_close: unexpected string",p);
  1027. return p + 1;
  1028. }
  1029. }
  1030. // 構文関連の処理
  1031. // break, case, continue, default, do, for, function,
  1032. // if, switch, while をこの内部で処理します。
  1033. const char* parse_syntax(const char* p)
  1034. {
  1035. const char *p2 = skip_word(p);
  1036. switch(*p) {
  1037. case 'B':
  1038. case 'b':
  1039. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1040. // break の処理
  1041. char label[256];
  1042. int pos = syntax.curly_count - 1;
  1043. while(pos >= 0) {
  1044. if(syntax.curly[pos].type == TYPE_DO) {
  1045. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1046. break;
  1047. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1048. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1049. break;
  1050. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1051. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1052. break;
  1053. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1054. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1055. break;
  1056. }
  1057. pos--;
  1058. }
  1059. if(pos < 0) {
  1060. disp_error_message("parse_syntax: unexpected 'break'",p);
  1061. } else {
  1062. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1063. parse_line(label);
  1064. syntax.curly_count--;
  1065. }
  1066. p = skip_space(p2);
  1067. if(*p != ';')
  1068. disp_error_message("parse_syntax: need ';'",p);
  1069. // if, for , while の閉じ判定
  1070. p = parse_syntax_close(p + 1);
  1071. return p;
  1072. }
  1073. break;
  1074. case 'c':
  1075. case 'C':
  1076. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1077. // case の処理
  1078. int pos = syntax.curly_count-1;
  1079. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1080. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1081. return p+1;
  1082. } else {
  1083. char label[256];
  1084. int l,v;
  1085. char *np;
  1086. if(syntax.curly[pos].count != 1) {
  1087. // FALLTHRU 用のジャンプ
  1088. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1089. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1090. parse_line(label);
  1091. syntax.curly_count--;
  1092. // 現在地のラベルを付ける
  1093. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1094. l=add_str(label);
  1095. set_label(l,script_pos, p);
  1096. }
  1097. // switch 判定文
  1098. p = skip_space(p2);
  1099. if(p == p2) {
  1100. disp_error_message("parse_syntax: expect space ' '",p);
  1101. }
  1102. // check whether case label is integer or not
  1103. v = strtol(p,&np,0);
  1104. if(np == p) { //Check for constants
  1105. p2 = skip_word(p);
  1106. v = p2-p; // length of word at p2
  1107. memcpy(label,p,v);
  1108. label[v]='\0';
  1109. v = search_str(label);
  1110. if (v < 0 || str_data[v].type != C_INT)
  1111. disp_error_message("parse_syntax: 'case' label not integer",p);
  1112. v = str_data[v].val;
  1113. p = skip_word(p);
  1114. } else { //Numeric value
  1115. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1116. p++;
  1117. p = skip_word(p);
  1118. if(np != p)
  1119. disp_error_message("parse_syntax: 'case' label not integer",np);
  1120. }
  1121. p = skip_space(p);
  1122. if(*p != ':')
  1123. disp_error_message("parse_syntax: expect ':'",p);
  1124. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1125. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1126. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1127. // 2回parse しないとダメ
  1128. p2 = parse_line(label);
  1129. parse_line(p2);
  1130. syntax.curly_count--;
  1131. if(syntax.curly[pos].count != 1) {
  1132. // FALLTHRU 終了後のラベル
  1133. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1134. l=add_str(label);
  1135. set_label(l,script_pos,p);
  1136. }
  1137. // check duplication of case label [Rayce]
  1138. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1139. disp_error_message("parse_syntax: dup 'case'",p);
  1140. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1141. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1142. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1143. parse_line(label);
  1144. syntax.curly_count--;
  1145. syntax.curly[pos].count++;
  1146. }
  1147. return p + 1;
  1148. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1149. // continue の処理
  1150. char label[256];
  1151. int pos = syntax.curly_count - 1;
  1152. while(pos >= 0) {
  1153. if(syntax.curly[pos].type == TYPE_DO) {
  1154. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1155. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1156. break;
  1157. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1158. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1159. break;
  1160. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1161. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1162. break;
  1163. }
  1164. pos--;
  1165. }
  1166. if(pos < 0) {
  1167. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1168. } else {
  1169. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1170. parse_line(label);
  1171. syntax.curly_count--;
  1172. }
  1173. p = skip_space(p2);
  1174. if(*p != ';')
  1175. disp_error_message("parse_syntax: need ';'",p);
  1176. // if, for , while の閉じ判定
  1177. p = parse_syntax_close(p + 1);
  1178. return p;
  1179. }
  1180. break;
  1181. case 'd':
  1182. case 'D':
  1183. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1184. // switch - default の処理
  1185. int pos = syntax.curly_count-1;
  1186. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1187. disp_error_message("parse_syntax: unexpected 'default'",p);
  1188. } else if(syntax.curly[pos].flag) {
  1189. disp_error_message("parse_syntax: dup 'default'",p);
  1190. } else {
  1191. char label[256];
  1192. int l;
  1193. // 現在地のラベルを付ける
  1194. p = skip_space(p2);
  1195. if(*p != ':') {
  1196. disp_error_message("parse_syntax: need ':'",p);
  1197. }
  1198. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1199. l=add_str(label);
  1200. set_label(l,script_pos,p);
  1201. // 無条件で次のリンクに飛ばす
  1202. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1203. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1204. parse_line(label);
  1205. syntax.curly_count--;
  1206. // default のラベルを付ける
  1207. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1208. l=add_str(label);
  1209. set_label(l,script_pos,p);
  1210. syntax.curly[syntax.curly_count - 1].flag = 1;
  1211. syntax.curly[pos].count++;
  1212. }
  1213. return p + 1;
  1214. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1215. int l;
  1216. char label[256];
  1217. p=skip_space(p2);
  1218. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1219. syntax.curly[syntax.curly_count].count = 1;
  1220. syntax.curly[syntax.curly_count].index = syntax.index++;
  1221. syntax.curly[syntax.curly_count].flag = 0;
  1222. // 現在地のラベル形成する
  1223. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1224. l=add_str(label);
  1225. set_label(l,script_pos,p);
  1226. syntax.curly_count++;
  1227. return p;
  1228. }
  1229. break;
  1230. case 'f':
  1231. case 'F':
  1232. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1233. int l;
  1234. char label[256];
  1235. int pos = syntax.curly_count;
  1236. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1237. syntax.curly[syntax.curly_count].count = 1;
  1238. syntax.curly[syntax.curly_count].index = syntax.index++;
  1239. syntax.curly[syntax.curly_count].flag = 0;
  1240. syntax.curly_count++;
  1241. p=skip_space(p2);
  1242. if(*p != '(')
  1243. disp_error_message("parse_syntax: need '('",p);
  1244. p++;
  1245. // 初期化文を実行する
  1246. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1247. p=parse_line(p);
  1248. syntax.curly_count--;
  1249. // 条件判断開始のラベル形成する
  1250. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1251. l=add_str(label);
  1252. set_label(l,script_pos,p);
  1253. p=skip_space(p);
  1254. if(*p == ';') {
  1255. // for(;;) のパターンなので必ず真
  1256. ;
  1257. } else {
  1258. // 条件が偽なら終了地点に飛ばす
  1259. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1260. add_scriptl(add_str("jump_zero"));
  1261. add_scriptc(C_ARG);
  1262. p=parse_expr(p);
  1263. p=skip_space(p);
  1264. add_scriptl(add_str(label));
  1265. add_scriptc(C_FUNC);
  1266. }
  1267. if(*p != ';')
  1268. disp_error_message("parse_syntax: need ';'",p);
  1269. p++;
  1270. // ループ開始に飛ばす
  1271. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1272. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1273. parse_line(label);
  1274. syntax.curly_count--;
  1275. // 次のループへのラベル形成する
  1276. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1277. l=add_str(label);
  1278. set_label(l,script_pos,p);
  1279. // 次のループに入る時の処理
  1280. // for 最後の ')' を ';' として扱うフラグ
  1281. parse_syntax_for_flag = 1;
  1282. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1283. p=parse_line(p);
  1284. syntax.curly_count--;
  1285. parse_syntax_for_flag = 0;
  1286. // 条件判定処理に飛ばす
  1287. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1288. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1289. parse_line(label);
  1290. syntax.curly_count--;
  1291. // ループ開始のラベル付け
  1292. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1293. l=add_str(label);
  1294. set_label(l,script_pos,p);
  1295. return p;
  1296. }
  1297. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1298. {// internal script function
  1299. const char *func_name;
  1300. func_name = skip_space(p2);
  1301. p = skip_word(func_name);
  1302. if( p == func_name )
  1303. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1304. p2 = skip_space(p);
  1305. if( *p2 == ';' )
  1306. {// function <name> ;
  1307. // function declaration - just register the name
  1308. int l;
  1309. l = add_word(func_name);
  1310. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1311. str_data[l].type = C_USERFUNC;
  1312. // if, for , while の閉じ判定
  1313. p = parse_syntax_close(p2 + 1);
  1314. return p; }
  1315. else if(*p2 == '{')
  1316. {// function <name> <line/block of code>
  1317. char label[256];
  1318. int l;
  1319. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1320. syntax.curly[syntax.curly_count].count = 1;
  1321. syntax.curly[syntax.curly_count].index = syntax.index++;
  1322. syntax.curly[syntax.curly_count].flag = 0;
  1323. ++syntax.curly_count;
  1324. // Jump over the function code
  1325. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1326. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1327. ++syntax.curly_count;
  1328. parse_line(label);
  1329. --syntax.curly_count;
  1330. // Set the position of the function (label)
  1331. l=add_word(func_name);
  1332. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1333. str_data[l].type = C_USERFUNC;
  1334. set_label(l, script_pos, p);
  1335. if( parse_options&SCRIPT_USE_LABEL_DB )
  1336. strdb_put(scriptlabel_db, get_str(l), (void*)script_pos);
  1337. return skip_space(p);
  1338. }
  1339. else
  1340. {
  1341. disp_error_message("expect ';' or '{' at function syntax",p);
  1342. }
  1343. }
  1344. break;
  1345. case 'i':
  1346. case 'I':
  1347. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1348. // if() の処理
  1349. char label[256];
  1350. p=skip_space(p2);
  1351. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1352. disp_error_message("need '('",p);
  1353. }
  1354. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1355. syntax.curly[syntax.curly_count].count = 1;
  1356. syntax.curly[syntax.curly_count].index = syntax.index++;
  1357. syntax.curly[syntax.curly_count].flag = 0;
  1358. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1359. syntax.curly_count++;
  1360. add_scriptl(add_str("jump_zero"));
  1361. add_scriptc(C_ARG);
  1362. p=parse_expr(p);
  1363. p=skip_space(p);
  1364. add_scriptl(add_str(label));
  1365. add_scriptc(C_FUNC);
  1366. return p;
  1367. }
  1368. break;
  1369. case 's':
  1370. case 'S':
  1371. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1372. // switch() の処理
  1373. char label[256];
  1374. p=skip_space(p2);
  1375. if(*p != '(') {
  1376. disp_error_message("need '('",p);
  1377. }
  1378. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1379. syntax.curly[syntax.curly_count].count = 1;
  1380. syntax.curly[syntax.curly_count].index = syntax.index++;
  1381. syntax.curly[syntax.curly_count].flag = 0;
  1382. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1383. syntax.curly_count++;
  1384. add_scriptl(add_str("set"));
  1385. add_scriptc(C_ARG);
  1386. add_scriptl(add_str(label));
  1387. p=parse_expr(p);
  1388. p=skip_space(p);
  1389. if(*p != '{') {
  1390. disp_error_message("parse_syntax: need '{'",p);
  1391. }
  1392. add_scriptc(C_FUNC);
  1393. return p + 1;
  1394. }
  1395. break;
  1396. case 'w':
  1397. case 'W':
  1398. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1399. int l;
  1400. char label[256];
  1401. p=skip_space(p2);
  1402. if(*p != '(') {
  1403. disp_error_message("need '('",p);
  1404. }
  1405. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1406. syntax.curly[syntax.curly_count].count = 1;
  1407. syntax.curly[syntax.curly_count].index = syntax.index++;
  1408. syntax.curly[syntax.curly_count].flag = 0;
  1409. // 条件判断開始のラベル形成する
  1410. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1411. l=add_str(label);
  1412. set_label(l,script_pos,p);
  1413. // 条件が偽なら終了地点に飛ばす
  1414. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1415. syntax.curly_count++;
  1416. add_scriptl(add_str("jump_zero"));
  1417. add_scriptc(C_ARG);
  1418. p=parse_expr(p);
  1419. p=skip_space(p);
  1420. add_scriptl(add_str(label));
  1421. add_scriptc(C_FUNC);
  1422. return p;
  1423. }
  1424. break;
  1425. }
  1426. return NULL;
  1427. }
  1428. const char* parse_syntax_close(const char *p) {
  1429. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1430. int flag;
  1431. do {
  1432. p = parse_syntax_close_sub(p,&flag);
  1433. } while(flag);
  1434. return p;
  1435. }
  1436. // if, for , while , do の閉じ判定
  1437. // flag == 1 : 閉じられた
  1438. // flag == 0 : 閉じられない
  1439. const char* parse_syntax_close_sub(const char* p,int* flag)
  1440. {
  1441. char label[256];
  1442. int pos = syntax.curly_count - 1;
  1443. int l;
  1444. *flag = 1;
  1445. if(syntax.curly_count <= 0) {
  1446. *flag = 0;
  1447. return p;
  1448. } else if(syntax.curly[pos].type == TYPE_IF) {
  1449. const char *bp = p;
  1450. const char *p2;
  1451. // if 最終場所へ飛ばす
  1452. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1453. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1454. parse_line(label);
  1455. syntax.curly_count--;
  1456. // 現在地のラベルを付ける
  1457. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1458. l=add_str(label);
  1459. set_label(l,script_pos,p);
  1460. syntax.curly[pos].count++;
  1461. p = skip_space(p);
  1462. p2 = skip_word(p);
  1463. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1464. // else or else - if
  1465. p = skip_space(p2);
  1466. p2 = skip_word(p);
  1467. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1468. // else - if
  1469. p=skip_space(p2);
  1470. if(*p != '(') {
  1471. disp_error_message("need '('",p);
  1472. }
  1473. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1474. add_scriptl(add_str("jump_zero"));
  1475. add_scriptc(C_ARG);
  1476. p=parse_expr(p);
  1477. p=skip_space(p);
  1478. add_scriptl(add_str(label));
  1479. add_scriptc(C_FUNC);
  1480. *flag = 0;
  1481. return p;
  1482. } else {
  1483. // else
  1484. if(!syntax.curly[pos].flag) {
  1485. syntax.curly[pos].flag = 1;
  1486. *flag = 0;
  1487. return p;
  1488. }
  1489. }
  1490. }
  1491. // if 閉じ
  1492. syntax.curly_count--;
  1493. // 最終地のラベルを付ける
  1494. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1495. l=add_str(label);
  1496. set_label(l,script_pos,p);
  1497. if(syntax.curly[pos].flag == 1) {
  1498. // このifに対するelseじゃないのでポインタの位置は同じ
  1499. return bp;
  1500. }
  1501. return p;
  1502. } else if(syntax.curly[pos].type == TYPE_DO) {
  1503. int l;
  1504. char label[256];
  1505. const char *p2;
  1506. if(syntax.curly[pos].flag) {
  1507. // 現在地のラベル形成する(continue でここに来る)
  1508. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1509. l=add_str(label);
  1510. set_label(l,script_pos,p);
  1511. }
  1512. // 条件が偽なら終了地点に飛ばす
  1513. p = skip_space(p);
  1514. p2 = skip_word(p);
  1515. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1516. disp_error_message("parse_syntax: need 'while'",p);
  1517. p = skip_space(p2);
  1518. if(*p != '(') {
  1519. disp_error_message("need '('",p);
  1520. }
  1521. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1522. add_scriptl(add_str("jump_zero"));
  1523. add_scriptc(C_ARG);
  1524. p=parse_expr(p);
  1525. p=skip_space(p);
  1526. add_scriptl(add_str(label));
  1527. add_scriptc(C_FUNC);
  1528. // 開始地点に飛ばす
  1529. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1530. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1531. parse_line(label);
  1532. syntax.curly_count--;
  1533. // 条件終了地点のラベル形成する
  1534. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1535. l=add_str(label);
  1536. set_label(l,script_pos,p);
  1537. p = skip_space(p);
  1538. if(*p != ';') {
  1539. disp_error_message("parse_syntax: need ';'",p);
  1540. return p+1;
  1541. }
  1542. p++;
  1543. syntax.curly_count--;
  1544. return p;
  1545. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1546. // 次のループに飛ばす
  1547. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1548. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1549. parse_line(label);
  1550. syntax.curly_count--;
  1551. // for 終了のラベル付け
  1552. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1553. l=add_str(label);
  1554. set_label(l,script_pos,p);
  1555. syntax.curly_count--;
  1556. return p;
  1557. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1558. // while 条件判断へ飛ばす
  1559. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1560. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1561. parse_line(label);
  1562. syntax.curly_count--;
  1563. // while 終了のラベル付け
  1564. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1565. l=add_str(label);
  1566. set_label(l,script_pos,p);
  1567. syntax.curly_count--;
  1568. return p;
  1569. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1570. int pos = syntax.curly_count-1;
  1571. char label[256];
  1572. int l;
  1573. // 戻す
  1574. sprintf(label,"return;");
  1575. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1576. parse_line(label);
  1577. syntax.curly_count--;
  1578. // 現在地のラベルを付ける
  1579. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1580. l=add_str(label);
  1581. set_label(l,script_pos,p);
  1582. syntax.curly_count--;
  1583. return p;
  1584. } else {
  1585. *flag = 0;
  1586. return p;
  1587. }
  1588. }
  1589. /*==========================================
  1590. * 組み込み関数の追加
  1591. *------------------------------------------*/
  1592. static void add_buildin_func(void)
  1593. {
  1594. int i,n;
  1595. const char* p;
  1596. for( i = 0; buildin_func[i].func; i++ )
  1597. {
  1598. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1599. // 'v' - value (either string or int or reference)
  1600. // 's' - string
  1601. // 'i' - int
  1602. // 'r' - reference (of a variable)
  1603. // 'l' - label
  1604. // '?' - one optional parameter
  1605. // '*' - unknown number of optional parameters
  1606. p = buildin_func[i].arg;
  1607. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1608. while( *p == '?' ) ++p;
  1609. if( *p == '*' ) ++p;
  1610. if( *p != 0){
  1611. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1612. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1613. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1614. } else {
  1615. n = add_str(buildin_func[i].name);
  1616. str_data[n].type = C_FUNC;
  1617. str_data[n].val = i;
  1618. str_data[n].func = buildin_func[i].func;
  1619. }
  1620. }
  1621. }
  1622. /*==========================================
  1623. * 定数データベースの読み込み
  1624. *------------------------------------------*/
  1625. static void read_constdb(void)
  1626. {
  1627. FILE *fp;
  1628. char line[1024],name[1024],val[1024];
  1629. int n,type;
  1630. sprintf(line, "%s/const.txt", db_path);
  1631. fp=fopen(line, "r");
  1632. if(fp==NULL){
  1633. ShowError("can't read %s\n", line);
  1634. return ;
  1635. }
  1636. while(fgets(line, sizeof(line), fp))
  1637. {
  1638. if(line[0]=='/' && line[1]=='/')
  1639. continue;
  1640. type=0;
  1641. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1642. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1643. n=add_str(name);
  1644. if(type==0)
  1645. str_data[n].type=C_INT;
  1646. else
  1647. str_data[n].type=C_PARAM;
  1648. str_data[n].val= (int)strtol(val,NULL,0);
  1649. }
  1650. }
  1651. fclose(fp);
  1652. }
  1653. /*==========================================
  1654. * エラー表示
  1655. *------------------------------------------*/
  1656. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1657. {
  1658. int i;
  1659. if( p == NULL || !p[0] ) return NULL;
  1660. if( line < 0 )
  1661. StringBuf_Printf(buf, "*% 5d : ", -line);
  1662. else
  1663. StringBuf_Printf(buf, " % 5d : ", line);
  1664. for(i=0;p[i] && p[i] != '\n';i++){
  1665. if(p + i != mark)
  1666. StringBuf_Printf(buf, "%c", p[i]);
  1667. else
  1668. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1669. }
  1670. StringBuf_AppendStr(buf, "\n");
  1671. return p+i+(p[i] == '\n' ? 1 : 0);
  1672. }
  1673. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1674. {
  1675. // エラーが発生した行を求める
  1676. int j;
  1677. int line = start_line;
  1678. const char *p;
  1679. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1680. StringBuf buf;
  1681. for(p=src;p && *p;line++){
  1682. const char *lineend=strchr(p,'\n');
  1683. if(lineend==NULL || error_pos<lineend){
  1684. break;
  1685. }
  1686. for( j = 0; j < 4; j++ ) {
  1687. linestart[j] = linestart[j+1];
  1688. }
  1689. linestart[4] = p;
  1690. p=lineend+1;
  1691. }
  1692. StringBuf_Init(&buf);
  1693. StringBuf_AppendStr(&buf, "\a\n");
  1694. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1695. StringBuf_Printf(&buf, " %s\n", error_msg);
  1696. for(j = 0; j < 5; j++ ) {
  1697. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1698. }
  1699. p = script_print_line(&buf, p, error_pos, -line);
  1700. for(j = 0; j < 5; j++) {
  1701. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1702. }
  1703. ShowError("%s", StringBuf_Value(&buf));
  1704. StringBuf_Destroy(&buf);
  1705. }
  1706. /*==========================================
  1707. * スクリプトの解析
  1708. *------------------------------------------*/
  1709. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1710. {
  1711. const char *p,*tmpp;
  1712. int i;
  1713. struct script_code* code = NULL;
  1714. static int first=1;
  1715. char end;
  1716. if( src == NULL )
  1717. return NULL;// empty script
  1718. memset(&syntax,0,sizeof(syntax));
  1719. if(first){
  1720. add_buildin_func();
  1721. read_constdb();
  1722. first=0;
  1723. }
  1724. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1725. script_pos=0;
  1726. script_size=SCRIPT_BLOCK_SIZE;
  1727. str_data[LABEL_NEXTLINE].type=C_NOP;
  1728. str_data[LABEL_NEXTLINE].backpatch=-1;
  1729. str_data[LABEL_NEXTLINE].label=-1;
  1730. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1731. if( options&SCRIPT_USE_LABEL_DB )
  1732. scriptlabel_db->clear(scriptlabel_db, NULL);
  1733. parse_options = options;
  1734. if( setjmp( error_jump ) != 0 ) {
  1735. //Restore program state when script has problems. [from jA]
  1736. int i;
  1737. const int size = ARRAYLENGTH(syntax.curly);
  1738. if( error_report )
  1739. script_error(src,file,line,error_msg,error_pos);
  1740. aFree( error_msg );
  1741. aFree( script_buf );
  1742. script_pos = 0;
  1743. script_size = 0;
  1744. script_buf = NULL;
  1745. for(i=LABEL_START;i<str_num;i++)
  1746. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1747. for(i=0; i<size; i++)
  1748. linkdb_final(&syntax.curly[i].case_label);
  1749. return NULL;
  1750. }
  1751. parse_syntax_for_flag=0;
  1752. p=src;
  1753. p=skip_space(p);
  1754. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1755. {// does not require brackets around the script
  1756. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1757. {// empty script and can return NULL
  1758. aFree( script_buf );
  1759. script_pos = 0;
  1760. script_size = 0;
  1761. script_buf = NULL;
  1762. return NULL;
  1763. }
  1764. end = '\0';
  1765. }
  1766. else
  1767. {// requires brackets around the script
  1768. if( *p != '{' )
  1769. disp_error_message("not found '{'",p);
  1770. p = skip_space(p+1);
  1771. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1772. {// empty script and can return NULL
  1773. aFree( script_buf );
  1774. script_pos = 0;
  1775. script_size = 0;
  1776. script_buf = NULL;
  1777. return NULL;
  1778. }
  1779. end = '}';
  1780. }
  1781. // clear references of labels, variables and internal functions
  1782. for(i=LABEL_START;i<str_num;i++){
  1783. if(
  1784. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1785. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1786. ){
  1787. str_data[i].type=C_NOP;
  1788. str_data[i].backpatch=-1;
  1789. str_data[i].label=-1;
  1790. }
  1791. }
  1792. while( syntax.curly_count != 0 || *p != end )
  1793. {
  1794. if( *p == '\0' )
  1795. disp_error_message("unexpected end of script",p);
  1796. // labelだけ特殊処理
  1797. tmpp=skip_space(skip_word(p));
  1798. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1799. i=add_word(p);
  1800. set_label(i,script_pos,p);
  1801. if( parse_options&SCRIPT_USE_LABEL_DB )
  1802. strdb_put(scriptlabel_db, get_str(i), (void*)script_pos);
  1803. p=tmpp+1;
  1804. p=skip_space(p);
  1805. continue;
  1806. }
  1807. // 他は全部一緒くた
  1808. p=parse_line(p);
  1809. p=skip_space(p);
  1810. add_scriptc(C_EOL);
  1811. set_label(LABEL_NEXTLINE,script_pos,p);
  1812. str_data[LABEL_NEXTLINE].type=C_NOP;
  1813. str_data[LABEL_NEXTLINE].backpatch=-1;
  1814. str_data[LABEL_NEXTLINE].label=-1;
  1815. }
  1816. add_scriptc(C_NOP);
  1817. // trim code to size
  1818. script_size = script_pos;
  1819. RECREATE(script_buf,unsigned char,script_pos);
  1820. // default unknown references to variables
  1821. for(i=LABEL_START;i<str_num;i++){
  1822. if(str_data[i].type==C_NOP){
  1823. int j,next;
  1824. str_data[i].type=C_NAME;
  1825. str_data[i].label=i;
  1826. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1827. next=GETVALUE(script_buf,j);
  1828. SETVALUE(script_buf,j,i);
  1829. j=next;
  1830. }
  1831. }
  1832. }
  1833. #ifdef DEBUG_DISP
  1834. for(i=0;i<script_pos;i++){
  1835. if((i&15)==0) printf("%04x : ",i);
  1836. ShowMessage("%02x ",script_buf[i]);
  1837. if((i&15)==15) printf("\n");
  1838. }
  1839. ShowMessage("\n");
  1840. #endif
  1841. #ifdef DEBUG_DISASM
  1842. {
  1843. int i = 0,j;
  1844. while(i < script_pos) {
  1845. ShowMessage("%06x ",i);
  1846. j = i;
  1847. switch(get_com(script_buf,&i)) {
  1848. case C_EOL: printf("C_EOL"); break;
  1849. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1850. case C_POS:
  1851. ShowMessage("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1852. i += 3;
  1853. break;
  1854. case C_NAME:
  1855. j = (*(int*)(script_buf+i)&0xffffff);
  1856. ShowMessage("C_NAME %s",j == 0xffffff ? "?? unknown ??" : get_str(j));
  1857. i += 3;
  1858. break;
  1859. case C_ARG: ShowMessage("C_ARG"); break;
  1860. case C_FUNC: ShowMessage("C_FUNC"); break;
  1861. case C_ADD: ShowMessage("C_ADD"); break;
  1862. case C_SUB: ShowMessage("C_SUB"); break;
  1863. case C_MUL: ShowMessage("C_MUL"); break;
  1864. case C_DIV: ShowMessage("C_DIV"); break;
  1865. case C_MOD: ShowMessage("C_MOD"); break;
  1866. case C_EQ: ShowMessage("C_EQ"); break;
  1867. case C_NE: ShowMessage("C_NE"); break;
  1868. case C_GT: ShowMessage("C_GT"); break;
  1869. case C_GE: ShowMessage("C_GE"); break;
  1870. case C_LT: ShowMessage("C_LT"); break;
  1871. case C_LE: ShowMessage("C_LE"); break;
  1872. case C_AND: ShowMessage("C_AND"); break;
  1873. case C_OR: ShowMessage("C_OR"); break;
  1874. case C_XOR: ShowMessage("C_XOR"); break;
  1875. case C_LAND: ShowMessage("C_LAND"); break;
  1876. case C_LOR: ShowMessage("C_LOR"); break;
  1877. case C_R_SHIFT: ShowMessage("C_R_SHIFT"); break;
  1878. case C_L_SHIFT: ShowMessage("C_L_SHIFT"); break;
  1879. case C_NEG: ShowMessage("C_NEG"); break;
  1880. case C_NOT: ShowMessage("C_NOT"); break;
  1881. case C_LNOT: ShowMessage("C_LNOT"); break;
  1882. case C_NOP: ShowMessage("C_NOP"); break;
  1883. case C_OP3: ShowMessage("C_OP3"); break;
  1884. case C_STR:
  1885. j = strlen(script_buf + i);
  1886. ShowMessage("C_STR %s",script_buf + i);
  1887. i+= j+1;
  1888. break;
  1889. default:
  1890. ShowMessage("unknown");
  1891. }
  1892. ShowMessage("\n");
  1893. }
  1894. }
  1895. #endif
  1896. CREATE(code,struct script_code,1);
  1897. code->script_buf = script_buf;
  1898. code->script_size = script_size;
  1899. code->script_vars = NULL;
  1900. return code;
  1901. }
  1902. /// Returns the player attached to this script, identified by the rid.
  1903. /// If there is no player attached, the script is terminated.
  1904. TBL_PC *script_rid2sd(struct script_state *st)
  1905. {
  1906. TBL_PC *sd=map_id2sd(st->rid);
  1907. if(!sd){
  1908. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1909. script_reportsrc(st);
  1910. st->state = END;
  1911. }
  1912. return sd;
  1913. }
  1914. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1915. ///
  1916. /// @param st Script state
  1917. /// @param data Variable/constant
  1918. void get_val(struct script_state* st, struct script_data* data)
  1919. {
  1920. const char* name;
  1921. char prefix;
  1922. char postfix;
  1923. TBL_PC* sd = NULL;
  1924. if( !data_isreference(data) )
  1925. return;// not a variable/constant
  1926. name = reference_getname(data);
  1927. prefix = name[0];
  1928. postfix = name[strlen(name) - 1];
  1929. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1930. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1931. {
  1932. sd = script_rid2sd(st);
  1933. if( sd == NULL )
  1934. {// needs player attached
  1935. if( postfix == '$' )
  1936. {// string variable
  1937. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1938. data->type = C_CONSTSTR;
  1939. data->u.str = "";
  1940. }
  1941. else
  1942. {// integer variable
  1943. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1944. data->type = C_INT;
  1945. data->u.num = 0;
  1946. }
  1947. return;
  1948. }
  1949. }
  1950. if( postfix == '$' )
  1951. {// string variable
  1952. switch( prefix )
  1953. {
  1954. case '@':
  1955. data->u.str = pc_readregstr(sd, data->u.num);
  1956. break;
  1957. case '$':
  1958. data->u.str = mapreg_readregstr(data->u.num);
  1959. break;
  1960. case '#':
  1961. if( name[1] == '#' )
  1962. data->u.str = pc_readaccountreg2str(sd, name);// global
  1963. else
  1964. data->u.str = pc_readaccountregstr(sd, name);// local
  1965. break;
  1966. case '.':
  1967. {
  1968. struct linkdb_node** n =
  1969. data->ref ? data->ref:
  1970. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  1971. &st->script->script_vars;// npc variable
  1972. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  1973. }
  1974. break;
  1975. default:
  1976. data->u.str = pc_readglobalreg_str(sd, name);
  1977. break;
  1978. }
  1979. if( data->u.str == NULL || data->u.str[0] == '\0' )
  1980. {// empty string
  1981. data->type = C_CONSTSTR;
  1982. data->u.str = "";
  1983. }
  1984. else
  1985. {// duplicate string
  1986. data->type = C_STR;
  1987. data->u.str = aStrdup(data->u.str);
  1988. }
  1989. }
  1990. else
  1991. {// integer variable
  1992. data->type = C_INT;
  1993. if( reference_toconstant(data) )
  1994. {
  1995. data->u.num = reference_getconstant(data);
  1996. }
  1997. else if( reference_toparam(data) )
  1998. {
  1999. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2000. }
  2001. else
  2002. switch( prefix )
  2003. {
  2004. case '@':
  2005. data->u.num = pc_readreg(sd, data->u.num);
  2006. break;
  2007. case '$':
  2008. data->u.num = mapreg_readreg(data->u.num);
  2009. break;
  2010. case '#':
  2011. if( name[1] == '#' )
  2012. data->u.num = pc_readaccountreg2(sd, name);// global
  2013. else
  2014. data->u.num = pc_readaccountreg(sd, name);// local
  2015. break;
  2016. case '.':
  2017. {
  2018. struct linkdb_node** n =
  2019. data->ref ? data->ref:
  2020. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2021. &st->script->script_vars;// npc variable
  2022. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2023. }
  2024. break;
  2025. default:
  2026. data->u.num = pc_readglobalreg(sd, name);
  2027. break;
  2028. }
  2029. }
  2030. return;
  2031. }
  2032. void push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2033. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2034. /// The value is left in the top of the stack and needs to be removed manually.
  2035. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2036. {
  2037. struct script_data* data;
  2038. push_val2(st->stack, C_NAME, uid, ref);
  2039. data = script_getdatatop(st, -1);
  2040. get_val(st, data);
  2041. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2042. }
  2043. /*==========================================
  2044. * Stores the value of a script variable
  2045. * Return value is 0 on fail, 1 on success.
  2046. *------------------------------------------*/
  2047. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2048. {
  2049. char prefix = name[0];
  2050. if( is_string_variable(name) )
  2051. {// string variable
  2052. const char* str = (const char*)value;
  2053. switch (prefix) {
  2054. case '@':
  2055. return pc_setregstr(sd, num, str);
  2056. case '$':
  2057. return mapreg_setregstr(num, str);
  2058. case '#':
  2059. return (name[1] == '#') ?
  2060. pc_setaccountreg2str(sd, name, str) :
  2061. pc_setaccountregstr(sd, name, str);
  2062. case '.': {
  2063. char* p;
  2064. struct linkdb_node** n;
  2065. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2066. p = (char*)linkdb_erase(n, (void*)num);
  2067. if (p) aFree(p);
  2068. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2069. }
  2070. return 1;
  2071. default:
  2072. return pc_setglobalreg_str(sd, name, str);
  2073. }
  2074. }
  2075. else
  2076. {// integer variable
  2077. int val = (int)value;
  2078. if(str_data[num&0x00ffffff].type == C_PARAM)
  2079. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  2080. switch (prefix) {
  2081. case '@':
  2082. return pc_setreg(sd, num, val);
  2083. case '$':
  2084. return mapreg_setreg(num, val);
  2085. case '#':
  2086. return (name[1] == '#') ?
  2087. pc_setaccountreg2(sd, name, val) :
  2088. pc_setaccountreg(sd, name, val);
  2089. case '.': {
  2090. struct linkdb_node** n;
  2091. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2092. if (val == 0)
  2093. linkdb_erase(n, (void*)num);
  2094. else
  2095. linkdb_replace(n, (void*)num, (void*)val);
  2096. }
  2097. return 1;
  2098. default:
  2099. return pc_setglobalreg(sd, name, val);
  2100. }
  2101. }
  2102. }
  2103. int set_var(TBL_PC* sd, char* name, void* val)
  2104. {
  2105. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2106. }
  2107. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2108. {
  2109. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2110. }
  2111. /// Converts the data to a string
  2112. const char* conv_str(struct script_state* st, struct script_data* data)
  2113. {
  2114. char* p;
  2115. get_val(st, data);
  2116. if( data_isstring(data) )
  2117. {// nothing to convert
  2118. }
  2119. else if( data_isint(data) )
  2120. {// int -> string
  2121. CREATE(p, char, ITEM_NAME_LENGTH);
  2122. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2123. p[ITEM_NAME_LENGTH-1] = '\0';
  2124. data->type = C_STR;
  2125. data->u.str = p;
  2126. }
  2127. else if( data_isreference(data) )
  2128. {// reference -> string
  2129. //##TODO when does this happen (check get_val) [FlavioJS]
  2130. data->type = C_CONSTSTR;
  2131. data->u.str = reference_getname(data);
  2132. }
  2133. else
  2134. {// unsupported data type
  2135. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2136. script_reportdata(data);
  2137. script_reportsrc(st);
  2138. data->type = C_CONSTSTR;
  2139. data->u.str = "";
  2140. }
  2141. return data->u.str;
  2142. }
  2143. /// Converts the data to an int
  2144. int conv_num(struct script_state* st, struct script_data* data)
  2145. {
  2146. char* p;
  2147. long num;
  2148. get_val(st, data);
  2149. if( data_isint(data) )
  2150. {// nothing to convert
  2151. }
  2152. else if( data_isstring(data) )
  2153. {// string -> int
  2154. // the result does not overflow or underflow, it is capped instead
  2155. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2156. p = data->u.str;
  2157. errno = 0;
  2158. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2159. if( errno == ERANGE
  2160. #if LONG_MAX > INT_MAX
  2161. || num < INT_MIN || num > INT_MAX
  2162. #endif
  2163. )
  2164. {
  2165. if( num <= INT_MIN )
  2166. {
  2167. num = INT_MIN;
  2168. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2169. }
  2170. else//if( num >= INT_MAX )
  2171. {
  2172. num = INT_MAX;
  2173. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2174. }
  2175. script_reportdata(data);
  2176. script_reportsrc(st);
  2177. }
  2178. if( data->type == C_STR )
  2179. aFree(p);
  2180. data->type = C_INT;
  2181. data->u.num = (int)num;
  2182. }
  2183. #if 0
  2184. // FIXME this function is being used to retrieve the position of labels and
  2185. // probably other stuff [FlavioJS]
  2186. else
  2187. {// unsupported data type
  2188. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2189. script_reportdata(data);
  2190. script_reportsrc(st);
  2191. data->type = C_INT;
  2192. data->u.num = 0;
  2193. }
  2194. #endif
  2195. return data->u.num;
  2196. }
  2197. //
  2198. // Stack operations
  2199. //
  2200. /// Increases the size of the stack
  2201. void stack_expand(struct script_stack* stack)
  2202. {
  2203. stack->sp_max += 64;
  2204. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2205. stack->sp_max * sizeof(stack->stack_data[0]) );
  2206. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2207. 64 * sizeof(stack->stack_data[0]) );
  2208. }
  2209. /// Pushes a value into the stack
  2210. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2211. /// Pushes a value into the stack (with reference)
  2212. void push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2213. {
  2214. if( stack->sp >= stack->sp_max )
  2215. stack_expand(stack);
  2216. stack->stack_data[stack->sp].type = type;
  2217. stack->stack_data[stack->sp].u.num = val;
  2218. stack->stack_data[stack->sp].ref = ref;
  2219. stack->sp++;
  2220. }
  2221. /// Pushes a string into the stack
  2222. void push_str(struct script_stack* stack, enum c_op type, char* str)
  2223. {
  2224. if( stack->sp >= stack->sp_max )
  2225. stack_expand(stack);
  2226. stack->stack_data[stack->sp].type = type;
  2227. stack->stack_data[stack->sp].u.str = str;
  2228. stack->stack_data[stack->sp].ref = NULL;
  2229. stack->sp++;
  2230. }
  2231. /// Pushes a copy of the target position into the stack
  2232. void push_copy(struct script_stack* stack, int pos)
  2233. {
  2234. switch( stack->stack_data[pos].type )
  2235. {
  2236. case C_CONSTSTR:
  2237. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2238. break;
  2239. case C_STR:
  2240. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2241. break;
  2242. default:
  2243. push_val2(
  2244. stack,stack->stack_data[pos].type,
  2245. stack->stack_data[pos].u.num,
  2246. stack->stack_data[pos].ref
  2247. );
  2248. break;
  2249. }
  2250. }
  2251. /// Removes the values in indexes [start,end[ from the stack
  2252. void pop_stack(struct script_stack* stack, int start, int end)
  2253. {
  2254. struct script_data* data;
  2255. int i;
  2256. if( start < 0 )
  2257. start = 0;
  2258. if( end > stack->sp_max )
  2259. end = stack->sp_max;
  2260. if( start >= end )
  2261. return;// nothing to pop
  2262. // free stack elements
  2263. for( i = start; i < end; i++ )
  2264. {
  2265. data = &stack->stack_data[i];
  2266. if( data->type == C_STR )
  2267. aFree(data->u.str);
  2268. data->type = C_NOP;
  2269. }
  2270. // move the rest of the elements
  2271. if( stack->sp > end )
  2272. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2273. stack->sp -= end - start;
  2274. }
  2275. ///
  2276. ///
  2277. ///
  2278. /*==========================================
  2279. * スクリプト依存変数、関数依存変数の解放
  2280. *------------------------------------------*/
  2281. void script_free_vars(struct linkdb_node **node)
  2282. {
  2283. struct linkdb_node* n = *node;
  2284. while( n != NULL)
  2285. {
  2286. const char* name = get_str((int)(n->key)&0x00ffffff);
  2287. if( is_string_variable(name) )
  2288. aFree(n->data); // 文字型変数なので、データ削除
  2289. n = n->next;
  2290. }
  2291. linkdb_final( node );
  2292. }
  2293. /*==========================================
  2294. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2295. *------------------------------------------*/
  2296. void script_free_stack(struct script_stack *stack)
  2297. {
  2298. int i;
  2299. for(i = 0; i < stack->sp; i++) {
  2300. if( stack->stack_data[i].type == C_STR ) {
  2301. aFree(stack->stack_data[i].u.str);
  2302. stack->stack_data[i].type = C_INT;
  2303. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2304. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2305. script_free_vars( n );
  2306. aFree( n );
  2307. }
  2308. }
  2309. script_free_vars( stack->var_function );
  2310. aFree(stack->var_function);
  2311. aFree(stack->stack_data);
  2312. aFree(stack);
  2313. }
  2314. void script_free_code(struct script_code* code)
  2315. {
  2316. script_free_vars( &code->script_vars );
  2317. aFree( code->script_buf );
  2318. aFree( code );
  2319. }
  2320. //
  2321. // 実行部main
  2322. //
  2323. /*==========================================
  2324. * コマンドの読み取り
  2325. *------------------------------------------*/
  2326. c_op get_com(unsigned char *script,int *pos)
  2327. {
  2328. int i = 0, j = 0;
  2329. if(script[*pos]>=0x80){
  2330. return C_INT;
  2331. }
  2332. while(script[*pos]>=0x40){
  2333. i=script[(*pos)++]<<j;
  2334. j+=6;
  2335. }
  2336. return (c_op)(i+(script[(*pos)++]<<j));
  2337. }
  2338. /*==========================================
  2339. * 数値の所得
  2340. *------------------------------------------*/
  2341. int get_num(unsigned char *script,int *pos)
  2342. {
  2343. int i,j;
  2344. i=0; j=0;
  2345. while(script[*pos]>=0xc0){
  2346. i+=(script[(*pos)++]&0x7f)<<j;
  2347. j+=6;
  2348. }
  2349. return i+((script[(*pos)++]&0x7f)<<j);
  2350. }
  2351. /*==========================================
  2352. * スタックから値を取り出す
  2353. *------------------------------------------*/
  2354. int pop_val(struct script_state* st)
  2355. {
  2356. if(st->stack->sp<=0)
  2357. return 0;
  2358. st->stack->sp--;
  2359. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2360. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2361. return st->stack->stack_data[st->stack->sp].u.num;
  2362. return 0;
  2363. }
  2364. /// Ternary operators
  2365. /// test ? if_true : if_false
  2366. void op_3(struct script_state* st, int op)
  2367. {
  2368. struct script_data* data;
  2369. int flag = 0;
  2370. data = script_getdatatop(st, -3);
  2371. get_val(st, data);
  2372. if( data_isstring(data) )
  2373. flag = data->u.str[0];// "" -> false
  2374. else if( data_isint(data) )
  2375. flag = data->u.num;// 0 -> false
  2376. else
  2377. {
  2378. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2379. script_reportdata(data);
  2380. script_reportsrc(st);
  2381. script_removetop(st, -3, 0);
  2382. script_pushnil(st);
  2383. return;
  2384. }
  2385. if( flag )
  2386. script_pushcopytop(st, -2);
  2387. else
  2388. script_pushcopytop(st, -1);
  2389. script_removetop(st, -4, -1);
  2390. }
  2391. /// Binary string operators
  2392. /// s1 EQ s2 -> i
  2393. /// s1 NE s2 -> i
  2394. /// s1 GT s2 -> i
  2395. /// s1 GE s2 -> i
  2396. /// s1 LT s2 -> i
  2397. /// s1 LE s2 -> i
  2398. /// s1 ADD s2 -> s
  2399. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2400. {
  2401. int a = 0;
  2402. switch(op){
  2403. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2404. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2405. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2406. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2407. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2408. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2409. case C_ADD:
  2410. {
  2411. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2412. strcpy(buf, s1);
  2413. strcat(buf, s2);
  2414. script_pushstr(st, buf);
  2415. return;
  2416. }
  2417. default:
  2418. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2419. script_reportsrc(st);
  2420. script_pushnil(st);
  2421. st->state = END;
  2422. return;
  2423. }
  2424. script_pushint(st,a);
  2425. }
  2426. /// Binary number operators
  2427. /// i OP i -> i
  2428. void op_2num(struct script_state* st, int op, int i1, int i2)
  2429. {
  2430. int ret;
  2431. double ret_double;
  2432. switch( op )
  2433. {
  2434. case C_AND: ret = i1 & i2; break;
  2435. case C_OR: ret = i1 | i2; break;
  2436. case C_XOR: ret = i1 ^ i2; break;
  2437. case C_LAND: ret = (i1 && i2); break;
  2438. case C_LOR: ret = (i1 || i2); break;
  2439. case C_EQ: ret = (i1 == i2); break;
  2440. case C_NE: ret = (i1 != i2); break;
  2441. case C_GT: ret = (i1 > i2); break;
  2442. case C_GE: ret = (i1 >= i2); break;
  2443. case C_LT: ret = (i1 < i2); break;
  2444. case C_LE: ret = (i1 <= i2); break;
  2445. case C_R_SHIFT: ret = i1>>i2; break;
  2446. case C_L_SHIFT: ret = i1<<i2; break;
  2447. case C_DIV:
  2448. case C_MOD:
  2449. if( i2 == 0 )
  2450. {
  2451. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2452. script_reportsrc(st);
  2453. script_pushnil(st);
  2454. st->state = END;
  2455. return;
  2456. }
  2457. else if( op == C_DIV )
  2458. ret = i1 / i2;
  2459. else//if( op == C_MOD )
  2460. ret = i1 % i2;
  2461. break;
  2462. default:
  2463. switch( op )
  2464. {// operators that can overflow/underflow
  2465. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2466. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2467. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2468. default:
  2469. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2470. script_reportsrc(st);
  2471. script_pushnil(st);
  2472. return;
  2473. }
  2474. if( ret_double < (double)INT_MIN )
  2475. {
  2476. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2477. script_reportsrc(st);
  2478. ret = INT_MIN;
  2479. }
  2480. else if( ret_double > (double)INT_MAX )
  2481. {
  2482. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2483. script_reportsrc(st);
  2484. ret = INT_MAX;
  2485. }
  2486. }
  2487. script_pushint(st, ret);
  2488. }
  2489. /// Binary operators
  2490. void op_2(struct script_state *st, int op)
  2491. {
  2492. struct script_data* left;
  2493. struct script_data* right;
  2494. left = script_getdatatop(st, -2);
  2495. right = script_getdatatop(st, -1);
  2496. get_val(st, left);
  2497. get_val(st, right);
  2498. // automatic conversions
  2499. switch( op )
  2500. {
  2501. case C_ADD:
  2502. if( data_isint(left) && data_isstring(right) )
  2503. {// convert int-string to string-string
  2504. conv_str(st, left);
  2505. }
  2506. else if( data_isstring(left) && data_isint(right) )
  2507. {// convert string-int to string-string
  2508. conv_str(st, right);
  2509. }
  2510. break;
  2511. }
  2512. if( data_isstring(left) && data_isstring(right) )
  2513. {// ss => op_2str
  2514. op_2str(st, op, left->u.str, right->u.str);
  2515. script_removetop(st, -3, -1);// pop the two values before the top one
  2516. }
  2517. else if( data_isint(left) && data_isint(right) )
  2518. {// ii => op_2num
  2519. int i1 = left->u.num;
  2520. int i2 = right->u.num;
  2521. script_removetop(st, -2, 0);
  2522. op_2num(st, op, i1, i2);
  2523. }
  2524. else
  2525. {// invalid argument
  2526. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2527. script_reportdata(left);
  2528. script_reportdata(right);
  2529. script_reportsrc(st);
  2530. script_removetop(st, -2, 0);
  2531. script_pushnil(st);
  2532. st->state = END;
  2533. }
  2534. }
  2535. /// Unary operators
  2536. /// NEG i -> i
  2537. /// NOT i -> i
  2538. /// LNOT i -> i
  2539. void op_1(struct script_state* st, int op)
  2540. {
  2541. struct script_data* data;
  2542. int i1;
  2543. data = script_getdatatop(st, -1);
  2544. get_val(st, data);
  2545. if( !data_isint(data) )
  2546. {// not a number
  2547. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2548. script_reportdata(data);
  2549. script_reportsrc(st);
  2550. script_pushnil(st);
  2551. st->state = END;
  2552. return;
  2553. }
  2554. i1 = data->u.num;
  2555. script_removetop(st, -1, 0);
  2556. switch( op )
  2557. {
  2558. case C_NEG: i1 = -i1; break;
  2559. case C_NOT: i1 = ~i1; break;
  2560. case C_LNOT: i1 = !i1; break;
  2561. default:
  2562. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2563. script_reportsrc(st);
  2564. script_pushnil(st);
  2565. st->state = END;
  2566. return;
  2567. }
  2568. script_pushint(st, i1);
  2569. }
  2570. /*==========================================
  2571. * 関数の実行
  2572. *------------------------------------------*/
  2573. int run_func(struct script_state *st)
  2574. {
  2575. int i,start_sp,end_sp,func;
  2576. end_sp=st->stack->sp;
  2577. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2578. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2579. ShowError("function not found\n");
  2580. // st->stack->sp=0;
  2581. st->state=END;
  2582. script_reportsrc(st);
  2583. return 1;
  2584. }
  2585. start_sp=i-1;
  2586. st->start=i-1;
  2587. st->end=end_sp;
  2588. func=st->stack->stack_data[st->start].u.num;
  2589. #ifdef DEBUG_RUN
  2590. if(battle_config.etc_log) {
  2591. ShowDebug("run_func : %s? (%d(%d)) sp=%d (%d...%d)\n", get_str(func), func, str_data[func].type, st->stack->sp, st->start, st->end);
  2592. ShowDebug("stack dump :");
  2593. for(i=0;i<end_sp;i++){
  2594. switch(st->stack->stack_data[i].type){
  2595. case C_INT:
  2596. ShowMessage(" int(%d)", st->stack->stack_data[i].u.num);
  2597. break;
  2598. case C_NAME:
  2599. ShowMessage(" name(%s)", get_str(st->stack->stack_data[i].u.num & 0xffffff);
  2600. break;
  2601. case C_ARG:
  2602. ShowMessage(" arg");
  2603. break;
  2604. case C_POS:
  2605. ShowMessage(" pos(%d)",st->stack->stack_data[i].u.num);
  2606. break;
  2607. case C_STR:
  2608. ShowMessage(" str(%s)",st->stack->stack_data[i].u.str);
  2609. break;
  2610. case C_CONSTSTR:
  2611. ShowMessage(" cstr(%s)",st->stack->stack_data[i].u.str);
  2612. break;
  2613. default:
  2614. ShowMessage(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2615. }
  2616. }
  2617. ShowMessage("\n");
  2618. }
  2619. #endif
  2620. if(str_data[func].type!=C_FUNC ){
  2621. ShowError("run_func: '"CL_WHITE"%s"CL_RESET"' (type %d) is not function and command!\n", get_str(func), str_data[func].type);
  2622. // st->stack->sp=0;
  2623. st->state=END;
  2624. script_reportsrc(st);
  2625. return 1;
  2626. }
  2627. #ifdef DEBUG_RUN
  2628. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n", get_str(func),func,str_data[func].type,st->pos);
  2629. #endif
  2630. if(str_data[func].func){
  2631. if (str_data[func].func(st)) //Report error
  2632. script_reportsrc(st);
  2633. } else {
  2634. ShowError("run_func : %s? (%d(%d))\n", get_str(func),func,str_data[func].type);
  2635. script_pushint(st,0);
  2636. script_reportsrc(st);
  2637. }
  2638. // Stack's datum are used when re-run functions [Eoe]
  2639. if(st->state != RERUNLINE) {
  2640. pop_stack(st->stack,start_sp,end_sp);
  2641. }
  2642. if(st->state==RETFUNC){
  2643. // ユーザー定義関数からの復帰
  2644. int olddefsp=st->stack->defsp;
  2645. int i;
  2646. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2647. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2648. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2649. st->state=END;
  2650. script_reportsrc(st);
  2651. return 1;
  2652. }
  2653. script_free_vars( st->stack->var_function );
  2654. aFree(st->stack->var_function);
  2655. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2656. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2657. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2658. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2659. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2660. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2661. st->state=GOTO;
  2662. }
  2663. return 0;
  2664. }
  2665. /*==========================================
  2666. * script execution
  2667. *------------------------------------------*/
  2668. void run_script_main(struct script_state *st);
  2669. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2670. {
  2671. struct script_state *st;
  2672. TBL_PC *sd=NULL;
  2673. if(rootscript==NULL || pos<0)
  2674. return;
  2675. if (rid) sd = map_id2sd(rid);
  2676. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2677. //Resume script.
  2678. st = sd->st;
  2679. } else {
  2680. st = (struct script_state*)aCalloc(sizeof(struct script_state), 1);
  2681. // the script is different, make new script_state and stack
  2682. st->stack = (struct script_stack*)aMalloc (sizeof(struct script_stack));
  2683. st->stack->sp=0;
  2684. st->stack->sp_max=64;
  2685. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2686. st->stack->defsp = st->stack->sp;
  2687. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  2688. st->state = RUN;
  2689. st->script = rootscript;
  2690. }
  2691. st->pos = pos;
  2692. st->rid = rid;
  2693. st->oid = oid;
  2694. st->sleep.timer = INVALID_TIMER;
  2695. run_script_main(st);
  2696. }
  2697. void script_stop_sleeptimers(int id)
  2698. {
  2699. struct script_state* st;
  2700. for(;;)
  2701. {
  2702. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2703. if( st == NULL )
  2704. break; // no more sleep timers
  2705. if( st->sleep.timer != INVALID_TIMER )
  2706. delete_timer(st->sleep.timer, run_script_timer);
  2707. script_free_stack(st->stack);
  2708. aFree(st);
  2709. }
  2710. }
  2711. /*==========================================
  2712. * 指定ノードをsleep_dbから削除
  2713. *------------------------------------------*/
  2714. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2715. {
  2716. struct linkdb_node *retnode;
  2717. if( n == NULL)
  2718. return NULL;
  2719. if( n->prev == NULL )
  2720. sleep_db = n->next;
  2721. else
  2722. n->prev->next = n->next;
  2723. if( n->next )
  2724. n->next->prev = n->prev;
  2725. retnode = n->next;
  2726. aFree( n );
  2727. return retnode; // 次のノードを返す
  2728. }
  2729. /*==========================================
  2730. * sleep用タイマー関数
  2731. *------------------------------------------*/
  2732. int run_script_timer(int tid, unsigned int tick, int id, intptr data)
  2733. {
  2734. struct script_state *st = (struct script_state *)data;
  2735. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2736. TBL_PC *sd = map_id2sd(st->rid);
  2737. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2738. { //Character mismatch. Cancel execution.
  2739. st->rid = 0;
  2740. st->state = END;
  2741. }
  2742. while( node && st->sleep.timer != -1 ) {
  2743. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2744. script_erase_sleepdb(node);
  2745. st->sleep.timer = INVALID_TIMER;
  2746. break;
  2747. }
  2748. node = node->next;
  2749. }
  2750. if(st->state != RERUNLINE)
  2751. st->sleep.tick = 0;
  2752. run_script_main(st);
  2753. return 0;
  2754. }
  2755. /*==========================================
  2756. * スクリプトの実行メイン部分
  2757. *------------------------------------------*/
  2758. void run_script_main(struct script_state *st)
  2759. {
  2760. int cmdcount=script_config.check_cmdcount;
  2761. int gotocount=script_config.check_gotocount;
  2762. TBL_PC *sd;
  2763. //For backing up purposes
  2764. struct script_state *bk_st = NULL;
  2765. int bk_npcid = 0;
  2766. struct script_stack *stack=st->stack;
  2767. sd = st->rid?map_id2sd(st->rid):NULL;
  2768. if(sd){
  2769. if(sd->st != st){
  2770. bk_st = sd->st;
  2771. bk_npcid = sd->npc_id;
  2772. }
  2773. sd->st = st;
  2774. sd->npc_id = st->oid;
  2775. }
  2776. if(st->state == RERUNLINE) {
  2777. st->state = RUN;
  2778. run_func(st);
  2779. if(st->state == GOTO)
  2780. st->state = RUN;
  2781. } else if(st->state != END)
  2782. st->state = RUN;
  2783. while(st->state == RUN)
  2784. {
  2785. enum c_op c = get_com(st->script->script_buf,&st->pos);
  2786. switch(c){
  2787. case C_EOL:
  2788. if( stack->sp != stack->defsp )
  2789. {
  2790. if( stack->sp > stack->defsp )
  2791. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2792. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2793. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2794. } else
  2795. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2796. stack->sp = stack->defsp;
  2797. }
  2798. break;
  2799. case C_INT:
  2800. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2801. break;
  2802. case C_POS:
  2803. case C_NAME:
  2804. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2805. st->pos+=3;
  2806. break;
  2807. case C_ARG:
  2808. push_val(stack,c,0);
  2809. break;
  2810. case C_STR:
  2811. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2812. while(st->script->script_buf[st->pos++]);
  2813. break;
  2814. case C_FUNC:
  2815. run_func(st);
  2816. if(st->state==GOTO){
  2817. st->state = RUN;
  2818. if( gotocount>0 && (--gotocount)<=0 ){
  2819. ShowError("run_script: infinity loop !\n");
  2820. script_reportsrc(st);
  2821. st->state=END;
  2822. }
  2823. }
  2824. break;
  2825. case C_NEG:
  2826. case C_NOT:
  2827. case C_LNOT:
  2828. op_1(st ,c);
  2829. break;
  2830. case C_ADD:
  2831. case C_SUB:
  2832. case C_MUL:
  2833. case C_DIV:
  2834. case C_MOD:
  2835. case C_EQ:
  2836. case C_NE:
  2837. case C_GT:
  2838. case C_GE:
  2839. case C_LT:
  2840. case C_LE:
  2841. case C_AND:
  2842. case C_OR:
  2843. case C_XOR:
  2844. case C_LAND:
  2845. case C_LOR:
  2846. case C_R_SHIFT:
  2847. case C_L_SHIFT:
  2848. op_2(st, c);
  2849. break;
  2850. case C_OP3:
  2851. op_3(st, c);
  2852. break;
  2853. case C_NOP:
  2854. st->state=END;
  2855. break;
  2856. default:
  2857. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2858. st->state=END;
  2859. break;
  2860. }
  2861. if( cmdcount>0 && (--cmdcount)<=0 ){
  2862. ShowError("run_script: infinity loop !\n");
  2863. script_reportsrc(st);
  2864. st->state=END;
  2865. }
  2866. }
  2867. if(st->sleep.tick > 0) {
  2868. //Delay execution
  2869. st->sleep.charid = sd?sd->status.char_id:0;
  2870. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2871. run_script_timer, st->sleep.charid, (int)st);
  2872. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2873. //Restore previous script
  2874. if (sd) {
  2875. sd->st = bk_st;
  2876. sd->npc_id = bk_npcid;
  2877. bk_st = NULL; //Remove tag for removal.
  2878. }
  2879. }
  2880. else if(st->state != END && sd){
  2881. //Resume later (st is already attached to player).
  2882. if(bk_st) {
  2883. ShowWarning("Unable to restore stack! Double continuation!\n");
  2884. //Report BOTH scripts to see if that can help somehow.
  2885. ShowDebug("Previous script (lost):\n");
  2886. script_reportsrc(bk_st);
  2887. ShowDebug("Current script:\n");
  2888. script_reportsrc(st);
  2889. }
  2890. } else {
  2891. //Dispose of script.
  2892. if (sd)
  2893. { //Restore previous stack and save char.
  2894. if(sd->state.using_fake_npc){
  2895. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2896. sd->state.using_fake_npc = 0;
  2897. }
  2898. //Restore previous script if any.
  2899. sd->st = bk_st;
  2900. sd->npc_id = bk_npcid;
  2901. if (!bk_st)
  2902. npc_event_dequeue(sd);
  2903. else
  2904. bk_st = NULL; //Remove tag for removal.
  2905. if (sd->state.reg_dirty&2)
  2906. intif_saveregistry(sd,2);
  2907. if (sd->state.reg_dirty&1)
  2908. intif_saveregistry(sd,1);
  2909. }
  2910. st->pos = -1;
  2911. script_free_stack (st->stack);
  2912. aFree(st);
  2913. }
  2914. if (bk_st)
  2915. { //Remove previous script
  2916. bk_st->pos = -1;
  2917. script_free_stack(bk_st->stack);
  2918. aFree(bk_st);
  2919. bk_st = NULL;
  2920. }
  2921. }
  2922. int script_config_read(char *cfgName)
  2923. {
  2924. int i;
  2925. char line[1024],w1[1024],w2[1024];
  2926. FILE *fp;
  2927. fp=fopen(cfgName,"r");
  2928. if(fp==NULL){
  2929. ShowError("file not found: [%s]\n", cfgName);
  2930. return 1;
  2931. }
  2932. while(fgets(line, sizeof(line), fp))
  2933. {
  2934. if(line[0] == '/' && line[1] == '/')
  2935. continue;
  2936. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  2937. if(i!=2)
  2938. continue;
  2939. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  2940. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  2941. }
  2942. else if(strcmpi(w1,"check_cmdcount")==0) {
  2943. script_config.check_cmdcount = config_switch(w2);
  2944. }
  2945. else if(strcmpi(w1,"check_gotocount")==0) {
  2946. script_config.check_gotocount = config_switch(w2);
  2947. }
  2948. else if(strcmpi(w1,"input_min_value")==0) {
  2949. script_config.input_min_value = config_switch(w2);
  2950. }
  2951. else if(strcmpi(w1,"input_max_value")==0) {
  2952. script_config.input_max_value = config_switch(w2);
  2953. }
  2954. else if(strcmpi(w1,"import")==0){
  2955. script_config_read(w2);
  2956. }
  2957. }
  2958. fclose(fp);
  2959. return 0;
  2960. }
  2961. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  2962. {
  2963. struct script_code *code = (struct script_code *)data;
  2964. if(code){
  2965. script_free_vars( &code->script_vars );
  2966. aFree( code->script_buf );
  2967. aFree( code );
  2968. }
  2969. return 0;
  2970. }
  2971. /*==========================================
  2972. * 終了
  2973. *------------------------------------------*/
  2974. int do_final_script()
  2975. {
  2976. #ifdef DEBUG_HASH
  2977. if (battle_config.etc_log)
  2978. {
  2979. FILE *fp = fopen("hash_dump.txt","wt");
  2980. if(fp) {
  2981. int i,count[SCRIPT_HASH_SIZE];
  2982. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  2983. int n=0;
  2984. int min=INT_MAX,max=0,zero=0;
  2985. double mean=0.0f;
  2986. double median=0.0f;
  2987. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  2988. memset(count, 0, sizeof(count));
  2989. fprintf(fp,"num : hash : data_name\n");
  2990. fprintf(fp,"---------------------------------------------------------------\n");
  2991. for(i=LABEL_START; i<str_num; i++) {
  2992. unsigned int h = calc_hash(get_str(i));
  2993. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  2994. ++count[h];
  2995. }
  2996. fprintf(fp,"--------------------\n\n");
  2997. memset(count2, 0, sizeof(count2));
  2998. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  2999. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3000. if(min > count[i])
  3001. min = count[i]; // minimun count of collision
  3002. if(max < count[i])
  3003. max = count[i]; // maximun count of collision
  3004. if(count[i] == 0)
  3005. zero++;
  3006. ++count2[count[i]];
  3007. }
  3008. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3009. for( i=min; i <= max; ++i ){
  3010. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3011. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3012. }
  3013. for( i=min; i <= max; ++i ){
  3014. n += count2[i];
  3015. if( n*2 >= SCRIPT_HASH_SIZE )
  3016. {
  3017. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3018. median = (i+i+1)/2.0f;
  3019. else
  3020. median = i;
  3021. break;
  3022. }
  3023. }
  3024. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3025. fclose(fp);
  3026. }
  3027. }
  3028. #endif
  3029. mapreg_final();
  3030. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3031. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3032. if(sleep_db) {
  3033. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3034. while(n) {
  3035. struct script_state *st = (struct script_state *)n->data;
  3036. script_free_stack(st->stack);
  3037. aFree(st);
  3038. n = n->next;
  3039. }
  3040. linkdb_final(&sleep_db);
  3041. }
  3042. if (str_data)
  3043. aFree(str_data);
  3044. if (str_buf)
  3045. aFree(str_buf);
  3046. return 0;
  3047. }
  3048. /*==========================================
  3049. * 初期化
  3050. *------------------------------------------*/
  3051. int do_init_script()
  3052. {
  3053. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3054. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3055. mapreg_init();
  3056. return 0;
  3057. }
  3058. int script_reload()
  3059. {
  3060. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3061. scriptlabel_db->clear(scriptlabel_db, NULL);
  3062. if(sleep_db) {
  3063. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3064. while(n) {
  3065. struct script_state *st = (struct script_state *)n->data;
  3066. if( st->sleep.timer != INVALID_TIMER )
  3067. delete_timer(st->sleep.timer, run_script_timer);
  3068. script_free_stack(st->stack);
  3069. aFree(st);
  3070. n = n->next;
  3071. }
  3072. linkdb_final(&sleep_db);
  3073. }
  3074. mapreg_reload();
  3075. return 0;
  3076. }
  3077. //-----------------------------------------------------------------------------
  3078. // buildin functions
  3079. //
  3080. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3081. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3082. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3083. /////////////////////////////////////////////////////////////////////
  3084. // NPC interaction
  3085. //
  3086. /// Appends a message to the npc dialog.
  3087. /// If a dialog doesn't exist yet, one is created.
  3088. ///
  3089. /// mes "<message>";
  3090. BUILDIN_FUNC(mes)
  3091. {
  3092. TBL_PC* sd = script_rid2sd(st);
  3093. if( sd == NULL )
  3094. return 0;
  3095. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3096. return 0;
  3097. }
  3098. /// Displays the button 'next' in the npc dialog.
  3099. /// The dialog text is cleared and the script continues when the button is pressed.
  3100. ///
  3101. /// next;
  3102. BUILDIN_FUNC(next)
  3103. {
  3104. TBL_PC* sd;
  3105. sd = script_rid2sd(st);
  3106. if( sd == NULL )
  3107. return 0;
  3108. st->state = STOP;
  3109. clif_scriptnext(sd, st->oid);
  3110. return 0;
  3111. }
  3112. /// Ends the script and displays the button 'close' on the npc dialog.
  3113. /// The dialog is closed when the button is pressed.
  3114. ///
  3115. /// close;
  3116. BUILDIN_FUNC(close)
  3117. {
  3118. TBL_PC* sd;
  3119. sd = script_rid2sd(st);
  3120. if( sd == NULL )
  3121. return 0;
  3122. st->state = END;
  3123. clif_scriptclose(sd, st->oid);
  3124. return 0;
  3125. }
  3126. /// Displays the button 'close' on the npc dialog.
  3127. /// The dialog is closed and the script continues when the button is pressed.
  3128. ///
  3129. /// close2;
  3130. BUILDIN_FUNC(close2)
  3131. {
  3132. TBL_PC* sd;
  3133. sd = script_rid2sd(st);
  3134. if( sd == NULL )
  3135. return 0;
  3136. st->state = STOP;
  3137. clif_scriptclose(sd, st->oid);
  3138. return 0;
  3139. }
  3140. /// Counts the number of valid and total number of options in 'str'
  3141. /// If max_count > 0 the counting stops when that valid option is reached
  3142. /// total is incremented for each option (NULL is supported)
  3143. static int menu_countoptions(const char* str, int max_count, int* total)
  3144. {
  3145. int count = 0;
  3146. int bogus_total;
  3147. if( total == NULL )
  3148. total = &bogus_total;
  3149. ++(*total);
  3150. // initial empty options
  3151. while( *str == ':' )
  3152. {
  3153. ++str;
  3154. ++(*total);
  3155. }
  3156. // count menu options
  3157. while( *str != '\0' )
  3158. {
  3159. ++count;
  3160. --max_count;
  3161. if( max_count == 0 )
  3162. break;
  3163. while( *str != ':' && *str != '\0' )
  3164. ++str;
  3165. while( *str == ':' )
  3166. {
  3167. ++str;
  3168. ++(*total);
  3169. }
  3170. }
  3171. return count;
  3172. }
  3173. /// Displays a menu with options and goes to the target label.
  3174. /// The script is stopped if cancel is pressed.
  3175. /// Options with no text are not displayed in the client.
  3176. ///
  3177. /// Options can be grouped together, separated by the character ':' in the text:
  3178. /// ex: menu "A:B:C",L_target;
  3179. /// All these options go to the specified target label.
  3180. ///
  3181. /// The index of the selected option is put in the variable @menu.
  3182. /// Indexes start with 1 and are consistent with grouped and empty options.
  3183. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3184. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3185. ///
  3186. /// NOTE: the client closes the npc dialog when cancel is pressed
  3187. ///
  3188. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3189. BUILDIN_FUNC(menu)
  3190. {
  3191. int i;
  3192. const char* text;
  3193. TBL_PC* sd;
  3194. sd = script_rid2sd(st);
  3195. if( sd == NULL )
  3196. return 0;
  3197. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3198. if( sd->state.menu_or_input == 0 )
  3199. {
  3200. struct StringBuf buf;
  3201. struct script_data* data;
  3202. if( script_lastdata(st) % 2 == 0 )
  3203. {// argument count is not even (1st argument is at index 2)
  3204. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3205. st->state = END;
  3206. return 1;
  3207. }
  3208. StringBuf_Init(&buf);
  3209. sd->npc_menu = 0;
  3210. for( i = 2; i < script_lastdata(st); i += 2 )
  3211. {
  3212. // menu options
  3213. text = script_getstr(st, i);
  3214. // target label
  3215. data = script_getdata(st, i+1);
  3216. if( !data_islabel(data) )
  3217. {// not a label
  3218. StringBuf_Destroy(&buf);
  3219. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3220. script_reportdata(data);
  3221. st->state = END;
  3222. return 1;
  3223. }
  3224. // append option(s)
  3225. if( text[0] == '\0' )
  3226. continue;// empty string, ignore
  3227. if( sd->npc_menu > 0 )
  3228. StringBuf_AppendStr(&buf, ":");
  3229. StringBuf_AppendStr(&buf, text);
  3230. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3231. }
  3232. st->state = RERUNLINE;
  3233. sd->state.menu_or_input = 1;
  3234. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3235. StringBuf_Destroy(&buf);
  3236. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3237. }
  3238. else if( sd->npc_menu == 0xff )
  3239. {// Cancel was pressed
  3240. sd->state.menu_or_input = 0;
  3241. st->state = END;
  3242. }
  3243. else
  3244. {// goto target label
  3245. int menu = 0;
  3246. sd->state.menu_or_input = 0;
  3247. if( sd->npc_menu <= 0 )
  3248. {
  3249. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3250. st->state = END;
  3251. return 1;
  3252. }
  3253. // get target label
  3254. for( i = 2; i < script_lastdata(st); i += 2 )
  3255. {
  3256. text = script_getstr(st, i);
  3257. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3258. if( sd->npc_menu <= 0 )
  3259. break;// entry found
  3260. }
  3261. if( sd->npc_menu > 0 )
  3262. {// Invalid selection
  3263. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3264. st->state = END;
  3265. return 1;
  3266. }
  3267. if( !data_islabel(script_getdata(st, i + 1)) )
  3268. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3269. ShowError("script:menu: unexpected data in label argument\n");
  3270. script_reportdata(script_getdata(st, i + 1));
  3271. st->state = END;
  3272. return 1;
  3273. }
  3274. pc_setreg(sd, add_str("@menu"), menu);
  3275. st->pos = script_getnum(st, i + 1);
  3276. st->state = GOTO;
  3277. }
  3278. return 0;
  3279. }
  3280. /// Displays a menu with options and returns the selected option.
  3281. /// Behaves like 'menu' without the target labels.
  3282. ///
  3283. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3284. ///
  3285. /// @see menu
  3286. BUILDIN_FUNC(select)
  3287. {
  3288. int i;
  3289. const char* text;
  3290. TBL_PC* sd;
  3291. sd = script_rid2sd(st);
  3292. if( sd == NULL )
  3293. return 0;
  3294. if( sd->state.menu_or_input == 0 )
  3295. {
  3296. struct StringBuf buf;
  3297. StringBuf_Init(&buf);
  3298. sd->npc_menu = 0;
  3299. for( i = 2; i <= script_lastdata(st); ++i )
  3300. {
  3301. text = script_getstr(st, i);
  3302. if( sd->npc_menu > 0 )
  3303. StringBuf_AppendStr(&buf, ":");
  3304. StringBuf_AppendStr(&buf, text);
  3305. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3306. }
  3307. st->state = RERUNLINE;
  3308. sd->state.menu_or_input = 1;
  3309. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3310. StringBuf_Destroy(&buf);
  3311. }
  3312. else if( sd->npc_menu == 0xff )
  3313. {// Cancel was pressed
  3314. sd->state.menu_or_input = 0;
  3315. st->state = END;
  3316. }
  3317. else
  3318. {// return selected option
  3319. int menu = 0;
  3320. sd->state.menu_or_input = 0;
  3321. for( i = 2; i <= script_lastdata(st); ++i )
  3322. {
  3323. text = script_getstr(st, i);
  3324. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3325. if( sd->npc_menu <= 0 )
  3326. break;// entry found
  3327. }
  3328. pc_setreg(sd, add_str("@menu"), menu);
  3329. script_pushint(st, menu);
  3330. }
  3331. return 0;
  3332. }
  3333. /// Displays a menu with options and returns the selected option.
  3334. /// Behaves like 'menu' without the target labels, except when cancel is
  3335. /// pressed.
  3336. /// When cancel is pressed, the script continues and 255 is returned.
  3337. ///
  3338. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3339. ///
  3340. /// @see menu
  3341. BUILDIN_FUNC(prompt)
  3342. {
  3343. int i;
  3344. const char *text;
  3345. TBL_PC* sd;
  3346. sd = script_rid2sd(st);
  3347. if( sd == NULL )
  3348. return 0;
  3349. if( sd->state.menu_or_input == 0 )
  3350. {
  3351. struct StringBuf buf;
  3352. StringBuf_Init(&buf);
  3353. sd->npc_menu = 0;
  3354. for( i = 2; i <= script_lastdata(st); ++i )
  3355. {
  3356. text = script_getstr(st, i);
  3357. if( sd->npc_menu > 0 )
  3358. StringBuf_AppendStr(&buf, ":");
  3359. StringBuf_AppendStr(&buf, text);
  3360. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3361. }
  3362. st->state = RERUNLINE;
  3363. sd->state.menu_or_input = 1;
  3364. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3365. StringBuf_Destroy(&buf);
  3366. }
  3367. else if( sd->npc_menu == 0xff )
  3368. {// Cancel was pressed
  3369. sd->state.menu_or_input = 0;
  3370. pc_setreg(sd, add_str("@menu"), 0xff);
  3371. script_pushint(st, 0xff);
  3372. }
  3373. else
  3374. {// return selected option
  3375. int menu = 0;
  3376. sd->state.menu_or_input = 0;
  3377. for( i = 2; i <= script_lastdata(st); ++i )
  3378. {
  3379. text = script_getstr(st, i);
  3380. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3381. if( sd->npc_menu <= 0 )
  3382. break;// entry found
  3383. }
  3384. pc_setreg(sd, add_str("@menu"), menu);
  3385. script_pushint(st, menu);
  3386. }
  3387. return 0;
  3388. }
  3389. /////////////////////////////////////////////////////////////////////
  3390. // ...
  3391. //
  3392. /// Jumps to the target script label.
  3393. ///
  3394. /// goto <label>;
  3395. BUILDIN_FUNC(goto)
  3396. {
  3397. if( !data_islabel(script_getdata(st,2)) )
  3398. {
  3399. ShowError("script:goto: not a label\n");
  3400. script_reportdata(script_getdata(st,2));
  3401. st->state = END;
  3402. return 1;
  3403. }
  3404. st->pos = script_getnum(st,2);
  3405. st->state = GOTO;
  3406. return 0;
  3407. }
  3408. /*==========================================
  3409. * user-defined function call
  3410. *------------------------------------------*/
  3411. BUILDIN_FUNC(callfunc)
  3412. {
  3413. int i, j;
  3414. struct linkdb_node** oldval;
  3415. struct script_code *scr, *oldscr;
  3416. const char* str = script_getstr(st,2);
  3417. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3418. if( !scr )
  3419. {
  3420. ShowError("script:callfunc: function not found! [%s]\n", str);
  3421. st->state = END;
  3422. return 1;
  3423. }
  3424. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3425. push_copy(st->stack,i);
  3426. script_pushint(st,j); // push argument count
  3427. script_pushint(st,st->stack->defsp); // push current stack pointer
  3428. script_pushint(st,(int)st->script); // push current script
  3429. script_pushint(st,(int)st->stack->var_function); // push function-dependent variables
  3430. push_val(st->stack,C_RETINFO,st->pos); // push current script location
  3431. oldscr = st->script;
  3432. oldval = st->stack->var_function;
  3433. st->pos = 0;
  3434. st->script = scr;
  3435. st->stack->defsp = st->start+5+j;
  3436. st->state = GOTO;
  3437. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3438. for( i = 0; i < j; i++ )
  3439. {
  3440. struct script_data* s = script_getdatatop(st, -6-i);
  3441. if( data_isreference(s) && !s->ref )
  3442. {
  3443. const char* name = reference_getname(s);
  3444. if( name[0] == '.' && name[1] == '@' )
  3445. s->ref = oldval;
  3446. else if( name[0] == '.' )
  3447. s->ref = &oldscr->script_vars;
  3448. }
  3449. }
  3450. return 0;
  3451. }
  3452. /*==========================================
  3453. * subroutine call
  3454. *------------------------------------------*/
  3455. BUILDIN_FUNC(callsub)
  3456. {
  3457. int i,j;
  3458. struct linkdb_node** oldval;
  3459. int pos = script_getnum(st,2);
  3460. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3461. {
  3462. ShowError("script:callsub: argument is not a label\n");
  3463. script_reportdata(script_getdata(st,2));
  3464. st->state = END;
  3465. return 1;
  3466. }
  3467. oldval = st->stack->var_function;
  3468. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3469. push_copy(st->stack,i);
  3470. script_pushint(st,j); // push argument count
  3471. script_pushint(st,st->stack->defsp); // push current stack pointer
  3472. script_pushint(st,(int)st->script); // push current script
  3473. script_pushint(st,(int)st->stack->var_function); // push function-dependent variables
  3474. push_val(st->stack,C_RETINFO,st->pos); // push current script location
  3475. st->pos = pos;
  3476. st->stack->defsp = st->start+5+j;
  3477. st->state = GOTO;
  3478. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3479. for(i = 0; i < j; i++)
  3480. {
  3481. struct script_data* s = script_getdatatop(st, -6-i);
  3482. if( data_isreference(s) && !s->ref )
  3483. {
  3484. const char* name = reference_getname(s);
  3485. if( name[0] == '.' && name[1] == '@' )
  3486. s->ref = oldval;
  3487. }
  3488. }
  3489. return 0;
  3490. }
  3491. /// Retrieves an argument provided to callfunc/callsub.
  3492. /// If the argument doesn't exist
  3493. ///
  3494. /// getarg(<index>{,<default_value>}) -> <value>
  3495. BUILDIN_FUNC(getarg)
  3496. {
  3497. int idx;
  3498. int count;
  3499. int stsp;
  3500. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3501. {
  3502. ShowError("script:getarg: no callfunc or callsub!\n");
  3503. st->state = END;
  3504. return 1;
  3505. }
  3506. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  3507. stsp = st->stack->defsp - count - 5;
  3508. idx = script_getnum(st,2);
  3509. if( idx < count )
  3510. push_copy(st->stack, stsp + idx);
  3511. else if( script_hasdata(st,3) )
  3512. script_pushcopy(st, 3);
  3513. else
  3514. {
  3515. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  3516. st->state = END;
  3517. return 1;
  3518. }
  3519. return 0;
  3520. }
  3521. /// Returns from the current function, optionaly returning a value from the functions.
  3522. /// Don't use outside script functions.
  3523. ///
  3524. /// return;
  3525. /// return <value>;
  3526. BUILDIN_FUNC(return)
  3527. {
  3528. if( script_hasdata(st,2) )
  3529. {// return value
  3530. struct script_data* data;
  3531. script_pushcopy(st, 2);
  3532. data = script_getdatatop(st, -1);
  3533. if( data_isreference(data) )
  3534. {
  3535. const char* name = reference_getname(data);
  3536. if( name[0] == '.' && name[1] == '@' )
  3537. {// temporary script variable, convert to value
  3538. get_val(st, data);
  3539. if( data_isstring(data) )
  3540. {// duplicate the string
  3541. data->type = C_STR;
  3542. data->u.str = aStrdup(data->u.str);
  3543. }
  3544. }
  3545. else if( name[0] == '.' && !data->ref )
  3546. {// script variable, link to current script
  3547. data->ref = &st->script->script_vars;
  3548. }
  3549. }
  3550. }
  3551. else
  3552. {// no return value
  3553. script_pushnil(st);
  3554. }
  3555. st->state = RETFUNC;
  3556. return 0;
  3557. }
  3558. /// Returns a random number from 0 to <range>-1.
  3559. /// Or returns a random number from <min> to <max>.
  3560. /// If <min> is greater than <max>, their numbers are switched.
  3561. /// rand(<range>) -> <int>
  3562. /// rand(<min>,<max>) -> <int>
  3563. BUILDIN_FUNC(rand)
  3564. {
  3565. int range;
  3566. int min;
  3567. int max;
  3568. if( script_hasdata(st,3) )
  3569. {// min,max
  3570. min = script_getnum(st,2);
  3571. max = script_getnum(st,3);
  3572. if( max < min )
  3573. swap(min, max);
  3574. range = max - min + 1;
  3575. }
  3576. else
  3577. {// range
  3578. min = 0;
  3579. range = script_getnum(st,2);
  3580. }
  3581. if( range <= 1 )
  3582. script_pushint(st, min);
  3583. else
  3584. script_pushint(st, rand()%range + min);
  3585. return 0;
  3586. }
  3587. /*==========================================
  3588. *
  3589. *------------------------------------------*/
  3590. BUILDIN_FUNC(warp)
  3591. {
  3592. int ret;
  3593. int x,y;
  3594. const char* str;
  3595. TBL_PC* sd;
  3596. sd = script_rid2sd(st);
  3597. if( sd == NULL )
  3598. return 0;
  3599. str = script_getstr(st,2);
  3600. x = script_getnum(st,3);
  3601. y = script_getnum(st,4);
  3602. if(strcmp(str,"Random")==0)
  3603. ret = pc_randomwarp(sd,3);
  3604. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3605. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3606. else
  3607. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3608. if( ret ) {
  3609. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3610. script_reportsrc(st);
  3611. }
  3612. return 0;
  3613. }
  3614. /*==========================================
  3615. * エリア指定ワープ
  3616. *------------------------------------------*/
  3617. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3618. {
  3619. int x,y;
  3620. unsigned int map;
  3621. map=va_arg(ap, unsigned int);
  3622. x=va_arg(ap,int);
  3623. y=va_arg(ap,int);
  3624. if(map == 0)
  3625. pc_randomwarp((TBL_PC *)bl,3);
  3626. else
  3627. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3628. return 0;
  3629. }
  3630. BUILDIN_FUNC(areawarp)
  3631. {
  3632. int x,y,m;
  3633. unsigned int index;
  3634. const char *str;
  3635. const char *mapname;
  3636. int x0,y0,x1,y1;
  3637. mapname=script_getstr(st,2);
  3638. x0=script_getnum(st,3);
  3639. y0=script_getnum(st,4);
  3640. x1=script_getnum(st,5);
  3641. y1=script_getnum(st,6);
  3642. str=script_getstr(st,7);
  3643. x=script_getnum(st,8);
  3644. y=script_getnum(st,9);
  3645. if( (m=map_mapname2mapid(mapname))< 0)
  3646. return 0;
  3647. if(strcmp(str,"Random")==0)
  3648. index = 0;
  3649. else if(!(index=mapindex_name2id(str)))
  3650. return 0;
  3651. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  3652. return 0;
  3653. }
  3654. /*==========================================
  3655. * warpchar [LuzZza]
  3656. * Useful for warp one player from
  3657. * another player npc-session.
  3658. * Using: warpchar "mapname",x,y,Char_ID;
  3659. *------------------------------------------*/
  3660. BUILDIN_FUNC(warpchar)
  3661. {
  3662. int x,y,a;
  3663. const char *str;
  3664. TBL_PC *sd;
  3665. str=script_getstr(st,2);
  3666. x=script_getnum(st,3);
  3667. y=script_getnum(st,4);
  3668. a=script_getnum(st,5);
  3669. sd = map_charid2sd(a);
  3670. if( sd == NULL )
  3671. return 0;
  3672. if(strcmp(str, "Random") == 0)
  3673. pc_randomwarp(sd, 3);
  3674. else
  3675. if(strcmp(str, "SavePoint") == 0)
  3676. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3677. else
  3678. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3679. return 0;
  3680. }
  3681. /*==========================================
  3682. * Warpparty - [Fredzilla]
  3683. * Syntax: warpparty "mapname",x,y,Party_ID;
  3684. *------------------------------------------*/
  3685. BUILDIN_FUNC(warpparty)
  3686. {
  3687. int x,y;
  3688. const char *str;
  3689. int p_id;
  3690. int i;
  3691. unsigned short mapindex;
  3692. TBL_PC *pl_sd;
  3693. struct party_data *p=NULL;
  3694. str=script_getstr(st,2);
  3695. x=script_getnum(st,3);
  3696. y=script_getnum(st,4);
  3697. p_id=script_getnum(st,5);
  3698. if(p_id < 1)
  3699. return 0;
  3700. p = party_search(p_id);
  3701. if (!p)
  3702. return 0;
  3703. if(strcmp(str,"Random")==0)
  3704. {
  3705. for (i = 0; i < MAX_PARTY; i++)
  3706. {
  3707. if ((pl_sd = p->data[i].sd))
  3708. {
  3709. if(map[pl_sd->bl.m].flag.nowarp)
  3710. continue;
  3711. pc_randomwarp(pl_sd,3);
  3712. }
  3713. }
  3714. }
  3715. else if(strcmp(str,"SavePointAll")==0)
  3716. {
  3717. for (i = 0; i < MAX_PARTY; i++)
  3718. {
  3719. if ((pl_sd = p->data[i].sd))
  3720. {
  3721. if(map[pl_sd->bl.m].flag.noreturn)
  3722. continue;
  3723. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3724. }
  3725. }
  3726. }
  3727. else if(strcmp(str,"SavePoint")==0)
  3728. {
  3729. pl_sd=script_rid2sd(st);
  3730. if (!pl_sd) return 0;
  3731. mapindex=pl_sd->status.save_point.map;
  3732. x=pl_sd->status.save_point.x;
  3733. y=pl_sd->status.save_point.y;
  3734. for (i = 0; i < MAX_PARTY; i++)
  3735. {
  3736. if ((pl_sd = p->data[i].sd))
  3737. {
  3738. if(map[pl_sd->bl.m].flag.noreturn)
  3739. continue;
  3740. pc_setpos(pl_sd,mapindex,x,y,3);
  3741. }
  3742. }
  3743. }
  3744. else if(strcmp(str,"Leader")==0)
  3745. {
  3746. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  3747. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  3748. return 0;
  3749. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  3750. return 0;
  3751. mapindex = p->data[i].sd->mapindex;
  3752. x = p->data[i].sd->bl.x;
  3753. y = p->data[i].sd->bl.y;
  3754. for (i = 0; i < MAX_PARTY; i++)
  3755. {
  3756. pl_sd = p->data[i].sd;
  3757. if (!pl_sd)
  3758. continue;
  3759. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3760. continue;
  3761. pc_setpos(pl_sd,mapindex,x,y,3);
  3762. }
  3763. }
  3764. else
  3765. {
  3766. mapindex = mapindex_name2id(str);
  3767. if (!mapindex) //Show source of npc error.
  3768. return 1;
  3769. for (i = 0; i < MAX_PARTY; i++)
  3770. {
  3771. if ((pl_sd = p->data[i].sd))
  3772. {
  3773. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3774. continue;
  3775. pc_setpos(pl_sd,mapindex,x,y,3);
  3776. }
  3777. }
  3778. }
  3779. return 0;
  3780. }
  3781. /*==========================================
  3782. * Warpguild - [Fredzilla]
  3783. * Syntax: warpguild "mapname",x,y,Guild_ID;
  3784. *------------------------------------------*/
  3785. BUILDIN_FUNC(warpguild)
  3786. {
  3787. TBL_PC *sd;
  3788. TBL_PC *pl_sd;
  3789. struct guild* g;
  3790. struct s_mapiterator* iter;
  3791. int type;
  3792. const char* str = script_getstr(st,2);
  3793. int x = script_getnum(st,3);
  3794. int y = script_getnum(st,4);
  3795. int gid = script_getnum(st,5);
  3796. sd=script_rid2sd(st);
  3797. if( sd == NULL )
  3798. return 0;
  3799. g = guild_search(gid);
  3800. if( g == NULL )
  3801. return 0;
  3802. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  3803. return 0;
  3804. type = ( strcmp(str,"Random")==0 ) ? 0
  3805. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  3806. : ( strcmp(str,"SavePoint")==0 ) ? 2
  3807. : 3;
  3808. iter = mapit_getallusers();
  3809. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  3810. {
  3811. if( pl_sd->status.guild_id != gid )
  3812. continue;
  3813. switch( type )
  3814. {
  3815. case 0: // Random
  3816. if(!map[pl_sd->bl.m].flag.nowarp)
  3817. pc_randomwarp(pl_sd,3);
  3818. break;
  3819. case 1: // SavePointAll
  3820. if(!map[pl_sd->bl.m].flag.noreturn)
  3821. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3822. break;
  3823. case 2: // SavePoint
  3824. if(!map[pl_sd->bl.m].flag.noreturn)
  3825. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3826. break;
  3827. case 3: // m,x,y
  3828. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  3829. pc_setpos(pl_sd,mapindex_name2id(str),x,y,3);
  3830. break;
  3831. }
  3832. }
  3833. mapit_free(iter);
  3834. return 0;
  3835. }
  3836. /*==========================================
  3837. *
  3838. *------------------------------------------*/
  3839. BUILDIN_FUNC(heal)
  3840. {
  3841. TBL_PC *sd;
  3842. int hp,sp;
  3843. sd = script_rid2sd(st);
  3844. if (!sd) return 0;
  3845. hp=script_getnum(st,2);
  3846. sp=script_getnum(st,3);
  3847. status_heal(&sd->bl, hp, sp, 1);
  3848. return 0;
  3849. }
  3850. /*==========================================
  3851. *
  3852. *------------------------------------------*/
  3853. BUILDIN_FUNC(itemheal)
  3854. {
  3855. TBL_PC *sd;
  3856. int hp,sp;
  3857. hp=script_getnum(st,2);
  3858. sp=script_getnum(st,3);
  3859. if(potion_flag==1) {
  3860. potion_hp = hp;
  3861. potion_sp = sp;
  3862. return 0;
  3863. }
  3864. sd = script_rid2sd(st);
  3865. if (!sd) return 0;
  3866. pc_itemheal(sd,sd->itemid,hp,sp);
  3867. return 0;
  3868. }
  3869. /*==========================================
  3870. *
  3871. *------------------------------------------*/
  3872. BUILDIN_FUNC(percentheal)
  3873. {
  3874. int hp,sp;
  3875. TBL_PC* sd;
  3876. hp=script_getnum(st,2);
  3877. sp=script_getnum(st,3);
  3878. if(potion_flag==1) {
  3879. potion_per_hp = hp;
  3880. potion_per_sp = sp;
  3881. return 0;
  3882. }
  3883. sd = script_rid2sd(st);
  3884. if( sd == NULL )
  3885. return 0;
  3886. pc_percentheal(sd,hp,sp);
  3887. return 0;
  3888. }
  3889. /*==========================================
  3890. *
  3891. *------------------------------------------*/
  3892. BUILDIN_FUNC(jobchange)
  3893. {
  3894. int job, upper=-1;
  3895. job=script_getnum(st,2);
  3896. if( script_hasdata(st,3) )
  3897. upper=script_getnum(st,3);
  3898. if (pcdb_checkid(job))
  3899. {
  3900. TBL_PC* sd;
  3901. sd = script_rid2sd(st);
  3902. if( sd == NULL )
  3903. return 0;
  3904. pc_jobchange(sd, job, upper);
  3905. }
  3906. return 0;
  3907. }
  3908. /*==========================================
  3909. *
  3910. *------------------------------------------*/
  3911. BUILDIN_FUNC(jobname)
  3912. {
  3913. int class_=script_getnum(st,2);
  3914. script_pushconststr(st,job_name(class_));
  3915. return 0;
  3916. }
  3917. /// Get input from the player.
  3918. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  3919. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  3920. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  3921. /// shorter than 'min' and 0 otherwise.
  3922. ///
  3923. /// input(<var>{,<min>{,<max>}}) -> <int>
  3924. BUILDIN_FUNC(input)
  3925. {
  3926. TBL_PC* sd;
  3927. struct script_data* data;
  3928. int uid;
  3929. const char* name;
  3930. int min;
  3931. int max;
  3932. sd = script_rid2sd(st);
  3933. if( sd == NULL )
  3934. return 0;
  3935. data = script_getdata(st,2);
  3936. if( !data_isreference(data) ){
  3937. ShowError("script:input: not a variable\n");
  3938. script_reportdata(data);
  3939. st->state = END;
  3940. return 1;
  3941. }
  3942. uid = reference_getuid(data);
  3943. name = reference_getname(data);
  3944. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  3945. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  3946. if( !sd->state.menu_or_input )
  3947. { // first invocation, display npc input box
  3948. sd->state.menu_or_input = 1;
  3949. st->state = RERUNLINE;
  3950. if( is_string_variable(name) )
  3951. clif_scriptinputstr(sd,st->oid);
  3952. else
  3953. clif_scriptinput(sd,st->oid);
  3954. }
  3955. else
  3956. { // take received text/value and store it in the designated variable
  3957. sd->state.menu_or_input = 0;
  3958. if( is_string_variable(name) )
  3959. {
  3960. size_t len = strlen(sd->npc_str);
  3961. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  3962. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  3963. }
  3964. else
  3965. {
  3966. int amount = sd->npc_amount;
  3967. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  3968. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  3969. }
  3970. }
  3971. return 0;
  3972. }
  3973. /// Sets the value of a variable.
  3974. /// The value is converted to the type of the variable.
  3975. ///
  3976. /// set(<variable>,<value>) -> <variable>
  3977. BUILDIN_FUNC(set)
  3978. {
  3979. TBL_PC* sd = NULL;
  3980. struct script_data* data;
  3981. int num;
  3982. const char* name;
  3983. char prefix;
  3984. data = script_getdata(st,2);
  3985. if( !data_isreference(data) )
  3986. {
  3987. ShowError("script:set: not a variable\n");
  3988. script_reportdata(script_getdata(st,2));
  3989. st->state = END;
  3990. return 1;
  3991. }
  3992. num = reference_getuid(data);
  3993. name = reference_getname(data);
  3994. prefix = *name;
  3995. if( not_server_variable(prefix) )
  3996. {
  3997. sd = script_rid2sd(st);
  3998. if( sd == NULL )
  3999. {
  4000. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4001. return 0;
  4002. }
  4003. }
  4004. if( is_string_variable(name) )
  4005. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4006. else
  4007. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4008. // return a copy of the variable reference
  4009. script_pushcopy(st,2);
  4010. return 0;
  4011. }
  4012. /////////////////////////////////////////////////////////////////////
  4013. /// Array variables
  4014. ///
  4015. /// Returns the size of the specified array
  4016. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4017. {
  4018. int32 ret = idx;
  4019. if( isstring )
  4020. {
  4021. for( ; idx < 128; ++idx )
  4022. {
  4023. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4024. if( str && *str )
  4025. ret = idx + 1;
  4026. script_removetop(st, -1, 0);
  4027. }
  4028. }
  4029. else
  4030. {
  4031. for( ; idx < 128; ++idx )
  4032. {
  4033. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4034. if( num )
  4035. ret = idx + 1;
  4036. script_removetop(st, -1, 0);
  4037. }
  4038. }
  4039. return ret;
  4040. }
  4041. /// Sets values of an array, from the starting index.
  4042. /// ex: setarray arr[1],1,2,3;
  4043. ///
  4044. /// setarray <array variable>,<value1>{,<value2>...};
  4045. BUILDIN_FUNC(setarray)
  4046. {
  4047. struct script_data* data;
  4048. const char* name;
  4049. int32 start;
  4050. int32 end;
  4051. int32 id;
  4052. int32 i;
  4053. TBL_PC* sd = NULL;
  4054. data = script_getdata(st, 2);
  4055. if( !data_isreference(data) )
  4056. {
  4057. ShowError("script:setarray: not a variable\n");
  4058. script_reportdata(data);
  4059. st->state = END;
  4060. return 1;// not a variable
  4061. }
  4062. id = reference_getid(data);
  4063. start = reference_getindex(data);
  4064. name = reference_getname(data);
  4065. if( not_array_variable(*name) )
  4066. {
  4067. ShowError("script:setarray: illegal scope\n");
  4068. script_reportdata(data);
  4069. st->state = END;
  4070. return 1;// not supported
  4071. }
  4072. if( not_server_variable(*name) )
  4073. {
  4074. sd = script_rid2sd(st);
  4075. if( sd == NULL )
  4076. return 0;// no player attached
  4077. }
  4078. end = start + script_lastdata(st) - 2;
  4079. if( end > 127 )
  4080. end = 127;
  4081. if( is_string_variable(name) )
  4082. {// string array
  4083. for( i = 3; start < end; ++start, ++i )
  4084. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4085. }
  4086. else
  4087. {// int array
  4088. for( i = 3; start < end; ++start, ++i )
  4089. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4090. }
  4091. return 0;
  4092. }
  4093. /// Sets count values of an array, from the starting index.
  4094. /// ex: cleararray arr[0],0,1;
  4095. ///
  4096. /// cleararray <array variable>,<value>,<count>;
  4097. BUILDIN_FUNC(cleararray)
  4098. {
  4099. struct script_data* data;
  4100. const char* name;
  4101. int32 start;
  4102. int32 end;
  4103. int32 id;
  4104. void* v;
  4105. TBL_PC* sd = NULL;
  4106. data = script_getdata(st, 2);
  4107. if( !data_isreference(data) )
  4108. {
  4109. ShowError("script:cleararray: not a variable\n");
  4110. script_reportdata(data);
  4111. st->state = END;
  4112. return 1;// not a variable
  4113. }
  4114. id = reference_getid(data);
  4115. start = reference_getindex(data);
  4116. name = reference_getname(data);
  4117. if( not_array_variable(*name) )
  4118. {
  4119. ShowError("script:cleararray: illegal scope\n");
  4120. script_reportdata(data);
  4121. st->state = END;
  4122. return 1;// not supported
  4123. }
  4124. if( not_server_variable(*name) )
  4125. {
  4126. sd = script_rid2sd(st);
  4127. if( sd == NULL )
  4128. return 0;// no player attached
  4129. }
  4130. if( is_string_variable(name) )
  4131. v = (void*)script_getstr(st, 3);
  4132. else
  4133. v = (void*)script_getnum(st, 3);
  4134. end = start + script_getnum(st, 4);
  4135. if( end > 127 )
  4136. end = 127;
  4137. for( ; start <= end; ++start )
  4138. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4139. return 0;
  4140. }
  4141. /// Copies data from one array to another.
  4142. /// ex: copyarray arr[0],arr[2],2;
  4143. ///
  4144. /// copyarray <destination array variable>,<source array variable>,<count>;
  4145. BUILDIN_FUNC(copyarray)
  4146. {
  4147. struct script_data* data1;
  4148. struct script_data* data2;
  4149. const char* name1;
  4150. const char* name2;
  4151. int32 idx1;
  4152. int32 idx2;
  4153. int32 id1;
  4154. int32 id2;
  4155. void* v;
  4156. int32 i;
  4157. int32 count;
  4158. TBL_PC* sd = NULL;
  4159. data1 = script_getdata(st, 2);
  4160. data2 = script_getdata(st, 3);
  4161. if( !data_isreference(data1) || !data_isreference(data2) )
  4162. {
  4163. ShowError("script:copyarray: not a variable\n");
  4164. script_reportdata(data1);
  4165. script_reportdata(data2);
  4166. st->state = END;
  4167. return 1;// not a variable
  4168. }
  4169. id1 = reference_getid(data1);
  4170. id2 = reference_getid(data2);
  4171. idx1 = reference_getindex(data1);
  4172. idx2 = reference_getindex(data2);
  4173. name1 = reference_getname(data1);
  4174. name2 = reference_getname(data2);
  4175. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4176. {
  4177. ShowError("script:copyarray: illegal scope\n");
  4178. script_reportdata(data1);
  4179. script_reportdata(data2);
  4180. st->state = END;
  4181. return 1;// not supported
  4182. }
  4183. if( is_string_variable(name1) != is_string_variable(name2) )
  4184. {
  4185. ShowError("script:copyarray: type mismatch\n");
  4186. script_reportdata(data1);
  4187. script_reportdata(data2);
  4188. st->state = END;
  4189. return 1;// data type mismatch
  4190. }
  4191. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4192. {
  4193. sd = script_rid2sd(st);
  4194. if( sd == NULL )
  4195. return 0;// no player attached
  4196. }
  4197. count = script_getnum(st, 4);
  4198. if( count > 128 - idx1 )
  4199. count = 128 - idx1;
  4200. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4201. return 0;// nothing to copy
  4202. if( id1 == id2 && idx1 > idx2 )
  4203. {// destination might be overlapping the source - copy in reverse order
  4204. for( i = count - 1; i >= 0; --i )
  4205. {
  4206. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4207. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4208. script_removetop(st, -1, 0);
  4209. }
  4210. }
  4211. else
  4212. {// normal copy
  4213. for( i = 0; i < count; ++i )
  4214. {
  4215. if( idx2 + i < 128 )
  4216. {
  4217. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4218. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4219. script_removetop(st, -1, 0);
  4220. }
  4221. else// out of range - assume ""/0
  4222. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4223. }
  4224. }
  4225. return 0;
  4226. }
  4227. /// Returns the size of the array.
  4228. /// Assumes that everything before the starting index exists.
  4229. /// ex: getarraysize(arr[3])
  4230. ///
  4231. /// getarraysize(<array variable>) -> <int>
  4232. BUILDIN_FUNC(getarraysize)
  4233. {
  4234. struct script_data* data;
  4235. const char* name;
  4236. data = script_getdata(st, 2);
  4237. if( !data_isreference(data) )
  4238. {
  4239. ShowError("script:getarraysize: not a variable\n");
  4240. script_reportdata(data);
  4241. script_pushnil(st);
  4242. st->state = END;
  4243. return 1;// not a variable
  4244. }
  4245. name = reference_getname(data);
  4246. if( not_array_variable(*name) )
  4247. {
  4248. ShowError("script:getarraysize: illegal scope\n");
  4249. script_reportdata(data);
  4250. script_pushnil(st);
  4251. st->state = END;
  4252. return 1;// not supported
  4253. }
  4254. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4255. return 0;
  4256. }
  4257. /// Deletes count or all the elements in an array, from the starting index.
  4258. /// ex: deletearray arr[4],2;
  4259. ///
  4260. /// deletearray <array variable>;
  4261. /// deletearray <array variable>,<count>;
  4262. BUILDIN_FUNC(deletearray)
  4263. {
  4264. struct script_data* data;
  4265. const char* name;
  4266. int start;
  4267. int end;
  4268. int id;
  4269. TBL_PC *sd = NULL;
  4270. data = script_getdata(st, 2);
  4271. if( !data_isreference(data) )
  4272. {
  4273. ShowError("script:deletearray: not a variable\n");
  4274. script_reportdata(data);
  4275. st->state = END;
  4276. return 1;// not a variable
  4277. }
  4278. id = reference_getid(data);
  4279. start = reference_getindex(data);
  4280. name = reference_getname(data);
  4281. if( not_array_variable(*name) )
  4282. {
  4283. ShowError("script:deletearray: illegal scope\n");
  4284. script_reportdata(data);
  4285. st->state = END;
  4286. return 1;// not supported
  4287. }
  4288. if( not_server_variable(*name) )
  4289. {
  4290. sd = script_rid2sd(st);
  4291. if( sd == NULL )
  4292. return 0;// no player attached
  4293. }
  4294. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4295. if( start >= end )
  4296. return 0;// nothing to free
  4297. if( script_hasdata(st,3) )
  4298. {
  4299. int count = script_getnum(st, 3);
  4300. if( count > end - start )
  4301. count = end - start;
  4302. if( count <= 0 )
  4303. return 0;// nothing to free
  4304. // move rest of the elements backward
  4305. for( ; start + count < end; ++start )
  4306. {
  4307. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4308. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4309. script_removetop(st, -1, 0);
  4310. }
  4311. }
  4312. // clear the rest of the array
  4313. if( is_string_variable(name) )
  4314. {
  4315. for( ; start < end; ++start )
  4316. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4317. }
  4318. else
  4319. {
  4320. for( ; start < end; ++start )
  4321. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4322. }
  4323. return 0;
  4324. }
  4325. /// Returns a reference to the target index of the array variable.
  4326. /// Equivalent to var[index].
  4327. ///
  4328. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4329. BUILDIN_FUNC(getelementofarray)
  4330. {
  4331. struct script_data* data;
  4332. const char* name;
  4333. int32 id;
  4334. int i;
  4335. data = script_getdata(st, 2);
  4336. if( !data_isreference(data) )
  4337. {
  4338. ShowError("script:getelementofarray: not a variable\n");
  4339. script_reportdata(data);
  4340. script_pushnil(st);
  4341. st->state = END;
  4342. return 1;// not a variable
  4343. }
  4344. id = reference_getid(data);
  4345. name = reference_getname(data);
  4346. if( not_array_variable(*name) )
  4347. {
  4348. ShowError("script:getelementofarray: illegal scope\n");
  4349. script_reportdata(data);
  4350. script_pushnil(st);
  4351. st->state = END;
  4352. return 1;// not supported
  4353. }
  4354. i = script_getnum(st, 3);
  4355. if( i < 0 || i >= 128 )
  4356. {
  4357. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4358. script_reportdata(data);
  4359. script_pushnil(st);
  4360. st->state = END;
  4361. return 1;// out of range
  4362. }
  4363. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4364. return 0;
  4365. }
  4366. /////////////////////////////////////////////////////////////////////
  4367. /// ...
  4368. ///
  4369. /*==========================================
  4370. *
  4371. *------------------------------------------*/
  4372. BUILDIN_FUNC(setlook)
  4373. {
  4374. int type,val;
  4375. TBL_PC* sd;
  4376. type=script_getnum(st,2);
  4377. val=script_getnum(st,3);
  4378. sd = script_rid2sd(st);
  4379. if( sd == NULL )
  4380. return 0;
  4381. pc_changelook(sd,type,val);
  4382. return 0;
  4383. }
  4384. BUILDIN_FUNC(changelook)
  4385. { // As setlook but only client side
  4386. int type,val;
  4387. TBL_PC* sd;
  4388. type=script_getnum(st,2);
  4389. val=script_getnum(st,3);
  4390. sd = script_rid2sd(st);
  4391. if( sd == NULL )
  4392. return 0;
  4393. clif_changelook(&sd->bl,type,val);
  4394. return 0;
  4395. }
  4396. /*==========================================
  4397. *
  4398. *------------------------------------------*/
  4399. BUILDIN_FUNC(cutin)
  4400. {
  4401. TBL_PC* sd;
  4402. sd = script_rid2sd(st);
  4403. if( sd == NULL )
  4404. return 0;
  4405. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4406. return 0;
  4407. }
  4408. /*==========================================
  4409. *
  4410. *------------------------------------------*/
  4411. BUILDIN_FUNC(viewpoint)
  4412. {
  4413. int type,x,y,id,color;
  4414. TBL_PC* sd;
  4415. type=script_getnum(st,2);
  4416. x=script_getnum(st,3);
  4417. y=script_getnum(st,4);
  4418. id=script_getnum(st,5);
  4419. color=script_getnum(st,6);
  4420. sd = script_rid2sd(st);
  4421. if( sd == NULL )
  4422. return 0;
  4423. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4424. return 0;
  4425. }
  4426. /*==========================================
  4427. *
  4428. *------------------------------------------*/
  4429. BUILDIN_FUNC(countitem)
  4430. {
  4431. int nameid, i;
  4432. int count = 0;
  4433. struct script_data* data;
  4434. TBL_PC* sd = script_rid2sd(st);
  4435. if (!sd) {
  4436. script_pushint(st,0);
  4437. return 0;
  4438. }
  4439. data = script_getdata(st,2);
  4440. get_val(st,data);
  4441. if( data_isstring(data) ) {
  4442. const char* name = conv_str(st,data);
  4443. struct item_data* item_data;
  4444. if((item_data = itemdb_searchname(name)) != NULL)
  4445. nameid = item_data->nameid;
  4446. else
  4447. nameid = 0;
  4448. } else
  4449. nameid = conv_num(st,data);
  4450. if (nameid < 500) {
  4451. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4452. script_reportsrc(st);
  4453. script_pushint(st,0);
  4454. return 1;
  4455. }
  4456. for(i = 0; i < MAX_INVENTORY; i++)
  4457. if(sd->status.inventory[i].nameid == nameid)
  4458. count += sd->status.inventory[i].amount;
  4459. script_pushint(st,count);
  4460. return 0;
  4461. }
  4462. /*==========================================
  4463. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4464. * returns number of items that meet the conditions
  4465. *------------------------------------------*/
  4466. BUILDIN_FUNC(countitem2)
  4467. {
  4468. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4469. int count = 0;
  4470. int i;
  4471. struct script_data* data;
  4472. TBL_PC* sd = script_rid2sd(st);
  4473. if (!sd) {
  4474. script_pushint(st,0);
  4475. return 0;
  4476. }
  4477. data = script_getdata(st,2);
  4478. get_val(st,data);
  4479. if( data_isstring(data) ) {
  4480. const char* name = conv_str(st,data);
  4481. struct item_data* item_data;
  4482. if((item_data = itemdb_searchname(name)) != NULL)
  4483. nameid = item_data->nameid;
  4484. else
  4485. nameid = 0;
  4486. } else
  4487. nameid = conv_num(st,data);
  4488. iden = script_getnum(st,3);
  4489. ref = script_getnum(st,4);
  4490. attr = script_getnum(st,5);
  4491. c1 = (short)script_getnum(st,6);
  4492. c2 = (short)script_getnum(st,7);
  4493. c3 = (short)script_getnum(st,8);
  4494. c4 = (short)script_getnum(st,9);
  4495. if (nameid < 500) {
  4496. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4497. script_pushint(st,0);
  4498. return 1;
  4499. }
  4500. for(i = 0; i < MAX_INVENTORY; i++)
  4501. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4502. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4503. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4504. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4505. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4506. sd->status.inventory[i].card[3] == c4
  4507. )
  4508. count += sd->status.inventory[i].amount;
  4509. script_pushint(st,count);
  4510. return 0;
  4511. }
  4512. /*==========================================
  4513. * 重量チェック
  4514. *------------------------------------------*/
  4515. BUILDIN_FUNC(checkweight)
  4516. {
  4517. int nameid=0,amount,i;
  4518. unsigned long weight;
  4519. TBL_PC *sd;
  4520. struct script_data *data;
  4521. sd = script_rid2sd(st);
  4522. if( sd == NULL )
  4523. return 0;
  4524. data=script_getdata(st,2);
  4525. get_val(st,data);
  4526. if( data_isstring(data) ){
  4527. const char *name=conv_str(st,data);
  4528. struct item_data *item_data = itemdb_searchname(name);
  4529. if( item_data )
  4530. nameid=item_data->nameid;
  4531. }else
  4532. nameid=conv_num(st,data);
  4533. amount=script_getnum(st,3);
  4534. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4535. script_pushint(st,0);
  4536. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4537. return 1;
  4538. }
  4539. weight = itemdb_weight(nameid)*amount;
  4540. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4541. script_pushint(st,0);
  4542. } else {
  4543. //Check if the inventory ain't full.
  4544. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4545. i = pc_search_inventory(sd,0);
  4546. if (i >= 0) //Empty slot available.
  4547. script_pushint(st,1);
  4548. else //Inventory full
  4549. script_pushint(st,0);
  4550. }
  4551. return 0;
  4552. }
  4553. /*==========================================
  4554. * getitem <item id>,<amount>{,<character ID>};
  4555. * getitem "<item name>",<amount>{,<character ID>};
  4556. *------------------------------------------*/
  4557. BUILDIN_FUNC(getitem)
  4558. {
  4559. int nameid,amount,get_count,i,flag = 0;
  4560. struct item it;
  4561. TBL_PC *sd;
  4562. struct script_data *data;
  4563. data=script_getdata(st,2);
  4564. get_val(st,data);
  4565. if( data_isstring(data) )
  4566. {// "<item name>"
  4567. const char *name=conv_str(st,data);
  4568. struct item_data *item_data = itemdb_searchname(name);
  4569. if( item_data == NULL ){
  4570. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4571. return 1; //No item created.
  4572. }
  4573. nameid=item_data->nameid;
  4574. } else if( data_isint(data) )
  4575. {// <item id>
  4576. nameid=conv_num(st,data);
  4577. //Violet Box, Blue Box, etc - random item pick
  4578. if( nameid < 0 ) {
  4579. nameid=itemdb_searchrandomid(-nameid);
  4580. flag = 1;
  4581. }
  4582. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4583. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4584. return 1; //No item created.
  4585. }
  4586. } else {
  4587. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4588. return 1;
  4589. }
  4590. // <amount>
  4591. if( (amount=script_getnum(st,3)) <= 0)
  4592. return 0; //return if amount <=0, skip the useles iteration
  4593. memset(&it,0,sizeof(it));
  4594. it.nameid=nameid;
  4595. if(!flag)
  4596. it.identify=1;
  4597. else
  4598. it.identify=itemdb_isidentified(nameid);
  4599. if( script_hasdata(st,4) )
  4600. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4601. else
  4602. sd=script_rid2sd(st); // Attached player
  4603. if( sd == NULL ) // no target
  4604. return 0;
  4605. //Check if it's stackable.
  4606. if (!itemdb_isstackable(nameid))
  4607. get_count = 1;
  4608. else
  4609. get_count = amount;
  4610. for (i = 0; i < amount; i += get_count)
  4611. {
  4612. // if not pet egg
  4613. if (!pet_create_egg(sd, nameid))
  4614. {
  4615. if ((flag = pc_additem(sd, &it, get_count)))
  4616. {
  4617. clif_additem(sd, 0, 0, flag);
  4618. if( pc_candrop(sd,&it) )
  4619. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4620. }
  4621. }
  4622. }
  4623. //Logs items, got from (N)PC scripts [Lupus]
  4624. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4625. log_pick_pc(sd, "N", nameid, amount, NULL);
  4626. return 0;
  4627. }
  4628. /*==========================================
  4629. *
  4630. *------------------------------------------*/
  4631. BUILDIN_FUNC(getitem2)
  4632. {
  4633. int nameid,amount,get_count,i,flag = 0;
  4634. int iden,ref,attr,c1,c2,c3,c4;
  4635. struct item_data *item_data;
  4636. struct item item_tmp;
  4637. TBL_PC *sd;
  4638. struct script_data *data;
  4639. if( script_hasdata(st,11) )
  4640. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4641. else
  4642. sd=script_rid2sd(st); // Attached player
  4643. if( sd == NULL ) // no target
  4644. return 0;
  4645. data=script_getdata(st,2);
  4646. get_val(st,data);
  4647. if( data_isstring(data) ){
  4648. const char *name=conv_str(st,data);
  4649. struct item_data *item_data = itemdb_searchname(name);
  4650. if( item_data )
  4651. nameid=item_data->nameid;
  4652. else
  4653. nameid=UNKNOWN_ITEM_ID;
  4654. }else
  4655. nameid=conv_num(st,data);
  4656. amount=script_getnum(st,3);
  4657. iden=script_getnum(st,4);
  4658. ref=script_getnum(st,5);
  4659. attr=script_getnum(st,6);
  4660. c1=script_getnum(st,7);
  4661. c2=script_getnum(st,8);
  4662. c3=script_getnum(st,9);
  4663. c4=script_getnum(st,10);
  4664. if(nameid<0) { // ランダム
  4665. nameid=itemdb_searchrandomid(-nameid);
  4666. flag = 1;
  4667. }
  4668. if(nameid > 0) {
  4669. memset(&item_tmp,0,sizeof(item_tmp));
  4670. item_data=itemdb_exists(nameid);
  4671. if (item_data == NULL)
  4672. return -1;
  4673. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4674. if(ref > 10) ref = 10;
  4675. }
  4676. else if(item_data->type==IT_PETEGG) {
  4677. iden = 1;
  4678. ref = 0;
  4679. }
  4680. else {
  4681. iden = 1;
  4682. ref = attr = 0;
  4683. }
  4684. item_tmp.nameid=nameid;
  4685. if(!flag)
  4686. item_tmp.identify=iden;
  4687. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4688. item_tmp.identify=0;
  4689. item_tmp.refine=ref;
  4690. item_tmp.attribute=attr;
  4691. item_tmp.card[0]=c1;
  4692. item_tmp.card[1]=c2;
  4693. item_tmp.card[2]=c3;
  4694. item_tmp.card[3]=c4;
  4695. //Check if it's stackable.
  4696. if (!itemdb_isstackable(nameid))
  4697. get_count = 1;
  4698. else
  4699. get_count = amount;
  4700. for (i = 0; i < amount; i += get_count)
  4701. {
  4702. // if not pet egg
  4703. if (!pet_create_egg(sd, nameid))
  4704. {
  4705. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  4706. {
  4707. clif_additem(sd, 0, 0, flag);
  4708. if( pc_candrop(sd,&item_tmp) )
  4709. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4710. }
  4711. }
  4712. }
  4713. //Logs items, got from (N)PC scripts [Lupus]
  4714. if(log_config.enable_logs&0x40)
  4715. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4716. }
  4717. return 0;
  4718. }
  4719. /*==========================================
  4720. * gets an item with someone's name inscribed [Skotlex]
  4721. * getinscribeditem item_num, character_name
  4722. * Returned Qty is always 1, only works on equip-able
  4723. * equipment
  4724. *------------------------------------------*/
  4725. BUILDIN_FUNC(getnameditem)
  4726. {
  4727. int nameid;
  4728. struct item item_tmp;
  4729. TBL_PC *sd, *tsd;
  4730. struct script_data *data;
  4731. sd = script_rid2sd(st);
  4732. if (sd == NULL)
  4733. { //Player not attached!
  4734. script_pushint(st,0);
  4735. return 0;
  4736. }
  4737. data=script_getdata(st,2);
  4738. get_val(st,data);
  4739. if( data_isstring(data) ){
  4740. const char *name=conv_str(st,data);
  4741. struct item_data *item_data = itemdb_searchname(name);
  4742. if( item_data == NULL)
  4743. { //Failed
  4744. script_pushint(st,0);
  4745. return 0;
  4746. }
  4747. nameid = item_data->nameid;
  4748. }else
  4749. nameid = conv_num(st,data);
  4750. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  4751. { //Even though named stackable items "could" be risky, they are required for certain quests.
  4752. script_pushint(st,0);
  4753. return 0;
  4754. }
  4755. data=script_getdata(st,3);
  4756. get_val(st,data);
  4757. if( data_isstring(data) ) //Char Name
  4758. tsd=map_nick2sd(conv_str(st,data));
  4759. else //Char Id was given
  4760. tsd=map_charid2sd(conv_num(st,data));
  4761. if( tsd == NULL )
  4762. { //Failed
  4763. script_pushint(st,0);
  4764. return 0;
  4765. }
  4766. memset(&item_tmp,0,sizeof(item_tmp));
  4767. item_tmp.nameid=nameid;
  4768. item_tmp.amount=1;
  4769. item_tmp.identify=1;
  4770. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  4771. item_tmp.card[2]=tsd->status.char_id;
  4772. item_tmp.card[3]=tsd->status.char_id >> 16;
  4773. if(pc_additem(sd,&item_tmp,1)) {
  4774. script_pushint(st,0);
  4775. return 0; //Failed to add item, we will not drop if they don't fit
  4776. }
  4777. //Logs items, got from (N)PC scripts [Lupus]
  4778. if(log_config.enable_logs&0x40)
  4779. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  4780. script_pushint(st,1);
  4781. return 0;
  4782. }
  4783. /*==========================================
  4784. * gets a random item ID from an item group [Skotlex]
  4785. * groupranditem group_num
  4786. *------------------------------------------*/
  4787. BUILDIN_FUNC(grouprandomitem)
  4788. {
  4789. int group;
  4790. group = script_getnum(st,2);
  4791. script_pushint(st,itemdb_searchrandomid(group));
  4792. return 0;
  4793. }
  4794. /*==========================================
  4795. *
  4796. *------------------------------------------*/
  4797. BUILDIN_FUNC(makeitem)
  4798. {
  4799. int nameid,amount,flag = 0;
  4800. int x,y,m;
  4801. const char *mapname;
  4802. struct item item_tmp;
  4803. struct script_data *data;
  4804. data=script_getdata(st,2);
  4805. get_val(st,data);
  4806. if( data_isstring(data) ){
  4807. const char *name=conv_str(st,data);
  4808. struct item_data *item_data = itemdb_searchname(name);
  4809. if( item_data )
  4810. nameid=item_data->nameid;
  4811. else
  4812. nameid=UNKNOWN_ITEM_ID;
  4813. }else
  4814. nameid=conv_num(st,data);
  4815. amount=script_getnum(st,3);
  4816. mapname =script_getstr(st,4);
  4817. x =script_getnum(st,5);
  4818. y =script_getnum(st,6);
  4819. if(strcmp(mapname,"this")==0)
  4820. {
  4821. TBL_PC *sd;
  4822. sd = script_rid2sd(st);
  4823. if (!sd) return 0; //Failed...
  4824. m=sd->bl.m;
  4825. } else
  4826. m=map_mapname2mapid(mapname);
  4827. if(nameid<0) { // ランダム
  4828. nameid=itemdb_searchrandomid(-nameid);
  4829. flag = 1;
  4830. }
  4831. if(nameid > 0) {
  4832. memset(&item_tmp,0,sizeof(item_tmp));
  4833. item_tmp.nameid=nameid;
  4834. if(!flag)
  4835. item_tmp.identify=1;
  4836. else
  4837. item_tmp.identify=itemdb_isidentified(nameid);
  4838. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  4839. }
  4840. return 0;
  4841. }
  4842. /*==========================================
  4843. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  4844. *------------------------------------------*/
  4845. BUILDIN_FUNC(delitem)
  4846. {
  4847. int nameid=0,amount,i,important_item=0;
  4848. TBL_PC *sd;
  4849. struct script_data *data;
  4850. if( script_hasdata(st,4) )
  4851. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4852. else
  4853. sd=script_rid2sd(st); // Attached player
  4854. if( sd == NULL ) // no target
  4855. return 0;
  4856. data=script_getdata(st,2);
  4857. get_val(st,data);
  4858. if( data_isstring(data) ){
  4859. const char *name=conv_str(st,data);
  4860. struct item_data *item_data = itemdb_searchname(name);
  4861. //nameid=UNKNOWN_ITEM_ID;
  4862. if( item_data )
  4863. nameid=item_data->nameid;
  4864. }else
  4865. nameid=conv_num(st,data);
  4866. amount=script_getnum(st,3);
  4867. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  4868. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  4869. return 0;
  4870. }
  4871. //1st pass
  4872. //here we won't delete items with CARDS, named items but we count them
  4873. for(i=0;i<MAX_INVENTORY;i++){
  4874. //we don't delete wrong item or equipped item
  4875. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  4876. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  4877. continue;
  4878. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  4879. if(sd->inventory_data[i]->type==IT_PETEGG &&
  4880. sd->status.inventory[i].card[0] == CARD0_PET)
  4881. {
  4882. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  4883. continue; //pet couldn't be sent for deletion.
  4884. } else
  4885. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  4886. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  4887. sd->status.inventory[i].card[0] ||
  4888. sd->status.inventory[i].refine) {
  4889. //this is important item, count it (except for pet eggs)
  4890. if(sd->status.inventory[i].card[0] != CARD0_PET)
  4891. important_item++;
  4892. continue;
  4893. }
  4894. if(sd->status.inventory[i].amount>=amount){
  4895. //Logs items, got from (N)PC scripts [Lupus]
  4896. if(log_config.enable_logs&0x40)
  4897. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  4898. pc_delitem(sd,i,amount,0);
  4899. return 0; //we deleted exact amount of items. now exit
  4900. } else {
  4901. amount-=sd->status.inventory[i].amount;
  4902. //Logs items, got from (N)PC scripts [Lupus]
  4903. if(log_config.enable_logs&0x40) {
  4904. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  4905. }
  4906. //Logs
  4907. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  4908. }
  4909. }
  4910. //2nd pass
  4911. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  4912. if (important_item>0 && amount>0)
  4913. for(i=0;i<MAX_INVENTORY;i++){
  4914. //we don't delete wrong item
  4915. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  4916. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  4917. continue;
  4918. if(sd->status.inventory[i].amount>=amount){
  4919. //Logs items, got from (N)PC scripts [Lupus]
  4920. if(log_config.enable_logs&0x40)
  4921. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  4922. pc_delitem(sd,i,amount,0);
  4923. return 0; //we deleted exact amount of items. now exit
  4924. } else {
  4925. amount-=sd->status.inventory[i].amount;
  4926. //Logs items, got from (N)PC scripts [Lupus]
  4927. if(log_config.enable_logs&0x40)
  4928. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  4929. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  4930. }
  4931. }
  4932. return 0;
  4933. }
  4934. /*==========================================
  4935. * advanced version of delitem [modified by Mihilion]
  4936. *------------------------------------------*/
  4937. BUILDIN_FUNC(delitem2)
  4938. {
  4939. int nameid=0,amount,i=0;
  4940. int iden,ref,attr,c1,c2,c3,c4;
  4941. TBL_PC *sd;
  4942. struct script_data *data;
  4943. if( script_hasdata(st,11) )
  4944. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4945. else
  4946. sd=script_rid2sd(st); // Attached player
  4947. if( sd == NULL ) // no target
  4948. return 0;
  4949. data=script_getdata(st,2);
  4950. get_val(st,data);
  4951. if( data_isstring(data) ){
  4952. const char *name=conv_str(st,data);
  4953. struct item_data *item_data = itemdb_searchname(name);
  4954. //nameid=UNKNOWN_ITEM_ID;
  4955. if( item_data )
  4956. nameid=item_data->nameid;
  4957. }else
  4958. nameid=conv_num(st,data);
  4959. amount=script_getnum(st,3);
  4960. iden=script_getnum(st,4);
  4961. ref=script_getnum(st,5);
  4962. attr=script_getnum(st,6);
  4963. c1=script_getnum(st,7);
  4964. c2=script_getnum(st,8);
  4965. c3=script_getnum(st,9);
  4966. c4=script_getnum(st,10);
  4967. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  4968. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  4969. return 0;
  4970. }
  4971. for(i=0;i<MAX_INVENTORY;i++){
  4972. //we don't delete wrong item or equipped item
  4973. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  4974. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  4975. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  4976. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  4977. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  4978. sd->status.inventory[i].card[3]!=c4)
  4979. continue;
  4980. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  4981. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  4982. {
  4983. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  4984. continue; //Failed to send delete the pet.
  4985. }
  4986. if(sd->status.inventory[i].amount>=amount){
  4987. //Logs items, got from (N)PC scripts [Lupus]
  4988. if(log_config.enable_logs&0x40)
  4989. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  4990. pc_delitem(sd,i,amount,0);
  4991. return 0; //we deleted exact amount of items. now exit
  4992. } else {
  4993. amount-=sd->status.inventory[i].amount;
  4994. //Logs items, got from (N)PC scripts [Lupus]
  4995. if(log_config.enable_logs&0x40)
  4996. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  4997. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  4998. }
  4999. }
  5000. return 0;
  5001. }
  5002. /*==========================================
  5003. * Enables/Disables use of items while in an NPC [Skotlex]
  5004. *------------------------------------------*/
  5005. BUILDIN_FUNC(enableitemuse)
  5006. {
  5007. TBL_PC *sd;
  5008. sd=script_rid2sd(st);
  5009. if (sd)
  5010. sd->npc_item_flag = st->oid;
  5011. return 0;
  5012. }
  5013. BUILDIN_FUNC(disableitemuse)
  5014. {
  5015. TBL_PC *sd;
  5016. sd=script_rid2sd(st);
  5017. if (sd)
  5018. sd->npc_item_flag = 0;
  5019. return 0;
  5020. }
  5021. /*==========================================
  5022. *キャラ関係のパラメータ取得
  5023. *------------------------------------------*/
  5024. BUILDIN_FUNC(readparam)
  5025. {
  5026. int type;
  5027. TBL_PC *sd;
  5028. type=script_getnum(st,2);
  5029. if( script_hasdata(st,3) )
  5030. sd=map_nick2sd(script_getstr(st,3));
  5031. else
  5032. sd=script_rid2sd(st);
  5033. if(sd==NULL){
  5034. script_pushint(st,-1);
  5035. return 0;
  5036. }
  5037. script_pushint(st,pc_readparam(sd,type));
  5038. return 0;
  5039. }
  5040. /*==========================================
  5041. *キャラ関係のID取得
  5042. *------------------------------------------*/
  5043. BUILDIN_FUNC(getcharid)
  5044. {
  5045. int num;
  5046. TBL_PC *sd;
  5047. num = script_getnum(st,2);
  5048. if( script_hasdata(st,3) )
  5049. sd=map_nick2sd(script_getstr(st,3));
  5050. else
  5051. sd=script_rid2sd(st);
  5052. if(sd==NULL){
  5053. script_pushint(st,0); //return 0, according docs
  5054. return 0;
  5055. }
  5056. switch( num ) {
  5057. case 0: script_pushint(st,sd->status.char_id); break;
  5058. case 1: script_pushint(st,sd->status.party_id); break;
  5059. case 2: script_pushint(st,sd->status.guild_id); break;
  5060. case 3: script_pushint(st,sd->status.account_id); break;
  5061. default:
  5062. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5063. script_pushint(st,0);
  5064. break;
  5065. }
  5066. return 0;
  5067. }
  5068. /*==========================================
  5069. *指定IDのPT名取得
  5070. *------------------------------------------*/
  5071. char *buildin_getpartyname_sub(int party_id)
  5072. {
  5073. struct party_data *p;
  5074. p=party_search(party_id);
  5075. if(p!=NULL){
  5076. char *buf;
  5077. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5078. memcpy(buf, p->party.name, NAME_LENGTH);
  5079. buf[NAME_LENGTH-1] = '\0';
  5080. return buf;
  5081. }
  5082. return 0;
  5083. }
  5084. BUILDIN_FUNC(getpartyname)
  5085. {
  5086. char *name;
  5087. int party_id;
  5088. party_id=script_getnum(st,2);
  5089. name=buildin_getpartyname_sub(party_id);
  5090. if(name != NULL)
  5091. script_pushstr(st,name);
  5092. else
  5093. script_pushconststr(st,"null");
  5094. return 0;
  5095. }
  5096. /*==========================================
  5097. *指定IDのPT人数とメンバーID取得
  5098. *------------------------------------------*/
  5099. BUILDIN_FUNC(getpartymember)
  5100. {
  5101. struct party_data *p;
  5102. int i,j=0,type=0;
  5103. p=party_search(script_getnum(st,2));
  5104. if( script_hasdata(st,3) )
  5105. type=script_getnum(st,3);
  5106. if(p!=NULL){
  5107. for(i=0;i<MAX_PARTY;i++){
  5108. if(p->party.member[i].account_id){
  5109. switch (type) {
  5110. case 2:
  5111. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5112. break;
  5113. case 1:
  5114. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5115. break;
  5116. default:
  5117. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5118. }
  5119. j++;
  5120. }
  5121. }
  5122. }
  5123. mapreg_setreg(add_str("$@partymembercount"),j);
  5124. return 0;
  5125. }
  5126. /*==========================================
  5127. * Retrieves party leader. if flag is specified,
  5128. * return some of the leader data. Otherwise, return name.
  5129. *------------------------------------------*/
  5130. BUILDIN_FUNC(getpartyleader)
  5131. {
  5132. int party_id, type = 0, i=0;
  5133. struct party_data *p;
  5134. party_id=script_getnum(st,2);
  5135. if( script_hasdata(st,3) )
  5136. type=script_getnum(st,3);
  5137. p=party_search(party_id);
  5138. if (p) //Search leader
  5139. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5140. if (!p || i == MAX_PARTY) { //leader not found
  5141. if (type)
  5142. script_pushint(st,-1);
  5143. else
  5144. script_pushconststr(st,"null");
  5145. return 0;
  5146. }
  5147. switch (type) {
  5148. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5149. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5150. case 3: script_pushint(st,p->party.member[i].class_); break;
  5151. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5152. case 5: script_pushint(st,p->party.member[i].lv); break;
  5153. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5154. }
  5155. return 0;
  5156. }
  5157. /*==========================================
  5158. *指定IDのギルド名取得
  5159. *------------------------------------------*/
  5160. char *buildin_getguildname_sub(int guild_id)
  5161. {
  5162. struct guild *g=NULL;
  5163. g=guild_search(guild_id);
  5164. if(g!=NULL){
  5165. char *buf;
  5166. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5167. memcpy(buf, g->name, NAME_LENGTH);
  5168. buf[NAME_LENGTH-1] = '\0';
  5169. return buf;
  5170. }
  5171. return NULL;
  5172. }
  5173. BUILDIN_FUNC(getguildname)
  5174. {
  5175. char *name;
  5176. int guild_id=script_getnum(st,2);
  5177. name=buildin_getguildname_sub(guild_id);
  5178. if(name != NULL)
  5179. script_pushstr(st,name);
  5180. else
  5181. script_pushconststr(st,"null");
  5182. return 0;
  5183. }
  5184. /*==========================================
  5185. *指定IDのGuildMaster名取得
  5186. *------------------------------------------*/
  5187. char *buildin_getguildmaster_sub(int guild_id)
  5188. {
  5189. struct guild *g=NULL;
  5190. g=guild_search(guild_id);
  5191. if(g!=NULL){
  5192. char *buf;
  5193. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5194. memcpy(buf, g->master, NAME_LENGTH);
  5195. buf[NAME_LENGTH-1] = '\0';
  5196. return buf;
  5197. }
  5198. return 0;
  5199. }
  5200. BUILDIN_FUNC(getguildmaster)
  5201. {
  5202. char *master;
  5203. int guild_id=script_getnum(st,2);
  5204. master=buildin_getguildmaster_sub(guild_id);
  5205. if(master!=0)
  5206. script_pushstr(st,master);
  5207. else
  5208. script_pushconststr(st,"null");
  5209. return 0;
  5210. }
  5211. BUILDIN_FUNC(getguildmasterid)
  5212. {
  5213. char *master;
  5214. TBL_PC *sd=NULL;
  5215. int guild_id=script_getnum(st,2);
  5216. master=buildin_getguildmaster_sub(guild_id);
  5217. if(master!=0){
  5218. if((sd=map_nick2sd(master)) == NULL){
  5219. script_pushint(st,0);
  5220. return 0;
  5221. }
  5222. script_pushint(st,sd->status.char_id);
  5223. }else{
  5224. script_pushint(st,0);
  5225. }
  5226. return 0;
  5227. }
  5228. /*==========================================
  5229. * キャラクタの名前
  5230. *------------------------------------------*/
  5231. BUILDIN_FUNC(strcharinfo)
  5232. {
  5233. TBL_PC *sd;
  5234. int num;
  5235. char *buf;
  5236. sd=script_rid2sd(st);
  5237. if (!sd) { //Avoid crashing....
  5238. script_pushconststr(st,"");
  5239. return 0;
  5240. }
  5241. num=script_getnum(st,2);
  5242. switch(num){
  5243. case 0:
  5244. script_pushstrcopy(st,sd->status.name);
  5245. break;
  5246. case 1:
  5247. buf=buildin_getpartyname_sub(sd->status.party_id);
  5248. if(buf!=0)
  5249. script_pushstr(st,buf);
  5250. else
  5251. script_pushconststr(st,"");
  5252. break;
  5253. case 2:
  5254. buf=buildin_getguildname_sub(sd->status.guild_id);
  5255. if(buf != NULL)
  5256. script_pushstr(st,buf);
  5257. else
  5258. script_pushconststr(st,"");
  5259. break;
  5260. default:
  5261. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5262. script_pushconststr(st,"");
  5263. break;
  5264. }
  5265. return 0;
  5266. }
  5267. /*==========================================
  5268. * 呼び出し元のNPC情報を取得する
  5269. *------------------------------------------*/
  5270. BUILDIN_FUNC(strnpcinfo)
  5271. {
  5272. TBL_NPC* nd;
  5273. int num;
  5274. char *buf,*name=NULL;
  5275. nd = map_id2nd(st->oid);
  5276. if (!nd) {
  5277. script_pushconststr(st, "");
  5278. return 0;
  5279. }
  5280. num = script_getnum(st,2);
  5281. switch(num){
  5282. case 0: // display name
  5283. name = aStrdup(nd->name);
  5284. break;
  5285. case 1: // visible part of display name name
  5286. if((buf = strchr(nd->name,'#')) != NULL)
  5287. {
  5288. name = aStrdup(nd->name);
  5289. name[buf - nd->name] = 0;
  5290. }
  5291. break;
  5292. case 2: // # fragment
  5293. if((buf = strchr(nd->name,'#')) != NULL)
  5294. name = aStrdup(buf+1);
  5295. break;
  5296. case 3: // unique name
  5297. name = aStrdup(nd->exname);
  5298. break;
  5299. }
  5300. if(name)
  5301. script_pushstr(st, name);
  5302. else
  5303. script_pushconststr(st, "");
  5304. return 0;
  5305. }
  5306. unsigned int equip[10]={EQP_HEAD_TOP,EQP_ARMOR,EQP_HAND_L,EQP_HAND_R,EQP_GARMENT,EQP_SHOES,EQP_ACC_L,EQP_ACC_R,EQP_HEAD_MID,EQP_HEAD_LOW};
  5307. /*==========================================
  5308. * GetEquipID(Pos); Pos: 1-10
  5309. *------------------------------------------*/
  5310. BUILDIN_FUNC(getequipid)
  5311. {
  5312. int i=-1,num;
  5313. TBL_PC *sd;
  5314. struct item_data* item;
  5315. sd=script_rid2sd(st);
  5316. if(sd == NULL)
  5317. return 0;
  5318. num=script_getnum(st,2);
  5319. if (num > 0 && num <= ARRAYLENGTH(equip))
  5320. i=pc_checkequip(sd,equip[num-1]);
  5321. if(i >= 0){
  5322. item=sd->inventory_data[i];
  5323. if(item)
  5324. script_pushint(st,item->nameid);
  5325. else
  5326. script_pushint(st,0);
  5327. }else{
  5328. script_pushint(st,-1);
  5329. }
  5330. return 0;
  5331. }
  5332. /*==========================================
  5333. * 装備名文字列(精錬メニュー用)
  5334. *------------------------------------------*/
  5335. BUILDIN_FUNC(getequipname)
  5336. {
  5337. int i=-1,num;
  5338. TBL_PC *sd;
  5339. struct item_data* item;
  5340. char *buf;
  5341. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  5342. sd = script_rid2sd(st);
  5343. if( sd == NULL )
  5344. return 0;
  5345. buf=(char *)aMallocA(64*sizeof(char));
  5346. num=script_getnum(st,2);
  5347. if (num > 0 && num <= ARRAYLENGTH(equip))
  5348. i=pc_checkequip(sd,equip[num-1]);
  5349. if(i >= 0){
  5350. item=sd->inventory_data[i];
  5351. if(item)
  5352. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5353. else
  5354. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5355. }else{
  5356. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5357. }
  5358. script_pushstr(st,buf);
  5359. return 0;
  5360. }
  5361. /*==========================================
  5362. * getbrokenid [Valaris]
  5363. *------------------------------------------*/
  5364. BUILDIN_FUNC(getbrokenid)
  5365. {
  5366. int i,num,id=0,brokencounter=0;
  5367. TBL_PC *sd;
  5368. sd = script_rid2sd(st);
  5369. if( sd == NULL )
  5370. return 0;
  5371. num=script_getnum(st,2);
  5372. for(i=0; i<MAX_INVENTORY; i++) {
  5373. if(sd->status.inventory[i].attribute==1){
  5374. brokencounter++;
  5375. if(num==brokencounter){
  5376. id=sd->status.inventory[i].nameid;
  5377. break;
  5378. }
  5379. }
  5380. }
  5381. script_pushint(st,id);
  5382. return 0;
  5383. }
  5384. /*==========================================
  5385. * repair [Valaris]
  5386. *------------------------------------------*/
  5387. BUILDIN_FUNC(repair)
  5388. {
  5389. int i,num;
  5390. int repaircounter=0;
  5391. TBL_PC *sd;
  5392. sd = script_rid2sd(st);
  5393. if( sd == NULL )
  5394. return 0;
  5395. num=script_getnum(st,2);
  5396. for(i=0; i<MAX_INVENTORY; i++) {
  5397. if(sd->status.inventory[i].attribute==1){
  5398. repaircounter++;
  5399. if(num==repaircounter){
  5400. sd->status.inventory[i].attribute=0;
  5401. clif_equiplist(sd);
  5402. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5403. clif_misceffect(&sd->bl, 3);
  5404. break;
  5405. }
  5406. }
  5407. }
  5408. return 0;
  5409. }
  5410. /*==========================================
  5411. * 装備チェック
  5412. *------------------------------------------*/
  5413. BUILDIN_FUNC(getequipisequiped)
  5414. {
  5415. int i=-1,num;
  5416. TBL_PC *sd;
  5417. num=script_getnum(st,2);
  5418. sd = script_rid2sd(st);
  5419. if( sd == NULL )
  5420. return 0;
  5421. if (num > 0 && num <= ARRAYLENGTH(equip))
  5422. i=pc_checkequip(sd,equip[num-1]);
  5423. if(i >= 0)
  5424. script_pushint(st,1);
  5425. else
  5426. script_pushint(st,0);
  5427. return 0;
  5428. }
  5429. /*==========================================
  5430. * 装備品精錬可能チェック
  5431. *------------------------------------------*/
  5432. BUILDIN_FUNC(getequipisenableref)
  5433. {
  5434. int i=-1,num;
  5435. TBL_PC *sd;
  5436. num=script_getnum(st,2);
  5437. sd = script_rid2sd(st);
  5438. if( sd == NULL )
  5439. return 0;
  5440. if (num > 0 && num <= ARRAYLENGTH(equip))
  5441. i=pc_checkequip(sd,equip[num-1]);
  5442. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5443. {
  5444. script_pushint(st,1);
  5445. } else {
  5446. script_pushint(st,0);
  5447. }
  5448. return 0;
  5449. }
  5450. /*==========================================
  5451. * 装備品鑑定チェック
  5452. *------------------------------------------*/
  5453. BUILDIN_FUNC(getequipisidentify)
  5454. {
  5455. int i=-1,num;
  5456. TBL_PC *sd;
  5457. num=script_getnum(st,2);
  5458. sd = script_rid2sd(st);
  5459. if( sd == NULL )
  5460. return 0;
  5461. if (num > 0 && num <= ARRAYLENGTH(equip))
  5462. i=pc_checkequip(sd,equip[num-1]);
  5463. if(i >= 0)
  5464. script_pushint(st,sd->status.inventory[i].identify);
  5465. else
  5466. script_pushint(st,0);
  5467. return 0;
  5468. }
  5469. /*==========================================
  5470. * 装備品精錬度
  5471. *------------------------------------------*/
  5472. BUILDIN_FUNC(getequiprefinerycnt)
  5473. {
  5474. int i=-1,num;
  5475. TBL_PC *sd;
  5476. num=script_getnum(st,2);
  5477. sd = script_rid2sd(st);
  5478. if( sd == NULL )
  5479. return 0;
  5480. if (num > 0 && num <= ARRAYLENGTH(equip))
  5481. i=pc_checkequip(sd,equip[num-1]);
  5482. if(i >= 0)
  5483. script_pushint(st,sd->status.inventory[i].refine);
  5484. else
  5485. script_pushint(st,0);
  5486. return 0;
  5487. }
  5488. /*==========================================
  5489. * 装備品武器LV
  5490. *------------------------------------------*/
  5491. BUILDIN_FUNC(getequipweaponlv)
  5492. {
  5493. int i=-1,num;
  5494. TBL_PC *sd;
  5495. num=script_getnum(st,2);
  5496. sd = script_rid2sd(st);
  5497. if( sd == NULL )
  5498. return 0;
  5499. if (num > 0 && num <= ARRAYLENGTH(equip))
  5500. i=pc_checkequip(sd,equip[num-1]);
  5501. if(i >= 0 && sd->inventory_data[i])
  5502. script_pushint(st,sd->inventory_data[i]->wlv);
  5503. else
  5504. script_pushint(st,0);
  5505. return 0;
  5506. }
  5507. /*==========================================
  5508. * 装備品精錬成功率
  5509. *------------------------------------------*/
  5510. BUILDIN_FUNC(getequippercentrefinery)
  5511. {
  5512. int i=-1,num;
  5513. TBL_PC *sd;
  5514. num=script_getnum(st,2);
  5515. sd = script_rid2sd(st);
  5516. if( sd == NULL )
  5517. return 0;
  5518. if (num > 0 && num <= ARRAYLENGTH(equip))
  5519. i=pc_checkequip(sd,equip[num-1]);
  5520. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5521. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5522. else
  5523. script_pushint(st,0);
  5524. return 0;
  5525. }
  5526. /*==========================================
  5527. * 精錬成功
  5528. *------------------------------------------*/
  5529. BUILDIN_FUNC(successrefitem)
  5530. {
  5531. int i=-1,num,ep;
  5532. TBL_PC *sd;
  5533. num=script_getnum(st,2);
  5534. sd = script_rid2sd(st);
  5535. if( sd == NULL )
  5536. return 0;
  5537. if (num > 0 && num <= ARRAYLENGTH(equip))
  5538. i=pc_checkequip(sd,equip[num-1]);
  5539. if(i >= 0) {
  5540. ep=sd->status.inventory[i].equip;
  5541. //Logs items, got from (N)PC scripts [Lupus]
  5542. if(log_config.enable_logs&0x40)
  5543. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5544. sd->status.inventory[i].refine++;
  5545. pc_unequipitem(sd,i,2);
  5546. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5547. clif_delitem(sd,i,1);
  5548. //Logs items, got from (N)PC scripts [Lupus]
  5549. if(log_config.enable_logs&0x40)
  5550. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5551. clif_additem(sd,i,1,0);
  5552. pc_equipitem(sd,i,ep);
  5553. clif_misceffect(&sd->bl,3);
  5554. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5555. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5556. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5557. ){ // Fame point system [DracoRPG]
  5558. switch (sd->inventory_data[i]->wlv){
  5559. case 1:
  5560. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5561. break;
  5562. case 2:
  5563. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5564. break;
  5565. case 3:
  5566. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5567. break;
  5568. }
  5569. }
  5570. }
  5571. return 0;
  5572. }
  5573. /*==========================================
  5574. * 精錬失敗
  5575. *------------------------------------------*/
  5576. BUILDIN_FUNC(failedrefitem)
  5577. {
  5578. int i=-1,num;
  5579. TBL_PC *sd;
  5580. num=script_getnum(st,2);
  5581. sd = script_rid2sd(st);
  5582. if( sd == NULL )
  5583. return 0;
  5584. if (num > 0 && num <= ARRAYLENGTH(equip))
  5585. i=pc_checkequip(sd,equip[num-1]);
  5586. if(i >= 0) {
  5587. //Logs items, got from (N)PC scripts [Lupus]
  5588. if(log_config.enable_logs&0x40)
  5589. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5590. sd->status.inventory[i].refine = 0;
  5591. pc_unequipitem(sd,i,3);
  5592. // 精錬失敗エフェクトのパケット
  5593. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5594. pc_delitem(sd,i,1,0);
  5595. // 他の人にも失敗を通知
  5596. clif_misceffect(&sd->bl,2);
  5597. }
  5598. return 0;
  5599. }
  5600. /*==========================================
  5601. *
  5602. *------------------------------------------*/
  5603. BUILDIN_FUNC(statusup)
  5604. {
  5605. int type;
  5606. TBL_PC *sd;
  5607. type=script_getnum(st,2);
  5608. sd = script_rid2sd(st);
  5609. if( sd == NULL )
  5610. return 0;
  5611. pc_statusup(sd,type);
  5612. return 0;
  5613. }
  5614. /*==========================================
  5615. *
  5616. *------------------------------------------*/
  5617. BUILDIN_FUNC(statusup2)
  5618. {
  5619. int type,val;
  5620. TBL_PC *sd;
  5621. type=script_getnum(st,2);
  5622. val=script_getnum(st,3);
  5623. sd = script_rid2sd(st);
  5624. if( sd == NULL )
  5625. return 0;
  5626. pc_statusup2(sd,type,val);
  5627. return 0;
  5628. }
  5629. /// See 'doc/item_bonus.txt'
  5630. ///
  5631. /// bonus <bonus type>,<val1>;
  5632. /// bonus2 <bonus type>,<val1>,<val2>;
  5633. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5634. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5635. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5636. BUILDIN_FUNC(bonus)
  5637. {
  5638. int type;
  5639. int val1;
  5640. int val2 = 0;
  5641. int val3 = 0;
  5642. int val4 = 0;
  5643. int val5 = 0;
  5644. TBL_PC* sd;
  5645. sd = script_rid2sd(st);
  5646. if( sd == NULL )
  5647. return 0; // no player attached
  5648. type = script_getnum(st,2);
  5649. switch( type )
  5650. {
  5651. case SP_AUTOSPELL:
  5652. case SP_AUTOSPELL_WHENHIT:
  5653. case SP_SKILL_ATK:
  5654. case SP_SKILL_HEAL:
  5655. case SP_ADD_SKILL_BLOW:
  5656. case SP_CASTRATE:
  5657. // these bonuses support skill names
  5658. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  5659. break;
  5660. default:
  5661. val1 = script_getnum(st,3);
  5662. break;
  5663. }
  5664. switch( script_lastdata(st)-2 )
  5665. {
  5666. case 1:
  5667. pc_bonus(sd, type, val1);
  5668. break;
  5669. case 2:
  5670. val2 = script_getnum(st,4);
  5671. pc_bonus2(sd, type, val1, val2);
  5672. break;
  5673. case 3:
  5674. val2 = script_getnum(st,4);
  5675. val3 = script_getnum(st,5);
  5676. pc_bonus3(sd, type, val1, val2, val3);
  5677. break;
  5678. case 4:
  5679. val2 = script_getnum(st,4);
  5680. val3 = script_getnum(st,5);
  5681. val4 = script_getnum(st,6);
  5682. pc_bonus4(sd, type, val1, val2, val3, val4);
  5683. break;
  5684. case 5:
  5685. val2 = script_getnum(st,4);
  5686. val3 = script_getnum(st,5);
  5687. val4 = script_getnum(st,6);
  5688. val5 = script_getnum(st,7);
  5689. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  5690. break;
  5691. default:
  5692. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  5693. break;
  5694. }
  5695. return 0;
  5696. }
  5697. /// Bonus script that has a chance of being executed on attack.
  5698. BUILDIN_FUNC(bonusautoscript)
  5699. {
  5700. int rate, flag = 0;
  5701. const char *str;
  5702. struct script_code *script;
  5703. TBL_PC* sd;
  5704. sd = script_rid2sd(st);
  5705. if( sd == NULL )
  5706. return 0;// no player attached, report source
  5707. str = script_getstr(st,2);
  5708. rate = script_getnum(st,3);
  5709. if( script_hasdata(st,4) )
  5710. flag = script_getnum(st,4);
  5711. script = parse_script(str, "autoscript bonus", 0, 0);
  5712. if (!script)
  5713. return 1;
  5714. if (!pc_autoscript_add(sd->autoscript, ARRAYLENGTH(sd->autoscript), rate, flag, script))
  5715. {
  5716. script_free_code(script);
  5717. return 1;
  5718. }
  5719. return 0;
  5720. }
  5721. /// Bonus script that has a chance of being executed when attacked.
  5722. BUILDIN_FUNC(bonusautoscript2)
  5723. {
  5724. int rate, flag = 0;
  5725. const char *str;
  5726. struct script_code *script;
  5727. TBL_PC* sd;
  5728. sd = script_rid2sd(st);
  5729. if( sd == NULL )
  5730. return 0;// no player attached, report source
  5731. str = script_getstr(st,2);
  5732. rate = script_getnum(st,3);
  5733. if( script_hasdata(st,4) )
  5734. flag = script_getnum(st,4);
  5735. script = parse_script(str, "autoscript2 bonus", 0, 0);
  5736. if (!script)
  5737. return 1;
  5738. if (!pc_autoscript_add(sd->autoscript2, ARRAYLENGTH(sd->autoscript2), rate, flag, script))
  5739. {
  5740. script_free_code(script);
  5741. return 1;
  5742. }
  5743. return 0;
  5744. }
  5745. /// Changes the level of a player skill.
  5746. /// <flag> defaults to 1
  5747. /// <flag>=0 : set the level of the skill
  5748. /// <flag>=1 : set the temporary level of the skill
  5749. /// <flag>=2 : add to the level of the skill
  5750. ///
  5751. /// skill <skill id>,<level>,<flag>
  5752. /// skill <skill id>,<level>
  5753. /// skill "<skill name>",<level>,<flag>
  5754. /// skill "<skill name>",<level>
  5755. BUILDIN_FUNC(skill)
  5756. {
  5757. int id;
  5758. int level;
  5759. int flag = 1;
  5760. TBL_PC* sd;
  5761. sd = script_rid2sd(st);
  5762. if( sd == NULL )
  5763. return 0;// no player attached, report source
  5764. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5765. level = script_getnum(st,3);
  5766. if( script_hasdata(st,4) )
  5767. flag = script_getnum(st,4);
  5768. pc_skill(sd, id, level, flag);
  5769. return 0;
  5770. }
  5771. /// Changes the level of a player skill.
  5772. /// like skill, but <flag> defaults to 2
  5773. ///
  5774. /// addtoskill <skill id>,<amount>,<flag>
  5775. /// addtoskill <skill id>,<amount>
  5776. /// addtoskill "<skill name>",<amount>,<flag>
  5777. /// addtoskill "<skill name>",<amount>
  5778. ///
  5779. /// @see skill
  5780. BUILDIN_FUNC(addtoskill)
  5781. {
  5782. int id;
  5783. int level;
  5784. int flag = 2;
  5785. TBL_PC* sd;
  5786. sd = script_rid2sd(st);
  5787. if( sd == NULL )
  5788. return 0;// no player attached, report source
  5789. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5790. level = script_getnum(st,3);
  5791. if( script_hasdata(st,4) )
  5792. flag = script_getnum(st,4);
  5793. pc_skill(sd, id, level, flag);
  5794. return 0;
  5795. }
  5796. /// Increases the level of a guild skill.
  5797. ///
  5798. /// guildskill <skill id>,<amount>;
  5799. /// guildskill "<skill name>",<amount>;
  5800. BUILDIN_FUNC(guildskill)
  5801. {
  5802. int id;
  5803. int level;
  5804. TBL_PC* sd;
  5805. int i;
  5806. sd = script_rid2sd(st);
  5807. if( sd == NULL )
  5808. return 0;// no player attached, report source
  5809. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5810. level = script_getnum(st,3);
  5811. for( i=0; i < level; i++ )
  5812. guild_skillup(sd, id);
  5813. return 0;
  5814. }
  5815. /// Returns the level of the player skill.
  5816. ///
  5817. /// getskilllv(<skill id>) -> <level>
  5818. /// getskilllv("<skill name>") -> <level>
  5819. BUILDIN_FUNC(getskilllv)
  5820. {
  5821. int id;
  5822. TBL_PC* sd;
  5823. sd = script_rid2sd(st);
  5824. if( sd == NULL )
  5825. return 0;// no player attached, report source
  5826. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5827. script_pushint(st, pc_checkskill(sd,id));
  5828. return 0;
  5829. }
  5830. /// Returns the level of the guild skill.
  5831. ///
  5832. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  5833. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  5834. BUILDIN_FUNC(getgdskilllv)
  5835. {
  5836. int guild_id;
  5837. int skill_id;
  5838. struct guild* g;
  5839. guild_id = script_getnum(st,2);
  5840. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  5841. g = guild_search(guild_id);
  5842. if( g == NULL )
  5843. script_pushint(st, -1);
  5844. else
  5845. script_pushint(st, guild_checkskill(g,skill_id));
  5846. return 0;
  5847. }
  5848. /// Returns the 'basic_skill_check' setting.
  5849. /// This config determines if the server checks the skill level of NV_BASIC
  5850. /// before allowing the basic actions.
  5851. ///
  5852. /// basicskillcheck() -> <bool>
  5853. BUILDIN_FUNC(basicskillcheck)
  5854. {
  5855. script_pushint(st, battle_config.basic_skill_check);
  5856. return 0;
  5857. }
  5858. /// Returns the GM level of the player.
  5859. ///
  5860. /// getgmlevel() -> <level>
  5861. BUILDIN_FUNC(getgmlevel)
  5862. {
  5863. TBL_PC* sd;
  5864. sd = script_rid2sd(st);
  5865. if( sd == NULL )
  5866. return 0;// no player attached, report source
  5867. script_pushint(st, pc_isGM(sd));
  5868. return 0;
  5869. }
  5870. /// Terminates the execution of this script instance.
  5871. ///
  5872. /// end
  5873. BUILDIN_FUNC(end)
  5874. {
  5875. st->state = END;
  5876. return 0;
  5877. }
  5878. /// Checks if the player has that effect state (option).
  5879. ///
  5880. /// checkoption(<option>) -> <bool>
  5881. BUILDIN_FUNC(checkoption)
  5882. {
  5883. int option;
  5884. TBL_PC* sd;
  5885. sd = script_rid2sd(st);
  5886. if( sd == NULL )
  5887. return 0;// no player attached, report source
  5888. option = script_getnum(st,2);
  5889. if( sd->sc.option&option )
  5890. script_pushint(st, 1);
  5891. else
  5892. script_pushint(st, 0);
  5893. return 0;
  5894. }
  5895. /// Checks if the player is in that body state (opt1).
  5896. ///
  5897. /// checkoption1(<opt1>) -> <bool>
  5898. BUILDIN_FUNC(checkoption1)
  5899. {
  5900. int opt1;
  5901. TBL_PC* sd;
  5902. sd = script_rid2sd(st);
  5903. if( sd == NULL )
  5904. return 0;// no player attached, report source
  5905. opt1 = script_getnum(st,2);
  5906. if( sd->sc.opt1 == opt1 )
  5907. script_pushint(st, 1);
  5908. else
  5909. script_pushint(st, 0);
  5910. return 0;
  5911. }
  5912. /// Checks if the player has that health state (opt2).
  5913. ///
  5914. /// checkoption2(<opt2>) -> <bool>
  5915. BUILDIN_FUNC(checkoption2)
  5916. {
  5917. int opt2;
  5918. TBL_PC* sd;
  5919. sd = script_rid2sd(st);
  5920. if( sd == NULL )
  5921. return 0;// no player attached, report source
  5922. opt2 = script_getnum(st,2);
  5923. if( sd->sc.opt2&opt2 )
  5924. script_pushint(st, 1);
  5925. else
  5926. script_pushint(st, 0);
  5927. return 0;
  5928. }
  5929. /// Changes the effect state (option) of the player.
  5930. /// <flag> defaults to 1
  5931. /// <flag>=0 : removes the option
  5932. /// <flag>=other : adds the option
  5933. ///
  5934. /// setoption <option>,<flag>;
  5935. /// setoption <option>;
  5936. BUILDIN_FUNC(setoption)
  5937. {
  5938. int option;
  5939. int flag = 1;
  5940. TBL_PC* sd;
  5941. sd = script_rid2sd(st);
  5942. if( sd == NULL )
  5943. return 0;// no player attached, report source
  5944. option = script_getnum(st,2);
  5945. if( script_hasdata(st,3) )
  5946. flag = script_getnum(st,3);
  5947. else if( !option ){// Request to remove everything.
  5948. flag = 0;
  5949. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  5950. }
  5951. if( flag ){// Add option
  5952. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  5953. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  5954. pc_setoption(sd, sd->sc.option|option);
  5955. } else// Remove option
  5956. pc_setoption(sd, sd->sc.option&~option);
  5957. return 0;
  5958. }
  5959. /// Returns if the player has a cart.
  5960. ///
  5961. /// checkcart() -> <bool>
  5962. ///
  5963. /// @author Valaris
  5964. BUILDIN_FUNC(checkcart)
  5965. {
  5966. TBL_PC* sd;
  5967. sd = script_rid2sd(st);
  5968. if( sd == NULL )
  5969. return 0;// no player attached, report source
  5970. if( pc_iscarton(sd) )
  5971. script_pushint(st, 1);
  5972. else
  5973. script_pushint(st, 0);
  5974. return 0;
  5975. }
  5976. /// Sets the cart of the player.
  5977. /// <type> defaults to 1
  5978. /// <type>=0 : removes the cart
  5979. /// <type>=1 : Normal cart
  5980. /// <type>=2 : Wooden cart
  5981. /// <type>=3 : Covered cart with flowers and ferns
  5982. /// <type>=4 : Wooden cart with a Panda doll on the back
  5983. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  5984. ///
  5985. /// setcart <type>;
  5986. /// setcart;
  5987. BUILDIN_FUNC(setcart)
  5988. {
  5989. int type = 1;
  5990. TBL_PC* sd;
  5991. sd = script_rid2sd(st);
  5992. if( sd == NULL )
  5993. return 0;// no player attached, report source
  5994. if( script_hasdata(st,2) )
  5995. type = script_getnum(st,2);
  5996. pc_setcart(sd, type);
  5997. return 0;
  5998. }
  5999. /// Returns if the player has a falcon.
  6000. ///
  6001. /// checkfalcon() -> <bool>
  6002. ///
  6003. /// @author Valaris
  6004. BUILDIN_FUNC(checkfalcon)
  6005. {
  6006. TBL_PC* sd;
  6007. sd = script_rid2sd(st);
  6008. if( sd == NULL )
  6009. return 0;// no player attached, report source
  6010. if( pc_isfalcon(sd) )
  6011. script_pushint(st, 1);
  6012. else
  6013. script_pushint(st, 0);
  6014. return 0;
  6015. }
  6016. /// Sets if the player has a falcon or not.
  6017. /// <flag> defaults to 1
  6018. ///
  6019. /// setfalcon <flag>;
  6020. /// setfalcon;
  6021. BUILDIN_FUNC(setfalcon)
  6022. {
  6023. int flag = 1;
  6024. TBL_PC* sd;
  6025. sd = script_rid2sd(st);
  6026. if( sd == NULL )
  6027. return 0;// no player attached, report source
  6028. if( script_hasdata(st,2) )
  6029. flag = script_getnum(st,2);
  6030. pc_setfalcon(sd, flag);
  6031. return 0;
  6032. }
  6033. /// Returns if the player is riding.
  6034. ///
  6035. /// checkriding() -> <bool>
  6036. ///
  6037. /// @author Valaris
  6038. BUILDIN_FUNC(checkriding)
  6039. {
  6040. TBL_PC* sd;
  6041. sd = script_rid2sd(st);
  6042. if( sd == NULL )
  6043. return 0;// no player attached, report source
  6044. if( pc_isriding(sd) )
  6045. script_pushint(st, 1);
  6046. else
  6047. script_pushint(st, 0);
  6048. return 0;
  6049. }
  6050. /// Sets if the player is riding.
  6051. /// <flag> defaults to 1
  6052. ///
  6053. /// setriding <flag>;
  6054. /// setriding;
  6055. BUILDIN_FUNC(setriding)
  6056. {
  6057. int flag = 1;
  6058. TBL_PC* sd;
  6059. sd = script_rid2sd(st);
  6060. if( sd == NULL )
  6061. return 0;// no player attached, report source
  6062. if( script_hasdata(st,2) )
  6063. flag = script_getnum(st,2);
  6064. pc_setriding(sd, flag);
  6065. return 0;
  6066. }
  6067. /// Sets the save point of the player.
  6068. ///
  6069. /// save "<map name>",<x>,<y>
  6070. /// savepoint "<map name>",<x>,<y>
  6071. BUILDIN_FUNC(savepoint)
  6072. {
  6073. int x;
  6074. int y;
  6075. short map;
  6076. const char* str;
  6077. TBL_PC* sd;
  6078. sd = script_rid2sd(st);
  6079. if( sd == NULL )
  6080. return 0;// no player attached, report source
  6081. str = script_getstr(st, 2);
  6082. x = script_getnum(st,3);
  6083. y = script_getnum(st,4);
  6084. map = mapindex_name2id(str);
  6085. if( map )
  6086. pc_setsavepoint(sd, map, x, y);
  6087. return 0;
  6088. }
  6089. /*==========================================
  6090. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6091. *------------------------------------------*/
  6092. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6093. {
  6094. int type;
  6095. time_t timer;
  6096. struct tm *t;
  6097. type=script_getnum(st,2);
  6098. switch(type){
  6099. case 2:
  6100. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6101. // from the system clock.)
  6102. script_pushint(st,(int)time(NULL));
  6103. break;
  6104. case 1:
  6105. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6106. time(&timer);
  6107. t=localtime(&timer);
  6108. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6109. break;
  6110. case 0:
  6111. default:
  6112. //type 0:(System Ticks)
  6113. script_pushint(st,gettick());
  6114. break;
  6115. }
  6116. return 0;
  6117. }
  6118. /*==========================================
  6119. * GetTime(Type);
  6120. * 1: Sec 2: Min 3: Hour
  6121. * 4: WeekDay 5: MonthDay 6: Month
  6122. * 7: Year
  6123. *------------------------------------------*/
  6124. BUILDIN_FUNC(gettime) /* Asgard Version */
  6125. {
  6126. int type;
  6127. time_t timer;
  6128. struct tm *t;
  6129. type=script_getnum(st,2);
  6130. time(&timer);
  6131. t=localtime(&timer);
  6132. switch(type){
  6133. case 1://Sec(0~59)
  6134. script_pushint(st,t->tm_sec);
  6135. break;
  6136. case 2://Min(0~59)
  6137. script_pushint(st,t->tm_min);
  6138. break;
  6139. case 3://Hour(0~23)
  6140. script_pushint(st,t->tm_hour);
  6141. break;
  6142. case 4://WeekDay(0~6)
  6143. script_pushint(st,t->tm_wday);
  6144. break;
  6145. case 5://MonthDay(01~31)
  6146. script_pushint(st,t->tm_mday);
  6147. break;
  6148. case 6://Month(01~12)
  6149. script_pushint(st,t->tm_mon+1);
  6150. break;
  6151. case 7://Year(20xx)
  6152. script_pushint(st,t->tm_year+1900);
  6153. break;
  6154. case 8://Year Day(01~366)
  6155. script_pushint(st,t->tm_yday+1);
  6156. break;
  6157. default://(format error)
  6158. script_pushint(st,-1);
  6159. break;
  6160. }
  6161. return 0;
  6162. }
  6163. /*==========================================
  6164. * GetTimeStr("TimeFMT", Length);
  6165. *------------------------------------------*/
  6166. BUILDIN_FUNC(gettimestr)
  6167. {
  6168. char *tmpstr;
  6169. const char *fmtstr;
  6170. int maxlen;
  6171. time_t now = time(NULL);
  6172. fmtstr=script_getstr(st,2);
  6173. maxlen=script_getnum(st,3);
  6174. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6175. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6176. tmpstr[maxlen]='\0';
  6177. script_pushstr(st,tmpstr);
  6178. return 0;
  6179. }
  6180. /*==========================================
  6181. * カプラ倉庫を開く
  6182. *------------------------------------------*/
  6183. BUILDIN_FUNC(openstorage)
  6184. {
  6185. TBL_PC* sd;
  6186. sd = script_rid2sd(st);
  6187. if( sd == NULL )
  6188. return 0;
  6189. storage_storageopen(sd);
  6190. return 0;
  6191. }
  6192. BUILDIN_FUNC(guildopenstorage)
  6193. {
  6194. TBL_PC* sd;
  6195. int ret;
  6196. sd = script_rid2sd(st);
  6197. if( sd == NULL )
  6198. return 0;
  6199. ret = storage_guild_storageopen(sd);
  6200. script_pushint(st,ret);
  6201. return 0;
  6202. }
  6203. /*==========================================
  6204. * アイテムによるスキル発動
  6205. *------------------------------------------*/
  6206. /// itemskill <skill id>,<level>
  6207. /// itemskill "<skill name>",<level>
  6208. BUILDIN_FUNC(itemskill)
  6209. {
  6210. int id;
  6211. int lv;
  6212. TBL_PC* sd;
  6213. sd = script_rid2sd(st);
  6214. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6215. return 0;
  6216. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6217. lv = script_getnum(st,3);
  6218. sd->skillitem=id;
  6219. sd->skillitemlv=lv;
  6220. clif_item_skill(sd,id,lv);
  6221. return 0;
  6222. }
  6223. /*==========================================
  6224. * アイテム作成
  6225. *------------------------------------------*/
  6226. BUILDIN_FUNC(produce)
  6227. {
  6228. int trigger;
  6229. TBL_PC* sd;
  6230. sd = script_rid2sd(st);
  6231. if( sd == NULL )
  6232. return 0;
  6233. trigger=script_getnum(st,2);
  6234. clif_skill_produce_mix_list(sd, trigger);
  6235. return 0;
  6236. }
  6237. /*==========================================
  6238. * NPCでペット作る
  6239. *------------------------------------------*/
  6240. BUILDIN_FUNC(makepet)
  6241. {
  6242. TBL_PC* sd;
  6243. int id,pet_id;
  6244. id=script_getnum(st,2);
  6245. sd = script_rid2sd(st);
  6246. if( sd == NULL )
  6247. return 0;
  6248. pet_id = search_petDB_index(id, PET_CLASS);
  6249. if (pet_id < 0)
  6250. pet_id = search_petDB_index(id, PET_EGG);
  6251. if (pet_id >= 0 && sd) {
  6252. sd->catch_target_class = pet_db[pet_id].class_;
  6253. intif_create_pet(
  6254. sd->status.account_id, sd->status.char_id,
  6255. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6256. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6257. 100, 0, 1, pet_db[pet_id].jname);
  6258. }
  6259. return 0;
  6260. }
  6261. /*==========================================
  6262. * NPCで経験値上げる
  6263. *------------------------------------------*/
  6264. BUILDIN_FUNC(getexp)
  6265. {
  6266. TBL_PC* sd;
  6267. int base=0,job=0;
  6268. double bonus;
  6269. sd = script_rid2sd(st);
  6270. if( sd == NULL )
  6271. return 0;
  6272. base=script_getnum(st,2);
  6273. job =script_getnum(st,3);
  6274. if(base<0 || job<0)
  6275. return 0;
  6276. // bonus for npc-given exp
  6277. bonus = battle_config.quest_exp_rate / 100.;
  6278. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6279. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6280. pc_gainexp(sd, NULL, base, job);
  6281. return 0;
  6282. }
  6283. /*==========================================
  6284. * Gain guild exp [Celest]
  6285. *------------------------------------------*/
  6286. BUILDIN_FUNC(guildgetexp)
  6287. {
  6288. TBL_PC* sd;
  6289. int exp;
  6290. sd = script_rid2sd(st);
  6291. if( sd == NULL )
  6292. return 0;
  6293. exp = script_getnum(st,2);
  6294. if(exp < 0)
  6295. return 0;
  6296. if(sd && sd->status.guild_id > 0)
  6297. guild_getexp (sd, exp);
  6298. return 0;
  6299. }
  6300. /*==========================================
  6301. * Changes the guild master of a guild [Skotlex]
  6302. *------------------------------------------*/
  6303. BUILDIN_FUNC(guildchangegm)
  6304. {
  6305. TBL_PC *sd;
  6306. int guild_id;
  6307. const char *name;
  6308. guild_id = script_getnum(st,2);
  6309. name = script_getstr(st,3);
  6310. sd=map_nick2sd(name);
  6311. if (!sd)
  6312. script_pushint(st,0);
  6313. else
  6314. script_pushint(st,guild_gm_change(guild_id, sd));
  6315. return 0;
  6316. }
  6317. /*==========================================
  6318. * モンスター発生
  6319. *------------------------------------------*/
  6320. BUILDIN_FUNC(monster)
  6321. {
  6322. const char* map = script_getstr(st,2);
  6323. int x = script_getnum(st,3);
  6324. int y = script_getnum(st,4);
  6325. const char* str = script_getstr(st,5);
  6326. int class_ = script_getnum(st,6);
  6327. int amount = script_getnum(st,7);
  6328. const char* event = "";
  6329. struct map_session_data* sd;
  6330. int m;
  6331. if( script_hasdata(st,8) )
  6332. {
  6333. event = script_getstr(st,8);
  6334. check_event(st, event);
  6335. }
  6336. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6337. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6338. return 1;
  6339. }
  6340. sd = map_id2sd(st->rid);
  6341. if( sd && strcmp(map,"this") == 0 )
  6342. m = sd->bl.m;
  6343. else
  6344. m = map_mapname2mapid(map);
  6345. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6346. return 0;
  6347. }
  6348. /*==========================================
  6349. * Request List of Monster Drops
  6350. *------------------------------------------*/
  6351. BUILDIN_FUNC(getmobdrops)
  6352. {
  6353. int class_ = script_getnum(st,2);
  6354. int i, j = 0;
  6355. struct item_data *i_data;
  6356. struct mob_db *mob;
  6357. if( !mobdb_checkid(class_) )
  6358. {
  6359. script_pushint(st, 0);
  6360. return 0;
  6361. }
  6362. mob = mob_db(class_);
  6363. for( i = 0; i < MAX_MOB_DROP; i++ )
  6364. {
  6365. if( mob->dropitem[i].nameid < 1 )
  6366. continue;
  6367. if( (i_data = itemdb_exists(mob->dropitem[i].nameid)) == NULL )
  6368. continue;
  6369. mapreg_setreg(add_str("$@MobDrop_item") + (j<<24), mob->dropitem[i].nameid);
  6370. mapreg_setreg(add_str("$@MobDrop_rate") + (j<<24), mob->dropitem[i].p);
  6371. j++;
  6372. }
  6373. mapreg_setreg(add_str("$@MobDrop_count"), j);
  6374. script_pushint(st, 1);
  6375. return 0;
  6376. }
  6377. /*==========================================
  6378. * モンスター発生
  6379. *------------------------------------------*/
  6380. BUILDIN_FUNC(areamonster)
  6381. {
  6382. const char* map = script_getstr(st,2);
  6383. int x0 = script_getnum(st,3);
  6384. int y0 = script_getnum(st,4);
  6385. int x1 = script_getnum(st,5);
  6386. int y1 = script_getnum(st,6);
  6387. const char* str = script_getstr(st,7);
  6388. int class_ = script_getnum(st,8);
  6389. int amount = script_getnum(st,9);
  6390. const char* event = "";
  6391. struct map_session_data* sd;
  6392. int m;
  6393. if( script_hasdata(st,10) )
  6394. {
  6395. event = script_getstr(st,10);
  6396. check_event(st, event);
  6397. }
  6398. sd = map_id2sd(st->rid);
  6399. if( sd && strcmp(map,"this") == 0 )
  6400. m = sd->bl.m;
  6401. else
  6402. m = map_mapname2mapid(map);
  6403. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6404. return 0;
  6405. }
  6406. /*==========================================
  6407. * モンスター削除
  6408. *------------------------------------------*/
  6409. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  6410. { //same fix but with killmonster instead - stripping events from mobs.
  6411. TBL_MOB* md = (TBL_MOB*)bl;
  6412. char *event=va_arg(ap,char *);
  6413. int allflag=va_arg(ap,int);
  6414. md->state.npc_killmonster = 1;
  6415. if(!allflag){
  6416. if(strcmp(event,md->npc_event)==0)
  6417. status_kill(bl);
  6418. }else{
  6419. if(!md->spawn)
  6420. status_kill(bl);
  6421. }
  6422. md->state.npc_killmonster = 0;
  6423. return 0;
  6424. }
  6425. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6426. {
  6427. TBL_MOB* md = (TBL_MOB*)bl;
  6428. char *event=va_arg(ap,char *);
  6429. int allflag=va_arg(ap,int);
  6430. if(!allflag){
  6431. if(strcmp(event,md->npc_event)==0)
  6432. status_kill(bl);
  6433. }else{
  6434. if(!md->spawn)
  6435. status_kill(bl);
  6436. }
  6437. return 0;
  6438. }
  6439. BUILDIN_FUNC(killmonster)
  6440. {
  6441. const char *mapname,*event;
  6442. int m,allflag=0;
  6443. mapname=script_getstr(st,2);
  6444. event=script_getstr(st,3);
  6445. if(strcmp(event,"All")==0)
  6446. allflag = 1;
  6447. else
  6448. check_event(st, event);
  6449. if( (m=map_mapname2mapid(mapname))<0 )
  6450. return 0;
  6451. if( script_hasdata(st,4) ) {
  6452. if ( script_getnum(st,4) == 1 ) {
  6453. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6454. return 0;
  6455. }
  6456. }
  6457. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  6458. return 0;
  6459. }
  6460. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  6461. { //Strips the event from the mob if it's killed the old method.
  6462. struct mob_data *md;
  6463. md = BL_CAST(BL_MOB, bl);
  6464. if (md->npc_event[0])
  6465. md->npc_event[0] = 0;
  6466. status_kill(bl);
  6467. return 0;
  6468. }
  6469. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6470. {
  6471. status_kill(bl);
  6472. return 0;
  6473. }
  6474. BUILDIN_FUNC(killmonsterall)
  6475. {
  6476. const char *mapname;
  6477. int m;
  6478. mapname=script_getstr(st,2);
  6479. if( (m=map_mapname2mapid(mapname))<0 )
  6480. return 0;
  6481. if( script_hasdata(st,3) ) {
  6482. if ( script_getnum(st,3) == 1 ) {
  6483. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  6484. return 0;
  6485. }
  6486. }
  6487. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  6488. return 0;
  6489. }
  6490. /*==========================================
  6491. * Creates a clone of a player.
  6492. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6493. *------------------------------------------*/
  6494. BUILDIN_FUNC(clone)
  6495. {
  6496. TBL_PC *sd, *msd=NULL;
  6497. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6498. unsigned int duration = 0;
  6499. const char *map,*event="";
  6500. map=script_getstr(st,2);
  6501. x=script_getnum(st,3);
  6502. y=script_getnum(st,4);
  6503. event=script_getstr(st,5);
  6504. char_id=script_getnum(st,6);
  6505. if( script_hasdata(st,7) )
  6506. master_id=script_getnum(st,7);
  6507. if( script_hasdata(st,8) )
  6508. mode=script_getnum(st,8);
  6509. if( script_hasdata(st,9) )
  6510. flag=script_getnum(st,9);
  6511. if( script_hasdata(st,10) )
  6512. duration=script_getnum(st,10);
  6513. check_event(st, event);
  6514. m = map_mapname2mapid(map);
  6515. if (m < 0) return 0;
  6516. sd = map_charid2sd(char_id);
  6517. if (master_id) {
  6518. msd = map_charid2sd(master_id);
  6519. if (msd)
  6520. master_id = msd->bl.id;
  6521. else
  6522. master_id = 0;
  6523. }
  6524. if (sd) //Return ID of newly crafted clone.
  6525. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6526. else //Failed to create clone.
  6527. script_pushint(st,0);
  6528. return 0;
  6529. }
  6530. /*==========================================
  6531. * イベント実行
  6532. *------------------------------------------*/
  6533. BUILDIN_FUNC(doevent)
  6534. {
  6535. const char* event = script_getstr(st,2);
  6536. check_event(st, event);
  6537. npc_event(map_id2sd(st->rid),event,0);
  6538. return 0;
  6539. }
  6540. /*==========================================
  6541. * NPC主体イベント実行
  6542. *------------------------------------------*/
  6543. BUILDIN_FUNC(donpcevent)
  6544. {
  6545. const char* event = script_getstr(st,2);
  6546. check_event(st, event);
  6547. npc_event_do(event);
  6548. return 0;
  6549. }
  6550. /// for Aegis compatibility
  6551. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6552. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6553. {
  6554. const char* npc = script_getstr(st,2);
  6555. const char* command = script_getstr(st,3);
  6556. char event[51];
  6557. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6558. check_event(st, event);
  6559. npc_event_do(event);
  6560. return 0;
  6561. }
  6562. /*==========================================
  6563. * イベントタイマー追加
  6564. *------------------------------------------*/
  6565. BUILDIN_FUNC(addtimer)
  6566. {
  6567. int tick = script_getnum(st,2);
  6568. const char* event = script_getstr(st, 3);
  6569. TBL_PC* sd;
  6570. check_event(st, event);
  6571. sd = script_rid2sd(st);
  6572. if( sd == NULL )
  6573. return 0;
  6574. pc_addeventtimer(sd,tick,event);
  6575. return 0;
  6576. }
  6577. /*==========================================
  6578. * イベントタイマー削除
  6579. *------------------------------------------*/
  6580. BUILDIN_FUNC(deltimer)
  6581. {
  6582. const char *event;
  6583. TBL_PC* sd;
  6584. event=script_getstr(st, 2);
  6585. sd = script_rid2sd(st);
  6586. if( sd == NULL )
  6587. return 0;
  6588. check_event(st, event);
  6589. pc_deleventtimer(sd,event);
  6590. return 0;
  6591. }
  6592. /*==========================================
  6593. * イベントタイマーのカウント値追加
  6594. *------------------------------------------*/
  6595. BUILDIN_FUNC(addtimercount)
  6596. {
  6597. const char *event;
  6598. int tick;
  6599. TBL_PC* sd;
  6600. event=script_getstr(st, 2);
  6601. tick=script_getnum(st,3);
  6602. sd = script_rid2sd(st);
  6603. if( sd == NULL )
  6604. return 0;
  6605. check_event(st, event);
  6606. pc_addeventtimercount(sd,event,tick);
  6607. return 0;
  6608. }
  6609. /*==========================================
  6610. * NPCタイマー初期化
  6611. *------------------------------------------*/
  6612. BUILDIN_FUNC(initnpctimer)
  6613. {
  6614. struct npc_data *nd;
  6615. int flag = 0;
  6616. if( script_hasdata(st,3) )
  6617. { //Two arguments: NPC name and attach flag.
  6618. nd = npc_name2id(script_getstr(st, 2));
  6619. flag = script_getnum(st,3);
  6620. }
  6621. else if( script_hasdata(st,2) )
  6622. { //Check if argument is numeric (flag) or string (npc name)
  6623. struct script_data *data;
  6624. data = script_getdata(st,2);
  6625. get_val(st,data);
  6626. if( data_isstring(data) ) //NPC name
  6627. nd = npc_name2id(conv_str(st, data));
  6628. else if( data_isint(data) ) //Flag
  6629. {
  6630. nd = (struct npc_data *)map_id2bl(st->oid);
  6631. flag = conv_num(st,data);
  6632. }
  6633. else
  6634. {
  6635. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6636. return 1;
  6637. }
  6638. }
  6639. else
  6640. nd=(struct npc_data *)map_id2bl(st->oid);
  6641. if (!nd)
  6642. return 0;
  6643. if (flag) //Attach
  6644. {
  6645. TBL_PC* sd = script_rid2sd(st);
  6646. if( sd == NULL )
  6647. return 0;
  6648. nd->u.scr.rid = sd->bl.id;
  6649. }
  6650. npc_settimerevent_tick(nd,0);
  6651. npc_timerevent_start(nd, st->rid);
  6652. return 0;
  6653. }
  6654. /*==========================================
  6655. * NPCタイマー開始
  6656. *------------------------------------------*/
  6657. BUILDIN_FUNC(startnpctimer)
  6658. {
  6659. struct npc_data *nd;
  6660. int flag = 0;
  6661. if( script_hasdata(st,3) )
  6662. { //Two arguments: NPC name and attach flag.
  6663. nd = npc_name2id(script_getstr(st, 2));
  6664. flag = script_getnum(st,3);
  6665. }
  6666. else if( script_hasdata(st,2) )
  6667. { //Check if argument is numeric (flag) or string (npc name)
  6668. struct script_data *data;
  6669. data = script_getdata(st,2);
  6670. get_val(st,data);
  6671. if( data_isstring(data) ) //NPC name
  6672. nd = npc_name2id(conv_str(st, data));
  6673. else if( data_isint(data) ) //Flag
  6674. {
  6675. nd = (struct npc_data *)map_id2bl(st->oid);
  6676. flag = conv_num(st,data);
  6677. }
  6678. else
  6679. {
  6680. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6681. return 1;
  6682. }
  6683. }
  6684. else
  6685. nd=(struct npc_data *)map_id2bl(st->oid);
  6686. if (!nd)
  6687. return 0;
  6688. if (flag) //Attach
  6689. {
  6690. TBL_PC* sd = script_rid2sd(st);
  6691. if( sd == NULL )
  6692. return 0;
  6693. nd->u.scr.rid = sd->bl.id;
  6694. }
  6695. npc_timerevent_start(nd, st->rid);
  6696. return 0;
  6697. }
  6698. /*==========================================
  6699. * NPCタイマー停止
  6700. *------------------------------------------*/
  6701. BUILDIN_FUNC(stopnpctimer)
  6702. {
  6703. struct npc_data *nd;
  6704. int flag = 0;
  6705. if( script_hasdata(st,3) )
  6706. { //Two arguments: NPC name and attach flag.
  6707. nd = npc_name2id(script_getstr(st, 2));
  6708. flag = script_getnum(st,3);
  6709. }
  6710. else if( script_hasdata(st,2) )
  6711. { //Check if argument is numeric (flag) or string (npc name)
  6712. struct script_data *data;
  6713. data = script_getdata(st,2);
  6714. get_val(st,data);
  6715. if( data_isstring(data) ) //NPC name
  6716. nd = npc_name2id(conv_str(st, data));
  6717. else if( data_isint(data) ) //Flag
  6718. {
  6719. nd = (struct npc_data *)map_id2bl(st->oid);
  6720. flag = conv_num(st,data);
  6721. }
  6722. else
  6723. {
  6724. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6725. return 1;
  6726. }
  6727. }
  6728. else
  6729. nd=(struct npc_data *)map_id2bl(st->oid);
  6730. if (!nd)
  6731. return 0;
  6732. if (flag) //Detach
  6733. nd->u.scr.rid = 0;
  6734. npc_timerevent_stop(nd);
  6735. return 0;
  6736. }
  6737. /*==========================================
  6738. * NPCタイマー情報所得
  6739. *------------------------------------------*/
  6740. BUILDIN_FUNC(getnpctimer)
  6741. {
  6742. struct npc_data *nd;
  6743. TBL_PC *sd;
  6744. int type=script_getnum(st,2);
  6745. int val=0;
  6746. if( script_hasdata(st,3) )
  6747. nd = npc_name2id(script_getstr(st,3));
  6748. else
  6749. nd = (struct npc_data *)map_id2bl(st->oid);
  6750. if (!nd || nd->bl.type != BL_NPC)
  6751. {
  6752. script_pushint(st,0);
  6753. ShowError("getnpctimer: Invalid NPC.\n");
  6754. return 1;
  6755. }
  6756. switch(type){
  6757. case 0: val=npc_gettimerevent_tick(nd); break;
  6758. case 1:
  6759. if (nd->u.scr.rid) {
  6760. sd = map_id2sd(nd->u.scr.rid);
  6761. if (!sd) {
  6762. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6763. break;
  6764. }
  6765. val = (sd->npc_timer_id != -1);
  6766. } else
  6767. val= (nd->u.scr.timerid !=-1);
  6768. break;
  6769. case 2: val= nd->u.scr.timeramount; break;
  6770. }
  6771. script_pushint(st,val);
  6772. return 0;
  6773. }
  6774. /*==========================================
  6775. * NPCタイマー値設定
  6776. *------------------------------------------*/
  6777. BUILDIN_FUNC(setnpctimer)
  6778. {
  6779. int tick;
  6780. struct npc_data *nd;
  6781. tick=script_getnum(st,2);
  6782. if( script_hasdata(st,3) )
  6783. nd=npc_name2id(script_getstr(st,3));
  6784. else
  6785. nd=(struct npc_data *)map_id2bl(st->oid);
  6786. if (!nd || nd->bl.type != BL_NPC)
  6787. {
  6788. script_pushint(st,1);
  6789. ShowError("setnpctimer: Invalid NPC.\n");
  6790. return 1;
  6791. }
  6792. npc_settimerevent_tick(nd,tick);
  6793. script_pushint(st,0);
  6794. return 0;
  6795. }
  6796. /*==========================================
  6797. * attaches the player rid to the timer [Celest]
  6798. *------------------------------------------*/
  6799. BUILDIN_FUNC(attachnpctimer)
  6800. {
  6801. TBL_PC *sd;
  6802. struct npc_data *nd;
  6803. nd=(struct npc_data *)map_id2bl(st->oid);
  6804. if (!nd || nd->bl.type != BL_NPC)
  6805. {
  6806. script_pushint(st,1);
  6807. ShowError("setnpctimer: Invalid NPC.\n");
  6808. return 1;
  6809. }
  6810. if( script_hasdata(st,2) )
  6811. sd=map_nick2sd(script_getstr(st,2));
  6812. else
  6813. sd = script_rid2sd(st);
  6814. if (!sd)
  6815. {
  6816. script_pushint(st,1);
  6817. ShowWarning("attachnpctimer: Invalid player.\n");
  6818. return 1;
  6819. }
  6820. nd->u.scr.rid = sd->bl.id;
  6821. script_pushint(st,0);
  6822. return 0;
  6823. }
  6824. /*==========================================
  6825. * detaches a player rid from the timer [Celest]
  6826. *------------------------------------------*/
  6827. BUILDIN_FUNC(detachnpctimer)
  6828. {
  6829. struct npc_data *nd;
  6830. if( script_hasdata(st,2) )
  6831. nd=npc_name2id(script_getstr(st,2));
  6832. else
  6833. nd=(struct npc_data *)map_id2bl(st->oid);
  6834. if (!nd || nd->bl.type != BL_NPC)
  6835. {
  6836. script_pushint(st,1);
  6837. ShowError("detachnpctimer: Invalid NPC.\n");
  6838. return 1;
  6839. }
  6840. nd->u.scr.rid = 0;
  6841. script_pushint(st,0);
  6842. return 0;
  6843. }
  6844. /*==========================================
  6845. * To avoid "player not attached" script errors, this function is provided,
  6846. * it checks if there is a player attached to the current script. [Skotlex]
  6847. * If no, returns 0, if yes, returns the account_id of the attached player.
  6848. *------------------------------------------*/
  6849. BUILDIN_FUNC(playerattached)
  6850. {
  6851. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  6852. script_pushint(st,0);
  6853. else
  6854. script_pushint(st,st->rid);
  6855. return 0;
  6856. }
  6857. /*==========================================
  6858. * 天の声アナウンス
  6859. *------------------------------------------*/
  6860. BUILDIN_FUNC(announce)
  6861. {
  6862. const char *str, *color=NULL;
  6863. int flag;
  6864. str=script_getstr(st,2);
  6865. flag=script_getnum(st,3);
  6866. if (script_hasdata(st,4))
  6867. color=script_getstr(st,4);
  6868. if(flag&0x0f){
  6869. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  6870. (struct block_list *)script_rid2sd(st);
  6871. if( bl == NULL )
  6872. return 0;
  6873. if (color)
  6874. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  6875. else
  6876. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  6877. }else {
  6878. if (color)
  6879. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  6880. else
  6881. intif_GMmessage(str,(int)strlen(str)+1,flag);
  6882. }
  6883. return 0;
  6884. }
  6885. /*==========================================
  6886. * 天の声アナウンス(特定マップ)
  6887. *------------------------------------------*/
  6888. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  6889. {
  6890. char *str, *color;
  6891. int len,flag;
  6892. str=va_arg(ap,char *);
  6893. len=va_arg(ap,int);
  6894. flag=va_arg(ap,int);
  6895. color=va_arg(ap,char *);
  6896. if (color)
  6897. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  6898. else
  6899. clif_GMmessage(bl,str,len,flag|3);
  6900. return 0;
  6901. }
  6902. BUILDIN_FUNC(mapannounce)
  6903. {
  6904. const char *mapname,*str, *color=NULL;
  6905. int flag,m;
  6906. mapname=script_getstr(st,2);
  6907. str=script_getstr(st,3);
  6908. flag=script_getnum(st,4);
  6909. if (script_hasdata(st,5))
  6910. color=script_getstr(st,5);
  6911. if( (m=map_mapname2mapid(mapname))<0 )
  6912. return 0;
  6913. map_foreachinmap(buildin_mapannounce_sub,
  6914. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  6915. return 0;
  6916. }
  6917. /*==========================================
  6918. * 天の声アナウンス(特定エリア)
  6919. *------------------------------------------*/
  6920. BUILDIN_FUNC(areaannounce)
  6921. {
  6922. const char *map,*str,*color=NULL;
  6923. int flag,m;
  6924. int x0,y0,x1,y1;
  6925. map=script_getstr(st,2);
  6926. x0=script_getnum(st,3);
  6927. y0=script_getnum(st,4);
  6928. x1=script_getnum(st,5);
  6929. y1=script_getnum(st,6);
  6930. str=script_getstr(st,7);
  6931. flag=script_getnum(st,8);
  6932. if (script_hasdata(st,9))
  6933. color=script_getstr(st,9);
  6934. if( (m=map_mapname2mapid(map))<0 )
  6935. return 0;
  6936. map_foreachinarea(buildin_mapannounce_sub,
  6937. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  6938. return 0;
  6939. }
  6940. /*==========================================
  6941. * ユーザー数所得
  6942. *------------------------------------------*/
  6943. BUILDIN_FUNC(getusers)
  6944. {
  6945. int flag=script_getnum(st,2);
  6946. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  6947. int val=0;
  6948. switch(flag&0x07){
  6949. case 0: val=map[bl->m].users; break;
  6950. case 1: val=map_getusers(); break;
  6951. }
  6952. script_pushint(st,val);
  6953. return 0;
  6954. }
  6955. /*==========================================
  6956. * Works like @WHO - displays all online users names in window
  6957. *------------------------------------------*/
  6958. BUILDIN_FUNC(getusersname)
  6959. {
  6960. TBL_PC *sd, *pl_sd;
  6961. int disp_num=1;
  6962. struct s_mapiterator* iter;
  6963. sd = script_rid2sd(st);
  6964. if (!sd) return 0;
  6965. iter = mapit_getallusers();
  6966. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  6967. {
  6968. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  6969. continue; // skip hidden GMs
  6970. if((disp_num++)%10==0)
  6971. clif_scriptnext(sd,st->oid);
  6972. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  6973. }
  6974. mapit_free(iter);
  6975. return 0;
  6976. }
  6977. /*==========================================
  6978. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  6979. *------------------------------------------*/
  6980. BUILDIN_FUNC(getmapguildusers)
  6981. {
  6982. const char *str;
  6983. int m, gid;
  6984. int i=0,c=0;
  6985. struct guild *g = NULL;
  6986. str=script_getstr(st,2);
  6987. gid=script_getnum(st,3);
  6988. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  6989. script_pushint(st,-1);
  6990. return 0;
  6991. }
  6992. g = guild_search(gid);
  6993. if (g){
  6994. for(i = 0; i < g->max_member; i++)
  6995. {
  6996. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  6997. c++;
  6998. }
  6999. }
  7000. script_pushint(st,c);
  7001. return 0;
  7002. }
  7003. /*==========================================
  7004. * マップ指定ユーザー数所得
  7005. *------------------------------------------*/
  7006. BUILDIN_FUNC(getmapusers)
  7007. {
  7008. const char *str;
  7009. int m;
  7010. str=script_getstr(st,2);
  7011. if( (m=map_mapname2mapid(str))< 0){
  7012. script_pushint(st,-1);
  7013. return 0;
  7014. }
  7015. script_pushint(st,map[m].users);
  7016. return 0;
  7017. }
  7018. /*==========================================
  7019. * エリア指定ユーザー数所得
  7020. *------------------------------------------*/
  7021. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7022. {
  7023. int *users=va_arg(ap,int *);
  7024. (*users)++;
  7025. return 0;
  7026. }
  7027. BUILDIN_FUNC(getareausers)
  7028. {
  7029. const char *str;
  7030. int m,x0,y0,x1,y1,users=0;
  7031. str=script_getstr(st,2);
  7032. x0=script_getnum(st,3);
  7033. y0=script_getnum(st,4);
  7034. x1=script_getnum(st,5);
  7035. y1=script_getnum(st,6);
  7036. if( (m=map_mapname2mapid(str))< 0){
  7037. script_pushint(st,-1);
  7038. return 0;
  7039. }
  7040. map_foreachinarea(buildin_getareausers_sub,
  7041. m,x0,y0,x1,y1,BL_PC,&users);
  7042. script_pushint(st,users);
  7043. return 0;
  7044. }
  7045. /*==========================================
  7046. * エリア指定ドロップアイテム数所得
  7047. *------------------------------------------*/
  7048. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7049. {
  7050. int item=va_arg(ap,int);
  7051. int *amount=va_arg(ap,int *);
  7052. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7053. if(drop->item_data.nameid==item)
  7054. (*amount)+=drop->item_data.amount;
  7055. return 0;
  7056. }
  7057. BUILDIN_FUNC(getareadropitem)
  7058. {
  7059. const char *str;
  7060. int m,x0,y0,x1,y1,item,amount=0;
  7061. struct script_data *data;
  7062. str=script_getstr(st,2);
  7063. x0=script_getnum(st,3);
  7064. y0=script_getnum(st,4);
  7065. x1=script_getnum(st,5);
  7066. y1=script_getnum(st,6);
  7067. data=script_getdata(st,7);
  7068. get_val(st,data);
  7069. if( data_isstring(data) ){
  7070. const char *name=conv_str(st,data);
  7071. struct item_data *item_data = itemdb_searchname(name);
  7072. item=UNKNOWN_ITEM_ID;
  7073. if( item_data )
  7074. item=item_data->nameid;
  7075. }else
  7076. item=conv_num(st,data);
  7077. if( (m=map_mapname2mapid(str))< 0){
  7078. script_pushint(st,-1);
  7079. return 0;
  7080. }
  7081. map_foreachinarea(buildin_getareadropitem_sub,
  7082. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7083. script_pushint(st,amount);
  7084. return 0;
  7085. }
  7086. /*==========================================
  7087. * NPCの有効化
  7088. *------------------------------------------*/
  7089. BUILDIN_FUNC(enablenpc)
  7090. {
  7091. const char *str;
  7092. str=script_getstr(st,2);
  7093. npc_enable(str,1);
  7094. return 0;
  7095. }
  7096. /*==========================================
  7097. * NPCの無効化
  7098. *------------------------------------------*/
  7099. BUILDIN_FUNC(disablenpc)
  7100. {
  7101. const char *str;
  7102. str=script_getstr(st,2);
  7103. npc_enable(str,0);
  7104. return 0;
  7105. }
  7106. /*==========================================
  7107. * 隠れているNPCの表示
  7108. *------------------------------------------*/
  7109. BUILDIN_FUNC(hideoffnpc)
  7110. {
  7111. const char *str;
  7112. str=script_getstr(st,2);
  7113. npc_enable(str,2);
  7114. return 0;
  7115. }
  7116. /*==========================================
  7117. * NPCをハイディング
  7118. *------------------------------------------*/
  7119. BUILDIN_FUNC(hideonnpc)
  7120. {
  7121. const char *str;
  7122. str=script_getstr(st,2);
  7123. npc_enable(str,4);
  7124. return 0;
  7125. }
  7126. /// Starts a status effect on the target unit or on the attached player.
  7127. ///
  7128. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7129. BUILDIN_FUNC(sc_start)
  7130. {
  7131. struct block_list* bl;
  7132. enum sc_type type;
  7133. int tick;
  7134. int val1;
  7135. int val4 = 0;
  7136. type = (sc_type)script_getnum(st,2);
  7137. tick = script_getnum(st,3);
  7138. val1 = script_getnum(st,4);
  7139. if( script_hasdata(st,5) )
  7140. bl = map_id2bl(script_getnum(st,5));
  7141. else
  7142. bl = map_id2bl(st->rid);
  7143. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7144. {// When there isn't a duration specified, try to get it from the skill_db
  7145. tick = skill_get_time(status_sc2skill(type), val1);
  7146. }
  7147. if( potion_flag == 1 && potion_target )
  7148. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7149. bl = map_id2bl(potion_target);
  7150. tick /= 2;// Thrown potions only last half.
  7151. val4 = 1;// Mark that this was a thrown sc_effect
  7152. }
  7153. if( bl )
  7154. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7155. return 0;
  7156. }
  7157. /// Starts a status effect on the target unit or on the attached player.
  7158. ///
  7159. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7160. BUILDIN_FUNC(sc_start2)
  7161. {
  7162. struct block_list* bl;
  7163. enum sc_type type;
  7164. int tick;
  7165. int val1;
  7166. int val4 = 0;
  7167. int rate;
  7168. type = (sc_type)script_getnum(st,2);
  7169. tick = script_getnum(st,3);
  7170. val1 = script_getnum(st,4);
  7171. rate = script_getnum(st,5);
  7172. if( script_hasdata(st,6) )
  7173. bl = map_id2bl(script_getnum(st,6));
  7174. else
  7175. bl = map_id2bl(st->rid);
  7176. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7177. {// When there isn't a duration specified, try to get it from the skill_db
  7178. tick = skill_get_time(status_sc2skill(type), val1);
  7179. }
  7180. if( potion_flag == 1 && potion_target )
  7181. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7182. bl = map_id2bl(potion_target);
  7183. tick /= 2;// Thrown potions only last half.
  7184. val4 = 1;// Mark that this was a thrown sc_effect
  7185. }
  7186. if( bl )
  7187. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7188. return 0;
  7189. }
  7190. /// Starts a status effect on the target unit or on the attached player.
  7191. ///
  7192. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7193. BUILDIN_FUNC(sc_start4)
  7194. {
  7195. struct block_list* bl;
  7196. enum sc_type type;
  7197. int tick;
  7198. int val1;
  7199. int val2;
  7200. int val3;
  7201. int val4;
  7202. type = (sc_type)script_getnum(st,2);
  7203. tick = script_getnum(st,3);
  7204. val1 = script_getnum(st,4);
  7205. val2 = script_getnum(st,5);
  7206. val3 = script_getnum(st,6);
  7207. val4 = script_getnum(st,7);
  7208. if( script_hasdata(st,8) )
  7209. bl = map_id2bl(script_getnum(st,8));
  7210. else
  7211. bl = map_id2bl(st->rid);
  7212. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7213. {// When there isn't a duration specified, try to get it from the skill_db
  7214. tick = skill_get_time(status_sc2skill(type), val1);
  7215. }
  7216. if( potion_flag == 1 && potion_target )
  7217. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7218. bl = map_id2bl(potion_target);
  7219. tick /= 2;// Thrown potions only last half.
  7220. }
  7221. if( bl )
  7222. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7223. return 0;
  7224. }
  7225. /// Ends one or all status effects on the target unit or on the attached player.
  7226. ///
  7227. /// sc_end <effect_id>{,<unit_id>};
  7228. BUILDIN_FUNC(sc_end)
  7229. {
  7230. struct block_list* bl;
  7231. int type;
  7232. type = script_getnum(st,2);
  7233. if( script_hasdata(st,3) )
  7234. bl = map_id2bl(script_getnum(st,3));
  7235. else
  7236. bl = map_id2bl(st->rid);
  7237. if( potion_flag==1 && potion_target )
  7238. {//##TODO how does this work [FlavioJS]
  7239. bl = map_id2bl(potion_target);
  7240. }
  7241. if( !bl ) return 0;
  7242. if( type >= 0 && type < SC_MAX )
  7243. {
  7244. struct status_change *sc = status_get_sc(bl);
  7245. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7246. if (!sce) return 0;
  7247. //This should help status_change_end force disabling the SC in case it has no limit.
  7248. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7249. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7250. } else
  7251. status_change_clear(bl, 2);// remove all effects
  7252. return 0;
  7253. }
  7254. /*==========================================
  7255. * 状態異常耐性を計算した確率を返す
  7256. *------------------------------------------*/
  7257. BUILDIN_FUNC(getscrate)
  7258. {
  7259. struct block_list *bl;
  7260. int type,rate;
  7261. type=script_getnum(st,2);
  7262. rate=script_getnum(st,3);
  7263. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7264. bl = map_id2bl(script_getnum(st,4));
  7265. else
  7266. bl = map_id2bl(st->rid);
  7267. if (bl)
  7268. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7269. script_pushint(st,rate);
  7270. return 0;
  7271. }
  7272. /*==========================================
  7273. *
  7274. *------------------------------------------*/
  7275. BUILDIN_FUNC(debugmes)
  7276. {
  7277. const char *str;
  7278. str=script_getstr(st,2);
  7279. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7280. return 0;
  7281. }
  7282. /*==========================================
  7283. *捕獲アイテム使用
  7284. *------------------------------------------*/
  7285. BUILDIN_FUNC(catchpet)
  7286. {
  7287. int pet_id;
  7288. TBL_PC *sd;
  7289. pet_id= script_getnum(st,2);
  7290. sd=script_rid2sd(st);
  7291. if( sd == NULL )
  7292. return 0;
  7293. pet_catch_process1(sd,pet_id);
  7294. return 0;
  7295. }
  7296. /*==========================================
  7297. * [orn]
  7298. *------------------------------------------*/
  7299. BUILDIN_FUNC(homunculus_evolution)
  7300. {
  7301. TBL_PC *sd;
  7302. sd=script_rid2sd(st);
  7303. if( sd == NULL )
  7304. return 0;
  7305. if(merc_is_hom_active(sd->hd))
  7306. {
  7307. if (sd->hd->homunculus.intimacy > 91000)
  7308. merc_hom_evolution(sd->hd);
  7309. else
  7310. clif_emotion(&sd->hd->bl, 4) ; //swt
  7311. }
  7312. return 0;
  7313. }
  7314. // [Zephyrus]
  7315. BUILDIN_FUNC(homunculus_shuffle)
  7316. {
  7317. TBL_PC *sd;
  7318. sd=script_rid2sd(st);
  7319. if( sd == NULL )
  7320. return 0;
  7321. if(merc_is_hom_active(sd->hd))
  7322. merc_hom_shuffle(sd->hd);
  7323. return 0;
  7324. }
  7325. //These two functions bring the eA MAPID_* class functionality to scripts.
  7326. BUILDIN_FUNC(eaclass)
  7327. {
  7328. int class_;
  7329. if( script_hasdata(st,2) )
  7330. class_ = script_getnum(st,2);
  7331. else {
  7332. TBL_PC *sd;
  7333. sd=script_rid2sd(st);
  7334. if (!sd) {
  7335. script_pushint(st,-1);
  7336. return 0;
  7337. }
  7338. class_ = sd->status.class_;
  7339. }
  7340. script_pushint(st,pc_jobid2mapid(class_));
  7341. return 0;
  7342. }
  7343. BUILDIN_FUNC(roclass)
  7344. {
  7345. int class_ =script_getnum(st,2);
  7346. int sex;
  7347. if( script_hasdata(st,3) )
  7348. sex = script_getnum(st,3);
  7349. else {
  7350. TBL_PC *sd;
  7351. if (st->rid && (sd=script_rid2sd(st)))
  7352. sex = sd->status.sex;
  7353. else
  7354. sex = 1; //Just use male when not found.
  7355. }
  7356. script_pushint(st,pc_mapid2jobid(class_, sex));
  7357. return 0;
  7358. }
  7359. /*==========================================
  7360. *携帯卵孵化機使用
  7361. *------------------------------------------*/
  7362. BUILDIN_FUNC(birthpet)
  7363. {
  7364. TBL_PC *sd;
  7365. sd=script_rid2sd(st);
  7366. if( sd == NULL )
  7367. return 0;
  7368. clif_sendegg(sd);
  7369. return 0;
  7370. }
  7371. /*==========================================
  7372. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7373. *------------------------------------------*/
  7374. BUILDIN_FUNC(resetlvl)
  7375. {
  7376. TBL_PC *sd;
  7377. int type=script_getnum(st,2);
  7378. sd=script_rid2sd(st);
  7379. if( sd == NULL )
  7380. return 0;
  7381. pc_resetlvl(sd,type);
  7382. return 0;
  7383. }
  7384. /*==========================================
  7385. * ステータスリセット
  7386. *------------------------------------------*/
  7387. BUILDIN_FUNC(resetstatus)
  7388. {
  7389. TBL_PC *sd;
  7390. sd=script_rid2sd(st);
  7391. pc_resetstate(sd);
  7392. return 0;
  7393. }
  7394. /*==========================================
  7395. * script command resetskill
  7396. *------------------------------------------*/
  7397. BUILDIN_FUNC(resetskill)
  7398. {
  7399. TBL_PC *sd;
  7400. sd=script_rid2sd(st);
  7401. pc_resetskill(sd,1);
  7402. return 0;
  7403. }
  7404. /*==========================================
  7405. * Counts total amount of skill points.
  7406. *------------------------------------------*/
  7407. BUILDIN_FUNC(skillpointcount)
  7408. {
  7409. TBL_PC *sd;
  7410. sd=script_rid2sd(st);
  7411. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7412. return 0;
  7413. }
  7414. /*==========================================
  7415. *
  7416. *------------------------------------------*/
  7417. BUILDIN_FUNC(changebase)
  7418. {
  7419. TBL_PC *sd=NULL;
  7420. int vclass;
  7421. if( script_hasdata(st,3) )
  7422. sd=map_id2sd(script_getnum(st,3));
  7423. else
  7424. sd=script_rid2sd(st);
  7425. if(sd == NULL)
  7426. return 0;
  7427. vclass = script_getnum(st,2);
  7428. if(vclass == JOB_WEDDING)
  7429. {
  7430. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7431. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7432. )
  7433. return 0;
  7434. }
  7435. if(!sd->disguise && vclass != sd->vd.class_) {
  7436. status_set_viewdata(&sd->bl, vclass);
  7437. //Updated client view. Base, Weapon and Cloth Colors.
  7438. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7439. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7440. if (sd->vd.cloth_color)
  7441. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7442. }
  7443. return 0;
  7444. }
  7445. /*==========================================
  7446. * 性別変換
  7447. *------------------------------------------*/
  7448. BUILDIN_FUNC(changesex)
  7449. {
  7450. TBL_PC *sd = NULL;
  7451. sd = script_rid2sd(st);
  7452. chrif_changesex(sd);
  7453. return 0;
  7454. }
  7455. /*==========================================
  7456. * Works like 'announce' but outputs in the common chat window
  7457. *------------------------------------------*/
  7458. BUILDIN_FUNC(globalmes)
  7459. {
  7460. struct block_list *bl = map_id2bl(st->oid);
  7461. struct npc_data *nd = (struct npc_data *)bl;
  7462. const char *name=NULL,*mes;
  7463. mes=script_getstr(st,2); // メッセージの取得
  7464. if(mes==NULL) return 0;
  7465. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7466. name=script_getstr(st,3);
  7467. } else {
  7468. name=nd->name;
  7469. }
  7470. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7471. return 0;
  7472. }
  7473. /////////////////////////////////////////////////////////////////////
  7474. // NPC waiting room (chat room)
  7475. //
  7476. /// Creates a waiting room (chat room) for this npc.
  7477. ///
  7478. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7479. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7480. /// waitingroom "<title>",<limit>,"<event>";
  7481. /// waitingroom "<title>",<limit>;
  7482. BUILDIN_FUNC(waitingroom)
  7483. {
  7484. struct npc_data* nd;
  7485. const char* title;
  7486. const char* ev = "";
  7487. int limit;
  7488. int trigger = 0;
  7489. int pub = 1;
  7490. title = script_getstr(st, 2);
  7491. limit = script_getnum(st, 3);
  7492. if( script_hasdata(st,5) )
  7493. {
  7494. struct script_data* last = script_getdata(st, 5);
  7495. get_val(st, last);
  7496. if( data_isstring(last) )
  7497. {// ,<trigger>,"<event>"
  7498. trigger = script_getnum(st, 4);
  7499. ev = script_getstr(st, 5);
  7500. }
  7501. else
  7502. {// ,"<event>",<trigger>
  7503. ev = script_getstr(st, 4);
  7504. trigger = script_getnum(st,5);
  7505. }
  7506. }
  7507. else if( script_hasdata(st,4) )
  7508. {// ,"<event>"
  7509. ev = script_getstr(st, 4);
  7510. trigger = limit;
  7511. }
  7512. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  7513. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7514. return 0;
  7515. }
  7516. /// Removes the waiting room of the current or target npc.
  7517. ///
  7518. /// delwaitingroom "<npc_name>";
  7519. /// delwaitingroom;
  7520. BUILDIN_FUNC(delwaitingroom)
  7521. {
  7522. struct npc_data* nd;
  7523. if( script_hasdata(st,2) )
  7524. nd = npc_name2id(script_getstr(st, 2));
  7525. else
  7526. nd = (struct npc_data *)map_id2bl(st->oid);
  7527. if( nd != NULL )
  7528. chat_deletenpcchat(nd);
  7529. return 0;
  7530. }
  7531. /// Kicks all the players from the waiting room of the current or target npc.
  7532. ///
  7533. /// kickwaitingroomall "<npc_name>";
  7534. /// kickwaitingroomall;
  7535. BUILDIN_FUNC(waitingroomkickall)
  7536. {
  7537. struct npc_data* nd;
  7538. struct chat_data* cd;
  7539. if( script_hasdata(st,2) )
  7540. nd = npc_name2id(script_getstr(st,2));
  7541. else
  7542. nd = (struct npc_data *)map_id2bl(st->oid);
  7543. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7544. chat_npckickall(cd);
  7545. return 0;
  7546. }
  7547. /// Enables the waiting room event of the current or target npc.
  7548. ///
  7549. /// enablewaitingroomevent "<npc_name>";
  7550. /// enablewaitingroomevent;
  7551. BUILDIN_FUNC(enablewaitingroomevent)
  7552. {
  7553. struct npc_data* nd;
  7554. struct chat_data* cd;
  7555. if( script_hasdata(st,2) )
  7556. nd = npc_name2id(script_getstr(st, 2));
  7557. else
  7558. nd = (struct npc_data *)map_id2bl(st->oid);
  7559. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7560. chat_enableevent(cd);
  7561. return 0;
  7562. }
  7563. /// Disables the waiting room event of the current or target npc.
  7564. ///
  7565. /// disablewaitingroomevent "<npc_name>";
  7566. /// disablewaitingroomevent;
  7567. BUILDIN_FUNC(disablewaitingroomevent)
  7568. {
  7569. struct npc_data *nd;
  7570. struct chat_data *cd;
  7571. if( script_hasdata(st,2) )
  7572. nd = npc_name2id(script_getstr(st, 2));
  7573. else
  7574. nd = (struct npc_data *)map_id2bl(st->oid);
  7575. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7576. chat_disableevent(cd);
  7577. return 0;
  7578. }
  7579. /// Returns info on the waiting room of the current or target npc.
  7580. /// Returns -1 if the type unknown
  7581. /// <type>=0 : current number of users
  7582. /// <type>=1 : maximum number of users allowed
  7583. /// <type>=2 : the number of users that trigger the event
  7584. /// <type>=3 : if the trigger is disabled
  7585. /// <type>=4 : the title of the waiting room
  7586. /// <type>=5 : the password of the waiting room
  7587. /// <type>=16 : the name of the waiting room event
  7588. /// <type>=32 : if the waiting room is full
  7589. /// <type>=33 : if there are enough users to trigger the event
  7590. ///
  7591. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7592. /// getwaitingroomstate(<type>) -> <info>
  7593. BUILDIN_FUNC(getwaitingroomstate)
  7594. {
  7595. struct npc_data *nd;
  7596. struct chat_data *cd;
  7597. int type;
  7598. type = script_getnum(st,2);
  7599. if( script_hasdata(st,3) )
  7600. nd = npc_name2id(script_getstr(st, 3));
  7601. else
  7602. nd = (struct npc_data *)map_id2bl(st->oid);
  7603. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7604. {
  7605. script_pushint(st, -1);
  7606. return 0;
  7607. }
  7608. switch(type)
  7609. {
  7610. case 0: script_pushint(st, cd->users); break;
  7611. case 1: script_pushint(st, cd->limit); break;
  7612. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7613. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7614. case 4: script_pushstrcopy(st, cd->title); break;
  7615. case 5: script_pushstrcopy(st, cd->pass); break;
  7616. case 16: script_pushstrcopy(st, cd->npc_event);break;
  7617. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7618. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7619. default: script_pushint(st, -1); break;
  7620. }
  7621. return 0;
  7622. }
  7623. /// Warps the trigger or target amount of players to the target map and position.
  7624. /// Players are automatically removed from the waiting room.
  7625. /// Those waiting the longest will get warped first.
  7626. /// The target map can be "Random" for a random position in the current map,
  7627. /// and "SavePoint" for the savepoint map+position.
  7628. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7629. ///
  7630. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7631. /// The total number of teleported players is put into $@warpwaitingpcnum
  7632. ///
  7633. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7634. /// warpwaitingpc "<map name>",<x>,<y>;
  7635. BUILDIN_FUNC(warpwaitingpc)
  7636. {
  7637. int x;
  7638. int y;
  7639. int i;
  7640. int n;
  7641. const char* map_name;
  7642. struct npc_data* nd;
  7643. struct chat_data* cd;
  7644. TBL_PC* sd;
  7645. nd = (struct npc_data *)map_id2bl(st->oid);
  7646. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7647. return 0;
  7648. map_name = script_getstr(st,2);
  7649. x = script_getnum(st,3);
  7650. y = script_getnum(st,4);
  7651. n = cd->trigger&0x7f;
  7652. if( script_hasdata(st,5) )
  7653. n = script_getnum(st,5);
  7654. for( i = 0; i < n && cd->users > 0; i++ )
  7655. {
  7656. sd = cd->usersd[0];
  7657. if( sd == NULL )
  7658. {
  7659. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7660. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  7661. continue;// Broken npc chat room?
  7662. }
  7663. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  7664. if( strcmp(map_name,"Random") == 0 )
  7665. pc_randomwarp(sd,3);
  7666. else if( strcmp(map_name,"SavePoint") == 0 )
  7667. {
  7668. if( map[sd->bl.m].flag.noteleport )
  7669. return 0;// can't teleport on this map
  7670. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  7671. }
  7672. else
  7673. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  7674. }
  7675. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  7676. return 0;
  7677. }
  7678. /////////////////////////////////////////////////////////////////////
  7679. // ...
  7680. //
  7681. /*==========================================
  7682. * RIDのアタッチ
  7683. *------------------------------------------*/
  7684. BUILDIN_FUNC(attachrid)
  7685. {
  7686. st->rid=script_getnum(st,2);
  7687. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7688. return 0;
  7689. }
  7690. /*==========================================
  7691. * RIDのデタッチ
  7692. *------------------------------------------*/
  7693. BUILDIN_FUNC(detachrid)
  7694. {
  7695. st->rid=0;
  7696. return 0;
  7697. }
  7698. /*==========================================
  7699. * 存在チェック
  7700. *------------------------------------------*/
  7701. BUILDIN_FUNC(isloggedin)
  7702. {
  7703. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  7704. if (script_hasdata(st,3) && sd &&
  7705. sd->status.char_id != script_getnum(st,3))
  7706. sd = NULL;
  7707. push_val(st->stack,C_INT,sd!=NULL);
  7708. return 0;
  7709. }
  7710. /*==========================================
  7711. *
  7712. *------------------------------------------*/
  7713. BUILDIN_FUNC(setmapflagnosave)
  7714. {
  7715. int m,x,y;
  7716. unsigned short mapindex;
  7717. const char *str,*str2;
  7718. str=script_getstr(st,2);
  7719. str2=script_getstr(st,3);
  7720. x=script_getnum(st,4);
  7721. y=script_getnum(st,5);
  7722. m = map_mapname2mapid(str);
  7723. mapindex = mapindex_name2id(str2);
  7724. if(m >= 0 && mapindex) {
  7725. map[m].flag.nosave=1;
  7726. map[m].save.map=mapindex;
  7727. map[m].save.x=x;
  7728. map[m].save.y=y;
  7729. }
  7730. return 0;
  7731. }
  7732. BUILDIN_FUNC(getmapflag)
  7733. {
  7734. int m,i;
  7735. const char *str;
  7736. str=script_getstr(st,2);
  7737. i=script_getnum(st,3);
  7738. m = map_mapname2mapid(str);
  7739. if(m >= 0) {
  7740. switch(i) {
  7741. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  7742. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  7743. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  7744. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  7745. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  7746. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  7747. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  7748. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  7749. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  7750. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  7751. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  7752. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  7753. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  7754. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  7755. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  7756. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  7757. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  7758. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  7759. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  7760. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  7761. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  7762. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  7763. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  7764. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  7765. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  7766. case MF_INDOORS: script_pushint(st,map[m].flag.indoors); break;
  7767. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  7768. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  7769. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  7770. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  7771. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  7772. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  7773. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  7774. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  7775. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  7776. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  7777. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  7778. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  7779. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  7780. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  7781. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  7782. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  7783. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  7784. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  7785. }
  7786. }
  7787. return 0;
  7788. }
  7789. BUILDIN_FUNC(setmapflag)
  7790. {
  7791. int m,i;
  7792. const char *str;
  7793. const char *val=NULL;
  7794. str=script_getstr(st,2);
  7795. i=script_getnum(st,3);
  7796. if(script_hasdata(st,4)){
  7797. val=script_getstr(st,4);
  7798. }
  7799. m = map_mapname2mapid(str);
  7800. if(m >= 0) {
  7801. switch(i) {
  7802. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  7803. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  7804. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  7805. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  7806. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  7807. case MF_PVP: map[m].flag.pvp=1; break;
  7808. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  7809. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  7810. case MF_GVG: map[m].flag.gvg=1; break;
  7811. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  7812. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  7813. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  7814. case MF_NOTRADE: map[m].flag.notrade=1; break;
  7815. case MF_NODROP: map[m].flag.nodrop=1; break;
  7816. case MF_NOSKILL: map[m].flag.noskill=1; break;
  7817. case MF_NOWARP: map[m].flag.nowarp=1; break;
  7818. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  7819. case MF_SNOW: map[m].flag.snow=1; break;
  7820. case MF_CLOUDS: map[m].flag.clouds=1; break;
  7821. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  7822. case MF_FOG: map[m].flag.fog=1; break;
  7823. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  7824. case MF_SAKURA: map[m].flag.sakura=1; break;
  7825. case MF_LEAVES: map[m].flag.leaves=1; break;
  7826. case MF_RAIN: map[m].flag.rain=1; break;
  7827. case MF_INDOORS: map[m].flag.indoors=1; break;
  7828. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  7829. case MF_NOGO: map[m].flag.nogo=1; break;
  7830. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  7831. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  7832. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  7833. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  7834. case MF_NORETURN: map[m].flag.noreturn=1; break;
  7835. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  7836. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  7837. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  7838. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  7839. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7840. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7841. case MF_NOVENDING: map[m].flag.novending=1; break;
  7842. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  7843. case MF_NOCHAT: map[m].flag.nochat=1; break;
  7844. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  7845. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  7846. }
  7847. }
  7848. return 0;
  7849. }
  7850. BUILDIN_FUNC(removemapflag)
  7851. {
  7852. int m,i;
  7853. const char *str;
  7854. str=script_getstr(st,2);
  7855. i=script_getnum(st,3);
  7856. m = map_mapname2mapid(str);
  7857. if(m >= 0) {
  7858. switch(i) {
  7859. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  7860. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  7861. case MF_NOSAVE: map[m].flag.nosave=0; break;
  7862. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  7863. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  7864. case MF_PVP: map[m].flag.pvp=0; break;
  7865. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  7866. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  7867. case MF_GVG: map[m].flag.gvg=0; break;
  7868. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  7869. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  7870. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  7871. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  7872. case MF_NOTRADE: map[m].flag.notrade=0; break;
  7873. case MF_NODROP: map[m].flag.nodrop=0; break;
  7874. case MF_NOSKILL: map[m].flag.noskill=0; break;
  7875. case MF_NOWARP: map[m].flag.nowarp=0; break;
  7876. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  7877. case MF_SNOW: map[m].flag.snow=0; break;
  7878. case MF_CLOUDS: map[m].flag.clouds=0; break;
  7879. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  7880. case MF_FOG: map[m].flag.fog=0; break;
  7881. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  7882. case MF_SAKURA: map[m].flag.sakura=0; break;
  7883. case MF_LEAVES: map[m].flag.leaves=0; break;
  7884. case MF_RAIN: map[m].flag.rain=0; break;
  7885. case MF_INDOORS: map[m].flag.indoors=0; break;
  7886. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  7887. case MF_NOGO: map[m].flag.nogo=0; break;
  7888. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  7889. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  7890. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  7891. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  7892. case MF_NORETURN: map[m].flag.noreturn=0; break;
  7893. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  7894. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  7895. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  7896. case MF_NOCOMMAND: map[m].nocommand=0; break;
  7897. case MF_JEXP: map[m].jexp=100; break;
  7898. case MF_BEXP: map[m].bexp=100; break;
  7899. case MF_NOVENDING: map[m].flag.novending=0; break;
  7900. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  7901. case MF_NOCHAT: map[m].flag.nochat=0; break;
  7902. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  7903. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  7904. }
  7905. }
  7906. return 0;
  7907. }
  7908. BUILDIN_FUNC(pvpon)
  7909. {
  7910. int m;
  7911. const char *str;
  7912. TBL_PC* sd = NULL;
  7913. struct s_mapiterator* iter;
  7914. str = script_getstr(st,2);
  7915. m = map_mapname2mapid(str);
  7916. if( m < 0 || map[m].flag.pvp )
  7917. return 0; // nothing to do
  7918. map[m].flag.pvp = 1;
  7919. clif_send0199(m,1);
  7920. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  7921. return 0;
  7922. iter = mapit_getallusers();
  7923. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  7924. {
  7925. if( sd->bl.m != m || sd->pvp_timer != -1 )
  7926. continue; // not applicable
  7927. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  7928. sd->pvp_rank = 0;
  7929. sd->pvp_lastusers = 0;
  7930. sd->pvp_point = 5;
  7931. sd->pvp_won = 0;
  7932. sd->pvp_lost = 0;
  7933. }
  7934. mapit_free(iter);
  7935. return 0;
  7936. }
  7937. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  7938. {
  7939. TBL_PC* sd = (TBL_PC*)bl;
  7940. clif_pvpset(sd, 0, 0, 2);
  7941. if (sd->pvp_timer != -1) {
  7942. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  7943. sd->pvp_timer = INVALID_TIMER;
  7944. }
  7945. return 0;
  7946. }
  7947. BUILDIN_FUNC(pvpoff)
  7948. {
  7949. int m;
  7950. const char *str;
  7951. str=script_getstr(st,2);
  7952. m = map_mapname2mapid(str);
  7953. if(m < 0 || !map[m].flag.pvp)
  7954. return 0; //fixed Lupus
  7955. map[m].flag.pvp = 0;
  7956. clif_send0199(m,0);
  7957. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  7958. return 0;
  7959. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  7960. return 0;
  7961. }
  7962. BUILDIN_FUNC(gvgon)
  7963. {
  7964. int m;
  7965. const char *str;
  7966. str=script_getstr(st,2);
  7967. m = map_mapname2mapid(str);
  7968. if(m >= 0 && !map[m].flag.gvg) {
  7969. map[m].flag.gvg = 1;
  7970. clif_send0199(m,3);
  7971. }
  7972. return 0;
  7973. }
  7974. BUILDIN_FUNC(gvgoff)
  7975. {
  7976. int m;
  7977. const char *str;
  7978. str=script_getstr(st,2);
  7979. m = map_mapname2mapid(str);
  7980. if(m >= 0 && map[m].flag.gvg) {
  7981. map[m].flag.gvg = 0;
  7982. clif_send0199(m,0);
  7983. }
  7984. return 0;
  7985. }
  7986. /*==========================================
  7987. * Shows an emoticon on top of the player/npc
  7988. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  7989. *------------------------------------------*/
  7990. //Optional second parameter added by [Skotlex]
  7991. BUILDIN_FUNC(emotion)
  7992. {
  7993. int type;
  7994. int player=0;
  7995. type=script_getnum(st,2);
  7996. if(type < 0 || type > 100)
  7997. return 0;
  7998. if( script_hasdata(st,3) )
  7999. player=script_getnum(st,3);
  8000. if (player) {
  8001. TBL_PC *sd = script_rid2sd(st);
  8002. if (sd)
  8003. clif_emotion(&sd->bl,type);
  8004. } else
  8005. clif_emotion(map_id2bl(st->oid),type);
  8006. return 0;
  8007. }
  8008. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8009. {
  8010. int m=va_arg(ap,int);
  8011. int g_id=va_arg(ap,int);
  8012. int flag=va_arg(ap,int);
  8013. if(!sd || sd->bl.m != m)
  8014. return 0;
  8015. if(
  8016. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8017. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8018. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8019. )
  8020. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8021. return 1;
  8022. }
  8023. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8024. {
  8025. struct mob_data *md=(struct mob_data *)bl;
  8026. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8027. status_kill(bl);
  8028. return 0;
  8029. }
  8030. BUILDIN_FUNC(maprespawnguildid)
  8031. {
  8032. const char *mapname=script_getstr(st,2);
  8033. int g_id=script_getnum(st,3);
  8034. int flag=script_getnum(st,4);
  8035. int m=map_mapname2mapid(mapname);
  8036. if(m == -1)
  8037. return 0;
  8038. //Catch ALL players (in case some are 'between maps' on execution time)
  8039. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8040. if (flag&4) //Remove script mobs.
  8041. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8042. return 0;
  8043. }
  8044. BUILDIN_FUNC(agitstart)
  8045. {
  8046. if(agit_flag==1) return 0; // Agit already Start.
  8047. agit_flag=1;
  8048. guild_agit_start();
  8049. return 0;
  8050. }
  8051. BUILDIN_FUNC(agitend)
  8052. {
  8053. if(agit_flag==0) return 0; // Agit already End.
  8054. agit_flag=0;
  8055. guild_agit_end();
  8056. return 0;
  8057. }
  8058. /*==========================================
  8059. * Returns whether woe is on or off. // choice script
  8060. *------------------------------------------*/
  8061. BUILDIN_FUNC(agitcheck)
  8062. {
  8063. script_pushint(st,agit_flag);
  8064. return 0;
  8065. }
  8066. /// Sets the guild_id of this npc.
  8067. ///
  8068. /// flagemblem <guild_id>;
  8069. BUILDIN_FUNC(flagemblem)
  8070. {
  8071. TBL_NPC* nd;
  8072. int g_id=script_getnum(st,2);
  8073. if(g_id < 0) return 0;
  8074. nd = (TBL_NPC*)map_id2nd(st->oid);
  8075. if( nd == NULL )
  8076. {
  8077. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8078. }
  8079. else if( nd->subtype != SCRIPT )
  8080. {
  8081. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8082. }
  8083. else
  8084. {
  8085. nd->u.scr.guild_id = g_id;
  8086. clif_guild_emblem_area(&nd->bl);
  8087. }
  8088. return 0;
  8089. }
  8090. BUILDIN_FUNC(getcastlename)
  8091. {
  8092. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8093. struct guild_castle* gc = guild_mapname2gc(mapname);
  8094. const char* name = (gc) ? gc->castle_name : "";
  8095. script_pushstrcopy(st,name);
  8096. return 0;
  8097. }
  8098. BUILDIN_FUNC(getcastledata)
  8099. {
  8100. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8101. int index = script_getnum(st,3);
  8102. struct guild_castle* gc = guild_mapname2gc(mapname);
  8103. if(script_hasdata(st,4) && index==0 && gc) {
  8104. const char* event = script_getstr(st,4);
  8105. check_event(st, event);
  8106. guild_addcastleinfoevent(gc->castle_id,17,event);
  8107. }
  8108. if(gc){
  8109. switch(index){
  8110. case 0: {
  8111. int i;
  8112. for(i=1;i<18;i++) // Initialize[AgitInit]
  8113. guild_castledataload(gc->castle_id,i);
  8114. } break;
  8115. case 1:
  8116. script_pushint(st,gc->guild_id); break;
  8117. case 2:
  8118. script_pushint(st,gc->economy); break;
  8119. case 3:
  8120. script_pushint(st,gc->defense); break;
  8121. case 4:
  8122. script_pushint(st,gc->triggerE); break;
  8123. case 5:
  8124. script_pushint(st,gc->triggerD); break;
  8125. case 6:
  8126. script_pushint(st,gc->nextTime); break;
  8127. case 7:
  8128. script_pushint(st,gc->payTime); break;
  8129. case 8:
  8130. script_pushint(st,gc->createTime); break;
  8131. case 9:
  8132. script_pushint(st,gc->visibleC); break;
  8133. case 10:
  8134. case 11:
  8135. case 12:
  8136. case 13:
  8137. case 14:
  8138. case 15:
  8139. case 16:
  8140. case 17:
  8141. script_pushint(st,gc->guardian[index-10].visible); break;
  8142. default:
  8143. script_pushint(st,0); break;
  8144. }
  8145. return 0;
  8146. }
  8147. script_pushint(st,0);
  8148. return 0;
  8149. }
  8150. BUILDIN_FUNC(setcastledata)
  8151. {
  8152. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8153. int index = script_getnum(st,3);
  8154. int value = script_getnum(st,4);
  8155. struct guild_castle* gc = guild_mapname2gc(mapname);
  8156. if(gc) {
  8157. // Save Data byself First
  8158. switch(index){
  8159. case 1:
  8160. gc->guild_id = value; break;
  8161. case 2:
  8162. gc->economy = value; break;
  8163. case 3:
  8164. gc->defense = value; break;
  8165. case 4:
  8166. gc->triggerE = value; break;
  8167. case 5:
  8168. gc->triggerD = value; break;
  8169. case 6:
  8170. gc->nextTime = value; break;
  8171. case 7:
  8172. gc->payTime = value; break;
  8173. case 8:
  8174. gc->createTime = value; break;
  8175. case 9:
  8176. gc->visibleC = value; break;
  8177. case 10:
  8178. case 11:
  8179. case 12:
  8180. case 13:
  8181. case 14:
  8182. case 15:
  8183. case 16:
  8184. case 17:
  8185. gc->guardian[index-10].visible = value; break;
  8186. default:
  8187. return 0;
  8188. }
  8189. guild_castledatasave(gc->castle_id,index,value);
  8190. }
  8191. return 0;
  8192. }
  8193. /* =====================================================================
  8194. * ギルド情報を要求する
  8195. * ---------------------------------------------------------------------*/
  8196. BUILDIN_FUNC(requestguildinfo)
  8197. {
  8198. int guild_id=script_getnum(st,2);
  8199. const char *event=NULL;
  8200. if( script_hasdata(st,3) ){
  8201. event=script_getstr(st,3);
  8202. check_event(st, event);
  8203. }
  8204. if(guild_id>0)
  8205. guild_npc_request_info(guild_id,event);
  8206. return 0;
  8207. }
  8208. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8209. /// getequipcardcnt(<equipment slot>);
  8210. BUILDIN_FUNC(getequipcardcnt)
  8211. {
  8212. int i=-1,j,num;
  8213. TBL_PC *sd;
  8214. int count;
  8215. num=script_getnum(st,2);
  8216. sd=script_rid2sd(st);
  8217. if (num > 0 && num <= ARRAYLENGTH(equip))
  8218. i=pc_checkequip(sd,equip[num-1]);
  8219. if (i < 0) {
  8220. script_pushint(st,0);
  8221. return 0;
  8222. }
  8223. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8224. {
  8225. script_pushint(st,0);
  8226. return 0;
  8227. }
  8228. count = 0;
  8229. for( j = 0; j < MAX_SLOTS; j++ )
  8230. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8231. count++;
  8232. script_pushint(st,count);
  8233. return 0;
  8234. }
  8235. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8236. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8237. /// successremovecards <slot>;
  8238. BUILDIN_FUNC(successremovecards)
  8239. {
  8240. int i=-1,j,c,cardflag=0;
  8241. TBL_PC* sd = script_rid2sd(st);
  8242. int num = script_getnum(st,2);
  8243. if (num > 0 && num <= ARRAYLENGTH(equip))
  8244. i=pc_checkequip(sd,equip[num-1]);
  8245. if (i < 0) {
  8246. return 0;
  8247. }
  8248. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8249. return 0;
  8250. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8251. {
  8252. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8253. {// extract this card from the item
  8254. int flag;
  8255. struct item item_tmp;
  8256. cardflag = 1;
  8257. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8258. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8259. item_tmp.attribute=0;
  8260. for (j = 0; j < MAX_SLOTS; j++)
  8261. item_tmp.card[j]=0;
  8262. //Logs items, got from (N)PC scripts [Lupus]
  8263. if(log_config.enable_logs&0x40)
  8264. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8265. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8266. clif_additem(sd,0,0,flag);
  8267. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8268. }
  8269. }
  8270. }
  8271. if(cardflag == 1)
  8272. { // カードを取り除いたアイテム所得
  8273. int flag;
  8274. struct item item_tmp;
  8275. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8276. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8277. item_tmp.attribute=sd->status.inventory[i].attribute;
  8278. for (j = 0; j < MAX_SLOTS; j++)
  8279. item_tmp.card[j]=0;
  8280. //Logs items, got from (N)PC scripts [Lupus]
  8281. if(log_config.enable_logs&0x40)
  8282. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8283. pc_delitem(sd,i,1,0);
  8284. //Logs items, got from (N)PC scripts [Lupus]
  8285. if(log_config.enable_logs&0x40)
  8286. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8287. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8288. clif_additem(sd,0,0,flag);
  8289. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8290. }
  8291. clif_misceffect(&sd->bl,3);
  8292. }
  8293. return 0;
  8294. }
  8295. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8296. /// failedremovecards <slot>, <type>;
  8297. /// <type>=0 : will destroy both the item and the cards.
  8298. /// <type>=1 : will keep the item, but destroy the cards.
  8299. /// <type>=2 : will keep the cards, but destroy the item.
  8300. /// <type>=? : will just display the failure effect.
  8301. BUILDIN_FUNC(failedremovecards)
  8302. {
  8303. int i=-1,j,c,cardflag=0;
  8304. TBL_PC* sd = script_rid2sd(st);
  8305. int num = script_getnum(st,2);
  8306. int typefail = script_getnum(st,3);
  8307. if (num > 0 && num <= ARRAYLENGTH(equip))
  8308. i=pc_checkequip(sd,equip[num-1]);
  8309. if (i < 0)
  8310. return 0;
  8311. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8312. return 0;
  8313. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8314. {
  8315. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8316. {
  8317. cardflag = 1;
  8318. if(typefail == 2)
  8319. {// add cards to inventory, clear
  8320. int flag;
  8321. struct item item_tmp;
  8322. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8323. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8324. item_tmp.attribute=0;
  8325. for (j = 0; j < MAX_SLOTS; j++)
  8326. item_tmp.card[j]=0;
  8327. //Logs items, got from (N)PC scripts [Lupus]
  8328. if(log_config.enable_logs&0x40)
  8329. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8330. if((flag=pc_additem(sd,&item_tmp,1))){
  8331. clif_additem(sd,0,0,flag);
  8332. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8333. }
  8334. }
  8335. }
  8336. }
  8337. if(cardflag == 1)
  8338. {
  8339. if(typefail == 0 || typefail == 2){ // 武具損失
  8340. //Logs items, got from (N)PC scripts [Lupus]
  8341. if(log_config.enable_logs&0x40)
  8342. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8343. pc_delitem(sd,i,1,0);
  8344. }
  8345. if(typefail == 1){ // カードのみ損失(武具を返す)
  8346. int flag;
  8347. struct item item_tmp;
  8348. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8349. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8350. item_tmp.attribute=sd->status.inventory[i].attribute;
  8351. //Logs items, got from (N)PC scripts [Lupus]
  8352. if(log_config.enable_logs&0x40)
  8353. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8354. for (j = 0; j < MAX_SLOTS; j++)
  8355. item_tmp.card[j]=0;
  8356. pc_delitem(sd,i,1,0);
  8357. //Logs items, got from (N)PC scripts [Lupus]
  8358. if(log_config.enable_logs&0x40)
  8359. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8360. if((flag=pc_additem(sd,&item_tmp,1))){
  8361. clif_additem(sd,0,0,flag);
  8362. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8363. }
  8364. }
  8365. clif_misceffect(&sd->bl,2);
  8366. }
  8367. return 0;
  8368. }
  8369. /* ================================================================
  8370. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8371. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8372. * improved by [Lance]
  8373. * ================================================================*/
  8374. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8375. {
  8376. int x,y,m,check_val=0,check_ID=0,i=0;
  8377. struct guild *g = NULL;
  8378. struct party_data *p = NULL;
  8379. const char *str;
  8380. const char *mapname;
  8381. unsigned int index;
  8382. mapname=script_getstr(st,2);
  8383. str=script_getstr(st,3);
  8384. x=script_getnum(st,4);
  8385. y=script_getnum(st,5);
  8386. if(script_hasdata(st,7)){
  8387. check_val=script_getnum(st,6);
  8388. check_ID=script_getnum(st,7);
  8389. }
  8390. if((m=map_mapname2mapid(mapname))< 0)
  8391. return 0;
  8392. if(!(index=mapindex_name2id(str)))
  8393. return 0;
  8394. switch(check_val){
  8395. case 1:
  8396. g = guild_search(check_ID);
  8397. if (g){
  8398. for( i=0; i < g->max_member; i++)
  8399. {
  8400. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8401. pc_setpos(g->member[i].sd,index,x,y,3);
  8402. }
  8403. }
  8404. }
  8405. break;
  8406. case 2:
  8407. p = party_search(check_ID);
  8408. if(p){
  8409. for(i=0;i<MAX_PARTY; i++){
  8410. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8411. pc_setpos(p->data[i].sd,index,x,y,3);
  8412. }
  8413. }
  8414. }
  8415. break;
  8416. default:
  8417. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8418. break;
  8419. }
  8420. return 0;
  8421. }
  8422. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8423. {
  8424. char *event=va_arg(ap,char *);
  8425. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8426. return 1;
  8427. return 0;
  8428. }
  8429. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8430. {
  8431. const char *mapname,*event;
  8432. int m;
  8433. mapname=script_getstr(st,2);
  8434. event=script_getstr(st,3);
  8435. check_event(st, event);
  8436. if( (m=map_mapname2mapid(mapname))<0 ) {
  8437. script_pushint(st,-1);
  8438. return 0;
  8439. }
  8440. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8441. return 0;
  8442. }
  8443. BUILDIN_FUNC(marriage)
  8444. {
  8445. const char *partner=script_getstr(st,2);
  8446. TBL_PC *sd=script_rid2sd(st);
  8447. TBL_PC *p_sd=map_nick2sd(partner);
  8448. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8449. script_pushint(st,0);
  8450. return 0;
  8451. }
  8452. script_pushint(st,1);
  8453. return 0;
  8454. }
  8455. BUILDIN_FUNC(wedding_effect)
  8456. {
  8457. TBL_PC *sd=script_rid2sd(st);
  8458. struct block_list *bl;
  8459. if(sd==NULL) {
  8460. bl=map_id2bl(st->oid);
  8461. } else
  8462. bl=&sd->bl;
  8463. clif_wedding_effect(bl);
  8464. return 0;
  8465. }
  8466. BUILDIN_FUNC(divorce)
  8467. {
  8468. TBL_PC *sd=script_rid2sd(st);
  8469. if(sd==NULL || pc_divorce(sd) < 0){
  8470. script_pushint(st,0);
  8471. return 0;
  8472. }
  8473. script_pushint(st,1);
  8474. return 0;
  8475. }
  8476. BUILDIN_FUNC(ispartneron)
  8477. {
  8478. TBL_PC *sd=script_rid2sd(st);
  8479. TBL_PC *p_sd=NULL;
  8480. if(sd==NULL || !pc_ismarried(sd) ||
  8481. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8482. script_pushint(st,0);
  8483. return 0;
  8484. }
  8485. script_pushint(st,1);
  8486. return 0;
  8487. }
  8488. BUILDIN_FUNC(getpartnerid)
  8489. {
  8490. TBL_PC *sd=script_rid2sd(st);
  8491. if (sd == NULL) {
  8492. script_pushint(st,0);
  8493. return 0;
  8494. }
  8495. script_pushint(st,sd->status.partner_id);
  8496. return 0;
  8497. }
  8498. BUILDIN_FUNC(getchildid)
  8499. {
  8500. TBL_PC *sd=script_rid2sd(st);
  8501. if (sd == NULL) {
  8502. script_pushint(st,0);
  8503. return 0;
  8504. }
  8505. script_pushint(st,sd->status.child);
  8506. return 0;
  8507. }
  8508. BUILDIN_FUNC(getmotherid)
  8509. {
  8510. TBL_PC *sd=script_rid2sd(st);
  8511. if (sd == NULL) {
  8512. script_pushint(st,0);
  8513. return 0;
  8514. }
  8515. script_pushint(st,sd->status.mother);
  8516. return 0;
  8517. }
  8518. BUILDIN_FUNC(getfatherid)
  8519. {
  8520. TBL_PC *sd=script_rid2sd(st);
  8521. if (sd == NULL) {
  8522. script_pushint(st,0);
  8523. return 0;
  8524. }
  8525. script_pushint(st,sd->status.father);
  8526. return 0;
  8527. }
  8528. BUILDIN_FUNC(warppartner)
  8529. {
  8530. int x,y;
  8531. unsigned short mapindex;
  8532. const char *str;
  8533. TBL_PC *sd=script_rid2sd(st);
  8534. TBL_PC *p_sd=NULL;
  8535. if(sd==NULL || !pc_ismarried(sd) ||
  8536. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8537. script_pushint(st,0);
  8538. return 0;
  8539. }
  8540. str=script_getstr(st,2);
  8541. x=script_getnum(st,3);
  8542. y=script_getnum(st,4);
  8543. mapindex = mapindex_name2id(str);
  8544. if (mapindex) {
  8545. pc_setpos(p_sd,mapindex,x,y,0);
  8546. script_pushint(st,1);
  8547. } else
  8548. script_pushint(st,0);
  8549. return 0;
  8550. }
  8551. /*================================================
  8552. * Script for Displaying MOB Information [Valaris]
  8553. *------------------------------------------------*/
  8554. BUILDIN_FUNC(strmobinfo)
  8555. {
  8556. int num=script_getnum(st,2);
  8557. int class_=script_getnum(st,3);
  8558. if(!mobdb_checkid(class_))
  8559. {
  8560. script_pushint(st,0);
  8561. return 0;
  8562. }
  8563. switch (num) {
  8564. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  8565. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  8566. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8567. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8568. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8569. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8570. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8571. default:
  8572. script_pushint(st,0);
  8573. break;
  8574. }
  8575. return 0;
  8576. }
  8577. /*==========================================
  8578. * Summon guardians [Valaris]
  8579. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  8580. *------------------------------------------*/
  8581. BUILDIN_FUNC(guardian)
  8582. {
  8583. int class_=0,x=0,y=0,guardian=0;
  8584. const char *str,*map,*evt="";
  8585. struct script_data *data;
  8586. bool has_index = false;
  8587. map =script_getstr(st,2);
  8588. x =script_getnum(st,3);
  8589. y =script_getnum(st,4);
  8590. str =script_getstr(st,5);
  8591. class_=script_getnum(st,6);
  8592. if( script_hasdata(st,8) )
  8593. {// "<event label>",<guardian index>
  8594. evt=script_getstr(st,7);
  8595. guardian=script_getnum(st,8);
  8596. has_index = true;
  8597. } else if( script_hasdata(st,7) ){
  8598. data=script_getdata(st,7);
  8599. get_val(st,data);
  8600. if( data_isstring(data) )
  8601. {// "<event label>"
  8602. evt=script_getstr(st,7);
  8603. } else if( data_isint(data) )
  8604. {// <guardian index>
  8605. guardian=script_getnum(st,7);
  8606. has_index = true;
  8607. } else {
  8608. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8609. script_reportdata(data);
  8610. return 1;
  8611. }
  8612. }
  8613. check_event(st, evt);
  8614. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  8615. return 0;
  8616. }
  8617. /*==========================================
  8618. * Invisible Walls [Zephyrus]
  8619. *------------------------------------------*/
  8620. BUILDIN_FUNC(setwall)
  8621. {
  8622. const char *map, *name;
  8623. int x, y, m, size, dir;
  8624. bool shootable;
  8625. map = script_getstr(st,2);
  8626. x = script_getnum(st,3);
  8627. y = script_getnum(st,4);
  8628. size = script_getnum(st,5);
  8629. dir = script_getnum(st,6);
  8630. shootable = script_getnum(st,7);
  8631. name = script_getstr(st,8);
  8632. if( (m = map_mapname2mapid(map)) < 0 )
  8633. return 0; // Invalid Map
  8634. map_iwall_set(m, x, y, size, dir, shootable, name);
  8635. return 0;
  8636. }
  8637. BUILDIN_FUNC(delwall)
  8638. {
  8639. const char *name = script_getstr(st,2);
  8640. map_iwall_remove(name);
  8641. return 0;
  8642. }
  8643. /// Retrieves various information about the specified guardian.
  8644. ///
  8645. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  8646. /// type: 0 - whether it is deployed or not
  8647. /// 1 - maximum hp
  8648. /// 2 - current hp
  8649. ///
  8650. BUILDIN_FUNC(guardianinfo)
  8651. {
  8652. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8653. int id = script_getnum(st,3);
  8654. int type = script_getnum(st,4);
  8655. struct guild_castle* gc = guild_mapname2gc(mapname);
  8656. struct mob_data* gd;
  8657. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  8658. {
  8659. script_pushint(st,-1);
  8660. return 0;
  8661. }
  8662. if( type == 0 )
  8663. script_pushint(st, gc->guardian[id].visible);
  8664. else
  8665. if( !gc->guardian[id].visible )
  8666. script_pushint(st,-1);
  8667. else
  8668. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  8669. script_pushint(st,-1);
  8670. else
  8671. {
  8672. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  8673. else if( type == 2 ) script_pushint(st,gd->status.hp);
  8674. else
  8675. script_pushint(st,-1);
  8676. }
  8677. return 0;
  8678. }
  8679. /*==========================================
  8680. * IDからItem名
  8681. *------------------------------------------*/
  8682. BUILDIN_FUNC(getitemname)
  8683. {
  8684. int item_id=0;
  8685. struct item_data *i_data;
  8686. char *item_name;
  8687. struct script_data *data;
  8688. data=script_getdata(st,2);
  8689. get_val(st,data);
  8690. if( data_isstring(data) ){
  8691. const char *name=conv_str(st,data);
  8692. struct item_data *item_data = itemdb_searchname(name);
  8693. if( item_data )
  8694. item_id=item_data->nameid;
  8695. }else
  8696. item_id=conv_num(st,data);
  8697. i_data = itemdb_exists(item_id);
  8698. if (i_data == NULL)
  8699. {
  8700. script_pushconststr(st,"null");
  8701. return 0;
  8702. }
  8703. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8704. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8705. script_pushstr(st,item_name);
  8706. return 0;
  8707. }
  8708. /*==========================================
  8709. * Returns number of slots an item has. [Skotlex]
  8710. *------------------------------------------*/
  8711. BUILDIN_FUNC(getitemslots)
  8712. {
  8713. int item_id;
  8714. struct item_data *i_data;
  8715. item_id=script_getnum(st,2);
  8716. i_data = itemdb_exists(item_id);
  8717. if (i_data)
  8718. script_pushint(st,i_data->slot);
  8719. else
  8720. script_pushint(st,-1);
  8721. return 0;
  8722. }
  8723. /*==========================================
  8724. * Returns some values of an item [Lupus]
  8725. * Price, Weight, etc...
  8726. getiteminfo(itemID,n), where n
  8727. 0 value_buy;
  8728. 1 value_sell;
  8729. 2 type;
  8730. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8731. if = 0, then monsters don't drop it at all (rare or a quest item)
  8732. if = 10000, then this item is sold in NPC shops only
  8733. 4 sex;
  8734. 5 equip;
  8735. 6 weight;
  8736. 7 atk;
  8737. 8 def;
  8738. 9 range;
  8739. 10 slot;
  8740. 11 look;
  8741. 12 elv;
  8742. 13 wlv;
  8743. 14 view id
  8744. *------------------------------------------*/
  8745. BUILDIN_FUNC(getiteminfo)
  8746. {
  8747. int item_id,n;
  8748. int *item_arr;
  8749. struct item_data *i_data;
  8750. item_id = script_getnum(st,2);
  8751. n = script_getnum(st,3);
  8752. i_data = itemdb_exists(item_id);
  8753. if (i_data && n>=0 && n<14) {
  8754. item_arr = (int*)&i_data->value_buy;
  8755. script_pushint(st,item_arr[n]);
  8756. } else
  8757. script_pushint(st,-1);
  8758. return 0;
  8759. }
  8760. /*==========================================
  8761. * Set some values of an item [Lupus]
  8762. * Price, Weight, etc...
  8763. setiteminfo(itemID,n,Value), where n
  8764. 0 value_buy;
  8765. 1 value_sell;
  8766. 2 type;
  8767. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8768. if = 0, then monsters don't drop it at all (rare or a quest item)
  8769. if = 10000, then this item is sold in NPC shops only
  8770. 4 sex;
  8771. 5 equip;
  8772. 6 weight;
  8773. 7 atk;
  8774. 8 def;
  8775. 9 range;
  8776. 10 slot;
  8777. 11 look;
  8778. 12 elv;
  8779. 13 wlv;
  8780. 14 view id
  8781. * Returns Value or -1 if the wrong field's been set
  8782. *------------------------------------------*/
  8783. BUILDIN_FUNC(setiteminfo)
  8784. {
  8785. int item_id,n,value;
  8786. int *item_arr;
  8787. struct item_data *i_data;
  8788. item_id = script_getnum(st,2);
  8789. n = script_getnum(st,3);
  8790. value = script_getnum(st,4);
  8791. i_data = itemdb_exists(item_id);
  8792. if (i_data && n>=0 && n<14) {
  8793. item_arr = (int*)&i_data->value_buy;
  8794. item_arr[n] = value;
  8795. script_pushint(st,value);
  8796. } else
  8797. script_pushint(st,-1);
  8798. return 0;
  8799. }
  8800. /*==========================================
  8801. * Returns value from equipped item slot n [Lupus]
  8802. getequipcardid(num,slot)
  8803. where
  8804. num = eqip position slot
  8805. slot = 0,1,2,3 (Card Slot N)
  8806. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  8807. it's useful when you want to check item cards or if it's signed
  8808. Useful for such quests as "Sign this refined item with players name" etc
  8809. Hat[0] +4 -> Player's Hat[0] +4
  8810. *------------------------------------------*/
  8811. BUILDIN_FUNC(getequipcardid)
  8812. {
  8813. int i=-1,num,slot;
  8814. TBL_PC *sd;
  8815. num=script_getnum(st,2);
  8816. slot=script_getnum(st,3);
  8817. sd=script_rid2sd(st);
  8818. if (num > 0 && num <= ARRAYLENGTH(equip))
  8819. i=pc_checkequip(sd,equip[num-1]);
  8820. if(i >= 0 && slot>=0 && slot<4)
  8821. script_pushint(st,sd->status.inventory[i].card[slot]);
  8822. else
  8823. script_pushint(st,0);
  8824. return 0;
  8825. }
  8826. /*==========================================
  8827. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  8828. *------------------------------------------*/
  8829. BUILDIN_FUNC(petskillbonus)
  8830. {
  8831. struct pet_data *pd;
  8832. TBL_PC *sd=script_rid2sd(st);
  8833. if(sd==NULL || sd->pd==NULL)
  8834. return 0;
  8835. pd=sd->pd;
  8836. if (pd->bonus)
  8837. { //Clear previous bonus
  8838. if (pd->bonus->timer != -1)
  8839. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  8840. } else //init
  8841. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  8842. pd->bonus->type=script_getnum(st,2);
  8843. pd->bonus->val=script_getnum(st,3);
  8844. pd->bonus->duration=script_getnum(st,4);
  8845. pd->bonus->delay=script_getnum(st,5);
  8846. if (pd->state.skillbonus == 1)
  8847. pd->state.skillbonus=0; // waiting state
  8848. // wait for timer to start
  8849. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  8850. pd->bonus->timer = INVALID_TIMER;
  8851. else
  8852. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  8853. return 0;
  8854. }
  8855. /*==========================================
  8856. * pet looting [Valaris] //Rewritten by [Skotlex]
  8857. *------------------------------------------*/
  8858. BUILDIN_FUNC(petloot)
  8859. {
  8860. int max;
  8861. struct pet_data *pd;
  8862. TBL_PC *sd=script_rid2sd(st);
  8863. if(sd==NULL || sd->pd==NULL)
  8864. return 0;
  8865. max=script_getnum(st,2);
  8866. if(max < 1)
  8867. max = 1; //Let'em loot at least 1 item.
  8868. else if (max > MAX_PETLOOT_SIZE)
  8869. max = MAX_PETLOOT_SIZE;
  8870. pd = sd->pd;
  8871. if (pd->loot != NULL)
  8872. { //Release whatever was there already and reallocate memory
  8873. pet_lootitem_drop(pd, pd->msd);
  8874. aFree(pd->loot->item);
  8875. }
  8876. else
  8877. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  8878. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  8879. pd->loot->max=max;
  8880. pd->loot->count = 0;
  8881. pd->loot->weight = 0;
  8882. return 0;
  8883. }
  8884. /*==========================================
  8885. * PCの所持品情報読み取り
  8886. *------------------------------------------*/
  8887. BUILDIN_FUNC(getinventorylist)
  8888. {
  8889. TBL_PC *sd=script_rid2sd(st);
  8890. char card_var[NAME_LENGTH];
  8891. int i,j=0,k;
  8892. if(!sd) return 0;
  8893. for(i=0;i<MAX_INVENTORY;i++){
  8894. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  8895. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  8896. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  8897. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  8898. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  8899. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  8900. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  8901. for (k = 0; k < MAX_SLOTS; k++)
  8902. {
  8903. sprintf(card_var, "@inventorylist_card%d",k+1);
  8904. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  8905. }
  8906. j++;
  8907. }
  8908. }
  8909. pc_setreg(sd,add_str("@inventorylist_count"),j);
  8910. return 0;
  8911. }
  8912. BUILDIN_FUNC(getskilllist)
  8913. {
  8914. TBL_PC *sd=script_rid2sd(st);
  8915. int i,j=0;
  8916. if(!sd) return 0;
  8917. for(i=0;i<MAX_SKILL;i++){
  8918. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  8919. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  8920. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  8921. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  8922. j++;
  8923. }
  8924. }
  8925. pc_setreg(sd,add_str("@skilllist_count"),j);
  8926. return 0;
  8927. }
  8928. BUILDIN_FUNC(clearitem)
  8929. {
  8930. TBL_PC *sd=script_rid2sd(st);
  8931. int i;
  8932. if(sd==NULL) return 0;
  8933. for (i=0; i<MAX_INVENTORY; i++) {
  8934. if (sd->status.inventory[i].amount) {
  8935. //Logs items, got from (N)PC scripts [Lupus]
  8936. if(log_config.enable_logs&0x40)
  8937. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  8938. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  8939. }
  8940. }
  8941. return 0;
  8942. }
  8943. /*==========================================
  8944. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  8945. *------------------------------------------*/
  8946. BUILDIN_FUNC(disguise)
  8947. {
  8948. int id;
  8949. TBL_PC* sd = script_rid2sd(st);
  8950. if (sd == NULL) return 0;
  8951. id = script_getnum(st,2);
  8952. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  8953. pc_disguise(sd, id);
  8954. script_pushint(st,id);
  8955. } else
  8956. script_pushint(st,0);
  8957. return 0;
  8958. }
  8959. /*==========================================
  8960. * Undisguise Player (returns 1 if success, 0 on fail)
  8961. *------------------------------------------*/
  8962. BUILDIN_FUNC(undisguise)
  8963. {
  8964. TBL_PC* sd = script_rid2sd(st);
  8965. if (sd == NULL) return 0;
  8966. if (sd->disguise) {
  8967. pc_disguise(sd, 0);
  8968. script_pushint(st,0);
  8969. } else {
  8970. script_pushint(st,1);
  8971. }
  8972. return 0;
  8973. }
  8974. /*==========================================
  8975. * NPCクラスチェンジ
  8976. * classは変わりたいclass
  8977. * typeは通常0なのかな?
  8978. *------------------------------------------*/
  8979. BUILDIN_FUNC(classchange)
  8980. {
  8981. int _class,type;
  8982. struct block_list *bl=map_id2bl(st->oid);
  8983. if(bl==NULL) return 0;
  8984. _class=script_getnum(st,2);
  8985. type=script_getnum(st,3);
  8986. clif_class_change(bl,_class,type);
  8987. return 0;
  8988. }
  8989. /*==========================================
  8990. * NPCから発生するエフェクト
  8991. *------------------------------------------*/
  8992. BUILDIN_FUNC(misceffect)
  8993. {
  8994. int type;
  8995. type=script_getnum(st,2);
  8996. if(st->oid && st->oid != fake_nd->bl.id) {
  8997. struct block_list *bl = map_id2bl(st->oid);
  8998. if (bl)
  8999. clif_misceffect2(bl,type);
  9000. } else{
  9001. TBL_PC *sd=script_rid2sd(st);
  9002. if(sd)
  9003. clif_misceffect2(&sd->bl,type);
  9004. }
  9005. return 0;
  9006. }
  9007. /*==========================================
  9008. * サウンドエフェクト
  9009. *------------------------------------------*/
  9010. BUILDIN_FUNC(soundeffect)
  9011. {
  9012. TBL_PC* sd = script_rid2sd(st);
  9013. const char* name = script_getstr(st,2);
  9014. int type = script_getnum(st,3);
  9015. if(sd)
  9016. {
  9017. if(!st->rid)
  9018. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9019. else
  9020. clif_soundeffect(sd,&sd->bl,name,type);
  9021. }
  9022. return 0;
  9023. }
  9024. int soundeffect_sub(struct block_list* bl,va_list ap)
  9025. {
  9026. char* name = va_arg(ap,char*);
  9027. int type = va_arg(ap,int);
  9028. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9029. return 0;
  9030. }
  9031. /*==========================================
  9032. * Play a sound effect (.wav) on multiple clients
  9033. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9034. *------------------------------------------*/
  9035. BUILDIN_FUNC(soundeffectall)
  9036. {
  9037. struct block_list* bl;
  9038. const char* name;
  9039. int type;
  9040. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9041. if (!bl)
  9042. return 0;
  9043. name = script_getstr(st,2);
  9044. type = script_getnum(st,3);
  9045. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9046. if(!script_hasdata(st,4))
  9047. { // area around
  9048. clif_soundeffectall(bl, name, type, AREA);
  9049. }
  9050. else
  9051. if(!script_hasdata(st,5))
  9052. { // entire map
  9053. const char* map = script_getstr(st,4);
  9054. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9055. }
  9056. else
  9057. if(script_hasdata(st,8))
  9058. { // specified part of map
  9059. const char* map = script_getstr(st,4);
  9060. int x0 = script_getnum(st,5);
  9061. int y0 = script_getnum(st,6);
  9062. int x1 = script_getnum(st,7);
  9063. int y1 = script_getnum(st,8);
  9064. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9065. }
  9066. else
  9067. {
  9068. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9069. }
  9070. return 0;
  9071. }
  9072. /*==========================================
  9073. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9074. *------------------------------------------*/
  9075. BUILDIN_FUNC(petrecovery)
  9076. {
  9077. struct pet_data *pd;
  9078. TBL_PC *sd=script_rid2sd(st);
  9079. if(sd==NULL || sd->pd==NULL)
  9080. return 0;
  9081. pd=sd->pd;
  9082. if (pd->recovery)
  9083. { //Halt previous bonus
  9084. if (pd->recovery->timer != -1)
  9085. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9086. } else //Init
  9087. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9088. pd->recovery->type = (sc_type)script_getnum(st,2);
  9089. pd->recovery->delay = script_getnum(st,3);
  9090. pd->recovery->timer = INVALID_TIMER;
  9091. return 0;
  9092. }
  9093. /*==========================================
  9094. * pet healing [Valaris] //Rewritten by [Skotlex]
  9095. *------------------------------------------*/
  9096. BUILDIN_FUNC(petheal)
  9097. {
  9098. struct pet_data *pd;
  9099. TBL_PC *sd=script_rid2sd(st);
  9100. if(sd==NULL || sd->pd==NULL)
  9101. return 0;
  9102. pd=sd->pd;
  9103. if (pd->s_skill)
  9104. { //Clear previous skill
  9105. if (pd->s_skill->timer != -1)
  9106. {
  9107. if (pd->s_skill->id)
  9108. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9109. else
  9110. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9111. }
  9112. } else //init memory
  9113. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9114. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9115. //Use the lv as the amount to heal
  9116. pd->s_skill->lv=script_getnum(st,2);
  9117. pd->s_skill->delay=script_getnum(st,3);
  9118. pd->s_skill->hp=script_getnum(st,4);
  9119. pd->s_skill->sp=script_getnum(st,5);
  9120. //Use delay as initial offset to avoid skill/heal exploits
  9121. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9122. pd->s_skill->timer = INVALID_TIMER;
  9123. else
  9124. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9125. return 0;
  9126. }
  9127. /*==========================================
  9128. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9129. *------------------------------------------*/
  9130. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  9131. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  9132. BUILDIN_FUNC(petskillattack)
  9133. {
  9134. struct pet_data *pd;
  9135. TBL_PC *sd=script_rid2sd(st);
  9136. if(sd==NULL || sd->pd==NULL)
  9137. return 0;
  9138. pd=sd->pd;
  9139. if (pd->a_skill == NULL)
  9140. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9141. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9142. pd->a_skill->lv=script_getnum(st,3);
  9143. pd->a_skill->div_ = 0;
  9144. pd->a_skill->rate=script_getnum(st,4);
  9145. pd->a_skill->bonusrate=script_getnum(st,5);
  9146. return 0;
  9147. }
  9148. /*==========================================
  9149. * pet attack skills [Valaris]
  9150. *------------------------------------------*/
  9151. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  9152. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  9153. BUILDIN_FUNC(petskillattack2)
  9154. {
  9155. struct pet_data *pd;
  9156. TBL_PC *sd=script_rid2sd(st);
  9157. if(sd==NULL || sd->pd==NULL)
  9158. return 0;
  9159. pd=sd->pd;
  9160. if (pd->a_skill == NULL)
  9161. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9162. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9163. pd->a_skill->lv=script_getnum(st,3);
  9164. pd->a_skill->div_ = script_getnum(st,4);
  9165. pd->a_skill->rate=script_getnum(st,5);
  9166. pd->a_skill->bonusrate=script_getnum(st,6);
  9167. return 0;
  9168. }
  9169. /*==========================================
  9170. * pet support skills [Skotlex]
  9171. *------------------------------------------*/
  9172. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  9173. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  9174. BUILDIN_FUNC(petskillsupport)
  9175. {
  9176. struct pet_data *pd;
  9177. TBL_PC *sd=script_rid2sd(st);
  9178. if(sd==NULL || sd->pd==NULL)
  9179. return 0;
  9180. pd=sd->pd;
  9181. if (pd->s_skill)
  9182. { //Clear previous skill
  9183. if (pd->s_skill->timer != -1)
  9184. {
  9185. if (pd->s_skill->id)
  9186. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9187. else
  9188. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9189. }
  9190. } else //init memory
  9191. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9192. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9193. pd->s_skill->lv=script_getnum(st,3);
  9194. pd->s_skill->delay=script_getnum(st,4);
  9195. pd->s_skill->hp=script_getnum(st,5);
  9196. pd->s_skill->sp=script_getnum(st,6);
  9197. //Use delay as initial offset to avoid skill/heal exploits
  9198. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9199. pd->s_skill->timer = INVALID_TIMER;
  9200. else
  9201. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9202. return 0;
  9203. }
  9204. /*==========================================
  9205. * Scripted skill effects [Celest]
  9206. *------------------------------------------*/
  9207. /// skilleffect <skill id>,<level>
  9208. /// skilleffect "<skill name>",<level>
  9209. BUILDIN_FUNC(skilleffect)
  9210. {
  9211. TBL_PC *sd;
  9212. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9213. int skilllv=script_getnum(st,3);
  9214. sd=script_rid2sd(st);
  9215. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9216. return 0;
  9217. }
  9218. /*==========================================
  9219. * NPC skill effects [Valaris]
  9220. *------------------------------------------*/
  9221. /// npcskilleffect <skill id>,<level>,<x>,<y>
  9222. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  9223. BUILDIN_FUNC(npcskilleffect)
  9224. {
  9225. struct block_list *bl= map_id2bl(st->oid);
  9226. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9227. int skilllv=script_getnum(st,3);
  9228. int x=script_getnum(st,4);
  9229. int y=script_getnum(st,5);
  9230. if (bl)
  9231. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9232. return 0;
  9233. }
  9234. /*==========================================
  9235. * Special effects [Valaris]
  9236. *------------------------------------------*/
  9237. BUILDIN_FUNC(specialeffect)
  9238. {
  9239. struct block_list *bl=map_id2bl(st->oid);
  9240. int type = script_getnum(st,2);
  9241. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9242. if(bl==NULL)
  9243. return 0;
  9244. clif_specialeffect(bl, type, target);
  9245. return 0;
  9246. }
  9247. BUILDIN_FUNC(specialeffect2)
  9248. {
  9249. TBL_PC *sd=script_rid2sd(st);
  9250. int type = script_getnum(st,2);
  9251. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9252. if(sd==NULL)
  9253. return 0;
  9254. clif_specialeffect(&sd->bl, type, target);
  9255. return 0;
  9256. }
  9257. /*==========================================
  9258. * Nude [Valaris]
  9259. *------------------------------------------*/
  9260. BUILDIN_FUNC(nude)
  9261. {
  9262. TBL_PC *sd=script_rid2sd(st);
  9263. int i,calcflag=0;
  9264. if(sd==NULL)
  9265. return 0;
  9266. for(i=0;i<11;i++)
  9267. if(sd->equip_index[i] >= 0) {
  9268. if(!calcflag)
  9269. calcflag=1;
  9270. pc_unequipitem(sd,sd->equip_index[i],2);
  9271. }
  9272. if(calcflag)
  9273. status_calc_pc(sd,0);
  9274. return 0;
  9275. }
  9276. /*==========================================
  9277. * gmcommand [MouseJstr]
  9278. *
  9279. * suggested on the forums...
  9280. * splitted into atcommand & charcommand by [Skotlex]
  9281. *------------------------------------------*/
  9282. BUILDIN_FUNC(atcommand)
  9283. {
  9284. TBL_PC dummy_sd;
  9285. TBL_PC* sd;
  9286. int fd;
  9287. const char* cmd;
  9288. cmd = script_getstr(st,2);
  9289. if (st->rid) {
  9290. sd = script_rid2sd(st);
  9291. fd = sd->fd;
  9292. } else { //Use a dummy character.
  9293. sd = &dummy_sd;
  9294. fd = 0;
  9295. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9296. if (st->oid)
  9297. {
  9298. struct block_list* bl = map_id2bl(st->oid);
  9299. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9300. if (bl->type == BL_NPC)
  9301. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9302. }
  9303. }
  9304. // compatibility with previous implementation (deprecated!)
  9305. if(cmd[0] != atcommand_symbol)
  9306. {
  9307. cmd += strlen(sd->status.name);
  9308. while(*cmd != atcommand_symbol && *cmd != 0)
  9309. cmd++;
  9310. }
  9311. is_atcommand_sub(fd, sd, cmd, 99);
  9312. return 0;
  9313. }
  9314. BUILDIN_FUNC(charcommand)
  9315. {
  9316. TBL_PC dummy_sd;
  9317. TBL_PC* sd;
  9318. int fd;
  9319. const char* cmd;
  9320. cmd = script_getstr(st,2);
  9321. if (st->rid) {
  9322. sd = script_rid2sd(st);
  9323. fd = sd->fd;
  9324. } else { //Use a dummy character.
  9325. sd = &dummy_sd;
  9326. fd = 0;
  9327. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9328. if (st->oid)
  9329. {
  9330. struct block_list* bl = map_id2bl(st->oid);
  9331. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9332. if (bl->type == BL_NPC)
  9333. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9334. }
  9335. }
  9336. // compatibility with previous implementation (deprecated!)
  9337. if(cmd[0] != charcommand_symbol)
  9338. {
  9339. cmd += strlen(sd->status.name);
  9340. while(*cmd != charcommand_symbol && *cmd != 0)
  9341. cmd++;
  9342. }
  9343. is_charcommand_sub(fd, sd, cmd, 99);
  9344. return 0;
  9345. }
  9346. /*==========================================
  9347. * Displays a message for the player only (like system messages like "you got an apple" )
  9348. *------------------------------------------*/
  9349. BUILDIN_FUNC(dispbottom)
  9350. {
  9351. TBL_PC *sd=script_rid2sd(st);
  9352. const char *message;
  9353. message=script_getstr(st,2);
  9354. if(sd)
  9355. clif_disp_onlyself(sd,message,(int)strlen(message));
  9356. return 0;
  9357. }
  9358. /*==========================================
  9359. * All The Players Full Recovery
  9360. * (HP/SP full restore and resurrect if need)
  9361. *------------------------------------------*/
  9362. BUILDIN_FUNC(recovery)
  9363. {
  9364. TBL_PC* sd;
  9365. struct s_mapiterator* iter;
  9366. iter = mapit_getallusers();
  9367. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9368. {
  9369. if(pc_isdead(sd))
  9370. status_revive(&sd->bl, 100, 100);
  9371. else
  9372. status_percent_heal(&sd->bl, 100, 100);
  9373. clif_displaymessage(sd->fd,"You have been recovered!");
  9374. }
  9375. mapit_free(iter);
  9376. return 0;
  9377. }
  9378. /*==========================================
  9379. * Get your pet info: getpetinfo(n)
  9380. * n -> 0:pet_id 1:pet_class 2:pet_name
  9381. * 3:friendly 4:hungry, 5: rename flag.
  9382. *------------------------------------------*/
  9383. BUILDIN_FUNC(getpetinfo)
  9384. {
  9385. TBL_PC *sd=script_rid2sd(st);
  9386. TBL_PET *pd;
  9387. int type=script_getnum(st,2);
  9388. if(!sd || !sd->pd) {
  9389. if (type == 2)
  9390. script_pushconststr(st,"null");
  9391. else
  9392. script_pushint(st,0);
  9393. return 0;
  9394. }
  9395. pd = sd->pd;
  9396. switch(type){
  9397. case 0: script_pushint(st,pd->pet.pet_id); break;
  9398. case 1: script_pushint(st,pd->pet.class_); break;
  9399. case 2: script_pushstrcopy(st,pd->pet.name); break;
  9400. case 3: script_pushint(st,pd->pet.intimate); break;
  9401. case 4: script_pushint(st,pd->pet.hungry); break;
  9402. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9403. default:
  9404. script_pushint(st,0);
  9405. break;
  9406. }
  9407. return 0;
  9408. }
  9409. /*==========================================
  9410. * Get your homunculus info: gethominfo(n)
  9411. * n -> 0:hom_id 1:class 2:name
  9412. * 3:friendly 4:hungry, 5: rename flag.
  9413. * 6: level
  9414. *------------------------------------------*/
  9415. BUILDIN_FUNC(gethominfo)
  9416. {
  9417. TBL_PC *sd=script_rid2sd(st);
  9418. TBL_HOM *hd;
  9419. int type=script_getnum(st,2);
  9420. hd = sd?sd->hd:NULL;
  9421. if(!merc_is_hom_active(hd))
  9422. {
  9423. if (type == 2)
  9424. script_pushconststr(st,"null");
  9425. else
  9426. script_pushint(st,0);
  9427. return 0;
  9428. }
  9429. switch(type){
  9430. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9431. case 1: script_pushint(st,hd->homunculus.class_); break;
  9432. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  9433. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9434. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9435. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9436. case 6: script_pushint(st,hd->homunculus.level); break;
  9437. default:
  9438. script_pushint(st,0);
  9439. break;
  9440. }
  9441. return 0;
  9442. }
  9443. /*==========================================
  9444. * Shows wether your inventory(and equips) contain
  9445. selected card or not.
  9446. checkequipedcard(4001);
  9447. *------------------------------------------*/
  9448. BUILDIN_FUNC(checkequipedcard)
  9449. {
  9450. TBL_PC *sd=script_rid2sd(st);
  9451. int n,i,c=0;
  9452. c=script_getnum(st,2);
  9453. if(sd){
  9454. for(i=0;i<MAX_INVENTORY;i++){
  9455. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9456. for(n=0;n<MAX_SLOTS;n++){
  9457. if(sd->status.inventory[i].card[n]==c){
  9458. script_pushint(st,1);
  9459. return 0;
  9460. }
  9461. }
  9462. }
  9463. }
  9464. }
  9465. script_pushint(st,0);
  9466. return 0;
  9467. }
  9468. BUILDIN_FUNC(jump_zero)
  9469. {
  9470. int sel;
  9471. sel=script_getnum(st,2);
  9472. if(!sel) {
  9473. int pos;
  9474. if( !data_islabel(script_getdata(st,3)) ){
  9475. ShowError("script: jump_zero: not label !\n");
  9476. st->state=END;
  9477. return 1;
  9478. }
  9479. pos=script_getnum(st,3);
  9480. st->pos=pos;
  9481. st->state=GOTO;
  9482. // printf("script: jump_zero: jumpto : %d\n",pos);
  9483. } else {
  9484. // printf("script: jump_zero: fail\n");
  9485. }
  9486. return 0;
  9487. }
  9488. /*==========================================
  9489. * GetMapMobs
  9490. returns mob counts on a set map:
  9491. e.g. GetMapMobs("prontera")
  9492. use "this" - for player's map
  9493. *------------------------------------------*/
  9494. BUILDIN_FUNC(getmapmobs)
  9495. {
  9496. const char *str=NULL;
  9497. int m=-1,bx,by;
  9498. int count=0;
  9499. struct block_list *bl;
  9500. str=script_getstr(st,2);
  9501. if(strcmp(str,"this")==0){
  9502. TBL_PC *sd=script_rid2sd(st);
  9503. if(sd)
  9504. m=sd->bl.m;
  9505. else{
  9506. script_pushint(st,-1);
  9507. return 0;
  9508. }
  9509. }else
  9510. m=map_mapname2mapid(str);
  9511. if(m < 0){
  9512. script_pushint(st,-1);
  9513. return 0;
  9514. }
  9515. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  9516. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  9517. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  9518. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9519. count++;
  9520. script_pushint(st,count);
  9521. return 0;
  9522. }
  9523. /*==========================================
  9524. * movenpc [MouseJstr]
  9525. *------------------------------------------*/
  9526. BUILDIN_FUNC(movenpc)
  9527. {
  9528. TBL_NPC *nd = NULL;
  9529. const char *npc;
  9530. int x,y;
  9531. npc = script_getstr(st,2);
  9532. x = script_getnum(st,3);
  9533. y = script_getnum(st,4);
  9534. if ((nd = npc_name2id(npc)) == NULL)
  9535. return -1;
  9536. npc_movenpc(nd, x, y);
  9537. return 0;
  9538. }
  9539. /*==========================================
  9540. * message [MouseJstr]
  9541. *------------------------------------------*/
  9542. BUILDIN_FUNC(message)
  9543. {
  9544. TBL_PC *sd;
  9545. const char *msg,*player;
  9546. TBL_PC *pl_sd = NULL;
  9547. sd = script_rid2sd(st);
  9548. player = script_getstr(st,2);
  9549. msg = script_getstr(st,3);
  9550. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9551. return 0;
  9552. clif_displaymessage(pl_sd->fd, msg);
  9553. return 0;
  9554. }
  9555. /*==========================================
  9556. * npctalk (sends message to surrounding area)
  9557. *------------------------------------------*/
  9558. BUILDIN_FUNC(npctalk)
  9559. {
  9560. const char* str;
  9561. char message[255];
  9562. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9563. str = script_getstr(st,2);
  9564. if(nd) {
  9565. memcpy(message, nd->name, NAME_LENGTH);
  9566. strtok(message, "#"); // discard extra name identifier if present
  9567. strcat(message, " : ");
  9568. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9569. clif_message(&(nd->bl), message);
  9570. }
  9571. return 0;
  9572. }
  9573. // change npc walkspeed [Valaris]
  9574. BUILDIN_FUNC(npcspeed)
  9575. {
  9576. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9577. int x=0;
  9578. x=script_getnum(st,2);
  9579. if(nd) {
  9580. nd->speed=x;
  9581. }
  9582. return 0;
  9583. }
  9584. // make an npc walk to a position [Valaris]
  9585. BUILDIN_FUNC(npcwalkto)
  9586. {
  9587. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9588. int x=0,y=0;
  9589. x=script_getnum(st,2);
  9590. y=script_getnum(st,3);
  9591. if(nd) {
  9592. unit_walktoxy(&nd->bl,x,y,0);
  9593. }
  9594. return 0;
  9595. }
  9596. // stop an npc's movement [Valaris]
  9597. BUILDIN_FUNC(npcstop)
  9598. {
  9599. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9600. if(nd) {
  9601. unit_stop_walking(&nd->bl,1);
  9602. }
  9603. return 0;
  9604. }
  9605. /*==========================================
  9606. * getlook char info. getlook(arg)
  9607. *------------------------------------------*/
  9608. BUILDIN_FUNC(getlook)
  9609. {
  9610. int type,val;
  9611. TBL_PC *sd;
  9612. sd=script_rid2sd(st);
  9613. type=script_getnum(st,2);
  9614. val=-1;
  9615. switch(type) {
  9616. case LOOK_HAIR: val=sd->status.hair; break; //1
  9617. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  9618. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  9619. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  9620. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  9621. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  9622. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  9623. case LOOK_SHIELD: val=sd->status.shield; break; //8
  9624. case LOOK_SHOES: break; //9
  9625. }
  9626. script_pushint(st,val);
  9627. return 0;
  9628. }
  9629. /*==========================================
  9630. * get char save point. argument: 0- map name, 1- x, 2- y
  9631. *------------------------------------------*/
  9632. BUILDIN_FUNC(getsavepoint)
  9633. {
  9634. TBL_PC* sd;
  9635. int type;
  9636. sd = script_rid2sd(st);
  9637. if (sd == NULL) {
  9638. script_pushint(st,0);
  9639. return 0;
  9640. }
  9641. type = script_getnum(st,2);
  9642. switch(type) {
  9643. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  9644. case 1: script_pushint(st,sd->status.save_point.x); break;
  9645. case 2: script_pushint(st,sd->status.save_point.y); break;
  9646. default:
  9647. script_pushint(st,0);
  9648. break;
  9649. }
  9650. return 0;
  9651. }
  9652. /*==========================================
  9653. * Get position for char/npc/pet/mob objects. Added by Lorky
  9654. *
  9655. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  9656. * where type:
  9657. * MapName$ - String variable for output map name
  9658. * MapX - Integer variable for output coord X
  9659. * MapY - Integer variable for output coord Y
  9660. * type - type of object
  9661. * 0 - Character coord
  9662. * 1 - NPC coord
  9663. * 2 - Pet coord
  9664. * 3 - Mob coord (not released)
  9665. * 4 - Homun coord
  9666. * CharName$ - Name object. If miss or "this" the current object
  9667. *
  9668. * Return:
  9669. * 0 - success
  9670. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  9671. *------------------------------------------*/
  9672. BUILDIN_FUNC(getmapxy)
  9673. {
  9674. struct block_list *bl = NULL;
  9675. TBL_PC *sd=NULL;
  9676. int num;
  9677. const char *name;
  9678. char prefix;
  9679. int x,y,type;
  9680. char mapname[MAP_NAME_LENGTH];
  9681. if( !data_isreference(script_getdata(st,2)) ){
  9682. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  9683. script_pushint(st,-1);
  9684. return 1;
  9685. }
  9686. if( !data_isreference(script_getdata(st,3)) ){
  9687. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  9688. script_pushint(st,-1);
  9689. return 1;
  9690. }
  9691. if( !data_isreference(script_getdata(st,4)) ){
  9692. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  9693. script_pushint(st,-1);
  9694. return 1;
  9695. }
  9696. // Possible needly check function parameters on C_STR,C_INT,C_INT
  9697. type=script_getnum(st,5);
  9698. switch (type){
  9699. case 0: //Get Character Position
  9700. if( script_hasdata(st,6) )
  9701. sd=map_nick2sd(script_getstr(st,6));
  9702. else
  9703. sd=script_rid2sd(st);
  9704. if (sd)
  9705. bl = &sd->bl;
  9706. break;
  9707. case 1: //Get NPC Position
  9708. if( script_hasdata(st,6) )
  9709. {
  9710. struct npc_data *nd;
  9711. nd=npc_name2id(script_getstr(st,6));
  9712. if (nd)
  9713. bl = &nd->bl;
  9714. } else //In case the origin is not an npc?
  9715. bl=map_id2bl(st->oid);
  9716. break;
  9717. case 2: //Get Pet Position
  9718. if(script_hasdata(st,6))
  9719. sd=map_nick2sd(script_getstr(st,6));
  9720. else
  9721. sd=script_rid2sd(st);
  9722. if (sd && sd->pd)
  9723. bl = &sd->pd->bl;
  9724. break;
  9725. case 3: //Get Mob Position
  9726. break; //Not supported?
  9727. case 4: //Get Homun Position
  9728. if(script_hasdata(st,6))
  9729. sd=map_nick2sd(script_getstr(st,6));
  9730. else
  9731. sd=script_rid2sd(st);
  9732. if (sd && sd->hd)
  9733. bl = &sd->hd->bl;
  9734. break;
  9735. }
  9736. if (!bl) { //No object found.
  9737. script_pushint(st,-1);
  9738. return 0;
  9739. }
  9740. x= bl->x;
  9741. y= bl->y;
  9742. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  9743. //Set MapName$
  9744. num=st->stack->stack_data[st->start+2].u.num;
  9745. name=get_str(num&0x00ffffff);
  9746. prefix=*name;
  9747. if(not_server_variable(prefix))
  9748. sd=script_rid2sd(st);
  9749. else
  9750. sd=NULL;
  9751. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  9752. //Set MapX
  9753. num=st->stack->stack_data[st->start+3].u.num;
  9754. name=get_str(num&0x00ffffff);
  9755. prefix=*name;
  9756. if(not_server_variable(prefix))
  9757. sd=script_rid2sd(st);
  9758. else
  9759. sd=NULL;
  9760. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  9761. //Set MapY
  9762. num=st->stack->stack_data[st->start+4].u.num;
  9763. name=get_str(num&0x00ffffff);
  9764. prefix=*name;
  9765. if(not_server_variable(prefix))
  9766. sd=script_rid2sd(st);
  9767. else
  9768. sd=NULL;
  9769. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  9770. //Return Success value
  9771. script_pushint(st,0);
  9772. return 0;
  9773. }
  9774. /*==========================================
  9775. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  9776. *------------------------------------------*/
  9777. BUILDIN_FUNC(logmes)
  9778. {
  9779. const char *str;
  9780. TBL_PC* sd;
  9781. if( log_config.npc <= 0 )
  9782. return 0;
  9783. sd = script_rid2sd(st);
  9784. if( sd == NULL )
  9785. return 1;
  9786. str = script_getstr(st,2);
  9787. log_npc(sd,str);
  9788. return 0;
  9789. }
  9790. BUILDIN_FUNC(summon)
  9791. {
  9792. int _class, timeout=0;
  9793. const char *str,*event="";
  9794. TBL_PC *sd;
  9795. struct mob_data *md;
  9796. int tick = gettick();
  9797. sd=script_rid2sd(st);
  9798. if (!sd) return 0;
  9799. str =script_getstr(st,2);
  9800. _class=script_getnum(st,3);
  9801. if( script_hasdata(st,4) )
  9802. timeout=script_getnum(st,4);
  9803. if( script_hasdata(st,5) ){
  9804. event=script_getstr(st,5);
  9805. check_event(st, event);
  9806. }
  9807. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  9808. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  9809. if (md) {
  9810. md->master_id=sd->bl.id;
  9811. md->special_state.ai=1;
  9812. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  9813. mob_spawn (md); //Now it is ready for spawning.
  9814. clif_misceffect2(&md->bl,344);
  9815. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  9816. }
  9817. return 0;
  9818. }
  9819. /*==========================================
  9820. * Checks whether it is daytime/nighttime
  9821. *------------------------------------------*/
  9822. BUILDIN_FUNC(isnight)
  9823. {
  9824. script_pushint(st,(night_flag == 1));
  9825. return 0;
  9826. }
  9827. BUILDIN_FUNC(isday)
  9828. {
  9829. script_pushint(st,(night_flag == 0));
  9830. return 0;
  9831. }
  9832. /*================================================
  9833. * Check whether another item/card has been
  9834. * equipped - used for 2/15's cards patch [celest]
  9835. *------------------------------------------------*/
  9836. // leave this here, just in case
  9837. #if 0
  9838. BUILDIN_FUNC(isequipped)
  9839. {
  9840. TBL_PC *sd;
  9841. int i, j, k, id = 1;
  9842. int ret = -1;
  9843. sd = script_rid2sd(st);
  9844. for (i=0; id!=0; i++) {
  9845. int flag = 0;
  9846. FETCH (i+2, id) else id = 0;
  9847. if (id <= 0)
  9848. continue;
  9849. for (j=0; j<EQI_MAX; j++) {
  9850. int index;
  9851. index = sd->equip_index[j];
  9852. if(index < 0) continue;
  9853. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9854. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9855. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9856. if(!sd->inventory_data[index])
  9857. continue;
  9858. if(itemdb_type(id) != IT_CARD) { //Non card
  9859. if (sd->inventory_data[index]->nameid != id)
  9860. continue;
  9861. flag = 1;
  9862. break;
  9863. } else { //Card
  9864. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9865. continue;
  9866. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9867. if (sd->status.inventory[index].card[k] == id)
  9868. {
  9869. flag = 1;
  9870. break;
  9871. }
  9872. }
  9873. }
  9874. if (flag) break;
  9875. }
  9876. if (ret == -1)
  9877. ret = flag;
  9878. else
  9879. ret &= flag;
  9880. if (!ret) break;
  9881. }
  9882. script_pushint(st,ret);
  9883. return 0;
  9884. }
  9885. #endif
  9886. /*================================================
  9887. * Check how many items/cards in the list are
  9888. * equipped - used for 2/15's cards patch [celest]
  9889. *------------------------------------------------*/
  9890. BUILDIN_FUNC(isequippedcnt)
  9891. {
  9892. TBL_PC *sd;
  9893. int i, j, k, id = 1;
  9894. int ret = 0;
  9895. sd = script_rid2sd(st);
  9896. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9897. script_pushint(st,0);
  9898. return 0;
  9899. }
  9900. for (i=0; id!=0; i++) {
  9901. FETCH (i+2, id) else id = 0;
  9902. if (id <= 0)
  9903. continue;
  9904. for (j=0; j<EQI_MAX; j++) {
  9905. int index;
  9906. index = sd->equip_index[j];
  9907. if(index < 0) continue;
  9908. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9909. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9910. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9911. if(!sd->inventory_data[index])
  9912. continue;
  9913. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  9914. if (sd->inventory_data[index]->nameid == id)
  9915. ret+= sd->status.inventory[index].amount;
  9916. } else { //Count cards.
  9917. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9918. continue; //No cards
  9919. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9920. if (sd->status.inventory[index].card[k] == id)
  9921. ret++; //[Lupus]
  9922. }
  9923. }
  9924. }
  9925. }
  9926. script_pushint(st,ret);
  9927. return 0;
  9928. }
  9929. /*================================================
  9930. * Check whether another card has been
  9931. * equipped - used for 2/15's cards patch [celest]
  9932. * -- Items checked cannot be reused in another
  9933. * card set to prevent exploits
  9934. *------------------------------------------------*/
  9935. BUILDIN_FUNC(isequipped)
  9936. {
  9937. TBL_PC *sd;
  9938. int i, j, k, id = 1;
  9939. int index, flag;
  9940. int ret = -1;
  9941. //Original hash to reverse it when full check fails.
  9942. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  9943. sd = script_rid2sd(st);
  9944. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9945. script_pushint(st,0);
  9946. return 0;
  9947. }
  9948. setitem_hash = sd->setitem_hash;
  9949. setitem_hash2 = sd->setitem_hash2;
  9950. for (i=0; id!=0; i++)
  9951. {
  9952. FETCH (i+2, id) else id = 0;
  9953. if (id <= 0)
  9954. continue;
  9955. flag = 0;
  9956. for (j=0; j<EQI_MAX; j++)
  9957. {
  9958. index = sd->equip_index[j];
  9959. if(index < 0) continue;
  9960. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9961. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9962. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9963. if(!sd->inventory_data[index])
  9964. continue;
  9965. if (itemdb_type(id) != IT_CARD) {
  9966. if (sd->inventory_data[index]->nameid != id)
  9967. continue;
  9968. flag = 1;
  9969. break;
  9970. } else { //Cards
  9971. if (sd->inventory_data[index]->slot == 0 ||
  9972. itemdb_isspecial(sd->status.inventory[index].card[0]))
  9973. continue;
  9974. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  9975. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  9976. unsigned int hash = 0;
  9977. if (sd->status.inventory[index].card[k] != id)
  9978. continue;
  9979. hash = 1<<((j<5?j:j-5)*4 + k);
  9980. // check if card is already used by another set
  9981. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  9982. continue;
  9983. // We have found a match
  9984. flag = 1;
  9985. // Set hash so this card cannot be used by another
  9986. if (j<5)
  9987. sd->setitem_hash |= hash;
  9988. else
  9989. sd->setitem_hash2 |= hash;
  9990. break;
  9991. }
  9992. }
  9993. if (flag) break; //Card found
  9994. }
  9995. if (ret == -1)
  9996. ret = flag;
  9997. else
  9998. ret &= flag;
  9999. if (!ret) break;
  10000. }
  10001. if (!ret)
  10002. { //When check fails, restore original hash values. [Skotlex]
  10003. sd->setitem_hash = setitem_hash;
  10004. sd->setitem_hash2 = setitem_hash2;
  10005. }
  10006. script_pushint(st,ret);
  10007. return 0;
  10008. }
  10009. /*================================================
  10010. * Check how many given inserted cards in the CURRENT
  10011. * weapon - used for 2/15's cards patch [Lupus]
  10012. *------------------------------------------------*/
  10013. BUILDIN_FUNC(cardscnt)
  10014. {
  10015. TBL_PC *sd;
  10016. int i, k, id = 1;
  10017. int ret = 0;
  10018. int index;
  10019. sd = script_rid2sd(st);
  10020. for (i=0; id!=0; i++) {
  10021. FETCH (i+2, id) else id = 0;
  10022. if (id <= 0)
  10023. continue;
  10024. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10025. if(index < 0) continue;
  10026. if(!sd->inventory_data[index])
  10027. continue;
  10028. if(itemdb_type(id) != IT_CARD) {
  10029. if (sd->inventory_data[index]->nameid == id)
  10030. ret+= sd->status.inventory[index].amount;
  10031. } else {
  10032. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10033. continue;
  10034. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10035. if (sd->status.inventory[index].card[k] == id)
  10036. ret++;
  10037. }
  10038. }
  10039. }
  10040. script_pushint(st,ret);
  10041. // script_pushint(st,current_equip_item_index);
  10042. return 0;
  10043. }
  10044. /*=======================================================
  10045. * Returns the refined number of the current item, or an
  10046. * item with inventory index specified
  10047. *-------------------------------------------------------*/
  10048. BUILDIN_FUNC(getrefine)
  10049. {
  10050. TBL_PC *sd;
  10051. if ((sd = script_rid2sd(st))!= NULL)
  10052. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10053. else
  10054. script_pushint(st,0);
  10055. return 0;
  10056. }
  10057. /*=======================================================
  10058. * Day/Night controls
  10059. *-------------------------------------------------------*/
  10060. BUILDIN_FUNC(night)
  10061. {
  10062. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10063. return 0;
  10064. }
  10065. BUILDIN_FUNC(day)
  10066. {
  10067. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10068. return 0;
  10069. }
  10070. //=======================================================
  10071. // Unequip [Spectre]
  10072. //-------------------------------------------------------
  10073. BUILDIN_FUNC(unequip)
  10074. {
  10075. int i;
  10076. size_t num;
  10077. TBL_PC *sd;
  10078. num = script_getnum(st,2);
  10079. sd = script_rid2sd(st);
  10080. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10081. {
  10082. i = pc_checkequip(sd,equip[num-1]);
  10083. if (i >= 0)
  10084. pc_unequipitem(sd,i,2);
  10085. return 0;
  10086. }
  10087. return 0;
  10088. }
  10089. BUILDIN_FUNC(equip)
  10090. {
  10091. int nameid=0,i;
  10092. TBL_PC *sd;
  10093. struct item_data *item_data;
  10094. sd = script_rid2sd(st);
  10095. nameid=script_getnum(st,2);
  10096. if((item_data = itemdb_exists(nameid)) == NULL)
  10097. {
  10098. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10099. return 1;
  10100. }
  10101. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10102. if( i < MAX_INVENTORY )
  10103. pc_equipitem(sd,i,item_data->equip);
  10104. return 0;
  10105. }
  10106. BUILDIN_FUNC(autoequip)
  10107. {
  10108. int nameid, flag;
  10109. struct item_data *item_data;
  10110. nameid=script_getnum(st,2);
  10111. flag=script_getnum(st,3);
  10112. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10113. item_data->flag.autoequip = flag>0?1:0;
  10114. }
  10115. return 0;
  10116. }
  10117. BUILDIN_FUNC(setbattleflag)
  10118. {
  10119. const char *flag, *value;
  10120. flag = script_getstr(st,2);
  10121. value = script_getstr(st,3);
  10122. if (battle_set_value(flag, value) == 0)
  10123. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10124. else
  10125. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10126. return 0;
  10127. }
  10128. BUILDIN_FUNC(getbattleflag)
  10129. {
  10130. const char *flag;
  10131. flag = script_getstr(st,2);
  10132. script_pushint(st,battle_get_value(flag));
  10133. return 0;
  10134. }
  10135. //=======================================================
  10136. // strlen [Valaris]
  10137. //-------------------------------------------------------
  10138. BUILDIN_FUNC(getstrlen)
  10139. {
  10140. const char *str = script_getstr(st,2);
  10141. int len = (str) ? (int)strlen(str) : 0;
  10142. script_pushint(st,len);
  10143. return 0;
  10144. }
  10145. //=======================================================
  10146. // isalpha [Valaris]
  10147. //-------------------------------------------------------
  10148. BUILDIN_FUNC(charisalpha)
  10149. {
  10150. const char *str=script_getstr(st,2);
  10151. int pos=script_getnum(st,3);
  10152. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10153. script_pushint(st,val);
  10154. return 0;
  10155. }
  10156. /// Changes the display name and/or display class of the npc.
  10157. /// Returns 0 is successful, 1 if the npc does not exist.
  10158. ///
  10159. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  10160. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10161. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10162. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10163. BUILDIN_FUNC(setnpcdisplay)
  10164. {
  10165. const char* name;
  10166. const char* newname = NULL;
  10167. int class_ = -1, size = -1;
  10168. struct script_data* data;
  10169. struct npc_data* nd;
  10170. name = script_getstr(st,2);
  10171. data = script_getdata(st,3);
  10172. if( script_hasdata(st,4) )
  10173. class_ = script_getnum(st,4);
  10174. if( script_hasdata(st,5) )
  10175. size = script_getnum(st,5);
  10176. get_val(st, data);
  10177. if( data_isstring(data) )
  10178. newname = conv_str(st,data);
  10179. else if( data_isint(data) )
  10180. class_ = conv_num(st,data);
  10181. else
  10182. {
  10183. ShowError("script:setnpcdisplay: expected a string or number\n");
  10184. script_reportdata(data);
  10185. return 1;
  10186. }
  10187. nd = npc_name2id(name);
  10188. if( nd == NULL )
  10189. {// not found
  10190. script_pushint(st,1);
  10191. return 0;
  10192. }
  10193. // update npc
  10194. if( newname )
  10195. npc_setdisplayname(nd, newname);
  10196. if( size != -1 && size != nd->size )
  10197. nd->size = size;
  10198. else
  10199. size = -1;
  10200. if( class_ != -1 && nd->class_ != class_ )
  10201. npc_setclass(nd, class_);
  10202. else if( size != -1 )
  10203. { // Required to update the visual size
  10204. clif_clearunit_area(&nd->bl, 0);
  10205. clif_spawn(&nd->bl);
  10206. }
  10207. script_pushint(st,0);
  10208. return 0;
  10209. }
  10210. BUILDIN_FUNC(atoi)
  10211. {
  10212. const char *value;
  10213. value = script_getstr(st,2);
  10214. script_pushint(st,atoi(value));
  10215. return 0;
  10216. }
  10217. // case-insensitive substring search [lordalfa]
  10218. BUILDIN_FUNC(compare)
  10219. {
  10220. const char *message;
  10221. const char *cmpstring;
  10222. message = script_getstr(st,2);
  10223. cmpstring = script_getstr(st,3);
  10224. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10225. return 0;
  10226. }
  10227. // [zBuffer] List of mathematics commands --->
  10228. BUILDIN_FUNC(sqrt)
  10229. {
  10230. double i, a;
  10231. i = script_getnum(st,2);
  10232. a = sqrt(i);
  10233. script_pushint(st,(int)a);
  10234. return 0;
  10235. }
  10236. BUILDIN_FUNC(pow)
  10237. {
  10238. double i, a, b;
  10239. a = script_getnum(st,2);
  10240. b = script_getnum(st,3);
  10241. i = pow(a,b);
  10242. script_pushint(st,(int)i);
  10243. return 0;
  10244. }
  10245. BUILDIN_FUNC(distance)
  10246. {
  10247. int x0, y0, x1, y1;
  10248. x0 = script_getnum(st,2);
  10249. y0 = script_getnum(st,3);
  10250. x1 = script_getnum(st,4);
  10251. y1 = script_getnum(st,5);
  10252. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10253. return 0;
  10254. }
  10255. // <--- [zBuffer] List of mathematics commands
  10256. // [zBuffer] List of dynamic var commands --->
  10257. BUILDIN_FUNC(setd)
  10258. {
  10259. TBL_PC *sd=NULL;
  10260. char varname[100];
  10261. const char *value, *buffer;
  10262. int elem;
  10263. buffer = script_getstr(st, 2);
  10264. value = script_getstr(st, 3);
  10265. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10266. elem = 0;
  10267. if( not_server_variable(*varname) )
  10268. {
  10269. sd = script_rid2sd(st);
  10270. if( sd == NULL )
  10271. {
  10272. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10273. return 0;
  10274. }
  10275. }
  10276. if(varname[strlen(varname)-1] != '$') {
  10277. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10278. } else {
  10279. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10280. }
  10281. return 0;
  10282. }
  10283. #ifndef TXT_ONLY
  10284. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10285. {
  10286. int i, j;
  10287. TBL_PC* sd = NULL;
  10288. const char* query;
  10289. struct script_data* data;
  10290. const char* name;
  10291. int max_rows = 128;// maximum number of rows
  10292. int num_vars;
  10293. int num_cols;
  10294. // check target variables
  10295. for( i = 3; script_hasdata(st,i); ++i )
  10296. {
  10297. data = script_getdata(st, i);
  10298. if( data_isreference(data) && reference_tovariable(data) )
  10299. {// it's a variable
  10300. name = reference_getname(data);
  10301. if( not_server_variable(*name) && sd == NULL )
  10302. {// requires a player
  10303. sd = script_rid2sd(st);
  10304. if( sd == NULL )
  10305. {// no player attached
  10306. script_reportdata(data);
  10307. st->state = END;
  10308. return 1;
  10309. }
  10310. }
  10311. if( not_array_variable(*name) )
  10312. max_rows = 1;// not an array, limit to one row
  10313. }
  10314. else
  10315. {
  10316. ShowError("script:query_sql: not a variable\n");
  10317. script_reportdata(data);
  10318. st->state = END;
  10319. return 1;
  10320. }
  10321. }
  10322. num_vars = i - 3;
  10323. // Execute the query
  10324. query = script_getstr(st,2);
  10325. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10326. {
  10327. Sql_ShowDebug(handle);
  10328. script_pushint(st, 0);
  10329. return 1;
  10330. }
  10331. if( Sql_NumRows(handle) == 0 )
  10332. {// No data received
  10333. Sql_FreeResult(handle);
  10334. script_pushint(st, 0);
  10335. return 0;
  10336. }
  10337. // Count the number of columns to store
  10338. num_cols = Sql_NumColumns(handle);
  10339. if( num_vars < num_cols )
  10340. {
  10341. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10342. script_reportsrc(st);
  10343. }
  10344. else if( num_vars > num_cols )
  10345. {
  10346. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10347. script_reportsrc(st);
  10348. }
  10349. // Store data
  10350. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10351. {
  10352. for( j = 0; j < num_vars; ++j )
  10353. {
  10354. char* str = NULL;
  10355. if( j < num_cols )
  10356. Sql_GetData(handle, j, &str, NULL);
  10357. data = script_getdata(st, j+3);
  10358. name = reference_getname(data);
  10359. if( is_string_variable(name) )
  10360. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10361. else
  10362. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10363. }
  10364. }
  10365. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10366. {
  10367. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10368. script_reportsrc(st);
  10369. }
  10370. // Free data
  10371. Sql_FreeResult(handle);
  10372. script_pushint(st, i);
  10373. return 0;
  10374. }
  10375. #endif
  10376. BUILDIN_FUNC(query_sql)
  10377. {
  10378. #ifndef TXT_ONLY
  10379. return buildin_query_sql_sub(st, mmysql_handle);
  10380. #else
  10381. //for TXT version, we always return -1
  10382. script_pushint(st,-1);
  10383. return 0;
  10384. #endif
  10385. }
  10386. BUILDIN_FUNC(query_logsql)
  10387. {
  10388. #ifndef TXT_ONLY
  10389. return buildin_query_sql_sub(st, logmysql_handle);
  10390. #else
  10391. //for TXT version, we always return -1
  10392. script_pushint(st,-1);
  10393. return 0;
  10394. #endif
  10395. }
  10396. //Allows escaping of a given string.
  10397. BUILDIN_FUNC(escape_sql)
  10398. {
  10399. const char *str;
  10400. char *esc_str;
  10401. size_t len;
  10402. str = script_getstr(st,2);
  10403. len = strlen(str);
  10404. esc_str = (char*)aMallocA(len*2+1);
  10405. #if defined(TXT_ONLY)
  10406. jstrescapecpy(esc_str, str);
  10407. #else
  10408. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10409. #endif
  10410. script_pushstr(st, esc_str);
  10411. return 0;
  10412. }
  10413. BUILDIN_FUNC(getd)
  10414. {
  10415. char varname[100];
  10416. const char *buffer;
  10417. int elem;
  10418. buffer = script_getstr(st, 2);
  10419. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10420. elem = 0;
  10421. // Push the 'pointer' so it's more flexible [Lance]
  10422. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10423. return 0;
  10424. }
  10425. // <--- [zBuffer] List of dynamic var commands
  10426. // Pet stat [Lance]
  10427. BUILDIN_FUNC(petstat)
  10428. {
  10429. TBL_PC *sd = NULL;
  10430. struct pet_data *pd;
  10431. int flag = script_getnum(st,2);
  10432. sd = script_rid2sd(st);
  10433. if(!sd || !sd->status.pet_id || !sd->pd){
  10434. if(flag == 2)
  10435. script_pushconststr(st, "");
  10436. else
  10437. script_pushint(st,0);
  10438. return 0;
  10439. }
  10440. pd = sd->pd;
  10441. switch(flag){
  10442. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10443. case 2: script_pushstrcopy(st, pd->pet.name); break;
  10444. case 3: script_pushint(st,(int)pd->pet.level); break;
  10445. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10446. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10447. default:
  10448. script_pushint(st,0);
  10449. break;
  10450. }
  10451. return 0;
  10452. }
  10453. BUILDIN_FUNC(callshop)
  10454. {
  10455. TBL_PC *sd = NULL;
  10456. struct npc_data *nd;
  10457. const char *shopname;
  10458. int flag = 0;
  10459. sd = script_rid2sd(st);
  10460. if (!sd) {
  10461. script_pushint(st,0);
  10462. return 0;
  10463. }
  10464. shopname = script_getstr(st, 2);
  10465. if( script_hasdata(st,3) )
  10466. flag = script_getnum(st,3);
  10467. nd = npc_name2id(shopname);
  10468. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  10469. {
  10470. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  10471. script_pushint(st,0);
  10472. return 1;
  10473. }
  10474. if( nd->subtype == SHOP )
  10475. {
  10476. switch( flag )
  10477. {
  10478. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  10479. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  10480. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  10481. }
  10482. }
  10483. else
  10484. clif_cashshop_show(sd, nd);
  10485. sd->npc_shopid = nd->bl.id;
  10486. script_pushint(st,1);
  10487. return 0;
  10488. }
  10489. BUILDIN_FUNC(npcshopitem)
  10490. {
  10491. const char* npcname = script_getstr(st, 2);
  10492. struct npc_data* nd = npc_name2id(npcname);
  10493. int n, i;
  10494. int amount;
  10495. if( !nd || nd->subtype != SHOP )
  10496. { //Not found.
  10497. script_pushint(st,0);
  10498. return 0;
  10499. }
  10500. // get the count of new entries
  10501. amount = (script_lastdata(st)-2)/2;
  10502. // generate new shop item list
  10503. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  10504. for( n = 0, i = 3; n < amount; n++, i+=2 )
  10505. {
  10506. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10507. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10508. }
  10509. nd->u.shop.count = n;
  10510. script_pushint(st,1);
  10511. return 0;
  10512. }
  10513. BUILDIN_FUNC(npcshopadditem)
  10514. {
  10515. const char* npcname = script_getstr(st,2);
  10516. struct npc_data* nd = npc_name2id(npcname);
  10517. int n, i;
  10518. int amount;
  10519. if( !nd || nd->subtype != SHOP )
  10520. { //Not found.
  10521. script_pushint(st,0);
  10522. return 0;
  10523. }
  10524. // get the count of new entries
  10525. amount = (script_lastdata(st)-2)/2;
  10526. // append new items to existing shop item list
  10527. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  10528. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  10529. {
  10530. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10531. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10532. }
  10533. nd->u.shop.count = n;
  10534. script_pushint(st,1);
  10535. return 0;
  10536. }
  10537. BUILDIN_FUNC(npcshopdelitem)
  10538. {
  10539. const char* npcname = script_getstr(st,2);
  10540. struct npc_data* nd = npc_name2id(npcname);
  10541. int n, i;
  10542. int amount;
  10543. int size;
  10544. if( !nd || nd->subtype != SHOP )
  10545. { //Not found.
  10546. script_pushint(st,0);
  10547. return 0;
  10548. }
  10549. amount = script_lastdata(st)-2;
  10550. size = nd->u.shop.count;
  10551. // remove specified items from the shop item list
  10552. for( i = 3; i < 3 + amount; i++ )
  10553. {
  10554. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
  10555. if( n < size )
  10556. {
  10557. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  10558. size--;
  10559. }
  10560. }
  10561. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  10562. nd->u.shop.count = size;
  10563. script_pushint(st,1);
  10564. return 0;
  10565. }
  10566. //Sets a script to attach to a shop npc.
  10567. BUILDIN_FUNC(npcshopattach)
  10568. {
  10569. const char* npcname = script_getstr(st,2);
  10570. struct npc_data* nd = npc_name2id(npcname);
  10571. int flag = 1;
  10572. if( script_hasdata(st,3) )
  10573. flag = script_getnum(st,3);
  10574. if( !nd || nd->subtype != SHOP )
  10575. { //Not found.
  10576. script_pushint(st,0);
  10577. return 0;
  10578. }
  10579. if (flag)
  10580. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10581. else
  10582. nd->master_nd = NULL;
  10583. script_pushint(st,1);
  10584. return 0;
  10585. }
  10586. /*==========================================
  10587. * Returns some values of an item [Lupus]
  10588. * Price, Weight, etc...
  10589. setitemscript(itemID,"{new item bonus script}",[n]);
  10590. Where n:
  10591. 0 - script
  10592. 1 - Equip script
  10593. 2 - Unequip script
  10594. *------------------------------------------*/
  10595. BUILDIN_FUNC(setitemscript)
  10596. {
  10597. int item_id,n=0;
  10598. const char *script;
  10599. struct item_data *i_data;
  10600. struct script_code **dstscript;
  10601. item_id = script_getnum(st,2);
  10602. script = script_getstr(st,3);
  10603. if( script_hasdata(st,4) )
  10604. n=script_getnum(st,4);
  10605. i_data = itemdb_exists(item_id);
  10606. if (!i_data || script==NULL || script[0]!='{') {
  10607. script_pushint(st,0);
  10608. return 0;
  10609. }
  10610. switch (n) {
  10611. case 2:
  10612. dstscript = &i_data->unequip_script;
  10613. break;
  10614. case 1:
  10615. dstscript = &i_data->equip_script;
  10616. break;
  10617. default:
  10618. dstscript = &i_data->script;
  10619. break;
  10620. }
  10621. if(*dstscript)
  10622. script_free_code(*dstscript);
  10623. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  10624. script_pushint(st,1);
  10625. return 0;
  10626. }
  10627. /* Work In Progress [Lupus]
  10628. BUILDIN_FUNC(addmonsterdrop)
  10629. {
  10630. int class_,item_id,chance;
  10631. class_=script_getnum(st,2);
  10632. item_id=script_getnum(st,3);
  10633. chance=script_getnum(st,4);
  10634. if(class_>1000 && item_id>500 && chance>0) {
  10635. script_pushint(st,1);
  10636. } else {
  10637. script_pushint(st,0);
  10638. }
  10639. }
  10640. BUILDIN_FUNC(delmonsterdrop)
  10641. {
  10642. int class_,item_id;
  10643. class_=script_getnum(st,2);
  10644. item_id=script_getnum(st,3);
  10645. if(class_>1000 && item_id>500) {
  10646. script_pushint(st,1);
  10647. } else {
  10648. script_pushint(st,0);
  10649. }
  10650. }
  10651. */
  10652. /*==========================================
  10653. * Returns some values of a monster [Lupus]
  10654. * Name, Level, race, size, etc...
  10655. getmonsterinfo(monsterID,queryIndex);
  10656. *------------------------------------------*/
  10657. BUILDIN_FUNC(getmonsterinfo)
  10658. {
  10659. struct mob_db *mob;
  10660. int mob_id;
  10661. mob_id = script_getnum(st,2);
  10662. if (!mobdb_checkid(mob_id)) {
  10663. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  10664. if ( !script_getnum(st,3) ) //requested a string
  10665. script_pushconststr(st,"null");
  10666. else
  10667. script_pushint(st,-1);
  10668. return -1;
  10669. }
  10670. mob = mob_db(mob_id);
  10671. switch ( script_getnum(st,3) ) {
  10672. case 0: script_pushstrcopy(st,mob->jname); break;
  10673. case 1: script_pushint(st,mob->lv); break;
  10674. case 2: script_pushint(st,mob->status.max_hp); break;
  10675. case 3: script_pushint(st,mob->base_exp); break;
  10676. case 4: script_pushint(st,mob->job_exp); break;
  10677. case 5: script_pushint(st,mob->status.rhw.atk); break;
  10678. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  10679. case 7: script_pushint(st,mob->status.def); break;
  10680. case 8: script_pushint(st,mob->status.mdef); break;
  10681. case 9: script_pushint(st,mob->status.str); break;
  10682. case 10: script_pushint(st,mob->status.agi); break;
  10683. case 11: script_pushint(st,mob->status.vit); break;
  10684. case 12: script_pushint(st,mob->status.int_); break;
  10685. case 13: script_pushint(st,mob->status.dex); break;
  10686. case 14: script_pushint(st,mob->status.luk); break;
  10687. case 15: script_pushint(st,mob->status.rhw.range); break;
  10688. case 16: script_pushint(st,mob->range2); break;
  10689. case 17: script_pushint(st,mob->range3); break;
  10690. case 18: script_pushint(st,mob->status.size); break;
  10691. case 19: script_pushint(st,mob->status.race); break;
  10692. case 20: script_pushint(st,mob->status.def_ele); break;
  10693. case 21: script_pushint(st,mob->status.mode); break;
  10694. default: script_pushint(st,-1); //wrong Index
  10695. }
  10696. return 0;
  10697. }
  10698. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  10699. {
  10700. TBL_PC *sd = NULL;
  10701. if(script_hasdata(st,2))
  10702. sd = map_nick2sd(script_getstr(st,2));
  10703. else
  10704. sd = script_rid2sd(st);
  10705. if(sd)
  10706. script_pushint(st,(sd->vender_id != 0));
  10707. else
  10708. script_pushint(st,0);
  10709. return 0;
  10710. }
  10711. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  10712. {
  10713. TBL_PC *sd = NULL;
  10714. if(script_hasdata(st,2))
  10715. sd = map_nick2sd(script_getstr(st,2));
  10716. else
  10717. sd = script_rid2sd(st);
  10718. if(sd)
  10719. script_pushint(st,(sd->chatID != 0));
  10720. else
  10721. script_pushint(st,0);
  10722. return 0;
  10723. }
  10724. int axtoi(const char *hexStg)
  10725. {
  10726. int n = 0; // position in string
  10727. int m = 0; // position in digit[] to shift
  10728. int count; // loop index
  10729. int intValue = 0; // integer value of hex string
  10730. int digit[11]; // hold values to convert
  10731. while (n < 10) {
  10732. if (hexStg[n]=='\0')
  10733. break;
  10734. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  10735. digit[n] = hexStg[n] & 0x0f; //convert to int
  10736. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  10737. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10738. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  10739. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10740. else break;
  10741. n++;
  10742. }
  10743. count = n;
  10744. m = n - 1;
  10745. n = 0;
  10746. while(n < count) {
  10747. // digit[n] is value of hex digit at position n
  10748. // (m << 2) is the number of positions to shift
  10749. // OR the bits into return value
  10750. intValue = intValue | (digit[n] << (m << 2));
  10751. m--; // adjust the position to set
  10752. n++; // next digit to process
  10753. }
  10754. return (intValue);
  10755. }
  10756. // [Lance] Hex string to integer converter
  10757. BUILDIN_FUNC(axtoi)
  10758. {
  10759. const char *hex = script_getstr(st,2);
  10760. script_pushint(st,axtoi(hex));
  10761. return 0;
  10762. }
  10763. // [zBuffer] List of player cont commands --->
  10764. BUILDIN_FUNC(rid2name)
  10765. {
  10766. struct block_list *bl = NULL;
  10767. int rid = script_getnum(st,2);
  10768. if((bl = map_id2bl(rid)))
  10769. {
  10770. switch(bl->type) {
  10771. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  10772. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  10773. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  10774. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  10775. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  10776. default:
  10777. ShowError("buildin_rid2name: BL type unknown.\n");
  10778. script_pushconststr(st,"");
  10779. break;
  10780. }
  10781. } else {
  10782. ShowError("buildin_rid2name: invalid RID\n");
  10783. script_pushconststr(st,"(null)");
  10784. }
  10785. return 0;
  10786. }
  10787. BUILDIN_FUNC(pcblockmove)
  10788. {
  10789. int id, flag;
  10790. TBL_PC *sd = NULL;
  10791. id = script_getnum(st,2);
  10792. flag = script_getnum(st,3);
  10793. if(id)
  10794. sd = map_id2sd(id);
  10795. else
  10796. sd = script_rid2sd(st);
  10797. if(sd)
  10798. sd->state.blockedmove = flag > 0;
  10799. return 0;
  10800. }
  10801. BUILDIN_FUNC(pcfollow)
  10802. {
  10803. int id, targetid;
  10804. TBL_PC *sd = NULL;
  10805. id = script_getnum(st,2);
  10806. targetid = script_getnum(st,3);
  10807. if(id)
  10808. sd = map_id2sd(id);
  10809. else
  10810. sd = script_rid2sd(st);
  10811. if(sd)
  10812. pc_follow(sd, targetid);
  10813. return 0;
  10814. }
  10815. BUILDIN_FUNC(pcstopfollow)
  10816. {
  10817. int id;
  10818. TBL_PC *sd = NULL;
  10819. id = script_getnum(st,2);
  10820. if(id)
  10821. sd = map_id2sd(id);
  10822. else
  10823. sd = script_rid2sd(st);
  10824. if(sd)
  10825. pc_stop_following(sd);
  10826. return 0;
  10827. }
  10828. // <--- [zBuffer] List of player cont commands
  10829. // [zBuffer] List of mob control commands --->
  10830. //## TODO always return if the request/whatever was successfull [FlavioJS]
  10831. /// Makes the unit walk to target position or map
  10832. /// Returns if it was successfull
  10833. ///
  10834. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  10835. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  10836. BUILDIN_FUNC(unitwalk)
  10837. {
  10838. struct block_list* bl;
  10839. bl = map_id2bl(script_getnum(st,2));
  10840. if( bl == NULL )
  10841. {
  10842. script_pushint(st, 0);
  10843. }
  10844. else if( script_hasdata(st,4) )
  10845. {
  10846. int x = script_getnum(st,3);
  10847. int y = script_getnum(st,4);
  10848. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  10849. }
  10850. else
  10851. {
  10852. int map_id = script_getnum(st,3);
  10853. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  10854. }
  10855. return 0;
  10856. }
  10857. /// Kills the unit
  10858. ///
  10859. /// unitkill <unit_id>;
  10860. BUILDIN_FUNC(unitkill)
  10861. {
  10862. struct block_list* bl = map_id2bl(script_getnum(st,2));
  10863. if( bl != NULL )
  10864. status_kill(bl);
  10865. return 0;
  10866. }
  10867. /// Warps the unit to the target position in the target map
  10868. /// Returns if it was successfull
  10869. ///
  10870. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  10871. BUILDIN_FUNC(unitwarp)
  10872. {
  10873. int unit_id;
  10874. int map;
  10875. short x;
  10876. short y;
  10877. struct block_list* bl;
  10878. unit_id = script_getnum(st,2);
  10879. map = map_mapname2mapid(script_getstr(st, 3));
  10880. x = (short)script_getnum(st,4);
  10881. y = (short)script_getnum(st,5);
  10882. bl = map_id2bl(unit_id);
  10883. if( map >= 0 && bl != NULL )
  10884. script_pushint(st, unit_warp(bl,map,x,y,0));
  10885. else
  10886. script_pushint(st, 0);
  10887. return 0;
  10888. }
  10889. /// Makes the unit attack the target.
  10890. /// If the unit is a player and <action type> is not 0, it does a continuous
  10891. /// attack instead of a single attack.
  10892. /// Returns if the request was successfull.
  10893. ///
  10894. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  10895. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  10896. BUILDIN_FUNC(unitattack)
  10897. {
  10898. struct block_list* unit_bl;
  10899. struct block_list* target_bl = NULL;
  10900. struct script_data* data;
  10901. int actiontype = 0;
  10902. // get unit
  10903. unit_bl = map_id2bl(script_getnum(st,2));
  10904. if( unit_bl == NULL ) {
  10905. script_pushint(st, 0);
  10906. return 0;
  10907. }
  10908. data = script_getdata(st, 3);
  10909. get_val(st, data);
  10910. if( data_isstring(data) )
  10911. {
  10912. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  10913. if( sd != NULL )
  10914. target_bl = &sd->bl;
  10915. } else
  10916. target_bl = map_id2bl(conv_num(st, data));
  10917. // request the attack
  10918. if( target_bl == NULL )
  10919. {
  10920. script_pushint(st, 0);
  10921. return 0;
  10922. }
  10923. // get actiontype
  10924. if( script_hasdata(st,4) )
  10925. actiontype = script_getnum(st,4);
  10926. switch( unit_bl->type )
  10927. {
  10928. case BL_PC:
  10929. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  10930. script_pushint(st, 1);
  10931. return 0;
  10932. case BL_MOB:
  10933. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  10934. break;
  10935. case BL_PET:
  10936. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  10937. break;
  10938. default:
  10939. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  10940. script_pushint(st, 0);
  10941. return 1;
  10942. }
  10943. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  10944. return 0;
  10945. }
  10946. /// Makes the unit stop attacking and moving
  10947. ///
  10948. /// unitstop <unit_id>;
  10949. BUILDIN_FUNC(unitstop)
  10950. {
  10951. int unit_id;
  10952. struct block_list* bl;
  10953. unit_id = script_getnum(st,2);
  10954. bl = map_id2bl(unit_id);
  10955. if( bl != NULL )
  10956. {
  10957. unit_stop_attack(bl);
  10958. unit_stop_walking(bl,0);
  10959. if( bl->type == BL_MOB )
  10960. ((TBL_MOB*)bl)->target_id = 0;
  10961. }
  10962. return 0;
  10963. }
  10964. /// Makes the unit say the message
  10965. ///
  10966. /// unittalk <unit_id>,"<message>";
  10967. BUILDIN_FUNC(unittalk)
  10968. {
  10969. int unit_id;
  10970. const char* message;
  10971. struct block_list* bl;
  10972. unit_id = script_getnum(st,2);
  10973. message = script_getstr(st, 3);
  10974. bl = map_id2bl(unit_id);
  10975. if( bl != NULL )
  10976. {
  10977. struct StringBuf sbuf;
  10978. StringBuf_Init(&sbuf);
  10979. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  10980. clif_message(bl, StringBuf_Value(&sbuf));
  10981. if( bl->type == BL_PC )
  10982. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  10983. StringBuf_Destroy(&sbuf);
  10984. }
  10985. return 0;
  10986. }
  10987. /// Makes the unit do an emotion
  10988. ///
  10989. /// unitemote <unit_id>,<emotion>;
  10990. ///
  10991. /// @see e_* in const.txt
  10992. BUILDIN_FUNC(unitemote)
  10993. {
  10994. int unit_id;
  10995. int emotion;
  10996. struct block_list* bl;
  10997. unit_id = script_getnum(st,2);
  10998. emotion = script_getnum(st,3);
  10999. bl = map_id2bl(unit_id);
  11000. if( bl != NULL )
  11001. clif_emotion(bl, emotion);
  11002. return 0;
  11003. }
  11004. /// Makes the unit cast the skill on the target or self if no target is specified
  11005. ///
  11006. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11007. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  11008. BUILDIN_FUNC(unitskilluseid)
  11009. {
  11010. int unit_id;
  11011. int skill_id;
  11012. int skill_lv;
  11013. int target_id;
  11014. struct block_list* bl;
  11015. unit_id = script_getnum(st,2);
  11016. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11017. skill_lv = script_getnum(st,4);
  11018. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11019. bl = map_id2bl(unit_id);
  11020. if( bl != NULL )
  11021. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11022. return 0;
  11023. }
  11024. /// Makes the unit cast the skill on the target position.
  11025. ///
  11026. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11027. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  11028. BUILDIN_FUNC(unitskillusepos)
  11029. {
  11030. int unit_id;
  11031. int skill_id;
  11032. int skill_lv;
  11033. int skill_x;
  11034. int skill_y;
  11035. struct block_list* bl;
  11036. unit_id = script_getnum(st,2);
  11037. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11038. skill_lv = script_getnum(st,4);
  11039. skill_x = script_getnum(st,5);
  11040. skill_y = script_getnum(st,6);
  11041. bl = map_id2bl(unit_id);
  11042. if( bl != NULL )
  11043. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11044. return 0;
  11045. }
  11046. // <--- [zBuffer] List of mob control commands
  11047. /// Pauses the execution of the script, detaching the player
  11048. ///
  11049. /// sleep <mili seconds>;
  11050. BUILDIN_FUNC(sleep)
  11051. {
  11052. int ticks;
  11053. TBL_PC* sd;
  11054. ticks = script_getnum(st,2);
  11055. sd = map_id2sd(st->rid);
  11056. // detach the player
  11057. if( sd && sd->npc_id == st->oid )
  11058. {
  11059. sd->npc_id = 0;
  11060. }
  11061. st->rid = 0;
  11062. if( ticks <= 0 )
  11063. {// do nothing
  11064. }
  11065. else if( st->sleep.tick == 0 )
  11066. {// sleep for the target amount of time
  11067. st->state = RERUNLINE;
  11068. st->sleep.tick = ticks;
  11069. }
  11070. else
  11071. {// sleep time is over
  11072. st->sleep.tick = 0;
  11073. }
  11074. return 0;
  11075. }
  11076. /// Pauses the execution of the script, keeping the player attached
  11077. /// Returns if a player is still attached
  11078. ///
  11079. /// sleep2(<mili secconds>) -> <bool>
  11080. BUILDIN_FUNC(sleep2)
  11081. {
  11082. int ticks;
  11083. ticks = script_getnum(st,2);
  11084. if( ticks <= 0 )
  11085. {// do nothing
  11086. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11087. }
  11088. else if( !st->sleep.tick )
  11089. {// sleep for the target amount of time
  11090. st->state = RERUNLINE;
  11091. st->sleep.tick = ticks;
  11092. }
  11093. else
  11094. {// sleep time is over
  11095. st->sleep.tick = 0;
  11096. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11097. }
  11098. return 0;
  11099. }
  11100. /// Awakes all the sleep timers of the target npc
  11101. ///
  11102. /// awake "<npc name>";
  11103. BUILDIN_FUNC(awake)
  11104. {
  11105. struct npc_data* nd;
  11106. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11107. nd = npc_name2id(script_getstr(st, 2));
  11108. if( nd == NULL )
  11109. return 0;
  11110. while( node )
  11111. {
  11112. if( (int)node->key == nd->bl.id )
  11113. {// sleep timer for the npc
  11114. struct script_state* tst = (struct script_state*)node->data;
  11115. TBL_PC* sd = map_id2sd(tst->rid);
  11116. if( tst->sleep.timer == -1 )
  11117. {// already awake ???
  11118. node = node->next;
  11119. continue;
  11120. }
  11121. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11122. {// char not online anymore / another char of the same account is online - Cancel execution
  11123. tst->state = END;
  11124. tst->rid = 0;
  11125. }
  11126. delete_timer(tst->sleep.timer, run_script_timer);
  11127. node = script_erase_sleepdb(node);
  11128. tst->sleep.timer = INVALID_TIMER;
  11129. tst->sleep.tick = 0;
  11130. run_script_main(tst);
  11131. }
  11132. else
  11133. {
  11134. node = node->next;
  11135. }
  11136. }
  11137. return 0;
  11138. }
  11139. /// Returns a reference to a variable of the target NPC.
  11140. /// Returns 0 if an error occurs.
  11141. ///
  11142. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11143. BUILDIN_FUNC(getvariableofnpc)
  11144. {
  11145. struct script_data* data;
  11146. const char* name;
  11147. struct npc_data* nd;
  11148. data = script_getdata(st,2);
  11149. if( !data_isreference(data) )
  11150. {// Not a reference (aka varaible name)
  11151. ShowError("script:getvariableofnpc: not a variable\n");
  11152. script_reportdata(data);
  11153. script_pushnil(st);
  11154. st->state = END;
  11155. return 1;
  11156. }
  11157. name = reference_getname(data);
  11158. if( *name != '.' || name[1] == '@' )
  11159. {// not a npc variable
  11160. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11161. script_reportdata(data);
  11162. script_pushnil(st);
  11163. st->state = END;
  11164. return 1;
  11165. }
  11166. nd = npc_name2id(script_getstr(st,3));
  11167. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11168. {// NPC not found or has no script
  11169. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11170. script_pushnil(st);
  11171. st->state = END;
  11172. return 1;
  11173. }
  11174. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11175. return 0;
  11176. }
  11177. /// Opens a warp portal.
  11178. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11179. ///
  11180. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11181. ///
  11182. /// @author blackhole89
  11183. BUILDIN_FUNC(warpportal)
  11184. {
  11185. int spx;
  11186. int spy;
  11187. unsigned short mapindex;
  11188. int tpx;
  11189. int tpy;
  11190. struct skill_unit_group* group;
  11191. struct block_list* bl;
  11192. bl = map_id2bl(st->oid);
  11193. if( bl == NULL )
  11194. {
  11195. ShowError("script:warpportal: npc is needed\n");
  11196. return 1;
  11197. }
  11198. spx = script_getnum(st,2);
  11199. spy = script_getnum(st,3);
  11200. mapindex = mapindex_name2id(script_getstr(st, 4));
  11201. tpx = script_getnum(st,5);
  11202. tpy = script_getnum(st,6);
  11203. if( mapindex == 0 )
  11204. return 0;// map not found
  11205. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11206. if( group == NULL )
  11207. return 0;// failed
  11208. group->val2 = (tpx<<16) | tpy;
  11209. group->val3 = mapindex;
  11210. return 0;
  11211. }
  11212. BUILDIN_FUNC(openmail)
  11213. {
  11214. TBL_PC* sd;
  11215. sd = script_rid2sd(st);
  11216. if( sd == NULL )
  11217. return 0;
  11218. #ifndef TXT_ONLY
  11219. mail_openmail(sd);
  11220. #endif
  11221. return 0;
  11222. }
  11223. BUILDIN_FUNC(openauction)
  11224. {
  11225. TBL_PC* sd;
  11226. sd = script_rid2sd(st);
  11227. if( sd == NULL )
  11228. return 0;
  11229. #ifndef TXT_ONLY
  11230. clif_Auction_openwindow(sd);
  11231. #endif
  11232. return 0;
  11233. }
  11234. /// Retrieves the value of the specified flag of the specified cell.
  11235. ///
  11236. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  11237. ///
  11238. /// @see cell_chk* constants in const.txt for the types
  11239. BUILDIN_FUNC(checkcell)
  11240. {
  11241. int m = map_mapname2mapid(script_getstr(st,2));
  11242. int x = script_getnum(st,3);
  11243. int y = script_getnum(st,4);
  11244. cell_chk type = (cell_chk)script_getnum(st,5);
  11245. script_pushint(st, map_getcell(m, x, y, type));
  11246. return 0;
  11247. }
  11248. /// Modifies flags of cells in the specified area.
  11249. ///
  11250. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11251. ///
  11252. /// @see cell_* constants in const.txt for the types
  11253. BUILDIN_FUNC(setcell)
  11254. {
  11255. int m = map_mapname2mapid(script_getstr(st,2));
  11256. int x1 = script_getnum(st,3);
  11257. int y1 = script_getnum(st,4);
  11258. int x2 = script_getnum(st,5);
  11259. int y2 = script_getnum(st,6);
  11260. cell_t type = (cell_t)script_getnum(st,7);
  11261. bool flag = (bool)script_getnum(st,8);
  11262. int x,y;
  11263. if( x1 > x2 ) swap(x1,x2);
  11264. if( y1 > y2 ) swap(y1,y2);
  11265. for( y = y1; y <= y2; ++y )
  11266. for( x = x1; x <= x2; ++x )
  11267. map_setcell(m, x, y, type, flag);
  11268. return 0;
  11269. }
  11270. /*==========================================
  11271. * Mercenary Commands
  11272. *------------------------------------------*/
  11273. BUILDIN_FUNC(createmercenary)
  11274. {
  11275. struct map_session_data *sd;
  11276. int class_, contract_time;
  11277. if( (sd = script_rid2sd(st)) == NULL || sd->md || sd->status.mer_id != 0 )
  11278. return 0;
  11279. class_ = script_getnum(st,2);
  11280. if( !merc_class(class_) )
  11281. return 0;
  11282. contract_time = script_getnum(st,3);
  11283. merc_create(sd, class_, contract_time);
  11284. return 0;
  11285. }
  11286. BUILDIN_FUNC(mercenary_heal)
  11287. {
  11288. struct map_session_data *sd = script_rid2sd(st);
  11289. int hp, sp;
  11290. if( sd == NULL || sd->md == NULL )
  11291. return 0;
  11292. hp = script_getnum(st,2);
  11293. sp = script_getnum(st,3);
  11294. status_heal(&sd->md->bl, hp, sp, 0);
  11295. return 0;
  11296. }
  11297. BUILDIN_FUNC(mercenary_sc_start)
  11298. {
  11299. struct map_session_data *sd = script_rid2sd(st);
  11300. enum sc_type type;
  11301. int tick, val1;
  11302. if( sd == NULL || sd->md == NULL )
  11303. return 0;
  11304. type = (sc_type)script_getnum(st,2);
  11305. tick = script_getnum(st,3);
  11306. val1 = script_getnum(st,4);
  11307. status_change_start(&sd->md->bl, type, 10000, val1, 0, 0, 0, tick, 2);
  11308. return 0;
  11309. }
  11310. /******************
  11311. Questlog script commands
  11312. *******************/
  11313. BUILDIN_FUNC(getquest)
  11314. {
  11315. TBL_PC * sd = script_rid2sd(st);
  11316. struct quest qd;
  11317. int i, count = 0;
  11318. char * temp;
  11319. memset(&qd, 0, sizeof(struct quest));
  11320. qd.quest_id = script_getnum(st, 2);
  11321. qd.time = script_getnum(st, 3);
  11322. qd.state = Q_ACTIVE;
  11323. for(i=0; i<(script_lastdata(st)-3) && (i/2) < MAX_QUEST_OBJECTIVES; i+=2)
  11324. {
  11325. temp = (char*)script_getstr(st, i+4);
  11326. memcpy(&qd.objectives[i/2].name, temp, NAME_LENGTH);
  11327. temp = NULL;
  11328. qd.objectives[i/2].count = script_getnum(st, i+5);
  11329. count++;
  11330. }
  11331. qd.num_objectives = count;
  11332. quest_add(sd, &qd);
  11333. return 0;
  11334. }
  11335. BUILDIN_FUNC(deletequest)
  11336. {
  11337. TBL_PC * sd = script_rid2sd(st);
  11338. int qid = script_getnum(st, 2);
  11339. quest_delete(sd, qid);
  11340. return 0;
  11341. }
  11342. BUILDIN_FUNC(setquestobjective)
  11343. {
  11344. TBL_PC * sd = script_rid2sd(st);
  11345. int qid = script_getnum(st, 2);
  11346. int num = script_getnum(st, 3);
  11347. const char * str = script_getstr(st, 4);
  11348. int count = script_getnum(st, 5);
  11349. quest_update_objective(sd, qid, num, str, count);
  11350. return 0;
  11351. }
  11352. BUILDIN_FUNC(hasquest)
  11353. {
  11354. TBL_PC * sd = script_rid2sd(st);
  11355. int qid = script_getnum(st, 2);
  11356. script_pushint(st, quest_has_quest(sd, qid)?1:0);
  11357. return 0;
  11358. }
  11359. BUILDIN_FUNC(setqueststatus)
  11360. {
  11361. TBL_PC * sd = script_rid2sd(st);
  11362. int qid = script_getnum(st, 2);
  11363. bool active = script_getnum(st, 3)?true:false;
  11364. quest_update_status(sd, qid, active);
  11365. return 0;
  11366. }
  11367. // declarations that were supposed to be exported from npc_chat.c
  11368. #ifdef PCRE_SUPPORT
  11369. BUILDIN_FUNC(defpattern);
  11370. BUILDIN_FUNC(activatepset);
  11371. BUILDIN_FUNC(deactivatepset);
  11372. BUILDIN_FUNC(deletepset);
  11373. #endif
  11374. struct script_function buildin_func[] = {
  11375. // NPC interaction
  11376. BUILDIN_DEF(mes,"s"),
  11377. BUILDIN_DEF(next,""),
  11378. BUILDIN_DEF(close,""),
  11379. BUILDIN_DEF(close2,""),
  11380. BUILDIN_DEF(menu,"sl*"),
  11381. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  11382. BUILDIN_DEF(prompt,"s*"),
  11383. //
  11384. BUILDIN_DEF(goto,"l"),
  11385. BUILDIN_DEF(callsub,"i*"),
  11386. BUILDIN_DEF(callfunc,"s*"),
  11387. BUILDIN_DEF(return,"?"),
  11388. BUILDIN_DEF(getarg,"i?"),
  11389. BUILDIN_DEF(jobchange,"i*"),
  11390. BUILDIN_DEF(jobname,"i"),
  11391. BUILDIN_DEF(input,"v??"),
  11392. BUILDIN_DEF(warp,"sii"),
  11393. BUILDIN_DEF(areawarp,"siiiisii"),
  11394. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  11395. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  11396. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  11397. BUILDIN_DEF(setlook,"ii"),
  11398. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  11399. BUILDIN_DEF(set,"ii"),
  11400. BUILDIN_DEF(setarray,"rv*"),
  11401. BUILDIN_DEF(cleararray,"rvi"),
  11402. BUILDIN_DEF(copyarray,"rri"),
  11403. BUILDIN_DEF(getarraysize,"r"),
  11404. BUILDIN_DEF(deletearray,"r?"),
  11405. BUILDIN_DEF(getelementofarray,"ri"),
  11406. BUILDIN_DEF(getitem,"vi?"),
  11407. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  11408. BUILDIN_DEF(getnameditem,"is"),
  11409. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  11410. BUILDIN_DEF(makeitem,"iisii"),
  11411. BUILDIN_DEF(delitem,"ii?"),
  11412. BUILDIN_DEF(delitem2,"iiiiiiiii?"),
  11413. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  11414. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  11415. BUILDIN_DEF(cutin,"si"),
  11416. BUILDIN_DEF(viewpoint,"iiiii"),
  11417. BUILDIN_DEF(heal,"ii"),
  11418. BUILDIN_DEF(itemheal,"ii"),
  11419. BUILDIN_DEF(percentheal,"ii"),
  11420. BUILDIN_DEF(rand,"i?"),
  11421. BUILDIN_DEF(countitem,"i"),
  11422. BUILDIN_DEF(countitem2,"iiiiiiii"),
  11423. BUILDIN_DEF(checkweight,"ii"),
  11424. BUILDIN_DEF(readparam,"i*"),
  11425. BUILDIN_DEF(getcharid,"i*"),
  11426. BUILDIN_DEF(getpartyname,"i"),
  11427. BUILDIN_DEF(getpartymember,"i*"),
  11428. BUILDIN_DEF(getpartyleader,"i?"),
  11429. BUILDIN_DEF(getguildname,"i"),
  11430. BUILDIN_DEF(getguildmaster,"i"),
  11431. BUILDIN_DEF(getguildmasterid,"i"),
  11432. BUILDIN_DEF(strcharinfo,"i"),
  11433. BUILDIN_DEF(strnpcinfo,"i"),
  11434. BUILDIN_DEF(getequipid,"i"),
  11435. BUILDIN_DEF(getequipname,"i"),
  11436. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  11437. BUILDIN_DEF(repair,"i"), // [Valaris]
  11438. BUILDIN_DEF(getequipisequiped,"i"),
  11439. BUILDIN_DEF(getequipisenableref,"i"),
  11440. BUILDIN_DEF(getequipisidentify,"i"),
  11441. BUILDIN_DEF(getequiprefinerycnt,"i"),
  11442. BUILDIN_DEF(getequipweaponlv,"i"),
  11443. BUILDIN_DEF(getequippercentrefinery,"i"),
  11444. BUILDIN_DEF(successrefitem,"i"),
  11445. BUILDIN_DEF(failedrefitem,"i"),
  11446. BUILDIN_DEF(statusup,"i"),
  11447. BUILDIN_DEF(statusup2,"ii"),
  11448. BUILDIN_DEF(bonus,"iv"),
  11449. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  11450. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  11451. BUILDIN_DEF2(bonus,"bonus4","iviii"),
  11452. BUILDIN_DEF2(bonus,"bonus5","iviiii"),
  11453. BUILDIN_DEF(bonusautoscript,"si?"),
  11454. BUILDIN_DEF(bonusautoscript2,"si?"),
  11455. BUILDIN_DEF(skill,"vi?"),
  11456. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  11457. BUILDIN_DEF(guildskill,"vi"),
  11458. BUILDIN_DEF(getskilllv,"v"),
  11459. BUILDIN_DEF(getgdskilllv,"iv"),
  11460. BUILDIN_DEF(basicskillcheck,""),
  11461. BUILDIN_DEF(getgmlevel,""),
  11462. BUILDIN_DEF(end,""),
  11463. BUILDIN_DEF(checkoption,"i"),
  11464. BUILDIN_DEF(setoption,"i?"),
  11465. BUILDIN_DEF(setcart,"?"),
  11466. BUILDIN_DEF(checkcart,""),
  11467. BUILDIN_DEF(setfalcon,"?"),
  11468. BUILDIN_DEF(checkfalcon,""),
  11469. BUILDIN_DEF(setriding,"?"),
  11470. BUILDIN_DEF(checkriding,""),
  11471. BUILDIN_DEF2(savepoint,"save","sii"),
  11472. BUILDIN_DEF(savepoint,"sii"),
  11473. BUILDIN_DEF(gettimetick,"i"),
  11474. BUILDIN_DEF(gettime,"i"),
  11475. BUILDIN_DEF(gettimestr,"si"),
  11476. BUILDIN_DEF(openstorage,""),
  11477. BUILDIN_DEF(guildopenstorage,"*"),
  11478. BUILDIN_DEF(itemskill,"vi"),
  11479. BUILDIN_DEF(produce,"i"),
  11480. BUILDIN_DEF(monster,"siisii*"),
  11481. BUILDIN_DEF(getmobdrops,"i"),
  11482. BUILDIN_DEF(areamonster,"siiiisii*"),
  11483. BUILDIN_DEF(killmonster,"ss?"),
  11484. BUILDIN_DEF(killmonsterall,"s?"),
  11485. BUILDIN_DEF(clone,"siisi*"),
  11486. BUILDIN_DEF(doevent,"s"),
  11487. BUILDIN_DEF(donpcevent,"s"),
  11488. BUILDIN_DEF(cmdothernpc,"ss"),
  11489. BUILDIN_DEF(addtimer,"is"),
  11490. BUILDIN_DEF(deltimer,"s"),
  11491. BUILDIN_DEF(addtimercount,"si"),
  11492. BUILDIN_DEF(initnpctimer,"??"),
  11493. BUILDIN_DEF(stopnpctimer,"??"),
  11494. BUILDIN_DEF(startnpctimer,"??"),
  11495. BUILDIN_DEF(setnpctimer,"i?"),
  11496. BUILDIN_DEF(getnpctimer,"i?"),
  11497. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  11498. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  11499. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  11500. BUILDIN_DEF(announce,"si*"),
  11501. BUILDIN_DEF(mapannounce,"ssi*"),
  11502. BUILDIN_DEF(areaannounce,"siiiisi*"),
  11503. BUILDIN_DEF(getusers,"i"),
  11504. BUILDIN_DEF(getmapguildusers,"si"),
  11505. BUILDIN_DEF(getmapusers,"s"),
  11506. BUILDIN_DEF(getareausers,"siiii"),
  11507. BUILDIN_DEF(getareadropitem,"siiiii"),
  11508. BUILDIN_DEF(enablenpc,"s"),
  11509. BUILDIN_DEF(disablenpc,"s"),
  11510. BUILDIN_DEF(hideoffnpc,"s"),
  11511. BUILDIN_DEF(hideonnpc,"s"),
  11512. BUILDIN_DEF(sc_start,"iii?"),
  11513. BUILDIN_DEF(sc_start2,"iiii?"),
  11514. BUILDIN_DEF(sc_start4,"iiiiii?"),
  11515. BUILDIN_DEF(sc_end,"i?"),
  11516. BUILDIN_DEF(getscrate,"ii*"),
  11517. BUILDIN_DEF(debugmes,"s"),
  11518. BUILDIN_DEF2(catchpet,"pet","i"),
  11519. BUILDIN_DEF2(birthpet,"bpet",""),
  11520. BUILDIN_DEF(resetlvl,"i"),
  11521. BUILDIN_DEF(resetstatus,""),
  11522. BUILDIN_DEF(resetskill,""),
  11523. BUILDIN_DEF(skillpointcount,""),
  11524. BUILDIN_DEF(changebase,"i"),
  11525. BUILDIN_DEF(changesex,""),
  11526. BUILDIN_DEF(waitingroom,"si??"),
  11527. BUILDIN_DEF(delwaitingroom,"?"),
  11528. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  11529. BUILDIN_DEF(enablewaitingroomevent,"?"),
  11530. BUILDIN_DEF(disablewaitingroomevent,"?"),
  11531. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  11532. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  11533. BUILDIN_DEF(getwaitingroomstate,"i?"),
  11534. BUILDIN_DEF(warpwaitingpc,"sii?"),
  11535. BUILDIN_DEF(attachrid,"i"),
  11536. BUILDIN_DEF(detachrid,""),
  11537. BUILDIN_DEF(isloggedin,"i?"),
  11538. BUILDIN_DEF(setmapflagnosave,"ssii"),
  11539. BUILDIN_DEF(getmapflag,"si"),
  11540. BUILDIN_DEF(setmapflag,"si*"),
  11541. BUILDIN_DEF(removemapflag,"si"),
  11542. BUILDIN_DEF(pvpon,"s"),
  11543. BUILDIN_DEF(pvpoff,"s"),
  11544. BUILDIN_DEF(gvgon,"s"),
  11545. BUILDIN_DEF(gvgoff,"s"),
  11546. BUILDIN_DEF(emotion,"i*"),
  11547. BUILDIN_DEF(maprespawnguildid,"sii"),
  11548. BUILDIN_DEF(agitstart,""), // <Agit>
  11549. BUILDIN_DEF(agitend,""),
  11550. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  11551. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  11552. BUILDIN_DEF(getcastlename,"s"),
  11553. BUILDIN_DEF(getcastledata,"si*"),
  11554. BUILDIN_DEF(setcastledata,"sii"),
  11555. BUILDIN_DEF(requestguildinfo,"i*"),
  11556. BUILDIN_DEF(getequipcardcnt,"i"),
  11557. BUILDIN_DEF(successremovecards,"i"),
  11558. BUILDIN_DEF(failedremovecards,"ii"),
  11559. BUILDIN_DEF(marriage,"s"),
  11560. BUILDIN_DEF2(wedding_effect,"wedding",""),
  11561. BUILDIN_DEF(divorce,""),
  11562. BUILDIN_DEF(ispartneron,""),
  11563. BUILDIN_DEF(getpartnerid,""),
  11564. BUILDIN_DEF(getchildid,""),
  11565. BUILDIN_DEF(getmotherid,""),
  11566. BUILDIN_DEF(getfatherid,""),
  11567. BUILDIN_DEF(warppartner,"sii"),
  11568. BUILDIN_DEF(getitemname,"i"),
  11569. BUILDIN_DEF(getitemslots,"i"),
  11570. BUILDIN_DEF(makepet,"i"),
  11571. BUILDIN_DEF(getexp,"ii"),
  11572. BUILDIN_DEF(getinventorylist,""),
  11573. BUILDIN_DEF(getskilllist,""),
  11574. BUILDIN_DEF(clearitem,""),
  11575. BUILDIN_DEF(classchange,"ii"),
  11576. BUILDIN_DEF(misceffect,"i"),
  11577. BUILDIN_DEF(soundeffect,"si"),
  11578. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  11579. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  11580. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  11581. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  11582. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  11583. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  11584. BUILDIN_DEF(petloot,"i"), // [Valaris]
  11585. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  11586. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  11587. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  11588. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  11589. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  11590. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  11591. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  11592. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  11593. BUILDIN_DEF(nude,""), // nude command [Valaris]
  11594. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  11595. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  11596. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  11597. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  11598. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  11599. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  11600. BUILDIN_DEF(mobcount,"ss"),
  11601. BUILDIN_DEF(getlook,"i"),
  11602. BUILDIN_DEF(getsavepoint,"i"),
  11603. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  11604. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  11605. BUILDIN_DEF(npcstop,""), // [Valaris]
  11606. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  11607. BUILDIN_DEF(checkoption1,"i"),
  11608. BUILDIN_DEF(checkoption2,"i"),
  11609. BUILDIN_DEF(guildgetexp,"i"),
  11610. BUILDIN_DEF(guildchangegm,"is"),
  11611. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  11612. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  11613. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  11614. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  11615. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  11616. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  11617. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  11618. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  11619. BUILDIN_DEF(night,""), // sets the server to night time
  11620. BUILDIN_DEF(day,""), // sets the server to day time
  11621. #ifdef PCRE_SUPPORT
  11622. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  11623. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  11624. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  11625. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  11626. #endif
  11627. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  11628. BUILDIN_DEF(getusersname,"*"),
  11629. BUILDIN_DEF(recovery,""),
  11630. BUILDIN_DEF(getpetinfo,"i"),
  11631. BUILDIN_DEF(gethominfo,"i"),
  11632. BUILDIN_DEF(checkequipedcard,"i"),
  11633. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  11634. BUILDIN_DEF(globalmes,"s*"),
  11635. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  11636. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  11637. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  11638. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  11639. BUILDIN_DEF(setnpcdisplay,"sv??"),
  11640. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  11641. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  11642. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  11643. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  11644. // [zBuffer] List of mathematics commands --->
  11645. BUILDIN_DEF(sqrt,"i"),
  11646. BUILDIN_DEF(pow,"ii"),
  11647. BUILDIN_DEF(distance,"iiii"),
  11648. // <--- [zBuffer] List of mathematics commands
  11649. // [zBuffer] List of dynamic var commands --->
  11650. BUILDIN_DEF(getd,"*"),
  11651. BUILDIN_DEF(setd,"*"),
  11652. // <--- [zBuffer] List of dynamic var commands
  11653. BUILDIN_DEF(petstat,"i"),
  11654. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  11655. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  11656. BUILDIN_DEF(npcshopadditem,"sii*"),
  11657. BUILDIN_DEF(npcshopdelitem,"si*"),
  11658. BUILDIN_DEF(npcshopattach,"s?"),
  11659. BUILDIN_DEF(equip,"i"),
  11660. BUILDIN_DEF(autoequip,"ii"),
  11661. BUILDIN_DEF(setbattleflag,"ss"),
  11662. BUILDIN_DEF(getbattleflag,"s"),
  11663. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  11664. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  11665. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  11666. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  11667. BUILDIN_DEF(axtoi,"s"),
  11668. BUILDIN_DEF(query_sql,"s*"),
  11669. BUILDIN_DEF(query_logsql,"s*"),
  11670. BUILDIN_DEF(escape_sql,"s"),
  11671. BUILDIN_DEF(atoi,"s"),
  11672. // [zBuffer] List of player cont commands --->
  11673. BUILDIN_DEF(rid2name,"i"),
  11674. BUILDIN_DEF(pcfollow,"ii"),
  11675. BUILDIN_DEF(pcstopfollow,"i"),
  11676. BUILDIN_DEF(pcblockmove,"ii"),
  11677. // <--- [zBuffer] List of player cont commands
  11678. // [zBuffer] List of mob control commands --->
  11679. BUILDIN_DEF(unitwalk,"ii?"),
  11680. BUILDIN_DEF(unitkill,"i"),
  11681. BUILDIN_DEF(unitwarp,"isii"),
  11682. BUILDIN_DEF(unitattack,"iv?"),
  11683. BUILDIN_DEF(unitstop,"i"),
  11684. BUILDIN_DEF(unittalk,"is"),
  11685. BUILDIN_DEF(unitemote,"ii"),
  11686. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  11687. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  11688. // <--- [zBuffer] List of mob control commands
  11689. BUILDIN_DEF(sleep,"i"),
  11690. BUILDIN_DEF(sleep2,"i"),
  11691. BUILDIN_DEF(awake,"s"),
  11692. BUILDIN_DEF(getvariableofnpc,"rs"),
  11693. BUILDIN_DEF(warpportal,"iisii"),
  11694. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  11695. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  11696. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  11697. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  11698. BUILDIN_DEF(checkvending,"*"),
  11699. BUILDIN_DEF(checkchatting,"*"),
  11700. BUILDIN_DEF(openmail,""),
  11701. BUILDIN_DEF(openauction,""),
  11702. BUILDIN_DEF(checkcell,"siii"),
  11703. BUILDIN_DEF(setcell,"siiiiii"),
  11704. BUILDIN_DEF(getquest, "ii*"),
  11705. BUILDIN_DEF(deletequest, "i"),
  11706. BUILDIN_DEF(setquestobjective, "iisi"),
  11707. BUILDIN_DEF(setqueststatus, "ii"),
  11708. BUILDIN_DEF(hasquest, "i"),
  11709. BUILDIN_DEF(setwall,"siiiiis"),
  11710. BUILDIN_DEF(delwall,"s"),
  11711. BUILDIN_DEF(createmercenary,"ii"),
  11712. BUILDIN_DEF(mercenary_heal,"ii"),
  11713. BUILDIN_DEF(mercenary_sc_start,"iii"),
  11714. {NULL,NULL,NULL},
  11715. };