script.c 329 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469
  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. p++;
  848. else if( *p == '\n' )
  849. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  850. add_scriptb(*p++);
  851. }
  852. if(!*p)
  853. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  854. add_scriptb(0);
  855. p++; //'"'
  856. } else {
  857. int l;
  858. // label , register , function etc
  859. if(skip_word(p)==p)
  860. disp_error_message("parse_simpleexpr: unexpected character",p);
  861. l=add_word(p);
  862. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  863. return parse_callfunc(p,1);
  864. p=skip_word(p);
  865. if( *p == '[' ){
  866. // array(name[i] => getelementofarray(name,i) )
  867. add_scriptl(search_str("getelementofarray"));
  868. add_scriptc(C_ARG);
  869. add_scriptl(l);
  870. p=parse_subexpr(p+1,-1);
  871. p=skip_space(p);
  872. if( *p != ']' )
  873. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  874. ++p;
  875. add_scriptc(C_FUNC);
  876. }else
  877. add_scriptl(l);
  878. }
  879. #ifdef DEBUG_FUNCIN
  880. if(battle_config.etc_log)
  881. ShowDebug("parse_simpleexpr end %s\n",p);
  882. #endif
  883. return p;
  884. }
  885. /*==========================================
  886. * 式の解析
  887. *------------------------------------------*/
  888. const char* parse_subexpr(const char* p,int limit)
  889. {
  890. int op,opl,len;
  891. const char* tmpp;
  892. #ifdef DEBUG_FUNCIN
  893. if(battle_config.etc_log)
  894. ShowDebug("parse_subexpr %s\n",p);
  895. #endif
  896. p=skip_space(p);
  897. if(*p=='-'){
  898. tmpp=skip_space(p+1);
  899. if(*tmpp==';' || *tmpp==','){
  900. add_scriptl(LABEL_NEXTLINE);
  901. p++;
  902. return p;
  903. }
  904. }
  905. tmpp=p;
  906. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  907. p=parse_subexpr(p+1,10);
  908. add_scriptc(op);
  909. } else
  910. p=parse_simpleexpr(p);
  911. p=skip_space(p);
  912. while((
  913. (op=C_OP3,opl=0,len=1,*p=='?') ||
  914. (op=C_ADD,opl=8,len=1,*p=='+') ||
  915. (op=C_SUB,opl=8,len=1,*p=='-') ||
  916. (op=C_MUL,opl=9,len=1,*p=='*') ||
  917. (op=C_DIV,opl=9,len=1,*p=='/') ||
  918. (op=C_MOD,opl=9,len=1,*p=='%') ||
  919. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  920. (op=C_AND,opl=6,len=1,*p=='&') ||
  921. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  922. (op=C_OR,opl=5,len=1,*p=='|') ||
  923. (op=C_XOR,opl=4,len=1,*p=='^') ||
  924. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  925. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  926. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  927. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  928. (op=C_GT,opl=3,len=1,*p=='>') ||
  929. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  930. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  931. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  932. p+=len;
  933. if(op == C_OP3) {
  934. p=parse_subexpr(p,-1);
  935. p=skip_space(p);
  936. if( *(p++) != ':')
  937. disp_error_message("parse_subexpr: need ':'", p-1);
  938. p=parse_subexpr(p,-1);
  939. } else {
  940. p=parse_subexpr(p,opl);
  941. }
  942. add_scriptc(op);
  943. p=skip_space(p);
  944. }
  945. #ifdef DEBUG_FUNCIN
  946. if(battle_config.etc_log)
  947. ShowDebug("parse_subexpr end %s\n",p);
  948. #endif
  949. return p; /* return first untreated operator */
  950. }
  951. /*==========================================
  952. * 式の評価
  953. *------------------------------------------*/
  954. const char* parse_expr(const char *p)
  955. {
  956. #ifdef DEBUG_FUNCIN
  957. if(battle_config.etc_log)
  958. ShowDebug("parse_expr %s\n",p);
  959. #endif
  960. switch(*p){
  961. case ')': case ';': case ':': case '[': case ']':
  962. case '}':
  963. disp_error_message("parse_expr: unexpected char",p);
  964. }
  965. p=parse_subexpr(p,-1);
  966. #ifdef DEBUG_FUNCIN
  967. if(battle_config.etc_log)
  968. ShowDebug("parse_expr end %s\n",p);
  969. #endif
  970. return p;
  971. }
  972. /*==========================================
  973. * 行の解析
  974. *------------------------------------------*/
  975. const char* parse_line(const char* p)
  976. {
  977. const char* p2;
  978. p=skip_space(p);
  979. if(*p==';') {
  980. // if(); for(); while(); のために閉じ判定
  981. p = parse_syntax_close(p + 1);
  982. return p;
  983. }
  984. if(*p==')' && parse_syntax_for_flag)
  985. return p+1;
  986. p = skip_space(p);
  987. if(p[0] == '{') {
  988. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  989. syntax.curly[syntax.curly_count].count = -1;
  990. syntax.curly[syntax.curly_count].index = -1;
  991. syntax.curly_count++;
  992. return p + 1;
  993. } else if(p[0] == '}') {
  994. return parse_curly_close(p);
  995. }
  996. // 構文関連の処理
  997. p2 = parse_syntax(p);
  998. if(p2 != NULL)
  999. return p2;
  1000. p = parse_callfunc(p,0);
  1001. p = skip_space(p);
  1002. if(parse_syntax_for_flag) {
  1003. if( *p != ')' )
  1004. disp_error_message("parse_line: need ')'",p);
  1005. } else {
  1006. if( *p != ';' )
  1007. disp_error_message("parse_line: need ';'",p);
  1008. }
  1009. // if, for , while の閉じ判定
  1010. p = parse_syntax_close(p+1);
  1011. return p;
  1012. }
  1013. // { ... } の閉じ処理
  1014. const char* parse_curly_close(const char* p)
  1015. {
  1016. if(syntax.curly_count <= 0) {
  1017. disp_error_message("parse_curly_close: unexpected string",p);
  1018. return p + 1;
  1019. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1020. syntax.curly_count--;
  1021. // if, for , while の閉じ判定
  1022. p = parse_syntax_close(p + 1);
  1023. return p;
  1024. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1025. // switch() 閉じ判定
  1026. int pos = syntax.curly_count-1;
  1027. char label[256];
  1028. int l;
  1029. // 一時変数を消す
  1030. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1031. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1032. parse_line(label);
  1033. syntax.curly_count--;
  1034. // 無条件で終了ポインタに移動
  1035. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1036. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1037. parse_line(label);
  1038. syntax.curly_count--;
  1039. // 現在地のラベルを付ける
  1040. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1041. l=add_str(label);
  1042. set_label(l,script_pos, p);
  1043. if(syntax.curly[pos].flag) {
  1044. // default が存在する
  1045. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1046. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1047. parse_line(label);
  1048. syntax.curly_count--;
  1049. }
  1050. // 終了ラベルを付ける
  1051. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1052. l=add_str(label);
  1053. set_label(l,script_pos, p);
  1054. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1055. syntax.curly_count--;
  1056. return p+1;
  1057. } else {
  1058. disp_error_message("parse_curly_close: unexpected string",p);
  1059. return p + 1;
  1060. }
  1061. }
  1062. // 構文関連の処理
  1063. // break, case, continue, default, do, for, function,
  1064. // if, switch, while をこの内部で処理します。
  1065. const char* parse_syntax(const char* p)
  1066. {
  1067. const char *p2 = skip_word(p);
  1068. switch(*p) {
  1069. case 'B':
  1070. case 'b':
  1071. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1072. // break の処理
  1073. char label[256];
  1074. int pos = syntax.curly_count - 1;
  1075. while(pos >= 0) {
  1076. if(syntax.curly[pos].type == TYPE_DO) {
  1077. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1078. break;
  1079. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1080. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1081. break;
  1082. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1083. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1084. break;
  1085. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1086. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1087. break;
  1088. }
  1089. pos--;
  1090. }
  1091. if(pos < 0) {
  1092. disp_error_message("parse_syntax: unexpected 'break'",p);
  1093. } else {
  1094. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1095. parse_line(label);
  1096. syntax.curly_count--;
  1097. }
  1098. p = skip_space(p2);
  1099. if(*p != ';')
  1100. disp_error_message("parse_syntax: need ';'",p);
  1101. // if, for , while の閉じ判定
  1102. p = parse_syntax_close(p + 1);
  1103. return p;
  1104. }
  1105. break;
  1106. case 'c':
  1107. case 'C':
  1108. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1109. // case の処理
  1110. int pos = syntax.curly_count-1;
  1111. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1112. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1113. return p+1;
  1114. } else {
  1115. char label[256];
  1116. int l,v;
  1117. char *np;
  1118. if(syntax.curly[pos].count != 1) {
  1119. // FALLTHRU 用のジャンプ
  1120. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1121. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1122. parse_line(label);
  1123. syntax.curly_count--;
  1124. // 現在地のラベルを付ける
  1125. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1126. l=add_str(label);
  1127. set_label(l,script_pos, p);
  1128. }
  1129. // switch 判定文
  1130. p = skip_space(p2);
  1131. if(p == p2) {
  1132. disp_error_message("parse_syntax: expect space ' '",p);
  1133. }
  1134. // check whether case label is integer or not
  1135. v = strtol(p,&np,0);
  1136. if(np == p) { //Check for constants
  1137. p2 = skip_word(p);
  1138. v = p2-p; // length of word at p2
  1139. memcpy(label,p,v);
  1140. label[v]='\0';
  1141. v = search_str(label);
  1142. if (v < 0 || str_data[v].type != C_INT)
  1143. disp_error_message("parse_syntax: 'case' label not integer",p);
  1144. v = str_data[v].val;
  1145. p = skip_word(p);
  1146. } else { //Numeric value
  1147. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1148. p++;
  1149. p = skip_word(p);
  1150. if(np != p)
  1151. disp_error_message("parse_syntax: 'case' label not integer",np);
  1152. }
  1153. p = skip_space(p);
  1154. if(*p != ':')
  1155. disp_error_message("parse_syntax: expect ':'",p);
  1156. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1157. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1158. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1159. // 2回parse しないとダメ
  1160. p2 = parse_line(label);
  1161. parse_line(p2);
  1162. syntax.curly_count--;
  1163. if(syntax.curly[pos].count != 1) {
  1164. // FALLTHRU 終了後のラベル
  1165. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1166. l=add_str(label);
  1167. set_label(l,script_pos,p);
  1168. }
  1169. // check duplication of case label [Rayce]
  1170. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1171. disp_error_message("parse_syntax: dup 'case'",p);
  1172. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1173. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1174. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1175. parse_line(label);
  1176. syntax.curly_count--;
  1177. syntax.curly[pos].count++;
  1178. }
  1179. return p + 1;
  1180. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1181. // continue の処理
  1182. char label[256];
  1183. int pos = syntax.curly_count - 1;
  1184. while(pos >= 0) {
  1185. if(syntax.curly[pos].type == TYPE_DO) {
  1186. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1187. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1188. break;
  1189. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1190. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1191. break;
  1192. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1193. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1194. break;
  1195. }
  1196. pos--;
  1197. }
  1198. if(pos < 0) {
  1199. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1200. } else {
  1201. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1202. parse_line(label);
  1203. syntax.curly_count--;
  1204. }
  1205. p = skip_space(p2);
  1206. if(*p != ';')
  1207. disp_error_message("parse_syntax: need ';'",p);
  1208. // if, for , while の閉じ判定
  1209. p = parse_syntax_close(p + 1);
  1210. return p;
  1211. }
  1212. break;
  1213. case 'd':
  1214. case 'D':
  1215. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1216. // switch - default の処理
  1217. int pos = syntax.curly_count-1;
  1218. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1219. disp_error_message("parse_syntax: unexpected 'default'",p);
  1220. } else if(syntax.curly[pos].flag) {
  1221. disp_error_message("parse_syntax: dup 'default'",p);
  1222. } else {
  1223. char label[256];
  1224. int l;
  1225. // 現在地のラベルを付ける
  1226. p = skip_space(p2);
  1227. if(*p != ':') {
  1228. disp_error_message("parse_syntax: need ':'",p);
  1229. }
  1230. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1231. l=add_str(label);
  1232. set_label(l,script_pos,p);
  1233. // 無条件で次のリンクに飛ばす
  1234. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1235. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1236. parse_line(label);
  1237. syntax.curly_count--;
  1238. // default のラベルを付ける
  1239. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1240. l=add_str(label);
  1241. set_label(l,script_pos,p);
  1242. syntax.curly[syntax.curly_count - 1].flag = 1;
  1243. syntax.curly[pos].count++;
  1244. }
  1245. return p + 1;
  1246. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1247. int l;
  1248. char label[256];
  1249. p=skip_space(p2);
  1250. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1251. syntax.curly[syntax.curly_count].count = 1;
  1252. syntax.curly[syntax.curly_count].index = syntax.index++;
  1253. syntax.curly[syntax.curly_count].flag = 0;
  1254. // 現在地のラベル形成する
  1255. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1256. l=add_str(label);
  1257. set_label(l,script_pos,p);
  1258. syntax.curly_count++;
  1259. return p;
  1260. }
  1261. break;
  1262. case 'f':
  1263. case 'F':
  1264. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1265. int l;
  1266. char label[256];
  1267. int pos = syntax.curly_count;
  1268. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1269. syntax.curly[syntax.curly_count].count = 1;
  1270. syntax.curly[syntax.curly_count].index = syntax.index++;
  1271. syntax.curly[syntax.curly_count].flag = 0;
  1272. syntax.curly_count++;
  1273. p=skip_space(p2);
  1274. if(*p != '(')
  1275. disp_error_message("parse_syntax: need '('",p);
  1276. p++;
  1277. // 初期化文を実行する
  1278. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1279. p=parse_line(p);
  1280. syntax.curly_count--;
  1281. // 条件判断開始のラベル形成する
  1282. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1283. l=add_str(label);
  1284. set_label(l,script_pos,p);
  1285. p=skip_space(p);
  1286. if(*p == ';') {
  1287. // for(;;) のパターンなので必ず真
  1288. ;
  1289. } else {
  1290. // 条件が偽なら終了地点に飛ばす
  1291. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1292. add_scriptl(add_str("jump_zero"));
  1293. add_scriptc(C_ARG);
  1294. p=parse_expr(p);
  1295. p=skip_space(p);
  1296. add_scriptl(add_str(label));
  1297. add_scriptc(C_FUNC);
  1298. }
  1299. if(*p != ';')
  1300. disp_error_message("parse_syntax: need ';'",p);
  1301. p++;
  1302. // ループ開始に飛ばす
  1303. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1304. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1305. parse_line(label);
  1306. syntax.curly_count--;
  1307. // 次のループへのラベル形成する
  1308. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1309. l=add_str(label);
  1310. set_label(l,script_pos,p);
  1311. // 次のループに入る時の処理
  1312. // for 最後の ')' を ';' として扱うフラグ
  1313. parse_syntax_for_flag = 1;
  1314. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1315. p=parse_line(p);
  1316. syntax.curly_count--;
  1317. parse_syntax_for_flag = 0;
  1318. // 条件判定処理に飛ばす
  1319. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1320. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1321. parse_line(label);
  1322. syntax.curly_count--;
  1323. // ループ開始のラベル付け
  1324. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1325. l=add_str(label);
  1326. set_label(l,script_pos,p);
  1327. return p;
  1328. }
  1329. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1330. {// internal script function
  1331. const char *func_name;
  1332. func_name = skip_space(p2);
  1333. p = skip_word(func_name);
  1334. if( p == func_name )
  1335. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1336. p2 = skip_space(p);
  1337. if( *p2 == ';' )
  1338. {// function <name> ;
  1339. // function declaration - just register the name
  1340. int l;
  1341. l = add_word(func_name);
  1342. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1343. str_data[l].type = C_USERFUNC;
  1344. // if, for , while の閉じ判定
  1345. p = parse_syntax_close(p2 + 1);
  1346. return p; }
  1347. else if(*p2 == '{')
  1348. {// function <name> <line/block of code>
  1349. char label[256];
  1350. int l;
  1351. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1352. syntax.curly[syntax.curly_count].count = 1;
  1353. syntax.curly[syntax.curly_count].index = syntax.index++;
  1354. syntax.curly[syntax.curly_count].flag = 0;
  1355. ++syntax.curly_count;
  1356. // Jump over the function code
  1357. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1358. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1359. ++syntax.curly_count;
  1360. parse_line(label);
  1361. --syntax.curly_count;
  1362. // Set the position of the function (label)
  1363. l=add_word(func_name);
  1364. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1365. str_data[l].type = C_USERFUNC;
  1366. set_label(l, script_pos, p);
  1367. if( parse_options&SCRIPT_USE_LABEL_DB )
  1368. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1369. return skip_space(p);
  1370. }
  1371. else
  1372. {
  1373. disp_error_message("expect ';' or '{' at function syntax",p);
  1374. }
  1375. }
  1376. break;
  1377. case 'i':
  1378. case 'I':
  1379. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1380. // if() の処理
  1381. char label[256];
  1382. p=skip_space(p2);
  1383. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1384. disp_error_message("need '('",p);
  1385. }
  1386. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1387. syntax.curly[syntax.curly_count].count = 1;
  1388. syntax.curly[syntax.curly_count].index = syntax.index++;
  1389. syntax.curly[syntax.curly_count].flag = 0;
  1390. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1391. syntax.curly_count++;
  1392. add_scriptl(add_str("jump_zero"));
  1393. add_scriptc(C_ARG);
  1394. p=parse_expr(p);
  1395. p=skip_space(p);
  1396. add_scriptl(add_str(label));
  1397. add_scriptc(C_FUNC);
  1398. return p;
  1399. }
  1400. break;
  1401. case 's':
  1402. case 'S':
  1403. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1404. // switch() の処理
  1405. char label[256];
  1406. p=skip_space(p2);
  1407. if(*p != '(') {
  1408. disp_error_message("need '('",p);
  1409. }
  1410. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1411. syntax.curly[syntax.curly_count].count = 1;
  1412. syntax.curly[syntax.curly_count].index = syntax.index++;
  1413. syntax.curly[syntax.curly_count].flag = 0;
  1414. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1415. syntax.curly_count++;
  1416. add_scriptl(add_str("set"));
  1417. add_scriptc(C_ARG);
  1418. add_scriptl(add_str(label));
  1419. p=parse_expr(p);
  1420. p=skip_space(p);
  1421. if(*p != '{') {
  1422. disp_error_message("parse_syntax: need '{'",p);
  1423. }
  1424. add_scriptc(C_FUNC);
  1425. return p + 1;
  1426. }
  1427. break;
  1428. case 'w':
  1429. case 'W':
  1430. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1431. int l;
  1432. char label[256];
  1433. p=skip_space(p2);
  1434. if(*p != '(') {
  1435. disp_error_message("need '('",p);
  1436. }
  1437. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1438. syntax.curly[syntax.curly_count].count = 1;
  1439. syntax.curly[syntax.curly_count].index = syntax.index++;
  1440. syntax.curly[syntax.curly_count].flag = 0;
  1441. // 条件判断開始のラベル形成する
  1442. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1443. l=add_str(label);
  1444. set_label(l,script_pos,p);
  1445. // 条件が偽なら終了地点に飛ばす
  1446. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1447. syntax.curly_count++;
  1448. add_scriptl(add_str("jump_zero"));
  1449. add_scriptc(C_ARG);
  1450. p=parse_expr(p);
  1451. p=skip_space(p);
  1452. add_scriptl(add_str(label));
  1453. add_scriptc(C_FUNC);
  1454. return p;
  1455. }
  1456. break;
  1457. }
  1458. return NULL;
  1459. }
  1460. const char* parse_syntax_close(const char *p) {
  1461. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1462. int flag;
  1463. do {
  1464. p = parse_syntax_close_sub(p,&flag);
  1465. } while(flag);
  1466. return p;
  1467. }
  1468. // if, for , while , do の閉じ判定
  1469. // flag == 1 : 閉じられた
  1470. // flag == 0 : 閉じられない
  1471. const char* parse_syntax_close_sub(const char* p,int* flag)
  1472. {
  1473. char label[256];
  1474. int pos = syntax.curly_count - 1;
  1475. int l;
  1476. *flag = 1;
  1477. if(syntax.curly_count <= 0) {
  1478. *flag = 0;
  1479. return p;
  1480. } else if(syntax.curly[pos].type == TYPE_IF) {
  1481. const char *bp = p;
  1482. const char *p2;
  1483. // if 最終場所へ飛ばす
  1484. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1485. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1486. parse_line(label);
  1487. syntax.curly_count--;
  1488. // 現在地のラベルを付ける
  1489. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1490. l=add_str(label);
  1491. set_label(l,script_pos,p);
  1492. syntax.curly[pos].count++;
  1493. p = skip_space(p);
  1494. p2 = skip_word(p);
  1495. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1496. // else or else - if
  1497. p = skip_space(p2);
  1498. p2 = skip_word(p);
  1499. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1500. // else - if
  1501. p=skip_space(p2);
  1502. if(*p != '(') {
  1503. disp_error_message("need '('",p);
  1504. }
  1505. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1506. add_scriptl(add_str("jump_zero"));
  1507. add_scriptc(C_ARG);
  1508. p=parse_expr(p);
  1509. p=skip_space(p);
  1510. add_scriptl(add_str(label));
  1511. add_scriptc(C_FUNC);
  1512. *flag = 0;
  1513. return p;
  1514. } else {
  1515. // else
  1516. if(!syntax.curly[pos].flag) {
  1517. syntax.curly[pos].flag = 1;
  1518. *flag = 0;
  1519. return p;
  1520. }
  1521. }
  1522. }
  1523. // if 閉じ
  1524. syntax.curly_count--;
  1525. // 最終地のラベルを付ける
  1526. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1527. l=add_str(label);
  1528. set_label(l,script_pos,p);
  1529. if(syntax.curly[pos].flag == 1) {
  1530. // このifに対するelseじゃないのでポインタの位置は同じ
  1531. return bp;
  1532. }
  1533. return p;
  1534. } else if(syntax.curly[pos].type == TYPE_DO) {
  1535. int l;
  1536. char label[256];
  1537. const char *p2;
  1538. if(syntax.curly[pos].flag) {
  1539. // 現在地のラベル形成する(continue でここに来る)
  1540. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1541. l=add_str(label);
  1542. set_label(l,script_pos,p);
  1543. }
  1544. // 条件が偽なら終了地点に飛ばす
  1545. p = skip_space(p);
  1546. p2 = skip_word(p);
  1547. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1548. disp_error_message("parse_syntax: need 'while'",p);
  1549. p = skip_space(p2);
  1550. if(*p != '(') {
  1551. disp_error_message("need '('",p);
  1552. }
  1553. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1554. add_scriptl(add_str("jump_zero"));
  1555. add_scriptc(C_ARG);
  1556. p=parse_expr(p);
  1557. p=skip_space(p);
  1558. add_scriptl(add_str(label));
  1559. add_scriptc(C_FUNC);
  1560. // 開始地点に飛ばす
  1561. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1562. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1563. parse_line(label);
  1564. syntax.curly_count--;
  1565. // 条件終了地点のラベル形成する
  1566. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1567. l=add_str(label);
  1568. set_label(l,script_pos,p);
  1569. p = skip_space(p);
  1570. if(*p != ';') {
  1571. disp_error_message("parse_syntax: need ';'",p);
  1572. return p+1;
  1573. }
  1574. p++;
  1575. syntax.curly_count--;
  1576. return p;
  1577. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1578. // 次のループに飛ばす
  1579. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1580. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1581. parse_line(label);
  1582. syntax.curly_count--;
  1583. // for 終了のラベル付け
  1584. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1585. l=add_str(label);
  1586. set_label(l,script_pos,p);
  1587. syntax.curly_count--;
  1588. return p;
  1589. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1590. // while 条件判断へ飛ばす
  1591. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1592. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1593. parse_line(label);
  1594. syntax.curly_count--;
  1595. // while 終了のラベル付け
  1596. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1597. l=add_str(label);
  1598. set_label(l,script_pos,p);
  1599. syntax.curly_count--;
  1600. return p;
  1601. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1602. int pos = syntax.curly_count-1;
  1603. char label[256];
  1604. int l;
  1605. // 戻す
  1606. sprintf(label,"return;");
  1607. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1608. parse_line(label);
  1609. syntax.curly_count--;
  1610. // 現在地のラベルを付ける
  1611. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1612. l=add_str(label);
  1613. set_label(l,script_pos,p);
  1614. syntax.curly_count--;
  1615. return p;
  1616. } else {
  1617. *flag = 0;
  1618. return p;
  1619. }
  1620. }
  1621. /*==========================================
  1622. * 組み込み関数の追加
  1623. *------------------------------------------*/
  1624. static void add_buildin_func(void)
  1625. {
  1626. int i,n;
  1627. const char* p;
  1628. for( i = 0; buildin_func[i].func; i++ )
  1629. {
  1630. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1631. // 'v' - value (either string or int or reference)
  1632. // 's' - string
  1633. // 'i' - int
  1634. // 'r' - reference (of a variable)
  1635. // 'l' - label
  1636. // '?' - one optional parameter
  1637. // '*' - unknown number of optional parameters
  1638. p = buildin_func[i].arg;
  1639. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1640. while( *p == '?' ) ++p;
  1641. if( *p == '*' ) ++p;
  1642. if( *p != 0){
  1643. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1644. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1645. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1646. } else {
  1647. n = add_str(buildin_func[i].name);
  1648. str_data[n].type = C_FUNC;
  1649. str_data[n].val = i;
  1650. str_data[n].func = buildin_func[i].func;
  1651. }
  1652. }
  1653. }
  1654. /*==========================================
  1655. * 定数データベースの読み込み
  1656. *------------------------------------------*/
  1657. static void read_constdb(void)
  1658. {
  1659. FILE *fp;
  1660. char line[1024],name[1024],val[1024];
  1661. int n,type;
  1662. sprintf(line, "%s/const.txt", db_path);
  1663. fp=fopen(line, "r");
  1664. if(fp==NULL){
  1665. ShowError("can't read %s\n", line);
  1666. return ;
  1667. }
  1668. while(fgets(line, sizeof(line), fp))
  1669. {
  1670. if(line[0]=='/' && line[1]=='/')
  1671. continue;
  1672. type=0;
  1673. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1674. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1675. n=add_str(name);
  1676. if(type==0)
  1677. str_data[n].type=C_INT;
  1678. else
  1679. str_data[n].type=C_PARAM;
  1680. str_data[n].val= (int)strtol(val,NULL,0);
  1681. }
  1682. }
  1683. fclose(fp);
  1684. }
  1685. /*==========================================
  1686. * エラー表示
  1687. *------------------------------------------*/
  1688. const char* script_print_line( const char *p, const char *mark, int line )
  1689. {
  1690. int i;
  1691. if( p == NULL || !p[0] ) return NULL;
  1692. if( line < 0 )
  1693. ShowMessage("*% 5d : ", -line);
  1694. else
  1695. ShowMessage(" % 5d : ", line);
  1696. for(i=0;p[i] && p[i] != '\n';i++){
  1697. if(p + i != mark)
  1698. ShowMessage("%c",p[i]);
  1699. else
  1700. ShowMessage("\'%c\'",p[i]);
  1701. }
  1702. ShowMessage("\n");
  1703. return p+i+(p[i] == '\n' ? 1 : 0);
  1704. }
  1705. void script_error(const char *src,const char *file,int start_line, const char *error_msg, const char *error_pos)
  1706. {
  1707. // エラーが発生した行を求める
  1708. int j;
  1709. int line = start_line;
  1710. const char *p;
  1711. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1712. for(p=src;p && *p;line++){
  1713. const char *lineend=strchr(p,'\n');
  1714. if(lineend==NULL || error_pos<lineend){
  1715. break;
  1716. }
  1717. for( j = 0; j < 4; j++ ) {
  1718. linestart[j] = linestart[j+1];
  1719. }
  1720. linestart[4] = p;
  1721. p=lineend+1;
  1722. }
  1723. ShowMessage("\a\n");
  1724. ShowMessage("script error on %s line %d\n", file, line);
  1725. ShowMessage(" %s\n", error_msg);
  1726. for(j = 0; j < 5; j++ ) {
  1727. script_print_line( linestart[j], NULL, line + j - 5);
  1728. }
  1729. p = script_print_line( p, error_pos, -line);
  1730. for(j = 0; j < 5; j++) {
  1731. p = script_print_line( p, NULL, line + j + 1 );
  1732. }
  1733. }
  1734. /*==========================================
  1735. * スクリプトの解析
  1736. *------------------------------------------*/
  1737. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1738. {
  1739. const char *p,*tmpp;
  1740. int i;
  1741. struct script_code* code = NULL;
  1742. static int first=1;
  1743. char end;
  1744. if( src == NULL )
  1745. return NULL;// empty script
  1746. memset(&syntax,0,sizeof(syntax));
  1747. if(first){
  1748. add_buildin_func();
  1749. read_constdb();
  1750. first=0;
  1751. }
  1752. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1753. script_pos=0;
  1754. script_size=SCRIPT_BLOCK_SIZE;
  1755. str_data[LABEL_NEXTLINE].type=C_NOP;
  1756. str_data[LABEL_NEXTLINE].backpatch=-1;
  1757. str_data[LABEL_NEXTLINE].label=-1;
  1758. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1759. if( options&SCRIPT_USE_LABEL_DB )
  1760. scriptlabel_db->clear(scriptlabel_db, NULL);
  1761. parse_options = options;
  1762. if( setjmp( error_jump ) != 0 ) {
  1763. //Restore program state when script has problems. [from jA]
  1764. int i;
  1765. const int size = ARRAYLENGTH(syntax.curly);
  1766. if( error_report )
  1767. script_error(src,file,line,error_msg,error_pos);
  1768. aFree( error_msg );
  1769. aFree( script_buf );
  1770. script_pos = 0;
  1771. script_size = 0;
  1772. script_buf = NULL;
  1773. for(i=LABEL_START;i<str_num;i++)
  1774. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1775. for(i=0; i<size; i++)
  1776. linkdb_final(&syntax.curly[i].case_label);
  1777. return NULL;
  1778. }
  1779. parse_syntax_for_flag=0;
  1780. p=src;
  1781. p=skip_space(p);
  1782. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1783. {// does not require brackets around the script
  1784. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1785. {// empty script and can return NULL
  1786. aFree( script_buf );
  1787. script_pos = 0;
  1788. script_size = 0;
  1789. script_buf = NULL;
  1790. return NULL;
  1791. }
  1792. end = '\0';
  1793. }
  1794. else
  1795. {// requires brackets around the script
  1796. if( *p != '{' )
  1797. disp_error_message("not found '{'",p);
  1798. p = skip_space(p+1);
  1799. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1800. {// empty script and can return NULL
  1801. aFree( script_buf );
  1802. script_pos = 0;
  1803. script_size = 0;
  1804. script_buf = NULL;
  1805. return NULL;
  1806. }
  1807. end = '}';
  1808. }
  1809. // clear references of labels, variables and internal functions
  1810. for(i=LABEL_START;i<str_num;i++){
  1811. if(
  1812. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1813. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1814. ){
  1815. str_data[i].type=C_NOP;
  1816. str_data[i].backpatch=-1;
  1817. str_data[i].label=-1;
  1818. }
  1819. }
  1820. while( syntax.curly_count != 0 || *p != end )
  1821. {
  1822. if( *p == '\0' )
  1823. disp_error_message("unexpected end of script",p);
  1824. // labelだけ特殊処理
  1825. tmpp=skip_space(skip_word(p));
  1826. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1827. i=add_word(p);
  1828. set_label(i,script_pos,p);
  1829. if( parse_options&SCRIPT_USE_LABEL_DB )
  1830. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1831. p=tmpp+1;
  1832. p=skip_space(p);
  1833. continue;
  1834. }
  1835. // 他は全部一緒くた
  1836. p=parse_line(p);
  1837. p=skip_space(p);
  1838. add_scriptc(C_EOL);
  1839. set_label(LABEL_NEXTLINE,script_pos,p);
  1840. str_data[LABEL_NEXTLINE].type=C_NOP;
  1841. str_data[LABEL_NEXTLINE].backpatch=-1;
  1842. str_data[LABEL_NEXTLINE].label=-1;
  1843. }
  1844. add_scriptc(C_NOP);
  1845. // trim code to size
  1846. script_size = script_pos;
  1847. RECREATE(script_buf,unsigned char,script_pos);
  1848. // default unknown references to variables
  1849. for(i=LABEL_START;i<str_num;i++){
  1850. if(str_data[i].type==C_NOP){
  1851. int j,next;
  1852. str_data[i].type=C_NAME;
  1853. str_data[i].label=i;
  1854. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1855. next=GETVALUE(script_buf,j);
  1856. SETVALUE(script_buf,j,i);
  1857. j=next;
  1858. }
  1859. }
  1860. }
  1861. #ifdef DEBUG_DISP
  1862. for(i=0;i<script_pos;i++){
  1863. if((i&15)==0) printf("%04x : ",i);
  1864. ShowMessage("%02x ",script_buf[i]);
  1865. if((i&15)==15) printf("\n");
  1866. }
  1867. ShowMessage("\n");
  1868. #endif
  1869. #ifdef DEBUG_DISASM
  1870. {
  1871. int i = 0,j;
  1872. while(i < script_pos) {
  1873. ShowMessage("%06x ",i);
  1874. j = i;
  1875. switch(get_com(script_buf,&i)) {
  1876. case C_EOL: printf("C_EOL"); break;
  1877. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1878. case C_POS:
  1879. ShowMessage("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1880. i += 3;
  1881. break;
  1882. case C_NAME:
  1883. j = (*(int*)(script_buf+i)&0xffffff);
  1884. ShowMessage("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1885. i += 3;
  1886. break;
  1887. case C_ARG: ShowMessage("C_ARG"); break;
  1888. case C_FUNC: ShowMessage("C_FUNC"); break;
  1889. case C_ADD: ShowMessage("C_ADD"); break;
  1890. case C_SUB: ShowMessage("C_SUB"); break;
  1891. case C_MUL: ShowMessage("C_MUL"); break;
  1892. case C_DIV: ShowMessage("C_DIV"); break;
  1893. case C_MOD: ShowMessage("C_MOD"); break;
  1894. case C_EQ: ShowMessage("C_EQ"); break;
  1895. case C_NE: ShowMessage("C_NE"); break;
  1896. case C_GT: ShowMessage("C_GT"); break;
  1897. case C_GE: ShowMessage("C_GE"); break;
  1898. case C_LT: ShowMessage("C_LT"); break;
  1899. case C_LE: ShowMessage("C_LE"); break;
  1900. case C_AND: ShowMessage("C_AND"); break;
  1901. case C_OR: ShowMessage("C_OR"); break;
  1902. case C_XOR: ShowMessage("C_XOR"); break;
  1903. case C_LAND: ShowMessage("C_LAND"); break;
  1904. case C_LOR: ShowMessage("C_LOR"); break;
  1905. case C_R_SHIFT: ShowMessage("C_R_SHIFT"); break;
  1906. case C_L_SHIFT: ShowMessage("C_L_SHIFT"); break;
  1907. case C_NEG: ShowMessage("C_NEG"); break;
  1908. case C_NOT: ShowMessage("C_NOT"); break;
  1909. case C_LNOT: ShowMessage("C_LNOT"); break;
  1910. case C_NOP: ShowMessage("C_NOP"); break;
  1911. case C_OP3: ShowMessage("C_OP3"); break;
  1912. case C_STR:
  1913. j = strlen(script_buf + i);
  1914. ShowMessage("C_STR %s",script_buf + i);
  1915. i+= j+1;
  1916. break;
  1917. default:
  1918. ShowMessage("unknown");
  1919. }
  1920. ShowMessage("\n");
  1921. }
  1922. }
  1923. #endif
  1924. CREATE(code,struct script_code,1);
  1925. code->script_buf = script_buf;
  1926. code->script_size = script_size;
  1927. code->script_vars = NULL;
  1928. return code;
  1929. }
  1930. //
  1931. // Script state
  1932. //
  1933. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1934. /// Returns the player attached to this script, identified by the rid.
  1935. /// If there is no player attached, the script is terminated.
  1936. TBL_PC *script_rid2sd(struct script_state *st)
  1937. {
  1938. TBL_PC *sd=map_id2sd(st->rid);
  1939. if(!sd){
  1940. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1941. script_reportsrc(st);
  1942. st->state = END;
  1943. }
  1944. return sd;
  1945. }
  1946. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1947. ///
  1948. /// @param st Script state
  1949. /// @param data Variable/constant
  1950. void get_val(struct script_state* st, struct script_data* data)
  1951. {
  1952. char* name;
  1953. char prefix;
  1954. char postfix;
  1955. TBL_PC* sd = NULL;
  1956. if( !data_isreference(data) )
  1957. return;// not a variable/constant
  1958. name = reference_getname(data);
  1959. prefix = name[0];
  1960. postfix = name[strlen(name) - 1];
  1961. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1962. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1963. {
  1964. sd = script_rid2sd(st);
  1965. if( sd == NULL )
  1966. {// needs player attached
  1967. if( postfix == '$' )
  1968. {// string variable
  1969. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1970. data->type = C_CONSTSTR;
  1971. data->u.str = "";
  1972. }
  1973. else
  1974. {// integer variable
  1975. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1976. data->type = C_INT;
  1977. data->u.num = 0;
  1978. }
  1979. return;
  1980. }
  1981. }
  1982. if( postfix == '$' )
  1983. {// string variable
  1984. switch( prefix )
  1985. {
  1986. case '@':
  1987. data->u.str = pc_readregstr(sd, data->u.num);
  1988. break;
  1989. case '$':
  1990. data->u.str = (char *)idb_get(mapregstr_db, data->u.num);
  1991. break;
  1992. case '#':
  1993. if( name[1] == '#' )
  1994. data->u.str = pc_readaccountreg2str(sd, name);// global
  1995. else
  1996. data->u.str = pc_readaccountregstr(sd, name);// local
  1997. break;
  1998. case '.':
  1999. {
  2000. struct linkdb_node** n =
  2001. data->ref ? data->ref:
  2002. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2003. &st->script->script_vars;// npc variable
  2004. data->u.str = (char*)linkdb_search(n, (void*)reference_getuid(data));
  2005. }
  2006. break;
  2007. default:
  2008. data->u.str = pc_readglobalreg_str(sd, name);
  2009. break;
  2010. }
  2011. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2012. {// empty string
  2013. data->type = C_CONSTSTR;
  2014. data->u.str = "";
  2015. }
  2016. else
  2017. {// duplicate string
  2018. data->type = C_STR;
  2019. data->u.str = aStrdup(data->u.str);
  2020. }
  2021. }
  2022. else
  2023. {// integer variable
  2024. data->type = C_INT;
  2025. if( reference_toconstant(data) )
  2026. {
  2027. data->u.num = reference_getconstant(data);
  2028. }
  2029. else if( reference_toparam(data) )
  2030. {
  2031. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2032. }
  2033. else
  2034. switch( prefix )
  2035. {
  2036. case '@':
  2037. data->u.num = pc_readreg(sd, data->u.num);
  2038. break;
  2039. case '$':
  2040. data->u.num = (int)idb_get(mapreg_db, data->u.num);
  2041. break;
  2042. case '#':
  2043. if( name[1] == '#' )
  2044. data->u.num = pc_readaccountreg2(sd, name);// global
  2045. else
  2046. data->u.num = pc_readaccountreg(sd, name);// local
  2047. break;
  2048. case '.':
  2049. {
  2050. struct linkdb_node** n =
  2051. data->ref ? data->ref:
  2052. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2053. &st->script->script_vars;// npc variable
  2054. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2055. }
  2056. break;
  2057. default:
  2058. data->u.num = pc_readglobalreg(sd, name);
  2059. break;
  2060. }
  2061. }
  2062. return;
  2063. }
  2064. void push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref);
  2065. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2066. /// The value is left in the top of the stack and needs to be removed manually.
  2067. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2068. {
  2069. struct script_data* data;
  2070. push_val2(st->stack, C_NAME, uid, ref);
  2071. data = script_getdatatop(st, -1);
  2072. get_val(st, data);
  2073. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2074. }
  2075. /*==========================================
  2076. * Stores the value of a script variable
  2077. * Return value is 0 on fail, 1 on success.
  2078. *------------------------------------------*/
  2079. static int set_reg(struct script_state* st, TBL_PC* sd, int num, char* name, const void* value, struct linkdb_node** ref)
  2080. {
  2081. char prefix = name[0]; char postfix = name[strlen(name)-1];
  2082. if (postfix == '$') { // string variable
  2083. const char* str = (const char*)value;
  2084. switch (prefix) {
  2085. case '@':
  2086. return pc_setregstr(sd, num, str);
  2087. case '$':
  2088. return mapreg_setregstr(num, str);
  2089. case '#':
  2090. return (name[1] == '#') ?
  2091. pc_setaccountreg2str(sd, name, str) :
  2092. pc_setaccountregstr(sd, name, str);
  2093. case '.': {
  2094. char* p;
  2095. struct linkdb_node** n;
  2096. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2097. p = (char*)linkdb_erase(n, (void*)num);
  2098. if (p) aFree(p);
  2099. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2100. }
  2101. return 1;
  2102. default:
  2103. return pc_setglobalreg_str(sd, name, str);
  2104. }
  2105. } else { // integer variable
  2106. int val = (int)value;
  2107. if(str_data[num&0x00ffffff].type == C_PARAM)
  2108. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  2109. switch (prefix) {
  2110. case '@':
  2111. return pc_setreg(sd, num, val);
  2112. case '$':
  2113. return mapreg_setreg(num, val);
  2114. case '#':
  2115. return (name[1] == '#') ?
  2116. pc_setaccountreg2(sd, name, val) :
  2117. pc_setaccountreg(sd, name, val);
  2118. case '.': {
  2119. struct linkdb_node** n;
  2120. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2121. if (val == 0)
  2122. linkdb_erase(n, (void*)num);
  2123. else
  2124. linkdb_replace(n, (void*)num, (void*)val);
  2125. }
  2126. return 1;
  2127. default:
  2128. return pc_setglobalreg(sd, name, val);
  2129. }
  2130. }
  2131. }
  2132. int set_var(TBL_PC* sd, char* name, void* val)
  2133. {
  2134. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  2135. }
  2136. /// Converts the data to a string
  2137. const char* conv_str(struct script_state* st, struct script_data* data)
  2138. {
  2139. char* p;
  2140. get_val(st, data);
  2141. if( data_isstring(data) )
  2142. {// nothing to convert
  2143. }
  2144. else if( data_isint(data) )
  2145. {// int -> string
  2146. CREATE(p, char, ITEM_NAME_LENGTH);
  2147. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2148. p[ITEM_NAME_LENGTH-1] = '\0';
  2149. data->type = C_STR;
  2150. data->u.str = p;
  2151. }
  2152. else if( data_isreference(data) )
  2153. {// reference -> string
  2154. //##TODO when does this happen (check get_val) [FlavioJS]
  2155. data->type = C_CONSTSTR;
  2156. data->u.str = reference_getname(data);
  2157. }
  2158. else
  2159. {// unsupported data type
  2160. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2161. script_reportdata(data);
  2162. script_reportsrc(st);
  2163. data->type = C_CONSTSTR;
  2164. data->u.str = "";
  2165. }
  2166. return data->u.str;
  2167. }
  2168. /// Converts the data to an int
  2169. int conv_num(struct script_state* st, struct script_data* data)
  2170. {
  2171. char* p;
  2172. long num;
  2173. get_val(st, data);
  2174. if( data_isint(data) )
  2175. {// nothing to convert
  2176. }
  2177. else if( data_isstring(data) )
  2178. {// string -> int
  2179. // the result does not overflow or underflow, it is capped instead
  2180. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2181. p = data->u.str;
  2182. errno = 0;
  2183. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2184. if( errno == ERANGE
  2185. #if LONG_MAX > INT_MAX
  2186. || num < INT_MIN || num > INT_MAX
  2187. #endif
  2188. )
  2189. {
  2190. if( num <= INT_MIN )
  2191. {
  2192. num = INT_MIN;
  2193. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2194. }
  2195. else//if( num >= INT_MAX )
  2196. {
  2197. num = INT_MAX;
  2198. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2199. }
  2200. script_reportdata(data);
  2201. script_reportsrc(st);
  2202. }
  2203. if( data->type == C_STR )
  2204. aFree(p);
  2205. data->type = C_INT;
  2206. data->u.num = (int)num;
  2207. }
  2208. #if 0
  2209. // FIXME this function is being used to retrieve the position of labels and
  2210. // probably other stuff [FlavioJS]
  2211. else
  2212. {// unsupported data type
  2213. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2214. script_reportdata(data);
  2215. script_reportsrc(st);
  2216. data->type = C_INT;
  2217. data->u.num = 0;
  2218. }
  2219. #endif
  2220. return data->u.num;
  2221. }
  2222. //
  2223. // Stack operations
  2224. //
  2225. /// Increases the size of the stack
  2226. void stack_expand(struct script_stack* stack)
  2227. {
  2228. stack->sp_max += 64;
  2229. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2230. stack->sp_max * sizeof(stack->stack_data[0]) );
  2231. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2232. 64 * sizeof(stack->stack_data[0]) );
  2233. }
  2234. /// Pushes a value into the stack
  2235. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2236. /// Pushes a value into the stack (with reference)
  2237. void push_val2(struct script_stack* stack, enum c_op type, int val, struct linkdb_node** ref)
  2238. {
  2239. if( stack->sp >= stack->sp_max )
  2240. stack_expand(stack);
  2241. stack->stack_data[stack->sp].type = type;
  2242. stack->stack_data[stack->sp].u.num = val;
  2243. stack->stack_data[stack->sp].ref = ref;
  2244. stack->sp++;
  2245. }
  2246. /// Pushes a string into the stack
  2247. void push_str(struct script_stack* stack, enum c_op type, char* str)
  2248. {
  2249. if( stack->sp >= stack->sp_max )
  2250. stack_expand(stack);
  2251. stack->stack_data[stack->sp].type = type;
  2252. stack->stack_data[stack->sp].u.str = str;
  2253. stack->stack_data[stack->sp].ref = NULL;
  2254. stack->sp++;
  2255. }
  2256. /// Pushes a copy of the target position into the stack
  2257. void push_copy(struct script_stack* stack, int pos)
  2258. {
  2259. switch( stack->stack_data[pos].type )
  2260. {
  2261. case C_CONSTSTR:
  2262. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2263. break;
  2264. case C_STR:
  2265. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2266. break;
  2267. default:
  2268. push_val2(
  2269. stack,stack->stack_data[pos].type,
  2270. stack->stack_data[pos].u.num,
  2271. stack->stack_data[pos].ref
  2272. );
  2273. break;
  2274. }
  2275. }
  2276. /// Removes the values in indexes [start,end[ from the stack
  2277. void pop_stack(struct script_stack* stack, int start, int end)
  2278. {
  2279. struct script_data* data;
  2280. int i;
  2281. if( start < 0 )
  2282. start = 0;
  2283. if( end > stack->sp_max )
  2284. end = stack->sp_max;
  2285. if( start >= end )
  2286. return;// nothing to pop
  2287. // free stack elements
  2288. for( i = start; i < end; i++ )
  2289. {
  2290. data = &stack->stack_data[i];
  2291. if( data->type == C_STR )
  2292. aFree(data->u.str);
  2293. data->type = C_NOP;
  2294. }
  2295. // move the rest of the elements
  2296. if( stack->sp > end )
  2297. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2298. stack->sp -= end - start;
  2299. }
  2300. ///
  2301. ///
  2302. ///
  2303. /*==========================================
  2304. * スクリプト依存変数、関数依存変数の解放
  2305. *------------------------------------------*/
  2306. void script_free_vars(struct linkdb_node **node)
  2307. {
  2308. struct linkdb_node *n = *node;
  2309. while(n) {
  2310. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2311. char postfix = name[strlen(name)-1];
  2312. if( postfix == '$' ) {
  2313. // 文字型変数なので、データ削除
  2314. aFree(n->data);
  2315. }
  2316. n = n->next;
  2317. }
  2318. linkdb_final( node );
  2319. }
  2320. /*==========================================
  2321. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2322. *------------------------------------------*/
  2323. void script_free_stack(struct script_stack *stack)
  2324. {
  2325. int i;
  2326. for(i = 0; i < stack->sp; i++) {
  2327. if( stack->stack_data[i].type == C_STR ) {
  2328. aFree(stack->stack_data[i].u.str);
  2329. stack->stack_data[i].type = C_INT;
  2330. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2331. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2332. script_free_vars( n );
  2333. aFree( n );
  2334. }
  2335. }
  2336. script_free_vars( stack->var_function );
  2337. aFree(stack->var_function);
  2338. aFree(stack->stack_data);
  2339. aFree(stack);
  2340. }
  2341. void script_free_code(struct script_code* code)
  2342. {
  2343. script_free_vars( &code->script_vars );
  2344. aFree( code->script_buf );
  2345. aFree( code );
  2346. }
  2347. //
  2348. // 実行部main
  2349. //
  2350. /*==========================================
  2351. * コマンドの読み取り
  2352. *------------------------------------------*/
  2353. c_op get_com(unsigned char *script,int *pos)
  2354. {
  2355. int i = 0, j = 0;
  2356. if(script[*pos]>=0x80){
  2357. return C_INT;
  2358. }
  2359. while(script[*pos]>=0x40){
  2360. i=script[(*pos)++]<<j;
  2361. j+=6;
  2362. }
  2363. return (c_op)(i+(script[(*pos)++]<<j));
  2364. }
  2365. /*==========================================
  2366. * 数値の所得
  2367. *------------------------------------------*/
  2368. int get_num(unsigned char *script,int *pos)
  2369. {
  2370. int i,j;
  2371. i=0; j=0;
  2372. while(script[*pos]>=0xc0){
  2373. i+=(script[(*pos)++]&0x7f)<<j;
  2374. j+=6;
  2375. }
  2376. return i+((script[(*pos)++]&0x7f)<<j);
  2377. }
  2378. /*==========================================
  2379. * スタックから値を取り出す
  2380. *------------------------------------------*/
  2381. int pop_val(struct script_state* st)
  2382. {
  2383. if(st->stack->sp<=0)
  2384. return 0;
  2385. st->stack->sp--;
  2386. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2387. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2388. return st->stack->stack_data[st->stack->sp].u.num;
  2389. return 0;
  2390. }
  2391. int isstr(struct script_data *c)
  2392. {
  2393. if( data_isstring(c) )
  2394. return 1;
  2395. else if( data_isreference(c) ) {
  2396. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2397. char postfix = p[strlen(p)-1];
  2398. return (postfix == '$');
  2399. }
  2400. return 0;
  2401. }
  2402. /// Ternary operators
  2403. /// test ? if_true : if_false
  2404. void op_3(struct script_state* st, int op)
  2405. {
  2406. struct script_data* data;
  2407. int flag = 0;
  2408. data = script_getdatatop(st, -3);
  2409. get_val(st, data);
  2410. if( data_isstring(data) )
  2411. flag = data->u.str[0];// "" -> false
  2412. else if( data_isint(data) )
  2413. flag = data->u.num;// 0 -> false
  2414. else
  2415. {
  2416. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2417. script_reportdata(data);
  2418. script_reportsrc(st);
  2419. script_removetop(st, -3, 0);
  2420. script_pushnil(st);
  2421. return;
  2422. }
  2423. if( flag )
  2424. script_pushcopytop(st, -2);
  2425. else
  2426. script_pushcopytop(st, -1);
  2427. script_removetop(st, -4, -1);
  2428. }
  2429. /// Binary string operators
  2430. /// s1 EQ s2 -> i
  2431. /// s1 NE s2 -> i
  2432. /// s1 GT s2 -> i
  2433. /// s1 GE s2 -> i
  2434. /// s1 LT s2 -> i
  2435. /// s1 LE s2 -> i
  2436. /// s1 ADD s2 -> s
  2437. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2438. {
  2439. int a = 0;
  2440. switch(op){
  2441. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2442. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2443. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2444. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2445. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2446. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2447. case C_ADD:
  2448. {
  2449. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2450. strcpy(buf, s1);
  2451. strcat(buf, s2);
  2452. script_pushstr(st, buf);
  2453. return;
  2454. }
  2455. default:
  2456. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2457. script_reportsrc(st);
  2458. script_pushnil(st);
  2459. st->state = END;
  2460. return;
  2461. }
  2462. script_pushint(st,a);
  2463. }
  2464. /// Binary number operators
  2465. /// i OP i -> i
  2466. void op_2num(struct script_state* st, int op, int i1, int i2)
  2467. {
  2468. int ret;
  2469. double ret_double;
  2470. switch( op )
  2471. {
  2472. case C_AND: ret = i1 & i2; break;
  2473. case C_OR: ret = i1 | i2; break;
  2474. case C_XOR: ret = i1 ^ i2; break;
  2475. case C_LAND: ret = (i1 && i2); break;
  2476. case C_LOR: ret = (i1 || i2); break;
  2477. case C_EQ: ret = (i1 == i2); break;
  2478. case C_NE: ret = (i1 != i2); break;
  2479. case C_GT: ret = (i1 > i2); break;
  2480. case C_GE: ret = (i1 >= i2); break;
  2481. case C_LT: ret = (i1 < i2); break;
  2482. case C_LE: ret = (i1 <= i2); break;
  2483. case C_R_SHIFT: ret = i1>>i2; break;
  2484. case C_L_SHIFT: ret = i1<<i2; break;
  2485. case C_DIV:
  2486. case C_MOD:
  2487. if( i2 == 0 )
  2488. {
  2489. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2490. script_reportsrc(st);
  2491. script_pushnil(st);
  2492. st->state = END;
  2493. return;
  2494. }
  2495. else if( op == C_DIV )
  2496. ret = i1 / i2;
  2497. else//if( op == C_MOD )
  2498. ret = i1 % i2;
  2499. break;
  2500. default:
  2501. switch( op )
  2502. {// operators that can overflow/underflow
  2503. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2504. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2505. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2506. default:
  2507. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2508. script_reportsrc(st);
  2509. script_pushnil(st);
  2510. return;
  2511. }
  2512. if( ret_double < (double)INT_MIN )
  2513. {
  2514. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2515. script_reportsrc(st);
  2516. ret = INT_MIN;
  2517. }
  2518. else if( ret_double > (double)INT_MAX )
  2519. {
  2520. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2521. script_reportsrc(st);
  2522. ret = INT_MAX;
  2523. }
  2524. }
  2525. script_pushint(st, ret);
  2526. }
  2527. /// Binary operators
  2528. void op_2(struct script_state *st, int op)
  2529. {
  2530. struct script_data* left;
  2531. struct script_data* right;
  2532. left = script_getdatatop(st, -2);
  2533. right = script_getdatatop(st, -1);
  2534. get_val(st, left);
  2535. get_val(st, right);
  2536. // automatic conversions
  2537. switch( op )
  2538. {
  2539. case C_ADD:
  2540. if( data_isint(left) && data_isstring(right) )
  2541. {// convert int-string to string-string
  2542. conv_str(st, left);
  2543. }
  2544. else if( data_isstring(left) && data_isint(right) )
  2545. {// convert string-int to string-string
  2546. conv_str(st, right);
  2547. }
  2548. break;
  2549. }
  2550. if( data_isstring(left) && data_isstring(right) )
  2551. {// ss => op_2str
  2552. op_2str(st, op, left->u.str, right->u.str);
  2553. script_removetop(st, -3, -1);// pop the two values before the top one
  2554. }
  2555. else if( data_isint(left) && data_isint(right) )
  2556. {// ii => op_2num
  2557. int i1 = left->u.num;
  2558. int i2 = right->u.num;
  2559. script_removetop(st, -2, 0);
  2560. op_2num(st, op, i1, i2);
  2561. }
  2562. else
  2563. {// invalid argument
  2564. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2565. script_reportdata(left);
  2566. script_reportdata(right);
  2567. script_reportsrc(st);
  2568. script_removetop(st, -2, 0);
  2569. script_pushnil(st);
  2570. st->state = END;
  2571. }
  2572. }
  2573. /// Unary operators
  2574. /// NEG i -> i
  2575. /// NOT i -> i
  2576. /// LNOT i -> i
  2577. void op_1(struct script_state* st, int op)
  2578. {
  2579. struct script_data* data;
  2580. int i1;
  2581. data = script_getdatatop(st, -1);
  2582. get_val(st, data);
  2583. if( !data_isint(data) )
  2584. {// not a number
  2585. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2586. script_reportdata(data);
  2587. script_reportsrc(st);
  2588. script_pushnil(st);
  2589. st->state = END;
  2590. return;
  2591. }
  2592. i1 = data->u.num;
  2593. script_removetop(st, -1, 0);
  2594. switch( op )
  2595. {
  2596. case C_NEG: i1 = -i1; break;
  2597. case C_NOT: i1 = ~i1; break;
  2598. case C_LNOT: i1 = !i1; break;
  2599. default:
  2600. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2601. script_reportsrc(st);
  2602. script_pushnil(st);
  2603. st->state = END;
  2604. return;
  2605. }
  2606. script_pushint(st, i1);
  2607. }
  2608. /*==========================================
  2609. * 関数の実行
  2610. *------------------------------------------*/
  2611. int run_func(struct script_state *st)
  2612. {
  2613. int i,start_sp,end_sp,func;
  2614. end_sp=st->stack->sp;
  2615. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2616. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2617. ShowError("function not found\n");
  2618. // st->stack->sp=0;
  2619. st->state=END;
  2620. script_reportsrc(st);
  2621. return 1;
  2622. }
  2623. start_sp=i-1;
  2624. st->start=i-1;
  2625. st->end=end_sp;
  2626. func=st->stack->stack_data[st->start].u.num;
  2627. #ifdef DEBUG_RUN
  2628. if(battle_config.etc_log) {
  2629. 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);
  2630. ShowDebug("stack dump :");
  2631. for(i=0;i<end_sp;i++){
  2632. switch(st->stack->stack_data[i].type){
  2633. case C_INT:
  2634. ShowMessage(" int(%d)",st->stack->stack_data[i].u.num);
  2635. break;
  2636. case C_NAME:
  2637. ShowMessage(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2638. break;
  2639. case C_ARG:
  2640. ShowMessage(" arg");
  2641. break;
  2642. case C_POS:
  2643. ShowMessage(" pos(%d)",st->stack->stack_data[i].u.num);
  2644. break;
  2645. case C_STR:
  2646. ShowMessage(" str(%s)",st->stack->stack_data[i].u.str);
  2647. break;
  2648. case C_CONSTSTR:
  2649. ShowMessage(" cstr(%s)",st->stack->stack_data[i].u.str);
  2650. break;
  2651. default:
  2652. ShowMessage(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2653. }
  2654. }
  2655. ShowMessage("\n");
  2656. }
  2657. #endif
  2658. if(str_data[func].type!=C_FUNC ){
  2659. 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);
  2660. // st->stack->sp=0;
  2661. st->state=END;
  2662. script_reportsrc(st);
  2663. return 1;
  2664. }
  2665. #ifdef DEBUG_RUN
  2666. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2667. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2668. );
  2669. #endif
  2670. if(str_data[func].func){
  2671. if (str_data[func].func(st)) //Report error
  2672. script_reportsrc(st);
  2673. } else {
  2674. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2675. script_pushint(st,0);
  2676. script_reportsrc(st);
  2677. }
  2678. // Stack's datum are used when re-run functions [Eoe]
  2679. if(st->state != RERUNLINE) {
  2680. pop_stack(st->stack,start_sp,end_sp);
  2681. }
  2682. if(st->state==RETFUNC){
  2683. // ユーザー定義関数からの復帰
  2684. int olddefsp=st->stack->defsp;
  2685. int i;
  2686. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2687. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2688. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2689. st->state=END;
  2690. script_reportsrc(st);
  2691. return 1;
  2692. }
  2693. script_free_vars( st->stack->var_function );
  2694. aFree(st->stack->var_function);
  2695. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2696. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2697. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2698. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2699. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2700. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2701. st->state=GOTO;
  2702. }
  2703. return 0;
  2704. }
  2705. /*==========================================
  2706. * スクリプトの実行
  2707. *------------------------------------------*/
  2708. void run_script_main(struct script_state *st);
  2709. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2710. {
  2711. struct script_state *st;
  2712. TBL_PC *sd=NULL;
  2713. if(rootscript==NULL || pos<0)
  2714. return;
  2715. if (rid) sd = map_id2sd(rid);
  2716. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2717. //Resume script.
  2718. st = sd->st;
  2719. } else {
  2720. st = (struct script_state*)aCalloc(sizeof(struct script_state), 1);
  2721. // the script is different, make new script_state and stack
  2722. st->stack = (struct script_stack*)aMalloc (sizeof(struct script_stack));
  2723. st->stack->sp=0;
  2724. st->stack->sp_max=64;
  2725. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2726. st->stack->defsp = st->stack->sp;
  2727. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  2728. st->state = RUN;
  2729. st->script = rootscript;
  2730. }
  2731. st->pos = pos;
  2732. st->rid = rid;
  2733. st->oid = oid;
  2734. st->sleep.timer = INVALID_TIMER;
  2735. run_script_main(st);
  2736. }
  2737. void script_stop_sleeptimers(int id)
  2738. {
  2739. struct script_state* st;
  2740. for(;;)
  2741. {
  2742. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2743. if( st == NULL )
  2744. break; // no more sleep timers
  2745. if( st->sleep.timer != INVALID_TIMER )
  2746. delete_timer(st->sleep.timer, run_script_timer);
  2747. script_free_stack(st->stack);
  2748. aFree(st);
  2749. }
  2750. }
  2751. /*==========================================
  2752. * 指定ノードをsleep_dbから削除
  2753. *------------------------------------------*/
  2754. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2755. {
  2756. struct linkdb_node *retnode;
  2757. if( n == NULL)
  2758. return NULL;
  2759. if( n->prev == NULL )
  2760. sleep_db = n->next;
  2761. else
  2762. n->prev->next = n->next;
  2763. if( n->next )
  2764. n->next->prev = n->prev;
  2765. retnode = n->next;
  2766. aFree( n );
  2767. return retnode; // 次のノードを返す
  2768. }
  2769. /*==========================================
  2770. * sleep用タイマー関数
  2771. *------------------------------------------*/
  2772. int run_script_timer(int tid, unsigned int tick, int id, int data)
  2773. {
  2774. struct script_state *st = (struct script_state *)data;
  2775. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2776. TBL_PC *sd = map_id2sd(st->rid);
  2777. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2778. { //Character mismatch. Cancel execution.
  2779. st->rid = 0;
  2780. st->state = END;
  2781. }
  2782. while( node && st->sleep.timer != -1 ) {
  2783. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2784. script_erase_sleepdb(node);
  2785. st->sleep.timer = -1;
  2786. break;
  2787. }
  2788. node = node->next;
  2789. }
  2790. if(st->state != RERUNLINE)
  2791. st->sleep.tick = 0;
  2792. run_script_main(st);
  2793. return 0;
  2794. }
  2795. /*==========================================
  2796. * スクリプトの実行メイン部分
  2797. *------------------------------------------*/
  2798. void run_script_main(struct script_state *st)
  2799. {
  2800. int cmdcount=script_config.check_cmdcount;
  2801. int gotocount=script_config.check_gotocount;
  2802. TBL_PC *sd;
  2803. //For backing up purposes
  2804. struct script_state *bk_st = NULL;
  2805. int bk_npcid = 0;
  2806. struct script_stack *stack=st->stack;
  2807. sd = st->rid?map_id2sd(st->rid):NULL;
  2808. if(sd){
  2809. if(sd->st != st){
  2810. bk_st = sd->st;
  2811. bk_npcid = sd->npc_id;
  2812. }
  2813. sd->st = st;
  2814. sd->npc_id = st->oid;
  2815. }
  2816. if(st->state == RERUNLINE) {
  2817. st->state = RUN;
  2818. run_func(st);
  2819. if(st->state == GOTO)
  2820. st->state = RUN;
  2821. } else if(st->state != END)
  2822. st->state = RUN;
  2823. while(st->state == RUN)
  2824. {
  2825. enum c_op c = get_com(st->script->script_buf,&st->pos);
  2826. switch(c){
  2827. case C_EOL:
  2828. if( stack->sp != stack->defsp )
  2829. {
  2830. if( stack->sp > stack->defsp )
  2831. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2832. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2833. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2834. } else
  2835. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2836. stack->sp = stack->defsp;
  2837. }
  2838. break;
  2839. case C_INT:
  2840. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2841. break;
  2842. case C_POS:
  2843. case C_NAME:
  2844. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2845. st->pos+=3;
  2846. break;
  2847. case C_ARG:
  2848. push_val(stack,c,0);
  2849. break;
  2850. case C_STR:
  2851. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2852. while(st->script->script_buf[st->pos++]);
  2853. break;
  2854. case C_FUNC:
  2855. run_func(st);
  2856. if(st->state==GOTO){
  2857. st->state = RUN;
  2858. if( gotocount>0 && (--gotocount)<=0 ){
  2859. ShowError("run_script: infinity loop !\n");
  2860. script_reportsrc(st);
  2861. st->state=END;
  2862. }
  2863. }
  2864. break;
  2865. case C_NEG:
  2866. case C_NOT:
  2867. case C_LNOT:
  2868. op_1(st ,c);
  2869. break;
  2870. case C_ADD:
  2871. case C_SUB:
  2872. case C_MUL:
  2873. case C_DIV:
  2874. case C_MOD:
  2875. case C_EQ:
  2876. case C_NE:
  2877. case C_GT:
  2878. case C_GE:
  2879. case C_LT:
  2880. case C_LE:
  2881. case C_AND:
  2882. case C_OR:
  2883. case C_XOR:
  2884. case C_LAND:
  2885. case C_LOR:
  2886. case C_R_SHIFT:
  2887. case C_L_SHIFT:
  2888. op_2(st, c);
  2889. break;
  2890. case C_OP3:
  2891. op_3(st, c);
  2892. break;
  2893. case C_NOP:
  2894. st->state=END;
  2895. break;
  2896. default:
  2897. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2898. st->state=END;
  2899. break;
  2900. }
  2901. if( cmdcount>0 && (--cmdcount)<=0 ){
  2902. ShowError("run_script: infinity loop !\n");
  2903. script_reportsrc(st);
  2904. st->state=END;
  2905. }
  2906. }
  2907. if(st->sleep.tick > 0) {
  2908. //Delay execution
  2909. st->sleep.charid = sd?sd->status.char_id:0;
  2910. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2911. run_script_timer, st->sleep.charid, (int)st);
  2912. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2913. //Restore previous script
  2914. if (sd) {
  2915. sd->st = bk_st;
  2916. sd->npc_id = bk_npcid;
  2917. bk_st = NULL; //Remove tag for removal.
  2918. }
  2919. }
  2920. else if(st->state != END && sd){
  2921. //Resume later (st is already attached to player).
  2922. if(bk_st) {
  2923. ShowWarning("Unable to restore stack! Double continuation!\n");
  2924. //Report BOTH scripts to see if that can help somehow.
  2925. ShowDebug("Previous script (lost):\n");
  2926. script_reportsrc(bk_st);
  2927. ShowDebug("Current script:\n");
  2928. script_reportsrc(st);
  2929. }
  2930. } else {
  2931. //Dispose of script.
  2932. if (sd)
  2933. { //Restore previous stack and save char.
  2934. if(sd->state.using_fake_npc){
  2935. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2936. sd->state.using_fake_npc = 0;
  2937. }
  2938. //Restore previous script if any.
  2939. sd->st = bk_st;
  2940. sd->npc_id = bk_npcid;
  2941. if (!bk_st)
  2942. npc_event_dequeue(sd);
  2943. else
  2944. bk_st = NULL; //Remove tag for removal.
  2945. if (sd->state.reg_dirty&2)
  2946. intif_saveregistry(sd,2);
  2947. if (sd->state.reg_dirty&1)
  2948. intif_saveregistry(sd,1);
  2949. }
  2950. st->pos = -1;
  2951. script_free_stack (st->stack);
  2952. aFree(st);
  2953. }
  2954. if (bk_st)
  2955. { //Remove previous script
  2956. bk_st->pos = -1;
  2957. script_free_stack(bk_st->stack);
  2958. aFree(bk_st);
  2959. bk_st = NULL;
  2960. }
  2961. }
  2962. /*==========================================
  2963. * マップ変数の変更
  2964. *------------------------------------------*/
  2965. int mapreg_setreg(int num, int val)
  2966. {
  2967. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2968. int i = num >> 24;
  2969. char* name = str_buf + str_data[num&0x00ffffff].str;
  2970. if( val != 0 ) {
  2971. if(idb_put(mapreg_db,num,(void*)val))
  2972. ;
  2973. else if(name[1] != '@') {
  2974. char tmp_str[32*2+1];
  2975. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  2976. 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) )
  2977. Sql_ShowDebug(mmysql_handle);
  2978. }
  2979. } else { // val == 0
  2980. if(name[1] != '@') { // Remove from database because it is unused.
  2981. 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) )
  2982. Sql_ShowDebug(mmysql_handle);
  2983. }
  2984. idb_remove(mapreg_db,num);
  2985. }
  2986. #else
  2987. if(val != 0)
  2988. idb_put(mapreg_db,num,(void*)val);
  2989. else
  2990. idb_remove(mapreg_db,num);
  2991. #endif
  2992. mapreg_dirty = 1;
  2993. return 1;
  2994. }
  2995. /*==========================================
  2996. * 文字列型マップ変数の変更
  2997. *------------------------------------------*/
  2998. int mapreg_setregstr(int num, const char* str)
  2999. {
  3000. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  3001. int i = num >> 24;
  3002. char* name = str_buf + str_data[num&0x00ffffff].str;
  3003. if( str==NULL || *str==0 ) {
  3004. if(name[1] != '@') {
  3005. 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) )
  3006. Sql_ShowDebug(mmysql_handle);
  3007. }
  3008. idb_remove(mapregstr_db,num);
  3009. mapreg_dirty = 1;
  3010. return 1;
  3011. }
  3012. if (idb_put(mapregstr_db,num, aStrdup(str)))
  3013. ;
  3014. else if(name[1] != '@') { //put returned null, so we must insert.
  3015. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  3016. char tmp_str[32*2+1];
  3017. char tmp_str2[255*2+1];
  3018. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  3019. Sql_EscapeStringLen(mmysql_handle, tmp_str2, str, strnlen(str, 255));
  3020. 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) )
  3021. Sql_ShowDebug(mmysql_handle);
  3022. }
  3023. #else
  3024. if( str==NULL || *str==0 )
  3025. idb_remove(mapregstr_db,num);
  3026. else
  3027. idb_put(mapregstr_db,num,aStrdup(str));
  3028. #endif
  3029. mapreg_dirty = 1;
  3030. return 1;
  3031. }
  3032. /*==========================================
  3033. * 永続的マップ変数の読み込み
  3034. *------------------------------------------*/
  3035. static int script_load_mapreg(void)
  3036. {
  3037. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3038. FILE* fp;
  3039. char line[1024];
  3040. if( (fp=fopen(mapreg_txt,"rt")) == NULL )
  3041. return -1;
  3042. while(fgets(line,sizeof(line),fp))
  3043. {
  3044. char buf1[256],buf2[1024];
  3045. char* p;
  3046. int n,v,s,i;
  3047. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  3048. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  3049. continue;
  3050. if( buf1[strlen(buf1)-1] == '$' ) {
  3051. if( sscanf(line + n, "%[^\n\r]", buf2) != 1 ) {
  3052. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3053. continue;
  3054. }
  3055. p = aStrdup(buf2);
  3056. s = add_str(buf1);
  3057. idb_put(mapregstr_db, (i<<24)|s, p);
  3058. } else {
  3059. if( sscanf(line + n, "%d", &v) != 1 ) {
  3060. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3061. continue;
  3062. }
  3063. s = add_str(buf1);
  3064. idb_put(mapreg_db, (i<<24)|s, (void*)v);
  3065. }
  3066. }
  3067. fclose(fp);
  3068. mapreg_dirty = 0;
  3069. return 0;
  3070. #else
  3071. /*
  3072. 0 1 2
  3073. +-------------------------+
  3074. | varname | index | value |
  3075. +-------------------------+
  3076. */
  3077. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  3078. char varname[32+1];
  3079. int index;
  3080. char value[255+1];
  3081. uint32 length;
  3082. if ( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `%s`, `%s`, `%s` FROM `%s`", mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, mapregsql_db)
  3083. || SQL_ERROR == SqlStmt_Execute(stmt)
  3084. ) {
  3085. SqlStmt_ShowDebug(stmt);
  3086. SqlStmt_Free(stmt);
  3087. return -1;
  3088. }
  3089. SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &varname[0], 32, &length, NULL);
  3090. SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &index, 0, NULL, NULL);
  3091. SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &value[0], 255, NULL, NULL);
  3092. while ( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  3093. {
  3094. if( varname[length-1] == '$' ) {
  3095. int s = add_str(varname);
  3096. int i = index;
  3097. char* p = aStrdup(value);
  3098. idb_put(mapregstr_db, (i<<24)|s, p);
  3099. } else {
  3100. int s = add_str(varname);
  3101. int i = index;
  3102. int v = atoi(value);
  3103. idb_put(mapreg_db, (i<<24)|s, (void *)v);
  3104. }
  3105. }
  3106. SqlStmt_Free(stmt);
  3107. mapreg_dirty = 0;
  3108. return 0;
  3109. #endif /* TXT_ONLY */
  3110. }
  3111. /*==========================================
  3112. * 永続的マップ変数の書き込み
  3113. *------------------------------------------*/
  3114. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  3115. {
  3116. int num=key.i&0x00ffffff, i=key.i>>24;
  3117. char *name=str_buf+str_data[num].str;
  3118. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3119. FILE *fp=va_arg(ap,FILE*);
  3120. if( name[1]!='@' ) {
  3121. if(i==0)
  3122. fprintf(fp,"%s\t%d\n", name, (int)data);
  3123. else
  3124. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  3125. }
  3126. #else
  3127. if ( name[1] != '@') {
  3128. 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) )
  3129. Sql_ShowDebug(mmysql_handle);
  3130. }
  3131. #endif
  3132. return 0;
  3133. }
  3134. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  3135. {
  3136. int num=key.i&0x00ffffff, i=key.i>>24;
  3137. char *name=str_buf+str_data[num].str;
  3138. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3139. FILE *fp=va_arg(ap,FILE*);
  3140. if( name[1]!='@' ) {
  3141. if(i==0)
  3142. fprintf(fp,"%s\t%s\n", name, (char *)data);
  3143. else
  3144. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  3145. }
  3146. #else
  3147. if ( name[1] != '@') {
  3148. char tmp_str2[2*255+1];
  3149. Sql_EscapeStringLen(mmysql_handle, tmp_str2, (char*)data, safestrnlen((char*)data, 255));
  3150. 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) )
  3151. Sql_ShowDebug(mmysql_handle);
  3152. }
  3153. #endif
  3154. return 0;
  3155. }
  3156. static int script_save_mapreg(void)
  3157. {
  3158. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3159. FILE *fp;
  3160. int lock;
  3161. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  3162. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  3163. return -1;
  3164. }
  3165. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  3166. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  3167. lock_fclose(fp,mapreg_txt,&lock);
  3168. #else
  3169. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub);
  3170. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  3171. #endif
  3172. mapreg_dirty=0;
  3173. return 0;
  3174. }
  3175. static int script_autosave_mapreg(int tid,unsigned int tick,int id,int data)
  3176. {
  3177. if(mapreg_dirty)
  3178. if (script_save_mapreg() == -1)
  3179. ShowError("Failed to save the mapreg data!\n");
  3180. return 0;
  3181. }
  3182. int script_config_read(char *cfgName)
  3183. {
  3184. int i;
  3185. char line[1024],w1[1024],w2[1024];
  3186. FILE *fp;
  3187. fp=fopen(cfgName,"r");
  3188. if(fp==NULL){
  3189. ShowError("file not found: [%s]\n", cfgName);
  3190. return 1;
  3191. }
  3192. while(fgets(line, sizeof(line), fp))
  3193. {
  3194. if(line[0] == '/' && line[1] == '/')
  3195. continue;
  3196. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3197. if(i!=2)
  3198. continue;
  3199. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3200. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3201. }
  3202. else if(strcmpi(w1,"check_cmdcount")==0) {
  3203. script_config.check_cmdcount = config_switch(w2);
  3204. }
  3205. else if(strcmpi(w1,"check_gotocount")==0) {
  3206. script_config.check_gotocount = config_switch(w2);
  3207. }
  3208. else if(strcmpi(w1,"input_min_value")==0) {
  3209. script_config.input_min_value = config_switch(w2);
  3210. }
  3211. else if(strcmpi(w1,"input_max_value")==0) {
  3212. script_config.input_max_value = config_switch(w2);
  3213. }
  3214. else if(strcmpi(w1,"import")==0){
  3215. script_config_read(w2);
  3216. }
  3217. }
  3218. fclose(fp);
  3219. return 0;
  3220. }
  3221. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3222. {
  3223. struct script_code *code = (struct script_code *)data;
  3224. if(code){
  3225. script_free_vars( &code->script_vars );
  3226. aFree( code->script_buf );
  3227. aFree( code );
  3228. }
  3229. return 0;
  3230. }
  3231. /*==========================================
  3232. * 終了
  3233. *------------------------------------------*/
  3234. int do_final_script()
  3235. {
  3236. #ifdef DEBUG_HASH
  3237. if (battle_config.etc_log)
  3238. {
  3239. FILE *fp = fopen("hash_dump.txt","wt");
  3240. if(fp) {
  3241. int i,count[SCRIPT_HASH_SIZE];
  3242. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3243. int n=0;
  3244. int min=INT_MAX,max=0,zero=0;
  3245. double mean=0.0f;
  3246. double median=0.0f;
  3247. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3248. memset(count, 0, sizeof(count));
  3249. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3250. fprintf(fp,"---------------------------------------------------------------\n");
  3251. for(i=LABEL_START; i<str_num; i++) {
  3252. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3253. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3254. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3255. ++count[h];
  3256. }
  3257. fprintf(fp,"--------------------\n\n");
  3258. memset(count2, 0, sizeof(count2));
  3259. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3260. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3261. if(min > count[i])
  3262. min = count[i]; // minimun count of collision
  3263. if(max < count[i])
  3264. max = count[i]; // maximun count of collision
  3265. if(count[i] == 0)
  3266. zero++;
  3267. ++count2[count[i]];
  3268. }
  3269. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3270. for( i=min; i <= max; ++i ){
  3271. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3272. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3273. }
  3274. for( i=min; i <= max; ++i ){
  3275. n += count2[i];
  3276. if( n*2 >= SCRIPT_HASH_SIZE )
  3277. {
  3278. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3279. median = (i+i+1)/2.0f;
  3280. else
  3281. median = i;
  3282. break;
  3283. }
  3284. }
  3285. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3286. fclose(fp);
  3287. }
  3288. }
  3289. #endif
  3290. if(mapreg_dirty>=0)
  3291. script_save_mapreg();
  3292. mapreg_db->destroy(mapreg_db,NULL);
  3293. mapregstr_db->destroy(mapregstr_db,NULL);
  3294. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3295. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3296. if(sleep_db) {
  3297. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3298. while(n) {
  3299. struct script_state *st = (struct script_state *)n->data;
  3300. script_free_stack(st->stack);
  3301. aFree(st);
  3302. n = n->next;
  3303. }
  3304. linkdb_final(&sleep_db);
  3305. }
  3306. if (str_data)
  3307. aFree(str_data);
  3308. if (str_buf)
  3309. aFree(str_buf);
  3310. return 0;
  3311. }
  3312. /*==========================================
  3313. * 初期化
  3314. *------------------------------------------*/
  3315. int do_init_script()
  3316. {
  3317. mapreg_db= idb_alloc(DB_OPT_BASE);
  3318. mapregstr_db=idb_alloc(DB_OPT_RELEASE_DATA);
  3319. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3320. scriptlabel_db=strdb_alloc((DBOptions)(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA),50);
  3321. script_load_mapreg();
  3322. add_timer_func_list(script_autosave_mapreg, "script_autosave_mapreg");
  3323. add_timer_interval(gettick() + MAPREG_AUTOSAVE_INTERVAL, script_autosave_mapreg, 0, 0, MAPREG_AUTOSAVE_INTERVAL);
  3324. return 0;
  3325. }
  3326. int script_reload()
  3327. {
  3328. if(mapreg_dirty>=0)
  3329. script_save_mapreg();
  3330. mapreg_db->clear(mapreg_db, NULL);
  3331. mapregstr_db->clear(mapregstr_db, NULL);
  3332. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3333. scriptlabel_db->clear(scriptlabel_db, NULL);
  3334. if(sleep_db) {
  3335. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3336. while(n) {
  3337. struct script_state *st = (struct script_state *)n->data;
  3338. if( st->sleep.timer != INVALID_TIMER )
  3339. delete_timer(st->sleep.timer, run_script_timer);
  3340. script_free_stack(st->stack);
  3341. aFree(st);
  3342. n = n->next;
  3343. }
  3344. linkdb_final(&sleep_db);
  3345. }
  3346. script_load_mapreg();
  3347. return 0;
  3348. }
  3349. //-----------------------------------------------------------------------------
  3350. // buildin functions
  3351. //
  3352. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3353. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3354. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3355. /////////////////////////////////////////////////////////////////////
  3356. // NPC interaction
  3357. //
  3358. /// Appends a message to the npc dialog.
  3359. /// If a dialog doesn't exist yet, one is created.
  3360. ///
  3361. /// mes "<message>";
  3362. BUILDIN_FUNC(mes)
  3363. {
  3364. TBL_PC* sd = script_rid2sd(st);
  3365. if( sd == NULL )
  3366. return 0;
  3367. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3368. return 0;
  3369. }
  3370. /// Displays the button 'next' in the npc dialog.
  3371. /// The dialog text is cleared and the script continues when the button is pressed.
  3372. ///
  3373. /// next;
  3374. BUILDIN_FUNC(next)
  3375. {
  3376. TBL_PC* sd;
  3377. sd = script_rid2sd(st);
  3378. if( sd == NULL )
  3379. return 0;
  3380. st->state = STOP;
  3381. clif_scriptnext(sd, st->oid);
  3382. return 0;
  3383. }
  3384. /// Ends the script and displays the button 'close' on the npc dialog.
  3385. /// The dialog is closed when the button is pressed.
  3386. ///
  3387. /// close;
  3388. BUILDIN_FUNC(close)
  3389. {
  3390. TBL_PC* sd;
  3391. sd = script_rid2sd(st);
  3392. if( sd == NULL )
  3393. return 0;
  3394. st->state = END;
  3395. clif_scriptclose(sd, st->oid);
  3396. return 0;
  3397. }
  3398. /// Displays the button 'close' on the npc dialog.
  3399. /// The dialog is closed and the script continues when the button is pressed.
  3400. ///
  3401. /// close2;
  3402. BUILDIN_FUNC(close2)
  3403. {
  3404. TBL_PC* sd;
  3405. sd = script_rid2sd(st);
  3406. if( sd == NULL )
  3407. return 0;
  3408. st->state = STOP;
  3409. clif_scriptclose(sd, st->oid);
  3410. return 0;
  3411. }
  3412. /// Counts the number of valid and total number of options in 'str'
  3413. /// If max_count > 0 the counting stops when that valid option is reached
  3414. /// total is incremented for each option (NULL is supported)
  3415. static int menu_countoptions(const char* str, int max_count, int* total)
  3416. {
  3417. int count = 0;
  3418. int bogus_total;
  3419. if( total == NULL )
  3420. total = &bogus_total;
  3421. ++(*total);
  3422. // initial empty options
  3423. while( *str == ':' )
  3424. {
  3425. ++str;
  3426. ++(*total);
  3427. }
  3428. // count menu options
  3429. while( *str != '\0' )
  3430. {
  3431. ++count;
  3432. --max_count;
  3433. if( max_count == 0 )
  3434. break;
  3435. while( *str != ':' && *str != '\0' )
  3436. ++str;
  3437. while( *str == ':' )
  3438. {
  3439. ++str;
  3440. ++(*total);
  3441. }
  3442. }
  3443. return count;
  3444. }
  3445. /// Displays a menu with options and goes to the target label.
  3446. /// The script is stopped if cancel is pressed.
  3447. /// Options with no text are not displayed in the client.
  3448. ///
  3449. /// Options can be grouped together, separated by the character ':' in the text:
  3450. /// ex: menu "A:B:C",L_target;
  3451. /// All these options go to the specified target label.
  3452. ///
  3453. /// The index of the selected option is put in the variable @menu.
  3454. /// Indexes start with 1 and are consistent with grouped and empty options.
  3455. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3456. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3457. ///
  3458. /// NOTE: the client closes the npc dialog when cancel is pressed
  3459. ///
  3460. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3461. BUILDIN_FUNC(menu)
  3462. {
  3463. int i;
  3464. const char* text;
  3465. TBL_PC* sd;
  3466. sd = script_rid2sd(st);
  3467. if( sd == NULL )
  3468. return 0;
  3469. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3470. if( sd->state.menu_or_input == 0 )
  3471. {
  3472. struct StringBuf buf;
  3473. struct script_data* data;
  3474. if( script_lastdata(st) % 2 == 0 )
  3475. {// argument count is not even (1st argument is at index 2)
  3476. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3477. st->state = END;
  3478. return 1;
  3479. }
  3480. StringBuf_Init(&buf);
  3481. for( i = 2, sd->npc_menu = 0; i < script_lastdata(st); i += 2 )
  3482. {
  3483. // menu options
  3484. text = script_getstr(st, i);
  3485. // target label
  3486. data = script_getdata(st, i+1);
  3487. if( !data_islabel(data) )
  3488. {// not a label
  3489. StringBuf_Destroy(&buf);
  3490. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3491. script_reportdata(data);
  3492. st->state = END;
  3493. return 1;
  3494. }
  3495. // append option(s)
  3496. if( text[0] == '\0' )
  3497. continue;// empty string, ignore
  3498. if( sd->npc_menu > 0 )
  3499. StringBuf_AppendStr(&buf, ":");
  3500. StringBuf_AppendStr(&buf, text);
  3501. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3502. }
  3503. st->state = RERUNLINE;
  3504. sd->state.menu_or_input = 1;
  3505. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3506. StringBuf_Destroy(&buf);
  3507. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3508. }
  3509. else if( sd->npc_menu == 0xff )
  3510. {// Cancel was pressed
  3511. sd->state.menu_or_input = 0;
  3512. st->state = END;
  3513. }
  3514. else
  3515. {// goto target label
  3516. int menu = 0;
  3517. sd->state.menu_or_input = 0;
  3518. if( sd->npc_menu <= 0 )
  3519. {
  3520. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3521. st->state = END;
  3522. return 1;
  3523. }
  3524. // get target label
  3525. for( i = 2; i < script_lastdata(st); i += 2 )
  3526. {
  3527. text = script_getstr(st, i);
  3528. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3529. if( sd->npc_menu <= 0 )
  3530. break;// entry found
  3531. }
  3532. if( sd->npc_menu > 0 )
  3533. {// Invalid selection
  3534. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3535. st->state = END;
  3536. return 1;
  3537. }
  3538. if( !data_islabel(script_getdata(st, i + 1)) )
  3539. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3540. ShowError("script:menu: unexpected data in label argument\n");
  3541. script_reportdata(script_getdata(st, i + 1));
  3542. st->state = END;
  3543. return 1;
  3544. }
  3545. pc_setreg(sd, add_str("@menu"), menu);
  3546. st->pos = script_getnum(st, i + 1);
  3547. st->state = GOTO;
  3548. }
  3549. return 0;
  3550. }
  3551. /// Displays a menu with options and returns the selected option.
  3552. /// Behaves like 'menu' without the target labels.
  3553. ///
  3554. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3555. ///
  3556. /// @see menu
  3557. BUILDIN_FUNC(select)
  3558. {
  3559. int i;
  3560. const char* text;
  3561. TBL_PC* sd;
  3562. sd = script_rid2sd(st);
  3563. if( sd == NULL )
  3564. return 0;
  3565. if( sd->state.menu_or_input == 0 )
  3566. {
  3567. struct StringBuf buf;
  3568. StringBuf_Init(&buf);
  3569. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  3570. {
  3571. text = script_getstr(st, i);
  3572. if( sd->npc_menu > 0 )
  3573. StringBuf_AppendStr(&buf, ":");
  3574. StringBuf_AppendStr(&buf, text);
  3575. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  3576. }
  3577. st->state = RERUNLINE;
  3578. sd->state.menu_or_input = 1;
  3579. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3580. StringBuf_Destroy(&buf);
  3581. }
  3582. else if( sd->npc_menu == 0xff )
  3583. {// Cancel was pressed
  3584. sd->state.menu_or_input = 0;
  3585. st->state = END;
  3586. }
  3587. else
  3588. {// return selected option
  3589. int menu = 0;
  3590. sd->state.menu_or_input = 0;
  3591. for( i = 2; i <= script_lastdata(st); ++i )
  3592. {
  3593. text = script_getstr(st, i);
  3594. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3595. if( sd->npc_menu <= 0 )
  3596. break;// entry found
  3597. }
  3598. pc_setreg(sd, add_str("@menu"), menu);
  3599. script_pushint(st, menu);
  3600. }
  3601. return 0;
  3602. }
  3603. /// Displays a menu with options and returns the selected option.
  3604. /// Behaves like 'menu' without the target labels, except when cancel is
  3605. /// pressed.
  3606. /// When cancel is pressed, the script continues and 255 is returned.
  3607. ///
  3608. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3609. ///
  3610. /// @see menu
  3611. BUILDIN_FUNC(prompt)
  3612. {
  3613. int i;
  3614. const char *text;
  3615. TBL_PC* sd;
  3616. sd = script_rid2sd(st);
  3617. if( sd == NULL )
  3618. return 0;
  3619. if( sd->state.menu_or_input == 0 )
  3620. {
  3621. struct StringBuf buf;
  3622. StringBuf_Init(&buf);
  3623. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  3624. {
  3625. text = script_getstr(st, i);
  3626. if( sd->npc_menu > 0 )
  3627. StringBuf_AppendStr(&buf, ":");
  3628. StringBuf_AppendStr(&buf, text);
  3629. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  3630. }
  3631. st->state = RERUNLINE;
  3632. sd->state.menu_or_input = 1;
  3633. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3634. StringBuf_Destroy(&buf);
  3635. }
  3636. else if( sd->npc_menu == 0xff )
  3637. {// Cancel was pressed
  3638. sd->state.menu_or_input = 0;
  3639. pc_setreg(sd, add_str("@menu"), 0xff);
  3640. script_pushint(st, 0xff);
  3641. }
  3642. else
  3643. {// return selected option
  3644. int menu = 0;
  3645. sd->state.menu_or_input = 0;
  3646. for( i = 2; i <= script_lastdata(st); ++i )
  3647. {
  3648. text = script_getstr(st, i);
  3649. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3650. if( sd->npc_menu <= 0 )
  3651. break;// entry found
  3652. }
  3653. pc_setreg(sd, add_str("@menu"), menu);
  3654. script_pushint(st, menu);
  3655. }
  3656. return 0;
  3657. }
  3658. /////////////////////////////////////////////////////////////////////
  3659. // ...
  3660. //
  3661. /// Jumps to the target script label.
  3662. ///
  3663. /// goto <label>;
  3664. BUILDIN_FUNC(goto)
  3665. {
  3666. if( !data_islabel(script_getdata(st,2)) )
  3667. {
  3668. ShowError("script:goto: not a label\n");
  3669. script_reportdata(script_getdata(st,2));
  3670. st->state = END;
  3671. return 1;
  3672. }
  3673. st->pos = script_getnum(st,2);
  3674. st->state = GOTO;
  3675. return 0;
  3676. }
  3677. /*==========================================
  3678. * ユーザー定義関数の呼び出し
  3679. *------------------------------------------*/
  3680. BUILDIN_FUNC(callfunc)
  3681. {
  3682. int i, j;
  3683. struct linkdb_node** oldval;
  3684. struct script_code *scr, *oldscr;
  3685. const char* str = script_getstr(st,2);
  3686. scr = (struct script_code*)strdb_get(userfunc_db, str);
  3687. if( !scr )
  3688. {
  3689. ShowError("script:callfunc: function not found! [%s]\n", str);
  3690. st->state = END;
  3691. return 1;
  3692. }
  3693. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3694. push_copy(st->stack,i);
  3695. script_pushint(st,j); // 引数の数をプッシュ
  3696. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3697. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3698. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3699. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3700. oldscr = st->script;
  3701. oldval = st->stack->var_function;
  3702. st->pos = 0;
  3703. st->script = scr;
  3704. st->stack->defsp = st->start+5+j;
  3705. st->state = GOTO;
  3706. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3707. // ' 変数の引き継ぎ
  3708. for( i = 0; i < j; i++ )
  3709. {
  3710. struct script_data* s = script_getdatatop(st, -6-i);
  3711. if( data_isreference(s) && !s->ref )
  3712. {
  3713. char* name = str_buf + str_data[s->u.num&0x00ffffff].str;
  3714. // '@ 変数の引き継ぎ
  3715. if( name[0] == '.' && name[1] == '@' ) {
  3716. s->ref = oldval;
  3717. } else if( name[0] == '.' ) {
  3718. s->ref = &oldscr->script_vars;
  3719. }
  3720. }
  3721. }
  3722. return 0;
  3723. }
  3724. /*==========================================
  3725. * サブルーティンの呼び出し
  3726. *------------------------------------------*/
  3727. BUILDIN_FUNC(callsub)
  3728. {
  3729. int pos=script_getnum(st,2);
  3730. int i,j;
  3731. if(!data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2))) {
  3732. ShowError("script:callsub: argument is not a label\n");
  3733. script_reportdata(script_getdata(st,2));
  3734. st->state=END;
  3735. return 1;
  3736. } else {
  3737. struct linkdb_node **oldval = st->stack->var_function;
  3738. for(i=st->start+3,j=0;i<st->end;i++,j++)
  3739. push_copy(st->stack,i);
  3740. script_pushint(st,j); // 引数の数をプッシュ
  3741. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3742. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3743. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3744. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3745. st->pos=pos;
  3746. st->stack->defsp=st->start+5+j;
  3747. st->state=GOTO;
  3748. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3749. // ' 変数の引き継ぎ
  3750. for(i = 0; i < j; i++) {
  3751. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  3752. if( data_isreference(s) && !s->ref ) {
  3753. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  3754. // '@ 変数の引き継ぎ
  3755. if( name[0] == '.' && name[1] == '@' ) {
  3756. s->ref = oldval;
  3757. }
  3758. }
  3759. }
  3760. }
  3761. return 0;
  3762. }
  3763. /// Retrieves an argument provided to callfunc/callsub.
  3764. /// If the argument doesn't exist
  3765. ///
  3766. /// getarg(<index>{,<default_value>}) -> <value>
  3767. BUILDIN_FUNC(getarg)
  3768. {
  3769. int idx;
  3770. int count;
  3771. int stsp;
  3772. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3773. {
  3774. ShowError("script:getarg: no callfunc or callsub!\n");
  3775. st->state = END;
  3776. return 1;
  3777. }
  3778. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  3779. stsp = st->stack->defsp - count - 5;
  3780. idx = script_getnum(st,2);
  3781. if( idx < count )
  3782. push_copy(st->stack, stsp + idx);
  3783. else if( script_hasdata(st,3) )
  3784. script_pushcopy(st, 3);
  3785. else
  3786. {
  3787. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  3788. st->state = END;
  3789. return 1;
  3790. }
  3791. return 0;
  3792. }
  3793. /// Returns from the current function, optionaly returning a value from the functions.
  3794. /// Don't use outside script functions.
  3795. ///
  3796. /// return;
  3797. /// return <value>;
  3798. BUILDIN_FUNC(return)
  3799. {
  3800. if( script_hasdata(st,2) )
  3801. {// return value
  3802. struct script_data* data;
  3803. script_pushcopy(st, 2);
  3804. data = script_getdatatop(st, -1);
  3805. if( data_isreference(data) )
  3806. {
  3807. char* name = reference_getname(data);
  3808. if( name[0] == '.' && name[1] == '@' )
  3809. {// temporary script variable, convert to value
  3810. get_val(st, data);
  3811. if( data_isstring(data) )
  3812. {// duplicate the string
  3813. data->type = C_STR;
  3814. data->u.str = aStrdup(data->u.str);
  3815. }
  3816. }
  3817. else if( name[0] == '.' && !data->ref )
  3818. {// script variable, link to current script
  3819. data->ref = &st->script->script_vars;
  3820. }
  3821. }
  3822. }
  3823. else
  3824. {// no return value
  3825. script_pushnil(st);
  3826. }
  3827. st->state = RETFUNC;
  3828. return 0;
  3829. }
  3830. /// Returns a random number from 0 to <range>-1.
  3831. /// Or returns a random number from <min> to <max>.
  3832. /// If <min> is greater than <max>, their numbers are switched.
  3833. /// rand(<range>) -> <int>
  3834. /// rand(<min>,<max>) -> <int>
  3835. BUILDIN_FUNC(rand)
  3836. {
  3837. int range;
  3838. int min;
  3839. int max;
  3840. if( script_hasdata(st,3) )
  3841. {// min,max
  3842. min = script_getnum(st,2);
  3843. max = script_getnum(st,3);
  3844. if( max < min )
  3845. swap(min, max);
  3846. range = max - min + 1;
  3847. }
  3848. else
  3849. {// range
  3850. min = 0;
  3851. range = script_getnum(st,2);
  3852. }
  3853. if( range <= 1 )
  3854. script_pushint(st, min);
  3855. else
  3856. script_pushint(st, rand()%range + min);
  3857. return 0;
  3858. }
  3859. /*==========================================
  3860. *
  3861. *------------------------------------------*/
  3862. BUILDIN_FUNC(warp)
  3863. {
  3864. int ret;
  3865. int x,y;
  3866. const char* str;
  3867. TBL_PC* sd;
  3868. sd = script_rid2sd(st);
  3869. if( sd == NULL )
  3870. return 0;
  3871. str = script_getstr(st,2);
  3872. x = script_getnum(st,3);
  3873. y = script_getnum(st,4);
  3874. if(strcmp(str,"Random")==0)
  3875. ret = pc_randomwarp(sd,3);
  3876. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3877. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3878. else
  3879. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3880. if( ret ) {
  3881. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3882. script_reportsrc(st);
  3883. }
  3884. return 0;
  3885. }
  3886. /*==========================================
  3887. * エリア指定ワープ
  3888. *------------------------------------------*/
  3889. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3890. {
  3891. int x,y;
  3892. unsigned int map;
  3893. map=va_arg(ap, unsigned int);
  3894. x=va_arg(ap,int);
  3895. y=va_arg(ap,int);
  3896. if(map == 0)
  3897. pc_randomwarp((TBL_PC *)bl,3);
  3898. else
  3899. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3900. return 0;
  3901. }
  3902. BUILDIN_FUNC(areawarp)
  3903. {
  3904. int x,y,m;
  3905. unsigned int index;
  3906. const char *str;
  3907. const char *mapname;
  3908. int x0,y0,x1,y1;
  3909. mapname=script_getstr(st,2);
  3910. x0=script_getnum(st,3);
  3911. y0=script_getnum(st,4);
  3912. x1=script_getnum(st,5);
  3913. y1=script_getnum(st,6);
  3914. str=script_getstr(st,7);
  3915. x=script_getnum(st,8);
  3916. y=script_getnum(st,9);
  3917. if( (m=map_mapname2mapid(mapname))< 0)
  3918. return 0;
  3919. if(strcmp(str,"Random")==0)
  3920. index = 0;
  3921. else if(!(index=mapindex_name2id(str)))
  3922. return 0;
  3923. map_foreachinarea(buildin_areawarp_sub,
  3924. m,x0,y0,x1,y1,BL_PC, index,x,y );
  3925. return 0;
  3926. }
  3927. /*==========================================
  3928. * warpchar [LuzZza]
  3929. * Useful for warp one player from
  3930. * another player npc-session.
  3931. * Using: warpchar "mapname",x,y,Char_ID;
  3932. *------------------------------------------*/
  3933. BUILDIN_FUNC(warpchar)
  3934. {
  3935. int x,y,a;
  3936. const char *str;
  3937. TBL_PC *sd;
  3938. str=script_getstr(st,2);
  3939. x=script_getnum(st,3);
  3940. y=script_getnum(st,4);
  3941. a=script_getnum(st,5);
  3942. sd = map_charid2sd(a);
  3943. if( sd == NULL )
  3944. return 0;
  3945. if(strcmp(str, "Random") == 0)
  3946. pc_randomwarp(sd, 3);
  3947. else
  3948. if(strcmp(str, "SavePoint") == 0)
  3949. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3950. else
  3951. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3952. return 0;
  3953. }
  3954. /*==========================================
  3955. * Warpparty - [Fredzilla]
  3956. * Syntax: warpparty "mapname",x,y,Party_ID;
  3957. *------------------------------------------*/
  3958. BUILDIN_FUNC(warpparty)
  3959. {
  3960. int x,y;
  3961. const char *str;
  3962. int p_id;
  3963. int i;
  3964. unsigned short mapindex;
  3965. TBL_PC *pl_sd;
  3966. struct party_data *p=NULL;
  3967. str=script_getstr(st,2);
  3968. x=script_getnum(st,3);
  3969. y=script_getnum(st,4);
  3970. p_id=script_getnum(st,5);
  3971. if(p_id < 1)
  3972. return 0;
  3973. p = party_search(p_id);
  3974. if (!p)
  3975. return 0;
  3976. if(strcmp(str,"Random")==0)
  3977. {
  3978. for (i = 0; i < MAX_PARTY; i++)
  3979. {
  3980. if ((pl_sd = p->data[i].sd))
  3981. {
  3982. if(map[pl_sd->bl.m].flag.nowarp)
  3983. continue;
  3984. pc_randomwarp(pl_sd,3);
  3985. }
  3986. }
  3987. }
  3988. else if(strcmp(str,"SavePointAll")==0)
  3989. {
  3990. for (i = 0; i < MAX_PARTY; i++)
  3991. {
  3992. if ((pl_sd = p->data[i].sd))
  3993. {
  3994. if(map[pl_sd->bl.m].flag.noreturn)
  3995. continue;
  3996. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  3997. }
  3998. }
  3999. }
  4000. else if(strcmp(str,"SavePoint")==0)
  4001. {
  4002. pl_sd=script_rid2sd(st);
  4003. if (!pl_sd) return 0;
  4004. mapindex=pl_sd->status.save_point.map;
  4005. x=pl_sd->status.save_point.x;
  4006. y=pl_sd->status.save_point.y;
  4007. for (i = 0; i < MAX_PARTY; i++)
  4008. {
  4009. if ((pl_sd = p->data[i].sd))
  4010. {
  4011. if(map[pl_sd->bl.m].flag.noreturn)
  4012. continue;
  4013. pc_setpos(pl_sd,mapindex,x,y,3);
  4014. }
  4015. }
  4016. }
  4017. else if(strcmp(str,"Leader")==0)
  4018. {
  4019. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4020. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4021. return 0;
  4022. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  4023. return 0;
  4024. mapindex = p->data[i].sd->mapindex;
  4025. x = p->data[i].sd->bl.x;
  4026. y = p->data[i].sd->bl.y;
  4027. for (i = 0; i < MAX_PARTY; i++)
  4028. {
  4029. pl_sd = p->data[i].sd;
  4030. if (!pl_sd)
  4031. continue;
  4032. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4033. continue;
  4034. pc_setpos(pl_sd,mapindex,x,y,3);
  4035. }
  4036. }
  4037. else
  4038. {
  4039. mapindex = mapindex_name2id(str);
  4040. if (!mapindex) //Show source of npc error.
  4041. return 1;
  4042. for (i = 0; i < MAX_PARTY; i++)
  4043. {
  4044. if ((pl_sd = p->data[i].sd))
  4045. {
  4046. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4047. continue;
  4048. pc_setpos(pl_sd,mapindex,x,y,3);
  4049. }
  4050. }
  4051. }
  4052. return 0;
  4053. }
  4054. /*==========================================
  4055. * Warpguild - [Fredzilla]
  4056. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4057. *------------------------------------------*/
  4058. BUILDIN_FUNC(warpguild)
  4059. {
  4060. TBL_PC *sd;
  4061. TBL_PC *pl_sd;
  4062. struct guild* g;
  4063. struct s_mapiterator* iter;
  4064. int type;
  4065. const char* str = script_getstr(st,2);
  4066. int x = script_getnum(st,3);
  4067. int y = script_getnum(st,4);
  4068. int gid = script_getnum(st,5);
  4069. sd=script_rid2sd(st);
  4070. if( sd == NULL )
  4071. return 0;
  4072. g = guild_search(gid);
  4073. if( g == NULL )
  4074. return 0;
  4075. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4076. return 0;
  4077. type = ( strcmp(str,"Random")==0 ) ? 0
  4078. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4079. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4080. : 3;
  4081. iter = mapit_getallusers();
  4082. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4083. {
  4084. if( pl_sd->status.guild_id != gid )
  4085. continue;
  4086. switch( type )
  4087. {
  4088. case 0: // Random
  4089. if(!map[pl_sd->bl.m].flag.nowarp)
  4090. pc_randomwarp(pl_sd,3);
  4091. break;
  4092. case 1: // SavePointAll
  4093. if(!map[pl_sd->bl.m].flag.noreturn)
  4094. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4095. break;
  4096. case 2: // SavePoint
  4097. if(!map[pl_sd->bl.m].flag.noreturn)
  4098. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  4099. break;
  4100. case 3: // m,x,y
  4101. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4102. pc_setpos(pl_sd,mapindex_name2id(str),x,y,3);
  4103. break;
  4104. }
  4105. }
  4106. mapit_free(iter);
  4107. return 0;
  4108. }
  4109. /*==========================================
  4110. *
  4111. *------------------------------------------*/
  4112. BUILDIN_FUNC(heal)
  4113. {
  4114. TBL_PC *sd;
  4115. int hp,sp;
  4116. sd = script_rid2sd(st);
  4117. if (!sd) return 0;
  4118. hp=script_getnum(st,2);
  4119. sp=script_getnum(st,3);
  4120. status_heal(&sd->bl, hp, sp, 1);
  4121. return 0;
  4122. }
  4123. /*==========================================
  4124. *
  4125. *------------------------------------------*/
  4126. BUILDIN_FUNC(itemheal)
  4127. {
  4128. TBL_PC *sd;
  4129. int hp,sp;
  4130. hp=script_getnum(st,2);
  4131. sp=script_getnum(st,3);
  4132. if(potion_flag==1) {
  4133. potion_hp = hp;
  4134. potion_sp = sp;
  4135. return 0;
  4136. }
  4137. sd = script_rid2sd(st);
  4138. if (!sd) return 0;
  4139. pc_itemheal(sd,sd->itemid,hp,sp);
  4140. return 0;
  4141. }
  4142. /*==========================================
  4143. *
  4144. *------------------------------------------*/
  4145. BUILDIN_FUNC(percentheal)
  4146. {
  4147. int hp,sp;
  4148. TBL_PC* sd;
  4149. hp=script_getnum(st,2);
  4150. sp=script_getnum(st,3);
  4151. if(potion_flag==1) {
  4152. potion_per_hp = hp;
  4153. potion_per_sp = sp;
  4154. return 0;
  4155. }
  4156. sd = script_rid2sd(st);
  4157. if( sd == NULL )
  4158. return 0;
  4159. pc_percentheal(sd,hp,sp);
  4160. return 0;
  4161. }
  4162. /*==========================================
  4163. *
  4164. *------------------------------------------*/
  4165. BUILDIN_FUNC(jobchange)
  4166. {
  4167. int job, upper=-1;
  4168. job=script_getnum(st,2);
  4169. if( script_hasdata(st,3) )
  4170. upper=script_getnum(st,3);
  4171. if (pcdb_checkid(job))
  4172. {
  4173. TBL_PC* sd;
  4174. sd = script_rid2sd(st);
  4175. if( sd == NULL )
  4176. return 0;
  4177. pc_jobchange(sd, job, upper);
  4178. if(use_irc && irc_announce_jobchange_flag)
  4179. irc_announce_jobchange(sd);
  4180. }
  4181. return 0;
  4182. }
  4183. /*==========================================
  4184. *
  4185. *------------------------------------------*/
  4186. BUILDIN_FUNC(jobname)
  4187. {
  4188. int class_=script_getnum(st,2);
  4189. script_pushconststr(st,job_name(class_));
  4190. return 0;
  4191. }
  4192. /// Get input from the player.
  4193. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4194. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4195. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4196. /// shorter than 'min' and 0 otherwise.
  4197. ///
  4198. /// input(<var>{,<min>{,<max>}}) -> <int>
  4199. BUILDIN_FUNC(input)
  4200. {
  4201. TBL_PC* sd;
  4202. struct script_data* data;
  4203. int uid;
  4204. char* name;
  4205. int min;
  4206. int max;
  4207. sd = script_rid2sd(st);
  4208. if( sd == NULL )
  4209. return 0;
  4210. data = script_getdata(st,2);
  4211. if( !data_isreference(data) ){
  4212. ShowError("script:input: not a variable\n");
  4213. script_reportdata(data);
  4214. st->state = END;
  4215. return 1;
  4216. }
  4217. uid = reference_getuid(data);
  4218. name = reference_getname(data);
  4219. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4220. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4221. if( !sd->state.menu_or_input )
  4222. { // first invocation, display npc input box
  4223. sd->state.menu_or_input = 1;
  4224. st->state = RERUNLINE;
  4225. if( is_string_variable(name) )
  4226. clif_scriptinputstr(sd,st->oid);
  4227. else
  4228. clif_scriptinput(sd,st->oid);
  4229. }
  4230. else
  4231. { // take received text/value and store it in the designated variable
  4232. sd->state.menu_or_input = 0;
  4233. if( is_string_variable(name) )
  4234. {
  4235. size_t len = strlen(sd->npc_str);
  4236. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4237. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4238. }
  4239. else
  4240. {
  4241. int amount = sd->npc_amount;
  4242. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4243. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4244. }
  4245. }
  4246. return 0;
  4247. }
  4248. /*==========================================
  4249. * 変数設定
  4250. *------------------------------------------*/
  4251. BUILDIN_FUNC(set)
  4252. {
  4253. TBL_PC *sd=NULL;
  4254. int num=st->stack->stack_data[st->start+2].u.num;
  4255. char *name=str_buf+str_data[num&0x00ffffff].str;
  4256. char prefix=*name;
  4257. char postfix=name[strlen(name)-1];
  4258. if( !data_isreference(script_getdata(st,2)) ){
  4259. ShowError("script:set: not a variable\n");
  4260. script_reportdata(script_getdata(st,2));
  4261. st->state = END;
  4262. return 1;
  4263. }
  4264. if(not_server_variable(prefix))
  4265. {
  4266. sd=script_rid2sd(st);
  4267. if( sd == NULL )
  4268. {
  4269. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4270. return 0;
  4271. }
  4272. }
  4273. if( postfix=='$' ){
  4274. // 文字列
  4275. const char *str = script_getstr(st,3);
  4276. set_reg(st,sd,num,name,(void*)str,script_getref(st,2));
  4277. }else{
  4278. // 数値
  4279. int val = script_getnum(st,3);
  4280. set_reg(st,sd,num,name,(void*)val,script_getref(st,2));
  4281. }
  4282. return 0;
  4283. }
  4284. /////////////////////////////////////////////////////////////////////
  4285. /// Array variables
  4286. ///
  4287. /// Returns the size of the specified array
  4288. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4289. {
  4290. int32 ret = idx;
  4291. if( isstring )
  4292. {
  4293. for( ; idx < 128; ++idx )
  4294. {
  4295. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4296. if( str && *str )
  4297. ret = idx + 1;
  4298. script_removetop(st, -1, 0);
  4299. }
  4300. }
  4301. else
  4302. {
  4303. for( ; idx < 128; ++idx )
  4304. {
  4305. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4306. if( num )
  4307. ret = idx + 1;
  4308. script_removetop(st, -1, 0);
  4309. }
  4310. }
  4311. return ret;
  4312. }
  4313. /// Sets values of an array, from the starting index.
  4314. /// ex: setarray arr[1],1,2,3;
  4315. ///
  4316. /// setarray <array variable>,<value1>{,<value2>...};
  4317. BUILDIN_FUNC(setarray)
  4318. {
  4319. struct script_data* data;
  4320. char* name;
  4321. int32 start;
  4322. int32 end;
  4323. int32 id;
  4324. int32 i;
  4325. TBL_PC* sd = NULL;
  4326. data = script_getdata(st, 2);
  4327. if( !data_isreference(data) )
  4328. {
  4329. ShowError("script:setarray: not a variable\n");
  4330. script_reportdata(data);
  4331. st->state = END;
  4332. return 1;// not a variable
  4333. }
  4334. id = reference_getid(data);
  4335. start = reference_getindex(data);
  4336. name = reference_getname(data);
  4337. if( not_array_variable(*name) )
  4338. {
  4339. ShowError("script:setarray: illegal scope\n");
  4340. script_reportdata(data);
  4341. st->state = END;
  4342. return 1;// not supported
  4343. }
  4344. if( not_server_variable(*name) )
  4345. {
  4346. sd = script_rid2sd(st);
  4347. if( sd == NULL )
  4348. return 0;// no player attached
  4349. }
  4350. end = start + script_lastdata(st) - 2;
  4351. if( end > 127 )
  4352. end = 127;
  4353. if( is_string_variable(name) )
  4354. {// string array
  4355. for( i = 3; start < end; ++start, ++i )
  4356. {
  4357. void* v = (void*)script_getstr(st,i);
  4358. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4359. }
  4360. }
  4361. else
  4362. {// int array
  4363. for( i = 3; start < end; ++start, ++i )
  4364. {
  4365. void* v = (void*)script_getnum(st,i);
  4366. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4367. }
  4368. }
  4369. return 0;
  4370. }
  4371. /// Sets count values of an array, from the starting index.
  4372. /// ex: cleararray arr[0],0,1;
  4373. ///
  4374. /// cleararray <array variable>,<value>,<count>;
  4375. BUILDIN_FUNC(cleararray)
  4376. {
  4377. struct script_data* data;
  4378. char* name;
  4379. int32 start;
  4380. int32 end;
  4381. int32 id;
  4382. void* v;
  4383. TBL_PC* sd = NULL;
  4384. data = script_getdata(st, 2);
  4385. if( !data_isreference(data) )
  4386. {
  4387. ShowError("script:cleararray: not a variable\n");
  4388. script_reportdata(data);
  4389. st->state = END;
  4390. return 1;// not a variable
  4391. }
  4392. id = reference_getid(data);
  4393. start = reference_getindex(data);
  4394. name = reference_getname(data);
  4395. if( not_array_variable(*name) )
  4396. {
  4397. ShowError("script:cleararray: illegal scope\n");
  4398. script_reportdata(data);
  4399. st->state = END;
  4400. return 1;// not supported
  4401. }
  4402. if( not_server_variable(*name) )
  4403. {
  4404. sd = script_rid2sd(st);
  4405. if( sd == NULL )
  4406. return 0;// no player attached
  4407. }
  4408. if( is_string_variable(name) )
  4409. v = (void*)script_getstr(st, 3);
  4410. else
  4411. v = (void*)script_getnum(st, 3);
  4412. end = start + script_getnum(st, 4);
  4413. if( end > 127 )
  4414. end = 127;
  4415. for( ; start <= end; ++start )
  4416. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4417. return 0;
  4418. }
  4419. /// Copies data from one array to another.
  4420. /// ex: copyarray arr[0],arr[2],2;
  4421. ///
  4422. /// copyarray <destination array variable>,<source array variable>,<count>;
  4423. BUILDIN_FUNC(copyarray)
  4424. {
  4425. struct script_data* data1;
  4426. struct script_data* data2;
  4427. char* name1;
  4428. char* name2;
  4429. int32 idx1;
  4430. int32 idx2;
  4431. int32 id1;
  4432. int32 id2;
  4433. void* v;
  4434. int32 i;
  4435. int32 count;
  4436. TBL_PC* sd = NULL;
  4437. data1 = script_getdata(st, 2);
  4438. data2 = script_getdata(st, 3);
  4439. if( !data_isreference(data1) || !data_isreference(data2) )
  4440. {
  4441. ShowError("script:copyarray: not a variable\n");
  4442. script_reportdata(data1);
  4443. script_reportdata(data2);
  4444. st->state = END;
  4445. return 1;// not a variable
  4446. }
  4447. id1 = reference_getid(data1);
  4448. id2 = reference_getid(data2);
  4449. idx1 = reference_getindex(data1);
  4450. idx2 = reference_getindex(data2);
  4451. name1 = reference_getname(data1);
  4452. name2 = reference_getname(data2);
  4453. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4454. {
  4455. ShowError("script:copyarray: illegal scope\n");
  4456. script_reportdata(data1);
  4457. script_reportdata(data2);
  4458. st->state = END;
  4459. return 1;// not supported
  4460. }
  4461. if( is_string_variable(name1) != is_string_variable(name2) )
  4462. {
  4463. ShowError("script:copyarray: type mismatch\n");
  4464. script_reportdata(data1);
  4465. script_reportdata(data2);
  4466. st->state = END;
  4467. return 1;// data type mismatch
  4468. }
  4469. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4470. {
  4471. sd = script_rid2sd(st);
  4472. if( sd == NULL )
  4473. return 0;// no player attached
  4474. }
  4475. count = script_getnum(st, 4);
  4476. if( count > 128 - idx1 )
  4477. count = 128 - idx1;
  4478. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4479. return 0;// nothing to copy
  4480. if( id1 == id2 && idx1 > idx2 )
  4481. {// destination might be overlapping the source - copy in reverse order
  4482. for( i = count - 1; i >= 0; --i )
  4483. {
  4484. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4485. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4486. script_removetop(st, -1, 0);
  4487. }
  4488. }
  4489. else
  4490. {// normal copy
  4491. for( i = 0; i < count; ++i )
  4492. {
  4493. if( idx2 + i < 128 )
  4494. {
  4495. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4496. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4497. script_removetop(st, -1, 0);
  4498. }
  4499. else// out of range - assume ""/0
  4500. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4501. }
  4502. }
  4503. return 0;
  4504. }
  4505. /// Returns the size of the array.
  4506. /// Assumes that everything before the starting index exists.
  4507. /// ex: getarraysize(arr[3])
  4508. ///
  4509. /// getarraysize(<array variable>) -> <int>
  4510. BUILDIN_FUNC(getarraysize)
  4511. {
  4512. struct script_data* data;
  4513. char* name;
  4514. data = script_getdata(st, 2);
  4515. if( !data_isreference(data) )
  4516. {
  4517. ShowError("script:getarraysize: not a variable\n");
  4518. script_reportdata(data);
  4519. script_pushnil(st);
  4520. st->state = END;
  4521. return 1;// not a variable
  4522. }
  4523. name = reference_getname(data);
  4524. if( not_array_variable(*name) )
  4525. {
  4526. ShowError("script:getarraysize: illegal scope\n");
  4527. script_reportdata(data);
  4528. script_pushnil(st);
  4529. st->state = END;
  4530. return 1;// not supported
  4531. }
  4532. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4533. return 0;
  4534. }
  4535. /// Deletes count or all the elements in an array, from the starting index.
  4536. /// ex: deletearray arr[4],2;
  4537. ///
  4538. /// deletearray <array variable>;
  4539. /// deletearray <array variable>,<count>;
  4540. BUILDIN_FUNC(deletearray)
  4541. {
  4542. struct script_data* data;
  4543. char* name;
  4544. int start;
  4545. int end;
  4546. int id;
  4547. TBL_PC *sd = NULL;
  4548. data = script_getdata(st, 2);
  4549. if( !data_isreference(data) )
  4550. {
  4551. ShowError("script:deletearray: not a variable\n");
  4552. script_reportdata(data);
  4553. st->state = END;
  4554. return 1;// not a variable
  4555. }
  4556. id = reference_getid(data);
  4557. start = reference_getindex(data);
  4558. name = reference_getname(data);
  4559. if( not_array_variable(*name) )
  4560. {
  4561. ShowError("script:deletearray: illegal scope\n");
  4562. script_reportdata(data);
  4563. st->state = END;
  4564. return 1;// not supported
  4565. }
  4566. if( not_server_variable(*name) )
  4567. {
  4568. sd = script_rid2sd(st);
  4569. if( sd == NULL )
  4570. return 0;// no player attached
  4571. }
  4572. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4573. if( start >= end )
  4574. return 0;// nothing to free
  4575. if( script_hasdata(st,3) )
  4576. {
  4577. int count = script_getnum(st, 3);
  4578. if( count > end - start )
  4579. count = end - start;
  4580. if( count <= 0 )
  4581. return 0;// nothing to free
  4582. // move rest of the elements backward
  4583. for( ; start + count < end; ++start )
  4584. {
  4585. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4586. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4587. script_removetop(st, -1, 0);
  4588. }
  4589. }
  4590. // clear the rest of the array
  4591. if( is_string_variable(name) )
  4592. {
  4593. for( ; start < end; ++start )
  4594. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4595. }
  4596. else
  4597. {
  4598. for( ; start < end; ++start )
  4599. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4600. }
  4601. return 0;
  4602. }
  4603. /// Returns a reference to the target index of the array variable.
  4604. /// Equivalent to var[index].
  4605. ///
  4606. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4607. BUILDIN_FUNC(getelementofarray)
  4608. {
  4609. struct script_data* data;
  4610. char* name;
  4611. int32 id;
  4612. int i;
  4613. data = script_getdata(st, 2);
  4614. if( !data_isreference(data) )
  4615. {
  4616. ShowError("script:getelementofarray: not a variable\n");
  4617. script_reportdata(data);
  4618. script_pushnil(st);
  4619. st->state = END;
  4620. return 1;// not a variable
  4621. }
  4622. id = reference_getid(data);
  4623. name = reference_getname(data);
  4624. if( not_array_variable(*name) )
  4625. {
  4626. ShowError("script:getelementofarray: illegal scope\n");
  4627. script_reportdata(data);
  4628. script_pushnil(st);
  4629. st->state = END;
  4630. return 1;// not supported
  4631. }
  4632. i = script_getnum(st, 3);
  4633. if( i < 0 || i >= 128 )
  4634. {
  4635. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4636. script_reportdata(data);
  4637. script_pushnil(st);
  4638. st->state = END;
  4639. return 1;// out of range
  4640. }
  4641. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4642. return 0;
  4643. }
  4644. /////////////////////////////////////////////////////////////////////
  4645. /// ...
  4646. ///
  4647. /*==========================================
  4648. *
  4649. *------------------------------------------*/
  4650. BUILDIN_FUNC(setlook)
  4651. {
  4652. int type,val;
  4653. TBL_PC* sd;
  4654. type=script_getnum(st,2);
  4655. val=script_getnum(st,3);
  4656. sd = script_rid2sd(st);
  4657. if( sd == NULL )
  4658. return 0;
  4659. pc_changelook(sd,type,val);
  4660. return 0;
  4661. }
  4662. /*==========================================
  4663. *
  4664. *------------------------------------------*/
  4665. BUILDIN_FUNC(cutin)
  4666. {
  4667. TBL_PC* sd;
  4668. sd = script_rid2sd(st);
  4669. if( sd == NULL )
  4670. return 0;
  4671. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4672. return 0;
  4673. }
  4674. /*==========================================
  4675. *
  4676. *------------------------------------------*/
  4677. BUILDIN_FUNC(viewpoint)
  4678. {
  4679. int type,x,y,id,color;
  4680. TBL_PC* sd;
  4681. type=script_getnum(st,2);
  4682. x=script_getnum(st,3);
  4683. y=script_getnum(st,4);
  4684. id=script_getnum(st,5);
  4685. color=script_getnum(st,6);
  4686. sd = script_rid2sd(st);
  4687. if( sd == NULL )
  4688. return 0;
  4689. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4690. return 0;
  4691. }
  4692. /*==========================================
  4693. *
  4694. *------------------------------------------*/
  4695. BUILDIN_FUNC(countitem)
  4696. {
  4697. int nameid, i;
  4698. int count = 0;
  4699. struct script_data* data;
  4700. TBL_PC* sd = script_rid2sd(st);
  4701. if (!sd) {
  4702. script_pushint(st,0);
  4703. return 0;
  4704. }
  4705. data = script_getdata(st,2);
  4706. get_val(st,data);
  4707. if( data_isstring(data) ) {
  4708. const char* name = conv_str(st,data);
  4709. struct item_data* item_data;
  4710. if((item_data = itemdb_searchname(name)) != NULL)
  4711. nameid = item_data->nameid;
  4712. else
  4713. nameid = 0;
  4714. } else
  4715. nameid = conv_num(st,data);
  4716. if (nameid < 500) {
  4717. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4718. script_reportsrc(st);
  4719. script_pushint(st,0);
  4720. return 1;
  4721. }
  4722. for(i = 0; i < MAX_INVENTORY; i++)
  4723. if(sd->status.inventory[i].nameid == nameid)
  4724. count += sd->status.inventory[i].amount;
  4725. script_pushint(st,count);
  4726. return 0;
  4727. }
  4728. /*==========================================
  4729. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4730. * returns number of items that meet the conditions
  4731. *------------------------------------------*/
  4732. BUILDIN_FUNC(countitem2)
  4733. {
  4734. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4735. int count = 0;
  4736. int i;
  4737. struct script_data* data;
  4738. TBL_PC* sd = script_rid2sd(st);
  4739. if (!sd) {
  4740. script_pushint(st,0);
  4741. return 0;
  4742. }
  4743. data = script_getdata(st,2);
  4744. get_val(st,data);
  4745. if( data_isstring(data) ) {
  4746. const char* name = conv_str(st,data);
  4747. struct item_data* item_data;
  4748. if((item_data = itemdb_searchname(name)) != NULL)
  4749. nameid = item_data->nameid;
  4750. else
  4751. nameid = 0;
  4752. } else
  4753. nameid = conv_num(st,data);
  4754. iden = script_getnum(st,3);
  4755. ref = script_getnum(st,4);
  4756. attr = script_getnum(st,5);
  4757. c1 = (short)script_getnum(st,6);
  4758. c2 = (short)script_getnum(st,7);
  4759. c3 = (short)script_getnum(st,8);
  4760. c4 = (short)script_getnum(st,9);
  4761. if (nameid < 500) {
  4762. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4763. script_pushint(st,0);
  4764. return 1;
  4765. }
  4766. for(i = 0; i < MAX_INVENTORY; i++)
  4767. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4768. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4769. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4770. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4771. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4772. sd->status.inventory[i].card[3] == c4
  4773. )
  4774. count += sd->status.inventory[i].amount;
  4775. script_pushint(st,count);
  4776. return 0;
  4777. }
  4778. /*==========================================
  4779. * 重量チェック
  4780. *------------------------------------------*/
  4781. BUILDIN_FUNC(checkweight)
  4782. {
  4783. int nameid=0,amount,i;
  4784. unsigned long weight;
  4785. TBL_PC *sd;
  4786. struct script_data *data;
  4787. sd = script_rid2sd(st);
  4788. if( sd == NULL )
  4789. return 0;
  4790. data=script_getdata(st,2);
  4791. get_val(st,data);
  4792. if( data_isstring(data) ){
  4793. const char *name=conv_str(st,data);
  4794. struct item_data *item_data = itemdb_searchname(name);
  4795. if( item_data )
  4796. nameid=item_data->nameid;
  4797. }else
  4798. nameid=conv_num(st,data);
  4799. amount=script_getnum(st,3);
  4800. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4801. script_pushint(st,0);
  4802. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4803. return 1;
  4804. }
  4805. weight = itemdb_weight(nameid)*amount;
  4806. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4807. script_pushint(st,0);
  4808. } else {
  4809. //Check if the inventory ain't full.
  4810. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4811. i = pc_search_inventory(sd,0);
  4812. if (i >= 0) //Empty slot available.
  4813. script_pushint(st,1);
  4814. else //Inventory full
  4815. script_pushint(st,0);
  4816. }
  4817. return 0;
  4818. }
  4819. /*==========================================
  4820. * getitem <item id>,<amount>{,<character ID>};
  4821. * getitem "<item name>",<amount>{,<character ID>};
  4822. *------------------------------------------*/
  4823. BUILDIN_FUNC(getitem)
  4824. {
  4825. int nameid,amount,flag = 0;
  4826. struct item it;
  4827. TBL_PC *sd;
  4828. struct script_data *data;
  4829. data=script_getdata(st,2);
  4830. get_val(st,data);
  4831. if( data_isstring(data) )
  4832. {// "<item name>"
  4833. const char *name=conv_str(st,data);
  4834. struct item_data *item_data = itemdb_searchname(name);
  4835. if( item_data == NULL ){
  4836. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4837. return 1; //No item created.
  4838. }
  4839. nameid=item_data->nameid;
  4840. } else if( data_isint(data) )
  4841. {// <item id>
  4842. nameid=conv_num(st,data);
  4843. //Violet Box, Blue Box, etc - random item pick
  4844. if( nameid < 0 ) {
  4845. nameid=itemdb_searchrandomid(-nameid);
  4846. flag = 1;
  4847. }
  4848. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4849. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4850. return 1; //No item created.
  4851. }
  4852. } else {
  4853. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4854. return 1;
  4855. }
  4856. // <amount>
  4857. if( (amount=script_getnum(st,3)) <= 0)
  4858. return 0; //return if amount <=0, skip the useles iteration
  4859. memset(&it,0,sizeof(it));
  4860. it.nameid=nameid;
  4861. if(!flag)
  4862. it.identify=1;
  4863. else
  4864. it.identify=itemdb_isidentified(nameid);
  4865. if( script_hasdata(st,4) )
  4866. {// <character ID>
  4867. sd=map_id2sd(script_getnum(st,4));
  4868. } else
  4869. {// attached player
  4870. sd=script_rid2sd(st);
  4871. }
  4872. if( sd == NULL ) // no target
  4873. return 0;
  4874. if( pet_create_egg(sd, nameid) )
  4875. amount = 1; //This is a pet!
  4876. else if( (flag=pc_additem(sd,&it,amount)) ){
  4877. clif_additem(sd,0,0,flag);
  4878. if( pc_candrop(sd,&it) )
  4879. map_addflooritem(&it,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4880. }
  4881. //Logs items, got from (N)PC scripts [Lupus]
  4882. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4883. log_pick_pc(sd, "N", nameid, amount, NULL);
  4884. return 0;
  4885. }
  4886. /*==========================================
  4887. *
  4888. *------------------------------------------*/
  4889. BUILDIN_FUNC(getitem2)
  4890. {
  4891. int nameid,amount,flag = 0;
  4892. int iden,ref,attr,c1,c2,c3,c4;
  4893. struct item_data *item_data;
  4894. struct item item_tmp;
  4895. TBL_PC *sd;
  4896. struct script_data *data;
  4897. sd = script_rid2sd(st);
  4898. if( sd == NULL )
  4899. return 0;
  4900. data=script_getdata(st,2);
  4901. get_val(st,data);
  4902. if( data_isstring(data) ){
  4903. const char *name=conv_str(st,data);
  4904. struct item_data *item_data = itemdb_searchname(name);
  4905. if( item_data )
  4906. nameid=item_data->nameid;
  4907. else
  4908. nameid=UNKNOWN_ITEM_ID;
  4909. }else
  4910. nameid=conv_num(st,data);
  4911. amount=script_getnum(st,3);
  4912. iden=script_getnum(st,4);
  4913. ref=script_getnum(st,5);
  4914. attr=script_getnum(st,6);
  4915. c1=script_getnum(st,7);
  4916. c2=script_getnum(st,8);
  4917. c3=script_getnum(st,9);
  4918. c4=script_getnum(st,10);
  4919. if( script_hasdata(st,11) ) //アイテムを指定したIDに渡す
  4920. sd=map_id2sd(script_getnum(st,11));
  4921. if(sd == NULL) //アイテムを渡す相手がいなかったらお帰り
  4922. return 0;
  4923. if(nameid<0) { // ランダム
  4924. nameid=itemdb_searchrandomid(-nameid);
  4925. flag = 1;
  4926. }
  4927. if(nameid > 0) {
  4928. memset(&item_tmp,0,sizeof(item_tmp));
  4929. item_data=itemdb_exists(nameid);
  4930. if (item_data == NULL)
  4931. return -1;
  4932. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4933. if(ref > 10) ref = 10;
  4934. }
  4935. else if(item_data->type==IT_PETEGG) {
  4936. iden = 1;
  4937. ref = 0;
  4938. }
  4939. else {
  4940. iden = 1;
  4941. ref = attr = 0;
  4942. }
  4943. item_tmp.nameid=nameid;
  4944. if(!flag)
  4945. item_tmp.identify=iden;
  4946. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4947. item_tmp.identify=0;
  4948. item_tmp.refine=ref;
  4949. item_tmp.attribute=attr;
  4950. item_tmp.card[0]=c1;
  4951. item_tmp.card[1]=c2;
  4952. item_tmp.card[2]=c3;
  4953. item_tmp.card[3]=c4;
  4954. if((flag = pc_additem(sd,&item_tmp,amount))) {
  4955. clif_additem(sd,0,0,flag);
  4956. map_addflooritem(&item_tmp,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4957. }
  4958. //Logs items, got from (N)PC scripts [Lupus]
  4959. if(log_config.enable_logs&0x40)
  4960. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4961. }
  4962. return 0;
  4963. }
  4964. /*==========================================
  4965. * gets an item with someone's name inscribed [Skotlex]
  4966. * getinscribeditem item_num, character_name
  4967. * Returned Qty is always 1, only works on equip-able
  4968. * equipment
  4969. *------------------------------------------*/
  4970. BUILDIN_FUNC(getnameditem)
  4971. {
  4972. int nameid;
  4973. struct item item_tmp;
  4974. TBL_PC *sd, *tsd;
  4975. struct script_data *data;
  4976. sd = script_rid2sd(st);
  4977. if (sd == NULL)
  4978. { //Player not attached!
  4979. script_pushint(st,0);
  4980. return 0;
  4981. }
  4982. data=script_getdata(st,2);
  4983. get_val(st,data);
  4984. if( data_isstring(data) ){
  4985. const char *name=conv_str(st,data);
  4986. struct item_data *item_data = itemdb_searchname(name);
  4987. if( item_data == NULL)
  4988. { //Failed
  4989. script_pushint(st,0);
  4990. return 0;
  4991. }
  4992. nameid = item_data->nameid;
  4993. }else
  4994. nameid = conv_num(st,data);
  4995. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  4996. { //Even though named stackable items "could" be risky, they are required for certain quests.
  4997. script_pushint(st,0);
  4998. return 0;
  4999. }
  5000. data=script_getdata(st,3);
  5001. get_val(st,data);
  5002. if( data_isstring(data) ) //Char Name
  5003. tsd=map_nick2sd(conv_str(st,data));
  5004. else //Char Id was given
  5005. tsd=map_charid2sd(conv_num(st,data));
  5006. if( tsd == NULL )
  5007. { //Failed
  5008. script_pushint(st,0);
  5009. return 0;
  5010. }
  5011. memset(&item_tmp,0,sizeof(item_tmp));
  5012. item_tmp.nameid=nameid;
  5013. item_tmp.amount=1;
  5014. item_tmp.identify=1;
  5015. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5016. item_tmp.card[2]=tsd->status.char_id;
  5017. item_tmp.card[3]=tsd->status.char_id >> 16;
  5018. if(pc_additem(sd,&item_tmp,1)) {
  5019. script_pushint(st,0);
  5020. return 0; //Failed to add item, we will not drop if they don't fit
  5021. }
  5022. //Logs items, got from (N)PC scripts [Lupus]
  5023. if(log_config.enable_logs&0x40)
  5024. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5025. script_pushint(st,1);
  5026. return 0;
  5027. }
  5028. /*==========================================
  5029. * gets a random item ID from an item group [Skotlex]
  5030. * groupranditem group_num
  5031. *------------------------------------------*/
  5032. BUILDIN_FUNC(grouprandomitem)
  5033. {
  5034. int group;
  5035. group = script_getnum(st,2);
  5036. script_pushint(st,itemdb_searchrandomid(group));
  5037. return 0;
  5038. }
  5039. /*==========================================
  5040. *
  5041. *------------------------------------------*/
  5042. BUILDIN_FUNC(makeitem)
  5043. {
  5044. int nameid,amount,flag = 0;
  5045. int x,y,m;
  5046. const char *mapname;
  5047. struct item item_tmp;
  5048. struct script_data *data;
  5049. data=script_getdata(st,2);
  5050. get_val(st,data);
  5051. if( data_isstring(data) ){
  5052. const char *name=conv_str(st,data);
  5053. struct item_data *item_data = itemdb_searchname(name);
  5054. if( item_data )
  5055. nameid=item_data->nameid;
  5056. else
  5057. nameid=UNKNOWN_ITEM_ID;
  5058. }else
  5059. nameid=conv_num(st,data);
  5060. amount=script_getnum(st,3);
  5061. mapname =script_getstr(st,4);
  5062. x =script_getnum(st,5);
  5063. y =script_getnum(st,6);
  5064. if(strcmp(mapname,"this")==0)
  5065. {
  5066. TBL_PC *sd;
  5067. sd = script_rid2sd(st);
  5068. if (!sd) return 0; //Failed...
  5069. m=sd->bl.m;
  5070. } else
  5071. m=map_mapname2mapid(mapname);
  5072. if(nameid<0) { // ランダム
  5073. nameid=itemdb_searchrandomid(-nameid);
  5074. flag = 1;
  5075. }
  5076. if(nameid > 0) {
  5077. memset(&item_tmp,0,sizeof(item_tmp));
  5078. item_tmp.nameid=nameid;
  5079. if(!flag)
  5080. item_tmp.identify=1;
  5081. else
  5082. item_tmp.identify=itemdb_isidentified(nameid);
  5083. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5084. }
  5085. return 0;
  5086. }
  5087. /*==========================================
  5088. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5089. *------------------------------------------*/
  5090. BUILDIN_FUNC(delitem)
  5091. {
  5092. int nameid=0,amount,i,important_item=0;
  5093. TBL_PC *sd;
  5094. struct script_data *data;
  5095. sd = script_rid2sd(st);
  5096. if( sd == NULL )
  5097. return 0;
  5098. data=script_getdata(st,2);
  5099. get_val(st,data);
  5100. if( data_isstring(data) ){
  5101. const char *name=conv_str(st,data);
  5102. struct item_data *item_data = itemdb_searchname(name);
  5103. //nameid=UNKNOWN_ITEM_ID;
  5104. if( item_data )
  5105. nameid=item_data->nameid;
  5106. }else
  5107. nameid=conv_num(st,data);
  5108. amount=script_getnum(st,3);
  5109. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5110. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5111. return 0;
  5112. }
  5113. //1st pass
  5114. //here we won't delete items with CARDS, named items but we count them
  5115. for(i=0;i<MAX_INVENTORY;i++){
  5116. //we don't delete wrong item or equipped item
  5117. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5118. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5119. continue;
  5120. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5121. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5122. sd->status.inventory[i].card[0] == CARD0_PET)
  5123. {
  5124. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5125. continue; //pet couldn't be sent for deletion.
  5126. } else
  5127. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5128. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5129. sd->status.inventory[i].card[0] ||
  5130. sd->status.inventory[i].refine) {
  5131. //this is important item, count it (except for pet eggs)
  5132. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5133. important_item++;
  5134. continue;
  5135. }
  5136. if(sd->status.inventory[i].amount>=amount){
  5137. //Logs items, got from (N)PC scripts [Lupus]
  5138. if(log_config.enable_logs&0x40)
  5139. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5140. pc_delitem(sd,i,amount,0);
  5141. return 0; //we deleted exact amount of items. now exit
  5142. } else {
  5143. amount-=sd->status.inventory[i].amount;
  5144. //Logs items, got from (N)PC scripts [Lupus]
  5145. if(log_config.enable_logs&0x40) {
  5146. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5147. }
  5148. //Logs
  5149. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5150. }
  5151. }
  5152. //2nd pass
  5153. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5154. if (important_item>0 && amount>0)
  5155. for(i=0;i<MAX_INVENTORY;i++){
  5156. //we don't delete wrong 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. if(sd->status.inventory[i].amount>=amount){
  5161. //Logs items, got from (N)PC scripts [Lupus]
  5162. if(log_config.enable_logs&0x40)
  5163. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5164. pc_delitem(sd,i,amount,0);
  5165. return 0; //we deleted exact amount of items. now exit
  5166. } else {
  5167. amount-=sd->status.inventory[i].amount;
  5168. //Logs items, got from (N)PC scripts [Lupus]
  5169. if(log_config.enable_logs&0x40)
  5170. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5171. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5172. }
  5173. }
  5174. return 0;
  5175. }
  5176. /*==========================================
  5177. * advanced version of delitem [modified by Mihilion]
  5178. *------------------------------------------*/
  5179. BUILDIN_FUNC(delitem2)
  5180. {
  5181. int nameid=0,amount,i=0;
  5182. int iden,ref,attr,c1,c2,c3,c4;
  5183. TBL_PC *sd;
  5184. struct script_data *data;
  5185. sd = script_rid2sd(st);
  5186. if( sd == NULL )
  5187. return 0;
  5188. data=script_getdata(st,2);
  5189. get_val(st,data);
  5190. if( data_isstring(data) ){
  5191. const char *name=conv_str(st,data);
  5192. struct item_data *item_data = itemdb_searchname(name);
  5193. //nameid=UNKNOWN_ITEM_ID;
  5194. if( item_data )
  5195. nameid=item_data->nameid;
  5196. }else
  5197. nameid=conv_num(st,data);
  5198. amount=script_getnum(st,3);
  5199. iden=script_getnum(st,4);
  5200. ref=script_getnum(st,5);
  5201. attr=script_getnum(st,6);
  5202. c1=script_getnum(st,7);
  5203. c2=script_getnum(st,8);
  5204. c3=script_getnum(st,9);
  5205. c4=script_getnum(st,10);
  5206. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5207. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5208. return 0;
  5209. }
  5210. for(i=0;i<MAX_INVENTORY;i++){
  5211. //we don't delete wrong item or equipped item
  5212. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5213. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5214. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5215. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5216. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5217. sd->status.inventory[i].card[3]!=c4)
  5218. continue;
  5219. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5220. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5221. {
  5222. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5223. continue; //Failed to send delete the pet.
  5224. }
  5225. if(sd->status.inventory[i].amount>=amount){
  5226. //Logs items, got from (N)PC scripts [Lupus]
  5227. if(log_config.enable_logs&0x40)
  5228. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5229. pc_delitem(sd,i,amount,0);
  5230. return 0; //we deleted exact amount of items. now exit
  5231. } else {
  5232. amount-=sd->status.inventory[i].amount;
  5233. //Logs items, got from (N)PC scripts [Lupus]
  5234. if(log_config.enable_logs&0x40)
  5235. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5236. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5237. }
  5238. }
  5239. return 0;
  5240. }
  5241. /*==========================================
  5242. * Enables/Disables use of items while in an NPC [Skotlex]
  5243. *------------------------------------------*/
  5244. BUILDIN_FUNC(enableitemuse)
  5245. {
  5246. TBL_PC *sd;
  5247. sd=script_rid2sd(st);
  5248. if (sd)
  5249. sd->npc_item_flag = st->oid;
  5250. return 0;
  5251. }
  5252. BUILDIN_FUNC(disableitemuse)
  5253. {
  5254. TBL_PC *sd;
  5255. sd=script_rid2sd(st);
  5256. if (sd)
  5257. sd->npc_item_flag = 0;
  5258. return 0;
  5259. }
  5260. /*==========================================
  5261. *キャラ関係のパラメータ取得
  5262. *------------------------------------------*/
  5263. BUILDIN_FUNC(readparam)
  5264. {
  5265. int type;
  5266. TBL_PC *sd;
  5267. type=script_getnum(st,2);
  5268. if( script_hasdata(st,3) )
  5269. sd=map_nick2sd(script_getstr(st,3));
  5270. else
  5271. sd=script_rid2sd(st);
  5272. if(sd==NULL){
  5273. script_pushint(st,-1);
  5274. return 0;
  5275. }
  5276. script_pushint(st,pc_readparam(sd,type));
  5277. return 0;
  5278. }
  5279. /*==========================================
  5280. *キャラ関係のID取得
  5281. *------------------------------------------*/
  5282. BUILDIN_FUNC(getcharid)
  5283. {
  5284. int num;
  5285. TBL_PC *sd;
  5286. num = script_getnum(st,2);
  5287. if( script_hasdata(st,3) )
  5288. sd=map_nick2sd(script_getstr(st,3));
  5289. else
  5290. sd=script_rid2sd(st);
  5291. if(sd==NULL){
  5292. script_pushint(st,0); //return 0, according docs
  5293. return 0;
  5294. }
  5295. switch( num ) {
  5296. case 0: script_pushint(st,sd->status.char_id); break;
  5297. case 1: script_pushint(st,sd->status.party_id); break;
  5298. case 2: script_pushint(st,sd->status.guild_id); break;
  5299. case 3: script_pushint(st,sd->status.account_id); break;
  5300. default:
  5301. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5302. script_pushint(st,0);
  5303. break;
  5304. }
  5305. return 0;
  5306. }
  5307. /*==========================================
  5308. *指定IDのPT名取得
  5309. *------------------------------------------*/
  5310. char *buildin_getpartyname_sub(int party_id)
  5311. {
  5312. struct party_data *p;
  5313. p=party_search(party_id);
  5314. if(p!=NULL){
  5315. char *buf;
  5316. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5317. memcpy(buf, p->party.name, NAME_LENGTH);
  5318. buf[NAME_LENGTH-1] = '\0';
  5319. return buf;
  5320. }
  5321. return 0;
  5322. }
  5323. BUILDIN_FUNC(getpartyname)
  5324. {
  5325. char *name;
  5326. int party_id;
  5327. party_id=script_getnum(st,2);
  5328. name=buildin_getpartyname_sub(party_id);
  5329. if(name != NULL)
  5330. script_pushstr(st,name);
  5331. else
  5332. script_pushconststr(st,"null");
  5333. return 0;
  5334. }
  5335. /*==========================================
  5336. *指定IDのPT人数とメンバーID取得
  5337. *------------------------------------------*/
  5338. BUILDIN_FUNC(getpartymember)
  5339. {
  5340. struct party_data *p;
  5341. int i,j=0,type=0;
  5342. p=party_search(script_getnum(st,2));
  5343. if( script_hasdata(st,3) )
  5344. type=script_getnum(st,3);
  5345. if(p!=NULL){
  5346. for(i=0;i<MAX_PARTY;i++){
  5347. if(p->party.member[i].account_id){
  5348. switch (type) {
  5349. case 2:
  5350. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5351. break;
  5352. case 1:
  5353. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5354. break;
  5355. default:
  5356. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5357. }
  5358. j++;
  5359. }
  5360. }
  5361. }
  5362. mapreg_setreg(add_str("$@partymembercount"),j);
  5363. return 0;
  5364. }
  5365. /*==========================================
  5366. * Retrieves party leader. if flag is specified,
  5367. * return some of the leader data. Otherwise, return name.
  5368. *------------------------------------------*/
  5369. BUILDIN_FUNC(getpartyleader)
  5370. {
  5371. int party_id, type = 0, i=0;
  5372. struct party_data *p;
  5373. party_id=script_getnum(st,2);
  5374. if( script_hasdata(st,3) )
  5375. type=script_getnum(st,3);
  5376. p=party_search(party_id);
  5377. if (p) //Search leader
  5378. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5379. if (!p || i == MAX_PARTY) { //leader not found
  5380. if (type)
  5381. script_pushint(st,-1);
  5382. else
  5383. script_pushconststr(st,"null");
  5384. return 0;
  5385. }
  5386. switch (type) {
  5387. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5388. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5389. case 3: script_pushint(st,p->party.member[i].class_); break;
  5390. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5391. case 5: script_pushint(st,p->party.member[i].lv); break;
  5392. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5393. }
  5394. return 0;
  5395. }
  5396. /*==========================================
  5397. *指定IDのギルド名取得
  5398. *------------------------------------------*/
  5399. char *buildin_getguildname_sub(int guild_id)
  5400. {
  5401. struct guild *g=NULL;
  5402. g=guild_search(guild_id);
  5403. if(g!=NULL){
  5404. char *buf;
  5405. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5406. memcpy(buf, g->name, NAME_LENGTH);
  5407. buf[NAME_LENGTH-1] = '\0';
  5408. return buf;
  5409. }
  5410. return NULL;
  5411. }
  5412. BUILDIN_FUNC(getguildname)
  5413. {
  5414. char *name;
  5415. int guild_id=script_getnum(st,2);
  5416. name=buildin_getguildname_sub(guild_id);
  5417. if(name != NULL)
  5418. script_pushstr(st,name);
  5419. else
  5420. script_pushconststr(st,"null");
  5421. return 0;
  5422. }
  5423. /*==========================================
  5424. *指定IDのGuildMaster名取得
  5425. *------------------------------------------*/
  5426. char *buildin_getguildmaster_sub(int guild_id)
  5427. {
  5428. struct guild *g=NULL;
  5429. g=guild_search(guild_id);
  5430. if(g!=NULL){
  5431. char *buf;
  5432. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5433. memcpy(buf, g->master, NAME_LENGTH);
  5434. buf[NAME_LENGTH-1] = '\0';
  5435. return buf;
  5436. }
  5437. return 0;
  5438. }
  5439. BUILDIN_FUNC(getguildmaster)
  5440. {
  5441. char *master;
  5442. int guild_id=script_getnum(st,2);
  5443. master=buildin_getguildmaster_sub(guild_id);
  5444. if(master!=0)
  5445. script_pushstr(st,master);
  5446. else
  5447. script_pushconststr(st,"null");
  5448. return 0;
  5449. }
  5450. BUILDIN_FUNC(getguildmasterid)
  5451. {
  5452. char *master;
  5453. TBL_PC *sd=NULL;
  5454. int guild_id=script_getnum(st,2);
  5455. master=buildin_getguildmaster_sub(guild_id);
  5456. if(master!=0){
  5457. if((sd=map_nick2sd(master)) == NULL){
  5458. script_pushint(st,0);
  5459. return 0;
  5460. }
  5461. script_pushint(st,sd->status.char_id);
  5462. }else{
  5463. script_pushint(st,0);
  5464. }
  5465. return 0;
  5466. }
  5467. /*==========================================
  5468. * キャラクタの名前
  5469. *------------------------------------------*/
  5470. BUILDIN_FUNC(strcharinfo)
  5471. {
  5472. TBL_PC *sd;
  5473. int num;
  5474. char *buf;
  5475. sd=script_rid2sd(st);
  5476. if (!sd) { //Avoid crashing....
  5477. script_pushconststr(st,"");
  5478. return 0;
  5479. }
  5480. num=script_getnum(st,2);
  5481. switch(num){
  5482. case 0:
  5483. script_pushstrcopy(st,sd->status.name);
  5484. break;
  5485. case 1:
  5486. buf=buildin_getpartyname_sub(sd->status.party_id);
  5487. if(buf!=0)
  5488. script_pushstr(st,buf);
  5489. else
  5490. script_pushconststr(st,"");
  5491. break;
  5492. case 2:
  5493. buf=buildin_getguildname_sub(sd->status.guild_id);
  5494. if(buf != NULL)
  5495. script_pushstr(st,buf);
  5496. else
  5497. script_pushconststr(st,"");
  5498. break;
  5499. default:
  5500. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5501. script_pushconststr(st,"");
  5502. break;
  5503. }
  5504. return 0;
  5505. }
  5506. /*==========================================
  5507. * 呼び出し元のNPC情報を取得する
  5508. *------------------------------------------*/
  5509. BUILDIN_FUNC(strnpcinfo)
  5510. {
  5511. TBL_NPC* nd;
  5512. int num;
  5513. char *buf,*name=NULL;
  5514. nd = map_id2nd(st->oid);
  5515. if (!nd) {
  5516. script_pushconststr(st, "");
  5517. return 0;
  5518. }
  5519. num = script_getnum(st,2);
  5520. switch(num){
  5521. case 0: // display name
  5522. name = aStrdup(nd->name);
  5523. break;
  5524. case 1: // visible part of display name name
  5525. if((buf = strchr(nd->name,'#')) != NULL)
  5526. {
  5527. name = aStrdup(nd->name);
  5528. name[buf - nd->name] = 0;
  5529. }
  5530. break;
  5531. case 2: // # fragment
  5532. if((buf = strchr(nd->name,'#')) != NULL)
  5533. name = aStrdup(buf+1);
  5534. break;
  5535. case 3: // unique name
  5536. name = aStrdup(nd->exname);
  5537. break;
  5538. }
  5539. if(name)
  5540. script_pushstr(st, name);
  5541. else
  5542. script_pushconststr(st, "");
  5543. return 0;
  5544. }
  5545. 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};
  5546. /*==========================================
  5547. * GetEquipID(Pos); Pos: 1-10
  5548. *------------------------------------------*/
  5549. BUILDIN_FUNC(getequipid)
  5550. {
  5551. int i=-1,num;
  5552. TBL_PC *sd;
  5553. struct item_data* item;
  5554. sd=script_rid2sd(st);
  5555. if(sd == NULL)
  5556. return 0;
  5557. num=script_getnum(st,2);
  5558. if (num > 0 && num <= ARRAYLENGTH(equip))
  5559. i=pc_checkequip(sd,equip[num-1]);
  5560. if(i >= 0){
  5561. item=sd->inventory_data[i];
  5562. if(item)
  5563. script_pushint(st,item->nameid);
  5564. else
  5565. script_pushint(st,0);
  5566. }else{
  5567. script_pushint(st,-1);
  5568. }
  5569. return 0;
  5570. }
  5571. /*==========================================
  5572. * 装備名文字列(精錬メニュー用)
  5573. *------------------------------------------*/
  5574. BUILDIN_FUNC(getequipname)
  5575. {
  5576. int i=-1,num;
  5577. TBL_PC *sd;
  5578. struct item_data* item;
  5579. char *buf;
  5580. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  5581. sd = script_rid2sd(st);
  5582. if( sd == NULL )
  5583. return 0;
  5584. buf=(char *)aMallocA(64*sizeof(char));
  5585. num=script_getnum(st,2);
  5586. if (num > 0 && num <= ARRAYLENGTH(equip))
  5587. i=pc_checkequip(sd,equip[num-1]);
  5588. if(i >= 0){
  5589. item=sd->inventory_data[i];
  5590. if(item)
  5591. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5592. else
  5593. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5594. }else{
  5595. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5596. }
  5597. script_pushstr(st,buf);
  5598. return 0;
  5599. }
  5600. /*==========================================
  5601. * getbrokenid [Valaris]
  5602. *------------------------------------------*/
  5603. BUILDIN_FUNC(getbrokenid)
  5604. {
  5605. int i,num,id=0,brokencounter=0;
  5606. TBL_PC *sd;
  5607. sd = script_rid2sd(st);
  5608. if( sd == NULL )
  5609. return 0;
  5610. num=script_getnum(st,2);
  5611. for(i=0; i<MAX_INVENTORY; i++) {
  5612. if(sd->status.inventory[i].attribute==1){
  5613. brokencounter++;
  5614. if(num==brokencounter){
  5615. id=sd->status.inventory[i].nameid;
  5616. break;
  5617. }
  5618. }
  5619. }
  5620. script_pushint(st,id);
  5621. return 0;
  5622. }
  5623. /*==========================================
  5624. * repair [Valaris]
  5625. *------------------------------------------*/
  5626. BUILDIN_FUNC(repair)
  5627. {
  5628. int i,num;
  5629. int repaircounter=0;
  5630. TBL_PC *sd;
  5631. sd = script_rid2sd(st);
  5632. if( sd == NULL )
  5633. return 0;
  5634. num=script_getnum(st,2);
  5635. for(i=0; i<MAX_INVENTORY; i++) {
  5636. if(sd->status.inventory[i].attribute==1){
  5637. repaircounter++;
  5638. if(num==repaircounter){
  5639. sd->status.inventory[i].attribute=0;
  5640. clif_equiplist(sd);
  5641. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5642. clif_misceffect(&sd->bl, 3);
  5643. break;
  5644. }
  5645. }
  5646. }
  5647. return 0;
  5648. }
  5649. /*==========================================
  5650. * 装備チェック
  5651. *------------------------------------------*/
  5652. BUILDIN_FUNC(getequipisequiped)
  5653. {
  5654. int i=-1,num;
  5655. TBL_PC *sd;
  5656. num=script_getnum(st,2);
  5657. sd = script_rid2sd(st);
  5658. if( sd == NULL )
  5659. return 0;
  5660. if (num > 0 && num <= ARRAYLENGTH(equip))
  5661. i=pc_checkequip(sd,equip[num-1]);
  5662. if(i >= 0)
  5663. script_pushint(st,1);
  5664. else
  5665. script_pushint(st,0);
  5666. return 0;
  5667. }
  5668. /*==========================================
  5669. * 装備品精錬可能チェック
  5670. *------------------------------------------*/
  5671. BUILDIN_FUNC(getequipisenableref)
  5672. {
  5673. int i=-1,num;
  5674. TBL_PC *sd;
  5675. num=script_getnum(st,2);
  5676. sd = script_rid2sd(st);
  5677. if( sd == NULL )
  5678. return 0;
  5679. if (num > 0 && num <= ARRAYLENGTH(equip))
  5680. i=pc_checkequip(sd,equip[num-1]);
  5681. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5682. {
  5683. script_pushint(st,1);
  5684. } else {
  5685. script_pushint(st,0);
  5686. }
  5687. return 0;
  5688. }
  5689. /*==========================================
  5690. * 装備品鑑定チェック
  5691. *------------------------------------------*/
  5692. BUILDIN_FUNC(getequipisidentify)
  5693. {
  5694. int i=-1,num;
  5695. TBL_PC *sd;
  5696. num=script_getnum(st,2);
  5697. sd = script_rid2sd(st);
  5698. if( sd == NULL )
  5699. return 0;
  5700. if (num > 0 && num <= ARRAYLENGTH(equip))
  5701. i=pc_checkequip(sd,equip[num-1]);
  5702. if(i >= 0)
  5703. script_pushint(st,sd->status.inventory[i].identify);
  5704. else
  5705. script_pushint(st,0);
  5706. return 0;
  5707. }
  5708. /*==========================================
  5709. * 装備品精錬度
  5710. *------------------------------------------*/
  5711. BUILDIN_FUNC(getequiprefinerycnt)
  5712. {
  5713. int i=-1,num;
  5714. TBL_PC *sd;
  5715. num=script_getnum(st,2);
  5716. sd = script_rid2sd(st);
  5717. if( sd == NULL )
  5718. return 0;
  5719. if (num > 0 && num <= ARRAYLENGTH(equip))
  5720. i=pc_checkequip(sd,equip[num-1]);
  5721. if(i >= 0)
  5722. script_pushint(st,sd->status.inventory[i].refine);
  5723. else
  5724. script_pushint(st,0);
  5725. return 0;
  5726. }
  5727. /*==========================================
  5728. * 装備品武器LV
  5729. *------------------------------------------*/
  5730. BUILDIN_FUNC(getequipweaponlv)
  5731. {
  5732. int i=-1,num;
  5733. TBL_PC *sd;
  5734. num=script_getnum(st,2);
  5735. sd = script_rid2sd(st);
  5736. if( sd == NULL )
  5737. return 0;
  5738. if (num > 0 && num <= ARRAYLENGTH(equip))
  5739. i=pc_checkequip(sd,equip[num-1]);
  5740. if(i >= 0 && sd->inventory_data[i])
  5741. script_pushint(st,sd->inventory_data[i]->wlv);
  5742. else
  5743. script_pushint(st,0);
  5744. return 0;
  5745. }
  5746. /*==========================================
  5747. * 装備品精錬成功率
  5748. *------------------------------------------*/
  5749. BUILDIN_FUNC(getequippercentrefinery)
  5750. {
  5751. int i=-1,num;
  5752. TBL_PC *sd;
  5753. num=script_getnum(st,2);
  5754. sd = script_rid2sd(st);
  5755. if( sd == NULL )
  5756. return 0;
  5757. if (num > 0 && num <= ARRAYLENGTH(equip))
  5758. i=pc_checkequip(sd,equip[num-1]);
  5759. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5760. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5761. else
  5762. script_pushint(st,0);
  5763. return 0;
  5764. }
  5765. /*==========================================
  5766. * 精錬成功
  5767. *------------------------------------------*/
  5768. BUILDIN_FUNC(successrefitem)
  5769. {
  5770. int i=-1,num,ep;
  5771. TBL_PC *sd;
  5772. num=script_getnum(st,2);
  5773. sd = script_rid2sd(st);
  5774. if( sd == NULL )
  5775. return 0;
  5776. if (num > 0 && num <= ARRAYLENGTH(equip))
  5777. i=pc_checkequip(sd,equip[num-1]);
  5778. if(i >= 0) {
  5779. ep=sd->status.inventory[i].equip;
  5780. //Logs items, got from (N)PC scripts [Lupus]
  5781. if(log_config.enable_logs&0x40)
  5782. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5783. sd->status.inventory[i].refine++;
  5784. pc_unequipitem(sd,i,2);
  5785. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5786. clif_delitem(sd,i,1);
  5787. //Logs items, got from (N)PC scripts [Lupus]
  5788. if(log_config.enable_logs&0x40)
  5789. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5790. clif_additem(sd,i,1,0);
  5791. pc_equipitem(sd,i,ep);
  5792. clif_misceffect(&sd->bl,3);
  5793. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5794. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5795. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5796. ){ // Fame point system [DracoRPG]
  5797. switch (sd->inventory_data[i]->wlv){
  5798. case 1:
  5799. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5800. break;
  5801. case 2:
  5802. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5803. break;
  5804. case 3:
  5805. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5806. break;
  5807. }
  5808. }
  5809. }
  5810. return 0;
  5811. }
  5812. /*==========================================
  5813. * 精錬失敗
  5814. *------------------------------------------*/
  5815. BUILDIN_FUNC(failedrefitem)
  5816. {
  5817. int i=-1,num;
  5818. TBL_PC *sd;
  5819. num=script_getnum(st,2);
  5820. sd = script_rid2sd(st);
  5821. if( sd == NULL )
  5822. return 0;
  5823. if (num > 0 && num <= ARRAYLENGTH(equip))
  5824. i=pc_checkequip(sd,equip[num-1]);
  5825. if(i >= 0) {
  5826. //Logs items, got from (N)PC scripts [Lupus]
  5827. if(log_config.enable_logs&0x40)
  5828. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5829. sd->status.inventory[i].refine = 0;
  5830. pc_unequipitem(sd,i,3);
  5831. // 精錬失敗エフェクトのパケット
  5832. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5833. pc_delitem(sd,i,1,0);
  5834. // 他の人にも失敗を通知
  5835. clif_misceffect(&sd->bl,2);
  5836. }
  5837. return 0;
  5838. }
  5839. /*==========================================
  5840. *
  5841. *------------------------------------------*/
  5842. BUILDIN_FUNC(statusup)
  5843. {
  5844. int type;
  5845. TBL_PC *sd;
  5846. type=script_getnum(st,2);
  5847. sd = script_rid2sd(st);
  5848. if( sd == NULL )
  5849. return 0;
  5850. pc_statusup(sd,type);
  5851. return 0;
  5852. }
  5853. /*==========================================
  5854. *
  5855. *------------------------------------------*/
  5856. BUILDIN_FUNC(statusup2)
  5857. {
  5858. int type,val;
  5859. TBL_PC *sd;
  5860. type=script_getnum(st,2);
  5861. val=script_getnum(st,3);
  5862. sd = script_rid2sd(st);
  5863. if( sd == NULL )
  5864. return 0;
  5865. pc_statusup2(sd,type,val);
  5866. return 0;
  5867. }
  5868. /// See 'doc/item_bonus.txt'
  5869. ///
  5870. /// bonus <bonus type>,<val1>;
  5871. /// bonus2 <bonus type>,<val1>,<val2>;
  5872. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5873. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5874. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5875. BUILDIN_FUNC(bonus)
  5876. {
  5877. int type;
  5878. int type2;
  5879. int type3;
  5880. int type4;
  5881. int type5;
  5882. int val;
  5883. TBL_PC* sd;
  5884. sd = script_rid2sd(st);
  5885. if( sd == NULL )
  5886. return 0; // no player attached
  5887. type = script_getnum(st,2);
  5888. switch( script_lastdata(st) ){
  5889. case 3:
  5890. val = script_getnum(st,3);
  5891. pc_bonus(sd, type, val);
  5892. break;
  5893. case 4:
  5894. type2 = script_getnum(st,3);
  5895. val = script_getnum(st,4);
  5896. pc_bonus2(sd, type, type2, val);
  5897. break;
  5898. case 5:
  5899. type2 = script_getnum(st,3);
  5900. type3 = script_getnum(st,4);
  5901. val = script_getnum(st,5);
  5902. pc_bonus3(sd, type, type2, type3, val);
  5903. break;
  5904. case 6:
  5905. type2 = script_getnum(st,3);
  5906. type3 = script_getnum(st,4);
  5907. type4 = script_getnum(st,5);
  5908. val = script_getnum(st,6);
  5909. pc_bonus4(sd, type, type2, type3, type4, val);
  5910. break;
  5911. case 7:
  5912. type2 = script_getnum(st,3);
  5913. type3 = script_getnum(st,4);
  5914. type4 = script_getnum(st,5);
  5915. type5 = script_getnum(st,6);
  5916. val = script_getnum(st,7);
  5917. pc_bonus5(sd, type, type2, type3, type4, type5, val);
  5918. break;
  5919. default:
  5920. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 2));
  5921. }
  5922. return 0;
  5923. }
  5924. /// Bonus script that has a chance of being executed on attack.
  5925. BUILDIN_FUNC(bonusautoscript)
  5926. {
  5927. int rate, flag = 0;
  5928. const char *str;
  5929. struct script_code *script;
  5930. TBL_PC* sd;
  5931. sd = script_rid2sd(st);
  5932. if( sd == NULL )
  5933. return 0;// no player attached, report source
  5934. str = script_getstr(st,2);
  5935. rate = script_getnum(st,3);
  5936. if( script_hasdata(st,4) )
  5937. flag = script_getnum(st,4);
  5938. script = parse_script(str, "autoscript bonus", 0, 0);
  5939. if (!script)
  5940. return 1;
  5941. if (!pc_autoscript_add(sd->autoscript, ARRAYLENGTH(sd->autoscript), rate, flag, script))
  5942. {
  5943. script_free_code(script);
  5944. return 1;
  5945. }
  5946. return 0;
  5947. }
  5948. /// Bonus script that has a chance of being executed when attacked.
  5949. BUILDIN_FUNC(bonusautoscript2)
  5950. {
  5951. int rate, flag = 0;
  5952. const char *str;
  5953. struct script_code *script;
  5954. TBL_PC* sd;
  5955. sd = script_rid2sd(st);
  5956. if( sd == NULL )
  5957. return 0;// no player attached, report source
  5958. str = script_getstr(st,2);
  5959. rate = script_getnum(st,3);
  5960. if( script_hasdata(st,4) )
  5961. flag = script_getnum(st,4);
  5962. script = parse_script(str, "autoscript2 bonus", 0, 0);
  5963. if (!script)
  5964. return 1;
  5965. if (!pc_autoscript_add(sd->autoscript2, ARRAYLENGTH(sd->autoscript2), rate, flag, script))
  5966. {
  5967. script_free_code(script);
  5968. return 1;
  5969. }
  5970. return 0;
  5971. }
  5972. /// Changes the level of a player skill.
  5973. /// <flag> defaults to 1
  5974. /// <flag>=0 : set the level of the skill
  5975. /// <flag>=1 : set the temporary level of the skill
  5976. /// <flag>=2 : add to the level of the skill
  5977. ///
  5978. /// skill <skill id>,<level>,<flag>
  5979. /// skill <skill id>,<level>
  5980. BUILDIN_FUNC(skill)
  5981. {
  5982. int id;
  5983. int level;
  5984. int flag = 1;
  5985. TBL_PC* sd;
  5986. sd = script_rid2sd(st);
  5987. if( sd == NULL )
  5988. return 0;// no player attached, report source
  5989. id = script_getnum(st,2);
  5990. level = script_getnum(st,3);
  5991. if( script_hasdata(st,4) )
  5992. flag = script_getnum(st,4);
  5993. pc_skill(sd, id, level, flag);
  5994. return 0;
  5995. }
  5996. /// Changes the level of a player skill.
  5997. /// like skill, but <flag> defaults to 2
  5998. ///
  5999. /// addtoskill <skill id>,<amount>,<flag>
  6000. /// addtoskill <skill id>,<amount>
  6001. ///
  6002. /// @see skill
  6003. BUILDIN_FUNC(addtoskill)
  6004. {
  6005. int id;
  6006. int level;
  6007. int flag = 2;
  6008. TBL_PC* sd;
  6009. sd = script_rid2sd(st);
  6010. if( sd == NULL )
  6011. return 0;// no player attached, report source
  6012. id = script_getnum(st,2);
  6013. level = script_getnum(st,3);
  6014. if( script_hasdata(st,4) )
  6015. flag = script_getnum(st,4);
  6016. pc_skill(sd, id, level, flag);
  6017. return 0;
  6018. }
  6019. /// Increases the level of a guild skill.
  6020. ///
  6021. /// guildskill <skill id>,<amount>;
  6022. BUILDIN_FUNC(guildskill)
  6023. {
  6024. int id;
  6025. int level;
  6026. TBL_PC* sd;
  6027. int i;
  6028. sd = script_rid2sd(st);
  6029. if( sd == NULL )
  6030. return 0;// no player attached, report source
  6031. id = script_getnum(st,2);
  6032. level = script_getnum(st,3);
  6033. for( i=0; i < level; i++ )
  6034. guild_skillup(sd, id);
  6035. return 0;
  6036. }
  6037. /// Returns the level of the player skill.
  6038. ///
  6039. /// getskilllv(<skill id>) -> <level>
  6040. BUILDIN_FUNC(getskilllv)
  6041. {
  6042. int id;
  6043. TBL_PC* sd;
  6044. sd = script_rid2sd(st);
  6045. if( sd == NULL )
  6046. return 0;// no player attached, report source
  6047. id = script_getnum(st,2);
  6048. script_pushint(st, pc_checkskill(sd,id));
  6049. return 0;
  6050. }
  6051. /// Returns the level of the guild skill.
  6052. ///
  6053. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6054. BUILDIN_FUNC(getgdskilllv)
  6055. {
  6056. int guild_id;
  6057. int skill_id;
  6058. struct guild* g;
  6059. guild_id = script_getnum(st,2);
  6060. skill_id = script_getnum(st,3);
  6061. g = guild_search(guild_id);
  6062. if( g == NULL )
  6063. script_pushint(st, -1);
  6064. else
  6065. script_pushint(st, guild_checkskill(g,skill_id));
  6066. return 0;
  6067. }
  6068. /// Returns the 'basic_skill_check' setting.
  6069. /// This config determines if the server checks the skill level of NV_BASIC
  6070. /// before allowing the basic actions.
  6071. ///
  6072. /// basicskillcheck() -> <bool>
  6073. BUILDIN_FUNC(basicskillcheck)
  6074. {
  6075. script_pushint(st, battle_config.basic_skill_check);
  6076. return 0;
  6077. }
  6078. /// Returns the GM level of the player.
  6079. ///
  6080. /// getgmlevel() -> <level>
  6081. BUILDIN_FUNC(getgmlevel)
  6082. {
  6083. TBL_PC* sd;
  6084. sd = script_rid2sd(st);
  6085. if( sd == NULL )
  6086. return 0;// no player attached, report source
  6087. script_pushint(st, pc_isGM(sd));
  6088. return 0;
  6089. }
  6090. /// Terminates the execution of this script instance.
  6091. ///
  6092. /// end
  6093. BUILDIN_FUNC(end)
  6094. {
  6095. st->state = END;
  6096. return 0;
  6097. }
  6098. /// Checks if the player has that effect state (option).
  6099. ///
  6100. /// checkoption(<option>) -> <bool>
  6101. BUILDIN_FUNC(checkoption)
  6102. {
  6103. int option;
  6104. TBL_PC* sd;
  6105. sd = script_rid2sd(st);
  6106. if( sd == NULL )
  6107. return 0;// no player attached, report source
  6108. option = script_getnum(st,2);
  6109. if( sd->sc.option&option )
  6110. script_pushint(st, 1);
  6111. else
  6112. script_pushint(st, 0);
  6113. return 0;
  6114. }
  6115. /// Checks if the player is in that body state (opt1).
  6116. ///
  6117. /// checkoption1(<opt1>) -> <bool>
  6118. BUILDIN_FUNC(checkoption1)
  6119. {
  6120. int opt1;
  6121. TBL_PC* sd;
  6122. sd = script_rid2sd(st);
  6123. if( sd == NULL )
  6124. return 0;// no player attached, report source
  6125. opt1 = script_getnum(st,2);
  6126. if( sd->sc.opt1 == opt1 )
  6127. script_pushint(st, 1);
  6128. else
  6129. script_pushint(st, 0);
  6130. return 0;
  6131. }
  6132. /// Checks if the player has that health state (opt2).
  6133. ///
  6134. /// checkoption2(<opt2>) -> <bool>
  6135. BUILDIN_FUNC(checkoption2)
  6136. {
  6137. int opt2;
  6138. TBL_PC* sd;
  6139. sd = script_rid2sd(st);
  6140. if( sd == NULL )
  6141. return 0;// no player attached, report source
  6142. opt2 = script_getnum(st,2);
  6143. if( sd->sc.opt2&opt2 )
  6144. script_pushint(st, 1);
  6145. else
  6146. script_pushint(st, 0);
  6147. return 0;
  6148. }
  6149. /// Changes the effect state (option) of the player.
  6150. /// <flag> defaults to 1
  6151. /// <flag>=0 : removes the option
  6152. /// <flag>=other : adds the option
  6153. ///
  6154. /// setoption <option>,<flag>;
  6155. /// setoption <option>;
  6156. BUILDIN_FUNC(setoption)
  6157. {
  6158. int option;
  6159. int flag = 1;
  6160. TBL_PC* sd;
  6161. sd = script_rid2sd(st);
  6162. if( sd == NULL )
  6163. return 0;// no player attached, report source
  6164. option = script_getnum(st,2);
  6165. if( script_hasdata(st,3) )
  6166. flag = script_getnum(st,3);
  6167. else if( !option ){// Request to remove everything.
  6168. flag = 0;
  6169. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6170. }
  6171. if( flag ){// Add option
  6172. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6173. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6174. pc_setoption(sd, sd->sc.option|option);
  6175. } else// Remove option
  6176. pc_setoption(sd, sd->sc.option&~option);
  6177. return 0;
  6178. }
  6179. /// Returns if the player has a cart.
  6180. ///
  6181. /// checkcart() -> <bool>
  6182. ///
  6183. /// @author Valaris
  6184. BUILDIN_FUNC(checkcart)
  6185. {
  6186. TBL_PC* sd;
  6187. sd = script_rid2sd(st);
  6188. if( sd == NULL )
  6189. return 0;// no player attached, report source
  6190. if( pc_iscarton(sd) )
  6191. script_pushint(st, 1);
  6192. else
  6193. script_pushint(st, 0);
  6194. return 0;
  6195. }
  6196. /// Sets the cart of the player.
  6197. /// <type> defaults to 1
  6198. /// <type>=0 : removes the cart
  6199. /// <type>=1 : Normal cart
  6200. /// <type>=2 : Wooden cart
  6201. /// <type>=3 : Covered cart with flowers and ferns
  6202. /// <type>=4 : Wooden cart with a Panda doll on the back
  6203. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6204. ///
  6205. /// setcart <type>;
  6206. /// setcart;
  6207. BUILDIN_FUNC(setcart)
  6208. {
  6209. int type = 1;
  6210. TBL_PC* sd;
  6211. sd = script_rid2sd(st);
  6212. if( sd == NULL )
  6213. return 0;// no player attached, report source
  6214. if( script_hasdata(st,2) )
  6215. type = script_getnum(st,2);
  6216. pc_setcart(sd, type);
  6217. return 0;
  6218. }
  6219. /// Returns if the player has a falcon.
  6220. ///
  6221. /// checkfalcon() -> <bool>
  6222. ///
  6223. /// @author Valaris
  6224. BUILDIN_FUNC(checkfalcon)
  6225. {
  6226. TBL_PC* sd;
  6227. sd = script_rid2sd(st);
  6228. if( sd == NULL )
  6229. return 0;// no player attached, report source
  6230. if( pc_isfalcon(sd) )
  6231. script_pushint(st, 1);
  6232. else
  6233. script_pushint(st, 0);
  6234. return 0;
  6235. }
  6236. /// Sets if the player has a falcon or not.
  6237. /// <flag> defaults to 1
  6238. ///
  6239. /// setfalcon <flag>;
  6240. /// setfalcon;
  6241. BUILDIN_FUNC(setfalcon)
  6242. {
  6243. int flag = 1;
  6244. TBL_PC* sd;
  6245. sd = script_rid2sd(st);
  6246. if( sd == NULL )
  6247. return 0;// no player attached, report source
  6248. if( script_hasdata(st,2) )
  6249. flag = script_getnum(st,2);
  6250. pc_setfalcon(sd, flag);
  6251. return 0;
  6252. }
  6253. /// Returns if the player is riding.
  6254. ///
  6255. /// checkriding() -> <bool>
  6256. ///
  6257. /// @author Valaris
  6258. BUILDIN_FUNC(checkriding)
  6259. {
  6260. TBL_PC* sd;
  6261. sd = script_rid2sd(st);
  6262. if( sd == NULL )
  6263. return 0;// no player attached, report source
  6264. if( pc_isriding(sd) )
  6265. script_pushint(st, 1);
  6266. else
  6267. script_pushint(st, 0);
  6268. return 0;
  6269. }
  6270. /// Sets if the player is riding.
  6271. /// <flag> defaults to 1
  6272. ///
  6273. /// setriding <flag>;
  6274. /// setriding;
  6275. BUILDIN_FUNC(setriding)
  6276. {
  6277. int flag = 1;
  6278. TBL_PC* sd;
  6279. sd = script_rid2sd(st);
  6280. if( sd == NULL )
  6281. return 0;// no player attached, report source
  6282. if( script_hasdata(st,2) )
  6283. flag = script_getnum(st,2);
  6284. pc_setriding(sd, flag);
  6285. return 0;
  6286. }
  6287. /// Sets the save point of the player.
  6288. ///
  6289. /// save "<map name>",<x>,<y>
  6290. /// savepoint "<map name>",<x>,<y>
  6291. BUILDIN_FUNC(savepoint)
  6292. {
  6293. int x;
  6294. int y;
  6295. short map;
  6296. const char* str;
  6297. TBL_PC* sd;
  6298. sd = script_rid2sd(st);
  6299. if( sd == NULL )
  6300. return 0;// no player attached, report source
  6301. str = script_getstr(st, 2);
  6302. x = script_getnum(st,3);
  6303. y = script_getnum(st,4);
  6304. map = mapindex_name2id(str);
  6305. if( map )
  6306. pc_setsavepoint(sd, map, x, y);
  6307. return 0;
  6308. }
  6309. /*==========================================
  6310. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6311. *------------------------------------------*/
  6312. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6313. {
  6314. int type;
  6315. time_t timer;
  6316. struct tm *t;
  6317. type=script_getnum(st,2);
  6318. switch(type){
  6319. case 2:
  6320. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6321. // from the system clock.)
  6322. script_pushint(st,(int)time(NULL));
  6323. break;
  6324. case 1:
  6325. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6326. time(&timer);
  6327. t=localtime(&timer);
  6328. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6329. break;
  6330. case 0:
  6331. default:
  6332. //type 0:(System Ticks)
  6333. script_pushint(st,gettick());
  6334. break;
  6335. }
  6336. return 0;
  6337. }
  6338. /*==========================================
  6339. * GetTime(Type);
  6340. * 1: Sec 2: Min 3: Hour
  6341. * 4: WeekDay 5: MonthDay 6: Month
  6342. * 7: Year
  6343. *------------------------------------------*/
  6344. BUILDIN_FUNC(gettime) /* Asgard Version */
  6345. {
  6346. int type;
  6347. time_t timer;
  6348. struct tm *t;
  6349. type=script_getnum(st,2);
  6350. time(&timer);
  6351. t=localtime(&timer);
  6352. switch(type){
  6353. case 1://Sec(0~59)
  6354. script_pushint(st,t->tm_sec);
  6355. break;
  6356. case 2://Min(0~59)
  6357. script_pushint(st,t->tm_min);
  6358. break;
  6359. case 3://Hour(0~23)
  6360. script_pushint(st,t->tm_hour);
  6361. break;
  6362. case 4://WeekDay(0~6)
  6363. script_pushint(st,t->tm_wday);
  6364. break;
  6365. case 5://MonthDay(01~31)
  6366. script_pushint(st,t->tm_mday);
  6367. break;
  6368. case 6://Month(01~12)
  6369. script_pushint(st,t->tm_mon+1);
  6370. break;
  6371. case 7://Year(20xx)
  6372. script_pushint(st,t->tm_year+1900);
  6373. break;
  6374. case 8://Year Day(01~366)
  6375. script_pushint(st,t->tm_yday+1);
  6376. break;
  6377. default://(format error)
  6378. script_pushint(st,-1);
  6379. break;
  6380. }
  6381. return 0;
  6382. }
  6383. /*==========================================
  6384. * GetTimeStr("TimeFMT", Length);
  6385. *------------------------------------------*/
  6386. BUILDIN_FUNC(gettimestr)
  6387. {
  6388. char *tmpstr;
  6389. const char *fmtstr;
  6390. int maxlen;
  6391. time_t now = time(NULL);
  6392. fmtstr=script_getstr(st,2);
  6393. maxlen=script_getnum(st,3);
  6394. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6395. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6396. tmpstr[maxlen]='\0';
  6397. script_pushstr(st,tmpstr);
  6398. return 0;
  6399. }
  6400. /*==========================================
  6401. * カプラ倉庫を開く
  6402. *------------------------------------------*/
  6403. BUILDIN_FUNC(openstorage)
  6404. {
  6405. TBL_PC* sd;
  6406. sd = script_rid2sd(st);
  6407. if( sd == NULL )
  6408. return 0;
  6409. storage_storageopen(sd);
  6410. return 0;
  6411. }
  6412. BUILDIN_FUNC(guildopenstorage)
  6413. {
  6414. TBL_PC* sd;
  6415. int ret;
  6416. sd = script_rid2sd(st);
  6417. if( sd == NULL )
  6418. return 0;
  6419. ret = storage_guild_storageopen(sd);
  6420. script_pushint(st,ret);
  6421. return 0;
  6422. }
  6423. /*==========================================
  6424. * アイテムによるスキル発動
  6425. *------------------------------------------*/
  6426. BUILDIN_FUNC(itemskill)
  6427. {
  6428. int id,lv;
  6429. TBL_PC *sd=script_rid2sd(st);
  6430. id=script_getnum(st,2);
  6431. lv=script_getnum(st,3);
  6432. if(!sd || sd->ud.skilltimer != -1)
  6433. return 0;
  6434. sd->skillitem=id;
  6435. sd->skillitemlv=lv;
  6436. clif_item_skill(sd,id,lv);
  6437. return 0;
  6438. }
  6439. /*==========================================
  6440. * アイテム作成
  6441. *------------------------------------------*/
  6442. BUILDIN_FUNC(produce)
  6443. {
  6444. int trigger;
  6445. TBL_PC* sd;
  6446. sd = script_rid2sd(st);
  6447. if( sd == NULL )
  6448. return 0;
  6449. trigger=script_getnum(st,2);
  6450. clif_skill_produce_mix_list(sd, trigger);
  6451. return 0;
  6452. }
  6453. /*==========================================
  6454. * NPCでペット作る
  6455. *------------------------------------------*/
  6456. BUILDIN_FUNC(makepet)
  6457. {
  6458. TBL_PC* sd;
  6459. int id,pet_id;
  6460. id=script_getnum(st,2);
  6461. sd = script_rid2sd(st);
  6462. if( sd == NULL )
  6463. return 0;
  6464. pet_id = search_petDB_index(id, PET_CLASS);
  6465. if (pet_id < 0)
  6466. pet_id = search_petDB_index(id, PET_EGG);
  6467. if (pet_id >= 0 && sd) {
  6468. sd->catch_target_class = pet_db[pet_id].class_;
  6469. intif_create_pet(
  6470. sd->status.account_id, sd->status.char_id,
  6471. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6472. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6473. 100, 0, 1, pet_db[pet_id].jname);
  6474. }
  6475. return 0;
  6476. }
  6477. /*==========================================
  6478. * NPCで経験値上げる
  6479. *------------------------------------------*/
  6480. BUILDIN_FUNC(getexp)
  6481. {
  6482. TBL_PC* sd;
  6483. int base=0,job=0;
  6484. double bonus;
  6485. sd = script_rid2sd(st);
  6486. if( sd == NULL )
  6487. return 0;
  6488. base=script_getnum(st,2);
  6489. job =script_getnum(st,3);
  6490. if(base<0 || job<0)
  6491. return 0;
  6492. // bonus for npc-given exp
  6493. bonus = battle_config.quest_exp_rate / 100.;
  6494. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6495. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6496. pc_gainexp(sd, NULL, base, job);
  6497. return 0;
  6498. }
  6499. /*==========================================
  6500. * Gain guild exp [Celest]
  6501. *------------------------------------------*/
  6502. BUILDIN_FUNC(guildgetexp)
  6503. {
  6504. TBL_PC* sd;
  6505. int exp;
  6506. sd = script_rid2sd(st);
  6507. if( sd == NULL )
  6508. return 0;
  6509. exp = script_getnum(st,2);
  6510. if(exp < 0)
  6511. return 0;
  6512. if(sd && sd->status.guild_id > 0)
  6513. guild_getexp (sd, exp);
  6514. return 0;
  6515. }
  6516. /*==========================================
  6517. * Changes the guild master of a guild [Skotlex]
  6518. *------------------------------------------*/
  6519. BUILDIN_FUNC(guildchangegm)
  6520. {
  6521. TBL_PC *sd;
  6522. int guild_id;
  6523. const char *name;
  6524. guild_id = script_getnum(st,2);
  6525. name = script_getstr(st,3);
  6526. sd=map_nick2sd(name);
  6527. if (!sd)
  6528. script_pushint(st,0);
  6529. else
  6530. script_pushint(st,guild_gm_change(guild_id, sd));
  6531. return 0;
  6532. }
  6533. /*==========================================
  6534. * モンスター発生
  6535. *------------------------------------------*/
  6536. BUILDIN_FUNC(monster)
  6537. {
  6538. const char* map = script_getstr(st,2);
  6539. int x = script_getnum(st,3);
  6540. int y = script_getnum(st,4);
  6541. const char* str = script_getstr(st,5);
  6542. int class_ = script_getnum(st,6);
  6543. int amount = script_getnum(st,7);
  6544. const char* event = "";
  6545. struct map_session_data* sd;
  6546. int m;
  6547. if( script_hasdata(st,8) )
  6548. {
  6549. event = script_getstr(st,8);
  6550. check_event(st, event);
  6551. }
  6552. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6553. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6554. return 1;
  6555. }
  6556. sd = map_id2sd(st->rid);
  6557. if( sd && strcmp(map,"this") == 0 )
  6558. m = sd->bl.m;
  6559. else
  6560. m = map_mapname2mapid(map);
  6561. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6562. return 0;
  6563. }
  6564. /*==========================================
  6565. * モンスター発生
  6566. *------------------------------------------*/
  6567. BUILDIN_FUNC(areamonster)
  6568. {
  6569. const char* map = script_getstr(st,2);
  6570. int x0 = script_getnum(st,3);
  6571. int y0 = script_getnum(st,4);
  6572. int x1 = script_getnum(st,5);
  6573. int y1 = script_getnum(st,6);
  6574. const char* str = script_getstr(st,7);
  6575. int class_ = script_getnum(st,8);
  6576. int amount = script_getnum(st,9);
  6577. const char* event = "";
  6578. struct map_session_data* sd;
  6579. int m;
  6580. if( script_hasdata(st,10) )
  6581. {
  6582. event = script_getstr(st,10);
  6583. check_event(st, event);
  6584. }
  6585. sd = map_id2sd(st->rid);
  6586. if( sd && strcmp(map,"this") == 0 )
  6587. m = sd->bl.m;
  6588. else
  6589. m = map_mapname2mapid(map);
  6590. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6591. return 0;
  6592. }
  6593. /*==========================================
  6594. * モンスター削除
  6595. *------------------------------------------*/
  6596. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6597. {
  6598. TBL_MOB* md = (TBL_MOB*)bl;
  6599. char *event=va_arg(ap,char *);
  6600. int allflag=va_arg(ap,int);
  6601. if(!allflag){
  6602. if(strcmp(event,md->npc_event)==0)
  6603. status_kill(bl);
  6604. }else{
  6605. if(!md->spawn)
  6606. status_kill(bl);
  6607. }
  6608. return 0;
  6609. }
  6610. BUILDIN_FUNC(killmonster)
  6611. {
  6612. const char *mapname,*event;
  6613. int m,allflag=0;
  6614. mapname=script_getstr(st,2);
  6615. event=script_getstr(st,3);
  6616. if(strcmp(event,"All")==0)
  6617. allflag = 1;
  6618. else
  6619. check_event(st, event);
  6620. if( (m=map_mapname2mapid(mapname))<0 )
  6621. return 0;
  6622. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6623. return 0;
  6624. }
  6625. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6626. {
  6627. status_kill(bl);
  6628. return 0;
  6629. }
  6630. BUILDIN_FUNC(killmonsterall)
  6631. {
  6632. const char *mapname;
  6633. int m;
  6634. mapname=script_getstr(st,2);
  6635. if( (m=map_mapname2mapid(mapname))<0 )
  6636. return 0;
  6637. map_foreachinmap(buildin_killmonsterall_sub,
  6638. m,BL_MOB);
  6639. return 0;
  6640. }
  6641. /*==========================================
  6642. * Creates a clone of a player.
  6643. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6644. *------------------------------------------*/
  6645. BUILDIN_FUNC(clone)
  6646. {
  6647. TBL_PC *sd, *msd=NULL;
  6648. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6649. unsigned int duration = 0;
  6650. const char *map,*event="";
  6651. map=script_getstr(st,2);
  6652. x=script_getnum(st,3);
  6653. y=script_getnum(st,4);
  6654. event=script_getstr(st,5);
  6655. char_id=script_getnum(st,6);
  6656. if( script_hasdata(st,7) )
  6657. master_id=script_getnum(st,7);
  6658. if( script_hasdata(st,8) )
  6659. mode=script_getnum(st,8);
  6660. if( script_hasdata(st,9) )
  6661. flag=script_getnum(st,9);
  6662. if( script_hasdata(st,10) )
  6663. duration=script_getnum(st,10);
  6664. check_event(st, event);
  6665. m = map_mapname2mapid(map);
  6666. if (m < 0) return 0;
  6667. sd = map_charid2sd(char_id);
  6668. if (master_id) {
  6669. msd = map_charid2sd(master_id);
  6670. if (msd)
  6671. master_id = msd->bl.id;
  6672. else
  6673. master_id = 0;
  6674. }
  6675. if (sd) //Return ID of newly crafted clone.
  6676. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6677. else //Failed to create clone.
  6678. script_pushint(st,0);
  6679. return 0;
  6680. }
  6681. /*==========================================
  6682. * イベント実行
  6683. *------------------------------------------*/
  6684. BUILDIN_FUNC(doevent)
  6685. {
  6686. const char* event = script_getstr(st,2);
  6687. check_event(st, event);
  6688. npc_event(map_id2sd(st->rid),event,0);
  6689. return 0;
  6690. }
  6691. /*==========================================
  6692. * NPC主体イベント実行
  6693. *------------------------------------------*/
  6694. BUILDIN_FUNC(donpcevent)
  6695. {
  6696. const char* event = script_getstr(st,2);
  6697. check_event(st, event);
  6698. npc_event_do(event);
  6699. return 0;
  6700. }
  6701. /// for Aegis compatibility
  6702. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6703. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6704. {
  6705. const char* npc = script_getstr(st,2);
  6706. const char* command = script_getstr(st,3);
  6707. char event[51];
  6708. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6709. check_event(st, event);
  6710. npc_event_do(event);
  6711. return 0;
  6712. }
  6713. /*==========================================
  6714. * イベントタイマー追加
  6715. *------------------------------------------*/
  6716. BUILDIN_FUNC(addtimer)
  6717. {
  6718. int tick = script_getnum(st,2);
  6719. const char* event = script_getstr(st, 3);
  6720. TBL_PC* sd;
  6721. check_event(st, event);
  6722. sd = script_rid2sd(st);
  6723. if( sd == NULL )
  6724. return 0;
  6725. pc_addeventtimer(sd,tick,event);
  6726. return 0;
  6727. }
  6728. /*==========================================
  6729. * イベントタイマー削除
  6730. *------------------------------------------*/
  6731. BUILDIN_FUNC(deltimer)
  6732. {
  6733. const char *event;
  6734. TBL_PC* sd;
  6735. event=script_getstr(st, 2);
  6736. sd = script_rid2sd(st);
  6737. if( sd == NULL )
  6738. return 0;
  6739. check_event(st, event);
  6740. pc_deleventtimer(sd,event);
  6741. return 0;
  6742. }
  6743. /*==========================================
  6744. * イベントタイマーのカウント値追加
  6745. *------------------------------------------*/
  6746. BUILDIN_FUNC(addtimercount)
  6747. {
  6748. const char *event;
  6749. int tick;
  6750. TBL_PC* sd;
  6751. event=script_getstr(st, 2);
  6752. tick=script_getnum(st,3);
  6753. sd = script_rid2sd(st);
  6754. if( sd == NULL )
  6755. return 0;
  6756. check_event(st, event);
  6757. pc_addeventtimercount(sd,event,tick);
  6758. return 0;
  6759. }
  6760. /*==========================================
  6761. * NPCタイマー初期化
  6762. *------------------------------------------*/
  6763. BUILDIN_FUNC(initnpctimer)
  6764. {
  6765. struct npc_data *nd;
  6766. int flag = 0;
  6767. if( script_hasdata(st,3) )
  6768. { //Two arguments: NPC name and attach flag.
  6769. nd = npc_name2id(script_getstr(st, 2));
  6770. flag = script_getnum(st,3);
  6771. } else
  6772. if( script_hasdata(st,2) )
  6773. { //Check if argument is numeric (flag) or string (npc name)
  6774. struct script_data *data;
  6775. data = script_getdata(st,2);
  6776. get_val(st,data);
  6777. if( data_isstring(data) ) //NPC name
  6778. nd = npc_name2id(script_getstr(st, 2));
  6779. else if( data_isint(data) ) { //Flag
  6780. nd = (struct npc_data *)map_id2bl(st->oid);
  6781. flag = script_getnum(st,3);
  6782. } else {
  6783. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6784. return 1;
  6785. }
  6786. } else
  6787. nd=(struct npc_data *)map_id2bl(st->oid);
  6788. if (!nd) return 0;
  6789. if (flag) { //Attach
  6790. TBL_PC* sd = script_rid2sd(st);
  6791. if( sd == NULL )
  6792. return 0;
  6793. nd->u.scr.rid = sd->bl.id;
  6794. }
  6795. npc_settimerevent_tick(nd,0);
  6796. npc_timerevent_start(nd, st->rid);
  6797. return 0;
  6798. }
  6799. /*==========================================
  6800. * NPCタイマー開始
  6801. *------------------------------------------*/
  6802. BUILDIN_FUNC(startnpctimer)
  6803. {
  6804. struct npc_data *nd;
  6805. int flag = 0;
  6806. if( script_hasdata(st,3) )
  6807. { //Two arguments: NPC name and attach flag.
  6808. nd = npc_name2id(script_getstr(st, 2));
  6809. flag = script_getnum(st,3);
  6810. } else
  6811. if( script_hasdata(st,2) )
  6812. { //Check if argument is numeric (flag) or string (npc name)
  6813. struct script_data *data;
  6814. data = script_getdata(st,2);
  6815. get_val(st,data);
  6816. if( data_isstring(data) ) //NPC name
  6817. nd = npc_name2id(script_getstr(st, 2));
  6818. else if( data_isint(data) ) { //Flag
  6819. nd = (struct npc_data *)map_id2bl(st->oid);
  6820. flag = script_getnum(st,3);
  6821. } else {
  6822. ShowError("startnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6823. return 1;
  6824. }
  6825. } else
  6826. nd=(struct npc_data *)map_id2bl(st->oid);
  6827. if (!nd) return 0;
  6828. if (flag) { //Attach
  6829. TBL_PC* sd = script_rid2sd(st);
  6830. if( sd == NULL )
  6831. return 0;
  6832. nd->u.scr.rid = sd->bl.id;
  6833. }
  6834. npc_timerevent_start(nd, st->rid);
  6835. return 0;
  6836. }
  6837. /*==========================================
  6838. * NPCタイマー停止
  6839. *------------------------------------------*/
  6840. BUILDIN_FUNC(stopnpctimer)
  6841. {
  6842. struct npc_data *nd;
  6843. int flag = 0;
  6844. if( script_hasdata(st,3) )
  6845. { //Two arguments: NPC name and attach flag.
  6846. nd = npc_name2id(script_getstr(st, 2));
  6847. flag = script_getnum(st,3);
  6848. } else
  6849. if( script_hasdata(st,2) )
  6850. { //Check if argument is numeric (flag) or string (npc name)
  6851. struct script_data *data;
  6852. data = script_getdata(st,2);
  6853. get_val(st,data);
  6854. if( data_isstring(data) ) //NPC name
  6855. nd = npc_name2id(script_getstr(st, 2));
  6856. else if( data_isint(data) ) { //Flag
  6857. nd = (struct npc_data *)map_id2bl(st->oid);
  6858. flag = script_getnum(st,3);
  6859. } else {
  6860. ShowError("stopnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6861. return 1;
  6862. }
  6863. } else
  6864. nd=(struct npc_data *)map_id2bl(st->oid);
  6865. if (!nd) return 0;
  6866. if (flag) //Detach
  6867. nd->u.scr.rid = 0;
  6868. npc_timerevent_stop(nd);
  6869. return 0;
  6870. }
  6871. /*==========================================
  6872. * NPCタイマー情報所得
  6873. *------------------------------------------*/
  6874. BUILDIN_FUNC(getnpctimer)
  6875. {
  6876. struct npc_data *nd;
  6877. TBL_PC *sd;
  6878. int type=script_getnum(st,2);
  6879. int val=0;
  6880. if( script_hasdata(st,3) )
  6881. nd = npc_name2id(script_getstr(st,3));
  6882. else
  6883. nd = (struct npc_data *)map_id2bl(st->oid);
  6884. if (!nd || nd->bl.type != BL_NPC)
  6885. {
  6886. script_pushint(st,0);
  6887. ShowError("getnpctimer: Invalid NPC\n");
  6888. return 1;
  6889. }
  6890. switch(type){
  6891. case 0: val=npc_gettimerevent_tick(nd); break;
  6892. case 1:
  6893. if (nd->u.scr.rid) {
  6894. sd = map_id2sd(nd->u.scr.rid);
  6895. if (!sd) {
  6896. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6897. break;
  6898. }
  6899. val = (sd->npc_timer_id != -1);
  6900. } else
  6901. val= (nd->u.scr.timerid !=-1);
  6902. break;
  6903. case 2: val= nd->u.scr.timeramount; break;
  6904. }
  6905. script_pushint(st,val);
  6906. return 0;
  6907. }
  6908. /*==========================================
  6909. * NPCタイマー値設定
  6910. *------------------------------------------*/
  6911. BUILDIN_FUNC(setnpctimer)
  6912. {
  6913. int tick;
  6914. struct npc_data *nd;
  6915. tick=script_getnum(st,2);
  6916. if( script_hasdata(st,3) )
  6917. nd=npc_name2id(script_getstr(st,3));
  6918. else
  6919. nd=(struct npc_data *)map_id2bl(st->oid);
  6920. npc_settimerevent_tick(nd,tick);
  6921. return 0;
  6922. }
  6923. /*==========================================
  6924. * attaches the player rid to the timer [Celest]
  6925. *------------------------------------------*/
  6926. BUILDIN_FUNC(attachnpctimer)
  6927. {
  6928. TBL_PC *sd;
  6929. struct npc_data *nd;
  6930. nd=(struct npc_data *)map_id2bl(st->oid);
  6931. if( script_hasdata(st,2) )
  6932. sd=map_nick2sd(script_getstr(st,2));
  6933. else
  6934. sd = script_rid2sd(st);
  6935. if (sd==NULL)
  6936. return 0;
  6937. nd->u.scr.rid = sd->bl.id;
  6938. return 0;
  6939. }
  6940. /*==========================================
  6941. * detaches a player rid from the timer [Celest]
  6942. *------------------------------------------*/
  6943. BUILDIN_FUNC(detachnpctimer)
  6944. {
  6945. struct npc_data *nd;
  6946. if( script_hasdata(st,2) )
  6947. nd=npc_name2id(script_getstr(st,2));
  6948. else
  6949. nd=(struct npc_data *)map_id2bl(st->oid);
  6950. nd->u.scr.rid = 0;
  6951. return 0;
  6952. }
  6953. /*==========================================
  6954. * To avoid "player not attached" script errors, this function is provided,
  6955. * it checks if there is a player attached to the current script. [Skotlex]
  6956. * If no, returns 0, if yes, returns the account_id of the attached player.
  6957. *------------------------------------------*/
  6958. BUILDIN_FUNC(playerattached)
  6959. {
  6960. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  6961. script_pushint(st,0);
  6962. else
  6963. script_pushint(st,st->rid);
  6964. return 0;
  6965. }
  6966. /*==========================================
  6967. * 天の声アナウンス
  6968. *------------------------------------------*/
  6969. BUILDIN_FUNC(announce)
  6970. {
  6971. const char *str, *color=NULL;
  6972. int flag;
  6973. str=script_getstr(st,2);
  6974. flag=script_getnum(st,3);
  6975. if (script_hasdata(st,4))
  6976. color=script_getstr(st,4);
  6977. if(flag&0x0f){
  6978. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  6979. (struct block_list *)script_rid2sd(st);
  6980. if( bl == NULL )
  6981. return 0;
  6982. if (color)
  6983. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  6984. else
  6985. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  6986. }else {
  6987. if (color)
  6988. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  6989. else
  6990. intif_GMmessage(str,(int)strlen(str)+1,flag);
  6991. }
  6992. return 0;
  6993. }
  6994. /*==========================================
  6995. * 天の声アナウンス(特定マップ)
  6996. *------------------------------------------*/
  6997. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  6998. {
  6999. char *str, *color;
  7000. int len,flag;
  7001. str=va_arg(ap,char *);
  7002. len=va_arg(ap,int);
  7003. flag=va_arg(ap,int);
  7004. color=va_arg(ap,char *);
  7005. if (color)
  7006. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  7007. else
  7008. clif_GMmessage(bl,str,len,flag|3);
  7009. return 0;
  7010. }
  7011. BUILDIN_FUNC(mapannounce)
  7012. {
  7013. const char *mapname,*str, *color=NULL;
  7014. int flag,m;
  7015. mapname=script_getstr(st,2);
  7016. str=script_getstr(st,3);
  7017. flag=script_getnum(st,4);
  7018. if (script_hasdata(st,5))
  7019. color=script_getstr(st,5);
  7020. if( (m=map_mapname2mapid(mapname))<0 )
  7021. return 0;
  7022. map_foreachinmap(buildin_mapannounce_sub,
  7023. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7024. return 0;
  7025. }
  7026. /*==========================================
  7027. * 天の声アナウンス(特定エリア)
  7028. *------------------------------------------*/
  7029. BUILDIN_FUNC(areaannounce)
  7030. {
  7031. const char *map,*str,*color=NULL;
  7032. int flag,m;
  7033. int x0,y0,x1,y1;
  7034. map=script_getstr(st,2);
  7035. x0=script_getnum(st,3);
  7036. y0=script_getnum(st,4);
  7037. x1=script_getnum(st,5);
  7038. y1=script_getnum(st,6);
  7039. str=script_getstr(st,7);
  7040. flag=script_getnum(st,8);
  7041. if (script_hasdata(st,9))
  7042. color=script_getstr(st,9);
  7043. if( (m=map_mapname2mapid(map))<0 )
  7044. return 0;
  7045. map_foreachinarea(buildin_mapannounce_sub,
  7046. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7047. return 0;
  7048. }
  7049. /*==========================================
  7050. * ユーザー数所得
  7051. *------------------------------------------*/
  7052. BUILDIN_FUNC(getusers)
  7053. {
  7054. int flag=script_getnum(st,2);
  7055. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7056. int val=0;
  7057. switch(flag&0x07){
  7058. case 0: val=map[bl->m].users; break;
  7059. case 1: val=map_getusers(); break;
  7060. }
  7061. script_pushint(st,val);
  7062. return 0;
  7063. }
  7064. /*==========================================
  7065. * Works like @WHO - displays all online users names in window
  7066. *------------------------------------------*/
  7067. BUILDIN_FUNC(getusersname)
  7068. {
  7069. TBL_PC *sd, *pl_sd;
  7070. int disp_num=1;
  7071. struct s_mapiterator* iter;
  7072. sd = script_rid2sd(st);
  7073. if (!sd) return 0;
  7074. iter = mapit_getallusers();
  7075. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7076. {
  7077. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7078. continue; // skip hidden GMs
  7079. if((disp_num++)%10==0)
  7080. clif_scriptnext(sd,st->oid);
  7081. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7082. }
  7083. mapit_free(iter);
  7084. return 0;
  7085. }
  7086. /*==========================================
  7087. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7088. *------------------------------------------*/
  7089. BUILDIN_FUNC(getmapguildusers)
  7090. {
  7091. const char *str;
  7092. int m, gid;
  7093. int i=0,c=0;
  7094. struct guild *g = NULL;
  7095. str=script_getstr(st,2);
  7096. gid=script_getnum(st,3);
  7097. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7098. script_pushint(st,-1);
  7099. return 0;
  7100. }
  7101. g = guild_search(gid);
  7102. if (g){
  7103. for(i = 0; i < g->max_member; i++)
  7104. {
  7105. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7106. c++;
  7107. }
  7108. }
  7109. script_pushint(st,c);
  7110. return 0;
  7111. }
  7112. /*==========================================
  7113. * マップ指定ユーザー数所得
  7114. *------------------------------------------*/
  7115. BUILDIN_FUNC(getmapusers)
  7116. {
  7117. const char *str;
  7118. int m;
  7119. str=script_getstr(st,2);
  7120. if( (m=map_mapname2mapid(str))< 0){
  7121. script_pushint(st,-1);
  7122. return 0;
  7123. }
  7124. script_pushint(st,map[m].users);
  7125. return 0;
  7126. }
  7127. /*==========================================
  7128. * エリア指定ユーザー数所得
  7129. *------------------------------------------*/
  7130. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7131. {
  7132. int *users=va_arg(ap,int *);
  7133. (*users)++;
  7134. return 0;
  7135. }
  7136. BUILDIN_FUNC(getareausers)
  7137. {
  7138. const char *str;
  7139. int m,x0,y0,x1,y1,users=0;
  7140. str=script_getstr(st,2);
  7141. x0=script_getnum(st,3);
  7142. y0=script_getnum(st,4);
  7143. x1=script_getnum(st,5);
  7144. y1=script_getnum(st,6);
  7145. if( (m=map_mapname2mapid(str))< 0){
  7146. script_pushint(st,-1);
  7147. return 0;
  7148. }
  7149. map_foreachinarea(buildin_getareausers_sub,
  7150. m,x0,y0,x1,y1,BL_PC,&users);
  7151. script_pushint(st,users);
  7152. return 0;
  7153. }
  7154. /*==========================================
  7155. * エリア指定ドロップアイテム数所得
  7156. *------------------------------------------*/
  7157. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7158. {
  7159. int item=va_arg(ap,int);
  7160. int *amount=va_arg(ap,int *);
  7161. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7162. if(drop->item_data.nameid==item)
  7163. (*amount)+=drop->item_data.amount;
  7164. return 0;
  7165. }
  7166. BUILDIN_FUNC(getareadropitem)
  7167. {
  7168. const char *str;
  7169. int m,x0,y0,x1,y1,item,amount=0;
  7170. struct script_data *data;
  7171. str=script_getstr(st,2);
  7172. x0=script_getnum(st,3);
  7173. y0=script_getnum(st,4);
  7174. x1=script_getnum(st,5);
  7175. y1=script_getnum(st,6);
  7176. data=script_getdata(st,7);
  7177. get_val(st,data);
  7178. if( data_isstring(data) ){
  7179. const char *name=conv_str(st,data);
  7180. struct item_data *item_data = itemdb_searchname(name);
  7181. item=UNKNOWN_ITEM_ID;
  7182. if( item_data )
  7183. item=item_data->nameid;
  7184. }else
  7185. item=conv_num(st,data);
  7186. if( (m=map_mapname2mapid(str))< 0){
  7187. script_pushint(st,-1);
  7188. return 0;
  7189. }
  7190. map_foreachinarea(buildin_getareadropitem_sub,
  7191. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7192. script_pushint(st,amount);
  7193. return 0;
  7194. }
  7195. /*==========================================
  7196. * NPCの有効化
  7197. *------------------------------------------*/
  7198. BUILDIN_FUNC(enablenpc)
  7199. {
  7200. const char *str;
  7201. str=script_getstr(st,2);
  7202. npc_enable(str,1);
  7203. return 0;
  7204. }
  7205. /*==========================================
  7206. * NPCの無効化
  7207. *------------------------------------------*/
  7208. BUILDIN_FUNC(disablenpc)
  7209. {
  7210. const char *str;
  7211. str=script_getstr(st,2);
  7212. npc_enable(str,0);
  7213. return 0;
  7214. }
  7215. /*==========================================
  7216. * 隠れているNPCの表示
  7217. *------------------------------------------*/
  7218. BUILDIN_FUNC(hideoffnpc)
  7219. {
  7220. const char *str;
  7221. str=script_getstr(st,2);
  7222. npc_enable(str,2);
  7223. return 0;
  7224. }
  7225. /*==========================================
  7226. * NPCをハイディング
  7227. *------------------------------------------*/
  7228. BUILDIN_FUNC(hideonnpc)
  7229. {
  7230. const char *str;
  7231. str=script_getstr(st,2);
  7232. npc_enable(str,4);
  7233. return 0;
  7234. }
  7235. /// Starts a status effect on the target unit or on the attached player.
  7236. ///
  7237. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7238. BUILDIN_FUNC(sc_start)
  7239. {
  7240. struct block_list* bl;
  7241. enum sc_type type;
  7242. int tick;
  7243. int val1;
  7244. int val4 = 0;
  7245. type = (sc_type)script_getnum(st,2);
  7246. tick = script_getnum(st,3);
  7247. val1 = script_getnum(st,4);
  7248. if( script_hasdata(st,5) )
  7249. bl = map_id2bl(script_getnum(st,5));
  7250. else
  7251. bl = map_id2bl(st->rid);
  7252. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7253. {// When there isn't a duration specified, try to get it from the skill_db
  7254. tick = skill_get_time(status_sc2skill(type), val1);
  7255. }
  7256. if( potion_flag == 1 && potion_target )
  7257. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7258. bl = map_id2bl(potion_target);
  7259. tick /= 2;// Thrown potions only last half.
  7260. val4 = 1;// Mark that this was a thrown sc_effect
  7261. }
  7262. if( bl )
  7263. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7264. return 0;
  7265. }
  7266. /// Starts a status effect on the target unit or on the attached player.
  7267. ///
  7268. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7269. BUILDIN_FUNC(sc_start2)
  7270. {
  7271. struct block_list* bl;
  7272. enum sc_type type;
  7273. int tick;
  7274. int val1;
  7275. int val4 = 0;
  7276. int rate;
  7277. type = (sc_type)script_getnum(st,2);
  7278. tick = script_getnum(st,3);
  7279. val1 = script_getnum(st,4);
  7280. rate = script_getnum(st,5);
  7281. if( script_hasdata(st,6) )
  7282. bl = map_id2bl(script_getnum(st,6));
  7283. else
  7284. bl = map_id2bl(st->rid);
  7285. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7286. {// When there isn't a duration specified, try to get it from the skill_db
  7287. tick = skill_get_time(status_sc2skill(type), val1);
  7288. }
  7289. if( potion_flag == 1 && potion_target )
  7290. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7291. bl = map_id2bl(potion_target);
  7292. tick /= 2;// Thrown potions only last half.
  7293. val4 = 1;// Mark that this was a thrown sc_effect
  7294. }
  7295. if( bl )
  7296. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7297. return 0;
  7298. }
  7299. /// Starts a status effect on the target unit or on the attached player.
  7300. ///
  7301. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7302. BUILDIN_FUNC(sc_start4)
  7303. {
  7304. struct block_list* bl;
  7305. enum sc_type type;
  7306. int tick;
  7307. int val1;
  7308. int val2;
  7309. int val3;
  7310. int val4;
  7311. type = (sc_type)script_getnum(st,2);
  7312. tick = script_getnum(st,3);
  7313. val1 = script_getnum(st,4);
  7314. val2 = script_getnum(st,5);
  7315. val3 = script_getnum(st,6);
  7316. val4 = script_getnum(st,7);
  7317. if( script_hasdata(st,8) )
  7318. bl = map_id2bl(script_getnum(st,8));
  7319. else
  7320. bl = map_id2bl(st->rid);
  7321. if( tick == 0 && val1 > 0 && type > SC_NONE && type < SC_MAX && status_sc2skill(type) != 0 )
  7322. {// When there isn't a duration specified, try to get it from the skill_db
  7323. tick = skill_get_time(status_sc2skill(type), val1);
  7324. }
  7325. if( potion_flag == 1 && potion_target )
  7326. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7327. bl = map_id2bl(potion_target);
  7328. tick /= 2;// Thrown potions only last half.
  7329. }
  7330. if( bl )
  7331. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7332. return 0;
  7333. }
  7334. /// Ends one or all status effects on the target unit or on the attached player.
  7335. ///
  7336. /// sc_end <effect_id>{,<unit_id>};
  7337. BUILDIN_FUNC(sc_end)
  7338. {
  7339. struct block_list* bl;
  7340. int type;
  7341. type = script_getnum(st,2);
  7342. if( script_hasdata(st,3) )
  7343. bl = map_id2bl(script_getnum(st,3));
  7344. else
  7345. bl = map_id2bl(st->rid);
  7346. if( potion_flag==1 && potion_target )
  7347. {//##TODO how does this work [FlavioJS]
  7348. bl = map_id2bl(potion_target);
  7349. }
  7350. if( !bl ) return 0;
  7351. if( type >= 0 && type < SC_MAX )
  7352. {
  7353. struct status_change *sc = status_get_sc(bl);
  7354. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7355. if (!sce) return 0;
  7356. //This should help status_change_end force disabling the SC in case it has no limit.
  7357. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7358. status_change_end(bl, (sc_type)type, INVALID_TIMER);
  7359. } else
  7360. status_change_clear(bl, 2);// remove all effects
  7361. return 0;
  7362. }
  7363. /*==========================================
  7364. * 状態異常耐性を計算した確率を返す
  7365. *------------------------------------------*/
  7366. BUILDIN_FUNC(getscrate)
  7367. {
  7368. struct block_list *bl;
  7369. int type,rate;
  7370. type=script_getnum(st,2);
  7371. rate=script_getnum(st,3);
  7372. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7373. bl = map_id2bl(script_getnum(st,4));
  7374. else
  7375. bl = map_id2bl(st->rid);
  7376. if (bl)
  7377. rate = status_get_sc_def(bl, (sc_type)type, 10000, 10000, 0);
  7378. script_pushint(st,rate);
  7379. return 0;
  7380. }
  7381. /*==========================================
  7382. *
  7383. *------------------------------------------*/
  7384. BUILDIN_FUNC(debugmes)
  7385. {
  7386. const char *str;
  7387. str=script_getstr(st,2);
  7388. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7389. return 0;
  7390. }
  7391. /*==========================================
  7392. *捕獲アイテム使用
  7393. *------------------------------------------*/
  7394. BUILDIN_FUNC(catchpet)
  7395. {
  7396. int pet_id;
  7397. TBL_PC *sd;
  7398. pet_id= script_getnum(st,2);
  7399. sd=script_rid2sd(st);
  7400. if( sd == NULL )
  7401. return 0;
  7402. pet_catch_process1(sd,pet_id);
  7403. return 0;
  7404. }
  7405. /*==========================================
  7406. * [orn]
  7407. *------------------------------------------*/
  7408. BUILDIN_FUNC(homunculus_evolution)
  7409. {
  7410. TBL_PC *sd;
  7411. sd=script_rid2sd(st);
  7412. if( sd == NULL )
  7413. return 0;
  7414. if(merc_is_hom_active(sd->hd))
  7415. {
  7416. if (sd->hd->homunculus.intimacy > 91000)
  7417. merc_hom_evolution(sd->hd);
  7418. else
  7419. clif_emotion(&sd->hd->bl, 4) ; //swt
  7420. }
  7421. return 0;
  7422. }
  7423. // [Zephyrus]
  7424. BUILDIN_FUNC(homunculus_shuffle)
  7425. {
  7426. TBL_PC *sd;
  7427. sd=script_rid2sd(st);
  7428. if( sd == NULL )
  7429. return 0;
  7430. if(merc_is_hom_active(sd->hd))
  7431. merc_hom_shuffle(sd->hd);
  7432. return 0;
  7433. }
  7434. //These two functions bring the eA MAPID_* class functionality to scripts.
  7435. BUILDIN_FUNC(eaclass)
  7436. {
  7437. int class_;
  7438. if( script_hasdata(st,2) )
  7439. class_ = script_getnum(st,2);
  7440. else {
  7441. TBL_PC *sd;
  7442. sd=script_rid2sd(st);
  7443. if (!sd) {
  7444. script_pushint(st,-1);
  7445. return 0;
  7446. }
  7447. class_ = sd->status.class_;
  7448. }
  7449. script_pushint(st,pc_jobid2mapid(class_));
  7450. return 0;
  7451. }
  7452. BUILDIN_FUNC(roclass)
  7453. {
  7454. int class_ =script_getnum(st,2);
  7455. int sex;
  7456. if( script_hasdata(st,3) )
  7457. sex = script_getnum(st,3);
  7458. else {
  7459. TBL_PC *sd;
  7460. if (st->rid && (sd=script_rid2sd(st)))
  7461. sex = sd->status.sex;
  7462. else
  7463. sex = 1; //Just use male when not found.
  7464. }
  7465. script_pushint(st,pc_mapid2jobid(class_, sex));
  7466. return 0;
  7467. }
  7468. /*==========================================
  7469. *携帯卵孵化機使用
  7470. *------------------------------------------*/
  7471. BUILDIN_FUNC(birthpet)
  7472. {
  7473. TBL_PC *sd;
  7474. sd=script_rid2sd(st);
  7475. if( sd == NULL )
  7476. return 0;
  7477. clif_sendegg(sd);
  7478. return 0;
  7479. }
  7480. /*==========================================
  7481. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7482. *------------------------------------------*/
  7483. BUILDIN_FUNC(resetlvl)
  7484. {
  7485. TBL_PC *sd;
  7486. int type=script_getnum(st,2);
  7487. sd=script_rid2sd(st);
  7488. if( sd == NULL )
  7489. return 0;
  7490. pc_resetlvl(sd,type);
  7491. return 0;
  7492. }
  7493. /*==========================================
  7494. * ステータスリセット
  7495. *------------------------------------------*/
  7496. BUILDIN_FUNC(resetstatus)
  7497. {
  7498. TBL_PC *sd;
  7499. sd=script_rid2sd(st);
  7500. pc_resetstate(sd);
  7501. return 0;
  7502. }
  7503. /*==========================================
  7504. * script command resetskill
  7505. *------------------------------------------*/
  7506. BUILDIN_FUNC(resetskill)
  7507. {
  7508. TBL_PC *sd;
  7509. sd=script_rid2sd(st);
  7510. pc_resetskill(sd,1);
  7511. return 0;
  7512. }
  7513. /*==========================================
  7514. * Counts total amount of skill points.
  7515. *------------------------------------------*/
  7516. BUILDIN_FUNC(skillpointcount)
  7517. {
  7518. TBL_PC *sd;
  7519. sd=script_rid2sd(st);
  7520. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7521. return 0;
  7522. }
  7523. /*==========================================
  7524. *
  7525. *------------------------------------------*/
  7526. BUILDIN_FUNC(changebase)
  7527. {
  7528. TBL_PC *sd=NULL;
  7529. int vclass;
  7530. if( script_hasdata(st,3) )
  7531. sd=map_id2sd(script_getnum(st,3));
  7532. else
  7533. sd=script_rid2sd(st);
  7534. if(sd == NULL)
  7535. return 0;
  7536. vclass = script_getnum(st,2);
  7537. if(vclass == JOB_WEDDING)
  7538. {
  7539. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7540. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7541. )
  7542. return 0;
  7543. }
  7544. if(!sd->disguise && vclass != sd->vd.class_) {
  7545. status_set_viewdata(&sd->bl, vclass);
  7546. //Updated client view. Base, Weapon and Cloth Colors.
  7547. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7548. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7549. if (sd->vd.cloth_color)
  7550. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7551. }
  7552. return 0;
  7553. }
  7554. /*==========================================
  7555. * 性別変換
  7556. *------------------------------------------*/
  7557. BUILDIN_FUNC(changesex)
  7558. {
  7559. TBL_PC *sd = NULL;
  7560. sd = script_rid2sd(st);
  7561. if (sd->status.sex == 0) {
  7562. sd->status.sex = 1;
  7563. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7564. sd->status.class_ -= 1;
  7565. } else if (sd->status.sex == 1) {
  7566. sd->status.sex = 0;
  7567. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7568. sd->status.class_ += 1;
  7569. }
  7570. chrif_char_ask_name(-1, sd->status.name, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex
  7571. chrif_save(sd,0);
  7572. return 0;
  7573. }
  7574. /*==========================================
  7575. * Works like 'announce' but outputs in the common chat window
  7576. *------------------------------------------*/
  7577. BUILDIN_FUNC(globalmes)
  7578. {
  7579. struct block_list *bl = map_id2bl(st->oid);
  7580. struct npc_data *nd = (struct npc_data *)bl;
  7581. const char *name=NULL,*mes;
  7582. mes=script_getstr(st,2); // メッセージの取得
  7583. if(mes==NULL) return 0;
  7584. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7585. name=script_getstr(st,3);
  7586. } else {
  7587. name=nd->name;
  7588. }
  7589. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7590. return 0;
  7591. }
  7592. /////////////////////////////////////////////////////////////////////
  7593. // NPC waiting room (chat room)
  7594. //
  7595. /// Creates a waiting room (chat room) for this npc.
  7596. ///
  7597. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7598. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7599. /// waitingroom "<title>",<limit>,"<event>";
  7600. /// waitingroom "<title>",<limit>;
  7601. BUILDIN_FUNC(waitingroom)
  7602. {
  7603. struct npc_data* nd;
  7604. const char* title;
  7605. const char* ev = "";
  7606. int limit;
  7607. int trigger = 0;
  7608. int pub = 1;
  7609. title = script_getstr(st, 2);
  7610. limit = script_getnum(st, 3);
  7611. if( script_hasdata(st,5) )
  7612. {
  7613. struct script_data* last = script_getdata(st, 5);
  7614. get_val(st, last);
  7615. if( data_isstring(last) )
  7616. {// ,<trigger>,"<event>"
  7617. trigger = script_getnum(st, 4);
  7618. ev = script_getstr(st, 5);
  7619. }
  7620. else
  7621. {// ,"<event>",<trigger>
  7622. ev = script_getstr(st, 4);
  7623. trigger = script_getnum(st,5);
  7624. }
  7625. }
  7626. else if( script_hasdata(st,4) )
  7627. {// ,"<event>"
  7628. ev = script_getstr(st, 4);
  7629. trigger = limit;
  7630. }
  7631. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  7632. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7633. return 0;
  7634. }
  7635. /// Removes the waiting room of the current or target npc.
  7636. ///
  7637. /// delwaitingroom "<npc_name>";
  7638. /// delwaitingroom;
  7639. BUILDIN_FUNC(delwaitingroom)
  7640. {
  7641. struct npc_data* nd;
  7642. if( script_hasdata(st,2) )
  7643. nd = npc_name2id(script_getstr(st, 2));
  7644. else
  7645. nd = (struct npc_data *)map_id2bl(st->oid);
  7646. if( nd != NULL )
  7647. chat_deletenpcchat(nd);
  7648. return 0;
  7649. }
  7650. /// Kicks all the players from the waiting room of the current or target npc.
  7651. ///
  7652. /// kickwaitingroomall "<npc_name>";
  7653. /// kickwaitingroomall;
  7654. BUILDIN_FUNC(waitingroomkickall)
  7655. {
  7656. struct npc_data* nd;
  7657. struct chat_data* cd;
  7658. if( script_hasdata(st,2) )
  7659. nd = npc_name2id(script_getstr(st,2));
  7660. else
  7661. nd = (struct npc_data *)map_id2bl(st->oid);
  7662. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7663. chat_npckickall(cd);
  7664. return 0;
  7665. }
  7666. /// Enables the waiting room event of the current or target npc.
  7667. ///
  7668. /// enablewaitingroomevent "<npc_name>";
  7669. /// enablewaitingroomevent;
  7670. BUILDIN_FUNC(enablewaitingroomevent)
  7671. {
  7672. struct npc_data* nd;
  7673. struct chat_data* cd;
  7674. if( script_hasdata(st,2) )
  7675. nd = npc_name2id(script_getstr(st, 2));
  7676. else
  7677. nd = (struct npc_data *)map_id2bl(st->oid);
  7678. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7679. chat_enableevent(cd);
  7680. return 0;
  7681. }
  7682. /// Disables the waiting room event of the current or target npc.
  7683. ///
  7684. /// disablewaitingroomevent "<npc_name>";
  7685. /// disablewaitingroomevent;
  7686. BUILDIN_FUNC(disablewaitingroomevent)
  7687. {
  7688. struct npc_data *nd;
  7689. struct chat_data *cd;
  7690. if( script_hasdata(st,2) )
  7691. nd = npc_name2id(script_getstr(st, 2));
  7692. else
  7693. nd = (struct npc_data *)map_id2bl(st->oid);
  7694. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7695. chat_disableevent(cd);
  7696. return 0;
  7697. }
  7698. /// Returns info on the waiting room of the current or target npc.
  7699. /// Returns -1 if the type unknown
  7700. /// <type>=0 : current number of users
  7701. /// <type>=1 : maximum number of users allowed
  7702. /// <type>=2 : the number of users that trigger the event
  7703. /// <type>=3 : if the trigger is disabled
  7704. /// <type>=4 : the title of the waiting room
  7705. /// <type>=5 : the password of the waiting room
  7706. /// <type>=16 : the name of the waiting room event
  7707. /// <type>=32 : if the waiting room is full
  7708. /// <type>=33 : if there are enough users to trigger the event
  7709. ///
  7710. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7711. /// getwaitingroomstate(<type>) -> <info>
  7712. BUILDIN_FUNC(getwaitingroomstate)
  7713. {
  7714. struct npc_data *nd;
  7715. struct chat_data *cd;
  7716. int type;
  7717. type = script_getnum(st,2);
  7718. if( script_hasdata(st,3) )
  7719. nd = npc_name2id(script_getstr(st, 3));
  7720. else
  7721. nd = (struct npc_data *)map_id2bl(st->oid);
  7722. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7723. {
  7724. script_pushint(st, -1);
  7725. return 0;
  7726. }
  7727. switch(type)
  7728. {
  7729. case 0: script_pushint(st, cd->users); break;
  7730. case 1: script_pushint(st, cd->limit); break;
  7731. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7732. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7733. case 4: script_pushstrcopy(st, cd->title); break;
  7734. case 5: script_pushstrcopy(st, cd->pass); break;
  7735. case 16: script_pushstrcopy(st, cd->npc_event);break;
  7736. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7737. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7738. default: script_pushint(st, -1); break;
  7739. }
  7740. return 0;
  7741. }
  7742. /// Warps the trigger or target amount of players to the target map and position.
  7743. /// Players are automatically removed from the waiting room.
  7744. /// Those waiting the longest will get warped first.
  7745. /// The target map can be "Random" for a random position in the current map,
  7746. /// and "SavePoint" for the savepoint map+position.
  7747. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7748. ///
  7749. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7750. /// The total number of teleported players is put into $@warpwaitingpcnum
  7751. ///
  7752. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7753. /// warpwaitingpc "<map name>",<x>,<y>;
  7754. BUILDIN_FUNC(warpwaitingpc)
  7755. {
  7756. int x;
  7757. int y;
  7758. int i;
  7759. int n;
  7760. const char* map_name;
  7761. struct npc_data* nd;
  7762. struct chat_data* cd;
  7763. TBL_PC* sd;
  7764. nd = (struct npc_data *)map_id2bl(st->oid);
  7765. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7766. return 0;
  7767. map_name = script_getstr(st,2);
  7768. x = script_getnum(st,3);
  7769. y = script_getnum(st,4);
  7770. n = cd->trigger&0x7f;
  7771. if( script_hasdata(st,5) )
  7772. n = script_getnum(st,5);
  7773. for( i = 0; i < n && cd->users > 0; i++ )
  7774. {
  7775. sd = cd->usersd[0];
  7776. if( sd == NULL )
  7777. {
  7778. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7779. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  7780. continue;// Broken npc chat room?
  7781. }
  7782. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  7783. if( strcmp(map_name,"Random") == 0 )
  7784. pc_randomwarp(sd,3);
  7785. else if( strcmp(map_name,"SavePoint") == 0 )
  7786. {
  7787. if( map[sd->bl.m].flag.noteleport )
  7788. return 0;// can't teleport on this map
  7789. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  7790. }
  7791. else
  7792. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  7793. }
  7794. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  7795. return 0;
  7796. }
  7797. /////////////////////////////////////////////////////////////////////
  7798. // ...
  7799. //
  7800. /*==========================================
  7801. * RIDのアタッチ
  7802. *------------------------------------------*/
  7803. BUILDIN_FUNC(attachrid)
  7804. {
  7805. st->rid=script_getnum(st,2);
  7806. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7807. return 0;
  7808. }
  7809. /*==========================================
  7810. * RIDのデタッチ
  7811. *------------------------------------------*/
  7812. BUILDIN_FUNC(detachrid)
  7813. {
  7814. st->rid=0;
  7815. return 0;
  7816. }
  7817. /*==========================================
  7818. * 存在チェック
  7819. *------------------------------------------*/
  7820. BUILDIN_FUNC(isloggedin)
  7821. {
  7822. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  7823. if (script_hasdata(st,3) && sd &&
  7824. sd->status.char_id != script_getnum(st,3))
  7825. sd = NULL;
  7826. push_val(st->stack,C_INT,sd!=NULL);
  7827. return 0;
  7828. }
  7829. /*==========================================
  7830. *
  7831. *------------------------------------------*/
  7832. BUILDIN_FUNC(setmapflagnosave)
  7833. {
  7834. int m,x,y;
  7835. unsigned short mapindex;
  7836. const char *str,*str2;
  7837. str=script_getstr(st,2);
  7838. str2=script_getstr(st,3);
  7839. x=script_getnum(st,4);
  7840. y=script_getnum(st,5);
  7841. m = map_mapname2mapid(str);
  7842. mapindex = mapindex_name2id(str2);
  7843. if(m >= 0 && mapindex) {
  7844. map[m].flag.nosave=1;
  7845. map[m].save.map=mapindex;
  7846. map[m].save.x=x;
  7847. map[m].save.y=y;
  7848. }
  7849. return 0;
  7850. }
  7851. BUILDIN_FUNC(getmapflag)
  7852. {
  7853. int m,i;
  7854. const char *str;
  7855. str=script_getstr(st,2);
  7856. i=script_getnum(st,3);
  7857. m = map_mapname2mapid(str);
  7858. if(m >= 0) {
  7859. switch(i) {
  7860. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  7861. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  7862. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  7863. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  7864. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  7865. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  7866. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  7867. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  7868. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  7869. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  7870. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  7871. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  7872. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  7873. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  7874. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  7875. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  7876. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  7877. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  7878. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  7879. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  7880. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  7881. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  7882. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  7883. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  7884. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  7885. case MF_INDOORS: script_pushint(st,map[m].flag.indoors); break;
  7886. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  7887. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  7888. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  7889. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  7890. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  7891. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  7892. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  7893. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  7894. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  7895. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  7896. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  7897. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  7898. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  7899. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  7900. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  7901. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  7902. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  7903. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  7904. }
  7905. }
  7906. return 0;
  7907. }
  7908. BUILDIN_FUNC(setmapflag)
  7909. {
  7910. int m,i;
  7911. const char *str;
  7912. const char *val=NULL;
  7913. str=script_getstr(st,2);
  7914. i=script_getnum(st,3);
  7915. if(script_hasdata(st,4)){
  7916. val=script_getstr(st,4);
  7917. }
  7918. m = map_mapname2mapid(str);
  7919. if(m >= 0) {
  7920. switch(i) {
  7921. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  7922. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  7923. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  7924. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  7925. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  7926. case MF_PVP: map[m].flag.pvp=1; break;
  7927. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  7928. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  7929. case MF_GVG: map[m].flag.gvg=1; break;
  7930. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  7931. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  7932. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  7933. case MF_NOTRADE: map[m].flag.notrade=1; break;
  7934. case MF_NODROP: map[m].flag.nodrop=1; break;
  7935. case MF_NOSKILL: map[m].flag.noskill=1; break;
  7936. case MF_NOWARP: map[m].flag.nowarp=1; break;
  7937. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  7938. case MF_SNOW: map[m].flag.snow=1; break;
  7939. case MF_CLOUDS: map[m].flag.clouds=1; break;
  7940. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  7941. case MF_FOG: map[m].flag.fog=1; break;
  7942. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  7943. case MF_SAKURA: map[m].flag.sakura=1; break;
  7944. case MF_LEAVES: map[m].flag.leaves=1; break;
  7945. case MF_RAIN: map[m].flag.rain=1; break;
  7946. case MF_INDOORS: map[m].flag.indoors=1; break;
  7947. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  7948. case MF_NOGO: map[m].flag.nogo=1; break;
  7949. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  7950. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  7951. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  7952. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  7953. case MF_NORETURN: map[m].flag.noreturn=1; break;
  7954. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  7955. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  7956. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  7957. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  7958. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7959. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7960. case MF_NOVENDING: map[m].flag.novending=1; break;
  7961. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  7962. case MF_NOCHAT: map[m].flag.nochat=1; break;
  7963. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  7964. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  7965. }
  7966. }
  7967. return 0;
  7968. }
  7969. BUILDIN_FUNC(removemapflag)
  7970. {
  7971. int m,i;
  7972. const char *str;
  7973. str=script_getstr(st,2);
  7974. i=script_getnum(st,3);
  7975. m = map_mapname2mapid(str);
  7976. if(m >= 0) {
  7977. switch(i) {
  7978. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  7979. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  7980. case MF_NOSAVE: map[m].flag.nosave=0; break;
  7981. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  7982. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  7983. case MF_PVP: map[m].flag.pvp=0; break;
  7984. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  7985. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  7986. case MF_GVG: map[m].flag.gvg=0; break;
  7987. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  7988. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  7989. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  7990. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  7991. case MF_NOTRADE: map[m].flag.notrade=0; break;
  7992. case MF_NODROP: map[m].flag.nodrop=0; break;
  7993. case MF_NOSKILL: map[m].flag.noskill=0; break;
  7994. case MF_NOWARP: map[m].flag.nowarp=0; break;
  7995. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  7996. case MF_SNOW: map[m].flag.snow=0; break;
  7997. case MF_CLOUDS: map[m].flag.clouds=0; break;
  7998. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  7999. case MF_FOG: map[m].flag.fog=0; break;
  8000. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8001. case MF_SAKURA: map[m].flag.sakura=0; break;
  8002. case MF_LEAVES: map[m].flag.leaves=0; break;
  8003. case MF_RAIN: map[m].flag.rain=0; break;
  8004. case MF_INDOORS: map[m].flag.indoors=0; break;
  8005. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8006. case MF_NOGO: map[m].flag.nogo=0; break;
  8007. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8008. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8009. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8010. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8011. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8012. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8013. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8014. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8015. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8016. case MF_JEXP: map[m].jexp=100; break;
  8017. case MF_BEXP: map[m].bexp=100; break;
  8018. case MF_NOVENDING: map[m].flag.novending=0; break;
  8019. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8020. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8021. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8022. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8023. }
  8024. }
  8025. return 0;
  8026. }
  8027. BUILDIN_FUNC(pvpon)
  8028. {
  8029. int m;
  8030. const char *str;
  8031. TBL_PC* sd = NULL;
  8032. struct s_mapiterator* iter;
  8033. str = script_getstr(st,2);
  8034. m = map_mapname2mapid(str);
  8035. if( m < 0 || map[m].flag.pvp )
  8036. return 0; // nothing to do
  8037. map[m].flag.pvp = 1;
  8038. clif_send0199(m,1);
  8039. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8040. return 0;
  8041. iter = mapit_getallusers();
  8042. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8043. {
  8044. if( sd->bl.m != m || sd->pvp_timer != -1 )
  8045. continue; // not applicable
  8046. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8047. sd->pvp_rank = 0;
  8048. sd->pvp_lastusers = 0;
  8049. sd->pvp_point = 5;
  8050. sd->pvp_won = 0;
  8051. sd->pvp_lost = 0;
  8052. }
  8053. mapit_free(iter);
  8054. return 0;
  8055. }
  8056. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8057. {
  8058. TBL_PC* sd = (TBL_PC*)bl;
  8059. clif_pvpset(sd, 0, 0, 2);
  8060. if (sd->pvp_timer != -1) {
  8061. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8062. sd->pvp_timer = -1;
  8063. }
  8064. return 0;
  8065. }
  8066. BUILDIN_FUNC(pvpoff)
  8067. {
  8068. int m;
  8069. const char *str;
  8070. str=script_getstr(st,2);
  8071. m = map_mapname2mapid(str);
  8072. if(m < 0 || !map[m].flag.pvp)
  8073. return 0; //fixed Lupus
  8074. map[m].flag.pvp = 0;
  8075. clif_send0199(m,0);
  8076. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8077. return 0;
  8078. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8079. return 0;
  8080. }
  8081. BUILDIN_FUNC(gvgon)
  8082. {
  8083. int m;
  8084. const char *str;
  8085. str=script_getstr(st,2);
  8086. m = map_mapname2mapid(str);
  8087. if(m >= 0 && !map[m].flag.gvg) {
  8088. map[m].flag.gvg = 1;
  8089. clif_send0199(m,3);
  8090. }
  8091. return 0;
  8092. }
  8093. BUILDIN_FUNC(gvgoff)
  8094. {
  8095. int m;
  8096. const char *str;
  8097. str=script_getstr(st,2);
  8098. m = map_mapname2mapid(str);
  8099. if(m >= 0 && map[m].flag.gvg) {
  8100. map[m].flag.gvg = 0;
  8101. clif_send0199(m,0);
  8102. }
  8103. return 0;
  8104. }
  8105. /*==========================================
  8106. * Shows an emoticon on top of the player/npc
  8107. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8108. *------------------------------------------*/
  8109. //Optional second parameter added by [Skotlex]
  8110. BUILDIN_FUNC(emotion)
  8111. {
  8112. int type;
  8113. int player=0;
  8114. type=script_getnum(st,2);
  8115. if(type < 0 || type > 100)
  8116. return 0;
  8117. if( script_hasdata(st,3) )
  8118. player=script_getnum(st,3);
  8119. if (player) {
  8120. TBL_PC *sd = script_rid2sd(st);
  8121. if (sd)
  8122. clif_emotion(&sd->bl,type);
  8123. } else
  8124. clif_emotion(map_id2bl(st->oid),type);
  8125. return 0;
  8126. }
  8127. static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
  8128. {
  8129. int m=va_arg(ap,int);
  8130. int g_id=va_arg(ap,int);
  8131. int flag=va_arg(ap,int);
  8132. TBL_PC *sd = (TBL_PC*)data;
  8133. if(!sd || sd->bl.m != m)
  8134. return 0;
  8135. if(
  8136. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8137. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8138. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8139. )
  8140. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8141. return 1;
  8142. }
  8143. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8144. {
  8145. struct mob_data *md=(struct mob_data *)bl;
  8146. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8147. status_kill(bl);
  8148. return 0;
  8149. }
  8150. BUILDIN_FUNC(maprespawnguildid)
  8151. {
  8152. const char *mapname=script_getstr(st,2);
  8153. int g_id=script_getnum(st,3);
  8154. int flag=script_getnum(st,4);
  8155. int m=map_mapname2mapid(mapname);
  8156. if(m == -1)
  8157. return 0;
  8158. //Catch ALL players (in case some are 'between maps' on execution time)
  8159. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8160. if (flag&4) //Remove script mobs.
  8161. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8162. return 0;
  8163. }
  8164. BUILDIN_FUNC(agitstart)
  8165. {
  8166. if(agit_flag==1) return 0; // Agit already Start.
  8167. agit_flag=1;
  8168. guild_agit_start();
  8169. return 0;
  8170. }
  8171. BUILDIN_FUNC(agitend)
  8172. {
  8173. if(agit_flag==0) return 0; // Agit already End.
  8174. agit_flag=0;
  8175. guild_agit_end();
  8176. return 0;
  8177. }
  8178. /*==========================================
  8179. * Returns whether woe is on or off. // choice script
  8180. *------------------------------------------*/
  8181. BUILDIN_FUNC(agitcheck)
  8182. {
  8183. script_pushint(st,agit_flag);
  8184. return 0;
  8185. }
  8186. /// Sets the guild_id of this npc.
  8187. ///
  8188. /// flagemblem <guild_id>;
  8189. BUILDIN_FUNC(flagemblem)
  8190. {
  8191. TBL_NPC* nd;
  8192. int g_id=script_getnum(st,2);
  8193. if(g_id < 0) return 0;
  8194. nd = (TBL_NPC*)map_id2nd(st->oid);
  8195. if( nd == NULL )
  8196. {
  8197. ShowError("script:flagemblem: npc %d not found\n", st->oid);
  8198. }
  8199. else if( nd->subtype != SCRIPT )
  8200. {
  8201. ShowError("script:flagemblem: unexpected subtype %d for npc %d '%s'\n", nd->subtype, st->oid, nd->exname);
  8202. }
  8203. else
  8204. {
  8205. nd->u.scr.guild_id = g_id;
  8206. clif_guild_emblem_area(&nd->bl);
  8207. }
  8208. return 0;
  8209. }
  8210. BUILDIN_FUNC(getcastlename)
  8211. {
  8212. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8213. struct guild_castle* gc = guild_mapname2gc(mapname);
  8214. const char* name = (gc) ? gc->castle_name : "";
  8215. script_pushstrcopy(st,name);
  8216. return 0;
  8217. }
  8218. BUILDIN_FUNC(getcastledata)
  8219. {
  8220. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8221. int index = script_getnum(st,3);
  8222. struct guild_castle* gc = guild_mapname2gc(mapname);
  8223. if(script_hasdata(st,4) && index==0 && gc) {
  8224. const char* event = script_getstr(st,4);
  8225. check_event(st, event);
  8226. guild_addcastleinfoevent(gc->castle_id,17,event);
  8227. }
  8228. if(gc){
  8229. switch(index){
  8230. case 0: {
  8231. int i;
  8232. for(i=1;i<18;i++) // Initialize[AgitInit]
  8233. guild_castledataload(gc->castle_id,i);
  8234. } break;
  8235. case 1:
  8236. script_pushint(st,gc->guild_id); break;
  8237. case 2:
  8238. script_pushint(st,gc->economy); break;
  8239. case 3:
  8240. script_pushint(st,gc->defense); break;
  8241. case 4:
  8242. script_pushint(st,gc->triggerE); break;
  8243. case 5:
  8244. script_pushint(st,gc->triggerD); break;
  8245. case 6:
  8246. script_pushint(st,gc->nextTime); break;
  8247. case 7:
  8248. script_pushint(st,gc->payTime); break;
  8249. case 8:
  8250. script_pushint(st,gc->createTime); break;
  8251. case 9:
  8252. script_pushint(st,gc->visibleC); break;
  8253. case 10:
  8254. case 11:
  8255. case 12:
  8256. case 13:
  8257. case 14:
  8258. case 15:
  8259. case 16:
  8260. case 17:
  8261. script_pushint(st,gc->guardian[index-10].visible); break;
  8262. default:
  8263. script_pushint(st,0); break;
  8264. }
  8265. return 0;
  8266. }
  8267. script_pushint(st,0);
  8268. return 0;
  8269. }
  8270. BUILDIN_FUNC(setcastledata)
  8271. {
  8272. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8273. int index = script_getnum(st,3);
  8274. int value = script_getnum(st,4);
  8275. struct guild_castle* gc = guild_mapname2gc(mapname);
  8276. if(gc) {
  8277. // Save Data byself First
  8278. switch(index){
  8279. case 1:
  8280. gc->guild_id = value; break;
  8281. case 2:
  8282. gc->economy = value; break;
  8283. case 3:
  8284. gc->defense = value; break;
  8285. case 4:
  8286. gc->triggerE = value; break;
  8287. case 5:
  8288. gc->triggerD = value; break;
  8289. case 6:
  8290. gc->nextTime = value; break;
  8291. case 7:
  8292. gc->payTime = value; break;
  8293. case 8:
  8294. gc->createTime = value; break;
  8295. case 9:
  8296. gc->visibleC = value; break;
  8297. case 10:
  8298. case 11:
  8299. case 12:
  8300. case 13:
  8301. case 14:
  8302. case 15:
  8303. case 16:
  8304. case 17:
  8305. gc->guardian[index-10].visible = value; break;
  8306. default:
  8307. return 0;
  8308. }
  8309. guild_castledatasave(gc->castle_id,index,value);
  8310. }
  8311. return 0;
  8312. }
  8313. /* =====================================================================
  8314. * ギルド情報を要求する
  8315. * ---------------------------------------------------------------------*/
  8316. BUILDIN_FUNC(requestguildinfo)
  8317. {
  8318. int guild_id=script_getnum(st,2);
  8319. const char *event=NULL;
  8320. if( script_hasdata(st,3) ){
  8321. event=script_getstr(st,3);
  8322. check_event(st, event);
  8323. }
  8324. if(guild_id>0)
  8325. guild_npc_request_info(guild_id,event);
  8326. return 0;
  8327. }
  8328. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8329. /// getequipcardcnt(<equipment slot>);
  8330. BUILDIN_FUNC(getequipcardcnt)
  8331. {
  8332. int i=-1,j,num;
  8333. TBL_PC *sd;
  8334. int count;
  8335. num=script_getnum(st,2);
  8336. sd=script_rid2sd(st);
  8337. if (num > 0 && num <= ARRAYLENGTH(equip))
  8338. i=pc_checkequip(sd,equip[num-1]);
  8339. if (i < 0) {
  8340. script_pushint(st,0);
  8341. return 0;
  8342. }
  8343. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8344. {
  8345. script_pushint(st,0);
  8346. return 0;
  8347. }
  8348. count = 0;
  8349. for( j = 0; j < MAX_SLOTS; j++ )
  8350. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8351. count++;
  8352. script_pushint(st,count);
  8353. return 0;
  8354. }
  8355. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8356. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8357. /// successremovecards <slot>;
  8358. BUILDIN_FUNC(successremovecards)
  8359. {
  8360. int i=-1,j,c,cardflag=0;
  8361. TBL_PC* sd = script_rid2sd(st);
  8362. int num = script_getnum(st,2);
  8363. if (num > 0 && num <= ARRAYLENGTH(equip))
  8364. i=pc_checkequip(sd,equip[num-1]);
  8365. if (i < 0) {
  8366. return 0;
  8367. }
  8368. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8369. return 0;
  8370. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8371. {
  8372. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8373. {// extract this card from the item
  8374. int flag;
  8375. struct item item_tmp;
  8376. cardflag = 1;
  8377. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8378. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8379. item_tmp.attribute=0;
  8380. for (j = 0; j < MAX_SLOTS; j++)
  8381. item_tmp.card[j]=0;
  8382. //Logs items, got from (N)PC scripts [Lupus]
  8383. if(log_config.enable_logs&0x40)
  8384. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8385. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8386. clif_additem(sd,0,0,flag);
  8387. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8388. }
  8389. }
  8390. }
  8391. if(cardflag == 1)
  8392. { // カードを取り除いたアイテム所得
  8393. int flag;
  8394. struct item item_tmp;
  8395. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8396. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8397. item_tmp.attribute=sd->status.inventory[i].attribute;
  8398. for (j = 0; j < MAX_SLOTS; j++)
  8399. item_tmp.card[j]=0;
  8400. //Logs items, got from (N)PC scripts [Lupus]
  8401. if(log_config.enable_logs&0x40)
  8402. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8403. pc_delitem(sd,i,1,0);
  8404. //Logs items, got from (N)PC scripts [Lupus]
  8405. if(log_config.enable_logs&0x40)
  8406. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8407. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8408. clif_additem(sd,0,0,flag);
  8409. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8410. }
  8411. clif_misceffect(&sd->bl,3);
  8412. }
  8413. return 0;
  8414. }
  8415. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8416. /// failedremovecards <slot>, <type>;
  8417. /// <type>=0 : will destroy both the item and the cards.
  8418. /// <type>=1 : will keep the item, but destroy the cards.
  8419. /// <type>=2 : will keep the cards, but destroy the item.
  8420. /// <type>=? : will just display the failure effect.
  8421. BUILDIN_FUNC(failedremovecards)
  8422. {
  8423. int i=-1,j,c,cardflag=0;
  8424. TBL_PC* sd = script_rid2sd(st);
  8425. int num = script_getnum(st,2);
  8426. int typefail = script_getnum(st,3);
  8427. if (num > 0 && num <= ARRAYLENGTH(equip))
  8428. i=pc_checkequip(sd,equip[num-1]);
  8429. if (i < 0)
  8430. return 0;
  8431. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8432. return 0;
  8433. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8434. {
  8435. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8436. {
  8437. cardflag = 1;
  8438. if(typefail == 2)
  8439. {// add cards to inventory, clear
  8440. int flag;
  8441. struct item item_tmp;
  8442. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8443. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8444. item_tmp.attribute=0;
  8445. for (j = 0; j < MAX_SLOTS; j++)
  8446. item_tmp.card[j]=0;
  8447. //Logs items, got from (N)PC scripts [Lupus]
  8448. if(log_config.enable_logs&0x40)
  8449. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8450. if((flag=pc_additem(sd,&item_tmp,1))){
  8451. clif_additem(sd,0,0,flag);
  8452. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8453. }
  8454. }
  8455. }
  8456. }
  8457. if(cardflag == 1)
  8458. {
  8459. if(typefail == 0 || typefail == 2){ // 武具損失
  8460. //Logs items, got from (N)PC scripts [Lupus]
  8461. if(log_config.enable_logs&0x40)
  8462. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8463. pc_delitem(sd,i,1,0);
  8464. }
  8465. if(typefail == 1){ // カードのみ損失(武具を返す)
  8466. int flag;
  8467. struct item item_tmp;
  8468. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8469. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8470. item_tmp.attribute=sd->status.inventory[i].attribute;
  8471. //Logs items, got from (N)PC scripts [Lupus]
  8472. if(log_config.enable_logs&0x40)
  8473. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8474. for (j = 0; j < MAX_SLOTS; j++)
  8475. item_tmp.card[j]=0;
  8476. pc_delitem(sd,i,1,0);
  8477. //Logs items, got from (N)PC scripts [Lupus]
  8478. if(log_config.enable_logs&0x40)
  8479. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8480. if((flag=pc_additem(sd,&item_tmp,1))){
  8481. clif_additem(sd,0,0,flag);
  8482. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8483. }
  8484. }
  8485. clif_misceffect(&sd->bl,2);
  8486. }
  8487. return 0;
  8488. }
  8489. /* ================================================================
  8490. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8491. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8492. * improved by [Lance]
  8493. * ================================================================*/
  8494. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8495. {
  8496. int x,y,m,check_val=0,check_ID=0,i=0;
  8497. struct guild *g = NULL;
  8498. struct party_data *p = NULL;
  8499. const char *str;
  8500. const char *mapname;
  8501. unsigned int index;
  8502. mapname=script_getstr(st,2);
  8503. str=script_getstr(st,3);
  8504. x=script_getnum(st,4);
  8505. y=script_getnum(st,5);
  8506. if(script_hasdata(st,7)){
  8507. check_val=script_getnum(st,6);
  8508. check_ID=script_getnum(st,7);
  8509. }
  8510. if((m=map_mapname2mapid(mapname))< 0)
  8511. return 0;
  8512. if(!(index=mapindex_name2id(str)))
  8513. return 0;
  8514. switch(check_val){
  8515. case 1:
  8516. g = guild_search(check_ID);
  8517. if (g){
  8518. for( i=0; i < g->max_member; i++)
  8519. {
  8520. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8521. pc_setpos(g->member[i].sd,index,x,y,3);
  8522. }
  8523. }
  8524. }
  8525. break;
  8526. case 2:
  8527. p = party_search(check_ID);
  8528. if(p){
  8529. for(i=0;i<MAX_PARTY; i++){
  8530. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8531. pc_setpos(p->data[i].sd,index,x,y,3);
  8532. }
  8533. }
  8534. }
  8535. break;
  8536. default:
  8537. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8538. break;
  8539. }
  8540. return 0;
  8541. }
  8542. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8543. {
  8544. char *event=va_arg(ap,char *);
  8545. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8546. return 1;
  8547. return 0;
  8548. }
  8549. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8550. {
  8551. const char *mapname,*event;
  8552. int m;
  8553. mapname=script_getstr(st,2);
  8554. event=script_getstr(st,3);
  8555. check_event(st, event);
  8556. if( (m=map_mapname2mapid(mapname))<0 ) {
  8557. script_pushint(st,-1);
  8558. return 0;
  8559. }
  8560. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8561. return 0;
  8562. }
  8563. BUILDIN_FUNC(marriage)
  8564. {
  8565. const char *partner=script_getstr(st,2);
  8566. TBL_PC *sd=script_rid2sd(st);
  8567. TBL_PC *p_sd=map_nick2sd(partner);
  8568. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8569. script_pushint(st,0);
  8570. return 0;
  8571. }
  8572. script_pushint(st,1);
  8573. return 0;
  8574. }
  8575. BUILDIN_FUNC(wedding_effect)
  8576. {
  8577. TBL_PC *sd=script_rid2sd(st);
  8578. struct block_list *bl;
  8579. if(sd==NULL) {
  8580. bl=map_id2bl(st->oid);
  8581. } else
  8582. bl=&sd->bl;
  8583. clif_wedding_effect(bl);
  8584. return 0;
  8585. }
  8586. BUILDIN_FUNC(divorce)
  8587. {
  8588. TBL_PC *sd=script_rid2sd(st);
  8589. if(sd==NULL || pc_divorce(sd) < 0){
  8590. script_pushint(st,0);
  8591. return 0;
  8592. }
  8593. script_pushint(st,1);
  8594. return 0;
  8595. }
  8596. BUILDIN_FUNC(ispartneron)
  8597. {
  8598. TBL_PC *sd=script_rid2sd(st);
  8599. TBL_PC *p_sd=NULL;
  8600. if(sd==NULL || !pc_ismarried(sd) ||
  8601. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8602. script_pushint(st,0);
  8603. return 0;
  8604. }
  8605. script_pushint(st,1);
  8606. return 0;
  8607. }
  8608. BUILDIN_FUNC(getpartnerid)
  8609. {
  8610. TBL_PC *sd=script_rid2sd(st);
  8611. if (sd == NULL) {
  8612. script_pushint(st,0);
  8613. return 0;
  8614. }
  8615. script_pushint(st,sd->status.partner_id);
  8616. return 0;
  8617. }
  8618. BUILDIN_FUNC(getchildid)
  8619. {
  8620. TBL_PC *sd=script_rid2sd(st);
  8621. if (sd == NULL) {
  8622. script_pushint(st,0);
  8623. return 0;
  8624. }
  8625. script_pushint(st,sd->status.child);
  8626. return 0;
  8627. }
  8628. BUILDIN_FUNC(getmotherid)
  8629. {
  8630. TBL_PC *sd=script_rid2sd(st);
  8631. if (sd == NULL) {
  8632. script_pushint(st,0);
  8633. return 0;
  8634. }
  8635. script_pushint(st,sd->status.mother);
  8636. return 0;
  8637. }
  8638. BUILDIN_FUNC(getfatherid)
  8639. {
  8640. TBL_PC *sd=script_rid2sd(st);
  8641. if (sd == NULL) {
  8642. script_pushint(st,0);
  8643. return 0;
  8644. }
  8645. script_pushint(st,sd->status.father);
  8646. return 0;
  8647. }
  8648. BUILDIN_FUNC(warppartner)
  8649. {
  8650. int x,y;
  8651. unsigned short mapindex;
  8652. const char *str;
  8653. TBL_PC *sd=script_rid2sd(st);
  8654. TBL_PC *p_sd=NULL;
  8655. if(sd==NULL || !pc_ismarried(sd) ||
  8656. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8657. script_pushint(st,0);
  8658. return 0;
  8659. }
  8660. str=script_getstr(st,2);
  8661. x=script_getnum(st,3);
  8662. y=script_getnum(st,4);
  8663. mapindex = mapindex_name2id(str);
  8664. if (mapindex) {
  8665. pc_setpos(p_sd,mapindex,x,y,0);
  8666. script_pushint(st,1);
  8667. } else
  8668. script_pushint(st,0);
  8669. return 0;
  8670. }
  8671. /*================================================
  8672. * Script for Displaying MOB Information [Valaris]
  8673. *------------------------------------------------*/
  8674. BUILDIN_FUNC(strmobinfo)
  8675. {
  8676. int num=script_getnum(st,2);
  8677. int class_=script_getnum(st,3);
  8678. if(!mobdb_checkid(class_))
  8679. {
  8680. script_pushint(st,0);
  8681. return 0;
  8682. }
  8683. switch (num) {
  8684. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  8685. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  8686. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8687. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8688. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8689. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8690. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8691. default:
  8692. script_pushint(st,0);
  8693. break;
  8694. }
  8695. return 0;
  8696. }
  8697. /*==========================================
  8698. * Summon guardians [Valaris]
  8699. * guardian("<map name>",<x>,<y>,"<name to show>",<mob id>{,"<event label>"}{,<guardian index>}) -> <id>
  8700. *------------------------------------------*/
  8701. BUILDIN_FUNC(guardian)
  8702. {
  8703. int class_=0,x=0,y=0,guardian=0;
  8704. const char *str,*map,*evt="";
  8705. struct script_data *data;
  8706. bool has_index = false;
  8707. map =script_getstr(st,2);
  8708. x =script_getnum(st,3);
  8709. y =script_getnum(st,4);
  8710. str =script_getstr(st,5);
  8711. class_=script_getnum(st,6);
  8712. if( script_hasdata(st,8) )
  8713. {// "<event label>",<guardian index>
  8714. evt=script_getstr(st,7);
  8715. guardian=script_getnum(st,8);
  8716. has_index = true;
  8717. } else if( script_hasdata(st,7) ){
  8718. data=script_getdata(st,7);
  8719. get_val(st,data);
  8720. if( data_isstring(data) )
  8721. {// "<event label>"
  8722. evt=script_getstr(st,7);
  8723. } else if( data_isint(data) )
  8724. {// <guardian index>
  8725. guardian=script_getnum(st,7);
  8726. has_index = true;
  8727. } else {
  8728. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8729. script_reportdata(data);
  8730. return 1;
  8731. }
  8732. }
  8733. check_event(st, evt);
  8734. script_pushint(st, mob_spawn_guardian(map,x,y,str,class_,evt,guardian,has_index));
  8735. return 0;
  8736. }
  8737. /// Retrieves various information about the specified guardian.
  8738. ///
  8739. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  8740. /// type: 0 - whether it is deployed or not
  8741. /// 1 - maximum hp
  8742. /// 2 - current hp
  8743. ///
  8744. BUILDIN_FUNC(guardianinfo)
  8745. {
  8746. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8747. int id = script_getnum(st,3);
  8748. int type = script_getnum(st,4);
  8749. struct guild_castle* gc = guild_mapname2gc(mapname);
  8750. struct mob_data* gd;
  8751. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  8752. {
  8753. script_pushint(st,-1);
  8754. return 0;
  8755. }
  8756. if( type == 0 )
  8757. script_pushint(st, gc->guardian[id].visible);
  8758. else
  8759. if( !gc->guardian[id].visible )
  8760. script_pushint(st,-1);
  8761. else
  8762. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  8763. script_pushint(st,-1);
  8764. else
  8765. {
  8766. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  8767. else if( type == 2 ) script_pushint(st,gd->status.hp);
  8768. else
  8769. script_pushint(st,-1);
  8770. }
  8771. return 0;
  8772. }
  8773. /*==========================================
  8774. * IDからItem名
  8775. *------------------------------------------*/
  8776. BUILDIN_FUNC(getitemname)
  8777. {
  8778. int item_id=0;
  8779. struct item_data *i_data;
  8780. char *item_name;
  8781. struct script_data *data;
  8782. data=script_getdata(st,2);
  8783. get_val(st,data);
  8784. if( data_isstring(data) ){
  8785. const char *name=conv_str(st,data);
  8786. struct item_data *item_data = itemdb_searchname(name);
  8787. if( item_data )
  8788. item_id=item_data->nameid;
  8789. }else
  8790. item_id=conv_num(st,data);
  8791. i_data = itemdb_exists(item_id);
  8792. if (i_data == NULL)
  8793. {
  8794. script_pushconststr(st,"null");
  8795. return 0;
  8796. }
  8797. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8798. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8799. script_pushstr(st,item_name);
  8800. return 0;
  8801. }
  8802. /*==========================================
  8803. * Returns number of slots an item has. [Skotlex]
  8804. *------------------------------------------*/
  8805. BUILDIN_FUNC(getitemslots)
  8806. {
  8807. int item_id;
  8808. struct item_data *i_data;
  8809. item_id=script_getnum(st,2);
  8810. i_data = itemdb_exists(item_id);
  8811. if (i_data)
  8812. script_pushint(st,i_data->slot);
  8813. else
  8814. script_pushint(st,-1);
  8815. return 0;
  8816. }
  8817. /*==========================================
  8818. * Returns some values of an item [Lupus]
  8819. * Price, Weight, etc...
  8820. getiteminfo(itemID,n), where n
  8821. 0 value_buy;
  8822. 1 value_sell;
  8823. 2 type;
  8824. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8825. if = 0, then monsters don't drop it at all (rare or a quest item)
  8826. if = 10000, then this item is sold in NPC shops only
  8827. 4 sex;
  8828. 5 equip;
  8829. 6 weight;
  8830. 7 atk;
  8831. 8 def;
  8832. 9 range;
  8833. 10 slot;
  8834. 11 look;
  8835. 12 elv;
  8836. 13 wlv;
  8837. 14 view id
  8838. *------------------------------------------*/
  8839. BUILDIN_FUNC(getiteminfo)
  8840. {
  8841. int item_id,n;
  8842. int *item_arr;
  8843. struct item_data *i_data;
  8844. item_id = script_getnum(st,2);
  8845. n = script_getnum(st,3);
  8846. i_data = itemdb_exists(item_id);
  8847. if (i_data && n>=0 && n<14) {
  8848. item_arr = (int*)&i_data->value_buy;
  8849. script_pushint(st,item_arr[n]);
  8850. } else
  8851. script_pushint(st,-1);
  8852. return 0;
  8853. }
  8854. /*==========================================
  8855. * Set some values of an item [Lupus]
  8856. * Price, Weight, etc...
  8857. setiteminfo(itemID,n,Value), where n
  8858. 0 value_buy;
  8859. 1 value_sell;
  8860. 2 type;
  8861. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8862. if = 0, then monsters don't drop it at all (rare or a quest item)
  8863. if = 10000, then this item is sold in NPC shops only
  8864. 4 sex;
  8865. 5 equip;
  8866. 6 weight;
  8867. 7 atk;
  8868. 8 def;
  8869. 9 range;
  8870. 10 slot;
  8871. 11 look;
  8872. 12 elv;
  8873. 13 wlv;
  8874. 14 view id
  8875. * Returns Value or -1 if the wrong field's been set
  8876. *------------------------------------------*/
  8877. BUILDIN_FUNC(setiteminfo)
  8878. {
  8879. int item_id,n,value;
  8880. int *item_arr;
  8881. struct item_data *i_data;
  8882. item_id = script_getnum(st,2);
  8883. n = script_getnum(st,3);
  8884. value = script_getnum(st,4);
  8885. i_data = itemdb_exists(item_id);
  8886. if (i_data && n>=0 && n<14) {
  8887. item_arr = (int*)&i_data->value_buy;
  8888. item_arr[n] = value;
  8889. script_pushint(st,value);
  8890. } else
  8891. script_pushint(st,-1);
  8892. return 0;
  8893. }
  8894. /*==========================================
  8895. * Returns value from equipped item slot n [Lupus]
  8896. getequipcardid(num,slot)
  8897. where
  8898. num = eqip position slot
  8899. slot = 0,1,2,3 (Card Slot N)
  8900. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  8901. it's useful when you want to check item cards or if it's signed
  8902. Useful for such quests as "Sign this refined item with players name" etc
  8903. Hat[0] +4 -> Player's Hat[0] +4
  8904. *------------------------------------------*/
  8905. BUILDIN_FUNC(getequipcardid)
  8906. {
  8907. int i=-1,num,slot;
  8908. TBL_PC *sd;
  8909. num=script_getnum(st,2);
  8910. slot=script_getnum(st,3);
  8911. sd=script_rid2sd(st);
  8912. if (num > 0 && num <= ARRAYLENGTH(equip))
  8913. i=pc_checkequip(sd,equip[num-1]);
  8914. if(i >= 0 && slot>=0 && slot<4)
  8915. script_pushint(st,sd->status.inventory[i].card[slot]);
  8916. else
  8917. script_pushint(st,0);
  8918. return 0;
  8919. }
  8920. /*==========================================
  8921. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  8922. *------------------------------------------*/
  8923. BUILDIN_FUNC(petskillbonus)
  8924. {
  8925. struct pet_data *pd;
  8926. TBL_PC *sd=script_rid2sd(st);
  8927. if(sd==NULL || sd->pd==NULL)
  8928. return 0;
  8929. pd=sd->pd;
  8930. if (pd->bonus)
  8931. { //Clear previous bonus
  8932. if (pd->bonus->timer != -1)
  8933. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  8934. } else //init
  8935. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  8936. pd->bonus->type=script_getnum(st,2);
  8937. pd->bonus->val=script_getnum(st,3);
  8938. pd->bonus->duration=script_getnum(st,4);
  8939. pd->bonus->delay=script_getnum(st,5);
  8940. if (pd->state.skillbonus == 1)
  8941. pd->state.skillbonus=0; // waiting state
  8942. // wait for timer to start
  8943. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  8944. pd->bonus->timer=-1;
  8945. else
  8946. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  8947. return 0;
  8948. }
  8949. /*==========================================
  8950. * pet looting [Valaris] //Rewritten by [Skotlex]
  8951. *------------------------------------------*/
  8952. BUILDIN_FUNC(petloot)
  8953. {
  8954. int max;
  8955. struct pet_data *pd;
  8956. TBL_PC *sd=script_rid2sd(st);
  8957. if(sd==NULL || sd->pd==NULL)
  8958. return 0;
  8959. max=script_getnum(st,2);
  8960. if(max < 1)
  8961. max = 1; //Let'em loot at least 1 item.
  8962. else if (max > MAX_PETLOOT_SIZE)
  8963. max = MAX_PETLOOT_SIZE;
  8964. pd = sd->pd;
  8965. if (pd->loot != NULL)
  8966. { //Release whatever was there already and reallocate memory
  8967. pet_lootitem_drop(pd, pd->msd);
  8968. aFree(pd->loot->item);
  8969. }
  8970. else
  8971. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  8972. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  8973. pd->loot->max=max;
  8974. pd->loot->count = 0;
  8975. pd->loot->weight = 0;
  8976. return 0;
  8977. }
  8978. /*==========================================
  8979. * PCの所持品情報読み取り
  8980. *------------------------------------------*/
  8981. BUILDIN_FUNC(getinventorylist)
  8982. {
  8983. TBL_PC *sd=script_rid2sd(st);
  8984. char card_var[NAME_LENGTH];
  8985. int i,j=0,k;
  8986. if(!sd) return 0;
  8987. for(i=0;i<MAX_INVENTORY;i++){
  8988. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  8989. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  8990. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  8991. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  8992. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  8993. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  8994. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  8995. for (k = 0; k < MAX_SLOTS; k++)
  8996. {
  8997. sprintf(card_var, "@inventorylist_card%d",k+1);
  8998. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  8999. }
  9000. j++;
  9001. }
  9002. }
  9003. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9004. return 0;
  9005. }
  9006. BUILDIN_FUNC(getskilllist)
  9007. {
  9008. TBL_PC *sd=script_rid2sd(st);
  9009. int i,j=0;
  9010. if(!sd) return 0;
  9011. for(i=0;i<MAX_SKILL;i++){
  9012. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9013. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9014. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9015. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9016. j++;
  9017. }
  9018. }
  9019. pc_setreg(sd,add_str("@skilllist_count"),j);
  9020. return 0;
  9021. }
  9022. BUILDIN_FUNC(clearitem)
  9023. {
  9024. TBL_PC *sd=script_rid2sd(st);
  9025. int i;
  9026. if(sd==NULL) return 0;
  9027. for (i=0; i<MAX_INVENTORY; i++) {
  9028. if (sd->status.inventory[i].amount) {
  9029. //Logs items, got from (N)PC scripts [Lupus]
  9030. if(log_config.enable_logs&0x40)
  9031. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9032. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9033. }
  9034. }
  9035. return 0;
  9036. }
  9037. /*==========================================
  9038. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9039. *------------------------------------------*/
  9040. BUILDIN_FUNC(disguise)
  9041. {
  9042. int id;
  9043. TBL_PC* sd = script_rid2sd(st);
  9044. if (sd == NULL) return 0;
  9045. id = script_getnum(st,2);
  9046. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9047. pc_disguise(sd, id);
  9048. script_pushint(st,id);
  9049. } else
  9050. script_pushint(st,0);
  9051. return 0;
  9052. }
  9053. /*==========================================
  9054. * Undisguise Player (returns 1 if success, 0 on fail)
  9055. *------------------------------------------*/
  9056. BUILDIN_FUNC(undisguise)
  9057. {
  9058. TBL_PC* sd = script_rid2sd(st);
  9059. if (sd == NULL) return 0;
  9060. if (sd->disguise) {
  9061. pc_disguise(sd, 0);
  9062. script_pushint(st,0);
  9063. } else {
  9064. script_pushint(st,1);
  9065. }
  9066. return 0;
  9067. }
  9068. /*==========================================
  9069. * NPCクラスチェンジ
  9070. * classは変わりたいclass
  9071. * typeは通常0なのかな?
  9072. *------------------------------------------*/
  9073. BUILDIN_FUNC(classchange)
  9074. {
  9075. int _class,type;
  9076. struct block_list *bl=map_id2bl(st->oid);
  9077. if(bl==NULL) return 0;
  9078. _class=script_getnum(st,2);
  9079. type=script_getnum(st,3);
  9080. clif_class_change(bl,_class,type);
  9081. return 0;
  9082. }
  9083. /*==========================================
  9084. * NPCから発生するエフェクト
  9085. *------------------------------------------*/
  9086. BUILDIN_FUNC(misceffect)
  9087. {
  9088. int type;
  9089. type=script_getnum(st,2);
  9090. if(st->oid && st->oid != fake_nd->bl.id) {
  9091. struct block_list *bl = map_id2bl(st->oid);
  9092. if (bl)
  9093. clif_misceffect2(bl,type);
  9094. } else{
  9095. TBL_PC *sd=script_rid2sd(st);
  9096. if(sd)
  9097. clif_misceffect2(&sd->bl,type);
  9098. }
  9099. return 0;
  9100. }
  9101. /*==========================================
  9102. * サウンドエフェクト
  9103. *------------------------------------------*/
  9104. BUILDIN_FUNC(soundeffect)
  9105. {
  9106. TBL_PC* sd = script_rid2sd(st);
  9107. const char* name = script_getstr(st,2);
  9108. int type = script_getnum(st,3);
  9109. if(sd)
  9110. {
  9111. if(!st->rid)
  9112. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9113. else
  9114. clif_soundeffect(sd,&sd->bl,name,type);
  9115. }
  9116. return 0;
  9117. }
  9118. int soundeffect_sub(struct block_list* bl,va_list ap)
  9119. {
  9120. char* name = va_arg(ap,char*);
  9121. int type = va_arg(ap,int);
  9122. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9123. return 0;
  9124. }
  9125. /*==========================================
  9126. * Play a sound effect (.wav) on multiple clients
  9127. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9128. *------------------------------------------*/
  9129. BUILDIN_FUNC(soundeffectall)
  9130. {
  9131. struct block_list* bl;
  9132. const char* name;
  9133. int type;
  9134. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9135. if (!bl)
  9136. return 0;
  9137. name = script_getstr(st,2);
  9138. type = script_getnum(st,3);
  9139. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9140. if(!script_hasdata(st,4))
  9141. { // area around
  9142. clif_soundeffectall(bl, name, type, AREA);
  9143. }
  9144. else
  9145. if(!script_hasdata(st,5))
  9146. { // entire map
  9147. const char* map = script_getstr(st,4);
  9148. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9149. }
  9150. else
  9151. if(script_hasdata(st,9))
  9152. { // specified part of map
  9153. const char* map = script_getstr(st,5);
  9154. int x0 = script_getnum(st,6);
  9155. int y0 = script_getnum(st,7);
  9156. int x1 = script_getnum(st,8);
  9157. int y1 = script_getnum(st,9);
  9158. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9159. }
  9160. else
  9161. {
  9162. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9163. }
  9164. return 0;
  9165. }
  9166. /*==========================================
  9167. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9168. *------------------------------------------*/
  9169. BUILDIN_FUNC(petrecovery)
  9170. {
  9171. struct pet_data *pd;
  9172. TBL_PC *sd=script_rid2sd(st);
  9173. if(sd==NULL || sd->pd==NULL)
  9174. return 0;
  9175. pd=sd->pd;
  9176. if (pd->recovery)
  9177. { //Halt previous bonus
  9178. if (pd->recovery->timer != -1)
  9179. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9180. } else //Init
  9181. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9182. pd->recovery->type=(sc_type)script_getnum(st,2);
  9183. pd->recovery->delay=script_getnum(st,3);
  9184. pd->recovery->timer=-1;
  9185. return 0;
  9186. }
  9187. /*==========================================
  9188. * pet healing [Valaris] //Rewritten by [Skotlex]
  9189. *------------------------------------------*/
  9190. BUILDIN_FUNC(petheal)
  9191. {
  9192. struct pet_data *pd;
  9193. TBL_PC *sd=script_rid2sd(st);
  9194. if(sd==NULL || sd->pd==NULL)
  9195. return 0;
  9196. pd=sd->pd;
  9197. if (pd->s_skill)
  9198. { //Clear previous skill
  9199. if (pd->s_skill->timer != -1)
  9200. {
  9201. if (pd->s_skill->id)
  9202. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9203. else
  9204. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9205. }
  9206. } else //init memory
  9207. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9208. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9209. //Use the lv as the amount to heal
  9210. pd->s_skill->lv=script_getnum(st,2);
  9211. pd->s_skill->delay=script_getnum(st,3);
  9212. pd->s_skill->hp=script_getnum(st,4);
  9213. pd->s_skill->sp=script_getnum(st,5);
  9214. //Use delay as initial offset to avoid skill/heal exploits
  9215. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9216. pd->s_skill->timer=-1;
  9217. else
  9218. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9219. return 0;
  9220. }
  9221. /*==========================================
  9222. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9223. *------------------------------------------*/
  9224. BUILDIN_FUNC(petskillattack)
  9225. {
  9226. struct pet_data *pd;
  9227. TBL_PC *sd=script_rid2sd(st);
  9228. if(sd==NULL || sd->pd==NULL)
  9229. return 0;
  9230. pd=sd->pd;
  9231. if (pd->a_skill == NULL)
  9232. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9233. pd->a_skill->id=script_getnum(st,2);
  9234. pd->a_skill->lv=script_getnum(st,3);
  9235. pd->a_skill->div_ = 0;
  9236. pd->a_skill->rate=script_getnum(st,4);
  9237. pd->a_skill->bonusrate=script_getnum(st,5);
  9238. return 0;
  9239. }
  9240. /*==========================================
  9241. * pet attack skills [Valaris]
  9242. *------------------------------------------*/
  9243. BUILDIN_FUNC(petskillattack2)
  9244. {
  9245. struct pet_data *pd;
  9246. TBL_PC *sd=script_rid2sd(st);
  9247. if(sd==NULL || sd->pd==NULL)
  9248. return 0;
  9249. pd=sd->pd;
  9250. if (pd->a_skill == NULL)
  9251. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9252. pd->a_skill->id=script_getnum(st,2);
  9253. pd->a_skill->lv=script_getnum(st,3);
  9254. pd->a_skill->div_ = script_getnum(st,4);
  9255. pd->a_skill->rate=script_getnum(st,5);
  9256. pd->a_skill->bonusrate=script_getnum(st,6);
  9257. return 0;
  9258. }
  9259. /*==========================================
  9260. * pet support skills [Skotlex]
  9261. *------------------------------------------*/
  9262. BUILDIN_FUNC(petskillsupport)
  9263. {
  9264. struct pet_data *pd;
  9265. TBL_PC *sd=script_rid2sd(st);
  9266. if(sd==NULL || sd->pd==NULL)
  9267. return 0;
  9268. pd=sd->pd;
  9269. if (pd->s_skill)
  9270. { //Clear previous skill
  9271. if (pd->s_skill->timer != -1)
  9272. {
  9273. if (pd->s_skill->id)
  9274. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9275. else
  9276. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9277. }
  9278. } else //init memory
  9279. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9280. pd->s_skill->id=script_getnum(st,2);
  9281. pd->s_skill->lv=script_getnum(st,3);
  9282. pd->s_skill->delay=script_getnum(st,4);
  9283. pd->s_skill->hp=script_getnum(st,5);
  9284. pd->s_skill->sp=script_getnum(st,6);
  9285. //Use delay as initial offset to avoid skill/heal exploits
  9286. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9287. pd->s_skill->timer=-1;
  9288. else
  9289. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9290. return 0;
  9291. }
  9292. /*==========================================
  9293. * Scripted skill effects [Celest]
  9294. *------------------------------------------*/
  9295. BUILDIN_FUNC(skilleffect)
  9296. {
  9297. TBL_PC *sd;
  9298. int skillid=script_getnum(st,2);
  9299. int skilllv=script_getnum(st,3);
  9300. sd=script_rid2sd(st);
  9301. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9302. return 0;
  9303. }
  9304. /*==========================================
  9305. * NPC skill effects [Valaris]
  9306. *------------------------------------------*/
  9307. BUILDIN_FUNC(npcskilleffect)
  9308. {
  9309. struct block_list *bl= map_id2bl(st->oid);
  9310. int skillid=script_getnum(st,2);
  9311. int skilllv=script_getnum(st,3);
  9312. int x=script_getnum(st,4);
  9313. int y=script_getnum(st,5);
  9314. if (bl)
  9315. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9316. return 0;
  9317. }
  9318. /*==========================================
  9319. * Special effects [Valaris]
  9320. *------------------------------------------*/
  9321. BUILDIN_FUNC(specialeffect)
  9322. {
  9323. struct block_list *bl=map_id2bl(st->oid);
  9324. int type = script_getnum(st,2);
  9325. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9326. if(bl==NULL)
  9327. return 0;
  9328. clif_specialeffect(bl, type, target);
  9329. return 0;
  9330. }
  9331. BUILDIN_FUNC(specialeffect2)
  9332. {
  9333. TBL_PC *sd=script_rid2sd(st);
  9334. int type = script_getnum(st,2);
  9335. enum send_target target = script_hasdata(st,3) ? (send_target)script_getnum(st,3) : AREA;
  9336. if(sd==NULL)
  9337. return 0;
  9338. clif_specialeffect(&sd->bl, type, target);
  9339. return 0;
  9340. }
  9341. /*==========================================
  9342. * Nude [Valaris]
  9343. *------------------------------------------*/
  9344. BUILDIN_FUNC(nude)
  9345. {
  9346. TBL_PC *sd=script_rid2sd(st);
  9347. int i,calcflag=0;
  9348. if(sd==NULL)
  9349. return 0;
  9350. for(i=0;i<11;i++)
  9351. if(sd->equip_index[i] >= 0) {
  9352. if(!calcflag)
  9353. calcflag=1;
  9354. pc_unequipitem(sd,sd->equip_index[i],2);
  9355. }
  9356. if(calcflag)
  9357. status_calc_pc(sd,0);
  9358. return 0;
  9359. }
  9360. /*==========================================
  9361. * gmcommand [MouseJstr]
  9362. *
  9363. * suggested on the forums...
  9364. * splitted into atcommand & charcommand by [Skotlex]
  9365. *------------------------------------------*/
  9366. BUILDIN_FUNC(atcommand)
  9367. {
  9368. TBL_PC dummy_sd;
  9369. TBL_PC* sd;
  9370. int fd;
  9371. const char* cmd;
  9372. cmd = script_getstr(st,2);
  9373. if (st->rid) {
  9374. sd = script_rid2sd(st);
  9375. fd = sd->fd;
  9376. } else { //Use a dummy character.
  9377. sd = &dummy_sd;
  9378. fd = 0;
  9379. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9380. if (st->oid)
  9381. {
  9382. struct block_list* bl = map_id2bl(st->oid);
  9383. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9384. if (bl->type == BL_NPC)
  9385. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9386. }
  9387. }
  9388. // compatibility with previous implementation (deprecated!)
  9389. if(cmd[0] != atcommand_symbol)
  9390. {
  9391. cmd += strlen(sd->status.name);
  9392. while(*cmd != atcommand_symbol && *cmd != 0)
  9393. cmd++;
  9394. }
  9395. is_atcommand_sub(fd, sd, cmd, 99);
  9396. return 0;
  9397. }
  9398. BUILDIN_FUNC(charcommand)
  9399. {
  9400. TBL_PC dummy_sd;
  9401. TBL_PC* sd;
  9402. int fd;
  9403. const char* cmd;
  9404. cmd = script_getstr(st,2);
  9405. if (st->rid) {
  9406. sd = script_rid2sd(st);
  9407. fd = sd->fd;
  9408. } else { //Use a dummy character.
  9409. sd = &dummy_sd;
  9410. fd = 0;
  9411. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9412. if (st->oid)
  9413. {
  9414. struct block_list* bl = map_id2bl(st->oid);
  9415. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9416. if (bl->type == BL_NPC)
  9417. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9418. }
  9419. }
  9420. // compatibility with previous implementation (deprecated!)
  9421. if(cmd[0] != charcommand_symbol)
  9422. {
  9423. cmd += strlen(sd->status.name);
  9424. while(*cmd != charcommand_symbol && *cmd != 0)
  9425. cmd++;
  9426. }
  9427. is_charcommand_sub(fd, sd, cmd, 99);
  9428. return 0;
  9429. }
  9430. /*==========================================
  9431. * Displays a message for the player only (like system messages like "you got an apple" )
  9432. *------------------------------------------*/
  9433. BUILDIN_FUNC(dispbottom)
  9434. {
  9435. TBL_PC *sd=script_rid2sd(st);
  9436. const char *message;
  9437. message=script_getstr(st,2);
  9438. if(sd)
  9439. clif_disp_onlyself(sd,message,(int)strlen(message));
  9440. return 0;
  9441. }
  9442. /*==========================================
  9443. * All The Players Full Recovery
  9444. * (HP/SP full restore and resurrect if need)
  9445. *------------------------------------------*/
  9446. BUILDIN_FUNC(recovery)
  9447. {
  9448. TBL_PC* sd;
  9449. struct s_mapiterator* iter;
  9450. iter = mapit_getallusers();
  9451. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9452. {
  9453. if(pc_isdead(sd))
  9454. status_revive(&sd->bl, 100, 100);
  9455. else
  9456. status_percent_heal(&sd->bl, 100, 100);
  9457. clif_displaymessage(sd->fd,"You have been recovered!");
  9458. }
  9459. mapit_free(iter);
  9460. return 0;
  9461. }
  9462. /*==========================================
  9463. * Get your pet info: getpetinfo(n)
  9464. * n -> 0:pet_id 1:pet_class 2:pet_name
  9465. * 3:friendly 4:hungry, 5: rename flag.
  9466. *------------------------------------------*/
  9467. BUILDIN_FUNC(getpetinfo)
  9468. {
  9469. TBL_PC *sd=script_rid2sd(st);
  9470. TBL_PET *pd;
  9471. int type=script_getnum(st,2);
  9472. if(!sd || !sd->pd) {
  9473. if (type == 2)
  9474. script_pushconststr(st,"null");
  9475. else
  9476. script_pushint(st,0);
  9477. return 0;
  9478. }
  9479. pd = sd->pd;
  9480. switch(type){
  9481. case 0: script_pushint(st,pd->pet.pet_id); break;
  9482. case 1: script_pushint(st,pd->pet.class_); break;
  9483. case 2: script_pushstrcopy(st,pd->pet.name); break;
  9484. case 3: script_pushint(st,pd->pet.intimate); break;
  9485. case 4: script_pushint(st,pd->pet.hungry); break;
  9486. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9487. default:
  9488. script_pushint(st,0);
  9489. break;
  9490. }
  9491. return 0;
  9492. }
  9493. /*==========================================
  9494. * Get your homunculus info: gethominfo(n)
  9495. * n -> 0:hom_id 1:class 2:name
  9496. * 3:friendly 4:hungry, 5: rename flag.
  9497. * 6: level
  9498. *------------------------------------------*/
  9499. BUILDIN_FUNC(gethominfo)
  9500. {
  9501. TBL_PC *sd=script_rid2sd(st);
  9502. TBL_HOM *hd;
  9503. int type=script_getnum(st,2);
  9504. hd = sd?sd->hd:NULL;
  9505. if(!merc_is_hom_active(hd))
  9506. {
  9507. if (type == 2)
  9508. script_pushconststr(st,"null");
  9509. else
  9510. script_pushint(st,0);
  9511. return 0;
  9512. }
  9513. switch(type){
  9514. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9515. case 1: script_pushint(st,hd->homunculus.class_); break;
  9516. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  9517. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9518. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9519. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9520. case 6: script_pushint(st,hd->homunculus.level); break;
  9521. default:
  9522. script_pushint(st,0);
  9523. break;
  9524. }
  9525. return 0;
  9526. }
  9527. /*==========================================
  9528. * Shows wether your inventory(and equips) contain
  9529. selected card or not.
  9530. checkequipedcard(4001);
  9531. *------------------------------------------*/
  9532. BUILDIN_FUNC(checkequipedcard)
  9533. {
  9534. TBL_PC *sd=script_rid2sd(st);
  9535. int n,i,c=0;
  9536. c=script_getnum(st,2);
  9537. if(sd){
  9538. for(i=0;i<MAX_INVENTORY;i++){
  9539. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9540. for(n=0;n<MAX_SLOTS;n++){
  9541. if(sd->status.inventory[i].card[n]==c){
  9542. script_pushint(st,1);
  9543. return 0;
  9544. }
  9545. }
  9546. }
  9547. }
  9548. }
  9549. script_pushint(st,0);
  9550. return 0;
  9551. }
  9552. BUILDIN_FUNC(jump_zero)
  9553. {
  9554. int sel;
  9555. sel=script_getnum(st,2);
  9556. if(!sel) {
  9557. int pos;
  9558. if( !data_islabel(script_getdata(st,3)) ){
  9559. ShowError("script: jump_zero: not label !\n");
  9560. st->state=END;
  9561. return 1;
  9562. }
  9563. pos=script_getnum(st,3);
  9564. st->pos=pos;
  9565. st->state=GOTO;
  9566. // printf("script: jump_zero: jumpto : %d\n",pos);
  9567. } else {
  9568. // printf("script: jump_zero: fail\n");
  9569. }
  9570. return 0;
  9571. }
  9572. /*==========================================
  9573. * GetMapMobs
  9574. returns mob counts on a set map:
  9575. e.g. GetMapMobs("prontera")
  9576. use "this" - for player's map
  9577. *------------------------------------------*/
  9578. BUILDIN_FUNC(getmapmobs)
  9579. {
  9580. const char *str=NULL;
  9581. int m=-1,bx,by;
  9582. int count=0;
  9583. struct block_list *bl;
  9584. str=script_getstr(st,2);
  9585. if(strcmp(str,"this")==0){
  9586. TBL_PC *sd=script_rid2sd(st);
  9587. if(sd)
  9588. m=sd->bl.m;
  9589. else{
  9590. script_pushint(st,-1);
  9591. return 0;
  9592. }
  9593. }else
  9594. m=map_mapname2mapid(str);
  9595. if(m < 0){
  9596. script_pushint(st,-1);
  9597. return 0;
  9598. }
  9599. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  9600. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  9601. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  9602. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9603. count++;
  9604. script_pushint(st,count);
  9605. return 0;
  9606. }
  9607. /*==========================================
  9608. * movenpc [MouseJstr]
  9609. *------------------------------------------*/
  9610. BUILDIN_FUNC(movenpc)
  9611. {
  9612. TBL_NPC *nd = NULL;
  9613. const char *npc;
  9614. int x,y;
  9615. npc = script_getstr(st,2);
  9616. x = script_getnum(st,3);
  9617. y = script_getnum(st,4);
  9618. if ((nd = npc_name2id(npc)) == NULL)
  9619. return -1;
  9620. npc_movenpc(nd, x, y);
  9621. return 0;
  9622. }
  9623. /*==========================================
  9624. * message [MouseJstr]
  9625. *------------------------------------------*/
  9626. BUILDIN_FUNC(message)
  9627. {
  9628. TBL_PC *sd;
  9629. const char *msg,*player;
  9630. TBL_PC *pl_sd = NULL;
  9631. sd = script_rid2sd(st);
  9632. player = script_getstr(st,2);
  9633. msg = script_getstr(st,3);
  9634. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9635. return 0;
  9636. clif_displaymessage(pl_sd->fd, msg);
  9637. return 0;
  9638. }
  9639. /*==========================================
  9640. * npctalk (sends message to surrounding area)
  9641. *------------------------------------------*/
  9642. BUILDIN_FUNC(npctalk)
  9643. {
  9644. const char* str;
  9645. char message[255];
  9646. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9647. str = script_getstr(st,2);
  9648. if(nd) {
  9649. memcpy(message, nd->name, NAME_LENGTH);
  9650. strtok(message, "#"); // discard extra name identifier if present
  9651. strcat(message, " : ");
  9652. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9653. clif_message(&(nd->bl), message);
  9654. }
  9655. return 0;
  9656. }
  9657. // change npc walkspeed [Valaris]
  9658. BUILDIN_FUNC(npcspeed)
  9659. {
  9660. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9661. int x=0;
  9662. x=script_getnum(st,2);
  9663. if(nd) {
  9664. nd->speed=x;
  9665. }
  9666. return 0;
  9667. }
  9668. // make an npc walk to a position [Valaris]
  9669. BUILDIN_FUNC(npcwalkto)
  9670. {
  9671. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9672. int x=0,y=0;
  9673. x=script_getnum(st,2);
  9674. y=script_getnum(st,3);
  9675. if(nd) {
  9676. unit_walktoxy(&nd->bl,x,y,0);
  9677. }
  9678. return 0;
  9679. }
  9680. // stop an npc's movement [Valaris]
  9681. BUILDIN_FUNC(npcstop)
  9682. {
  9683. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9684. if(nd) {
  9685. unit_stop_walking(&nd->bl,1);
  9686. }
  9687. return 0;
  9688. }
  9689. /*==========================================
  9690. * getlook char info. getlook(arg)
  9691. *------------------------------------------*/
  9692. BUILDIN_FUNC(getlook)
  9693. {
  9694. int type,val;
  9695. TBL_PC *sd;
  9696. sd=script_rid2sd(st);
  9697. type=script_getnum(st,2);
  9698. val=-1;
  9699. switch(type) {
  9700. case LOOK_HAIR: val=sd->status.hair; break; //1
  9701. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  9702. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  9703. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  9704. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  9705. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  9706. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  9707. case LOOK_SHIELD: val=sd->status.shield; break; //8
  9708. case LOOK_SHOES: break; //9
  9709. }
  9710. script_pushint(st,val);
  9711. return 0;
  9712. }
  9713. /*==========================================
  9714. * get char save point. argument: 0- map name, 1- x, 2- y
  9715. *------------------------------------------*/
  9716. BUILDIN_FUNC(getsavepoint)
  9717. {
  9718. TBL_PC* sd;
  9719. int type;
  9720. sd = script_rid2sd(st);
  9721. if (sd == NULL) {
  9722. script_pushint(st,0);
  9723. return 0;
  9724. }
  9725. type = script_getnum(st,2);
  9726. switch(type) {
  9727. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  9728. case 1: script_pushint(st,sd->status.save_point.x); break;
  9729. case 2: script_pushint(st,sd->status.save_point.y); break;
  9730. default:
  9731. script_pushint(st,0);
  9732. break;
  9733. }
  9734. return 0;
  9735. }
  9736. /*==========================================
  9737. * Get position for char/npc/pet/mob objects. Added by Lorky
  9738. *
  9739. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  9740. * where type:
  9741. * MapName$ - String variable for output map name
  9742. * MapX - Integer variable for output coord X
  9743. * MapY - Integer variable for output coord Y
  9744. * type - type of object
  9745. * 0 - Character coord
  9746. * 1 - NPC coord
  9747. * 2 - Pet coord
  9748. * 3 - Mob coord (not released)
  9749. * 4 - Homun coord
  9750. * CharName$ - Name object. If miss or "this" the current object
  9751. *
  9752. * Return:
  9753. * 0 - success
  9754. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  9755. *------------------------------------------*/
  9756. BUILDIN_FUNC(getmapxy)
  9757. {
  9758. struct block_list *bl = NULL;
  9759. TBL_PC *sd=NULL;
  9760. int num;
  9761. char *name;
  9762. char prefix;
  9763. int x,y,type;
  9764. char mapname[MAP_NAME_LENGTH];
  9765. memset(mapname, 0, sizeof(mapname));
  9766. if( !data_isreference(script_getdata(st,2)) ){
  9767. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  9768. script_pushint(st,-1);
  9769. return 1;
  9770. }
  9771. if( !data_isreference(script_getdata(st,3)) ){
  9772. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  9773. script_pushint(st,-1);
  9774. return 1;
  9775. }
  9776. if( !data_isreference(script_getdata(st,4)) ){
  9777. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  9778. script_pushint(st,-1);
  9779. return 1;
  9780. }
  9781. // Possible needly check function parameters on C_STR,C_INT,C_INT
  9782. type=script_getnum(st,5);
  9783. switch (type){
  9784. case 0: //Get Character Position
  9785. if( script_hasdata(st,6) )
  9786. sd=map_nick2sd(script_getstr(st,6));
  9787. else
  9788. sd=script_rid2sd(st);
  9789. if (sd)
  9790. bl = &sd->bl;
  9791. break;
  9792. case 1: //Get NPC Position
  9793. if( script_hasdata(st,6) )
  9794. {
  9795. struct npc_data *nd;
  9796. nd=npc_name2id(script_getstr(st,6));
  9797. if (nd)
  9798. bl = &nd->bl;
  9799. } else //In case the origin is not an npc?
  9800. bl=map_id2bl(st->oid);
  9801. break;
  9802. case 2: //Get Pet Position
  9803. if(script_hasdata(st,6))
  9804. sd=map_nick2sd(script_getstr(st,6));
  9805. else
  9806. sd=script_rid2sd(st);
  9807. if (sd && sd->pd)
  9808. bl = &sd->pd->bl;
  9809. break;
  9810. case 3: //Get Mob Position
  9811. break; //Not supported?
  9812. case 4: //Get Homun Position
  9813. if(script_hasdata(st,6))
  9814. sd=map_nick2sd(script_getstr(st,6));
  9815. else
  9816. sd=script_rid2sd(st);
  9817. if (sd && sd->hd)
  9818. bl = &sd->hd->bl;
  9819. break;
  9820. }
  9821. if (!bl) { //No object found.
  9822. script_pushint(st,-1);
  9823. return 0;
  9824. }
  9825. x= bl->x;
  9826. y= bl->y;
  9827. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  9828. //Set MapName$
  9829. num=st->stack->stack_data[st->start+2].u.num;
  9830. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9831. prefix=*name;
  9832. if(not_server_variable(prefix))
  9833. sd=script_rid2sd(st);
  9834. else
  9835. sd=NULL;
  9836. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  9837. //Set MapX
  9838. num=st->stack->stack_data[st->start+3].u.num;
  9839. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9840. prefix=*name;
  9841. if(not_server_variable(prefix))
  9842. sd=script_rid2sd(st);
  9843. else
  9844. sd=NULL;
  9845. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  9846. //Set MapY
  9847. num=st->stack->stack_data[st->start+4].u.num;
  9848. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9849. prefix=*name;
  9850. if(not_server_variable(prefix))
  9851. sd=script_rid2sd(st);
  9852. else
  9853. sd=NULL;
  9854. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  9855. //Return Success value
  9856. script_pushint(st,0);
  9857. return 0;
  9858. }
  9859. /*==========================================
  9860. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  9861. *------------------------------------------*/
  9862. BUILDIN_FUNC(logmes)
  9863. {
  9864. const char *str;
  9865. if (log_config.npc <= 0 ) return 0;
  9866. str = script_getstr(st,2);
  9867. log_npc(script_rid2sd(st),str);
  9868. return 0;
  9869. }
  9870. BUILDIN_FUNC(summon)
  9871. {
  9872. int _class, timeout=0;
  9873. const char *str,*event="";
  9874. TBL_PC *sd;
  9875. struct mob_data *md;
  9876. int tick = gettick();
  9877. sd=script_rid2sd(st);
  9878. if (!sd) return 0;
  9879. str =script_getstr(st,2);
  9880. _class=script_getnum(st,3);
  9881. if( script_hasdata(st,4) )
  9882. timeout=script_getnum(st,4);
  9883. if( script_hasdata(st,5) ){
  9884. event=script_getstr(st,5);
  9885. check_event(st, event);
  9886. }
  9887. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  9888. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  9889. if (md) {
  9890. md->master_id=sd->bl.id;
  9891. md->special_state.ai=1;
  9892. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  9893. mob_spawn (md); //Now it is ready for spawning.
  9894. clif_misceffect2(&md->bl,344);
  9895. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  9896. }
  9897. return 0;
  9898. }
  9899. /*==========================================
  9900. * Checks whether it is daytime/nighttime
  9901. *------------------------------------------*/
  9902. BUILDIN_FUNC(isnight)
  9903. {
  9904. script_pushint(st,(night_flag == 1));
  9905. return 0;
  9906. }
  9907. BUILDIN_FUNC(isday)
  9908. {
  9909. script_pushint(st,(night_flag == 0));
  9910. return 0;
  9911. }
  9912. /*================================================
  9913. * Check whether another item/card has been
  9914. * equipped - used for 2/15's cards patch [celest]
  9915. *------------------------------------------------*/
  9916. // leave this here, just in case
  9917. #if 0
  9918. BUILDIN_FUNC(isequipped)
  9919. {
  9920. TBL_PC *sd;
  9921. int i, j, k, id = 1;
  9922. int ret = -1;
  9923. sd = script_rid2sd(st);
  9924. for (i=0; id!=0; i++) {
  9925. int flag = 0;
  9926. FETCH (i+2, id) else id = 0;
  9927. if (id <= 0)
  9928. continue;
  9929. for (j=0; j<EQI_MAX; j++) {
  9930. int index;
  9931. index = sd->equip_index[j];
  9932. if(index < 0) continue;
  9933. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9934. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9935. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9936. if(!sd->inventory_data[index])
  9937. continue;
  9938. if(itemdb_type(id) != IT_CARD) { //Non card
  9939. if (sd->inventory_data[index]->nameid != id)
  9940. continue;
  9941. flag = 1;
  9942. break;
  9943. } else { //Card
  9944. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9945. continue;
  9946. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9947. if (sd->status.inventory[index].card[k] == id)
  9948. {
  9949. flag = 1;
  9950. break;
  9951. }
  9952. }
  9953. }
  9954. if (flag) break;
  9955. }
  9956. if (ret == -1)
  9957. ret = flag;
  9958. else
  9959. ret &= flag;
  9960. if (!ret) break;
  9961. }
  9962. script_pushint(st,ret);
  9963. return 0;
  9964. }
  9965. #endif
  9966. /*================================================
  9967. * Check how many items/cards in the list are
  9968. * equipped - used for 2/15's cards patch [celest]
  9969. *------------------------------------------------*/
  9970. BUILDIN_FUNC(isequippedcnt)
  9971. {
  9972. TBL_PC *sd;
  9973. int i, j, k, id = 1;
  9974. int ret = 0;
  9975. sd = script_rid2sd(st);
  9976. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9977. script_pushint(st,0);
  9978. return 0;
  9979. }
  9980. for (i=0; id!=0; i++) {
  9981. FETCH (i+2, id) else id = 0;
  9982. if (id <= 0)
  9983. continue;
  9984. for (j=0; j<EQI_MAX; j++) {
  9985. int index;
  9986. index = sd->equip_index[j];
  9987. if(index < 0) continue;
  9988. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9989. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9990. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9991. if(!sd->inventory_data[index])
  9992. continue;
  9993. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  9994. if (sd->inventory_data[index]->nameid == id)
  9995. ret+= sd->status.inventory[index].amount;
  9996. } else { //Count cards.
  9997. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9998. continue; //No cards
  9999. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10000. if (sd->status.inventory[index].card[k] == id)
  10001. ret++; //[Lupus]
  10002. }
  10003. }
  10004. }
  10005. }
  10006. script_pushint(st,ret);
  10007. return 0;
  10008. }
  10009. /*================================================
  10010. * Check whether another card has been
  10011. * equipped - used for 2/15's cards patch [celest]
  10012. * -- Items checked cannot be reused in another
  10013. * card set to prevent exploits
  10014. *------------------------------------------------*/
  10015. BUILDIN_FUNC(isequipped)
  10016. {
  10017. TBL_PC *sd;
  10018. int i, j, k, id = 1;
  10019. int index, flag;
  10020. int ret = -1;
  10021. //Original hash to reverse it when full check fails.
  10022. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10023. sd = script_rid2sd(st);
  10024. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10025. script_pushint(st,0);
  10026. return 0;
  10027. }
  10028. setitem_hash = sd->setitem_hash;
  10029. setitem_hash2 = sd->setitem_hash2;
  10030. for (i=0; id!=0; i++)
  10031. {
  10032. FETCH (i+2, id) else id = 0;
  10033. if (id <= 0)
  10034. continue;
  10035. flag = 0;
  10036. for (j=0; j<EQI_MAX; j++)
  10037. {
  10038. index = sd->equip_index[j];
  10039. if(index < 0) continue;
  10040. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10041. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10042. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10043. if(!sd->inventory_data[index])
  10044. continue;
  10045. if (itemdb_type(id) != IT_CARD) {
  10046. if (sd->inventory_data[index]->nameid != id)
  10047. continue;
  10048. flag = 1;
  10049. break;
  10050. } else { //Cards
  10051. if (sd->inventory_data[index]->slot == 0 ||
  10052. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10053. continue;
  10054. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10055. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10056. unsigned int hash = 0;
  10057. if (sd->status.inventory[index].card[k] != id)
  10058. continue;
  10059. hash = 1<<((j<5?j:j-5)*4 + k);
  10060. // check if card is already used by another set
  10061. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10062. continue;
  10063. // We have found a match
  10064. flag = 1;
  10065. // Set hash so this card cannot be used by another
  10066. if (j<5)
  10067. sd->setitem_hash |= hash;
  10068. else
  10069. sd->setitem_hash2 |= hash;
  10070. break;
  10071. }
  10072. }
  10073. if (flag) break; //Card found
  10074. }
  10075. if (ret == -1)
  10076. ret = flag;
  10077. else
  10078. ret &= flag;
  10079. if (!ret) break;
  10080. }
  10081. if (!ret)
  10082. { //When check fails, restore original hash values. [Skotlex]
  10083. sd->setitem_hash = setitem_hash;
  10084. sd->setitem_hash2 = setitem_hash2;
  10085. }
  10086. script_pushint(st,ret);
  10087. return 0;
  10088. }
  10089. /*================================================
  10090. * Check how many given inserted cards in the CURRENT
  10091. * weapon - used for 2/15's cards patch [Lupus]
  10092. *------------------------------------------------*/
  10093. BUILDIN_FUNC(cardscnt)
  10094. {
  10095. TBL_PC *sd;
  10096. int i, k, id = 1;
  10097. int ret = 0;
  10098. int index;
  10099. sd = script_rid2sd(st);
  10100. for (i=0; id!=0; i++) {
  10101. FETCH (i+2, id) else id = 0;
  10102. if (id <= 0)
  10103. continue;
  10104. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10105. if(index < 0) continue;
  10106. if(!sd->inventory_data[index])
  10107. continue;
  10108. if(itemdb_type(id) != IT_CARD) {
  10109. if (sd->inventory_data[index]->nameid == id)
  10110. ret+= sd->status.inventory[index].amount;
  10111. } else {
  10112. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10113. continue;
  10114. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10115. if (sd->status.inventory[index].card[k] == id)
  10116. ret++;
  10117. }
  10118. }
  10119. }
  10120. script_pushint(st,ret);
  10121. // script_pushint(st,current_equip_item_index);
  10122. return 0;
  10123. }
  10124. /*=======================================================
  10125. * Returns the refined number of the current item, or an
  10126. * item with inventory index specified
  10127. *-------------------------------------------------------*/
  10128. BUILDIN_FUNC(getrefine)
  10129. {
  10130. TBL_PC *sd;
  10131. if ((sd = script_rid2sd(st))!= NULL)
  10132. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10133. else
  10134. script_pushint(st,0);
  10135. return 0;
  10136. }
  10137. /*=======================================================
  10138. * Day/Night controls
  10139. *-------------------------------------------------------*/
  10140. BUILDIN_FUNC(night)
  10141. {
  10142. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10143. return 0;
  10144. }
  10145. BUILDIN_FUNC(day)
  10146. {
  10147. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10148. return 0;
  10149. }
  10150. //=======================================================
  10151. // Unequip [Spectre]
  10152. //-------------------------------------------------------
  10153. BUILDIN_FUNC(unequip)
  10154. {
  10155. int i;
  10156. size_t num;
  10157. TBL_PC *sd;
  10158. num = script_getnum(st,2);
  10159. sd = script_rid2sd(st);
  10160. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10161. {
  10162. i = pc_checkequip(sd,equip[num-1]);
  10163. if (i >= 0)
  10164. pc_unequipitem(sd,i,2);
  10165. return 0;
  10166. }
  10167. return 0;
  10168. }
  10169. BUILDIN_FUNC(equip)
  10170. {
  10171. int nameid=0,i;
  10172. TBL_PC *sd;
  10173. struct item_data *item_data;
  10174. sd = script_rid2sd(st);
  10175. nameid=script_getnum(st,2);
  10176. if((item_data = itemdb_exists(nameid)) == NULL)
  10177. {
  10178. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10179. return 1;
  10180. }
  10181. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10182. if( i < MAX_INVENTORY )
  10183. pc_equipitem(sd,i,item_data->equip);
  10184. return 0;
  10185. }
  10186. BUILDIN_FUNC(autoequip)
  10187. {
  10188. int nameid, flag;
  10189. struct item_data *item_data;
  10190. nameid=script_getnum(st,2);
  10191. flag=script_getnum(st,3);
  10192. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10193. item_data->flag.autoequip = flag>0?1:0;
  10194. }
  10195. return 0;
  10196. }
  10197. BUILDIN_FUNC(setbattleflag)
  10198. {
  10199. const char *flag, *value;
  10200. flag = script_getstr(st,2);
  10201. value = script_getstr(st,3);
  10202. if (battle_set_value(flag, value) == 0)
  10203. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10204. else
  10205. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10206. return 0;
  10207. }
  10208. BUILDIN_FUNC(getbattleflag)
  10209. {
  10210. const char *flag;
  10211. flag = script_getstr(st,2);
  10212. script_pushint(st,battle_get_value(flag));
  10213. return 0;
  10214. }
  10215. //=======================================================
  10216. // strlen [Valaris]
  10217. //-------------------------------------------------------
  10218. BUILDIN_FUNC(getstrlen)
  10219. {
  10220. const char *str = script_getstr(st,2);
  10221. int len = (str) ? (int)strlen(str) : 0;
  10222. script_pushint(st,len);
  10223. return 0;
  10224. }
  10225. //=======================================================
  10226. // isalpha [Valaris]
  10227. //-------------------------------------------------------
  10228. BUILDIN_FUNC(charisalpha)
  10229. {
  10230. const char *str=script_getstr(st,2);
  10231. int pos=script_getnum(st,3);
  10232. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10233. script_pushint(st,val);
  10234. return 0;
  10235. }
  10236. /// Changes the display name and/or display class of the npc.
  10237. /// Returns 0 is successful, 1 if the npc does not exist.
  10238. ///
  10239. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10240. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10241. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10242. BUILDIN_FUNC(setnpcdisplay)
  10243. {
  10244. const char* name;
  10245. const char* newname = NULL;
  10246. int class_ = -1;
  10247. struct script_data* data;
  10248. struct npc_data* nd;
  10249. name = script_getstr(st,2);
  10250. data = script_getdata(st,3);
  10251. get_val(st, data);
  10252. if( script_hasdata(st,4) )
  10253. {
  10254. newname = conv_str(st,data);
  10255. class_ = script_getnum(st,4);
  10256. }
  10257. else if( data_isstring(data) )
  10258. {
  10259. newname = conv_str(st,data);
  10260. }
  10261. else if( data_isint(data) )
  10262. {
  10263. class_ = conv_num(st,data);
  10264. }
  10265. else
  10266. {
  10267. ShowError("script:setnpcdisplay: expected a string or number\n");
  10268. script_reportdata(data);
  10269. return 1;
  10270. }
  10271. nd = npc_name2id(name);
  10272. if( nd == NULL )
  10273. {// not found
  10274. script_pushint(st,1);
  10275. return 0;
  10276. }
  10277. // update npc
  10278. if( newname )
  10279. npc_setdisplayname(nd, newname);
  10280. if( class_ != -1 )
  10281. npc_setclass(nd, class_);
  10282. script_pushint(st,0);
  10283. return 0;
  10284. }
  10285. BUILDIN_FUNC(atoi)
  10286. {
  10287. const char *value;
  10288. value = script_getstr(st,2);
  10289. script_pushint(st,atoi(value));
  10290. return 0;
  10291. }
  10292. // case-insensitive substring search [lordalfa]
  10293. BUILDIN_FUNC(compare)
  10294. {
  10295. const char *message;
  10296. const char *cmpstring;
  10297. message = script_getstr(st,2);
  10298. cmpstring = script_getstr(st,3);
  10299. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10300. return 0;
  10301. }
  10302. // [zBuffer] List of mathematics commands --->
  10303. BUILDIN_FUNC(sqrt)
  10304. {
  10305. double i, a;
  10306. i = script_getnum(st,2);
  10307. a = sqrt(i);
  10308. script_pushint(st,(int)a);
  10309. return 0;
  10310. }
  10311. BUILDIN_FUNC(pow)
  10312. {
  10313. double i, a, b;
  10314. a = script_getnum(st,2);
  10315. b = script_getnum(st,3);
  10316. i = pow(a,b);
  10317. script_pushint(st,(int)i);
  10318. return 0;
  10319. }
  10320. BUILDIN_FUNC(distance)
  10321. {
  10322. int x0, y0, x1, y1;
  10323. x0 = script_getnum(st,2);
  10324. y0 = script_getnum(st,3);
  10325. x1 = script_getnum(st,4);
  10326. y1 = script_getnum(st,5);
  10327. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10328. return 0;
  10329. }
  10330. // <--- [zBuffer] List of mathematics commands
  10331. // [zBuffer] List of dynamic var commands --->
  10332. //FIXME: some other functions are using this private function
  10333. void setd_sub(struct script_state *st, TBL_PC *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10334. {
  10335. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10336. return;
  10337. }
  10338. BUILDIN_FUNC(setd)
  10339. {
  10340. TBL_PC *sd=NULL;
  10341. char varname[100];
  10342. const char *value, *buffer;
  10343. int elem;
  10344. buffer = script_getstr(st, 2);
  10345. value = script_getstr(st, 3);
  10346. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10347. elem = 0;
  10348. if( not_server_variable(*varname) )
  10349. {
  10350. sd = script_rid2sd(st);
  10351. if( sd == NULL )
  10352. {
  10353. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10354. return 0;
  10355. }
  10356. }
  10357. if(varname[strlen(varname)-1] != '$') {
  10358. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10359. } else {
  10360. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10361. }
  10362. return 0;
  10363. }
  10364. #ifndef TXT_ONLY
  10365. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10366. {
  10367. int i, j;
  10368. TBL_PC* sd = NULL;
  10369. const char* query;
  10370. struct script_data* data;
  10371. char* name;
  10372. int max_rows = 128;// maximum number of rows
  10373. int num_vars;
  10374. int num_cols;
  10375. // check target variables
  10376. for( i = 3; script_hasdata(st,i); ++i )
  10377. {
  10378. data = script_getdata(st, i);
  10379. if( data_isreference(data) && reference_tovariable(data) )
  10380. {// it's a variable
  10381. name = reference_getname(data);
  10382. if( not_server_variable(*name) && sd == NULL )
  10383. {// requires a player
  10384. sd = script_rid2sd(st);
  10385. if( sd == NULL )
  10386. {// no player attached
  10387. script_reportdata(data);
  10388. st->state = END;
  10389. return 1;
  10390. }
  10391. }
  10392. if( not_array_variable(*name) )
  10393. max_rows = 1;// not an array, limit to one row
  10394. }
  10395. else
  10396. {
  10397. ShowError("script:query_sql: not a variable\n");
  10398. script_reportdata(data);
  10399. st->state = END;
  10400. return 1;
  10401. }
  10402. }
  10403. num_vars = i - 3;
  10404. // Execute the query
  10405. query = script_getstr(st,2);
  10406. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10407. {
  10408. Sql_ShowDebug(handle);
  10409. script_pushint(st, 0);
  10410. return 1;
  10411. }
  10412. if( Sql_NumRows(handle) == 0 )
  10413. {// No data received
  10414. Sql_FreeResult(handle);
  10415. script_pushint(st, 0);
  10416. return 0;
  10417. }
  10418. // Count the number of columns to store
  10419. num_cols = Sql_NumColumns(handle);
  10420. if( num_vars < num_cols )
  10421. {
  10422. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10423. script_reportsrc(st);
  10424. }
  10425. else if( num_vars > num_cols )
  10426. {
  10427. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10428. script_reportsrc(st);
  10429. }
  10430. // Store data
  10431. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10432. {
  10433. for( j = 0; j < num_vars; ++j )
  10434. {
  10435. char* str = NULL;
  10436. if( j < num_cols )
  10437. Sql_GetData(handle, j, &str, NULL);
  10438. data = script_getdata(st, j+3);
  10439. name = reference_getname(data);
  10440. if( is_string_variable(name) )
  10441. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10442. else
  10443. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10444. }
  10445. }
  10446. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10447. {
  10448. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10449. script_reportsrc(st);
  10450. }
  10451. // Free data
  10452. Sql_FreeResult(handle);
  10453. script_pushint(st, i);
  10454. return 0;
  10455. }
  10456. #endif
  10457. BUILDIN_FUNC(query_sql)
  10458. {
  10459. #ifndef TXT_ONLY
  10460. return buildin_query_sql_sub(st, mmysql_handle);
  10461. #else
  10462. //for TXT version, we always return -1
  10463. script_pushint(st,-1);
  10464. return 0;
  10465. #endif
  10466. }
  10467. BUILDIN_FUNC(query_logsql)
  10468. {
  10469. #ifndef TXT_ONLY
  10470. return buildin_query_sql_sub(st, logmysql_handle);
  10471. #else
  10472. //for TXT version, we always return -1
  10473. script_pushint(st,-1);
  10474. return 0;
  10475. #endif
  10476. }
  10477. //Allows escaping of a given string.
  10478. BUILDIN_FUNC(escape_sql)
  10479. {
  10480. const char *str;
  10481. char *esc_str;
  10482. size_t len;
  10483. str = script_getstr(st,2);
  10484. len = strlen(str);
  10485. esc_str = (char*)aMallocA(len*2+1);
  10486. #if defined(TXT_ONLY)
  10487. jstrescapecpy(esc_str, str);
  10488. #else
  10489. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10490. #endif
  10491. script_pushstr(st, esc_str);
  10492. return 0;
  10493. }
  10494. BUILDIN_FUNC(getd)
  10495. {
  10496. char varname[100];
  10497. const char *buffer;
  10498. int elem;
  10499. buffer = script_getstr(st, 2);
  10500. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10501. elem = 0;
  10502. // Push the 'pointer' so it's more flexible [Lance]
  10503. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10504. return 0;
  10505. }
  10506. // <--- [zBuffer] List of dynamic var commands
  10507. // Pet stat [Lance]
  10508. BUILDIN_FUNC(petstat)
  10509. {
  10510. TBL_PC *sd = NULL;
  10511. struct pet_data *pd;
  10512. int flag = script_getnum(st,2);
  10513. sd = script_rid2sd(st);
  10514. if(!sd || !sd->status.pet_id || !sd->pd){
  10515. if(flag == 2)
  10516. script_pushconststr(st, "");
  10517. else
  10518. script_pushint(st,0);
  10519. return 0;
  10520. }
  10521. pd = sd->pd;
  10522. switch(flag){
  10523. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10524. case 2: script_pushstrcopy(st, pd->pet.name); break;
  10525. case 3: script_pushint(st,(int)pd->pet.level); break;
  10526. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10527. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10528. default:
  10529. script_pushint(st,0);
  10530. break;
  10531. }
  10532. return 0;
  10533. }
  10534. BUILDIN_FUNC(callshop)
  10535. {
  10536. TBL_PC *sd = NULL;
  10537. struct npc_data *nd;
  10538. const char *shopname;
  10539. int flag = 0;
  10540. sd = script_rid2sd(st);
  10541. if (!sd) {
  10542. script_pushint(st,0);
  10543. return 0;
  10544. }
  10545. shopname = script_getstr(st, 2);
  10546. if( script_hasdata(st,3) )
  10547. flag = script_getnum(st,3);
  10548. nd = npc_name2id(shopname);
  10549. if (!nd || nd->bl.type!=BL_NPC || nd->subtype!=SHOP) {
  10550. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  10551. script_pushint(st,0);
  10552. return 1;
  10553. }
  10554. switch (flag) {
  10555. case 1: //Buy window
  10556. npc_buysellsel(sd,nd->bl.id,0);
  10557. break;
  10558. case 2: //Sell window
  10559. npc_buysellsel(sd,nd->bl.id,1);
  10560. break;
  10561. default: //Show menu
  10562. clif_npcbuysell(sd,nd->bl.id);
  10563. break;
  10564. }
  10565. sd->npc_shopid = nd->bl.id;
  10566. script_pushint(st,1);
  10567. return 0;
  10568. }
  10569. BUILDIN_FUNC(npcshopitem)
  10570. {
  10571. const char* npcname = script_getstr(st, 2);
  10572. struct npc_data* nd = npc_name2id(npcname);
  10573. int n, i;
  10574. int amount;
  10575. if( !nd || nd->subtype != SHOP )
  10576. { //Not found.
  10577. script_pushint(st,0);
  10578. return 0;
  10579. }
  10580. // get the count of new entries
  10581. amount = (script_lastdata(st)-2)/2;
  10582. // generate new shop item list
  10583. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  10584. for( n = 0, i = 3; n < amount; n++, i+=2 )
  10585. {
  10586. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10587. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10588. }
  10589. nd->u.shop.count = n;
  10590. script_pushint(st,1);
  10591. return 0;
  10592. }
  10593. BUILDIN_FUNC(npcshopadditem)
  10594. {
  10595. const char* npcname = script_getstr(st,2);
  10596. struct npc_data* nd = npc_name2id(npcname);
  10597. int n, i;
  10598. int amount;
  10599. if( !nd || nd->subtype != SHOP )
  10600. { //Not found.
  10601. script_pushint(st,0);
  10602. return 0;
  10603. }
  10604. // get the count of new entries
  10605. amount = (script_lastdata(st)-2)/2;
  10606. // append new items to existing shop item list
  10607. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  10608. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  10609. {
  10610. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10611. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10612. }
  10613. nd->u.shop.count = n;
  10614. script_pushint(st,1);
  10615. return 0;
  10616. }
  10617. BUILDIN_FUNC(npcshopdelitem)
  10618. {
  10619. const char* npcname = script_getstr(st,2);
  10620. struct npc_data* nd = npc_name2id(npcname);
  10621. int n, i;
  10622. int amount;
  10623. int size;
  10624. if( !nd || nd->subtype != SHOP )
  10625. { //Not found.
  10626. script_pushint(st,0);
  10627. return 0;
  10628. }
  10629. amount = script_lastdata(st)-2;
  10630. size = nd->u.shop.count;
  10631. // remove specified items from the shop item list
  10632. for( i = 3; i < 3 + amount; i++ )
  10633. {
  10634. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
  10635. if( n < size )
  10636. {
  10637. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  10638. size--;
  10639. }
  10640. }
  10641. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  10642. nd->u.shop.count = size;
  10643. script_pushint(st,1);
  10644. return 0;
  10645. }
  10646. //Sets a script to attach to a shop npc.
  10647. BUILDIN_FUNC(npcshopattach)
  10648. {
  10649. const char* npcname = script_getstr(st,2);
  10650. struct npc_data* nd = npc_name2id(npcname);
  10651. int flag = 1;
  10652. if( script_hasdata(st,3) )
  10653. flag = script_getnum(st,3);
  10654. if( !nd || nd->subtype != SHOP )
  10655. { //Not found.
  10656. script_pushint(st,0);
  10657. return 0;
  10658. }
  10659. if (flag)
  10660. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10661. else
  10662. nd->master_nd = NULL;
  10663. script_pushint(st,1);
  10664. return 0;
  10665. }
  10666. /*==========================================
  10667. * Returns some values of an item [Lupus]
  10668. * Price, Weight, etc...
  10669. setitemscript(itemID,"{new item bonus script}",[n]);
  10670. Where n:
  10671. 0 - script
  10672. 1 - Equip script
  10673. 2 - Unequip script
  10674. *------------------------------------------*/
  10675. BUILDIN_FUNC(setitemscript)
  10676. {
  10677. int item_id,n=0;
  10678. const char *script;
  10679. struct item_data *i_data;
  10680. struct script_code **dstscript;
  10681. item_id = script_getnum(st,2);
  10682. script = script_getstr(st,3);
  10683. if( script_hasdata(st,4) )
  10684. n=script_getnum(st,4);
  10685. i_data = itemdb_exists(item_id);
  10686. if (!i_data || script==NULL || script[0]!='{') {
  10687. script_pushint(st,0);
  10688. return 0;
  10689. }
  10690. switch (n) {
  10691. case 2:
  10692. dstscript = &i_data->unequip_script;
  10693. break;
  10694. case 1:
  10695. dstscript = &i_data->equip_script;
  10696. break;
  10697. default:
  10698. dstscript = &i_data->script;
  10699. break;
  10700. }
  10701. if(*dstscript)
  10702. script_free_code(*dstscript);
  10703. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  10704. script_pushint(st,1);
  10705. return 0;
  10706. }
  10707. /* Work In Progress [Lupus]
  10708. BUILDIN_FUNC(addmonsterdrop)
  10709. {
  10710. int class_,item_id,chance;
  10711. class_=script_getnum(st,2);
  10712. item_id=script_getnum(st,3);
  10713. chance=script_getnum(st,4);
  10714. if(class_>1000 && item_id>500 && chance>0) {
  10715. script_pushint(st,1);
  10716. } else {
  10717. script_pushint(st,0);
  10718. }
  10719. }
  10720. BUILDIN_FUNC(delmonsterdrop)
  10721. {
  10722. int class_,item_id;
  10723. class_=script_getnum(st,2);
  10724. item_id=script_getnum(st,3);
  10725. if(class_>1000 && item_id>500) {
  10726. script_pushint(st,1);
  10727. } else {
  10728. script_pushint(st,0);
  10729. }
  10730. }
  10731. */
  10732. /*==========================================
  10733. * Returns some values of a monster [Lupus]
  10734. * Name, Level, race, size, etc...
  10735. getmonsterinfo(monsterID,queryIndex);
  10736. *------------------------------------------*/
  10737. BUILDIN_FUNC(getmonsterinfo)
  10738. {
  10739. struct mob_db *mob;
  10740. int mob_id;
  10741. mob_id = script_getnum(st,2);
  10742. if (!mobdb_checkid(mob_id)) {
  10743. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  10744. if ( !script_getnum(st,3) ) //requested a string
  10745. script_pushconststr(st,"null");
  10746. else
  10747. script_pushint(st,-1);
  10748. return -1;
  10749. }
  10750. mob = mob_db(mob_id);
  10751. switch ( script_getnum(st,3) ) {
  10752. case 0: script_pushstrcopy(st,mob->jname); break;
  10753. case 1: script_pushint(st,mob->lv); break;
  10754. case 2: script_pushint(st,mob->status.max_hp); break;
  10755. case 3: script_pushint(st,mob->base_exp); break;
  10756. case 4: script_pushint(st,mob->job_exp); break;
  10757. case 5: script_pushint(st,mob->status.rhw.atk); break;
  10758. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  10759. case 7: script_pushint(st,mob->status.def); break;
  10760. case 8: script_pushint(st,mob->status.mdef); break;
  10761. case 9: script_pushint(st,mob->status.str); break;
  10762. case 10: script_pushint(st,mob->status.agi); break;
  10763. case 11: script_pushint(st,mob->status.vit); break;
  10764. case 12: script_pushint(st,mob->status.int_); break;
  10765. case 13: script_pushint(st,mob->status.dex); break;
  10766. case 14: script_pushint(st,mob->status.luk); break;
  10767. case 15: script_pushint(st,mob->status.rhw.range); break;
  10768. case 16: script_pushint(st,mob->range2); break;
  10769. case 17: script_pushint(st,mob->range3); break;
  10770. case 18: script_pushint(st,mob->status.size); break;
  10771. case 19: script_pushint(st,mob->status.race); break;
  10772. case 20: script_pushint(st,mob->status.def_ele); break;
  10773. case 21: script_pushint(st,mob->status.mode); break;
  10774. default: script_pushint(st,-1); //wrong Index
  10775. }
  10776. return 0;
  10777. }
  10778. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  10779. {
  10780. TBL_PC *sd = NULL;
  10781. if(script_hasdata(st,2))
  10782. sd = map_nick2sd(script_getstr(st,2));
  10783. else
  10784. sd = script_rid2sd(st);
  10785. if(sd)
  10786. script_pushint(st,(sd->vender_id != 0));
  10787. else
  10788. script_pushint(st,0);
  10789. return 0;
  10790. }
  10791. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  10792. {
  10793. TBL_PC *sd = NULL;
  10794. if(script_hasdata(st,2))
  10795. sd = map_nick2sd(script_getstr(st,2));
  10796. else
  10797. sd = script_rid2sd(st);
  10798. if(sd)
  10799. script_pushint(st,(sd->chatID != 0));
  10800. else
  10801. script_pushint(st,0);
  10802. return 0;
  10803. }
  10804. int axtoi(const char *hexStg)
  10805. {
  10806. int n = 0; // position in string
  10807. int m = 0; // position in digit[] to shift
  10808. int count; // loop index
  10809. int intValue = 0; // integer value of hex string
  10810. int digit[11]; // hold values to convert
  10811. while (n < 10) {
  10812. if (hexStg[n]=='\0')
  10813. break;
  10814. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  10815. digit[n] = hexStg[n] & 0x0f; //convert to int
  10816. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  10817. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10818. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  10819. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10820. else break;
  10821. n++;
  10822. }
  10823. count = n;
  10824. m = n - 1;
  10825. n = 0;
  10826. while(n < count) {
  10827. // digit[n] is value of hex digit at position n
  10828. // (m << 2) is the number of positions to shift
  10829. // OR the bits into return value
  10830. intValue = intValue | (digit[n] << (m << 2));
  10831. m--; // adjust the position to set
  10832. n++; // next digit to process
  10833. }
  10834. return (intValue);
  10835. }
  10836. // [Lance] Hex string to integer converter
  10837. BUILDIN_FUNC(axtoi)
  10838. {
  10839. const char *hex = script_getstr(st,2);
  10840. script_pushint(st,axtoi(hex));
  10841. return 0;
  10842. }
  10843. // [zBuffer] List of player cont commands --->
  10844. BUILDIN_FUNC(rid2name)
  10845. {
  10846. struct block_list *bl = NULL;
  10847. int rid = script_getnum(st,2);
  10848. if((bl = map_id2bl(rid)))
  10849. {
  10850. switch(bl->type) {
  10851. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  10852. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  10853. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  10854. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  10855. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  10856. default:
  10857. ShowError("buildin_rid2name: BL type unknown.\n");
  10858. script_pushconststr(st,"");
  10859. break;
  10860. }
  10861. } else {
  10862. ShowError("buildin_rid2name: invalid RID\n");
  10863. script_pushconststr(st,"(null)");
  10864. }
  10865. return 0;
  10866. }
  10867. BUILDIN_FUNC(pcblockmove)
  10868. {
  10869. int id, flag;
  10870. TBL_PC *sd = NULL;
  10871. id = script_getnum(st,2);
  10872. flag = script_getnum(st,3);
  10873. if(id)
  10874. sd = map_id2sd(id);
  10875. else
  10876. sd = script_rid2sd(st);
  10877. if(sd)
  10878. sd->state.blockedmove = flag > 0;
  10879. return 0;
  10880. }
  10881. BUILDIN_FUNC(pcfollow)
  10882. {
  10883. int id, targetid;
  10884. TBL_PC *sd = NULL;
  10885. id = script_getnum(st,2);
  10886. targetid = script_getnum(st,3);
  10887. if(id)
  10888. sd = map_id2sd(id);
  10889. else
  10890. sd = script_rid2sd(st);
  10891. if(sd)
  10892. pc_follow(sd, targetid);
  10893. return 0;
  10894. }
  10895. BUILDIN_FUNC(pcstopfollow)
  10896. {
  10897. int id;
  10898. TBL_PC *sd = NULL;
  10899. id = script_getnum(st,2);
  10900. if(id)
  10901. sd = map_id2sd(id);
  10902. else
  10903. sd = script_rid2sd(st);
  10904. if(sd)
  10905. pc_stop_following(sd);
  10906. return 0;
  10907. }
  10908. // <--- [zBuffer] List of player cont commands
  10909. // [zBuffer] List of mob control commands --->
  10910. //## TODO always return if the request/whatever was successfull [FlavioJS]
  10911. BUILDIN_FUNC(mobspawn)
  10912. {
  10913. int class_,x,y,id;
  10914. const char *str,*map;
  10915. // Who?
  10916. str =script_getstr(st,2);
  10917. // What?
  10918. class_ =script_getnum(st,3);
  10919. // Where?
  10920. map =script_getstr(st,4);
  10921. x =script_getnum(st,5);
  10922. y =script_getnum(st,6);
  10923. id = mob_once_spawn(map_id2sd(st->rid),map_mapname2mapid(map),x,y,str,class_,1,"");
  10924. script_pushint(st,id);
  10925. return 0;
  10926. }
  10927. BUILDIN_FUNC(mobremove)
  10928. {
  10929. int id;
  10930. struct block_list *bl = NULL;
  10931. id = script_getnum(st,2);
  10932. bl = map_id2bl(id);
  10933. if (bl && bl->type == BL_MOB)
  10934. unit_free(bl,0);
  10935. return 0;
  10936. }
  10937. BUILDIN_FUNC(getmobdata)
  10938. {
  10939. int num, id;
  10940. char *name;
  10941. struct mob_data *md = NULL;
  10942. TBL_PC *sd = st->rid?map_id2sd(st->rid):NULL;
  10943. id = script_getnum(st,2);
  10944. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || !data_isreference(script_getdata(st,3)) ){
  10945. ShowWarning("buildin_getmobdata: Error in argument!\n");
  10946. return -1;
  10947. }
  10948. num=st->stack->stack_data[st->start+3].u.num;
  10949. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10950. setd_sub(st,sd,name,0,(void *)(int)md->class_,script_getref(st,3));
  10951. setd_sub(st,sd,name,1,(void *)(int)md->level,script_getref(st,3));
  10952. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,script_getref(st,3));
  10953. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,script_getref(st,3));
  10954. setd_sub(st,sd,name,4,(void *)(int)md->master_id,script_getref(st,3));
  10955. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,script_getref(st,3));
  10956. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,script_getref(st,3));
  10957. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,script_getref(st,3));
  10958. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,script_getref(st,3));
  10959. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,script_getref(st,3));
  10960. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,script_getref(st,3));
  10961. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,script_getref(st,3));
  10962. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,script_getref(st,3));
  10963. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,script_getref(st,3));
  10964. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,script_getref(st,3));
  10965. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,script_getref(st,3));
  10966. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,script_getref(st,3));
  10967. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,script_getref(st,3));
  10968. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,script_getref(st,3));
  10969. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,script_getref(st,3));
  10970. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,script_getref(st,3));
  10971. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,script_getref(st,3));
  10972. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,script_getref(st,3));
  10973. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,script_getref(st,3));
  10974. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,script_getref(st,3));
  10975. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,script_getref(st,3));
  10976. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, script_getref(st,3));
  10977. return 0;
  10978. }
  10979. /// Changes the data of a mob
  10980. ///
  10981. /// setmobdata <mob unit id>,<type>,<value>;
  10982. BUILDIN_FUNC(setmobdata)
  10983. {
  10984. struct block_list* mob_bl;
  10985. mob_bl = map_id2bl(script_getnum(st,2));
  10986. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  10987. {
  10988. TBL_MOB* md = (TBL_MOB*)mob_bl;
  10989. int type;
  10990. int value;
  10991. type = script_getnum(st,3);
  10992. value = script_getnum(st,4);
  10993. switch( type )
  10994. {
  10995. case 0: md->class_ = (short)value; break;
  10996. case 1: md->level = (unsigned short)value; break;
  10997. case 2: md->status.hp = (unsigned int)value; break;
  10998. case 3: md->status.max_hp = (unsigned int)value; break;
  10999. case 4: md->master_id = value; break;
  11000. case 5: md->bl.m = (short)value; break;
  11001. case 6: md->bl.x = (short)value; break;
  11002. case 7: md->bl.y = (short)value; break;
  11003. case 8: md->status.speed = (unsigned short)value; break;
  11004. case 9: md->status.mode = (unsigned short)value; break;
  11005. case 10: md->special_state.ai = (unsigned int)value; break;
  11006. case 11: md->sc.option = (unsigned short)value; break;
  11007. case 12: md->vd->sex = (char)value; break;
  11008. case 13: md->vd->class_ = (unsigned short)value; break;
  11009. case 14: md->vd->hair_style = (unsigned short)value; break;
  11010. case 15: md->vd->hair_color = (unsigned short)value; break;
  11011. case 16: md->vd->head_bottom = (unsigned short)value; break;
  11012. case 17: md->vd->head_mid = (unsigned short)value; break;
  11013. case 18: md->vd->head_top = (unsigned short)value; break;
  11014. case 19: md->vd->cloth_color = (unsigned short)value; break;
  11015. case 20: md->vd->shield = (unsigned short)value; break;
  11016. case 21: md->vd->weapon = (unsigned short)value; break;
  11017. case 22: md->vd->shield = (unsigned short)value; break;
  11018. case 23: md->ud.dir = (unsigned char)value; break;
  11019. case 24: md->state.killer = value > 0 ? 1 : 0; break;
  11020. case 25: md->callback_flag = (short)value; break;
  11021. case 26: md->state.no_random_walk = value > 0 ? 1 : 0; break;
  11022. default:
  11023. ShowError("script:setmobdata: unknown data identifier %d\n", type);
  11024. return 1;
  11025. }
  11026. }
  11027. return 0;
  11028. }
  11029. /// Makes the mob assist the target unit as a slave
  11030. ///
  11031. /// mobassist <mob unit id>,"<player name>";
  11032. /// mobassist <mob unit id>,<target id>;
  11033. BUILDIN_FUNC(mobassist)
  11034. {
  11035. struct block_list* mob_bl;
  11036. // get mob
  11037. mob_bl = map_id2bl(script_getnum(st,2));
  11038. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11039. {
  11040. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11041. struct block_list* target_bl = NULL;
  11042. struct script_data* data;
  11043. // get target
  11044. data = script_getdata(st, 3);
  11045. get_val(st, data);
  11046. if( data_isstring(data) )
  11047. {
  11048. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11049. if( sd != NULL )
  11050. target_bl = &sd->bl;
  11051. }
  11052. if( target_bl == NULL )
  11053. target_bl = map_id2bl(conv_num(st, data));
  11054. // set mob as slave
  11055. if( target_bl != NULL )
  11056. {
  11057. struct unit_data* ud;
  11058. md->master_id = target_bl->id;
  11059. md->state.killer = 1;
  11060. mob_convertslave(md);
  11061. ud = unit_bl2ud(mob_bl);
  11062. if( ud != NULL )
  11063. {
  11064. if( ud->target != 0 )
  11065. md->target_id = ud->target;
  11066. else if( ud->skilltarget != 0 )
  11067. md->target_id = ud->skilltarget;
  11068. if( md->target_id != 0 )
  11069. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  11070. }
  11071. }
  11072. }
  11073. return 0;
  11074. }
  11075. /// Attaches the current npc or the target npc to the mob unit
  11076. ///
  11077. /// mobattach <mob unit id>{,"<npc name>"};
  11078. BUILDIN_FUNC(mobattach)
  11079. {
  11080. struct block_list* mob_bl;
  11081. mob_bl = map_id2bl(script_getnum(st,2));
  11082. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11083. {
  11084. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11085. TBL_NPC* nd = NULL;
  11086. if( script_hasdata(st,3) )
  11087. nd = npc_name2id(script_getstr(st, 3));
  11088. else
  11089. {
  11090. struct block_list* npc_bl = map_id2bl(st->oid);
  11091. if( npc_bl != NULL && npc_bl->type == BL_NPC )
  11092. nd = (TBL_NPC*)npc_bl;
  11093. }
  11094. if( nd != NULL )
  11095. md->nd = nd;
  11096. }
  11097. return 0;
  11098. }
  11099. /// Makes the unit walk to target position or map
  11100. /// Returns if it was successfull
  11101. ///
  11102. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11103. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11104. BUILDIN_FUNC(unitwalk)
  11105. {
  11106. struct block_list* bl;
  11107. bl = map_id2bl(script_getnum(st,2));
  11108. if( bl == NULL )
  11109. {
  11110. script_pushint(st, 0);
  11111. }
  11112. else if( script_hasdata(st,4) )
  11113. {
  11114. int x = script_getnum(st,3);
  11115. int y = script_getnum(st,4);
  11116. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11117. }
  11118. else
  11119. {
  11120. int map_id = script_getnum(st,3);
  11121. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11122. }
  11123. return 0;
  11124. }
  11125. /// Kills the unit
  11126. ///
  11127. /// unitkill <unit_id>;
  11128. BUILDIN_FUNC(unitkill)
  11129. {
  11130. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11131. if( bl != NULL )
  11132. status_kill(bl);
  11133. return 0;
  11134. }
  11135. /// Warps the unit to the target position in the target map
  11136. /// Returns if it was successfull
  11137. ///
  11138. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11139. BUILDIN_FUNC(unitwarp)
  11140. {
  11141. int unit_id;
  11142. int map;
  11143. short x;
  11144. short y;
  11145. struct block_list* bl;
  11146. unit_id = script_getnum(st,2);
  11147. map = map_mapname2mapid(script_getstr(st, 3));
  11148. x = (short)script_getnum(st,4);
  11149. y = (short)script_getnum(st,5);
  11150. bl = map_id2bl(unit_id);
  11151. if( map >= 0 && bl != NULL )
  11152. script_pushint(st, unit_warp(bl,map,x,y,0));
  11153. else
  11154. script_pushint(st, 0);
  11155. return 0;
  11156. }
  11157. /// Makes the unit attack the target.
  11158. /// If the unit is a player and <action type> is not 0, it does a continuous
  11159. /// attack instead of a single attack.
  11160. /// Returns if the request was successfull.
  11161. ///
  11162. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11163. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11164. BUILDIN_FUNC(unitattack)
  11165. {
  11166. struct block_list* unit_bl;
  11167. struct block_list* target_bl = NULL;
  11168. struct script_data* data;
  11169. int actiontype = 0;
  11170. // get unit
  11171. unit_bl = map_id2bl(script_getnum(st,2));
  11172. if( unit_bl == NULL ) {
  11173. script_pushint(st, 0);
  11174. return 0;
  11175. }
  11176. data = script_getdata(st, 3);
  11177. get_val(st, data);
  11178. if( data_isstring(data) )
  11179. {
  11180. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11181. if( sd != NULL )
  11182. target_bl = &sd->bl;
  11183. } else
  11184. target_bl = map_id2bl(conv_num(st, data));
  11185. // request the attack
  11186. if( target_bl == NULL )
  11187. {
  11188. script_pushint(st, 0);
  11189. return 0;
  11190. }
  11191. // get actiontype
  11192. if( script_hasdata(st,4) )
  11193. actiontype = script_getnum(st,4);
  11194. switch( unit_bl->type )
  11195. {
  11196. case BL_PC:
  11197. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11198. script_pushint(st, 1);
  11199. return 0;
  11200. case BL_MOB:
  11201. ((TBL_MOB *)unit_bl)->state.killer = 1;
  11202. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11203. break;
  11204. case BL_PET:
  11205. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11206. break;
  11207. default:
  11208. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11209. script_pushint(st, 0);
  11210. return 1;
  11211. }
  11212. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11213. return 0;
  11214. }
  11215. /// Makes the unit stop attacking and moving
  11216. ///
  11217. /// unitstop <unit_id>;
  11218. BUILDIN_FUNC(unitstop)
  11219. {
  11220. int unit_id;
  11221. struct block_list* bl;
  11222. unit_id = script_getnum(st,2);
  11223. bl = map_id2bl(unit_id);
  11224. if( bl != NULL )
  11225. {
  11226. unit_stop_attack(bl);
  11227. unit_stop_walking(bl,0);
  11228. if( bl->type == BL_MOB )
  11229. ((TBL_MOB*)bl)->target_id = 0;
  11230. }
  11231. return 0;
  11232. }
  11233. /// Makes the unit say the message
  11234. ///
  11235. /// unittalk <unit_id>,"<message>";
  11236. BUILDIN_FUNC(unittalk)
  11237. {
  11238. int unit_id;
  11239. const char* message;
  11240. struct block_list* bl;
  11241. unit_id = script_getnum(st,2);
  11242. message = script_getstr(st, 3);
  11243. bl = map_id2bl(unit_id);
  11244. if( bl != NULL )
  11245. {
  11246. struct StringBuf sbuf;
  11247. StringBuf_Init(&sbuf);
  11248. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11249. clif_message(bl, StringBuf_Value(&sbuf));
  11250. if( bl->type == BL_PC )
  11251. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11252. StringBuf_Destroy(&sbuf);
  11253. }
  11254. return 0;
  11255. }
  11256. /// Makes the unit do an emotion
  11257. ///
  11258. /// unitemote <unit_id>,<emotion>;
  11259. ///
  11260. /// @see e_* in const.txt
  11261. BUILDIN_FUNC(unitemote)
  11262. {
  11263. int unit_id;
  11264. int emotion;
  11265. struct block_list* bl;
  11266. unit_id = script_getnum(st,2);
  11267. emotion = script_getnum(st,3);
  11268. bl = map_id2bl(unit_id);
  11269. if( bl != NULL )
  11270. clif_emotion(bl, emotion);
  11271. return 0;
  11272. }
  11273. /// Makes the unit cast the skill on the target or self if no target is specified
  11274. ///
  11275. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11276. BUILDIN_FUNC(unitskilluseid)
  11277. {
  11278. int unit_id;
  11279. int skill_id;
  11280. int skill_lv;
  11281. int target_id;
  11282. struct block_list* bl;
  11283. unit_id = script_getnum(st,2);
  11284. skill_id = script_getnum(st,3);
  11285. skill_lv = script_getnum(st,4);
  11286. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11287. bl = map_id2bl(unit_id);
  11288. if( bl != NULL )
  11289. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11290. return 0;
  11291. }
  11292. /// Makes the unit cast the skill on the target position.
  11293. ///
  11294. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11295. BUILDIN_FUNC(unitskillusepos)
  11296. {
  11297. int unit_id;
  11298. int skill_id;
  11299. int skill_lv;
  11300. int skill_x;
  11301. int skill_y;
  11302. struct block_list* bl;
  11303. unit_id = script_getnum(st,2);
  11304. skill_id = script_getnum(st,3);
  11305. skill_lv = script_getnum(st,4);
  11306. skill_x = script_getnum(st,5);
  11307. skill_y = script_getnum(st,6);
  11308. bl = map_id2bl(unit_id);
  11309. if( bl != NULL )
  11310. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11311. return 0;
  11312. }
  11313. // <--- [zBuffer] List of mob control commands
  11314. /// Pauses the execution of the script, detaching the player
  11315. ///
  11316. /// sleep <mili seconds>;
  11317. BUILDIN_FUNC(sleep)
  11318. {
  11319. int ticks;
  11320. TBL_PC* sd;
  11321. ticks = script_getnum(st,2);
  11322. sd = map_id2sd(st->rid);
  11323. // detach the player
  11324. if( sd && sd->npc_id == st->oid )
  11325. {
  11326. sd->npc_id = 0;
  11327. }
  11328. st->rid = 0;
  11329. if( ticks <= 0 )
  11330. {// do nothing
  11331. }
  11332. else if( st->sleep.tick == 0 )
  11333. {// sleep for the target amount of time
  11334. st->state = RERUNLINE;
  11335. st->sleep.tick = ticks;
  11336. }
  11337. else
  11338. {// sleep time is over
  11339. st->sleep.tick = 0;
  11340. }
  11341. return 0;
  11342. }
  11343. /// Pauses the execution of the script, keeping the player attached
  11344. /// Returns if a player is still attached
  11345. ///
  11346. /// sleep2(<mili secconds>) -> <bool>
  11347. BUILDIN_FUNC(sleep2)
  11348. {
  11349. int ticks;
  11350. ticks = script_getnum(st,2);
  11351. if( ticks <= 0 )
  11352. {// do nothing
  11353. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11354. }
  11355. else if( !st->sleep.tick )
  11356. {// sleep for the target amount of time
  11357. st->state = RERUNLINE;
  11358. st->sleep.tick = ticks;
  11359. }
  11360. else
  11361. {// sleep time is over
  11362. st->sleep.tick = 0;
  11363. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11364. }
  11365. return 0;
  11366. }
  11367. /// Awakes all the sleep timers of the target npc
  11368. ///
  11369. /// awake "<npc name>";
  11370. BUILDIN_FUNC(awake)
  11371. {
  11372. struct npc_data* nd;
  11373. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11374. nd = npc_name2id(script_getstr(st, 2));
  11375. if( nd == NULL )
  11376. return 0;
  11377. while( node )
  11378. {
  11379. if( (int)node->key == nd->bl.id )
  11380. {// sleep timer for the npc
  11381. struct script_state* tst = (struct script_state*)node->data;
  11382. TBL_PC* sd = map_id2sd(tst->rid);
  11383. if( tst->sleep.timer == -1 )
  11384. {// already awake ???
  11385. node = node->next;
  11386. continue;
  11387. }
  11388. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11389. {// char not online anymore / another char of the same account is online - Cancel execution
  11390. tst->state = END;
  11391. tst->rid = 0;
  11392. }
  11393. delete_timer(tst->sleep.timer, run_script_timer);
  11394. node = script_erase_sleepdb(node);
  11395. tst->sleep.timer = -1;
  11396. tst->sleep.tick = 0;
  11397. run_script_main(tst);
  11398. }
  11399. else
  11400. {
  11401. node = node->next;
  11402. }
  11403. }
  11404. return 0;
  11405. }
  11406. /// Returns a reference to a variable of the target NPC.
  11407. /// Returns 0 if an error occurs.
  11408. ///
  11409. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11410. BUILDIN_FUNC(getvariableofnpc)
  11411. {
  11412. struct script_data* data;
  11413. char* name;
  11414. struct npc_data* nd;
  11415. data = script_getdata(st,2);
  11416. if( !data_isreference(data) )
  11417. {// Not a reference (aka varaible name)
  11418. ShowError("script:getvariableofnpc: not a variable\n");
  11419. script_reportdata(data);
  11420. script_pushnil(st);
  11421. st->state = END;
  11422. return 1;
  11423. }
  11424. name = reference_getname(data);
  11425. if( *name != '.' || name[1] == '@' )
  11426. {// not a npc variable
  11427. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11428. script_reportdata(data);
  11429. script_pushnil(st);
  11430. st->state = END;
  11431. return 1;
  11432. }
  11433. nd = npc_name2id(script_getstr(st,3));
  11434. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11435. {// NPC not found or has no script
  11436. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11437. script_pushnil(st);
  11438. st->state = END;
  11439. return 1;
  11440. }
  11441. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11442. return 0;
  11443. }
  11444. /// Opens a warp portal.
  11445. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11446. ///
  11447. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11448. ///
  11449. /// @author blackhole89
  11450. BUILDIN_FUNC(warpportal)
  11451. {
  11452. int spx;
  11453. int spy;
  11454. unsigned short mapindex;
  11455. int tpx;
  11456. int tpy;
  11457. struct skill_unit_group* group;
  11458. struct block_list* bl;
  11459. bl = map_id2bl(st->oid);
  11460. if( bl == NULL )
  11461. {
  11462. ShowError("script:warpportal: npc is needed\n");
  11463. return 1;
  11464. }
  11465. spx = script_getnum(st,2);
  11466. spy = script_getnum(st,3);
  11467. mapindex = mapindex_name2id(script_getstr(st, 4));
  11468. tpx = script_getnum(st,5);
  11469. tpy = script_getnum(st,6);
  11470. if( mapindex == 0 )
  11471. return 0;// map not found
  11472. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11473. if( group == NULL )
  11474. return 0;// failed
  11475. group->val2 = (tpx<<16) | tpy;
  11476. group->val3 = mapindex;
  11477. return 0;
  11478. }
  11479. BUILDIN_FUNC(openmail)
  11480. {
  11481. TBL_PC* sd;
  11482. sd = script_rid2sd(st);
  11483. if( sd == NULL )
  11484. return 0;
  11485. #ifndef TXT_ONLY
  11486. mail_openmail(sd);
  11487. #endif
  11488. return 0;
  11489. }
  11490. BUILDIN_FUNC(openauction)
  11491. {
  11492. TBL_PC* sd;
  11493. sd = script_rid2sd(st);
  11494. if( sd == NULL )
  11495. return 0;
  11496. #ifndef TXT_ONLY
  11497. clif_Auction_openwindow(sd);
  11498. #endif
  11499. return 0;
  11500. }
  11501. /// Retrieves the value of the specified flag of the specified cell.
  11502. ///
  11503. /// checkcell("<map name>",<x>,<y>,<type>) -> <bool>
  11504. ///
  11505. /// @see cell_chk* constants in const.txt for the types
  11506. BUILDIN_FUNC(checkcell)
  11507. {
  11508. int m = map_mapname2mapid(script_getstr(st,2));
  11509. int x = script_getnum(st,3);
  11510. int y = script_getnum(st,4);
  11511. cell_chk type = (cell_chk)script_getnum(st,5);
  11512. script_pushint(st, map_getcell(m, x, y, type));
  11513. return 0;
  11514. }
  11515. /// Modifies flags of cells in the specified area.
  11516. ///
  11517. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11518. ///
  11519. /// @see cell_* constants in const.txt for the types
  11520. BUILDIN_FUNC(setcell)
  11521. {
  11522. int m = map_mapname2mapid(script_getstr(st,2));
  11523. int x1 = script_getnum(st,3);
  11524. int y1 = script_getnum(st,4);
  11525. int x2 = script_getnum(st,5);
  11526. int y2 = script_getnum(st,6);
  11527. cell_t type = (cell_t)script_getnum(st,7);
  11528. bool flag = (bool)script_getnum(st,8);
  11529. int x,y;
  11530. if( x1 > x2 ) swap(x1,x2);
  11531. if( y1 > y2 ) swap(y1,y2);
  11532. for( y = y1; y <= y2; ++y )
  11533. for( x = x1; x <= x2; ++x )
  11534. map_setcell(m, x, y, type, flag);
  11535. return 0;
  11536. }
  11537. /******************
  11538. Questlog script commands
  11539. *******************/
  11540. BUILDIN_FUNC(getquest)
  11541. {
  11542. TBL_PC * sd = script_rid2sd(st);
  11543. struct quest qd;
  11544. int i, count = 0;
  11545. char * temp;
  11546. memset(&qd, 0, sizeof(struct quest));
  11547. qd.quest_id = script_getnum(st, 2);
  11548. qd.time = script_getnum(st, 3);
  11549. qd.state = Q_ACTIVE;
  11550. for(i=0; i<(script_lastdata(st)-3) && (i/2) < MAX_QUEST_OBJECTIVES; i+=2)
  11551. {
  11552. temp = (char*)script_getstr(st, i+4);
  11553. memcpy(&qd.objectives[i/2].name, temp, NAME_LENGTH);
  11554. temp = NULL;
  11555. qd.objectives[i/2].count = script_getnum(st, i+5);
  11556. count++;
  11557. }
  11558. qd.num_objectives = count;
  11559. quest_add(sd, &qd);
  11560. return 0;
  11561. }
  11562. BUILDIN_FUNC(deletequest)
  11563. {
  11564. TBL_PC * sd = script_rid2sd(st);
  11565. int qid = script_getnum(st, 2);
  11566. quest_delete(sd, qid);
  11567. return 0;
  11568. }
  11569. BUILDIN_FUNC(setquestobjective)
  11570. {
  11571. TBL_PC * sd = script_rid2sd(st);
  11572. int qid = script_getnum(st, 2);
  11573. int num = script_getnum(st, 3);
  11574. const char * str = script_getstr(st, 4);
  11575. int count = script_getnum(st, 5);
  11576. quest_update_objective(sd, qid, num, str, count);
  11577. return 0;
  11578. }
  11579. BUILDIN_FUNC(setqueststatus)
  11580. {
  11581. TBL_PC * sd = script_rid2sd(st);
  11582. int qid = script_getnum(st, 2);
  11583. bool active = script_getnum(st, 3)?true:false;
  11584. quest_update_status(sd, qid, active);
  11585. return 0;
  11586. }
  11587. // declarations that were supposed to be exported from npc_chat.c
  11588. #ifdef PCRE_SUPPORT
  11589. BUILDIN_FUNC(defpattern);
  11590. BUILDIN_FUNC(activatepset);
  11591. BUILDIN_FUNC(deactivatepset);
  11592. BUILDIN_FUNC(deletepset);
  11593. #endif
  11594. struct script_function buildin_func[] = {
  11595. // NPC interaction
  11596. BUILDIN_DEF(mes,"s"),
  11597. BUILDIN_DEF(next,""),
  11598. BUILDIN_DEF(close,""),
  11599. BUILDIN_DEF(close2,""),
  11600. BUILDIN_DEF(menu,"sl*"),
  11601. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  11602. BUILDIN_DEF(prompt,"s*"),
  11603. //
  11604. BUILDIN_DEF(goto,"l"),
  11605. BUILDIN_DEF(callsub,"i*"),
  11606. BUILDIN_DEF(callfunc,"s*"),
  11607. BUILDIN_DEF(return,"?"),
  11608. BUILDIN_DEF(getarg,"i?"),
  11609. BUILDIN_DEF(jobchange,"i*"),
  11610. BUILDIN_DEF(jobname,"i"),
  11611. BUILDIN_DEF(input,"v??"),
  11612. BUILDIN_DEF(warp,"sii"),
  11613. BUILDIN_DEF(areawarp,"siiiisii"),
  11614. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  11615. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  11616. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  11617. BUILDIN_DEF(setlook,"ii"),
  11618. BUILDIN_DEF(set,"ii"),
  11619. BUILDIN_DEF(setarray,"rv*"),
  11620. BUILDIN_DEF(cleararray,"rvi"),
  11621. BUILDIN_DEF(copyarray,"rri"),
  11622. BUILDIN_DEF(getarraysize,"r"),
  11623. BUILDIN_DEF(deletearray,"r?"),
  11624. BUILDIN_DEF(getelementofarray,"ri"),
  11625. BUILDIN_DEF(getitem,"vi?"),
  11626. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  11627. BUILDIN_DEF(getnameditem,"is"),
  11628. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  11629. BUILDIN_DEF(makeitem,"iisii"),
  11630. BUILDIN_DEF(delitem,"ii"),
  11631. BUILDIN_DEF(delitem2,"iiiiiiiii"),
  11632. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  11633. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  11634. BUILDIN_DEF(cutin,"si"),
  11635. BUILDIN_DEF(viewpoint,"iiiii"),
  11636. BUILDIN_DEF(heal,"ii"),
  11637. BUILDIN_DEF(itemheal,"ii"),
  11638. BUILDIN_DEF(percentheal,"ii"),
  11639. BUILDIN_DEF(rand,"i?"),
  11640. BUILDIN_DEF(countitem,"i"),
  11641. BUILDIN_DEF(countitem2,"iiiiiiii"),
  11642. BUILDIN_DEF(checkweight,"ii"),
  11643. BUILDIN_DEF(readparam,"i*"),
  11644. BUILDIN_DEF(getcharid,"i*"),
  11645. BUILDIN_DEF(getpartyname,"i"),
  11646. BUILDIN_DEF(getpartymember,"i*"),
  11647. BUILDIN_DEF(getpartyleader,"i?"),
  11648. BUILDIN_DEF(getguildname,"i"),
  11649. BUILDIN_DEF(getguildmaster,"i"),
  11650. BUILDIN_DEF(getguildmasterid,"i"),
  11651. BUILDIN_DEF(strcharinfo,"i"),
  11652. BUILDIN_DEF(strnpcinfo,"i"),
  11653. BUILDIN_DEF(getequipid,"i"),
  11654. BUILDIN_DEF(getequipname,"i"),
  11655. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  11656. BUILDIN_DEF(repair,"i"), // [Valaris]
  11657. BUILDIN_DEF(getequipisequiped,"i"),
  11658. BUILDIN_DEF(getequipisenableref,"i"),
  11659. BUILDIN_DEF(getequipisidentify,"i"),
  11660. BUILDIN_DEF(getequiprefinerycnt,"i"),
  11661. BUILDIN_DEF(getequipweaponlv,"i"),
  11662. BUILDIN_DEF(getequippercentrefinery,"i"),
  11663. BUILDIN_DEF(successrefitem,"i"),
  11664. BUILDIN_DEF(failedrefitem,"i"),
  11665. BUILDIN_DEF(statusup,"i"),
  11666. BUILDIN_DEF(statusup2,"ii"),
  11667. BUILDIN_DEF(bonus,"ii"),
  11668. BUILDIN_DEF2(bonus,"bonus2","iii"),
  11669. BUILDIN_DEF2(bonus,"bonus3","iiii"),
  11670. BUILDIN_DEF2(bonus,"bonus4","iiiii"),
  11671. BUILDIN_DEF2(bonus,"bonus5","iiiiii"),
  11672. BUILDIN_DEF(bonusautoscript,"si?"),
  11673. BUILDIN_DEF(bonusautoscript2,"si?"),
  11674. BUILDIN_DEF(skill,"ii?"),
  11675. BUILDIN_DEF(addtoskill,"ii?"), // [Valaris]
  11676. BUILDIN_DEF(guildskill,"ii"),
  11677. BUILDIN_DEF(getskilllv,"i"),
  11678. BUILDIN_DEF(getgdskilllv,"ii"),
  11679. BUILDIN_DEF(basicskillcheck,""),
  11680. BUILDIN_DEF(getgmlevel,""),
  11681. BUILDIN_DEF(end,""),
  11682. BUILDIN_DEF(checkoption,"i"),
  11683. BUILDIN_DEF(setoption,"i?"),
  11684. BUILDIN_DEF(setcart,"?"),
  11685. BUILDIN_DEF(checkcart,""),
  11686. BUILDIN_DEF(setfalcon,"?"),
  11687. BUILDIN_DEF(checkfalcon,""),
  11688. BUILDIN_DEF(setriding,"?"),
  11689. BUILDIN_DEF(checkriding,""),
  11690. BUILDIN_DEF2(savepoint,"save","sii"),
  11691. BUILDIN_DEF(savepoint,"sii"),
  11692. BUILDIN_DEF(gettimetick,"i"),
  11693. BUILDIN_DEF(gettime,"i"),
  11694. BUILDIN_DEF(gettimestr,"si"),
  11695. BUILDIN_DEF(openstorage,""),
  11696. BUILDIN_DEF(guildopenstorage,"*"),
  11697. BUILDIN_DEF(itemskill,"ii"),
  11698. BUILDIN_DEF(produce,"i"),
  11699. BUILDIN_DEF(monster,"siisii*"),
  11700. BUILDIN_DEF(areamonster,"siiiisii*"),
  11701. BUILDIN_DEF(killmonster,"ss"),
  11702. BUILDIN_DEF(killmonsterall,"s"),
  11703. BUILDIN_DEF(clone,"siisi*"),
  11704. BUILDIN_DEF(doevent,"s"),
  11705. BUILDIN_DEF(donpcevent,"s"),
  11706. BUILDIN_DEF(cmdothernpc,"ss"),
  11707. BUILDIN_DEF(addtimer,"is"),
  11708. BUILDIN_DEF(deltimer,"s"),
  11709. BUILDIN_DEF(addtimercount,"si"),
  11710. BUILDIN_DEF(initnpctimer,"??"),
  11711. BUILDIN_DEF(stopnpctimer,"??"),
  11712. BUILDIN_DEF(startnpctimer,"??"),
  11713. BUILDIN_DEF(setnpctimer,"i?"),
  11714. BUILDIN_DEF(getnpctimer,"i?"),
  11715. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  11716. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  11717. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  11718. BUILDIN_DEF(announce,"si*"),
  11719. BUILDIN_DEF(mapannounce,"ssi*"),
  11720. BUILDIN_DEF(areaannounce,"siiiisi*"),
  11721. BUILDIN_DEF(getusers,"i"),
  11722. BUILDIN_DEF(getmapguildusers,"si"),
  11723. BUILDIN_DEF(getmapusers,"s"),
  11724. BUILDIN_DEF(getareausers,"siiii"),
  11725. BUILDIN_DEF(getareadropitem,"siiiii"),
  11726. BUILDIN_DEF(enablenpc,"s"),
  11727. BUILDIN_DEF(disablenpc,"s"),
  11728. BUILDIN_DEF(hideoffnpc,"s"),
  11729. BUILDIN_DEF(hideonnpc,"s"),
  11730. BUILDIN_DEF(sc_start,"iii?"),
  11731. BUILDIN_DEF(sc_start2,"iiii?"),
  11732. BUILDIN_DEF(sc_start4,"iiiiii?"),
  11733. BUILDIN_DEF(sc_end,"i?"),
  11734. BUILDIN_DEF(getscrate,"ii*"),
  11735. BUILDIN_DEF(debugmes,"s"),
  11736. BUILDIN_DEF2(catchpet,"pet","i"),
  11737. BUILDIN_DEF2(birthpet,"bpet",""),
  11738. BUILDIN_DEF(resetlvl,"i"),
  11739. BUILDIN_DEF(resetstatus,""),
  11740. BUILDIN_DEF(resetskill,""),
  11741. BUILDIN_DEF(skillpointcount,""),
  11742. BUILDIN_DEF(changebase,"i"),
  11743. BUILDIN_DEF(changesex,""),
  11744. BUILDIN_DEF(waitingroom,"si??"),
  11745. BUILDIN_DEF(delwaitingroom,"?"),
  11746. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  11747. BUILDIN_DEF(enablewaitingroomevent,"?"),
  11748. BUILDIN_DEF(disablewaitingroomevent,"?"),
  11749. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  11750. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  11751. BUILDIN_DEF(getwaitingroomstate,"i?"),
  11752. BUILDIN_DEF(warpwaitingpc,"sii?"),
  11753. BUILDIN_DEF(attachrid,"i"),
  11754. BUILDIN_DEF(detachrid,""),
  11755. BUILDIN_DEF(isloggedin,"i?"),
  11756. BUILDIN_DEF(setmapflagnosave,"ssii"),
  11757. BUILDIN_DEF(getmapflag,"si"),
  11758. BUILDIN_DEF(setmapflag,"si*"),
  11759. BUILDIN_DEF(removemapflag,"si"),
  11760. BUILDIN_DEF(pvpon,"s"),
  11761. BUILDIN_DEF(pvpoff,"s"),
  11762. BUILDIN_DEF(gvgon,"s"),
  11763. BUILDIN_DEF(gvgoff,"s"),
  11764. BUILDIN_DEF(emotion,"i*"),
  11765. BUILDIN_DEF(maprespawnguildid,"sii"),
  11766. BUILDIN_DEF(agitstart,""), // <Agit>
  11767. BUILDIN_DEF(agitend,""),
  11768. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  11769. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  11770. BUILDIN_DEF(getcastlename,"s"),
  11771. BUILDIN_DEF(getcastledata,"si*"),
  11772. BUILDIN_DEF(setcastledata,"sii"),
  11773. BUILDIN_DEF(requestguildinfo,"i*"),
  11774. BUILDIN_DEF(getequipcardcnt,"i"),
  11775. BUILDIN_DEF(successremovecards,"i"),
  11776. BUILDIN_DEF(failedremovecards,"ii"),
  11777. BUILDIN_DEF(marriage,"s"),
  11778. BUILDIN_DEF2(wedding_effect,"wedding",""),
  11779. BUILDIN_DEF(divorce,""),
  11780. BUILDIN_DEF(ispartneron,""),
  11781. BUILDIN_DEF(getpartnerid,""),
  11782. BUILDIN_DEF(getchildid,""),
  11783. BUILDIN_DEF(getmotherid,""),
  11784. BUILDIN_DEF(getfatherid,""),
  11785. BUILDIN_DEF(warppartner,"sii"),
  11786. BUILDIN_DEF(getitemname,"i"),
  11787. BUILDIN_DEF(getitemslots,"i"),
  11788. BUILDIN_DEF(makepet,"i"),
  11789. BUILDIN_DEF(getexp,"ii"),
  11790. BUILDIN_DEF(getinventorylist,""),
  11791. BUILDIN_DEF(getskilllist,""),
  11792. BUILDIN_DEF(clearitem,""),
  11793. BUILDIN_DEF(classchange,"ii"),
  11794. BUILDIN_DEF(misceffect,"i"),
  11795. BUILDIN_DEF(soundeffect,"si"),
  11796. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  11797. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  11798. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  11799. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  11800. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  11801. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  11802. BUILDIN_DEF(petloot,"i"), // [Valaris]
  11803. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  11804. BUILDIN_DEF(petskillattack,"iiii"), // [Skotlex]
  11805. BUILDIN_DEF(petskillattack2,"iiiii"), // [Valaris]
  11806. BUILDIN_DEF(petskillsupport,"iiiii"), // [Skotlex]
  11807. BUILDIN_DEF(skilleffect,"ii"), // skill effect [Celest]
  11808. BUILDIN_DEF(npcskilleffect,"iiii"), // npc skill effect [Valaris]
  11809. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  11810. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  11811. BUILDIN_DEF(nude,""), // nude command [Valaris]
  11812. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  11813. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  11814. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  11815. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  11816. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  11817. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  11818. BUILDIN_DEF(mobcount,"ss"),
  11819. BUILDIN_DEF(getlook,"i"),
  11820. BUILDIN_DEF(getsavepoint,"i"),
  11821. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  11822. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  11823. BUILDIN_DEF(npcstop,""), // [Valaris]
  11824. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  11825. BUILDIN_DEF(checkoption1,"i"),
  11826. BUILDIN_DEF(checkoption2,"i"),
  11827. BUILDIN_DEF(guildgetexp,"i"),
  11828. BUILDIN_DEF(guildchangegm,"is"),
  11829. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  11830. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  11831. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  11832. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  11833. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  11834. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  11835. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  11836. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  11837. BUILDIN_DEF(night,""), // sets the server to night time
  11838. BUILDIN_DEF(day,""), // sets the server to day time
  11839. #ifdef PCRE_SUPPORT
  11840. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  11841. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  11842. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  11843. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  11844. #endif
  11845. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  11846. BUILDIN_DEF(getusersname,"*"),
  11847. BUILDIN_DEF(recovery,""),
  11848. BUILDIN_DEF(getpetinfo,"i"),
  11849. BUILDIN_DEF(gethominfo,"i"),
  11850. BUILDIN_DEF(checkequipedcard,"i"),
  11851. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  11852. BUILDIN_DEF(globalmes,"s*"),
  11853. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  11854. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  11855. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  11856. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  11857. BUILDIN_DEF(setnpcdisplay,"sv?"),
  11858. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  11859. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  11860. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  11861. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  11862. // [zBuffer] List of mathematics commands --->
  11863. BUILDIN_DEF(sqrt,"i"),
  11864. BUILDIN_DEF(pow,"ii"),
  11865. BUILDIN_DEF(distance,"iiii"),
  11866. // <--- [zBuffer] List of mathematics commands
  11867. // [zBuffer] List of dynamic var commands --->
  11868. BUILDIN_DEF(getd,"*"),
  11869. BUILDIN_DEF(setd,"*"),
  11870. // <--- [zBuffer] List of dynamic var commands
  11871. BUILDIN_DEF(petstat,"i"),
  11872. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  11873. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  11874. BUILDIN_DEF(npcshopadditem,"sii*"),
  11875. BUILDIN_DEF(npcshopdelitem,"si*"),
  11876. BUILDIN_DEF(npcshopattach,"s?"),
  11877. BUILDIN_DEF(equip,"i"),
  11878. BUILDIN_DEF(autoequip,"ii"),
  11879. BUILDIN_DEF(setbattleflag,"ss"),
  11880. BUILDIN_DEF(getbattleflag,"s"),
  11881. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  11882. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  11883. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  11884. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  11885. BUILDIN_DEF(axtoi,"s"),
  11886. BUILDIN_DEF(query_sql,"s*"),
  11887. BUILDIN_DEF(query_logsql,"s*"),
  11888. BUILDIN_DEF(escape_sql,"s"),
  11889. BUILDIN_DEF(atoi,"s"),
  11890. // [zBuffer] List of player cont commands --->
  11891. BUILDIN_DEF(rid2name,"i"),
  11892. BUILDIN_DEF(pcfollow,"ii"),
  11893. BUILDIN_DEF(pcstopfollow,"i"),
  11894. BUILDIN_DEF(pcblockmove,"ii"),
  11895. // <--- [zBuffer] List of player cont commands
  11896. // [zBuffer] List of mob control commands --->
  11897. BUILDIN_DEF(mobspawn,"*"),
  11898. BUILDIN_DEF(mobremove,"i"),
  11899. BUILDIN_DEF(getmobdata,"i*"),
  11900. BUILDIN_DEF(setmobdata,"iii"),
  11901. BUILDIN_DEF(mobassist,"i?"),
  11902. BUILDIN_DEF(mobattach,"i?"),
  11903. BUILDIN_DEF(unitwalk,"ii?"),
  11904. BUILDIN_DEF(unitkill,"i"),
  11905. BUILDIN_DEF(unitwarp,"isii"),
  11906. BUILDIN_DEF(unitattack,"iv?"),
  11907. BUILDIN_DEF(unitstop,"i"),
  11908. BUILDIN_DEF(unittalk,"is"),
  11909. BUILDIN_DEF(unitemote,"ii"),
  11910. BUILDIN_DEF(unitskilluseid,"iii?"), // originally by Qamera [Celest]
  11911. BUILDIN_DEF(unitskillusepos,"iiiii"), // [Celest]
  11912. // <--- [zBuffer] List of mob control commands
  11913. BUILDIN_DEF(sleep,"i"),
  11914. BUILDIN_DEF(sleep2,"i"),
  11915. BUILDIN_DEF(awake,"s"),
  11916. BUILDIN_DEF(getvariableofnpc,"rs"),
  11917. BUILDIN_DEF(warpportal,"iisii"),
  11918. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  11919. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  11920. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  11921. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  11922. BUILDIN_DEF(checkvending,"*"),
  11923. BUILDIN_DEF(checkchatting,"*"),
  11924. BUILDIN_DEF(openmail,""),
  11925. BUILDIN_DEF(openauction,""),
  11926. BUILDIN_DEF(checkcell,"siii"),
  11927. BUILDIN_DEF(setcell,"siiiiii"),
  11928. BUILDIN_DEF(getquest, "ii*"),
  11929. BUILDIN_DEF(deletequest, "i"),
  11930. BUILDIN_DEF(setquestobjective, "iisi"),
  11931. BUILDIN_DEF(setqueststatus, "ii"),
  11932. {NULL,NULL,NULL},
  11933. };