script.c 332 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616
  1. // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. //#define DEBUG_FUNCIN
  4. //#define DEBUG_DISP
  5. //#define DEBUG_DISASM
  6. //#define DEBUG_RUN
  7. //#define DEBUG_HASH
  8. #include "../common/cbasetypes.h"
  9. #include "../common/timer.h"
  10. #include "../common/malloc.h"
  11. #include "../common/lock.h"
  12. #include "../common/nullpo.h"
  13. #include "../common/showmsg.h"
  14. #include "../common/strlib.h"
  15. #include "../common/utils.h"
  16. #include "map.h"
  17. #include "path.h"
  18. #include "clif.h"
  19. #include "chrif.h"
  20. #include "itemdb.h"
  21. #include "pc.h"
  22. #include "status.h"
  23. #include "storage.h"
  24. #include "mob.h"
  25. #include "npc.h"
  26. #include "pet.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 "irc.h"
  40. #include "pet.h"
  41. #include "mail.h"
  42. #include "script.h"
  43. #include "quest.h"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #include <math.h>
  48. #ifndef WIN32
  49. #include <sys/time.h>
  50. #endif
  51. #include <time.h>
  52. #include <setjmp.h>
  53. #include <errno.h>
  54. ///////////////////////////////////////////////////////////////////////////////
  55. //## TODO possible enhancements: [FlavioJS]
  56. // - 'callfunc' supporting labels in the current npc "::LabelName"
  57. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  58. // - 'function FuncName;' function declarations reverting to global functions
  59. // if local label isn't found
  60. // - join callfunc and callsub's functionality
  61. //
  62. // struct script_state* st;
  63. //
  64. /// Returns the script_data at the target index
  65. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  66. /// Returns if the stack contains data at the target index
  67. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  68. /// Returns the index of the last data in the stack
  69. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  70. /// Pushes an int into the stack
  71. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  72. /// Pushes a string into the stack (script engine frees it automatically)
  73. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  74. /// Pushes a copy of a string into the stack
  75. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  76. /// Pushes a constant string into the stack (must never change or be freed)
  77. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  78. /// Pushes a nil into the stack
  79. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  80. /// Pushes a copy of the data in the target index
  81. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  82. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  83. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  84. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  85. // Note: "top" functions/defines use indexes relative to the top of the stack
  86. // -1 is the index of the data at the top
  87. /// Returns the script_data at the target index relative to the top of the stack
  88. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  89. /// Pushes a copy of the data in the target index relative to the top of the stack
  90. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  91. /// Removes the range of values [start,end[ relative to the top of the stack
  92. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  93. //
  94. // struct script_data* data;
  95. //
  96. /// Returns if the script data is a string
  97. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  98. /// Returns if the script data is an int
  99. #define data_isint(data) ( (data)->type == C_INT )
  100. /// Returns if the script data is a reference
  101. #define data_isreference(data) ( (data)->type == C_NAME )
  102. /// Returns if the script data is a label
  103. #define data_islabel(data) ( (data)->type == C_POS )
  104. /// Returns if the script data is an internal script function label
  105. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  106. /// Returns if this is a reference to a constant
  107. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  108. /// Returns if this a reference to a param
  109. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  110. /// Returns if this a reference to a variable
  111. //##TODO confirm it's C_NAME [FlavioJS]
  112. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  113. /// Returns the unique id of the reference (id and index)
  114. #define reference_getuid(data) ( (data)->u.num )
  115. /// Returns the id of the reference
  116. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  117. /// Returns the array index of the reference
  118. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  119. /// Returns the name of the reference
  120. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  121. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  122. #define reference_getref(data) ( (data)->ref )
  123. /// Returns the value of the constant
  124. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  125. /// Returns the type of param
  126. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  127. /// Composes the uid of a reference from the id and the index
  128. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  129. #define FETCH(n, t) \
  130. if( script_hasdata(st,n) ) \
  131. (t)=script_getnum(st,n);
  132. #define SCRIPT_BLOCK_SIZE 512
  133. enum { LABEL_NEXTLINE=1,LABEL_START };
  134. static unsigned char * script_buf = NULL;
  135. static int script_pos,script_size;
  136. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  137. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  138. #define GETSTRING(off) (str_buf+(off))
  139. static char *str_buf;
  140. static int str_pos,str_size;
  141. static struct str_data_struct {
  142. enum c_op type;
  143. int str;
  144. int backpatch;
  145. int label;
  146. int (*func)(struct script_state *st);
  147. int val;
  148. int next;
  149. } *str_data = NULL;
  150. int str_num=LABEL_START,str_data_size;
  151. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  152. #define SCRIPT_HASH_SIZE 1021
  153. int str_hash[SCRIPT_HASH_SIZE];
  154. //#define SCRIPT_HASH_DJB2
  155. #define SCRIPT_HASH_SDBM
  156. //#define SCRIPT_HASH_ELF
  157. //#define SCRIPT_HASH_PJW
  158. static DBMap* mapreg_db=NULL; // int var_id -> int value
  159. static DBMap* mapregstr_db=NULL; // int var_id -> char* value
  160. static int mapreg_dirty=-1;
  161. char mapreg_txt[256]="save/mapreg.txt";
  162. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  163. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  164. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  165. static int parse_options=0;
  166. DBMap* script_get_label_db(){ return scriptlabel_db; }
  167. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  168. struct Script_Config script_config = {
  169. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  170. 0, INT_MAX, // input_min_value/input_max_value
  171. "OnPCDieEvent", //die_event_name
  172. "OnPCKillEvent", //kill_pc_event_name
  173. "OnNPCKillEvent", //kill_mob_event_name
  174. "OnPCLoginEvent", //login_event_name
  175. "OnPCLogoutEvent", //logout_event_name
  176. "OnPCLoadMapEvent", //loadmap_event_name
  177. "OnPCBaseLvUpEvent", //baselvup_event_name
  178. "OnPCJobLvUpEvent" //joblvup_event_name
  179. };
  180. static jmp_buf error_jump;
  181. static char* error_msg;
  182. static const char* error_pos;
  183. static int error_report; // if the error should produce output
  184. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  185. // [Eoe / jA 1080, 1081, 1094, 1164]
  186. enum curly_type {
  187. TYPE_NULL = 0,
  188. TYPE_IF,
  189. TYPE_SWITCH,
  190. TYPE_WHILE,
  191. TYPE_FOR,
  192. TYPE_DO,
  193. TYPE_USERFUNC,
  194. TYPE_ARGLIST // function argument list
  195. };
  196. #define ARGLIST_UNDEFINED 0
  197. #define ARGLIST_NO_PAREN 1
  198. #define ARGLIST_PAREN 2
  199. static struct {
  200. struct {
  201. enum curly_type type;
  202. int index;
  203. int count;
  204. int flag;
  205. struct linkdb_node *case_label;
  206. } curly[256]; // 右カッコの情報
  207. int curly_count; // 右カッコの数
  208. int index; // スクリプト内で使用した構文の数
  209. } syntax;
  210. const char* parse_curly_close(const char* p);
  211. const char* parse_syntax_close(const char* p);
  212. const char* parse_syntax_close_sub(const char* p,int* flag);
  213. const char* parse_syntax(const char* p);
  214. static int parse_syntax_for_flag = 0;
  215. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  216. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  217. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  218. int potion_target=0;
  219. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  220. char mapregsql_db[32] = "mapreg";
  221. char mapregsql_db_varname[32] = "varname";
  222. char mapregsql_db_index[32] = "index";
  223. char mapregsql_db_value[32] = "value";
  224. #endif
  225. c_op get_com(unsigned char *script,int *pos);
  226. int get_num(unsigned char *script,int *pos);
  227. typedef struct script_function {
  228. int (*func)(struct script_state *st);
  229. const char *name;
  230. const char *arg;
  231. } script_function;
  232. extern script_function buildin_func[];
  233. static struct linkdb_node *sleep_db;
  234. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.')
  235. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' )
  236. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  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. int mapreg_setreg(int num,int val);
  244. int mapreg_setregstr(int num,const char *str);
  245. enum {
  246. MF_NOMEMO, //0
  247. MF_NOTELEPORT,
  248. MF_NOSAVE,
  249. MF_NOBRANCH,
  250. MF_NOPENALTY,
  251. MF_NOZENYPENALTY,
  252. MF_PVP,
  253. MF_PVP_NOPARTY,
  254. MF_PVP_NOGUILD,
  255. MF_GVG,
  256. MF_GVG_NOPARTY, //10
  257. MF_NOTRADE,
  258. MF_NOSKILL,
  259. MF_NOWARP,
  260. MF_PARTYLOCK,
  261. MF_NOICEWALL,
  262. MF_SNOW,
  263. MF_FOG,
  264. MF_SAKURA,
  265. MF_LEAVES,
  266. MF_RAIN, //20
  267. MF_INDOORS,
  268. MF_NOGO,
  269. MF_CLOUDS,
  270. MF_CLOUDS2,
  271. MF_FIREWORKS,
  272. MF_GVG_CASTLE,
  273. MF_GVG_DUNGEON,
  274. MF_NIGHTENABLED,
  275. MF_NOBASEEXP,
  276. MF_NOJOBEXP, //30
  277. MF_NOMOBLOOT,
  278. MF_NOMVPLOOT,
  279. MF_NORETURN,
  280. MF_NOWARPTO,
  281. MF_NIGHTMAREDROP,
  282. MF_RESTRICTED,
  283. MF_NOCOMMAND,
  284. MF_NODROP,
  285. MF_JEXP,
  286. MF_BEXP, //40
  287. MF_NOVENDING,
  288. MF_LOADEVENT,
  289. MF_NOCHAT,
  290. MF_NOEXPPENALTY,
  291. MF_GUILDLOCK
  292. };
  293. const char* script_op2name(int op)
  294. {
  295. #define RETURN_OP_NAME(type) case type: return #type
  296. switch( op )
  297. {
  298. RETURN_OP_NAME(C_NOP);
  299. RETURN_OP_NAME(C_POS);
  300. RETURN_OP_NAME(C_INT);
  301. RETURN_OP_NAME(C_PARAM);
  302. RETURN_OP_NAME(C_FUNC);
  303. RETURN_OP_NAME(C_STR);
  304. RETURN_OP_NAME(C_CONSTSTR);
  305. RETURN_OP_NAME(C_ARG);
  306. RETURN_OP_NAME(C_NAME);
  307. RETURN_OP_NAME(C_EOL);
  308. RETURN_OP_NAME(C_RETINFO);
  309. RETURN_OP_NAME(C_USERFUNC);
  310. RETURN_OP_NAME(C_USERFUNC_POS);
  311. // operators
  312. RETURN_OP_NAME(C_OP3);
  313. RETURN_OP_NAME(C_LOR);
  314. RETURN_OP_NAME(C_LAND);
  315. RETURN_OP_NAME(C_LE);
  316. RETURN_OP_NAME(C_LT);
  317. RETURN_OP_NAME(C_GE);
  318. RETURN_OP_NAME(C_GT);
  319. RETURN_OP_NAME(C_EQ);
  320. RETURN_OP_NAME(C_NE);
  321. RETURN_OP_NAME(C_XOR);
  322. RETURN_OP_NAME(C_OR);
  323. RETURN_OP_NAME(C_AND);
  324. RETURN_OP_NAME(C_ADD);
  325. RETURN_OP_NAME(C_SUB);
  326. RETURN_OP_NAME(C_MUL);
  327. RETURN_OP_NAME(C_DIV);
  328. RETURN_OP_NAME(C_MOD);
  329. RETURN_OP_NAME(C_NEG);
  330. RETURN_OP_NAME(C_LNOT);
  331. RETURN_OP_NAME(C_NOT);
  332. RETURN_OP_NAME(C_R_SHIFT);
  333. RETURN_OP_NAME(C_L_SHIFT);
  334. default:
  335. ShowDebug("script_op2name: unexpected op=%d\n", op);
  336. return "???";
  337. }
  338. #undef RETURN_OP_NAME
  339. }
  340. /// Reports on the console the src of a script error.
  341. static void script_reportsrc(struct script_state *st)
  342. {
  343. struct block_list* bl;
  344. if( st->oid == 0 )
  345. return; //Can't report source.
  346. bl = map_id2bl(st->oid);
  347. if( bl == NULL )
  348. return;
  349. switch( bl->type )
  350. {
  351. case BL_NPC:
  352. if( bl->m >= 0 )
  353. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  354. else
  355. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  356. break;
  357. default:
  358. if( bl->m >= 0 )
  359. 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);
  360. else
  361. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  362. break;
  363. }
  364. }
  365. /// Reports on the console information about the script data.
  366. static void script_reportdata(struct script_data* data)
  367. {
  368. if( data == NULL )
  369. return;
  370. switch( data->type )
  371. {
  372. case C_NOP:// no value
  373. ShowDebug("Data: nothing (nil)\n");
  374. break;
  375. case C_INT:// number
  376. ShowDebug("Data: number value=%d\n", data->u.num);
  377. break;
  378. case C_STR:
  379. case C_CONSTSTR:// string
  380. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  381. break;
  382. case C_NAME:// reference
  383. if( reference_tovariable(data) )
  384. {// variable
  385. char* name = reference_getname(data);
  386. if( not_array_variable(*name) )
  387. ShowDebug("Data: variable name='%s'\n", name);
  388. else
  389. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  390. }
  391. else if( reference_toconstant(data) )
  392. {// constant
  393. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  394. }
  395. else if( reference_toparam(data) )
  396. {// param
  397. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  398. }
  399. else
  400. {// ???
  401. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  402. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  403. }
  404. break;
  405. case C_POS:// label
  406. ShowDebug("Data: label pos=%d\n", data->u.num);
  407. break;
  408. default:
  409. ShowDebug("Data: %s\n", script_op2name(data->type));
  410. break;
  411. }
  412. }
  413. /*==========================================
  414. * エラーメッセージ出力
  415. *------------------------------------------*/
  416. static void disp_error_message2(const char *mes,const char *pos,int report)
  417. {
  418. error_msg = aStrdup(mes);
  419. error_pos = pos;
  420. error_report = report;
  421. longjmp( error_jump, 1 );
  422. }
  423. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  424. /// Checks event parameter validity
  425. static void check_event(struct script_state *st, const char *evt)
  426. {
  427. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  428. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  429. script_reportsrc(st);
  430. }
  431. }
  432. /*==========================================
  433. * 文字列のハッシュを計算
  434. *------------------------------------------*/
  435. #define calc_hash(x) (calc_hash2(x)%SCRIPT_HASH_SIZE)
  436. static unsigned int calc_hash2(const char* p)
  437. {
  438. #if defined(SCRIPT_HASH_DJB2)
  439. unsigned int h = 5381;
  440. while( *p ) // hash*33 + c
  441. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  442. return h;
  443. #elif defined(SCRIPT_HASH_SDBM)
  444. unsigned int h = 0;
  445. while( *p )
  446. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  447. return h;
  448. #elif defined(SCRIPT_HASH_ELF)
  449. unsigned int h = 0;
  450. unsigned int g;
  451. while( *p ){ // UNIX ELF hash
  452. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  453. g = h & 0xF0000000;
  454. if( g )
  455. h ^= g >> 24;
  456. h &= ~g;
  457. }
  458. return h;
  459. #elif defined(SCRIPT_HASH_PJW)
  460. unsigned int h = 0;
  461. unsigned int g;
  462. while( *p ){
  463. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  464. g = h & 0xF0000000;
  465. if( g )
  466. {
  467. h ^= g>>24;
  468. h ^= g;
  469. }
  470. }
  471. return h;
  472. #else
  473. unsigned int h = 0;
  474. while( *p ){
  475. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 );
  476. h += (unsigned char)TOLOWER(*p++);
  477. }
  478. return h;
  479. #endif
  480. }
  481. /*==========================================
  482. * str_dataの中に名前があるか検索する
  483. *------------------------------------------*/
  484. // 既存のであれば番号、無ければ-1
  485. static int search_str(const char *p)
  486. {
  487. int i;
  488. i=str_hash[calc_hash(p)];
  489. while(i){
  490. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  491. return i;
  492. }
  493. i=str_data[i].next;
  494. }
  495. return -1;
  496. }
  497. /*==========================================
  498. * str_dataに名前を登録
  499. *------------------------------------------*/
  500. // 既存のであれば番号、無ければ登録して新規番号
  501. int add_str(const char* p)
  502. {
  503. int i;
  504. int len;
  505. i=calc_hash(p);
  506. if(str_hash[i]==0){
  507. str_hash[i]=str_num;
  508. } else {
  509. i=str_hash[i];
  510. for(;;){
  511. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  512. return i;
  513. }
  514. if(str_data[i].next==0)
  515. break;
  516. i=str_data[i].next;
  517. }
  518. str_data[i].next=str_num;
  519. }
  520. if(str_num>=str_data_size){
  521. str_data_size+=128;
  522. RECREATE(str_data,struct str_data_struct,str_data_size);
  523. memset(str_data + (str_data_size - 128), '\0', 128);
  524. }
  525. len=(int)strlen(p);
  526. while(str_pos+len+1>=str_size){
  527. str_size+=256;
  528. RECREATE(str_buf,char,str_size);
  529. memset(str_buf + (str_size - 256), '\0', 256);
  530. }
  531. memcpy(str_buf+str_pos,p,len+1);
  532. str_data[str_num].type=C_NOP;
  533. str_data[str_num].str=str_pos;
  534. str_data[str_num].next=0;
  535. str_data[str_num].func=NULL;
  536. str_data[str_num].backpatch=-1;
  537. str_data[str_num].label=-1;
  538. str_pos+=len+1;
  539. return str_num++;
  540. }
  541. /*==========================================
  542. * スクリプトバッファサイズの確認と拡張
  543. *------------------------------------------*/
  544. static void expand_script_buf(void)
  545. {
  546. script_size+=SCRIPT_BLOCK_SIZE;
  547. RECREATE(script_buf,unsigned char,script_size);
  548. }
  549. /*==========================================
  550. * スクリプトバッファに1バイト書き込む
  551. *------------------------------------------*/
  552. #define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a)
  553. #if 0
  554. static void add_scriptb(int a)
  555. {
  556. expand_script_buf();
  557. script_buf[script_pos++]=a;
  558. }
  559. #endif
  560. /*==========================================
  561. * スクリプトバッファにデータタイプを書き込む
  562. *------------------------------------------*/
  563. static void add_scriptc(int a)
  564. {
  565. while(a>=0x40){
  566. add_scriptb((a&0x3f)|0x40);
  567. a=(a-0x40)>>6;
  568. }
  569. add_scriptb(a&0x3f);
  570. }
  571. /*==========================================
  572. * スクリプトバッファに整数を書き込む
  573. *------------------------------------------*/
  574. static void add_scripti(int a)
  575. {
  576. while(a>=0x40){
  577. add_scriptb(a|0xc0);
  578. a=(a-0x40)>>6;
  579. }
  580. add_scriptb(a|0x80);
  581. }
  582. /*==========================================
  583. * スクリプトバッファにラベル/変数/関数を書き込む
  584. *------------------------------------------*/
  585. // 最大16Mまで
  586. static void add_scriptl(int l)
  587. {
  588. int backpatch = str_data[l].backpatch;
  589. switch(str_data[l].type){
  590. case C_POS:
  591. case C_USERFUNC_POS:
  592. add_scriptc(C_POS);
  593. add_scriptb(str_data[l].label);
  594. add_scriptb(str_data[l].label>>8);
  595. add_scriptb(str_data[l].label>>16);
  596. break;
  597. case C_NOP:
  598. case C_USERFUNC:
  599. // ラベルの可能性があるのでbackpatch用データ埋め込み
  600. add_scriptc(C_NAME);
  601. str_data[l].backpatch=script_pos;
  602. add_scriptb(backpatch);
  603. add_scriptb(backpatch>>8);
  604. add_scriptb(backpatch>>16);
  605. break;
  606. case C_INT:
  607. add_scripti(abs(str_data[l].val));
  608. if(str_data[l].val < 0) //Notice that this is negative, from jA (Rayce)
  609. add_scriptc(C_NEG);
  610. break;
  611. default:
  612. // もう他の用途と確定してるので数字をそのまま
  613. add_scriptc(C_NAME);
  614. add_scriptb(l);
  615. add_scriptb(l>>8);
  616. add_scriptb(l>>16);
  617. break;
  618. }
  619. }
  620. /*==========================================
  621. * ラベルを解決する
  622. *------------------------------------------*/
  623. void set_label(int l,int pos, const char* script_pos)
  624. {
  625. int i,next;
  626. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  627. { //Prevent overwriting constants values and parameters [Skotlex]
  628. disp_error_message("set_label: invalid label name",script_pos);
  629. return;
  630. }
  631. if(str_data[l].label!=-1){
  632. disp_error_message("set_label: dup label ",script_pos);
  633. return;
  634. }
  635. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  636. str_data[l].label=pos;
  637. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  638. next=GETVALUE(script_buf,i);
  639. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  640. SETVALUE(script_buf,i,pos);
  641. i=next;
  642. }
  643. }
  644. /// Skips spaces and/or comments.
  645. const char* skip_space(const char* p)
  646. {
  647. if( p == NULL )
  648. return NULL;
  649. for(;;)
  650. {
  651. while( ISSPACE(*p) )
  652. ++p;
  653. if( *p == '/' && p[1] == '/' )
  654. {// line comment
  655. while(*p && *p!='\n')
  656. ++p;
  657. }
  658. else if( *p == '/' && p[1] == '*' )
  659. {// block comment
  660. p += 2;
  661. for(;;)
  662. {
  663. if( *p == '\0' )
  664. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  665. if( *p == '*' && p[1] == '/' )
  666. {// end of block comment
  667. p += 2;
  668. break;
  669. }
  670. ++p;
  671. }
  672. }
  673. else
  674. break;
  675. }
  676. return p;
  677. }
  678. /// Skips a word.
  679. /// A word consists of undercores and/or alfanumeric characters,
  680. /// and valid variable prefixes/postfixes.
  681. static
  682. const char* skip_word(const char* p)
  683. {
  684. // prefix
  685. switch( *p )
  686. {
  687. case '@':// temporary char variable
  688. ++p; break;
  689. case '#':// account variable
  690. p += ( p[1] == '#' ? 2 : 1 ); break;
  691. case '.':// npc variable
  692. p += ( p[1] == '@' ? 2 : 1 ); break;
  693. case '$':// global variable
  694. p += ( p[1] == '@' ? 2 : 1 ); break;
  695. }
  696. while( ISALNUM(*p) || *p == '_' )
  697. ++p;
  698. // postfix
  699. if( *p == '$' )// string
  700. p++;
  701. return p;
  702. }
  703. /// Adds a word to str_data.
  704. /// @see skip_word
  705. /// @see add_str
  706. static
  707. int add_word(const char* p)
  708. {
  709. char* word;
  710. int len;
  711. int i;
  712. // Check for a word
  713. len = skip_word(p) - p;
  714. if( len == 0 )
  715. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  716. // Duplicate the word
  717. CREATE(word, char, len+1);
  718. memcpy(word, p, len);
  719. word[len] = 0;
  720. // add the word
  721. i = add_str(word);
  722. aFree(word);
  723. return i;
  724. }
  725. /// Parses a function call.
  726. /// The argument list can have parenthesis or not.
  727. /// The number of arguments is checked.
  728. static
  729. const char* parse_callfunc(const char* p, int require_paren)
  730. {
  731. const char* p2;
  732. const char* arg=NULL;
  733. int func;
  734. func = add_word(p);
  735. if( str_data[func].type == C_FUNC ){
  736. // buildin function
  737. add_scriptl(func);
  738. add_scriptc(C_ARG);
  739. arg = buildin_func[str_data[func].val].arg;
  740. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  741. // script defined function
  742. int callsub = search_str("callsub");
  743. add_scriptl(callsub);
  744. add_scriptc(C_ARG);
  745. add_scriptl(func);
  746. arg = buildin_func[str_data[callsub].val].arg;
  747. if( *arg == 0 )
  748. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  749. if( *arg != '*' )
  750. ++arg; // count func as argument
  751. } else
  752. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  753. p = skip_word(p);
  754. p = skip_space(p);
  755. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  756. syntax.curly[syntax.curly_count].count = 0;
  757. if( *p == ';' )
  758. {// <func name> ';'
  759. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  760. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  761. {// <func name> '(' ')'
  762. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  763. p = p2;
  764. /*
  765. } else if( 0 && require_paren && *p != '(' )
  766. {// <func name>
  767. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  768. */
  769. } else
  770. {// <func name> <arg list>
  771. if( require_paren ){
  772. if( *p != '(' )
  773. disp_error_message("need '('",p);
  774. ++p; // skip '('
  775. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  776. } else if( *p == '(' ){
  777. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  778. } else {
  779. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  780. }
  781. ++syntax.curly_count;
  782. while( *arg ) {
  783. p2=parse_subexpr(p,-1);
  784. if( p == p2 )
  785. break; // not an argument
  786. if( *arg != '*' )
  787. ++arg; // next argument
  788. p=skip_space(p2);
  789. if( *arg == 0 || *p != ',' )
  790. break; // no more arguments
  791. ++p; // skip comma
  792. }
  793. --syntax.curly_count;
  794. }
  795. if( *arg && *arg != '?' && *arg != '*' )
  796. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  797. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  798. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  799. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  800. if( *p != ')' )
  801. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  802. ++p;
  803. }
  804. add_scriptc(C_FUNC);
  805. return p;
  806. }
  807. /*==========================================
  808. * 項の解析
  809. *------------------------------------------*/
  810. const char* parse_simpleexpr(const char *p)
  811. {
  812. int i;
  813. p=skip_space(p);
  814. #ifdef DEBUG_FUNCIN
  815. if(battle_config.etc_log)
  816. ShowDebug("parse_simpleexpr %s\n",p);
  817. #endif
  818. if(*p==';' || *p==',')
  819. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  820. if(*p=='('){
  821. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  822. ++syntax.curly[i].count;
  823. p=parse_subexpr(p+1,-1);
  824. p=skip_space(p);
  825. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  826. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  827. ){
  828. if( *p == ',' ){
  829. syntax.curly[i].flag = ARGLIST_PAREN;
  830. return p;
  831. } else
  832. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  833. }
  834. if( *p != ')' )
  835. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  836. ++p;
  837. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  838. char *np;
  839. i=strtoul(p,&np,0);
  840. add_scripti(i);
  841. p=np;
  842. } else if(*p=='"'){
  843. add_scriptc(C_STR);
  844. p++;
  845. while( *p && *p != '"' ){
  846. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  847. {
  848. char buf[8];
  849. size_t len = skip_escaped_c(p) - p;
  850. size_t n = sv_unescape_c(buf, p, len);
  851. if( n != 1 )
  852. ShowDebug("parse_simpleexpr: unexpected length %d after unescape (\"%.*s\" -> %.*s)\n", (int)n, (int)len, p, (int)n, buf);
  853. p += len;
  854. add_scriptb(*buf);
  855. continue;
  856. }
  857. else if( *p == '\n' )
  858. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  859. add_scriptb(*p++);
  860. }
  861. if(!*p)
  862. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  863. add_scriptb(0);
  864. p++; //'"'
  865. } else {
  866. int l;
  867. // label , register , function etc
  868. if(skip_word(p)==p)
  869. disp_error_message("parse_simpleexpr: unexpected character",p);
  870. l=add_word(p);
  871. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  872. return parse_callfunc(p,1);
  873. p=skip_word(p);
  874. if( *p == '[' ){
  875. // array(name[i] => getelementofarray(name,i) )
  876. add_scriptl(search_str("getelementofarray"));
  877. add_scriptc(C_ARG);
  878. add_scriptl(l);
  879. p=parse_subexpr(p+1,-1);
  880. p=skip_space(p);
  881. if( *p != ']' )
  882. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  883. ++p;
  884. add_scriptc(C_FUNC);
  885. }else
  886. add_scriptl(l);
  887. }
  888. #ifdef DEBUG_FUNCIN
  889. if(battle_config.etc_log)
  890. ShowDebug("parse_simpleexpr end %s\n",p);
  891. #endif
  892. return p;
  893. }
  894. /*==========================================
  895. * 式の解析
  896. *------------------------------------------*/
  897. const char* parse_subexpr(const char* p,int limit)
  898. {
  899. int op,opl,len;
  900. const char* tmpp;
  901. #ifdef DEBUG_FUNCIN
  902. if(battle_config.etc_log)
  903. ShowDebug("parse_subexpr %s\n",p);
  904. #endif
  905. p=skip_space(p);
  906. if(*p=='-'){
  907. tmpp=skip_space(p+1);
  908. if(*tmpp==';' || *tmpp==','){
  909. add_scriptl(LABEL_NEXTLINE);
  910. p++;
  911. return p;
  912. }
  913. }
  914. tmpp=p;
  915. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  916. p=parse_subexpr(p+1,10);
  917. add_scriptc(op);
  918. } else
  919. p=parse_simpleexpr(p);
  920. p=skip_space(p);
  921. while((
  922. (op=C_OP3,opl=0,len=1,*p=='?') ||
  923. (op=C_ADD,opl=8,len=1,*p=='+') ||
  924. (op=C_SUB,opl=8,len=1,*p=='-') ||
  925. (op=C_MUL,opl=9,len=1,*p=='*') ||
  926. (op=C_DIV,opl=9,len=1,*p=='/') ||
  927. (op=C_MOD,opl=9,len=1,*p=='%') ||
  928. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  929. (op=C_AND,opl=6,len=1,*p=='&') ||
  930. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  931. (op=C_OR,opl=5,len=1,*p=='|') ||
  932. (op=C_XOR,opl=4,len=1,*p=='^') ||
  933. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  934. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  935. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  936. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  937. (op=C_GT,opl=3,len=1,*p=='>') ||
  938. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  939. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  940. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  941. p+=len;
  942. if(op == C_OP3) {
  943. p=parse_subexpr(p,-1);
  944. p=skip_space(p);
  945. if( *(p++) != ':')
  946. disp_error_message("parse_subexpr: need ':'", p-1);
  947. p=parse_subexpr(p,-1);
  948. } else {
  949. p=parse_subexpr(p,opl);
  950. }
  951. add_scriptc(op);
  952. p=skip_space(p);
  953. }
  954. #ifdef DEBUG_FUNCIN
  955. if(battle_config.etc_log)
  956. ShowDebug("parse_subexpr end %s\n",p);
  957. #endif
  958. return p; /* return first untreated operator */
  959. }
  960. /*==========================================
  961. * 式の評価
  962. *------------------------------------------*/
  963. const char* parse_expr(const char *p)
  964. {
  965. #ifdef DEBUG_FUNCIN
  966. if(battle_config.etc_log)
  967. ShowDebug("parse_expr %s\n",p);
  968. #endif
  969. switch(*p){
  970. case ')': case ';': case ':': case '[': case ']':
  971. case '}':
  972. disp_error_message("parse_expr: unexpected char",p);
  973. }
  974. p=parse_subexpr(p,-1);
  975. #ifdef DEBUG_FUNCIN
  976. if(battle_config.etc_log)
  977. ShowDebug("parse_expr end %s\n",p);
  978. #endif
  979. return p;
  980. }
  981. /*==========================================
  982. * 行の解析
  983. *------------------------------------------*/
  984. const char* parse_line(const char* p)
  985. {
  986. const char* p2;
  987. p=skip_space(p);
  988. if(*p==';') {
  989. // if(); for(); while(); のために閉じ判定
  990. p = parse_syntax_close(p + 1);
  991. return p;
  992. }
  993. if(*p==')' && parse_syntax_for_flag)
  994. return p+1;
  995. p = skip_space(p);
  996. if(p[0] == '{') {
  997. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  998. syntax.curly[syntax.curly_count].count = -1;
  999. syntax.curly[syntax.curly_count].index = -1;
  1000. syntax.curly_count++;
  1001. return p + 1;
  1002. } else if(p[0] == '}') {
  1003. return parse_curly_close(p);
  1004. }
  1005. // 構文関連の処理
  1006. p2 = parse_syntax(p);
  1007. if(p2 != NULL)
  1008. return p2;
  1009. p = parse_callfunc(p,0);
  1010. p = skip_space(p);
  1011. if(parse_syntax_for_flag) {
  1012. if( *p != ')' )
  1013. disp_error_message("parse_line: need ')'",p);
  1014. } else {
  1015. if( *p != ';' )
  1016. disp_error_message("parse_line: need ';'",p);
  1017. }
  1018. // if, for , while の閉じ判定
  1019. p = parse_syntax_close(p+1);
  1020. return p;
  1021. }
  1022. // { ... } の閉じ処理
  1023. const char* parse_curly_close(const char* p)
  1024. {
  1025. if(syntax.curly_count <= 0) {
  1026. disp_error_message("parse_curly_close: unexpected string",p);
  1027. return p + 1;
  1028. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1029. syntax.curly_count--;
  1030. // if, for , while の閉じ判定
  1031. p = parse_syntax_close(p + 1);
  1032. return p;
  1033. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1034. // switch() 閉じ判定
  1035. int pos = syntax.curly_count-1;
  1036. char label[256];
  1037. int l;
  1038. // 一時変数を消す
  1039. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1040. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1041. parse_line(label);
  1042. syntax.curly_count--;
  1043. // 無条件で終了ポインタに移動
  1044. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1045. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1046. parse_line(label);
  1047. syntax.curly_count--;
  1048. // 現在地のラベルを付ける
  1049. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1050. l=add_str(label);
  1051. set_label(l,script_pos, p);
  1052. if(syntax.curly[pos].flag) {
  1053. // default が存在する
  1054. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1055. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1056. parse_line(label);
  1057. syntax.curly_count--;
  1058. }
  1059. // 終了ラベルを付ける
  1060. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1061. l=add_str(label);
  1062. set_label(l,script_pos, p);
  1063. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1064. syntax.curly_count--;
  1065. return p+1;
  1066. } else {
  1067. disp_error_message("parse_curly_close: unexpected string",p);
  1068. return p + 1;
  1069. }
  1070. }
  1071. // 構文関連の処理
  1072. // break, case, continue, default, do, for, function,
  1073. // if, switch, while をこの内部で処理します。
  1074. const char* parse_syntax(const char* p)
  1075. {
  1076. const char *p2 = skip_word(p);
  1077. switch(*p) {
  1078. case 'B':
  1079. case 'b':
  1080. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1081. // break の処理
  1082. char label[256];
  1083. int pos = syntax.curly_count - 1;
  1084. while(pos >= 0) {
  1085. if(syntax.curly[pos].type == TYPE_DO) {
  1086. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1087. break;
  1088. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1089. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1090. break;
  1091. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1092. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1093. break;
  1094. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1095. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1096. break;
  1097. }
  1098. pos--;
  1099. }
  1100. if(pos < 0) {
  1101. disp_error_message("parse_syntax: unexpected 'break'",p);
  1102. } else {
  1103. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1104. parse_line(label);
  1105. syntax.curly_count--;
  1106. }
  1107. p = skip_space(p2);
  1108. if(*p != ';')
  1109. disp_error_message("parse_syntax: need ';'",p);
  1110. // if, for , while の閉じ判定
  1111. p = parse_syntax_close(p + 1);
  1112. return p;
  1113. }
  1114. break;
  1115. case 'c':
  1116. case 'C':
  1117. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1118. // case の処理
  1119. int pos = syntax.curly_count-1;
  1120. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1121. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1122. return p+1;
  1123. } else {
  1124. char label[256];
  1125. int l,v;
  1126. char *np;
  1127. if(syntax.curly[pos].count != 1) {
  1128. // FALLTHRU 用のジャンプ
  1129. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1130. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1131. parse_line(label);
  1132. syntax.curly_count--;
  1133. // 現在地のラベルを付ける
  1134. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1135. l=add_str(label);
  1136. set_label(l,script_pos, p);
  1137. }
  1138. // switch 判定文
  1139. p = skip_space(p2);
  1140. if(p == p2) {
  1141. disp_error_message("parse_syntax: expect space ' '",p);
  1142. }
  1143. // check whether case label is integer or not
  1144. v = strtol(p,&np,0);
  1145. if(np == p) { //Check for constants
  1146. p2 = skip_word(p);
  1147. v = p2-p; // length of word at p2
  1148. memcpy(label,p,v);
  1149. label[v]='\0';
  1150. v = search_str(label);
  1151. if (v < 0 || str_data[v].type != C_INT)
  1152. disp_error_message("parse_syntax: 'case' label not integer",p);
  1153. v = str_data[v].val;
  1154. p = skip_word(p);
  1155. } else { //Numeric value
  1156. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1157. p++;
  1158. p = skip_word(p);
  1159. if(np != p)
  1160. disp_error_message("parse_syntax: 'case' label not integer",np);
  1161. }
  1162. p = skip_space(p);
  1163. if(*p != ':')
  1164. disp_error_message("parse_syntax: expect ':'",p);
  1165. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1166. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1167. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1168. // 2回parse しないとダメ
  1169. p2 = parse_line(label);
  1170. parse_line(p2);
  1171. syntax.curly_count--;
  1172. if(syntax.curly[pos].count != 1) {
  1173. // FALLTHRU 終了後のラベル
  1174. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1175. l=add_str(label);
  1176. set_label(l,script_pos,p);
  1177. }
  1178. // check duplication of case label [Rayce]
  1179. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1180. disp_error_message("parse_syntax: dup 'case'",p);
  1181. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1182. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1183. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1184. parse_line(label);
  1185. syntax.curly_count--;
  1186. syntax.curly[pos].count++;
  1187. }
  1188. return p + 1;
  1189. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1190. // continue の処理
  1191. char label[256];
  1192. int pos = syntax.curly_count - 1;
  1193. while(pos >= 0) {
  1194. if(syntax.curly[pos].type == TYPE_DO) {
  1195. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1196. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1197. break;
  1198. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1199. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1200. break;
  1201. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1202. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1203. break;
  1204. }
  1205. pos--;
  1206. }
  1207. if(pos < 0) {
  1208. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1209. } else {
  1210. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1211. parse_line(label);
  1212. syntax.curly_count--;
  1213. }
  1214. p = skip_space(p2);
  1215. if(*p != ';')
  1216. disp_error_message("parse_syntax: need ';'",p);
  1217. // if, for , while の閉じ判定
  1218. p = parse_syntax_close(p + 1);
  1219. return p;
  1220. }
  1221. break;
  1222. case 'd':
  1223. case 'D':
  1224. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1225. // switch - default の処理
  1226. int pos = syntax.curly_count-1;
  1227. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1228. disp_error_message("parse_syntax: unexpected 'default'",p);
  1229. } else if(syntax.curly[pos].flag) {
  1230. disp_error_message("parse_syntax: dup 'default'",p);
  1231. } else {
  1232. char label[256];
  1233. int l;
  1234. // 現在地のラベルを付ける
  1235. p = skip_space(p2);
  1236. if(*p != ':') {
  1237. disp_error_message("parse_syntax: need ':'",p);
  1238. }
  1239. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1240. l=add_str(label);
  1241. set_label(l,script_pos,p);
  1242. // 無条件で次のリンクに飛ばす
  1243. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1244. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1245. parse_line(label);
  1246. syntax.curly_count--;
  1247. // default のラベルを付ける
  1248. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1249. l=add_str(label);
  1250. set_label(l,script_pos,p);
  1251. syntax.curly[syntax.curly_count - 1].flag = 1;
  1252. syntax.curly[pos].count++;
  1253. }
  1254. return p + 1;
  1255. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1256. int l;
  1257. char label[256];
  1258. p=skip_space(p2);
  1259. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1260. syntax.curly[syntax.curly_count].count = 1;
  1261. syntax.curly[syntax.curly_count].index = syntax.index++;
  1262. syntax.curly[syntax.curly_count].flag = 0;
  1263. // 現在地のラベル形成する
  1264. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1265. l=add_str(label);
  1266. set_label(l,script_pos,p);
  1267. syntax.curly_count++;
  1268. return p;
  1269. }
  1270. break;
  1271. case 'f':
  1272. case 'F':
  1273. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1274. int l;
  1275. char label[256];
  1276. int pos = syntax.curly_count;
  1277. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1278. syntax.curly[syntax.curly_count].count = 1;
  1279. syntax.curly[syntax.curly_count].index = syntax.index++;
  1280. syntax.curly[syntax.curly_count].flag = 0;
  1281. syntax.curly_count++;
  1282. p=skip_space(p2);
  1283. if(*p != '(')
  1284. disp_error_message("parse_syntax: need '('",p);
  1285. p++;
  1286. // 初期化文を実行する
  1287. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1288. p=parse_line(p);
  1289. syntax.curly_count--;
  1290. // 条件判断開始のラベル形成する
  1291. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1292. l=add_str(label);
  1293. set_label(l,script_pos,p);
  1294. p=skip_space(p);
  1295. if(*p == ';') {
  1296. // for(;;) のパターンなので必ず真
  1297. ;
  1298. } else {
  1299. // 条件が偽なら終了地点に飛ばす
  1300. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1301. add_scriptl(add_str("jump_zero"));
  1302. add_scriptc(C_ARG);
  1303. p=parse_expr(p);
  1304. p=skip_space(p);
  1305. add_scriptl(add_str(label));
  1306. add_scriptc(C_FUNC);
  1307. }
  1308. if(*p != ';')
  1309. disp_error_message("parse_syntax: need ';'",p);
  1310. p++;
  1311. // ループ開始に飛ばす
  1312. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1313. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1314. parse_line(label);
  1315. syntax.curly_count--;
  1316. // 次のループへのラベル形成する
  1317. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1318. l=add_str(label);
  1319. set_label(l,script_pos,p);
  1320. // 次のループに入る時の処理
  1321. // for 最後の ')' を ';' として扱うフラグ
  1322. parse_syntax_for_flag = 1;
  1323. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1324. p=parse_line(p);
  1325. syntax.curly_count--;
  1326. parse_syntax_for_flag = 0;
  1327. // 条件判定処理に飛ばす
  1328. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1329. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1330. parse_line(label);
  1331. syntax.curly_count--;
  1332. // ループ開始のラベル付け
  1333. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1334. l=add_str(label);
  1335. set_label(l,script_pos,p);
  1336. return p;
  1337. }
  1338. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1339. {// internal script function
  1340. const char *func_name;
  1341. func_name = skip_space(p2);
  1342. p = skip_word(func_name);
  1343. if( p == func_name )
  1344. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1345. p2 = skip_space(p);
  1346. if( *p2 == ';' )
  1347. {// function <name> ;
  1348. // function declaration - just register the name
  1349. int l;
  1350. l = add_word(func_name);
  1351. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1352. str_data[l].type = C_USERFUNC;
  1353. // if, for , while の閉じ判定
  1354. p = parse_syntax_close(p2 + 1);
  1355. return p; }
  1356. else if(*p2 == '{')
  1357. {// function <name> <line/block of code>
  1358. char label[256];
  1359. int l;
  1360. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1361. syntax.curly[syntax.curly_count].count = 1;
  1362. syntax.curly[syntax.curly_count].index = syntax.index++;
  1363. syntax.curly[syntax.curly_count].flag = 0;
  1364. ++syntax.curly_count;
  1365. // Jump over the function code
  1366. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1367. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1368. ++syntax.curly_count;
  1369. parse_line(label);
  1370. --syntax.curly_count;
  1371. // Set the position of the function (label)
  1372. l=add_word(func_name);
  1373. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1374. str_data[l].type = C_USERFUNC;
  1375. set_label(l, script_pos, p);
  1376. if( parse_options&SCRIPT_USE_LABEL_DB )
  1377. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1378. return skip_space(p);
  1379. }
  1380. else
  1381. {
  1382. disp_error_message("expect ';' or '{' at function syntax",p);
  1383. }
  1384. }
  1385. break;
  1386. case 'i':
  1387. case 'I':
  1388. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1389. // if() の処理
  1390. char label[256];
  1391. p=skip_space(p2);
  1392. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1393. disp_error_message("need '('",p);
  1394. }
  1395. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1396. syntax.curly[syntax.curly_count].count = 1;
  1397. syntax.curly[syntax.curly_count].index = syntax.index++;
  1398. syntax.curly[syntax.curly_count].flag = 0;
  1399. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1400. syntax.curly_count++;
  1401. add_scriptl(add_str("jump_zero"));
  1402. add_scriptc(C_ARG);
  1403. p=parse_expr(p);
  1404. p=skip_space(p);
  1405. add_scriptl(add_str(label));
  1406. add_scriptc(C_FUNC);
  1407. return p;
  1408. }
  1409. break;
  1410. case 's':
  1411. case 'S':
  1412. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1413. // switch() の処理
  1414. char label[256];
  1415. p=skip_space(p2);
  1416. if(*p != '(') {
  1417. disp_error_message("need '('",p);
  1418. }
  1419. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1420. syntax.curly[syntax.curly_count].count = 1;
  1421. syntax.curly[syntax.curly_count].index = syntax.index++;
  1422. syntax.curly[syntax.curly_count].flag = 0;
  1423. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1424. syntax.curly_count++;
  1425. add_scriptl(add_str("set"));
  1426. add_scriptc(C_ARG);
  1427. add_scriptl(add_str(label));
  1428. p=parse_expr(p);
  1429. p=skip_space(p);
  1430. if(*p != '{') {
  1431. disp_error_message("parse_syntax: need '{'",p);
  1432. }
  1433. add_scriptc(C_FUNC);
  1434. return p + 1;
  1435. }
  1436. break;
  1437. case 'w':
  1438. case 'W':
  1439. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1440. int l;
  1441. char label[256];
  1442. p=skip_space(p2);
  1443. if(*p != '(') {
  1444. disp_error_message("need '('",p);
  1445. }
  1446. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1447. syntax.curly[syntax.curly_count].count = 1;
  1448. syntax.curly[syntax.curly_count].index = syntax.index++;
  1449. syntax.curly[syntax.curly_count].flag = 0;
  1450. // 条件判断開始のラベル形成する
  1451. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1452. l=add_str(label);
  1453. set_label(l,script_pos,p);
  1454. // 条件が偽なら終了地点に飛ばす
  1455. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1456. syntax.curly_count++;
  1457. add_scriptl(add_str("jump_zero"));
  1458. add_scriptc(C_ARG);
  1459. p=parse_expr(p);
  1460. p=skip_space(p);
  1461. add_scriptl(add_str(label));
  1462. add_scriptc(C_FUNC);
  1463. return p;
  1464. }
  1465. break;
  1466. }
  1467. return NULL;
  1468. }
  1469. const char* parse_syntax_close(const char *p) {
  1470. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1471. int flag;
  1472. do {
  1473. p = parse_syntax_close_sub(p,&flag);
  1474. } while(flag);
  1475. return p;
  1476. }
  1477. // if, for , while , do の閉じ判定
  1478. // flag == 1 : 閉じられた
  1479. // flag == 0 : 閉じられない
  1480. const char* parse_syntax_close_sub(const char* p,int* flag)
  1481. {
  1482. char label[256];
  1483. int pos = syntax.curly_count - 1;
  1484. int l;
  1485. *flag = 1;
  1486. if(syntax.curly_count <= 0) {
  1487. *flag = 0;
  1488. return p;
  1489. } else if(syntax.curly[pos].type == TYPE_IF) {
  1490. const char *bp = p;
  1491. const char *p2;
  1492. // if 最終場所へ飛ばす
  1493. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1494. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1495. parse_line(label);
  1496. syntax.curly_count--;
  1497. // 現在地のラベルを付ける
  1498. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1499. l=add_str(label);
  1500. set_label(l,script_pos,p);
  1501. syntax.curly[pos].count++;
  1502. p = skip_space(p);
  1503. p2 = skip_word(p);
  1504. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1505. // else or else - if
  1506. p = skip_space(p2);
  1507. p2 = skip_word(p);
  1508. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1509. // else - if
  1510. p=skip_space(p2);
  1511. if(*p != '(') {
  1512. disp_error_message("need '('",p);
  1513. }
  1514. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1515. add_scriptl(add_str("jump_zero"));
  1516. add_scriptc(C_ARG);
  1517. p=parse_expr(p);
  1518. p=skip_space(p);
  1519. add_scriptl(add_str(label));
  1520. add_scriptc(C_FUNC);
  1521. *flag = 0;
  1522. return p;
  1523. } else {
  1524. // else
  1525. if(!syntax.curly[pos].flag) {
  1526. syntax.curly[pos].flag = 1;
  1527. *flag = 0;
  1528. return p;
  1529. }
  1530. }
  1531. }
  1532. // if 閉じ
  1533. syntax.curly_count--;
  1534. // 最終地のラベルを付ける
  1535. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1536. l=add_str(label);
  1537. set_label(l,script_pos,p);
  1538. if(syntax.curly[pos].flag == 1) {
  1539. // このifに対するelseじゃないのでポインタの位置は同じ
  1540. return bp;
  1541. }
  1542. return p;
  1543. } else if(syntax.curly[pos].type == TYPE_DO) {
  1544. int l;
  1545. char label[256];
  1546. const char *p2;
  1547. if(syntax.curly[pos].flag) {
  1548. // 現在地のラベル形成する(continue でここに来る)
  1549. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1550. l=add_str(label);
  1551. set_label(l,script_pos,p);
  1552. }
  1553. // 条件が偽なら終了地点に飛ばす
  1554. p = skip_space(p);
  1555. p2 = skip_word(p);
  1556. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1557. disp_error_message("parse_syntax: need 'while'",p);
  1558. p = skip_space(p2);
  1559. if(*p != '(') {
  1560. disp_error_message("need '('",p);
  1561. }
  1562. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1563. add_scriptl(add_str("jump_zero"));
  1564. add_scriptc(C_ARG);
  1565. p=parse_expr(p);
  1566. p=skip_space(p);
  1567. add_scriptl(add_str(label));
  1568. add_scriptc(C_FUNC);
  1569. // 開始地点に飛ばす
  1570. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1571. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1572. parse_line(label);
  1573. syntax.curly_count--;
  1574. // 条件終了地点のラベル形成する
  1575. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1576. l=add_str(label);
  1577. set_label(l,script_pos,p);
  1578. p = skip_space(p);
  1579. if(*p != ';') {
  1580. disp_error_message("parse_syntax: need ';'",p);
  1581. return p+1;
  1582. }
  1583. p++;
  1584. syntax.curly_count--;
  1585. return p;
  1586. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1587. // 次のループに飛ばす
  1588. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1589. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1590. parse_line(label);
  1591. syntax.curly_count--;
  1592. // for 終了のラベル付け
  1593. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1594. l=add_str(label);
  1595. set_label(l,script_pos,p);
  1596. syntax.curly_count--;
  1597. return p;
  1598. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1599. // while 条件判断へ飛ばす
  1600. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1601. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1602. parse_line(label);
  1603. syntax.curly_count--;
  1604. // while 終了のラベル付け
  1605. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1606. l=add_str(label);
  1607. set_label(l,script_pos,p);
  1608. syntax.curly_count--;
  1609. return p;
  1610. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1611. int pos = syntax.curly_count-1;
  1612. char label[256];
  1613. int l;
  1614. // 戻す
  1615. sprintf(label,"return;");
  1616. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1617. parse_line(label);
  1618. syntax.curly_count--;
  1619. // 現在地のラベルを付ける
  1620. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1621. l=add_str(label);
  1622. set_label(l,script_pos,p);
  1623. syntax.curly_count--;
  1624. return p;
  1625. } else {
  1626. *flag = 0;
  1627. return p;
  1628. }
  1629. }
  1630. /*==========================================
  1631. * 組み込み関数の追加
  1632. *------------------------------------------*/
  1633. static void add_buildin_func(void)
  1634. {
  1635. int i,n;
  1636. const char* p;
  1637. for( i = 0; buildin_func[i].func; i++ )
  1638. {
  1639. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1640. // 'v' - value (either string or int or reference)
  1641. // 's' - string
  1642. // 'i' - int
  1643. // 'r' - reference (of a variable)
  1644. // 'l' - label
  1645. // '?' - one optional parameter
  1646. // '*' - unknown number of optional parameters
  1647. p = buildin_func[i].arg;
  1648. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1649. while( *p == '?' ) ++p;
  1650. if( *p == '*' ) ++p;
  1651. if( *p != 0){
  1652. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1653. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1654. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1655. } else {
  1656. n = add_str(buildin_func[i].name);
  1657. str_data[n].type = C_FUNC;
  1658. str_data[n].val = i;
  1659. str_data[n].func = buildin_func[i].func;
  1660. }
  1661. }
  1662. }
  1663. /*==========================================
  1664. * 定数データベースの読み込み
  1665. *------------------------------------------*/
  1666. static void read_constdb(void)
  1667. {
  1668. FILE *fp;
  1669. char line[1024],name[1024],val[1024];
  1670. int n,type;
  1671. sprintf(line, "%s/const.txt", db_path);
  1672. fp=fopen(line, "r");
  1673. if(fp==NULL){
  1674. ShowError("can't read %s\n", line);
  1675. return ;
  1676. }
  1677. while(fgets(line, sizeof(line), fp))
  1678. {
  1679. if(line[0]=='/' && line[1]=='/')
  1680. continue;
  1681. type=0;
  1682. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1683. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1684. n=add_str(name);
  1685. if(type==0)
  1686. str_data[n].type=C_INT;
  1687. else
  1688. str_data[n].type=C_PARAM;
  1689. str_data[n].val= (int)strtol(val,NULL,0);
  1690. }
  1691. }
  1692. fclose(fp);
  1693. }
  1694. /*==========================================
  1695. * エラー表示
  1696. *------------------------------------------*/
  1697. static const char* script_print_line(StringBuf* buf, const char* p, const char* mark, int line)
  1698. {
  1699. int i;
  1700. if( p == NULL || !p[0] ) return NULL;
  1701. if( line < 0 )
  1702. StringBuf_Printf(buf, "*% 5d : ", -line);
  1703. else
  1704. StringBuf_Printf(buf, " % 5d : ", line);
  1705. for(i=0;p[i] && p[i] != '\n';i++){
  1706. if(p + i != mark)
  1707. StringBuf_Printf(buf, "%c", p[i]);
  1708. else
  1709. StringBuf_Printf(buf, "\'%c\'", p[i]);
  1710. }
  1711. StringBuf_AppendStr(buf, "\n");
  1712. return p+i+(p[i] == '\n' ? 1 : 0);
  1713. }
  1714. void script_error(const char* src, const char* file, int start_line, const char* error_msg, const char* error_pos)
  1715. {
  1716. // エラーが発生した行を求める
  1717. int j;
  1718. int line = start_line;
  1719. const char *p;
  1720. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1721. StringBuf buf;
  1722. for(p=src;p && *p;line++){
  1723. const char *lineend=strchr(p,'\n');
  1724. if(lineend==NULL || error_pos<lineend){
  1725. break;
  1726. }
  1727. for( j = 0; j < 4; j++ ) {
  1728. linestart[j] = linestart[j+1];
  1729. }
  1730. linestart[4] = p;
  1731. p=lineend+1;
  1732. }
  1733. StringBuf_Init(&buf);
  1734. StringBuf_AppendStr(&buf, "\a\n");
  1735. StringBuf_Printf(&buf, "script error on %s line %d\n", file, line);
  1736. StringBuf_Printf(&buf, " %s\n", error_msg);
  1737. for(j = 0; j < 5; j++ ) {
  1738. script_print_line(&buf, linestart[j], NULL, line + j - 5);
  1739. }
  1740. p = script_print_line(&buf, p, error_pos, -line);
  1741. for(j = 0; j < 5; j++) {
  1742. p = script_print_line(&buf, p, NULL, line + j + 1 );
  1743. }
  1744. ShowError("%s", StringBuf_Value(&buf));
  1745. StringBuf_Destroy(&buf);
  1746. }
  1747. /*==========================================
  1748. * スクリプトの解析
  1749. *------------------------------------------*/
  1750. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1751. {
  1752. const char *p,*tmpp;
  1753. int i;
  1754. struct script_code* code = NULL;
  1755. static int first=1;
  1756. char end;
  1757. if( src == NULL )
  1758. return NULL;// empty script
  1759. memset(&syntax,0,sizeof(syntax));
  1760. if(first){
  1761. add_buildin_func();
  1762. read_constdb();
  1763. first=0;
  1764. }
  1765. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1766. script_pos=0;
  1767. script_size=SCRIPT_BLOCK_SIZE;
  1768. str_data[LABEL_NEXTLINE].type=C_NOP;
  1769. str_data[LABEL_NEXTLINE].backpatch=-1;
  1770. str_data[LABEL_NEXTLINE].label=-1;
  1771. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1772. if( options&SCRIPT_USE_LABEL_DB )
  1773. scriptlabel_db->clear(scriptlabel_db, NULL);
  1774. parse_options = options;
  1775. if( setjmp( error_jump ) != 0 ) {
  1776. //Restore program state when script has problems. [from jA]
  1777. int i;
  1778. const int size = ARRAYLENGTH(syntax.curly);
  1779. if( error_report )
  1780. script_error(src,file,line,error_msg,error_pos);
  1781. aFree( error_msg );
  1782. aFree( script_buf );
  1783. script_pos = 0;
  1784. script_size = 0;
  1785. script_buf = NULL;
  1786. for(i=LABEL_START;i<str_num;i++)
  1787. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1788. for(i=0; i<size; i++)
  1789. linkdb_final(&syntax.curly[i].case_label);
  1790. return NULL;
  1791. }
  1792. parse_syntax_for_flag=0;
  1793. p=src;
  1794. p=skip_space(p);
  1795. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1796. {// does not require brackets around the script
  1797. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1798. {// empty script and can return NULL
  1799. aFree( script_buf );
  1800. script_pos = 0;
  1801. script_size = 0;
  1802. script_buf = NULL;
  1803. return NULL;
  1804. }
  1805. end = '\0';
  1806. }
  1807. else
  1808. {// requires brackets around the script
  1809. if( *p != '{' )
  1810. disp_error_message("not found '{'",p);
  1811. p = skip_space(p+1);
  1812. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1813. {// empty script and can return NULL
  1814. aFree( script_buf );
  1815. script_pos = 0;
  1816. script_size = 0;
  1817. script_buf = NULL;
  1818. return NULL;
  1819. }
  1820. end = '}';
  1821. }
  1822. // clear references of labels, variables and internal functions
  1823. for(i=LABEL_START;i<str_num;i++){
  1824. if(
  1825. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1826. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1827. ){
  1828. str_data[i].type=C_NOP;
  1829. str_data[i].backpatch=-1;
  1830. str_data[i].label=-1;
  1831. }
  1832. }
  1833. while( syntax.curly_count != 0 || *p != end )
  1834. {
  1835. if( *p == '\0' )
  1836. disp_error_message("unexpected end of script",p);
  1837. // labelだけ特殊処理
  1838. tmpp=skip_space(skip_word(p));
  1839. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1840. i=add_word(p);
  1841. set_label(i,script_pos,p);
  1842. if( parse_options&SCRIPT_USE_LABEL_DB )
  1843. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1844. p=tmpp+1;
  1845. p=skip_space(p);
  1846. continue;
  1847. }
  1848. // 他は全部一緒くた
  1849. p=parse_line(p);
  1850. p=skip_space(p);
  1851. add_scriptc(C_EOL);
  1852. set_label(LABEL_NEXTLINE,script_pos,p);
  1853. str_data[LABEL_NEXTLINE].type=C_NOP;
  1854. str_data[LABEL_NEXTLINE].backpatch=-1;
  1855. str_data[LABEL_NEXTLINE].label=-1;
  1856. }
  1857. add_scriptc(C_NOP);
  1858. // trim code to size
  1859. script_size = script_pos;
  1860. RECREATE(script_buf,unsigned char,script_pos);
  1861. // default unknown references to variables
  1862. for(i=LABEL_START;i<str_num;i++){
  1863. if(str_data[i].type==C_NOP){
  1864. int j,next;
  1865. str_data[i].type=C_NAME;
  1866. str_data[i].label=i;
  1867. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1868. next=GETVALUE(script_buf,j);
  1869. SETVALUE(script_buf,j,i);
  1870. j=next;
  1871. }
  1872. }
  1873. }
  1874. #ifdef DEBUG_DISP
  1875. for(i=0;i<script_pos;i++){
  1876. if((i&15)==0) printf("%04x : ",i);
  1877. ShowMessage("%02x ",script_buf[i]);
  1878. if((i&15)==15) printf("\n");
  1879. }
  1880. ShowMessage("\n");
  1881. #endif
  1882. #ifdef DEBUG_DISASM
  1883. {
  1884. int i = 0,j;
  1885. while(i < script_pos) {
  1886. ShowMessage("%06x ",i);
  1887. j = i;
  1888. switch(get_com(script_buf,&i)) {
  1889. case C_EOL: printf("C_EOL"); break;
  1890. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1891. case C_POS:
  1892. ShowMessage("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1893. i += 3;
  1894. break;
  1895. case C_NAME:
  1896. j = (*(int*)(script_buf+i)&0xffffff);
  1897. ShowMessage("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1898. i += 3;
  1899. break;
  1900. case C_ARG: ShowMessage("C_ARG"); break;
  1901. case C_FUNC: ShowMessage("C_FUNC"); break;
  1902. case C_ADD: ShowMessage("C_ADD"); break;
  1903. case C_SUB: ShowMessage("C_SUB"); break;
  1904. case C_MUL: ShowMessage("C_MUL"); break;
  1905. case C_DIV: ShowMessage("C_DIV"); break;
  1906. case C_MOD: ShowMessage("C_MOD"); break;
  1907. case C_EQ: ShowMessage("C_EQ"); break;
  1908. case C_NE: ShowMessage("C_NE"); break;
  1909. case C_GT: ShowMessage("C_GT"); break;
  1910. case C_GE: ShowMessage("C_GE"); break;
  1911. case C_LT: ShowMessage("C_LT"); break;
  1912. case C_LE: ShowMessage("C_LE"); break;
  1913. case C_AND: ShowMessage("C_AND"); break;
  1914. case C_OR: ShowMessage("C_OR"); break;
  1915. case C_XOR: ShowMessage("C_XOR"); break;
  1916. case C_LAND: ShowMessage("C_LAND"); break;
  1917. case C_LOR: ShowMessage("C_LOR"); break;
  1918. case C_R_SHIFT: ShowMessage("C_R_SHIFT"); break;
  1919. case C_L_SHIFT: ShowMessage("C_L_SHIFT"); break;
  1920. case C_NEG: ShowMessage("C_NEG"); break;
  1921. case C_NOT: ShowMessage("C_NOT"); break;
  1922. case C_LNOT: ShowMessage("C_LNOT"); break;
  1923. case C_NOP: ShowMessage("C_NOP"); break;
  1924. case C_OP3: ShowMessage("C_OP3"); break;
  1925. case C_STR:
  1926. j = strlen(script_buf + i);
  1927. ShowMessage("C_STR %s",script_buf + i);
  1928. i+= j+1;
  1929. break;
  1930. default:
  1931. ShowMessage("unknown");
  1932. }
  1933. ShowMessage("\n");
  1934. }
  1935. }
  1936. #endif
  1937. CREATE(code,struct script_code,1);
  1938. code->script_buf = script_buf;
  1939. code->script_size = script_size;
  1940. code->script_vars = NULL;
  1941. return code;
  1942. }
  1943. //
  1944. // Script state
  1945. //
  1946. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1947. /// Returns the player attached to this script, identified by the rid.
  1948. /// If there is no player attached, the script is terminated.
  1949. TBL_PC *script_rid2sd(struct script_state *st)
  1950. {
  1951. TBL_PC *sd=map_id2sd(st->rid);
  1952. if(!sd){
  1953. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1954. script_reportsrc(st);
  1955. st->state = END;
  1956. }
  1957. return sd;
  1958. }
  1959. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1960. ///
  1961. /// @param st Script state
  1962. /// @param data Variable/constant
  1963. void get_val(struct script_state* st, struct script_data* data)
  1964. {
  1965. char* name;
  1966. char prefix;
  1967. char postfix;
  1968. TBL_PC* sd = NULL;
  1969. if( !data_isreference(data) )
  1970. return;// not a variable/constant
  1971. name = reference_getname(data);
  1972. prefix = name[0];
  1973. postfix = name[strlen(name) - 1];
  1974. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1975. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1976. {
  1977. sd = script_rid2sd(st);
  1978. if( sd == NULL )
  1979. {// needs player attached
  1980. if( postfix == '$' )
  1981. {// string variable
  1982. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1983. data->type = C_CONSTSTR;
  1984. data->u.str = "";
  1985. }
  1986. else
  1987. {// integer variable
  1988. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1989. data->type = C_INT;
  1990. data->u.num = 0;
  1991. }
  1992. return;
  1993. }
  1994. }
  1995. if( postfix == '$' )
  1996. {// string variable
  1997. switch( prefix )
  1998. {
  1999. case '@':
  2000. data->u.str = pc_readregstr(sd, data->u.num);
  2001. break;
  2002. case '$':
  2003. data->u.str = (char *)idb_get(mapregstr_db, data->u.num);
  2004. break;
  2005. case '#':
  2006. if( name[1] == '#' )
  2007. data->u.str = pc_readaccountreg2str(sd, name);// global
  2008. else
  2009. data->u.str = pc_readaccountregstr(sd, name);// local
  2010. break;
  2011. case '.':
  2012. {
  2013. struct linkdb_node** n =
  2014. data->ref ? data->ref:
  2015. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2016. &st->script->script_vars;// npc variable
  2017. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2018. }
  2019. break;
  2020. default:
  2021. data->u.str = pc_readglobalreg_str(sd, name);
  2022. break;
  2023. }
  2024. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2025. {// empty string
  2026. data->type = C_CONSTSTR;
  2027. data->u.str = "";
  2028. }
  2029. else
  2030. {// duplicate string
  2031. data->type = C_STR;
  2032. data->u.str = aStrdup(data->u.str);
  2033. }
  2034. }
  2035. else
  2036. {// integer variable
  2037. data->type = C_INT;
  2038. if( reference_toconstant(data) )
  2039. {
  2040. data->u.num = reference_getconstant(data);
  2041. }
  2042. else if( reference_toparam(data) )
  2043. {
  2044. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2045. }
  2046. else
  2047. switch( prefix )
  2048. {
  2049. case '@':
  2050. data->u.num = pc_readreg(sd, data->u.num);
  2051. break;
  2052. case '$':
  2053. data->u.num = (int)idb_get(mapreg_db, data->u.num);
  2054. break;
  2055. case '#':
  2056. if( name[1] == '#' )
  2057. data->u.num = pc_readaccountreg2(sd, name);// global
  2058. else
  2059. data->u.num = pc_readaccountreg(sd, name);// local
  2060. break;
  2061. case '.':
  2062. {
  2063. struct linkdb_node** n =
  2064. data->ref ? data->ref:
  2065. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2066. &st->script->script_vars;// npc variable
  2067. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2068. }
  2069. break;
  2070. default:
  2071. data->u.num = pc_readglobalreg(sd, name);
  2072. break;
  2073. }
  2074. }
  2075. return;
  2076. }
  2077. void push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2078. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2079. /// The value is left in the top of the stack and needs to be removed manually.
  2080. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2081. {
  2082. struct script_data* data;
  2083. push_val2(st->stack, C_NAME, uid, ref);
  2084. data = script_getdatatop(st, -1);
  2085. get_val(st, data);
  2086. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2087. }
  2088. /*==========================================
  2089. * Stores the value of a script variable
  2090. * Return value is 0 on fail, 1 on success.
  2091. *------------------------------------------*/
  2092. static int set_reg(struct script_state* st, TBL_PC* sd, int num, char* name, const void* value, struct linkdb_node** ref)
  2093. {
  2094. char prefix = name[0]; char postfix = name[strlen(name)-1];
  2095. if (postfix == '$') { // string variable
  2096. const char* str = (const char*)value;
  2097. switch (prefix) {
  2098. case '@':
  2099. return pc_setregstr(sd, num, str);
  2100. case '$':
  2101. return mapreg_setregstr(num, str);
  2102. case '#':
  2103. return (name[1] == '#') ?
  2104. pc_setaccountreg2str(sd, name, str) :
  2105. pc_setaccountregstr(sd, name, str);
  2106. case '.': {
  2107. char* p;
  2108. struct linkdb_node** n;
  2109. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2110. p = (char*)linkdb_erase(n, (void*)num);
  2111. if (p) aFree(p);
  2112. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2113. }
  2114. return 1;
  2115. default:
  2116. return pc_setglobalreg_str(sd, name, str);
  2117. }
  2118. } else { // integer variable
  2119. int val = (int)value;
  2120. if(str_data[num&0x00ffffff].type == C_PARAM)
  2121. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  2122. switch (prefix) {
  2123. case '@':
  2124. return pc_setreg(sd, num, val);
  2125. case '$':
  2126. return mapreg_setreg(num, val);
  2127. case '#':
  2128. return (name[1] == '#') ?
  2129. pc_setaccountreg2(sd, name, val) :
  2130. pc_setaccountreg(sd, name, val);
  2131. case '.': {
  2132. struct linkdb_node** n;
  2133. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2134. if (val == 0)
  2135. linkdb_erase(n, (void*)num);
  2136. else
  2137. linkdb_replace(n, (void*)num, (void*)val);
  2138. }
  2139. return 1;
  2140. default:
  2141. return pc_setglobalreg(sd, name, val);
  2142. }
  2143. }
  2144. }
  2145. int set_var(TBL_PC* sd, char* name, void* val)
  2146. {
  2147. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  2148. }
  2149. /// Converts the data to a string
  2150. const char* conv_str(struct script_state* st, struct script_data* data)
  2151. {
  2152. char* p;
  2153. get_val(st, data);
  2154. if( data_isstring(data) )
  2155. {// nothing to convert
  2156. }
  2157. else if( data_isint(data) )
  2158. {// int -> string
  2159. CREATE(p, char, ITEM_NAME_LENGTH);
  2160. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2161. p[ITEM_NAME_LENGTH-1] = '\0';
  2162. data->type = C_STR;
  2163. data->u.str = p;
  2164. }
  2165. else if( data_isreference(data) )
  2166. {// reference -> string
  2167. //##TODO when does this happen (check get_val) [FlavioJS]
  2168. data->type = C_CONSTSTR;
  2169. data->u.str = reference_getname(data);
  2170. }
  2171. else
  2172. {// unsupported data type
  2173. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2174. script_reportdata(data);
  2175. script_reportsrc(st);
  2176. data->type = C_CONSTSTR;
  2177. data->u.str = "";
  2178. }
  2179. return data->u.str;
  2180. }
  2181. /// Converts the data to an int
  2182. int conv_num(struct script_state* st, struct script_data* data)
  2183. {
  2184. char* p;
  2185. long num;
  2186. get_val(st, data);
  2187. if( data_isint(data) )
  2188. {// nothing to convert
  2189. }
  2190. else if( data_isstring(data) )
  2191. {// string -> int
  2192. // the result does not overflow or underflow, it is capped instead
  2193. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2194. p = data->u.str;
  2195. errno = 0;
  2196. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2197. if( errno == ERANGE
  2198. #if LONG_MAX > INT_MAX
  2199. || num < INT_MIN || num > INT_MAX
  2200. #endif
  2201. )
  2202. {
  2203. if( num <= INT_MIN )
  2204. {
  2205. num = INT_MIN;
  2206. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2207. }
  2208. else//if( num >= INT_MAX )
  2209. {
  2210. num = INT_MAX;
  2211. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2212. }
  2213. script_reportdata(data);
  2214. script_reportsrc(st);
  2215. }
  2216. if( data->type == C_STR )
  2217. aFree(p);
  2218. data->type = C_INT;
  2219. data->u.num = (int)num;
  2220. }
  2221. #if 0
  2222. // FIXME this function is being used to retrieve the position of labels and
  2223. // probably other stuff [FlavioJS]
  2224. else
  2225. {// unsupported data type
  2226. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2227. script_reportdata(data);
  2228. script_reportsrc(st);
  2229. data->type = C_INT;
  2230. data->u.num = 0;
  2231. }
  2232. #endif
  2233. return data->u.num;
  2234. }
  2235. //
  2236. // Stack operations
  2237. //
  2238. /// Increases the size of the stack
  2239. void stack_expand(struct script_stack* stack)
  2240. {
  2241. stack->sp_max += 64;
  2242. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2243. stack->sp_max * sizeof(stack->stack_data[0]) );
  2244. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2245. 64 * sizeof(stack->stack_data[0]) );
  2246. }
  2247. /// Pushes a value into the stack
  2248. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2249. /// Pushes a value into the stack (with reference)
  2250. void push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2251. {
  2252. if( stack->sp >= stack->sp_max )
  2253. stack_expand(stack);
  2254. stack->stack_data[stack->sp].type = type;
  2255. stack->stack_data[stack->sp].u.num = val;
  2256. stack->stack_data[stack->sp].ref = ref;
  2257. stack->sp++;
  2258. }
  2259. /// Pushes a string into the stack
  2260. void push_str(struct script_stack* stack, enum c_op type, char* str)
  2261. {
  2262. if( stack->sp >= stack->sp_max )
  2263. stack_expand(stack);
  2264. stack->stack_data[stack->sp].type = type;
  2265. stack->stack_data[stack->sp].u.str = str;
  2266. stack->stack_data[stack->sp].ref = NULL;
  2267. stack->sp++;
  2268. }
  2269. /// Pushes a copy of the target position into the stack
  2270. void push_copy(struct script_stack* stack, int pos)
  2271. {
  2272. switch( stack->stack_data[pos].type )
  2273. {
  2274. case C_CONSTSTR:
  2275. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2276. break;
  2277. case C_STR:
  2278. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2279. break;
  2280. default:
  2281. push_val2(
  2282. stack,stack->stack_data[pos].type,
  2283. stack->stack_data[pos].u.num,
  2284. stack->stack_data[pos].ref
  2285. );
  2286. break;
  2287. }
  2288. }
  2289. /// Removes the values in indexes [start,end[ from the stack
  2290. void pop_stack(struct script_stack* stack, int start, int end)
  2291. {
  2292. struct script_data* data;
  2293. int i;
  2294. if( start < 0 )
  2295. start = 0;
  2296. if( end > stack->sp_max )
  2297. end = stack->sp_max;
  2298. if( start >= end )
  2299. return;// nothing to pop
  2300. // free stack elements
  2301. for( i = start; i < end; i++ )
  2302. {
  2303. data = &stack->stack_data[i];
  2304. if( data->type == C_STR )
  2305. aFree(data->u.str);
  2306. data->type = C_NOP;
  2307. }
  2308. // move the rest of the elements
  2309. if( stack->sp > end )
  2310. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2311. stack->sp -= end - start;
  2312. }
  2313. ///
  2314. ///
  2315. ///
  2316. /*==========================================
  2317. * スクリプト依存変数、関数依存変数の解放
  2318. *------------------------------------------*/
  2319. void script_free_vars(struct linkdb_node **node)
  2320. {
  2321. struct linkdb_node *n = *node;
  2322. while(n) {
  2323. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2324. char postfix = name[strlen(name)-1];
  2325. if( postfix == '$' ) {
  2326. // 文字型変数なので、データ削除
  2327. aFree(n->data);
  2328. }
  2329. n = n->next;
  2330. }
  2331. linkdb_final( node );
  2332. }
  2333. /*==========================================
  2334. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2335. *------------------------------------------*/
  2336. void script_free_stack(struct script_stack *stack)
  2337. {
  2338. int i;
  2339. for(i = 0; i < stack->sp; i++) {
  2340. if( stack->stack_data[i].type == C_STR ) {
  2341. aFree(stack->stack_data[i].u.str);
  2342. stack->stack_data[i].type = C_INT;
  2343. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2344. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2345. script_free_vars( n );
  2346. aFree( n );
  2347. }
  2348. }
  2349. script_free_vars( stack->var_function );
  2350. aFree(stack->var_function);
  2351. aFree(stack->stack_data);
  2352. aFree(stack);
  2353. }
  2354. void script_free_code(struct script_code* code)
  2355. {
  2356. script_free_vars( &code->script_vars );
  2357. aFree( code->script_buf );
  2358. aFree( code );
  2359. }
  2360. //
  2361. // 実行部main
  2362. //
  2363. /*==========================================
  2364. * コマンドの読み取り
  2365. *------------------------------------------*/
  2366. c_op get_com(unsigned char *script,int *pos)
  2367. {
  2368. int i = 0, j = 0;
  2369. if(script[*pos]>=0x80){
  2370. return C_INT;
  2371. }
  2372. while(script[*pos]>=0x40){
  2373. i=script[(*pos)++]<<j;
  2374. j+=6;
  2375. }
  2376. return (c_op)(i+(script[(*pos)++]<<j));
  2377. }
  2378. /*==========================================
  2379. * 数値の所得
  2380. *------------------------------------------*/
  2381. int get_num(unsigned char *script,int *pos)
  2382. {
  2383. int i,j;
  2384. i=0; j=0;
  2385. while(script[*pos]>=0xc0){
  2386. i+=(script[(*pos)++]&0x7f)<<j;
  2387. j+=6;
  2388. }
  2389. return i+((script[(*pos)++]&0x7f)<<j);
  2390. }
  2391. /*==========================================
  2392. * スタックから値を取り出す
  2393. *------------------------------------------*/
  2394. int pop_val(struct script_state* st)
  2395. {
  2396. if(st->stack->sp<=0)
  2397. return 0;
  2398. st->stack->sp--;
  2399. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2400. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2401. return st->stack->stack_data[st->stack->sp].u.num;
  2402. return 0;
  2403. }
  2404. int isstr(struct script_data *c)
  2405. {
  2406. if( data_isstring(c) )
  2407. return 1;
  2408. else if( data_isreference(c) ) {
  2409. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2410. char postfix = p[strlen(p)-1];
  2411. return (postfix == '$');
  2412. }
  2413. return 0;
  2414. }
  2415. /// Ternary operators
  2416. /// test ? if_true : if_false
  2417. void op_3(struct script_state* st, int op)
  2418. {
  2419. struct script_data* data;
  2420. int flag = 0;
  2421. data = script_getdatatop(st, -3);
  2422. get_val(st, data);
  2423. if( data_isstring(data) )
  2424. flag = data->u.str[0];// "" -> false
  2425. else if( data_isint(data) )
  2426. flag = data->u.num;// 0 -> false
  2427. else
  2428. {
  2429. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2430. script_reportdata(data);
  2431. script_reportsrc(st);
  2432. script_removetop(st, -3, 0);
  2433. script_pushnil(st);
  2434. return;
  2435. }
  2436. if( flag )
  2437. script_pushcopytop(st, -2);
  2438. else
  2439. script_pushcopytop(st, -1);
  2440. script_removetop(st, -4, -1);
  2441. }
  2442. /// Binary string operators
  2443. /// s1 EQ s2 -> i
  2444. /// s1 NE s2 -> i
  2445. /// s1 GT s2 -> i
  2446. /// s1 GE s2 -> i
  2447. /// s1 LT s2 -> i
  2448. /// s1 LE s2 -> i
  2449. /// s1 ADD s2 -> s
  2450. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2451. {
  2452. int a = 0;
  2453. switch(op){
  2454. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2455. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2456. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2457. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2458. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2459. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2460. case C_ADD:
  2461. {
  2462. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2463. strcpy(buf, s1);
  2464. strcat(buf, s2);
  2465. script_pushstr(st, buf);
  2466. return;
  2467. }
  2468. default:
  2469. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2470. script_reportsrc(st);
  2471. script_pushnil(st);
  2472. st->state = END;
  2473. return;
  2474. }
  2475. script_pushint(st,a);
  2476. }
  2477. /// Binary number operators
  2478. /// i OP i -> i
  2479. void op_2num(struct script_state* st, int op, int i1, int i2)
  2480. {
  2481. int ret;
  2482. double ret_double;
  2483. switch( op )
  2484. {
  2485. case C_AND: ret = i1 & i2; break;
  2486. case C_OR: ret = i1 | i2; break;
  2487. case C_XOR: ret = i1 ^ i2; break;
  2488. case C_LAND: ret = (i1 && i2); break;
  2489. case C_LOR: ret = (i1 || i2); break;
  2490. case C_EQ: ret = (i1 == i2); break;
  2491. case C_NE: ret = (i1 != i2); break;
  2492. case C_GT: ret = (i1 > i2); break;
  2493. case C_GE: ret = (i1 >= i2); break;
  2494. case C_LT: ret = (i1 < i2); break;
  2495. case C_LE: ret = (i1 <= i2); break;
  2496. case C_R_SHIFT: ret = i1>>i2; break;
  2497. case C_L_SHIFT: ret = i1<<i2; break;
  2498. case C_DIV:
  2499. case C_MOD:
  2500. if( i2 == 0 )
  2501. {
  2502. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2503. script_reportsrc(st);
  2504. script_pushnil(st);
  2505. st->state = END;
  2506. return;
  2507. }
  2508. else if( op == C_DIV )
  2509. ret = i1 / i2;
  2510. else//if( op == C_MOD )
  2511. ret = i1 % i2;
  2512. break;
  2513. default:
  2514. switch( op )
  2515. {// operators that can overflow/underflow
  2516. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2517. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2518. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2519. default:
  2520. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2521. script_reportsrc(st);
  2522. script_pushnil(st);
  2523. return;
  2524. }
  2525. if( ret_double < (double)INT_MIN )
  2526. {
  2527. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2528. script_reportsrc(st);
  2529. ret = INT_MIN;
  2530. }
  2531. else if( ret_double > (double)INT_MAX )
  2532. {
  2533. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2534. script_reportsrc(st);
  2535. ret = INT_MAX;
  2536. }
  2537. }
  2538. script_pushint(st, ret);
  2539. }
  2540. /// Binary operators
  2541. void op_2(struct script_state *st, int op)
  2542. {
  2543. struct script_data* left;
  2544. struct script_data* right;
  2545. left = script_getdatatop(st, -2);
  2546. right = script_getdatatop(st, -1);
  2547. get_val(st, left);
  2548. get_val(st, right);
  2549. // automatic conversions
  2550. switch( op )
  2551. {
  2552. case C_ADD:
  2553. if( data_isint(left) && data_isstring(right) )
  2554. {// convert int-string to string-string
  2555. conv_str(st, left);
  2556. }
  2557. else if( data_isstring(left) && data_isint(right) )
  2558. {// convert string-int to string-string
  2559. conv_str(st, right);
  2560. }
  2561. break;
  2562. }
  2563. if( data_isstring(left) && data_isstring(right) )
  2564. {// ss => op_2str
  2565. op_2str(st, op, left->u.str, right->u.str);
  2566. script_removetop(st, -3, -1);// pop the two values before the top one
  2567. }
  2568. else if( data_isint(left) && data_isint(right) )
  2569. {// ii => op_2num
  2570. int i1 = left->u.num;
  2571. int i2 = right->u.num;
  2572. script_removetop(st, -2, 0);
  2573. op_2num(st, op, i1, i2);
  2574. }
  2575. else
  2576. {// invalid argument
  2577. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2578. script_reportdata(left);
  2579. script_reportdata(right);
  2580. script_reportsrc(st);
  2581. script_removetop(st, -2, 0);
  2582. script_pushnil(st);
  2583. st->state = END;
  2584. }
  2585. }
  2586. /// Unary operators
  2587. /// NEG i -> i
  2588. /// NOT i -> i
  2589. /// LNOT i -> i
  2590. void op_1(struct script_state* st, int op)
  2591. {
  2592. struct script_data* data;
  2593. int i1;
  2594. data = script_getdatatop(st, -1);
  2595. get_val(st, data);
  2596. if( !data_isint(data) )
  2597. {// not a number
  2598. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2599. script_reportdata(data);
  2600. script_reportsrc(st);
  2601. script_pushnil(st);
  2602. st->state = END;
  2603. return;
  2604. }
  2605. i1 = data->u.num;
  2606. script_removetop(st, -1, 0);
  2607. switch( op )
  2608. {
  2609. case C_NEG: i1 = -i1; break;
  2610. case C_NOT: i1 = ~i1; break;
  2611. case C_LNOT: i1 = !i1; break;
  2612. default:
  2613. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2614. script_reportsrc(st);
  2615. script_pushnil(st);
  2616. st->state = END;
  2617. return;
  2618. }
  2619. script_pushint(st, i1);
  2620. }
  2621. /*==========================================
  2622. * 関数の実行
  2623. *------------------------------------------*/
  2624. int run_func(struct script_state *st)
  2625. {
  2626. int i,start_sp,end_sp,func;
  2627. end_sp=st->stack->sp;
  2628. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2629. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2630. ShowError("function not found\n");
  2631. // st->stack->sp=0;
  2632. st->state=END;
  2633. script_reportsrc(st);
  2634. return 1;
  2635. }
  2636. start_sp=i-1;
  2637. st->start=i-1;
  2638. st->end=end_sp;
  2639. func=st->stack->stack_data[st->start].u.num;
  2640. #ifdef DEBUG_RUN
  2641. if(battle_config.etc_log) {
  2642. ShowDebug("run_func : %s? (%d(%d)) sp=%d (%d...%d)\n",str_buf+str_data[func].str, func, str_data[func].type, st->stack->sp, st->start, st->end);
  2643. ShowDebug("stack dump :");
  2644. for(i=0;i<end_sp;i++){
  2645. switch(st->stack->stack_data[i].type){
  2646. case C_INT:
  2647. ShowMessage(" int(%d)",st->stack->stack_data[i].u.num);
  2648. break;
  2649. case C_NAME:
  2650. ShowMessage(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2651. break;
  2652. case C_ARG:
  2653. ShowMessage(" arg");
  2654. break;
  2655. case C_POS:
  2656. ShowMessage(" pos(%d)",st->stack->stack_data[i].u.num);
  2657. break;
  2658. case C_STR:
  2659. ShowMessage(" str(%s)",st->stack->stack_data[i].u.str);
  2660. break;
  2661. case C_CONSTSTR:
  2662. ShowMessage(" cstr(%s)",st->stack->stack_data[i].u.str);
  2663. break;
  2664. default:
  2665. ShowMessage(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2666. }
  2667. }
  2668. ShowMessage("\n");
  2669. }
  2670. #endif
  2671. if(str_data[func].type!=C_FUNC ){
  2672. ShowError("run_func: '"CL_WHITE"%s"CL_RESET"' (type %d) is not function and command!\n", str_buf+str_data[func].str, str_data[func].type);
  2673. // st->stack->sp=0;
  2674. st->state=END;
  2675. script_reportsrc(st);
  2676. return 1;
  2677. }
  2678. #ifdef DEBUG_RUN
  2679. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2680. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2681. );
  2682. #endif
  2683. if(str_data[func].func){
  2684. if (str_data[func].func(st)) //Report error
  2685. script_reportsrc(st);
  2686. } else {
  2687. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2688. script_pushint(st,0);
  2689. script_reportsrc(st);
  2690. }
  2691. // Stack's datum are used when re-run functions [Eoe]
  2692. if(st->state != RERUNLINE) {
  2693. pop_stack(st->stack,start_sp,end_sp);
  2694. }
  2695. if(st->state==RETFUNC){
  2696. // ユーザー定義関数からの復帰
  2697. int olddefsp=st->stack->defsp;
  2698. int i;
  2699. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2700. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2701. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2702. st->state=END;
  2703. script_reportsrc(st);
  2704. return 1;
  2705. }
  2706. script_free_vars( st->stack->var_function );
  2707. aFree(st->stack->var_function);
  2708. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2709. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2710. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2711. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2712. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2713. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2714. st->state=GOTO;
  2715. }
  2716. return 0;
  2717. }
  2718. /*==========================================
  2719. * スクリプトの実行
  2720. *------------------------------------------*/
  2721. void run_script_main(struct script_state *st);
  2722. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2723. {
  2724. struct script_state *st;
  2725. TBL_PC *sd=NULL;
  2726. if(rootscript==NULL || pos<0)
  2727. return;
  2728. if (rid) sd = map_id2sd(rid);
  2729. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2730. //Resume script.
  2731. st = sd->st;
  2732. } else {
  2733. st = (struct script_state*)aCalloc(sizeof(struct script_state), 1);
  2734. // the script is different, make new script_state and stack
  2735. st->stack = (struct script_stack*)aMalloc (sizeof(struct script_stack));
  2736. st->stack->sp=0;
  2737. st->stack->sp_max=64;
  2738. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2739. st->stack->defsp = st->stack->sp;
  2740. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  2741. st->state = RUN;
  2742. st->script = rootscript;
  2743. }
  2744. st->pos = pos;
  2745. st->rid = rid;
  2746. st->oid = oid;
  2747. st->sleep.timer = INVALID_TIMER;
  2748. run_script_main(st);
  2749. }
  2750. void script_stop_sleeptimers(int id)
  2751. {
  2752. struct script_state* st;
  2753. for(;;)
  2754. {
  2755. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2756. if( st == NULL )
  2757. break; // no more sleep timers
  2758. if( st->sleep.timer != INVALID_TIMER )
  2759. delete_timer(st->sleep.timer, run_script_timer);
  2760. script_free_stack(st->stack);
  2761. aFree(st);
  2762. }
  2763. }
  2764. /*==========================================
  2765. * 指定ノードをsleep_dbから削除
  2766. *------------------------------------------*/
  2767. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2768. {
  2769. struct linkdb_node *retnode;
  2770. if( n == NULL)
  2771. return NULL;
  2772. if( n->prev == NULL )
  2773. sleep_db = n->next;
  2774. else
  2775. n->prev->next = n->next;
  2776. if( n->next )
  2777. n->next->prev = n->prev;
  2778. retnode = n->next;
  2779. aFree( n );
  2780. return retnode; // 次のノードを返す
  2781. }
  2782. /*==========================================
  2783. * sleep用タイマー関数
  2784. *------------------------------------------*/
  2785. int run_script_timer(int tid, unsigned int tick, int id, intptr data)
  2786. {
  2787. struct script_state *st = (struct script_state *)data;
  2788. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2789. TBL_PC *sd = map_id2sd(st->rid);
  2790. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2791. { //Character mismatch. Cancel execution.
  2792. st->rid = 0;
  2793. st->state = END;
  2794. }
  2795. while( node && st->sleep.timer != -1 ) {
  2796. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2797. script_erase_sleepdb(node);
  2798. st->sleep.timer = -1;
  2799. break;
  2800. }
  2801. node = node->next;
  2802. }
  2803. if(st->state != RERUNLINE)
  2804. st->sleep.tick = 0;
  2805. run_script_main(st);
  2806. return 0;
  2807. }
  2808. /*==========================================
  2809. * スクリプトの実行メイン部分
  2810. *------------------------------------------*/
  2811. void run_script_main(struct script_state *st)
  2812. {
  2813. int cmdcount=script_config.check_cmdcount;
  2814. int gotocount=script_config.check_gotocount;
  2815. TBL_PC *sd;
  2816. //For backing up purposes
  2817. struct script_state *bk_st = NULL;
  2818. int bk_npcid = 0;
  2819. struct script_stack *stack=st->stack;
  2820. sd = st->rid?map_id2sd(st->rid):NULL;
  2821. if(sd){
  2822. if(sd->st != st){
  2823. bk_st = sd->st;
  2824. bk_npcid = sd->npc_id;
  2825. }
  2826. sd->st = st;
  2827. sd->npc_id = st->oid;
  2828. }
  2829. if(st->state == RERUNLINE) {
  2830. st->state = RUN;
  2831. run_func(st);
  2832. if(st->state == GOTO)
  2833. st->state = RUN;
  2834. } else if(st->state != END)
  2835. st->state = RUN;
  2836. while(st->state == RUN)
  2837. {
  2838. enum c_op c = get_com(st->script->script_buf,&st->pos);
  2839. switch(c){
  2840. case C_EOL:
  2841. if( stack->sp != stack->defsp )
  2842. {
  2843. if( stack->sp > stack->defsp )
  2844. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2845. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2846. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2847. } else
  2848. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2849. stack->sp = stack->defsp;
  2850. }
  2851. break;
  2852. case C_INT:
  2853. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2854. break;
  2855. case C_POS:
  2856. case C_NAME:
  2857. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2858. st->pos+=3;
  2859. break;
  2860. case C_ARG:
  2861. push_val(stack,c,0);
  2862. break;
  2863. case C_STR:
  2864. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2865. while(st->script->script_buf[st->pos++]);
  2866. break;
  2867. case C_FUNC:
  2868. run_func(st);
  2869. if(st->state==GOTO){
  2870. st->state = RUN;
  2871. if( gotocount>0 && (--gotocount)<=0 ){
  2872. ShowError("run_script: infinity loop !\n");
  2873. script_reportsrc(st);
  2874. st->state=END;
  2875. }
  2876. }
  2877. break;
  2878. case C_NEG:
  2879. case C_NOT:
  2880. case C_LNOT:
  2881. op_1(st ,c);
  2882. break;
  2883. case C_ADD:
  2884. case C_SUB:
  2885. case C_MUL:
  2886. case C_DIV:
  2887. case C_MOD:
  2888. case C_EQ:
  2889. case C_NE:
  2890. case C_GT:
  2891. case C_GE:
  2892. case C_LT:
  2893. case C_LE:
  2894. case C_AND:
  2895. case C_OR:
  2896. case C_XOR:
  2897. case C_LAND:
  2898. case C_LOR:
  2899. case C_R_SHIFT:
  2900. case C_L_SHIFT:
  2901. op_2(st, c);
  2902. break;
  2903. case C_OP3:
  2904. op_3(st, c);
  2905. break;
  2906. case C_NOP:
  2907. st->state=END;
  2908. break;
  2909. default:
  2910. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2911. st->state=END;
  2912. break;
  2913. }
  2914. if( cmdcount>0 && (--cmdcount)<=0 ){
  2915. ShowError("run_script: infinity loop !\n");
  2916. script_reportsrc(st);
  2917. st->state=END;
  2918. }
  2919. }
  2920. if(st->sleep.tick > 0) {
  2921. //Delay execution
  2922. st->sleep.charid = sd?sd->status.char_id:0;
  2923. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2924. run_script_timer, st->sleep.charid, (int)st);
  2925. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2926. //Restore previous script
  2927. if (sd) {
  2928. sd->st = bk_st;
  2929. sd->npc_id = bk_npcid;
  2930. bk_st = NULL; //Remove tag for removal.
  2931. }
  2932. }
  2933. else if(st->state != END && sd){
  2934. //Resume later (st is already attached to player).
  2935. if(bk_st) {
  2936. ShowWarning("Unable to restore stack! Double continuation!\n");
  2937. //Report BOTH scripts to see if that can help somehow.
  2938. ShowDebug("Previous script (lost):\n");
  2939. script_reportsrc(bk_st);
  2940. ShowDebug("Current script:\n");
  2941. script_reportsrc(st);
  2942. }
  2943. } else {
  2944. //Dispose of script.
  2945. if (sd)
  2946. { //Restore previous stack and save char.
  2947. if(sd->state.using_fake_npc){
  2948. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2949. sd->state.using_fake_npc = 0;
  2950. }
  2951. //Restore previous script if any.
  2952. sd->st = bk_st;
  2953. sd->npc_id = bk_npcid;
  2954. if (!bk_st)
  2955. npc_event_dequeue(sd);
  2956. else
  2957. bk_st = NULL; //Remove tag for removal.
  2958. if (sd->state.reg_dirty&2)
  2959. intif_saveregistry(sd,2);
  2960. if (sd->state.reg_dirty&1)
  2961. intif_saveregistry(sd,1);
  2962. }
  2963. st->pos = -1;
  2964. script_free_stack (st->stack);
  2965. aFree(st);
  2966. }
  2967. if (bk_st)
  2968. { //Remove previous script
  2969. bk_st->pos = -1;
  2970. script_free_stack(bk_st->stack);
  2971. aFree(bk_st);
  2972. bk_st = NULL;
  2973. }
  2974. }
  2975. /*==========================================
  2976. * マップ変数の変更
  2977. *------------------------------------------*/
  2978. int mapreg_setreg(int num, int val)
  2979. {
  2980. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2981. int i = num >> 24;
  2982. char* name = str_buf + str_data[num&0x00ffffff].str;
  2983. if( val != 0 ) {
  2984. if(idb_put(mapreg_db,num,(void*)val))
  2985. ;
  2986. else if(name[1] != '@') {
  2987. char tmp_str[32*2+1];
  2988. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  2989. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%d')", mapregsql_db, mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, tmp_str, i, val) )
  2990. Sql_ShowDebug(mmysql_handle);
  2991. }
  2992. } else { // val == 0
  2993. if(name[1] != '@') { // Remove from database because it is unused.
  2994. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_varname, name, mapregsql_db_index, i) )
  2995. Sql_ShowDebug(mmysql_handle);
  2996. }
  2997. idb_remove(mapreg_db,num);
  2998. }
  2999. #else
  3000. if(val != 0)
  3001. idb_put(mapreg_db,num,(void*)val);
  3002. else
  3003. idb_remove(mapreg_db,num);
  3004. #endif
  3005. mapreg_dirty = 1;
  3006. return 1;
  3007. }
  3008. /*==========================================
  3009. * 文字列型マップ変数の変更
  3010. *------------------------------------------*/
  3011. int mapreg_setregstr(int num, const char* str)
  3012. {
  3013. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  3014. int i = num >> 24;
  3015. char* name = str_buf + str_data[num&0x00ffffff].str;
  3016. if( str==NULL || *str==0 ) {
  3017. if(name[1] != '@') {
  3018. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3019. Sql_ShowDebug(mmysql_handle);
  3020. }
  3021. idb_remove(mapregstr_db,num);
  3022. mapreg_dirty = 1;
  3023. return 1;
  3024. }
  3025. if (idb_put(mapregstr_db,num, aStrdup(str)))
  3026. ;
  3027. else if(name[1] != '@') { //put returned null, so we must insert.
  3028. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  3029. char tmp_str[32*2+1];
  3030. char tmp_str2[255*2+1];
  3031. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  3032. Sql_EscapeStringLen(mmysql_handle, tmp_str2, str, strnlen(str, 255));
  3033. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%s')", mapregsql_db, mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, tmp_str, i, tmp_str2) )
  3034. Sql_ShowDebug(mmysql_handle);
  3035. }
  3036. #else
  3037. if( str==NULL || *str==0 )
  3038. idb_remove(mapregstr_db,num);
  3039. else
  3040. idb_put(mapregstr_db,num,aStrdup(str));
  3041. #endif
  3042. mapreg_dirty = 1;
  3043. return 1;
  3044. }
  3045. /*==========================================
  3046. * 永続的マップ変数の読み込み
  3047. *------------------------------------------*/
  3048. static int script_load_mapreg(void)
  3049. {
  3050. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3051. FILE* fp;
  3052. char line[1024];
  3053. if( (fp=fopen(mapreg_txt,"rt")) == NULL )
  3054. return -1;
  3055. while(fgets(line,sizeof(line),fp))
  3056. {
  3057. char buf1[256],buf2[1024];
  3058. char* p;
  3059. int n,v,s,i;
  3060. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  3061. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  3062. continue;
  3063. if( buf1[strlen(buf1)-1] == '$' ) {
  3064. if( sscanf(line + n, "%[^\n\r]", buf2) != 1 ) {
  3065. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3066. continue;
  3067. }
  3068. p = aStrdup(buf2);
  3069. s = add_str(buf1);
  3070. idb_put(mapregstr_db, (i<<24)|s, p);
  3071. } else {
  3072. if( sscanf(line + n, "%d", &v) != 1 ) {
  3073. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3074. continue;
  3075. }
  3076. s = add_str(buf1);
  3077. idb_put(mapreg_db, (i<<24)|s, (void*)v);
  3078. }
  3079. }
  3080. fclose(fp);
  3081. mapreg_dirty = 0;
  3082. return 0;
  3083. #else
  3084. /*
  3085. 0 1 2
  3086. +-------------------------+
  3087. | varname | index | value |
  3088. +-------------------------+
  3089. */
  3090. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  3091. char varname[32+1];
  3092. int index;
  3093. char value[255+1];
  3094. uint32 length;
  3095. if ( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `%s`, `%s`, `%s` FROM `%s`", mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, mapregsql_db)
  3096. || SQL_ERROR == SqlStmt_Execute(stmt)
  3097. ) {
  3098. SqlStmt_ShowDebug(stmt);
  3099. SqlStmt_Free(stmt);
  3100. return -1;
  3101. }
  3102. SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &varname[0], 32, &length, NULL);
  3103. SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &index, 0, NULL, NULL);
  3104. SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &value[0], 255, NULL, NULL);
  3105. while ( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  3106. {
  3107. if( varname[length-1] == '$' ) {
  3108. int s = add_str(varname);
  3109. int i = index;
  3110. char* p = aStrdup(value);
  3111. idb_put(mapregstr_db, (i<<24)|s, p);
  3112. } else {
  3113. int s = add_str(varname);
  3114. int i = index;
  3115. int v = atoi(value);
  3116. idb_put(mapreg_db, (i<<24)|s, (void *)v);
  3117. }
  3118. }
  3119. SqlStmt_Free(stmt);
  3120. mapreg_dirty = 0;
  3121. return 0;
  3122. #endif /* TXT_ONLY */
  3123. }
  3124. /*==========================================
  3125. * 永続的マップ変数の書き込み
  3126. *------------------------------------------*/
  3127. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  3128. {
  3129. int num=key.i&0x00ffffff, i=key.i>>24;
  3130. char *name=str_buf+str_data[num].str;
  3131. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3132. FILE *fp=va_arg(ap,FILE*);
  3133. if( name[1]!='@' ) {
  3134. if(i==0)
  3135. fprintf(fp,"%s\t%d\n", name, (int)data);
  3136. else
  3137. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  3138. }
  3139. #else
  3140. if ( name[1] != '@') {
  3141. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `%s`='%d' WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_value, (int)data, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3142. Sql_ShowDebug(mmysql_handle);
  3143. }
  3144. #endif
  3145. return 0;
  3146. }
  3147. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  3148. {
  3149. int num=key.i&0x00ffffff, i=key.i>>24;
  3150. char *name=str_buf+str_data[num].str;
  3151. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3152. FILE *fp=va_arg(ap,FILE*);
  3153. if( name[1]!='@' ) {
  3154. if(i==0)
  3155. fprintf(fp,"%s\t%s\n", name, (char *)data);
  3156. else
  3157. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  3158. }
  3159. #else
  3160. if ( name[1] != '@') {
  3161. char tmp_str2[2*255+1];
  3162. Sql_EscapeStringLen(mmysql_handle, tmp_str2, (char*)data, safestrnlen((char*)data, 255));
  3163. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `%s`='%s' WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_value, tmp_str2, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3164. Sql_ShowDebug(mmysql_handle);
  3165. }
  3166. #endif
  3167. return 0;
  3168. }
  3169. static int script_save_mapreg(void)
  3170. {
  3171. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3172. FILE *fp;
  3173. int lock;
  3174. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  3175. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  3176. return -1;
  3177. }
  3178. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  3179. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  3180. lock_fclose(fp,mapreg_txt,&lock);
  3181. #else
  3182. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub);
  3183. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  3184. #endif
  3185. mapreg_dirty=0;
  3186. return 0;
  3187. }
  3188. static int script_autosave_mapreg(int tid, unsigned int tick, int id, intptr data)
  3189. {
  3190. if(mapreg_dirty)
  3191. if (script_save_mapreg() == -1)
  3192. ShowError("Failed to save the mapreg data!\n");
  3193. return 0;
  3194. }
  3195. int script_config_read(char *cfgName)
  3196. {
  3197. int i;
  3198. char line[1024],w1[1024],w2[1024];
  3199. FILE *fp;
  3200. fp=fopen(cfgName,"r");
  3201. if(fp==NULL){
  3202. ShowError("file not found: [%s]\n", cfgName);
  3203. return 1;
  3204. }
  3205. while(fgets(line, sizeof(line), fp))
  3206. {
  3207. if(line[0] == '/' && line[1] == '/')
  3208. continue;
  3209. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3210. if(i!=2)
  3211. continue;
  3212. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3213. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3214. }
  3215. else if(strcmpi(w1,"check_cmdcount")==0) {
  3216. script_config.check_cmdcount = config_switch(w2);
  3217. }
  3218. else if(strcmpi(w1,"check_gotocount")==0) {
  3219. script_config.check_gotocount = config_switch(w2);
  3220. }
  3221. else if(strcmpi(w1,"input_min_value")==0) {
  3222. script_config.input_min_value = config_switch(w2);
  3223. }
  3224. else if(strcmpi(w1,"input_max_value")==0) {
  3225. script_config.input_max_value = config_switch(w2);
  3226. }
  3227. else if(strcmpi(w1,"import")==0){
  3228. script_config_read(w2);
  3229. }
  3230. }
  3231. fclose(fp);
  3232. return 0;
  3233. }
  3234. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3235. {
  3236. struct script_code *code = (struct script_code *)data;
  3237. if(code){
  3238. script_free_vars( &code->script_vars );
  3239. aFree( code->script_buf );
  3240. aFree( code );
  3241. }
  3242. return 0;
  3243. }
  3244. /*==========================================
  3245. * 終了
  3246. *------------------------------------------*/
  3247. int do_final_script()
  3248. {
  3249. #ifdef DEBUG_HASH
  3250. if (battle_config.etc_log)
  3251. {
  3252. FILE *fp = fopen("hash_dump.txt","wt");
  3253. if(fp) {
  3254. int i,count[SCRIPT_HASH_SIZE];
  3255. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3256. int n=0;
  3257. int min=INT_MAX,max=0,zero=0;
  3258. double mean=0.0f;
  3259. double median=0.0f;
  3260. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3261. memset(count, 0, sizeof(count));
  3262. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3263. fprintf(fp,"---------------------------------------------------------------\n");
  3264. for(i=LABEL_START; i<str_num; i++) {
  3265. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3266. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3267. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3268. ++count[h];
  3269. }
  3270. fprintf(fp,"--------------------\n\n");
  3271. memset(count2, 0, sizeof(count2));
  3272. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3273. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3274. if(min > count[i])
  3275. min = count[i]; // minimun count of collision
  3276. if(max < count[i])
  3277. max = count[i]; // maximun count of collision
  3278. if(count[i] == 0)
  3279. zero++;
  3280. ++count2[count[i]];
  3281. }
  3282. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3283. for( i=min; i <= max; ++i ){
  3284. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3285. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3286. }
  3287. for( i=min; i <= max; ++i ){
  3288. n += count2[i];
  3289. if( n*2 >= SCRIPT_HASH_SIZE )
  3290. {
  3291. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3292. median = (i+i+1)/2.0f;
  3293. else
  3294. median = i;
  3295. break;
  3296. }
  3297. }
  3298. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3299. fclose(fp);
  3300. }
  3301. }
  3302. #endif
  3303. if(mapreg_dirty>=0)
  3304. script_save_mapreg();
  3305. mapreg_db->destroy(mapreg_db,NULL);
  3306. mapregstr_db->destroy(mapregstr_db,NULL);
  3307. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3308. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3309. if(sleep_db) {
  3310. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3311. while(n) {
  3312. struct script_state *st = (struct script_state *)n->data;
  3313. script_free_stack(st->stack);
  3314. aFree(st);
  3315. n = n->next;
  3316. }
  3317. linkdb_final(&sleep_db);
  3318. }
  3319. if (str_data)
  3320. aFree(str_data);
  3321. if (str_buf)
  3322. aFree(str_buf);
  3323. return 0;
  3324. }
  3325. /*==========================================
  3326. * 初期化
  3327. *------------------------------------------*/
  3328. int do_init_script()
  3329. {
  3330. mapreg_db= idb_alloc(DB_OPT_BASE);
  3331. mapregstr_db=idb_alloc(DB_OPT_RELEASE_DATA);
  3332. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3333. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3334. script_load_mapreg();
  3335. add_timer_func_list(script_autosave_mapreg, "script_autosave_mapreg");
  3336. add_timer_interval(gettick() + MAPREG_AUTOSAVE_INTERVAL, script_autosave_mapreg, 0, 0, MAPREG_AUTOSAVE_INTERVAL);
  3337. return 0;
  3338. }
  3339. int script_reload()
  3340. {
  3341. if(mapreg_dirty>=0)
  3342. script_save_mapreg();
  3343. mapreg_db->clear(mapreg_db, NULL);
  3344. mapregstr_db->clear(mapregstr_db, NULL);
  3345. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3346. scriptlabel_db->clear(scriptlabel_db, NULL);
  3347. if(sleep_db) {
  3348. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3349. while(n) {
  3350. struct script_state *st = (struct script_state *)n->data;
  3351. if( st->sleep.timer != INVALID_TIMER )
  3352. delete_timer(st->sleep.timer, run_script_timer);
  3353. script_free_stack(st->stack);
  3354. aFree(st);
  3355. n = n->next;
  3356. }
  3357. linkdb_final(&sleep_db);
  3358. }
  3359. script_load_mapreg();
  3360. return 0;
  3361. }
  3362. //-----------------------------------------------------------------------------
  3363. // buildin functions
  3364. //
  3365. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3366. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3367. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3368. /////////////////////////////////////////////////////////////////////
  3369. // NPC interaction
  3370. //
  3371. /// Appends a message to the npc dialog.
  3372. /// If a dialog doesn't exist yet, one is created.
  3373. ///
  3374. /// mes "<message>";
  3375. BUILDIN_FUNC(mes)
  3376. {
  3377. TBL_PC* sd = script_rid2sd(st);
  3378. if( sd == NULL )
  3379. return 0;
  3380. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3381. return 0;
  3382. }
  3383. /// Displays the button 'next' in the npc dialog.
  3384. /// The dialog text is cleared and the script continues when the button is pressed.
  3385. ///
  3386. /// next;
  3387. BUILDIN_FUNC(next)
  3388. {
  3389. TBL_PC* sd;
  3390. sd = script_rid2sd(st);
  3391. if( sd == NULL )
  3392. return 0;
  3393. st->state = STOP;
  3394. clif_scriptnext(sd, st->oid);
  3395. return 0;
  3396. }
  3397. /// Ends the script and displays the button 'close' on the npc dialog.
  3398. /// The dialog is closed when the button is pressed.
  3399. ///
  3400. /// close;
  3401. BUILDIN_FUNC(close)
  3402. {
  3403. TBL_PC* sd;
  3404. sd = script_rid2sd(st);
  3405. if( sd == NULL )
  3406. return 0;
  3407. st->state = END;
  3408. clif_scriptclose(sd, st->oid);
  3409. return 0;
  3410. }
  3411. /// Displays the button 'close' on the npc dialog.
  3412. /// The dialog is closed and the script continues when the button is pressed.
  3413. ///
  3414. /// close2;
  3415. BUILDIN_FUNC(close2)
  3416. {
  3417. TBL_PC* sd;
  3418. sd = script_rid2sd(st);
  3419. if( sd == NULL )
  3420. return 0;
  3421. st->state = STOP;
  3422. clif_scriptclose(sd, st->oid);
  3423. return 0;
  3424. }
  3425. /// Counts the number of valid and total number of options in 'str'
  3426. /// If max_count > 0 the counting stops when that valid option is reached
  3427. /// total is incremented for each option (NULL is supported)
  3428. static int menu_countoptions(const char* str, int max_count, int* total)
  3429. {
  3430. int count = 0;
  3431. int bogus_total;
  3432. if( total == NULL )
  3433. total = &bogus_total;
  3434. ++(*total);
  3435. // initial empty options
  3436. while( *str == ':' )
  3437. {
  3438. ++str;
  3439. ++(*total);
  3440. }
  3441. // count menu options
  3442. while( *str != '\0' )
  3443. {
  3444. ++count;
  3445. --max_count;
  3446. if( max_count == 0 )
  3447. break;
  3448. while( *str != ':' && *str != '\0' )
  3449. ++str;
  3450. while( *str == ':' )
  3451. {
  3452. ++str;
  3453. ++(*total);
  3454. }
  3455. }
  3456. return count;
  3457. }
  3458. /// Displays a menu with options and goes to the target label.
  3459. /// The script is stopped if cancel is pressed.
  3460. /// Options with no text are not displayed in the client.
  3461. ///
  3462. /// Options can be grouped together, separated by the character ':' in the text:
  3463. /// ex: menu "A:B:C",L_target;
  3464. /// All these options go to the specified target label.
  3465. ///
  3466. /// The index of the selected option is put in the variable @menu.
  3467. /// Indexes start with 1 and are consistent with grouped and empty options.
  3468. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3469. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3470. ///
  3471. /// NOTE: the client closes the npc dialog when cancel is pressed
  3472. ///
  3473. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3474. BUILDIN_FUNC(menu)
  3475. {
  3476. int i;
  3477. const char* text;
  3478. TBL_PC* sd;
  3479. sd = script_rid2sd(st);
  3480. if( sd == NULL )
  3481. return 0;
  3482. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3483. if( sd->state.menu_or_input == 0 )
  3484. {
  3485. struct StringBuf buf;
  3486. struct script_data* data;
  3487. if( script_lastdata(st) % 2 == 0 )
  3488. {// argument count is not even (1st argument is at index 2)
  3489. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3490. st->state = END;
  3491. return 1;
  3492. }
  3493. StringBuf_Init(&buf);
  3494. sd->npc_menu = 0;
  3495. for( i = 2; i < script_lastdata(st); i += 2 )
  3496. {
  3497. // menu options
  3498. text = script_getstr(st, i);
  3499. // target label
  3500. data = script_getdata(st, i+1);
  3501. if( !data_islabel(data) )
  3502. {// not a label
  3503. StringBuf_Destroy(&buf);
  3504. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3505. script_reportdata(data);
  3506. st->state = END;
  3507. return 1;
  3508. }
  3509. // append option(s)
  3510. if( text[0] == '\0' )
  3511. continue;// empty string, ignore
  3512. if( sd->npc_menu > 0 )
  3513. StringBuf_AppendStr(&buf, ":");
  3514. StringBuf_AppendStr(&buf, text);
  3515. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3516. }
  3517. st->state = RERUNLINE;
  3518. sd->state.menu_or_input = 1;
  3519. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3520. StringBuf_Destroy(&buf);
  3521. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3522. }
  3523. else if( sd->npc_menu == 0xff )
  3524. {// Cancel was pressed
  3525. sd->state.menu_or_input = 0;
  3526. st->state = END;
  3527. }
  3528. else
  3529. {// goto target label
  3530. int menu = 0;
  3531. sd->state.menu_or_input = 0;
  3532. if( sd->npc_menu <= 0 )
  3533. {
  3534. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3535. st->state = END;
  3536. return 1;
  3537. }
  3538. // get target label
  3539. for( i = 2; i < script_lastdata(st); i += 2 )
  3540. {
  3541. text = script_getstr(st, i);
  3542. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3543. if( sd->npc_menu <= 0 )
  3544. break;// entry found
  3545. }
  3546. if( sd->npc_menu > 0 )
  3547. {// Invalid selection
  3548. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3549. st->state = END;
  3550. return 1;
  3551. }
  3552. if( !data_islabel(script_getdata(st, i + 1)) )
  3553. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3554. ShowError("script:menu: unexpected data in label argument\n");
  3555. script_reportdata(script_getdata(st, i + 1));
  3556. st->state = END;
  3557. return 1;
  3558. }
  3559. pc_setreg(sd, add_str("@menu"), menu);
  3560. st->pos = script_getnum(st, i + 1);
  3561. st->state = GOTO;
  3562. }
  3563. return 0;
  3564. }
  3565. /// Displays a menu with options and returns the selected option.
  3566. /// Behaves like 'menu' without the target labels.
  3567. ///
  3568. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3569. ///
  3570. /// @see menu
  3571. BUILDIN_FUNC(select)
  3572. {
  3573. int i;
  3574. const char* text;
  3575. TBL_PC* sd;
  3576. sd = script_rid2sd(st);
  3577. if( sd == NULL )
  3578. return 0;
  3579. if( sd->state.menu_or_input == 0 )
  3580. {
  3581. struct StringBuf buf;
  3582. StringBuf_Init(&buf);
  3583. sd->npc_menu = 0;
  3584. for( i = 2; i <= script_lastdata(st); ++i )
  3585. {
  3586. text = script_getstr(st, i);
  3587. if( sd->npc_menu > 0 )
  3588. StringBuf_AppendStr(&buf, ":");
  3589. StringBuf_AppendStr(&buf, text);
  3590. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3591. }
  3592. st->state = RERUNLINE;
  3593. sd->state.menu_or_input = 1;
  3594. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3595. StringBuf_Destroy(&buf);
  3596. }
  3597. else if( sd->npc_menu == 0xff )
  3598. {// Cancel was pressed
  3599. sd->state.menu_or_input = 0;
  3600. st->state = END;
  3601. }
  3602. else
  3603. {// return selected option
  3604. int menu = 0;
  3605. sd->state.menu_or_input = 0;
  3606. for( i = 2; i <= script_lastdata(st); ++i )
  3607. {
  3608. text = script_getstr(st, i);
  3609. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3610. if( sd->npc_menu <= 0 )
  3611. break;// entry found
  3612. }
  3613. pc_setreg(sd, add_str("@menu"), menu);
  3614. script_pushint(st, menu);
  3615. }
  3616. return 0;
  3617. }
  3618. /// Displays a menu with options and returns the selected option.
  3619. /// Behaves like 'menu' without the target labels, except when cancel is
  3620. /// pressed.
  3621. /// When cancel is pressed, the script continues and 255 is returned.
  3622. ///
  3623. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3624. ///
  3625. /// @see menu
  3626. BUILDIN_FUNC(prompt)
  3627. {
  3628. int i;
  3629. const char *text;
  3630. TBL_PC* sd;
  3631. sd = script_rid2sd(st);
  3632. if( sd == NULL )
  3633. return 0;
  3634. if( sd->state.menu_or_input == 0 )
  3635. {
  3636. struct StringBuf buf;
  3637. StringBuf_Init(&buf);
  3638. sd->npc_menu = 0;
  3639. for( i = 2; i <= script_lastdata(st); ++i )
  3640. {
  3641. text = script_getstr(st, i);
  3642. if( sd->npc_menu > 0 )
  3643. StringBuf_AppendStr(&buf, ":");
  3644. StringBuf_AppendStr(&buf, text);
  3645. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3646. }
  3647. st->state = RERUNLINE;
  3648. sd->state.menu_or_input = 1;
  3649. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3650. StringBuf_Destroy(&buf);
  3651. }
  3652. else if( sd->npc_menu == 0xff )
  3653. {// Cancel was pressed
  3654. sd->state.menu_or_input = 0;
  3655. pc_setreg(sd, add_str("@menu"), 0xff);
  3656. script_pushint(st, 0xff);
  3657. }
  3658. else
  3659. {// return selected option
  3660. int menu = 0;
  3661. sd->state.menu_or_input = 0;
  3662. for( i = 2; i <= script_lastdata(st); ++i )
  3663. {
  3664. text = script_getstr(st, i);
  3665. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3666. if( sd->npc_menu <= 0 )
  3667. break;// entry found
  3668. }
  3669. pc_setreg(sd, add_str("@menu"), menu);
  3670. script_pushint(st, menu);
  3671. }
  3672. return 0;
  3673. }
  3674. /////////////////////////////////////////////////////////////////////
  3675. // ...
  3676. //
  3677. /// Jumps to the target script label.
  3678. ///
  3679. /// goto <label>;
  3680. BUILDIN_FUNC(goto)
  3681. {
  3682. if( !data_islabel(script_getdata(st,2)) )
  3683. {
  3684. ShowError("script:goto: not a label\n");
  3685. script_reportdata(script_getdata(st,2));
  3686. st->state = END;
  3687. return 1;
  3688. }
  3689. st->pos = script_getnum(st,2);
  3690. st->state = GOTO;
  3691. return 0;
  3692. }
  3693. /*==========================================
  3694. * ユーザー定義関数の呼び出し
  3695. *------------------------------------------*/
  3696. BUILDIN_FUNC(callfunc)
  3697. {
  3698. int i, j;
  3699. struct linkdb_node** oldval;
  3700. struct script_code *scr, *oldscr;
  3701. const char* str = script_getstr(st,2);
  3702. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3703. if( !scr )
  3704. {
  3705. ShowError("script:callfunc: function not found! [%s]\n", str);
  3706. st->state = END;
  3707. return 1;
  3708. }
  3709. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3710. push_copy(st->stack,i);
  3711. script_pushint(st,j); // 引数の数をプッシュ
  3712. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3713. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3714. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3715. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3716. oldscr = st->script;
  3717. oldval = st->stack->var_function;
  3718. st->pos = 0;
  3719. st->script = scr;
  3720. st->stack->defsp = st->start+5+j;
  3721. st->state = GOTO;
  3722. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3723. // ' 変数の引き継ぎ
  3724. for( i = 0; i < j; i++ )
  3725. {
  3726. struct script_data* s = script_getdatatop(st, -6-i);
  3727. if( data_isreference(s) && !s->ref )
  3728. {
  3729. char* name = str_buf + str_data[s->u.num&0x00ffffff].str;
  3730. // '@ 変数の引き継ぎ
  3731. if( name[0] == '.' && name[1] == '@' ) {
  3732. s->ref = oldval;
  3733. } else if( name[0] == '.' ) {
  3734. s->ref = &oldscr->script_vars;
  3735. }
  3736. }
  3737. }
  3738. return 0;
  3739. }
  3740. /*==========================================
  3741. * サブルーティンの呼び出し
  3742. *------------------------------------------*/
  3743. BUILDIN_FUNC(callsub)
  3744. {
  3745. int pos=script_getnum(st,2);
  3746. int i,j;
  3747. if(!data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2))) {
  3748. ShowError("script:callsub: argument is not a label\n");
  3749. script_reportdata(script_getdata(st,2));
  3750. st->state=END;
  3751. return 1;
  3752. } else {
  3753. struct linkdb_node **oldval = st->stack->var_function;
  3754. for(i=st->start+3,j=0;i<st->end;i++,j++)
  3755. push_copy(st->stack,i);
  3756. script_pushint(st,j); // 引数の数をプッシュ
  3757. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3758. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3759. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3760. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3761. st->pos=pos;
  3762. st->stack->defsp=st->start+5+j;
  3763. st->state=GOTO;
  3764. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3765. // ' 変数の引き継ぎ
  3766. for(i = 0; i < j; i++) {
  3767. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  3768. if( data_isreference(s) && !s->ref ) {
  3769. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  3770. // '@ 変数の引き継ぎ
  3771. if( name[0] == '.' && name[1] == '@' ) {
  3772. s->ref = oldval;
  3773. }
  3774. }
  3775. }
  3776. }
  3777. return 0;
  3778. }
  3779. /// Retrieves an argument provided to callfunc/callsub.
  3780. /// If the argument doesn't exist
  3781. ///
  3782. /// getarg(<index>{,<default_value>}) -> <value>
  3783. BUILDIN_FUNC(getarg)
  3784. {
  3785. int idx;
  3786. int count;
  3787. int stsp;
  3788. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3789. {
  3790. ShowError("script:getarg: no callfunc or callsub!\n");
  3791. st->state = END;
  3792. return 1;
  3793. }
  3794. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  3795. stsp = st->stack->defsp - count - 5;
  3796. idx = script_getnum(st,2);
  3797. if( idx < count )
  3798. push_copy(st->stack, stsp + idx);
  3799. else if( script_hasdata(st,3) )
  3800. script_pushcopy(st, 3);
  3801. else
  3802. {
  3803. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  3804. st->state = END;
  3805. return 1;
  3806. }
  3807. return 0;
  3808. }
  3809. /// Returns from the current function, optionaly returning a value from the functions.
  3810. /// Don't use outside script functions.
  3811. ///
  3812. /// return;
  3813. /// return <value>;
  3814. BUILDIN_FUNC(return)
  3815. {
  3816. if( script_hasdata(st,2) )
  3817. {// return value
  3818. struct script_data* data;
  3819. script_pushcopy(st, 2);
  3820. data = script_getdatatop(st, -1);
  3821. if( data_isreference(data) )
  3822. {
  3823. char* name = reference_getname(data);
  3824. if( name[0] == '.' && name[1] == '@' )
  3825. {// temporary script variable, convert to value
  3826. get_val(st, data);
  3827. if( data_isstring(data) )
  3828. {// duplicate the string
  3829. data->type = C_STR;
  3830. data->u.str = aStrdup(data->u.str);
  3831. }
  3832. }
  3833. else if( name[0] == '.' && !data->ref )
  3834. {// script variable, link to current script
  3835. data->ref = &st->script->script_vars;
  3836. }
  3837. }
  3838. }
  3839. else
  3840. {// no return value
  3841. script_pushnil(st);
  3842. }
  3843. st->state = RETFUNC;
  3844. return 0;
  3845. }
  3846. /// Returns a random number from 0 to <range>-1.
  3847. /// Or returns a random number from <min> to <max>.
  3848. /// If <min> is greater than <max>, their numbers are switched.
  3849. /// rand(<range>) -> <int>
  3850. /// rand(<min>,<max>) -> <int>
  3851. BUILDIN_FUNC(rand)
  3852. {
  3853. int range;
  3854. int min;
  3855. int max;
  3856. if( script_hasdata(st,3) )
  3857. {// min,max
  3858. min = script_getnum(st,2);
  3859. max = script_getnum(st,3);
  3860. if( max < min )
  3861. swap(min, max);
  3862. range = max - min + 1;
  3863. }
  3864. else
  3865. {// range
  3866. min = 0;
  3867. range = script_getnum(st,2);
  3868. }
  3869. if( range <= 1 )
  3870. script_pushint(st, min);
  3871. else
  3872. script_pushint(st, rand()%range + min);
  3873. return 0;
  3874. }
  3875. /*==========================================
  3876. *
  3877. *------------------------------------------*/
  3878. BUILDIN_FUNC(warp)
  3879. {
  3880. int ret;
  3881. int x,y;
  3882. const char* str;
  3883. TBL_PC* sd;
  3884. sd = script_rid2sd(st);
  3885. if( sd == NULL )
  3886. return 0;
  3887. str = script_getstr(st,2);
  3888. x = script_getnum(st,3);
  3889. y = script_getnum(st,4);
  3890. if(strcmp(str,"Random")==0)
  3891. ret = pc_randomwarp(sd,3);
  3892. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3893. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3894. else
  3895. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3896. if( ret ) {
  3897. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3898. script_reportsrc(st);
  3899. }
  3900. return 0;
  3901. }
  3902. /*==========================================
  3903. * エリア指定ワープ
  3904. *------------------------------------------*/
  3905. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3906. {
  3907. int x,y;
  3908. unsigned int map;
  3909. map=va_arg(ap, unsigned int);
  3910. x=va_arg(ap,int);
  3911. y=va_arg(ap,int);
  3912. if(map == 0)
  3913. pc_randomwarp((TBL_PC *)bl,3);
  3914. else
  3915. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3916. return 0;
  3917. }
  3918. BUILDIN_FUNC(areawarp)
  3919. {
  3920. int x,y,m;
  3921. unsigned int index;
  3922. const char *str;
  3923. const char *mapname;
  3924. int x0,y0,x1,y1;
  3925. mapname=script_getstr(st,2);
  3926. x0=script_getnum(st,3);
  3927. y0=script_getnum(st,4);
  3928. x1=script_getnum(st,5);
  3929. y1=script_getnum(st,6);
  3930. str=script_getstr(st,7);
  3931. x=script_getnum(st,8);
  3932. y=script_getnum(st,9);
  3933. if( (m=map_mapname2mapid(mapname))< 0)
  3934. return 0;
  3935. if(strcmp(str,"Random")==0)
  3936. index = 0;
  3937. else if(!(index=mapindex_name2id(str)))
  3938. return 0;
  3939. map_foreachinarea(buildin_areawarp_sub,
  3940. m,x0,y0,x1,y1,BL_PC, index,x,y );
  3941. return 0;
  3942. }
  3943. /*==========================================
  3944. * warpchar [LuzZza]
  3945. * Useful for warp one player from
  3946. * another player npc-session.
  3947. * Using: warpchar "mapname",x,y,Char_ID;
  3948. *------------------------------------------*/
  3949. BUILDIN_FUNC(warpchar)
  3950. {
  3951. int x,y,a;
  3952. const char *str;
  3953. TBL_PC *sd;
  3954. str=script_getstr(st,2);
  3955. x=script_getnum(st,3);
  3956. y=script_getnum(st,4);
  3957. a=script_getnum(st,5);
  3958. sd = map_charid2sd(a);
  3959. if( sd == NULL )
  3960. return 0;
  3961. if(strcmp(str, "Random") == 0)
  3962. pc_randomwarp(sd, 3);
  3963. else
  3964. if(strcmp(str, "SavePoint") == 0)
  3965. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3966. else
  3967. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3968. return 0;
  3969. }
  3970. /*==========================================
  3971. * Warpparty - [Fredzilla]
  3972. * Syntax: warpparty "mapname",x,y,Party_ID;
  3973. *------------------------------------------*/
  3974. BUILDIN_FUNC(warpparty)
  3975. {
  3976. int x,y;
  3977. const char *str;
  3978. int p_id;
  3979. int i;
  3980. unsigned short mapindex;
  3981. TBL_PC *pl_sd;
  3982. struct party_data *p=NULL;
  3983. str=script_getstr(st,2);
  3984. x=script_getnum(st,3);
  3985. y=script_getnum(st,4);
  3986. p_id=script_getnum(st,5);
  3987. if(p_id < 1)
  3988. return 0;
  3989. p = party_search(p_id);
  3990. if (!p)
  3991. return 0;
  3992. if(strcmp(str,"Random")==0)
  3993. {
  3994. for (i = 0; i < MAX_PARTY; i++)
  3995. {
  3996. if ((pl_sd = p->data[i].sd))
  3997. {
  3998. if(map[pl_sd->bl.m].flag.nowarp)
  3999. continue;
  4000. pc_randomwarp(pl_sd,3);
  4001. }
  4002. }
  4003. }
  4004. else if(strcmp(str,"SavePointAll")==0)
  4005. {
  4006. for (i = 0; i < MAX_PARTY; i++)
  4007. {
  4008. if ((pl_sd = p->data[i].sd))
  4009. {
  4010. if(map[pl_sd->bl.m].flag.noreturn)
  4011. continue;
  4012. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4013. }
  4014. }
  4015. }
  4016. else if(strcmp(str,"SavePoint")==0)
  4017. {
  4018. pl_sd=script_rid2sd(st);
  4019. if (!pl_sd) return 0;
  4020. mapindex=pl_sd->status.save_point.map;
  4021. x=pl_sd->status.save_point.x;
  4022. y=pl_sd->status.save_point.y;
  4023. for (i = 0; i < MAX_PARTY; i++)
  4024. {
  4025. if ((pl_sd = p->data[i].sd))
  4026. {
  4027. if(map[pl_sd->bl.m].flag.noreturn)
  4028. continue;
  4029. pc_setpos(pl_sd,mapindex,x,y,3);
  4030. }
  4031. }
  4032. }
  4033. else if(strcmp(str,"Leader")==0)
  4034. {
  4035. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4036. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4037. return 0;
  4038. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  4039. return 0;
  4040. mapindex = p->data[i].sd->mapindex;
  4041. x = p->data[i].sd->bl.x;
  4042. y = p->data[i].sd->bl.y;
  4043. for (i = 0; i < MAX_PARTY; i++)
  4044. {
  4045. pl_sd = p->data[i].sd;
  4046. if (!pl_sd)
  4047. continue;
  4048. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4049. continue;
  4050. pc_setpos(pl_sd,mapindex,x,y,3);
  4051. }
  4052. }
  4053. else
  4054. {
  4055. mapindex = mapindex_name2id(str);
  4056. if (!mapindex) //Show source of npc error.
  4057. return 1;
  4058. for (i = 0; i < MAX_PARTY; i++)
  4059. {
  4060. if ((pl_sd = p->data[i].sd))
  4061. {
  4062. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4063. continue;
  4064. pc_setpos(pl_sd,mapindex,x,y,3);
  4065. }
  4066. }
  4067. }
  4068. return 0;
  4069. }
  4070. /*==========================================
  4071. * Warpguild - [Fredzilla]
  4072. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4073. *------------------------------------------*/
  4074. BUILDIN_FUNC(warpguild)
  4075. {
  4076. TBL_PC *sd;
  4077. TBL_PC *pl_sd;
  4078. struct guild* g;
  4079. struct s_mapiterator* iter;
  4080. int type;
  4081. const char* str = script_getstr(st,2);
  4082. int x = script_getnum(st,3);
  4083. int y = script_getnum(st,4);
  4084. int gid = script_getnum(st,5);
  4085. sd=script_rid2sd(st);
  4086. if( sd == NULL )
  4087. return 0;
  4088. g = guild_search(gid);
  4089. if( g == NULL )
  4090. return 0;
  4091. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4092. return 0;
  4093. type = ( strcmp(str,"Random")==0 ) ? 0
  4094. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4095. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4096. : 3;
  4097. iter = mapit_getallusers();
  4098. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4099. {
  4100. if( pl_sd->status.guild_id != gid )
  4101. continue;
  4102. switch( type )
  4103. {
  4104. case 0: // Random
  4105. if(!map[pl_sd->bl.m].flag.nowarp)
  4106. pc_randomwarp(pl_sd,3);
  4107. break;
  4108. case 1: // SavePointAll
  4109. if(!map[pl_sd->bl.m].flag.noreturn)
  4110. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4111. break;
  4112. case 2: // SavePoint
  4113. if(!map[pl_sd->bl.m].flag.noreturn)
  4114. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  4115. break;
  4116. case 3: // m,x,y
  4117. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4118. pc_setpos(pl_sd,mapindex_name2id(str),x,y,3);
  4119. break;
  4120. }
  4121. }
  4122. mapit_free(iter);
  4123. return 0;
  4124. }
  4125. /*==========================================
  4126. *
  4127. *------------------------------------------*/
  4128. BUILDIN_FUNC(heal)
  4129. {
  4130. TBL_PC *sd;
  4131. int hp,sp;
  4132. sd = script_rid2sd(st);
  4133. if (!sd) return 0;
  4134. hp=script_getnum(st,2);
  4135. sp=script_getnum(st,3);
  4136. status_heal(&sd->bl, hp, sp, 1);
  4137. return 0;
  4138. }
  4139. /*==========================================
  4140. *
  4141. *------------------------------------------*/
  4142. BUILDIN_FUNC(itemheal)
  4143. {
  4144. TBL_PC *sd;
  4145. int hp,sp;
  4146. hp=script_getnum(st,2);
  4147. sp=script_getnum(st,3);
  4148. if(potion_flag==1) {
  4149. potion_hp = hp;
  4150. potion_sp = sp;
  4151. return 0;
  4152. }
  4153. sd = script_rid2sd(st);
  4154. if (!sd) return 0;
  4155. pc_itemheal(sd,sd->itemid,hp,sp);
  4156. return 0;
  4157. }
  4158. /*==========================================
  4159. *
  4160. *------------------------------------------*/
  4161. BUILDIN_FUNC(percentheal)
  4162. {
  4163. int hp,sp;
  4164. TBL_PC* sd;
  4165. hp=script_getnum(st,2);
  4166. sp=script_getnum(st,3);
  4167. if(potion_flag==1) {
  4168. potion_per_hp = hp;
  4169. potion_per_sp = sp;
  4170. return 0;
  4171. }
  4172. sd = script_rid2sd(st);
  4173. if( sd == NULL )
  4174. return 0;
  4175. pc_percentheal(sd,hp,sp);
  4176. return 0;
  4177. }
  4178. /*==========================================
  4179. *
  4180. *------------------------------------------*/
  4181. BUILDIN_FUNC(jobchange)
  4182. {
  4183. int job, upper=-1;
  4184. job=script_getnum(st,2);
  4185. if( script_hasdata(st,3) )
  4186. upper=script_getnum(st,3);
  4187. if (pcdb_checkid(job))
  4188. {
  4189. TBL_PC* sd;
  4190. sd = script_rid2sd(st);
  4191. if( sd == NULL )
  4192. return 0;
  4193. pc_jobchange(sd, job, upper);
  4194. if(use_irc && irc_announce_jobchange_flag)
  4195. irc_announce_jobchange(sd);
  4196. }
  4197. return 0;
  4198. }
  4199. /*==========================================
  4200. *
  4201. *------------------------------------------*/
  4202. BUILDIN_FUNC(jobname)
  4203. {
  4204. int class_=script_getnum(st,2);
  4205. script_pushconststr(st,job_name(class_));
  4206. return 0;
  4207. }
  4208. /// Get input from the player.
  4209. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4210. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4211. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4212. /// shorter than 'min' and 0 otherwise.
  4213. ///
  4214. /// input(<var>{,<min>{,<max>}}) -> <int>
  4215. BUILDIN_FUNC(input)
  4216. {
  4217. TBL_PC* sd;
  4218. struct script_data* data;
  4219. int uid;
  4220. char* name;
  4221. int min;
  4222. int max;
  4223. sd = script_rid2sd(st);
  4224. if( sd == NULL )
  4225. return 0;
  4226. data = script_getdata(st,2);
  4227. if( !data_isreference(data) ){
  4228. ShowError("script:input: not a variable\n");
  4229. script_reportdata(data);
  4230. st->state = END;
  4231. return 1;
  4232. }
  4233. uid = reference_getuid(data);
  4234. name = reference_getname(data);
  4235. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4236. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4237. if( !sd->state.menu_or_input )
  4238. { // first invocation, display npc input box
  4239. sd->state.menu_or_input = 1;
  4240. st->state = RERUNLINE;
  4241. if( is_string_variable(name) )
  4242. clif_scriptinputstr(sd,st->oid);
  4243. else
  4244. clif_scriptinput(sd,st->oid);
  4245. }
  4246. else
  4247. { // take received text/value and store it in the designated variable
  4248. sd->state.menu_or_input = 0;
  4249. if( is_string_variable(name) )
  4250. {
  4251. size_t len = strlen(sd->npc_str);
  4252. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4253. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4254. }
  4255. else
  4256. {
  4257. int amount = sd->npc_amount;
  4258. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4259. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4260. }
  4261. }
  4262. return 0;
  4263. }
  4264. /*==========================================
  4265. * 変数設定
  4266. *------------------------------------------*/
  4267. BUILDIN_FUNC(set)
  4268. {
  4269. TBL_PC *sd=NULL;
  4270. int num=st->stack->stack_data[st->start+2].u.num;
  4271. char *name=str_buf+str_data[num&0x00ffffff].str;
  4272. char prefix=*name;
  4273. char postfix=name[strlen(name)-1];
  4274. if( !data_isreference(script_getdata(st,2)) ){
  4275. ShowError("script:set: not a variable\n");
  4276. script_reportdata(script_getdata(st,2));
  4277. st->state = END;
  4278. return 1;
  4279. }
  4280. if(not_server_variable(prefix))
  4281. {
  4282. sd=script_rid2sd(st);
  4283. if( sd == NULL )
  4284. {
  4285. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4286. return 0;
  4287. }
  4288. }
  4289. if( postfix=='$' ){
  4290. // 文字列
  4291. const char *str = script_getstr(st,3);
  4292. set_reg(st,sd,num,name,(void*)str,script_getref(st,2));
  4293. }else{
  4294. // 数値
  4295. int val = script_getnum(st,3);
  4296. set_reg(st,sd,num,name,(void*)val,script_getref(st,2));
  4297. }
  4298. return 0;
  4299. }
  4300. /////////////////////////////////////////////////////////////////////
  4301. /// Array variables
  4302. ///
  4303. /// Returns the size of the specified array
  4304. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4305. {
  4306. int32 ret = idx;
  4307. if( isstring )
  4308. {
  4309. for( ; idx < 128; ++idx )
  4310. {
  4311. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4312. if( str && *str )
  4313. ret = idx + 1;
  4314. script_removetop(st, -1, 0);
  4315. }
  4316. }
  4317. else
  4318. {
  4319. for( ; idx < 128; ++idx )
  4320. {
  4321. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4322. if( num )
  4323. ret = idx + 1;
  4324. script_removetop(st, -1, 0);
  4325. }
  4326. }
  4327. return ret;
  4328. }
  4329. /// Sets values of an array, from the starting index.
  4330. /// ex: setarray arr[1],1,2,3;
  4331. ///
  4332. /// setarray <array variable>,<value1>{,<value2>...};
  4333. BUILDIN_FUNC(setarray)
  4334. {
  4335. struct script_data* data;
  4336. char* name;
  4337. int32 start;
  4338. int32 end;
  4339. int32 id;
  4340. int32 i;
  4341. TBL_PC* sd = NULL;
  4342. data = script_getdata(st, 2);
  4343. if( !data_isreference(data) )
  4344. {
  4345. ShowError("script:setarray: not a variable\n");
  4346. script_reportdata(data);
  4347. st->state = END;
  4348. return 1;// not a variable
  4349. }
  4350. id = reference_getid(data);
  4351. start = reference_getindex(data);
  4352. name = reference_getname(data);
  4353. if( not_array_variable(*name) )
  4354. {
  4355. ShowError("script:setarray: illegal scope\n");
  4356. script_reportdata(data);
  4357. st->state = END;
  4358. return 1;// not supported
  4359. }
  4360. if( not_server_variable(*name) )
  4361. {
  4362. sd = script_rid2sd(st);
  4363. if( sd == NULL )
  4364. return 0;// no player attached
  4365. }
  4366. end = start + script_lastdata(st) - 2;
  4367. if( end > 127 )
  4368. end = 127;
  4369. if( is_string_variable(name) )
  4370. {// string array
  4371. for( i = 3; start < end; ++start, ++i )
  4372. {
  4373. void* v = (void*)script_getstr(st,i);
  4374. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4375. }
  4376. }
  4377. else
  4378. {// int array
  4379. for( i = 3; start < end; ++start, ++i )
  4380. {
  4381. void* v = (void*)script_getnum(st,i);
  4382. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4383. }
  4384. }
  4385. return 0;
  4386. }
  4387. /// Sets count values of an array, from the starting index.
  4388. /// ex: cleararray arr[0],0,1;
  4389. ///
  4390. /// cleararray <array variable>,<value>,<count>;
  4391. BUILDIN_FUNC(cleararray)
  4392. {
  4393. struct script_data* data;
  4394. char* name;
  4395. int32 start;
  4396. int32 end;
  4397. int32 id;
  4398. void* v;
  4399. TBL_PC* sd = NULL;
  4400. data = script_getdata(st, 2);
  4401. if( !data_isreference(data) )
  4402. {
  4403. ShowError("script:cleararray: not a variable\n");
  4404. script_reportdata(data);
  4405. st->state = END;
  4406. return 1;// not a variable
  4407. }
  4408. id = reference_getid(data);
  4409. start = reference_getindex(data);
  4410. name = reference_getname(data);
  4411. if( not_array_variable(*name) )
  4412. {
  4413. ShowError("script:cleararray: illegal scope\n");
  4414. script_reportdata(data);
  4415. st->state = END;
  4416. return 1;// not supported
  4417. }
  4418. if( not_server_variable(*name) )
  4419. {
  4420. sd = script_rid2sd(st);
  4421. if( sd == NULL )
  4422. return 0;// no player attached
  4423. }
  4424. if( is_string_variable(name) )
  4425. v = (void*)script_getstr(st, 3);
  4426. else
  4427. v = (void*)script_getnum(st, 3);
  4428. end = start + script_getnum(st, 4);
  4429. if( end > 127 )
  4430. end = 127;
  4431. for( ; start <= end; ++start )
  4432. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4433. return 0;
  4434. }
  4435. /// Copies data from one array to another.
  4436. /// ex: copyarray arr[0],arr[2],2;
  4437. ///
  4438. /// copyarray <destination array variable>,<source array variable>,<count>;
  4439. BUILDIN_FUNC(copyarray)
  4440. {
  4441. struct script_data* data1;
  4442. struct script_data* data2;
  4443. char* name1;
  4444. char* name2;
  4445. int32 idx1;
  4446. int32 idx2;
  4447. int32 id1;
  4448. int32 id2;
  4449. void* v;
  4450. int32 i;
  4451. int32 count;
  4452. TBL_PC* sd = NULL;
  4453. data1 = script_getdata(st, 2);
  4454. data2 = script_getdata(st, 3);
  4455. if( !data_isreference(data1) || !data_isreference(data2) )
  4456. {
  4457. ShowError("script:copyarray: not a variable\n");
  4458. script_reportdata(data1);
  4459. script_reportdata(data2);
  4460. st->state = END;
  4461. return 1;// not a variable
  4462. }
  4463. id1 = reference_getid(data1);
  4464. id2 = reference_getid(data2);
  4465. idx1 = reference_getindex(data1);
  4466. idx2 = reference_getindex(data2);
  4467. name1 = reference_getname(data1);
  4468. name2 = reference_getname(data2);
  4469. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4470. {
  4471. ShowError("script:copyarray: illegal scope\n");
  4472. script_reportdata(data1);
  4473. script_reportdata(data2);
  4474. st->state = END;
  4475. return 1;// not supported
  4476. }
  4477. if( is_string_variable(name1) != is_string_variable(name2) )
  4478. {
  4479. ShowError("script:copyarray: type mismatch\n");
  4480. script_reportdata(data1);
  4481. script_reportdata(data2);
  4482. st->state = END;
  4483. return 1;// data type mismatch
  4484. }
  4485. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4486. {
  4487. sd = script_rid2sd(st);
  4488. if( sd == NULL )
  4489. return 0;// no player attached
  4490. }
  4491. count = script_getnum(st, 4);
  4492. if( count > 128 - idx1 )
  4493. count = 128 - idx1;
  4494. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4495. return 0;// nothing to copy
  4496. if( id1 == id2 && idx1 > idx2 )
  4497. {// destination might be overlapping the source - copy in reverse order
  4498. for( i = count - 1; i >= 0; --i )
  4499. {
  4500. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4501. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4502. script_removetop(st, -1, 0);
  4503. }
  4504. }
  4505. else
  4506. {// normal copy
  4507. for( i = 0; i < count; ++i )
  4508. {
  4509. if( idx2 + i < 128 )
  4510. {
  4511. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4512. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4513. script_removetop(st, -1, 0);
  4514. }
  4515. else// out of range - assume ""/0
  4516. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4517. }
  4518. }
  4519. return 0;
  4520. }
  4521. /// Returns the size of the array.
  4522. /// Assumes that everything before the starting index exists.
  4523. /// ex: getarraysize(arr[3])
  4524. ///
  4525. /// getarraysize(<array variable>) -> <int>
  4526. BUILDIN_FUNC(getarraysize)
  4527. {
  4528. struct script_data* data;
  4529. char* name;
  4530. data = script_getdata(st, 2);
  4531. if( !data_isreference(data) )
  4532. {
  4533. ShowError("script:getarraysize: not a variable\n");
  4534. script_reportdata(data);
  4535. script_pushnil(st);
  4536. st->state = END;
  4537. return 1;// not a variable
  4538. }
  4539. name = reference_getname(data);
  4540. if( not_array_variable(*name) )
  4541. {
  4542. ShowError("script:getarraysize: illegal scope\n");
  4543. script_reportdata(data);
  4544. script_pushnil(st);
  4545. st->state = END;
  4546. return 1;// not supported
  4547. }
  4548. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4549. return 0;
  4550. }
  4551. /// Deletes count or all the elements in an array, from the starting index.
  4552. /// ex: deletearray arr[4],2;
  4553. ///
  4554. /// deletearray <array variable>;
  4555. /// deletearray <array variable>,<count>;
  4556. BUILDIN_FUNC(deletearray)
  4557. {
  4558. struct script_data* data;
  4559. char* name;
  4560. int start;
  4561. int end;
  4562. int id;
  4563. TBL_PC *sd = NULL;
  4564. data = script_getdata(st, 2);
  4565. if( !data_isreference(data) )
  4566. {
  4567. ShowError("script:deletearray: not a variable\n");
  4568. script_reportdata(data);
  4569. st->state = END;
  4570. return 1;// not a variable
  4571. }
  4572. id = reference_getid(data);
  4573. start = reference_getindex(data);
  4574. name = reference_getname(data);
  4575. if( not_array_variable(*name) )
  4576. {
  4577. ShowError("script:deletearray: illegal scope\n");
  4578. script_reportdata(data);
  4579. st->state = END;
  4580. return 1;// not supported
  4581. }
  4582. if( not_server_variable(*name) )
  4583. {
  4584. sd = script_rid2sd(st);
  4585. if( sd == NULL )
  4586. return 0;// no player attached
  4587. }
  4588. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4589. if( start >= end )
  4590. return 0;// nothing to free
  4591. if( script_hasdata(st,3) )
  4592. {
  4593. int count = script_getnum(st, 3);
  4594. if( count > end - start )
  4595. count = end - start;
  4596. if( count <= 0 )
  4597. return 0;// nothing to free
  4598. // move rest of the elements backward
  4599. for( ; start + count < end; ++start )
  4600. {
  4601. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4602. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4603. script_removetop(st, -1, 0);
  4604. }
  4605. }
  4606. // clear the rest of the array
  4607. if( is_string_variable(name) )
  4608. {
  4609. for( ; start < end; ++start )
  4610. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4611. }
  4612. else
  4613. {
  4614. for( ; start < end; ++start )
  4615. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4616. }
  4617. return 0;
  4618. }
  4619. /// Returns a reference to the target index of the array variable.
  4620. /// Equivalent to var[index].
  4621. ///
  4622. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4623. BUILDIN_FUNC(getelementofarray)
  4624. {
  4625. struct script_data* data;
  4626. char* name;
  4627. int32 id;
  4628. int i;
  4629. data = script_getdata(st, 2);
  4630. if( !data_isreference(data) )
  4631. {
  4632. ShowError("script:getelementofarray: not a variable\n");
  4633. script_reportdata(data);
  4634. script_pushnil(st);
  4635. st->state = END;
  4636. return 1;// not a variable
  4637. }
  4638. id = reference_getid(data);
  4639. name = reference_getname(data);
  4640. if( not_array_variable(*name) )
  4641. {
  4642. ShowError("script:getelementofarray: illegal scope\n");
  4643. script_reportdata(data);
  4644. script_pushnil(st);
  4645. st->state = END;
  4646. return 1;// not supported
  4647. }
  4648. i = script_getnum(st, 3);
  4649. if( i < 0 || i >= 128 )
  4650. {
  4651. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4652. script_reportdata(data);
  4653. script_pushnil(st);
  4654. st->state = END;
  4655. return 1;// out of range
  4656. }
  4657. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4658. return 0;
  4659. }
  4660. /////////////////////////////////////////////////////////////////////
  4661. /// ...
  4662. ///
  4663. /*==========================================
  4664. *
  4665. *------------------------------------------*/
  4666. BUILDIN_FUNC(setlook)
  4667. {
  4668. int type,val;
  4669. TBL_PC* sd;
  4670. type=script_getnum(st,2);
  4671. val=script_getnum(st,3);
  4672. sd = script_rid2sd(st);
  4673. if( sd == NULL )
  4674. return 0;
  4675. pc_changelook(sd,type,val);
  4676. return 0;
  4677. }
  4678. /*==========================================
  4679. *
  4680. *------------------------------------------*/
  4681. BUILDIN_FUNC(cutin)
  4682. {
  4683. TBL_PC* sd;
  4684. sd = script_rid2sd(st);
  4685. if( sd == NULL )
  4686. return 0;
  4687. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4688. return 0;
  4689. }
  4690. /*==========================================
  4691. *
  4692. *------------------------------------------*/
  4693. BUILDIN_FUNC(viewpoint)
  4694. {
  4695. int type,x,y,id,color;
  4696. TBL_PC* sd;
  4697. type=script_getnum(st,2);
  4698. x=script_getnum(st,3);
  4699. y=script_getnum(st,4);
  4700. id=script_getnum(st,5);
  4701. color=script_getnum(st,6);
  4702. sd = script_rid2sd(st);
  4703. if( sd == NULL )
  4704. return 0;
  4705. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4706. return 0;
  4707. }
  4708. /*==========================================
  4709. *
  4710. *------------------------------------------*/
  4711. BUILDIN_FUNC(countitem)
  4712. {
  4713. int nameid, i;
  4714. int count = 0;
  4715. struct script_data* data;
  4716. TBL_PC* sd = script_rid2sd(st);
  4717. if (!sd) {
  4718. script_pushint(st,0);
  4719. return 0;
  4720. }
  4721. data = script_getdata(st,2);
  4722. get_val(st,data);
  4723. if( data_isstring(data) ) {
  4724. const char* name = conv_str(st,data);
  4725. struct item_data* item_data;
  4726. if((item_data = itemdb_searchname(name)) != NULL)
  4727. nameid = item_data->nameid;
  4728. else
  4729. nameid = 0;
  4730. } else
  4731. nameid = conv_num(st,data);
  4732. if (nameid < 500) {
  4733. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4734. script_reportsrc(st);
  4735. script_pushint(st,0);
  4736. return 1;
  4737. }
  4738. for(i = 0; i < MAX_INVENTORY; i++)
  4739. if(sd->status.inventory[i].nameid == nameid)
  4740. count += sd->status.inventory[i].amount;
  4741. script_pushint(st,count);
  4742. return 0;
  4743. }
  4744. /*==========================================
  4745. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4746. * returns number of items that meet the conditions
  4747. *------------------------------------------*/
  4748. BUILDIN_FUNC(countitem2)
  4749. {
  4750. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4751. int count = 0;
  4752. int i;
  4753. struct script_data* data;
  4754. TBL_PC* sd = script_rid2sd(st);
  4755. if (!sd) {
  4756. script_pushint(st,0);
  4757. return 0;
  4758. }
  4759. data = script_getdata(st,2);
  4760. get_val(st,data);
  4761. if( data_isstring(data) ) {
  4762. const char* name = conv_str(st,data);
  4763. struct item_data* item_data;
  4764. if((item_data = itemdb_searchname(name)) != NULL)
  4765. nameid = item_data->nameid;
  4766. else
  4767. nameid = 0;
  4768. } else
  4769. nameid = conv_num(st,data);
  4770. iden = script_getnum(st,3);
  4771. ref = script_getnum(st,4);
  4772. attr = script_getnum(st,5);
  4773. c1 = (short)script_getnum(st,6);
  4774. c2 = (short)script_getnum(st,7);
  4775. c3 = (short)script_getnum(st,8);
  4776. c4 = (short)script_getnum(st,9);
  4777. if (nameid < 500) {
  4778. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4779. script_pushint(st,0);
  4780. return 1;
  4781. }
  4782. for(i = 0; i < MAX_INVENTORY; i++)
  4783. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4784. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4785. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4786. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4787. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4788. sd->status.inventory[i].card[3] == c4
  4789. )
  4790. count += sd->status.inventory[i].amount;
  4791. script_pushint(st,count);
  4792. return 0;
  4793. }
  4794. /*==========================================
  4795. * 重量チェック
  4796. *------------------------------------------*/
  4797. BUILDIN_FUNC(checkweight)
  4798. {
  4799. int nameid=0,amount,i;
  4800. unsigned long weight;
  4801. TBL_PC *sd;
  4802. struct script_data *data;
  4803. sd = script_rid2sd(st);
  4804. if( sd == NULL )
  4805. return 0;
  4806. data=script_getdata(st,2);
  4807. get_val(st,data);
  4808. if( data_isstring(data) ){
  4809. const char *name=conv_str(st,data);
  4810. struct item_data *item_data = itemdb_searchname(name);
  4811. if( item_data )
  4812. nameid=item_data->nameid;
  4813. }else
  4814. nameid=conv_num(st,data);
  4815. amount=script_getnum(st,3);
  4816. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4817. script_pushint(st,0);
  4818. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4819. return 1;
  4820. }
  4821. weight = itemdb_weight(nameid)*amount;
  4822. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4823. script_pushint(st,0);
  4824. } else {
  4825. //Check if the inventory ain't full.
  4826. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4827. i = pc_search_inventory(sd,0);
  4828. if (i >= 0) //Empty slot available.
  4829. script_pushint(st,1);
  4830. else //Inventory full
  4831. script_pushint(st,0);
  4832. }
  4833. return 0;
  4834. }
  4835. /*==========================================
  4836. * getitem <item id>,<amount>{,<character ID>};
  4837. * getitem "<item name>",<amount>{,<character ID>};
  4838. *------------------------------------------*/
  4839. BUILDIN_FUNC(getitem)
  4840. {
  4841. int nameid,amount,get_count,i,flag = 0;
  4842. struct item it;
  4843. TBL_PC *sd;
  4844. struct script_data *data;
  4845. data=script_getdata(st,2);
  4846. get_val(st,data);
  4847. if( data_isstring(data) )
  4848. {// "<item name>"
  4849. const char *name=conv_str(st,data);
  4850. struct item_data *item_data = itemdb_searchname(name);
  4851. if( item_data == NULL ){
  4852. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4853. return 1; //No item created.
  4854. }
  4855. nameid=item_data->nameid;
  4856. } else if( data_isint(data) )
  4857. {// <item id>
  4858. nameid=conv_num(st,data);
  4859. //Violet Box, Blue Box, etc - random item pick
  4860. if( nameid < 0 ) {
  4861. nameid=itemdb_searchrandomid(-nameid);
  4862. flag = 1;
  4863. }
  4864. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4865. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4866. return 1; //No item created.
  4867. }
  4868. } else {
  4869. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4870. return 1;
  4871. }
  4872. // <amount>
  4873. if( (amount=script_getnum(st,3)) <= 0)
  4874. return 0; //return if amount <=0, skip the useles iteration
  4875. memset(&it,0,sizeof(it));
  4876. it.nameid=nameid;
  4877. if(!flag)
  4878. it.identify=1;
  4879. else
  4880. it.identify=itemdb_isidentified(nameid);
  4881. if( script_hasdata(st,4) )
  4882. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  4883. else
  4884. sd=script_rid2sd(st); // Attached player
  4885. if( sd == NULL ) // no target
  4886. return 0;
  4887. //Check if it's stackable.
  4888. if (!itemdb_isstackable(nameid))
  4889. get_count = 1;
  4890. else
  4891. get_count = amount;
  4892. for (i = 0; i < amount; i += get_count)
  4893. {
  4894. // if not pet egg
  4895. if (!pet_create_egg(sd, nameid))
  4896. {
  4897. if ((flag = pc_additem(sd, &it, get_count)))
  4898. {
  4899. clif_additem(sd, 0, 0, flag);
  4900. if( pc_candrop(sd,&it) )
  4901. map_addflooritem(&it,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4902. }
  4903. }
  4904. }
  4905. //Logs items, got from (N)PC scripts [Lupus]
  4906. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4907. log_pick_pc(sd, "N", nameid, amount, NULL);
  4908. return 0;
  4909. }
  4910. /*==========================================
  4911. *
  4912. *------------------------------------------*/
  4913. BUILDIN_FUNC(getitem2)
  4914. {
  4915. int nameid,amount,get_count,i,flag = 0;
  4916. int iden,ref,attr,c1,c2,c3,c4;
  4917. struct item_data *item_data;
  4918. struct item item_tmp;
  4919. TBL_PC *sd;
  4920. struct script_data *data;
  4921. if( script_hasdata(st,11) )
  4922. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  4923. else
  4924. sd=script_rid2sd(st); // Attached player
  4925. if( sd == NULL ) // no target
  4926. return 0;
  4927. data=script_getdata(st,2);
  4928. get_val(st,data);
  4929. if( data_isstring(data) ){
  4930. const char *name=conv_str(st,data);
  4931. struct item_data *item_data = itemdb_searchname(name);
  4932. if( item_data )
  4933. nameid=item_data->nameid;
  4934. else
  4935. nameid=UNKNOWN_ITEM_ID;
  4936. }else
  4937. nameid=conv_num(st,data);
  4938. amount=script_getnum(st,3);
  4939. iden=script_getnum(st,4);
  4940. ref=script_getnum(st,5);
  4941. attr=script_getnum(st,6);
  4942. c1=script_getnum(st,7);
  4943. c2=script_getnum(st,8);
  4944. c3=script_getnum(st,9);
  4945. c4=script_getnum(st,10);
  4946. if(nameid<0) { // ランダム
  4947. nameid=itemdb_searchrandomid(-nameid);
  4948. flag = 1;
  4949. }
  4950. if(nameid > 0) {
  4951. memset(&item_tmp,0,sizeof(item_tmp));
  4952. item_data=itemdb_exists(nameid);
  4953. if (item_data == NULL)
  4954. return -1;
  4955. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4956. if(ref > 10) ref = 10;
  4957. }
  4958. else if(item_data->type==IT_PETEGG) {
  4959. iden = 1;
  4960. ref = 0;
  4961. }
  4962. else {
  4963. iden = 1;
  4964. ref = attr = 0;
  4965. }
  4966. item_tmp.nameid=nameid;
  4967. if(!flag)
  4968. item_tmp.identify=iden;
  4969. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4970. item_tmp.identify=0;
  4971. item_tmp.refine=ref;
  4972. item_tmp.attribute=attr;
  4973. item_tmp.card[0]=c1;
  4974. item_tmp.card[1]=c2;
  4975. item_tmp.card[2]=c3;
  4976. item_tmp.card[3]=c4;
  4977. //Check if it's stackable.
  4978. if (!itemdb_isstackable(nameid))
  4979. get_count = 1;
  4980. else
  4981. get_count = amount;
  4982. for (i = 0; i < amount; i += get_count)
  4983. {
  4984. // if not pet egg
  4985. if (!pet_create_egg(sd, nameid))
  4986. {
  4987. if ((flag = pc_additem(sd, &item_tmp, get_count)))
  4988. {
  4989. clif_additem(sd, 0, 0, flag);
  4990. if( pc_candrop(sd,&item_tmp) )
  4991. map_addflooritem(&item_tmp,get_count,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4992. }
  4993. }
  4994. }
  4995. //Logs items, got from (N)PC scripts [Lupus]
  4996. if(log_config.enable_logs&0x40)
  4997. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4998. }
  4999. return 0;
  5000. }
  5001. /*==========================================
  5002. * gets an item with someone's name inscribed [Skotlex]
  5003. * getinscribeditem item_num, character_name
  5004. * Returned Qty is always 1, only works on equip-able
  5005. * equipment
  5006. *------------------------------------------*/
  5007. BUILDIN_FUNC(getnameditem)
  5008. {
  5009. int nameid;
  5010. struct item item_tmp;
  5011. TBL_PC *sd, *tsd;
  5012. struct script_data *data;
  5013. sd = script_rid2sd(st);
  5014. if (sd == NULL)
  5015. { //Player not attached!
  5016. script_pushint(st,0);
  5017. return 0;
  5018. }
  5019. data=script_getdata(st,2);
  5020. get_val(st,data);
  5021. if( data_isstring(data) ){
  5022. const char *name=conv_str(st,data);
  5023. struct item_data *item_data = itemdb_searchname(name);
  5024. if( item_data == NULL)
  5025. { //Failed
  5026. script_pushint(st,0);
  5027. return 0;
  5028. }
  5029. nameid = item_data->nameid;
  5030. }else
  5031. nameid = conv_num(st,data);
  5032. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5033. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5034. script_pushint(st,0);
  5035. return 0;
  5036. }
  5037. data=script_getdata(st,3);
  5038. get_val(st,data);
  5039. if( data_isstring(data) ) //Char Name
  5040. tsd=map_nick2sd(conv_str(st,data));
  5041. else //Char Id was given
  5042. tsd=map_charid2sd(conv_num(st,data));
  5043. if( tsd == NULL )
  5044. { //Failed
  5045. script_pushint(st,0);
  5046. return 0;
  5047. }
  5048. memset(&item_tmp,0,sizeof(item_tmp));
  5049. item_tmp.nameid=nameid;
  5050. item_tmp.amount=1;
  5051. item_tmp.identify=1;
  5052. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5053. item_tmp.card[2]=tsd->status.char_id;
  5054. item_tmp.card[3]=tsd->status.char_id >> 16;
  5055. if(pc_additem(sd,&item_tmp,1)) {
  5056. script_pushint(st,0);
  5057. return 0; //Failed to add item, we will not drop if they don't fit
  5058. }
  5059. //Logs items, got from (N)PC scripts [Lupus]
  5060. if(log_config.enable_logs&0x40)
  5061. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5062. script_pushint(st,1);
  5063. return 0;
  5064. }
  5065. /*==========================================
  5066. * gets a random item ID from an item group [Skotlex]
  5067. * groupranditem group_num
  5068. *------------------------------------------*/
  5069. BUILDIN_FUNC(grouprandomitem)
  5070. {
  5071. int group;
  5072. group = script_getnum(st,2);
  5073. script_pushint(st,itemdb_searchrandomid(group));
  5074. return 0;
  5075. }
  5076. /*==========================================
  5077. *
  5078. *------------------------------------------*/
  5079. BUILDIN_FUNC(makeitem)
  5080. {
  5081. int nameid,amount,flag = 0;
  5082. int x,y,m;
  5083. const char *mapname;
  5084. struct item item_tmp;
  5085. struct script_data *data;
  5086. data=script_getdata(st,2);
  5087. get_val(st,data);
  5088. if( data_isstring(data) ){
  5089. const char *name=conv_str(st,data);
  5090. struct item_data *item_data = itemdb_searchname(name);
  5091. if( item_data )
  5092. nameid=item_data->nameid;
  5093. else
  5094. nameid=UNKNOWN_ITEM_ID;
  5095. }else
  5096. nameid=conv_num(st,data);
  5097. amount=script_getnum(st,3);
  5098. mapname =script_getstr(st,4);
  5099. x =script_getnum(st,5);
  5100. y =script_getnum(st,6);
  5101. if(strcmp(mapname,"this")==0)
  5102. {
  5103. TBL_PC *sd;
  5104. sd = script_rid2sd(st);
  5105. if (!sd) return 0; //Failed...
  5106. m=sd->bl.m;
  5107. } else
  5108. m=map_mapname2mapid(mapname);
  5109. if(nameid<0) { // ランダム
  5110. nameid=itemdb_searchrandomid(-nameid);
  5111. flag = 1;
  5112. }
  5113. if(nameid > 0) {
  5114. memset(&item_tmp,0,sizeof(item_tmp));
  5115. item_tmp.nameid=nameid;
  5116. if(!flag)
  5117. item_tmp.identify=1;
  5118. else
  5119. item_tmp.identify=itemdb_isidentified(nameid);
  5120. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5121. }
  5122. return 0;
  5123. }
  5124. /*==========================================
  5125. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5126. *------------------------------------------*/
  5127. BUILDIN_FUNC(delitem)
  5128. {
  5129. int nameid=0,amount,i,important_item=0;
  5130. TBL_PC *sd;
  5131. struct script_data *data;
  5132. if( script_hasdata(st,4) )
  5133. sd=map_id2sd(script_getnum(st,4)); // <Account ID>
  5134. else
  5135. sd=script_rid2sd(st); // Attached player
  5136. if( sd == NULL ) // no target
  5137. return 0;
  5138. data=script_getdata(st,2);
  5139. get_val(st,data);
  5140. if( data_isstring(data) ){
  5141. const char *name=conv_str(st,data);
  5142. struct item_data *item_data = itemdb_searchname(name);
  5143. //nameid=UNKNOWN_ITEM_ID;
  5144. if( item_data )
  5145. nameid=item_data->nameid;
  5146. }else
  5147. nameid=conv_num(st,data);
  5148. amount=script_getnum(st,3);
  5149. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5150. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5151. return 0;
  5152. }
  5153. //1st pass
  5154. //here we won't delete items with CARDS, named items but we count them
  5155. for(i=0;i<MAX_INVENTORY;i++){
  5156. //we don't delete wrong item or equipped item
  5157. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5158. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5159. continue;
  5160. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5161. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5162. sd->status.inventory[i].card[0] == CARD0_PET)
  5163. {
  5164. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5165. continue; //pet couldn't be sent for deletion.
  5166. } else
  5167. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5168. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5169. sd->status.inventory[i].card[0] ||
  5170. sd->status.inventory[i].refine) {
  5171. //this is important item, count it (except for pet eggs)
  5172. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5173. important_item++;
  5174. continue;
  5175. }
  5176. if(sd->status.inventory[i].amount>=amount){
  5177. //Logs items, got from (N)PC scripts [Lupus]
  5178. if(log_config.enable_logs&0x40)
  5179. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5180. pc_delitem(sd,i,amount,0);
  5181. return 0; //we deleted exact amount of items. now exit
  5182. } else {
  5183. amount-=sd->status.inventory[i].amount;
  5184. //Logs items, got from (N)PC scripts [Lupus]
  5185. if(log_config.enable_logs&0x40) {
  5186. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5187. }
  5188. //Logs
  5189. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5190. }
  5191. }
  5192. //2nd pass
  5193. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5194. if (important_item>0 && amount>0)
  5195. for(i=0;i<MAX_INVENTORY;i++){
  5196. //we don't delete wrong item
  5197. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5198. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5199. continue;
  5200. if(sd->status.inventory[i].amount>=amount){
  5201. //Logs items, got from (N)PC scripts [Lupus]
  5202. if(log_config.enable_logs&0x40)
  5203. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5204. pc_delitem(sd,i,amount,0);
  5205. return 0; //we deleted exact amount of items. now exit
  5206. } else {
  5207. amount-=sd->status.inventory[i].amount;
  5208. //Logs items, got from (N)PC scripts [Lupus]
  5209. if(log_config.enable_logs&0x40)
  5210. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5211. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5212. }
  5213. }
  5214. return 0;
  5215. }
  5216. /*==========================================
  5217. * advanced version of delitem [modified by Mihilion]
  5218. *------------------------------------------*/
  5219. BUILDIN_FUNC(delitem2)
  5220. {
  5221. int nameid=0,amount,i=0;
  5222. int iden,ref,attr,c1,c2,c3,c4;
  5223. TBL_PC *sd;
  5224. struct script_data *data;
  5225. if( script_hasdata(st,11) )
  5226. sd=map_id2sd(script_getnum(st,11)); // <Account ID>
  5227. else
  5228. sd=script_rid2sd(st); // Attached player
  5229. if( sd == NULL ) // no target
  5230. return 0;
  5231. data=script_getdata(st,2);
  5232. get_val(st,data);
  5233. if( data_isstring(data) ){
  5234. const char *name=conv_str(st,data);
  5235. struct item_data *item_data = itemdb_searchname(name);
  5236. //nameid=UNKNOWN_ITEM_ID;
  5237. if( item_data )
  5238. nameid=item_data->nameid;
  5239. }else
  5240. nameid=conv_num(st,data);
  5241. amount=script_getnum(st,3);
  5242. iden=script_getnum(st,4);
  5243. ref=script_getnum(st,5);
  5244. attr=script_getnum(st,6);
  5245. c1=script_getnum(st,7);
  5246. c2=script_getnum(st,8);
  5247. c3=script_getnum(st,9);
  5248. c4=script_getnum(st,10);
  5249. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5250. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5251. return 0;
  5252. }
  5253. for(i=0;i<MAX_INVENTORY;i++){
  5254. //we don't delete wrong item or equipped item
  5255. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5256. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5257. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5258. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5259. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5260. sd->status.inventory[i].card[3]!=c4)
  5261. continue;
  5262. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5263. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5264. {
  5265. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5266. continue; //Failed to send delete the pet.
  5267. }
  5268. if(sd->status.inventory[i].amount>=amount){
  5269. //Logs items, got from (N)PC scripts [Lupus]
  5270. if(log_config.enable_logs&0x40)
  5271. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5272. pc_delitem(sd,i,amount,0);
  5273. return 0; //we deleted exact amount of items. now exit
  5274. } else {
  5275. amount-=sd->status.inventory[i].amount;
  5276. //Logs items, got from (N)PC scripts [Lupus]
  5277. if(log_config.enable_logs&0x40)
  5278. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5279. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5280. }
  5281. }
  5282. return 0;
  5283. }
  5284. /*==========================================
  5285. * Enables/Disables use of items while in an NPC [Skotlex]
  5286. *------------------------------------------*/
  5287. BUILDIN_FUNC(enableitemuse)
  5288. {
  5289. TBL_PC *sd;
  5290. sd=script_rid2sd(st);
  5291. if (sd)
  5292. sd->npc_item_flag = st->oid;
  5293. return 0;
  5294. }
  5295. BUILDIN_FUNC(disableitemuse)
  5296. {
  5297. TBL_PC *sd;
  5298. sd=script_rid2sd(st);
  5299. if (sd)
  5300. sd->npc_item_flag = 0;
  5301. return 0;
  5302. }
  5303. /*==========================================
  5304. *キャラ関係のパラメータ取得
  5305. *------------------------------------------*/
  5306. BUILDIN_FUNC(readparam)
  5307. {
  5308. int type;
  5309. TBL_PC *sd;
  5310. type=script_getnum(st,2);
  5311. if( script_hasdata(st,3) )
  5312. sd=map_nick2sd(script_getstr(st,3));
  5313. else
  5314. sd=script_rid2sd(st);
  5315. if(sd==NULL){
  5316. script_pushint(st,-1);
  5317. return 0;
  5318. }
  5319. script_pushint(st,pc_readparam(sd,type));
  5320. return 0;
  5321. }
  5322. /*==========================================
  5323. *キャラ関係のID取得
  5324. *------------------------------------------*/
  5325. BUILDIN_FUNC(getcharid)
  5326. {
  5327. int num;
  5328. TBL_PC *sd;
  5329. num = script_getnum(st,2);
  5330. if( script_hasdata(st,3) )
  5331. sd=map_nick2sd(script_getstr(st,3));
  5332. else
  5333. sd=script_rid2sd(st);
  5334. if(sd==NULL){
  5335. script_pushint(st,0); //return 0, according docs
  5336. return 0;
  5337. }
  5338. switch( num ) {
  5339. case 0: script_pushint(st,sd->status.char_id); break;
  5340. case 1: script_pushint(st,sd->status.party_id); break;
  5341. case 2: script_pushint(st,sd->status.guild_id); break;
  5342. case 3: script_pushint(st,sd->status.account_id); break;
  5343. default:
  5344. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5345. script_pushint(st,0);
  5346. break;
  5347. }
  5348. return 0;
  5349. }
  5350. /*==========================================
  5351. *指定IDのPT名取得
  5352. *------------------------------------------*/
  5353. char *buildin_getpartyname_sub(int party_id)
  5354. {
  5355. struct party_data *p;
  5356. p=party_search(party_id);
  5357. if(p!=NULL){
  5358. char *buf;
  5359. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5360. memcpy(buf, p->party.name, NAME_LENGTH);
  5361. buf[NAME_LENGTH-1] = '\0';
  5362. return buf;
  5363. }
  5364. return 0;
  5365. }
  5366. BUILDIN_FUNC(getpartyname)
  5367. {
  5368. char *name;
  5369. int party_id;
  5370. party_id=script_getnum(st,2);
  5371. name=buildin_getpartyname_sub(party_id);
  5372. if(name != NULL)
  5373. script_pushstr(st,name);
  5374. else
  5375. script_pushconststr(st,"null");
  5376. return 0;
  5377. }
  5378. /*==========================================
  5379. *指定IDのPT人数とメンバーID取得
  5380. *------------------------------------------*/
  5381. BUILDIN_FUNC(getpartymember)
  5382. {
  5383. struct party_data *p;
  5384. int i,j=0,type=0;
  5385. p=party_search(script_getnum(st,2));
  5386. if( script_hasdata(st,3) )
  5387. type=script_getnum(st,3);
  5388. if(p!=NULL){
  5389. for(i=0;i<MAX_PARTY;i++){
  5390. if(p->party.member[i].account_id){
  5391. switch (type) {
  5392. case 2:
  5393. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5394. break;
  5395. case 1:
  5396. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5397. break;
  5398. default:
  5399. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5400. }
  5401. j++;
  5402. }
  5403. }
  5404. }
  5405. mapreg_setreg(add_str("$@partymembercount"),j);
  5406. return 0;
  5407. }
  5408. /*==========================================
  5409. * Retrieves party leader. if flag is specified,
  5410. * return some of the leader data. Otherwise, return name.
  5411. *------------------------------------------*/
  5412. BUILDIN_FUNC(getpartyleader)
  5413. {
  5414. int party_id, type = 0, i=0;
  5415. struct party_data *p;
  5416. party_id=script_getnum(st,2);
  5417. if( script_hasdata(st,3) )
  5418. type=script_getnum(st,3);
  5419. p=party_search(party_id);
  5420. if (p) //Search leader
  5421. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5422. if (!p || i == MAX_PARTY) { //leader not found
  5423. if (type)
  5424. script_pushint(st,-1);
  5425. else
  5426. script_pushconststr(st,"null");
  5427. return 0;
  5428. }
  5429. switch (type) {
  5430. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5431. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5432. case 3: script_pushint(st,p->party.member[i].class_); break;
  5433. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5434. case 5: script_pushint(st,p->party.member[i].lv); break;
  5435. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5436. }
  5437. return 0;
  5438. }
  5439. /*==========================================
  5440. *指定IDのギルド名取得
  5441. *------------------------------------------*/
  5442. char *buildin_getguildname_sub(int guild_id)
  5443. {
  5444. struct guild *g=NULL;
  5445. g=guild_search(guild_id);
  5446. if(g!=NULL){
  5447. char *buf;
  5448. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5449. memcpy(buf, g->name, NAME_LENGTH);
  5450. buf[NAME_LENGTH-1] = '\0';
  5451. return buf;
  5452. }
  5453. return NULL;
  5454. }
  5455. BUILDIN_FUNC(getguildname)
  5456. {
  5457. char *name;
  5458. int guild_id=script_getnum(st,2);
  5459. name=buildin_getguildname_sub(guild_id);
  5460. if(name != NULL)
  5461. script_pushstr(st,name);
  5462. else
  5463. script_pushconststr(st,"null");
  5464. return 0;
  5465. }
  5466. /*==========================================
  5467. *指定IDのGuildMaster名取得
  5468. *------------------------------------------*/
  5469. char *buildin_getguildmaster_sub(int guild_id)
  5470. {
  5471. struct guild *g=NULL;
  5472. g=guild_search(guild_id);
  5473. if(g!=NULL){
  5474. char *buf;
  5475. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5476. memcpy(buf, g->master, NAME_LENGTH);
  5477. buf[NAME_LENGTH-1] = '\0';
  5478. return buf;
  5479. }
  5480. return 0;
  5481. }
  5482. BUILDIN_FUNC(getguildmaster)
  5483. {
  5484. char *master;
  5485. int guild_id=script_getnum(st,2);
  5486. master=buildin_getguildmaster_sub(guild_id);
  5487. if(master!=0)
  5488. script_pushstr(st,master);
  5489. else
  5490. script_pushconststr(st,"null");
  5491. return 0;
  5492. }
  5493. BUILDIN_FUNC(getguildmasterid)
  5494. {
  5495. char *master;
  5496. TBL_PC *sd=NULL;
  5497. int guild_id=script_getnum(st,2);
  5498. master=buildin_getguildmaster_sub(guild_id);
  5499. if(master!=0){
  5500. if((sd=map_nick2sd(master)) == NULL){
  5501. script_pushint(st,0);
  5502. return 0;
  5503. }
  5504. script_pushint(st,sd->status.char_id);
  5505. }else{
  5506. script_pushint(st,0);
  5507. }
  5508. return 0;
  5509. }
  5510. /*==========================================
  5511. * キャラクタの名前
  5512. *------------------------------------------*/
  5513. BUILDIN_FUNC(strcharinfo)
  5514. {
  5515. TBL_PC *sd;
  5516. int num;
  5517. char *buf;
  5518. sd=script_rid2sd(st);
  5519. if (!sd) { //Avoid crashing....
  5520. script_pushconststr(st,"");
  5521. return 0;
  5522. }
  5523. num=script_getnum(st,2);
  5524. switch(num){
  5525. case 0:
  5526. script_pushstrcopy(st,sd->status.name);
  5527. break;
  5528. case 1:
  5529. buf=buildin_getpartyname_sub(sd->status.party_id);
  5530. if(buf!=0)
  5531. script_pushstr(st,buf);
  5532. else
  5533. script_pushconststr(st,"");
  5534. break;
  5535. case 2:
  5536. buf=buildin_getguildname_sub(sd->status.guild_id);
  5537. if(buf != NULL)
  5538. script_pushstr(st,buf);
  5539. else
  5540. script_pushconststr(st,"");
  5541. break;
  5542. default:
  5543. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5544. script_pushconststr(st,"");
  5545. break;
  5546. }
  5547. return 0;
  5548. }
  5549. /*==========================================
  5550. * 呼び出し元のNPC情報を取得する
  5551. *------------------------------------------*/
  5552. BUILDIN_FUNC(strnpcinfo)
  5553. {
  5554. TBL_NPC* nd;
  5555. int num;
  5556. char *buf,*name=NULL;
  5557. nd = map_id2nd(st->oid);
  5558. if (!nd) {
  5559. script_pushconststr(st, "");
  5560. return 0;
  5561. }
  5562. num = script_getnum(st,2);
  5563. switch(num){
  5564. case 0: // display name
  5565. name = aStrdup(nd->name);
  5566. break;
  5567. case 1: // visible part of display name name
  5568. if((buf = strchr(nd->name,'#')) != NULL)
  5569. {
  5570. name = aStrdup(nd->name);
  5571. name[buf - nd->name] = 0;
  5572. }
  5573. break;
  5574. case 2: // # fragment
  5575. if((buf = strchr(nd->name,'#')) != NULL)
  5576. name = aStrdup(buf+1);
  5577. break;
  5578. case 3: // unique name
  5579. name = aStrdup(nd->exname);
  5580. break;
  5581. }
  5582. if(name)
  5583. script_pushstr(st, name);
  5584. else
  5585. script_pushconststr(st, "");
  5586. return 0;
  5587. }
  5588. 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};
  5589. /*==========================================
  5590. * GetEquipID(Pos); Pos: 1-10
  5591. *------------------------------------------*/
  5592. BUILDIN_FUNC(getequipid)
  5593. {
  5594. int i=-1,num;
  5595. TBL_PC *sd;
  5596. struct item_data* item;
  5597. sd=script_rid2sd(st);
  5598. if(sd == NULL)
  5599. return 0;
  5600. num=script_getnum(st,2);
  5601. if (num > 0 && num <= ARRAYLENGTH(equip))
  5602. i=pc_checkequip(sd,equip[num-1]);
  5603. if(i >= 0){
  5604. item=sd->inventory_data[i];
  5605. if(item)
  5606. script_pushint(st,item->nameid);
  5607. else
  5608. script_pushint(st,0);
  5609. }else{
  5610. script_pushint(st,-1);
  5611. }
  5612. return 0;
  5613. }
  5614. /*==========================================
  5615. * 装備名文字列(精錬メニュー用)
  5616. *------------------------------------------*/
  5617. BUILDIN_FUNC(getequipname)
  5618. {
  5619. int i=-1,num;
  5620. TBL_PC *sd;
  5621. struct item_data* item;
  5622. char *buf;
  5623. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  5624. sd = script_rid2sd(st);
  5625. if( sd == NULL )
  5626. return 0;
  5627. buf=(char *)aMallocA(64*sizeof(char));
  5628. num=script_getnum(st,2);
  5629. if (num > 0 && num <= ARRAYLENGTH(equip))
  5630. i=pc_checkequip(sd,equip[num-1]);
  5631. if(i >= 0){
  5632. item=sd->inventory_data[i];
  5633. if(item)
  5634. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5635. else
  5636. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5637. }else{
  5638. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5639. }
  5640. script_pushstr(st,buf);
  5641. return 0;
  5642. }
  5643. /*==========================================
  5644. * getbrokenid [Valaris]
  5645. *------------------------------------------*/
  5646. BUILDIN_FUNC(getbrokenid)
  5647. {
  5648. int i,num,id=0,brokencounter=0;
  5649. TBL_PC *sd;
  5650. sd = script_rid2sd(st);
  5651. if( sd == NULL )
  5652. return 0;
  5653. num=script_getnum(st,2);
  5654. for(i=0; i<MAX_INVENTORY; i++) {
  5655. if(sd->status.inventory[i].attribute==1){
  5656. brokencounter++;
  5657. if(num==brokencounter){
  5658. id=sd->status.inventory[i].nameid;
  5659. break;
  5660. }
  5661. }
  5662. }
  5663. script_pushint(st,id);
  5664. return 0;
  5665. }
  5666. /*==========================================
  5667. * repair [Valaris]
  5668. *------------------------------------------*/
  5669. BUILDIN_FUNC(repair)
  5670. {
  5671. int i,num;
  5672. int repaircounter=0;
  5673. TBL_PC *sd;
  5674. sd = script_rid2sd(st);
  5675. if( sd == NULL )
  5676. return 0;
  5677. num=script_getnum(st,2);
  5678. for(i=0; i<MAX_INVENTORY; i++) {
  5679. if(sd->status.inventory[i].attribute==1){
  5680. repaircounter++;
  5681. if(num==repaircounter){
  5682. sd->status.inventory[i].attribute=0;
  5683. clif_equiplist(sd);
  5684. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5685. clif_misceffect(&sd->bl, 3);
  5686. break;
  5687. }
  5688. }
  5689. }
  5690. return 0;
  5691. }
  5692. /*==========================================
  5693. * 装備チェック
  5694. *------------------------------------------*/
  5695. BUILDIN_FUNC(getequipisequiped)
  5696. {
  5697. int i=-1,num;
  5698. TBL_PC *sd;
  5699. num=script_getnum(st,2);
  5700. sd = script_rid2sd(st);
  5701. if( sd == NULL )
  5702. return 0;
  5703. if (num > 0 && num <= ARRAYLENGTH(equip))
  5704. i=pc_checkequip(sd,equip[num-1]);
  5705. if(i >= 0)
  5706. script_pushint(st,1);
  5707. else
  5708. script_pushint(st,0);
  5709. return 0;
  5710. }
  5711. /*==========================================
  5712. * 装備品精錬可能チェック
  5713. *------------------------------------------*/
  5714. BUILDIN_FUNC(getequipisenableref)
  5715. {
  5716. int i=-1,num;
  5717. TBL_PC *sd;
  5718. num=script_getnum(st,2);
  5719. sd = script_rid2sd(st);
  5720. if( sd == NULL )
  5721. return 0;
  5722. if (num > 0 && num <= ARRAYLENGTH(equip))
  5723. i=pc_checkequip(sd,equip[num-1]);
  5724. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5725. {
  5726. script_pushint(st,1);
  5727. } else {
  5728. script_pushint(st,0);
  5729. }
  5730. return 0;
  5731. }
  5732. /*==========================================
  5733. * 装備品鑑定チェック
  5734. *------------------------------------------*/
  5735. BUILDIN_FUNC(getequipisidentify)
  5736. {
  5737. int i=-1,num;
  5738. TBL_PC *sd;
  5739. num=script_getnum(st,2);
  5740. sd = script_rid2sd(st);
  5741. if( sd == NULL )
  5742. return 0;
  5743. if (num > 0 && num <= ARRAYLENGTH(equip))
  5744. i=pc_checkequip(sd,equip[num-1]);
  5745. if(i >= 0)
  5746. script_pushint(st,sd->status.inventory[i].identify);
  5747. else
  5748. script_pushint(st,0);
  5749. return 0;
  5750. }
  5751. /*==========================================
  5752. * 装備品精錬度
  5753. *------------------------------------------*/
  5754. BUILDIN_FUNC(getequiprefinerycnt)
  5755. {
  5756. int i=-1,num;
  5757. TBL_PC *sd;
  5758. num=script_getnum(st,2);
  5759. sd = script_rid2sd(st);
  5760. if( sd == NULL )
  5761. return 0;
  5762. if (num > 0 && num <= ARRAYLENGTH(equip))
  5763. i=pc_checkequip(sd,equip[num-1]);
  5764. if(i >= 0)
  5765. script_pushint(st,sd->status.inventory[i].refine);
  5766. else
  5767. script_pushint(st,0);
  5768. return 0;
  5769. }
  5770. /*==========================================
  5771. * 装備品武器LV
  5772. *------------------------------------------*/
  5773. BUILDIN_FUNC(getequipweaponlv)
  5774. {
  5775. int i=-1,num;
  5776. TBL_PC *sd;
  5777. num=script_getnum(st,2);
  5778. sd = script_rid2sd(st);
  5779. if( sd == NULL )
  5780. return 0;
  5781. if (num > 0 && num <= ARRAYLENGTH(equip))
  5782. i=pc_checkequip(sd,equip[num-1]);
  5783. if(i >= 0 && sd->inventory_data[i])
  5784. script_pushint(st,sd->inventory_data[i]->wlv);
  5785. else
  5786. script_pushint(st,0);
  5787. return 0;
  5788. }
  5789. /*==========================================
  5790. * 装備品精錬成功率
  5791. *------------------------------------------*/
  5792. BUILDIN_FUNC(getequippercentrefinery)
  5793. {
  5794. int i=-1,num;
  5795. TBL_PC *sd;
  5796. num=script_getnum(st,2);
  5797. sd = script_rid2sd(st);
  5798. if( sd == NULL )
  5799. return 0;
  5800. if (num > 0 && num <= ARRAYLENGTH(equip))
  5801. i=pc_checkequip(sd,equip[num-1]);
  5802. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5803. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5804. else
  5805. script_pushint(st,0);
  5806. return 0;
  5807. }
  5808. /*==========================================
  5809. * 精錬成功
  5810. *------------------------------------------*/
  5811. BUILDIN_FUNC(successrefitem)
  5812. {
  5813. int i=-1,num,ep;
  5814. TBL_PC *sd;
  5815. num=script_getnum(st,2);
  5816. sd = script_rid2sd(st);
  5817. if( sd == NULL )
  5818. return 0;
  5819. if (num > 0 && num <= ARRAYLENGTH(equip))
  5820. i=pc_checkequip(sd,equip[num-1]);
  5821. if(i >= 0) {
  5822. ep=sd->status.inventory[i].equip;
  5823. //Logs items, got from (N)PC scripts [Lupus]
  5824. if(log_config.enable_logs&0x40)
  5825. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5826. sd->status.inventory[i].refine++;
  5827. pc_unequipitem(sd,i,2);
  5828. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5829. clif_delitem(sd,i,1);
  5830. //Logs items, got from (N)PC scripts [Lupus]
  5831. if(log_config.enable_logs&0x40)
  5832. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5833. clif_additem(sd,i,1,0);
  5834. pc_equipitem(sd,i,ep);
  5835. clif_misceffect(&sd->bl,3);
  5836. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5837. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5838. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5839. ){ // Fame point system [DracoRPG]
  5840. switch (sd->inventory_data[i]->wlv){
  5841. case 1:
  5842. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5843. break;
  5844. case 2:
  5845. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5846. break;
  5847. case 3:
  5848. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5849. break;
  5850. }
  5851. }
  5852. }
  5853. return 0;
  5854. }
  5855. /*==========================================
  5856. * 精錬失敗
  5857. *------------------------------------------*/
  5858. BUILDIN_FUNC(failedrefitem)
  5859. {
  5860. int i=-1,num;
  5861. TBL_PC *sd;
  5862. num=script_getnum(st,2);
  5863. sd = script_rid2sd(st);
  5864. if( sd == NULL )
  5865. return 0;
  5866. if (num > 0 && num <= ARRAYLENGTH(equip))
  5867. i=pc_checkequip(sd,equip[num-1]);
  5868. if(i >= 0) {
  5869. //Logs items, got from (N)PC scripts [Lupus]
  5870. if(log_config.enable_logs&0x40)
  5871. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5872. sd->status.inventory[i].refine = 0;
  5873. pc_unequipitem(sd,i,3);
  5874. // 精錬失敗エフェクトのパケット
  5875. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5876. pc_delitem(sd,i,1,0);
  5877. // 他の人にも失敗を通知
  5878. clif_misceffect(&sd->bl,2);
  5879. }
  5880. return 0;
  5881. }
  5882. /*==========================================
  5883. *
  5884. *------------------------------------------*/
  5885. BUILDIN_FUNC(statusup)
  5886. {
  5887. int type;
  5888. TBL_PC *sd;
  5889. type=script_getnum(st,2);
  5890. sd = script_rid2sd(st);
  5891. if( sd == NULL )
  5892. return 0;
  5893. pc_statusup(sd,type);
  5894. return 0;
  5895. }
  5896. /*==========================================
  5897. *
  5898. *------------------------------------------*/
  5899. BUILDIN_FUNC(statusup2)
  5900. {
  5901. int type,val;
  5902. TBL_PC *sd;
  5903. type=script_getnum(st,2);
  5904. val=script_getnum(st,3);
  5905. sd = script_rid2sd(st);
  5906. if( sd == NULL )
  5907. return 0;
  5908. pc_statusup2(sd,type,val);
  5909. return 0;
  5910. }
  5911. /// See 'doc/item_bonus.txt'
  5912. ///
  5913. /// bonus <bonus type>,<val1>;
  5914. /// bonus2 <bonus type>,<val1>,<val2>;
  5915. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5916. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5917. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5918. BUILDIN_FUNC(bonus)
  5919. {
  5920. int type;
  5921. int type2;
  5922. int type3;
  5923. int type4;
  5924. int type5;
  5925. int val;
  5926. TBL_PC* sd;
  5927. sd = script_rid2sd(st);
  5928. if( sd == NULL )
  5929. return 0; // no player attached
  5930. type = script_getnum(st,2);
  5931. switch( script_lastdata(st) ){
  5932. case 3:
  5933. val = script_getnum(st,3);
  5934. pc_bonus(sd, type, val);
  5935. break;
  5936. case 4:
  5937. type2 = script_getnum(st,3);
  5938. val = script_getnum(st,4);
  5939. pc_bonus2(sd, type, type2, val);
  5940. break;
  5941. case 5:
  5942. type2 = script_getnum(st,3);
  5943. type3 = script_getnum(st,4);
  5944. val = script_getnum(st,5);
  5945. pc_bonus3(sd, type, type2, type3, val);
  5946. break;
  5947. case 6:
  5948. type2 = script_getnum(st,3);
  5949. type3 = script_getnum(st,4);
  5950. type4 = script_getnum(st,5);
  5951. val = script_getnum(st,6);
  5952. pc_bonus4(sd, type, type2, type3, type4, val);
  5953. break;
  5954. case 7:
  5955. type2 = script_getnum(st,3);
  5956. type3 = script_getnum(st,4);
  5957. type4 = script_getnum(st,5);
  5958. type5 = script_getnum(st,6);
  5959. val = script_getnum(st,7);
  5960. pc_bonus5(sd, type, type2, type3, type4, type5, val);
  5961. break;
  5962. default:
  5963. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 2));
  5964. }
  5965. return 0;
  5966. }
  5967. /// Bonus script that has a chance of being executed on attack.
  5968. BUILDIN_FUNC(bonusautoscript)
  5969. {
  5970. int rate, flag = 0;
  5971. const char *str;
  5972. struct script_code *script;
  5973. TBL_PC* sd;
  5974. sd = script_rid2sd(st);
  5975. if( sd == NULL )
  5976. return 0;// no player attached, report source
  5977. str = script_getstr(st,2);
  5978. rate = script_getnum(st,3);
  5979. if( script_hasdata(st,4) )
  5980. flag = script_getnum(st,4);
  5981. script = parse_script(str, "autoscript bonus", 0, 0);
  5982. if (!script)
  5983. return 1;
  5984. if (!pc_autoscript_add(sd->autoscript, ARRAYLENGTH(sd->autoscript), rate, flag, script))
  5985. {
  5986. script_free_code(script);
  5987. return 1;
  5988. }
  5989. return 0;
  5990. }
  5991. /// Bonus script that has a chance of being executed when attacked.
  5992. BUILDIN_FUNC(bonusautoscript2)
  5993. {
  5994. int rate, flag = 0;
  5995. const char *str;
  5996. struct script_code *script;
  5997. TBL_PC* sd;
  5998. sd = script_rid2sd(st);
  5999. if( sd == NULL )
  6000. return 0;// no player attached, report source
  6001. str = script_getstr(st,2);
  6002. rate = script_getnum(st,3);
  6003. if( script_hasdata(st,4) )
  6004. flag = script_getnum(st,4);
  6005. script = parse_script(str, "autoscript2 bonus", 0, 0);
  6006. if (!script)
  6007. return 1;
  6008. if (!pc_autoscript_add(sd->autoscript2, ARRAYLENGTH(sd->autoscript2), rate, flag, script))
  6009. {
  6010. script_free_code(script);
  6011. return 1;
  6012. }
  6013. return 0;
  6014. }
  6015. /// Changes the level of a player skill.
  6016. /// <flag> defaults to 1
  6017. /// <flag>=0 : set the level of the skill
  6018. /// <flag>=1 : set the temporary level of the skill
  6019. /// <flag>=2 : add to the level of the skill
  6020. ///
  6021. /// skill <skill id>,<level>,<flag>
  6022. /// skill <skill id>,<level>
  6023. BUILDIN_FUNC(skill)
  6024. {
  6025. int id;
  6026. int level;
  6027. int flag = 1;
  6028. TBL_PC* sd;
  6029. sd = script_rid2sd(st);
  6030. if( sd == NULL )
  6031. return 0;// no player attached, report source
  6032. id = script_getnum(st,2);
  6033. level = script_getnum(st,3);
  6034. if( script_hasdata(st,4) )
  6035. flag = script_getnum(st,4);
  6036. pc_skill(sd, id, level, flag);
  6037. return 0;
  6038. }
  6039. /// Changes the level of a player skill.
  6040. /// like skill, but <flag> defaults to 2
  6041. ///
  6042. /// addtoskill <skill id>,<amount>,<flag>
  6043. /// addtoskill <skill id>,<amount>
  6044. ///
  6045. /// @see skill
  6046. BUILDIN_FUNC(addtoskill)
  6047. {
  6048. int id;
  6049. int level;
  6050. int flag = 2;
  6051. TBL_PC* sd;
  6052. sd = script_rid2sd(st);
  6053. if( sd == NULL )
  6054. return 0;// no player attached, report source
  6055. id = script_getnum(st,2);
  6056. level = script_getnum(st,3);
  6057. if( script_hasdata(st,4) )
  6058. flag = script_getnum(st,4);
  6059. pc_skill(sd, id, level, flag);
  6060. return 0;
  6061. }
  6062. /// Increases the level of a guild skill.
  6063. ///
  6064. /// guildskill <skill id>,<amount>;
  6065. BUILDIN_FUNC(guildskill)
  6066. {
  6067. int id;
  6068. int level;
  6069. TBL_PC* sd;
  6070. int i;
  6071. sd = script_rid2sd(st);
  6072. if( sd == NULL )
  6073. return 0;// no player attached, report source
  6074. id = script_getnum(st,2);
  6075. level = script_getnum(st,3);
  6076. for( i=0; i < level; i++ )
  6077. guild_skillup(sd, id);
  6078. return 0;
  6079. }
  6080. /// Returns the level of the player skill.
  6081. ///
  6082. /// getskilllv(<skill id>) -> <level>
  6083. BUILDIN_FUNC(getskilllv)
  6084. {
  6085. int id;
  6086. TBL_PC* sd;
  6087. sd = script_rid2sd(st);
  6088. if( sd == NULL )
  6089. return 0;// no player attached, report source
  6090. id = script_getnum(st,2);
  6091. script_pushint(st, pc_checkskill(sd,id));
  6092. return 0;
  6093. }
  6094. /// Returns the level of the guild skill.
  6095. ///
  6096. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6097. BUILDIN_FUNC(getgdskilllv)
  6098. {
  6099. int guild_id;
  6100. int skill_id;
  6101. struct guild* g;
  6102. guild_id = script_getnum(st,2);
  6103. skill_id = script_getnum(st,3);
  6104. g = guild_search(guild_id);
  6105. if( g == NULL )
  6106. script_pushint(st, -1);
  6107. else
  6108. script_pushint(st, guild_checkskill(g,skill_id));
  6109. return 0;
  6110. }
  6111. /// Returns the 'basic_skill_check' setting.
  6112. /// This config determines if the server checks the skill level of NV_BASIC
  6113. /// before allowing the basic actions.
  6114. ///
  6115. /// basicskillcheck() -> <bool>
  6116. BUILDIN_FUNC(basicskillcheck)
  6117. {
  6118. script_pushint(st, battle_config.basic_skill_check);
  6119. return 0;
  6120. }
  6121. /// Returns the GM level of the player.
  6122. ///
  6123. /// getgmlevel() -> <level>
  6124. BUILDIN_FUNC(getgmlevel)
  6125. {
  6126. TBL_PC* sd;
  6127. sd = script_rid2sd(st);
  6128. if( sd == NULL )
  6129. return 0;// no player attached, report source
  6130. script_pushint(st, pc_isGM(sd));
  6131. return 0;
  6132. }
  6133. /// Terminates the execution of this script instance.
  6134. ///
  6135. /// end
  6136. BUILDIN_FUNC(end)
  6137. {
  6138. st->state = END;
  6139. return 0;
  6140. }
  6141. /// Checks if the player has that effect state (option).
  6142. ///
  6143. /// checkoption(<option>) -> <bool>
  6144. BUILDIN_FUNC(checkoption)
  6145. {
  6146. int option;
  6147. TBL_PC* sd;
  6148. sd = script_rid2sd(st);
  6149. if( sd == NULL )
  6150. return 0;// no player attached, report source
  6151. option = script_getnum(st,2);
  6152. if( sd->sc.option&option )
  6153. script_pushint(st, 1);
  6154. else
  6155. script_pushint(st, 0);
  6156. return 0;
  6157. }
  6158. /// Checks if the player is in that body state (opt1).
  6159. ///
  6160. /// checkoption1(<opt1>) -> <bool>
  6161. BUILDIN_FUNC(checkoption1)
  6162. {
  6163. int opt1;
  6164. TBL_PC* sd;
  6165. sd = script_rid2sd(st);
  6166. if( sd == NULL )
  6167. return 0;// no player attached, report source
  6168. opt1 = script_getnum(st,2);
  6169. if( sd->sc.opt1 == opt1 )
  6170. script_pushint(st, 1);
  6171. else
  6172. script_pushint(st, 0);
  6173. return 0;
  6174. }
  6175. /// Checks if the player has that health state (opt2).
  6176. ///
  6177. /// checkoption2(<opt2>) -> <bool>
  6178. BUILDIN_FUNC(checkoption2)
  6179. {
  6180. int opt2;
  6181. TBL_PC* sd;
  6182. sd = script_rid2sd(st);
  6183. if( sd == NULL )
  6184. return 0;// no player attached, report source
  6185. opt2 = script_getnum(st,2);
  6186. if( sd->sc.opt2&opt2 )
  6187. script_pushint(st, 1);
  6188. else
  6189. script_pushint(st, 0);
  6190. return 0;
  6191. }
  6192. /// Changes the effect state (option) of the player.
  6193. /// <flag> defaults to 1
  6194. /// <flag>=0 : removes the option
  6195. /// <flag>=other : adds the option
  6196. ///
  6197. /// setoption <option>,<flag>;
  6198. /// setoption <option>;
  6199. BUILDIN_FUNC(setoption)
  6200. {
  6201. int option;
  6202. int flag = 1;
  6203. TBL_PC* sd;
  6204. sd = script_rid2sd(st);
  6205. if( sd == NULL )
  6206. return 0;// no player attached, report source
  6207. option = script_getnum(st,2);
  6208. if( script_hasdata(st,3) )
  6209. flag = script_getnum(st,3);
  6210. else if( !option ){// Request to remove everything.
  6211. flag = 0;
  6212. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6213. }
  6214. if( flag ){// Add option
  6215. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6216. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6217. pc_setoption(sd, sd->sc.option|option);
  6218. } else// Remove option
  6219. pc_setoption(sd, sd->sc.option&~option);
  6220. return 0;
  6221. }
  6222. /// Returns if the player has a cart.
  6223. ///
  6224. /// checkcart() -> <bool>
  6225. ///
  6226. /// @author Valaris
  6227. BUILDIN_FUNC(checkcart)
  6228. {
  6229. TBL_PC* sd;
  6230. sd = script_rid2sd(st);
  6231. if( sd == NULL )
  6232. return 0;// no player attached, report source
  6233. if( pc_iscarton(sd) )
  6234. script_pushint(st, 1);
  6235. else
  6236. script_pushint(st, 0);
  6237. return 0;
  6238. }
  6239. /// Sets the cart of the player.
  6240. /// <type> defaults to 1
  6241. /// <type>=0 : removes the cart
  6242. /// <type>=1 : Normal cart
  6243. /// <type>=2 : Wooden cart
  6244. /// <type>=3 : Covered cart with flowers and ferns
  6245. /// <type>=4 : Wooden cart with a Panda doll on the back
  6246. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6247. ///
  6248. /// setcart <type>;
  6249. /// setcart;
  6250. BUILDIN_FUNC(setcart)
  6251. {
  6252. int type = 1;
  6253. TBL_PC* sd;
  6254. sd = script_rid2sd(st);
  6255. if( sd == NULL )
  6256. return 0;// no player attached, report source
  6257. if( script_hasdata(st,2) )
  6258. type = script_getnum(st,2);
  6259. pc_setcart(sd, type);
  6260. return 0;
  6261. }
  6262. /// Returns if the player has a falcon.
  6263. ///
  6264. /// checkfalcon() -> <bool>
  6265. ///
  6266. /// @author Valaris
  6267. BUILDIN_FUNC(checkfalcon)
  6268. {
  6269. TBL_PC* sd;
  6270. sd = script_rid2sd(st);
  6271. if( sd == NULL )
  6272. return 0;// no player attached, report source
  6273. if( pc_isfalcon(sd) )
  6274. script_pushint(st, 1);
  6275. else
  6276. script_pushint(st, 0);
  6277. return 0;
  6278. }
  6279. /// Sets if the player has a falcon or not.
  6280. /// <flag> defaults to 1
  6281. ///
  6282. /// setfalcon <flag>;
  6283. /// setfalcon;
  6284. BUILDIN_FUNC(setfalcon)
  6285. {
  6286. int flag = 1;
  6287. TBL_PC* sd;
  6288. sd = script_rid2sd(st);
  6289. if( sd == NULL )
  6290. return 0;// no player attached, report source
  6291. if( script_hasdata(st,2) )
  6292. flag = script_getnum(st,2);
  6293. pc_setfalcon(sd, flag);
  6294. return 0;
  6295. }
  6296. /// Returns if the player is riding.
  6297. ///
  6298. /// checkriding() -> <bool>
  6299. ///
  6300. /// @author Valaris
  6301. BUILDIN_FUNC(checkriding)
  6302. {
  6303. TBL_PC* sd;
  6304. sd = script_rid2sd(st);
  6305. if( sd == NULL )
  6306. return 0;// no player attached, report source
  6307. if( pc_isriding(sd) )
  6308. script_pushint(st, 1);
  6309. else
  6310. script_pushint(st, 0);
  6311. return 0;
  6312. }
  6313. /// Sets if the player is riding.
  6314. /// <flag> defaults to 1
  6315. ///
  6316. /// setriding <flag>;
  6317. /// setriding;
  6318. BUILDIN_FUNC(setriding)
  6319. {
  6320. int flag = 1;
  6321. TBL_PC* sd;
  6322. sd = script_rid2sd(st);
  6323. if( sd == NULL )
  6324. return 0;// no player attached, report source
  6325. if( script_hasdata(st,2) )
  6326. flag = script_getnum(st,2);
  6327. pc_setriding(sd, flag);
  6328. return 0;
  6329. }
  6330. /// Sets the save point of the player.
  6331. ///
  6332. /// save "<map name>",<x>,<y>
  6333. /// savepoint "<map name>",<x>,<y>
  6334. BUILDIN_FUNC(savepoint)
  6335. {
  6336. int x;
  6337. int y;
  6338. short map;
  6339. const char* str;
  6340. TBL_PC* sd;
  6341. sd = script_rid2sd(st);
  6342. if( sd == NULL )
  6343. return 0;// no player attached, report source
  6344. str = script_getstr(st, 2);
  6345. x = script_getnum(st,3);
  6346. y = script_getnum(st,4);
  6347. map = mapindex_name2id(str);
  6348. if( map )
  6349. pc_setsavepoint(sd, map, x, y);
  6350. return 0;
  6351. }
  6352. /*==========================================
  6353. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6354. *------------------------------------------*/
  6355. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6356. {
  6357. int type;
  6358. time_t timer;
  6359. struct tm *t;
  6360. type=script_getnum(st,2);
  6361. switch(type){
  6362. case 2:
  6363. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6364. // from the system clock.)
  6365. script_pushint(st,(int)time(NULL));
  6366. break;
  6367. case 1:
  6368. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6369. time(&timer);
  6370. t=localtime(&timer);
  6371. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6372. break;
  6373. case 0:
  6374. default:
  6375. //type 0:(System Ticks)
  6376. script_pushint(st,gettick());
  6377. break;
  6378. }
  6379. return 0;
  6380. }
  6381. /*==========================================
  6382. * GetTime(Type);
  6383. * 1: Sec 2: Min 3: Hour
  6384. * 4: WeekDay 5: MonthDay 6: Month
  6385. * 7: Year
  6386. *------------------------------------------*/
  6387. BUILDIN_FUNC(gettime) /* Asgard Version */
  6388. {
  6389. int type;
  6390. time_t timer;
  6391. struct tm *t;
  6392. type=script_getnum(st,2);
  6393. time(&timer);
  6394. t=localtime(&timer);
  6395. switch(type){
  6396. case 1://Sec(0~59)
  6397. script_pushint(st,t->tm_sec);
  6398. break;
  6399. case 2://Min(0~59)
  6400. script_pushint(st,t->tm_min);
  6401. break;
  6402. case 3://Hour(0~23)
  6403. script_pushint(st,t->tm_hour);
  6404. break;
  6405. case 4://WeekDay(0~6)
  6406. script_pushint(st,t->tm_wday);
  6407. break;
  6408. case 5://MonthDay(01~31)
  6409. script_pushint(st,t->tm_mday);
  6410. break;
  6411. case 6://Month(01~12)
  6412. script_pushint(st,t->tm_mon+1);
  6413. break;
  6414. case 7://Year(20xx)
  6415. script_pushint(st,t->tm_year+1900);
  6416. break;
  6417. case 8://Year Day(01~366)
  6418. script_pushint(st,t->tm_yday+1);
  6419. break;
  6420. default://(format error)
  6421. script_pushint(st,-1);
  6422. break;
  6423. }
  6424. return 0;
  6425. }
  6426. /*==========================================
  6427. * GetTimeStr("TimeFMT", Length);
  6428. *------------------------------------------*/
  6429. BUILDIN_FUNC(gettimestr)
  6430. {
  6431. char *tmpstr;
  6432. const char *fmtstr;
  6433. int maxlen;
  6434. time_t now = time(NULL);
  6435. fmtstr=script_getstr(st,2);
  6436. maxlen=script_getnum(st,3);
  6437. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6438. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6439. tmpstr[maxlen]='\0';
  6440. script_pushstr(st,tmpstr);
  6441. return 0;
  6442. }
  6443. /*==========================================
  6444. * カプラ倉庫を開く
  6445. *------------------------------------------*/
  6446. BUILDIN_FUNC(openstorage)
  6447. {
  6448. TBL_PC* sd;
  6449. sd = script_rid2sd(st);
  6450. if( sd == NULL )
  6451. return 0;
  6452. storage_storageopen(sd);
  6453. return 0;
  6454. }
  6455. BUILDIN_FUNC(guildopenstorage)
  6456. {
  6457. TBL_PC* sd;
  6458. int ret;
  6459. sd = script_rid2sd(st);
  6460. if( sd == NULL )
  6461. return 0;
  6462. ret = storage_guild_storageopen(sd);
  6463. script_pushint(st,ret);
  6464. return 0;
  6465. }
  6466. /*==========================================
  6467. * アイテムによるスキル発動
  6468. *------------------------------------------*/
  6469. BUILDIN_FUNC(itemskill)
  6470. {
  6471. int id,lv;
  6472. TBL_PC *sd=script_rid2sd(st);
  6473. id=script_getnum(st,2);
  6474. lv=script_getnum(st,3);
  6475. if(!sd || sd->ud.skilltimer != -1)
  6476. return 0;
  6477. sd->skillitem=id;
  6478. sd->skillitemlv=lv;
  6479. clif_item_skill(sd,id,lv);
  6480. return 0;
  6481. }
  6482. /*==========================================
  6483. * アイテム作成
  6484. *------------------------------------------*/
  6485. BUILDIN_FUNC(produce)
  6486. {
  6487. int trigger;
  6488. TBL_PC* sd;
  6489. sd = script_rid2sd(st);
  6490. if( sd == NULL )
  6491. return 0;
  6492. trigger=script_getnum(st,2);
  6493. clif_skill_produce_mix_list(sd, trigger);
  6494. return 0;
  6495. }
  6496. /*==========================================
  6497. * NPCでペット作る
  6498. *------------------------------------------*/
  6499. BUILDIN_FUNC(makepet)
  6500. {
  6501. TBL_PC* sd;
  6502. int id,pet_id;
  6503. id=script_getnum(st,2);
  6504. sd = script_rid2sd(st);
  6505. if( sd == NULL )
  6506. return 0;
  6507. pet_id = search_petDB_index(id, PET_CLASS);
  6508. if (pet_id < 0)
  6509. pet_id = search_petDB_index(id, PET_EGG);
  6510. if (pet_id >= 0 && sd) {
  6511. sd->catch_target_class = pet_db[pet_id].class_;
  6512. intif_create_pet(
  6513. sd->status.account_id, sd->status.char_id,
  6514. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6515. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6516. 100, 0, 1, pet_db[pet_id].jname);
  6517. }
  6518. return 0;
  6519. }
  6520. /*==========================================
  6521. * NPCで経験値上げる
  6522. *------------------------------------------*/
  6523. BUILDIN_FUNC(getexp)
  6524. {
  6525. TBL_PC* sd;
  6526. int base=0,job=0;
  6527. double bonus;
  6528. sd = script_rid2sd(st);
  6529. if( sd == NULL )
  6530. return 0;
  6531. base=script_getnum(st,2);
  6532. job =script_getnum(st,3);
  6533. if(base<0 || job<0)
  6534. return 0;
  6535. // bonus for npc-given exp
  6536. bonus = battle_config.quest_exp_rate / 100.;
  6537. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6538. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6539. pc_gainexp(sd, NULL, base, job);
  6540. return 0;
  6541. }
  6542. /*==========================================
  6543. * Gain guild exp [Celest]
  6544. *------------------------------------------*/
  6545. BUILDIN_FUNC(guildgetexp)
  6546. {
  6547. TBL_PC* sd;
  6548. int exp;
  6549. sd = script_rid2sd(st);
  6550. if( sd == NULL )
  6551. return 0;
  6552. exp = script_getnum(st,2);
  6553. if(exp < 0)
  6554. return 0;
  6555. if(sd && sd->status.guild_id > 0)
  6556. guild_getexp (sd, exp);
  6557. return 0;
  6558. }
  6559. /*==========================================
  6560. * Changes the guild master of a guild [Skotlex]
  6561. *------------------------------------------*/
  6562. BUILDIN_FUNC(guildchangegm)
  6563. {
  6564. TBL_PC *sd;
  6565. int guild_id;
  6566. const char *name;
  6567. guild_id = script_getnum(st,2);
  6568. name = script_getstr(st,3);
  6569. sd=map_nick2sd(name);
  6570. if (!sd)
  6571. script_pushint(st,0);
  6572. else
  6573. script_pushint(st,guild_gm_change(guild_id, sd));
  6574. return 0;
  6575. }
  6576. /*==========================================
  6577. * モンスター発生
  6578. *------------------------------------------*/
  6579. BUILDIN_FUNC(monster)
  6580. {
  6581. const char* map = script_getstr(st,2);
  6582. int x = script_getnum(st,3);
  6583. int y = script_getnum(st,4);
  6584. const char* str = script_getstr(st,5);
  6585. int class_ = script_getnum(st,6);
  6586. int amount = script_getnum(st,7);
  6587. const char* event = "";
  6588. struct map_session_data* sd;
  6589. int m;
  6590. if( script_hasdata(st,8) )
  6591. {
  6592. event = script_getstr(st,8);
  6593. check_event(st, event);
  6594. }
  6595. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6596. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6597. return 1;
  6598. }
  6599. sd = map_id2sd(st->rid);
  6600. if( sd && strcmp(map,"this") == 0 )
  6601. m = sd->bl.m;
  6602. else
  6603. m = map_mapname2mapid(map);
  6604. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6605. return 0;
  6606. }
  6607. /*==========================================
  6608. * モンスター発生
  6609. *------------------------------------------*/
  6610. BUILDIN_FUNC(areamonster)
  6611. {
  6612. const char* map = script_getstr(st,2);
  6613. int x0 = script_getnum(st,3);
  6614. int y0 = script_getnum(st,4);
  6615. int x1 = script_getnum(st,5);
  6616. int y1 = script_getnum(st,6);
  6617. const char* str = script_getstr(st,7);
  6618. int class_ = script_getnum(st,8);
  6619. int amount = script_getnum(st,9);
  6620. const char* event = "";
  6621. struct map_session_data* sd;
  6622. int m;
  6623. if( script_hasdata(st,10) )
  6624. {
  6625. event = script_getstr(st,10);
  6626. check_event(st, event);
  6627. }
  6628. sd = map_id2sd(st->rid);
  6629. if( sd && strcmp(map,"this") == 0 )
  6630. m = sd->bl.m;
  6631. else
  6632. m = map_mapname2mapid(map);
  6633. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6634. return 0;
  6635. }
  6636. /*==========================================
  6637. * モンスター削除
  6638. *------------------------------------------*/
  6639. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6640. {
  6641. TBL_MOB* md = (TBL_MOB*)bl;
  6642. char *event=va_arg(ap,char *);
  6643. int allflag=va_arg(ap,int);
  6644. if(!allflag){
  6645. if(strcmp(event,md->npc_event)==0)
  6646. status_kill(bl);
  6647. }else{
  6648. if(!md->spawn)
  6649. status_kill(bl);
  6650. }
  6651. return 0;
  6652. }
  6653. BUILDIN_FUNC(killmonster)
  6654. {
  6655. const char *mapname,*event;
  6656. int m,allflag=0;
  6657. mapname=script_getstr(st,2);
  6658. event=script_getstr(st,3);
  6659. if(strcmp(event,"All")==0)
  6660. allflag = 1;
  6661. else
  6662. check_event(st, event);
  6663. if( (m=map_mapname2mapid(mapname))<0 )
  6664. return 0;
  6665. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6666. return 0;
  6667. }
  6668. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6669. {
  6670. status_kill(bl);
  6671. return 0;
  6672. }
  6673. BUILDIN_FUNC(killmonsterall)
  6674. {
  6675. const char *mapname;
  6676. int m;
  6677. mapname=script_getstr(st,2);
  6678. if( (m=map_mapname2mapid(mapname))<0 )
  6679. return 0;
  6680. map_foreachinmap(buildin_killmonsterall_sub,
  6681. m,BL_MOB);
  6682. return 0;
  6683. }
  6684. /*==========================================
  6685. * Creates a clone of a player.
  6686. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6687. *------------------------------------------*/
  6688. BUILDIN_FUNC(clone)
  6689. {
  6690. TBL_PC *sd, *msd=NULL;
  6691. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6692. unsigned int duration = 0;
  6693. const char *map,*event="";
  6694. map=script_getstr(st,2);
  6695. x=script_getnum(st,3);
  6696. y=script_getnum(st,4);
  6697. event=script_getstr(st,5);
  6698. char_id=script_getnum(st,6);
  6699. if( script_hasdata(st,7) )
  6700. master_id=script_getnum(st,7);
  6701. if( script_hasdata(st,8) )
  6702. mode=script_getnum(st,8);
  6703. if( script_hasdata(st,9) )
  6704. flag=script_getnum(st,9);
  6705. if( script_hasdata(st,10) )
  6706. duration=script_getnum(st,10);
  6707. check_event(st, event);
  6708. m = map_mapname2mapid(map);
  6709. if (m < 0) return 0;
  6710. sd = map_charid2sd(char_id);
  6711. if (master_id) {
  6712. msd = map_charid2sd(master_id);
  6713. if (msd)
  6714. master_id = msd->bl.id;
  6715. else
  6716. master_id = 0;
  6717. }
  6718. if (sd) //Return ID of newly crafted clone.
  6719. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6720. else //Failed to create clone.
  6721. script_pushint(st,0);
  6722. return 0;
  6723. }
  6724. /*==========================================
  6725. * イベント実行
  6726. *------------------------------------------*/
  6727. BUILDIN_FUNC(doevent)
  6728. {
  6729. const char* event = script_getstr(st,2);
  6730. check_event(st, event);
  6731. npc_event(map_id2sd(st->rid),event,0);
  6732. return 0;
  6733. }
  6734. /*==========================================
  6735. * NPC主体イベント実行
  6736. *------------------------------------------*/
  6737. BUILDIN_FUNC(donpcevent)
  6738. {
  6739. const char* event = script_getstr(st,2);
  6740. check_event(st, event);
  6741. npc_event_do(event);
  6742. return 0;
  6743. }
  6744. /// for Aegis compatibility
  6745. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6746. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6747. {
  6748. const char* npc = script_getstr(st,2);
  6749. const char* command = script_getstr(st,3);
  6750. char event[51];
  6751. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6752. check_event(st, event);
  6753. npc_event_do(event);
  6754. return 0;
  6755. }
  6756. /*==========================================
  6757. * イベントタイマー追加
  6758. *------------------------------------------*/
  6759. BUILDIN_FUNC(addtimer)
  6760. {
  6761. int tick = script_getnum(st,2);
  6762. const char* event = script_getstr(st, 3);
  6763. TBL_PC* sd;
  6764. check_event(st, event);
  6765. sd = script_rid2sd(st);
  6766. if( sd == NULL )
  6767. return 0;
  6768. pc_addeventtimer(sd,tick,event);
  6769. return 0;
  6770. }
  6771. /*==========================================
  6772. * イベントタイマー削除
  6773. *------------------------------------------*/
  6774. BUILDIN_FUNC(deltimer)
  6775. {
  6776. const char *event;
  6777. TBL_PC* sd;
  6778. event=script_getstr(st, 2);
  6779. sd = script_rid2sd(st);
  6780. if( sd == NULL )
  6781. return 0;
  6782. check_event(st, event);
  6783. pc_deleventtimer(sd,event);
  6784. return 0;
  6785. }
  6786. /*==========================================
  6787. * イベントタイマーのカウント値追加
  6788. *------------------------------------------*/
  6789. BUILDIN_FUNC(addtimercount)
  6790. {
  6791. const char *event;
  6792. int tick;
  6793. TBL_PC* sd;
  6794. event=script_getstr(st, 2);
  6795. tick=script_getnum(st,3);
  6796. sd = script_rid2sd(st);
  6797. if( sd == NULL )
  6798. return 0;
  6799. check_event(st, event);
  6800. pc_addeventtimercount(sd,event,tick);
  6801. return 0;
  6802. }
  6803. /*==========================================
  6804. * NPCタイマー初期化
  6805. *------------------------------------------*/
  6806. BUILDIN_FUNC(initnpctimer)
  6807. {
  6808. struct npc_data *nd;
  6809. int flag = 0;
  6810. if( script_hasdata(st,3) )
  6811. { //Two arguments: NPC name and attach flag.
  6812. nd = npc_name2id(script_getstr(st, 2));
  6813. flag = script_getnum(st,3);
  6814. }
  6815. else if( script_hasdata(st,2) )
  6816. { //Check if argument is numeric (flag) or string (npc name)
  6817. struct script_data *data;
  6818. data = script_getdata(st,2);
  6819. get_val(st,data);
  6820. if( data_isstring(data) ) //NPC name
  6821. nd = npc_name2id(conv_str(st, data));
  6822. else if( data_isint(data) ) //Flag
  6823. {
  6824. nd = (struct npc_data *)map_id2bl(st->oid);
  6825. flag = conv_num(st,data);
  6826. }
  6827. else
  6828. {
  6829. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6830. return 1;
  6831. }
  6832. }
  6833. else
  6834. nd=(struct npc_data *)map_id2bl(st->oid);
  6835. if (!nd)
  6836. return 0;
  6837. if (flag) //Attach
  6838. {
  6839. TBL_PC* sd = script_rid2sd(st);
  6840. if( sd == NULL )
  6841. return 0;
  6842. nd->u.scr.rid = sd->bl.id;
  6843. }
  6844. npc_settimerevent_tick(nd,0);
  6845. npc_timerevent_start(nd, st->rid);
  6846. return 0;
  6847. }
  6848. /*==========================================
  6849. * NPCタイマー開始
  6850. *------------------------------------------*/
  6851. BUILDIN_FUNC(startnpctimer)
  6852. {
  6853. struct npc_data *nd;
  6854. int flag = 0;
  6855. if( script_hasdata(st,3) )
  6856. { //Two arguments: NPC name and attach flag.
  6857. nd = npc_name2id(script_getstr(st, 2));
  6858. flag = script_getnum(st,3);
  6859. }
  6860. else if( script_hasdata(st,2) )
  6861. { //Check if argument is numeric (flag) or string (npc name)
  6862. struct script_data *data;
  6863. data = script_getdata(st,2);
  6864. get_val(st,data);
  6865. if( data_isstring(data) ) //NPC name
  6866. nd = npc_name2id(conv_str(st, data));
  6867. else if( data_isint(data) ) //Flag
  6868. {
  6869. nd = (struct npc_data *)map_id2bl(st->oid);
  6870. flag = conv_num(st,data);
  6871. }
  6872. else
  6873. {
  6874. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6875. return 1;
  6876. }
  6877. }
  6878. else
  6879. nd=(struct npc_data *)map_id2bl(st->oid);
  6880. if (!nd)
  6881. return 0;
  6882. if (flag) //Attach
  6883. {
  6884. TBL_PC* sd = script_rid2sd(st);
  6885. if( sd == NULL )
  6886. return 0;
  6887. nd->u.scr.rid = sd->bl.id;
  6888. }
  6889. npc_timerevent_start(nd, st->rid);
  6890. return 0;
  6891. }
  6892. /*==========================================
  6893. * NPCタイマー停止
  6894. *------------------------------------------*/
  6895. BUILDIN_FUNC(stopnpctimer)
  6896. {
  6897. struct npc_data *nd;
  6898. int flag = 0;
  6899. if( script_hasdata(st,3) )
  6900. { //Two arguments: NPC name and attach flag.
  6901. nd = npc_name2id(script_getstr(st, 2));
  6902. flag = script_getnum(st,3);
  6903. }
  6904. else if( script_hasdata(st,2) )
  6905. { //Check if argument is numeric (flag) or string (npc name)
  6906. struct script_data *data;
  6907. data = script_getdata(st,2);
  6908. get_val(st,data);
  6909. if( data_isstring(data) ) //NPC name
  6910. nd = npc_name2id(conv_str(st, data));
  6911. else if( data_isint(data) ) //Flag
  6912. {
  6913. nd = (struct npc_data *)map_id2bl(st->oid);
  6914. flag = conv_num(st,data);
  6915. }
  6916. else
  6917. {
  6918. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6919. return 1;
  6920. }
  6921. }
  6922. else
  6923. nd=(struct npc_data *)map_id2bl(st->oid);
  6924. if (!nd)
  6925. return 0;
  6926. if (flag) //Detach
  6927. nd->u.scr.rid = 0;
  6928. npc_timerevent_stop(nd);
  6929. return 0;
  6930. }
  6931. /*==========================================
  6932. * NPCタイマー情報所得
  6933. *------------------------------------------*/
  6934. BUILDIN_FUNC(getnpctimer)
  6935. {
  6936. struct npc_data *nd;
  6937. TBL_PC *sd;
  6938. int type=script_getnum(st,2);
  6939. int val=0;
  6940. if( script_hasdata(st,3) )
  6941. nd = npc_name2id(script_getstr(st,3));
  6942. else
  6943. nd = (struct npc_data *)map_id2bl(st->oid);
  6944. if (!nd || nd->bl.type != BL_NPC)
  6945. {
  6946. script_pushint(st,0);
  6947. ShowError("getnpctimer: Invalid NPC.\n");
  6948. return 1;
  6949. }
  6950. switch(type){
  6951. case 0: val=npc_gettimerevent_tick(nd); break;
  6952. case 1:
  6953. if (nd->u.scr.rid) {
  6954. sd = map_id2sd(nd->u.scr.rid);
  6955. if (!sd) {
  6956. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6957. break;
  6958. }
  6959. val = (sd->npc_timer_id != -1);
  6960. } else
  6961. val= (nd->u.scr.timerid !=-1);
  6962. break;
  6963. case 2: val= nd->u.scr.timeramount; break;
  6964. }
  6965. script_pushint(st,val);
  6966. return 0;
  6967. }
  6968. /*==========================================
  6969. * NPCタイマー値設定
  6970. *------------------------------------------*/
  6971. BUILDIN_FUNC(setnpctimer)
  6972. {
  6973. int tick;
  6974. struct npc_data *nd;
  6975. tick=script_getnum(st,2);
  6976. if( script_hasdata(st,3) )
  6977. nd=npc_name2id(script_getstr(st,3));
  6978. else
  6979. nd=(struct npc_data *)map_id2bl(st->oid);
  6980. if (!nd || nd->bl.type != BL_NPC)
  6981. {
  6982. script_pushint(st,1);
  6983. ShowError("setnpctimer: Invalid NPC.\n");
  6984. return 1;
  6985. }
  6986. npc_settimerevent_tick(nd,tick);
  6987. script_pushint(st,0);
  6988. return 0;
  6989. }
  6990. /*==========================================
  6991. * attaches the player rid to the timer [Celest]
  6992. *------------------------------------------*/
  6993. BUILDIN_FUNC(attachnpctimer)
  6994. {
  6995. TBL_PC *sd;
  6996. struct npc_data *nd;
  6997. nd=(struct npc_data *)map_id2bl(st->oid);
  6998. if (!nd || nd->bl.type != BL_NPC)
  6999. {
  7000. script_pushint(st,1);
  7001. ShowError("setnpctimer: Invalid NPC.\n");
  7002. return 1;
  7003. }
  7004. if( script_hasdata(st,2) )
  7005. sd=map_nick2sd(script_getstr(st,2));
  7006. else
  7007. sd = script_rid2sd(st);
  7008. if (!sd)
  7009. {
  7010. script_pushint(st,1);
  7011. ShowWarning("attachnpctimer: Invalid player.\n");
  7012. return 1;
  7013. }
  7014. nd->u.scr.rid = sd->bl.id;
  7015. script_pushint(st,0);
  7016. return 0;
  7017. }
  7018. /*==========================================
  7019. * detaches a player rid from the timer [Celest]
  7020. *------------------------------------------*/
  7021. BUILDIN_FUNC(detachnpctimer)
  7022. {
  7023. struct npc_data *nd;
  7024. if( script_hasdata(st,2) )
  7025. nd=npc_name2id(script_getstr(st,2));
  7026. else
  7027. nd=(struct npc_data *)map_id2bl(st->oid);
  7028. if (!nd || nd->bl.type != BL_NPC)
  7029. {
  7030. script_pushint(st,1);
  7031. ShowError("detachnpctimer: Invalid NPC.\n");
  7032. return 1;
  7033. }
  7034. nd->u.scr.rid = 0;
  7035. script_pushint(st,0);
  7036. return 0;
  7037. }
  7038. /*==========================================
  7039. * To avoid "player not attached" script errors, this function is provided,
  7040. * it checks if there is a player attached to the current script. [Skotlex]
  7041. * If no, returns 0, if yes, returns the account_id of the attached player.
  7042. *------------------------------------------*/
  7043. BUILDIN_FUNC(playerattached)
  7044. {
  7045. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7046. script_pushint(st,0);
  7047. else
  7048. script_pushint(st,st->rid);
  7049. return 0;
  7050. }
  7051. /*==========================================
  7052. * 天の声アナウンス
  7053. *------------------------------------------*/
  7054. BUILDIN_FUNC(announce)
  7055. {
  7056. const char *str, *color=NULL;
  7057. int flag;
  7058. str=script_getstr(st,2);
  7059. flag=script_getnum(st,3);
  7060. if (script_hasdata(st,4))
  7061. color=script_getstr(st,4);
  7062. if(flag&0x0f){
  7063. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  7064. (struct block_list *)script_rid2sd(st);
  7065. if( bl == NULL )
  7066. return 0;
  7067. if (color)
  7068. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  7069. else
  7070. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  7071. }else {
  7072. if (color)
  7073. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  7074. else
  7075. intif_GMmessage(str,(int)strlen(str)+1,flag);
  7076. }
  7077. return 0;
  7078. }
  7079. /*==========================================
  7080. * 天の声アナウンス(特定マップ)
  7081. *------------------------------------------*/
  7082. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  7083. {
  7084. char *str, *color;
  7085. int len,flag;
  7086. str=va_arg(ap,char *);
  7087. len=va_arg(ap,int);
  7088. flag=va_arg(ap,int);
  7089. color=va_arg(ap,char *);
  7090. if (color)
  7091. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  7092. else
  7093. clif_GMmessage(bl,str,len,flag|3);
  7094. return 0;
  7095. }
  7096. BUILDIN_FUNC(mapannounce)
  7097. {
  7098. const char *mapname,*str, *color=NULL;
  7099. int flag,m;
  7100. mapname=script_getstr(st,2);
  7101. str=script_getstr(st,3);
  7102. flag=script_getnum(st,4);
  7103. if (script_hasdata(st,5))
  7104. color=script_getstr(st,5);
  7105. if( (m=map_mapname2mapid(mapname))<0 )
  7106. return 0;
  7107. map_foreachinmap(buildin_mapannounce_sub,
  7108. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7109. return 0;
  7110. }
  7111. /*==========================================
  7112. * 天の声アナウンス(特定エリア)
  7113. *------------------------------------------*/
  7114. BUILDIN_FUNC(areaannounce)
  7115. {
  7116. const char *map,*str,*color=NULL;
  7117. int flag,m;
  7118. int x0,y0,x1,y1;
  7119. map=script_getstr(st,2);
  7120. x0=script_getnum(st,3);
  7121. y0=script_getnum(st,4);
  7122. x1=script_getnum(st,5);
  7123. y1=script_getnum(st,6);
  7124. str=script_getstr(st,7);
  7125. flag=script_getnum(st,8);
  7126. if (script_hasdata(st,9))
  7127. color=script_getstr(st,9);
  7128. if( (m=map_mapname2mapid(map))<0 )
  7129. return 0;
  7130. map_foreachinarea(buildin_mapannounce_sub,
  7131. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7132. return 0;
  7133. }
  7134. /*==========================================
  7135. * ユーザー数所得
  7136. *------------------------------------------*/
  7137. BUILDIN_FUNC(getusers)
  7138. {
  7139. int flag=script_getnum(st,2);
  7140. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7141. int val=0;
  7142. switch(flag&0x07){
  7143. case 0: val=map[bl->m].users; break;
  7144. case 1: val=map_getusers(); break;
  7145. }
  7146. script_pushint(st,val);
  7147. return 0;
  7148. }
  7149. /*==========================================
  7150. * Works like @WHO - displays all online users names in window
  7151. *------------------------------------------*/
  7152. BUILDIN_FUNC(getusersname)
  7153. {
  7154. TBL_PC *sd, *pl_sd;
  7155. int disp_num=1;
  7156. struct s_mapiterator* iter;
  7157. sd = script_rid2sd(st);
  7158. if (!sd) return 0;
  7159. iter = mapit_getallusers();
  7160. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7161. {
  7162. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7163. continue; // skip hidden GMs
  7164. if((disp_num++)%10==0)
  7165. clif_scriptnext(sd,st->oid);
  7166. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7167. }
  7168. mapit_free(iter);
  7169. return 0;
  7170. }
  7171. /*==========================================
  7172. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7173. *------------------------------------------*/
  7174. BUILDIN_FUNC(getmapguildusers)
  7175. {
  7176. const char *str;
  7177. int m, gid;
  7178. int i=0,c=0;
  7179. struct guild *g = NULL;
  7180. str=script_getstr(st,2);
  7181. gid=script_getnum(st,3);
  7182. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7183. script_pushint(st,-1);
  7184. return 0;
  7185. }
  7186. g = guild_search(gid);
  7187. if (g){
  7188. for(i = 0; i < g->max_member; i++)
  7189. {
  7190. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7191. c++;
  7192. }
  7193. }
  7194. script_pushint(st,c);
  7195. return 0;
  7196. }
  7197. /*==========================================
  7198. * マップ指定ユーザー数所得
  7199. *------------------------------------------*/
  7200. BUILDIN_FUNC(getmapusers)
  7201. {
  7202. const char *str;
  7203. int m;
  7204. str=script_getstr(st,2);
  7205. if( (m=map_mapname2mapid(str))< 0){
  7206. script_pushint(st,-1);
  7207. return 0;
  7208. }
  7209. script_pushint(st,map[m].users);
  7210. return 0;
  7211. }
  7212. /*==========================================
  7213. * エリア指定ユーザー数所得
  7214. *------------------------------------------*/
  7215. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7216. {
  7217. int *users=va_arg(ap,int *);
  7218. (*users)++;
  7219. return 0;
  7220. }
  7221. BUILDIN_FUNC(getareausers)
  7222. {
  7223. const char *str;
  7224. int m,x0,y0,x1,y1,users=0;
  7225. str=script_getstr(st,2);
  7226. x0=script_getnum(st,3);
  7227. y0=script_getnum(st,4);
  7228. x1=script_getnum(st,5);
  7229. y1=script_getnum(st,6);
  7230. if( (m=map_mapname2mapid(str))< 0){
  7231. script_pushint(st,-1);
  7232. return 0;
  7233. }
  7234. map_foreachinarea(buildin_getareausers_sub,
  7235. m,x0,y0,x1,y1,BL_PC,&users);
  7236. script_pushint(st,users);
  7237. return 0;
  7238. }
  7239. /*==========================================
  7240. * エリア指定ドロップアイテム数所得
  7241. *------------------------------------------*/
  7242. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7243. {
  7244. int item=va_arg(ap,int);
  7245. int *amount=va_arg(ap,int *);
  7246. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7247. if(drop->item_data.nameid==item)
  7248. (*amount)+=drop->item_data.amount;
  7249. return 0;
  7250. }
  7251. BUILDIN_FUNC(getareadropitem)
  7252. {
  7253. const char *str;
  7254. int m,x0,y0,x1,y1,item,amount=0;
  7255. struct script_data *data;
  7256. str=script_getstr(st,2);
  7257. x0=script_getnum(st,3);
  7258. y0=script_getnum(st,4);
  7259. x1=script_getnum(st,5);
  7260. y1=script_getnum(st,6);
  7261. data=script_getdata(st,7);
  7262. get_val(st,data);
  7263. if( data_isstring(data) ){
  7264. const char *name=conv_str(st,data);
  7265. struct item_data *item_data = itemdb_searchname(name);
  7266. item=UNKNOWN_ITEM_ID;
  7267. if( item_data )
  7268. item=item_data->nameid;
  7269. }else
  7270. item=conv_num(st,data);
  7271. if( (m=map_mapname2mapid(str))< 0){
  7272. script_pushint(st,-1);
  7273. return 0;
  7274. }
  7275. map_foreachinarea(buildin_getareadropitem_sub,
  7276. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7277. script_pushint(st,amount);
  7278. return 0;
  7279. }
  7280. /*==========================================
  7281. * NPCの有効化
  7282. *------------------------------------------*/
  7283. BUILDIN_FUNC(enablenpc)
  7284. {
  7285. const char *str;
  7286. str=script_getstr(st,2);
  7287. npc_enable(str,1);
  7288. return 0;
  7289. }
  7290. /*==========================================
  7291. * NPCの無効化
  7292. *------------------------------------------*/
  7293. BUILDIN_FUNC(disablenpc)
  7294. {
  7295. const char *str;
  7296. str=script_getstr(st,2);
  7297. npc_enable(str,0);
  7298. return 0;
  7299. }
  7300. /*==========================================
  7301. * 隠れているNPCの表示
  7302. *------------------------------------------*/
  7303. BUILDIN_FUNC(hideoffnpc)
  7304. {
  7305. const char *str;
  7306. str=script_getstr(st,2);
  7307. npc_enable(str,2);
  7308. return 0;
  7309. }
  7310. /*==========================================
  7311. * NPCをハイディング
  7312. *------------------------------------------*/
  7313. BUILDIN_FUNC(hideonnpc)
  7314. {
  7315. const char *str;
  7316. str=script_getstr(st,2);
  7317. npc_enable(str,4);
  7318. return 0;
  7319. }
  7320. /// Starts a status effect on the target unit or on the attached player.
  7321. ///
  7322. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7323. BUILDIN_FUNC(sc_start)
  7324. {
  7325. struct block_list* bl;
  7326. enum sc_type type;
  7327. int tick;
  7328. int val1;
  7329. int val4 = 0;
  7330. type = (sc_type)script_getnum(st,2);
  7331. tick = script_getnum(st,3);
  7332. val1 = script_getnum(st,4);
  7333. if( script_hasdata(st,5) )
  7334. bl = map_id2bl(script_getnum(st,5));
  7335. else
  7336. bl = map_id2bl(st->rid);
  7337. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7338. {// When there isn't a duration specified, try to get it from the skill_db
  7339. tick = skill_get_time(status_sc2skill(type), val1);
  7340. }
  7341. if( potion_flag == 1 && potion_target )
  7342. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7343. bl = map_id2bl(potion_target);
  7344. tick /= 2;// Thrown potions only last half.
  7345. val4 = 1;// Mark that this was a thrown sc_effect
  7346. }
  7347. if( bl )
  7348. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7349. return 0;
  7350. }
  7351. /// Starts a status effect on the target unit or on the attached player.
  7352. ///
  7353. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7354. BUILDIN_FUNC(sc_start2)
  7355. {
  7356. struct block_list* bl;
  7357. enum sc_type type;
  7358. int tick;
  7359. int val1;
  7360. int val4 = 0;
  7361. int rate;
  7362. type = (sc_type)script_getnum(st,2);
  7363. tick = script_getnum(st,3);
  7364. val1 = script_getnum(st,4);
  7365. rate = script_getnum(st,5);
  7366. if( script_hasdata(st,6) )
  7367. bl = map_id2bl(script_getnum(st,6));
  7368. else
  7369. bl = map_id2bl(st->rid);
  7370. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7371. {// When there isn't a duration specified, try to get it from the skill_db
  7372. tick = skill_get_time(status_sc2skill(type), val1);
  7373. }
  7374. if( potion_flag == 1 && potion_target )
  7375. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7376. bl = map_id2bl(potion_target);
  7377. tick /= 2;// Thrown potions only last half.
  7378. val4 = 1;// Mark that this was a thrown sc_effect
  7379. }
  7380. if( bl )
  7381. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7382. return 0;
  7383. }
  7384. /// Starts a status effect on the target unit or on the attached player.
  7385. ///
  7386. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7387. BUILDIN_FUNC(sc_start4)
  7388. {
  7389. struct block_list* bl;
  7390. enum sc_type type;
  7391. int tick;
  7392. int val1;
  7393. int val2;
  7394. int val3;
  7395. int val4;
  7396. type = (sc_type)script_getnum(st,2);
  7397. tick = script_getnum(st,3);
  7398. val1 = script_getnum(st,4);
  7399. val2 = script_getnum(st,5);
  7400. val3 = script_getnum(st,6);
  7401. val4 = script_getnum(st,7);
  7402. if( script_hasdata(st,8) )
  7403. bl = map_id2bl(script_getnum(st,8));
  7404. else
  7405. bl = map_id2bl(st->rid);
  7406. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7407. {// When there isn't a duration specified, try to get it from the skill_db
  7408. tick = skill_get_time(status_sc2skill(type), val1);
  7409. }
  7410. if( potion_flag == 1 && potion_target )
  7411. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7412. bl = map_id2bl(potion_target);
  7413. tick /= 2;// Thrown potions only last half.
  7414. }
  7415. if( bl )
  7416. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7417. return 0;
  7418. }
  7419. /// Ends one or all status effects on the target unit or on the attached player.
  7420. ///
  7421. /// sc_end <effect_id>{,<unit_id>};
  7422. BUILDIN_FUNC(sc_end)
  7423. {
  7424. struct block_list* bl;
  7425. int type;
  7426. type = script_getnum(st,2);
  7427. if( script_hasdata(st,3) )
  7428. bl = map_id2bl(script_getnum(st,3));
  7429. else
  7430. bl = map_id2bl(st->rid);
  7431. if( potion_flag==1 && potion_target )
  7432. {//##TODO how does this work [FlavioJS]
  7433. bl = map_id2bl(potion_target);
  7434. }
  7435. if( !bl ) return 0;
  7436. if( type >= 0 && type < SC_MAX )
  7437. {
  7438. struct status_change *sc = status_get_sc(bl);
  7439. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7440. if (!sce) return 0;
  7441. //This should help status_change_end force disabling the SC in case it has no limit.
  7442. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7443. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7444. } else
  7445. status_change_clear(bl, 2);// remove all effects
  7446. return 0;
  7447. }
  7448. /*==========================================
  7449. * 状態異常耐性を計算した確率を返す
  7450. *------------------------------------------*/
  7451. BUILDIN_FUNC(getscrate)
  7452. {
  7453. struct block_list *bl;
  7454. int type,rate;
  7455. type=script_getnum(st,2);
  7456. rate=script_getnum(st,3);
  7457. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7458. bl = map_id2bl(script_getnum(st,4));
  7459. else
  7460. bl = map_id2bl(st->rid);
  7461. if (bl)
  7462. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7463. script_pushint(st,rate);
  7464. return 0;
  7465. }
  7466. /*==========================================
  7467. *
  7468. *------------------------------------------*/
  7469. BUILDIN_FUNC(debugmes)
  7470. {
  7471. const char *str;
  7472. str=script_getstr(st,2);
  7473. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7474. return 0;
  7475. }
  7476. /*==========================================
  7477. *捕獲アイテム使用
  7478. *------------------------------------------*/
  7479. BUILDIN_FUNC(catchpet)
  7480. {
  7481. int pet_id;
  7482. TBL_PC *sd;
  7483. pet_id= script_getnum(st,2);
  7484. sd=script_rid2sd(st);
  7485. if( sd == NULL )
  7486. return 0;
  7487. pet_catch_process1(sd,pet_id);
  7488. return 0;
  7489. }
  7490. /*==========================================
  7491. * [orn]
  7492. *------------------------------------------*/
  7493. BUILDIN_FUNC(homunculus_evolution)
  7494. {
  7495. TBL_PC *sd;
  7496. sd=script_rid2sd(st);
  7497. if( sd == NULL )
  7498. return 0;
  7499. if(merc_is_hom_active(sd->hd))
  7500. {
  7501. if (sd->hd->homunculus.intimacy > 91000)
  7502. merc_hom_evolution(sd->hd);
  7503. else
  7504. clif_emotion(&sd->hd->bl, 4) ; //swt
  7505. }
  7506. return 0;
  7507. }
  7508. // [Zephyrus]
  7509. BUILDIN_FUNC(homunculus_shuffle)
  7510. {
  7511. TBL_PC *sd;
  7512. sd=script_rid2sd(st);
  7513. if( sd == NULL )
  7514. return 0;
  7515. if(merc_is_hom_active(sd->hd))
  7516. merc_hom_shuffle(sd->hd);
  7517. return 0;
  7518. }
  7519. //These two functions bring the eA MAPID_* class functionality to scripts.
  7520. BUILDIN_FUNC(eaclass)
  7521. {
  7522. int class_;
  7523. if( script_hasdata(st,2) )
  7524. class_ = script_getnum(st,2);
  7525. else {
  7526. TBL_PC *sd;
  7527. sd=script_rid2sd(st);
  7528. if (!sd) {
  7529. script_pushint(st,-1);
  7530. return 0;
  7531. }
  7532. class_ = sd->status.class_;
  7533. }
  7534. script_pushint(st,pc_jobid2mapid(class_));
  7535. return 0;
  7536. }
  7537. BUILDIN_FUNC(roclass)
  7538. {
  7539. int class_ =script_getnum(st,2);
  7540. int sex;
  7541. if( script_hasdata(st,3) )
  7542. sex = script_getnum(st,3);
  7543. else {
  7544. TBL_PC *sd;
  7545. if (st->rid && (sd=script_rid2sd(st)))
  7546. sex = sd->status.sex;
  7547. else
  7548. sex = 1; //Just use male when not found.
  7549. }
  7550. script_pushint(st,pc_mapid2jobid(class_, sex));
  7551. return 0;
  7552. }
  7553. /*==========================================
  7554. *携帯卵孵化機使用
  7555. *------------------------------------------*/
  7556. BUILDIN_FUNC(birthpet)
  7557. {
  7558. TBL_PC *sd;
  7559. sd=script_rid2sd(st);
  7560. if( sd == NULL )
  7561. return 0;
  7562. clif_sendegg(sd);
  7563. return 0;
  7564. }
  7565. /*==========================================
  7566. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7567. *------------------------------------------*/
  7568. BUILDIN_FUNC(resetlvl)
  7569. {
  7570. TBL_PC *sd;
  7571. int type=script_getnum(st,2);
  7572. sd=script_rid2sd(st);
  7573. if( sd == NULL )
  7574. return 0;
  7575. pc_resetlvl(sd,type);
  7576. return 0;
  7577. }
  7578. /*==========================================
  7579. * ステータスリセット
  7580. *------------------------------------------*/
  7581. BUILDIN_FUNC(resetstatus)
  7582. {
  7583. TBL_PC *sd;
  7584. sd=script_rid2sd(st);
  7585. pc_resetstate(sd);
  7586. return 0;
  7587. }
  7588. /*==========================================
  7589. * script command resetskill
  7590. *------------------------------------------*/
  7591. BUILDIN_FUNC(resetskill)
  7592. {
  7593. TBL_PC *sd;
  7594. sd=script_rid2sd(st);
  7595. pc_resetskill(sd,1);
  7596. return 0;
  7597. }
  7598. /*==========================================
  7599. * Counts total amount of skill points.
  7600. *------------------------------------------*/
  7601. BUILDIN_FUNC(skillpointcount)
  7602. {
  7603. TBL_PC *sd;
  7604. sd=script_rid2sd(st);
  7605. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7606. return 0;
  7607. }
  7608. /*==========================================
  7609. *
  7610. *------------------------------------------*/
  7611. BUILDIN_FUNC(changebase)
  7612. {
  7613. TBL_PC *sd=NULL;
  7614. int vclass;
  7615. if( script_hasdata(st,3) )
  7616. sd=map_id2sd(script_getnum(st,3));
  7617. else
  7618. sd=script_rid2sd(st);
  7619. if(sd == NULL)
  7620. return 0;
  7621. vclass = script_getnum(st,2);
  7622. if(vclass == JOB_WEDDING)
  7623. {
  7624. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7625. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7626. )
  7627. return 0;
  7628. }
  7629. if(!sd->disguise && vclass != sd->vd.class_) {
  7630. status_set_viewdata(&sd->bl, vclass);
  7631. //Updated client view. Base, Weapon and Cloth Colors.
  7632. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7633. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7634. if (sd->vd.cloth_color)
  7635. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7636. }
  7637. return 0;
  7638. }
  7639. /*==========================================
  7640. * 性別変換
  7641. *------------------------------------------*/
  7642. BUILDIN_FUNC(changesex)
  7643. {
  7644. TBL_PC *sd = NULL;
  7645. sd = script_rid2sd(st);
  7646. chrif_changesex(sd);
  7647. return 0;
  7648. }
  7649. /*==========================================
  7650. * Works like 'announce' but outputs in the common chat window
  7651. *------------------------------------------*/
  7652. BUILDIN_FUNC(globalmes)
  7653. {
  7654. struct block_list *bl = map_id2bl(st->oid);
  7655. struct npc_data *nd = (struct npc_data *)bl;
  7656. const char *name=NULL,*mes;
  7657. mes=script_getstr(st,2); // メッセージの取得
  7658. if(mes==NULL) return 0;
  7659. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7660. name=script_getstr(st,3);
  7661. } else {
  7662. name=nd->name;
  7663. }
  7664. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7665. return 0;
  7666. }
  7667. /////////////////////////////////////////////////////////////////////
  7668. // NPC waiting room (chat room)
  7669. //
  7670. /// Creates a waiting room (chat room) for this npc.
  7671. ///
  7672. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7673. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7674. /// waitingroom "<title>",<limit>,"<event>";
  7675. /// waitingroom "<title>",<limit>;
  7676. BUILDIN_FUNC(waitingroom)
  7677. {
  7678. struct npc_data* nd;
  7679. const char* title;
  7680. const char* ev = "";
  7681. int limit;
  7682. int trigger = 0;
  7683. int pub = 1;
  7684. title = script_getstr(st, 2);
  7685. limit = script_getnum(st, 3);
  7686. if( script_hasdata(st,5) )
  7687. {
  7688. struct script_data* last = script_getdata(st, 5);
  7689. get_val(st, last);
  7690. if( data_isstring(last) )
  7691. {// ,<trigger>,"<event>"
  7692. trigger = script_getnum(st, 4);
  7693. ev = script_getstr(st, 5);
  7694. }
  7695. else
  7696. {// ,"<event>",<trigger>
  7697. ev = script_getstr(st, 4);
  7698. trigger = script_getnum(st,5);
  7699. }
  7700. }
  7701. else if( script_hasdata(st,4) )
  7702. {// ,"<event>"
  7703. ev = script_getstr(st, 4);
  7704. trigger = limit;
  7705. }
  7706. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  7707. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7708. return 0;
  7709. }
  7710. /// Removes the waiting room of the current or target npc.
  7711. ///
  7712. /// delwaitingroom "<npc_name>";
  7713. /// delwaitingroom;
  7714. BUILDIN_FUNC(delwaitingroom)
  7715. {
  7716. struct npc_data* nd;
  7717. if( script_hasdata(st,2) )
  7718. nd = npc_name2id(script_getstr(st, 2));
  7719. else
  7720. nd = (struct npc_data *)map_id2bl(st->oid);
  7721. if( nd != NULL )
  7722. chat_deletenpcchat(nd);
  7723. return 0;
  7724. }
  7725. /// Kicks all the players from the waiting room of the current or target npc.
  7726. ///
  7727. /// kickwaitingroomall "<npc_name>";
  7728. /// kickwaitingroomall;
  7729. BUILDIN_FUNC(waitingroomkickall)
  7730. {
  7731. struct npc_data* nd;
  7732. struct chat_data* cd;
  7733. if( script_hasdata(st,2) )
  7734. nd = npc_name2id(script_getstr(st,2));
  7735. else
  7736. nd = (struct npc_data *)map_id2bl(st->oid);
  7737. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7738. chat_npckickall(cd);
  7739. return 0;
  7740. }
  7741. /// Enables the waiting room event of the current or target npc.
  7742. ///
  7743. /// enablewaitingroomevent "<npc_name>";
  7744. /// enablewaitingroomevent;
  7745. BUILDIN_FUNC(enablewaitingroomevent)
  7746. {
  7747. struct npc_data* nd;
  7748. struct chat_data* cd;
  7749. if( script_hasdata(st,2) )
  7750. nd = npc_name2id(script_getstr(st, 2));
  7751. else
  7752. nd = (struct npc_data *)map_id2bl(st->oid);
  7753. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7754. chat_enableevent(cd);
  7755. return 0;
  7756. }
  7757. /// Disables the waiting room event of the current or target npc.
  7758. ///
  7759. /// disablewaitingroomevent "<npc_name>";
  7760. /// disablewaitingroomevent;
  7761. BUILDIN_FUNC(disablewaitingroomevent)
  7762. {
  7763. struct npc_data *nd;
  7764. struct chat_data *cd;
  7765. if( script_hasdata(st,2) )
  7766. nd = npc_name2id(script_getstr(st, 2));
  7767. else
  7768. nd = (struct npc_data *)map_id2bl(st->oid);
  7769. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7770. chat_disableevent(cd);
  7771. return 0;
  7772. }
  7773. /// Returns info on the waiting room of the current or target npc.
  7774. /// Returns -1 if the type unknown
  7775. /// <type>=0 : current number of users
  7776. /// <type>=1 : maximum number of users allowed
  7777. /// <type>=2 : the number of users that trigger the event
  7778. /// <type>=3 : if the trigger is disabled
  7779. /// <type>=4 : the title of the waiting room
  7780. /// <type>=5 : the password of the waiting room
  7781. /// <type>=16 : the name of the waiting room event
  7782. /// <type>=32 : if the waiting room is full
  7783. /// <type>=33 : if there are enough users to trigger the event
  7784. ///
  7785. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7786. /// getwaitingroomstate(<type>) -> <info>
  7787. BUILDIN_FUNC(getwaitingroomstate)
  7788. {
  7789. struct npc_data *nd;
  7790. struct chat_data *cd;
  7791. int type;
  7792. type = script_getnum(st,2);
  7793. if( script_hasdata(st,3) )
  7794. nd = npc_name2id(script_getstr(st, 3));
  7795. else
  7796. nd = (struct npc_data *)map_id2bl(st->oid);
  7797. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7798. {
  7799. script_pushint(st, -1);
  7800. return 0;
  7801. }
  7802. switch(type)
  7803. {
  7804. case 0: script_pushint(st, cd->users); break;
  7805. case 1: script_pushint(st, cd->limit); break;
  7806. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7807. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7808. case 4: script_pushstrcopy(st, cd->title); break;
  7809. case 5: script_pushstrcopy(st, cd->pass); break;
  7810. case 16: script_pushstrcopy(st, cd->npc_event);break;
  7811. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7812. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7813. default: script_pushint(st, -1); break;
  7814. }
  7815. return 0;
  7816. }
  7817. /// Warps the trigger or target amount of players to the target map and position.
  7818. /// Players are automatically removed from the waiting room.
  7819. /// Those waiting the longest will get warped first.
  7820. /// The target map can be "Random" for a random position in the current map,
  7821. /// and "SavePoint" for the savepoint map+position.
  7822. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7823. ///
  7824. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7825. /// The total number of teleported players is put into $@warpwaitingpcnum
  7826. ///
  7827. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7828. /// warpwaitingpc "<map name>",<x>,<y>;
  7829. BUILDIN_FUNC(warpwaitingpc)
  7830. {
  7831. int x;
  7832. int y;
  7833. int i;
  7834. int n;
  7835. const char* map_name;
  7836. struct npc_data* nd;
  7837. struct chat_data* cd;
  7838. TBL_PC* sd;
  7839. nd = (struct npc_data *)map_id2bl(st->oid);
  7840. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7841. return 0;
  7842. map_name = script_getstr(st,2);
  7843. x = script_getnum(st,3);
  7844. y = script_getnum(st,4);
  7845. n = cd->trigger&0x7f;
  7846. if( script_hasdata(st,5) )
  7847. n = script_getnum(st,5);
  7848. for( i = 0; i < n && cd->users > 0; i++ )
  7849. {
  7850. sd = cd->usersd[0];
  7851. if( sd == NULL )
  7852. {
  7853. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7854. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  7855. continue;// Broken npc chat room?
  7856. }
  7857. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  7858. if( strcmp(map_name,"Random") == 0 )
  7859. pc_randomwarp(sd,3);
  7860. else if( strcmp(map_name,"SavePoint") == 0 )
  7861. {
  7862. if( map[sd->bl.m].flag.noteleport )
  7863. return 0;// can't teleport on this map
  7864. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  7865. }
  7866. else
  7867. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  7868. }
  7869. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  7870. return 0;
  7871. }
  7872. /////////////////////////////////////////////////////////////////////
  7873. // ...
  7874. //
  7875. /*==========================================
  7876. * RIDのアタッチ
  7877. *------------------------------------------*/
  7878. BUILDIN_FUNC(attachrid)
  7879. {
  7880. st->rid=script_getnum(st,2);
  7881. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7882. return 0;
  7883. }
  7884. /*==========================================
  7885. * RIDのデタッチ
  7886. *------------------------------------------*/
  7887. BUILDIN_FUNC(detachrid)
  7888. {
  7889. st->rid=0;
  7890. return 0;
  7891. }
  7892. /*==========================================
  7893. * 存在チェック
  7894. *------------------------------------------*/
  7895. BUILDIN_FUNC(isloggedin)
  7896. {
  7897. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  7898. if (script_hasdata(st,3) && sd &&
  7899. sd->status.char_id != script_getnum(st,3))
  7900. sd = NULL;
  7901. push_val(st->stack,C_INT,sd!=NULL);
  7902. return 0;
  7903. }
  7904. /*==========================================
  7905. *
  7906. *------------------------------------------*/
  7907. BUILDIN_FUNC(setmapflagnosave)
  7908. {
  7909. int m,x,y;
  7910. unsigned short mapindex;
  7911. const char *str,*str2;
  7912. str=script_getstr(st,2);
  7913. str2=script_getstr(st,3);
  7914. x=script_getnum(st,4);
  7915. y=script_getnum(st,5);
  7916. m = map_mapname2mapid(str);
  7917. mapindex = mapindex_name2id(str2);
  7918. if(m >= 0 && mapindex) {
  7919. map[m].flag.nosave=1;
  7920. map[m].save.map=mapindex;
  7921. map[m].save.x=x;
  7922. map[m].save.y=y;
  7923. }
  7924. return 0;
  7925. }
  7926. BUILDIN_FUNC(getmapflag)
  7927. {
  7928. int m,i;
  7929. const char *str;
  7930. str=script_getstr(st,2);
  7931. i=script_getnum(st,3);
  7932. m = map_mapname2mapid(str);
  7933. if(m >= 0) {
  7934. switch(i) {
  7935. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  7936. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  7937. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  7938. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  7939. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  7940. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  7941. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  7942. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  7943. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  7944. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  7945. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  7946. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  7947. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  7948. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  7949. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  7950. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  7951. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  7952. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  7953. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  7954. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  7955. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  7956. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  7957. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  7958. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  7959. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  7960. case MF_INDOORS: script_pushint(st,map[m].flag.indoors); break;
  7961. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  7962. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  7963. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  7964. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  7965. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  7966. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  7967. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  7968. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  7969. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  7970. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  7971. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  7972. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  7973. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  7974. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  7975. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  7976. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  7977. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  7978. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  7979. }
  7980. }
  7981. return 0;
  7982. }
  7983. BUILDIN_FUNC(setmapflag)
  7984. {
  7985. int m,i;
  7986. const char *str;
  7987. const char *val=NULL;
  7988. str=script_getstr(st,2);
  7989. i=script_getnum(st,3);
  7990. if(script_hasdata(st,4)){
  7991. val=script_getstr(st,4);
  7992. }
  7993. m = map_mapname2mapid(str);
  7994. if(m >= 0) {
  7995. switch(i) {
  7996. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  7997. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  7998. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  7999. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  8000. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  8001. case MF_PVP: map[m].flag.pvp=1; break;
  8002. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  8003. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  8004. case MF_GVG: map[m].flag.gvg=1; break;
  8005. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  8006. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  8007. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  8008. case MF_NOTRADE: map[m].flag.notrade=1; break;
  8009. case MF_NODROP: map[m].flag.nodrop=1; break;
  8010. case MF_NOSKILL: map[m].flag.noskill=1; break;
  8011. case MF_NOWARP: map[m].flag.nowarp=1; break;
  8012. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  8013. case MF_SNOW: map[m].flag.snow=1; break;
  8014. case MF_CLOUDS: map[m].flag.clouds=1; break;
  8015. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  8016. case MF_FOG: map[m].flag.fog=1; break;
  8017. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  8018. case MF_SAKURA: map[m].flag.sakura=1; break;
  8019. case MF_LEAVES: map[m].flag.leaves=1; break;
  8020. case MF_RAIN: map[m].flag.rain=1; break;
  8021. case MF_INDOORS: map[m].flag.indoors=1; break;
  8022. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  8023. case MF_NOGO: map[m].flag.nogo=1; break;
  8024. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  8025. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  8026. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  8027. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  8028. case MF_NORETURN: map[m].flag.noreturn=1; break;
  8029. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  8030. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  8031. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  8032. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8033. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8034. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8035. case MF_NOVENDING: map[m].flag.novending=1; break;
  8036. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  8037. case MF_NOCHAT: map[m].flag.nochat=1; break;
  8038. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  8039. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  8040. }
  8041. }
  8042. return 0;
  8043. }
  8044. BUILDIN_FUNC(removemapflag)
  8045. {
  8046. int m,i;
  8047. const char *str;
  8048. str=script_getstr(st,2);
  8049. i=script_getnum(st,3);
  8050. m = map_mapname2mapid(str);
  8051. if(m >= 0) {
  8052. switch(i) {
  8053. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  8054. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  8055. case MF_NOSAVE: map[m].flag.nosave=0; break;
  8056. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  8057. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  8058. case MF_PVP: map[m].flag.pvp=0; break;
  8059. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  8060. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  8061. case MF_GVG: map[m].flag.gvg=0; break;
  8062. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  8063. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  8064. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8065. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8066. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8067. case MF_NODROP: map[m].flag.nodrop=0; break;
  8068. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8069. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8070. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8071. case MF_SNOW: map[m].flag.snow=0; break;
  8072. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8073. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8074. case MF_FOG: map[m].flag.fog=0; break;
  8075. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8076. case MF_SAKURA: map[m].flag.sakura=0; break;
  8077. case MF_LEAVES: map[m].flag.leaves=0; break;
  8078. case MF_RAIN: map[m].flag.rain=0; break;
  8079. case MF_INDOORS: map[m].flag.indoors=0; break;
  8080. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8081. case MF_NOGO: map[m].flag.nogo=0; break;
  8082. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8083. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8084. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8085. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8086. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8087. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8088. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8089. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8090. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8091. case MF_JEXP: map[m].jexp=100; break;
  8092. case MF_BEXP: map[m].bexp=100; break;
  8093. case MF_NOVENDING: map[m].flag.novending=0; break;
  8094. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8095. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8096. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8097. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8098. }
  8099. }
  8100. return 0;
  8101. }
  8102. BUILDIN_FUNC(pvpon)
  8103. {
  8104. int m;
  8105. const char *str;
  8106. TBL_PC* sd = NULL;
  8107. struct s_mapiterator* iter;
  8108. str = script_getstr(st,2);
  8109. m = map_mapname2mapid(str);
  8110. if( m < 0 || map[m].flag.pvp )
  8111. return 0; // nothing to do
  8112. map[m].flag.pvp = 1;
  8113. clif_send0199(m,1);
  8114. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8115. return 0;
  8116. iter = mapit_getallusers();
  8117. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8118. {
  8119. if( sd->bl.m != m || sd->pvp_timer != -1 )
  8120. continue; // not applicable
  8121. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8122. sd->pvp_rank = 0;
  8123. sd->pvp_lastusers = 0;
  8124. sd->pvp_point = 5;
  8125. sd->pvp_won = 0;
  8126. sd->pvp_lost = 0;
  8127. }
  8128. mapit_free(iter);
  8129. return 0;
  8130. }
  8131. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8132. {
  8133. TBL_PC* sd = (TBL_PC*)bl;
  8134. clif_pvpset(sd, 0, 0, 2);
  8135. if (sd->pvp_timer != -1) {
  8136. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8137. sd->pvp_timer = -1;
  8138. }
  8139. return 0;
  8140. }
  8141. BUILDIN_FUNC(pvpoff)
  8142. {
  8143. int m;
  8144. const char *str;
  8145. str=script_getstr(st,2);
  8146. m = map_mapname2mapid(str);
  8147. if(m < 0 || !map[m].flag.pvp)
  8148. return 0; //fixed Lupus
  8149. map[m].flag.pvp = 0;
  8150. clif_send0199(m,0);
  8151. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8152. return 0;
  8153. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8154. return 0;
  8155. }
  8156. BUILDIN_FUNC(gvgon)
  8157. {
  8158. int m;
  8159. const char *str;
  8160. str=script_getstr(st,2);
  8161. m = map_mapname2mapid(str);
  8162. if(m >= 0 && !map[m].flag.gvg) {
  8163. map[m].flag.gvg = 1;
  8164. clif_send0199(m,3);
  8165. }
  8166. return 0;
  8167. }
  8168. BUILDIN_FUNC(gvgoff)
  8169. {
  8170. int m;
  8171. const char *str;
  8172. str=script_getstr(st,2);
  8173. m = map_mapname2mapid(str);
  8174. if(m >= 0 && map[m].flag.gvg) {
  8175. map[m].flag.gvg = 0;
  8176. clif_send0199(m,0);
  8177. }
  8178. return 0;
  8179. }
  8180. /*==========================================
  8181. * Shows an emoticon on top of the player/npc
  8182. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8183. *------------------------------------------*/
  8184. //Optional second parameter added by [Skotlex]
  8185. BUILDIN_FUNC(emotion)
  8186. {
  8187. int type;
  8188. int player=0;
  8189. type=script_getnum(st,2);
  8190. if(type < 0 || type > 100)
  8191. return 0;
  8192. if( script_hasdata(st,3) )
  8193. player=script_getnum(st,3);
  8194. if (player) {
  8195. TBL_PC *sd = script_rid2sd(st);
  8196. if (sd)
  8197. clif_emotion(&sd->bl,type);
  8198. } else
  8199. clif_emotion(map_id2bl(st->oid),type);
  8200. return 0;
  8201. }
  8202. static int buildin_maprespawnguildid_sub_pc(struct map_session_data* sd, va_list ap)
  8203. {
  8204. int m=va_arg(ap,int);
  8205. int g_id=va_arg(ap,int);
  8206. int flag=va_arg(ap,int);
  8207. if(!sd || sd->bl.m != m)
  8208. return 0;
  8209. if(
  8210. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8211. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8212. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8213. )
  8214. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8215. return 1;
  8216. }
  8217. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8218. {
  8219. struct mob_data *md=(struct mob_data *)bl;
  8220. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8221. status_kill(bl);
  8222. return 0;
  8223. }
  8224. BUILDIN_FUNC(maprespawnguildid)
  8225. {
  8226. const char *mapname=script_getstr(st,2);
  8227. int g_id=script_getnum(st,3);
  8228. int flag=script_getnum(st,4);
  8229. int m=map_mapname2mapid(mapname);
  8230. if(m == -1)
  8231. return 0;
  8232. //Catch ALL players (in case some are 'between maps' on execution time)
  8233. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8234. if (flag&4) //Remove script mobs.
  8235. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8236. return 0;
  8237. }
  8238. BUILDIN_FUNC(agitstart)
  8239. {
  8240. if(agit_flag==1) return 0; // Agit already Start.
  8241. agit_flag=1;
  8242. guild_agit_start();
  8243. return 0;
  8244. }
  8245. BUILDIN_FUNC(agitend)
  8246. {
  8247. if(agit_flag==0) return 0; // Agit already End.
  8248. agit_flag=0;
  8249. guild_agit_end();
  8250. return 0;
  8251. }
  8252. /*==========================================
  8253. * Returns whether woe is on or off. // choice script
  8254. *------------------------------------------*/
  8255. BUILDIN_FUNC(agitcheck)
  8256. {
  8257. script_pushint(st,agit_flag);
  8258. return 0;
  8259. }
  8260. /// Sets the guild_id of this npc.
  8261. ///
  8262. /// flagemblem <guild_id>;
  8263. BUILDIN_FUNC(flagemblem)
  8264. {
  8265. TBL_NPC* nd;
  8266. int g_id=script_getnum(st,2);
  8267. if(g_id < 0) return 0;
  8268. nd = (TBL_NPC*)map_id2nd(st->oid);
  8269. if( nd == NULL )
  8270. {
  8271. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8272. }
  8273. else if( nd->subtype != SCRIPT )
  8274. {
  8275. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8276. }
  8277. else
  8278. {
  8279. nd->u.scr.guild_id = g_id;
  8280. clif_guild_emblem_area(&nd->bl);
  8281. }
  8282. return 0;
  8283. }
  8284. BUILDIN_FUNC(getcastlename)
  8285. {
  8286. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8287. struct guild_castle* gc = guild_mapname2gc(mapname);
  8288. const char* name = (gc) ? gc->castle_name : "";
  8289. script_pushstrcopy(st,name);
  8290. return 0;
  8291. }
  8292. BUILDIN_FUNC(getcastledata)
  8293. {
  8294. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8295. int index = script_getnum(st,3);
  8296. struct guild_castle* gc = guild_mapname2gc(mapname);
  8297. if(script_hasdata(st,4) && index==0 && gc) {
  8298. const char* event = script_getstr(st,4);
  8299. check_event(st, event);
  8300. guild_addcastleinfoevent(gc->castle_id,17,event);
  8301. }
  8302. if(gc){
  8303. switch(index){
  8304. case 0: {
  8305. int i;
  8306. for(i=1;i<18;i++) // Initialize[AgitInit]
  8307. guild_castledataload(gc->castle_id,i);
  8308. } break;
  8309. case 1:
  8310. script_pushint(st,gc->guild_id); break;
  8311. case 2:
  8312. script_pushint(st,gc->economy); break;
  8313. case 3:
  8314. script_pushint(st,gc->defense); break;
  8315. case 4:
  8316. script_pushint(st,gc->triggerE); break;
  8317. case 5:
  8318. script_pushint(st,gc->triggerD); break;
  8319. case 6:
  8320. script_pushint(st,gc->nextTime); break;
  8321. case 7:
  8322. script_pushint(st,gc->payTime); break;
  8323. case 8:
  8324. script_pushint(st,gc->createTime); break;
  8325. case 9:
  8326. script_pushint(st,gc->visibleC); break;
  8327. case 10:
  8328. case 11:
  8329. case 12:
  8330. case 13:
  8331. case 14:
  8332. case 15:
  8333. case 16:
  8334. case 17:
  8335. script_pushint(st,gc->guardian[index-10].visible); break;
  8336. default:
  8337. script_pushint(st,0); break;
  8338. }
  8339. return 0;
  8340. }
  8341. script_pushint(st,0);
  8342. return 0;
  8343. }
  8344. BUILDIN_FUNC(setcastledata)
  8345. {
  8346. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8347. int index = script_getnum(st,3);
  8348. int value = script_getnum(st,4);
  8349. struct guild_castle* gc = guild_mapname2gc(mapname);
  8350. if(gc) {
  8351. // Save Data byself First
  8352. switch(index){
  8353. case 1:
  8354. gc->guild_id = value; break;
  8355. case 2:
  8356. gc->economy = value; break;
  8357. case 3:
  8358. gc->defense = value; break;
  8359. case 4:
  8360. gc->triggerE = value; break;
  8361. case 5:
  8362. gc->triggerD = value; break;
  8363. case 6:
  8364. gc->nextTime = value; break;
  8365. case 7:
  8366. gc->payTime = value; break;
  8367. case 8:
  8368. gc->createTime = value; break;
  8369. case 9:
  8370. gc->visibleC = value; break;
  8371. case 10:
  8372. case 11:
  8373. case 12:
  8374. case 13:
  8375. case 14:
  8376. case 15:
  8377. case 16:
  8378. case 17:
  8379. gc->guardian[index-10].visible = value; break;
  8380. default:
  8381. return 0;
  8382. }
  8383. guild_castledatasave(gc->castle_id,index,value);
  8384. }
  8385. return 0;
  8386. }
  8387. /* =====================================================================
  8388. * ギルド情報を要求する
  8389. * ---------------------------------------------------------------------*/
  8390. BUILDIN_FUNC(requestguildinfo)
  8391. {
  8392. int guild_id=script_getnum(st,2);
  8393. const char *event=NULL;
  8394. if( script_hasdata(st,3) ){
  8395. event=script_getstr(st,3);
  8396. check_event(st, event);
  8397. }
  8398. if(guild_id>0)
  8399. guild_npc_request_info(guild_id,event);
  8400. return 0;
  8401. }
  8402. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8403. /// getequipcardcnt(<equipment slot>);
  8404. BUILDIN_FUNC(getequipcardcnt)
  8405. {
  8406. int i=-1,j,num;
  8407. TBL_PC *sd;
  8408. int count;
  8409. num=script_getnum(st,2);
  8410. sd=script_rid2sd(st);
  8411. if (num > 0 && num <= ARRAYLENGTH(equip))
  8412. i=pc_checkequip(sd,equip[num-1]);
  8413. if (i < 0) {
  8414. script_pushint(st,0);
  8415. return 0;
  8416. }
  8417. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8418. {
  8419. script_pushint(st,0);
  8420. return 0;
  8421. }
  8422. count = 0;
  8423. for( j = 0; j < MAX_SLOTS; j++ )
  8424. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8425. count++;
  8426. script_pushint(st,count);
  8427. return 0;
  8428. }
  8429. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8430. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8431. /// successremovecards <slot>;
  8432. BUILDIN_FUNC(successremovecards)
  8433. {
  8434. int i=-1,j,c,cardflag=0;
  8435. TBL_PC* sd = script_rid2sd(st);
  8436. int num = script_getnum(st,2);
  8437. if (num > 0 && num <= ARRAYLENGTH(equip))
  8438. i=pc_checkequip(sd,equip[num-1]);
  8439. if (i < 0) {
  8440. return 0;
  8441. }
  8442. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8443. return 0;
  8444. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8445. {
  8446. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8447. {// extract this card from the item
  8448. int flag;
  8449. struct item item_tmp;
  8450. cardflag = 1;
  8451. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8452. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8453. item_tmp.attribute=0;
  8454. for (j = 0; j < MAX_SLOTS; j++)
  8455. item_tmp.card[j]=0;
  8456. //Logs items, got from (N)PC scripts [Lupus]
  8457. if(log_config.enable_logs&0x40)
  8458. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8459. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8460. clif_additem(sd,0,0,flag);
  8461. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8462. }
  8463. }
  8464. }
  8465. if(cardflag == 1)
  8466. { // カードを取り除いたアイテム所得
  8467. int flag;
  8468. struct item item_tmp;
  8469. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8470. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8471. item_tmp.attribute=sd->status.inventory[i].attribute;
  8472. for (j = 0; j < MAX_SLOTS; j++)
  8473. item_tmp.card[j]=0;
  8474. //Logs items, got from (N)PC scripts [Lupus]
  8475. if(log_config.enable_logs&0x40)
  8476. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8477. pc_delitem(sd,i,1,0);
  8478. //Logs items, got from (N)PC scripts [Lupus]
  8479. if(log_config.enable_logs&0x40)
  8480. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8481. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8482. clif_additem(sd,0,0,flag);
  8483. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8484. }
  8485. clif_misceffect(&sd->bl,3);
  8486. }
  8487. return 0;
  8488. }
  8489. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8490. /// failedremovecards <slot>, <type>;
  8491. /// <type>=0 : will destroy both the item and the cards.
  8492. /// <type>=1 : will keep the item, but destroy the cards.
  8493. /// <type>=2 : will keep the cards, but destroy the item.
  8494. /// <type>=? : will just display the failure effect.
  8495. BUILDIN_FUNC(failedremovecards)
  8496. {
  8497. int i=-1,j,c,cardflag=0;
  8498. TBL_PC* sd = script_rid2sd(st);
  8499. int num = script_getnum(st,2);
  8500. int typefail = script_getnum(st,3);
  8501. if (num > 0 && num <= ARRAYLENGTH(equip))
  8502. i=pc_checkequip(sd,equip[num-1]);
  8503. if (i < 0)
  8504. return 0;
  8505. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8506. return 0;
  8507. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8508. {
  8509. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8510. {
  8511. cardflag = 1;
  8512. if(typefail == 2)
  8513. {// add cards to inventory, clear
  8514. int flag;
  8515. struct item item_tmp;
  8516. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8517. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8518. item_tmp.attribute=0;
  8519. for (j = 0; j < MAX_SLOTS; j++)
  8520. item_tmp.card[j]=0;
  8521. //Logs items, got from (N)PC scripts [Lupus]
  8522. if(log_config.enable_logs&0x40)
  8523. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8524. if((flag=pc_additem(sd,&item_tmp,1))){
  8525. clif_additem(sd,0,0,flag);
  8526. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8527. }
  8528. }
  8529. }
  8530. }
  8531. if(cardflag == 1)
  8532. {
  8533. if(typefail == 0 || typefail == 2){ // 武具損失
  8534. //Logs items, got from (N)PC scripts [Lupus]
  8535. if(log_config.enable_logs&0x40)
  8536. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8537. pc_delitem(sd,i,1,0);
  8538. }
  8539. if(typefail == 1){ // カードのみ損失(武具を返す)
  8540. int flag;
  8541. struct item item_tmp;
  8542. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8543. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8544. item_tmp.attribute=sd->status.inventory[i].attribute;
  8545. //Logs items, got from (N)PC scripts [Lupus]
  8546. if(log_config.enable_logs&0x40)
  8547. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8548. for (j = 0; j < MAX_SLOTS; j++)
  8549. item_tmp.card[j]=0;
  8550. pc_delitem(sd,i,1,0);
  8551. //Logs items, got from (N)PC scripts [Lupus]
  8552. if(log_config.enable_logs&0x40)
  8553. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8554. if((flag=pc_additem(sd,&item_tmp,1))){
  8555. clif_additem(sd,0,0,flag);
  8556. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8557. }
  8558. }
  8559. clif_misceffect(&sd->bl,2);
  8560. }
  8561. return 0;
  8562. }
  8563. /* ================================================================
  8564. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8565. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8566. * improved by [Lance]
  8567. * ================================================================*/
  8568. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8569. {
  8570. int x,y,m,check_val=0,check_ID=0,i=0;
  8571. struct guild *g = NULL;
  8572. struct party_data *p = NULL;
  8573. const char *str;
  8574. const char *mapname;
  8575. unsigned int index;
  8576. mapname=script_getstr(st,2);
  8577. str=script_getstr(st,3);
  8578. x=script_getnum(st,4);
  8579. y=script_getnum(st,5);
  8580. if(script_hasdata(st,7)){
  8581. check_val=script_getnum(st,6);
  8582. check_ID=script_getnum(st,7);
  8583. }
  8584. if((m=map_mapname2mapid(mapname))< 0)
  8585. return 0;
  8586. if(!(index=mapindex_name2id(str)))
  8587. return 0;
  8588. switch(check_val){
  8589. case 1:
  8590. g = guild_search(check_ID);
  8591. if (g){
  8592. for( i=0; i < g->max_member; i++)
  8593. {
  8594. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8595. pc_setpos(g->member[i].sd,index,x,y,3);
  8596. }
  8597. }
  8598. }
  8599. break;
  8600. case 2:
  8601. p = party_search(check_ID);
  8602. if(p){
  8603. for(i=0;i<MAX_PARTY; i++){
  8604. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8605. pc_setpos(p->data[i].sd,index,x,y,3);
  8606. }
  8607. }
  8608. }
  8609. break;
  8610. default:
  8611. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8612. break;
  8613. }
  8614. return 0;
  8615. }
  8616. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8617. {
  8618. char *event=va_arg(ap,char *);
  8619. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8620. return 1;
  8621. return 0;
  8622. }
  8623. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8624. {
  8625. const char *mapname,*event;
  8626. int m;
  8627. mapname=script_getstr(st,2);
  8628. event=script_getstr(st,3);
  8629. check_event(st, event);
  8630. if( (m=map_mapname2mapid(mapname))<0 ) {
  8631. script_pushint(st,-1);
  8632. return 0;
  8633. }
  8634. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8635. return 0;
  8636. }
  8637. BUILDIN_FUNC(marriage)
  8638. {
  8639. const char *partner=script_getstr(st,2);
  8640. TBL_PC *sd=script_rid2sd(st);
  8641. TBL_PC *p_sd=map_nick2sd(partner);
  8642. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8643. script_pushint(st,0);
  8644. return 0;
  8645. }
  8646. script_pushint(st,1);
  8647. return 0;
  8648. }
  8649. BUILDIN_FUNC(wedding_effect)
  8650. {
  8651. TBL_PC *sd=script_rid2sd(st);
  8652. struct block_list *bl;
  8653. if(sd==NULL) {
  8654. bl=map_id2bl(st->oid);
  8655. } else
  8656. bl=&sd->bl;
  8657. clif_wedding_effect(bl);
  8658. return 0;
  8659. }
  8660. BUILDIN_FUNC(divorce)
  8661. {
  8662. TBL_PC *sd=script_rid2sd(st);
  8663. if(sd==NULL || pc_divorce(sd) < 0){
  8664. script_pushint(st,0);
  8665. return 0;
  8666. }
  8667. script_pushint(st,1);
  8668. return 0;
  8669. }
  8670. BUILDIN_FUNC(ispartneron)
  8671. {
  8672. TBL_PC *sd=script_rid2sd(st);
  8673. TBL_PC *p_sd=NULL;
  8674. if(sd==NULL || !pc_ismarried(sd) ||
  8675. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8676. script_pushint(st,0);
  8677. return 0;
  8678. }
  8679. script_pushint(st,1);
  8680. return 0;
  8681. }
  8682. BUILDIN_FUNC(getpartnerid)
  8683. {
  8684. TBL_PC *sd=script_rid2sd(st);
  8685. if (sd == NULL) {
  8686. script_pushint(st,0);
  8687. return 0;
  8688. }
  8689. script_pushint(st,sd->status.partner_id);
  8690. return 0;
  8691. }
  8692. BUILDIN_FUNC(getchildid)
  8693. {
  8694. TBL_PC *sd=script_rid2sd(st);
  8695. if (sd == NULL) {
  8696. script_pushint(st,0);
  8697. return 0;
  8698. }
  8699. script_pushint(st,sd->status.child);
  8700. return 0;
  8701. }
  8702. BUILDIN_FUNC(getmotherid)
  8703. {
  8704. TBL_PC *sd=script_rid2sd(st);
  8705. if (sd == NULL) {
  8706. script_pushint(st,0);
  8707. return 0;
  8708. }
  8709. script_pushint(st,sd->status.mother);
  8710. return 0;
  8711. }
  8712. BUILDIN_FUNC(getfatherid)
  8713. {
  8714. TBL_PC *sd=script_rid2sd(st);
  8715. if (sd == NULL) {
  8716. script_pushint(st,0);
  8717. return 0;
  8718. }
  8719. script_pushint(st,sd->status.father);
  8720. return 0;
  8721. }
  8722. BUILDIN_FUNC(warppartner)
  8723. {
  8724. int x,y;
  8725. unsigned short mapindex;
  8726. const char *str;
  8727. TBL_PC *sd=script_rid2sd(st);
  8728. TBL_PC *p_sd=NULL;
  8729. if(sd==NULL || !pc_ismarried(sd) ||
  8730. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8731. script_pushint(st,0);
  8732. return 0;
  8733. }
  8734. str=script_getstr(st,2);
  8735. x=script_getnum(st,3);
  8736. y=script_getnum(st,4);
  8737. mapindex = mapindex_name2id(str);
  8738. if (mapindex) {
  8739. pc_setpos(p_sd,mapindex,x,y,0);
  8740. script_pushint(st,1);
  8741. } else
  8742. script_pushint(st,0);
  8743. return 0;
  8744. }
  8745. /*================================================
  8746. * Script for Displaying MOB Information [Valaris]
  8747. *------------------------------------------------*/
  8748. BUILDIN_FUNC(strmobinfo)
  8749. {
  8750. int num=script_getnum(st,2);
  8751. int class_=script_getnum(st,3);
  8752. if(!mobdb_checkid(class_))
  8753. {
  8754. script_pushint(st,0);
  8755. return 0;
  8756. }
  8757. switch (num) {
  8758. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  8759. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  8760. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8761. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8762. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8763. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8764. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8765. default:
  8766. script_pushint(st,0);
  8767. break;
  8768. }
  8769. return 0;
  8770. }
  8771. /*==========================================
  8772. * Summon guardians [Valaris]
  8773. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  8774. *------------------------------------------*/
  8775. BUILDIN_FUNC(guardian)
  8776. {
  8777. int class_=0,x=0,y=0,guardian=0;
  8778. const char *str,*map,*evt="";
  8779. struct script_data *data;
  8780. bool has_index = false;
  8781. map =script_getstr(st,2);
  8782. x =script_getnum(st,3);
  8783. y =script_getnum(st,4);
  8784. str =script_getstr(st,5);
  8785. class_=script_getnum(st,6);
  8786. if( script_hasdata(st,8) )
  8787. {// "<event label>",<guardian index>
  8788. evt=script_getstr(st,7);
  8789. guardian=script_getnum(st,8);
  8790. has_index = true;
  8791. } else if( script_hasdata(st,7) ){
  8792. data=script_getdata(st,7);
  8793. get_val(st,data);
  8794. if( data_isstring(data) )
  8795. {// "<event label>"
  8796. evt=script_getstr(st,7);
  8797. } else if( data_isint(data) )
  8798. {// <guardian index>
  8799. guardian=script_getnum(st,7);
  8800. has_index = true;
  8801. } else {
  8802. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8803. script_reportdata(data);
  8804. return 1;
  8805. }
  8806. }
  8807. check_event(st, evt);
  8808. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  8809. return 0;
  8810. }
  8811. /*==========================================
  8812. * Build Barricade [Zephyrus]
  8813. * buildbarricade("<map name>",<x>,<y>,"<name to show>",<size>,<dir>,<killable>,<walkable>,<shootable>,<odd>,"<event label>")
  8814. *------------------------------------------*/
  8815. BUILDIN_FUNC(buildbarricade)
  8816. {
  8817. int m = map_mapname2mapid(script_getstr(st,2));
  8818. int x = script_getnum(st,3);
  8819. int y = script_getnum(st,4);
  8820. const char *str = script_getstr(st,5);
  8821. int size = script_getnum(st,6);
  8822. short dir = script_getnum(st,7);
  8823. bool killable = (bool)script_getnum(st,8);
  8824. bool walkable = (bool)script_getnum(st,9);
  8825. bool shootable = (bool)script_getnum(st,10);
  8826. bool odd = (bool)script_getnum(st,11);
  8827. const char *evt = script_getstr(st,12);
  8828. if( m < 0 ) return -1;
  8829. check_event(st, evt);
  8830. mob_barricade_build(m, x, y , str, size, dir, killable, walkable, shootable, odd, evt);
  8831. return 0;
  8832. }
  8833. BUILDIN_FUNC(killbarricade)
  8834. {
  8835. int m = map_mapname2mapid(script_getstr(st,2));
  8836. const char *evt = script_getstr(st,3);
  8837. mob_barricade_destroy(m, evt);
  8838. return 0;
  8839. }
  8840. /// Retrieves various information about the specified guardian.
  8841. ///
  8842. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  8843. /// type: 0 - whether it is deployed or not
  8844. /// 1 - maximum hp
  8845. /// 2 - current hp
  8846. ///
  8847. BUILDIN_FUNC(guardianinfo)
  8848. {
  8849. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8850. int id = script_getnum(st,3);
  8851. int type = script_getnum(st,4);
  8852. struct guild_castle* gc = guild_mapname2gc(mapname);
  8853. struct mob_data* gd;
  8854. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  8855. {
  8856. script_pushint(st,-1);
  8857. return 0;
  8858. }
  8859. if( type == 0 )
  8860. script_pushint(st, gc->guardian[id].visible);
  8861. else
  8862. if( !gc->guardian[id].visible )
  8863. script_pushint(st,-1);
  8864. else
  8865. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  8866. script_pushint(st,-1);
  8867. else
  8868. {
  8869. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  8870. else if( type == 2 ) script_pushint(st,gd->status.hp);
  8871. else
  8872. script_pushint(st,-1);
  8873. }
  8874. return 0;
  8875. }
  8876. /*==========================================
  8877. * IDからItem名
  8878. *------------------------------------------*/
  8879. BUILDIN_FUNC(getitemname)
  8880. {
  8881. int item_id=0;
  8882. struct item_data *i_data;
  8883. char *item_name;
  8884. struct script_data *data;
  8885. data=script_getdata(st,2);
  8886. get_val(st,data);
  8887. if( data_isstring(data) ){
  8888. const char *name=conv_str(st,data);
  8889. struct item_data *item_data = itemdb_searchname(name);
  8890. if( item_data )
  8891. item_id=item_data->nameid;
  8892. }else
  8893. item_id=conv_num(st,data);
  8894. i_data = itemdb_exists(item_id);
  8895. if (i_data == NULL)
  8896. {
  8897. script_pushconststr(st,"null");
  8898. return 0;
  8899. }
  8900. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8901. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8902. script_pushstr(st,item_name);
  8903. return 0;
  8904. }
  8905. /*==========================================
  8906. * Returns number of slots an item has. [Skotlex]
  8907. *------------------------------------------*/
  8908. BUILDIN_FUNC(getitemslots)
  8909. {
  8910. int item_id;
  8911. struct item_data *i_data;
  8912. item_id=script_getnum(st,2);
  8913. i_data = itemdb_exists(item_id);
  8914. if (i_data)
  8915. script_pushint(st,i_data->slot);
  8916. else
  8917. script_pushint(st,-1);
  8918. return 0;
  8919. }
  8920. /*==========================================
  8921. * Returns some values of an item [Lupus]
  8922. * Price, Weight, etc...
  8923. getiteminfo(itemID,n), where n
  8924. 0 value_buy;
  8925. 1 value_sell;
  8926. 2 type;
  8927. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8928. if = 0, then monsters don't drop it at all (rare or a quest item)
  8929. if = 10000, then this item is sold in NPC shops only
  8930. 4 sex;
  8931. 5 equip;
  8932. 6 weight;
  8933. 7 atk;
  8934. 8 def;
  8935. 9 range;
  8936. 10 slot;
  8937. 11 look;
  8938. 12 elv;
  8939. 13 wlv;
  8940. 14 view id
  8941. *------------------------------------------*/
  8942. BUILDIN_FUNC(getiteminfo)
  8943. {
  8944. int item_id,n;
  8945. int *item_arr;
  8946. struct item_data *i_data;
  8947. item_id = script_getnum(st,2);
  8948. n = script_getnum(st,3);
  8949. i_data = itemdb_exists(item_id);
  8950. if (i_data && n>=0 && n<14) {
  8951. item_arr = (int*)&i_data->value_buy;
  8952. script_pushint(st,item_arr[n]);
  8953. } else
  8954. script_pushint(st,-1);
  8955. return 0;
  8956. }
  8957. /*==========================================
  8958. * Set some values of an item [Lupus]
  8959. * Price, Weight, etc...
  8960. setiteminfo(itemID,n,Value), where n
  8961. 0 value_buy;
  8962. 1 value_sell;
  8963. 2 type;
  8964. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8965. if = 0, then monsters don't drop it at all (rare or a quest item)
  8966. if = 10000, then this item is sold in NPC shops only
  8967. 4 sex;
  8968. 5 equip;
  8969. 6 weight;
  8970. 7 atk;
  8971. 8 def;
  8972. 9 range;
  8973. 10 slot;
  8974. 11 look;
  8975. 12 elv;
  8976. 13 wlv;
  8977. 14 view id
  8978. * Returns Value or -1 if the wrong field's been set
  8979. *------------------------------------------*/
  8980. BUILDIN_FUNC(setiteminfo)
  8981. {
  8982. int item_id,n,value;
  8983. int *item_arr;
  8984. struct item_data *i_data;
  8985. item_id = script_getnum(st,2);
  8986. n = script_getnum(st,3);
  8987. value = script_getnum(st,4);
  8988. i_data = itemdb_exists(item_id);
  8989. if (i_data && n>=0 && n<14) {
  8990. item_arr = (int*)&i_data->value_buy;
  8991. item_arr[n] = value;
  8992. script_pushint(st,value);
  8993. } else
  8994. script_pushint(st,-1);
  8995. return 0;
  8996. }
  8997. /*==========================================
  8998. * Returns value from equipped item slot n [Lupus]
  8999. getequipcardid(num,slot)
  9000. where
  9001. num = eqip position slot
  9002. slot = 0,1,2,3 (Card Slot N)
  9003. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9004. it's useful when you want to check item cards or if it's signed
  9005. Useful for such quests as "Sign this refined item with players name" etc
  9006. Hat[0] +4 -> Player's Hat[0] +4
  9007. *------------------------------------------*/
  9008. BUILDIN_FUNC(getequipcardid)
  9009. {
  9010. int i=-1,num,slot;
  9011. TBL_PC *sd;
  9012. num=script_getnum(st,2);
  9013. slot=script_getnum(st,3);
  9014. sd=script_rid2sd(st);
  9015. if (num > 0 && num <= ARRAYLENGTH(equip))
  9016. i=pc_checkequip(sd,equip[num-1]);
  9017. if(i >= 0 && slot>=0 && slot<4)
  9018. script_pushint(st,sd->status.inventory[i].card[slot]);
  9019. else
  9020. script_pushint(st,0);
  9021. return 0;
  9022. }
  9023. /*==========================================
  9024. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9025. *------------------------------------------*/
  9026. BUILDIN_FUNC(petskillbonus)
  9027. {
  9028. struct pet_data *pd;
  9029. TBL_PC *sd=script_rid2sd(st);
  9030. if(sd==NULL || sd->pd==NULL)
  9031. return 0;
  9032. pd=sd->pd;
  9033. if (pd->bonus)
  9034. { //Clear previous bonus
  9035. if (pd->bonus->timer != -1)
  9036. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9037. } else //init
  9038. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9039. pd->bonus->type=script_getnum(st,2);
  9040. pd->bonus->val=script_getnum(st,3);
  9041. pd->bonus->duration=script_getnum(st,4);
  9042. pd->bonus->delay=script_getnum(st,5);
  9043. if (pd->state.skillbonus == 1)
  9044. pd->state.skillbonus=0; // waiting state
  9045. // wait for timer to start
  9046. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9047. pd->bonus->timer=-1;
  9048. else
  9049. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9050. return 0;
  9051. }
  9052. /*==========================================
  9053. * pet looting [Valaris] //Rewritten by [Skotlex]
  9054. *------------------------------------------*/
  9055. BUILDIN_FUNC(petloot)
  9056. {
  9057. int max;
  9058. struct pet_data *pd;
  9059. TBL_PC *sd=script_rid2sd(st);
  9060. if(sd==NULL || sd->pd==NULL)
  9061. return 0;
  9062. max=script_getnum(st,2);
  9063. if(max < 1)
  9064. max = 1; //Let'em loot at least 1 item.
  9065. else if (max > MAX_PETLOOT_SIZE)
  9066. max = MAX_PETLOOT_SIZE;
  9067. pd = sd->pd;
  9068. if (pd->loot != NULL)
  9069. { //Release whatever was there already and reallocate memory
  9070. pet_lootitem_drop(pd, pd->msd);
  9071. aFree(pd->loot->item);
  9072. }
  9073. else
  9074. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9075. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9076. pd->loot->max=max;
  9077. pd->loot->count = 0;
  9078. pd->loot->weight = 0;
  9079. return 0;
  9080. }
  9081. /*==========================================
  9082. * PCの所持品情報読み取り
  9083. *------------------------------------------*/
  9084. BUILDIN_FUNC(getinventorylist)
  9085. {
  9086. TBL_PC *sd=script_rid2sd(st);
  9087. char card_var[NAME_LENGTH];
  9088. int i,j=0,k;
  9089. if(!sd) return 0;
  9090. for(i=0;i<MAX_INVENTORY;i++){
  9091. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9092. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9093. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9094. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9095. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9096. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9097. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9098. for (k = 0; k < MAX_SLOTS; k++)
  9099. {
  9100. sprintf(card_var, "@inventorylist_card%d",k+1);
  9101. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9102. }
  9103. j++;
  9104. }
  9105. }
  9106. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9107. return 0;
  9108. }
  9109. BUILDIN_FUNC(getskilllist)
  9110. {
  9111. TBL_PC *sd=script_rid2sd(st);
  9112. int i,j=0;
  9113. if(!sd) return 0;
  9114. for(i=0;i<MAX_SKILL;i++){
  9115. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9116. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9117. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9118. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9119. j++;
  9120. }
  9121. }
  9122. pc_setreg(sd,add_str("@skilllist_count"),j);
  9123. return 0;
  9124. }
  9125. BUILDIN_FUNC(clearitem)
  9126. {
  9127. TBL_PC *sd=script_rid2sd(st);
  9128. int i;
  9129. if(sd==NULL) return 0;
  9130. for (i=0; i<MAX_INVENTORY; i++) {
  9131. if (sd->status.inventory[i].amount) {
  9132. //Logs items, got from (N)PC scripts [Lupus]
  9133. if(log_config.enable_logs&0x40)
  9134. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9135. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9136. }
  9137. }
  9138. return 0;
  9139. }
  9140. /*==========================================
  9141. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9142. *------------------------------------------*/
  9143. BUILDIN_FUNC(disguise)
  9144. {
  9145. int id;
  9146. TBL_PC* sd = script_rid2sd(st);
  9147. if (sd == NULL) return 0;
  9148. id = script_getnum(st,2);
  9149. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9150. pc_disguise(sd, id);
  9151. script_pushint(st,id);
  9152. } else
  9153. script_pushint(st,0);
  9154. return 0;
  9155. }
  9156. /*==========================================
  9157. * Undisguise Player (returns 1 if success, 0 on fail)
  9158. *------------------------------------------*/
  9159. BUILDIN_FUNC(undisguise)
  9160. {
  9161. TBL_PC* sd = script_rid2sd(st);
  9162. if (sd == NULL) return 0;
  9163. if (sd->disguise) {
  9164. pc_disguise(sd, 0);
  9165. script_pushint(st,0);
  9166. } else {
  9167. script_pushint(st,1);
  9168. }
  9169. return 0;
  9170. }
  9171. /*==========================================
  9172. * NPCクラスチェンジ
  9173. * classは変わりたいclass
  9174. * typeは通常0なのかな?
  9175. *------------------------------------------*/
  9176. BUILDIN_FUNC(classchange)
  9177. {
  9178. int _class,type;
  9179. struct block_list *bl=map_id2bl(st->oid);
  9180. if(bl==NULL) return 0;
  9181. _class=script_getnum(st,2);
  9182. type=script_getnum(st,3);
  9183. clif_class_change(bl,_class,type);
  9184. return 0;
  9185. }
  9186. /*==========================================
  9187. * NPCから発生するエフェクト
  9188. *------------------------------------------*/
  9189. BUILDIN_FUNC(misceffect)
  9190. {
  9191. int type;
  9192. type=script_getnum(st,2);
  9193. if(st->oid && st->oid != fake_nd->bl.id) {
  9194. struct block_list *bl = map_id2bl(st->oid);
  9195. if (bl)
  9196. clif_misceffect2(bl,type);
  9197. } else{
  9198. TBL_PC *sd=script_rid2sd(st);
  9199. if(sd)
  9200. clif_misceffect2(&sd->bl,type);
  9201. }
  9202. return 0;
  9203. }
  9204. /*==========================================
  9205. * サウンドエフェクト
  9206. *------------------------------------------*/
  9207. BUILDIN_FUNC(soundeffect)
  9208. {
  9209. TBL_PC* sd = script_rid2sd(st);
  9210. const char* name = script_getstr(st,2);
  9211. int type = script_getnum(st,3);
  9212. if(sd)
  9213. {
  9214. if(!st->rid)
  9215. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9216. else
  9217. clif_soundeffect(sd,&sd->bl,name,type);
  9218. }
  9219. return 0;
  9220. }
  9221. int soundeffect_sub(struct block_list* bl,va_list ap)
  9222. {
  9223. char* name = va_arg(ap,char*);
  9224. int type = va_arg(ap,int);
  9225. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9226. return 0;
  9227. }
  9228. /*==========================================
  9229. * Play a sound effect (.wav) on multiple clients
  9230. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9231. *------------------------------------------*/
  9232. BUILDIN_FUNC(soundeffectall)
  9233. {
  9234. struct block_list* bl;
  9235. const char* name;
  9236. int type;
  9237. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9238. if (!bl)
  9239. return 0;
  9240. name = script_getstr(st,2);
  9241. type = script_getnum(st,3);
  9242. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9243. if(!script_hasdata(st,4))
  9244. { // area around
  9245. clif_soundeffectall(bl, name, type, AREA);
  9246. }
  9247. else
  9248. if(!script_hasdata(st,5))
  9249. { // entire map
  9250. const char* map = script_getstr(st,4);
  9251. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9252. }
  9253. else
  9254. if(script_hasdata(st,9))
  9255. { // specified part of map
  9256. const char* map = script_getstr(st,5);
  9257. int x0 = script_getnum(st,6);
  9258. int y0 = script_getnum(st,7);
  9259. int x1 = script_getnum(st,8);
  9260. int y1 = script_getnum(st,9);
  9261. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9262. }
  9263. else
  9264. {
  9265. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9266. }
  9267. return 0;
  9268. }
  9269. /*==========================================
  9270. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9271. *------------------------------------------*/
  9272. BUILDIN_FUNC(petrecovery)
  9273. {
  9274. struct pet_data *pd;
  9275. TBL_PC *sd=script_rid2sd(st);
  9276. if(sd==NULL || sd->pd==NULL)
  9277. return 0;
  9278. pd=sd->pd;
  9279. if (pd->recovery)
  9280. { //Halt previous bonus
  9281. if (pd->recovery->timer != -1)
  9282. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9283. } else //Init
  9284. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9285. pd->recovery->type=(sc_type)script_getnum(st,2);
  9286. pd->recovery->delay=script_getnum(st,3);
  9287. pd->recovery->timer=-1;
  9288. return 0;
  9289. }
  9290. /*==========================================
  9291. * pet healing [Valaris] //Rewritten by [Skotlex]
  9292. *------------------------------------------*/
  9293. BUILDIN_FUNC(petheal)
  9294. {
  9295. struct pet_data *pd;
  9296. TBL_PC *sd=script_rid2sd(st);
  9297. if(sd==NULL || sd->pd==NULL)
  9298. return 0;
  9299. pd=sd->pd;
  9300. if (pd->s_skill)
  9301. { //Clear previous skill
  9302. if (pd->s_skill->timer != -1)
  9303. {
  9304. if (pd->s_skill->id)
  9305. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9306. else
  9307. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9308. }
  9309. } else //init memory
  9310. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9311. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9312. //Use the lv as the amount to heal
  9313. pd->s_skill->lv=script_getnum(st,2);
  9314. pd->s_skill->delay=script_getnum(st,3);
  9315. pd->s_skill->hp=script_getnum(st,4);
  9316. pd->s_skill->sp=script_getnum(st,5);
  9317. //Use delay as initial offset to avoid skill/heal exploits
  9318. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9319. pd->s_skill->timer=-1;
  9320. else
  9321. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9322. return 0;
  9323. }
  9324. /*==========================================
  9325. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9326. *------------------------------------------*/
  9327. BUILDIN_FUNC(petskillattack)
  9328. {
  9329. struct pet_data *pd;
  9330. TBL_PC *sd=script_rid2sd(st);
  9331. if(sd==NULL || sd->pd==NULL)
  9332. return 0;
  9333. pd=sd->pd;
  9334. if (pd->a_skill == NULL)
  9335. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9336. pd->a_skill->id=script_getnum(st,2);
  9337. pd->a_skill->lv=script_getnum(st,3);
  9338. pd->a_skill->div_ = 0;
  9339. pd->a_skill->rate=script_getnum(st,4);
  9340. pd->a_skill->bonusrate=script_getnum(st,5);
  9341. return 0;
  9342. }
  9343. /*==========================================
  9344. * pet attack skills [Valaris]
  9345. *------------------------------------------*/
  9346. BUILDIN_FUNC(petskillattack2)
  9347. {
  9348. struct pet_data *pd;
  9349. TBL_PC *sd=script_rid2sd(st);
  9350. if(sd==NULL || sd->pd==NULL)
  9351. return 0;
  9352. pd=sd->pd;
  9353. if (pd->a_skill == NULL)
  9354. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9355. pd->a_skill->id=script_getnum(st,2);
  9356. pd->a_skill->lv=script_getnum(st,3);
  9357. pd->a_skill->div_ = script_getnum(st,4);
  9358. pd->a_skill->rate=script_getnum(st,5);
  9359. pd->a_skill->bonusrate=script_getnum(st,6);
  9360. return 0;
  9361. }
  9362. /*==========================================
  9363. * pet support skills [Skotlex]
  9364. *------------------------------------------*/
  9365. BUILDIN_FUNC(petskillsupport)
  9366. {
  9367. struct pet_data *pd;
  9368. TBL_PC *sd=script_rid2sd(st);
  9369. if(sd==NULL || sd->pd==NULL)
  9370. return 0;
  9371. pd=sd->pd;
  9372. if (pd->s_skill)
  9373. { //Clear previous skill
  9374. if (pd->s_skill->timer != -1)
  9375. {
  9376. if (pd->s_skill->id)
  9377. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9378. else
  9379. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9380. }
  9381. } else //init memory
  9382. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9383. pd->s_skill->id=script_getnum(st,2);
  9384. pd->s_skill->lv=script_getnum(st,3);
  9385. pd->s_skill->delay=script_getnum(st,4);
  9386. pd->s_skill->hp=script_getnum(st,5);
  9387. pd->s_skill->sp=script_getnum(st,6);
  9388. //Use delay as initial offset to avoid skill/heal exploits
  9389. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9390. pd->s_skill->timer=-1;
  9391. else
  9392. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9393. return 0;
  9394. }
  9395. /*==========================================
  9396. * Scripted skill effects [Celest]
  9397. *------------------------------------------*/
  9398. BUILDIN_FUNC(skilleffect)
  9399. {
  9400. TBL_PC *sd;
  9401. int skillid=script_getnum(st,2);
  9402. int skilllv=script_getnum(st,3);
  9403. sd=script_rid2sd(st);
  9404. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9405. return 0;
  9406. }
  9407. /*==========================================
  9408. * NPC skill effects [Valaris]
  9409. *------------------------------------------*/
  9410. BUILDIN_FUNC(npcskilleffect)
  9411. {
  9412. struct block_list *bl= map_id2bl(st->oid);
  9413. int skillid=script_getnum(st,2);
  9414. int skilllv=script_getnum(st,3);
  9415. int x=script_getnum(st,4);
  9416. int y=script_getnum(st,5);
  9417. if (bl)
  9418. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9419. return 0;
  9420. }
  9421. /*==========================================
  9422. * Special effects [Valaris]
  9423. *------------------------------------------*/
  9424. BUILDIN_FUNC(specialeffect)
  9425. {
  9426. struct block_list *bl=map_id2bl(st->oid);
  9427. int type = script_getnum(st,2);
  9428. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9429. if(bl==NULL)
  9430. return 0;
  9431. clif_specialeffect(bl, type, target);
  9432. return 0;
  9433. }
  9434. BUILDIN_FUNC(specialeffect2)
  9435. {
  9436. TBL_PC *sd=script_rid2sd(st);
  9437. int type = script_getnum(st,2);
  9438. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9439. if(sd==NULL)
  9440. return 0;
  9441. clif_specialeffect(&sd->bl, type, target);
  9442. return 0;
  9443. }
  9444. /*==========================================
  9445. * Nude [Valaris]
  9446. *------------------------------------------*/
  9447. BUILDIN_FUNC(nude)
  9448. {
  9449. TBL_PC *sd=script_rid2sd(st);
  9450. int i,calcflag=0;
  9451. if(sd==NULL)
  9452. return 0;
  9453. for(i=0;i<11;i++)
  9454. if(sd->equip_index[i] >= 0) {
  9455. if(!calcflag)
  9456. calcflag=1;
  9457. pc_unequipitem(sd,sd->equip_index[i],2);
  9458. }
  9459. if(calcflag)
  9460. status_calc_pc(sd,0);
  9461. return 0;
  9462. }
  9463. /*==========================================
  9464. * gmcommand [MouseJstr]
  9465. *
  9466. * suggested on the forums...
  9467. * splitted into atcommand & charcommand by [Skotlex]
  9468. *------------------------------------------*/
  9469. BUILDIN_FUNC(atcommand)
  9470. {
  9471. TBL_PC dummy_sd;
  9472. TBL_PC* sd;
  9473. int fd;
  9474. const char* cmd;
  9475. cmd = script_getstr(st,2);
  9476. if (st->rid) {
  9477. sd = script_rid2sd(st);
  9478. fd = sd->fd;
  9479. } else { //Use a dummy character.
  9480. sd = &dummy_sd;
  9481. fd = 0;
  9482. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9483. if (st->oid)
  9484. {
  9485. struct block_list* bl = map_id2bl(st->oid);
  9486. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9487. if (bl->type == BL_NPC)
  9488. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9489. }
  9490. }
  9491. // compatibility with previous implementation (deprecated!)
  9492. if(cmd[0] != atcommand_symbol)
  9493. {
  9494. cmd += strlen(sd->status.name);
  9495. while(*cmd != atcommand_symbol && *cmd != 0)
  9496. cmd++;
  9497. }
  9498. is_atcommand_sub(fd, sd, cmd, 99);
  9499. return 0;
  9500. }
  9501. BUILDIN_FUNC(charcommand)
  9502. {
  9503. TBL_PC dummy_sd;
  9504. TBL_PC* sd;
  9505. int fd;
  9506. const char* cmd;
  9507. cmd = script_getstr(st,2);
  9508. if (st->rid) {
  9509. sd = script_rid2sd(st);
  9510. fd = sd->fd;
  9511. } else { //Use a dummy character.
  9512. sd = &dummy_sd;
  9513. fd = 0;
  9514. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9515. if (st->oid)
  9516. {
  9517. struct block_list* bl = map_id2bl(st->oid);
  9518. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9519. if (bl->type == BL_NPC)
  9520. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9521. }
  9522. }
  9523. // compatibility with previous implementation (deprecated!)
  9524. if(cmd[0] != charcommand_symbol)
  9525. {
  9526. cmd += strlen(sd->status.name);
  9527. while(*cmd != charcommand_symbol && *cmd != 0)
  9528. cmd++;
  9529. }
  9530. is_charcommand_sub(fd, sd, cmd, 99);
  9531. return 0;
  9532. }
  9533. /*==========================================
  9534. * Displays a message for the player only (like system messages like "you got an apple" )
  9535. *------------------------------------------*/
  9536. BUILDIN_FUNC(dispbottom)
  9537. {
  9538. TBL_PC *sd=script_rid2sd(st);
  9539. const char *message;
  9540. message=script_getstr(st,2);
  9541. if(sd)
  9542. clif_disp_onlyself(sd,message,(int)strlen(message));
  9543. return 0;
  9544. }
  9545. /*==========================================
  9546. * All The Players Full Recovery
  9547. * (HP/SP full restore and resurrect if need)
  9548. *------------------------------------------*/
  9549. BUILDIN_FUNC(recovery)
  9550. {
  9551. TBL_PC* sd;
  9552. struct s_mapiterator* iter;
  9553. iter = mapit_getallusers();
  9554. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9555. {
  9556. if(pc_isdead(sd))
  9557. status_revive(&sd->bl, 100, 100);
  9558. else
  9559. status_percent_heal(&sd->bl, 100, 100);
  9560. clif_displaymessage(sd->fd,"You have been recovered!");
  9561. }
  9562. mapit_free(iter);
  9563. return 0;
  9564. }
  9565. /*==========================================
  9566. * Get your pet info: getpetinfo(n)
  9567. * n -> 0:pet_id 1:pet_class 2:pet_name
  9568. * 3:friendly 4:hungry, 5: rename flag.
  9569. *------------------------------------------*/
  9570. BUILDIN_FUNC(getpetinfo)
  9571. {
  9572. TBL_PC *sd=script_rid2sd(st);
  9573. TBL_PET *pd;
  9574. int type=script_getnum(st,2);
  9575. if(!sd || !sd->pd) {
  9576. if (type == 2)
  9577. script_pushconststr(st,"null");
  9578. else
  9579. script_pushint(st,0);
  9580. return 0;
  9581. }
  9582. pd = sd->pd;
  9583. switch(type){
  9584. case 0: script_pushint(st,pd->pet.pet_id); break;
  9585. case 1: script_pushint(st,pd->pet.class_); break;
  9586. case 2: script_pushstrcopy(st,pd->pet.name); break;
  9587. case 3: script_pushint(st,pd->pet.intimate); break;
  9588. case 4: script_pushint(st,pd->pet.hungry); break;
  9589. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9590. default:
  9591. script_pushint(st,0);
  9592. break;
  9593. }
  9594. return 0;
  9595. }
  9596. /*==========================================
  9597. * Get your homunculus info: gethominfo(n)
  9598. * n -> 0:hom_id 1:class 2:name
  9599. * 3:friendly 4:hungry, 5: rename flag.
  9600. * 6: level
  9601. *------------------------------------------*/
  9602. BUILDIN_FUNC(gethominfo)
  9603. {
  9604. TBL_PC *sd=script_rid2sd(st);
  9605. TBL_HOM *hd;
  9606. int type=script_getnum(st,2);
  9607. hd = sd?sd->hd:NULL;
  9608. if(!merc_is_hom_active(hd))
  9609. {
  9610. if (type == 2)
  9611. script_pushconststr(st,"null");
  9612. else
  9613. script_pushint(st,0);
  9614. return 0;
  9615. }
  9616. switch(type){
  9617. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9618. case 1: script_pushint(st,hd->homunculus.class_); break;
  9619. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  9620. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9621. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9622. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9623. case 6: script_pushint(st,hd->homunculus.level); break;
  9624. default:
  9625. script_pushint(st,0);
  9626. break;
  9627. }
  9628. return 0;
  9629. }
  9630. /*==========================================
  9631. * Shows wether your inventory(and equips) contain
  9632. selected card or not.
  9633. checkequipedcard(4001);
  9634. *------------------------------------------*/
  9635. BUILDIN_FUNC(checkequipedcard)
  9636. {
  9637. TBL_PC *sd=script_rid2sd(st);
  9638. int n,i,c=0;
  9639. c=script_getnum(st,2);
  9640. if(sd){
  9641. for(i=0;i<MAX_INVENTORY;i++){
  9642. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9643. for(n=0;n<MAX_SLOTS;n++){
  9644. if(sd->status.inventory[i].card[n]==c){
  9645. script_pushint(st,1);
  9646. return 0;
  9647. }
  9648. }
  9649. }
  9650. }
  9651. }
  9652. script_pushint(st,0);
  9653. return 0;
  9654. }
  9655. BUILDIN_FUNC(jump_zero)
  9656. {
  9657. int sel;
  9658. sel=script_getnum(st,2);
  9659. if(!sel) {
  9660. int pos;
  9661. if( !data_islabel(script_getdata(st,3)) ){
  9662. ShowError("script: jump_zero: not label !\n");
  9663. st->state=END;
  9664. return 1;
  9665. }
  9666. pos=script_getnum(st,3);
  9667. st->pos=pos;
  9668. st->state=GOTO;
  9669. // printf("script: jump_zero: jumpto : %d\n",pos);
  9670. } else {
  9671. // printf("script: jump_zero: fail\n");
  9672. }
  9673. return 0;
  9674. }
  9675. /*==========================================
  9676. * GetMapMobs
  9677. returns mob counts on a set map:
  9678. e.g. GetMapMobs("prontera")
  9679. use "this" - for player's map
  9680. *------------------------------------------*/
  9681. BUILDIN_FUNC(getmapmobs)
  9682. {
  9683. const char *str=NULL;
  9684. int m=-1,bx,by;
  9685. int count=0;
  9686. struct block_list *bl;
  9687. str=script_getstr(st,2);
  9688. if(strcmp(str,"this")==0){
  9689. TBL_PC *sd=script_rid2sd(st);
  9690. if(sd)
  9691. m=sd->bl.m;
  9692. else{
  9693. script_pushint(st,-1);
  9694. return 0;
  9695. }
  9696. }else
  9697. m=map_mapname2mapid(str);
  9698. if(m < 0){
  9699. script_pushint(st,-1);
  9700. return 0;
  9701. }
  9702. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  9703. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  9704. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  9705. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9706. count++;
  9707. script_pushint(st,count);
  9708. return 0;
  9709. }
  9710. /*==========================================
  9711. * movenpc [MouseJstr]
  9712. *------------------------------------------*/
  9713. BUILDIN_FUNC(movenpc)
  9714. {
  9715. TBL_NPC *nd = NULL;
  9716. const char *npc;
  9717. int x,y;
  9718. npc = script_getstr(st,2);
  9719. x = script_getnum(st,3);
  9720. y = script_getnum(st,4);
  9721. if ((nd = npc_name2id(npc)) == NULL)
  9722. return -1;
  9723. npc_movenpc(nd, x, y);
  9724. return 0;
  9725. }
  9726. /*==========================================
  9727. * message [MouseJstr]
  9728. *------------------------------------------*/
  9729. BUILDIN_FUNC(message)
  9730. {
  9731. TBL_PC *sd;
  9732. const char *msg,*player;
  9733. TBL_PC *pl_sd = NULL;
  9734. sd = script_rid2sd(st);
  9735. player = script_getstr(st,2);
  9736. msg = script_getstr(st,3);
  9737. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9738. return 0;
  9739. clif_displaymessage(pl_sd->fd, msg);
  9740. return 0;
  9741. }
  9742. /*==========================================
  9743. * npctalk (sends message to surrounding area)
  9744. *------------------------------------------*/
  9745. BUILDIN_FUNC(npctalk)
  9746. {
  9747. const char* str;
  9748. char message[255];
  9749. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9750. str = script_getstr(st,2);
  9751. if(nd) {
  9752. memcpy(message, nd->name, NAME_LENGTH);
  9753. strtok(message, "#"); // discard extra name identifier if present
  9754. strcat(message, " : ");
  9755. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9756. clif_message(&(nd->bl), message);
  9757. }
  9758. return 0;
  9759. }
  9760. // change npc walkspeed [Valaris]
  9761. BUILDIN_FUNC(npcspeed)
  9762. {
  9763. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9764. int x=0;
  9765. x=script_getnum(st,2);
  9766. if(nd) {
  9767. nd->speed=x;
  9768. }
  9769. return 0;
  9770. }
  9771. // make an npc walk to a position [Valaris]
  9772. BUILDIN_FUNC(npcwalkto)
  9773. {
  9774. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9775. int x=0,y=0;
  9776. x=script_getnum(st,2);
  9777. y=script_getnum(st,3);
  9778. if(nd) {
  9779. unit_walktoxy(&nd->bl,x,y,0);
  9780. }
  9781. return 0;
  9782. }
  9783. // stop an npc's movement [Valaris]
  9784. BUILDIN_FUNC(npcstop)
  9785. {
  9786. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9787. if(nd) {
  9788. unit_stop_walking(&nd->bl,1);
  9789. }
  9790. return 0;
  9791. }
  9792. /*==========================================
  9793. * getlook char info. getlook(arg)
  9794. *------------------------------------------*/
  9795. BUILDIN_FUNC(getlook)
  9796. {
  9797. int type,val;
  9798. TBL_PC *sd;
  9799. sd=script_rid2sd(st);
  9800. type=script_getnum(st,2);
  9801. val=-1;
  9802. switch(type) {
  9803. case LOOK_HAIR: val=sd->status.hair; break; //1
  9804. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  9805. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  9806. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  9807. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  9808. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  9809. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  9810. case LOOK_SHIELD: val=sd->status.shield; break; //8
  9811. case LOOK_SHOES: break; //9
  9812. }
  9813. script_pushint(st,val);
  9814. return 0;
  9815. }
  9816. /*==========================================
  9817. * get char save point. argument: 0- map name, 1- x, 2- y
  9818. *------------------------------------------*/
  9819. BUILDIN_FUNC(getsavepoint)
  9820. {
  9821. TBL_PC* sd;
  9822. int type;
  9823. sd = script_rid2sd(st);
  9824. if (sd == NULL) {
  9825. script_pushint(st,0);
  9826. return 0;
  9827. }
  9828. type = script_getnum(st,2);
  9829. switch(type) {
  9830. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  9831. case 1: script_pushint(st,sd->status.save_point.x); break;
  9832. case 2: script_pushint(st,sd->status.save_point.y); break;
  9833. default:
  9834. script_pushint(st,0);
  9835. break;
  9836. }
  9837. return 0;
  9838. }
  9839. /*==========================================
  9840. * Get position for char/npc/pet/mob objects. Added by Lorky
  9841. *
  9842. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  9843. * where type:
  9844. * MapName$ - String variable for output map name
  9845. * MapX - Integer variable for output coord X
  9846. * MapY - Integer variable for output coord Y
  9847. * type - type of object
  9848. * 0 - Character coord
  9849. * 1 - NPC coord
  9850. * 2 - Pet coord
  9851. * 3 - Mob coord (not released)
  9852. * 4 - Homun coord
  9853. * CharName$ - Name object. If miss or "this" the current object
  9854. *
  9855. * Return:
  9856. * 0 - success
  9857. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  9858. *------------------------------------------*/
  9859. BUILDIN_FUNC(getmapxy)
  9860. {
  9861. struct block_list *bl = NULL;
  9862. TBL_PC *sd=NULL;
  9863. int num;
  9864. char *name;
  9865. char prefix;
  9866. int x,y,type;
  9867. char mapname[MAP_NAME_LENGTH];
  9868. memset(mapname, 0, sizeof(mapname));
  9869. if( !data_isreference(script_getdata(st,2)) ){
  9870. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  9871. script_pushint(st,-1);
  9872. return 1;
  9873. }
  9874. if( !data_isreference(script_getdata(st,3)) ){
  9875. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  9876. script_pushint(st,-1);
  9877. return 1;
  9878. }
  9879. if( !data_isreference(script_getdata(st,4)) ){
  9880. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  9881. script_pushint(st,-1);
  9882. return 1;
  9883. }
  9884. // Possible needly check function parameters on C_STR,C_INT,C_INT
  9885. type=script_getnum(st,5);
  9886. switch (type){
  9887. case 0: //Get Character Position
  9888. if( script_hasdata(st,6) )
  9889. sd=map_nick2sd(script_getstr(st,6));
  9890. else
  9891. sd=script_rid2sd(st);
  9892. if (sd)
  9893. bl = &sd->bl;
  9894. break;
  9895. case 1: //Get NPC Position
  9896. if( script_hasdata(st,6) )
  9897. {
  9898. struct npc_data *nd;
  9899. nd=npc_name2id(script_getstr(st,6));
  9900. if (nd)
  9901. bl = &nd->bl;
  9902. } else //In case the origin is not an npc?
  9903. bl=map_id2bl(st->oid);
  9904. break;
  9905. case 2: //Get Pet Position
  9906. if(script_hasdata(st,6))
  9907. sd=map_nick2sd(script_getstr(st,6));
  9908. else
  9909. sd=script_rid2sd(st);
  9910. if (sd && sd->pd)
  9911. bl = &sd->pd->bl;
  9912. break;
  9913. case 3: //Get Mob Position
  9914. break; //Not supported?
  9915. case 4: //Get Homun Position
  9916. if(script_hasdata(st,6))
  9917. sd=map_nick2sd(script_getstr(st,6));
  9918. else
  9919. sd=script_rid2sd(st);
  9920. if (sd && sd->hd)
  9921. bl = &sd->hd->bl;
  9922. break;
  9923. }
  9924. if (!bl) { //No object found.
  9925. script_pushint(st,-1);
  9926. return 0;
  9927. }
  9928. x= bl->x;
  9929. y= bl->y;
  9930. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  9931. //Set MapName$
  9932. num=st->stack->stack_data[st->start+2].u.num;
  9933. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9934. prefix=*name;
  9935. if(not_server_variable(prefix))
  9936. sd=script_rid2sd(st);
  9937. else
  9938. sd=NULL;
  9939. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  9940. //Set MapX
  9941. num=st->stack->stack_data[st->start+3].u.num;
  9942. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9943. prefix=*name;
  9944. if(not_server_variable(prefix))
  9945. sd=script_rid2sd(st);
  9946. else
  9947. sd=NULL;
  9948. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  9949. //Set MapY
  9950. num=st->stack->stack_data[st->start+4].u.num;
  9951. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9952. prefix=*name;
  9953. if(not_server_variable(prefix))
  9954. sd=script_rid2sd(st);
  9955. else
  9956. sd=NULL;
  9957. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  9958. //Return Success value
  9959. script_pushint(st,0);
  9960. return 0;
  9961. }
  9962. /*==========================================
  9963. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  9964. *------------------------------------------*/
  9965. BUILDIN_FUNC(logmes)
  9966. {
  9967. const char *str;
  9968. if (log_config.npc <= 0 ) return 0;
  9969. str = script_getstr(st,2);
  9970. log_npc(script_rid2sd(st),str);
  9971. return 0;
  9972. }
  9973. BUILDIN_FUNC(summon)
  9974. {
  9975. int _class, timeout=0;
  9976. const char *str,*event="";
  9977. TBL_PC *sd;
  9978. struct mob_data *md;
  9979. int tick = gettick();
  9980. sd=script_rid2sd(st);
  9981. if (!sd) return 0;
  9982. str =script_getstr(st,2);
  9983. _class=script_getnum(st,3);
  9984. if( script_hasdata(st,4) )
  9985. timeout=script_getnum(st,4);
  9986. if( script_hasdata(st,5) ){
  9987. event=script_getstr(st,5);
  9988. check_event(st, event);
  9989. }
  9990. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  9991. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  9992. if (md) {
  9993. md->master_id=sd->bl.id;
  9994. md->special_state.ai=1;
  9995. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  9996. mob_spawn (md); //Now it is ready for spawning.
  9997. clif_misceffect2(&md->bl,344);
  9998. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  9999. }
  10000. return 0;
  10001. }
  10002. /*==========================================
  10003. * Checks whether it is daytime/nighttime
  10004. *------------------------------------------*/
  10005. BUILDIN_FUNC(isnight)
  10006. {
  10007. script_pushint(st,(night_flag == 1));
  10008. return 0;
  10009. }
  10010. BUILDIN_FUNC(isday)
  10011. {
  10012. script_pushint(st,(night_flag == 0));
  10013. return 0;
  10014. }
  10015. /*================================================
  10016. * Check whether another item/card has been
  10017. * equipped - used for 2/15's cards patch [celest]
  10018. *------------------------------------------------*/
  10019. // leave this here, just in case
  10020. #if 0
  10021. BUILDIN_FUNC(isequipped)
  10022. {
  10023. TBL_PC *sd;
  10024. int i, j, k, id = 1;
  10025. int ret = -1;
  10026. sd = script_rid2sd(st);
  10027. for (i=0; id!=0; i++) {
  10028. int flag = 0;
  10029. FETCH (i+2, id) else id = 0;
  10030. if (id <= 0)
  10031. continue;
  10032. for (j=0; j<EQI_MAX; j++) {
  10033. int index;
  10034. index = sd->equip_index[j];
  10035. if(index < 0) continue;
  10036. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10037. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10038. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10039. if(!sd->inventory_data[index])
  10040. continue;
  10041. if(itemdb_type(id) != IT_CARD) { //Non card
  10042. if (sd->inventory_data[index]->nameid != id)
  10043. continue;
  10044. flag = 1;
  10045. break;
  10046. } else { //Card
  10047. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10048. continue;
  10049. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10050. if (sd->status.inventory[index].card[k] == id)
  10051. {
  10052. flag = 1;
  10053. break;
  10054. }
  10055. }
  10056. }
  10057. if (flag) break;
  10058. }
  10059. if (ret == -1)
  10060. ret = flag;
  10061. else
  10062. ret &= flag;
  10063. if (!ret) break;
  10064. }
  10065. script_pushint(st,ret);
  10066. return 0;
  10067. }
  10068. #endif
  10069. /*================================================
  10070. * Check how many items/cards in the list are
  10071. * equipped - used for 2/15's cards patch [celest]
  10072. *------------------------------------------------*/
  10073. BUILDIN_FUNC(isequippedcnt)
  10074. {
  10075. TBL_PC *sd;
  10076. int i, j, k, id = 1;
  10077. int ret = 0;
  10078. sd = script_rid2sd(st);
  10079. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10080. script_pushint(st,0);
  10081. return 0;
  10082. }
  10083. for (i=0; id!=0; i++) {
  10084. FETCH (i+2, id) else id = 0;
  10085. if (id <= 0)
  10086. continue;
  10087. for (j=0; j<EQI_MAX; j++) {
  10088. int index;
  10089. index = sd->equip_index[j];
  10090. if(index < 0) continue;
  10091. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10092. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10093. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10094. if(!sd->inventory_data[index])
  10095. continue;
  10096. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10097. if (sd->inventory_data[index]->nameid == id)
  10098. ret+= sd->status.inventory[index].amount;
  10099. } else { //Count cards.
  10100. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10101. continue; //No cards
  10102. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10103. if (sd->status.inventory[index].card[k] == id)
  10104. ret++; //[Lupus]
  10105. }
  10106. }
  10107. }
  10108. }
  10109. script_pushint(st,ret);
  10110. return 0;
  10111. }
  10112. /*================================================
  10113. * Check whether another card has been
  10114. * equipped - used for 2/15's cards patch [celest]
  10115. * -- Items checked cannot be reused in another
  10116. * card set to prevent exploits
  10117. *------------------------------------------------*/
  10118. BUILDIN_FUNC(isequipped)
  10119. {
  10120. TBL_PC *sd;
  10121. int i, j, k, id = 1;
  10122. int index, flag;
  10123. int ret = -1;
  10124. //Original hash to reverse it when full check fails.
  10125. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10126. sd = script_rid2sd(st);
  10127. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10128. script_pushint(st,0);
  10129. return 0;
  10130. }
  10131. setitem_hash = sd->setitem_hash;
  10132. setitem_hash2 = sd->setitem_hash2;
  10133. for (i=0; id!=0; i++)
  10134. {
  10135. FETCH (i+2, id) else id = 0;
  10136. if (id <= 0)
  10137. continue;
  10138. flag = 0;
  10139. for (j=0; j<EQI_MAX; j++)
  10140. {
  10141. index = sd->equip_index[j];
  10142. if(index < 0) continue;
  10143. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10144. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10145. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10146. if(!sd->inventory_data[index])
  10147. continue;
  10148. if (itemdb_type(id) != IT_CARD) {
  10149. if (sd->inventory_data[index]->nameid != id)
  10150. continue;
  10151. flag = 1;
  10152. break;
  10153. } else { //Cards
  10154. if (sd->inventory_data[index]->slot == 0 ||
  10155. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10156. continue;
  10157. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10158. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10159. unsigned int hash = 0;
  10160. if (sd->status.inventory[index].card[k] != id)
  10161. continue;
  10162. hash = 1<<((j<5?j:j-5)*4 + k);
  10163. // check if card is already used by another set
  10164. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10165. continue;
  10166. // We have found a match
  10167. flag = 1;
  10168. // Set hash so this card cannot be used by another
  10169. if (j<5)
  10170. sd->setitem_hash |= hash;
  10171. else
  10172. sd->setitem_hash2 |= hash;
  10173. break;
  10174. }
  10175. }
  10176. if (flag) break; //Card found
  10177. }
  10178. if (ret == -1)
  10179. ret = flag;
  10180. else
  10181. ret &= flag;
  10182. if (!ret) break;
  10183. }
  10184. if (!ret)
  10185. { //When check fails, restore original hash values. [Skotlex]
  10186. sd->setitem_hash = setitem_hash;
  10187. sd->setitem_hash2 = setitem_hash2;
  10188. }
  10189. script_pushint(st,ret);
  10190. return 0;
  10191. }
  10192. /*================================================
  10193. * Check how many given inserted cards in the CURRENT
  10194. * weapon - used for 2/15's cards patch [Lupus]
  10195. *------------------------------------------------*/
  10196. BUILDIN_FUNC(cardscnt)
  10197. {
  10198. TBL_PC *sd;
  10199. int i, k, id = 1;
  10200. int ret = 0;
  10201. int index;
  10202. sd = script_rid2sd(st);
  10203. for (i=0; id!=0; i++) {
  10204. FETCH (i+2, id) else id = 0;
  10205. if (id <= 0)
  10206. continue;
  10207. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10208. if(index < 0) continue;
  10209. if(!sd->inventory_data[index])
  10210. continue;
  10211. if(itemdb_type(id) != IT_CARD) {
  10212. if (sd->inventory_data[index]->nameid == id)
  10213. ret+= sd->status.inventory[index].amount;
  10214. } else {
  10215. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10216. continue;
  10217. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10218. if (sd->status.inventory[index].card[k] == id)
  10219. ret++;
  10220. }
  10221. }
  10222. }
  10223. script_pushint(st,ret);
  10224. // script_pushint(st,current_equip_item_index);
  10225. return 0;
  10226. }
  10227. /*=======================================================
  10228. * Returns the refined number of the current item, or an
  10229. * item with inventory index specified
  10230. *-------------------------------------------------------*/
  10231. BUILDIN_FUNC(getrefine)
  10232. {
  10233. TBL_PC *sd;
  10234. if ((sd = script_rid2sd(st))!= NULL)
  10235. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10236. else
  10237. script_pushint(st,0);
  10238. return 0;
  10239. }
  10240. /*=======================================================
  10241. * Day/Night controls
  10242. *-------------------------------------------------------*/
  10243. BUILDIN_FUNC(night)
  10244. {
  10245. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10246. return 0;
  10247. }
  10248. BUILDIN_FUNC(day)
  10249. {
  10250. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10251. return 0;
  10252. }
  10253. //=======================================================
  10254. // Unequip [Spectre]
  10255. //-------------------------------------------------------
  10256. BUILDIN_FUNC(unequip)
  10257. {
  10258. int i;
  10259. size_t num;
  10260. TBL_PC *sd;
  10261. num = script_getnum(st,2);
  10262. sd = script_rid2sd(st);
  10263. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10264. {
  10265. i = pc_checkequip(sd,equip[num-1]);
  10266. if (i >= 0)
  10267. pc_unequipitem(sd,i,2);
  10268. return 0;
  10269. }
  10270. return 0;
  10271. }
  10272. BUILDIN_FUNC(equip)
  10273. {
  10274. int nameid=0,i;
  10275. TBL_PC *sd;
  10276. struct item_data *item_data;
  10277. sd = script_rid2sd(st);
  10278. nameid=script_getnum(st,2);
  10279. if((item_data = itemdb_exists(nameid)) == NULL)
  10280. {
  10281. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10282. return 1;
  10283. }
  10284. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10285. if( i < MAX_INVENTORY )
  10286. pc_equipitem(sd,i,item_data->equip);
  10287. return 0;
  10288. }
  10289. BUILDIN_FUNC(autoequip)
  10290. {
  10291. int nameid, flag;
  10292. struct item_data *item_data;
  10293. nameid=script_getnum(st,2);
  10294. flag=script_getnum(st,3);
  10295. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10296. item_data->flag.autoequip = flag>0?1:0;
  10297. }
  10298. return 0;
  10299. }
  10300. BUILDIN_FUNC(setbattleflag)
  10301. {
  10302. const char *flag, *value;
  10303. flag = script_getstr(st,2);
  10304. value = script_getstr(st,3);
  10305. if (battle_set_value(flag, value) == 0)
  10306. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10307. else
  10308. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10309. return 0;
  10310. }
  10311. BUILDIN_FUNC(getbattleflag)
  10312. {
  10313. const char *flag;
  10314. flag = script_getstr(st,2);
  10315. script_pushint(st,battle_get_value(flag));
  10316. return 0;
  10317. }
  10318. //=======================================================
  10319. // strlen [Valaris]
  10320. //-------------------------------------------------------
  10321. BUILDIN_FUNC(getstrlen)
  10322. {
  10323. const char *str = script_getstr(st,2);
  10324. int len = (str) ? (int)strlen(str) : 0;
  10325. script_pushint(st,len);
  10326. return 0;
  10327. }
  10328. //=======================================================
  10329. // isalpha [Valaris]
  10330. //-------------------------------------------------------
  10331. BUILDIN_FUNC(charisalpha)
  10332. {
  10333. const char *str=script_getstr(st,2);
  10334. int pos=script_getnum(st,3);
  10335. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10336. script_pushint(st,val);
  10337. return 0;
  10338. }
  10339. /// Changes the display name and/or display class of the npc.
  10340. /// Returns 0 is successful, 1 if the npc does not exist.
  10341. ///
  10342. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>, <new size>) -> <int>
  10343. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10344. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10345. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10346. BUILDIN_FUNC(setnpcdisplay)
  10347. {
  10348. const char* name;
  10349. const char* newname = NULL;
  10350. int class_ = -1, size = -1;
  10351. struct script_data* data;
  10352. struct npc_data* nd;
  10353. name = script_getstr(st,2);
  10354. data = script_getdata(st,3);
  10355. if( script_hasdata(st,5) )
  10356. size = script_getnum(st,5);
  10357. if( script_hasdata(st,4) )
  10358. class_ = script_getnum(st,4);
  10359. get_val(st, data);
  10360. if( data_isstring(data) )
  10361. newname = conv_str(st,data);
  10362. else if( data_isint(data) )
  10363. class_ = conv_num(st,data);
  10364. else
  10365. {
  10366. ShowError("script:setnpcdisplay: expected a string or number\n");
  10367. script_reportdata(data);
  10368. return 1;
  10369. }
  10370. nd = npc_name2id(name);
  10371. if( nd == NULL )
  10372. {// not found
  10373. script_pushint(st,1);
  10374. return 0;
  10375. }
  10376. // update npc
  10377. if( newname )
  10378. npc_setdisplayname(nd, newname);
  10379. if( size != -1 && size != nd->size )
  10380. nd->size = size;
  10381. else
  10382. size = -1;
  10383. if( class_ != -1 && nd->class_ != class_ )
  10384. npc_setclass(nd, class_);
  10385. else if( size != -1 )
  10386. { // Required to update the visual size
  10387. clif_clearunit_area(&nd->bl, 0);
  10388. clif_spawn(&nd->bl);
  10389. }
  10390. script_pushint(st,0);
  10391. return 0;
  10392. }
  10393. BUILDIN_FUNC(atoi)
  10394. {
  10395. const char *value;
  10396. value = script_getstr(st,2);
  10397. script_pushint(st,atoi(value));
  10398. return 0;
  10399. }
  10400. // case-insensitive substring search [lordalfa]
  10401. BUILDIN_FUNC(compare)
  10402. {
  10403. const char *message;
  10404. const char *cmpstring;
  10405. message = script_getstr(st,2);
  10406. cmpstring = script_getstr(st,3);
  10407. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10408. return 0;
  10409. }
  10410. // [zBuffer] List of mathematics commands --->
  10411. BUILDIN_FUNC(sqrt)
  10412. {
  10413. double i, a;
  10414. i = script_getnum(st,2);
  10415. a = sqrt(i);
  10416. script_pushint(st,(int)a);
  10417. return 0;
  10418. }
  10419. BUILDIN_FUNC(pow)
  10420. {
  10421. double i, a, b;
  10422. a = script_getnum(st,2);
  10423. b = script_getnum(st,3);
  10424. i = pow(a,b);
  10425. script_pushint(st,(int)i);
  10426. return 0;
  10427. }
  10428. BUILDIN_FUNC(distance)
  10429. {
  10430. int x0, y0, x1, y1;
  10431. x0 = script_getnum(st,2);
  10432. y0 = script_getnum(st,3);
  10433. x1 = script_getnum(st,4);
  10434. y1 = script_getnum(st,5);
  10435. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10436. return 0;
  10437. }
  10438. // <--- [zBuffer] List of mathematics commands
  10439. // [zBuffer] List of dynamic var commands --->
  10440. //FIXME: some other functions are using this private function
  10441. void setd_sub(struct script_state *st, TBL_PC *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10442. {
  10443. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10444. return;
  10445. }
  10446. BUILDIN_FUNC(setd)
  10447. {
  10448. TBL_PC *sd=NULL;
  10449. char varname[100];
  10450. const char *value, *buffer;
  10451. int elem;
  10452. buffer = script_getstr(st, 2);
  10453. value = script_getstr(st, 3);
  10454. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10455. elem = 0;
  10456. if( not_server_variable(*varname) )
  10457. {
  10458. sd = script_rid2sd(st);
  10459. if( sd == NULL )
  10460. {
  10461. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10462. return 0;
  10463. }
  10464. }
  10465. if(varname[strlen(varname)-1] != '$') {
  10466. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10467. } else {
  10468. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10469. }
  10470. return 0;
  10471. }
  10472. #ifndef TXT_ONLY
  10473. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10474. {
  10475. int i, j;
  10476. TBL_PC* sd = NULL;
  10477. const char* query;
  10478. struct script_data* data;
  10479. char* name;
  10480. int max_rows = 128;// maximum number of rows
  10481. int num_vars;
  10482. int num_cols;
  10483. // check target variables
  10484. for( i = 3; script_hasdata(st,i); ++i )
  10485. {
  10486. data = script_getdata(st, i);
  10487. if( data_isreference(data) && reference_tovariable(data) )
  10488. {// it's a variable
  10489. name = reference_getname(data);
  10490. if( not_server_variable(*name) && sd == NULL )
  10491. {// requires a player
  10492. sd = script_rid2sd(st);
  10493. if( sd == NULL )
  10494. {// no player attached
  10495. script_reportdata(data);
  10496. st->state = END;
  10497. return 1;
  10498. }
  10499. }
  10500. if( not_array_variable(*name) )
  10501. max_rows = 1;// not an array, limit to one row
  10502. }
  10503. else
  10504. {
  10505. ShowError("script:query_sql: not a variable\n");
  10506. script_reportdata(data);
  10507. st->state = END;
  10508. return 1;
  10509. }
  10510. }
  10511. num_vars = i - 3;
  10512. // Execute the query
  10513. query = script_getstr(st,2);
  10514. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10515. {
  10516. Sql_ShowDebug(handle);
  10517. script_pushint(st, 0);
  10518. return 1;
  10519. }
  10520. if( Sql_NumRows(handle) == 0 )
  10521. {// No data received
  10522. Sql_FreeResult(handle);
  10523. script_pushint(st, 0);
  10524. return 0;
  10525. }
  10526. // Count the number of columns to store
  10527. num_cols = Sql_NumColumns(handle);
  10528. if( num_vars < num_cols )
  10529. {
  10530. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10531. script_reportsrc(st);
  10532. }
  10533. else if( num_vars > num_cols )
  10534. {
  10535. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10536. script_reportsrc(st);
  10537. }
  10538. // Store data
  10539. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10540. {
  10541. for( j = 0; j < num_vars; ++j )
  10542. {
  10543. char* str = NULL;
  10544. if( j < num_cols )
  10545. Sql_GetData(handle, j, &str, NULL);
  10546. data = script_getdata(st, j+3);
  10547. name = reference_getname(data);
  10548. if( is_string_variable(name) )
  10549. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10550. else
  10551. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10552. }
  10553. }
  10554. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10555. {
  10556. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10557. script_reportsrc(st);
  10558. }
  10559. // Free data
  10560. Sql_FreeResult(handle);
  10561. script_pushint(st, i);
  10562. return 0;
  10563. }
  10564. #endif
  10565. BUILDIN_FUNC(query_sql)
  10566. {
  10567. #ifndef TXT_ONLY
  10568. return buildin_query_sql_sub(st, mmysql_handle);
  10569. #else
  10570. //for TXT version, we always return -1
  10571. script_pushint(st,-1);
  10572. return 0;
  10573. #endif
  10574. }
  10575. BUILDIN_FUNC(query_logsql)
  10576. {
  10577. #ifndef TXT_ONLY
  10578. return buildin_query_sql_sub(st, logmysql_handle);
  10579. #else
  10580. //for TXT version, we always return -1
  10581. script_pushint(st,-1);
  10582. return 0;
  10583. #endif
  10584. }
  10585. //Allows escaping of a given string.
  10586. BUILDIN_FUNC(escape_sql)
  10587. {
  10588. const char *str;
  10589. char *esc_str;
  10590. size_t len;
  10591. str = script_getstr(st,2);
  10592. len = strlen(str);
  10593. esc_str = (char*)aMallocA(len*2+1);
  10594. #if defined(TXT_ONLY)
  10595. jstrescapecpy(esc_str, str);
  10596. #else
  10597. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10598. #endif
  10599. script_pushstr(st, esc_str);
  10600. return 0;
  10601. }
  10602. BUILDIN_FUNC(getd)
  10603. {
  10604. char varname[100];
  10605. const char *buffer;
  10606. int elem;
  10607. buffer = script_getstr(st, 2);
  10608. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10609. elem = 0;
  10610. // Push the 'pointer' so it's more flexible [Lance]
  10611. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10612. return 0;
  10613. }
  10614. // <--- [zBuffer] List of dynamic var commands
  10615. // Pet stat [Lance]
  10616. BUILDIN_FUNC(petstat)
  10617. {
  10618. TBL_PC *sd = NULL;
  10619. struct pet_data *pd;
  10620. int flag = script_getnum(st,2);
  10621. sd = script_rid2sd(st);
  10622. if(!sd || !sd->status.pet_id || !sd->pd){
  10623. if(flag == 2)
  10624. script_pushconststr(st, "");
  10625. else
  10626. script_pushint(st,0);
  10627. return 0;
  10628. }
  10629. pd = sd->pd;
  10630. switch(flag){
  10631. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10632. case 2: script_pushstrcopy(st, pd->pet.name); break;
  10633. case 3: script_pushint(st,(int)pd->pet.level); break;
  10634. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10635. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10636. default:
  10637. script_pushint(st,0);
  10638. break;
  10639. }
  10640. return 0;
  10641. }
  10642. BUILDIN_FUNC(callshop)
  10643. {
  10644. TBL_PC *sd = NULL;
  10645. struct npc_data *nd;
  10646. const char *shopname;
  10647. int flag = 0;
  10648. sd = script_rid2sd(st);
  10649. if (!sd) {
  10650. script_pushint(st,0);
  10651. return 0;
  10652. }
  10653. shopname = script_getstr(st, 2);
  10654. if( script_hasdata(st,3) )
  10655. flag = script_getnum(st,3);
  10656. nd = npc_name2id(shopname);
  10657. if (!nd || nd->bl.type!=BL_NPC || nd->subtype!=SHOP) {
  10658. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  10659. script_pushint(st,0);
  10660. return 1;
  10661. }
  10662. switch (flag) {
  10663. case 1: //Buy window
  10664. npc_buysellsel(sd,nd->bl.id,0);
  10665. break;
  10666. case 2: //Sell window
  10667. npc_buysellsel(sd,nd->bl.id,1);
  10668. break;
  10669. default: //Show menu
  10670. clif_npcbuysell(sd,nd->bl.id);
  10671. break;
  10672. }
  10673. sd->npc_shopid = nd->bl.id;
  10674. script_pushint(st,1);
  10675. return 0;
  10676. }
  10677. BUILDIN_FUNC(npcshopitem)
  10678. {
  10679. const char* npcname = script_getstr(st, 2);
  10680. struct npc_data* nd = npc_name2id(npcname);
  10681. int n, i;
  10682. int amount;
  10683. if( !nd || nd->subtype != SHOP )
  10684. { //Not found.
  10685. script_pushint(st,0);
  10686. return 0;
  10687. }
  10688. // get the count of new entries
  10689. amount = (script_lastdata(st)-2)/2;
  10690. // generate new shop item list
  10691. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  10692. for( n = 0, i = 3; n < amount; n++, i+=2 )
  10693. {
  10694. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10695. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10696. }
  10697. nd->u.shop.count = n;
  10698. script_pushint(st,1);
  10699. return 0;
  10700. }
  10701. BUILDIN_FUNC(npcshopadditem)
  10702. {
  10703. const char* npcname = script_getstr(st,2);
  10704. struct npc_data* nd = npc_name2id(npcname);
  10705. int n, i;
  10706. int amount;
  10707. if( !nd || nd->subtype != SHOP )
  10708. { //Not found.
  10709. script_pushint(st,0);
  10710. return 0;
  10711. }
  10712. // get the count of new entries
  10713. amount = (script_lastdata(st)-2)/2;
  10714. // append new items to existing shop item list
  10715. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  10716. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  10717. {
  10718. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10719. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10720. }
  10721. nd->u.shop.count = n;
  10722. script_pushint(st,1);
  10723. return 0;
  10724. }
  10725. BUILDIN_FUNC(npcshopdelitem)
  10726. {
  10727. const char* npcname = script_getstr(st,2);
  10728. struct npc_data* nd = npc_name2id(npcname);
  10729. int n, i;
  10730. int amount;
  10731. int size;
  10732. if( !nd || nd->subtype != SHOP )
  10733. { //Not found.
  10734. script_pushint(st,0);
  10735. return 0;
  10736. }
  10737. amount = script_lastdata(st)-2;
  10738. size = nd->u.shop.count;
  10739. // remove specified items from the shop item list
  10740. for( i = 3; i < 3 + amount; i++ )
  10741. {
  10742. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
  10743. if( n < size )
  10744. {
  10745. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  10746. size--;
  10747. }
  10748. }
  10749. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  10750. nd->u.shop.count = size;
  10751. script_pushint(st,1);
  10752. return 0;
  10753. }
  10754. //Sets a script to attach to a shop npc.
  10755. BUILDIN_FUNC(npcshopattach)
  10756. {
  10757. const char* npcname = script_getstr(st,2);
  10758. struct npc_data* nd = npc_name2id(npcname);
  10759. int flag = 1;
  10760. if( script_hasdata(st,3) )
  10761. flag = script_getnum(st,3);
  10762. if( !nd || nd->subtype != SHOP )
  10763. { //Not found.
  10764. script_pushint(st,0);
  10765. return 0;
  10766. }
  10767. if (flag)
  10768. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10769. else
  10770. nd->master_nd = NULL;
  10771. script_pushint(st,1);
  10772. return 0;
  10773. }
  10774. /*==========================================
  10775. * Returns some values of an item [Lupus]
  10776. * Price, Weight, etc...
  10777. setitemscript(itemID,"{new item bonus script}",[n]);
  10778. Where n:
  10779. 0 - script
  10780. 1 - Equip script
  10781. 2 - Unequip script
  10782. *------------------------------------------*/
  10783. BUILDIN_FUNC(setitemscript)
  10784. {
  10785. int item_id,n=0;
  10786. const char *script;
  10787. struct item_data *i_data;
  10788. struct script_code **dstscript;
  10789. item_id = script_getnum(st,2);
  10790. script = script_getstr(st,3);
  10791. if( script_hasdata(st,4) )
  10792. n=script_getnum(st,4);
  10793. i_data = itemdb_exists(item_id);
  10794. if (!i_data || script==NULL || script[0]!='{') {
  10795. script_pushint(st,0);
  10796. return 0;
  10797. }
  10798. switch (n) {
  10799. case 2:
  10800. dstscript = &i_data->unequip_script;
  10801. break;
  10802. case 1:
  10803. dstscript = &i_data->equip_script;
  10804. break;
  10805. default:
  10806. dstscript = &i_data->script;
  10807. break;
  10808. }
  10809. if(*dstscript)
  10810. script_free_code(*dstscript);
  10811. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  10812. script_pushint(st,1);
  10813. return 0;
  10814. }
  10815. /* Work In Progress [Lupus]
  10816. BUILDIN_FUNC(addmonsterdrop)
  10817. {
  10818. int class_,item_id,chance;
  10819. class_=script_getnum(st,2);
  10820. item_id=script_getnum(st,3);
  10821. chance=script_getnum(st,4);
  10822. if(class_>1000 && item_id>500 && chance>0) {
  10823. script_pushint(st,1);
  10824. } else {
  10825. script_pushint(st,0);
  10826. }
  10827. }
  10828. BUILDIN_FUNC(delmonsterdrop)
  10829. {
  10830. int class_,item_id;
  10831. class_=script_getnum(st,2);
  10832. item_id=script_getnum(st,3);
  10833. if(class_>1000 && item_id>500) {
  10834. script_pushint(st,1);
  10835. } else {
  10836. script_pushint(st,0);
  10837. }
  10838. }
  10839. */
  10840. /*==========================================
  10841. * Returns some values of a monster [Lupus]
  10842. * Name, Level, race, size, etc...
  10843. getmonsterinfo(monsterID,queryIndex);
  10844. *------------------------------------------*/
  10845. BUILDIN_FUNC(getmonsterinfo)
  10846. {
  10847. struct mob_db *mob;
  10848. int mob_id;
  10849. mob_id = script_getnum(st,2);
  10850. if (!mobdb_checkid(mob_id)) {
  10851. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  10852. if ( !script_getnum(st,3) ) //requested a string
  10853. script_pushconststr(st,"null");
  10854. else
  10855. script_pushint(st,-1);
  10856. return -1;
  10857. }
  10858. mob = mob_db(mob_id);
  10859. switch ( script_getnum(st,3) ) {
  10860. case 0: script_pushstrcopy(st,mob->jname); break;
  10861. case 1: script_pushint(st,mob->lv); break;
  10862. case 2: script_pushint(st,mob->status.max_hp); break;
  10863. case 3: script_pushint(st,mob->base_exp); break;
  10864. case 4: script_pushint(st,mob->job_exp); break;
  10865. case 5: script_pushint(st,mob->status.rhw.atk); break;
  10866. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  10867. case 7: script_pushint(st,mob->status.def); break;
  10868. case 8: script_pushint(st,mob->status.mdef); break;
  10869. case 9: script_pushint(st,mob->status.str); break;
  10870. case 10: script_pushint(st,mob->status.agi); break;
  10871. case 11: script_pushint(st,mob->status.vit); break;
  10872. case 12: script_pushint(st,mob->status.int_); break;
  10873. case 13: script_pushint(st,mob->status.dex); break;
  10874. case 14: script_pushint(st,mob->status.luk); break;
  10875. case 15: script_pushint(st,mob->status.rhw.range); break;
  10876. case 16: script_pushint(st,mob->range2); break;
  10877. case 17: script_pushint(st,mob->range3); break;
  10878. case 18: script_pushint(st,mob->status.size); break;
  10879. case 19: script_pushint(st,mob->status.race); break;
  10880. case 20: script_pushint(st,mob->status.def_ele); break;
  10881. case 21: script_pushint(st,mob->status.mode); break;
  10882. default: script_pushint(st,-1); //wrong Index
  10883. }
  10884. return 0;
  10885. }
  10886. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  10887. {
  10888. TBL_PC *sd = NULL;
  10889. if(script_hasdata(st,2))
  10890. sd = map_nick2sd(script_getstr(st,2));
  10891. else
  10892. sd = script_rid2sd(st);
  10893. if(sd)
  10894. script_pushint(st,(sd->vender_id != 0));
  10895. else
  10896. script_pushint(st,0);
  10897. return 0;
  10898. }
  10899. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  10900. {
  10901. TBL_PC *sd = NULL;
  10902. if(script_hasdata(st,2))
  10903. sd = map_nick2sd(script_getstr(st,2));
  10904. else
  10905. sd = script_rid2sd(st);
  10906. if(sd)
  10907. script_pushint(st,(sd->chatID != 0));
  10908. else
  10909. script_pushint(st,0);
  10910. return 0;
  10911. }
  10912. int axtoi(const char *hexStg)
  10913. {
  10914. int n = 0; // position in string
  10915. int m = 0; // position in digit[] to shift
  10916. int count; // loop index
  10917. int intValue = 0; // integer value of hex string
  10918. int digit[11]; // hold values to convert
  10919. while (n < 10) {
  10920. if (hexStg[n]=='\0')
  10921. break;
  10922. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  10923. digit[n] = hexStg[n] & 0x0f; //convert to int
  10924. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  10925. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10926. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  10927. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10928. else break;
  10929. n++;
  10930. }
  10931. count = n;
  10932. m = n - 1;
  10933. n = 0;
  10934. while(n < count) {
  10935. // digit[n] is value of hex digit at position n
  10936. // (m << 2) is the number of positions to shift
  10937. // OR the bits into return value
  10938. intValue = intValue | (digit[n] << (m << 2));
  10939. m--; // adjust the position to set
  10940. n++; // next digit to process
  10941. }
  10942. return (intValue);
  10943. }
  10944. // [Lance] Hex string to integer converter
  10945. BUILDIN_FUNC(axtoi)
  10946. {
  10947. const char *hex = script_getstr(st,2);
  10948. script_pushint(st,axtoi(hex));
  10949. return 0;
  10950. }
  10951. // [zBuffer] List of player cont commands --->
  10952. BUILDIN_FUNC(rid2name)
  10953. {
  10954. struct block_list *bl = NULL;
  10955. int rid = script_getnum(st,2);
  10956. if((bl = map_id2bl(rid)))
  10957. {
  10958. switch(bl->type) {
  10959. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  10960. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  10961. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  10962. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  10963. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  10964. default:
  10965. ShowError("buildin_rid2name: BL type unknown.\n");
  10966. script_pushconststr(st,"");
  10967. break;
  10968. }
  10969. } else {
  10970. ShowError("buildin_rid2name: invalid RID\n");
  10971. script_pushconststr(st,"(null)");
  10972. }
  10973. return 0;
  10974. }
  10975. BUILDIN_FUNC(pcblockmove)
  10976. {
  10977. int id, flag;
  10978. TBL_PC *sd = NULL;
  10979. id = script_getnum(st,2);
  10980. flag = script_getnum(st,3);
  10981. if(id)
  10982. sd = map_id2sd(id);
  10983. else
  10984. sd = script_rid2sd(st);
  10985. if(sd)
  10986. sd->state.blockedmove = flag > 0;
  10987. return 0;
  10988. }
  10989. BUILDIN_FUNC(pcfollow)
  10990. {
  10991. int id, targetid;
  10992. TBL_PC *sd = NULL;
  10993. id = script_getnum(st,2);
  10994. targetid = script_getnum(st,3);
  10995. if(id)
  10996. sd = map_id2sd(id);
  10997. else
  10998. sd = script_rid2sd(st);
  10999. if(sd)
  11000. pc_follow(sd, targetid);
  11001. return 0;
  11002. }
  11003. BUILDIN_FUNC(pcstopfollow)
  11004. {
  11005. int id;
  11006. TBL_PC *sd = NULL;
  11007. id = script_getnum(st,2);
  11008. if(id)
  11009. sd = map_id2sd(id);
  11010. else
  11011. sd = script_rid2sd(st);
  11012. if(sd)
  11013. pc_stop_following(sd);
  11014. return 0;
  11015. }
  11016. // <--- [zBuffer] List of player cont commands
  11017. // [zBuffer] List of mob control commands --->
  11018. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11019. BUILDIN_FUNC(mobspawn)
  11020. {
  11021. int class_,x,y,id;
  11022. const char *str,*map;
  11023. // Who?
  11024. str =script_getstr(st,2);
  11025. // What?
  11026. class_ =script_getnum(st,3);
  11027. // Where?
  11028. map =script_getstr(st,4);
  11029. x =script_getnum(st,5);
  11030. y =script_getnum(st,6);
  11031. id = mob_once_spawn(map_id2sd(st->rid),map_mapname2mapid(map),x,y,str,class_,1,"");
  11032. script_pushint(st,id);
  11033. return 0;
  11034. }
  11035. BUILDIN_FUNC(mobremove)
  11036. {
  11037. int id;
  11038. struct block_list *bl = NULL;
  11039. id = script_getnum(st,2);
  11040. bl = map_id2bl(id);
  11041. if (bl && bl->type == BL_MOB)
  11042. unit_free(bl,0);
  11043. return 0;
  11044. }
  11045. BUILDIN_FUNC(getmobdata)
  11046. {
  11047. int num, id;
  11048. char *name;
  11049. struct mob_data *md = NULL;
  11050. TBL_PC *sd = st->rid?map_id2sd(st->rid):NULL;
  11051. id = script_getnum(st,2);
  11052. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || !data_isreference(script_getdata(st,3)) ){
  11053. ShowWarning("buildin_getmobdata: Error in argument!\n");
  11054. return -1;
  11055. }
  11056. num=st->stack->stack_data[st->start+3].u.num;
  11057. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  11058. setd_sub(st,sd,name,0,(void *)(int)md->class_,script_getref(st,3));
  11059. setd_sub(st,sd,name,1,(void *)(int)md->level,script_getref(st,3));
  11060. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,script_getref(st,3));
  11061. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,script_getref(st,3));
  11062. setd_sub(st,sd,name,4,(void *)(int)md->master_id,script_getref(st,3));
  11063. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,script_getref(st,3));
  11064. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,script_getref(st,3));
  11065. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,script_getref(st,3));
  11066. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,script_getref(st,3));
  11067. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,script_getref(st,3));
  11068. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,script_getref(st,3));
  11069. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,script_getref(st,3));
  11070. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,script_getref(st,3));
  11071. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,script_getref(st,3));
  11072. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,script_getref(st,3));
  11073. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,script_getref(st,3));
  11074. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,script_getref(st,3));
  11075. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,script_getref(st,3));
  11076. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,script_getref(st,3));
  11077. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,script_getref(st,3));
  11078. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,script_getref(st,3));
  11079. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,script_getref(st,3));
  11080. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,script_getref(st,3));
  11081. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,script_getref(st,3));
  11082. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,script_getref(st,3));
  11083. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,script_getref(st,3));
  11084. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, script_getref(st,3));
  11085. return 0;
  11086. }
  11087. /// Changes the data of a mob
  11088. ///
  11089. /// setmobdata <mob unit id>,<type>,<value>;
  11090. BUILDIN_FUNC(setmobdata)
  11091. {
  11092. struct block_list* mob_bl;
  11093. mob_bl = map_id2bl(script_getnum(st,2));
  11094. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11095. {
  11096. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11097. int type;
  11098. int value;
  11099. type = script_getnum(st,3);
  11100. value = script_getnum(st,4);
  11101. switch( type )
  11102. {
  11103. case 0: md->class_ = (short)value; break;
  11104. case 1: md->level = (unsigned short)value; break;
  11105. case 2: md->status.hp = (unsigned int)value; break;
  11106. case 3: md->status.max_hp = (unsigned int)value; break;
  11107. case 4: md->master_id = value; break;
  11108. case 5: md->bl.m = (short)value; break;
  11109. case 6: md->bl.x = (short)value; break;
  11110. case 7: md->bl.y = (short)value; break;
  11111. case 8: md->status.speed = (unsigned short)value; break;
  11112. case 9: md->status.mode = (unsigned short)value; break;
  11113. case 10: md->special_state.ai = (unsigned int)value; break;
  11114. case 11: md->sc.option = (unsigned short)value; break;
  11115. case 12: md->vd->sex = (char)value; break;
  11116. case 13: md->vd->class_ = (unsigned short)value; break;
  11117. case 14: md->vd->hair_style = (unsigned short)value; break;
  11118. case 15: md->vd->hair_color = (unsigned short)value; break;
  11119. case 16: md->vd->head_bottom = (unsigned short)value; break;
  11120. case 17: md->vd->head_mid = (unsigned short)value; break;
  11121. case 18: md->vd->head_top = (unsigned short)value; break;
  11122. case 19: md->vd->cloth_color = (unsigned short)value; break;
  11123. case 20: md->vd->shield = (unsigned short)value; break;
  11124. case 21: md->vd->weapon = (unsigned short)value; break;
  11125. case 22: md->vd->shield = (unsigned short)value; break;
  11126. case 23: md->ud.dir = (unsigned char)value; break;
  11127. case 24: md->state.killer = value > 0 ? 1 : 0; break;
  11128. case 25: md->callback_flag = (short)value; break;
  11129. case 26: md->state.no_random_walk = value > 0 ? 1 : 0; break;
  11130. default:
  11131. ShowError("script:setmobdata: unknown data identifier %d\n", type);
  11132. return 1;
  11133. }
  11134. }
  11135. return 0;
  11136. }
  11137. /// Makes the mob assist the target unit as a slave
  11138. ///
  11139. /// mobassist <mob unit id>,"<player name>";
  11140. /// mobassist <mob unit id>,<target id>;
  11141. BUILDIN_FUNC(mobassist)
  11142. {
  11143. struct block_list* mob_bl;
  11144. // get mob
  11145. mob_bl = map_id2bl(script_getnum(st,2));
  11146. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11147. {
  11148. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11149. struct block_list* target_bl = NULL;
  11150. struct script_data* data;
  11151. // get target
  11152. data = script_getdata(st, 3);
  11153. get_val(st, data);
  11154. if( data_isstring(data) )
  11155. {
  11156. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11157. if( sd != NULL )
  11158. target_bl = &sd->bl;
  11159. }
  11160. if( target_bl == NULL )
  11161. target_bl = map_id2bl(conv_num(st, data));
  11162. // set mob as slave
  11163. if( target_bl != NULL )
  11164. {
  11165. struct unit_data* ud;
  11166. md->master_id = target_bl->id;
  11167. md->state.killer = 1;
  11168. mob_convertslave(md);
  11169. ud = unit_bl2ud(mob_bl);
  11170. if( ud != NULL )
  11171. {
  11172. if( ud->target != 0 )
  11173. md->target_id = ud->target;
  11174. else if( ud->skilltarget != 0 )
  11175. md->target_id = ud->skilltarget;
  11176. if( md->target_id != 0 )
  11177. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  11178. }
  11179. }
  11180. }
  11181. return 0;
  11182. }
  11183. /// Attaches the current npc or the target npc to the mob unit
  11184. ///
  11185. /// mobattach <mob unit id>{,"<npc name>"};
  11186. BUILDIN_FUNC(mobattach)
  11187. {
  11188. struct block_list* mob_bl;
  11189. mob_bl = map_id2bl(script_getnum(st,2));
  11190. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11191. {
  11192. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11193. TBL_NPC* nd = NULL;
  11194. if( script_hasdata(st,3) )
  11195. nd = npc_name2id(script_getstr(st, 3));
  11196. else
  11197. {
  11198. struct block_list* npc_bl = map_id2bl(st->oid);
  11199. if( npc_bl != NULL && npc_bl->type == BL_NPC )
  11200. nd = (TBL_NPC*)npc_bl;
  11201. }
  11202. if( nd != NULL )
  11203. md->nd = nd;
  11204. }
  11205. return 0;
  11206. }
  11207. /// Makes the unit walk to target position or map
  11208. /// Returns if it was successfull
  11209. ///
  11210. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11211. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11212. BUILDIN_FUNC(unitwalk)
  11213. {
  11214. struct block_list* bl;
  11215. bl = map_id2bl(script_getnum(st,2));
  11216. if( bl == NULL )
  11217. {
  11218. script_pushint(st, 0);
  11219. }
  11220. else if( script_hasdata(st,4) )
  11221. {
  11222. int x = script_getnum(st,3);
  11223. int y = script_getnum(st,4);
  11224. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11225. }
  11226. else
  11227. {
  11228. int map_id = script_getnum(st,3);
  11229. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11230. }
  11231. return 0;
  11232. }
  11233. /// Kills the unit
  11234. ///
  11235. /// unitkill <unit_id>;
  11236. BUILDIN_FUNC(unitkill)
  11237. {
  11238. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11239. if( bl != NULL )
  11240. status_kill(bl);
  11241. return 0;
  11242. }
  11243. /// Warps the unit to the target position in the target map
  11244. /// Returns if it was successfull
  11245. ///
  11246. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11247. BUILDIN_FUNC(unitwarp)
  11248. {
  11249. int unit_id;
  11250. int map;
  11251. short x;
  11252. short y;
  11253. struct block_list* bl;
  11254. unit_id = script_getnum(st,2);
  11255. map = map_mapname2mapid(script_getstr(st, 3));
  11256. x = (short)script_getnum(st,4);
  11257. y = (short)script_getnum(st,5);
  11258. bl = map_id2bl(unit_id);
  11259. if( map >= 0 && bl != NULL )
  11260. script_pushint(st, unit_warp(bl,map,x,y,0));
  11261. else
  11262. script_pushint(st, 0);
  11263. return 0;
  11264. }
  11265. /// Makes the unit attack the target.
  11266. /// If the unit is a player and <action type> is not 0, it does a continuous
  11267. /// attack instead of a single attack.
  11268. /// Returns if the request was successfull.
  11269. ///
  11270. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11271. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11272. BUILDIN_FUNC(unitattack)
  11273. {
  11274. struct block_list* unit_bl;
  11275. struct block_list* target_bl = NULL;
  11276. struct script_data* data;
  11277. int actiontype = 0;
  11278. // get unit
  11279. unit_bl = map_id2bl(script_getnum(st,2));
  11280. if( unit_bl == NULL ) {
  11281. script_pushint(st, 0);
  11282. return 0;
  11283. }
  11284. data = script_getdata(st, 3);
  11285. get_val(st, data);
  11286. if( data_isstring(data) )
  11287. {
  11288. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11289. if( sd != NULL )
  11290. target_bl = &sd->bl;
  11291. } else
  11292. target_bl = map_id2bl(conv_num(st, data));
  11293. // request the attack
  11294. if( target_bl == NULL )
  11295. {
  11296. script_pushint(st, 0);
  11297. return 0;
  11298. }
  11299. // get actiontype
  11300. if( script_hasdata(st,4) )
  11301. actiontype = script_getnum(st,4);
  11302. switch( unit_bl->type )
  11303. {
  11304. case BL_PC:
  11305. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11306. script_pushint(st, 1);
  11307. return 0;
  11308. case BL_MOB:
  11309. ((TBL_MOB *)unit_bl)->state.killer = 1;
  11310. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11311. break;
  11312. case BL_PET:
  11313. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11314. break;
  11315. default:
  11316. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11317. script_pushint(st, 0);
  11318. return 1;
  11319. }
  11320. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11321. return 0;
  11322. }
  11323. /// Makes the unit stop attacking and moving
  11324. ///
  11325. /// unitstop <unit_id>;
  11326. BUILDIN_FUNC(unitstop)
  11327. {
  11328. int unit_id;
  11329. struct block_list* bl;
  11330. unit_id = script_getnum(st,2);
  11331. bl = map_id2bl(unit_id);
  11332. if( bl != NULL )
  11333. {
  11334. unit_stop_attack(bl);
  11335. unit_stop_walking(bl,0);
  11336. if( bl->type == BL_MOB )
  11337. ((TBL_MOB*)bl)->target_id = 0;
  11338. }
  11339. return 0;
  11340. }
  11341. /// Makes the unit say the message
  11342. ///
  11343. /// unittalk <unit_id>,"<message>";
  11344. BUILDIN_FUNC(unittalk)
  11345. {
  11346. int unit_id;
  11347. const char* message;
  11348. struct block_list* bl;
  11349. unit_id = script_getnum(st,2);
  11350. message = script_getstr(st, 3);
  11351. bl = map_id2bl(unit_id);
  11352. if( bl != NULL )
  11353. {
  11354. struct StringBuf sbuf;
  11355. StringBuf_Init(&sbuf);
  11356. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11357. clif_message(bl, StringBuf_Value(&sbuf));
  11358. if( bl->type == BL_PC )
  11359. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11360. StringBuf_Destroy(&sbuf);
  11361. }
  11362. return 0;
  11363. }
  11364. /// Makes the unit do an emotion
  11365. ///
  11366. /// unitemote <unit_id>,<emotion>;
  11367. ///
  11368. /// @see e_* in const.txt
  11369. BUILDIN_FUNC(unitemote)
  11370. {
  11371. int unit_id;
  11372. int emotion;
  11373. struct block_list* bl;
  11374. unit_id = script_getnum(st,2);
  11375. emotion = script_getnum(st,3);
  11376. bl = map_id2bl(unit_id);
  11377. if( bl != NULL )
  11378. clif_emotion(bl, emotion);
  11379. return 0;
  11380. }
  11381. /// Makes the unit cast the skill on the target or self if no target is specified
  11382. ///
  11383. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11384. BUILDIN_FUNC(unitskilluseid)
  11385. {
  11386. int unit_id;
  11387. int skill_id;
  11388. int skill_lv;
  11389. int target_id;
  11390. struct block_list* bl;
  11391. unit_id = script_getnum(st,2);
  11392. skill_id = script_getnum(st,3);
  11393. skill_lv = script_getnum(st,4);
  11394. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11395. bl = map_id2bl(unit_id);
  11396. if( bl != NULL )
  11397. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11398. return 0;
  11399. }
  11400. /// Makes the unit cast the skill on the target position.
  11401. ///
  11402. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11403. BUILDIN_FUNC(unitskillusepos)
  11404. {
  11405. int unit_id;
  11406. int skill_id;
  11407. int skill_lv;
  11408. int skill_x;
  11409. int skill_y;
  11410. struct block_list* bl;
  11411. unit_id = script_getnum(st,2);
  11412. skill_id = script_getnum(st,3);
  11413. skill_lv = script_getnum(st,4);
  11414. skill_x = script_getnum(st,5);
  11415. skill_y = script_getnum(st,6);
  11416. bl = map_id2bl(unit_id);
  11417. if( bl != NULL )
  11418. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11419. return 0;
  11420. }
  11421. // <--- [zBuffer] List of mob control commands
  11422. /// Pauses the execution of the script, detaching the player
  11423. ///
  11424. /// sleep <mili seconds>;
  11425. BUILDIN_FUNC(sleep)
  11426. {
  11427. int ticks;
  11428. TBL_PC* sd;
  11429. ticks = script_getnum(st,2);
  11430. sd = map_id2sd(st->rid);
  11431. // detach the player
  11432. if( sd && sd->npc_id == st->oid )
  11433. {
  11434. sd->npc_id = 0;
  11435. }
  11436. st->rid = 0;
  11437. if( ticks <= 0 )
  11438. {// do nothing
  11439. }
  11440. else if( st->sleep.tick == 0 )
  11441. {// sleep for the target amount of time
  11442. st->state = RERUNLINE;
  11443. st->sleep.tick = ticks;
  11444. }
  11445. else
  11446. {// sleep time is over
  11447. st->sleep.tick = 0;
  11448. }
  11449. return 0;
  11450. }
  11451. /// Pauses the execution of the script, keeping the player attached
  11452. /// Returns if a player is still attached
  11453. ///
  11454. /// sleep2(<mili secconds>) -> <bool>
  11455. BUILDIN_FUNC(sleep2)
  11456. {
  11457. int ticks;
  11458. ticks = script_getnum(st,2);
  11459. if( ticks <= 0 )
  11460. {// do nothing
  11461. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11462. }
  11463. else if( !st->sleep.tick )
  11464. {// sleep for the target amount of time
  11465. st->state = RERUNLINE;
  11466. st->sleep.tick = ticks;
  11467. }
  11468. else
  11469. {// sleep time is over
  11470. st->sleep.tick = 0;
  11471. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11472. }
  11473. return 0;
  11474. }
  11475. /// Awakes all the sleep timers of the target npc
  11476. ///
  11477. /// awake "<npc name>";
  11478. BUILDIN_FUNC(awake)
  11479. {
  11480. struct npc_data* nd;
  11481. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11482. nd = npc_name2id(script_getstr(st, 2));
  11483. if( nd == NULL )
  11484. return 0;
  11485. while( node )
  11486. {
  11487. if( (int)node->key == nd->bl.id )
  11488. {// sleep timer for the npc
  11489. struct script_state* tst = (struct script_state*)node->data;
  11490. TBL_PC* sd = map_id2sd(tst->rid);
  11491. if( tst->sleep.timer == -1 )
  11492. {// already awake ???
  11493. node = node->next;
  11494. continue;
  11495. }
  11496. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11497. {// char not online anymore / another char of the same account is online - Cancel execution
  11498. tst->state = END;
  11499. tst->rid = 0;
  11500. }
  11501. delete_timer(tst->sleep.timer, run_script_timer);
  11502. node = script_erase_sleepdb(node);
  11503. tst->sleep.timer = -1;
  11504. tst->sleep.tick = 0;
  11505. run_script_main(tst);
  11506. }
  11507. else
  11508. {
  11509. node = node->next;
  11510. }
  11511. }
  11512. return 0;
  11513. }
  11514. /// Returns a reference to a variable of the target NPC.
  11515. /// Returns 0 if an error occurs.
  11516. ///
  11517. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11518. BUILDIN_FUNC(getvariableofnpc)
  11519. {
  11520. struct script_data* data;
  11521. char* name;
  11522. struct npc_data* nd;
  11523. data = script_getdata(st,2);
  11524. if( !data_isreference(data) )
  11525. {// Not a reference (aka varaible name)
  11526. ShowError("script:getvariableofnpc: not a variable\n");
  11527. script_reportdata(data);
  11528. script_pushnil(st);
  11529. st->state = END;
  11530. return 1;
  11531. }
  11532. name = reference_getname(data);
  11533. if( *name != '.' || name[1] == '@' )
  11534. {// not a npc variable
  11535. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11536. script_reportdata(data);
  11537. script_pushnil(st);
  11538. st->state = END;
  11539. return 1;
  11540. }
  11541. nd = npc_name2id(script_getstr(st,3));
  11542. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11543. {// NPC not found or has no script
  11544. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11545. script_pushnil(st);
  11546. st->state = END;
  11547. return 1;
  11548. }
  11549. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11550. return 0;
  11551. }
  11552. /// Opens a warp portal.
  11553. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11554. ///
  11555. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11556. ///
  11557. /// @author blackhole89
  11558. BUILDIN_FUNC(warpportal)
  11559. {
  11560. int spx;
  11561. int spy;
  11562. unsigned short mapindex;
  11563. int tpx;
  11564. int tpy;
  11565. struct skill_unit_group* group;
  11566. struct block_list* bl;
  11567. bl = map_id2bl(st->oid);
  11568. if( bl == NULL )
  11569. {
  11570. ShowError("script:warpportal: npc is needed\n");
  11571. return 1;
  11572. }
  11573. spx = script_getnum(st,2);
  11574. spy = script_getnum(st,3);
  11575. mapindex = mapindex_name2id(script_getstr(st, 4));
  11576. tpx = script_getnum(st,5);
  11577. tpy = script_getnum(st,6);
  11578. if( mapindex == 0 )
  11579. return 0;// map not found
  11580. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11581. if( group == NULL )
  11582. return 0;// failed
  11583. group->val2 = (tpx<<16) | tpy;
  11584. group->val3 = mapindex;
  11585. return 0;
  11586. }
  11587. BUILDIN_FUNC(openmail)
  11588. {
  11589. TBL_PC* sd;
  11590. sd = script_rid2sd(st);
  11591. if( sd == NULL )
  11592. return 0;
  11593. #ifndef TXT_ONLY
  11594. mail_openmail(sd);
  11595. #endif
  11596. return 0;
  11597. }
  11598. BUILDIN_FUNC(openauction)
  11599. {
  11600. TBL_PC* sd;
  11601. sd = script_rid2sd(st);
  11602. if( sd == NULL )
  11603. return 0;
  11604. #ifndef TXT_ONLY
  11605. clif_Auction_openwindow(sd);
  11606. #endif
  11607. return 0;
  11608. }
  11609. /// Retrieves the value of the specified flag of the specified cell.
  11610. ///
  11611. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  11612. ///
  11613. /// @see cell_chk* constants in const.txt for the types
  11614. BUILDIN_FUNC(checkcell)
  11615. {
  11616. int m = map_mapname2mapid(script_getstr(st,2));
  11617. int x = script_getnum(st,3);
  11618. int y = script_getnum(st,4);
  11619. cell_chk type = (cell_chk)script_getnum(st,5);
  11620. script_pushint(st, map_getcell(m, x, y, type));
  11621. return 0;
  11622. }
  11623. /// Modifies flags of cells in the specified area.
  11624. ///
  11625. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11626. ///
  11627. /// @see cell_* constants in const.txt for the types
  11628. BUILDIN_FUNC(setcell)
  11629. {
  11630. int m = map_mapname2mapid(script_getstr(st,2));
  11631. int x1 = script_getnum(st,3);
  11632. int y1 = script_getnum(st,4);
  11633. int x2 = script_getnum(st,5);
  11634. int y2 = script_getnum(st,6);
  11635. cell_t type = (cell_t)script_getnum(st,7);
  11636. bool flag = (bool)script_getnum(st,8);
  11637. int x,y;
  11638. if( x1 > x2 ) swap(x1,x2);
  11639. if( y1 > y2 ) swap(y1,y2);
  11640. for( y = y1; y <= y2; ++y )
  11641. for( x = x1; x <= x2; ++x )
  11642. map_setcell(m, x, y, type, flag);
  11643. return 0;
  11644. }
  11645. /******************
  11646. Questlog script commands
  11647. *******************/
  11648. BUILDIN_FUNC(getquest)
  11649. {
  11650. TBL_PC * sd = script_rid2sd(st);
  11651. struct quest qd;
  11652. int i, count = 0;
  11653. char * temp;
  11654. memset(&qd, 0, sizeof(struct quest));
  11655. qd.quest_id = script_getnum(st, 2);
  11656. qd.time = script_getnum(st, 3);
  11657. qd.state = Q_ACTIVE;
  11658. for(i=0; i<(script_lastdata(st)-3) && (i/2) < MAX_QUEST_OBJECTIVES; i+=2)
  11659. {
  11660. temp = (char*)script_getstr(st, i+4);
  11661. memcpy(&qd.objectives[i/2].name, temp, NAME_LENGTH);
  11662. temp = NULL;
  11663. qd.objectives[i/2].count = script_getnum(st, i+5);
  11664. count++;
  11665. }
  11666. qd.num_objectives = count;
  11667. quest_add(sd, &qd);
  11668. return 0;
  11669. }
  11670. BUILDIN_FUNC(deletequest)
  11671. {
  11672. TBL_PC * sd = script_rid2sd(st);
  11673. int qid = script_getnum(st, 2);
  11674. quest_delete(sd, qid);
  11675. return 0;
  11676. }
  11677. BUILDIN_FUNC(setquestobjective)
  11678. {
  11679. TBL_PC * sd = script_rid2sd(st);
  11680. int qid = script_getnum(st, 2);
  11681. int num = script_getnum(st, 3);
  11682. const char * str = script_getstr(st, 4);
  11683. int count = script_getnum(st, 5);
  11684. quest_update_objective(sd, qid, num, str, count);
  11685. return 0;
  11686. }
  11687. BUILDIN_FUNC(hasquest)
  11688. {
  11689. TBL_PC * sd = script_rid2sd(st);
  11690. int qid = script_getnum(st, 2);
  11691. script_pushint(st, quest_has_quest(sd, qid)?1:0);
  11692. return 0;
  11693. }
  11694. BUILDIN_FUNC(setqueststatus)
  11695. {
  11696. TBL_PC * sd = script_rid2sd(st);
  11697. int qid = script_getnum(st, 2);
  11698. bool active = script_getnum(st, 3)?true:false;
  11699. quest_update_status(sd, qid, active);
  11700. return 0;
  11701. }
  11702. // declarations that were supposed to be exported from npc_chat.c
  11703. #ifdef PCRE_SUPPORT
  11704. BUILDIN_FUNC(defpattern);
  11705. BUILDIN_FUNC(activatepset);
  11706. BUILDIN_FUNC(deactivatepset);
  11707. BUILDIN_FUNC(deletepset);
  11708. #endif
  11709. struct script_function buildin_func[] = {
  11710. // NPC interaction
  11711. BUILDIN_DEF(mes,"s"),
  11712. BUILDIN_DEF(next,""),
  11713. BUILDIN_DEF(close,""),
  11714. BUILDIN_DEF(close2,""),
  11715. BUILDIN_DEF(menu,"sl*"),
  11716. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  11717. BUILDIN_DEF(prompt,"s*"),
  11718. //
  11719. BUILDIN_DEF(goto,"l"),
  11720. BUILDIN_DEF(callsub,"i*"),
  11721. BUILDIN_DEF(callfunc,"s*"),
  11722. BUILDIN_DEF(return,"?"),
  11723. BUILDIN_DEF(getarg,"i?"),
  11724. BUILDIN_DEF(jobchange,"i*"),
  11725. BUILDIN_DEF(jobname,"i"),
  11726. BUILDIN_DEF(input,"v??"),
  11727. BUILDIN_DEF(warp,"sii"),
  11728. BUILDIN_DEF(areawarp,"siiiisii"),
  11729. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  11730. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  11731. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  11732. BUILDIN_DEF(setlook,"ii"),
  11733. BUILDIN_DEF(set,"ii"),
  11734. BUILDIN_DEF(setarray,"rv*"),
  11735. BUILDIN_DEF(cleararray,"rvi"),
  11736. BUILDIN_DEF(copyarray,"rri"),
  11737. BUILDIN_DEF(getarraysize,"r"),
  11738. BUILDIN_DEF(deletearray,"r?"),
  11739. BUILDIN_DEF(getelementofarray,"ri"),
  11740. BUILDIN_DEF(getitem,"vi?"),
  11741. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  11742. BUILDIN_DEF(getnameditem,"is"),
  11743. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  11744. BUILDIN_DEF(makeitem,"iisii"),
  11745. BUILDIN_DEF(delitem,"ii"),
  11746. BUILDIN_DEF(delitem2,"iiiiiiiii"),
  11747. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  11748. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  11749. BUILDIN_DEF(cutin,"si"),
  11750. BUILDIN_DEF(viewpoint,"iiiii"),
  11751. BUILDIN_DEF(heal,"ii"),
  11752. BUILDIN_DEF(itemheal,"ii"),
  11753. BUILDIN_DEF(percentheal,"ii"),
  11754. BUILDIN_DEF(rand,"i?"),
  11755. BUILDIN_DEF(countitem,"i"),
  11756. BUILDIN_DEF(countitem2,"iiiiiiii"),
  11757. BUILDIN_DEF(checkweight,"ii"),
  11758. BUILDIN_DEF(readparam,"i*"),
  11759. BUILDIN_DEF(getcharid,"i*"),
  11760. BUILDIN_DEF(getpartyname,"i"),
  11761. BUILDIN_DEF(getpartymember,"i*"),
  11762. BUILDIN_DEF(getpartyleader,"i?"),
  11763. BUILDIN_DEF(getguildname,"i"),
  11764. BUILDIN_DEF(getguildmaster,"i"),
  11765. BUILDIN_DEF(getguildmasterid,"i"),
  11766. BUILDIN_DEF(strcharinfo,"i"),
  11767. BUILDIN_DEF(strnpcinfo,"i"),
  11768. BUILDIN_DEF(getequipid,"i"),
  11769. BUILDIN_DEF(getequipname,"i"),
  11770. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  11771. BUILDIN_DEF(repair,"i"), // [Valaris]
  11772. BUILDIN_DEF(getequipisequiped,"i"),
  11773. BUILDIN_DEF(getequipisenableref,"i"),
  11774. BUILDIN_DEF(getequipisidentify,"i"),
  11775. BUILDIN_DEF(getequiprefinerycnt,"i"),
  11776. BUILDIN_DEF(getequipweaponlv,"i"),
  11777. BUILDIN_DEF(getequippercentrefinery,"i"),
  11778. BUILDIN_DEF(successrefitem,"i"),
  11779. BUILDIN_DEF(failedrefitem,"i"),
  11780. BUILDIN_DEF(statusup,"i"),
  11781. BUILDIN_DEF(statusup2,"ii"),
  11782. BUILDIN_DEF(bonus,"ii"),
  11783. BUILDIN_DEF2(bonus,"bonus2","iii"),
  11784. BUILDIN_DEF2(bonus,"bonus3","iiii"),
  11785. BUILDIN_DEF2(bonus,"bonus4","iiiii"),
  11786. BUILDIN_DEF2(bonus,"bonus5","iiiiii"),
  11787. BUILDIN_DEF(bonusautoscript,"si?"),
  11788. BUILDIN_DEF(bonusautoscript2,"si?"),
  11789. BUILDIN_DEF(skill,"ii?"),
  11790. BUILDIN_DEF(addtoskill,"ii?"), // [Valaris]
  11791. BUILDIN_DEF(guildskill,"ii"),
  11792. BUILDIN_DEF(getskilllv,"i"),
  11793. BUILDIN_DEF(getgdskilllv,"ii"),
  11794. BUILDIN_DEF(basicskillcheck,""),
  11795. BUILDIN_DEF(getgmlevel,""),
  11796. BUILDIN_DEF(end,""),
  11797. BUILDIN_DEF(checkoption,"i"),
  11798. BUILDIN_DEF(setoption,"i?"),
  11799. BUILDIN_DEF(setcart,"?"),
  11800. BUILDIN_DEF(checkcart,""),
  11801. BUILDIN_DEF(setfalcon,"?"),
  11802. BUILDIN_DEF(checkfalcon,""),
  11803. BUILDIN_DEF(setriding,"?"),
  11804. BUILDIN_DEF(checkriding,""),
  11805. BUILDIN_DEF2(savepoint,"save","sii"),
  11806. BUILDIN_DEF(savepoint,"sii"),
  11807. BUILDIN_DEF(gettimetick,"i"),
  11808. BUILDIN_DEF(gettime,"i"),
  11809. BUILDIN_DEF(gettimestr,"si"),
  11810. BUILDIN_DEF(openstorage,""),
  11811. BUILDIN_DEF(guildopenstorage,"*"),
  11812. BUILDIN_DEF(itemskill,"ii"),
  11813. BUILDIN_DEF(produce,"i"),
  11814. BUILDIN_DEF(monster,"siisii*"),
  11815. BUILDIN_DEF(areamonster,"siiiisii*"),
  11816. BUILDIN_DEF(killmonster,"ss"),
  11817. BUILDIN_DEF(killmonsterall,"s"),
  11818. BUILDIN_DEF(clone,"siisi*"),
  11819. BUILDIN_DEF(doevent,"s"),
  11820. BUILDIN_DEF(donpcevent,"s"),
  11821. BUILDIN_DEF(cmdothernpc,"ss"),
  11822. BUILDIN_DEF(addtimer,"is"),
  11823. BUILDIN_DEF(deltimer,"s"),
  11824. BUILDIN_DEF(addtimercount,"si"),
  11825. BUILDIN_DEF(initnpctimer,"??"),
  11826. BUILDIN_DEF(stopnpctimer,"??"),
  11827. BUILDIN_DEF(startnpctimer,"??"),
  11828. BUILDIN_DEF(setnpctimer,"i?"),
  11829. BUILDIN_DEF(getnpctimer,"i?"),
  11830. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  11831. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  11832. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  11833. BUILDIN_DEF(announce,"si*"),
  11834. BUILDIN_DEF(mapannounce,"ssi*"),
  11835. BUILDIN_DEF(areaannounce,"siiiisi*"),
  11836. BUILDIN_DEF(getusers,"i"),
  11837. BUILDIN_DEF(getmapguildusers,"si"),
  11838. BUILDIN_DEF(getmapusers,"s"),
  11839. BUILDIN_DEF(getareausers,"siiii"),
  11840. BUILDIN_DEF(getareadropitem,"siiiii"),
  11841. BUILDIN_DEF(enablenpc,"s"),
  11842. BUILDIN_DEF(disablenpc,"s"),
  11843. BUILDIN_DEF(hideoffnpc,"s"),
  11844. BUILDIN_DEF(hideonnpc,"s"),
  11845. BUILDIN_DEF(sc_start,"iii?"),
  11846. BUILDIN_DEF(sc_start2,"iiii?"),
  11847. BUILDIN_DEF(sc_start4,"iiiiii?"),
  11848. BUILDIN_DEF(sc_end,"i?"),
  11849. BUILDIN_DEF(getscrate,"ii*"),
  11850. BUILDIN_DEF(debugmes,"s"),
  11851. BUILDIN_DEF2(catchpet,"pet","i"),
  11852. BUILDIN_DEF2(birthpet,"bpet",""),
  11853. BUILDIN_DEF(resetlvl,"i"),
  11854. BUILDIN_DEF(resetstatus,""),
  11855. BUILDIN_DEF(resetskill,""),
  11856. BUILDIN_DEF(skillpointcount,""),
  11857. BUILDIN_DEF(changebase,"i"),
  11858. BUILDIN_DEF(changesex,""),
  11859. BUILDIN_DEF(waitingroom,"si??"),
  11860. BUILDIN_DEF(delwaitingroom,"?"),
  11861. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  11862. BUILDIN_DEF(enablewaitingroomevent,"?"),
  11863. BUILDIN_DEF(disablewaitingroomevent,"?"),
  11864. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  11865. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  11866. BUILDIN_DEF(getwaitingroomstate,"i?"),
  11867. BUILDIN_DEF(warpwaitingpc,"sii?"),
  11868. BUILDIN_DEF(attachrid,"i"),
  11869. BUILDIN_DEF(detachrid,""),
  11870. BUILDIN_DEF(isloggedin,"i?"),
  11871. BUILDIN_DEF(setmapflagnosave,"ssii"),
  11872. BUILDIN_DEF(getmapflag,"si"),
  11873. BUILDIN_DEF(setmapflag,"si*"),
  11874. BUILDIN_DEF(removemapflag,"si"),
  11875. BUILDIN_DEF(pvpon,"s"),
  11876. BUILDIN_DEF(pvpoff,"s"),
  11877. BUILDIN_DEF(gvgon,"s"),
  11878. BUILDIN_DEF(gvgoff,"s"),
  11879. BUILDIN_DEF(emotion,"i*"),
  11880. BUILDIN_DEF(maprespawnguildid,"sii"),
  11881. BUILDIN_DEF(agitstart,""), // <Agit>
  11882. BUILDIN_DEF(agitend,""),
  11883. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  11884. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  11885. BUILDIN_DEF(getcastlename,"s"),
  11886. BUILDIN_DEF(getcastledata,"si*"),
  11887. BUILDIN_DEF(setcastledata,"sii"),
  11888. BUILDIN_DEF(requestguildinfo,"i*"),
  11889. BUILDIN_DEF(getequipcardcnt,"i"),
  11890. BUILDIN_DEF(successremovecards,"i"),
  11891. BUILDIN_DEF(failedremovecards,"ii"),
  11892. BUILDIN_DEF(marriage,"s"),
  11893. BUILDIN_DEF2(wedding_effect,"wedding",""),
  11894. BUILDIN_DEF(divorce,""),
  11895. BUILDIN_DEF(ispartneron,""),
  11896. BUILDIN_DEF(getpartnerid,""),
  11897. BUILDIN_DEF(getchildid,""),
  11898. BUILDIN_DEF(getmotherid,""),
  11899. BUILDIN_DEF(getfatherid,""),
  11900. BUILDIN_DEF(warppartner,"sii"),
  11901. BUILDIN_DEF(getitemname,"i"),
  11902. BUILDIN_DEF(getitemslots,"i"),
  11903. BUILDIN_DEF(makepet,"i"),
  11904. BUILDIN_DEF(getexp,"ii"),
  11905. BUILDIN_DEF(getinventorylist,""),
  11906. BUILDIN_DEF(getskilllist,""),
  11907. BUILDIN_DEF(clearitem,""),
  11908. BUILDIN_DEF(classchange,"ii"),
  11909. BUILDIN_DEF(misceffect,"i"),
  11910. BUILDIN_DEF(soundeffect,"si"),
  11911. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  11912. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  11913. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  11914. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  11915. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  11916. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  11917. BUILDIN_DEF(petloot,"i"), // [Valaris]
  11918. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  11919. BUILDIN_DEF(petskillattack,"iiii"), // [Skotlex]
  11920. BUILDIN_DEF(petskillattack2,"iiiii"), // [Valaris]
  11921. BUILDIN_DEF(petskillsupport,"iiiii"), // [Skotlex]
  11922. BUILDIN_DEF(skilleffect,"ii"), // skill effect [Celest]
  11923. BUILDIN_DEF(npcskilleffect,"iiii"), // npc skill effect [Valaris]
  11924. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  11925. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  11926. BUILDIN_DEF(nude,""), // nude command [Valaris]
  11927. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  11928. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  11929. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  11930. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  11931. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  11932. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  11933. BUILDIN_DEF(mobcount,"ss"),
  11934. BUILDIN_DEF(getlook,"i"),
  11935. BUILDIN_DEF(getsavepoint,"i"),
  11936. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  11937. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  11938. BUILDIN_DEF(npcstop,""), // [Valaris]
  11939. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  11940. BUILDIN_DEF(checkoption1,"i"),
  11941. BUILDIN_DEF(checkoption2,"i"),
  11942. BUILDIN_DEF(guildgetexp,"i"),
  11943. BUILDIN_DEF(guildchangegm,"is"),
  11944. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  11945. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  11946. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  11947. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  11948. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  11949. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  11950. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  11951. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  11952. BUILDIN_DEF(night,""), // sets the server to night time
  11953. BUILDIN_DEF(day,""), // sets the server to day time
  11954. #ifdef PCRE_SUPPORT
  11955. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  11956. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  11957. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  11958. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  11959. #endif
  11960. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  11961. BUILDIN_DEF(getusersname,"*"),
  11962. BUILDIN_DEF(recovery,""),
  11963. BUILDIN_DEF(getpetinfo,"i"),
  11964. BUILDIN_DEF(gethominfo,"i"),
  11965. BUILDIN_DEF(checkequipedcard,"i"),
  11966. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  11967. BUILDIN_DEF(globalmes,"s*"),
  11968. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  11969. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  11970. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  11971. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  11972. BUILDIN_DEF(setnpcdisplay,"sv?"),
  11973. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  11974. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  11975. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  11976. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  11977. // [zBuffer] List of mathematics commands --->
  11978. BUILDIN_DEF(sqrt,"i"),
  11979. BUILDIN_DEF(pow,"ii"),
  11980. BUILDIN_DEF(distance,"iiii"),
  11981. // <--- [zBuffer] List of mathematics commands
  11982. // [zBuffer] List of dynamic var commands --->
  11983. BUILDIN_DEF(getd,"*"),
  11984. BUILDIN_DEF(setd,"*"),
  11985. // <--- [zBuffer] List of dynamic var commands
  11986. BUILDIN_DEF(petstat,"i"),
  11987. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  11988. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  11989. BUILDIN_DEF(npcshopadditem,"sii*"),
  11990. BUILDIN_DEF(npcshopdelitem,"si*"),
  11991. BUILDIN_DEF(npcshopattach,"s?"),
  11992. BUILDIN_DEF(equip,"i"),
  11993. BUILDIN_DEF(autoequip,"ii"),
  11994. BUILDIN_DEF(setbattleflag,"ss"),
  11995. BUILDIN_DEF(getbattleflag,"s"),
  11996. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  11997. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  11998. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  11999. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  12000. BUILDIN_DEF(axtoi,"s"),
  12001. BUILDIN_DEF(query_sql,"s*"),
  12002. BUILDIN_DEF(query_logsql,"s*"),
  12003. BUILDIN_DEF(escape_sql,"s"),
  12004. BUILDIN_DEF(atoi,"s"),
  12005. // [zBuffer] List of player cont commands --->
  12006. BUILDIN_DEF(rid2name,"i"),
  12007. BUILDIN_DEF(pcfollow,"ii"),
  12008. BUILDIN_DEF(pcstopfollow,"i"),
  12009. BUILDIN_DEF(pcblockmove,"ii"),
  12010. // <--- [zBuffer] List of player cont commands
  12011. // [zBuffer] List of mob control commands --->
  12012. BUILDIN_DEF(mobspawn,"*"),
  12013. BUILDIN_DEF(mobremove,"i"),
  12014. BUILDIN_DEF(getmobdata,"i*"),
  12015. BUILDIN_DEF(setmobdata,"iii"),
  12016. BUILDIN_DEF(mobassist,"i?"),
  12017. BUILDIN_DEF(mobattach,"i?"),
  12018. BUILDIN_DEF(unitwalk,"ii?"),
  12019. BUILDIN_DEF(unitkill,"i"),
  12020. BUILDIN_DEF(unitwarp,"isii"),
  12021. BUILDIN_DEF(unitattack,"iv?"),
  12022. BUILDIN_DEF(unitstop,"i"),
  12023. BUILDIN_DEF(unittalk,"is"),
  12024. BUILDIN_DEF(unitemote,"ii"),
  12025. BUILDIN_DEF(unitskilluseid,"iii?"), // originally by Qamera [Celest]
  12026. BUILDIN_DEF(unitskillusepos,"iiiii"), // [Celest]
  12027. // <--- [zBuffer] List of mob control commands
  12028. BUILDIN_DEF(sleep,"i"),
  12029. BUILDIN_DEF(sleep2,"i"),
  12030. BUILDIN_DEF(awake,"s"),
  12031. BUILDIN_DEF(getvariableofnpc,"rs"),
  12032. BUILDIN_DEF(warpportal,"iisii"),
  12033. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  12034. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  12035. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  12036. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  12037. BUILDIN_DEF(checkvending,"*"),
  12038. BUILDIN_DEF(checkchatting,"*"),
  12039. BUILDIN_DEF(openmail,""),
  12040. BUILDIN_DEF(openauction,""),
  12041. BUILDIN_DEF(checkcell,"siii"),
  12042. BUILDIN_DEF(setcell,"siiiiii"),
  12043. BUILDIN_DEF(buildbarricade,"siisiiiiiis"),
  12044. BUILDIN_DEF(killbarricade,"ss"),
  12045. BUILDIN_DEF(getquest, "ii*"),
  12046. BUILDIN_DEF(deletequest, "i"),
  12047. BUILDIN_DEF(setquestobjective, "iisi"),
  12048. BUILDIN_DEF(setqueststatus, "ii"),
  12049. BUILDIN_DEF(hasquest, "i"),
  12050. {NULL,NULL,NULL},
  12051. };