script.c 321 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226
  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 "mercenary.h" //[orn]
  28. #include "intif.h"
  29. #include "skill.h"
  30. #include "status.h"
  31. #include "chat.h"
  32. #include "battle.h"
  33. #include "party.h"
  34. #include "guild.h"
  35. #include "atcommand.h"
  36. #include "charcommand.h"
  37. #include "log.h"
  38. #include "unit.h"
  39. #include "pet.h"
  40. #include "mail.h"
  41. #include "script.h"
  42. #include "quest.h"
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <math.h>
  47. #ifndef WIN32
  48. #include <sys/time.h>
  49. #endif
  50. #include <time.h>
  51. #include <setjmp.h>
  52. #include <errno.h>
  53. ///////////////////////////////////////////////////////////////////////////////
  54. //## TODO possible enhancements: [FlavioJS]
  55. // - 'callfunc' supporting labels in the current npc "::LabelName"
  56. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  57. // - 'function FuncName;' function declarations reverting to global functions
  58. // if local label isn't found
  59. // - join callfunc and callsub's functionality
  60. // - use script_op2name in the DEBUG_DISASM block
  61. // - remove dynamic allocation in add_word()
  62. // - remove GETVALUE / SETVALUE
  63. // - clean up the set_reg / set_val / setd_sub mess
  64. //
  65. // struct script_state* st;
  66. //
  67. /// Returns the script_data at the target index
  68. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  69. /// Returns if the stack contains data at the target index
  70. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  71. /// Returns the index of the last data in the stack
  72. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  73. /// Pushes an int into the stack
  74. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  75. /// Pushes a string into the stack (script engine frees it automatically)
  76. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  77. /// Pushes a copy of a string into the stack
  78. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  79. /// Pushes a constant string into the stack (must never change or be freed)
  80. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  81. /// Pushes a nil into the stack
  82. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  83. /// Pushes a copy of the data in the target index
  84. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  85. #define script_isstring(st,i) data_isstring(script_getdata(st,i))
  86. #define script_isint(st,i) data_isint(script_getdata(st,i))
  87. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  88. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  89. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  90. // Note: "top" functions/defines use indexes relative to the top of the stack
  91. // -1 is the index of the data at the top
  92. /// Returns the script_data at the target index relative to the top of the stack
  93. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  94. /// Pushes a copy of the data in the target index relative to the top of the stack
  95. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  96. /// Removes the range of values [start,end[ relative to the top of the stack
  97. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  98. //
  99. // struct script_data* data;
  100. //
  101. /// Returns if the script data is a string
  102. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  103. /// Returns if the script data is an int
  104. #define data_isint(data) ( (data)->type == C_INT )
  105. /// Returns if the script data is a reference
  106. #define data_isreference(data) ( (data)->type == C_NAME )
  107. /// Returns if the script data is a label
  108. #define data_islabel(data) ( (data)->type == C_POS )
  109. /// Returns if the script data is an internal script function label
  110. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  111. /// Returns if this is a reference to a constant
  112. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  113. /// Returns if this a reference to a param
  114. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  115. /// Returns if this a reference to a variable
  116. //##TODO confirm it's C_NAME [FlavioJS]
  117. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  118. /// Returns the unique id of the reference (id and index)
  119. #define reference_getuid(data) ( (data)->u.num )
  120. /// Returns the id of the reference
  121. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  122. /// Returns the array index of the reference
  123. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  124. /// Returns the name of the reference
  125. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  126. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  127. #define reference_getref(data) ( (data)->ref )
  128. /// Returns the value of the constant
  129. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  130. /// Returns the type of param
  131. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  132. /// Composes the uid of a reference from the id and the index
  133. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  134. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.')
  135. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' )
  136. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  137. #define FETCH(n, t) \
  138. if( script_hasdata(st,n) ) \
  139. (t)=script_getnum(st,n);
  140. #define SCRIPT_BLOCK_SIZE 512
  141. enum { LABEL_NEXTLINE=1,LABEL_START };
  142. /// temporary buffer for passing around compiled bytecode
  143. /// @see add_scriptb, set_label, parse_script
  144. static unsigned char* script_buf = NULL;
  145. static int script_pos = 0, script_size = 0;
  146. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  147. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  148. // String buffer structures.
  149. // str_data stores string information
  150. static struct str_data_struct {
  151. enum c_op type;
  152. int str;
  153. int backpatch;
  154. int label;
  155. int (*func)(struct script_state *st);
  156. int val;
  157. int next;
  158. } *str_data = NULL;
  159. static int str_data_size = 0; // size of the data
  160. static int str_num = LABEL_START; // next id to be assigned
  161. // str_buf holds the strings themselves
  162. static char *str_buf;
  163. static int str_size = 0; // size of the buffer
  164. static int str_pos = 0; // next position to be assigned
  165. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  166. #define SCRIPT_HASH_SIZE 1021
  167. int str_hash[SCRIPT_HASH_SIZE];
  168. // Specifies which string hashing method to use
  169. //#define SCRIPT_HASH_DJB2
  170. //#define SCRIPT_HASH_SDBM
  171. #define SCRIPT_HASH_ELF
  172. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  173. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  174. static int parse_options=0;
  175. DBMap* script_get_label_db(){ return scriptlabel_db; }
  176. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  177. struct Script_Config script_config = {
  178. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  179. 0, INT_MAX, // input_min_value/input_max_value
  180. "OnPCDieEvent", //die_event_name
  181. "OnPCKillEvent", //kill_pc_event_name
  182. "OnNPCKillEvent", //kill_mob_event_name
  183. "OnPCLoginEvent", //login_event_name
  184. "OnPCLogoutEvent", //logout_event_name
  185. "OnPCLoadMapEvent", //loadmap_event_name
  186. "OnPCBaseLvUpEvent", //baselvup_event_name
  187. "OnPCJobLvUpEvent" //joblvup_event_name
  188. };
  189. static jmp_buf error_jump;
  190. static char* error_msg;
  191. static const char* error_pos;
  192. static int error_report; // if the error should produce output
  193. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  194. // [Eoe / jA 1080, 1081, 1094, 1164]
  195. enum curly_type {
  196. TYPE_NULL = 0,
  197. TYPE_IF,
  198. TYPE_SWITCH,
  199. TYPE_WHILE,
  200. TYPE_FOR,
  201. TYPE_DO,
  202. TYPE_USERFUNC,
  203. TYPE_ARGLIST // function argument list
  204. };
  205. #define ARGLIST_UNDEFINED 0
  206. #define ARGLIST_NO_PAREN 1
  207. #define ARGLIST_PAREN 2
  208. static struct {
  209. struct {
  210. enum curly_type type;
  211. int index;
  212. int count;
  213. int flag;
  214. struct linkdb_node *case_label;
  215. } curly[256]; // 右カッコの情報
  216. int curly_count; // 右カッコの数
  217. int index; // スクリプト内で使用した構文の数
  218. } syntax;
  219. const char* parse_curly_close(const char* p);
  220. const char* parse_syntax_close(const char* p);
  221. const char* parse_syntax_close_sub(const char* p,int* flag);
  222. const char* parse_syntax(const char* p);
  223. static int parse_syntax_for_flag = 0;
  224. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  225. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  226. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  227. int potion_target=0;
  228. c_op get_com(unsigned char *script,int *pos);
  229. int get_num(unsigned char *script,int *pos);
  230. typedef struct script_function {
  231. int (*func)(struct script_state *st);
  232. const char *name;
  233. const char *arg;
  234. } script_function;
  235. extern script_function buildin_func[];
  236. static struct linkdb_node *sleep_db;
  237. /*==========================================
  238. * ローカルプロトタイプ宣言 (必要な物のみ)
  239. *------------------------------------------*/
  240. const char* parse_subexpr(const char* p,int limit);
  241. void push_val(struct script_stack *stack,int type,int val);
  242. int run_func(struct script_state *st);
  243. enum {
  244. MF_NOMEMO, //0
  245. MF_NOTELEPORT,
  246. MF_NOSAVE,
  247. MF_NOBRANCH,
  248. MF_NOPENALTY,
  249. MF_NOZENYPENALTY,
  250. MF_PVP,
  251. MF_PVP_NOPARTY,
  252. MF_PVP_NOGUILD,
  253. MF_GVG,
  254. MF_GVG_NOPARTY, //10
  255. MF_NOTRADE,
  256. MF_NOSKILL,
  257. MF_NOWARP,
  258. MF_PARTYLOCK,
  259. MF_NOICEWALL,
  260. MF_SNOW,
  261. MF_FOG,
  262. MF_SAKURA,
  263. MF_LEAVES,
  264. MF_RAIN, //20
  265. MF_INDOORS,
  266. MF_NOGO,
  267. MF_CLOUDS,
  268. MF_CLOUDS2,
  269. MF_FIREWORKS,
  270. MF_GVG_CASTLE,
  271. MF_GVG_DUNGEON,
  272. MF_NIGHTENABLED,
  273. MF_NOBASEEXP,
  274. MF_NOJOBEXP, //30
  275. MF_NOMOBLOOT,
  276. MF_NOMVPLOOT,
  277. MF_NORETURN,
  278. MF_NOWARPTO,
  279. MF_NIGHTMAREDROP,
  280. MF_RESTRICTED,
  281. MF_NOCOMMAND,
  282. MF_NODROP,
  283. MF_JEXP,
  284. MF_BEXP, //40
  285. MF_NOVENDING,
  286. MF_LOADEVENT,
  287. MF_NOCHAT,
  288. MF_NOEXPPENALTY,
  289. MF_GUILDLOCK
  290. };
  291. const char* script_op2name(int op)
  292. {
  293. #define RETURN_OP_NAME(type) case type: return #type
  294. switch( op )
  295. {
  296. RETURN_OP_NAME(C_NOP);
  297. RETURN_OP_NAME(C_POS);
  298. RETURN_OP_NAME(C_INT);
  299. RETURN_OP_NAME(C_PARAM);
  300. RETURN_OP_NAME(C_FUNC);
  301. RETURN_OP_NAME(C_STR);
  302. RETURN_OP_NAME(C_CONSTSTR);
  303. RETURN_OP_NAME(C_ARG);
  304. RETURN_OP_NAME(C_NAME);
  305. RETURN_OP_NAME(C_EOL);
  306. RETURN_OP_NAME(C_RETINFO);
  307. RETURN_OP_NAME(C_USERFUNC);
  308. RETURN_OP_NAME(C_USERFUNC_POS);
  309. // operators
  310. RETURN_OP_NAME(C_OP3);
  311. RETURN_OP_NAME(C_LOR);
  312. RETURN_OP_NAME(C_LAND);
  313. RETURN_OP_NAME(C_LE);
  314. RETURN_OP_NAME(C_LT);
  315. RETURN_OP_NAME(C_GE);
  316. RETURN_OP_NAME(C_GT);
  317. RETURN_OP_NAME(C_EQ);
  318. RETURN_OP_NAME(C_NE);
  319. RETURN_OP_NAME(C_XOR);
  320. RETURN_OP_NAME(C_OR);
  321. RETURN_OP_NAME(C_AND);
  322. RETURN_OP_NAME(C_ADD);
  323. RETURN_OP_NAME(C_SUB);
  324. RETURN_OP_NAME(C_MUL);
  325. RETURN_OP_NAME(C_DIV);
  326. RETURN_OP_NAME(C_MOD);
  327. RETURN_OP_NAME(C_NEG);
  328. RETURN_OP_NAME(C_LNOT);
  329. RETURN_OP_NAME(C_NOT);
  330. RETURN_OP_NAME(C_R_SHIFT);
  331. RETURN_OP_NAME(C_L_SHIFT);
  332. default:
  333. ShowDebug("script_op2name: unexpected op=%d\n", op);
  334. return "???";
  335. }
  336. #undef RETURN_OP_NAME
  337. }
  338. /// Reports on the console the src of a script error.
  339. static void script_reportsrc(struct script_state *st)
  340. {
  341. struct block_list* bl;
  342. if( st->oid == 0 )
  343. return; //Can't report source.
  344. bl = map_id2bl(st->oid);
  345. if( bl == NULL )
  346. return;
  347. switch( bl->type )
  348. {
  349. case BL_NPC:
  350. if( bl->m >= 0 )
  351. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  352. else
  353. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  354. break;
  355. default:
  356. if( bl->m >= 0 )
  357. ShowDebug("Source (Non-NPC type %d): name %s at %s (%d,%d)\n", bl->type, status_get_name(bl), map[bl->m].name, bl->x, bl->y);
  358. else
  359. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  360. break;
  361. }
  362. }
  363. /// Reports on the console information about the script data.
  364. static void script_reportdata(struct script_data* data)
  365. {
  366. if( data == NULL )
  367. return;
  368. switch( data->type )
  369. {
  370. case C_NOP:// no value
  371. ShowDebug("Data: nothing (nil)\n");
  372. break;
  373. case C_INT:// number
  374. ShowDebug("Data: number value=%d\n", data->u.num);
  375. break;
  376. case C_STR:
  377. case C_CONSTSTR:// string
  378. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  379. break;
  380. case C_NAME:// reference
  381. if( reference_tovariable(data) )
  382. {// variable
  383. const char* name = reference_getname(data);
  384. if( not_array_variable(*name) )
  385. ShowDebug("Data: variable name='%s'\n", name);
  386. else
  387. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  388. }
  389. else if( reference_toconstant(data) )
  390. {// constant
  391. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  392. }
  393. else if( reference_toparam(data) )
  394. {// param
  395. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  396. }
  397. else
  398. {// ???
  399. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  400. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  401. }
  402. break;
  403. case C_POS:// label
  404. ShowDebug("Data: label pos=%d\n", data->u.num);
  405. break;
  406. default:
  407. ShowDebug("Data: %s\n", script_op2name(data->type));
  408. break;
  409. }
  410. }
  411. /*==========================================
  412. * エラーメッセージ出力
  413. *------------------------------------------*/
  414. static void disp_error_message2(const char *mes,const char *pos,int report)
  415. {
  416. error_msg = aStrdup(mes);
  417. error_pos = pos;
  418. error_report = report;
  419. longjmp( error_jump, 1 );
  420. }
  421. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  422. /// Checks event parameter validity
  423. static void check_event(struct script_state *st, const char *evt)
  424. {
  425. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  426. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  427. script_reportsrc(st);
  428. }
  429. }
  430. /*==========================================
  431. * Hashes the input string
  432. *------------------------------------------*/
  433. static unsigned int calc_hash(const char* p)
  434. {
  435. unsigned int h;
  436. #if defined(SCRIPT_HASH_DJB2)
  437. h = 5381;
  438. while( *p ) // hash*33 + c
  439. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  440. #elif defined(SCRIPT_HASH_SDBM)
  441. h = 0;
  442. while( *p ) // hash*65599 + c
  443. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  444. #elif defined(SCRIPT_HASH_ELF) // UNIX ELF hash
  445. h = 0;
  446. while( *p ){
  447. unsigned int g;
  448. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  449. g = h & 0xF0000000;
  450. if( g )
  451. {
  452. h ^= g >> 24;
  453. h &= ~g;
  454. }
  455. }
  456. #else // athena hash
  457. h = 0;
  458. while( *p )
  459. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 ) + (unsigned char)TOLOWER(*p++);
  460. #endif
  461. return h % SCRIPT_HASH_SIZE;
  462. }
  463. /*==========================================
  464. * str_data manipulation functions
  465. *------------------------------------------*/
  466. /// Looks up string using the provided id.
  467. const char* get_str(int id)
  468. {
  469. Assert( id >= LABEL_START && id < str_size );
  470. return str_buf+str_data[id].str;
  471. }
  472. /// Returns the uid of the string, or -1.
  473. static int search_str(const char* p)
  474. {
  475. int i;
  476. for( i = str_hash[calc_hash(p)]; i != 0; i = str_data[i].next )
  477. if( strcasecmp(get_str(i),p) == 0 )
  478. return i;
  479. return -1;
  480. }
  481. /// Stores a copy of the string and returns its id.
  482. /// If an identical string is already present, returns its id instead.
  483. int add_str(const char* p)
  484. {
  485. int i, h;
  486. int len;
  487. h = calc_hash(p);
  488. if( str_hash[h] == 0 )
  489. {// empty bucket, add new node here
  490. str_hash[h] = str_num;
  491. }
  492. else
  493. {// scan for end of list, or occurence of identical string
  494. for( i = str_hash[h]; ; i = str_data[i].next )
  495. {
  496. if( strcasecmp(get_str(i),p) == 0 )
  497. return i; // string already in list
  498. if( str_data[i].next == 0 )
  499. break; // reached the end
  500. }
  501. // append node to end of list
  502. str_data[i].next = str_num;
  503. }
  504. // grow list if neccessary
  505. if( str_num >= str_data_size )
  506. {
  507. str_data_size += 128;
  508. RECREATE(str_data,struct str_data_struct,str_data_size);
  509. memset(str_data + (str_data_size - 128), '\0', 128);
  510. }
  511. len=(int)strlen(p);
  512. // grow string buffer if neccessary
  513. while( str_pos+len+1 >= str_size )
  514. {
  515. str_size += 256;
  516. RECREATE(str_buf,char,str_size);
  517. memset(str_buf + (str_size - 256), '\0', 256);
  518. }
  519. safestrncpy(str_buf+str_pos, p, len+1);
  520. str_data[str_num].type = C_NOP;
  521. str_data[str_num].str = str_pos;
  522. str_data[str_num].next = 0;
  523. str_data[str_num].func = NULL;
  524. str_data[str_num].backpatch = -1;
  525. str_data[str_num].label = -1;
  526. str_pos += len+1;
  527. return str_num++;
  528. }
  529. /// Appends 1 byte to the script buffer.
  530. static void add_scriptb(int a)
  531. {
  532. if( script_pos+1 >= script_size )
  533. {
  534. script_size += SCRIPT_BLOCK_SIZE;
  535. RECREATE(script_buf,unsigned char,script_size);
  536. }
  537. script_buf[script_pos++] = (uint8)(a);
  538. }
  539. /// Appends a c_op value to the script buffer.
  540. /// The value is variable-length encoded into 8-bit blocks.
  541. /// The encoding scheme is ( 01?????? )* 00??????, LSB first.
  542. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  543. static void add_scriptc(int a)
  544. {
  545. while( a >= 0x40 )
  546. {
  547. add_scriptb((a&0x3f)|0x40);
  548. a = (a - 0x40) >> 6;
  549. }
  550. add_scriptb(a);
  551. }
  552. /// Appends an integer value to the script buffer.
  553. /// The value is variable-length encoded into 8-bit blocks.
  554. /// The encoding scheme is ( 11?????? )* 10??????, LSB first.
  555. /// All blocks but the last hold 7 bits of data, topmost bit is always 1 (carries).
  556. static void add_scripti(int a)
  557. {
  558. while( a >= 0x40 )
  559. {
  560. add_scriptb((a&0x3f)|0xc0);
  561. a = (a - 0x40) >> 6;
  562. }
  563. add_scriptb(a|0x80);
  564. }
  565. /// Appends a str_data object (label/function/variable/integer) to the script buffer.
  566. ///
  567. /// @param l The id of the str_data entry
  568. // 最大16Mまで
  569. static void add_scriptl(int l)
  570. {
  571. int backpatch = str_data[l].backpatch;
  572. switch(str_data[l].type){
  573. case C_POS:
  574. case C_USERFUNC_POS:
  575. add_scriptc(C_POS);
  576. add_scriptb(str_data[l].label);
  577. add_scriptb(str_data[l].label>>8);
  578. add_scriptb(str_data[l].label>>16);
  579. break;
  580. case C_NOP:
  581. case C_USERFUNC:
  582. // ラベルの可能性があるのでbackpatch用データ埋め込み
  583. add_scriptc(C_NAME);
  584. str_data[l].backpatch = script_pos;
  585. add_scriptb(backpatch);
  586. add_scriptb(backpatch>>8);
  587. add_scriptb(backpatch>>16);
  588. break;
  589. case C_INT:
  590. add_scripti(abs(str_data[l].val));
  591. if( str_data[l].val < 0 ) //Notice that this is negative, from jA (Rayce)
  592. add_scriptc(C_NEG);
  593. break;
  594. default: // assume C_NAME
  595. add_scriptc(C_NAME);
  596. add_scriptb(l);
  597. add_scriptb(l>>8);
  598. add_scriptb(l>>16);
  599. break;
  600. }
  601. }
  602. /*==========================================
  603. * ラベルを解決する
  604. *------------------------------------------*/
  605. void set_label(int l,int pos, const char* script_pos)
  606. {
  607. int i,next;
  608. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  609. { //Prevent overwriting constants values and parameters [Skotlex]
  610. disp_error_message("set_label: invalid label name",script_pos);
  611. return;
  612. }
  613. if(str_data[l].label!=-1){
  614. disp_error_message("set_label: dup label ",script_pos);
  615. return;
  616. }
  617. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  618. str_data[l].label=pos;
  619. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  620. next=GETVALUE(script_buf,i);
  621. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  622. SETVALUE(script_buf,i,pos);
  623. i=next;
  624. }
  625. }
  626. /// Skips spaces and/or comments.
  627. const char* skip_space(const char* p)
  628. {
  629. if( p == NULL )
  630. return NULL;
  631. for(;;)
  632. {
  633. while( ISSPACE(*p) )
  634. ++p;
  635. if( *p == '/' && p[1] == '/' )
  636. {// line comment
  637. while(*p && *p!='\n')
  638. ++p;
  639. }
  640. else if( *p == '/' && p[1] == '*' )
  641. {// block comment
  642. p += 2;
  643. for(;;)
  644. {
  645. if( *p == '\0' )
  646. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  647. if( *p == '*' && p[1] == '/' )
  648. {// end of block comment
  649. p += 2;
  650. break;
  651. }
  652. ++p;
  653. }
  654. }
  655. else
  656. break;
  657. }
  658. return p;
  659. }
  660. /// Skips a word.
  661. /// A word consists of undercores and/or alfanumeric characters,
  662. /// and valid variable prefixes/postfixes.
  663. static
  664. const char* skip_word(const char* p)
  665. {
  666. // prefix
  667. switch( *p )
  668. {
  669. case '@':// temporary char variable
  670. ++p; break;
  671. case '#':// account variable
  672. p += ( p[1] == '#' ? 2 : 1 ); break;
  673. case '.':// npc variable
  674. p += ( p[1] == '@' ? 2 : 1 ); break;
  675. case '$':// global variable
  676. p += ( p[1] == '@' ? 2 : 1 ); break;
  677. }
  678. while( ISALNUM(*p) || *p == '_' )
  679. ++p;
  680. // postfix
  681. if( *p == '$' )// string
  682. p++;
  683. return p;
  684. }
  685. /// Adds a word to str_data.
  686. /// @see skip_word
  687. /// @see add_str
  688. static
  689. int add_word(const char* p)
  690. {
  691. char* word;
  692. int len;
  693. int i;
  694. // Check for a word
  695. len = skip_word(p) - p;
  696. if( len == 0 )
  697. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  698. // Duplicate the word
  699. CREATE(word, char, len+1);
  700. memcpy(word, p, len);
  701. word[len] = 0;
  702. // add the word
  703. i = add_str(word);
  704. aFree(word);
  705. return i;
  706. }
  707. /// Parses a function call.
  708. /// The argument list can have parenthesis or not.
  709. /// The number of arguments is checked.
  710. static
  711. const char* parse_callfunc(const char* p, int require_paren)
  712. {
  713. const char* p2;
  714. const char* arg=NULL;
  715. int func;
  716. func = add_word(p);
  717. if( str_data[func].type == C_FUNC ){
  718. // buildin function
  719. add_scriptl(func);
  720. add_scriptc(C_ARG);
  721. arg = buildin_func[str_data[func].val].arg;
  722. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  723. // script defined function
  724. int callsub = search_str("callsub");
  725. add_scriptl(callsub);
  726. add_scriptc(C_ARG);
  727. add_scriptl(func);
  728. arg = buildin_func[str_data[callsub].val].arg;
  729. if( *arg == 0 )
  730. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  731. if( *arg != '*' )
  732. ++arg; // count func as argument
  733. } else
  734. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  735. p = skip_word(p);
  736. p = skip_space(p);
  737. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  738. syntax.curly[syntax.curly_count].count = 0;
  739. if( *p == ';' )
  740. {// <func name> ';'
  741. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  742. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  743. {// <func name> '(' ')'
  744. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  745. p = p2;
  746. /*
  747. } else if( 0 && require_paren && *p != '(' )
  748. {// <func name>
  749. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  750. */
  751. } else
  752. {// <func name> <arg list>
  753. if( require_paren ){
  754. if( *p != '(' )
  755. disp_error_message("need '('",p);
  756. ++p; // skip '('
  757. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  758. } else if( *p == '(' ){
  759. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  760. } else {
  761. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  762. }
  763. ++syntax.curly_count;
  764. while( *arg ) {
  765. p2=parse_subexpr(p,-1);
  766. if( p == p2 )
  767. break; // not an argument
  768. if( *arg != '*' )
  769. ++arg; // next argument
  770. p=skip_space(p2);
  771. if( *arg == 0 || *p != ',' )
  772. break; // no more arguments
  773. ++p; // skip comma
  774. }
  775. --syntax.curly_count;
  776. }
  777. if( *arg && *arg != '?' && *arg != '*' )
  778. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  779. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  780. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  781. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  782. if( *p != ')' )
  783. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  784. ++p;
  785. }
  786. add_scriptc(C_FUNC);
  787. return p;
  788. }
  789. /*==========================================
  790. * 項の解析
  791. *------------------------------------------*/
  792. const char* parse_simpleexpr(const char *p)
  793. {
  794. int i;
  795. p=skip_space(p);
  796. if(*p==';' || *p==',')
  797. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  798. if(*p=='('){
  799. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  800. ++syntax.curly[i].count;
  801. p=parse_subexpr(p+1,-1);
  802. p=skip_space(p);
  803. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  804. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  805. ){
  806. if( *p == ',' ){
  807. syntax.curly[i].flag = ARGLIST_PAREN;
  808. return p;
  809. } else
  810. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  811. }
  812. if( *p != ')' )
  813. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  814. ++p;
  815. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  816. char *np;
  817. i=strtoul(p,&np,0);
  818. add_scripti(i);
  819. p=np;
  820. } else if(*p=='"'){
  821. add_scriptc(C_STR);
  822. p++;
  823. while( *p && *p != '"' ){
  824. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  825. {
  826. char buf[8];
  827. size_t len = skip_escaped_c(p) - p;
  828. size_t n = sv_unescape_c(buf, p, len);
  829. if( n != 1 )
  830. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  831. p += len;
  832. add_scriptb(*buf);
  833. continue;
  834. }
  835. else if( *p == '\n' )
  836. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  837. add_scriptb(*p++);
  838. }
  839. if(!*p)
  840. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  841. add_scriptb(0);
  842. p++; //'"'
  843. } else {
  844. int l;
  845. // label , register , function etc
  846. if(skip_word(p)==p)
  847. disp_error_message("parse_simpleexpr: unexpected character",p);
  848. l=add_word(p);
  849. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  850. return parse_callfunc(p,1);
  851. p=skip_word(p);
  852. if( *p == '[' ){
  853. // array(name[i] => getelementofarray(name,i) )
  854. add_scriptl(search_str("getelementofarray"));
  855. add_scriptc(C_ARG);
  856. add_scriptl(l);
  857. p=parse_subexpr(p+1,-1);
  858. p=skip_space(p);
  859. if( *p != ']' )
  860. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  861. ++p;
  862. add_scriptc(C_FUNC);
  863. }else
  864. add_scriptl(l);
  865. }
  866. return p;
  867. }
  868. /*==========================================
  869. * 式の解析
  870. *------------------------------------------*/
  871. const char* parse_subexpr(const char* p,int limit)
  872. {
  873. int op,opl,len;
  874. const char* tmpp;
  875. p=skip_space(p);
  876. if(*p=='-'){
  877. tmpp=skip_space(p+1);
  878. if(*tmpp==';' || *tmpp==','){
  879. add_scriptl(LABEL_NEXTLINE);
  880. p++;
  881. return p;
  882. }
  883. }
  884. tmpp=p;
  885. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  886. p=parse_subexpr(p+1,10);
  887. add_scriptc(op);
  888. } else
  889. p=parse_simpleexpr(p);
  890. p=skip_space(p);
  891. while((
  892. (op=C_OP3,opl=0,len=1,*p=='?') ||
  893. (op=C_ADD,opl=8,len=1,*p=='+') ||
  894. (op=C_SUB,opl=8,len=1,*p=='-') ||
  895. (op=C_MUL,opl=9,len=1,*p=='*') ||
  896. (op=C_DIV,opl=9,len=1,*p=='/') ||
  897. (op=C_MOD,opl=9,len=1,*p=='%') ||
  898. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  899. (op=C_AND,opl=6,len=1,*p=='&') ||
  900. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  901. (op=C_OR,opl=5,len=1,*p=='|') ||
  902. (op=C_XOR,opl=4,len=1,*p=='^') ||
  903. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  904. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  905. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  906. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  907. (op=C_GT,opl=3,len=1,*p=='>') ||
  908. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  909. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  910. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  911. p+=len;
  912. if(op == C_OP3) {
  913. p=parse_subexpr(p,-1);
  914. p=skip_space(p);
  915. if( *(p++) != ':')
  916. disp_error_message("parse_subexpr: need ':'", p-1);
  917. p=parse_subexpr(p,-1);
  918. } else {
  919. p=parse_subexpr(p,opl);
  920. }
  921. add_scriptc(op);
  922. p=skip_space(p);
  923. }
  924. return p; /* return first untreated operator */
  925. }
  926. /*==========================================
  927. * 式の評価
  928. *------------------------------------------*/
  929. const char* parse_expr(const char *p)
  930. {
  931. switch(*p){
  932. case ')': case ';': case ':': case '[': case ']':
  933. case '}':
  934. disp_error_message("parse_expr: unexpected char",p);
  935. }
  936. p=parse_subexpr(p,-1);
  937. return p;
  938. }
  939. /*==========================================
  940. * 行の解析
  941. *------------------------------------------*/
  942. const char* parse_line(const char* p)
  943. {
  944. const char* p2;
  945. p=skip_space(p);
  946. if(*p==';') {
  947. // if(); for(); while(); のために閉じ判定
  948. p = parse_syntax_close(p + 1);
  949. return p;
  950. }
  951. if(*p==')' && parse_syntax_for_flag)
  952. return p+1;
  953. p = skip_space(p);
  954. if(p[0] == '{') {
  955. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  956. syntax.curly[syntax.curly_count].count = -1;
  957. syntax.curly[syntax.curly_count].index = -1;
  958. syntax.curly_count++;
  959. return p + 1;
  960. } else if(p[0] == '}') {
  961. return parse_curly_close(p);
  962. }
  963. // 構文関連の処理
  964. p2 = parse_syntax(p);
  965. if(p2 != NULL)
  966. return p2;
  967. p = parse_callfunc(p,0);
  968. p = skip_space(p);
  969. if(parse_syntax_for_flag) {
  970. if( *p != ')' )
  971. disp_error_message("parse_line: need ')'",p);
  972. } else {
  973. if( *p != ';' )
  974. disp_error_message("parse_line: need ';'",p);
  975. }
  976. // if, for , while の閉じ判定
  977. p = parse_syntax_close(p+1);
  978. return p;
  979. }
  980. // { ... } の閉じ処理
  981. const char* parse_curly_close(const char* p)
  982. {
  983. if(syntax.curly_count <= 0) {
  984. disp_error_message("parse_curly_close: unexpected string",p);
  985. return p + 1;
  986. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  987. syntax.curly_count--;
  988. // if, for , while の閉じ判定
  989. p = parse_syntax_close(p + 1);
  990. return p;
  991. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  992. // switch() 閉じ判定
  993. int pos = syntax.curly_count-1;
  994. char label[256];
  995. int l;
  996. // 一時変数を消す
  997. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  998. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  999. parse_line(label);
  1000. syntax.curly_count--;
  1001. // 無条件で終了ポインタに移動
  1002. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1003. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1004. parse_line(label);
  1005. syntax.curly_count--;
  1006. // 現在地のラベルを付ける
  1007. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1008. l=add_str(label);
  1009. set_label(l,script_pos, p);
  1010. if(syntax.curly[pos].flag) {
  1011. // default が存在する
  1012. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1013. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1014. parse_line(label);
  1015. syntax.curly_count--;
  1016. }
  1017. // 終了ラベルを付ける
  1018. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1019. l=add_str(label);
  1020. set_label(l,script_pos, p);
  1021. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1022. syntax.curly_count--;
  1023. return p+1;
  1024. } else {
  1025. disp_error_message("parse_curly_close: unexpected string",p);
  1026. return p + 1;
  1027. }
  1028. }
  1029. // 構文関連の処理
  1030. // break, case, continue, default, do, for, function,
  1031. // if, switch, while をこの内部で処理します。
  1032. const char* parse_syntax(const char* p)
  1033. {
  1034. const char *p2 = skip_word(p);
  1035. switch(*p) {
  1036. case 'B':
  1037. case 'b':
  1038. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1039. // break の処理
  1040. char label[256];
  1041. int pos = syntax.curly_count - 1;
  1042. while(pos >= 0) {
  1043. if(syntax.curly[pos].type == TYPE_DO) {
  1044. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1045. break;
  1046. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1047. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1048. break;
  1049. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1050. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1051. break;
  1052. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1053. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1054. break;
  1055. }
  1056. pos--;
  1057. }
  1058. if(pos < 0) {
  1059. disp_error_message("parse_syntax: unexpected 'break'",p);
  1060. } else {
  1061. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1062. parse_line(label);
  1063. syntax.curly_count--;
  1064. }
  1065. p = skip_space(p2);
  1066. if(*p != ';')
  1067. disp_error_message("parse_syntax: need ';'",p);
  1068. // if, for , while の閉じ判定
  1069. p = parse_syntax_close(p + 1);
  1070. return p;
  1071. }
  1072. break;
  1073. case 'c':
  1074. case 'C':
  1075. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1076. // case の処理
  1077. int pos = syntax.curly_count-1;
  1078. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1079. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1080. return p+1;
  1081. } else {
  1082. char label[256];
  1083. int l,v;
  1084. char *np;
  1085. if(syntax.curly[pos].count != 1) {
  1086. // FALLTHRU 用のジャンプ
  1087. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1088. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1089. parse_line(label);
  1090. syntax.curly_count--;
  1091. // 現在地のラベルを付ける
  1092. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1093. l=add_str(label);
  1094. set_label(l,script_pos, p);
  1095. }
  1096. // switch 判定文
  1097. p = skip_space(p2);
  1098. if(p == p2) {
  1099. disp_error_message("parse_syntax: expect space ' '",p);
  1100. }
  1101. // check whether case label is integer or not
  1102. v = strtol(p,&np,0);
  1103. if(np == p) { //Check for constants
  1104. p2 = skip_word(p);
  1105. v = p2-p; // length of word at p2
  1106. memcpy(label,p,v);
  1107. label[v]='\0';
  1108. v = search_str(label);
  1109. if (v < 0 || str_data[v].type != C_INT)
  1110. disp_error_message("parse_syntax: 'case' label not integer",p);
  1111. v = str_data[v].val;
  1112. p = skip_word(p);
  1113. } else { //Numeric value
  1114. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1115. p++;
  1116. p = skip_word(p);
  1117. if(np != p)
  1118. disp_error_message("parse_syntax: 'case' label not integer",np);
  1119. }
  1120. p = skip_space(p);
  1121. if(*p != ':')
  1122. disp_error_message("parse_syntax: expect ':'",p);
  1123. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1124. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1125. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1126. // 2回parse しないとダメ
  1127. p2 = parse_line(label);
  1128. parse_line(p2);
  1129. syntax.curly_count--;
  1130. if(syntax.curly[pos].count != 1) {
  1131. // FALLTHRU 終了後のラベル
  1132. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1133. l=add_str(label);
  1134. set_label(l,script_pos,p);
  1135. }
  1136. // check duplication of case label [Rayce]
  1137. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1138. disp_error_message("parse_syntax: dup 'case'",p);
  1139. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1140. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1141. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1142. parse_line(label);
  1143. syntax.curly_count--;
  1144. syntax.curly[pos].count++;
  1145. }
  1146. return p + 1;
  1147. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1148. // continue の処理
  1149. char label[256];
  1150. int pos = syntax.curly_count - 1;
  1151. while(pos >= 0) {
  1152. if(syntax.curly[pos].type == TYPE_DO) {
  1153. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1154. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1155. break;
  1156. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1157. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1158. break;
  1159. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1160. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1161. break;
  1162. }
  1163. pos--;
  1164. }
  1165. if(pos < 0) {
  1166. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1167. } else {
  1168. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1169. parse_line(label);
  1170. syntax.curly_count--;
  1171. }
  1172. p = skip_space(p2);
  1173. if(*p != ';')
  1174. disp_error_message("parse_syntax: need ';'",p);
  1175. // if, for , while の閉じ判定
  1176. p = parse_syntax_close(p + 1);
  1177. return p;
  1178. }
  1179. break;
  1180. case 'd':
  1181. case 'D':
  1182. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1183. // switch - default の処理
  1184. int pos = syntax.curly_count-1;
  1185. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1186. disp_error_message("parse_syntax: unexpected 'default'",p);
  1187. } else if(syntax.curly[pos].flag) {
  1188. disp_error_message("parse_syntax: dup 'default'",p);
  1189. } else {
  1190. char label[256];
  1191. int l;
  1192. // 現在地のラベルを付ける
  1193. p = skip_space(p2);
  1194. if(*p != ':') {
  1195. disp_error_message("parse_syntax: need ':'",p);
  1196. }
  1197. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1198. l=add_str(label);
  1199. set_label(l,script_pos,p);
  1200. // 無条件で次のリンクに飛ばす
  1201. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1202. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1203. parse_line(label);
  1204. syntax.curly_count--;
  1205. // default のラベルを付ける
  1206. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1207. l=add_str(label);
  1208. set_label(l,script_pos,p);
  1209. syntax.curly[syntax.curly_count - 1].flag = 1;
  1210. syntax.curly[pos].count++;
  1211. }
  1212. return p + 1;
  1213. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1214. int l;
  1215. char label[256];
  1216. p=skip_space(p2);
  1217. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1218. syntax.curly[syntax.curly_count].count = 1;
  1219. syntax.curly[syntax.curly_count].index = syntax.index++;
  1220. syntax.curly[syntax.curly_count].flag = 0;
  1221. // 現在地のラベル形成する
  1222. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1223. l=add_str(label);
  1224. set_label(l,script_pos,p);
  1225. syntax.curly_count++;
  1226. return p;
  1227. }
  1228. break;
  1229. case 'f':
  1230. case 'F':
  1231. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1232. int l;
  1233. char label[256];
  1234. int pos = syntax.curly_count;
  1235. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1236. syntax.curly[syntax.curly_count].count = 1;
  1237. syntax.curly[syntax.curly_count].index = syntax.index++;
  1238. syntax.curly[syntax.curly_count].flag = 0;
  1239. syntax.curly_count++;
  1240. p=skip_space(p2);
  1241. if(*p != '(')
  1242. disp_error_message("parse_syntax: need '('",p);
  1243. p++;
  1244. // 初期化文を実行する
  1245. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1246. p=parse_line(p);
  1247. syntax.curly_count--;
  1248. // 条件判断開始のラベル形成する
  1249. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1250. l=add_str(label);
  1251. set_label(l,script_pos,p);
  1252. p=skip_space(p);
  1253. if(*p == ';') {
  1254. // for(;;) のパターンなので必ず真
  1255. ;
  1256. } else {
  1257. // 条件が偽なら終了地点に飛ばす
  1258. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1259. add_scriptl(add_str("jump_zero"));
  1260. add_scriptc(C_ARG);
  1261. p=parse_expr(p);
  1262. p=skip_space(p);
  1263. add_scriptl(add_str(label));
  1264. add_scriptc(C_FUNC);
  1265. }
  1266. if(*p != ';')
  1267. disp_error_message("parse_syntax: need ';'",p);
  1268. p++;
  1269. // ループ開始に飛ばす
  1270. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1271. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1272. parse_line(label);
  1273. syntax.curly_count--;
  1274. // 次のループへのラベル形成する
  1275. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1276. l=add_str(label);
  1277. set_label(l,script_pos,p);
  1278. // 次のループに入る時の処理
  1279. // for 最後の ')' を ';' として扱うフラグ
  1280. parse_syntax_for_flag = 1;
  1281. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1282. p=parse_line(p);
  1283. syntax.curly_count--;
  1284. parse_syntax_for_flag = 0;
  1285. // 条件判定処理に飛ばす
  1286. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1287. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1288. parse_line(label);
  1289. syntax.curly_count--;
  1290. // ループ開始のラベル付け
  1291. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1292. l=add_str(label);
  1293. set_label(l,script_pos,p);
  1294. return p;
  1295. }
  1296. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1297. {// internal script function
  1298. const char *func_name;
  1299. func_name = skip_space(p2);
  1300. p = skip_word(func_name);
  1301. if( p == func_name )
  1302. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1303. p2 = skip_space(p);
  1304. if( *p2 == ';' )
  1305. {// function <name> ;
  1306. // function declaration - just register the name
  1307. int l;
  1308. l = add_word(func_name);
  1309. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1310. str_data[l].type = C_USERFUNC;
  1311. // if, for , while の閉じ判定
  1312. p = parse_syntax_close(p2 + 1);
  1313. return p; }
  1314. else if(*p2 == '{')
  1315. {// function <name> <line/block of code>
  1316. char label[256];
  1317. int l;
  1318. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1319. syntax.curly[syntax.curly_count].count = 1;
  1320. syntax.curly[syntax.curly_count].index = syntax.index++;
  1321. syntax.curly[syntax.curly_count].flag = 0;
  1322. ++syntax.curly_count;
  1323. // Jump over the function code
  1324. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1325. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1326. ++syntax.curly_count;
  1327. parse_line(label);
  1328. --syntax.curly_count;
  1329. // Set the position of the function (label)
  1330. l=add_word(func_name);
  1331. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1332. str_data[l].type = C_USERFUNC;
  1333. set_label(l, script_pos, p);
  1334. if( parse_options&SCRIPT_USE_LABEL_DB )
  1335. strdb_put(scriptlabel_db, get_str(l), (void*)script_pos);
  1336. return skip_space(p);
  1337. }
  1338. else
  1339. {
  1340. disp_error_message("expect ';' or '{' at function syntax",p);
  1341. }
  1342. }
  1343. break;
  1344. case 'i':
  1345. case 'I':
  1346. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1347. // if() の処理
  1348. char label[256];
  1349. p=skip_space(p2);
  1350. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1351. disp_error_message("need '('",p);
  1352. }
  1353. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1354. syntax.curly[syntax.curly_count].count = 1;
  1355. syntax.curly[syntax.curly_count].index = syntax.index++;
  1356. syntax.curly[syntax.curly_count].flag = 0;
  1357. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1358. syntax.curly_count++;
  1359. add_scriptl(add_str("jump_zero"));
  1360. add_scriptc(C_ARG);
  1361. p=parse_expr(p);
  1362. p=skip_space(p);
  1363. add_scriptl(add_str(label));
  1364. add_scriptc(C_FUNC);
  1365. return p;
  1366. }
  1367. break;
  1368. case 's':
  1369. case 'S':
  1370. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1371. // switch() の処理
  1372. char label[256];
  1373. p=skip_space(p2);
  1374. if(*p != '(') {
  1375. disp_error_message("need '('",p);
  1376. }
  1377. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1378. syntax.curly[syntax.curly_count].count = 1;
  1379. syntax.curly[syntax.curly_count].index = syntax.index++;
  1380. syntax.curly[syntax.curly_count].flag = 0;
  1381. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1382. syntax.curly_count++;
  1383. add_scriptl(add_str("set"));
  1384. add_scriptc(C_ARG);
  1385. add_scriptl(add_str(label));
  1386. p=parse_expr(p);
  1387. p=skip_space(p);
  1388. if(*p != '{') {
  1389. disp_error_message("parse_syntax: need '{'",p);
  1390. }
  1391. add_scriptc(C_FUNC);
  1392. return p + 1;
  1393. }
  1394. break;
  1395. case 'w':
  1396. case 'W':
  1397. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1398. int l;
  1399. char label[256];
  1400. p=skip_space(p2);
  1401. if(*p != '(') {
  1402. disp_error_message("need '('",p);
  1403. }
  1404. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1405. syntax.curly[syntax.curly_count].count = 1;
  1406. syntax.curly[syntax.curly_count].index = syntax.index++;
  1407. syntax.curly[syntax.curly_count].flag = 0;
  1408. // 条件判断開始のラベル形成する
  1409. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1410. l=add_str(label);
  1411. set_label(l,script_pos,p);
  1412. // 条件が偽なら終了地点に飛ばす
  1413. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1414. syntax.curly_count++;
  1415. add_scriptl(add_str("jump_zero"));
  1416. add_scriptc(C_ARG);
  1417. p=parse_expr(p);
  1418. p=skip_space(p);
  1419. add_scriptl(add_str(label));
  1420. add_scriptc(C_FUNC);
  1421. return p;
  1422. }
  1423. break;
  1424. }
  1425. return NULL;
  1426. }
  1427. const char* parse_syntax_close(const char *p) {
  1428. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1429. int flag;
  1430. do {
  1431. p = parse_syntax_close_sub(p,&flag);
  1432. } while(flag);
  1433. return p;
  1434. }
  1435. // if, for , while , do の閉じ判定
  1436. // flag == 1 : 閉じられた
  1437. // flag == 0 : 閉じられない
  1438. const char* parse_syntax_close_sub(const char* p,int* flag)
  1439. {
  1440. char label[256];
  1441. int pos = syntax.curly_count - 1;
  1442. int l;
  1443. *flag = 1;
  1444. if(syntax.curly_count <= 0) {
  1445. *flag = 0;
  1446. return p;
  1447. } else if(syntax.curly[pos].type == TYPE_IF) {
  1448. const char *bp = p;
  1449. const char *p2;
  1450. // if 最終場所へ飛ばす
  1451. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1452. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1453. parse_line(label);
  1454. syntax.curly_count--;
  1455. // 現在地のラベルを付ける
  1456. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1457. l=add_str(label);
  1458. set_label(l,script_pos,p);
  1459. syntax.curly[pos].count++;
  1460. p = skip_space(p);
  1461. p2 = skip_word(p);
  1462. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1463. // else or else - if
  1464. p = skip_space(p2);
  1465. p2 = skip_word(p);
  1466. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1467. // else - if
  1468. p=skip_space(p2);
  1469. if(*p != '(') {
  1470. disp_error_message("need '('",p);
  1471. }
  1472. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1473. add_scriptl(add_str("jump_zero"));
  1474. add_scriptc(C_ARG);
  1475. p=parse_expr(p);
  1476. p=skip_space(p);
  1477. add_scriptl(add_str(label));
  1478. add_scriptc(C_FUNC);
  1479. *flag = 0;
  1480. return p;
  1481. } else {
  1482. // else
  1483. if(!syntax.curly[pos].flag) {
  1484. syntax.curly[pos].flag = 1;
  1485. *flag = 0;
  1486. return p;
  1487. }
  1488. }
  1489. }
  1490. // if 閉じ
  1491. syntax.curly_count--;
  1492. // 最終地のラベルを付ける
  1493. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1494. l=add_str(label);
  1495. set_label(l,script_pos,p);
  1496. if(syntax.curly[pos].flag == 1) {
  1497. // このifに対するelseじゃないのでポインタの位置は同じ
  1498. return bp;
  1499. }
  1500. return p;
  1501. } else if(syntax.curly[pos].type == TYPE_DO) {
  1502. int l;
  1503. char label[256];
  1504. const char *p2;
  1505. if(syntax.curly[pos].flag) {
  1506. // 現在地のラベル形成する(continue でここに来る)
  1507. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1508. l=add_str(label);
  1509. set_label(l,script_pos,p);
  1510. }
  1511. // 条件が偽なら終了地点に飛ばす
  1512. p = skip_space(p);
  1513. p2 = skip_word(p);
  1514. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1515. disp_error_message("parse_syntax: need 'while'",p);
  1516. p = skip_space(p2);
  1517. if(*p != '(') {
  1518. disp_error_message("need '('",p);
  1519. }
  1520. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1521. add_scriptl(add_str("jump_zero"));
  1522. add_scriptc(C_ARG);
  1523. p=parse_expr(p);
  1524. p=skip_space(p);
  1525. add_scriptl(add_str(label));
  1526. add_scriptc(C_FUNC);
  1527. // 開始地点に飛ばす
  1528. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1529. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1530. parse_line(label);
  1531. syntax.curly_count--;
  1532. // 条件終了地点のラベル形成する
  1533. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1534. l=add_str(label);
  1535. set_label(l,script_pos,p);
  1536. p = skip_space(p);
  1537. if(*p != ';') {
  1538. disp_error_message("parse_syntax: need ';'",p);
  1539. return p+1;
  1540. }
  1541. p++;
  1542. syntax.curly_count--;
  1543. return p;
  1544. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1545. // 次のループに飛ばす
  1546. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1547. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1548. parse_line(label);
  1549. syntax.curly_count--;
  1550. // for 終了のラベル付け
  1551. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1552. l=add_str(label);
  1553. set_label(l,script_pos,p);
  1554. syntax.curly_count--;
  1555. return p;
  1556. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1557. // while 条件判断へ飛ばす
  1558. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1559. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1560. parse_line(label);
  1561. syntax.curly_count--;
  1562. // while 終了のラベル付け
  1563. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1564. l=add_str(label);
  1565. set_label(l,script_pos,p);
  1566. syntax.curly_count--;
  1567. return p;
  1568. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1569. int pos = syntax.curly_count-1;
  1570. char label[256];
  1571. int l;
  1572. // 戻す
  1573. sprintf(label,"return;");
  1574. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1575. parse_line(label);
  1576. syntax.curly_count--;
  1577. // 現在地のラベルを付ける
  1578. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1579. l=add_str(label);
  1580. set_label(l,script_pos,p);
  1581. syntax.curly_count--;
  1582. return p;
  1583. } else {
  1584. *flag = 0;
  1585. return p;
  1586. }
  1587. }
  1588. /*==========================================
  1589. * 組み込み関数の追加
  1590. *------------------------------------------*/
  1591. static void add_buildin_func(void)
  1592. {
  1593. int i,n;
  1594. const char* p;
  1595. for( i = 0; buildin_func[i].func; i++ )
  1596. {
  1597. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1598. // 'v' - value (either string or int or reference)
  1599. // 's' - string
  1600. // 'i' - int
  1601. // 'r' - reference (of a variable)
  1602. // 'l' - label
  1603. // '?' - one optional parameter
  1604. // '*' - unknown number of optional parameters
  1605. p = buildin_func[i].arg;
  1606. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1607. while( *p == '?' ) ++p;
  1608. if( *p == '*' ) ++p;
  1609. if( *p != 0){
  1610. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1611. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1612. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1613. } else {
  1614. n = add_str(buildin_func[i].name);
  1615. str_data[n].type = C_FUNC;
  1616. str_data[n].val = i;
  1617. str_data[n].func = buildin_func[i].func;
  1618. }
  1619. }
  1620. }
  1621. /*==========================================
  1622. * 定数データベースの読み込み
  1623. *------------------------------------------*/
  1624. static void read_constdb(void)
  1625. {
  1626. FILE *fp;
  1627. char line[1024],name[1024],val[1024];
  1628. int n,type;
  1629. sprintf(line, "%s/const.txt", db_path);
  1630. fp=fopen(line, "r");
  1631. if(fp==NULL){
  1632. ShowError("can't read %s\n", line);
  1633. return ;
  1634. }
  1635. while(fgets(line, sizeof(line), fp))
  1636. {
  1637. if(line[0]=='/' && line[1]=='/')
  1638. continue;
  1639. type=0;
  1640. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1641. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1642. n=add_str(name);
  1643. if(type==0)
  1644. str_data[n].type=C_INT;
  1645. else
  1646. str_data[n].type=C_PARAM;
  1647. str_data[n].val= (int)strtol(val,NULL,0);
  1648. }
  1649. }
  1650. fclose(fp);
  1651. }
  1652. /*==========================================
  1653. * エラー表示
  1654. *------------------------------------------*/
  1655. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1656. {
  1657. int i;
  1658. if( p == NULL || !p[0] ) return NULL;
  1659. if( line < 0 )
  1660. StringBuf_Printf(buf, "*% 5d : ", -line);
  1661. else
  1662. StringBuf_Printf(buf, " % 5d : ", line);
  1663. for(i=0;p[i] && p[i] != '\n';i++){
  1664. if(p + i != mark)
  1665. StringBuf_Printf(buf, "%c", p[i]);
  1666. else
  1667. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1668. }
  1669. StringBuf_AppendStr(buf, "\n");
  1670. return p+i+(p[i] == '\n' ? 1 : 0);
  1671. }
  1672. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1673. {
  1674. // エラーが発生した行を求める
  1675. int j;
  1676. int line = start_line;
  1677. const char *p;
  1678. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1679. StringBuf buf;
  1680. for(p=src;p && *p;line++){
  1681. const char *lineend=strchr(p,'\n');
  1682. if(lineend==NULL || error_pos<lineend){
  1683. break;
  1684. }
  1685. for( j = 0; j < 4; j++ ) {
  1686. linestart[j] = linestart[j+1];
  1687. }
  1688. linestart[4] = p;
  1689. p=lineend+1;
  1690. }
  1691. StringBuf_Init(&buf);
  1692. StringBuf_AppendStr(&buf, "\a\n");
  1693. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1694. StringBuf_Printf(&buf, " %s\n", error_msg);
  1695. for(j = 0; j < 5; j++ ) {
  1696. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1697. }
  1698. p = script_print_line(&buf, p, error_pos, -line);
  1699. for(j = 0; j < 5; j++) {
  1700. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1701. }
  1702. ShowError("%s", StringBuf_Value(&buf));
  1703. StringBuf_Destroy(&buf);
  1704. }
  1705. /*==========================================
  1706. * スクリプトの解析
  1707. *------------------------------------------*/
  1708. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1709. {
  1710. const char *p,*tmpp;
  1711. int i;
  1712. struct script_code* code = NULL;
  1713. static int first=1;
  1714. char end;
  1715. if( src == NULL )
  1716. return NULL;// empty script
  1717. memset(&syntax,0,sizeof(syntax));
  1718. if(first){
  1719. add_buildin_func();
  1720. read_constdb();
  1721. first=0;
  1722. }
  1723. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1724. script_pos=0;
  1725. script_size=SCRIPT_BLOCK_SIZE;
  1726. str_data[LABEL_NEXTLINE].type=C_NOP;
  1727. str_data[LABEL_NEXTLINE].backpatch=-1;
  1728. str_data[LABEL_NEXTLINE].label=-1;
  1729. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1730. if( options&SCRIPT_USE_LABEL_DB )
  1731. scriptlabel_db->clear(scriptlabel_db, NULL);
  1732. parse_options = options;
  1733. if( setjmp( error_jump ) != 0 ) {
  1734. //Restore program state when script has problems. [from jA]
  1735. int i;
  1736. const int size = ARRAYLENGTH(syntax.curly);
  1737. if( error_report )
  1738. script_error(src,file,line,error_msg,error_pos);
  1739. aFree( error_msg );
  1740. aFree( script_buf );
  1741. script_pos = 0;
  1742. script_size = 0;
  1743. script_buf = NULL;
  1744. for(i=LABEL_START;i<str_num;i++)
  1745. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1746. for(i=0; i<size; i++)
  1747. linkdb_final(&syntax.curly[i].case_label);
  1748. return NULL;
  1749. }
  1750. parse_syntax_for_flag=0;
  1751. p=src;
  1752. p=skip_space(p);
  1753. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1754. {// does not require brackets around the script
  1755. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1756. {// empty script and can return NULL
  1757. aFree( script_buf );
  1758. script_pos = 0;
  1759. script_size = 0;
  1760. script_buf = NULL;
  1761. return NULL;
  1762. }
  1763. end = '\0';
  1764. }
  1765. else
  1766. {// requires brackets around the script
  1767. if( *p != '{' )
  1768. disp_error_message("not found '{'",p);
  1769. p = skip_space(p+1);
  1770. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1771. {// empty script and can return NULL
  1772. aFree( script_buf );
  1773. script_pos = 0;
  1774. script_size = 0;
  1775. script_buf = NULL;
  1776. return NULL;
  1777. }
  1778. end = '}';
  1779. }
  1780. // clear references of labels, variables and internal functions
  1781. for(i=LABEL_START;i<str_num;i++){
  1782. if(
  1783. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1784. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1785. ){
  1786. str_data[i].type=C_NOP;
  1787. str_data[i].backpatch=-1;
  1788. str_data[i].label=-1;
  1789. }
  1790. }
  1791. while( syntax.curly_count != 0 || *p != end )
  1792. {
  1793. if( *p == '\0' )
  1794. disp_error_message("unexpected end of script",p);
  1795. // labelだけ特殊処理
  1796. tmpp=skip_space(skip_word(p));
  1797. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1798. i=add_word(p);
  1799. set_label(i,script_pos,p);
  1800. if( parse_options&SCRIPT_USE_LABEL_DB )
  1801. strdb_put(scriptlabel_db, get_str(i), (void*)script_pos);
  1802. p=tmpp+1;
  1803. p=skip_space(p);
  1804. continue;
  1805. }
  1806. // 他は全部一緒くた
  1807. p=parse_line(p);
  1808. p=skip_space(p);
  1809. add_scriptc(C_EOL);
  1810. set_label(LABEL_NEXTLINE,script_pos,p);
  1811. str_data[LABEL_NEXTLINE].type=C_NOP;
  1812. str_data[LABEL_NEXTLINE].backpatch=-1;
  1813. str_data[LABEL_NEXTLINE].label=-1;
  1814. }
  1815. add_scriptc(C_NOP);
  1816. // trim code to size
  1817. script_size = script_pos;
  1818. RECREATE(script_buf,unsigned char,script_pos);
  1819. // default unknown references to variables
  1820. for(i=LABEL_START;i<str_num;i++){
  1821. if(str_data[i].type==C_NOP){
  1822. int j,next;
  1823. str_data[i].type=C_NAME;
  1824. str_data[i].label=i;
  1825. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1826. next=GETVALUE(script_buf,j);
  1827. SETVALUE(script_buf,j,i);
  1828. j=next;
  1829. }
  1830. }
  1831. }
  1832. #ifdef DEBUG_DISP
  1833. for(i=0;i<script_pos;i++){
  1834. if((i&15)==0) printf("%04x : ",i);
  1835. ShowMessage("%02x ",script_buf[i]);
  1836. if((i&15)==15) printf("\n");
  1837. }
  1838. ShowMessage("\n");
  1839. #endif
  1840. #ifdef DEBUG_DISASM
  1841. {
  1842. int i = 0,j;
  1843. while(i < script_pos) {
  1844. ShowMessage("%06x ",i);
  1845. j = i;
  1846. switch(get_com(script_buf,&i)) {
  1847. case C_EOL: printf("C_EOL"); break;
  1848. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1849. case C_POS:
  1850. ShowMessage("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1851. i += 3;
  1852. break;
  1853. case C_NAME:
  1854. j = (*(int*)(script_buf+i)&0xffffff);
  1855. ShowMessage("C_NAME %s",j == 0xffffff ? "?? unknown ??" : get_str(j));
  1856. i += 3;
  1857. break;
  1858. case C_ARG: ShowMessage("C_ARG"); break;
  1859. case C_FUNC: ShowMessage("C_FUNC"); break;
  1860. case C_ADD: ShowMessage("C_ADD"); break;
  1861. case C_SUB: ShowMessage("C_SUB"); break;
  1862. case C_MUL: ShowMessage("C_MUL"); break;
  1863. case C_DIV: ShowMessage("C_DIV"); break;
  1864. case C_MOD: ShowMessage("C_MOD"); break;
  1865. case C_EQ: ShowMessage("C_EQ"); break;
  1866. case C_NE: ShowMessage("C_NE"); break;
  1867. case C_GT: ShowMessage("C_GT"); break;
  1868. case C_GE: ShowMessage("C_GE"); break;
  1869. case C_LT: ShowMessage("C_LT"); break;
  1870. case C_LE: ShowMessage("C_LE"); break;
  1871. case C_AND: ShowMessage("C_AND"); break;
  1872. case C_OR: ShowMessage("C_OR"); break;
  1873. case C_XOR: ShowMessage("C_XOR"); break;
  1874. case C_LAND: ShowMessage("C_LAND"); break;
  1875. case C_LOR: ShowMessage("C_LOR"); break;
  1876. case C_R_SHIFT: ShowMessage("C_R_SHIFT"); break;
  1877. case C_L_SHIFT: ShowMessage("C_L_SHIFT"); break;
  1878. case C_NEG: ShowMessage("C_NEG"); break;
  1879. case C_NOT: ShowMessage("C_NOT"); break;
  1880. case C_LNOT: ShowMessage("C_LNOT"); break;
  1881. case C_NOP: ShowMessage("C_NOP"); break;
  1882. case C_OP3: ShowMessage("C_OP3"); break;
  1883. case C_STR:
  1884. j = strlen(script_buf + i);
  1885. ShowMessage("C_STR %s",script_buf + i);
  1886. i+= j+1;
  1887. break;
  1888. default:
  1889. ShowMessage("unknown");
  1890. }
  1891. ShowMessage("\n");
  1892. }
  1893. }
  1894. #endif
  1895. CREATE(code,struct script_code,1);
  1896. code->script_buf = script_buf;
  1897. code->script_size = script_size;
  1898. code->script_vars = NULL;
  1899. return code;
  1900. }
  1901. /// Returns the player attached to this script, identified by the rid.
  1902. /// If there is no player attached, the script is terminated.
  1903. TBL_PC *script_rid2sd(struct script_state *st)
  1904. {
  1905. TBL_PC *sd=map_id2sd(st->rid);
  1906. if(!sd){
  1907. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1908. script_reportsrc(st);
  1909. st->state = END;
  1910. }
  1911. return sd;
  1912. }
  1913. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1914. ///
  1915. /// @param st Script state
  1916. /// @param data Variable/constant
  1917. void get_val(struct script_state* st, struct script_data* data)
  1918. {
  1919. const char* name;
  1920. char prefix;
  1921. char postfix;
  1922. TBL_PC* sd = NULL;
  1923. if( !data_isreference(data) )
  1924. return;// not a variable/constant
  1925. name = reference_getname(data);
  1926. prefix = name[0];
  1927. postfix = name[strlen(name) - 1];
  1928. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1929. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1930. {
  1931. sd = script_rid2sd(st);
  1932. if( sd == NULL )
  1933. {// needs player attached
  1934. if( postfix == '$' )
  1935. {// string variable
  1936. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1937. data->type = C_CONSTSTR;
  1938. data->u.str = "";
  1939. }
  1940. else
  1941. {// integer variable
  1942. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1943. data->type = C_INT;
  1944. data->u.num = 0;
  1945. }
  1946. return;
  1947. }
  1948. }
  1949. if( postfix == '$' )
  1950. {// string variable
  1951. switch( prefix )
  1952. {
  1953. case '@':
  1954. data->u.str = pc_readregstr(sd, data->u.num);
  1955. break;
  1956. case '$':
  1957. data->u.str = mapreg_readregstr(data->u.num);
  1958. break;
  1959. case '#':
  1960. if( name[1] == '#' )
  1961. data->u.str = pc_readaccountreg2str(sd, name);// global
  1962. else
  1963. data->u.str = pc_readaccountregstr(sd, name);// local
  1964. break;
  1965. case '.':
  1966. {
  1967. struct linkdb_node** n =
  1968. data->ref ? data->ref:
  1969. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  1970. &st->script->script_vars;// npc variable
  1971. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  1972. }
  1973. break;
  1974. default:
  1975. data->u.str = pc_readglobalreg_str(sd, name);
  1976. break;
  1977. }
  1978. if( data->u.str == NULL || data->u.str[0] == '\0' )
  1979. {// empty string
  1980. data->type = C_CONSTSTR;
  1981. data->u.str = "";
  1982. }
  1983. else
  1984. {// duplicate string
  1985. data->type = C_STR;
  1986. data->u.str = aStrdup(data->u.str);
  1987. }
  1988. }
  1989. else
  1990. {// integer variable
  1991. data->type = C_INT;
  1992. if( reference_toconstant(data) )
  1993. {
  1994. data->u.num = reference_getconstant(data);
  1995. }
  1996. else if( reference_toparam(data) )
  1997. {
  1998. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  1999. }
  2000. else
  2001. switch( prefix )
  2002. {
  2003. case '@':
  2004. data->u.num = pc_readreg(sd, data->u.num);
  2005. break;
  2006. case '$':
  2007. data->u.num = mapreg_readreg(data->u.num);
  2008. break;
  2009. case '#':
  2010. if( name[1] == '#' )
  2011. data->u.num = pc_readaccountreg2(sd, name);// global
  2012. else
  2013. data->u.num = pc_readaccountreg(sd, name);// local
  2014. break;
  2015. case '.':
  2016. {
  2017. struct linkdb_node** n =
  2018. data->ref ? data->ref:
  2019. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2020. &st->script->script_vars;// npc variable
  2021. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2022. }
  2023. break;
  2024. default:
  2025. data->u.num = pc_readglobalreg(sd, name);
  2026. break;
  2027. }
  2028. }
  2029. return;
  2030. }
  2031. void push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2032. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2033. /// The value is left in the top of the stack and needs to be removed manually.
  2034. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2035. {
  2036. struct script_data* data;
  2037. push_val2(st->stack, C_NAME, uid, ref);
  2038. data = script_getdatatop(st, -1);
  2039. get_val(st, data);
  2040. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2041. }
  2042. /*==========================================
  2043. * Stores the value of a script variable
  2044. * Return value is 0 on fail, 1 on success.
  2045. *------------------------------------------*/
  2046. static int set_reg(struct script_state* st, TBL_PC* sd, int num, const char* name, const void* value, struct linkdb_node** ref)
  2047. {
  2048. char prefix = name[0];
  2049. if( is_string_variable(name) )
  2050. {// string variable
  2051. const char* str = (const char*)value;
  2052. switch (prefix) {
  2053. case '@':
  2054. return pc_setregstr(sd, num, str);
  2055. case '$':
  2056. return mapreg_setregstr(num, str);
  2057. case '#':
  2058. return (name[1] == '#') ?
  2059. pc_setaccountreg2str(sd, name, str) :
  2060. pc_setaccountregstr(sd, name, str);
  2061. case '.': {
  2062. char* p;
  2063. struct linkdb_node** n;
  2064. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2065. p = (char*)linkdb_erase(n, (void*)num);
  2066. if (p) aFree(p);
  2067. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2068. }
  2069. return 1;
  2070. default:
  2071. return pc_setglobalreg_str(sd, name, str);
  2072. }
  2073. }
  2074. else
  2075. {// integer variable
  2076. int val = (int)value;
  2077. if(str_data[num&0x00ffffff].type == C_PARAM)
  2078. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  2079. switch (prefix) {
  2080. case '@':
  2081. return pc_setreg(sd, num, val);
  2082. case '$':
  2083. return mapreg_setreg(num, val);
  2084. case '#':
  2085. return (name[1] == '#') ?
  2086. pc_setaccountreg2(sd, name, val) :
  2087. pc_setaccountreg(sd, name, val);
  2088. case '.': {
  2089. struct linkdb_node** n;
  2090. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2091. if (val == 0)
  2092. linkdb_erase(n, (void*)num);
  2093. else
  2094. linkdb_replace(n, (void*)num, (void*)val);
  2095. }
  2096. return 1;
  2097. default:
  2098. return pc_setglobalreg(sd, name, val);
  2099. }
  2100. }
  2101. }
  2102. int set_var(TBL_PC* sd, char* name, void* val)
  2103. {
  2104. return set_reg(NULL, sd, reference_uid(add_str(name),0), name, val, NULL);
  2105. }
  2106. void setd_sub(struct script_state *st, TBL_PC *sd, const char *varname, int elem, void *value, struct linkdb_node **ref)
  2107. {
  2108. set_reg(st, sd, reference_uid(add_str(varname),elem), varname, value, ref);
  2109. }
  2110. /// Converts the data to a string
  2111. const char* conv_str(struct script_state* st, struct script_data* data)
  2112. {
  2113. char* p;
  2114. get_val(st, data);
  2115. if( data_isstring(data) )
  2116. {// nothing to convert
  2117. }
  2118. else if( data_isint(data) )
  2119. {// int -> string
  2120. CREATE(p, char, ITEM_NAME_LENGTH);
  2121. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2122. p[ITEM_NAME_LENGTH-1] = '\0';
  2123. data->type = C_STR;
  2124. data->u.str = p;
  2125. }
  2126. else if( data_isreference(data) )
  2127. {// reference -> string
  2128. //##TODO when does this happen (check get_val) [FlavioJS]
  2129. data->type = C_CONSTSTR;
  2130. data->u.str = reference_getname(data);
  2131. }
  2132. else
  2133. {// unsupported data type
  2134. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2135. script_reportdata(data);
  2136. script_reportsrc(st);
  2137. data->type = C_CONSTSTR;
  2138. data->u.str = "";
  2139. }
  2140. return data->u.str;
  2141. }
  2142. /// Converts the data to an int
  2143. int conv_num(struct script_state* st, struct script_data* data)
  2144. {
  2145. char* p;
  2146. long num;
  2147. get_val(st, data);
  2148. if( data_isint(data) )
  2149. {// nothing to convert
  2150. }
  2151. else if( data_isstring(data) )
  2152. {// string -> int
  2153. // the result does not overflow or underflow, it is capped instead
  2154. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2155. p = data->u.str;
  2156. errno = 0;
  2157. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2158. if( errno == ERANGE
  2159. #if LONG_MAX > INT_MAX
  2160. || num < INT_MIN || num > INT_MAX
  2161. #endif
  2162. )
  2163. {
  2164. if( num <= INT_MIN )
  2165. {
  2166. num = INT_MIN;
  2167. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2168. }
  2169. else//if( num >= INT_MAX )
  2170. {
  2171. num = INT_MAX;
  2172. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2173. }
  2174. script_reportdata(data);
  2175. script_reportsrc(st);
  2176. }
  2177. if( data->type == C_STR )
  2178. aFree(p);
  2179. data->type = C_INT;
  2180. data->u.num = (int)num;
  2181. }
  2182. #if 0
  2183. // FIXME this function is being used to retrieve the position of labels and
  2184. // probably other stuff [FlavioJS]
  2185. else
  2186. {// unsupported data type
  2187. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2188. script_reportdata(data);
  2189. script_reportsrc(st);
  2190. data->type = C_INT;
  2191. data->u.num = 0;
  2192. }
  2193. #endif
  2194. return data->u.num;
  2195. }
  2196. //
  2197. // Stack operations
  2198. //
  2199. /// Increases the size of the stack
  2200. void stack_expand(struct script_stack* stack)
  2201. {
  2202. stack->sp_max += 64;
  2203. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2204. stack->sp_max * sizeof(stack->stack_data[0]) );
  2205. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2206. 64 * sizeof(stack->stack_data[0]) );
  2207. }
  2208. /// Pushes a value into the stack
  2209. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2210. /// Pushes a value into the stack (with reference)
  2211. void push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2212. {
  2213. if( stack->sp >= stack->sp_max )
  2214. stack_expand(stack);
  2215. stack->stack_data[stack->sp].type = type;
  2216. stack->stack_data[stack->sp].u.num = val;
  2217. stack->stack_data[stack->sp].ref = ref;
  2218. stack->sp++;
  2219. }
  2220. /// Pushes a string into the stack
  2221. void push_str(struct script_stack* stack, enum c_op type, char* str)
  2222. {
  2223. if( stack->sp >= stack->sp_max )
  2224. stack_expand(stack);
  2225. stack->stack_data[stack->sp].type = type;
  2226. stack->stack_data[stack->sp].u.str = str;
  2227. stack->stack_data[stack->sp].ref = NULL;
  2228. stack->sp++;
  2229. }
  2230. /// Pushes a copy of the target position into the stack
  2231. void push_copy(struct script_stack* stack, int pos)
  2232. {
  2233. switch( stack->stack_data[pos].type )
  2234. {
  2235. case C_CONSTSTR:
  2236. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2237. break;
  2238. case C_STR:
  2239. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2240. break;
  2241. default:
  2242. push_val2(
  2243. stack,stack->stack_data[pos].type,
  2244. stack->stack_data[pos].u.num,
  2245. stack->stack_data[pos].ref
  2246. );
  2247. break;
  2248. }
  2249. }
  2250. /// Removes the values in indexes [start,end[ from the stack
  2251. void pop_stack(struct script_stack* stack, int start, int end)
  2252. {
  2253. struct script_data* data;
  2254. int i;
  2255. if( start < 0 )
  2256. start = 0;
  2257. if( end > stack->sp_max )
  2258. end = stack->sp_max;
  2259. if( start >= end )
  2260. return;// nothing to pop
  2261. // free stack elements
  2262. for( i = start; i < end; i++ )
  2263. {
  2264. data = &stack->stack_data[i];
  2265. if( data->type == C_STR )
  2266. aFree(data->u.str);
  2267. data->type = C_NOP;
  2268. }
  2269. // move the rest of the elements
  2270. if( stack->sp > end )
  2271. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2272. stack->sp -= end - start;
  2273. }
  2274. ///
  2275. ///
  2276. ///
  2277. /*==========================================
  2278. * スクリプト依存変数、関数依存変数の解放
  2279. *------------------------------------------*/
  2280. void script_free_vars(struct linkdb_node **node)
  2281. {
  2282. struct linkdb_node* n = *node;
  2283. while( n != NULL)
  2284. {
  2285. const char* name = get_str((int)(n->key)&0x00ffffff);
  2286. if( is_string_variable(name) )
  2287. aFree(n->data); // 文字型変数なので、データ削除
  2288. n = n->next;
  2289. }
  2290. linkdb_final( node );
  2291. }
  2292. /*==========================================
  2293. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2294. *------------------------------------------*/
  2295. void script_free_stack(struct script_stack *stack)
  2296. {
  2297. int i;
  2298. for(i = 0; i < stack->sp; i++) {
  2299. if( stack->stack_data[i].type == C_STR ) {
  2300. aFree(stack->stack_data[i].u.str);
  2301. stack->stack_data[i].type = C_INT;
  2302. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2303. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2304. script_free_vars( n );
  2305. aFree( n );
  2306. }
  2307. }
  2308. script_free_vars( stack->var_function );
  2309. aFree(stack->var_function);
  2310. aFree(stack->stack_data);
  2311. aFree(stack);
  2312. }
  2313. void script_free_code(struct script_code* code)
  2314. {
  2315. script_free_vars( &code->script_vars );
  2316. aFree( code->script_buf );
  2317. aFree( code );
  2318. }
  2319. //
  2320. // 実行部main
  2321. //
  2322. /*==========================================
  2323. * コマンドの読み取り
  2324. *------------------------------------------*/
  2325. c_op get_com(unsigned char *script,int *pos)
  2326. {
  2327. int i = 0, j = 0;
  2328. if(script[*pos]>=0x80){
  2329. return C_INT;
  2330. }
  2331. while(script[*pos]>=0x40){
  2332. i=script[(*pos)++]<<j;
  2333. j+=6;
  2334. }
  2335. return (c_op)(i+(script[(*pos)++]<<j));
  2336. }
  2337. /*==========================================
  2338. * 数値の所得
  2339. *------------------------------------------*/
  2340. int get_num(unsigned char *script,int *pos)
  2341. {
  2342. int i,j;
  2343. i=0; j=0;
  2344. while(script[*pos]>=0xc0){
  2345. i+=(script[(*pos)++]&0x7f)<<j;
  2346. j+=6;
  2347. }
  2348. return i+((script[(*pos)++]&0x7f)<<j);
  2349. }
  2350. /*==========================================
  2351. * スタックから値を取り出す
  2352. *------------------------------------------*/
  2353. int pop_val(struct script_state* st)
  2354. {
  2355. if(st->stack->sp<=0)
  2356. return 0;
  2357. st->stack->sp--;
  2358. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2359. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2360. return st->stack->stack_data[st->stack->sp].u.num;
  2361. return 0;
  2362. }
  2363. /// Ternary operators
  2364. /// test ? if_true : if_false
  2365. void op_3(struct script_state* st, int op)
  2366. {
  2367. struct script_data* data;
  2368. int flag = 0;
  2369. data = script_getdatatop(st, -3);
  2370. get_val(st, data);
  2371. if( data_isstring(data) )
  2372. flag = data->u.str[0];// "" -> false
  2373. else if( data_isint(data) )
  2374. flag = data->u.num;// 0 -> false
  2375. else
  2376. {
  2377. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2378. script_reportdata(data);
  2379. script_reportsrc(st);
  2380. script_removetop(st, -3, 0);
  2381. script_pushnil(st);
  2382. return;
  2383. }
  2384. if( flag )
  2385. script_pushcopytop(st, -2);
  2386. else
  2387. script_pushcopytop(st, -1);
  2388. script_removetop(st, -4, -1);
  2389. }
  2390. /// Binary string operators
  2391. /// s1 EQ s2 -> i
  2392. /// s1 NE s2 -> i
  2393. /// s1 GT s2 -> i
  2394. /// s1 GE s2 -> i
  2395. /// s1 LT s2 -> i
  2396. /// s1 LE s2 -> i
  2397. /// s1 ADD s2 -> s
  2398. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2399. {
  2400. int a = 0;
  2401. switch(op){
  2402. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2403. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2404. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2405. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2406. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2407. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2408. case C_ADD:
  2409. {
  2410. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2411. strcpy(buf, s1);
  2412. strcat(buf, s2);
  2413. script_pushstr(st, buf);
  2414. return;
  2415. }
  2416. default:
  2417. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2418. script_reportsrc(st);
  2419. script_pushnil(st);
  2420. st->state = END;
  2421. return;
  2422. }
  2423. script_pushint(st,a);
  2424. }
  2425. /// Binary number operators
  2426. /// i OP i -> i
  2427. void op_2num(struct script_state* st, int op, int i1, int i2)
  2428. {
  2429. int ret;
  2430. double ret_double;
  2431. switch( op )
  2432. {
  2433. case C_AND: ret = i1 & i2; break;
  2434. case C_OR: ret = i1 | i2; break;
  2435. case C_XOR: ret = i1 ^ i2; break;
  2436. case C_LAND: ret = (i1 && i2); break;
  2437. case C_LOR: ret = (i1 || i2); break;
  2438. case C_EQ: ret = (i1 == i2); break;
  2439. case C_NE: ret = (i1 != i2); break;
  2440. case C_GT: ret = (i1 > i2); break;
  2441. case C_GE: ret = (i1 >= i2); break;
  2442. case C_LT: ret = (i1 < i2); break;
  2443. case C_LE: ret = (i1 <= i2); break;
  2444. case C_R_SHIFT: ret = i1>>i2; break;
  2445. case C_L_SHIFT: ret = i1<<i2; break;
  2446. case C_DIV:
  2447. case C_MOD:
  2448. if( i2 == 0 )
  2449. {
  2450. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2451. script_reportsrc(st);
  2452. script_pushnil(st);
  2453. st->state = END;
  2454. return;
  2455. }
  2456. else if( op == C_DIV )
  2457. ret = i1 / i2;
  2458. else//if( op == C_MOD )
  2459. ret = i1 % i2;
  2460. break;
  2461. default:
  2462. switch( op )
  2463. {// operators that can overflow/underflow
  2464. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2465. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2466. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2467. default:
  2468. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2469. script_reportsrc(st);
  2470. script_pushnil(st);
  2471. return;
  2472. }
  2473. if( ret_double < (double)INT_MIN )
  2474. {
  2475. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2476. script_reportsrc(st);
  2477. ret = INT_MIN;
  2478. }
  2479. else if( ret_double > (double)INT_MAX )
  2480. {
  2481. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2482. script_reportsrc(st);
  2483. ret = INT_MAX;
  2484. }
  2485. }
  2486. script_pushint(st, ret);
  2487. }
  2488. /// Binary operators
  2489. void op_2(struct script_state *st, int op)
  2490. {
  2491. struct script_data* left;
  2492. struct script_data* right;
  2493. left = script_getdatatop(st, -2);
  2494. right = script_getdatatop(st, -1);
  2495. get_val(st, left);
  2496. get_val(st, right);
  2497. // automatic conversions
  2498. switch( op )
  2499. {
  2500. case C_ADD:
  2501. if( data_isint(left) && data_isstring(right) )
  2502. {// convert int-string to string-string
  2503. conv_str(st, left);
  2504. }
  2505. else if( data_isstring(left) && data_isint(right) )
  2506. {// convert string-int to string-string
  2507. conv_str(st, right);
  2508. }
  2509. break;
  2510. }
  2511. if( data_isstring(left) && data_isstring(right) )
  2512. {// ss => op_2str
  2513. op_2str(st, op, left->u.str, right->u.str);
  2514. script_removetop(st, -3, -1);// pop the two values before the top one
  2515. }
  2516. else if( data_isint(left) && data_isint(right) )
  2517. {// ii => op_2num
  2518. int i1 = left->u.num;
  2519. int i2 = right->u.num;
  2520. script_removetop(st, -2, 0);
  2521. op_2num(st, op, i1, i2);
  2522. }
  2523. else
  2524. {// invalid argument
  2525. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2526. script_reportdata(left);
  2527. script_reportdata(right);
  2528. script_reportsrc(st);
  2529. script_removetop(st, -2, 0);
  2530. script_pushnil(st);
  2531. st->state = END;
  2532. }
  2533. }
  2534. /// Unary operators
  2535. /// NEG i -> i
  2536. /// NOT i -> i
  2537. /// LNOT i -> i
  2538. void op_1(struct script_state* st, int op)
  2539. {
  2540. struct script_data* data;
  2541. int i1;
  2542. data = script_getdatatop(st, -1);
  2543. get_val(st, data);
  2544. if( !data_isint(data) )
  2545. {// not a number
  2546. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2547. script_reportdata(data);
  2548. script_reportsrc(st);
  2549. script_pushnil(st);
  2550. st->state = END;
  2551. return;
  2552. }
  2553. i1 = data->u.num;
  2554. script_removetop(st, -1, 0);
  2555. switch( op )
  2556. {
  2557. case C_NEG: i1 = -i1; break;
  2558. case C_NOT: i1 = ~i1; break;
  2559. case C_LNOT: i1 = !i1; break;
  2560. default:
  2561. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2562. script_reportsrc(st);
  2563. script_pushnil(st);
  2564. st->state = END;
  2565. return;
  2566. }
  2567. script_pushint(st, i1);
  2568. }
  2569. /*==========================================
  2570. * 関数の実行
  2571. *------------------------------------------*/
  2572. int run_func(struct script_state *st)
  2573. {
  2574. int i,start_sp,end_sp,func;
  2575. end_sp=st->stack->sp;
  2576. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2577. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2578. ShowError("function not found\n");
  2579. // st->stack->sp=0;
  2580. st->state=END;
  2581. script_reportsrc(st);
  2582. return 1;
  2583. }
  2584. start_sp=i-1;
  2585. st->start=i-1;
  2586. st->end=end_sp;
  2587. func=st->stack->stack_data[st->start].u.num;
  2588. #ifdef DEBUG_RUN
  2589. if(battle_config.etc_log) {
  2590. 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);
  2591. ShowDebug("stack dump :");
  2592. for(i=0;i<end_sp;i++){
  2593. switch(st->stack->stack_data[i].type){
  2594. case C_INT:
  2595. ShowMessage(" int(%d)", st->stack->stack_data[i].u.num);
  2596. break;
  2597. case C_NAME:
  2598. ShowMessage(" name(%s)", get_str(st->stack->stack_data[i].u.num & 0xffffff);
  2599. break;
  2600. case C_ARG:
  2601. ShowMessage(" arg");
  2602. break;
  2603. case C_POS:
  2604. ShowMessage(" pos(%d)",st->stack->stack_data[i].u.num);
  2605. break;
  2606. case C_STR:
  2607. ShowMessage(" str(%s)",st->stack->stack_data[i].u.str);
  2608. break;
  2609. case C_CONSTSTR:
  2610. ShowMessage(" cstr(%s)",st->stack->stack_data[i].u.str);
  2611. break;
  2612. default:
  2613. ShowMessage(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2614. }
  2615. }
  2616. ShowMessage("\n");
  2617. }
  2618. #endif
  2619. if(str_data[func].type!=C_FUNC ){
  2620. ShowError("run_func: '"CL_WHITE"%s"CL_RESET"' (type %d) is not function and command!\n", get_str(func), str_data[func].type);
  2621. // st->stack->sp=0;
  2622. st->state=END;
  2623. script_reportsrc(st);
  2624. return 1;
  2625. }
  2626. #ifdef DEBUG_RUN
  2627. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n", get_str(func),func,str_data[func].type,st->pos);
  2628. #endif
  2629. if(str_data[func].func){
  2630. if (str_data[func].func(st)) //Report error
  2631. script_reportsrc(st);
  2632. } else {
  2633. ShowError("run_func : %s? (%d(%d))\n", get_str(func),func,str_data[func].type);
  2634. script_pushint(st,0);
  2635. script_reportsrc(st);
  2636. }
  2637. // Stack's datum are used when re-run functions [Eoe]
  2638. if(st->state != RERUNLINE) {
  2639. pop_stack(st->stack,start_sp,end_sp);
  2640. }
  2641. if(st->state==RETFUNC){
  2642. // ユーザー定義関数からの復帰
  2643. int olddefsp=st->stack->defsp;
  2644. int i;
  2645. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2646. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2647. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2648. st->state=END;
  2649. script_reportsrc(st);
  2650. return 1;
  2651. }
  2652. script_free_vars( st->stack->var_function );
  2653. aFree(st->stack->var_function);
  2654. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2655. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2656. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2657. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2658. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2659. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2660. st->state=GOTO;
  2661. }
  2662. return 0;
  2663. }
  2664. /*==========================================
  2665. * script execution
  2666. *------------------------------------------*/
  2667. void run_script_main(struct script_state *st);
  2668. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2669. {
  2670. struct script_state *st;
  2671. TBL_PC *sd=NULL;
  2672. if(rootscript==NULL || pos<0)
  2673. return;
  2674. if (rid) sd = map_id2sd(rid);
  2675. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2676. //Resume script.
  2677. st = sd->st;
  2678. } else {
  2679. st = (struct script_state*)aCalloc(sizeof(struct script_state), 1);
  2680. // the script is different, make new script_state and stack
  2681. st->stack = (struct script_stack*)aMalloc (sizeof(struct script_stack));
  2682. st->stack->sp=0;
  2683. st->stack->sp_max=64;
  2684. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2685. st->stack->defsp = st->stack->sp;
  2686. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  2687. st->state = RUN;
  2688. st->script = rootscript;
  2689. }
  2690. st->pos = pos;
  2691. st->rid = rid;
  2692. st->oid = oid;
  2693. st->sleep.timer = INVALID_TIMER;
  2694. run_script_main(st);
  2695. }
  2696. void script_stop_sleeptimers(int id)
  2697. {
  2698. struct script_state* st;
  2699. for(;;)
  2700. {
  2701. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2702. if( st == NULL )
  2703. break; // no more sleep timers
  2704. if( st->sleep.timer != INVALID_TIMER )
  2705. delete_timer(st->sleep.timer, run_script_timer);
  2706. script_free_stack(st->stack);
  2707. aFree(st);
  2708. }
  2709. }
  2710. /*==========================================
  2711. * 指定ノードをsleep_dbから削除
  2712. *------------------------------------------*/
  2713. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2714. {
  2715. struct linkdb_node *retnode;
  2716. if( n == NULL)
  2717. return NULL;
  2718. if( n->prev == NULL )
  2719. sleep_db = n->next;
  2720. else
  2721. n->prev->next = n->next;
  2722. if( n->next )
  2723. n->next->prev = n->prev;
  2724. retnode = n->next;
  2725. aFree( n );
  2726. return retnode; // 次のノードを返す
  2727. }
  2728. /*==========================================
  2729. * sleep用タイマー関数
  2730. *------------------------------------------*/
  2731. int run_script_timer(int tid, unsigned int tick, int id, intptr data)
  2732. {
  2733. struct script_state *st = (struct script_state *)data;
  2734. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2735. TBL_PC *sd = map_id2sd(st->rid);
  2736. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2737. { //Character mismatch. Cancel execution.
  2738. st->rid = 0;
  2739. st->state = END;
  2740. }
  2741. while( node && st->sleep.timer != -1 ) {
  2742. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2743. script_erase_sleepdb(node);
  2744. st->sleep.timer = INVALID_TIMER;
  2745. break;
  2746. }
  2747. node = node->next;
  2748. }
  2749. if(st->state != RERUNLINE)
  2750. st->sleep.tick = 0;
  2751. run_script_main(st);
  2752. return 0;
  2753. }
  2754. /*==========================================
  2755. * スクリプトの実行メイン部分
  2756. *------------------------------------------*/
  2757. void run_script_main(struct script_state *st)
  2758. {
  2759. int cmdcount=script_config.check_cmdcount;
  2760. int gotocount=script_config.check_gotocount;
  2761. TBL_PC *sd;
  2762. //For backing up purposes
  2763. struct script_state *bk_st = NULL;
  2764. int bk_npcid = 0;
  2765. struct script_stack *stack=st->stack;
  2766. sd = st->rid?map_id2sd(st->rid):NULL;
  2767. if(sd){
  2768. if(sd->st != st){
  2769. bk_st = sd->st;
  2770. bk_npcid = sd->npc_id;
  2771. }
  2772. sd->st = st;
  2773. sd->npc_id = st->oid;
  2774. }
  2775. if(st->state == RERUNLINE) {
  2776. st->state = RUN;
  2777. run_func(st);
  2778. if(st->state == GOTO)
  2779. st->state = RUN;
  2780. } else if(st->state != END)
  2781. st->state = RUN;
  2782. while(st->state == RUN)
  2783. {
  2784. enum c_op c = get_com(st->script->script_buf,&st->pos);
  2785. switch(c){
  2786. case C_EOL:
  2787. if( stack->sp != stack->defsp )
  2788. {
  2789. if( stack->sp > stack->defsp )
  2790. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2791. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2792. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2793. } else
  2794. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2795. stack->sp = stack->defsp;
  2796. }
  2797. break;
  2798. case C_INT:
  2799. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2800. break;
  2801. case C_POS:
  2802. case C_NAME:
  2803. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2804. st->pos+=3;
  2805. break;
  2806. case C_ARG:
  2807. push_val(stack,c,0);
  2808. break;
  2809. case C_STR:
  2810. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2811. while(st->script->script_buf[st->pos++]);
  2812. break;
  2813. case C_FUNC:
  2814. run_func(st);
  2815. if(st->state==GOTO){
  2816. st->state = RUN;
  2817. if( gotocount>0 && (--gotocount)<=0 ){
  2818. ShowError("run_script: infinity loop !\n");
  2819. script_reportsrc(st);
  2820. st->state=END;
  2821. }
  2822. }
  2823. break;
  2824. case C_NEG:
  2825. case C_NOT:
  2826. case C_LNOT:
  2827. op_1(st ,c);
  2828. break;
  2829. case C_ADD:
  2830. case C_SUB:
  2831. case C_MUL:
  2832. case C_DIV:
  2833. case C_MOD:
  2834. case C_EQ:
  2835. case C_NE:
  2836. case C_GT:
  2837. case C_GE:
  2838. case C_LT:
  2839. case C_LE:
  2840. case C_AND:
  2841. case C_OR:
  2842. case C_XOR:
  2843. case C_LAND:
  2844. case C_LOR:
  2845. case C_R_SHIFT:
  2846. case C_L_SHIFT:
  2847. op_2(st, c);
  2848. break;
  2849. case C_OP3:
  2850. op_3(st, c);
  2851. break;
  2852. case C_NOP:
  2853. st->state=END;
  2854. break;
  2855. default:
  2856. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2857. st->state=END;
  2858. break;
  2859. }
  2860. if( cmdcount>0 && (--cmdcount)<=0 ){
  2861. ShowError("run_script: infinity loop !\n");
  2862. script_reportsrc(st);
  2863. st->state=END;
  2864. }
  2865. }
  2866. if(st->sleep.tick > 0) {
  2867. //Delay execution
  2868. st->sleep.charid = sd?sd->status.char_id:0;
  2869. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2870. run_script_timer, st->sleep.charid, (int)st);
  2871. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2872. //Restore previous script
  2873. if (sd) {
  2874. sd->st = bk_st;
  2875. sd->npc_id = bk_npcid;
  2876. bk_st = NULL; //Remove tag for removal.
  2877. }
  2878. }
  2879. else if(st->state != END && sd){
  2880. //Resume later (st is already attached to player).
  2881. if(bk_st) {
  2882. ShowWarning("Unable to restore stack! Double continuation!\n");
  2883. //Report BOTH scripts to see if that can help somehow.
  2884. ShowDebug("Previous script (lost):\n");
  2885. script_reportsrc(bk_st);
  2886. ShowDebug("Current script:\n");
  2887. script_reportsrc(st);
  2888. }
  2889. } else {
  2890. //Dispose of script.
  2891. if (sd)
  2892. { //Restore previous stack and save char.
  2893. if(sd->state.using_fake_npc){
  2894. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2895. sd->state.using_fake_npc = 0;
  2896. }
  2897. //Restore previous script if any.
  2898. sd->st = bk_st;
  2899. sd->npc_id = bk_npcid;
  2900. if (!bk_st)
  2901. npc_event_dequeue(sd);
  2902. else
  2903. bk_st = NULL; //Remove tag for removal.
  2904. if (sd->state.reg_dirty&2)
  2905. intif_saveregistry(sd,2);
  2906. if (sd->state.reg_dirty&1)
  2907. intif_saveregistry(sd,1);
  2908. }
  2909. st->pos = -1;
  2910. script_free_stack (st->stack);
  2911. aFree(st);
  2912. }
  2913. if (bk_st)
  2914. { //Remove previous script
  2915. bk_st->pos = -1;
  2916. script_free_stack(bk_st->stack);
  2917. aFree(bk_st);
  2918. bk_st = NULL;
  2919. }
  2920. }
  2921. int script_config_read(char *cfgName)
  2922. {
  2923. int i;
  2924. char line[1024],w1[1024],w2[1024];
  2925. FILE *fp;
  2926. fp=fopen(cfgName,"r");
  2927. if(fp==NULL){
  2928. ShowError("file not found: [%s]\n", cfgName);
  2929. return 1;
  2930. }
  2931. while(fgets(line, sizeof(line), fp))
  2932. {
  2933. if(line[0] == '/' && line[1] == '/')
  2934. continue;
  2935. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  2936. if(i!=2)
  2937. continue;
  2938. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  2939. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  2940. }
  2941. else if(strcmpi(w1,"check_cmdcount")==0) {
  2942. script_config.check_cmdcount = config_switch(w2);
  2943. }
  2944. else if(strcmpi(w1,"check_gotocount")==0) {
  2945. script_config.check_gotocount = config_switch(w2);
  2946. }
  2947. else if(strcmpi(w1,"input_min_value")==0) {
  2948. script_config.input_min_value = config_switch(w2);
  2949. }
  2950. else if(strcmpi(w1,"input_max_value")==0) {
  2951. script_config.input_max_value = config_switch(w2);
  2952. }
  2953. else if(strcmpi(w1,"import")==0){
  2954. script_config_read(w2);
  2955. }
  2956. }
  2957. fclose(fp);
  2958. return 0;
  2959. }
  2960. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  2961. {
  2962. struct script_code *code = (struct script_code *)data;
  2963. if(code){
  2964. script_free_vars( &code->script_vars );
  2965. aFree( code->script_buf );
  2966. aFree( code );
  2967. }
  2968. return 0;
  2969. }
  2970. /*==========================================
  2971. * 終了
  2972. *------------------------------------------*/
  2973. int do_final_script()
  2974. {
  2975. #ifdef DEBUG_HASH
  2976. if (battle_config.etc_log)
  2977. {
  2978. FILE *fp = fopen("hash_dump.txt","wt");
  2979. if(fp) {
  2980. int i,count[SCRIPT_HASH_SIZE];
  2981. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  2982. int n=0;
  2983. int min=INT_MAX,max=0,zero=0;
  2984. double mean=0.0f;
  2985. double median=0.0f;
  2986. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  2987. memset(count, 0, sizeof(count));
  2988. fprintf(fp,"num : hash : data_name\n");
  2989. fprintf(fp,"---------------------------------------------------------------\n");
  2990. for(i=LABEL_START; i<str_num; i++) {
  2991. unsigned int h = calc_hash(get_str(i));
  2992. fprintf(fp,"%04d : %4u : %s\n",i,h, get_str(i));
  2993. ++count[h];
  2994. }
  2995. fprintf(fp,"--------------------\n\n");
  2996. memset(count2, 0, sizeof(count2));
  2997. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  2998. fprintf(fp," hash %3d = %d\n",i,count[i]);
  2999. if(min > count[i])
  3000. min = count[i]; // minimun count of collision
  3001. if(max < count[i])
  3002. max = count[i]; // maximun count of collision
  3003. if(count[i] == 0)
  3004. zero++;
  3005. ++count2[count[i]];
  3006. }
  3007. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3008. for( i=min; i <= max; ++i ){
  3009. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3010. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3011. }
  3012. for( i=min; i <= max; ++i ){
  3013. n += count2[i];
  3014. if( n*2 >= SCRIPT_HASH_SIZE )
  3015. {
  3016. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3017. median = (i+i+1)/2.0f;
  3018. else
  3019. median = i;
  3020. break;
  3021. }
  3022. }
  3023. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3024. fclose(fp);
  3025. }
  3026. }
  3027. #endif
  3028. mapreg_final();
  3029. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3030. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3031. if(sleep_db) {
  3032. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3033. while(n) {
  3034. struct script_state *st = (struct script_state *)n->data;
  3035. script_free_stack(st->stack);
  3036. aFree(st);
  3037. n = n->next;
  3038. }
  3039. linkdb_final(&sleep_db);
  3040. }
  3041. if (str_data)
  3042. aFree(str_data);
  3043. if (str_buf)
  3044. aFree(str_buf);
  3045. return 0;
  3046. }
  3047. /*==========================================
  3048. * 初期化
  3049. *------------------------------------------*/
  3050. int do_init_script()
  3051. {
  3052. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3053. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3054. mapreg_init();
  3055. return 0;
  3056. }
  3057. int script_reload()
  3058. {
  3059. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3060. scriptlabel_db->clear(scriptlabel_db, NULL);
  3061. if(sleep_db) {
  3062. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3063. while(n) {
  3064. struct script_state *st = (struct script_state *)n->data;
  3065. if( st->sleep.timer != INVALID_TIMER )
  3066. delete_timer(st->sleep.timer, run_script_timer);
  3067. script_free_stack(st->stack);
  3068. aFree(st);
  3069. n = n->next;
  3070. }
  3071. linkdb_final(&sleep_db);
  3072. }
  3073. mapreg_reload();
  3074. return 0;
  3075. }
  3076. //-----------------------------------------------------------------------------
  3077. // buildin functions
  3078. //
  3079. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3080. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3081. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3082. /////////////////////////////////////////////////////////////////////
  3083. // NPC interaction
  3084. //
  3085. /// Appends a message to the npc dialog.
  3086. /// If a dialog doesn't exist yet, one is created.
  3087. ///
  3088. /// mes "<message>";
  3089. BUILDIN_FUNC(mes)
  3090. {
  3091. TBL_PC* sd = script_rid2sd(st);
  3092. if( sd == NULL )
  3093. return 0;
  3094. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3095. return 0;
  3096. }
  3097. /// Displays the button 'next' in the npc dialog.
  3098. /// The dialog text is cleared and the script continues when the button is pressed.
  3099. ///
  3100. /// next;
  3101. BUILDIN_FUNC(next)
  3102. {
  3103. TBL_PC* sd;
  3104. sd = script_rid2sd(st);
  3105. if( sd == NULL )
  3106. return 0;
  3107. st->state = STOP;
  3108. clif_scriptnext(sd, st->oid);
  3109. return 0;
  3110. }
  3111. /// Ends the script and displays the button 'close' on the npc dialog.
  3112. /// The dialog is closed when the button is pressed.
  3113. ///
  3114. /// close;
  3115. BUILDIN_FUNC(close)
  3116. {
  3117. TBL_PC* sd;
  3118. sd = script_rid2sd(st);
  3119. if( sd == NULL )
  3120. return 0;
  3121. st->state = END;
  3122. clif_scriptclose(sd, st->oid);
  3123. return 0;
  3124. }
  3125. /// Displays the button 'close' on the npc dialog.
  3126. /// The dialog is closed and the script continues when the button is pressed.
  3127. ///
  3128. /// close2;
  3129. BUILDIN_FUNC(close2)
  3130. {
  3131. TBL_PC* sd;
  3132. sd = script_rid2sd(st);
  3133. if( sd == NULL )
  3134. return 0;
  3135. st->state = STOP;
  3136. clif_scriptclose(sd, st->oid);
  3137. return 0;
  3138. }
  3139. /// Counts the number of valid and total number of options in 'str'
  3140. /// If max_count > 0 the counting stops when that valid option is reached
  3141. /// total is incremented for each option (NULL is supported)
  3142. static int menu_countoptions(const char* str, int max_count, int* total)
  3143. {
  3144. int count = 0;
  3145. int bogus_total;
  3146. if( total == NULL )
  3147. total = &bogus_total;
  3148. ++(*total);
  3149. // initial empty options
  3150. while( *str == ':' )
  3151. {
  3152. ++str;
  3153. ++(*total);
  3154. }
  3155. // count menu options
  3156. while( *str != '\0' )
  3157. {
  3158. ++count;
  3159. --max_count;
  3160. if( max_count == 0 )
  3161. break;
  3162. while( *str != ':' && *str != '\0' )
  3163. ++str;
  3164. while( *str == ':' )
  3165. {
  3166. ++str;
  3167. ++(*total);
  3168. }
  3169. }
  3170. return count;
  3171. }
  3172. /// Displays a menu with options and goes to the target label.
  3173. /// The script is stopped if cancel is pressed.
  3174. /// Options with no text are not displayed in the client.
  3175. ///
  3176. /// Options can be grouped together, separated by the character ':' in the text:
  3177. /// ex: menu "A:B:C",L_target;
  3178. /// All these options go to the specified target label.
  3179. ///
  3180. /// The index of the selected option is put in the variable @menu.
  3181. /// Indexes start with 1 and are consistent with grouped and empty options.
  3182. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3183. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3184. ///
  3185. /// NOTE: the client closes the npc dialog when cancel is pressed
  3186. ///
  3187. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3188. BUILDIN_FUNC(menu)
  3189. {
  3190. int i;
  3191. const char* text;
  3192. TBL_PC* sd;
  3193. sd = script_rid2sd(st);
  3194. if( sd == NULL )
  3195. return 0;
  3196. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3197. if( sd->state.menu_or_input == 0 )
  3198. {
  3199. struct StringBuf buf;
  3200. struct script_data* data;
  3201. if( script_lastdata(st) % 2 == 0 )
  3202. {// argument count is not even (1st argument is at index 2)
  3203. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3204. st->state = END;
  3205. return 1;
  3206. }
  3207. StringBuf_Init(&buf);
  3208. sd->npc_menu = 0;
  3209. for( i = 2; i < script_lastdata(st); i += 2 )
  3210. {
  3211. // menu options
  3212. text = script_getstr(st, i);
  3213. // target label
  3214. data = script_getdata(st, i+1);
  3215. if( !data_islabel(data) )
  3216. {// not a label
  3217. StringBuf_Destroy(&buf);
  3218. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3219. script_reportdata(data);
  3220. st->state = END;
  3221. return 1;
  3222. }
  3223. // append option(s)
  3224. if( text[0] == '\0' )
  3225. continue;// empty string, ignore
  3226. if( sd->npc_menu > 0 )
  3227. StringBuf_AppendStr(&buf, ":");
  3228. StringBuf_AppendStr(&buf, text);
  3229. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3230. }
  3231. st->state = RERUNLINE;
  3232. sd->state.menu_or_input = 1;
  3233. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3234. StringBuf_Destroy(&buf);
  3235. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3236. }
  3237. else if( sd->npc_menu == 0xff )
  3238. {// Cancel was pressed
  3239. sd->state.menu_or_input = 0;
  3240. st->state = END;
  3241. }
  3242. else
  3243. {// goto target label
  3244. int menu = 0;
  3245. sd->state.menu_or_input = 0;
  3246. if( sd->npc_menu <= 0 )
  3247. {
  3248. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3249. st->state = END;
  3250. return 1;
  3251. }
  3252. // get target label
  3253. for( i = 2; i < script_lastdata(st); i += 2 )
  3254. {
  3255. text = script_getstr(st, i);
  3256. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3257. if( sd->npc_menu <= 0 )
  3258. break;// entry found
  3259. }
  3260. if( sd->npc_menu > 0 )
  3261. {// Invalid selection
  3262. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3263. st->state = END;
  3264. return 1;
  3265. }
  3266. if( !data_islabel(script_getdata(st, i + 1)) )
  3267. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3268. ShowError("script:menu: unexpected data in label argument\n");
  3269. script_reportdata(script_getdata(st, i + 1));
  3270. st->state = END;
  3271. return 1;
  3272. }
  3273. pc_setreg(sd, add_str("@menu"), menu);
  3274. st->pos = script_getnum(st, i + 1);
  3275. st->state = GOTO;
  3276. }
  3277. return 0;
  3278. }
  3279. /// Displays a menu with options and returns the selected option.
  3280. /// Behaves like 'menu' without the target labels.
  3281. ///
  3282. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3283. ///
  3284. /// @see menu
  3285. BUILDIN_FUNC(select)
  3286. {
  3287. int i;
  3288. const char* text;
  3289. TBL_PC* sd;
  3290. sd = script_rid2sd(st);
  3291. if( sd == NULL )
  3292. return 0;
  3293. if( sd->state.menu_or_input == 0 )
  3294. {
  3295. struct StringBuf buf;
  3296. StringBuf_Init(&buf);
  3297. sd->npc_menu = 0;
  3298. for( i = 2; i <= script_lastdata(st); ++i )
  3299. {
  3300. text = script_getstr(st, i);
  3301. if( sd->npc_menu > 0 )
  3302. StringBuf_AppendStr(&buf, ":");
  3303. StringBuf_AppendStr(&buf, text);
  3304. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3305. }
  3306. st->state = RERUNLINE;
  3307. sd->state.menu_or_input = 1;
  3308. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3309. StringBuf_Destroy(&buf);
  3310. }
  3311. else if( sd->npc_menu == 0xff )
  3312. {// Cancel was pressed
  3313. sd->state.menu_or_input = 0;
  3314. st->state = END;
  3315. }
  3316. else
  3317. {// return selected option
  3318. int menu = 0;
  3319. sd->state.menu_or_input = 0;
  3320. for( i = 2; i <= script_lastdata(st); ++i )
  3321. {
  3322. text = script_getstr(st, i);
  3323. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3324. if( sd->npc_menu <= 0 )
  3325. break;// entry found
  3326. }
  3327. pc_setreg(sd, add_str("@menu"), menu);
  3328. script_pushint(st, menu);
  3329. }
  3330. return 0;
  3331. }
  3332. /// Displays a menu with options and returns the selected option.
  3333. /// Behaves like 'menu' without the target labels, except when cancel is
  3334. /// pressed.
  3335. /// When cancel is pressed, the script continues and 255 is returned.
  3336. ///
  3337. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3338. ///
  3339. /// @see menu
  3340. BUILDIN_FUNC(prompt)
  3341. {
  3342. int i;
  3343. const char *text;
  3344. TBL_PC* sd;
  3345. sd = script_rid2sd(st);
  3346. if( sd == NULL )
  3347. return 0;
  3348. if( sd->state.menu_or_input == 0 )
  3349. {
  3350. struct StringBuf buf;
  3351. StringBuf_Init(&buf);
  3352. sd->npc_menu = 0;
  3353. for( i = 2; i <= script_lastdata(st); ++i )
  3354. {
  3355. text = script_getstr(st, i);
  3356. if( sd->npc_menu > 0 )
  3357. StringBuf_AppendStr(&buf, ":");
  3358. StringBuf_AppendStr(&buf, text);
  3359. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3360. }
  3361. st->state = RERUNLINE;
  3362. sd->state.menu_or_input = 1;
  3363. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3364. StringBuf_Destroy(&buf);
  3365. }
  3366. else if( sd->npc_menu == 0xff )
  3367. {// Cancel was pressed
  3368. sd->state.menu_or_input = 0;
  3369. pc_setreg(sd, add_str("@menu"), 0xff);
  3370. script_pushint(st, 0xff);
  3371. }
  3372. else
  3373. {// return selected option
  3374. int menu = 0;
  3375. sd->state.menu_or_input = 0;
  3376. for( i = 2; i <= script_lastdata(st); ++i )
  3377. {
  3378. text = script_getstr(st, i);
  3379. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3380. if( sd->npc_menu <= 0 )
  3381. break;// entry found
  3382. }
  3383. pc_setreg(sd, add_str("@menu"), menu);
  3384. script_pushint(st, menu);
  3385. }
  3386. return 0;
  3387. }
  3388. /////////////////////////////////////////////////////////////////////
  3389. // ...
  3390. //
  3391. /// Jumps to the target script label.
  3392. ///
  3393. /// goto <label>;
  3394. BUILDIN_FUNC(goto)
  3395. {
  3396. if( !data_islabel(script_getdata(st,2)) )
  3397. {
  3398. ShowError("script:goto: not a label\n");
  3399. script_reportdata(script_getdata(st,2));
  3400. st->state = END;
  3401. return 1;
  3402. }
  3403. st->pos = script_getnum(st,2);
  3404. st->state = GOTO;
  3405. return 0;
  3406. }
  3407. /*==========================================
  3408. * user-defined function call
  3409. *------------------------------------------*/
  3410. BUILDIN_FUNC(callfunc)
  3411. {
  3412. int i, j;
  3413. struct linkdb_node** oldval;
  3414. struct script_code *scr, *oldscr;
  3415. const char* str = script_getstr(st,2);
  3416. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3417. if( !scr )
  3418. {
  3419. ShowError("script:callfunc: function not found! [%s]\n", str);
  3420. st->state = END;
  3421. return 1;
  3422. }
  3423. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3424. push_copy(st->stack,i);
  3425. script_pushint(st,j); // push argument count
  3426. script_pushint(st,st->stack->defsp); // push current stack pointer
  3427. script_pushint(st,(int)st->script); // push current script
  3428. script_pushint(st,(int)st->stack->var_function); // push function-dependent variables
  3429. push_val(st->stack,C_RETINFO,st->pos); // push current script location
  3430. oldscr = st->script;
  3431. oldval = st->stack->var_function;
  3432. st->pos = 0;
  3433. st->script = scr;
  3434. st->stack->defsp = st->start+5+j;
  3435. st->state = GOTO;
  3436. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3437. for( i = 0; i < j; i++ )
  3438. {
  3439. struct script_data* s = script_getdatatop(st, -6-i);
  3440. if( data_isreference(s) && !s->ref )
  3441. {
  3442. const char* name = reference_getname(s);
  3443. if( name[0] == '.' && name[1] == '@' )
  3444. s->ref = oldval;
  3445. else if( name[0] == '.' )
  3446. s->ref = &oldscr->script_vars;
  3447. }
  3448. }
  3449. return 0;
  3450. }
  3451. /*==========================================
  3452. * subroutine call
  3453. *------------------------------------------*/
  3454. BUILDIN_FUNC(callsub)
  3455. {
  3456. int i,j;
  3457. struct linkdb_node** oldval;
  3458. int pos = script_getnum(st,2);
  3459. if( !data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2)) )
  3460. {
  3461. ShowError("script:callsub: argument is not a label\n");
  3462. script_reportdata(script_getdata(st,2));
  3463. st->state = END;
  3464. return 1;
  3465. }
  3466. oldval = st->stack->var_function;
  3467. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3468. push_copy(st->stack,i);
  3469. script_pushint(st,j); // push argument count
  3470. script_pushint(st,st->stack->defsp); // push current stack pointer
  3471. script_pushint(st,(int)st->script); // push current script
  3472. script_pushint(st,(int)st->stack->var_function); // push function-dependent variables
  3473. push_val(st->stack,C_RETINFO,st->pos); // push current script location
  3474. st->pos = pos;
  3475. st->stack->defsp = st->start+5+j;
  3476. st->state = GOTO;
  3477. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3478. for(i = 0; i < j; i++)
  3479. {
  3480. struct script_data* s = script_getdatatop(st, -6-i);
  3481. if( data_isreference(s) && !s->ref )
  3482. {
  3483. const char* name = reference_getname(s);
  3484. if( name[0] == '.' && name[1] == '@' )
  3485. s->ref = oldval;
  3486. }
  3487. }
  3488. return 0;
  3489. }
  3490. /// Retrieves an argument provided to callfunc/callsub.
  3491. /// If the argument doesn't exist
  3492. ///
  3493. /// getarg(<index>{,<default_value>}) -> <value>
  3494. BUILDIN_FUNC(getarg)
  3495. {
  3496. int idx;
  3497. int count;
  3498. int stsp;
  3499. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3500. {
  3501. ShowError("script:getarg: no callfunc or callsub!\n");
  3502. st->state = END;
  3503. return 1;
  3504. }
  3505. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  3506. stsp = st->stack->defsp - count - 5;
  3507. idx = script_getnum(st,2);
  3508. if( idx < count )
  3509. push_copy(st->stack, stsp + idx);
  3510. else if( script_hasdata(st,3) )
  3511. script_pushcopy(st, 3);
  3512. else
  3513. {
  3514. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  3515. st->state = END;
  3516. return 1;
  3517. }
  3518. return 0;
  3519. }
  3520. /// Returns from the current function, optionaly returning a value from the functions.
  3521. /// Don't use outside script functions.
  3522. ///
  3523. /// return;
  3524. /// return <value>;
  3525. BUILDIN_FUNC(return)
  3526. {
  3527. if( script_hasdata(st,2) )
  3528. {// return value
  3529. struct script_data* data;
  3530. script_pushcopy(st, 2);
  3531. data = script_getdatatop(st, -1);
  3532. if( data_isreference(data) )
  3533. {
  3534. const char* name = reference_getname(data);
  3535. if( name[0] == '.' && name[1] == '@' )
  3536. {// temporary script variable, convert to value
  3537. get_val(st, data);
  3538. if( data_isstring(data) )
  3539. {// duplicate the string
  3540. data->type = C_STR;
  3541. data->u.str = aStrdup(data->u.str);
  3542. }
  3543. }
  3544. else if( name[0] == '.' && !data->ref )
  3545. {// script variable, link to current script
  3546. data->ref = &st->script->script_vars;
  3547. }
  3548. }
  3549. }
  3550. else
  3551. {// no return value
  3552. script_pushnil(st);
  3553. }
  3554. st->state = RETFUNC;
  3555. return 0;
  3556. }
  3557. /// Returns a random number from 0 to <range>-1.
  3558. /// Or returns a random number from <min> to <max>.
  3559. /// If <min> is greater than <max>, their numbers are switched.
  3560. /// rand(<range>) -> <int>
  3561. /// rand(<min>,<max>) -> <int>
  3562. BUILDIN_FUNC(rand)
  3563. {
  3564. int range;
  3565. int min;
  3566. int max;
  3567. if( script_hasdata(st,3) )
  3568. {// min,max
  3569. min = script_getnum(st,2);
  3570. max = script_getnum(st,3);
  3571. if( max < min )
  3572. swap(min, max);
  3573. range = max - min + 1;
  3574. }
  3575. else
  3576. {// range
  3577. min = 0;
  3578. range = script_getnum(st,2);
  3579. }
  3580. if( range <= 1 )
  3581. script_pushint(st, min);
  3582. else
  3583. script_pushint(st, rand()%range + min);
  3584. return 0;
  3585. }
  3586. /*==========================================
  3587. *
  3588. *------------------------------------------*/
  3589. BUILDIN_FUNC(warp)
  3590. {
  3591. int ret;
  3592. int x,y;
  3593. const char* str;
  3594. TBL_PC* sd;
  3595. sd = script_rid2sd(st);
  3596. if( sd == NULL )
  3597. return 0;
  3598. str = script_getstr(st,2);
  3599. x = script_getnum(st,3);
  3600. y = script_getnum(st,4);
  3601. if(strcmp(str,"Random")==0)
  3602. ret = pc_randomwarp(sd,3);
  3603. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3604. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3605. else
  3606. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3607. if( ret ) {
  3608. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3609. script_reportsrc(st);
  3610. }
  3611. return 0;
  3612. }
  3613. /*==========================================
  3614. * エリア指定ワープ
  3615. *------------------------------------------*/
  3616. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3617. {
  3618. int x,y;
  3619. unsigned int map;
  3620. map=va_arg(ap, unsigned int);
  3621. x=va_arg(ap,int);
  3622. y=va_arg(ap,int);
  3623. if(map == 0)
  3624. pc_randomwarp((TBL_PC *)bl,3);
  3625. else
  3626. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3627. return 0;
  3628. }
  3629. BUILDIN_FUNC(areawarp)
  3630. {
  3631. int x,y,m;
  3632. unsigned int index;
  3633. const char *str;
  3634. const char *mapname;
  3635. int x0,y0,x1,y1;
  3636. mapname=script_getstr(st,2);
  3637. x0=script_getnum(st,3);
  3638. y0=script_getnum(st,4);
  3639. x1=script_getnum(st,5);
  3640. y1=script_getnum(st,6);
  3641. str=script_getstr(st,7);
  3642. x=script_getnum(st,8);
  3643. y=script_getnum(st,9);
  3644. if( (m=map_mapname2mapid(mapname))< 0)
  3645. return 0;
  3646. if(strcmp(str,"Random")==0)
  3647. index = 0;
  3648. else if(!(index=mapindex_name2id(str)))
  3649. return 0;
  3650. map_foreachinarea(buildin_areawarp_sub, m,x0,y0,x1,y1,BL_PC, index,x,y);
  3651. return 0;
  3652. }
  3653. /*==========================================
  3654. * warpchar [LuzZza]
  3655. * Useful for warp one player from
  3656. * another player npc-session.
  3657. * Using: warpchar "mapname",x,y,Char_ID;
  3658. *------------------------------------------*/
  3659. BUILDIN_FUNC(warpchar)
  3660. {
  3661. int x,y,a;
  3662. const char *str;
  3663. TBL_PC *sd;
  3664. str=script_getstr(st,2);
  3665. x=script_getnum(st,3);
  3666. y=script_getnum(st,4);
  3667. a=script_getnum(st,5);
  3668. sd = map_charid2sd(a);
  3669. if( sd == NULL )
  3670. return 0;
  3671. if(strcmp(str, "Random") == 0)
  3672. pc_randomwarp(sd, 3);
  3673. else
  3674. if(strcmp(str, "SavePoint") == 0)
  3675. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3676. else
  3677. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3678. return 0;
  3679. }
  3680. /*==========================================
  3681. * Warpparty - [Fredzilla]
  3682. * Syntax: warpparty "mapname",x,y,Party_ID;
  3683. *------------------------------------------*/
  3684. BUILDIN_FUNC(warpparty)
  3685. {
  3686. int x,y;
  3687. const char *str;
  3688. int p_id;
  3689. int i;
  3690. unsigned short mapindex;
  3691. TBL_PC *pl_sd;
  3692. struct party_data *p=NULL;
  3693. str=script_getstr(st,2);
  3694. x=script_getnum(st,3);
  3695. y=script_getnum(st,4);
  3696. p_id=script_getnum(st,5);
  3697. if(p_id < 1)
  3698. return 0;
  3699. p = party_search(p_id);
  3700. if (!p)
  3701. return 0;
  3702. if(strcmp(str,"Random")==0)
  3703. {
  3704. for (i = 0; i < MAX_PARTY; i++)
  3705. {
  3706. if ((pl_sd = p->data[i].sd))
  3707. {
  3708. if(map[pl_sd->bl.m].flag.nowarp)
  3709. continue;
  3710. pc_randomwarp(pl_sd,3);
  3711. }
  3712. }
  3713. }
  3714. else if(strcmp(str,"SavePointAll")==0)
  3715. {
  3716. for (i = 0; i < MAX_PARTY; i++)
  3717. {
  3718. if ((pl_sd = p->data[i].sd))
  3719. {
  3720. if(map[pl_sd->bl.m].flag.noreturn)
  3721. continue;
  3722. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3723. }
  3724. }
  3725. }
  3726. else if(strcmp(str,"SavePoint")==0)
  3727. {
  3728. pl_sd=script_rid2sd(st);
  3729. if (!pl_sd) return 0;
  3730. mapindex=pl_sd->status.save_point.map;
  3731. x=pl_sd->status.save_point.x;
  3732. y=pl_sd->status.save_point.y;
  3733. for (i = 0; i < MAX_PARTY; i++)
  3734. {
  3735. if ((pl_sd = p->data[i].sd))
  3736. {
  3737. if(map[pl_sd->bl.m].flag.noreturn)
  3738. continue;
  3739. pc_setpos(pl_sd,mapindex,x,y,3);
  3740. }
  3741. }
  3742. }
  3743. else if(strcmp(str,"Leader")==0)
  3744. {
  3745. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  3746. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  3747. return 0;
  3748. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  3749. return 0;
  3750. mapindex = p->data[i].sd->mapindex;
  3751. x = p->data[i].sd->bl.x;
  3752. y = p->data[i].sd->bl.y;
  3753. for (i = 0; i < MAX_PARTY; i++)
  3754. {
  3755. pl_sd = p->data[i].sd;
  3756. if (!pl_sd)
  3757. continue;
  3758. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3759. continue;
  3760. pc_setpos(pl_sd,mapindex,x,y,3);
  3761. }
  3762. }
  3763. else
  3764. {
  3765. mapindex = mapindex_name2id(str);
  3766. if (!mapindex) //Show source of npc error.
  3767. return 1;
  3768. for (i = 0; i < MAX_PARTY; i++)
  3769. {
  3770. if ((pl_sd = p->data[i].sd))
  3771. {
  3772. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  3773. continue;
  3774. pc_setpos(pl_sd,mapindex,x,y,3);
  3775. }
  3776. }
  3777. }
  3778. return 0;
  3779. }
  3780. /*==========================================
  3781. * Warpguild - [Fredzilla]
  3782. * Syntax: warpguild "mapname",x,y,Guild_ID;
  3783. *------------------------------------------*/
  3784. BUILDIN_FUNC(warpguild)
  3785. {
  3786. TBL_PC *sd;
  3787. TBL_PC *pl_sd;
  3788. struct guild* g;
  3789. struct s_mapiterator* iter;
  3790. int type;
  3791. const char* str = script_getstr(st,2);
  3792. int x = script_getnum(st,3);
  3793. int y = script_getnum(st,4);
  3794. int gid = script_getnum(st,5);
  3795. sd=script_rid2sd(st);
  3796. if( sd == NULL )
  3797. return 0;
  3798. g = guild_search(gid);
  3799. if( g == NULL )
  3800. return 0;
  3801. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  3802. return 0;
  3803. type = ( strcmp(str,"Random")==0 ) ? 0
  3804. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  3805. : ( strcmp(str,"SavePoint")==0 ) ? 2
  3806. : 3;
  3807. iter = mapit_getallusers();
  3808. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  3809. {
  3810. if( pl_sd->status.guild_id != gid )
  3811. continue;
  3812. switch( type )
  3813. {
  3814. case 0: // Random
  3815. if(!map[pl_sd->bl.m].flag.nowarp)
  3816. pc_randomwarp(pl_sd,3);
  3817. break;
  3818. case 1: // SavePointAll
  3819. if(!map[pl_sd->bl.m].flag.noreturn)
  3820. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3821. break;
  3822. case 2: // SavePoint
  3823. if(!map[pl_sd->bl.m].flag.noreturn)
  3824. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3825. break;
  3826. case 3: // m,x,y
  3827. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  3828. pc_setpos(pl_sd,mapindex_name2id(str),x,y,3);
  3829. break;
  3830. }
  3831. }
  3832. mapit_free(iter);
  3833. return 0;
  3834. }
  3835. /*==========================================
  3836. *
  3837. *------------------------------------------*/
  3838. BUILDIN_FUNC(heal)
  3839. {
  3840. TBL_PC *sd;
  3841. int hp,sp;
  3842. sd = script_rid2sd(st);
  3843. if (!sd) return 0;
  3844. hp=script_getnum(st,2);
  3845. sp=script_getnum(st,3);
  3846. status_heal(&sd->bl, hp, sp, 1);
  3847. return 0;
  3848. }
  3849. /*==========================================
  3850. *
  3851. *------------------------------------------*/
  3852. BUILDIN_FUNC(itemheal)
  3853. {
  3854. TBL_PC *sd;
  3855. int hp,sp;
  3856. hp=script_getnum(st,2);
  3857. sp=script_getnum(st,3);
  3858. if(potion_flag==1) {
  3859. potion_hp = hp;
  3860. potion_sp = sp;
  3861. return 0;
  3862. }
  3863. sd = script_rid2sd(st);
  3864. if (!sd) return 0;
  3865. pc_itemheal(sd,sd->itemid,hp,sp);
  3866. return 0;
  3867. }
  3868. /*==========================================
  3869. *
  3870. *------------------------------------------*/
  3871. BUILDIN_FUNC(percentheal)
  3872. {
  3873. int hp,sp;
  3874. TBL_PC* sd;
  3875. hp=script_getnum(st,2);
  3876. sp=script_getnum(st,3);
  3877. if(potion_flag==1) {
  3878. potion_per_hp = hp;
  3879. potion_per_sp = sp;
  3880. return 0;
  3881. }
  3882. sd = script_rid2sd(st);
  3883. if( sd == NULL )
  3884. return 0;
  3885. pc_percentheal(sd,hp,sp);
  3886. return 0;
  3887. }
  3888. /*==========================================
  3889. *
  3890. *------------------------------------------*/
  3891. BUILDIN_FUNC(jobchange)
  3892. {
  3893. int job, upper=-1;
  3894. job=script_getnum(st,2);
  3895. if( script_hasdata(st,3) )
  3896. upper=script_getnum(st,3);
  3897. if (pcdb_checkid(job))
  3898. {
  3899. TBL_PC* sd;
  3900. sd = script_rid2sd(st);
  3901. if( sd == NULL )
  3902. return 0;
  3903. pc_jobchange(sd, job, upper);
  3904. }
  3905. return 0;
  3906. }
  3907. /*==========================================
  3908. *
  3909. *------------------------------------------*/
  3910. BUILDIN_FUNC(jobname)
  3911. {
  3912. int class_=script_getnum(st,2);
  3913. script_pushconststr(st,job_name(class_));
  3914. return 0;
  3915. }
  3916. /// Get input from the player.
  3917. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  3918. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  3919. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  3920. /// shorter than 'min' and 0 otherwise.
  3921. ///
  3922. /// input(<var>{,<min>{,<max>}}) -> <int>
  3923. BUILDIN_FUNC(input)
  3924. {
  3925. TBL_PC* sd;
  3926. struct script_data* data;
  3927. int uid;
  3928. const char* name;
  3929. int min;
  3930. int max;
  3931. sd = script_rid2sd(st);
  3932. if( sd == NULL )
  3933. return 0;
  3934. data = script_getdata(st,2);
  3935. if( !data_isreference(data) ){
  3936. ShowError("script:input: not a variable\n");
  3937. script_reportdata(data);
  3938. st->state = END;
  3939. return 1;
  3940. }
  3941. uid = reference_getuid(data);
  3942. name = reference_getname(data);
  3943. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  3944. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  3945. if( !sd->state.menu_or_input )
  3946. { // first invocation, display npc input box
  3947. sd->state.menu_or_input = 1;
  3948. st->state = RERUNLINE;
  3949. if( is_string_variable(name) )
  3950. clif_scriptinputstr(sd,st->oid);
  3951. else
  3952. clif_scriptinput(sd,st->oid);
  3953. }
  3954. else
  3955. { // take received text/value and store it in the designated variable
  3956. sd->state.menu_or_input = 0;
  3957. if( is_string_variable(name) )
  3958. {
  3959. size_t len = strlen(sd->npc_str);
  3960. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  3961. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  3962. }
  3963. else
  3964. {
  3965. int amount = sd->npc_amount;
  3966. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  3967. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  3968. }
  3969. }
  3970. return 0;
  3971. }
  3972. /// Sets the value of a variable.
  3973. /// The value is converted to the type of the variable.
  3974. ///
  3975. /// set(<variable>,<value>) -> <variable>
  3976. BUILDIN_FUNC(set)
  3977. {
  3978. TBL_PC* sd = NULL;
  3979. struct script_data* data;
  3980. int num;
  3981. const char* name;
  3982. char prefix;
  3983. data = script_getdata(st,2);
  3984. if( !data_isreference(data) )
  3985. {
  3986. ShowError("script:set: not a variable\n");
  3987. script_reportdata(script_getdata(st,2));
  3988. st->state = END;
  3989. return 1;
  3990. }
  3991. num = reference_getuid(data);
  3992. name = reference_getname(data);
  3993. prefix = *name;
  3994. if( not_server_variable(prefix) )
  3995. {
  3996. sd = script_rid2sd(st);
  3997. if( sd == NULL )
  3998. {
  3999. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4000. return 0;
  4001. }
  4002. }
  4003. if( is_string_variable(name) )
  4004. set_reg(st,sd,num,name,(void*)script_getstr(st,3),script_getref(st,2));
  4005. else
  4006. set_reg(st,sd,num,name,(void*)script_getnum(st,3),script_getref(st,2));
  4007. // return a copy of the variable reference
  4008. script_pushcopy(st,2);
  4009. return 0;
  4010. }
  4011. /////////////////////////////////////////////////////////////////////
  4012. /// Array variables
  4013. ///
  4014. /// Returns the size of the specified array
  4015. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4016. {
  4017. int32 ret = idx;
  4018. if( isstring )
  4019. {
  4020. for( ; idx < 128; ++idx )
  4021. {
  4022. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4023. if( str && *str )
  4024. ret = idx + 1;
  4025. script_removetop(st, -1, 0);
  4026. }
  4027. }
  4028. else
  4029. {
  4030. for( ; idx < 128; ++idx )
  4031. {
  4032. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4033. if( num )
  4034. ret = idx + 1;
  4035. script_removetop(st, -1, 0);
  4036. }
  4037. }
  4038. return ret;
  4039. }
  4040. /// Sets values of an array, from the starting index.
  4041. /// ex: setarray arr[1],1,2,3;
  4042. ///
  4043. /// setarray <array variable>,<value1>{,<value2>...};
  4044. BUILDIN_FUNC(setarray)
  4045. {
  4046. struct script_data* data;
  4047. const char* name;
  4048. int32 start;
  4049. int32 end;
  4050. int32 id;
  4051. int32 i;
  4052. TBL_PC* sd = NULL;
  4053. data = script_getdata(st, 2);
  4054. if( !data_isreference(data) )
  4055. {
  4056. ShowError("script:setarray: not a variable\n");
  4057. script_reportdata(data);
  4058. st->state = END;
  4059. return 1;// not a variable
  4060. }
  4061. id = reference_getid(data);
  4062. start = reference_getindex(data);
  4063. name = reference_getname(data);
  4064. if( not_array_variable(*name) )
  4065. {
  4066. ShowError("script:setarray: illegal scope\n");
  4067. script_reportdata(data);
  4068. st->state = END;
  4069. return 1;// not supported
  4070. }
  4071. if( not_server_variable(*name) )
  4072. {
  4073. sd = script_rid2sd(st);
  4074. if( sd == NULL )
  4075. return 0;// no player attached
  4076. }
  4077. end = start + script_lastdata(st) - 2;
  4078. if( end > 127 )
  4079. end = 127;
  4080. if( is_string_variable(name) )
  4081. {// string array
  4082. for( i = 3; start < end; ++start, ++i )
  4083. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getstr(st,i), reference_getref(data));
  4084. }
  4085. else
  4086. {// int array
  4087. for( i = 3; start < end; ++start, ++i )
  4088. set_reg(st, sd, reference_uid(id, start), name, (void*)script_getnum(st,i), reference_getref(data));
  4089. }
  4090. return 0;
  4091. }
  4092. /// Sets count values of an array, from the starting index.
  4093. /// ex: cleararray arr[0],0,1;
  4094. ///
  4095. /// cleararray <array variable>,<value>,<count>;
  4096. BUILDIN_FUNC(cleararray)
  4097. {
  4098. struct script_data* data;
  4099. const char* name;
  4100. int32 start;
  4101. int32 end;
  4102. int32 id;
  4103. void* v;
  4104. TBL_PC* sd = NULL;
  4105. data = script_getdata(st, 2);
  4106. if( !data_isreference(data) )
  4107. {
  4108. ShowError("script:cleararray: not a variable\n");
  4109. script_reportdata(data);
  4110. st->state = END;
  4111. return 1;// not a variable
  4112. }
  4113. id = reference_getid(data);
  4114. start = reference_getindex(data);
  4115. name = reference_getname(data);
  4116. if( not_array_variable(*name) )
  4117. {
  4118. ShowError("script:cleararray: illegal scope\n");
  4119. script_reportdata(data);
  4120. st->state = END;
  4121. return 1;// not supported
  4122. }
  4123. if( not_server_variable(*name) )
  4124. {
  4125. sd = script_rid2sd(st);
  4126. if( sd == NULL )
  4127. return 0;// no player attached
  4128. }
  4129. if( is_string_variable(name) )
  4130. v = (void*)script_getstr(st, 3);
  4131. else
  4132. v = (void*)script_getnum(st, 3);
  4133. end = start + script_getnum(st, 4);
  4134. if( end > 127 )
  4135. end = 127;
  4136. for( ; start <= end; ++start )
  4137. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4138. return 0;
  4139. }
  4140. /// Copies data from one array to another.
  4141. /// ex: copyarray arr[0],arr[2],2;
  4142. ///
  4143. /// copyarray <destination array variable>,<source array variable>,<count>;
  4144. BUILDIN_FUNC(copyarray)
  4145. {
  4146. struct script_data* data1;
  4147. struct script_data* data2;
  4148. const char* name1;
  4149. const char* name2;
  4150. int32 idx1;
  4151. int32 idx2;
  4152. int32 id1;
  4153. int32 id2;
  4154. void* v;
  4155. int32 i;
  4156. int32 count;
  4157. TBL_PC* sd = NULL;
  4158. data1 = script_getdata(st, 2);
  4159. data2 = script_getdata(st, 3);
  4160. if( !data_isreference(data1) || !data_isreference(data2) )
  4161. {
  4162. ShowError("script:copyarray: not a variable\n");
  4163. script_reportdata(data1);
  4164. script_reportdata(data2);
  4165. st->state = END;
  4166. return 1;// not a variable
  4167. }
  4168. id1 = reference_getid(data1);
  4169. id2 = reference_getid(data2);
  4170. idx1 = reference_getindex(data1);
  4171. idx2 = reference_getindex(data2);
  4172. name1 = reference_getname(data1);
  4173. name2 = reference_getname(data2);
  4174. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4175. {
  4176. ShowError("script:copyarray: illegal scope\n");
  4177. script_reportdata(data1);
  4178. script_reportdata(data2);
  4179. st->state = END;
  4180. return 1;// not supported
  4181. }
  4182. if( is_string_variable(name1) != is_string_variable(name2) )
  4183. {
  4184. ShowError("script:copyarray: type mismatch\n");
  4185. script_reportdata(data1);
  4186. script_reportdata(data2);
  4187. st->state = END;
  4188. return 1;// data type mismatch
  4189. }
  4190. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4191. {
  4192. sd = script_rid2sd(st);
  4193. if( sd == NULL )
  4194. return 0;// no player attached
  4195. }
  4196. count = script_getnum(st, 4);
  4197. if( count > 128 - idx1 )
  4198. count = 128 - idx1;
  4199. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4200. return 0;// nothing to copy
  4201. if( id1 == id2 && idx1 > idx2 )
  4202. {// destination might be overlapping the source - copy in reverse order
  4203. for( i = count - 1; i >= 0; --i )
  4204. {
  4205. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4206. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4207. script_removetop(st, -1, 0);
  4208. }
  4209. }
  4210. else
  4211. {// normal copy
  4212. for( i = 0; i < count; ++i )
  4213. {
  4214. if( idx2 + i < 128 )
  4215. {
  4216. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4217. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4218. script_removetop(st, -1, 0);
  4219. }
  4220. else// out of range - assume ""/0
  4221. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4222. }
  4223. }
  4224. return 0;
  4225. }
  4226. /// Returns the size of the array.
  4227. /// Assumes that everything before the starting index exists.
  4228. /// ex: getarraysize(arr[3])
  4229. ///
  4230. /// getarraysize(<array variable>) -> <int>
  4231. BUILDIN_FUNC(getarraysize)
  4232. {
  4233. struct script_data* data;
  4234. const char* name;
  4235. data = script_getdata(st, 2);
  4236. if( !data_isreference(data) )
  4237. {
  4238. ShowError("script:getarraysize: not a variable\n");
  4239. script_reportdata(data);
  4240. script_pushnil(st);
  4241. st->state = END;
  4242. return 1;// not a variable
  4243. }
  4244. name = reference_getname(data);
  4245. if( not_array_variable(*name) )
  4246. {
  4247. ShowError("script:getarraysize: illegal scope\n");
  4248. script_reportdata(data);
  4249. script_pushnil(st);
  4250. st->state = END;
  4251. return 1;// not supported
  4252. }
  4253. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4254. return 0;
  4255. }
  4256. /// Deletes count or all the elements in an array, from the starting index.
  4257. /// ex: deletearray arr[4],2;
  4258. ///
  4259. /// deletearray <array variable>;
  4260. /// deletearray <array variable>,<count>;
  4261. BUILDIN_FUNC(deletearray)
  4262. {
  4263. struct script_data* data;
  4264. const char* name;
  4265. int start;
  4266. int end;
  4267. int id;
  4268. TBL_PC *sd = NULL;
  4269. data = script_getdata(st, 2);
  4270. if( !data_isreference(data) )
  4271. {
  4272. ShowError("script:deletearray: not a variable\n");
  4273. script_reportdata(data);
  4274. st->state = END;
  4275. return 1;// not a variable
  4276. }
  4277. id = reference_getid(data);
  4278. start = reference_getindex(data);
  4279. name = reference_getname(data);
  4280. if( not_array_variable(*name) )
  4281. {
  4282. ShowError("script:deletearray: illegal scope\n");
  4283. script_reportdata(data);
  4284. st->state = END;
  4285. return 1;// not supported
  4286. }
  4287. if( not_server_variable(*name) )
  4288. {
  4289. sd = script_rid2sd(st);
  4290. if( sd == NULL )
  4291. return 0;// no player attached
  4292. }
  4293. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4294. if( start >= end )
  4295. return 0;// nothing to free
  4296. if( script_hasdata(st,3) )
  4297. {
  4298. int count = script_getnum(st, 3);
  4299. if( count > end - start )
  4300. count = end - start;
  4301. if( count <= 0 )
  4302. return 0;// nothing to free
  4303. // move rest of the elements backward
  4304. for( ; start + count < end; ++start )
  4305. {
  4306. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4307. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4308. script_removetop(st, -1, 0);
  4309. }
  4310. }
  4311. // clear the rest of the array
  4312. if( is_string_variable(name) )
  4313. {
  4314. for( ; start < end; ++start )
  4315. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4316. }
  4317. else
  4318. {
  4319. for( ; start < end; ++start )
  4320. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4321. }
  4322. return 0;
  4323. }
  4324. /// Returns a reference to the target index of the array variable.
  4325. /// Equivalent to var[index].
  4326. ///
  4327. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4328. BUILDIN_FUNC(getelementofarray)
  4329. {
  4330. struct script_data* data;
  4331. const char* name;
  4332. int32 id;
  4333. int i;
  4334. data = script_getdata(st, 2);
  4335. if( !data_isreference(data) )
  4336. {
  4337. ShowError("script:getelementofarray: not a variable\n");
  4338. script_reportdata(data);
  4339. script_pushnil(st);
  4340. st->state = END;
  4341. return 1;// not a variable
  4342. }
  4343. id = reference_getid(data);
  4344. name = reference_getname(data);
  4345. if( not_array_variable(*name) )
  4346. {
  4347. ShowError("script:getelementofarray: illegal scope\n");
  4348. script_reportdata(data);
  4349. script_pushnil(st);
  4350. st->state = END;
  4351. return 1;// not supported
  4352. }
  4353. i = script_getnum(st, 3);
  4354. if( i < 0 || i >= 128 )
  4355. {
  4356. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4357. script_reportdata(data);
  4358. script_pushnil(st);
  4359. st->state = END;
  4360. return 1;// out of range
  4361. }
  4362. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4363. return 0;
  4364. }
  4365. /////////////////////////////////////////////////////////////////////
  4366. /// ...
  4367. ///
  4368. /*==========================================
  4369. *
  4370. *------------------------------------------*/
  4371. BUILDIN_FUNC(setlook)
  4372. {
  4373. int type,val;
  4374. TBL_PC* sd;
  4375. type=script_getnum(st,2);
  4376. val=script_getnum(st,3);
  4377. sd = script_rid2sd(st);
  4378. if( sd == NULL )
  4379. return 0;
  4380. pc_changelook(sd,type,val);
  4381. return 0;
  4382. }
  4383. BUILDIN_FUNC(changelook)
  4384. { // As setlook but only client side
  4385. int type,val;
  4386. TBL_PC* sd;
  4387. type=script_getnum(st,2);
  4388. val=script_getnum(st,3);
  4389. sd = script_rid2sd(st);
  4390. if( sd == NULL )
  4391. return 0;
  4392. clif_changelook(&sd->bl,type,val);
  4393. return 0;
  4394. }
  4395. /*==========================================
  4396. *
  4397. *------------------------------------------*/
  4398. BUILDIN_FUNC(cutin)
  4399. {
  4400. TBL_PC* sd;
  4401. sd = script_rid2sd(st);
  4402. if( sd == NULL )
  4403. return 0;
  4404. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4405. return 0;
  4406. }
  4407. /*==========================================
  4408. *
  4409. *------------------------------------------*/
  4410. BUILDIN_FUNC(viewpoint)
  4411. {
  4412. int type,x,y,id,color;
  4413. TBL_PC* sd;
  4414. type=script_getnum(st,2);
  4415. x=script_getnum(st,3);
  4416. y=script_getnum(st,4);
  4417. id=script_getnum(st,5);
  4418. color=script_getnum(st,6);
  4419. sd = script_rid2sd(st);
  4420. if( sd == NULL )
  4421. return 0;
  4422. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4423. return 0;
  4424. }
  4425. /*==========================================
  4426. *
  4427. *------------------------------------------*/
  4428. BUILDIN_FUNC(countitem)
  4429. {
  4430. int nameid, i;
  4431. int count = 0;
  4432. struct script_data* data;
  4433. TBL_PC* sd = script_rid2sd(st);
  4434. if (!sd) {
  4435. script_pushint(st,0);
  4436. return 0;
  4437. }
  4438. data = script_getdata(st,2);
  4439. get_val(st,data);
  4440. if( data_isstring(data) ) {
  4441. const char* name = conv_str(st,data);
  4442. struct item_data* item_data;
  4443. if((item_data = itemdb_searchname(name)) != NULL)
  4444. nameid = item_data->nameid;
  4445. else
  4446. nameid = 0;
  4447. } else
  4448. nameid = conv_num(st,data);
  4449. if (nameid < 500) {
  4450. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4451. script_reportsrc(st);
  4452. script_pushint(st,0);
  4453. return 1;
  4454. }
  4455. for(i = 0; i < MAX_INVENTORY; i++)
  4456. if(sd->status.inventory[i].nameid == nameid)
  4457. count += sd->status.inventory[i].amount;
  4458. script_pushint(st,count);
  4459. return 0;
  4460. }
  4461. /*==========================================
  4462. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4463. * returns number of items that meet the conditions
  4464. *------------------------------------------*/
  4465. BUILDIN_FUNC(countitem2)
  4466. {
  4467. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4468. int count = 0;
  4469. int i;
  4470. struct script_data* data;
  4471. TBL_PC* sd = script_rid2sd(st);
  4472. if (!sd) {
  4473. script_pushint(st,0);
  4474. return 0;
  4475. }
  4476. data = script_getdata(st,2);
  4477. get_val(st,data);
  4478. if( data_isstring(data) ) {
  4479. const char* name = conv_str(st,data);
  4480. struct item_data* item_data;
  4481. if((item_data = itemdb_searchname(name)) != NULL)
  4482. nameid = item_data->nameid;
  4483. else
  4484. nameid = 0;
  4485. } else
  4486. nameid = conv_num(st,data);
  4487. iden = script_getnum(st,3);
  4488. ref = script_getnum(st,4);
  4489. attr = script_getnum(st,5);
  4490. c1 = (short)script_getnum(st,6);
  4491. c2 = (short)script_getnum(st,7);
  4492. c3 = (short)script_getnum(st,8);
  4493. c4 = (short)script_getnum(st,9);
  4494. if (nameid < 500) {
  4495. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4496. script_pushint(st,0);
  4497. return 1;
  4498. }
  4499. for(i = 0; i < MAX_INVENTORY; i++)
  4500. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4501. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4502. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4503. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4504. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4505. sd->status.inventory[i].card[3] == c4
  4506. )
  4507. count += sd->status.inventory[i].amount;
  4508. script_pushint(st,count);
  4509. return 0;
  4510. }
  4511. /*==========================================
  4512. * 重量チェック
  4513. *------------------------------------------*/
  4514. BUILDIN_FUNC(checkweight)
  4515. {
  4516. int nameid=0,amount,i;
  4517. unsigned long weight;
  4518. TBL_PC *sd;
  4519. struct script_data *data;
  4520. sd = script_rid2sd(st);
  4521. if( sd == NULL )
  4522. return 0;
  4523. data=script_getdata(st,2);
  4524. get_val(st,data);
  4525. if( data_isstring(data) ){
  4526. const char *name=conv_str(st,data);
  4527. struct item_data *item_data = itemdb_searchname(name);
  4528. if( item_data )
  4529. nameid=item_data->nameid;
  4530. }else
  4531. nameid=conv_num(st,data);
  4532. amount=script_getnum(st,3);
  4533. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4534. script_pushint(st,0);
  4535. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4536. return 1;
  4537. }
  4538. weight = itemdb_weight(nameid)*amount;
  4539. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4540. script_pushint(st,0);
  4541. } else {
  4542. //Check if the inventory ain't full.
  4543. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4544. i = pc_search_inventory(sd,0);
  4545. if (i >= 0) //Empty slot available.
  4546. script_pushint(st,1);
  4547. else //Inventory full
  4548. script_pushint(st,0);
  4549. }
  4550. return 0;
  4551. }
  4552. /*==========================================
  4553. * getitem <item id>,<amount>{,<character ID>};
  4554. * getitem "<item name>",<amount>{,<character ID>};
  4555. *------------------------------------------*/
  4556. BUILDIN_FUNC(getitem)
  4557. {
  4558. int nameid,amount,get_count,i,flag = 0;
  4559. struct item it;
  4560. TBL_PC *sd;
  4561. struct script_data *data;
  4562. data=script_getdata(st,2);
  4563. get_val(st,data);
  4564. if( data_isstring(data) )
  4565. {// "<item name>"
  4566. const char *name=conv_str(st,data);
  4567. struct item_data *item_data = itemdb_searchname(name);
  4568. if( item_data == NULL ){
  4569. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4570. return 1; //No item created.
  4571. }
  4572. nameid=item_data->nameid;
  4573. } else if( data_isint(data) )
  4574. {// <item id>
  4575. nameid=conv_num(st,data);
  4576. //Violet Box, Blue Box, etc - random item pick
  4577. if( nameid < 0 ) {
  4578. nameid=itemdb_searchrandomid(-nameid);
  4579. flag = 1;
  4580. }
  4581. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4582. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4583. return 1; //No item created.
  4584. }
  4585. } else {
  4586. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4587. return 1;
  4588. }
  4589. // <amount>
  4590. if( (amount=script_getnum(st,3)) <= 0)
  4591. return 0; //return if amount <=0, skip the useles iteration
  4592. memset(&it,0,sizeof(it));
  4593. it.nameid=nameid;
  4594. if(!flag)
  4595. it.identify=1;
  4596. else
  4597. it.identify=itemdb_isidentified(nameid);
  4598. if( script_hasdata(st,4) )
  4599. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4600. else
  4601. sd=script_rid2sd(st); // Attached player
  4602. if( sd == NULL ) // no target
  4603. return 0;
  4604. //Check if it's stackable.
  4605. if (!itemdb_isstackable(nameid))
  4606. get_count = 1;
  4607. else
  4608. get_count = amount;
  4609. for (i = 0; i < amount; i += get_count)
  4610. {
  4611. // if not pet egg
  4612. if (!pet_create_egg(sd, nameid))
  4613. {
  4614. if ((flag = pc_additem(sd, &it, get_count)))
  4615. {
  4616. clif_additem(sd, 0, 0, flag);
  4617. if( pc_candrop(sd,&it) )
  4618. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4619. }
  4620. }
  4621. }
  4622. //Logs items, got from (N)PC scripts [Lupus]
  4623. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4624. log_pick_pc(sd, "N", nameid, amount, NULL);
  4625. return 0;
  4626. }
  4627. /*==========================================
  4628. *
  4629. *------------------------------------------*/
  4630. BUILDIN_FUNC(getitem2)
  4631. {
  4632. int nameid,amount,get_count,i,flag = 0;
  4633. int iden,ref,attr,c1,c2,c3,c4;
  4634. struct item_data *item_data;
  4635. struct item item_tmp;
  4636. TBL_PC *sd;
  4637. struct script_data *data;
  4638. if( script_hasdata(st,11) )
  4639. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4640. else
  4641. sd=script_rid2sd(st); // Attached player
  4642. if( sd == NULL ) // no target
  4643. return 0;
  4644. data=script_getdata(st,2);
  4645. get_val(st,data);
  4646. if( data_isstring(data) ){
  4647. const char *name=conv_str(st,data);
  4648. struct item_data *item_data = itemdb_searchname(name);
  4649. if( item_data )
  4650. nameid=item_data->nameid;
  4651. else
  4652. nameid=UNKNOWN_ITEM_ID;
  4653. }else
  4654. nameid=conv_num(st,data);
  4655. amount=script_getnum(st,3);
  4656. iden=script_getnum(st,4);
  4657. ref=script_getnum(st,5);
  4658. attr=script_getnum(st,6);
  4659. c1=script_getnum(st,7);
  4660. c2=script_getnum(st,8);
  4661. c3=script_getnum(st,9);
  4662. c4=script_getnum(st,10);
  4663. if(nameid<0) { // ランダム
  4664. nameid=itemdb_searchrandomid(-nameid);
  4665. flag = 1;
  4666. }
  4667. if(nameid > 0) {
  4668. memset(&item_tmp,0,sizeof(item_tmp));
  4669. item_data=itemdb_exists(nameid);
  4670. if (item_data == NULL)
  4671. return -1;
  4672. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4673. if(ref > 10) ref = 10;
  4674. }
  4675. else if(item_data->type==IT_PETEGG) {
  4676. iden = 1;
  4677. ref = 0;
  4678. }
  4679. else {
  4680. iden = 1;
  4681. ref = attr = 0;
  4682. }
  4683. item_tmp.nameid=nameid;
  4684. if(!flag)
  4685. item_tmp.identify=iden;
  4686. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4687. item_tmp.identify=0;
  4688. item_tmp.refine=ref;
  4689. item_tmp.attribute=attr;
  4690. item_tmp.card[0]=c1;
  4691. item_tmp.card[1]=c2;
  4692. item_tmp.card[2]=c3;
  4693. item_tmp.card[3]=c4;
  4694. //Check if it's stackable.
  4695. if (!itemdb_isstackable(nameid))
  4696. get_count = 1;
  4697. else
  4698. get_count = amount;
  4699. for (i = 0; i < amount; i += get_count)
  4700. {
  4701. // if not pet egg
  4702. if (!pet_create_egg(sd, nameid))
  4703. {
  4704. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  4705. {
  4706. clif_additem(sd, 0, 0, flag);
  4707. if( pc_candrop(sd,&item_tmp) )
  4708. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4709. }
  4710. }
  4711. }
  4712. //Logs items, got from (N)PC scripts [Lupus]
  4713. if(log_config.enable_logs&0x40)
  4714. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4715. }
  4716. return 0;
  4717. }
  4718. /*==========================================
  4719. * gets an item with someone's name inscribed [Skotlex]
  4720. * getinscribeditem item_num, character_name
  4721. * Returned Qty is always 1, only works on equip-able
  4722. * equipment
  4723. *------------------------------------------*/
  4724. BUILDIN_FUNC(getnameditem)
  4725. {
  4726. int nameid;
  4727. struct item item_tmp;
  4728. TBL_PC *sd, *tsd;
  4729. struct script_data *data;
  4730. sd = script_rid2sd(st);
  4731. if (sd == NULL)
  4732. { //Player not attached!
  4733. script_pushint(st,0);
  4734. return 0;
  4735. }
  4736. data=script_getdata(st,2);
  4737. get_val(st,data);
  4738. if( data_isstring(data) ){
  4739. const char *name=conv_str(st,data);
  4740. struct item_data *item_data = itemdb_searchname(name);
  4741. if( item_data == NULL)
  4742. { //Failed
  4743. script_pushint(st,0);
  4744. return 0;
  4745. }
  4746. nameid = item_data->nameid;
  4747. }else
  4748. nameid = conv_num(st,data);
  4749. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  4750. { //Even though named stackable items "could" be risky, they are required for certain quests.
  4751. script_pushint(st,0);
  4752. return 0;
  4753. }
  4754. data=script_getdata(st,3);
  4755. get_val(st,data);
  4756. if( data_isstring(data) ) //Char Name
  4757. tsd=map_nick2sd(conv_str(st,data));
  4758. else //Char Id was given
  4759. tsd=map_charid2sd(conv_num(st,data));
  4760. if( tsd == NULL )
  4761. { //Failed
  4762. script_pushint(st,0);
  4763. return 0;
  4764. }
  4765. memset(&item_tmp,0,sizeof(item_tmp));
  4766. item_tmp.nameid=nameid;
  4767. item_tmp.amount=1;
  4768. item_tmp.identify=1;
  4769. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  4770. item_tmp.card[2]=tsd->status.char_id;
  4771. item_tmp.card[3]=tsd->status.char_id >> 16;
  4772. if(pc_additem(sd,&item_tmp,1)) {
  4773. script_pushint(st,0);
  4774. return 0; //Failed to add item, we will not drop if they don't fit
  4775. }
  4776. //Logs items, got from (N)PC scripts [Lupus]
  4777. if(log_config.enable_logs&0x40)
  4778. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  4779. script_pushint(st,1);
  4780. return 0;
  4781. }
  4782. /*==========================================
  4783. * gets a random item ID from an item group [Skotlex]
  4784. * groupranditem group_num
  4785. *------------------------------------------*/
  4786. BUILDIN_FUNC(grouprandomitem)
  4787. {
  4788. int group;
  4789. group = script_getnum(st,2);
  4790. script_pushint(st,itemdb_searchrandomid(group));
  4791. return 0;
  4792. }
  4793. /*==========================================
  4794. *
  4795. *------------------------------------------*/
  4796. BUILDIN_FUNC(makeitem)
  4797. {
  4798. int nameid,amount,flag = 0;
  4799. int x,y,m;
  4800. const char *mapname;
  4801. struct item item_tmp;
  4802. struct script_data *data;
  4803. data=script_getdata(st,2);
  4804. get_val(st,data);
  4805. if( data_isstring(data) ){
  4806. const char *name=conv_str(st,data);
  4807. struct item_data *item_data = itemdb_searchname(name);
  4808. if( item_data )
  4809. nameid=item_data->nameid;
  4810. else
  4811. nameid=UNKNOWN_ITEM_ID;
  4812. }else
  4813. nameid=conv_num(st,data);
  4814. amount=script_getnum(st,3);
  4815. mapname =script_getstr(st,4);
  4816. x =script_getnum(st,5);
  4817. y =script_getnum(st,6);
  4818. if(strcmp(mapname,"this")==0)
  4819. {
  4820. TBL_PC *sd;
  4821. sd = script_rid2sd(st);
  4822. if (!sd) return 0; //Failed...
  4823. m=sd->bl.m;
  4824. } else
  4825. m=map_mapname2mapid(mapname);
  4826. if(nameid<0) { // ランダム
  4827. nameid=itemdb_searchrandomid(-nameid);
  4828. flag = 1;
  4829. }
  4830. if(nameid > 0) {
  4831. memset(&item_tmp,0,sizeof(item_tmp));
  4832. item_tmp.nameid=nameid;
  4833. if(!flag)
  4834. item_tmp.identify=1;
  4835. else
  4836. item_tmp.identify=itemdb_isidentified(nameid);
  4837. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  4838. }
  4839. return 0;
  4840. }
  4841. /*==========================================
  4842. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  4843. *------------------------------------------*/
  4844. BUILDIN_FUNC(delitem)
  4845. {
  4846. int nameid=0,amount,i,important_item=0;
  4847. TBL_PC *sd;
  4848. struct script_data *data;
  4849. if( script_hasdata(st,4) )
  4850. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4851. else
  4852. sd=script_rid2sd(st); // Attached player
  4853. if( sd == NULL ) // no target
  4854. return 0;
  4855. data=script_getdata(st,2);
  4856. get_val(st,data);
  4857. if( data_isstring(data) ){
  4858. const char *name=conv_str(st,data);
  4859. struct item_data *item_data = itemdb_searchname(name);
  4860. //nameid=UNKNOWN_ITEM_ID;
  4861. if( item_data )
  4862. nameid=item_data->nameid;
  4863. }else
  4864. nameid=conv_num(st,data);
  4865. amount=script_getnum(st,3);
  4866. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  4867. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  4868. return 0;
  4869. }
  4870. //1st pass
  4871. //here we won't delete items with CARDS, named items but we count them
  4872. for(i=0;i<MAX_INVENTORY;i++){
  4873. //we don't delete wrong item or equipped item
  4874. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  4875. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  4876. continue;
  4877. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  4878. if(sd->inventory_data[i]->type==IT_PETEGG &&
  4879. sd->status.inventory[i].card[0] == CARD0_PET)
  4880. {
  4881. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  4882. continue; //pet couldn't be sent for deletion.
  4883. } else
  4884. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  4885. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  4886. sd->status.inventory[i].card[0] ||
  4887. sd->status.inventory[i].refine) {
  4888. //this is important item, count it (except for pet eggs)
  4889. if(sd->status.inventory[i].card[0] != CARD0_PET)
  4890. important_item++;
  4891. continue;
  4892. }
  4893. if(sd->status.inventory[i].amount>=amount){
  4894. //Logs items, got from (N)PC scripts [Lupus]
  4895. if(log_config.enable_logs&0x40)
  4896. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  4897. pc_delitem(sd,i,amount,0);
  4898. return 0; //we deleted exact amount of items. now exit
  4899. } else {
  4900. amount-=sd->status.inventory[i].amount;
  4901. //Logs items, got from (N)PC scripts [Lupus]
  4902. if(log_config.enable_logs&0x40) {
  4903. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  4904. }
  4905. //Logs
  4906. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  4907. }
  4908. }
  4909. //2nd pass
  4910. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  4911. if (important_item>0 && amount>0)
  4912. for(i=0;i<MAX_INVENTORY;i++){
  4913. //we don't delete wrong item
  4914. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  4915. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  4916. continue;
  4917. if(sd->status.inventory[i].amount>=amount){
  4918. //Logs items, got from (N)PC scripts [Lupus]
  4919. if(log_config.enable_logs&0x40)
  4920. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  4921. pc_delitem(sd,i,amount,0);
  4922. return 0; //we deleted exact amount of items. now exit
  4923. } else {
  4924. amount-=sd->status.inventory[i].amount;
  4925. //Logs items, got from (N)PC scripts [Lupus]
  4926. if(log_config.enable_logs&0x40)
  4927. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  4928. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  4929. }
  4930. }
  4931. return 0;
  4932. }
  4933. /*==========================================
  4934. * advanced version of delitem [modified by Mihilion]
  4935. *------------------------------------------*/
  4936. BUILDIN_FUNC(delitem2)
  4937. {
  4938. int nameid=0,amount,i=0;
  4939. int iden,ref,attr,c1,c2,c3,c4;
  4940. TBL_PC *sd;
  4941. struct script_data *data;
  4942. if( script_hasdata(st,11) )
  4943. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4944. else
  4945. sd=script_rid2sd(st); // Attached player
  4946. if( sd == NULL ) // no target
  4947. return 0;
  4948. data=script_getdata(st,2);
  4949. get_val(st,data);
  4950. if( data_isstring(data) ){
  4951. const char *name=conv_str(st,data);
  4952. struct item_data *item_data = itemdb_searchname(name);
  4953. //nameid=UNKNOWN_ITEM_ID;
  4954. if( item_data )
  4955. nameid=item_data->nameid;
  4956. }else
  4957. nameid=conv_num(st,data);
  4958. amount=script_getnum(st,3);
  4959. iden=script_getnum(st,4);
  4960. ref=script_getnum(st,5);
  4961. attr=script_getnum(st,6);
  4962. c1=script_getnum(st,7);
  4963. c2=script_getnum(st,8);
  4964. c3=script_getnum(st,9);
  4965. c4=script_getnum(st,10);
  4966. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  4967. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  4968. return 0;
  4969. }
  4970. for(i=0;i<MAX_INVENTORY;i++){
  4971. //we don't delete wrong item or equipped item
  4972. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  4973. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  4974. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  4975. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  4976. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  4977. sd->status.inventory[i].card[3]!=c4)
  4978. continue;
  4979. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  4980. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  4981. {
  4982. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  4983. continue; //Failed to send delete the pet.
  4984. }
  4985. if(sd->status.inventory[i].amount>=amount){
  4986. //Logs items, got from (N)PC scripts [Lupus]
  4987. if(log_config.enable_logs&0x40)
  4988. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  4989. pc_delitem(sd,i,amount,0);
  4990. return 0; //we deleted exact amount of items. now exit
  4991. } else {
  4992. amount-=sd->status.inventory[i].amount;
  4993. //Logs items, got from (N)PC scripts [Lupus]
  4994. if(log_config.enable_logs&0x40)
  4995. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  4996. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  4997. }
  4998. }
  4999. return 0;
  5000. }
  5001. /*==========================================
  5002. * Enables/Disables use of items while in an NPC [Skotlex]
  5003. *------------------------------------------*/
  5004. BUILDIN_FUNC(enableitemuse)
  5005. {
  5006. TBL_PC *sd;
  5007. sd=script_rid2sd(st);
  5008. if (sd)
  5009. sd->npc_item_flag = st->oid;
  5010. return 0;
  5011. }
  5012. BUILDIN_FUNC(disableitemuse)
  5013. {
  5014. TBL_PC *sd;
  5015. sd=script_rid2sd(st);
  5016. if (sd)
  5017. sd->npc_item_flag = 0;
  5018. return 0;
  5019. }
  5020. /*==========================================
  5021. *キャラ関係のパラメータ取得
  5022. *------------------------------------------*/
  5023. BUILDIN_FUNC(readparam)
  5024. {
  5025. int type;
  5026. TBL_PC *sd;
  5027. type=script_getnum(st,2);
  5028. if( script_hasdata(st,3) )
  5029. sd=map_nick2sd(script_getstr(st,3));
  5030. else
  5031. sd=script_rid2sd(st);
  5032. if(sd==NULL){
  5033. script_pushint(st,-1);
  5034. return 0;
  5035. }
  5036. script_pushint(st,pc_readparam(sd,type));
  5037. return 0;
  5038. }
  5039. /*==========================================
  5040. *キャラ関係のID取得
  5041. *------------------------------------------*/
  5042. BUILDIN_FUNC(getcharid)
  5043. {
  5044. int num;
  5045. TBL_PC *sd;
  5046. num = script_getnum(st,2);
  5047. if( script_hasdata(st,3) )
  5048. sd=map_nick2sd(script_getstr(st,3));
  5049. else
  5050. sd=script_rid2sd(st);
  5051. if(sd==NULL){
  5052. script_pushint(st,0); //return 0, according docs
  5053. return 0;
  5054. }
  5055. switch( num ) {
  5056. case 0: script_pushint(st,sd->status.char_id); break;
  5057. case 1: script_pushint(st,sd->status.party_id); break;
  5058. case 2: script_pushint(st,sd->status.guild_id); break;
  5059. case 3: script_pushint(st,sd->status.account_id); break;
  5060. default:
  5061. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5062. script_pushint(st,0);
  5063. break;
  5064. }
  5065. return 0;
  5066. }
  5067. /*==========================================
  5068. *指定IDのPT名取得
  5069. *------------------------------------------*/
  5070. char *buildin_getpartyname_sub(int party_id)
  5071. {
  5072. struct party_data *p;
  5073. p=party_search(party_id);
  5074. if(p!=NULL){
  5075. char *buf;
  5076. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5077. memcpy(buf, p->party.name, NAME_LENGTH);
  5078. buf[NAME_LENGTH-1] = '\0';
  5079. return buf;
  5080. }
  5081. return 0;
  5082. }
  5083. BUILDIN_FUNC(getpartyname)
  5084. {
  5085. char *name;
  5086. int party_id;
  5087. party_id=script_getnum(st,2);
  5088. name=buildin_getpartyname_sub(party_id);
  5089. if(name != NULL)
  5090. script_pushstr(st,name);
  5091. else
  5092. script_pushconststr(st,"null");
  5093. return 0;
  5094. }
  5095. /*==========================================
  5096. *指定IDのPT人数とメンバーID取得
  5097. *------------------------------------------*/
  5098. BUILDIN_FUNC(getpartymember)
  5099. {
  5100. struct party_data *p;
  5101. int i,j=0,type=0;
  5102. p=party_search(script_getnum(st,2));
  5103. if( script_hasdata(st,3) )
  5104. type=script_getnum(st,3);
  5105. if(p!=NULL){
  5106. for(i=0;i<MAX_PARTY;i++){
  5107. if(p->party.member[i].account_id){
  5108. switch (type) {
  5109. case 2:
  5110. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5111. break;
  5112. case 1:
  5113. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5114. break;
  5115. default:
  5116. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5117. }
  5118. j++;
  5119. }
  5120. }
  5121. }
  5122. mapreg_setreg(add_str("$@partymembercount"),j);
  5123. return 0;
  5124. }
  5125. /*==========================================
  5126. * Retrieves party leader. if flag is specified,
  5127. * return some of the leader data. Otherwise, return name.
  5128. *------------------------------------------*/
  5129. BUILDIN_FUNC(getpartyleader)
  5130. {
  5131. int party_id, type = 0, i=0;
  5132. struct party_data *p;
  5133. party_id=script_getnum(st,2);
  5134. if( script_hasdata(st,3) )
  5135. type=script_getnum(st,3);
  5136. p=party_search(party_id);
  5137. if (p) //Search leader
  5138. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5139. if (!p || i == MAX_PARTY) { //leader not found
  5140. if (type)
  5141. script_pushint(st,-1);
  5142. else
  5143. script_pushconststr(st,"null");
  5144. return 0;
  5145. }
  5146. switch (type) {
  5147. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5148. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5149. case 3: script_pushint(st,p->party.member[i].class_); break;
  5150. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5151. case 5: script_pushint(st,p->party.member[i].lv); break;
  5152. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5153. }
  5154. return 0;
  5155. }
  5156. /*==========================================
  5157. *指定IDのギルド名取得
  5158. *------------------------------------------*/
  5159. char *buildin_getguildname_sub(int guild_id)
  5160. {
  5161. struct guild *g=NULL;
  5162. g=guild_search(guild_id);
  5163. if(g!=NULL){
  5164. char *buf;
  5165. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5166. memcpy(buf, g->name, NAME_LENGTH);
  5167. buf[NAME_LENGTH-1] = '\0';
  5168. return buf;
  5169. }
  5170. return NULL;
  5171. }
  5172. BUILDIN_FUNC(getguildname)
  5173. {
  5174. char *name;
  5175. int guild_id=script_getnum(st,2);
  5176. name=buildin_getguildname_sub(guild_id);
  5177. if(name != NULL)
  5178. script_pushstr(st,name);
  5179. else
  5180. script_pushconststr(st,"null");
  5181. return 0;
  5182. }
  5183. /*==========================================
  5184. *指定IDのGuildMaster名取得
  5185. *------------------------------------------*/
  5186. char *buildin_getguildmaster_sub(int guild_id)
  5187. {
  5188. struct guild *g=NULL;
  5189. g=guild_search(guild_id);
  5190. if(g!=NULL){
  5191. char *buf;
  5192. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5193. memcpy(buf, g->master, NAME_LENGTH);
  5194. buf[NAME_LENGTH-1] = '\0';
  5195. return buf;
  5196. }
  5197. return 0;
  5198. }
  5199. BUILDIN_FUNC(getguildmaster)
  5200. {
  5201. char *master;
  5202. int guild_id=script_getnum(st,2);
  5203. master=buildin_getguildmaster_sub(guild_id);
  5204. if(master!=0)
  5205. script_pushstr(st,master);
  5206. else
  5207. script_pushconststr(st,"null");
  5208. return 0;
  5209. }
  5210. BUILDIN_FUNC(getguildmasterid)
  5211. {
  5212. char *master;
  5213. TBL_PC *sd=NULL;
  5214. int guild_id=script_getnum(st,2);
  5215. master=buildin_getguildmaster_sub(guild_id);
  5216. if(master!=0){
  5217. if((sd=map_nick2sd(master)) == NULL){
  5218. script_pushint(st,0);
  5219. return 0;
  5220. }
  5221. script_pushint(st,sd->status.char_id);
  5222. }else{
  5223. script_pushint(st,0);
  5224. }
  5225. return 0;
  5226. }
  5227. /*==========================================
  5228. * キャラクタの名前
  5229. *------------------------------------------*/
  5230. BUILDIN_FUNC(strcharinfo)
  5231. {
  5232. TBL_PC *sd;
  5233. int num;
  5234. char *buf;
  5235. sd=script_rid2sd(st);
  5236. if (!sd) { //Avoid crashing....
  5237. script_pushconststr(st,"");
  5238. return 0;
  5239. }
  5240. num=script_getnum(st,2);
  5241. switch(num){
  5242. case 0:
  5243. script_pushstrcopy(st,sd->status.name);
  5244. break;
  5245. case 1:
  5246. buf=buildin_getpartyname_sub(sd->status.party_id);
  5247. if(buf!=0)
  5248. script_pushstr(st,buf);
  5249. else
  5250. script_pushconststr(st,"");
  5251. break;
  5252. case 2:
  5253. buf=buildin_getguildname_sub(sd->status.guild_id);
  5254. if(buf != NULL)
  5255. script_pushstr(st,buf);
  5256. else
  5257. script_pushconststr(st,"");
  5258. break;
  5259. default:
  5260. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5261. script_pushconststr(st,"");
  5262. break;
  5263. }
  5264. return 0;
  5265. }
  5266. /*==========================================
  5267. * 呼び出し元のNPC情報を取得する
  5268. *------------------------------------------*/
  5269. BUILDIN_FUNC(strnpcinfo)
  5270. {
  5271. TBL_NPC* nd;
  5272. int num;
  5273. char *buf,*name=NULL;
  5274. nd = map_id2nd(st->oid);
  5275. if (!nd) {
  5276. script_pushconststr(st, "");
  5277. return 0;
  5278. }
  5279. num = script_getnum(st,2);
  5280. switch(num){
  5281. case 0: // display name
  5282. name = aStrdup(nd->name);
  5283. break;
  5284. case 1: // visible part of display name name
  5285. if((buf = strchr(nd->name,'#')) != NULL)
  5286. {
  5287. name = aStrdup(nd->name);
  5288. name[buf - nd->name] = 0;
  5289. }
  5290. break;
  5291. case 2: // # fragment
  5292. if((buf = strchr(nd->name,'#')) != NULL)
  5293. name = aStrdup(buf+1);
  5294. break;
  5295. case 3: // unique name
  5296. name = aStrdup(nd->exname);
  5297. break;
  5298. }
  5299. if(name)
  5300. script_pushstr(st, name);
  5301. else
  5302. script_pushconststr(st, "");
  5303. return 0;
  5304. }
  5305. 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};
  5306. /*==========================================
  5307. * GetEquipID(Pos); Pos: 1-10
  5308. *------------------------------------------*/
  5309. BUILDIN_FUNC(getequipid)
  5310. {
  5311. int i=-1,num;
  5312. TBL_PC *sd;
  5313. struct item_data* item;
  5314. sd=script_rid2sd(st);
  5315. if(sd == NULL)
  5316. return 0;
  5317. num=script_getnum(st,2);
  5318. if (num > 0 && num <= ARRAYLENGTH(equip))
  5319. i=pc_checkequip(sd,equip[num-1]);
  5320. if(i >= 0){
  5321. item=sd->inventory_data[i];
  5322. if(item)
  5323. script_pushint(st,item->nameid);
  5324. else
  5325. script_pushint(st,0);
  5326. }else{
  5327. script_pushint(st,-1);
  5328. }
  5329. return 0;
  5330. }
  5331. /*==========================================
  5332. * 装備名文字列(精錬メニュー用)
  5333. *------------------------------------------*/
  5334. BUILDIN_FUNC(getequipname)
  5335. {
  5336. int i=-1,num;
  5337. TBL_PC *sd;
  5338. struct item_data* item;
  5339. char *buf;
  5340. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  5341. sd = script_rid2sd(st);
  5342. if( sd == NULL )
  5343. return 0;
  5344. buf=(char *)aMallocA(64*sizeof(char));
  5345. num=script_getnum(st,2);
  5346. if (num > 0 && num <= ARRAYLENGTH(equip))
  5347. i=pc_checkequip(sd,equip[num-1]);
  5348. if(i >= 0){
  5349. item=sd->inventory_data[i];
  5350. if(item)
  5351. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5352. else
  5353. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5354. }else{
  5355. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5356. }
  5357. script_pushstr(st,buf);
  5358. return 0;
  5359. }
  5360. /*==========================================
  5361. * getbrokenid [Valaris]
  5362. *------------------------------------------*/
  5363. BUILDIN_FUNC(getbrokenid)
  5364. {
  5365. int i,num,id=0,brokencounter=0;
  5366. TBL_PC *sd;
  5367. sd = script_rid2sd(st);
  5368. if( sd == NULL )
  5369. return 0;
  5370. num=script_getnum(st,2);
  5371. for(i=0; i<MAX_INVENTORY; i++) {
  5372. if(sd->status.inventory[i].attribute==1){
  5373. brokencounter++;
  5374. if(num==brokencounter){
  5375. id=sd->status.inventory[i].nameid;
  5376. break;
  5377. }
  5378. }
  5379. }
  5380. script_pushint(st,id);
  5381. return 0;
  5382. }
  5383. /*==========================================
  5384. * repair [Valaris]
  5385. *------------------------------------------*/
  5386. BUILDIN_FUNC(repair)
  5387. {
  5388. int i,num;
  5389. int repaircounter=0;
  5390. TBL_PC *sd;
  5391. sd = script_rid2sd(st);
  5392. if( sd == NULL )
  5393. return 0;
  5394. num=script_getnum(st,2);
  5395. for(i=0; i<MAX_INVENTORY; i++) {
  5396. if(sd->status.inventory[i].attribute==1){
  5397. repaircounter++;
  5398. if(num==repaircounter){
  5399. sd->status.inventory[i].attribute=0;
  5400. clif_equiplist(sd);
  5401. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5402. clif_misceffect(&sd->bl, 3);
  5403. break;
  5404. }
  5405. }
  5406. }
  5407. return 0;
  5408. }
  5409. /*==========================================
  5410. * 装備チェック
  5411. *------------------------------------------*/
  5412. BUILDIN_FUNC(getequipisequiped)
  5413. {
  5414. int i=-1,num;
  5415. TBL_PC *sd;
  5416. num=script_getnum(st,2);
  5417. sd = script_rid2sd(st);
  5418. if( sd == NULL )
  5419. return 0;
  5420. if (num > 0 && num <= ARRAYLENGTH(equip))
  5421. i=pc_checkequip(sd,equip[num-1]);
  5422. if(i >= 0)
  5423. script_pushint(st,1);
  5424. else
  5425. script_pushint(st,0);
  5426. return 0;
  5427. }
  5428. /*==========================================
  5429. * 装備品精錬可能チェック
  5430. *------------------------------------------*/
  5431. BUILDIN_FUNC(getequipisenableref)
  5432. {
  5433. int i=-1,num;
  5434. TBL_PC *sd;
  5435. num=script_getnum(st,2);
  5436. sd = script_rid2sd(st);
  5437. if( sd == NULL )
  5438. return 0;
  5439. if (num > 0 && num <= ARRAYLENGTH(equip))
  5440. i=pc_checkequip(sd,equip[num-1]);
  5441. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5442. {
  5443. script_pushint(st,1);
  5444. } else {
  5445. script_pushint(st,0);
  5446. }
  5447. return 0;
  5448. }
  5449. /*==========================================
  5450. * 装備品鑑定チェック
  5451. *------------------------------------------*/
  5452. BUILDIN_FUNC(getequipisidentify)
  5453. {
  5454. int i=-1,num;
  5455. TBL_PC *sd;
  5456. num=script_getnum(st,2);
  5457. sd = script_rid2sd(st);
  5458. if( sd == NULL )
  5459. return 0;
  5460. if (num > 0 && num <= ARRAYLENGTH(equip))
  5461. i=pc_checkequip(sd,equip[num-1]);
  5462. if(i >= 0)
  5463. script_pushint(st,sd->status.inventory[i].identify);
  5464. else
  5465. script_pushint(st,0);
  5466. return 0;
  5467. }
  5468. /*==========================================
  5469. * 装備品精錬度
  5470. *------------------------------------------*/
  5471. BUILDIN_FUNC(getequiprefinerycnt)
  5472. {
  5473. int i=-1,num;
  5474. TBL_PC *sd;
  5475. num=script_getnum(st,2);
  5476. sd = script_rid2sd(st);
  5477. if( sd == NULL )
  5478. return 0;
  5479. if (num > 0 && num <= ARRAYLENGTH(equip))
  5480. i=pc_checkequip(sd,equip[num-1]);
  5481. if(i >= 0)
  5482. script_pushint(st,sd->status.inventory[i].refine);
  5483. else
  5484. script_pushint(st,0);
  5485. return 0;
  5486. }
  5487. /*==========================================
  5488. * 装備品武器LV
  5489. *------------------------------------------*/
  5490. BUILDIN_FUNC(getequipweaponlv)
  5491. {
  5492. int i=-1,num;
  5493. TBL_PC *sd;
  5494. num=script_getnum(st,2);
  5495. sd = script_rid2sd(st);
  5496. if( sd == NULL )
  5497. return 0;
  5498. if (num > 0 && num <= ARRAYLENGTH(equip))
  5499. i=pc_checkequip(sd,equip[num-1]);
  5500. if(i >= 0 && sd->inventory_data[i])
  5501. script_pushint(st,sd->inventory_data[i]->wlv);
  5502. else
  5503. script_pushint(st,0);
  5504. return 0;
  5505. }
  5506. /*==========================================
  5507. * 装備品精錬成功率
  5508. *------------------------------------------*/
  5509. BUILDIN_FUNC(getequippercentrefinery)
  5510. {
  5511. int i=-1,num;
  5512. TBL_PC *sd;
  5513. num=script_getnum(st,2);
  5514. sd = script_rid2sd(st);
  5515. if( sd == NULL )
  5516. return 0;
  5517. if (num > 0 && num <= ARRAYLENGTH(equip))
  5518. i=pc_checkequip(sd,equip[num-1]);
  5519. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5520. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5521. else
  5522. script_pushint(st,0);
  5523. return 0;
  5524. }
  5525. /*==========================================
  5526. * 精錬成功
  5527. *------------------------------------------*/
  5528. BUILDIN_FUNC(successrefitem)
  5529. {
  5530. int i=-1,num,ep;
  5531. TBL_PC *sd;
  5532. num=script_getnum(st,2);
  5533. sd = script_rid2sd(st);
  5534. if( sd == NULL )
  5535. return 0;
  5536. if (num > 0 && num <= ARRAYLENGTH(equip))
  5537. i=pc_checkequip(sd,equip[num-1]);
  5538. if(i >= 0) {
  5539. ep=sd->status.inventory[i].equip;
  5540. //Logs items, got from (N)PC scripts [Lupus]
  5541. if(log_config.enable_logs&0x40)
  5542. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5543. sd->status.inventory[i].refine++;
  5544. pc_unequipitem(sd,i,2);
  5545. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5546. clif_delitem(sd,i,1);
  5547. //Logs items, got from (N)PC scripts [Lupus]
  5548. if(log_config.enable_logs&0x40)
  5549. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5550. clif_additem(sd,i,1,0);
  5551. pc_equipitem(sd,i,ep);
  5552. clif_misceffect(&sd->bl,3);
  5553. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5554. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5555. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5556. ){ // Fame point system [DracoRPG]
  5557. switch (sd->inventory_data[i]->wlv){
  5558. case 1:
  5559. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5560. break;
  5561. case 2:
  5562. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5563. break;
  5564. case 3:
  5565. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5566. break;
  5567. }
  5568. }
  5569. }
  5570. return 0;
  5571. }
  5572. /*==========================================
  5573. * 精錬失敗
  5574. *------------------------------------------*/
  5575. BUILDIN_FUNC(failedrefitem)
  5576. {
  5577. int i=-1,num;
  5578. TBL_PC *sd;
  5579. num=script_getnum(st,2);
  5580. sd = script_rid2sd(st);
  5581. if( sd == NULL )
  5582. return 0;
  5583. if (num > 0 && num <= ARRAYLENGTH(equip))
  5584. i=pc_checkequip(sd,equip[num-1]);
  5585. if(i >= 0) {
  5586. //Logs items, got from (N)PC scripts [Lupus]
  5587. if(log_config.enable_logs&0x40)
  5588. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5589. sd->status.inventory[i].refine = 0;
  5590. pc_unequipitem(sd,i,3);
  5591. // 精錬失敗エフェクトのパケット
  5592. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5593. pc_delitem(sd,i,1,0);
  5594. // 他の人にも失敗を通知
  5595. clif_misceffect(&sd->bl,2);
  5596. }
  5597. return 0;
  5598. }
  5599. /*==========================================
  5600. *
  5601. *------------------------------------------*/
  5602. BUILDIN_FUNC(statusup)
  5603. {
  5604. int type;
  5605. TBL_PC *sd;
  5606. type=script_getnum(st,2);
  5607. sd = script_rid2sd(st);
  5608. if( sd == NULL )
  5609. return 0;
  5610. pc_statusup(sd,type);
  5611. return 0;
  5612. }
  5613. /*==========================================
  5614. *
  5615. *------------------------------------------*/
  5616. BUILDIN_FUNC(statusup2)
  5617. {
  5618. int type,val;
  5619. TBL_PC *sd;
  5620. type=script_getnum(st,2);
  5621. val=script_getnum(st,3);
  5622. sd = script_rid2sd(st);
  5623. if( sd == NULL )
  5624. return 0;
  5625. pc_statusup2(sd,type,val);
  5626. return 0;
  5627. }
  5628. /// See 'doc/item_bonus.txt'
  5629. ///
  5630. /// bonus <bonus type>,<val1>;
  5631. /// bonus2 <bonus type>,<val1>,<val2>;
  5632. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5633. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5634. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5635. BUILDIN_FUNC(bonus)
  5636. {
  5637. int type;
  5638. int val1;
  5639. int val2 = 0;
  5640. int val3 = 0;
  5641. int val4 = 0;
  5642. int val5 = 0;
  5643. TBL_PC* sd;
  5644. sd = script_rid2sd(st);
  5645. if( sd == NULL )
  5646. return 0; // no player attached
  5647. type = script_getnum(st,2);
  5648. switch( type )
  5649. {
  5650. case SP_AUTOSPELL:
  5651. case SP_AUTOSPELL_WHENHIT:
  5652. case SP_SKILL_ATK:
  5653. case SP_SKILL_HEAL:
  5654. case SP_ADD_SKILL_BLOW:
  5655. case SP_CASTRATE:
  5656. // these bonuses support skill names
  5657. val1 = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  5658. break;
  5659. default:
  5660. val1 = script_getnum(st,3);
  5661. break;
  5662. }
  5663. switch( script_lastdata(st)-2 )
  5664. {
  5665. case 1:
  5666. pc_bonus(sd, type, val1);
  5667. break;
  5668. case 2:
  5669. val2 = script_getnum(st,4);
  5670. pc_bonus2(sd, type, val1, val2);
  5671. break;
  5672. case 3:
  5673. val2 = script_getnum(st,4);
  5674. val3 = script_getnum(st,5);
  5675. pc_bonus3(sd, type, val1, val2, val3);
  5676. break;
  5677. case 4:
  5678. val2 = script_getnum(st,4);
  5679. val3 = script_getnum(st,5);
  5680. val4 = script_getnum(st,6);
  5681. pc_bonus4(sd, type, val1, val2, val3, val4);
  5682. break;
  5683. case 5:
  5684. val2 = script_getnum(st,4);
  5685. val3 = script_getnum(st,5);
  5686. val4 = script_getnum(st,6);
  5687. val5 = script_getnum(st,7);
  5688. pc_bonus5(sd, type, val1, val2, val3, val4, val5);
  5689. break;
  5690. default:
  5691. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 1));
  5692. break;
  5693. }
  5694. return 0;
  5695. }
  5696. /// Bonus script that has a chance of being executed on attack.
  5697. BUILDIN_FUNC(bonusautoscript)
  5698. {
  5699. int rate, flag = 0;
  5700. const char *str;
  5701. struct script_code *script;
  5702. TBL_PC* sd;
  5703. sd = script_rid2sd(st);
  5704. if( sd == NULL )
  5705. return 0;// no player attached, report source
  5706. str = script_getstr(st,2);
  5707. rate = script_getnum(st,3);
  5708. if( script_hasdata(st,4) )
  5709. flag = script_getnum(st,4);
  5710. script = parse_script(str, "autoscript bonus", 0, 0);
  5711. if (!script)
  5712. return 1;
  5713. if (!pc_autoscript_add(sd->autoscript, ARRAYLENGTH(sd->autoscript), rate, flag, script))
  5714. {
  5715. script_free_code(script);
  5716. return 1;
  5717. }
  5718. return 0;
  5719. }
  5720. /// Bonus script that has a chance of being executed when attacked.
  5721. BUILDIN_FUNC(bonusautoscript2)
  5722. {
  5723. int rate, flag = 0;
  5724. const char *str;
  5725. struct script_code *script;
  5726. TBL_PC* sd;
  5727. sd = script_rid2sd(st);
  5728. if( sd == NULL )
  5729. return 0;// no player attached, report source
  5730. str = script_getstr(st,2);
  5731. rate = script_getnum(st,3);
  5732. if( script_hasdata(st,4) )
  5733. flag = script_getnum(st,4);
  5734. script = parse_script(str, "autoscript2 bonus", 0, 0);
  5735. if (!script)
  5736. return 1;
  5737. if (!pc_autoscript_add(sd->autoscript2, ARRAYLENGTH(sd->autoscript2), rate, flag, script))
  5738. {
  5739. script_free_code(script);
  5740. return 1;
  5741. }
  5742. return 0;
  5743. }
  5744. /// Changes the level of a player skill.
  5745. /// <flag> defaults to 1
  5746. /// <flag>=0 : set the level of the skill
  5747. /// <flag>=1 : set the temporary level of the skill
  5748. /// <flag>=2 : add to the level of the skill
  5749. ///
  5750. /// skill <skill id>,<level>,<flag>
  5751. /// skill <skill id>,<level>
  5752. /// skill "<skill name>",<level>,<flag>
  5753. /// skill "<skill name>",<level>
  5754. BUILDIN_FUNC(skill)
  5755. {
  5756. int id;
  5757. int level;
  5758. int flag = 1;
  5759. TBL_PC* sd;
  5760. sd = script_rid2sd(st);
  5761. if( sd == NULL )
  5762. return 0;// no player attached, report source
  5763. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5764. level = script_getnum(st,3);
  5765. if( script_hasdata(st,4) )
  5766. flag = script_getnum(st,4);
  5767. pc_skill(sd, id, level, flag);
  5768. return 0;
  5769. }
  5770. /// Changes the level of a player skill.
  5771. /// like skill, but <flag> defaults to 2
  5772. ///
  5773. /// addtoskill <skill id>,<amount>,<flag>
  5774. /// addtoskill <skill id>,<amount>
  5775. /// addtoskill "<skill name>",<amount>,<flag>
  5776. /// addtoskill "<skill name>",<amount>
  5777. ///
  5778. /// @see skill
  5779. BUILDIN_FUNC(addtoskill)
  5780. {
  5781. int id;
  5782. int level;
  5783. int flag = 2;
  5784. TBL_PC* sd;
  5785. sd = script_rid2sd(st);
  5786. if( sd == NULL )
  5787. return 0;// no player attached, report source
  5788. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5789. level = script_getnum(st,3);
  5790. if( script_hasdata(st,4) )
  5791. flag = script_getnum(st,4);
  5792. pc_skill(sd, id, level, flag);
  5793. return 0;
  5794. }
  5795. /// Increases the level of a guild skill.
  5796. ///
  5797. /// guildskill <skill id>,<amount>;
  5798. /// guildskill "<skill name>",<amount>;
  5799. BUILDIN_FUNC(guildskill)
  5800. {
  5801. int id;
  5802. int level;
  5803. TBL_PC* sd;
  5804. int i;
  5805. sd = script_rid2sd(st);
  5806. if( sd == NULL )
  5807. return 0;// no player attached, report source
  5808. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5809. level = script_getnum(st,3);
  5810. for( i=0; i < level; i++ )
  5811. guild_skillup(sd, id);
  5812. return 0;
  5813. }
  5814. /// Returns the level of the player skill.
  5815. ///
  5816. /// getskilllv(<skill id>) -> <level>
  5817. /// getskilllv("<skill name>") -> <level>
  5818. BUILDIN_FUNC(getskilllv)
  5819. {
  5820. int id;
  5821. TBL_PC* sd;
  5822. sd = script_rid2sd(st);
  5823. if( sd == NULL )
  5824. return 0;// no player attached, report source
  5825. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  5826. script_pushint(st, pc_checkskill(sd,id));
  5827. return 0;
  5828. }
  5829. /// Returns the level of the guild skill.
  5830. ///
  5831. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  5832. /// getgdskilllv(<guild id>,"<skill name>") -> <level>
  5833. BUILDIN_FUNC(getgdskilllv)
  5834. {
  5835. int guild_id;
  5836. int skill_id;
  5837. struct guild* g;
  5838. guild_id = script_getnum(st,2);
  5839. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  5840. g = guild_search(guild_id);
  5841. if( g == NULL )
  5842. script_pushint(st, -1);
  5843. else
  5844. script_pushint(st, guild_checkskill(g,skill_id));
  5845. return 0;
  5846. }
  5847. /// Returns the 'basic_skill_check' setting.
  5848. /// This config determines if the server checks the skill level of NV_BASIC
  5849. /// before allowing the basic actions.
  5850. ///
  5851. /// basicskillcheck() -> <bool>
  5852. BUILDIN_FUNC(basicskillcheck)
  5853. {
  5854. script_pushint(st, battle_config.basic_skill_check);
  5855. return 0;
  5856. }
  5857. /// Returns the GM level of the player.
  5858. ///
  5859. /// getgmlevel() -> <level>
  5860. BUILDIN_FUNC(getgmlevel)
  5861. {
  5862. TBL_PC* sd;
  5863. sd = script_rid2sd(st);
  5864. if( sd == NULL )
  5865. return 0;// no player attached, report source
  5866. script_pushint(st, pc_isGM(sd));
  5867. return 0;
  5868. }
  5869. /// Terminates the execution of this script instance.
  5870. ///
  5871. /// end
  5872. BUILDIN_FUNC(end)
  5873. {
  5874. st->state = END;
  5875. return 0;
  5876. }
  5877. /// Checks if the player has that effect state (option).
  5878. ///
  5879. /// checkoption(<option>) -> <bool>
  5880. BUILDIN_FUNC(checkoption)
  5881. {
  5882. int option;
  5883. TBL_PC* sd;
  5884. sd = script_rid2sd(st);
  5885. if( sd == NULL )
  5886. return 0;// no player attached, report source
  5887. option = script_getnum(st,2);
  5888. if( sd->sc.option&option )
  5889. script_pushint(st, 1);
  5890. else
  5891. script_pushint(st, 0);
  5892. return 0;
  5893. }
  5894. /// Checks if the player is in that body state (opt1).
  5895. ///
  5896. /// checkoption1(<opt1>) -> <bool>
  5897. BUILDIN_FUNC(checkoption1)
  5898. {
  5899. int opt1;
  5900. TBL_PC* sd;
  5901. sd = script_rid2sd(st);
  5902. if( sd == NULL )
  5903. return 0;// no player attached, report source
  5904. opt1 = script_getnum(st,2);
  5905. if( sd->sc.opt1 == opt1 )
  5906. script_pushint(st, 1);
  5907. else
  5908. script_pushint(st, 0);
  5909. return 0;
  5910. }
  5911. /// Checks if the player has that health state (opt2).
  5912. ///
  5913. /// checkoption2(<opt2>) -> <bool>
  5914. BUILDIN_FUNC(checkoption2)
  5915. {
  5916. int opt2;
  5917. TBL_PC* sd;
  5918. sd = script_rid2sd(st);
  5919. if( sd == NULL )
  5920. return 0;// no player attached, report source
  5921. opt2 = script_getnum(st,2);
  5922. if( sd->sc.opt2&opt2 )
  5923. script_pushint(st, 1);
  5924. else
  5925. script_pushint(st, 0);
  5926. return 0;
  5927. }
  5928. /// Changes the effect state (option) of the player.
  5929. /// <flag> defaults to 1
  5930. /// <flag>=0 : removes the option
  5931. /// <flag>=other : adds the option
  5932. ///
  5933. /// setoption <option>,<flag>;
  5934. /// setoption <option>;
  5935. BUILDIN_FUNC(setoption)
  5936. {
  5937. int option;
  5938. int flag = 1;
  5939. TBL_PC* sd;
  5940. sd = script_rid2sd(st);
  5941. if( sd == NULL )
  5942. return 0;// no player attached, report source
  5943. option = script_getnum(st,2);
  5944. if( script_hasdata(st,3) )
  5945. flag = script_getnum(st,3);
  5946. else if( !option ){// Request to remove everything.
  5947. flag = 0;
  5948. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  5949. }
  5950. if( flag ){// Add option
  5951. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  5952. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  5953. pc_setoption(sd, sd->sc.option|option);
  5954. } else// Remove option
  5955. pc_setoption(sd, sd->sc.option&~option);
  5956. return 0;
  5957. }
  5958. /// Returns if the player has a cart.
  5959. ///
  5960. /// checkcart() -> <bool>
  5961. ///
  5962. /// @author Valaris
  5963. BUILDIN_FUNC(checkcart)
  5964. {
  5965. TBL_PC* sd;
  5966. sd = script_rid2sd(st);
  5967. if( sd == NULL )
  5968. return 0;// no player attached, report source
  5969. if( pc_iscarton(sd) )
  5970. script_pushint(st, 1);
  5971. else
  5972. script_pushint(st, 0);
  5973. return 0;
  5974. }
  5975. /// Sets the cart of the player.
  5976. /// <type> defaults to 1
  5977. /// <type>=0 : removes the cart
  5978. /// <type>=1 : Normal cart
  5979. /// <type>=2 : Wooden cart
  5980. /// <type>=3 : Covered cart with flowers and ferns
  5981. /// <type>=4 : Wooden cart with a Panda doll on the back
  5982. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  5983. ///
  5984. /// setcart <type>;
  5985. /// setcart;
  5986. BUILDIN_FUNC(setcart)
  5987. {
  5988. int type = 1;
  5989. TBL_PC* sd;
  5990. sd = script_rid2sd(st);
  5991. if( sd == NULL )
  5992. return 0;// no player attached, report source
  5993. if( script_hasdata(st,2) )
  5994. type = script_getnum(st,2);
  5995. pc_setcart(sd, type);
  5996. return 0;
  5997. }
  5998. /// Returns if the player has a falcon.
  5999. ///
  6000. /// checkfalcon() -> <bool>
  6001. ///
  6002. /// @author Valaris
  6003. BUILDIN_FUNC(checkfalcon)
  6004. {
  6005. TBL_PC* sd;
  6006. sd = script_rid2sd(st);
  6007. if( sd == NULL )
  6008. return 0;// no player attached, report source
  6009. if( pc_isfalcon(sd) )
  6010. script_pushint(st, 1);
  6011. else
  6012. script_pushint(st, 0);
  6013. return 0;
  6014. }
  6015. /// Sets if the player has a falcon or not.
  6016. /// <flag> defaults to 1
  6017. ///
  6018. /// setfalcon <flag>;
  6019. /// setfalcon;
  6020. BUILDIN_FUNC(setfalcon)
  6021. {
  6022. int flag = 1;
  6023. TBL_PC* sd;
  6024. sd = script_rid2sd(st);
  6025. if( sd == NULL )
  6026. return 0;// no player attached, report source
  6027. if( script_hasdata(st,2) )
  6028. flag = script_getnum(st,2);
  6029. pc_setfalcon(sd, flag);
  6030. return 0;
  6031. }
  6032. /// Returns if the player is riding.
  6033. ///
  6034. /// checkriding() -> <bool>
  6035. ///
  6036. /// @author Valaris
  6037. BUILDIN_FUNC(checkriding)
  6038. {
  6039. TBL_PC* sd;
  6040. sd = script_rid2sd(st);
  6041. if( sd == NULL )
  6042. return 0;// no player attached, report source
  6043. if( pc_isriding(sd) )
  6044. script_pushint(st, 1);
  6045. else
  6046. script_pushint(st, 0);
  6047. return 0;
  6048. }
  6049. /// Sets if the player is riding.
  6050. /// <flag> defaults to 1
  6051. ///
  6052. /// setriding <flag>;
  6053. /// setriding;
  6054. BUILDIN_FUNC(setriding)
  6055. {
  6056. int flag = 1;
  6057. TBL_PC* sd;
  6058. sd = script_rid2sd(st);
  6059. if( sd == NULL )
  6060. return 0;// no player attached, report source
  6061. if( script_hasdata(st,2) )
  6062. flag = script_getnum(st,2);
  6063. pc_setriding(sd, flag);
  6064. return 0;
  6065. }
  6066. /// Sets the save point of the player.
  6067. ///
  6068. /// save "<map name>",<x>,<y>
  6069. /// savepoint "<map name>",<x>,<y>
  6070. BUILDIN_FUNC(savepoint)
  6071. {
  6072. int x;
  6073. int y;
  6074. short map;
  6075. const char* str;
  6076. TBL_PC* sd;
  6077. sd = script_rid2sd(st);
  6078. if( sd == NULL )
  6079. return 0;// no player attached, report source
  6080. str = script_getstr(st, 2);
  6081. x = script_getnum(st,3);
  6082. y = script_getnum(st,4);
  6083. map = mapindex_name2id(str);
  6084. if( map )
  6085. pc_setsavepoint(sd, map, x, y);
  6086. return 0;
  6087. }
  6088. /*==========================================
  6089. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6090. *------------------------------------------*/
  6091. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6092. {
  6093. int type;
  6094. time_t timer;
  6095. struct tm *t;
  6096. type=script_getnum(st,2);
  6097. switch(type){
  6098. case 2:
  6099. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6100. // from the system clock.)
  6101. script_pushint(st,(int)time(NULL));
  6102. break;
  6103. case 1:
  6104. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6105. time(&timer);
  6106. t=localtime(&timer);
  6107. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6108. break;
  6109. case 0:
  6110. default:
  6111. //type 0:(System Ticks)
  6112. script_pushint(st,gettick());
  6113. break;
  6114. }
  6115. return 0;
  6116. }
  6117. /*==========================================
  6118. * GetTime(Type);
  6119. * 1: Sec 2: Min 3: Hour
  6120. * 4: WeekDay 5: MonthDay 6: Month
  6121. * 7: Year
  6122. *------------------------------------------*/
  6123. BUILDIN_FUNC(gettime) /* Asgard Version */
  6124. {
  6125. int type;
  6126. time_t timer;
  6127. struct tm *t;
  6128. type=script_getnum(st,2);
  6129. time(&timer);
  6130. t=localtime(&timer);
  6131. switch(type){
  6132. case 1://Sec(0~59)
  6133. script_pushint(st,t->tm_sec);
  6134. break;
  6135. case 2://Min(0~59)
  6136. script_pushint(st,t->tm_min);
  6137. break;
  6138. case 3://Hour(0~23)
  6139. script_pushint(st,t->tm_hour);
  6140. break;
  6141. case 4://WeekDay(0~6)
  6142. script_pushint(st,t->tm_wday);
  6143. break;
  6144. case 5://MonthDay(01~31)
  6145. script_pushint(st,t->tm_mday);
  6146. break;
  6147. case 6://Month(01~12)
  6148. script_pushint(st,t->tm_mon+1);
  6149. break;
  6150. case 7://Year(20xx)
  6151. script_pushint(st,t->tm_year+1900);
  6152. break;
  6153. case 8://Year Day(01~366)
  6154. script_pushint(st,t->tm_yday+1);
  6155. break;
  6156. default://(format error)
  6157. script_pushint(st,-1);
  6158. break;
  6159. }
  6160. return 0;
  6161. }
  6162. /*==========================================
  6163. * GetTimeStr("TimeFMT", Length);
  6164. *------------------------------------------*/
  6165. BUILDIN_FUNC(gettimestr)
  6166. {
  6167. char *tmpstr;
  6168. const char *fmtstr;
  6169. int maxlen;
  6170. time_t now = time(NULL);
  6171. fmtstr=script_getstr(st,2);
  6172. maxlen=script_getnum(st,3);
  6173. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6174. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6175. tmpstr[maxlen]='\0';
  6176. script_pushstr(st,tmpstr);
  6177. return 0;
  6178. }
  6179. /*==========================================
  6180. * カプラ倉庫を開く
  6181. *------------------------------------------*/
  6182. BUILDIN_FUNC(openstorage)
  6183. {
  6184. TBL_PC* sd;
  6185. sd = script_rid2sd(st);
  6186. if( sd == NULL )
  6187. return 0;
  6188. storage_storageopen(sd);
  6189. return 0;
  6190. }
  6191. BUILDIN_FUNC(guildopenstorage)
  6192. {
  6193. TBL_PC* sd;
  6194. int ret;
  6195. sd = script_rid2sd(st);
  6196. if( sd == NULL )
  6197. return 0;
  6198. ret = storage_guild_storageopen(sd);
  6199. script_pushint(st,ret);
  6200. return 0;
  6201. }
  6202. /*==========================================
  6203. * アイテムによるスキル発動
  6204. *------------------------------------------*/
  6205. /// itemskill <skill id>,<level>
  6206. /// itemskill "<skill name>",<level>
  6207. BUILDIN_FUNC(itemskill)
  6208. {
  6209. int id;
  6210. int lv;
  6211. TBL_PC* sd;
  6212. sd = script_rid2sd(st);
  6213. if( sd == NULL || sd->ud.skilltimer != INVALID_TIMER )
  6214. return 0;
  6215. id = ( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  6216. lv = script_getnum(st,3);
  6217. sd->skillitem=id;
  6218. sd->skillitemlv=lv;
  6219. clif_item_skill(sd,id,lv);
  6220. return 0;
  6221. }
  6222. /*==========================================
  6223. * アイテム作成
  6224. *------------------------------------------*/
  6225. BUILDIN_FUNC(produce)
  6226. {
  6227. int trigger;
  6228. TBL_PC* sd;
  6229. sd = script_rid2sd(st);
  6230. if( sd == NULL )
  6231. return 0;
  6232. trigger=script_getnum(st,2);
  6233. clif_skill_produce_mix_list(sd, trigger);
  6234. return 0;
  6235. }
  6236. /*==========================================
  6237. * NPCでペット作る
  6238. *------------------------------------------*/
  6239. BUILDIN_FUNC(makepet)
  6240. {
  6241. TBL_PC* sd;
  6242. int id,pet_id;
  6243. id=script_getnum(st,2);
  6244. sd = script_rid2sd(st);
  6245. if( sd == NULL )
  6246. return 0;
  6247. pet_id = search_petDB_index(id, PET_CLASS);
  6248. if (pet_id < 0)
  6249. pet_id = search_petDB_index(id, PET_EGG);
  6250. if (pet_id >= 0 && sd) {
  6251. sd->catch_target_class = pet_db[pet_id].class_;
  6252. intif_create_pet(
  6253. sd->status.account_id, sd->status.char_id,
  6254. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6255. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6256. 100, 0, 1, pet_db[pet_id].jname);
  6257. }
  6258. return 0;
  6259. }
  6260. /*==========================================
  6261. * NPCで経験値上げる
  6262. *------------------------------------------*/
  6263. BUILDIN_FUNC(getexp)
  6264. {
  6265. TBL_PC* sd;
  6266. int base=0,job=0;
  6267. double bonus;
  6268. sd = script_rid2sd(st);
  6269. if( sd == NULL )
  6270. return 0;
  6271. base=script_getnum(st,2);
  6272. job =script_getnum(st,3);
  6273. if(base<0 || job<0)
  6274. return 0;
  6275. // bonus for npc-given exp
  6276. bonus = battle_config.quest_exp_rate / 100.;
  6277. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6278. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6279. pc_gainexp(sd, NULL, base, job);
  6280. return 0;
  6281. }
  6282. /*==========================================
  6283. * Gain guild exp [Celest]
  6284. *------------------------------------------*/
  6285. BUILDIN_FUNC(guildgetexp)
  6286. {
  6287. TBL_PC* sd;
  6288. int exp;
  6289. sd = script_rid2sd(st);
  6290. if( sd == NULL )
  6291. return 0;
  6292. exp = script_getnum(st,2);
  6293. if(exp < 0)
  6294. return 0;
  6295. if(sd && sd->status.guild_id > 0)
  6296. guild_getexp (sd, exp);
  6297. return 0;
  6298. }
  6299. /*==========================================
  6300. * Changes the guild master of a guild [Skotlex]
  6301. *------------------------------------------*/
  6302. BUILDIN_FUNC(guildchangegm)
  6303. {
  6304. TBL_PC *sd;
  6305. int guild_id;
  6306. const char *name;
  6307. guild_id = script_getnum(st,2);
  6308. name = script_getstr(st,3);
  6309. sd=map_nick2sd(name);
  6310. if (!sd)
  6311. script_pushint(st,0);
  6312. else
  6313. script_pushint(st,guild_gm_change(guild_id, sd));
  6314. return 0;
  6315. }
  6316. /*==========================================
  6317. * モンスター発生
  6318. *------------------------------------------*/
  6319. BUILDIN_FUNC(monster)
  6320. {
  6321. const char* map = script_getstr(st,2);
  6322. int x = script_getnum(st,3);
  6323. int y = script_getnum(st,4);
  6324. const char* str = script_getstr(st,5);
  6325. int class_ = script_getnum(st,6);
  6326. int amount = script_getnum(st,7);
  6327. const char* event = "";
  6328. struct map_session_data* sd;
  6329. int m;
  6330. if( script_hasdata(st,8) )
  6331. {
  6332. event = script_getstr(st,8);
  6333. check_event(st, event);
  6334. }
  6335. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6336. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6337. return 1;
  6338. }
  6339. sd = map_id2sd(st->rid);
  6340. if( sd && strcmp(map,"this") == 0 )
  6341. m = sd->bl.m;
  6342. else
  6343. m = map_mapname2mapid(map);
  6344. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6345. return 0;
  6346. }
  6347. /*==========================================
  6348. * Request List of Monster Drops
  6349. *------------------------------------------*/
  6350. BUILDIN_FUNC(getmobdrops)
  6351. {
  6352. int class_ = script_getnum(st,2);
  6353. int i, j = 0;
  6354. struct item_data *i_data;
  6355. struct mob_db *mob;
  6356. if( !mobdb_checkid(class_) )
  6357. {
  6358. script_pushint(st, 0);
  6359. return 0;
  6360. }
  6361. mob = mob_db(class_);
  6362. for( i = 0; i < MAX_MOB_DROP; i++ )
  6363. {
  6364. if( mob->dropitem[i].nameid < 1 )
  6365. continue;
  6366. if( (i_data = itemdb_exists(mob->dropitem[i].nameid)) == NULL )
  6367. continue;
  6368. mapreg_setreg(add_str("$@MobDrop_item") + (j<<24), mob->dropitem[i].nameid);
  6369. mapreg_setreg(add_str("$@MobDrop_rate") + (j<<24), mob->dropitem[i].p);
  6370. j++;
  6371. }
  6372. mapreg_setreg(add_str("$@MobDrop_count"), j);
  6373. script_pushint(st, 1);
  6374. return 0;
  6375. }
  6376. /*==========================================
  6377. * モンスター発生
  6378. *------------------------------------------*/
  6379. BUILDIN_FUNC(areamonster)
  6380. {
  6381. const char* map = script_getstr(st,2);
  6382. int x0 = script_getnum(st,3);
  6383. int y0 = script_getnum(st,4);
  6384. int x1 = script_getnum(st,5);
  6385. int y1 = script_getnum(st,6);
  6386. const char* str = script_getstr(st,7);
  6387. int class_ = script_getnum(st,8);
  6388. int amount = script_getnum(st,9);
  6389. const char* event = "";
  6390. struct map_session_data* sd;
  6391. int m;
  6392. if( script_hasdata(st,10) )
  6393. {
  6394. event = script_getstr(st,10);
  6395. check_event(st, event);
  6396. }
  6397. sd = map_id2sd(st->rid);
  6398. if( sd && strcmp(map,"this") == 0 )
  6399. m = sd->bl.m;
  6400. else
  6401. m = map_mapname2mapid(map);
  6402. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6403. return 0;
  6404. }
  6405. /*==========================================
  6406. * モンスター削除
  6407. *------------------------------------------*/
  6408. static int buildin_killmonster_sub_strip(struct block_list *bl,va_list ap)
  6409. { //same fix but with killmonster instead - stripping events from mobs.
  6410. TBL_MOB* md = (TBL_MOB*)bl;
  6411. char *event=va_arg(ap,char *);
  6412. int allflag=va_arg(ap,int);
  6413. md->state.npc_killmonster = 1;
  6414. if(!allflag){
  6415. if(strcmp(event,md->npc_event)==0)
  6416. status_kill(bl);
  6417. }else{
  6418. if(!md->spawn)
  6419. status_kill(bl);
  6420. }
  6421. md->state.npc_killmonster = 0;
  6422. return 0;
  6423. }
  6424. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6425. {
  6426. TBL_MOB* md = (TBL_MOB*)bl;
  6427. char *event=va_arg(ap,char *);
  6428. int allflag=va_arg(ap,int);
  6429. if(!allflag){
  6430. if(strcmp(event,md->npc_event)==0)
  6431. status_kill(bl);
  6432. }else{
  6433. if(!md->spawn)
  6434. status_kill(bl);
  6435. }
  6436. return 0;
  6437. }
  6438. BUILDIN_FUNC(killmonster)
  6439. {
  6440. const char *mapname,*event;
  6441. int m,allflag=0;
  6442. mapname=script_getstr(st,2);
  6443. event=script_getstr(st,3);
  6444. if(strcmp(event,"All")==0)
  6445. allflag = 1;
  6446. else
  6447. check_event(st, event);
  6448. if( (m=map_mapname2mapid(mapname))<0 )
  6449. return 0;
  6450. if( script_hasdata(st,4) ) {
  6451. if ( script_getnum(st,4) == 1 ) {
  6452. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6453. return 0;
  6454. }
  6455. }
  6456. map_foreachinmap(buildin_killmonster_sub_strip, m, BL_MOB, event ,allflag);
  6457. return 0;
  6458. }
  6459. static int buildin_killmonsterall_sub_strip(struct block_list *bl,va_list ap)
  6460. { //Strips the event from the mob if it's killed the old method.
  6461. struct mob_data *md;
  6462. md = BL_CAST(BL_MOB, bl);
  6463. if (md->npc_event[0])
  6464. md->npc_event[0] = 0;
  6465. status_kill(bl);
  6466. return 0;
  6467. }
  6468. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6469. {
  6470. status_kill(bl);
  6471. return 0;
  6472. }
  6473. BUILDIN_FUNC(killmonsterall)
  6474. {
  6475. const char *mapname;
  6476. int m;
  6477. mapname=script_getstr(st,2);
  6478. if( (m=map_mapname2mapid(mapname))<0 )
  6479. return 0;
  6480. if( script_hasdata(st,3) ) {
  6481. if ( script_getnum(st,3) == 1 ) {
  6482. map_foreachinmap(buildin_killmonsterall_sub,m,BL_MOB);
  6483. return 0;
  6484. }
  6485. }
  6486. map_foreachinmap(buildin_killmonsterall_sub_strip,m,BL_MOB);
  6487. return 0;
  6488. }
  6489. /*==========================================
  6490. * Creates a clone of a player.
  6491. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6492. *------------------------------------------*/
  6493. BUILDIN_FUNC(clone)
  6494. {
  6495. TBL_PC *sd, *msd=NULL;
  6496. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6497. unsigned int duration = 0;
  6498. const char *map,*event="";
  6499. map=script_getstr(st,2);
  6500. x=script_getnum(st,3);
  6501. y=script_getnum(st,4);
  6502. event=script_getstr(st,5);
  6503. char_id=script_getnum(st,6);
  6504. if( script_hasdata(st,7) )
  6505. master_id=script_getnum(st,7);
  6506. if( script_hasdata(st,8) )
  6507. mode=script_getnum(st,8);
  6508. if( script_hasdata(st,9) )
  6509. flag=script_getnum(st,9);
  6510. if( script_hasdata(st,10) )
  6511. duration=script_getnum(st,10);
  6512. check_event(st, event);
  6513. m = map_mapname2mapid(map);
  6514. if (m < 0) return 0;
  6515. sd = map_charid2sd(char_id);
  6516. if (master_id) {
  6517. msd = map_charid2sd(master_id);
  6518. if (msd)
  6519. master_id = msd->bl.id;
  6520. else
  6521. master_id = 0;
  6522. }
  6523. if (sd) //Return ID of newly crafted clone.
  6524. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6525. else //Failed to create clone.
  6526. script_pushint(st,0);
  6527. return 0;
  6528. }
  6529. /*==========================================
  6530. * イベント実行
  6531. *------------------------------------------*/
  6532. BUILDIN_FUNC(doevent)
  6533. {
  6534. const char* event = script_getstr(st,2);
  6535. check_event(st, event);
  6536. npc_event(map_id2sd(st->rid),event,0);
  6537. return 0;
  6538. }
  6539. /*==========================================
  6540. * NPC主体イベント実行
  6541. *------------------------------------------*/
  6542. BUILDIN_FUNC(donpcevent)
  6543. {
  6544. const char* event = script_getstr(st,2);
  6545. check_event(st, event);
  6546. npc_event_do(event);
  6547. return 0;
  6548. }
  6549. /// for Aegis compatibility
  6550. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6551. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6552. {
  6553. const char* npc = script_getstr(st,2);
  6554. const char* command = script_getstr(st,3);
  6555. char event[51];
  6556. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6557. check_event(st, event);
  6558. npc_event_do(event);
  6559. return 0;
  6560. }
  6561. /*==========================================
  6562. * イベントタイマー追加
  6563. *------------------------------------------*/
  6564. BUILDIN_FUNC(addtimer)
  6565. {
  6566. int tick = script_getnum(st,2);
  6567. const char* event = script_getstr(st, 3);
  6568. TBL_PC* sd;
  6569. check_event(st, event);
  6570. sd = script_rid2sd(st);
  6571. if( sd == NULL )
  6572. return 0;
  6573. pc_addeventtimer(sd,tick,event);
  6574. return 0;
  6575. }
  6576. /*==========================================
  6577. * イベントタイマー削除
  6578. *------------------------------------------*/
  6579. BUILDIN_FUNC(deltimer)
  6580. {
  6581. const char *event;
  6582. TBL_PC* sd;
  6583. event=script_getstr(st, 2);
  6584. sd = script_rid2sd(st);
  6585. if( sd == NULL )
  6586. return 0;
  6587. check_event(st, event);
  6588. pc_deleventtimer(sd,event);
  6589. return 0;
  6590. }
  6591. /*==========================================
  6592. * イベントタイマーのカウント値追加
  6593. *------------------------------------------*/
  6594. BUILDIN_FUNC(addtimercount)
  6595. {
  6596. const char *event;
  6597. int tick;
  6598. TBL_PC* sd;
  6599. event=script_getstr(st, 2);
  6600. tick=script_getnum(st,3);
  6601. sd = script_rid2sd(st);
  6602. if( sd == NULL )
  6603. return 0;
  6604. check_event(st, event);
  6605. pc_addeventtimercount(sd,event,tick);
  6606. return 0;
  6607. }
  6608. /*==========================================
  6609. * NPCタイマー初期化
  6610. *------------------------------------------*/
  6611. BUILDIN_FUNC(initnpctimer)
  6612. {
  6613. struct npc_data *nd;
  6614. int flag = 0;
  6615. if( script_hasdata(st,3) )
  6616. { //Two arguments: NPC name and attach flag.
  6617. nd = npc_name2id(script_getstr(st, 2));
  6618. flag = script_getnum(st,3);
  6619. }
  6620. else if( script_hasdata(st,2) )
  6621. { //Check if argument is numeric (flag) or string (npc name)
  6622. struct script_data *data;
  6623. data = script_getdata(st,2);
  6624. get_val(st,data);
  6625. if( data_isstring(data) ) //NPC name
  6626. nd = npc_name2id(conv_str(st, data));
  6627. else if( data_isint(data) ) //Flag
  6628. {
  6629. nd = (struct npc_data *)map_id2bl(st->oid);
  6630. flag = conv_num(st,data);
  6631. }
  6632. else
  6633. {
  6634. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6635. return 1;
  6636. }
  6637. }
  6638. else
  6639. nd=(struct npc_data *)map_id2bl(st->oid);
  6640. if (!nd)
  6641. return 0;
  6642. if (flag) //Attach
  6643. {
  6644. TBL_PC* sd = script_rid2sd(st);
  6645. if( sd == NULL )
  6646. return 0;
  6647. nd->u.scr.rid = sd->bl.id;
  6648. }
  6649. npc_settimerevent_tick(nd,0);
  6650. npc_timerevent_start(nd, st->rid);
  6651. return 0;
  6652. }
  6653. /*==========================================
  6654. * NPCタイマー開始
  6655. *------------------------------------------*/
  6656. BUILDIN_FUNC(startnpctimer)
  6657. {
  6658. struct npc_data *nd;
  6659. int flag = 0;
  6660. if( script_hasdata(st,3) )
  6661. { //Two arguments: NPC name and attach flag.
  6662. nd = npc_name2id(script_getstr(st, 2));
  6663. flag = script_getnum(st,3);
  6664. }
  6665. else if( script_hasdata(st,2) )
  6666. { //Check if argument is numeric (flag) or string (npc name)
  6667. struct script_data *data;
  6668. data = script_getdata(st,2);
  6669. get_val(st,data);
  6670. if( data_isstring(data) ) //NPC name
  6671. nd = npc_name2id(conv_str(st, data));
  6672. else if( data_isint(data) ) //Flag
  6673. {
  6674. nd = (struct npc_data *)map_id2bl(st->oid);
  6675. flag = conv_num(st,data);
  6676. }
  6677. else
  6678. {
  6679. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6680. return 1;
  6681. }
  6682. }
  6683. else
  6684. nd=(struct npc_data *)map_id2bl(st->oid);
  6685. if (!nd)
  6686. return 0;
  6687. if (flag) //Attach
  6688. {
  6689. TBL_PC* sd = script_rid2sd(st);
  6690. if( sd == NULL )
  6691. return 0;
  6692. nd->u.scr.rid = sd->bl.id;
  6693. }
  6694. npc_timerevent_start(nd, st->rid);
  6695. return 0;
  6696. }
  6697. /*==========================================
  6698. * NPCタイマー停止
  6699. *------------------------------------------*/
  6700. BUILDIN_FUNC(stopnpctimer)
  6701. {
  6702. struct npc_data *nd;
  6703. int flag = 0;
  6704. if( script_hasdata(st,3) )
  6705. { //Two arguments: NPC name and attach flag.
  6706. nd = npc_name2id(script_getstr(st, 2));
  6707. flag = script_getnum(st,3);
  6708. }
  6709. else if( script_hasdata(st,2) )
  6710. { //Check if argument is numeric (flag) or string (npc name)
  6711. struct script_data *data;
  6712. data = script_getdata(st,2);
  6713. get_val(st,data);
  6714. if( data_isstring(data) ) //NPC name
  6715. nd = npc_name2id(conv_str(st, data));
  6716. else if( data_isint(data) ) //Flag
  6717. {
  6718. nd = (struct npc_data *)map_id2bl(st->oid);
  6719. flag = conv_num(st,data);
  6720. }
  6721. else
  6722. {
  6723. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6724. return 1;
  6725. }
  6726. }
  6727. else
  6728. nd=(struct npc_data *)map_id2bl(st->oid);
  6729. if (!nd)
  6730. return 0;
  6731. if (flag) //Detach
  6732. nd->u.scr.rid = 0;
  6733. npc_timerevent_stop(nd);
  6734. return 0;
  6735. }
  6736. /*==========================================
  6737. * NPCタイマー情報所得
  6738. *------------------------------------------*/
  6739. BUILDIN_FUNC(getnpctimer)
  6740. {
  6741. struct npc_data *nd;
  6742. TBL_PC *sd;
  6743. int type=script_getnum(st,2);
  6744. int val=0;
  6745. if( script_hasdata(st,3) )
  6746. nd = npc_name2id(script_getstr(st,3));
  6747. else
  6748. nd = (struct npc_data *)map_id2bl(st->oid);
  6749. if (!nd || nd->bl.type != BL_NPC)
  6750. {
  6751. script_pushint(st,0);
  6752. ShowError("getnpctimer: Invalid NPC.\n");
  6753. return 1;
  6754. }
  6755. switch(type){
  6756. case 0: val=npc_gettimerevent_tick(nd); break;
  6757. case 1:
  6758. if (nd->u.scr.rid) {
  6759. sd = map_id2sd(nd->u.scr.rid);
  6760. if (!sd) {
  6761. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6762. break;
  6763. }
  6764. val = (sd->npc_timer_id != -1);
  6765. } else
  6766. val= (nd->u.scr.timerid !=-1);
  6767. break;
  6768. case 2: val= nd->u.scr.timeramount; break;
  6769. }
  6770. script_pushint(st,val);
  6771. return 0;
  6772. }
  6773. /*==========================================
  6774. * NPCタイマー値設定
  6775. *------------------------------------------*/
  6776. BUILDIN_FUNC(setnpctimer)
  6777. {
  6778. int tick;
  6779. struct npc_data *nd;
  6780. tick=script_getnum(st,2);
  6781. if( script_hasdata(st,3) )
  6782. nd=npc_name2id(script_getstr(st,3));
  6783. else
  6784. nd=(struct npc_data *)map_id2bl(st->oid);
  6785. if (!nd || nd->bl.type != BL_NPC)
  6786. {
  6787. script_pushint(st,1);
  6788. ShowError("setnpctimer: Invalid NPC.\n");
  6789. return 1;
  6790. }
  6791. npc_settimerevent_tick(nd,tick);
  6792. script_pushint(st,0);
  6793. return 0;
  6794. }
  6795. /*==========================================
  6796. * attaches the player rid to the timer [Celest]
  6797. *------------------------------------------*/
  6798. BUILDIN_FUNC(attachnpctimer)
  6799. {
  6800. TBL_PC *sd;
  6801. struct npc_data *nd;
  6802. nd=(struct npc_data *)map_id2bl(st->oid);
  6803. if (!nd || nd->bl.type != BL_NPC)
  6804. {
  6805. script_pushint(st,1);
  6806. ShowError("setnpctimer: Invalid NPC.\n");
  6807. return 1;
  6808. }
  6809. if( script_hasdata(st,2) )
  6810. sd=map_nick2sd(script_getstr(st,2));
  6811. else
  6812. sd = script_rid2sd(st);
  6813. if (!sd)
  6814. {
  6815. script_pushint(st,1);
  6816. ShowWarning("attachnpctimer: Invalid player.\n");
  6817. return 1;
  6818. }
  6819. nd->u.scr.rid = sd->bl.id;
  6820. script_pushint(st,0);
  6821. return 0;
  6822. }
  6823. /*==========================================
  6824. * detaches a player rid from the timer [Celest]
  6825. *------------------------------------------*/
  6826. BUILDIN_FUNC(detachnpctimer)
  6827. {
  6828. struct npc_data *nd;
  6829. if( script_hasdata(st,2) )
  6830. nd=npc_name2id(script_getstr(st,2));
  6831. else
  6832. nd=(struct npc_data *)map_id2bl(st->oid);
  6833. if (!nd || nd->bl.type != BL_NPC)
  6834. {
  6835. script_pushint(st,1);
  6836. ShowError("detachnpctimer: Invalid NPC.\n");
  6837. return 1;
  6838. }
  6839. nd->u.scr.rid = 0;
  6840. script_pushint(st,0);
  6841. return 0;
  6842. }
  6843. /*==========================================
  6844. * To avoid "player not attached" script errors, this function is provided,
  6845. * it checks if there is a player attached to the current script. [Skotlex]
  6846. * If no, returns 0, if yes, returns the account_id of the attached player.
  6847. *------------------------------------------*/
  6848. BUILDIN_FUNC(playerattached)
  6849. {
  6850. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  6851. script_pushint(st,0);
  6852. else
  6853. script_pushint(st,st->rid);
  6854. return 0;
  6855. }
  6856. /*==========================================
  6857. * 天の声アナウンス
  6858. *------------------------------------------*/
  6859. BUILDIN_FUNC(announce)
  6860. {
  6861. const char *str, *color=NULL;
  6862. int flag;
  6863. str=script_getstr(st,2);
  6864. flag=script_getnum(st,3);
  6865. if (script_hasdata(st,4))
  6866. color=script_getstr(st,4);
  6867. if(flag&0x0f){
  6868. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  6869. (struct block_list *)script_rid2sd(st);
  6870. if( bl == NULL )
  6871. return 0;
  6872. if (color)
  6873. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  6874. else
  6875. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  6876. }else {
  6877. if (color)
  6878. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  6879. else
  6880. intif_GMmessage(str,(int)strlen(str)+1,flag);
  6881. }
  6882. return 0;
  6883. }
  6884. /*==========================================
  6885. * 天の声アナウンス(特定マップ)
  6886. *------------------------------------------*/
  6887. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  6888. {
  6889. char *str, *color;
  6890. int len,flag;
  6891. str=va_arg(ap,char *);
  6892. len=va_arg(ap,int);
  6893. flag=va_arg(ap,int);
  6894. color=va_arg(ap,char *);
  6895. if (color)
  6896. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  6897. else
  6898. clif_GMmessage(bl,str,len,flag|3);
  6899. return 0;
  6900. }
  6901. BUILDIN_FUNC(mapannounce)
  6902. {
  6903. const char *mapname,*str, *color=NULL;
  6904. int flag,m;
  6905. mapname=script_getstr(st,2);
  6906. str=script_getstr(st,3);
  6907. flag=script_getnum(st,4);
  6908. if (script_hasdata(st,5))
  6909. color=script_getstr(st,5);
  6910. if( (m=map_mapname2mapid(mapname))<0 )
  6911. return 0;
  6912. map_foreachinmap(buildin_mapannounce_sub,
  6913. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  6914. return 0;
  6915. }
  6916. /*==========================================
  6917. * 天の声アナウンス(特定エリア)
  6918. *------------------------------------------*/
  6919. BUILDIN_FUNC(areaannounce)
  6920. {
  6921. const char *map,*str,*color=NULL;
  6922. int flag,m;
  6923. int x0,y0,x1,y1;
  6924. map=script_getstr(st,2);
  6925. x0=script_getnum(st,3);
  6926. y0=script_getnum(st,4);
  6927. x1=script_getnum(st,5);
  6928. y1=script_getnum(st,6);
  6929. str=script_getstr(st,7);
  6930. flag=script_getnum(st,8);
  6931. if (script_hasdata(st,9))
  6932. color=script_getstr(st,9);
  6933. if( (m=map_mapname2mapid(map))<0 )
  6934. return 0;
  6935. map_foreachinarea(buildin_mapannounce_sub,
  6936. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  6937. return 0;
  6938. }
  6939. /*==========================================
  6940. * ユーザー数所得
  6941. *------------------------------------------*/
  6942. BUILDIN_FUNC(getusers)
  6943. {
  6944. int flag=script_getnum(st,2);
  6945. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  6946. int val=0;
  6947. switch(flag&0x07){
  6948. case 0: val=map[bl->m].users; break;
  6949. case 1: val=map_getusers(); break;
  6950. }
  6951. script_pushint(st,val);
  6952. return 0;
  6953. }
  6954. /*==========================================
  6955. * Works like @WHO - displays all online users names in window
  6956. *------------------------------------------*/
  6957. BUILDIN_FUNC(getusersname)
  6958. {
  6959. TBL_PC *sd, *pl_sd;
  6960. int disp_num=1;
  6961. struct s_mapiterator* iter;
  6962. sd = script_rid2sd(st);
  6963. if (!sd) return 0;
  6964. iter = mapit_getallusers();
  6965. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  6966. {
  6967. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  6968. continue; // skip hidden GMs
  6969. if((disp_num++)%10==0)
  6970. clif_scriptnext(sd,st->oid);
  6971. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  6972. }
  6973. mapit_free(iter);
  6974. return 0;
  6975. }
  6976. /*==========================================
  6977. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  6978. *------------------------------------------*/
  6979. BUILDIN_FUNC(getmapguildusers)
  6980. {
  6981. const char *str;
  6982. int m, gid;
  6983. int i=0,c=0;
  6984. struct guild *g = NULL;
  6985. str=script_getstr(st,2);
  6986. gid=script_getnum(st,3);
  6987. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  6988. script_pushint(st,-1);
  6989. return 0;
  6990. }
  6991. g = guild_search(gid);
  6992. if (g){
  6993. for(i = 0; i < g->max_member; i++)
  6994. {
  6995. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  6996. c++;
  6997. }
  6998. }
  6999. script_pushint(st,c);
  7000. return 0;
  7001. }
  7002. /*==========================================
  7003. * マップ指定ユーザー数所得
  7004. *------------------------------------------*/
  7005. BUILDIN_FUNC(getmapusers)
  7006. {
  7007. const char *str;
  7008. int m;
  7009. str=script_getstr(st,2);
  7010. if( (m=map_mapname2mapid(str))< 0){
  7011. script_pushint(st,-1);
  7012. return 0;
  7013. }
  7014. script_pushint(st,map[m].users);
  7015. return 0;
  7016. }
  7017. /*==========================================
  7018. * エリア指定ユーザー数所得
  7019. *------------------------------------------*/
  7020. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7021. {
  7022. int *users=va_arg(ap,int *);
  7023. (*users)++;
  7024. return 0;
  7025. }
  7026. BUILDIN_FUNC(getareausers)
  7027. {
  7028. const char *str;
  7029. int m,x0,y0,x1,y1,users=0;
  7030. str=script_getstr(st,2);
  7031. x0=script_getnum(st,3);
  7032. y0=script_getnum(st,4);
  7033. x1=script_getnum(st,5);
  7034. y1=script_getnum(st,6);
  7035. if( (m=map_mapname2mapid(str))< 0){
  7036. script_pushint(st,-1);
  7037. return 0;
  7038. }
  7039. map_foreachinarea(buildin_getareausers_sub,
  7040. m,x0,y0,x1,y1,BL_PC,&users);
  7041. script_pushint(st,users);
  7042. return 0;
  7043. }
  7044. /*==========================================
  7045. * エリア指定ドロップアイテム数所得
  7046. *------------------------------------------*/
  7047. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7048. {
  7049. int item=va_arg(ap,int);
  7050. int *amount=va_arg(ap,int *);
  7051. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7052. if(drop->item_data.nameid==item)
  7053. (*amount)+=drop->item_data.amount;
  7054. return 0;
  7055. }
  7056. BUILDIN_FUNC(getareadropitem)
  7057. {
  7058. const char *str;
  7059. int m,x0,y0,x1,y1,item,amount=0;
  7060. struct script_data *data;
  7061. str=script_getstr(st,2);
  7062. x0=script_getnum(st,3);
  7063. y0=script_getnum(st,4);
  7064. x1=script_getnum(st,5);
  7065. y1=script_getnum(st,6);
  7066. data=script_getdata(st,7);
  7067. get_val(st,data);
  7068. if( data_isstring(data) ){
  7069. const char *name=conv_str(st,data);
  7070. struct item_data *item_data = itemdb_searchname(name);
  7071. item=UNKNOWN_ITEM_ID;
  7072. if( item_data )
  7073. item=item_data->nameid;
  7074. }else
  7075. item=conv_num(st,data);
  7076. if( (m=map_mapname2mapid(str))< 0){
  7077. script_pushint(st,-1);
  7078. return 0;
  7079. }
  7080. map_foreachinarea(buildin_getareadropitem_sub,
  7081. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7082. script_pushint(st,amount);
  7083. return 0;
  7084. }
  7085. /*==========================================
  7086. * NPCの有効化
  7087. *------------------------------------------*/
  7088. BUILDIN_FUNC(enablenpc)
  7089. {
  7090. const char *str;
  7091. str=script_getstr(st,2);
  7092. npc_enable(str,1);
  7093. return 0;
  7094. }
  7095. /*==========================================
  7096. * NPCの無効化
  7097. *------------------------------------------*/
  7098. BUILDIN_FUNC(disablenpc)
  7099. {
  7100. const char *str;
  7101. str=script_getstr(st,2);
  7102. npc_enable(str,0);
  7103. return 0;
  7104. }
  7105. /*==========================================
  7106. * 隠れているNPCの表示
  7107. *------------------------------------------*/
  7108. BUILDIN_FUNC(hideoffnpc)
  7109. {
  7110. const char *str;
  7111. str=script_getstr(st,2);
  7112. npc_enable(str,2);
  7113. return 0;
  7114. }
  7115. /*==========================================
  7116. * NPCをハイディング
  7117. *------------------------------------------*/
  7118. BUILDIN_FUNC(hideonnpc)
  7119. {
  7120. const char *str;
  7121. str=script_getstr(st,2);
  7122. npc_enable(str,4);
  7123. return 0;
  7124. }
  7125. /// Starts a status effect on the target unit or on the attached player.
  7126. ///
  7127. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7128. BUILDIN_FUNC(sc_start)
  7129. {
  7130. struct block_list* bl;
  7131. enum sc_type type;
  7132. int tick;
  7133. int val1;
  7134. int val4 = 0;
  7135. type = (sc_type)script_getnum(st,2);
  7136. tick = script_getnum(st,3);
  7137. val1 = script_getnum(st,4);
  7138. if( script_hasdata(st,5) )
  7139. bl = map_id2bl(script_getnum(st,5));
  7140. else
  7141. bl = map_id2bl(st->rid);
  7142. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7143. {// When there isn't a duration specified, try to get it from the skill_db
  7144. tick = skill_get_time(status_sc2skill(type), val1);
  7145. }
  7146. if( potion_flag == 1 && potion_target )
  7147. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7148. bl = map_id2bl(potion_target);
  7149. tick /= 2;// Thrown potions only last half.
  7150. val4 = 1;// Mark that this was a thrown sc_effect
  7151. }
  7152. if( bl )
  7153. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7154. return 0;
  7155. }
  7156. /// Starts a status effect on the target unit or on the attached player.
  7157. ///
  7158. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7159. BUILDIN_FUNC(sc_start2)
  7160. {
  7161. struct block_list* bl;
  7162. enum sc_type type;
  7163. int tick;
  7164. int val1;
  7165. int val4 = 0;
  7166. int rate;
  7167. type = (sc_type)script_getnum(st,2);
  7168. tick = script_getnum(st,3);
  7169. val1 = script_getnum(st,4);
  7170. rate = script_getnum(st,5);
  7171. if( script_hasdata(st,6) )
  7172. bl = map_id2bl(script_getnum(st,6));
  7173. else
  7174. bl = map_id2bl(st->rid);
  7175. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7176. {// When there isn't a duration specified, try to get it from the skill_db
  7177. tick = skill_get_time(status_sc2skill(type), val1);
  7178. }
  7179. if( potion_flag == 1 && potion_target )
  7180. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7181. bl = map_id2bl(potion_target);
  7182. tick /= 2;// Thrown potions only last half.
  7183. val4 = 1;// Mark that this was a thrown sc_effect
  7184. }
  7185. if( bl )
  7186. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7187. return 0;
  7188. }
  7189. /// Starts a status effect on the target unit or on the attached player.
  7190. ///
  7191. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7192. BUILDIN_FUNC(sc_start4)
  7193. {
  7194. struct block_list* bl;
  7195. enum sc_type type;
  7196. int tick;
  7197. int val1;
  7198. int val2;
  7199. int val3;
  7200. int val4;
  7201. type = (sc_type)script_getnum(st,2);
  7202. tick = script_getnum(st,3);
  7203. val1 = script_getnum(st,4);
  7204. val2 = script_getnum(st,5);
  7205. val3 = script_getnum(st,6);
  7206. val4 = script_getnum(st,7);
  7207. if( script_hasdata(st,8) )
  7208. bl = map_id2bl(script_getnum(st,8));
  7209. else
  7210. bl = map_id2bl(st->rid);
  7211. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7212. {// When there isn't a duration specified, try to get it from the skill_db
  7213. tick = skill_get_time(status_sc2skill(type), val1);
  7214. }
  7215. if( potion_flag == 1 && potion_target )
  7216. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7217. bl = map_id2bl(potion_target);
  7218. tick /= 2;// Thrown potions only last half.
  7219. }
  7220. if( bl )
  7221. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7222. return 0;
  7223. }
  7224. /// Ends one or all status effects on the target unit or on the attached player.
  7225. ///
  7226. /// sc_end <effect_id>{,<unit_id>};
  7227. BUILDIN_FUNC(sc_end)
  7228. {
  7229. struct block_list* bl;
  7230. int type;
  7231. type = script_getnum(st,2);
  7232. if( script_hasdata(st,3) )
  7233. bl = map_id2bl(script_getnum(st,3));
  7234. else
  7235. bl = map_id2bl(st->rid);
  7236. if( potion_flag==1 && potion_target )
  7237. {//##TODO how does this work [FlavioJS]
  7238. bl = map_id2bl(potion_target);
  7239. }
  7240. if( !bl ) return 0;
  7241. if( type >= 0 && type < SC_MAX )
  7242. {
  7243. struct status_change *sc = status_get_sc(bl);
  7244. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7245. if (!sce) return 0;
  7246. //This should help status_change_end force disabling the SC in case it has no limit.
  7247. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7248. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7249. } else
  7250. status_change_clear(bl, 2);// remove all effects
  7251. return 0;
  7252. }
  7253. /*==========================================
  7254. * 状態異常耐性を計算した確率を返す
  7255. *------------------------------------------*/
  7256. BUILDIN_FUNC(getscrate)
  7257. {
  7258. struct block_list *bl;
  7259. int type,rate;
  7260. type=script_getnum(st,2);
  7261. rate=script_getnum(st,3);
  7262. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7263. bl = map_id2bl(script_getnum(st,4));
  7264. else
  7265. bl = map_id2bl(st->rid);
  7266. if (bl)
  7267. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7268. script_pushint(st,rate);
  7269. return 0;
  7270. }
  7271. /*==========================================
  7272. *
  7273. *------------------------------------------*/
  7274. BUILDIN_FUNC(debugmes)
  7275. {
  7276. const char *str;
  7277. str=script_getstr(st,2);
  7278. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7279. return 0;
  7280. }
  7281. /*==========================================
  7282. *捕獲アイテム使用
  7283. *------------------------------------------*/
  7284. BUILDIN_FUNC(catchpet)
  7285. {
  7286. int pet_id;
  7287. TBL_PC *sd;
  7288. pet_id= script_getnum(st,2);
  7289. sd=script_rid2sd(st);
  7290. if( sd == NULL )
  7291. return 0;
  7292. pet_catch_process1(sd,pet_id);
  7293. return 0;
  7294. }
  7295. /*==========================================
  7296. * [orn]
  7297. *------------------------------------------*/
  7298. BUILDIN_FUNC(homunculus_evolution)
  7299. {
  7300. TBL_PC *sd;
  7301. sd=script_rid2sd(st);
  7302. if( sd == NULL )
  7303. return 0;
  7304. if(merc_is_hom_active(sd->hd))
  7305. {
  7306. if (sd->hd->homunculus.intimacy > 91000)
  7307. merc_hom_evolution(sd->hd);
  7308. else
  7309. clif_emotion(&sd->hd->bl, 4) ; //swt
  7310. }
  7311. return 0;
  7312. }
  7313. // [Zephyrus]
  7314. BUILDIN_FUNC(homunculus_shuffle)
  7315. {
  7316. TBL_PC *sd;
  7317. sd=script_rid2sd(st);
  7318. if( sd == NULL )
  7319. return 0;
  7320. if(merc_is_hom_active(sd->hd))
  7321. merc_hom_shuffle(sd->hd);
  7322. return 0;
  7323. }
  7324. //These two functions bring the eA MAPID_* class functionality to scripts.
  7325. BUILDIN_FUNC(eaclass)
  7326. {
  7327. int class_;
  7328. if( script_hasdata(st,2) )
  7329. class_ = script_getnum(st,2);
  7330. else {
  7331. TBL_PC *sd;
  7332. sd=script_rid2sd(st);
  7333. if (!sd) {
  7334. script_pushint(st,-1);
  7335. return 0;
  7336. }
  7337. class_ = sd->status.class_;
  7338. }
  7339. script_pushint(st,pc_jobid2mapid(class_));
  7340. return 0;
  7341. }
  7342. BUILDIN_FUNC(roclass)
  7343. {
  7344. int class_ =script_getnum(st,2);
  7345. int sex;
  7346. if( script_hasdata(st,3) )
  7347. sex = script_getnum(st,3);
  7348. else {
  7349. TBL_PC *sd;
  7350. if (st->rid && (sd=script_rid2sd(st)))
  7351. sex = sd->status.sex;
  7352. else
  7353. sex = 1; //Just use male when not found.
  7354. }
  7355. script_pushint(st,pc_mapid2jobid(class_, sex));
  7356. return 0;
  7357. }
  7358. /*==========================================
  7359. *携帯卵孵化機使用
  7360. *------------------------------------------*/
  7361. BUILDIN_FUNC(birthpet)
  7362. {
  7363. TBL_PC *sd;
  7364. sd=script_rid2sd(st);
  7365. if( sd == NULL )
  7366. return 0;
  7367. clif_sendegg(sd);
  7368. return 0;
  7369. }
  7370. /*==========================================
  7371. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7372. *------------------------------------------*/
  7373. BUILDIN_FUNC(resetlvl)
  7374. {
  7375. TBL_PC *sd;
  7376. int type=script_getnum(st,2);
  7377. sd=script_rid2sd(st);
  7378. if( sd == NULL )
  7379. return 0;
  7380. pc_resetlvl(sd,type);
  7381. return 0;
  7382. }
  7383. /*==========================================
  7384. * ステータスリセット
  7385. *------------------------------------------*/
  7386. BUILDIN_FUNC(resetstatus)
  7387. {
  7388. TBL_PC *sd;
  7389. sd=script_rid2sd(st);
  7390. pc_resetstate(sd);
  7391. return 0;
  7392. }
  7393. /*==========================================
  7394. * script command resetskill
  7395. *------------------------------------------*/
  7396. BUILDIN_FUNC(resetskill)
  7397. {
  7398. TBL_PC *sd;
  7399. sd=script_rid2sd(st);
  7400. pc_resetskill(sd,1);
  7401. return 0;
  7402. }
  7403. /*==========================================
  7404. * Counts total amount of skill points.
  7405. *------------------------------------------*/
  7406. BUILDIN_FUNC(skillpointcount)
  7407. {
  7408. TBL_PC *sd;
  7409. sd=script_rid2sd(st);
  7410. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7411. return 0;
  7412. }
  7413. /*==========================================
  7414. *
  7415. *------------------------------------------*/
  7416. BUILDIN_FUNC(changebase)
  7417. {
  7418. TBL_PC *sd=NULL;
  7419. int vclass;
  7420. if( script_hasdata(st,3) )
  7421. sd=map_id2sd(script_getnum(st,3));
  7422. else
  7423. sd=script_rid2sd(st);
  7424. if(sd == NULL)
  7425. return 0;
  7426. vclass = script_getnum(st,2);
  7427. if(vclass == JOB_WEDDING)
  7428. {
  7429. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7430. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7431. )
  7432. return 0;
  7433. }
  7434. if(!sd->disguise && vclass != sd->vd.class_) {
  7435. status_set_viewdata(&sd->bl, vclass);
  7436. //Updated client view. Base, Weapon and Cloth Colors.
  7437. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7438. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7439. if (sd->vd.cloth_color)
  7440. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7441. }
  7442. return 0;
  7443. }
  7444. /*==========================================
  7445. * 性別変換
  7446. *------------------------------------------*/
  7447. BUILDIN_FUNC(changesex)
  7448. {
  7449. TBL_PC *sd = NULL;
  7450. sd = script_rid2sd(st);
  7451. chrif_changesex(sd);
  7452. return 0;
  7453. }
  7454. /*==========================================
  7455. * Works like 'announce' but outputs in the common chat window
  7456. *------------------------------------------*/
  7457. BUILDIN_FUNC(globalmes)
  7458. {
  7459. struct block_list *bl = map_id2bl(st->oid);
  7460. struct npc_data *nd = (struct npc_data *)bl;
  7461. const char *name=NULL,*mes;
  7462. mes=script_getstr(st,2); // メッセージの取得
  7463. if(mes==NULL) return 0;
  7464. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7465. name=script_getstr(st,3);
  7466. } else {
  7467. name=nd->name;
  7468. }
  7469. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7470. return 0;
  7471. }
  7472. /////////////////////////////////////////////////////////////////////
  7473. // NPC waiting room (chat room)
  7474. //
  7475. /// Creates a waiting room (chat room) for this npc.
  7476. ///
  7477. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7478. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7479. /// waitingroom "<title>",<limit>,"<event>";
  7480. /// waitingroom "<title>",<limit>;
  7481. BUILDIN_FUNC(waitingroom)
  7482. {
  7483. struct npc_data* nd;
  7484. const char* title;
  7485. const char* ev = "";
  7486. int limit;
  7487. int trigger = 0;
  7488. int pub = 1;
  7489. title = script_getstr(st, 2);
  7490. limit = script_getnum(st, 3);
  7491. if( script_hasdata(st,5) )
  7492. {
  7493. struct script_data* last = script_getdata(st, 5);
  7494. get_val(st, last);
  7495. if( data_isstring(last) )
  7496. {// ,<trigger>,"<event>"
  7497. trigger = script_getnum(st, 4);
  7498. ev = script_getstr(st, 5);
  7499. }
  7500. else
  7501. {// ,"<event>",<trigger>
  7502. ev = script_getstr(st, 4);
  7503. trigger = script_getnum(st,5);
  7504. }
  7505. }
  7506. else if( script_hasdata(st,4) )
  7507. {// ,"<event>"
  7508. ev = script_getstr(st, 4);
  7509. trigger = limit;
  7510. }
  7511. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  7512. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7513. return 0;
  7514. }
  7515. /// Removes the waiting room of the current or target npc.
  7516. ///
  7517. /// delwaitingroom "<npc_name>";
  7518. /// delwaitingroom;
  7519. BUILDIN_FUNC(delwaitingroom)
  7520. {
  7521. struct npc_data* nd;
  7522. if( script_hasdata(st,2) )
  7523. nd = npc_name2id(script_getstr(st, 2));
  7524. else
  7525. nd = (struct npc_data *)map_id2bl(st->oid);
  7526. if( nd != NULL )
  7527. chat_deletenpcchat(nd);
  7528. return 0;
  7529. }
  7530. /// Kicks all the players from the waiting room of the current or target npc.
  7531. ///
  7532. /// kickwaitingroomall "<npc_name>";
  7533. /// kickwaitingroomall;
  7534. BUILDIN_FUNC(waitingroomkickall)
  7535. {
  7536. struct npc_data* nd;
  7537. struct chat_data* cd;
  7538. if( script_hasdata(st,2) )
  7539. nd = npc_name2id(script_getstr(st,2));
  7540. else
  7541. nd = (struct npc_data *)map_id2bl(st->oid);
  7542. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7543. chat_npckickall(cd);
  7544. return 0;
  7545. }
  7546. /// Enables the waiting room event of the current or target npc.
  7547. ///
  7548. /// enablewaitingroomevent "<npc_name>";
  7549. /// enablewaitingroomevent;
  7550. BUILDIN_FUNC(enablewaitingroomevent)
  7551. {
  7552. struct npc_data* nd;
  7553. struct chat_data* cd;
  7554. if( script_hasdata(st,2) )
  7555. nd = npc_name2id(script_getstr(st, 2));
  7556. else
  7557. nd = (struct npc_data *)map_id2bl(st->oid);
  7558. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7559. chat_enableevent(cd);
  7560. return 0;
  7561. }
  7562. /// Disables the waiting room event of the current or target npc.
  7563. ///
  7564. /// disablewaitingroomevent "<npc_name>";
  7565. /// disablewaitingroomevent;
  7566. BUILDIN_FUNC(disablewaitingroomevent)
  7567. {
  7568. struct npc_data *nd;
  7569. struct chat_data *cd;
  7570. if( script_hasdata(st,2) )
  7571. nd = npc_name2id(script_getstr(st, 2));
  7572. else
  7573. nd = (struct npc_data *)map_id2bl(st->oid);
  7574. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7575. chat_disableevent(cd);
  7576. return 0;
  7577. }
  7578. /// Returns info on the waiting room of the current or target npc.
  7579. /// Returns -1 if the type unknown
  7580. /// <type>=0 : current number of users
  7581. /// <type>=1 : maximum number of users allowed
  7582. /// <type>=2 : the number of users that trigger the event
  7583. /// <type>=3 : if the trigger is disabled
  7584. /// <type>=4 : the title of the waiting room
  7585. /// <type>=5 : the password of the waiting room
  7586. /// <type>=16 : the name of the waiting room event
  7587. /// <type>=32 : if the waiting room is full
  7588. /// <type>=33 : if there are enough users to trigger the event
  7589. ///
  7590. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7591. /// getwaitingroomstate(<type>) -> <info>
  7592. BUILDIN_FUNC(getwaitingroomstate)
  7593. {
  7594. struct npc_data *nd;
  7595. struct chat_data *cd;
  7596. int type;
  7597. type = script_getnum(st,2);
  7598. if( script_hasdata(st,3) )
  7599. nd = npc_name2id(script_getstr(st, 3));
  7600. else
  7601. nd = (struct npc_data *)map_id2bl(st->oid);
  7602. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7603. {
  7604. script_pushint(st, -1);
  7605. return 0;
  7606. }
  7607. switch(type)
  7608. {
  7609. case 0: script_pushint(st, cd->users); break;
  7610. case 1: script_pushint(st, cd->limit); break;
  7611. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7612. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7613. case 4: script_pushstrcopy(st, cd->title); break;
  7614. case 5: script_pushstrcopy(st, cd->pass); break;
  7615. case 16: script_pushstrcopy(st, cd->npc_event);break;
  7616. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7617. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7618. default: script_pushint(st, -1); break;
  7619. }
  7620. return 0;
  7621. }
  7622. /// Warps the trigger or target amount of players to the target map and position.
  7623. /// Players are automatically removed from the waiting room.
  7624. /// Those waiting the longest will get warped first.
  7625. /// The target map can be "Random" for a random position in the current map,
  7626. /// and "SavePoint" for the savepoint map+position.
  7627. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7628. ///
  7629. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7630. /// The total number of teleported players is put into $@warpwaitingpcnum
  7631. ///
  7632. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7633. /// warpwaitingpc "<map name>",<x>,<y>;
  7634. BUILDIN_FUNC(warpwaitingpc)
  7635. {
  7636. int x;
  7637. int y;
  7638. int i;
  7639. int n;
  7640. const char* map_name;
  7641. struct npc_data* nd;
  7642. struct chat_data* cd;
  7643. TBL_PC* sd;
  7644. nd = (struct npc_data *)map_id2bl(st->oid);
  7645. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7646. return 0;
  7647. map_name = script_getstr(st,2);
  7648. x = script_getnum(st,3);
  7649. y = script_getnum(st,4);
  7650. n = cd->trigger&0x7f;
  7651. if( script_hasdata(st,5) )
  7652. n = script_getnum(st,5);
  7653. for( i = 0; i < n && cd->users > 0; i++ )
  7654. {
  7655. sd = cd->usersd[0];
  7656. if( sd == NULL )
  7657. {
  7658. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7659. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  7660. continue;// Broken npc chat room?
  7661. }
  7662. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  7663. if( strcmp(map_name,"Random") == 0 )
  7664. pc_randomwarp(sd,3);
  7665. else if( strcmp(map_name,"SavePoint") == 0 )
  7666. {
  7667. if( map[sd->bl.m].flag.noteleport )
  7668. return 0;// can't teleport on this map
  7669. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  7670. }
  7671. else
  7672. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  7673. }
  7674. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  7675. return 0;
  7676. }
  7677. /////////////////////////////////////////////////////////////////////
  7678. // ...
  7679. //
  7680. /*==========================================
  7681. * RIDのアタッチ
  7682. *------------------------------------------*/
  7683. BUILDIN_FUNC(attachrid)
  7684. {
  7685. st->rid=script_getnum(st,2);
  7686. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7687. return 0;
  7688. }
  7689. /*==========================================
  7690. * RIDのデタッチ
  7691. *------------------------------------------*/
  7692. BUILDIN_FUNC(detachrid)
  7693. {
  7694. st->rid=0;
  7695. return 0;
  7696. }
  7697. /*==========================================
  7698. * 存在チェック
  7699. *------------------------------------------*/
  7700. BUILDIN_FUNC(isloggedin)
  7701. {
  7702. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  7703. if (script_hasdata(st,3) && sd &&
  7704. sd->status.char_id != script_getnum(st,3))
  7705. sd = NULL;
  7706. push_val(st->stack,C_INT,sd!=NULL);
  7707. return 0;
  7708. }
  7709. /*==========================================
  7710. *
  7711. *------------------------------------------*/
  7712. BUILDIN_FUNC(setmapflagnosave)
  7713. {
  7714. int m,x,y;
  7715. unsigned short mapindex;
  7716. const char *str,*str2;
  7717. str=script_getstr(st,2);
  7718. str2=script_getstr(st,3);
  7719. x=script_getnum(st,4);
  7720. y=script_getnum(st,5);
  7721. m = map_mapname2mapid(str);
  7722. mapindex = mapindex_name2id(str2);
  7723. if(m >= 0 && mapindex) {
  7724. map[m].flag.nosave=1;
  7725. map[m].save.map=mapindex;
  7726. map[m].save.x=x;
  7727. map[m].save.y=y;
  7728. }
  7729. return 0;
  7730. }
  7731. BUILDIN_FUNC(getmapflag)
  7732. {
  7733. int m,i;
  7734. const char *str;
  7735. str=script_getstr(st,2);
  7736. i=script_getnum(st,3);
  7737. m = map_mapname2mapid(str);
  7738. if(m >= 0) {
  7739. switch(i) {
  7740. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  7741. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  7742. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  7743. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  7744. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  7745. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  7746. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  7747. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  7748. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  7749. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  7750. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  7751. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  7752. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  7753. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  7754. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  7755. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  7756. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  7757. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  7758. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  7759. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  7760. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  7761. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  7762. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  7763. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  7764. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  7765. case MF_INDOORS: script_pushint(st,map[m].flag.indoors); break;
  7766. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  7767. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  7768. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  7769. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  7770. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  7771. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  7772. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  7773. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  7774. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  7775. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  7776. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  7777. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  7778. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  7779. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  7780. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  7781. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  7782. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  7783. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  7784. }
  7785. }
  7786. return 0;
  7787. }
  7788. BUILDIN_FUNC(setmapflag)
  7789. {
  7790. int m,i;
  7791. const char *str;
  7792. const char *val=NULL;
  7793. str=script_getstr(st,2);
  7794. i=script_getnum(st,3);
  7795. if(script_hasdata(st,4)){
  7796. val=script_getstr(st,4);
  7797. }
  7798. m = map_mapname2mapid(str);
  7799. if(m >= 0) {
  7800. switch(i) {
  7801. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  7802. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  7803. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  7804. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  7805. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  7806. case MF_PVP: map[m].flag.pvp=1; break;
  7807. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  7808. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  7809. case MF_GVG: map[m].flag.gvg=1; break;
  7810. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  7811. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  7812. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  7813. case MF_NOTRADE: map[m].flag.notrade=1; break;
  7814. case MF_NODROP: map[m].flag.nodrop=1; break;
  7815. case MF_NOSKILL: map[m].flag.noskill=1; break;
  7816. case MF_NOWARP: map[m].flag.nowarp=1; break;
  7817. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  7818. case MF_SNOW: map[m].flag.snow=1; break;
  7819. case MF_CLOUDS: map[m].flag.clouds=1; break;
  7820. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  7821. case MF_FOG: map[m].flag.fog=1; break;
  7822. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  7823. case MF_SAKURA: map[m].flag.sakura=1; break;
  7824. case MF_LEAVES: map[m].flag.leaves=1; break;
  7825. case MF_RAIN: map[m].flag.rain=1; break;
  7826. case MF_INDOORS: map[m].flag.indoors=1; break;
  7827. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  7828. case MF_NOGO: map[m].flag.nogo=1; break;
  7829. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  7830. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  7831. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  7832. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  7833. case MF_NORETURN: map[m].flag.noreturn=1; break;
  7834. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  7835. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  7836. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  7837. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  7838. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7839. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7840. case MF_NOVENDING: map[m].flag.novending=1; break;
  7841. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  7842. case MF_NOCHAT: map[m].flag.nochat=1; break;
  7843. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  7844. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  7845. }
  7846. }
  7847. return 0;
  7848. }
  7849. BUILDIN_FUNC(removemapflag)
  7850. {
  7851. int m,i;
  7852. const char *str;
  7853. str=script_getstr(st,2);
  7854. i=script_getnum(st,3);
  7855. m = map_mapname2mapid(str);
  7856. if(m >= 0) {
  7857. switch(i) {
  7858. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  7859. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  7860. case MF_NOSAVE: map[m].flag.nosave=0; break;
  7861. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  7862. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  7863. case MF_PVP: map[m].flag.pvp=0; break;
  7864. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  7865. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  7866. case MF_GVG: map[m].flag.gvg=0; break;
  7867. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  7868. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  7869. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  7870. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  7871. case MF_NOTRADE: map[m].flag.notrade=0; break;
  7872. case MF_NODROP: map[m].flag.nodrop=0; break;
  7873. case MF_NOSKILL: map[m].flag.noskill=0; break;
  7874. case MF_NOWARP: map[m].flag.nowarp=0; break;
  7875. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  7876. case MF_SNOW: map[m].flag.snow=0; break;
  7877. case MF_CLOUDS: map[m].flag.clouds=0; break;
  7878. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  7879. case MF_FOG: map[m].flag.fog=0; break;
  7880. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  7881. case MF_SAKURA: map[m].flag.sakura=0; break;
  7882. case MF_LEAVES: map[m].flag.leaves=0; break;
  7883. case MF_RAIN: map[m].flag.rain=0; break;
  7884. case MF_INDOORS: map[m].flag.indoors=0; break;
  7885. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  7886. case MF_NOGO: map[m].flag.nogo=0; break;
  7887. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  7888. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  7889. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  7890. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  7891. case MF_NORETURN: map[m].flag.noreturn=0; break;
  7892. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  7893. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  7894. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  7895. case MF_NOCOMMAND: map[m].nocommand=0; break;
  7896. case MF_JEXP: map[m].jexp=100; break;
  7897. case MF_BEXP: map[m].bexp=100; break;
  7898. case MF_NOVENDING: map[m].flag.novending=0; break;
  7899. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  7900. case MF_NOCHAT: map[m].flag.nochat=0; break;
  7901. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  7902. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  7903. }
  7904. }
  7905. return 0;
  7906. }
  7907. BUILDIN_FUNC(pvpon)
  7908. {
  7909. int m;
  7910. const char *str;
  7911. TBL_PC* sd = NULL;
  7912. struct s_mapiterator* iter;
  7913. str = script_getstr(st,2);
  7914. m = map_mapname2mapid(str);
  7915. if( m < 0 || map[m].flag.pvp )
  7916. return 0; // nothing to do
  7917. map[m].flag.pvp = 1;
  7918. clif_send0199(m,1);
  7919. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  7920. return 0;
  7921. iter = mapit_getallusers();
  7922. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  7923. {
  7924. if( sd->bl.m != m || sd->pvp_timer != -1 )
  7925. continue; // not applicable
  7926. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  7927. sd->pvp_rank = 0;
  7928. sd->pvp_lastusers = 0;
  7929. sd->pvp_point = 5;
  7930. sd->pvp_won = 0;
  7931. sd->pvp_lost = 0;
  7932. }
  7933. mapit_free(iter);
  7934. return 0;
  7935. }
  7936. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  7937. {
  7938. TBL_PC* sd = (TBL_PC*)bl;
  7939. clif_pvpset(sd, 0, 0, 2);
  7940. if (sd->pvp_timer != -1) {
  7941. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  7942. sd->pvp_timer = INVALID_TIMER;
  7943. }
  7944. return 0;
  7945. }
  7946. BUILDIN_FUNC(pvpoff)
  7947. {
  7948. int m;
  7949. const char *str;
  7950. str=script_getstr(st,2);
  7951. m = map_mapname2mapid(str);
  7952. if(m < 0 || !map[m].flag.pvp)
  7953. return 0; //fixed Lupus
  7954. map[m].flag.pvp = 0;
  7955. clif_send0199(m,0);
  7956. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  7957. return 0;
  7958. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  7959. return 0;
  7960. }
  7961. BUILDIN_FUNC(gvgon)
  7962. {
  7963. int m;
  7964. const char *str;
  7965. str=script_getstr(st,2);
  7966. m = map_mapname2mapid(str);
  7967. if(m >= 0 && !map[m].flag.gvg) {
  7968. map[m].flag.gvg = 1;
  7969. clif_send0199(m,3);
  7970. }
  7971. return 0;
  7972. }
  7973. BUILDIN_FUNC(gvgoff)
  7974. {
  7975. int m;
  7976. const char *str;
  7977. str=script_getstr(st,2);
  7978. m = map_mapname2mapid(str);
  7979. if(m >= 0 && map[m].flag.gvg) {
  7980. map[m].flag.gvg = 0;
  7981. clif_send0199(m,0);
  7982. }
  7983. return 0;
  7984. }
  7985. /*==========================================
  7986. * Shows an emoticon on top of the player/npc
  7987. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  7988. *------------------------------------------*/
  7989. //Optional second parameter added by [Skotlex]
  7990. BUILDIN_FUNC(emotion)
  7991. {
  7992. int type;
  7993. int player=0;
  7994. type=script_getnum(st,2);
  7995. if(type < 0 || type > 100)
  7996. return 0;
  7997. if( script_hasdata(st,3) )
  7998. player=script_getnum(st,3);
  7999. if (player) {
  8000. TBL_PC *sd = script_rid2sd(st);
  8001. if (sd)
  8002. clif_emotion(&sd->bl,type);
  8003. } else
  8004. clif_emotion(map_id2bl(st->oid),type);
  8005. return 0;
  8006. }
  8007. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8008. {
  8009. int m=va_arg(ap,int);
  8010. int g_id=va_arg(ap,int);
  8011. int flag=va_arg(ap,int);
  8012. if(!sd || sd->bl.m != m)
  8013. return 0;
  8014. if(
  8015. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8016. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8017. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8018. )
  8019. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8020. return 1;
  8021. }
  8022. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8023. {
  8024. struct mob_data *md=(struct mob_data *)bl;
  8025. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8026. status_kill(bl);
  8027. return 0;
  8028. }
  8029. BUILDIN_FUNC(maprespawnguildid)
  8030. {
  8031. const char *mapname=script_getstr(st,2);
  8032. int g_id=script_getnum(st,3);
  8033. int flag=script_getnum(st,4);
  8034. int m=map_mapname2mapid(mapname);
  8035. if(m == -1)
  8036. return 0;
  8037. //Catch ALL players (in case some are 'between maps' on execution time)
  8038. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8039. if (flag&4) //Remove script mobs.
  8040. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8041. return 0;
  8042. }
  8043. BUILDIN_FUNC(agitstart)
  8044. {
  8045. if(agit_flag==1) return 0; // Agit already Start.
  8046. agit_flag=1;
  8047. guild_agit_start();
  8048. return 0;
  8049. }
  8050. BUILDIN_FUNC(agitend)
  8051. {
  8052. if(agit_flag==0) return 0; // Agit already End.
  8053. agit_flag=0;
  8054. guild_agit_end();
  8055. return 0;
  8056. }
  8057. /*==========================================
  8058. * Returns whether woe is on or off. // choice script
  8059. *------------------------------------------*/
  8060. BUILDIN_FUNC(agitcheck)
  8061. {
  8062. script_pushint(st,agit_flag);
  8063. return 0;
  8064. }
  8065. /// Sets the guild_id of this npc.
  8066. ///
  8067. /// flagemblem <guild_id>;
  8068. BUILDIN_FUNC(flagemblem)
  8069. {
  8070. TBL_NPC* nd;
  8071. int g_id=script_getnum(st,2);
  8072. if(g_id < 0) return 0;
  8073. nd = (TBL_NPC*)map_id2nd(st->oid);
  8074. if( nd == NULL )
  8075. {
  8076. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8077. }
  8078. else if( nd->subtype != SCRIPT )
  8079. {
  8080. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8081. }
  8082. else
  8083. {
  8084. nd->u.scr.guild_id = g_id;
  8085. clif_guild_emblem_area(&nd->bl);
  8086. }
  8087. return 0;
  8088. }
  8089. BUILDIN_FUNC(getcastlename)
  8090. {
  8091. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8092. struct guild_castle* gc = guild_mapname2gc(mapname);
  8093. const char* name = (gc) ? gc->castle_name : "";
  8094. script_pushstrcopy(st,name);
  8095. return 0;
  8096. }
  8097. BUILDIN_FUNC(getcastledata)
  8098. {
  8099. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8100. int index = script_getnum(st,3);
  8101. struct guild_castle* gc = guild_mapname2gc(mapname);
  8102. if(script_hasdata(st,4) && index==0 && gc) {
  8103. const char* event = script_getstr(st,4);
  8104. check_event(st, event);
  8105. guild_addcastleinfoevent(gc->castle_id,17,event);
  8106. }
  8107. if(gc){
  8108. switch(index){
  8109. case 0: {
  8110. int i;
  8111. for(i=1;i<18;i++) // Initialize[AgitInit]
  8112. guild_castledataload(gc->castle_id,i);
  8113. } break;
  8114. case 1:
  8115. script_pushint(st,gc->guild_id); break;
  8116. case 2:
  8117. script_pushint(st,gc->economy); break;
  8118. case 3:
  8119. script_pushint(st,gc->defense); break;
  8120. case 4:
  8121. script_pushint(st,gc->triggerE); break;
  8122. case 5:
  8123. script_pushint(st,gc->triggerD); break;
  8124. case 6:
  8125. script_pushint(st,gc->nextTime); break;
  8126. case 7:
  8127. script_pushint(st,gc->payTime); break;
  8128. case 8:
  8129. script_pushint(st,gc->createTime); break;
  8130. case 9:
  8131. script_pushint(st,gc->visibleC); break;
  8132. case 10:
  8133. case 11:
  8134. case 12:
  8135. case 13:
  8136. case 14:
  8137. case 15:
  8138. case 16:
  8139. case 17:
  8140. script_pushint(st,gc->guardian[index-10].visible); break;
  8141. default:
  8142. script_pushint(st,0); break;
  8143. }
  8144. return 0;
  8145. }
  8146. script_pushint(st,0);
  8147. return 0;
  8148. }
  8149. BUILDIN_FUNC(setcastledata)
  8150. {
  8151. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8152. int index = script_getnum(st,3);
  8153. int value = script_getnum(st,4);
  8154. struct guild_castle* gc = guild_mapname2gc(mapname);
  8155. if(gc) {
  8156. // Save Data byself First
  8157. switch(index){
  8158. case 1:
  8159. gc->guild_id = value; break;
  8160. case 2:
  8161. gc->economy = value; break;
  8162. case 3:
  8163. gc->defense = value; break;
  8164. case 4:
  8165. gc->triggerE = value; break;
  8166. case 5:
  8167. gc->triggerD = value; break;
  8168. case 6:
  8169. gc->nextTime = value; break;
  8170. case 7:
  8171. gc->payTime = value; break;
  8172. case 8:
  8173. gc->createTime = value; break;
  8174. case 9:
  8175. gc->visibleC = value; break;
  8176. case 10:
  8177. case 11:
  8178. case 12:
  8179. case 13:
  8180. case 14:
  8181. case 15:
  8182. case 16:
  8183. case 17:
  8184. gc->guardian[index-10].visible = value; break;
  8185. default:
  8186. return 0;
  8187. }
  8188. guild_castledatasave(gc->castle_id,index,value);
  8189. }
  8190. return 0;
  8191. }
  8192. /* =====================================================================
  8193. * ギルド情報を要求する
  8194. * ---------------------------------------------------------------------*/
  8195. BUILDIN_FUNC(requestguildinfo)
  8196. {
  8197. int guild_id=script_getnum(st,2);
  8198. const char *event=NULL;
  8199. if( script_hasdata(st,3) ){
  8200. event=script_getstr(st,3);
  8201. check_event(st, event);
  8202. }
  8203. if(guild_id>0)
  8204. guild_npc_request_info(guild_id,event);
  8205. return 0;
  8206. }
  8207. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8208. /// getequipcardcnt(<equipment slot>);
  8209. BUILDIN_FUNC(getequipcardcnt)
  8210. {
  8211. int i=-1,j,num;
  8212. TBL_PC *sd;
  8213. int count;
  8214. num=script_getnum(st,2);
  8215. sd=script_rid2sd(st);
  8216. if (num > 0 && num <= ARRAYLENGTH(equip))
  8217. i=pc_checkequip(sd,equip[num-1]);
  8218. if (i < 0) {
  8219. script_pushint(st,0);
  8220. return 0;
  8221. }
  8222. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8223. {
  8224. script_pushint(st,0);
  8225. return 0;
  8226. }
  8227. count = 0;
  8228. for( j = 0; j < MAX_SLOTS; j++ )
  8229. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8230. count++;
  8231. script_pushint(st,count);
  8232. return 0;
  8233. }
  8234. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8235. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8236. /// successremovecards <slot>;
  8237. BUILDIN_FUNC(successremovecards)
  8238. {
  8239. int i=-1,j,c,cardflag=0;
  8240. TBL_PC* sd = script_rid2sd(st);
  8241. int num = script_getnum(st,2);
  8242. if (num > 0 && num <= ARRAYLENGTH(equip))
  8243. i=pc_checkequip(sd,equip[num-1]);
  8244. if (i < 0) {
  8245. return 0;
  8246. }
  8247. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8248. return 0;
  8249. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8250. {
  8251. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8252. {// extract this card from the item
  8253. int flag;
  8254. struct item item_tmp;
  8255. cardflag = 1;
  8256. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8257. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8258. item_tmp.attribute=0;
  8259. for (j = 0; j < MAX_SLOTS; j++)
  8260. item_tmp.card[j]=0;
  8261. //Logs items, got from (N)PC scripts [Lupus]
  8262. if(log_config.enable_logs&0x40)
  8263. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8264. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8265. clif_additem(sd,0,0,flag);
  8266. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8267. }
  8268. }
  8269. }
  8270. if(cardflag == 1)
  8271. { // カードを取り除いたアイテム所得
  8272. int flag;
  8273. struct item item_tmp;
  8274. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8275. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8276. item_tmp.attribute=sd->status.inventory[i].attribute;
  8277. for (j = 0; j < MAX_SLOTS; j++)
  8278. item_tmp.card[j]=0;
  8279. //Logs items, got from (N)PC scripts [Lupus]
  8280. if(log_config.enable_logs&0x40)
  8281. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8282. pc_delitem(sd,i,1,0);
  8283. //Logs items, got from (N)PC scripts [Lupus]
  8284. if(log_config.enable_logs&0x40)
  8285. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8286. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8287. clif_additem(sd,0,0,flag);
  8288. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8289. }
  8290. clif_misceffect(&sd->bl,3);
  8291. }
  8292. return 0;
  8293. }
  8294. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8295. /// failedremovecards <slot>, <type>;
  8296. /// <type>=0 : will destroy both the item and the cards.
  8297. /// <type>=1 : will keep the item, but destroy the cards.
  8298. /// <type>=2 : will keep the cards, but destroy the item.
  8299. /// <type>=? : will just display the failure effect.
  8300. BUILDIN_FUNC(failedremovecards)
  8301. {
  8302. int i=-1,j,c,cardflag=0;
  8303. TBL_PC* sd = script_rid2sd(st);
  8304. int num = script_getnum(st,2);
  8305. int typefail = script_getnum(st,3);
  8306. if (num > 0 && num <= ARRAYLENGTH(equip))
  8307. i=pc_checkequip(sd,equip[num-1]);
  8308. if (i < 0)
  8309. return 0;
  8310. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8311. return 0;
  8312. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8313. {
  8314. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8315. {
  8316. cardflag = 1;
  8317. if(typefail == 2)
  8318. {// add cards to inventory, clear
  8319. int flag;
  8320. struct item item_tmp;
  8321. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8322. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8323. item_tmp.attribute=0;
  8324. for (j = 0; j < MAX_SLOTS; j++)
  8325. item_tmp.card[j]=0;
  8326. //Logs items, got from (N)PC scripts [Lupus]
  8327. if(log_config.enable_logs&0x40)
  8328. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8329. if((flag=pc_additem(sd,&item_tmp,1))){
  8330. clif_additem(sd,0,0,flag);
  8331. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8332. }
  8333. }
  8334. }
  8335. }
  8336. if(cardflag == 1)
  8337. {
  8338. if(typefail == 0 || typefail == 2){ // 武具損失
  8339. //Logs items, got from (N)PC scripts [Lupus]
  8340. if(log_config.enable_logs&0x40)
  8341. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8342. pc_delitem(sd,i,1,0);
  8343. }
  8344. if(typefail == 1){ // カードのみ損失(武具を返す)
  8345. int flag;
  8346. struct item item_tmp;
  8347. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8348. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8349. item_tmp.attribute=sd->status.inventory[i].attribute;
  8350. //Logs items, got from (N)PC scripts [Lupus]
  8351. if(log_config.enable_logs&0x40)
  8352. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8353. for (j = 0; j < MAX_SLOTS; j++)
  8354. item_tmp.card[j]=0;
  8355. pc_delitem(sd,i,1,0);
  8356. //Logs items, got from (N)PC scripts [Lupus]
  8357. if(log_config.enable_logs&0x40)
  8358. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8359. if((flag=pc_additem(sd,&item_tmp,1))){
  8360. clif_additem(sd,0,0,flag);
  8361. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8362. }
  8363. }
  8364. clif_misceffect(&sd->bl,2);
  8365. }
  8366. return 0;
  8367. }
  8368. /* ================================================================
  8369. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8370. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8371. * improved by [Lance]
  8372. * ================================================================*/
  8373. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8374. {
  8375. int x,y,m,check_val=0,check_ID=0,i=0;
  8376. struct guild *g = NULL;
  8377. struct party_data *p = NULL;
  8378. const char *str;
  8379. const char *mapname;
  8380. unsigned int index;
  8381. mapname=script_getstr(st,2);
  8382. str=script_getstr(st,3);
  8383. x=script_getnum(st,4);
  8384. y=script_getnum(st,5);
  8385. if(script_hasdata(st,7)){
  8386. check_val=script_getnum(st,6);
  8387. check_ID=script_getnum(st,7);
  8388. }
  8389. if((m=map_mapname2mapid(mapname))< 0)
  8390. return 0;
  8391. if(!(index=mapindex_name2id(str)))
  8392. return 0;
  8393. switch(check_val){
  8394. case 1:
  8395. g = guild_search(check_ID);
  8396. if (g){
  8397. for( i=0; i < g->max_member; i++)
  8398. {
  8399. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8400. pc_setpos(g->member[i].sd,index,x,y,3);
  8401. }
  8402. }
  8403. }
  8404. break;
  8405. case 2:
  8406. p = party_search(check_ID);
  8407. if(p){
  8408. for(i=0;i<MAX_PARTY; i++){
  8409. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8410. pc_setpos(p->data[i].sd,index,x,y,3);
  8411. }
  8412. }
  8413. }
  8414. break;
  8415. default:
  8416. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8417. break;
  8418. }
  8419. return 0;
  8420. }
  8421. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8422. {
  8423. char *event=va_arg(ap,char *);
  8424. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8425. return 1;
  8426. return 0;
  8427. }
  8428. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8429. {
  8430. const char *mapname,*event;
  8431. int m;
  8432. mapname=script_getstr(st,2);
  8433. event=script_getstr(st,3);
  8434. check_event(st, event);
  8435. if( (m=map_mapname2mapid(mapname))<0 ) {
  8436. script_pushint(st,-1);
  8437. return 0;
  8438. }
  8439. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8440. return 0;
  8441. }
  8442. BUILDIN_FUNC(marriage)
  8443. {
  8444. const char *partner=script_getstr(st,2);
  8445. TBL_PC *sd=script_rid2sd(st);
  8446. TBL_PC *p_sd=map_nick2sd(partner);
  8447. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8448. script_pushint(st,0);
  8449. return 0;
  8450. }
  8451. script_pushint(st,1);
  8452. return 0;
  8453. }
  8454. BUILDIN_FUNC(wedding_effect)
  8455. {
  8456. TBL_PC *sd=script_rid2sd(st);
  8457. struct block_list *bl;
  8458. if(sd==NULL) {
  8459. bl=map_id2bl(st->oid);
  8460. } else
  8461. bl=&sd->bl;
  8462. clif_wedding_effect(bl);
  8463. return 0;
  8464. }
  8465. BUILDIN_FUNC(divorce)
  8466. {
  8467. TBL_PC *sd=script_rid2sd(st);
  8468. if(sd==NULL || pc_divorce(sd) < 0){
  8469. script_pushint(st,0);
  8470. return 0;
  8471. }
  8472. script_pushint(st,1);
  8473. return 0;
  8474. }
  8475. BUILDIN_FUNC(ispartneron)
  8476. {
  8477. TBL_PC *sd=script_rid2sd(st);
  8478. TBL_PC *p_sd=NULL;
  8479. if(sd==NULL || !pc_ismarried(sd) ||
  8480. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8481. script_pushint(st,0);
  8482. return 0;
  8483. }
  8484. script_pushint(st,1);
  8485. return 0;
  8486. }
  8487. BUILDIN_FUNC(getpartnerid)
  8488. {
  8489. TBL_PC *sd=script_rid2sd(st);
  8490. if (sd == NULL) {
  8491. script_pushint(st,0);
  8492. return 0;
  8493. }
  8494. script_pushint(st,sd->status.partner_id);
  8495. return 0;
  8496. }
  8497. BUILDIN_FUNC(getchildid)
  8498. {
  8499. TBL_PC *sd=script_rid2sd(st);
  8500. if (sd == NULL) {
  8501. script_pushint(st,0);
  8502. return 0;
  8503. }
  8504. script_pushint(st,sd->status.child);
  8505. return 0;
  8506. }
  8507. BUILDIN_FUNC(getmotherid)
  8508. {
  8509. TBL_PC *sd=script_rid2sd(st);
  8510. if (sd == NULL) {
  8511. script_pushint(st,0);
  8512. return 0;
  8513. }
  8514. script_pushint(st,sd->status.mother);
  8515. return 0;
  8516. }
  8517. BUILDIN_FUNC(getfatherid)
  8518. {
  8519. TBL_PC *sd=script_rid2sd(st);
  8520. if (sd == NULL) {
  8521. script_pushint(st,0);
  8522. return 0;
  8523. }
  8524. script_pushint(st,sd->status.father);
  8525. return 0;
  8526. }
  8527. BUILDIN_FUNC(warppartner)
  8528. {
  8529. int x,y;
  8530. unsigned short mapindex;
  8531. const char *str;
  8532. TBL_PC *sd=script_rid2sd(st);
  8533. TBL_PC *p_sd=NULL;
  8534. if(sd==NULL || !pc_ismarried(sd) ||
  8535. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8536. script_pushint(st,0);
  8537. return 0;
  8538. }
  8539. str=script_getstr(st,2);
  8540. x=script_getnum(st,3);
  8541. y=script_getnum(st,4);
  8542. mapindex = mapindex_name2id(str);
  8543. if (mapindex) {
  8544. pc_setpos(p_sd,mapindex,x,y,0);
  8545. script_pushint(st,1);
  8546. } else
  8547. script_pushint(st,0);
  8548. return 0;
  8549. }
  8550. /*================================================
  8551. * Script for Displaying MOB Information [Valaris]
  8552. *------------------------------------------------*/
  8553. BUILDIN_FUNC(strmobinfo)
  8554. {
  8555. int num=script_getnum(st,2);
  8556. int class_=script_getnum(st,3);
  8557. if(!mobdb_checkid(class_))
  8558. {
  8559. script_pushint(st,0);
  8560. return 0;
  8561. }
  8562. switch (num) {
  8563. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  8564. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  8565. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8566. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8567. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8568. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8569. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8570. default:
  8571. script_pushint(st,0);
  8572. break;
  8573. }
  8574. return 0;
  8575. }
  8576. /*==========================================
  8577. * Summon guardians [Valaris]
  8578. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  8579. *------------------------------------------*/
  8580. BUILDIN_FUNC(guardian)
  8581. {
  8582. int class_=0,x=0,y=0,guardian=0;
  8583. const char *str,*map,*evt="";
  8584. struct script_data *data;
  8585. bool has_index = false;
  8586. map =script_getstr(st,2);
  8587. x =script_getnum(st,3);
  8588. y =script_getnum(st,4);
  8589. str =script_getstr(st,5);
  8590. class_=script_getnum(st,6);
  8591. if( script_hasdata(st,8) )
  8592. {// "<event label>",<guardian index>
  8593. evt=script_getstr(st,7);
  8594. guardian=script_getnum(st,8);
  8595. has_index = true;
  8596. } else if( script_hasdata(st,7) ){
  8597. data=script_getdata(st,7);
  8598. get_val(st,data);
  8599. if( data_isstring(data) )
  8600. {// "<event label>"
  8601. evt=script_getstr(st,7);
  8602. } else if( data_isint(data) )
  8603. {// <guardian index>
  8604. guardian=script_getnum(st,7);
  8605. has_index = true;
  8606. } else {
  8607. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8608. script_reportdata(data);
  8609. return 1;
  8610. }
  8611. }
  8612. check_event(st, evt);
  8613. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  8614. return 0;
  8615. }
  8616. /*==========================================
  8617. * Invisible Walls [Zephyrus]
  8618. *------------------------------------------*/
  8619. BUILDIN_FUNC(setwall)
  8620. {
  8621. const char *map, *name;
  8622. int x, y, m, size, dir;
  8623. bool shootable;
  8624. map = script_getstr(st,2);
  8625. x = script_getnum(st,3);
  8626. y = script_getnum(st,4);
  8627. size = script_getnum(st,5);
  8628. dir = script_getnum(st,6);
  8629. shootable = script_getnum(st,7);
  8630. name = script_getstr(st,8);
  8631. if( (m = map_mapname2mapid(map)) < 0 )
  8632. return 0; // Invalid Map
  8633. map_iwall_set(m, x, y, size, dir, shootable, name);
  8634. return 0;
  8635. }
  8636. BUILDIN_FUNC(delwall)
  8637. {
  8638. const char *name = script_getstr(st,2);
  8639. map_iwall_remove(name);
  8640. return 0;
  8641. }
  8642. /// Retrieves various information about the specified guardian.
  8643. ///
  8644. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  8645. /// type: 0 - whether it is deployed or not
  8646. /// 1 - maximum hp
  8647. /// 2 - current hp
  8648. ///
  8649. BUILDIN_FUNC(guardianinfo)
  8650. {
  8651. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8652. int id = script_getnum(st,3);
  8653. int type = script_getnum(st,4);
  8654. struct guild_castle* gc = guild_mapname2gc(mapname);
  8655. struct mob_data* gd;
  8656. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  8657. {
  8658. script_pushint(st,-1);
  8659. return 0;
  8660. }
  8661. if( type == 0 )
  8662. script_pushint(st, gc->guardian[id].visible);
  8663. else
  8664. if( !gc->guardian[id].visible )
  8665. script_pushint(st,-1);
  8666. else
  8667. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  8668. script_pushint(st,-1);
  8669. else
  8670. {
  8671. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  8672. else if( type == 2 ) script_pushint(st,gd->status.hp);
  8673. else
  8674. script_pushint(st,-1);
  8675. }
  8676. return 0;
  8677. }
  8678. /*==========================================
  8679. * IDからItem名
  8680. *------------------------------------------*/
  8681. BUILDIN_FUNC(getitemname)
  8682. {
  8683. int item_id=0;
  8684. struct item_data *i_data;
  8685. char *item_name;
  8686. struct script_data *data;
  8687. data=script_getdata(st,2);
  8688. get_val(st,data);
  8689. if( data_isstring(data) ){
  8690. const char *name=conv_str(st,data);
  8691. struct item_data *item_data = itemdb_searchname(name);
  8692. if( item_data )
  8693. item_id=item_data->nameid;
  8694. }else
  8695. item_id=conv_num(st,data);
  8696. i_data = itemdb_exists(item_id);
  8697. if (i_data == NULL)
  8698. {
  8699. script_pushconststr(st,"null");
  8700. return 0;
  8701. }
  8702. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8703. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8704. script_pushstr(st,item_name);
  8705. return 0;
  8706. }
  8707. /*==========================================
  8708. * Returns number of slots an item has. [Skotlex]
  8709. *------------------------------------------*/
  8710. BUILDIN_FUNC(getitemslots)
  8711. {
  8712. int item_id;
  8713. struct item_data *i_data;
  8714. item_id=script_getnum(st,2);
  8715. i_data = itemdb_exists(item_id);
  8716. if (i_data)
  8717. script_pushint(st,i_data->slot);
  8718. else
  8719. script_pushint(st,-1);
  8720. return 0;
  8721. }
  8722. /*==========================================
  8723. * Returns some values of an item [Lupus]
  8724. * Price, Weight, etc...
  8725. getiteminfo(itemID,n), where n
  8726. 0 value_buy;
  8727. 1 value_sell;
  8728. 2 type;
  8729. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8730. if = 0, then monsters don't drop it at all (rare or a quest item)
  8731. if = 10000, then this item is sold in NPC shops only
  8732. 4 sex;
  8733. 5 equip;
  8734. 6 weight;
  8735. 7 atk;
  8736. 8 def;
  8737. 9 range;
  8738. 10 slot;
  8739. 11 look;
  8740. 12 elv;
  8741. 13 wlv;
  8742. 14 view id
  8743. *------------------------------------------*/
  8744. BUILDIN_FUNC(getiteminfo)
  8745. {
  8746. int item_id,n;
  8747. int *item_arr;
  8748. struct item_data *i_data;
  8749. item_id = script_getnum(st,2);
  8750. n = script_getnum(st,3);
  8751. i_data = itemdb_exists(item_id);
  8752. if (i_data && n>=0 && n<14) {
  8753. item_arr = (int*)&i_data->value_buy;
  8754. script_pushint(st,item_arr[n]);
  8755. } else
  8756. script_pushint(st,-1);
  8757. return 0;
  8758. }
  8759. /*==========================================
  8760. * Set some values of an item [Lupus]
  8761. * Price, Weight, etc...
  8762. setiteminfo(itemID,n,Value), where n
  8763. 0 value_buy;
  8764. 1 value_sell;
  8765. 2 type;
  8766. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8767. if = 0, then monsters don't drop it at all (rare or a quest item)
  8768. if = 10000, then this item is sold in NPC shops only
  8769. 4 sex;
  8770. 5 equip;
  8771. 6 weight;
  8772. 7 atk;
  8773. 8 def;
  8774. 9 range;
  8775. 10 slot;
  8776. 11 look;
  8777. 12 elv;
  8778. 13 wlv;
  8779. 14 view id
  8780. * Returns Value or -1 if the wrong field's been set
  8781. *------------------------------------------*/
  8782. BUILDIN_FUNC(setiteminfo)
  8783. {
  8784. int item_id,n,value;
  8785. int *item_arr;
  8786. struct item_data *i_data;
  8787. item_id = script_getnum(st,2);
  8788. n = script_getnum(st,3);
  8789. value = script_getnum(st,4);
  8790. i_data = itemdb_exists(item_id);
  8791. if (i_data && n>=0 && n<14) {
  8792. item_arr = (int*)&i_data->value_buy;
  8793. item_arr[n] = value;
  8794. script_pushint(st,value);
  8795. } else
  8796. script_pushint(st,-1);
  8797. return 0;
  8798. }
  8799. /*==========================================
  8800. * Returns value from equipped item slot n [Lupus]
  8801. getequipcardid(num,slot)
  8802. where
  8803. num = eqip position slot
  8804. slot = 0,1,2,3 (Card Slot N)
  8805. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  8806. it's useful when you want to check item cards or if it's signed
  8807. Useful for such quests as "Sign this refined item with players name" etc
  8808. Hat[0] +4 -> Player's Hat[0] +4
  8809. *------------------------------------------*/
  8810. BUILDIN_FUNC(getequipcardid)
  8811. {
  8812. int i=-1,num,slot;
  8813. TBL_PC *sd;
  8814. num=script_getnum(st,2);
  8815. slot=script_getnum(st,3);
  8816. sd=script_rid2sd(st);
  8817. if (num > 0 && num <= ARRAYLENGTH(equip))
  8818. i=pc_checkequip(sd,equip[num-1]);
  8819. if(i >= 0 && slot>=0 && slot<4)
  8820. script_pushint(st,sd->status.inventory[i].card[slot]);
  8821. else
  8822. script_pushint(st,0);
  8823. return 0;
  8824. }
  8825. /*==========================================
  8826. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  8827. *------------------------------------------*/
  8828. BUILDIN_FUNC(petskillbonus)
  8829. {
  8830. struct pet_data *pd;
  8831. TBL_PC *sd=script_rid2sd(st);
  8832. if(sd==NULL || sd->pd==NULL)
  8833. return 0;
  8834. pd=sd->pd;
  8835. if (pd->bonus)
  8836. { //Clear previous bonus
  8837. if (pd->bonus->timer != -1)
  8838. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  8839. } else //init
  8840. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  8841. pd->bonus->type=script_getnum(st,2);
  8842. pd->bonus->val=script_getnum(st,3);
  8843. pd->bonus->duration=script_getnum(st,4);
  8844. pd->bonus->delay=script_getnum(st,5);
  8845. if (pd->state.skillbonus == 1)
  8846. pd->state.skillbonus=0; // waiting state
  8847. // wait for timer to start
  8848. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  8849. pd->bonus->timer = INVALID_TIMER;
  8850. else
  8851. pd->bonus->timer = add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  8852. return 0;
  8853. }
  8854. /*==========================================
  8855. * pet looting [Valaris] //Rewritten by [Skotlex]
  8856. *------------------------------------------*/
  8857. BUILDIN_FUNC(petloot)
  8858. {
  8859. int max;
  8860. struct pet_data *pd;
  8861. TBL_PC *sd=script_rid2sd(st);
  8862. if(sd==NULL || sd->pd==NULL)
  8863. return 0;
  8864. max=script_getnum(st,2);
  8865. if(max < 1)
  8866. max = 1; //Let'em loot at least 1 item.
  8867. else if (max > MAX_PETLOOT_SIZE)
  8868. max = MAX_PETLOOT_SIZE;
  8869. pd = sd->pd;
  8870. if (pd->loot != NULL)
  8871. { //Release whatever was there already and reallocate memory
  8872. pet_lootitem_drop(pd, pd->msd);
  8873. aFree(pd->loot->item);
  8874. }
  8875. else
  8876. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  8877. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  8878. pd->loot->max=max;
  8879. pd->loot->count = 0;
  8880. pd->loot->weight = 0;
  8881. return 0;
  8882. }
  8883. /*==========================================
  8884. * PCの所持品情報読み取り
  8885. *------------------------------------------*/
  8886. BUILDIN_FUNC(getinventorylist)
  8887. {
  8888. TBL_PC *sd=script_rid2sd(st);
  8889. char card_var[NAME_LENGTH];
  8890. int i,j=0,k;
  8891. if(!sd) return 0;
  8892. for(i=0;i<MAX_INVENTORY;i++){
  8893. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  8894. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  8895. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  8896. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  8897. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  8898. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  8899. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  8900. for (k = 0; k < MAX_SLOTS; k++)
  8901. {
  8902. sprintf(card_var, "@inventorylist_card%d",k+1);
  8903. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  8904. }
  8905. j++;
  8906. }
  8907. }
  8908. pc_setreg(sd,add_str("@inventorylist_count"),j);
  8909. return 0;
  8910. }
  8911. BUILDIN_FUNC(getskilllist)
  8912. {
  8913. TBL_PC *sd=script_rid2sd(st);
  8914. int i,j=0;
  8915. if(!sd) return 0;
  8916. for(i=0;i<MAX_SKILL;i++){
  8917. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  8918. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  8919. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  8920. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  8921. j++;
  8922. }
  8923. }
  8924. pc_setreg(sd,add_str("@skilllist_count"),j);
  8925. return 0;
  8926. }
  8927. BUILDIN_FUNC(clearitem)
  8928. {
  8929. TBL_PC *sd=script_rid2sd(st);
  8930. int i;
  8931. if(sd==NULL) return 0;
  8932. for (i=0; i<MAX_INVENTORY; i++) {
  8933. if (sd->status.inventory[i].amount) {
  8934. //Logs items, got from (N)PC scripts [Lupus]
  8935. if(log_config.enable_logs&0x40)
  8936. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  8937. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  8938. }
  8939. }
  8940. return 0;
  8941. }
  8942. /*==========================================
  8943. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  8944. *------------------------------------------*/
  8945. BUILDIN_FUNC(disguise)
  8946. {
  8947. int id;
  8948. TBL_PC* sd = script_rid2sd(st);
  8949. if (sd == NULL) return 0;
  8950. id = script_getnum(st,2);
  8951. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  8952. pc_disguise(sd, id);
  8953. script_pushint(st,id);
  8954. } else
  8955. script_pushint(st,0);
  8956. return 0;
  8957. }
  8958. /*==========================================
  8959. * Undisguise Player (returns 1 if success, 0 on fail)
  8960. *------------------------------------------*/
  8961. BUILDIN_FUNC(undisguise)
  8962. {
  8963. TBL_PC* sd = script_rid2sd(st);
  8964. if (sd == NULL) return 0;
  8965. if (sd->disguise) {
  8966. pc_disguise(sd, 0);
  8967. script_pushint(st,0);
  8968. } else {
  8969. script_pushint(st,1);
  8970. }
  8971. return 0;
  8972. }
  8973. /*==========================================
  8974. * NPCクラスチェンジ
  8975. * classは変わりたいclass
  8976. * typeは通常0なのかな?
  8977. *------------------------------------------*/
  8978. BUILDIN_FUNC(classchange)
  8979. {
  8980. int _class,type;
  8981. struct block_list *bl=map_id2bl(st->oid);
  8982. if(bl==NULL) return 0;
  8983. _class=script_getnum(st,2);
  8984. type=script_getnum(st,3);
  8985. clif_class_change(bl,_class,type);
  8986. return 0;
  8987. }
  8988. /*==========================================
  8989. * NPCから発生するエフェクト
  8990. *------------------------------------------*/
  8991. BUILDIN_FUNC(misceffect)
  8992. {
  8993. int type;
  8994. type=script_getnum(st,2);
  8995. if(st->oid && st->oid != fake_nd->bl.id) {
  8996. struct block_list *bl = map_id2bl(st->oid);
  8997. if (bl)
  8998. clif_misceffect2(bl,type);
  8999. } else{
  9000. TBL_PC *sd=script_rid2sd(st);
  9001. if(sd)
  9002. clif_misceffect2(&sd->bl,type);
  9003. }
  9004. return 0;
  9005. }
  9006. /*==========================================
  9007. * サウンドエフェクト
  9008. *------------------------------------------*/
  9009. BUILDIN_FUNC(soundeffect)
  9010. {
  9011. TBL_PC* sd = script_rid2sd(st);
  9012. const char* name = script_getstr(st,2);
  9013. int type = script_getnum(st,3);
  9014. if(sd)
  9015. {
  9016. if(!st->rid)
  9017. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9018. else
  9019. clif_soundeffect(sd,&sd->bl,name,type);
  9020. }
  9021. return 0;
  9022. }
  9023. int soundeffect_sub(struct block_list* bl,va_list ap)
  9024. {
  9025. char* name = va_arg(ap,char*);
  9026. int type = va_arg(ap,int);
  9027. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9028. return 0;
  9029. }
  9030. /*==========================================
  9031. * Play a sound effect (.wav) on multiple clients
  9032. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9033. *------------------------------------------*/
  9034. BUILDIN_FUNC(soundeffectall)
  9035. {
  9036. struct block_list* bl;
  9037. const char* name;
  9038. int type;
  9039. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9040. if (!bl)
  9041. return 0;
  9042. name = script_getstr(st,2);
  9043. type = script_getnum(st,3);
  9044. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9045. if(!script_hasdata(st,4))
  9046. { // area around
  9047. clif_soundeffectall(bl, name, type, AREA);
  9048. }
  9049. else
  9050. if(!script_hasdata(st,5))
  9051. { // entire map
  9052. const char* map = script_getstr(st,4);
  9053. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9054. }
  9055. else
  9056. if(script_hasdata(st,8))
  9057. { // specified part of map
  9058. const char* map = script_getstr(st,4);
  9059. int x0 = script_getnum(st,5);
  9060. int y0 = script_getnum(st,6);
  9061. int x1 = script_getnum(st,7);
  9062. int y1 = script_getnum(st,8);
  9063. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9064. }
  9065. else
  9066. {
  9067. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9068. }
  9069. return 0;
  9070. }
  9071. /*==========================================
  9072. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9073. *------------------------------------------*/
  9074. BUILDIN_FUNC(petrecovery)
  9075. {
  9076. struct pet_data *pd;
  9077. TBL_PC *sd=script_rid2sd(st);
  9078. if(sd==NULL || sd->pd==NULL)
  9079. return 0;
  9080. pd=sd->pd;
  9081. if (pd->recovery)
  9082. { //Halt previous bonus
  9083. if (pd->recovery->timer != -1)
  9084. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9085. } else //Init
  9086. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9087. pd->recovery->type = (sc_type)script_getnum(st,2);
  9088. pd->recovery->delay = script_getnum(st,3);
  9089. pd->recovery->timer = INVALID_TIMER;
  9090. return 0;
  9091. }
  9092. /*==========================================
  9093. * pet healing [Valaris] //Rewritten by [Skotlex]
  9094. *------------------------------------------*/
  9095. BUILDIN_FUNC(petheal)
  9096. {
  9097. struct pet_data *pd;
  9098. TBL_PC *sd=script_rid2sd(st);
  9099. if(sd==NULL || sd->pd==NULL)
  9100. return 0;
  9101. pd=sd->pd;
  9102. if (pd->s_skill)
  9103. { //Clear previous skill
  9104. if (pd->s_skill->timer != -1)
  9105. {
  9106. if (pd->s_skill->id)
  9107. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9108. else
  9109. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9110. }
  9111. } else //init memory
  9112. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9113. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9114. //Use the lv as the amount to heal
  9115. pd->s_skill->lv=script_getnum(st,2);
  9116. pd->s_skill->delay=script_getnum(st,3);
  9117. pd->s_skill->hp=script_getnum(st,4);
  9118. pd->s_skill->sp=script_getnum(st,5);
  9119. //Use delay as initial offset to avoid skill/heal exploits
  9120. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9121. pd->s_skill->timer = INVALID_TIMER;
  9122. else
  9123. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9124. return 0;
  9125. }
  9126. /*==========================================
  9127. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9128. *------------------------------------------*/
  9129. /// petskillattack <skill id>,<level>,<rate>,<bonusrate>
  9130. /// petskillattack "<skill name>",<level>,<rate>,<bonusrate>
  9131. BUILDIN_FUNC(petskillattack)
  9132. {
  9133. struct pet_data *pd;
  9134. TBL_PC *sd=script_rid2sd(st);
  9135. if(sd==NULL || sd->pd==NULL)
  9136. return 0;
  9137. pd=sd->pd;
  9138. if (pd->a_skill == NULL)
  9139. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9140. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9141. pd->a_skill->lv=script_getnum(st,3);
  9142. pd->a_skill->div_ = 0;
  9143. pd->a_skill->rate=script_getnum(st,4);
  9144. pd->a_skill->bonusrate=script_getnum(st,5);
  9145. return 0;
  9146. }
  9147. /*==========================================
  9148. * pet attack skills [Valaris]
  9149. *------------------------------------------*/
  9150. /// petskillattack2 <skill id>,<level>,<div>,<rate>,<bonusrate>
  9151. /// petskillattack2 "<skill name>",<level>,<div>,<rate>,<bonusrate>
  9152. BUILDIN_FUNC(petskillattack2)
  9153. {
  9154. struct pet_data *pd;
  9155. TBL_PC *sd=script_rid2sd(st);
  9156. if(sd==NULL || sd->pd==NULL)
  9157. return 0;
  9158. pd=sd->pd;
  9159. if (pd->a_skill == NULL)
  9160. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9161. pd->a_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9162. pd->a_skill->lv=script_getnum(st,3);
  9163. pd->a_skill->div_ = script_getnum(st,4);
  9164. pd->a_skill->rate=script_getnum(st,5);
  9165. pd->a_skill->bonusrate=script_getnum(st,6);
  9166. return 0;
  9167. }
  9168. /*==========================================
  9169. * pet support skills [Skotlex]
  9170. *------------------------------------------*/
  9171. /// petskillsupport <skill id>,<level>,<delay>,<hp>,<sp>
  9172. /// petskillsupport "<skill name>",<level>,<delay>,<hp>,<sp>
  9173. BUILDIN_FUNC(petskillsupport)
  9174. {
  9175. struct pet_data *pd;
  9176. TBL_PC *sd=script_rid2sd(st);
  9177. if(sd==NULL || sd->pd==NULL)
  9178. return 0;
  9179. pd=sd->pd;
  9180. if (pd->s_skill)
  9181. { //Clear previous skill
  9182. if (pd->s_skill->timer != -1)
  9183. {
  9184. if (pd->s_skill->id)
  9185. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9186. else
  9187. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9188. }
  9189. } else //init memory
  9190. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9191. pd->s_skill->id=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9192. pd->s_skill->lv=script_getnum(st,3);
  9193. pd->s_skill->delay=script_getnum(st,4);
  9194. pd->s_skill->hp=script_getnum(st,5);
  9195. pd->s_skill->sp=script_getnum(st,6);
  9196. //Use delay as initial offset to avoid skill/heal exploits
  9197. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9198. pd->s_skill->timer = INVALID_TIMER;
  9199. else
  9200. pd->s_skill->timer = add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9201. return 0;
  9202. }
  9203. /*==========================================
  9204. * Scripted skill effects [Celest]
  9205. *------------------------------------------*/
  9206. /// skilleffect <skill id>,<level>
  9207. /// skilleffect "<skill name>",<level>
  9208. BUILDIN_FUNC(skilleffect)
  9209. {
  9210. TBL_PC *sd;
  9211. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9212. int skilllv=script_getnum(st,3);
  9213. sd=script_rid2sd(st);
  9214. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9215. return 0;
  9216. }
  9217. /*==========================================
  9218. * NPC skill effects [Valaris]
  9219. *------------------------------------------*/
  9220. /// npcskilleffect <skill id>,<level>,<x>,<y>
  9221. /// npcskilleffect "<skill name>",<level>,<x>,<y>
  9222. BUILDIN_FUNC(npcskilleffect)
  9223. {
  9224. struct block_list *bl= map_id2bl(st->oid);
  9225. int skillid=( script_isstring(st,2) ? skill_name2id(script_getstr(st,2)) : script_getnum(st,2) );
  9226. int skilllv=script_getnum(st,3);
  9227. int x=script_getnum(st,4);
  9228. int y=script_getnum(st,5);
  9229. if (bl)
  9230. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9231. return 0;
  9232. }
  9233. /*==========================================
  9234. * Special effects [Valaris]
  9235. *------------------------------------------*/
  9236. BUILDIN_FUNC(specialeffect)
  9237. {
  9238. struct block_list *bl=map_id2bl(st->oid);
  9239. int type = script_getnum(st,2);
  9240. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9241. if(bl==NULL)
  9242. return 0;
  9243. clif_specialeffect(bl, type, target);
  9244. return 0;
  9245. }
  9246. BUILDIN_FUNC(specialeffect2)
  9247. {
  9248. TBL_PC *sd=script_rid2sd(st);
  9249. int type = script_getnum(st,2);
  9250. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9251. if(sd==NULL)
  9252. return 0;
  9253. clif_specialeffect(&sd->bl, type, target);
  9254. return 0;
  9255. }
  9256. /*==========================================
  9257. * Nude [Valaris]
  9258. *------------------------------------------*/
  9259. BUILDIN_FUNC(nude)
  9260. {
  9261. TBL_PC *sd=script_rid2sd(st);
  9262. int i,calcflag=0;
  9263. if(sd==NULL)
  9264. return 0;
  9265. for(i=0;i<11;i++)
  9266. if(sd->equip_index[i] >= 0) {
  9267. if(!calcflag)
  9268. calcflag=1;
  9269. pc_unequipitem(sd,sd->equip_index[i],2);
  9270. }
  9271. if(calcflag)
  9272. status_calc_pc(sd,0);
  9273. return 0;
  9274. }
  9275. /*==========================================
  9276. * gmcommand [MouseJstr]
  9277. *
  9278. * suggested on the forums...
  9279. * splitted into atcommand & charcommand by [Skotlex]
  9280. *------------------------------------------*/
  9281. BUILDIN_FUNC(atcommand)
  9282. {
  9283. TBL_PC dummy_sd;
  9284. TBL_PC* sd;
  9285. int fd;
  9286. const char* cmd;
  9287. cmd = script_getstr(st,2);
  9288. if (st->rid) {
  9289. sd = script_rid2sd(st);
  9290. fd = sd->fd;
  9291. } else { //Use a dummy character.
  9292. sd = &dummy_sd;
  9293. fd = 0;
  9294. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9295. if (st->oid)
  9296. {
  9297. struct block_list* bl = map_id2bl(st->oid);
  9298. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9299. if (bl->type == BL_NPC)
  9300. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9301. }
  9302. }
  9303. // compatibility with previous implementation (deprecated!)
  9304. if(cmd[0] != atcommand_symbol)
  9305. {
  9306. cmd += strlen(sd->status.name);
  9307. while(*cmd != atcommand_symbol && *cmd != 0)
  9308. cmd++;
  9309. }
  9310. is_atcommand_sub(fd, sd, cmd, 99);
  9311. return 0;
  9312. }
  9313. BUILDIN_FUNC(charcommand)
  9314. {
  9315. TBL_PC dummy_sd;
  9316. TBL_PC* sd;
  9317. int fd;
  9318. const char* cmd;
  9319. cmd = script_getstr(st,2);
  9320. if (st->rid) {
  9321. sd = script_rid2sd(st);
  9322. fd = sd->fd;
  9323. } else { //Use a dummy character.
  9324. sd = &dummy_sd;
  9325. fd = 0;
  9326. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9327. if (st->oid)
  9328. {
  9329. struct block_list* bl = map_id2bl(st->oid);
  9330. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9331. if (bl->type == BL_NPC)
  9332. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9333. }
  9334. }
  9335. // compatibility with previous implementation (deprecated!)
  9336. if(cmd[0] != charcommand_symbol)
  9337. {
  9338. cmd += strlen(sd->status.name);
  9339. while(*cmd != charcommand_symbol && *cmd != 0)
  9340. cmd++;
  9341. }
  9342. is_charcommand_sub(fd, sd, cmd, 99);
  9343. return 0;
  9344. }
  9345. /*==========================================
  9346. * Displays a message for the player only (like system messages like "you got an apple" )
  9347. *------------------------------------------*/
  9348. BUILDIN_FUNC(dispbottom)
  9349. {
  9350. TBL_PC *sd=script_rid2sd(st);
  9351. const char *message;
  9352. message=script_getstr(st,2);
  9353. if(sd)
  9354. clif_disp_onlyself(sd,message,(int)strlen(message));
  9355. return 0;
  9356. }
  9357. /*==========================================
  9358. * All The Players Full Recovery
  9359. * (HP/SP full restore and resurrect if need)
  9360. *------------------------------------------*/
  9361. BUILDIN_FUNC(recovery)
  9362. {
  9363. TBL_PC* sd;
  9364. struct s_mapiterator* iter;
  9365. iter = mapit_getallusers();
  9366. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9367. {
  9368. if(pc_isdead(sd))
  9369. status_revive(&sd->bl, 100, 100);
  9370. else
  9371. status_percent_heal(&sd->bl, 100, 100);
  9372. clif_displaymessage(sd->fd,"You have been recovered!");
  9373. }
  9374. mapit_free(iter);
  9375. return 0;
  9376. }
  9377. /*==========================================
  9378. * Get your pet info: getpetinfo(n)
  9379. * n -> 0:pet_id 1:pet_class 2:pet_name
  9380. * 3:friendly 4:hungry, 5: rename flag.
  9381. *------------------------------------------*/
  9382. BUILDIN_FUNC(getpetinfo)
  9383. {
  9384. TBL_PC *sd=script_rid2sd(st);
  9385. TBL_PET *pd;
  9386. int type=script_getnum(st,2);
  9387. if(!sd || !sd->pd) {
  9388. if (type == 2)
  9389. script_pushconststr(st,"null");
  9390. else
  9391. script_pushint(st,0);
  9392. return 0;
  9393. }
  9394. pd = sd->pd;
  9395. switch(type){
  9396. case 0: script_pushint(st,pd->pet.pet_id); break;
  9397. case 1: script_pushint(st,pd->pet.class_); break;
  9398. case 2: script_pushstrcopy(st,pd->pet.name); break;
  9399. case 3: script_pushint(st,pd->pet.intimate); break;
  9400. case 4: script_pushint(st,pd->pet.hungry); break;
  9401. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9402. default:
  9403. script_pushint(st,0);
  9404. break;
  9405. }
  9406. return 0;
  9407. }
  9408. /*==========================================
  9409. * Get your homunculus info: gethominfo(n)
  9410. * n -> 0:hom_id 1:class 2:name
  9411. * 3:friendly 4:hungry, 5: rename flag.
  9412. * 6: level
  9413. *------------------------------------------*/
  9414. BUILDIN_FUNC(gethominfo)
  9415. {
  9416. TBL_PC *sd=script_rid2sd(st);
  9417. TBL_HOM *hd;
  9418. int type=script_getnum(st,2);
  9419. hd = sd?sd->hd:NULL;
  9420. if(!merc_is_hom_active(hd))
  9421. {
  9422. if (type == 2)
  9423. script_pushconststr(st,"null");
  9424. else
  9425. script_pushint(st,0);
  9426. return 0;
  9427. }
  9428. switch(type){
  9429. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9430. case 1: script_pushint(st,hd->homunculus.class_); break;
  9431. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  9432. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9433. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9434. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9435. case 6: script_pushint(st,hd->homunculus.level); break;
  9436. default:
  9437. script_pushint(st,0);
  9438. break;
  9439. }
  9440. return 0;
  9441. }
  9442. /*==========================================
  9443. * Shows wether your inventory(and equips) contain
  9444. selected card or not.
  9445. checkequipedcard(4001);
  9446. *------------------------------------------*/
  9447. BUILDIN_FUNC(checkequipedcard)
  9448. {
  9449. TBL_PC *sd=script_rid2sd(st);
  9450. int n,i,c=0;
  9451. c=script_getnum(st,2);
  9452. if(sd){
  9453. for(i=0;i<MAX_INVENTORY;i++){
  9454. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9455. for(n=0;n<MAX_SLOTS;n++){
  9456. if(sd->status.inventory[i].card[n]==c){
  9457. script_pushint(st,1);
  9458. return 0;
  9459. }
  9460. }
  9461. }
  9462. }
  9463. }
  9464. script_pushint(st,0);
  9465. return 0;
  9466. }
  9467. BUILDIN_FUNC(jump_zero)
  9468. {
  9469. int sel;
  9470. sel=script_getnum(st,2);
  9471. if(!sel) {
  9472. int pos;
  9473. if( !data_islabel(script_getdata(st,3)) ){
  9474. ShowError("script: jump_zero: not label !\n");
  9475. st->state=END;
  9476. return 1;
  9477. }
  9478. pos=script_getnum(st,3);
  9479. st->pos=pos;
  9480. st->state=GOTO;
  9481. // printf("script: jump_zero: jumpto : %d\n",pos);
  9482. } else {
  9483. // printf("script: jump_zero: fail\n");
  9484. }
  9485. return 0;
  9486. }
  9487. /*==========================================
  9488. * GetMapMobs
  9489. returns mob counts on a set map:
  9490. e.g. GetMapMobs("prontera")
  9491. use "this" - for player's map
  9492. *------------------------------------------*/
  9493. BUILDIN_FUNC(getmapmobs)
  9494. {
  9495. const char *str=NULL;
  9496. int m=-1,bx,by;
  9497. int count=0;
  9498. struct block_list *bl;
  9499. str=script_getstr(st,2);
  9500. if(strcmp(str,"this")==0){
  9501. TBL_PC *sd=script_rid2sd(st);
  9502. if(sd)
  9503. m=sd->bl.m;
  9504. else{
  9505. script_pushint(st,-1);
  9506. return 0;
  9507. }
  9508. }else
  9509. m=map_mapname2mapid(str);
  9510. if(m < 0){
  9511. script_pushint(st,-1);
  9512. return 0;
  9513. }
  9514. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  9515. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  9516. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  9517. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9518. count++;
  9519. script_pushint(st,count);
  9520. return 0;
  9521. }
  9522. /*==========================================
  9523. * movenpc [MouseJstr]
  9524. *------------------------------------------*/
  9525. BUILDIN_FUNC(movenpc)
  9526. {
  9527. TBL_NPC *nd = NULL;
  9528. const char *npc;
  9529. int x,y;
  9530. npc = script_getstr(st,2);
  9531. x = script_getnum(st,3);
  9532. y = script_getnum(st,4);
  9533. if ((nd = npc_name2id(npc)) == NULL)
  9534. return -1;
  9535. npc_movenpc(nd, x, y);
  9536. return 0;
  9537. }
  9538. /*==========================================
  9539. * message [MouseJstr]
  9540. *------------------------------------------*/
  9541. BUILDIN_FUNC(message)
  9542. {
  9543. TBL_PC *sd;
  9544. const char *msg,*player;
  9545. TBL_PC *pl_sd = NULL;
  9546. sd = script_rid2sd(st);
  9547. player = script_getstr(st,2);
  9548. msg = script_getstr(st,3);
  9549. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9550. return 0;
  9551. clif_displaymessage(pl_sd->fd, msg);
  9552. return 0;
  9553. }
  9554. /*==========================================
  9555. * npctalk (sends message to surrounding area)
  9556. *------------------------------------------*/
  9557. BUILDIN_FUNC(npctalk)
  9558. {
  9559. const char* str;
  9560. char message[255];
  9561. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9562. str = script_getstr(st,2);
  9563. if(nd) {
  9564. memcpy(message, nd->name, NAME_LENGTH);
  9565. strtok(message, "#"); // discard extra name identifier if present
  9566. strcat(message, " : ");
  9567. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9568. clif_message(&(nd->bl), message);
  9569. }
  9570. return 0;
  9571. }
  9572. // change npc walkspeed [Valaris]
  9573. BUILDIN_FUNC(npcspeed)
  9574. {
  9575. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9576. int x=0;
  9577. x=script_getnum(st,2);
  9578. if(nd) {
  9579. nd->speed=x;
  9580. }
  9581. return 0;
  9582. }
  9583. // make an npc walk to a position [Valaris]
  9584. BUILDIN_FUNC(npcwalkto)
  9585. {
  9586. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9587. int x=0,y=0;
  9588. x=script_getnum(st,2);
  9589. y=script_getnum(st,3);
  9590. if(nd) {
  9591. unit_walktoxy(&nd->bl,x,y,0);
  9592. }
  9593. return 0;
  9594. }
  9595. // stop an npc's movement [Valaris]
  9596. BUILDIN_FUNC(npcstop)
  9597. {
  9598. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9599. if(nd) {
  9600. unit_stop_walking(&nd->bl,1);
  9601. }
  9602. return 0;
  9603. }
  9604. /*==========================================
  9605. * getlook char info. getlook(arg)
  9606. *------------------------------------------*/
  9607. BUILDIN_FUNC(getlook)
  9608. {
  9609. int type,val;
  9610. TBL_PC *sd;
  9611. sd=script_rid2sd(st);
  9612. type=script_getnum(st,2);
  9613. val=-1;
  9614. switch(type) {
  9615. case LOOK_HAIR: val=sd->status.hair; break; //1
  9616. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  9617. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  9618. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  9619. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  9620. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  9621. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  9622. case LOOK_SHIELD: val=sd->status.shield; break; //8
  9623. case LOOK_SHOES: break; //9
  9624. }
  9625. script_pushint(st,val);
  9626. return 0;
  9627. }
  9628. /*==========================================
  9629. * get char save point. argument: 0- map name, 1- x, 2- y
  9630. *------------------------------------------*/
  9631. BUILDIN_FUNC(getsavepoint)
  9632. {
  9633. TBL_PC* sd;
  9634. int type;
  9635. sd = script_rid2sd(st);
  9636. if (sd == NULL) {
  9637. script_pushint(st,0);
  9638. return 0;
  9639. }
  9640. type = script_getnum(st,2);
  9641. switch(type) {
  9642. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  9643. case 1: script_pushint(st,sd->status.save_point.x); break;
  9644. case 2: script_pushint(st,sd->status.save_point.y); break;
  9645. default:
  9646. script_pushint(st,0);
  9647. break;
  9648. }
  9649. return 0;
  9650. }
  9651. /*==========================================
  9652. * Get position for char/npc/pet/mob objects. Added by Lorky
  9653. *
  9654. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  9655. * where type:
  9656. * MapName$ - String variable for output map name
  9657. * MapX - Integer variable for output coord X
  9658. * MapY - Integer variable for output coord Y
  9659. * type - type of object
  9660. * 0 - Character coord
  9661. * 1 - NPC coord
  9662. * 2 - Pet coord
  9663. * 3 - Mob coord (not released)
  9664. * 4 - Homun coord
  9665. * CharName$ - Name object. If miss or "this" the current object
  9666. *
  9667. * Return:
  9668. * 0 - success
  9669. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  9670. *------------------------------------------*/
  9671. BUILDIN_FUNC(getmapxy)
  9672. {
  9673. struct block_list *bl = NULL;
  9674. TBL_PC *sd=NULL;
  9675. int num;
  9676. const char *name;
  9677. char prefix;
  9678. int x,y,type;
  9679. char mapname[MAP_NAME_LENGTH];
  9680. if( !data_isreference(script_getdata(st,2)) ){
  9681. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  9682. script_pushint(st,-1);
  9683. return 1;
  9684. }
  9685. if( !data_isreference(script_getdata(st,3)) ){
  9686. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  9687. script_pushint(st,-1);
  9688. return 1;
  9689. }
  9690. if( !data_isreference(script_getdata(st,4)) ){
  9691. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  9692. script_pushint(st,-1);
  9693. return 1;
  9694. }
  9695. // Possible needly check function parameters on C_STR,C_INT,C_INT
  9696. type=script_getnum(st,5);
  9697. switch (type){
  9698. case 0: //Get Character Position
  9699. if( script_hasdata(st,6) )
  9700. sd=map_nick2sd(script_getstr(st,6));
  9701. else
  9702. sd=script_rid2sd(st);
  9703. if (sd)
  9704. bl = &sd->bl;
  9705. break;
  9706. case 1: //Get NPC Position
  9707. if( script_hasdata(st,6) )
  9708. {
  9709. struct npc_data *nd;
  9710. nd=npc_name2id(script_getstr(st,6));
  9711. if (nd)
  9712. bl = &nd->bl;
  9713. } else //In case the origin is not an npc?
  9714. bl=map_id2bl(st->oid);
  9715. break;
  9716. case 2: //Get Pet Position
  9717. if(script_hasdata(st,6))
  9718. sd=map_nick2sd(script_getstr(st,6));
  9719. else
  9720. sd=script_rid2sd(st);
  9721. if (sd && sd->pd)
  9722. bl = &sd->pd->bl;
  9723. break;
  9724. case 3: //Get Mob Position
  9725. break; //Not supported?
  9726. case 4: //Get Homun Position
  9727. if(script_hasdata(st,6))
  9728. sd=map_nick2sd(script_getstr(st,6));
  9729. else
  9730. sd=script_rid2sd(st);
  9731. if (sd && sd->hd)
  9732. bl = &sd->hd->bl;
  9733. break;
  9734. }
  9735. if (!bl) { //No object found.
  9736. script_pushint(st,-1);
  9737. return 0;
  9738. }
  9739. x= bl->x;
  9740. y= bl->y;
  9741. safestrncpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  9742. //Set MapName$
  9743. num=st->stack->stack_data[st->start+2].u.num;
  9744. name=get_str(num&0x00ffffff);
  9745. prefix=*name;
  9746. if(not_server_variable(prefix))
  9747. sd=script_rid2sd(st);
  9748. else
  9749. sd=NULL;
  9750. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  9751. //Set MapX
  9752. num=st->stack->stack_data[st->start+3].u.num;
  9753. name=get_str(num&0x00ffffff);
  9754. prefix=*name;
  9755. if(not_server_variable(prefix))
  9756. sd=script_rid2sd(st);
  9757. else
  9758. sd=NULL;
  9759. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  9760. //Set MapY
  9761. num=st->stack->stack_data[st->start+4].u.num;
  9762. name=get_str(num&0x00ffffff);
  9763. prefix=*name;
  9764. if(not_server_variable(prefix))
  9765. sd=script_rid2sd(st);
  9766. else
  9767. sd=NULL;
  9768. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  9769. //Return Success value
  9770. script_pushint(st,0);
  9771. return 0;
  9772. }
  9773. /*==========================================
  9774. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  9775. *------------------------------------------*/
  9776. BUILDIN_FUNC(logmes)
  9777. {
  9778. const char *str;
  9779. TBL_PC* sd;
  9780. if( log_config.npc <= 0 )
  9781. return 0;
  9782. sd = script_rid2sd(st);
  9783. if( sd == NULL )
  9784. return 1;
  9785. str = script_getstr(st,2);
  9786. log_npc(sd,str);
  9787. return 0;
  9788. }
  9789. BUILDIN_FUNC(summon)
  9790. {
  9791. int _class, timeout=0;
  9792. const char *str,*event="";
  9793. TBL_PC *sd;
  9794. struct mob_data *md;
  9795. int tick = gettick();
  9796. sd=script_rid2sd(st);
  9797. if (!sd) return 0;
  9798. str =script_getstr(st,2);
  9799. _class=script_getnum(st,3);
  9800. if( script_hasdata(st,4) )
  9801. timeout=script_getnum(st,4);
  9802. if( script_hasdata(st,5) ){
  9803. event=script_getstr(st,5);
  9804. check_event(st, event);
  9805. }
  9806. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  9807. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  9808. if (md) {
  9809. md->master_id=sd->bl.id;
  9810. md->special_state.ai=1;
  9811. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  9812. mob_spawn (md); //Now it is ready for spawning.
  9813. clif_misceffect2(&md->bl,344);
  9814. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  9815. }
  9816. return 0;
  9817. }
  9818. /*==========================================
  9819. * Checks whether it is daytime/nighttime
  9820. *------------------------------------------*/
  9821. BUILDIN_FUNC(isnight)
  9822. {
  9823. script_pushint(st,(night_flag == 1));
  9824. return 0;
  9825. }
  9826. BUILDIN_FUNC(isday)
  9827. {
  9828. script_pushint(st,(night_flag == 0));
  9829. return 0;
  9830. }
  9831. /*================================================
  9832. * Check whether another item/card has been
  9833. * equipped - used for 2/15's cards patch [celest]
  9834. *------------------------------------------------*/
  9835. // leave this here, just in case
  9836. #if 0
  9837. BUILDIN_FUNC(isequipped)
  9838. {
  9839. TBL_PC *sd;
  9840. int i, j, k, id = 1;
  9841. int ret = -1;
  9842. sd = script_rid2sd(st);
  9843. for (i=0; id!=0; i++) {
  9844. int flag = 0;
  9845. FETCH (i+2, id) else id = 0;
  9846. if (id <= 0)
  9847. continue;
  9848. for (j=0; j<EQI_MAX; j++) {
  9849. int index;
  9850. index = sd->equip_index[j];
  9851. if(index < 0) continue;
  9852. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9853. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9854. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9855. if(!sd->inventory_data[index])
  9856. continue;
  9857. if(itemdb_type(id) != IT_CARD) { //Non card
  9858. if (sd->inventory_data[index]->nameid != id)
  9859. continue;
  9860. flag = 1;
  9861. break;
  9862. } else { //Card
  9863. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9864. continue;
  9865. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9866. if (sd->status.inventory[index].card[k] == id)
  9867. {
  9868. flag = 1;
  9869. break;
  9870. }
  9871. }
  9872. }
  9873. if (flag) break;
  9874. }
  9875. if (ret == -1)
  9876. ret = flag;
  9877. else
  9878. ret &= flag;
  9879. if (!ret) break;
  9880. }
  9881. script_pushint(st,ret);
  9882. return 0;
  9883. }
  9884. #endif
  9885. /*================================================
  9886. * Check how many items/cards in the list are
  9887. * equipped - used for 2/15's cards patch [celest]
  9888. *------------------------------------------------*/
  9889. BUILDIN_FUNC(isequippedcnt)
  9890. {
  9891. TBL_PC *sd;
  9892. int i, j, k, id = 1;
  9893. int ret = 0;
  9894. sd = script_rid2sd(st);
  9895. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9896. script_pushint(st,0);
  9897. return 0;
  9898. }
  9899. for (i=0; id!=0; i++) {
  9900. FETCH (i+2, id) else id = 0;
  9901. if (id <= 0)
  9902. continue;
  9903. for (j=0; j<EQI_MAX; j++) {
  9904. int index;
  9905. index = sd->equip_index[j];
  9906. if(index < 0) continue;
  9907. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9908. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9909. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9910. if(!sd->inventory_data[index])
  9911. continue;
  9912. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  9913. if (sd->inventory_data[index]->nameid == id)
  9914. ret+= sd->status.inventory[index].amount;
  9915. } else { //Count cards.
  9916. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9917. continue; //No cards
  9918. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9919. if (sd->status.inventory[index].card[k] == id)
  9920. ret++; //[Lupus]
  9921. }
  9922. }
  9923. }
  9924. }
  9925. script_pushint(st,ret);
  9926. return 0;
  9927. }
  9928. /*================================================
  9929. * Check whether another card has been
  9930. * equipped - used for 2/15's cards patch [celest]
  9931. * -- Items checked cannot be reused in another
  9932. * card set to prevent exploits
  9933. *------------------------------------------------*/
  9934. BUILDIN_FUNC(isequipped)
  9935. {
  9936. TBL_PC *sd;
  9937. int i, j, k, id = 1;
  9938. int index, flag;
  9939. int ret = -1;
  9940. //Original hash to reverse it when full check fails.
  9941. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  9942. sd = script_rid2sd(st);
  9943. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9944. script_pushint(st,0);
  9945. return 0;
  9946. }
  9947. setitem_hash = sd->setitem_hash;
  9948. setitem_hash2 = sd->setitem_hash2;
  9949. for (i=0; id!=0; i++)
  9950. {
  9951. FETCH (i+2, id) else id = 0;
  9952. if (id <= 0)
  9953. continue;
  9954. flag = 0;
  9955. for (j=0; j<EQI_MAX; j++)
  9956. {
  9957. index = sd->equip_index[j];
  9958. if(index < 0) continue;
  9959. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9960. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9961. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9962. if(!sd->inventory_data[index])
  9963. continue;
  9964. if (itemdb_type(id) != IT_CARD) {
  9965. if (sd->inventory_data[index]->nameid != id)
  9966. continue;
  9967. flag = 1;
  9968. break;
  9969. } else { //Cards
  9970. if (sd->inventory_data[index]->slot == 0 ||
  9971. itemdb_isspecial(sd->status.inventory[index].card[0]))
  9972. continue;
  9973. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  9974. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  9975. unsigned int hash = 0;
  9976. if (sd->status.inventory[index].card[k] != id)
  9977. continue;
  9978. hash = 1<<((j<5?j:j-5)*4 + k);
  9979. // check if card is already used by another set
  9980. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  9981. continue;
  9982. // We have found a match
  9983. flag = 1;
  9984. // Set hash so this card cannot be used by another
  9985. if (j<5)
  9986. sd->setitem_hash |= hash;
  9987. else
  9988. sd->setitem_hash2 |= hash;
  9989. break;
  9990. }
  9991. }
  9992. if (flag) break; //Card found
  9993. }
  9994. if (ret == -1)
  9995. ret = flag;
  9996. else
  9997. ret &= flag;
  9998. if (!ret) break;
  9999. }
  10000. if (!ret)
  10001. { //When check fails, restore original hash values. [Skotlex]
  10002. sd->setitem_hash = setitem_hash;
  10003. sd->setitem_hash2 = setitem_hash2;
  10004. }
  10005. script_pushint(st,ret);
  10006. return 0;
  10007. }
  10008. /*================================================
  10009. * Check how many given inserted cards in the CURRENT
  10010. * weapon - used for 2/15's cards patch [Lupus]
  10011. *------------------------------------------------*/
  10012. BUILDIN_FUNC(cardscnt)
  10013. {
  10014. TBL_PC *sd;
  10015. int i, k, id = 1;
  10016. int ret = 0;
  10017. int index;
  10018. sd = script_rid2sd(st);
  10019. for (i=0; id!=0; i++) {
  10020. FETCH (i+2, id) else id = 0;
  10021. if (id <= 0)
  10022. continue;
  10023. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10024. if(index < 0) continue;
  10025. if(!sd->inventory_data[index])
  10026. continue;
  10027. if(itemdb_type(id) != IT_CARD) {
  10028. if (sd->inventory_data[index]->nameid == id)
  10029. ret+= sd->status.inventory[index].amount;
  10030. } else {
  10031. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10032. continue;
  10033. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10034. if (sd->status.inventory[index].card[k] == id)
  10035. ret++;
  10036. }
  10037. }
  10038. }
  10039. script_pushint(st,ret);
  10040. // script_pushint(st,current_equip_item_index);
  10041. return 0;
  10042. }
  10043. /*=======================================================
  10044. * Returns the refined number of the current item, or an
  10045. * item with inventory index specified
  10046. *-------------------------------------------------------*/
  10047. BUILDIN_FUNC(getrefine)
  10048. {
  10049. TBL_PC *sd;
  10050. if ((sd = script_rid2sd(st))!= NULL)
  10051. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10052. else
  10053. script_pushint(st,0);
  10054. return 0;
  10055. }
  10056. /*=======================================================
  10057. * Day/Night controls
  10058. *-------------------------------------------------------*/
  10059. BUILDIN_FUNC(night)
  10060. {
  10061. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10062. return 0;
  10063. }
  10064. BUILDIN_FUNC(day)
  10065. {
  10066. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10067. return 0;
  10068. }
  10069. //=======================================================
  10070. // Unequip [Spectre]
  10071. //-------------------------------------------------------
  10072. BUILDIN_FUNC(unequip)
  10073. {
  10074. int i;
  10075. size_t num;
  10076. TBL_PC *sd;
  10077. num = script_getnum(st,2);
  10078. sd = script_rid2sd(st);
  10079. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10080. {
  10081. i = pc_checkequip(sd,equip[num-1]);
  10082. if (i >= 0)
  10083. pc_unequipitem(sd,i,2);
  10084. return 0;
  10085. }
  10086. return 0;
  10087. }
  10088. BUILDIN_FUNC(equip)
  10089. {
  10090. int nameid=0,i;
  10091. TBL_PC *sd;
  10092. struct item_data *item_data;
  10093. sd = script_rid2sd(st);
  10094. nameid=script_getnum(st,2);
  10095. if((item_data = itemdb_exists(nameid)) == NULL)
  10096. {
  10097. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10098. return 1;
  10099. }
  10100. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10101. if( i < MAX_INVENTORY )
  10102. pc_equipitem(sd,i,item_data->equip);
  10103. return 0;
  10104. }
  10105. BUILDIN_FUNC(autoequip)
  10106. {
  10107. int nameid, flag;
  10108. struct item_data *item_data;
  10109. nameid=script_getnum(st,2);
  10110. flag=script_getnum(st,3);
  10111. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10112. item_data->flag.autoequip = flag>0?1:0;
  10113. }
  10114. return 0;
  10115. }
  10116. BUILDIN_FUNC(setbattleflag)
  10117. {
  10118. const char *flag, *value;
  10119. flag = script_getstr(st,2);
  10120. value = script_getstr(st,3);
  10121. if (battle_set_value(flag, value) == 0)
  10122. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10123. else
  10124. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10125. return 0;
  10126. }
  10127. BUILDIN_FUNC(getbattleflag)
  10128. {
  10129. const char *flag;
  10130. flag = script_getstr(st,2);
  10131. script_pushint(st,battle_get_value(flag));
  10132. return 0;
  10133. }
  10134. //=======================================================
  10135. // strlen [Valaris]
  10136. //-------------------------------------------------------
  10137. BUILDIN_FUNC(getstrlen)
  10138. {
  10139. const char *str = script_getstr(st,2);
  10140. int len = (str) ? (int)strlen(str) : 0;
  10141. script_pushint(st,len);
  10142. return 0;
  10143. }
  10144. //=======================================================
  10145. // isalpha [Valaris]
  10146. //-------------------------------------------------------
  10147. BUILDIN_FUNC(charisalpha)
  10148. {
  10149. const char *str=script_getstr(st,2);
  10150. int pos=script_getnum(st,3);
  10151. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10152. script_pushint(st,val);
  10153. return 0;
  10154. }
  10155. /// Changes the display name and/or display class of the npc.
  10156. /// Returns 0 is successful, 1 if the npc does not exist.
  10157. ///
  10158. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  10159. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10160. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10161. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10162. BUILDIN_FUNC(setnpcdisplay)
  10163. {
  10164. const char* name;
  10165. const char* newname = NULL;
  10166. int class_ = -1, size = -1;
  10167. struct script_data* data;
  10168. struct npc_data* nd;
  10169. name = script_getstr(st,2);
  10170. data = script_getdata(st,3);
  10171. if( script_hasdata(st,4) )
  10172. class_ = script_getnum(st,4);
  10173. if( script_hasdata(st,5) )
  10174. size = script_getnum(st,5);
  10175. get_val(st, data);
  10176. if( data_isstring(data) )
  10177. newname = conv_str(st,data);
  10178. else if( data_isint(data) )
  10179. class_ = conv_num(st,data);
  10180. else
  10181. {
  10182. ShowError("script:setnpcdisplay: expected a string or number\n");
  10183. script_reportdata(data);
  10184. return 1;
  10185. }
  10186. nd = npc_name2id(name);
  10187. if( nd == NULL )
  10188. {// not found
  10189. script_pushint(st,1);
  10190. return 0;
  10191. }
  10192. // update npc
  10193. if( newname )
  10194. npc_setdisplayname(nd, newname);
  10195. if( size != -1 && size != nd->size )
  10196. nd->size = size;
  10197. else
  10198. size = -1;
  10199. if( class_ != -1 && nd->class_ != class_ )
  10200. npc_setclass(nd, class_);
  10201. else if( size != -1 )
  10202. { // Required to update the visual size
  10203. clif_clearunit_area(&nd->bl, 0);
  10204. clif_spawn(&nd->bl);
  10205. }
  10206. script_pushint(st,0);
  10207. return 0;
  10208. }
  10209. BUILDIN_FUNC(atoi)
  10210. {
  10211. const char *value;
  10212. value = script_getstr(st,2);
  10213. script_pushint(st,atoi(value));
  10214. return 0;
  10215. }
  10216. // case-insensitive substring search [lordalfa]
  10217. BUILDIN_FUNC(compare)
  10218. {
  10219. const char *message;
  10220. const char *cmpstring;
  10221. message = script_getstr(st,2);
  10222. cmpstring = script_getstr(st,3);
  10223. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10224. return 0;
  10225. }
  10226. // [zBuffer] List of mathematics commands --->
  10227. BUILDIN_FUNC(sqrt)
  10228. {
  10229. double i, a;
  10230. i = script_getnum(st,2);
  10231. a = sqrt(i);
  10232. script_pushint(st,(int)a);
  10233. return 0;
  10234. }
  10235. BUILDIN_FUNC(pow)
  10236. {
  10237. double i, a, b;
  10238. a = script_getnum(st,2);
  10239. b = script_getnum(st,3);
  10240. i = pow(a,b);
  10241. script_pushint(st,(int)i);
  10242. return 0;
  10243. }
  10244. BUILDIN_FUNC(distance)
  10245. {
  10246. int x0, y0, x1, y1;
  10247. x0 = script_getnum(st,2);
  10248. y0 = script_getnum(st,3);
  10249. x1 = script_getnum(st,4);
  10250. y1 = script_getnum(st,5);
  10251. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10252. return 0;
  10253. }
  10254. // <--- [zBuffer] List of mathematics commands
  10255. // [zBuffer] List of dynamic var commands --->
  10256. BUILDIN_FUNC(setd)
  10257. {
  10258. TBL_PC *sd=NULL;
  10259. char varname[100];
  10260. const char *value, *buffer;
  10261. int elem;
  10262. buffer = script_getstr(st, 2);
  10263. value = script_getstr(st, 3);
  10264. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10265. elem = 0;
  10266. if( not_server_variable(*varname) )
  10267. {
  10268. sd = script_rid2sd(st);
  10269. if( sd == NULL )
  10270. {
  10271. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10272. return 0;
  10273. }
  10274. }
  10275. if(varname[strlen(varname)-1] != '$') {
  10276. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10277. } else {
  10278. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10279. }
  10280. return 0;
  10281. }
  10282. #ifndef TXT_ONLY
  10283. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10284. {
  10285. int i, j;
  10286. TBL_PC* sd = NULL;
  10287. const char* query;
  10288. struct script_data* data;
  10289. const char* name;
  10290. int max_rows = 128;// maximum number of rows
  10291. int num_vars;
  10292. int num_cols;
  10293. // check target variables
  10294. for( i = 3; script_hasdata(st,i); ++i )
  10295. {
  10296. data = script_getdata(st, i);
  10297. if( data_isreference(data) && reference_tovariable(data) )
  10298. {// it's a variable
  10299. name = reference_getname(data);
  10300. if( not_server_variable(*name) && sd == NULL )
  10301. {// requires a player
  10302. sd = script_rid2sd(st);
  10303. if( sd == NULL )
  10304. {// no player attached
  10305. script_reportdata(data);
  10306. st->state = END;
  10307. return 1;
  10308. }
  10309. }
  10310. if( not_array_variable(*name) )
  10311. max_rows = 1;// not an array, limit to one row
  10312. }
  10313. else
  10314. {
  10315. ShowError("script:query_sql: not a variable\n");
  10316. script_reportdata(data);
  10317. st->state = END;
  10318. return 1;
  10319. }
  10320. }
  10321. num_vars = i - 3;
  10322. // Execute the query
  10323. query = script_getstr(st,2);
  10324. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10325. {
  10326. Sql_ShowDebug(handle);
  10327. script_pushint(st, 0);
  10328. return 1;
  10329. }
  10330. if( Sql_NumRows(handle) == 0 )
  10331. {// No data received
  10332. Sql_FreeResult(handle);
  10333. script_pushint(st, 0);
  10334. return 0;
  10335. }
  10336. // Count the number of columns to store
  10337. num_cols = Sql_NumColumns(handle);
  10338. if( num_vars < num_cols )
  10339. {
  10340. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10341. script_reportsrc(st);
  10342. }
  10343. else if( num_vars > num_cols )
  10344. {
  10345. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10346. script_reportsrc(st);
  10347. }
  10348. // Store data
  10349. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10350. {
  10351. for( j = 0; j < num_vars; ++j )
  10352. {
  10353. char* str = NULL;
  10354. if( j < num_cols )
  10355. Sql_GetData(handle, j, &str, NULL);
  10356. data = script_getdata(st, j+3);
  10357. name = reference_getname(data);
  10358. if( is_string_variable(name) )
  10359. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10360. else
  10361. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10362. }
  10363. }
  10364. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10365. {
  10366. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10367. script_reportsrc(st);
  10368. }
  10369. // Free data
  10370. Sql_FreeResult(handle);
  10371. script_pushint(st, i);
  10372. return 0;
  10373. }
  10374. #endif
  10375. BUILDIN_FUNC(query_sql)
  10376. {
  10377. #ifndef TXT_ONLY
  10378. return buildin_query_sql_sub(st, mmysql_handle);
  10379. #else
  10380. //for TXT version, we always return -1
  10381. script_pushint(st,-1);
  10382. return 0;
  10383. #endif
  10384. }
  10385. BUILDIN_FUNC(query_logsql)
  10386. {
  10387. #ifndef TXT_ONLY
  10388. return buildin_query_sql_sub(st, logmysql_handle);
  10389. #else
  10390. //for TXT version, we always return -1
  10391. script_pushint(st,-1);
  10392. return 0;
  10393. #endif
  10394. }
  10395. //Allows escaping of a given string.
  10396. BUILDIN_FUNC(escape_sql)
  10397. {
  10398. const char *str;
  10399. char *esc_str;
  10400. size_t len;
  10401. str = script_getstr(st,2);
  10402. len = strlen(str);
  10403. esc_str = (char*)aMallocA(len*2+1);
  10404. #if defined(TXT_ONLY)
  10405. jstrescapecpy(esc_str, str);
  10406. #else
  10407. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10408. #endif
  10409. script_pushstr(st, esc_str);
  10410. return 0;
  10411. }
  10412. BUILDIN_FUNC(getd)
  10413. {
  10414. char varname[100];
  10415. const char *buffer;
  10416. int elem;
  10417. buffer = script_getstr(st, 2);
  10418. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10419. elem = 0;
  10420. // Push the 'pointer' so it's more flexible [Lance]
  10421. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10422. return 0;
  10423. }
  10424. // <--- [zBuffer] List of dynamic var commands
  10425. // Pet stat [Lance]
  10426. BUILDIN_FUNC(petstat)
  10427. {
  10428. TBL_PC *sd = NULL;
  10429. struct pet_data *pd;
  10430. int flag = script_getnum(st,2);
  10431. sd = script_rid2sd(st);
  10432. if(!sd || !sd->status.pet_id || !sd->pd){
  10433. if(flag == 2)
  10434. script_pushconststr(st, "");
  10435. else
  10436. script_pushint(st,0);
  10437. return 0;
  10438. }
  10439. pd = sd->pd;
  10440. switch(flag){
  10441. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10442. case 2: script_pushstrcopy(st, pd->pet.name); break;
  10443. case 3: script_pushint(st,(int)pd->pet.level); break;
  10444. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10445. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10446. default:
  10447. script_pushint(st,0);
  10448. break;
  10449. }
  10450. return 0;
  10451. }
  10452. BUILDIN_FUNC(callshop)
  10453. {
  10454. TBL_PC *sd = NULL;
  10455. struct npc_data *nd;
  10456. const char *shopname;
  10457. int flag = 0;
  10458. sd = script_rid2sd(st);
  10459. if (!sd) {
  10460. script_pushint(st,0);
  10461. return 0;
  10462. }
  10463. shopname = script_getstr(st, 2);
  10464. if( script_hasdata(st,3) )
  10465. flag = script_getnum(st,3);
  10466. nd = npc_name2id(shopname);
  10467. if( !nd || nd->bl.type != BL_NPC || (nd->subtype != SHOP && nd->subtype != CASHSHOP) )
  10468. {
  10469. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  10470. script_pushint(st,0);
  10471. return 1;
  10472. }
  10473. if( nd->subtype == SHOP )
  10474. {
  10475. switch( flag )
  10476. {
  10477. case 1: npc_buysellsel(sd,nd->bl.id,0); break; //Buy window
  10478. case 2: npc_buysellsel(sd,nd->bl.id,1); break; //Sell window
  10479. default: clif_npcbuysell(sd,nd->bl.id); break; //Show menu
  10480. }
  10481. }
  10482. else
  10483. clif_cashshop_show(sd, nd);
  10484. sd->npc_shopid = nd->bl.id;
  10485. script_pushint(st,1);
  10486. return 0;
  10487. }
  10488. BUILDIN_FUNC(npcshopitem)
  10489. {
  10490. const char* npcname = script_getstr(st, 2);
  10491. struct npc_data* nd = npc_name2id(npcname);
  10492. int n, i;
  10493. int amount;
  10494. if( !nd || nd->subtype != SHOP )
  10495. { //Not found.
  10496. script_pushint(st,0);
  10497. return 0;
  10498. }
  10499. // get the count of new entries
  10500. amount = (script_lastdata(st)-2)/2;
  10501. // generate new shop item list
  10502. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  10503. for( n = 0, i = 3; n < amount; n++, i+=2 )
  10504. {
  10505. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10506. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10507. }
  10508. nd->u.shop.count = n;
  10509. script_pushint(st,1);
  10510. return 0;
  10511. }
  10512. BUILDIN_FUNC(npcshopadditem)
  10513. {
  10514. const char* npcname = script_getstr(st,2);
  10515. struct npc_data* nd = npc_name2id(npcname);
  10516. int n, i;
  10517. int amount;
  10518. if( !nd || nd->subtype != SHOP )
  10519. { //Not found.
  10520. script_pushint(st,0);
  10521. return 0;
  10522. }
  10523. // get the count of new entries
  10524. amount = (script_lastdata(st)-2)/2;
  10525. // append new items to existing shop item list
  10526. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  10527. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  10528. {
  10529. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10530. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10531. }
  10532. nd->u.shop.count = n;
  10533. script_pushint(st,1);
  10534. return 0;
  10535. }
  10536. BUILDIN_FUNC(npcshopdelitem)
  10537. {
  10538. const char* npcname = script_getstr(st,2);
  10539. struct npc_data* nd = npc_name2id(npcname);
  10540. int n, i;
  10541. int amount;
  10542. int size;
  10543. if( !nd || nd->subtype != SHOP )
  10544. { //Not found.
  10545. script_pushint(st,0);
  10546. return 0;
  10547. }
  10548. amount = script_lastdata(st)-2;
  10549. size = nd->u.shop.count;
  10550. // remove specified items from the shop item list
  10551. for( i = 3; i < 3 + amount; i++ )
  10552. {
  10553. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
  10554. if( n < size )
  10555. {
  10556. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  10557. size--;
  10558. }
  10559. }
  10560. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  10561. nd->u.shop.count = size;
  10562. script_pushint(st,1);
  10563. return 0;
  10564. }
  10565. //Sets a script to attach to a shop npc.
  10566. BUILDIN_FUNC(npcshopattach)
  10567. {
  10568. const char* npcname = script_getstr(st,2);
  10569. struct npc_data* nd = npc_name2id(npcname);
  10570. int flag = 1;
  10571. if( script_hasdata(st,3) )
  10572. flag = script_getnum(st,3);
  10573. if( !nd || nd->subtype != SHOP )
  10574. { //Not found.
  10575. script_pushint(st,0);
  10576. return 0;
  10577. }
  10578. if (flag)
  10579. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10580. else
  10581. nd->master_nd = NULL;
  10582. script_pushint(st,1);
  10583. return 0;
  10584. }
  10585. /*==========================================
  10586. * Returns some values of an item [Lupus]
  10587. * Price, Weight, etc...
  10588. setitemscript(itemID,"{new item bonus script}",[n]);
  10589. Where n:
  10590. 0 - script
  10591. 1 - Equip script
  10592. 2 - Unequip script
  10593. *------------------------------------------*/
  10594. BUILDIN_FUNC(setitemscript)
  10595. {
  10596. int item_id,n=0;
  10597. const char *script;
  10598. struct item_data *i_data;
  10599. struct script_code **dstscript;
  10600. item_id = script_getnum(st,2);
  10601. script = script_getstr(st,3);
  10602. if( script_hasdata(st,4) )
  10603. n=script_getnum(st,4);
  10604. i_data = itemdb_exists(item_id);
  10605. if (!i_data || script==NULL || script[0]!='{') {
  10606. script_pushint(st,0);
  10607. return 0;
  10608. }
  10609. switch (n) {
  10610. case 2:
  10611. dstscript = &i_data->unequip_script;
  10612. break;
  10613. case 1:
  10614. dstscript = &i_data->equip_script;
  10615. break;
  10616. default:
  10617. dstscript = &i_data->script;
  10618. break;
  10619. }
  10620. if(*dstscript)
  10621. script_free_code(*dstscript);
  10622. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  10623. script_pushint(st,1);
  10624. return 0;
  10625. }
  10626. /* Work In Progress [Lupus]
  10627. BUILDIN_FUNC(addmonsterdrop)
  10628. {
  10629. int class_,item_id,chance;
  10630. class_=script_getnum(st,2);
  10631. item_id=script_getnum(st,3);
  10632. chance=script_getnum(st,4);
  10633. if(class_>1000 && item_id>500 && chance>0) {
  10634. script_pushint(st,1);
  10635. } else {
  10636. script_pushint(st,0);
  10637. }
  10638. }
  10639. BUILDIN_FUNC(delmonsterdrop)
  10640. {
  10641. int class_,item_id;
  10642. class_=script_getnum(st,2);
  10643. item_id=script_getnum(st,3);
  10644. if(class_>1000 && item_id>500) {
  10645. script_pushint(st,1);
  10646. } else {
  10647. script_pushint(st,0);
  10648. }
  10649. }
  10650. */
  10651. /*==========================================
  10652. * Returns some values of a monster [Lupus]
  10653. * Name, Level, race, size, etc...
  10654. getmonsterinfo(monsterID,queryIndex);
  10655. *------------------------------------------*/
  10656. BUILDIN_FUNC(getmonsterinfo)
  10657. {
  10658. struct mob_db *mob;
  10659. int mob_id;
  10660. mob_id = script_getnum(st,2);
  10661. if (!mobdb_checkid(mob_id)) {
  10662. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  10663. if ( !script_getnum(st,3) ) //requested a string
  10664. script_pushconststr(st,"null");
  10665. else
  10666. script_pushint(st,-1);
  10667. return -1;
  10668. }
  10669. mob = mob_db(mob_id);
  10670. switch ( script_getnum(st,3) ) {
  10671. case 0: script_pushstrcopy(st,mob->jname); break;
  10672. case 1: script_pushint(st,mob->lv); break;
  10673. case 2: script_pushint(st,mob->status.max_hp); break;
  10674. case 3: script_pushint(st,mob->base_exp); break;
  10675. case 4: script_pushint(st,mob->job_exp); break;
  10676. case 5: script_pushint(st,mob->status.rhw.atk); break;
  10677. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  10678. case 7: script_pushint(st,mob->status.def); break;
  10679. case 8: script_pushint(st,mob->status.mdef); break;
  10680. case 9: script_pushint(st,mob->status.str); break;
  10681. case 10: script_pushint(st,mob->status.agi); break;
  10682. case 11: script_pushint(st,mob->status.vit); break;
  10683. case 12: script_pushint(st,mob->status.int_); break;
  10684. case 13: script_pushint(st,mob->status.dex); break;
  10685. case 14: script_pushint(st,mob->status.luk); break;
  10686. case 15: script_pushint(st,mob->status.rhw.range); break;
  10687. case 16: script_pushint(st,mob->range2); break;
  10688. case 17: script_pushint(st,mob->range3); break;
  10689. case 18: script_pushint(st,mob->status.size); break;
  10690. case 19: script_pushint(st,mob->status.race); break;
  10691. case 20: script_pushint(st,mob->status.def_ele); break;
  10692. case 21: script_pushint(st,mob->status.mode); break;
  10693. default: script_pushint(st,-1); //wrong Index
  10694. }
  10695. return 0;
  10696. }
  10697. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  10698. {
  10699. TBL_PC *sd = NULL;
  10700. if(script_hasdata(st,2))
  10701. sd = map_nick2sd(script_getstr(st,2));
  10702. else
  10703. sd = script_rid2sd(st);
  10704. if(sd)
  10705. script_pushint(st,(sd->vender_id != 0));
  10706. else
  10707. script_pushint(st,0);
  10708. return 0;
  10709. }
  10710. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  10711. {
  10712. TBL_PC *sd = NULL;
  10713. if(script_hasdata(st,2))
  10714. sd = map_nick2sd(script_getstr(st,2));
  10715. else
  10716. sd = script_rid2sd(st);
  10717. if(sd)
  10718. script_pushint(st,(sd->chatID != 0));
  10719. else
  10720. script_pushint(st,0);
  10721. return 0;
  10722. }
  10723. int axtoi(const char *hexStg)
  10724. {
  10725. int n = 0; // position in string
  10726. int m = 0; // position in digit[] to shift
  10727. int count; // loop index
  10728. int intValue = 0; // integer value of hex string
  10729. int digit[11]; // hold values to convert
  10730. while (n < 10) {
  10731. if (hexStg[n]=='\0')
  10732. break;
  10733. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  10734. digit[n] = hexStg[n] & 0x0f; //convert to int
  10735. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  10736. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10737. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  10738. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10739. else break;
  10740. n++;
  10741. }
  10742. count = n;
  10743. m = n - 1;
  10744. n = 0;
  10745. while(n < count) {
  10746. // digit[n] is value of hex digit at position n
  10747. // (m << 2) is the number of positions to shift
  10748. // OR the bits into return value
  10749. intValue = intValue | (digit[n] << (m << 2));
  10750. m--; // adjust the position to set
  10751. n++; // next digit to process
  10752. }
  10753. return (intValue);
  10754. }
  10755. // [Lance] Hex string to integer converter
  10756. BUILDIN_FUNC(axtoi)
  10757. {
  10758. const char *hex = script_getstr(st,2);
  10759. script_pushint(st,axtoi(hex));
  10760. return 0;
  10761. }
  10762. // [zBuffer] List of player cont commands --->
  10763. BUILDIN_FUNC(rid2name)
  10764. {
  10765. struct block_list *bl = NULL;
  10766. int rid = script_getnum(st,2);
  10767. if((bl = map_id2bl(rid)))
  10768. {
  10769. switch(bl->type) {
  10770. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  10771. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  10772. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  10773. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  10774. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  10775. default:
  10776. ShowError("buildin_rid2name: BL type unknown.\n");
  10777. script_pushconststr(st,"");
  10778. break;
  10779. }
  10780. } else {
  10781. ShowError("buildin_rid2name: invalid RID\n");
  10782. script_pushconststr(st,"(null)");
  10783. }
  10784. return 0;
  10785. }
  10786. BUILDIN_FUNC(pcblockmove)
  10787. {
  10788. int id, flag;
  10789. TBL_PC *sd = NULL;
  10790. id = script_getnum(st,2);
  10791. flag = script_getnum(st,3);
  10792. if(id)
  10793. sd = map_id2sd(id);
  10794. else
  10795. sd = script_rid2sd(st);
  10796. if(sd)
  10797. sd->state.blockedmove = flag > 0;
  10798. return 0;
  10799. }
  10800. BUILDIN_FUNC(pcfollow)
  10801. {
  10802. int id, targetid;
  10803. TBL_PC *sd = NULL;
  10804. id = script_getnum(st,2);
  10805. targetid = script_getnum(st,3);
  10806. if(id)
  10807. sd = map_id2sd(id);
  10808. else
  10809. sd = script_rid2sd(st);
  10810. if(sd)
  10811. pc_follow(sd, targetid);
  10812. return 0;
  10813. }
  10814. BUILDIN_FUNC(pcstopfollow)
  10815. {
  10816. int id;
  10817. TBL_PC *sd = NULL;
  10818. id = script_getnum(st,2);
  10819. if(id)
  10820. sd = map_id2sd(id);
  10821. else
  10822. sd = script_rid2sd(st);
  10823. if(sd)
  10824. pc_stop_following(sd);
  10825. return 0;
  10826. }
  10827. // <--- [zBuffer] List of player cont commands
  10828. // [zBuffer] List of mob control commands --->
  10829. //## TODO always return if the request/whatever was successfull [FlavioJS]
  10830. /// Makes the unit walk to target position or map
  10831. /// Returns if it was successfull
  10832. ///
  10833. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  10834. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  10835. BUILDIN_FUNC(unitwalk)
  10836. {
  10837. struct block_list* bl;
  10838. bl = map_id2bl(script_getnum(st,2));
  10839. if( bl == NULL )
  10840. {
  10841. script_pushint(st, 0);
  10842. }
  10843. else if( script_hasdata(st,4) )
  10844. {
  10845. int x = script_getnum(st,3);
  10846. int y = script_getnum(st,4);
  10847. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  10848. }
  10849. else
  10850. {
  10851. int map_id = script_getnum(st,3);
  10852. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  10853. }
  10854. return 0;
  10855. }
  10856. /// Kills the unit
  10857. ///
  10858. /// unitkill <unit_id>;
  10859. BUILDIN_FUNC(unitkill)
  10860. {
  10861. struct block_list* bl = map_id2bl(script_getnum(st,2));
  10862. if( bl != NULL )
  10863. status_kill(bl);
  10864. return 0;
  10865. }
  10866. /// Warps the unit to the target position in the target map
  10867. /// Returns if it was successfull
  10868. ///
  10869. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  10870. BUILDIN_FUNC(unitwarp)
  10871. {
  10872. int unit_id;
  10873. int map;
  10874. short x;
  10875. short y;
  10876. struct block_list* bl;
  10877. unit_id = script_getnum(st,2);
  10878. map = map_mapname2mapid(script_getstr(st, 3));
  10879. x = (short)script_getnum(st,4);
  10880. y = (short)script_getnum(st,5);
  10881. bl = map_id2bl(unit_id);
  10882. if( map >= 0 && bl != NULL )
  10883. script_pushint(st, unit_warp(bl,map,x,y,0));
  10884. else
  10885. script_pushint(st, 0);
  10886. return 0;
  10887. }
  10888. /// Makes the unit attack the target.
  10889. /// If the unit is a player and <action type> is not 0, it does a continuous
  10890. /// attack instead of a single attack.
  10891. /// Returns if the request was successfull.
  10892. ///
  10893. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  10894. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  10895. BUILDIN_FUNC(unitattack)
  10896. {
  10897. struct block_list* unit_bl;
  10898. struct block_list* target_bl = NULL;
  10899. struct script_data* data;
  10900. int actiontype = 0;
  10901. // get unit
  10902. unit_bl = map_id2bl(script_getnum(st,2));
  10903. if( unit_bl == NULL ) {
  10904. script_pushint(st, 0);
  10905. return 0;
  10906. }
  10907. data = script_getdata(st, 3);
  10908. get_val(st, data);
  10909. if( data_isstring(data) )
  10910. {
  10911. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  10912. if( sd != NULL )
  10913. target_bl = &sd->bl;
  10914. } else
  10915. target_bl = map_id2bl(conv_num(st, data));
  10916. // request the attack
  10917. if( target_bl == NULL )
  10918. {
  10919. script_pushint(st, 0);
  10920. return 0;
  10921. }
  10922. // get actiontype
  10923. if( script_hasdata(st,4) )
  10924. actiontype = script_getnum(st,4);
  10925. switch( unit_bl->type )
  10926. {
  10927. case BL_PC:
  10928. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  10929. script_pushint(st, 1);
  10930. return 0;
  10931. case BL_MOB:
  10932. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  10933. break;
  10934. case BL_PET:
  10935. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  10936. break;
  10937. default:
  10938. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  10939. script_pushint(st, 0);
  10940. return 1;
  10941. }
  10942. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  10943. return 0;
  10944. }
  10945. /// Makes the unit stop attacking and moving
  10946. ///
  10947. /// unitstop <unit_id>;
  10948. BUILDIN_FUNC(unitstop)
  10949. {
  10950. int unit_id;
  10951. struct block_list* bl;
  10952. unit_id = script_getnum(st,2);
  10953. bl = map_id2bl(unit_id);
  10954. if( bl != NULL )
  10955. {
  10956. unit_stop_attack(bl);
  10957. unit_stop_walking(bl,0);
  10958. if( bl->type == BL_MOB )
  10959. ((TBL_MOB*)bl)->target_id = 0;
  10960. }
  10961. return 0;
  10962. }
  10963. /// Makes the unit say the message
  10964. ///
  10965. /// unittalk <unit_id>,"<message>";
  10966. BUILDIN_FUNC(unittalk)
  10967. {
  10968. int unit_id;
  10969. const char* message;
  10970. struct block_list* bl;
  10971. unit_id = script_getnum(st,2);
  10972. message = script_getstr(st, 3);
  10973. bl = map_id2bl(unit_id);
  10974. if( bl != NULL )
  10975. {
  10976. struct StringBuf sbuf;
  10977. StringBuf_Init(&sbuf);
  10978. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  10979. clif_message(bl, StringBuf_Value(&sbuf));
  10980. if( bl->type == BL_PC )
  10981. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  10982. StringBuf_Destroy(&sbuf);
  10983. }
  10984. return 0;
  10985. }
  10986. /// Makes the unit do an emotion
  10987. ///
  10988. /// unitemote <unit_id>,<emotion>;
  10989. ///
  10990. /// @see e_* in const.txt
  10991. BUILDIN_FUNC(unitemote)
  10992. {
  10993. int unit_id;
  10994. int emotion;
  10995. struct block_list* bl;
  10996. unit_id = script_getnum(st,2);
  10997. emotion = script_getnum(st,3);
  10998. bl = map_id2bl(unit_id);
  10999. if( bl != NULL )
  11000. clif_emotion(bl, emotion);
  11001. return 0;
  11002. }
  11003. /// Makes the unit cast the skill on the target or self if no target is specified
  11004. ///
  11005. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11006. /// unitskilluseid <unit_id>,"<skill name>",<skill_lv>{,<target_id>};
  11007. BUILDIN_FUNC(unitskilluseid)
  11008. {
  11009. int unit_id;
  11010. int skill_id;
  11011. int skill_lv;
  11012. int target_id;
  11013. struct block_list* bl;
  11014. unit_id = script_getnum(st,2);
  11015. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11016. skill_lv = script_getnum(st,4);
  11017. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11018. bl = map_id2bl(unit_id);
  11019. if( bl != NULL )
  11020. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11021. return 0;
  11022. }
  11023. /// Makes the unit cast the skill on the target position.
  11024. ///
  11025. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11026. /// unitskillusepos <unit_id>,"<skill name>",<skill_lv>,<target_x>,<target_y>;
  11027. BUILDIN_FUNC(unitskillusepos)
  11028. {
  11029. int unit_id;
  11030. int skill_id;
  11031. int skill_lv;
  11032. int skill_x;
  11033. int skill_y;
  11034. struct block_list* bl;
  11035. unit_id = script_getnum(st,2);
  11036. skill_id = ( script_isstring(st,3) ? skill_name2id(script_getstr(st,3)) : script_getnum(st,3) );
  11037. skill_lv = script_getnum(st,4);
  11038. skill_x = script_getnum(st,5);
  11039. skill_y = script_getnum(st,6);
  11040. bl = map_id2bl(unit_id);
  11041. if( bl != NULL )
  11042. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11043. return 0;
  11044. }
  11045. // <--- [zBuffer] List of mob control commands
  11046. /// Pauses the execution of the script, detaching the player
  11047. ///
  11048. /// sleep <mili seconds>;
  11049. BUILDIN_FUNC(sleep)
  11050. {
  11051. int ticks;
  11052. TBL_PC* sd;
  11053. ticks = script_getnum(st,2);
  11054. sd = map_id2sd(st->rid);
  11055. // detach the player
  11056. if( sd && sd->npc_id == st->oid )
  11057. {
  11058. sd->npc_id = 0;
  11059. }
  11060. st->rid = 0;
  11061. if( ticks <= 0 )
  11062. {// do nothing
  11063. }
  11064. else if( st->sleep.tick == 0 )
  11065. {// sleep for the target amount of time
  11066. st->state = RERUNLINE;
  11067. st->sleep.tick = ticks;
  11068. }
  11069. else
  11070. {// sleep time is over
  11071. st->sleep.tick = 0;
  11072. }
  11073. return 0;
  11074. }
  11075. /// Pauses the execution of the script, keeping the player attached
  11076. /// Returns if a player is still attached
  11077. ///
  11078. /// sleep2(<mili secconds>) -> <bool>
  11079. BUILDIN_FUNC(sleep2)
  11080. {
  11081. int ticks;
  11082. ticks = script_getnum(st,2);
  11083. if( ticks <= 0 )
  11084. {// do nothing
  11085. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11086. }
  11087. else if( !st->sleep.tick )
  11088. {// sleep for the target amount of time
  11089. st->state = RERUNLINE;
  11090. st->sleep.tick = ticks;
  11091. }
  11092. else
  11093. {// sleep time is over
  11094. st->sleep.tick = 0;
  11095. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11096. }
  11097. return 0;
  11098. }
  11099. /// Awakes all the sleep timers of the target npc
  11100. ///
  11101. /// awake "<npc name>";
  11102. BUILDIN_FUNC(awake)
  11103. {
  11104. struct npc_data* nd;
  11105. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11106. nd = npc_name2id(script_getstr(st, 2));
  11107. if( nd == NULL )
  11108. return 0;
  11109. while( node )
  11110. {
  11111. if( (int)node->key == nd->bl.id )
  11112. {// sleep timer for the npc
  11113. struct script_state* tst = (struct script_state*)node->data;
  11114. TBL_PC* sd = map_id2sd(tst->rid);
  11115. if( tst->sleep.timer == -1 )
  11116. {// already awake ???
  11117. node = node->next;
  11118. continue;
  11119. }
  11120. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11121. {// char not online anymore / another char of the same account is online - Cancel execution
  11122. tst->state = END;
  11123. tst->rid = 0;
  11124. }
  11125. delete_timer(tst->sleep.timer, run_script_timer);
  11126. node = script_erase_sleepdb(node);
  11127. tst->sleep.timer = INVALID_TIMER;
  11128. tst->sleep.tick = 0;
  11129. run_script_main(tst);
  11130. }
  11131. else
  11132. {
  11133. node = node->next;
  11134. }
  11135. }
  11136. return 0;
  11137. }
  11138. /// Returns a reference to a variable of the target NPC.
  11139. /// Returns 0 if an error occurs.
  11140. ///
  11141. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11142. BUILDIN_FUNC(getvariableofnpc)
  11143. {
  11144. struct script_data* data;
  11145. const char* name;
  11146. struct npc_data* nd;
  11147. data = script_getdata(st,2);
  11148. if( !data_isreference(data) )
  11149. {// Not a reference (aka varaible name)
  11150. ShowError("script:getvariableofnpc: not a variable\n");
  11151. script_reportdata(data);
  11152. script_pushnil(st);
  11153. st->state = END;
  11154. return 1;
  11155. }
  11156. name = reference_getname(data);
  11157. if( *name != '.' || name[1] == '@' )
  11158. {// not a npc variable
  11159. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11160. script_reportdata(data);
  11161. script_pushnil(st);
  11162. st->state = END;
  11163. return 1;
  11164. }
  11165. nd = npc_name2id(script_getstr(st,3));
  11166. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11167. {// NPC not found or has no script
  11168. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11169. script_pushnil(st);
  11170. st->state = END;
  11171. return 1;
  11172. }
  11173. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11174. return 0;
  11175. }
  11176. /// Opens a warp portal.
  11177. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11178. ///
  11179. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11180. ///
  11181. /// @author blackhole89
  11182. BUILDIN_FUNC(warpportal)
  11183. {
  11184. int spx;
  11185. int spy;
  11186. unsigned short mapindex;
  11187. int tpx;
  11188. int tpy;
  11189. struct skill_unit_group* group;
  11190. struct block_list* bl;
  11191. bl = map_id2bl(st->oid);
  11192. if( bl == NULL )
  11193. {
  11194. ShowError("script:warpportal: npc is needed\n");
  11195. return 1;
  11196. }
  11197. spx = script_getnum(st,2);
  11198. spy = script_getnum(st,3);
  11199. mapindex = mapindex_name2id(script_getstr(st, 4));
  11200. tpx = script_getnum(st,5);
  11201. tpy = script_getnum(st,6);
  11202. if( mapindex == 0 )
  11203. return 0;// map not found
  11204. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11205. if( group == NULL )
  11206. return 0;// failed
  11207. group->val2 = (tpx<<16) | tpy;
  11208. group->val3 = mapindex;
  11209. return 0;
  11210. }
  11211. BUILDIN_FUNC(openmail)
  11212. {
  11213. TBL_PC* sd;
  11214. sd = script_rid2sd(st);
  11215. if( sd == NULL )
  11216. return 0;
  11217. #ifndef TXT_ONLY
  11218. mail_openmail(sd);
  11219. #endif
  11220. return 0;
  11221. }
  11222. BUILDIN_FUNC(openauction)
  11223. {
  11224. TBL_PC* sd;
  11225. sd = script_rid2sd(st);
  11226. if( sd == NULL )
  11227. return 0;
  11228. #ifndef TXT_ONLY
  11229. clif_Auction_openwindow(sd);
  11230. #endif
  11231. return 0;
  11232. }
  11233. /// Retrieves the value of the specified flag of the specified cell.
  11234. ///
  11235. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  11236. ///
  11237. /// @see cell_chk* constants in const.txt for the types
  11238. BUILDIN_FUNC(checkcell)
  11239. {
  11240. int m = map_mapname2mapid(script_getstr(st,2));
  11241. int x = script_getnum(st,3);
  11242. int y = script_getnum(st,4);
  11243. cell_chk type = (cell_chk)script_getnum(st,5);
  11244. script_pushint(st, map_getcell(m, x, y, type));
  11245. return 0;
  11246. }
  11247. /// Modifies flags of cells in the specified area.
  11248. ///
  11249. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11250. ///
  11251. /// @see cell_* constants in const.txt for the types
  11252. BUILDIN_FUNC(setcell)
  11253. {
  11254. int m = map_mapname2mapid(script_getstr(st,2));
  11255. int x1 = script_getnum(st,3);
  11256. int y1 = script_getnum(st,4);
  11257. int x2 = script_getnum(st,5);
  11258. int y2 = script_getnum(st,6);
  11259. cell_t type = (cell_t)script_getnum(st,7);
  11260. bool flag = (bool)script_getnum(st,8);
  11261. int x,y;
  11262. if( x1 > x2 ) swap(x1,x2);
  11263. if( y1 > y2 ) swap(y1,y2);
  11264. for( y = y1; y <= y2; ++y )
  11265. for( x = x1; x <= x2; ++x )
  11266. map_setcell(m, x, y, type, flag);
  11267. return 0;
  11268. }
  11269. /******************
  11270. Questlog script commands
  11271. *******************/
  11272. BUILDIN_FUNC(getquest)
  11273. {
  11274. TBL_PC * sd = script_rid2sd(st);
  11275. struct quest qd;
  11276. int i, count = 0;
  11277. char * temp;
  11278. memset(&qd, 0, sizeof(struct quest));
  11279. qd.quest_id = script_getnum(st, 2);
  11280. qd.time = script_getnum(st, 3);
  11281. qd.state = Q_ACTIVE;
  11282. for(i=0; i<(script_lastdata(st)-3) && (i/2) < MAX_QUEST_OBJECTIVES; i+=2)
  11283. {
  11284. temp = (char*)script_getstr(st, i+4);
  11285. memcpy(&qd.objectives[i/2].name, temp, NAME_LENGTH);
  11286. temp = NULL;
  11287. qd.objectives[i/2].count = script_getnum(st, i+5);
  11288. count++;
  11289. }
  11290. qd.num_objectives = count;
  11291. quest_add(sd, &qd);
  11292. return 0;
  11293. }
  11294. BUILDIN_FUNC(deletequest)
  11295. {
  11296. TBL_PC * sd = script_rid2sd(st);
  11297. int qid = script_getnum(st, 2);
  11298. quest_delete(sd, qid);
  11299. return 0;
  11300. }
  11301. BUILDIN_FUNC(setquestobjective)
  11302. {
  11303. TBL_PC * sd = script_rid2sd(st);
  11304. int qid = script_getnum(st, 2);
  11305. int num = script_getnum(st, 3);
  11306. const char * str = script_getstr(st, 4);
  11307. int count = script_getnum(st, 5);
  11308. quest_update_objective(sd, qid, num, str, count);
  11309. return 0;
  11310. }
  11311. BUILDIN_FUNC(hasquest)
  11312. {
  11313. TBL_PC * sd = script_rid2sd(st);
  11314. int qid = script_getnum(st, 2);
  11315. script_pushint(st, quest_has_quest(sd, qid)?1:0);
  11316. return 0;
  11317. }
  11318. BUILDIN_FUNC(setqueststatus)
  11319. {
  11320. TBL_PC * sd = script_rid2sd(st);
  11321. int qid = script_getnum(st, 2);
  11322. bool active = script_getnum(st, 3)?true:false;
  11323. quest_update_status(sd, qid, active);
  11324. return 0;
  11325. }
  11326. // declarations that were supposed to be exported from npc_chat.c
  11327. #ifdef PCRE_SUPPORT
  11328. BUILDIN_FUNC(defpattern);
  11329. BUILDIN_FUNC(activatepset);
  11330. BUILDIN_FUNC(deactivatepset);
  11331. BUILDIN_FUNC(deletepset);
  11332. #endif
  11333. struct script_function buildin_func[] = {
  11334. // NPC interaction
  11335. BUILDIN_DEF(mes,"s"),
  11336. BUILDIN_DEF(next,""),
  11337. BUILDIN_DEF(close,""),
  11338. BUILDIN_DEF(close2,""),
  11339. BUILDIN_DEF(menu,"sl*"),
  11340. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  11341. BUILDIN_DEF(prompt,"s*"),
  11342. //
  11343. BUILDIN_DEF(goto,"l"),
  11344. BUILDIN_DEF(callsub,"i*"),
  11345. BUILDIN_DEF(callfunc,"s*"),
  11346. BUILDIN_DEF(return,"?"),
  11347. BUILDIN_DEF(getarg,"i?"),
  11348. BUILDIN_DEF(jobchange,"i*"),
  11349. BUILDIN_DEF(jobname,"i"),
  11350. BUILDIN_DEF(input,"v??"),
  11351. BUILDIN_DEF(warp,"sii"),
  11352. BUILDIN_DEF(areawarp,"siiiisii"),
  11353. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  11354. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  11355. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  11356. BUILDIN_DEF(setlook,"ii"),
  11357. BUILDIN_DEF(changelook,"ii"), // Simulates but don't Store it
  11358. BUILDIN_DEF(set,"ii"),
  11359. BUILDIN_DEF(setarray,"rv*"),
  11360. BUILDIN_DEF(cleararray,"rvi"),
  11361. BUILDIN_DEF(copyarray,"rri"),
  11362. BUILDIN_DEF(getarraysize,"r"),
  11363. BUILDIN_DEF(deletearray,"r?"),
  11364. BUILDIN_DEF(getelementofarray,"ri"),
  11365. BUILDIN_DEF(getitem,"vi?"),
  11366. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  11367. BUILDIN_DEF(getnameditem,"is"),
  11368. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  11369. BUILDIN_DEF(makeitem,"iisii"),
  11370. BUILDIN_DEF(delitem,"ii?"),
  11371. BUILDIN_DEF(delitem2,"iiiiiiiii?"),
  11372. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  11373. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  11374. BUILDIN_DEF(cutin,"si"),
  11375. BUILDIN_DEF(viewpoint,"iiiii"),
  11376. BUILDIN_DEF(heal,"ii"),
  11377. BUILDIN_DEF(itemheal,"ii"),
  11378. BUILDIN_DEF(percentheal,"ii"),
  11379. BUILDIN_DEF(rand,"i?"),
  11380. BUILDIN_DEF(countitem,"i"),
  11381. BUILDIN_DEF(countitem2,"iiiiiiii"),
  11382. BUILDIN_DEF(checkweight,"ii"),
  11383. BUILDIN_DEF(readparam,"i*"),
  11384. BUILDIN_DEF(getcharid,"i*"),
  11385. BUILDIN_DEF(getpartyname,"i"),
  11386. BUILDIN_DEF(getpartymember,"i*"),
  11387. BUILDIN_DEF(getpartyleader,"i?"),
  11388. BUILDIN_DEF(getguildname,"i"),
  11389. BUILDIN_DEF(getguildmaster,"i"),
  11390. BUILDIN_DEF(getguildmasterid,"i"),
  11391. BUILDIN_DEF(strcharinfo,"i"),
  11392. BUILDIN_DEF(strnpcinfo,"i"),
  11393. BUILDIN_DEF(getequipid,"i"),
  11394. BUILDIN_DEF(getequipname,"i"),
  11395. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  11396. BUILDIN_DEF(repair,"i"), // [Valaris]
  11397. BUILDIN_DEF(getequipisequiped,"i"),
  11398. BUILDIN_DEF(getequipisenableref,"i"),
  11399. BUILDIN_DEF(getequipisidentify,"i"),
  11400. BUILDIN_DEF(getequiprefinerycnt,"i"),
  11401. BUILDIN_DEF(getequipweaponlv,"i"),
  11402. BUILDIN_DEF(getequippercentrefinery,"i"),
  11403. BUILDIN_DEF(successrefitem,"i"),
  11404. BUILDIN_DEF(failedrefitem,"i"),
  11405. BUILDIN_DEF(statusup,"i"),
  11406. BUILDIN_DEF(statusup2,"ii"),
  11407. BUILDIN_DEF(bonus,"iv"),
  11408. BUILDIN_DEF2(bonus,"bonus2","ivi"),
  11409. BUILDIN_DEF2(bonus,"bonus3","ivii"),
  11410. BUILDIN_DEF2(bonus,"bonus4","iviii"),
  11411. BUILDIN_DEF2(bonus,"bonus5","iviiii"),
  11412. BUILDIN_DEF(bonusautoscript,"si?"),
  11413. BUILDIN_DEF(bonusautoscript2,"si?"),
  11414. BUILDIN_DEF(skill,"vi?"),
  11415. BUILDIN_DEF(addtoskill,"vi?"), // [Valaris]
  11416. BUILDIN_DEF(guildskill,"vi"),
  11417. BUILDIN_DEF(getskilllv,"v"),
  11418. BUILDIN_DEF(getgdskilllv,"iv"),
  11419. BUILDIN_DEF(basicskillcheck,""),
  11420. BUILDIN_DEF(getgmlevel,""),
  11421. BUILDIN_DEF(end,""),
  11422. BUILDIN_DEF(checkoption,"i"),
  11423. BUILDIN_DEF(setoption,"i?"),
  11424. BUILDIN_DEF(setcart,"?"),
  11425. BUILDIN_DEF(checkcart,""),
  11426. BUILDIN_DEF(setfalcon,"?"),
  11427. BUILDIN_DEF(checkfalcon,""),
  11428. BUILDIN_DEF(setriding,"?"),
  11429. BUILDIN_DEF(checkriding,""),
  11430. BUILDIN_DEF2(savepoint,"save","sii"),
  11431. BUILDIN_DEF(savepoint,"sii"),
  11432. BUILDIN_DEF(gettimetick,"i"),
  11433. BUILDIN_DEF(gettime,"i"),
  11434. BUILDIN_DEF(gettimestr,"si"),
  11435. BUILDIN_DEF(openstorage,""),
  11436. BUILDIN_DEF(guildopenstorage,"*"),
  11437. BUILDIN_DEF(itemskill,"vi"),
  11438. BUILDIN_DEF(produce,"i"),
  11439. BUILDIN_DEF(monster,"siisii*"),
  11440. BUILDIN_DEF(getmobdrops,"i"),
  11441. BUILDIN_DEF(areamonster,"siiiisii*"),
  11442. BUILDIN_DEF(killmonster,"ss?"),
  11443. BUILDIN_DEF(killmonsterall,"s?"),
  11444. BUILDIN_DEF(clone,"siisi*"),
  11445. BUILDIN_DEF(doevent,"s"),
  11446. BUILDIN_DEF(donpcevent,"s"),
  11447. BUILDIN_DEF(cmdothernpc,"ss"),
  11448. BUILDIN_DEF(addtimer,"is"),
  11449. BUILDIN_DEF(deltimer,"s"),
  11450. BUILDIN_DEF(addtimercount,"si"),
  11451. BUILDIN_DEF(initnpctimer,"??"),
  11452. BUILDIN_DEF(stopnpctimer,"??"),
  11453. BUILDIN_DEF(startnpctimer,"??"),
  11454. BUILDIN_DEF(setnpctimer,"i?"),
  11455. BUILDIN_DEF(getnpctimer,"i?"),
  11456. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  11457. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  11458. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  11459. BUILDIN_DEF(announce,"si*"),
  11460. BUILDIN_DEF(mapannounce,"ssi*"),
  11461. BUILDIN_DEF(areaannounce,"siiiisi*"),
  11462. BUILDIN_DEF(getusers,"i"),
  11463. BUILDIN_DEF(getmapguildusers,"si"),
  11464. BUILDIN_DEF(getmapusers,"s"),
  11465. BUILDIN_DEF(getareausers,"siiii"),
  11466. BUILDIN_DEF(getareadropitem,"siiiii"),
  11467. BUILDIN_DEF(enablenpc,"s"),
  11468. BUILDIN_DEF(disablenpc,"s"),
  11469. BUILDIN_DEF(hideoffnpc,"s"),
  11470. BUILDIN_DEF(hideonnpc,"s"),
  11471. BUILDIN_DEF(sc_start,"iii?"),
  11472. BUILDIN_DEF(sc_start2,"iiii?"),
  11473. BUILDIN_DEF(sc_start4,"iiiiii?"),
  11474. BUILDIN_DEF(sc_end,"i?"),
  11475. BUILDIN_DEF(getscrate,"ii*"),
  11476. BUILDIN_DEF(debugmes,"s"),
  11477. BUILDIN_DEF2(catchpet,"pet","i"),
  11478. BUILDIN_DEF2(birthpet,"bpet",""),
  11479. BUILDIN_DEF(resetlvl,"i"),
  11480. BUILDIN_DEF(resetstatus,""),
  11481. BUILDIN_DEF(resetskill,""),
  11482. BUILDIN_DEF(skillpointcount,""),
  11483. BUILDIN_DEF(changebase,"i"),
  11484. BUILDIN_DEF(changesex,""),
  11485. BUILDIN_DEF(waitingroom,"si??"),
  11486. BUILDIN_DEF(delwaitingroom,"?"),
  11487. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  11488. BUILDIN_DEF(enablewaitingroomevent,"?"),
  11489. BUILDIN_DEF(disablewaitingroomevent,"?"),
  11490. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  11491. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  11492. BUILDIN_DEF(getwaitingroomstate,"i?"),
  11493. BUILDIN_DEF(warpwaitingpc,"sii?"),
  11494. BUILDIN_DEF(attachrid,"i"),
  11495. BUILDIN_DEF(detachrid,""),
  11496. BUILDIN_DEF(isloggedin,"i?"),
  11497. BUILDIN_DEF(setmapflagnosave,"ssii"),
  11498. BUILDIN_DEF(getmapflag,"si"),
  11499. BUILDIN_DEF(setmapflag,"si*"),
  11500. BUILDIN_DEF(removemapflag,"si"),
  11501. BUILDIN_DEF(pvpon,"s"),
  11502. BUILDIN_DEF(pvpoff,"s"),
  11503. BUILDIN_DEF(gvgon,"s"),
  11504. BUILDIN_DEF(gvgoff,"s"),
  11505. BUILDIN_DEF(emotion,"i*"),
  11506. BUILDIN_DEF(maprespawnguildid,"sii"),
  11507. BUILDIN_DEF(agitstart,""), // <Agit>
  11508. BUILDIN_DEF(agitend,""),
  11509. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  11510. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  11511. BUILDIN_DEF(getcastlename,"s"),
  11512. BUILDIN_DEF(getcastledata,"si*"),
  11513. BUILDIN_DEF(setcastledata,"sii"),
  11514. BUILDIN_DEF(requestguildinfo,"i*"),
  11515. BUILDIN_DEF(getequipcardcnt,"i"),
  11516. BUILDIN_DEF(successremovecards,"i"),
  11517. BUILDIN_DEF(failedremovecards,"ii"),
  11518. BUILDIN_DEF(marriage,"s"),
  11519. BUILDIN_DEF2(wedding_effect,"wedding",""),
  11520. BUILDIN_DEF(divorce,""),
  11521. BUILDIN_DEF(ispartneron,""),
  11522. BUILDIN_DEF(getpartnerid,""),
  11523. BUILDIN_DEF(getchildid,""),
  11524. BUILDIN_DEF(getmotherid,""),
  11525. BUILDIN_DEF(getfatherid,""),
  11526. BUILDIN_DEF(warppartner,"sii"),
  11527. BUILDIN_DEF(getitemname,"i"),
  11528. BUILDIN_DEF(getitemslots,"i"),
  11529. BUILDIN_DEF(makepet,"i"),
  11530. BUILDIN_DEF(getexp,"ii"),
  11531. BUILDIN_DEF(getinventorylist,""),
  11532. BUILDIN_DEF(getskilllist,""),
  11533. BUILDIN_DEF(clearitem,""),
  11534. BUILDIN_DEF(classchange,"ii"),
  11535. BUILDIN_DEF(misceffect,"i"),
  11536. BUILDIN_DEF(soundeffect,"si"),
  11537. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  11538. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  11539. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  11540. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  11541. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  11542. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  11543. BUILDIN_DEF(petloot,"i"), // [Valaris]
  11544. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  11545. BUILDIN_DEF(petskillattack,"viii"), // [Skotlex]
  11546. BUILDIN_DEF(petskillattack2,"viiii"), // [Valaris]
  11547. BUILDIN_DEF(petskillsupport,"viiii"), // [Skotlex]
  11548. BUILDIN_DEF(skilleffect,"vi"), // skill effect [Celest]
  11549. BUILDIN_DEF(npcskilleffect,"viii"), // npc skill effect [Valaris]
  11550. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  11551. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  11552. BUILDIN_DEF(nude,""), // nude command [Valaris]
  11553. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  11554. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  11555. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  11556. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  11557. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  11558. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  11559. BUILDIN_DEF(mobcount,"ss"),
  11560. BUILDIN_DEF(getlook,"i"),
  11561. BUILDIN_DEF(getsavepoint,"i"),
  11562. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  11563. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  11564. BUILDIN_DEF(npcstop,""), // [Valaris]
  11565. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  11566. BUILDIN_DEF(checkoption1,"i"),
  11567. BUILDIN_DEF(checkoption2,"i"),
  11568. BUILDIN_DEF(guildgetexp,"i"),
  11569. BUILDIN_DEF(guildchangegm,"is"),
  11570. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  11571. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  11572. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  11573. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  11574. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  11575. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  11576. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  11577. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  11578. BUILDIN_DEF(night,""), // sets the server to night time
  11579. BUILDIN_DEF(day,""), // sets the server to day time
  11580. #ifdef PCRE_SUPPORT
  11581. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  11582. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  11583. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  11584. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  11585. #endif
  11586. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  11587. BUILDIN_DEF(getusersname,"*"),
  11588. BUILDIN_DEF(recovery,""),
  11589. BUILDIN_DEF(getpetinfo,"i"),
  11590. BUILDIN_DEF(gethominfo,"i"),
  11591. BUILDIN_DEF(checkequipedcard,"i"),
  11592. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  11593. BUILDIN_DEF(globalmes,"s*"),
  11594. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  11595. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  11596. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  11597. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  11598. BUILDIN_DEF(setnpcdisplay,"sv??"),
  11599. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  11600. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  11601. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  11602. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  11603. // [zBuffer] List of mathematics commands --->
  11604. BUILDIN_DEF(sqrt,"i"),
  11605. BUILDIN_DEF(pow,"ii"),
  11606. BUILDIN_DEF(distance,"iiii"),
  11607. // <--- [zBuffer] List of mathematics commands
  11608. // [zBuffer] List of dynamic var commands --->
  11609. BUILDIN_DEF(getd,"*"),
  11610. BUILDIN_DEF(setd,"*"),
  11611. // <--- [zBuffer] List of dynamic var commands
  11612. BUILDIN_DEF(petstat,"i"),
  11613. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  11614. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  11615. BUILDIN_DEF(npcshopadditem,"sii*"),
  11616. BUILDIN_DEF(npcshopdelitem,"si*"),
  11617. BUILDIN_DEF(npcshopattach,"s?"),
  11618. BUILDIN_DEF(equip,"i"),
  11619. BUILDIN_DEF(autoequip,"ii"),
  11620. BUILDIN_DEF(setbattleflag,"ss"),
  11621. BUILDIN_DEF(getbattleflag,"s"),
  11622. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  11623. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  11624. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  11625. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  11626. BUILDIN_DEF(axtoi,"s"),
  11627. BUILDIN_DEF(query_sql,"s*"),
  11628. BUILDIN_DEF(query_logsql,"s*"),
  11629. BUILDIN_DEF(escape_sql,"s"),
  11630. BUILDIN_DEF(atoi,"s"),
  11631. // [zBuffer] List of player cont commands --->
  11632. BUILDIN_DEF(rid2name,"i"),
  11633. BUILDIN_DEF(pcfollow,"ii"),
  11634. BUILDIN_DEF(pcstopfollow,"i"),
  11635. BUILDIN_DEF(pcblockmove,"ii"),
  11636. // <--- [zBuffer] List of player cont commands
  11637. // [zBuffer] List of mob control commands --->
  11638. BUILDIN_DEF(unitwalk,"ii?"),
  11639. BUILDIN_DEF(unitkill,"i"),
  11640. BUILDIN_DEF(unitwarp,"isii"),
  11641. BUILDIN_DEF(unitattack,"iv?"),
  11642. BUILDIN_DEF(unitstop,"i"),
  11643. BUILDIN_DEF(unittalk,"is"),
  11644. BUILDIN_DEF(unitemote,"ii"),
  11645. BUILDIN_DEF(unitskilluseid,"ivi?"), // originally by Qamera [Celest]
  11646. BUILDIN_DEF(unitskillusepos,"iviii"), // [Celest]
  11647. // <--- [zBuffer] List of mob control commands
  11648. BUILDIN_DEF(sleep,"i"),
  11649. BUILDIN_DEF(sleep2,"i"),
  11650. BUILDIN_DEF(awake,"s"),
  11651. BUILDIN_DEF(getvariableofnpc,"rs"),
  11652. BUILDIN_DEF(warpportal,"iisii"),
  11653. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  11654. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  11655. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  11656. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  11657. BUILDIN_DEF(checkvending,"*"),
  11658. BUILDIN_DEF(checkchatting,"*"),
  11659. BUILDIN_DEF(openmail,""),
  11660. BUILDIN_DEF(openauction,""),
  11661. BUILDIN_DEF(checkcell,"siii"),
  11662. BUILDIN_DEF(setcell,"siiiiii"),
  11663. BUILDIN_DEF(getquest, "ii*"),
  11664. BUILDIN_DEF(deletequest, "i"),
  11665. BUILDIN_DEF(setquestobjective, "iisi"),
  11666. BUILDIN_DEF(setqueststatus, "ii"),
  11667. BUILDIN_DEF(hasquest, "i"),
  11668. BUILDIN_DEF(setwall,"siiiiis"),
  11669. BUILDIN_DEF(delwall,"s"),
  11670. {NULL,NULL,NULL},
  11671. };