script.c 327 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391
  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 "chat.h"
  31. #include "battle.h"
  32. #include "party.h"
  33. #include "guild.h"
  34. #include "atcommand.h"
  35. #include "charcommand.h"
  36. #include "log.h"
  37. #include "unit.h"
  38. #include "irc.h"
  39. #include "pet.h"
  40. #include "mail.h"
  41. #include "script.h"
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <math.h>
  46. #ifndef WIN32
  47. #include <sys/time.h>
  48. #endif
  49. #include <time.h>
  50. #include <setjmp.h>
  51. #include <errno.h>
  52. ///////////////////////////////////////////////////////////////////////////////
  53. //## TODO possible enhancements: [FlavioJS]
  54. // - 'callfunc' supporting labels in the current npc "::LabelName"
  55. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  56. // - 'function FuncName;' function declarations reverting to global functions
  57. // if local label isn't found
  58. // - join callfunc and callsub's functionality
  59. //
  60. // struct script_state* st;
  61. //
  62. /// Returns the script_data at the target index
  63. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  64. /// Returns if the stack contains data at the target index
  65. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  66. /// Returns the index of the last data in the stack
  67. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  68. /// Pushes an int into the stack
  69. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  70. /// Pushes a string into the stack (script engine frees it automatically)
  71. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  72. /// Pushes a copy of a string into the stack
  73. #define script_pushstrcopy(st,val) push_str((st)->stack, C_STR, aStrdup(val))
  74. /// Pushes a constant string into the stack (must never change or be freed)
  75. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  76. /// Pushes a nil into the stack
  77. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  78. /// Pushes a copy of the data in the target index
  79. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  80. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  81. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  82. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  83. // Note: "top" functions/defines use indexes relative to the top of the stack
  84. // -1 is the index of the data at the top
  85. /// Returns the script_data at the target index relative to the top of the stack
  86. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  87. /// Pushes a copy of the data in the target index relative to the top of the stack
  88. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  89. /// Removes the range of values [start,end[ relative to the top of the stack
  90. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  91. //
  92. // struct script_data* data;
  93. //
  94. /// Returns if the script data is a string
  95. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  96. /// Returns if the script data is an int
  97. #define data_isint(data) ( (data)->type == C_INT )
  98. /// Returns if the script data is a reference
  99. #define data_isreference(data) ( (data)->type == C_NAME )
  100. /// Returns if the script data is a label
  101. #define data_islabel(data) ( (data)->type == C_POS )
  102. /// Returns if the script data is an internal script function label
  103. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  104. /// Returns if this is a reference to a constant
  105. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  106. /// Returns if this a reference to a param
  107. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  108. /// Returns if this a reference to a variable
  109. //##TODO confirm it's C_NAME [FlavioJS]
  110. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  111. /// Returns the unique id of the reference (id and index)
  112. #define reference_getuid(data) ( (data)->u.num )
  113. /// Returns the id of the reference
  114. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  115. /// Returns the array index of the reference
  116. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  117. /// Returns the name of the reference
  118. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  119. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  120. #define reference_getref(data) ( (data)->ref )
  121. /// Returns the value of the constant
  122. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  123. /// Returns the type of param
  124. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  125. /// Composes the uid of a reference from the id and the index
  126. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  127. #define FETCH(n, t) \
  128. if( script_hasdata(st,n) ) \
  129. (t)=script_getnum(st,n);
  130. #define SCRIPT_BLOCK_SIZE 512
  131. enum { LABEL_NEXTLINE=1,LABEL_START };
  132. static unsigned char * script_buf = NULL;
  133. static int script_pos,script_size;
  134. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  135. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  136. #define GETSTRING(off) (str_buf+(off))
  137. static char *str_buf;
  138. static int str_pos,str_size;
  139. static struct str_data_struct {
  140. enum c_op type;
  141. int str;
  142. int backpatch;
  143. int label;
  144. int (*func)(struct script_state *st);
  145. int val;
  146. int next;
  147. } *str_data = NULL;
  148. int str_num=LABEL_START,str_data_size;
  149. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  150. #define SCRIPT_HASH_SIZE 1021
  151. int str_hash[SCRIPT_HASH_SIZE];
  152. //#define SCRIPT_HASH_DJB2
  153. #define SCRIPT_HASH_SDBM
  154. //#define SCRIPT_HASH_ELF
  155. //#define SCRIPT_HASH_PJW
  156. static DBMap* mapreg_db=NULL; // int var_id -> int value
  157. static DBMap* mapregstr_db=NULL; // int var_id -> char* value
  158. static int mapreg_dirty=-1;
  159. char mapreg_txt[256]="save/mapreg.txt";
  160. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  161. static DBMap* scriptlabel_db=NULL; // const char* label_name -> int script_pos
  162. static DBMap* userfunc_db=NULL; // const char* func_name -> struct script_code*
  163. static int parse_options=0;
  164. DBMap* script_get_label_db(){ return scriptlabel_db; }
  165. DBMap* script_get_userfunc_db(){ return userfunc_db; }
  166. struct Script_Config script_config = {
  167. 1, 65535, 2048, //warn_func_mismatch_paramnum/check_cmdcount/check_gotocount
  168. 0, INT_MAX, // input_min_value/input_max_value
  169. "OnPCDieEvent", //die_event_name
  170. "OnPCKillEvent", //kill_pc_event_name
  171. "OnNPCKillEvent", //kill_mob_event_name
  172. "OnPCLoginEvent", //login_event_name
  173. "OnPCLogoutEvent", //logout_event_name
  174. "OnPCLoadMapEvent", //loadmap_event_name
  175. "OnPCBaseLvUpEvent", //baselvup_event_name
  176. "OnPCJobLvUpEvent" //joblvup_event_name
  177. };
  178. static jmp_buf error_jump;
  179. static char* error_msg;
  180. static const char* error_pos;
  181. static int error_report; // if the error should produce output
  182. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  183. // [Eoe / jA 1080, 1081, 1094, 1164]
  184. enum curly_type {
  185. TYPE_NULL = 0,
  186. TYPE_IF,
  187. TYPE_SWITCH,
  188. TYPE_WHILE,
  189. TYPE_FOR,
  190. TYPE_DO,
  191. TYPE_USERFUNC,
  192. TYPE_ARGLIST // function argument list
  193. };
  194. #define ARGLIST_UNDEFINED 0
  195. #define ARGLIST_NO_PAREN 1
  196. #define ARGLIST_PAREN 2
  197. static struct {
  198. struct {
  199. enum curly_type type;
  200. int index;
  201. int count;
  202. int flag;
  203. struct linkdb_node *case_label;
  204. } curly[256]; // 右カッコの情報
  205. int curly_count; // 右カッコの数
  206. int index; // スクリプト内で使用した構文の数
  207. } syntax;
  208. const char* parse_curly_close(const char* p);
  209. const char* parse_syntax_close(const char* p);
  210. const char* parse_syntax_close_sub(const char* p,int* flag);
  211. const char* parse_syntax(const char* p);
  212. static int parse_syntax_for_flag = 0;
  213. extern int current_equip_item_index; //for New CARDS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  214. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  215. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  216. int potion_target=0;
  217. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  218. char mapregsql_db[32] = "mapreg";
  219. char mapregsql_db_varname[32] = "varname";
  220. char mapregsql_db_index[32] = "index";
  221. char mapregsql_db_value[32] = "value";
  222. #endif
  223. int get_com(unsigned char *script,int *pos);
  224. int get_num(unsigned char *script,int *pos);
  225. struct script_function {
  226. int (*func)(struct script_state *st);
  227. const char *name;
  228. const char *arg;
  229. } buildin_func[];
  230. static struct linkdb_node *sleep_db;
  231. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.')
  232. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' )
  233. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  234. /*==========================================
  235. * ローカルプロトタイプ宣言 (必要な物のみ)
  236. *------------------------------------------*/
  237. const char* parse_subexpr(const char* p,int limit);
  238. void push_val(struct script_stack *stack,int type,int val);
  239. int run_func(struct script_state *st);
  240. int mapreg_setreg(int num,int val);
  241. int mapreg_setregstr(int num,const char *str);
  242. enum {
  243. MF_NOMEMO, //0
  244. MF_NOTELEPORT,
  245. MF_NOSAVE,
  246. MF_NOBRANCH,
  247. MF_NOPENALTY,
  248. MF_NOZENYPENALTY,
  249. MF_PVP,
  250. MF_PVP_NOPARTY,
  251. MF_PVP_NOGUILD,
  252. MF_GVG,
  253. MF_GVG_NOPARTY, //10
  254. MF_NOTRADE,
  255. MF_NOSKILL,
  256. MF_NOWARP,
  257. MF_PARTYLOCK,
  258. MF_NOICEWALL,
  259. MF_SNOW,
  260. MF_FOG,
  261. MF_SAKURA,
  262. MF_LEAVES,
  263. MF_RAIN, //20
  264. MF_INDOORS,
  265. MF_NOGO,
  266. MF_CLOUDS,
  267. MF_CLOUDS2,
  268. MF_FIREWORKS,
  269. MF_GVG_CASTLE,
  270. MF_GVG_DUNGEON,
  271. MF_NIGHTENABLED,
  272. MF_NOBASEEXP,
  273. MF_NOJOBEXP, //30
  274. MF_NOMOBLOOT,
  275. MF_NOMVPLOOT,
  276. MF_NORETURN,
  277. MF_NOWARPTO,
  278. MF_NIGHTMAREDROP,
  279. MF_RESTRICTED,
  280. MF_NOCOMMAND,
  281. MF_NODROP,
  282. MF_JEXP,
  283. MF_BEXP, //40
  284. MF_NOVENDING,
  285. MF_LOADEVENT,
  286. MF_NOCHAT,
  287. MF_NOEXPPENALTY,
  288. MF_GUILDLOCK
  289. };
  290. const char* script_op2name(int op)
  291. {
  292. #define RETURN_OP_NAME(type) case type: return #type
  293. switch( op )
  294. {
  295. RETURN_OP_NAME(C_NOP);
  296. RETURN_OP_NAME(C_POS);
  297. RETURN_OP_NAME(C_INT);
  298. RETURN_OP_NAME(C_PARAM);
  299. RETURN_OP_NAME(C_FUNC);
  300. RETURN_OP_NAME(C_STR);
  301. RETURN_OP_NAME(C_CONSTSTR);
  302. RETURN_OP_NAME(C_ARG);
  303. RETURN_OP_NAME(C_NAME);
  304. RETURN_OP_NAME(C_EOL);
  305. RETURN_OP_NAME(C_RETINFO);
  306. RETURN_OP_NAME(C_USERFUNC);
  307. RETURN_OP_NAME(C_USERFUNC_POS);
  308. // operators
  309. RETURN_OP_NAME(C_OP3);
  310. RETURN_OP_NAME(C_LOR);
  311. RETURN_OP_NAME(C_LAND);
  312. RETURN_OP_NAME(C_LE);
  313. RETURN_OP_NAME(C_LT);
  314. RETURN_OP_NAME(C_GE);
  315. RETURN_OP_NAME(C_GT);
  316. RETURN_OP_NAME(C_EQ);
  317. RETURN_OP_NAME(C_NE);
  318. RETURN_OP_NAME(C_XOR);
  319. RETURN_OP_NAME(C_OR);
  320. RETURN_OP_NAME(C_AND);
  321. RETURN_OP_NAME(C_ADD);
  322. RETURN_OP_NAME(C_SUB);
  323. RETURN_OP_NAME(C_MUL);
  324. RETURN_OP_NAME(C_DIV);
  325. RETURN_OP_NAME(C_MOD);
  326. RETURN_OP_NAME(C_NEG);
  327. RETURN_OP_NAME(C_LNOT);
  328. RETURN_OP_NAME(C_NOT);
  329. RETURN_OP_NAME(C_R_SHIFT);
  330. RETURN_OP_NAME(C_L_SHIFT);
  331. default:
  332. ShowDebug("script_op2name: unexpected op=%d\n", op);
  333. return "???";
  334. }
  335. #undef RETURN_OP_NAME
  336. }
  337. /// Reports on the console the src of a script error.
  338. static void script_reportsrc(struct script_state *st)
  339. {
  340. struct block_list* bl;
  341. if( st->oid == 0 )
  342. return; //Can't report source.
  343. bl = map_id2bl(st->oid);
  344. if( bl == NULL )
  345. return;
  346. switch( bl->type )
  347. {
  348. case BL_NPC:
  349. if( bl->m >= 0 )
  350. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  351. else
  352. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  353. break;
  354. default:
  355. if( bl->m >= 0 )
  356. 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);
  357. else
  358. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  359. break;
  360. }
  361. }
  362. /// Reports on the console information about the script data.
  363. static void script_reportdata(struct script_data* data)
  364. {
  365. if( data == NULL )
  366. return;
  367. switch( data->type )
  368. {
  369. case C_NOP:// no value
  370. ShowDebug("Data: nothing (nil)\n");
  371. break;
  372. case C_INT:// number
  373. ShowDebug("Data: number value=%d\n", data->u.num);
  374. break;
  375. case C_STR:
  376. case C_CONSTSTR:// string
  377. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  378. break;
  379. case C_NAME:// reference
  380. if( reference_tovariable(data) )
  381. {// variable
  382. char* name = reference_getname(data);
  383. if( not_array_variable(*name) )
  384. ShowDebug("Data: variable name='%s'\n", name);
  385. else
  386. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  387. }
  388. else if( reference_toconstant(data) )
  389. {// constant
  390. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  391. }
  392. else if( reference_toparam(data) )
  393. {// param
  394. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  395. }
  396. else
  397. {// ???
  398. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  399. ShowDebug("Please report this!!! - str_data.type=%s\n", script_op2name(str_data[reference_getid(data)].type));
  400. }
  401. break;
  402. case C_POS:// label
  403. ShowDebug("Data: label pos=%d\n", data->u.num);
  404. break;
  405. default:
  406. ShowDebug("Data: %s\n", script_op2name(data->type));
  407. break;
  408. }
  409. }
  410. /*==========================================
  411. * エラーメッセージ出力
  412. *------------------------------------------*/
  413. static void disp_error_message2(const char *mes,const char *pos,int report)
  414. {
  415. error_msg = aStrdup(mes);
  416. error_pos = pos;
  417. error_report = report;
  418. longjmp( error_jump, 1 );
  419. }
  420. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  421. /// Checks event parameter validity
  422. static void check_event(struct script_state *st, const char *evt)
  423. {
  424. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  425. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  426. script_reportsrc(st);
  427. }
  428. }
  429. /*==========================================
  430. * 文字列のハッシュを計算
  431. *------------------------------------------*/
  432. #define calc_hash(x) (calc_hash2(x)%SCRIPT_HASH_SIZE)
  433. static unsigned int calc_hash2(const char* p)
  434. {
  435. #if defined(SCRIPT_HASH_DJB2)
  436. unsigned int h = 5381;
  437. while( *p ) // hash*33 + c
  438. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  439. return h;
  440. #elif defined(SCRIPT_HASH_SDBM)
  441. unsigned int h = 0;
  442. while( *p )
  443. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  444. return h;
  445. #elif defined(SCRIPT_HASH_ELF)
  446. unsigned int h = 0;
  447. unsigned int g;
  448. while( *p ){ // UNIX ELF hash
  449. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  450. g = h & 0xF0000000;
  451. if( g )
  452. h ^= g >> 24;
  453. h &= ~g;
  454. }
  455. return h;
  456. #elif defined(SCRIPT_HASH_PJW)
  457. unsigned int h = 0;
  458. unsigned int g;
  459. while( *p ){
  460. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  461. g = h & 0xF0000000;
  462. if( g )
  463. {
  464. h ^= g>>24;
  465. h ^= g;
  466. }
  467. }
  468. return h;
  469. #else
  470. unsigned int h = 0;
  471. while( *p ){
  472. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 );
  473. h += (unsigned char)TOLOWER(*p++);
  474. }
  475. return h;
  476. #endif
  477. }
  478. /*==========================================
  479. * str_dataの中に名前があるか検索する
  480. *------------------------------------------*/
  481. // 既存のであれば番号、無ければ-1
  482. static int search_str(const char *p)
  483. {
  484. int i;
  485. i=str_hash[calc_hash(p)];
  486. while(i){
  487. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  488. return i;
  489. }
  490. i=str_data[i].next;
  491. }
  492. return -1;
  493. }
  494. /*==========================================
  495. * str_dataに名前を登録
  496. *------------------------------------------*/
  497. // 既存のであれば番号、無ければ登録して新規番号
  498. int add_str(const char* p)
  499. {
  500. int i;
  501. int len;
  502. i=calc_hash(p);
  503. if(str_hash[i]==0){
  504. str_hash[i]=str_num;
  505. } else {
  506. i=str_hash[i];
  507. for(;;){
  508. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  509. return i;
  510. }
  511. if(str_data[i].next==0)
  512. break;
  513. i=str_data[i].next;
  514. }
  515. str_data[i].next=str_num;
  516. }
  517. if(str_num>=str_data_size){
  518. str_data_size+=128;
  519. RECREATE(str_data,struct str_data_struct,str_data_size);
  520. memset(str_data + (str_data_size - 128), '\0', 128);
  521. }
  522. len=(int)strlen(p);
  523. while(str_pos+len+1>=str_size){
  524. str_size+=256;
  525. RECREATE(str_buf,char,str_size);
  526. memset(str_buf + (str_size - 256), '\0', 256);
  527. }
  528. memcpy(str_buf+str_pos,p,len+1);
  529. str_data[str_num].type=C_NOP;
  530. str_data[str_num].str=str_pos;
  531. str_data[str_num].next=0;
  532. str_data[str_num].func=NULL;
  533. str_data[str_num].backpatch=-1;
  534. str_data[str_num].label=-1;
  535. str_pos+=len+1;
  536. return str_num++;
  537. }
  538. /*==========================================
  539. * スクリプトバッファサイズの確認と拡張
  540. *------------------------------------------*/
  541. static void expand_script_buf(void)
  542. {
  543. script_size+=SCRIPT_BLOCK_SIZE;
  544. RECREATE(script_buf,unsigned char,script_size);
  545. }
  546. /*==========================================
  547. * スクリプトバッファに1バイト書き込む
  548. *------------------------------------------*/
  549. #define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a)
  550. #if 0
  551. static void add_scriptb(int a)
  552. {
  553. expand_script_buf();
  554. script_buf[script_pos++]=a;
  555. }
  556. #endif
  557. /*==========================================
  558. * スクリプトバッファにデータタイプを書き込む
  559. *------------------------------------------*/
  560. static void add_scriptc(int a)
  561. {
  562. while(a>=0x40){
  563. add_scriptb((a&0x3f)|0x40);
  564. a=(a-0x40)>>6;
  565. }
  566. add_scriptb(a&0x3f);
  567. }
  568. /*==========================================
  569. * スクリプトバッファに整数を書き込む
  570. *------------------------------------------*/
  571. static void add_scripti(int a)
  572. {
  573. while(a>=0x40){
  574. add_scriptb(a|0xc0);
  575. a=(a-0x40)>>6;
  576. }
  577. add_scriptb(a|0x80);
  578. }
  579. /*==========================================
  580. * スクリプトバッファにラベル/変数/関数を書き込む
  581. *------------------------------------------*/
  582. // 最大16Mまで
  583. static void add_scriptl(int l)
  584. {
  585. int backpatch = str_data[l].backpatch;
  586. switch(str_data[l].type){
  587. case C_POS:
  588. case C_USERFUNC_POS:
  589. add_scriptc(C_POS);
  590. add_scriptb(str_data[l].label);
  591. add_scriptb(str_data[l].label>>8);
  592. add_scriptb(str_data[l].label>>16);
  593. break;
  594. case C_NOP:
  595. case C_USERFUNC:
  596. // ラベルの可能性があるのでbackpatch用データ埋め込み
  597. add_scriptc(C_NAME);
  598. str_data[l].backpatch=script_pos;
  599. add_scriptb(backpatch);
  600. add_scriptb(backpatch>>8);
  601. add_scriptb(backpatch>>16);
  602. break;
  603. case C_INT:
  604. add_scripti(abs(str_data[l].val));
  605. if(str_data[l].val < 0) //Notice that this is negative, from jA (Rayce)
  606. add_scriptc(C_NEG);
  607. break;
  608. default:
  609. // もう他の用途と確定してるので数字をそのまま
  610. add_scriptc(C_NAME);
  611. add_scriptb(l);
  612. add_scriptb(l>>8);
  613. add_scriptb(l>>16);
  614. break;
  615. }
  616. }
  617. /*==========================================
  618. * ラベルを解決する
  619. *------------------------------------------*/
  620. void set_label(int l,int pos, const char* script_pos)
  621. {
  622. int i,next;
  623. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  624. { //Prevent overwriting constants values and parameters [Skotlex]
  625. disp_error_message("set_label: invalid label name",script_pos);
  626. return;
  627. }
  628. if(str_data[l].label!=-1){
  629. disp_error_message("set_label: dup label ",script_pos);
  630. return;
  631. }
  632. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  633. str_data[l].label=pos;
  634. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  635. next=GETVALUE(script_buf,i);
  636. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  637. SETVALUE(script_buf,i,pos);
  638. i=next;
  639. }
  640. }
  641. /// Skips spaces and/or comments.
  642. const char* skip_space(const char* p)
  643. {
  644. if( p == NULL )
  645. return NULL;
  646. for(;;)
  647. {
  648. while( ISSPACE(*p) )
  649. ++p;
  650. if( *p == '/' && p[1] == '/' )
  651. {// line comment
  652. while(*p && *p!='\n')
  653. ++p;
  654. }
  655. else if( *p == '/' && p[1] == '*' )
  656. {// block comment
  657. p += 2;
  658. for(;;)
  659. {
  660. if( *p == '\0' )
  661. return p;//disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  662. if( *p == '*' && p[1] == '/' )
  663. {// end of block comment
  664. p += 2;
  665. break;
  666. }
  667. ++p;
  668. }
  669. }
  670. else
  671. break;
  672. }
  673. return p;
  674. }
  675. /// Skips a word.
  676. /// A word consists of undercores and/or alfanumeric characters,
  677. /// and valid variable prefixes/postfixes.
  678. static
  679. const char* skip_word(const char* p)
  680. {
  681. // prefix
  682. switch( *p )
  683. {
  684. case '@':// temporary char variable
  685. ++p; break;
  686. case '#':// account variable
  687. p += ( p[1] == '#' ? 2 : 1 ); break;
  688. case '.':// npc variable
  689. p += ( p[1] == '@' ? 2 : 1 ); break;
  690. case '$':// global variable
  691. p += ( p[1] == '@' ? 2 : 1 ); break;
  692. }
  693. while( ISALNUM(*p) || *p == '_' )
  694. ++p;
  695. // postfix
  696. if( *p == '$' )// string
  697. p++;
  698. return p;
  699. }
  700. /// Adds a word to str_data.
  701. /// @see skip_word
  702. /// @see add_str
  703. static
  704. int add_word(const char* p)
  705. {
  706. char* word;
  707. int len;
  708. int i;
  709. // Check for a word
  710. len = skip_word(p) - p;
  711. if( len == 0 )
  712. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  713. // Duplicate the word
  714. CREATE(word, char, len+1);
  715. memcpy(word, p, len);
  716. word[len] = 0;
  717. // add the word
  718. i = add_str(word);
  719. aFree(word);
  720. return i;
  721. }
  722. /// Parses a function call.
  723. /// The argument list can have parenthesis or not.
  724. /// The number of arguments is checked.
  725. static
  726. const char* parse_callfunc(const char* p, int require_paren)
  727. {
  728. const char* p2;
  729. const char* arg=NULL;
  730. int func;
  731. func = add_word(p);
  732. if( str_data[func].type == C_FUNC ){
  733. // buildin function
  734. add_scriptl(func);
  735. add_scriptc(C_ARG);
  736. arg = buildin_func[str_data[func].val].arg;
  737. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  738. // script defined function
  739. int callsub = search_str("callsub");
  740. add_scriptl(callsub);
  741. add_scriptc(C_ARG);
  742. add_scriptl(func);
  743. arg = buildin_func[str_data[callsub].val].arg;
  744. if( *arg == 0 )
  745. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  746. if( *arg != '*' )
  747. ++arg; // count func as argument
  748. } else
  749. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  750. p = skip_word(p);
  751. p = skip_space(p);
  752. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  753. syntax.curly[syntax.curly_count].count = 0;
  754. if( *p == ';' )
  755. {// <func name> ';'
  756. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  757. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  758. {// <func name> '(' ')'
  759. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  760. p = p2;
  761. /*
  762. } else if( 0 && require_paren && *p != '(' )
  763. {// <func name>
  764. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  765. */
  766. } else
  767. {// <func name> <arg list>
  768. if( require_paren ){
  769. if( *p != '(' )
  770. disp_error_message("need '('",p);
  771. ++p; // skip '('
  772. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  773. } else if( *p == '(' ){
  774. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  775. } else {
  776. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  777. }
  778. ++syntax.curly_count;
  779. while( *arg ) {
  780. p2=parse_subexpr(p,-1);
  781. if( p == p2 )
  782. break; // not an argument
  783. if( *arg != '*' )
  784. ++arg; // next argument
  785. p=skip_space(p2);
  786. if( *arg == 0 || *p != ',' )
  787. break; // no more arguments
  788. ++p; // skip comma
  789. }
  790. --syntax.curly_count;
  791. }
  792. if( *arg && *arg != '?' && *arg != '*' )
  793. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  794. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  795. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  796. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  797. if( *p != ')' )
  798. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  799. ++p;
  800. }
  801. add_scriptc(C_FUNC);
  802. return p;
  803. }
  804. /*==========================================
  805. * 項の解析
  806. *------------------------------------------*/
  807. const char* parse_simpleexpr(const char *p)
  808. {
  809. int i;
  810. p=skip_space(p);
  811. #ifdef DEBUG_FUNCIN
  812. if(battle_config.etc_log)
  813. ShowDebug("parse_simpleexpr %s\n",p);
  814. #endif
  815. if(*p==';' || *p==',')
  816. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  817. if(*p=='('){
  818. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  819. ++syntax.curly[i].count;
  820. p=parse_subexpr(p+1,-1);
  821. p=skip_space(p);
  822. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  823. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  824. ){
  825. if( *p == ',' ){
  826. syntax.curly[i].flag = ARGLIST_PAREN;
  827. return p;
  828. } else
  829. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  830. }
  831. if( *p != ')' )
  832. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  833. ++p;
  834. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  835. char *np;
  836. i=strtoul(p,&np,0);
  837. add_scripti(i);
  838. p=np;
  839. } else if(*p=='"'){
  840. add_scriptc(C_STR);
  841. p++;
  842. while( *p && *p != '"' ){
  843. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  844. p++;
  845. else if( *p == '\n' )
  846. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  847. add_scriptb(*p++);
  848. }
  849. if(!*p)
  850. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  851. add_scriptb(0);
  852. p++; //'"'
  853. } else {
  854. int l;
  855. // label , register , function etc
  856. if(skip_word(p)==p)
  857. disp_error_message("parse_simpleexpr: unexpected character",p);
  858. l=add_word(p);
  859. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  860. return parse_callfunc(p,1);
  861. p=skip_word(p);
  862. if( *p == '[' ){
  863. // array(name[i] => getelementofarray(name,i) )
  864. add_scriptl(search_str("getelementofarray"));
  865. add_scriptc(C_ARG);
  866. add_scriptl(l);
  867. p=parse_subexpr(p+1,-1);
  868. p=skip_space(p);
  869. if( *p != ']' )
  870. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  871. ++p;
  872. add_scriptc(C_FUNC);
  873. }else
  874. add_scriptl(l);
  875. }
  876. #ifdef DEBUG_FUNCIN
  877. if(battle_config.etc_log)
  878. ShowDebug("parse_simpleexpr end %s\n",p);
  879. #endif
  880. return p;
  881. }
  882. /*==========================================
  883. * 式の解析
  884. *------------------------------------------*/
  885. const char* parse_subexpr(const char* p,int limit)
  886. {
  887. int op,opl,len;
  888. const char* tmpp;
  889. #ifdef DEBUG_FUNCIN
  890. if(battle_config.etc_log)
  891. ShowDebug("parse_subexpr %s\n",p);
  892. #endif
  893. p=skip_space(p);
  894. if(*p=='-'){
  895. tmpp=skip_space(p+1);
  896. if(*tmpp==';' || *tmpp==','){
  897. add_scriptl(LABEL_NEXTLINE);
  898. p++;
  899. return p;
  900. }
  901. }
  902. tmpp=p;
  903. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  904. p=parse_subexpr(p+1,10);
  905. add_scriptc(op);
  906. } else
  907. p=parse_simpleexpr(p);
  908. p=skip_space(p);
  909. while((
  910. (op=C_OP3,opl=0,len=1,*p=='?') ||
  911. (op=C_ADD,opl=8,len=1,*p=='+') ||
  912. (op=C_SUB,opl=8,len=1,*p=='-') ||
  913. (op=C_MUL,opl=9,len=1,*p=='*') ||
  914. (op=C_DIV,opl=9,len=1,*p=='/') ||
  915. (op=C_MOD,opl=9,len=1,*p=='%') ||
  916. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  917. (op=C_AND,opl=6,len=1,*p=='&') ||
  918. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  919. (op=C_OR,opl=5,len=1,*p=='|') ||
  920. (op=C_XOR,opl=4,len=1,*p=='^') ||
  921. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  922. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  923. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  924. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  925. (op=C_GT,opl=3,len=1,*p=='>') ||
  926. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  927. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  928. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  929. p+=len;
  930. if(op == C_OP3) {
  931. p=parse_subexpr(p,-1);
  932. p=skip_space(p);
  933. if( *(p++) != ':')
  934. disp_error_message("parse_subexpr: need ':'", p-1);
  935. p=parse_subexpr(p,-1);
  936. } else {
  937. p=parse_subexpr(p,opl);
  938. }
  939. add_scriptc(op);
  940. p=skip_space(p);
  941. }
  942. #ifdef DEBUG_FUNCIN
  943. if(battle_config.etc_log)
  944. ShowDebug("parse_subexpr end %s\n",p);
  945. #endif
  946. return p; /* return first untreated operator */
  947. }
  948. /*==========================================
  949. * 式の評価
  950. *------------------------------------------*/
  951. const char* parse_expr(const char *p)
  952. {
  953. #ifdef DEBUG_FUNCIN
  954. if(battle_config.etc_log)
  955. ShowDebug("parse_expr %s\n",p);
  956. #endif
  957. switch(*p){
  958. case ')': case ';': case ':': case '[': case ']':
  959. case '}':
  960. disp_error_message("parse_expr: unexpected char",p);
  961. }
  962. p=parse_subexpr(p,-1);
  963. #ifdef DEBUG_FUNCIN
  964. if(battle_config.etc_log)
  965. ShowDebug("parse_expr end %s\n",p);
  966. #endif
  967. return p;
  968. }
  969. /*==========================================
  970. * 行の解析
  971. *------------------------------------------*/
  972. const char* parse_line(const char* p)
  973. {
  974. const char* p2;
  975. p=skip_space(p);
  976. if(*p==';') {
  977. // if(); for(); while(); のために閉じ判定
  978. p = parse_syntax_close(p + 1);
  979. return p;
  980. }
  981. if(*p==')' && parse_syntax_for_flag)
  982. return p+1;
  983. p = skip_space(p);
  984. if(p[0] == '{') {
  985. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  986. syntax.curly[syntax.curly_count].count = -1;
  987. syntax.curly[syntax.curly_count].index = -1;
  988. syntax.curly_count++;
  989. return p + 1;
  990. } else if(p[0] == '}') {
  991. return parse_curly_close(p);
  992. }
  993. // 構文関連の処理
  994. p2 = parse_syntax(p);
  995. if(p2 != NULL)
  996. return p2;
  997. p = parse_callfunc(p,0);
  998. p = skip_space(p);
  999. if(parse_syntax_for_flag) {
  1000. if( *p != ')' )
  1001. disp_error_message("parse_line: need ')'",p);
  1002. } else {
  1003. if( *p != ';' )
  1004. disp_error_message("parse_line: need ';'",p);
  1005. }
  1006. // if, for , while の閉じ判定
  1007. p = parse_syntax_close(p+1);
  1008. return p;
  1009. }
  1010. // { ... } の閉じ処理
  1011. const char* parse_curly_close(const char* p)
  1012. {
  1013. if(syntax.curly_count <= 0) {
  1014. disp_error_message("parse_curly_close: unexpected string",p);
  1015. return p + 1;
  1016. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1017. syntax.curly_count--;
  1018. // if, for , while の閉じ判定
  1019. p = parse_syntax_close(p + 1);
  1020. return p;
  1021. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1022. // switch() 閉じ判定
  1023. int pos = syntax.curly_count-1;
  1024. char label[256];
  1025. int l;
  1026. // 一時変数を消す
  1027. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1028. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1029. parse_line(label);
  1030. syntax.curly_count--;
  1031. // 無条件で終了ポインタに移動
  1032. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1033. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1034. parse_line(label);
  1035. syntax.curly_count--;
  1036. // 現在地のラベルを付ける
  1037. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1038. l=add_str(label);
  1039. set_label(l,script_pos, p);
  1040. if(syntax.curly[pos].flag) {
  1041. // default が存在する
  1042. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1043. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1044. parse_line(label);
  1045. syntax.curly_count--;
  1046. }
  1047. // 終了ラベルを付ける
  1048. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1049. l=add_str(label);
  1050. set_label(l,script_pos, p);
  1051. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1052. syntax.curly_count--;
  1053. return p+1;
  1054. } else {
  1055. disp_error_message("parse_curly_close: unexpected string",p);
  1056. return p + 1;
  1057. }
  1058. }
  1059. // 構文関連の処理
  1060. // break, case, continue, default, do, for, function,
  1061. // if, switch, while をこの内部で処理します。
  1062. const char* parse_syntax(const char* p)
  1063. {
  1064. const char *p2 = skip_word(p);
  1065. switch(*p) {
  1066. case 'B':
  1067. case 'b':
  1068. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1069. // break の処理
  1070. char label[256];
  1071. int pos = syntax.curly_count - 1;
  1072. while(pos >= 0) {
  1073. if(syntax.curly[pos].type == TYPE_DO) {
  1074. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1075. break;
  1076. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1077. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1078. break;
  1079. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1080. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1081. break;
  1082. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1083. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1084. break;
  1085. }
  1086. pos--;
  1087. }
  1088. if(pos < 0) {
  1089. disp_error_message("parse_syntax: unexpected 'break'",p);
  1090. } else {
  1091. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1092. parse_line(label);
  1093. syntax.curly_count--;
  1094. }
  1095. p = skip_space(p2);
  1096. if(*p != ';')
  1097. disp_error_message("parse_syntax: need ';'",p);
  1098. // if, for , while の閉じ判定
  1099. p = parse_syntax_close(p + 1);
  1100. return p;
  1101. }
  1102. break;
  1103. case 'c':
  1104. case 'C':
  1105. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1106. // case の処理
  1107. int pos = syntax.curly_count-1;
  1108. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1109. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1110. return p+1;
  1111. } else {
  1112. char label[256];
  1113. int l,v;
  1114. char *np;
  1115. if(syntax.curly[pos].count != 1) {
  1116. // FALLTHRU 用のジャンプ
  1117. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1118. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1119. parse_line(label);
  1120. syntax.curly_count--;
  1121. // 現在地のラベルを付ける
  1122. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1123. l=add_str(label);
  1124. set_label(l,script_pos, p);
  1125. }
  1126. // switch 判定文
  1127. p = skip_space(p2);
  1128. if(p == p2) {
  1129. disp_error_message("parse_syntax: expect space ' '",p);
  1130. }
  1131. // check whether case label is integer or not
  1132. v = strtol(p,&np,0);
  1133. if(np == p) { //Check for constants
  1134. p2 = skip_word(p);
  1135. v = p2-p; // length of word at p2
  1136. memcpy(label,p,v);
  1137. label[v]='\0';
  1138. v = search_str(label);
  1139. if (v < 0 || str_data[v].type != C_INT)
  1140. disp_error_message("parse_syntax: 'case' label not integer",p);
  1141. v = str_data[v].val;
  1142. p = skip_word(p);
  1143. } else { //Numeric value
  1144. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1145. p++;
  1146. p = skip_word(p);
  1147. if(np != p)
  1148. disp_error_message("parse_syntax: 'case' label not integer",np);
  1149. }
  1150. p = skip_space(p);
  1151. if(*p != ':')
  1152. disp_error_message("parse_syntax: expect ':'",p);
  1153. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1154. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1155. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1156. // 2回parse しないとダメ
  1157. p2 = parse_line(label);
  1158. parse_line(p2);
  1159. syntax.curly_count--;
  1160. if(syntax.curly[pos].count != 1) {
  1161. // FALLTHRU 終了後のラベル
  1162. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1163. l=add_str(label);
  1164. set_label(l,script_pos,p);
  1165. }
  1166. // check duplication of case label [Rayce]
  1167. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1168. disp_error_message("parse_syntax: dup 'case'",p);
  1169. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1170. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1171. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1172. parse_line(label);
  1173. syntax.curly_count--;
  1174. syntax.curly[pos].count++;
  1175. }
  1176. return p + 1;
  1177. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1178. // continue の処理
  1179. char label[256];
  1180. int pos = syntax.curly_count - 1;
  1181. while(pos >= 0) {
  1182. if(syntax.curly[pos].type == TYPE_DO) {
  1183. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1184. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1185. break;
  1186. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1187. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1188. break;
  1189. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1190. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1191. break;
  1192. }
  1193. pos--;
  1194. }
  1195. if(pos < 0) {
  1196. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1197. } else {
  1198. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1199. parse_line(label);
  1200. syntax.curly_count--;
  1201. }
  1202. p = skip_space(p2);
  1203. if(*p != ';')
  1204. disp_error_message("parse_syntax: need ';'",p);
  1205. // if, for , while の閉じ判定
  1206. p = parse_syntax_close(p + 1);
  1207. return p;
  1208. }
  1209. break;
  1210. case 'd':
  1211. case 'D':
  1212. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1213. // switch - default の処理
  1214. int pos = syntax.curly_count-1;
  1215. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1216. disp_error_message("parse_syntax: unexpected 'default'",p);
  1217. } else if(syntax.curly[pos].flag) {
  1218. disp_error_message("parse_syntax: dup 'default'",p);
  1219. } else {
  1220. char label[256];
  1221. int l;
  1222. // 現在地のラベルを付ける
  1223. p = skip_space(p2);
  1224. if(*p != ':') {
  1225. disp_error_message("parse_syntax: need ':'",p);
  1226. }
  1227. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1228. l=add_str(label);
  1229. set_label(l,script_pos,p);
  1230. // 無条件で次のリンクに飛ばす
  1231. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1232. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1233. parse_line(label);
  1234. syntax.curly_count--;
  1235. // default のラベルを付ける
  1236. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1237. l=add_str(label);
  1238. set_label(l,script_pos,p);
  1239. syntax.curly[syntax.curly_count - 1].flag = 1;
  1240. syntax.curly[pos].count++;
  1241. }
  1242. return p + 1;
  1243. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1244. int l;
  1245. char label[256];
  1246. p=skip_space(p2);
  1247. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1248. syntax.curly[syntax.curly_count].count = 1;
  1249. syntax.curly[syntax.curly_count].index = syntax.index++;
  1250. syntax.curly[syntax.curly_count].flag = 0;
  1251. // 現在地のラベル形成する
  1252. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1253. l=add_str(label);
  1254. set_label(l,script_pos,p);
  1255. syntax.curly_count++;
  1256. return p;
  1257. }
  1258. break;
  1259. case 'f':
  1260. case 'F':
  1261. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1262. int l;
  1263. char label[256];
  1264. int pos = syntax.curly_count;
  1265. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1266. syntax.curly[syntax.curly_count].count = 1;
  1267. syntax.curly[syntax.curly_count].index = syntax.index++;
  1268. syntax.curly[syntax.curly_count].flag = 0;
  1269. syntax.curly_count++;
  1270. p=skip_space(p2);
  1271. if(*p != '(')
  1272. disp_error_message("parse_syntax: need '('",p);
  1273. p++;
  1274. // 初期化文を実行する
  1275. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1276. p=parse_line(p);
  1277. syntax.curly_count--;
  1278. // 条件判断開始のラベル形成する
  1279. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1280. l=add_str(label);
  1281. set_label(l,script_pos,p);
  1282. p=skip_space(p);
  1283. if(*p == ';') {
  1284. // for(;;) のパターンなので必ず真
  1285. ;
  1286. } else {
  1287. // 条件が偽なら終了地点に飛ばす
  1288. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1289. add_scriptl(add_str("jump_zero"));
  1290. add_scriptc(C_ARG);
  1291. p=parse_expr(p);
  1292. p=skip_space(p);
  1293. add_scriptl(add_str(label));
  1294. add_scriptc(C_FUNC);
  1295. }
  1296. if(*p != ';')
  1297. disp_error_message("parse_syntax: need ';'",p);
  1298. p++;
  1299. // ループ開始に飛ばす
  1300. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1301. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1302. parse_line(label);
  1303. syntax.curly_count--;
  1304. // 次のループへのラベル形成する
  1305. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1306. l=add_str(label);
  1307. set_label(l,script_pos,p);
  1308. // 次のループに入る時の処理
  1309. // for 最後の ')' を ';' として扱うフラグ
  1310. parse_syntax_for_flag = 1;
  1311. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1312. p=parse_line(p);
  1313. syntax.curly_count--;
  1314. parse_syntax_for_flag = 0;
  1315. // 条件判定処理に飛ばす
  1316. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1317. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1318. parse_line(label);
  1319. syntax.curly_count--;
  1320. // ループ開始のラベル付け
  1321. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1322. l=add_str(label);
  1323. set_label(l,script_pos,p);
  1324. return p;
  1325. }
  1326. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1327. {// internal script function
  1328. const char *func_name;
  1329. func_name = skip_space(p2);
  1330. p = skip_word(func_name);
  1331. if( p == func_name )
  1332. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1333. p2 = skip_space(p);
  1334. if( *p2 == ';' )
  1335. {// function <name> ;
  1336. // function declaration - just register the name
  1337. int l;
  1338. l = add_word(func_name);
  1339. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1340. str_data[l].type = C_USERFUNC;
  1341. // if, for , while の閉じ判定
  1342. p = parse_syntax_close(p2 + 1);
  1343. return p; }
  1344. else if(*p2 == '{')
  1345. {// function <name> <line/block of code>
  1346. char label[256];
  1347. int l;
  1348. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1349. syntax.curly[syntax.curly_count].count = 1;
  1350. syntax.curly[syntax.curly_count].index = syntax.index++;
  1351. syntax.curly[syntax.curly_count].flag = 0;
  1352. ++syntax.curly_count;
  1353. // Jump over the function code
  1354. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1355. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1356. ++syntax.curly_count;
  1357. parse_line(label);
  1358. --syntax.curly_count;
  1359. // Set the position of the function (label)
  1360. l=add_word(func_name);
  1361. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1362. str_data[l].type = C_USERFUNC;
  1363. set_label(l, script_pos, p);
  1364. if( parse_options&SCRIPT_USE_LABEL_DB )
  1365. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1366. return skip_space(p);
  1367. }
  1368. else
  1369. {
  1370. disp_error_message("expect ';' or '{' at function syntax",p);
  1371. }
  1372. }
  1373. break;
  1374. case 'i':
  1375. case 'I':
  1376. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1377. // if() の処理
  1378. char label[256];
  1379. p=skip_space(p2);
  1380. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1381. disp_error_message("need '('",p);
  1382. }
  1383. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1384. syntax.curly[syntax.curly_count].count = 1;
  1385. syntax.curly[syntax.curly_count].index = syntax.index++;
  1386. syntax.curly[syntax.curly_count].flag = 0;
  1387. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1388. syntax.curly_count++;
  1389. add_scriptl(add_str("jump_zero"));
  1390. add_scriptc(C_ARG);
  1391. p=parse_expr(p);
  1392. p=skip_space(p);
  1393. add_scriptl(add_str(label));
  1394. add_scriptc(C_FUNC);
  1395. return p;
  1396. }
  1397. break;
  1398. case 's':
  1399. case 'S':
  1400. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1401. // switch() の処理
  1402. char label[256];
  1403. p=skip_space(p2);
  1404. if(*p != '(') {
  1405. disp_error_message("need '('",p);
  1406. }
  1407. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1408. syntax.curly[syntax.curly_count].count = 1;
  1409. syntax.curly[syntax.curly_count].index = syntax.index++;
  1410. syntax.curly[syntax.curly_count].flag = 0;
  1411. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1412. syntax.curly_count++;
  1413. add_scriptl(add_str("set"));
  1414. add_scriptc(C_ARG);
  1415. add_scriptl(add_str(label));
  1416. p=parse_expr(p);
  1417. p=skip_space(p);
  1418. if(*p != '{') {
  1419. disp_error_message("parse_syntax: need '{'",p);
  1420. }
  1421. add_scriptc(C_FUNC);
  1422. return p + 1;
  1423. }
  1424. break;
  1425. case 'w':
  1426. case 'W':
  1427. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1428. int l;
  1429. char label[256];
  1430. p=skip_space(p2);
  1431. if(*p != '(') {
  1432. disp_error_message("need '('",p);
  1433. }
  1434. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1435. syntax.curly[syntax.curly_count].count = 1;
  1436. syntax.curly[syntax.curly_count].index = syntax.index++;
  1437. syntax.curly[syntax.curly_count].flag = 0;
  1438. // 条件判断開始のラベル形成する
  1439. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1440. l=add_str(label);
  1441. set_label(l,script_pos,p);
  1442. // 条件が偽なら終了地点に飛ばす
  1443. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1444. syntax.curly_count++;
  1445. add_scriptl(add_str("jump_zero"));
  1446. add_scriptc(C_ARG);
  1447. p=parse_expr(p);
  1448. p=skip_space(p);
  1449. add_scriptl(add_str(label));
  1450. add_scriptc(C_FUNC);
  1451. return p;
  1452. }
  1453. break;
  1454. }
  1455. return NULL;
  1456. }
  1457. const char* parse_syntax_close(const char *p) {
  1458. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1459. int flag;
  1460. do {
  1461. p = parse_syntax_close_sub(p,&flag);
  1462. } while(flag);
  1463. return p;
  1464. }
  1465. // if, for , while , do の閉じ判定
  1466. // flag == 1 : 閉じられた
  1467. // flag == 0 : 閉じられない
  1468. const char* parse_syntax_close_sub(const char* p,int* flag)
  1469. {
  1470. char label[256];
  1471. int pos = syntax.curly_count - 1;
  1472. int l;
  1473. *flag = 1;
  1474. if(syntax.curly_count <= 0) {
  1475. *flag = 0;
  1476. return p;
  1477. } else if(syntax.curly[pos].type == TYPE_IF) {
  1478. const char *bp = p;
  1479. const char *p2;
  1480. // if 最終場所へ飛ばす
  1481. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1482. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1483. parse_line(label);
  1484. syntax.curly_count--;
  1485. // 現在地のラベルを付ける
  1486. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1487. l=add_str(label);
  1488. set_label(l,script_pos,p);
  1489. syntax.curly[pos].count++;
  1490. p = skip_space(p);
  1491. p2 = skip_word(p);
  1492. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1493. // else or else - if
  1494. p = skip_space(p2);
  1495. p2 = skip_word(p);
  1496. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1497. // else - if
  1498. p=skip_space(p2);
  1499. if(*p != '(') {
  1500. disp_error_message("need '('",p);
  1501. }
  1502. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1503. add_scriptl(add_str("jump_zero"));
  1504. add_scriptc(C_ARG);
  1505. p=parse_expr(p);
  1506. p=skip_space(p);
  1507. add_scriptl(add_str(label));
  1508. add_scriptc(C_FUNC);
  1509. *flag = 0;
  1510. return p;
  1511. } else {
  1512. // else
  1513. if(!syntax.curly[pos].flag) {
  1514. syntax.curly[pos].flag = 1;
  1515. *flag = 0;
  1516. return p;
  1517. }
  1518. }
  1519. }
  1520. // if 閉じ
  1521. syntax.curly_count--;
  1522. // 最終地のラベルを付ける
  1523. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1524. l=add_str(label);
  1525. set_label(l,script_pos,p);
  1526. if(syntax.curly[pos].flag == 1) {
  1527. // このifに対するelseじゃないのでポインタの位置は同じ
  1528. return bp;
  1529. }
  1530. return p;
  1531. } else if(syntax.curly[pos].type == TYPE_DO) {
  1532. int l;
  1533. char label[256];
  1534. const char *p2;
  1535. if(syntax.curly[pos].flag) {
  1536. // 現在地のラベル形成する(continue でここに来る)
  1537. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1538. l=add_str(label);
  1539. set_label(l,script_pos,p);
  1540. }
  1541. // 条件が偽なら終了地点に飛ばす
  1542. p = skip_space(p);
  1543. p2 = skip_word(p);
  1544. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1545. disp_error_message("parse_syntax: need 'while'",p);
  1546. p = skip_space(p2);
  1547. if(*p != '(') {
  1548. disp_error_message("need '('",p);
  1549. }
  1550. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1551. add_scriptl(add_str("jump_zero"));
  1552. add_scriptc(C_ARG);
  1553. p=parse_expr(p);
  1554. p=skip_space(p);
  1555. add_scriptl(add_str(label));
  1556. add_scriptc(C_FUNC);
  1557. // 開始地点に飛ばす
  1558. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1559. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1560. parse_line(label);
  1561. syntax.curly_count--;
  1562. // 条件終了地点のラベル形成する
  1563. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1564. l=add_str(label);
  1565. set_label(l,script_pos,p);
  1566. p = skip_space(p);
  1567. if(*p != ';') {
  1568. disp_error_message("parse_syntax: need ';'",p);
  1569. return p+1;
  1570. }
  1571. p++;
  1572. syntax.curly_count--;
  1573. return p;
  1574. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1575. // 次のループに飛ばす
  1576. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1577. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1578. parse_line(label);
  1579. syntax.curly_count--;
  1580. // for 終了のラベル付け
  1581. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1582. l=add_str(label);
  1583. set_label(l,script_pos,p);
  1584. syntax.curly_count--;
  1585. return p;
  1586. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1587. // while 条件判断へ飛ばす
  1588. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1589. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1590. parse_line(label);
  1591. syntax.curly_count--;
  1592. // while 終了のラベル付け
  1593. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1594. l=add_str(label);
  1595. set_label(l,script_pos,p);
  1596. syntax.curly_count--;
  1597. return p;
  1598. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1599. int pos = syntax.curly_count-1;
  1600. char label[256];
  1601. int l;
  1602. // 戻す
  1603. sprintf(label,"return;");
  1604. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1605. parse_line(label);
  1606. syntax.curly_count--;
  1607. // 現在地のラベルを付ける
  1608. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1609. l=add_str(label);
  1610. set_label(l,script_pos,p);
  1611. syntax.curly_count--;
  1612. return p;
  1613. } else {
  1614. *flag = 0;
  1615. return p;
  1616. }
  1617. }
  1618. /*==========================================
  1619. * 組み込み関数の追加
  1620. *------------------------------------------*/
  1621. static void add_buildin_func(void)
  1622. {
  1623. int i,n;
  1624. const char* p;
  1625. for( i = 0; buildin_func[i].func; i++ )
  1626. {
  1627. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1628. // 'v' - value (either string or int or reference)
  1629. // 's' - string
  1630. // 'i' - int
  1631. // 'r' - reference (of a variable)
  1632. // 'l' - label
  1633. // '?' - one optional parameter
  1634. // '*' - unknown number of optional parameters
  1635. p = buildin_func[i].arg;
  1636. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1637. while( *p == '?' ) ++p;
  1638. if( *p == '*' ) ++p;
  1639. if( *p != 0){
  1640. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1641. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1642. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1643. } else {
  1644. n = add_str(buildin_func[i].name);
  1645. str_data[n].type = C_FUNC;
  1646. str_data[n].val = i;
  1647. str_data[n].func = buildin_func[i].func;
  1648. }
  1649. }
  1650. }
  1651. /*==========================================
  1652. * 定数データベースの読み込み
  1653. *------------------------------------------*/
  1654. static void read_constdb(void)
  1655. {
  1656. FILE *fp;
  1657. char line[1024],name[1024],val[1024];
  1658. int n,type;
  1659. sprintf(line, "%s/const.txt", db_path);
  1660. fp=fopen(line, "r");
  1661. if(fp==NULL){
  1662. ShowError("can't read %s\n", line);
  1663. return ;
  1664. }
  1665. while(fgets(line, sizeof(line), fp))
  1666. {
  1667. if(line[0]=='/' && line[1]=='/')
  1668. continue;
  1669. type=0;
  1670. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1671. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1672. n=add_str(name);
  1673. if(type==0)
  1674. str_data[n].type=C_INT;
  1675. else
  1676. str_data[n].type=C_PARAM;
  1677. str_data[n].val= (int)strtol(val,NULL,0);
  1678. }
  1679. }
  1680. fclose(fp);
  1681. }
  1682. /*==========================================
  1683. * エラー表示
  1684. *------------------------------------------*/
  1685. const char* script_print_line( const char *p, const char *mark, int line )
  1686. {
  1687. int i;
  1688. if( p == NULL || !p[0] ) return NULL;
  1689. if( line < 0 )
  1690. ShowMessage("*% 5d : ", -line);
  1691. else
  1692. ShowMessage(" % 5d : ", line);
  1693. for(i=0;p[i] && p[i] != '\n';i++){
  1694. if(p + i != mark)
  1695. ShowMessage("%c",p[i]);
  1696. else
  1697. ShowMessage("\'%c\'",p[i]);
  1698. }
  1699. ShowMessage("\n");
  1700. return p+i+(p[i] == '\n' ? 1 : 0);
  1701. }
  1702. void script_error(const char *src,const char *file,int start_line, const char *error_msg, const char *error_pos)
  1703. {
  1704. // エラーが発生した行を求める
  1705. int j;
  1706. int line = start_line;
  1707. const char *p;
  1708. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1709. for(p=src;p && *p;line++){
  1710. char *lineend=strchr(p,'\n');
  1711. if(lineend==NULL || error_pos<lineend){
  1712. break;
  1713. }
  1714. for( j = 0; j < 4; j++ ) {
  1715. linestart[j] = linestart[j+1];
  1716. }
  1717. linestart[4] = p;
  1718. p=lineend+1;
  1719. }
  1720. ShowMessage("\a\n");
  1721. ShowMessage("script error on %s line %d\n", file, line);
  1722. ShowMessage(" %s\n", error_msg);
  1723. for(j = 0; j < 5; j++ ) {
  1724. script_print_line( linestart[j], NULL, line + j - 5);
  1725. }
  1726. p = script_print_line( p, error_pos, -line);
  1727. for(j = 0; j < 5; j++) {
  1728. p = script_print_line( p, NULL, line + j + 1 );
  1729. }
  1730. }
  1731. /*==========================================
  1732. * スクリプトの解析
  1733. *------------------------------------------*/
  1734. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1735. {
  1736. const char *p,*tmpp;
  1737. int i;
  1738. struct script_code* code = NULL;
  1739. static int first=1;
  1740. char end;
  1741. if( src == NULL )
  1742. return NULL;// empty script
  1743. memset(&syntax,0,sizeof(syntax));
  1744. if(first){
  1745. add_buildin_func();
  1746. read_constdb();
  1747. first=0;
  1748. }
  1749. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1750. script_pos=0;
  1751. script_size=SCRIPT_BLOCK_SIZE;
  1752. str_data[LABEL_NEXTLINE].type=C_NOP;
  1753. str_data[LABEL_NEXTLINE].backpatch=-1;
  1754. str_data[LABEL_NEXTLINE].label=-1;
  1755. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1756. if( options&SCRIPT_USE_LABEL_DB )
  1757. scriptlabel_db->clear(scriptlabel_db, NULL);
  1758. parse_options = options;
  1759. if( setjmp( error_jump ) != 0 ) {
  1760. //Restore program state when script has problems. [from jA]
  1761. int i;
  1762. const int size = ARRAYLENGTH(syntax.curly);
  1763. if( error_report )
  1764. script_error(src,file,line,error_msg,error_pos);
  1765. aFree( error_msg );
  1766. aFree( script_buf );
  1767. script_pos = 0;
  1768. script_size = 0;
  1769. script_buf = NULL;
  1770. for(i=LABEL_START;i<str_num;i++)
  1771. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1772. for(i=0; i<size; i++)
  1773. linkdb_final(&syntax.curly[i].case_label);
  1774. return NULL;
  1775. }
  1776. parse_syntax_for_flag=0;
  1777. p=src;
  1778. p=skip_space(p);
  1779. if( options&SCRIPT_IGNORE_EXTERNAL_BRACKETS )
  1780. {// does not require brackets around the script
  1781. if( *p == '\0' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1782. {// empty script and can return NULL
  1783. aFree( script_buf );
  1784. script_pos = 0;
  1785. script_size = 0;
  1786. script_buf = NULL;
  1787. return NULL;
  1788. }
  1789. end = '\0';
  1790. }
  1791. else
  1792. {// requires brackets around the script
  1793. if( *p != '{' )
  1794. disp_error_message("not found '{'",p);
  1795. p = skip_space(p+1);
  1796. if( *p == '}' && !(options&SCRIPT_RETURN_EMPTY_SCRIPT) )
  1797. {// empty script and can return NULL
  1798. aFree( script_buf );
  1799. script_pos = 0;
  1800. script_size = 0;
  1801. script_buf = NULL;
  1802. return NULL;
  1803. }
  1804. end = '}';
  1805. }
  1806. // clear references of labels, variables and internal functions
  1807. for(i=LABEL_START;i<str_num;i++){
  1808. if(
  1809. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1810. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1811. ){
  1812. str_data[i].type=C_NOP;
  1813. str_data[i].backpatch=-1;
  1814. str_data[i].label=-1;
  1815. }
  1816. }
  1817. while( syntax.curly_count != 0 || *p != end )
  1818. {
  1819. if( *p == '\0' )
  1820. disp_error_message("unexpected end of script",p);
  1821. // labelだけ特殊処理
  1822. tmpp=skip_space(skip_word(p));
  1823. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1824. i=add_word(p);
  1825. set_label(i,script_pos,p);
  1826. if( parse_options&SCRIPT_USE_LABEL_DB )
  1827. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1828. p=tmpp+1;
  1829. p=skip_space(p);
  1830. continue;
  1831. }
  1832. // 他は全部一緒くた
  1833. p=parse_line(p);
  1834. p=skip_space(p);
  1835. add_scriptc(C_EOL);
  1836. set_label(LABEL_NEXTLINE,script_pos,p);
  1837. str_data[LABEL_NEXTLINE].type=C_NOP;
  1838. str_data[LABEL_NEXTLINE].backpatch=-1;
  1839. str_data[LABEL_NEXTLINE].label=-1;
  1840. }
  1841. add_scriptc(C_NOP);
  1842. // trim code to size
  1843. script_size = script_pos;
  1844. RECREATE(script_buf,unsigned char,script_pos);
  1845. // default unknown references to variables
  1846. for(i=LABEL_START;i<str_num;i++){
  1847. if(str_data[i].type==C_NOP){
  1848. int j,next;
  1849. str_data[i].type=C_NAME;
  1850. str_data[i].label=i;
  1851. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1852. next=GETVALUE(script_buf,j);
  1853. SETVALUE(script_buf,j,i);
  1854. j=next;
  1855. }
  1856. }
  1857. }
  1858. #ifdef DEBUG_DISP
  1859. for(i=0;i<script_pos;i++){
  1860. if((i&15)==0) printf("%04x : ",i);
  1861. ShowMessage("%02x ",script_buf[i]);
  1862. if((i&15)==15) printf("\n");
  1863. }
  1864. ShowMessage("\n");
  1865. #endif
  1866. #ifdef DEBUG_DISASM
  1867. {
  1868. int i = 0,j;
  1869. while(i < script_pos) {
  1870. ShowMessage("%06x ",i);
  1871. j = i;
  1872. switch(get_com(script_buf,&i)) {
  1873. case C_EOL: printf("C_EOL"); break;
  1874. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1875. case C_POS:
  1876. ShowMessage("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1877. i += 3;
  1878. break;
  1879. case C_NAME:
  1880. j = (*(int*)(script_buf+i)&0xffffff);
  1881. ShowMessage("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1882. i += 3;
  1883. break;
  1884. case C_ARG: ShowMessage("C_ARG"); break;
  1885. case C_FUNC: ShowMessage("C_FUNC"); break;
  1886. case C_ADD: ShowMessage("C_ADD"); break;
  1887. case C_SUB: ShowMessage("C_SUB"); break;
  1888. case C_MUL: ShowMessage("C_MUL"); break;
  1889. case C_DIV: ShowMessage("C_DIV"); break;
  1890. case C_MOD: ShowMessage("C_MOD"); break;
  1891. case C_EQ: ShowMessage("C_EQ"); break;
  1892. case C_NE: ShowMessage("C_NE"); break;
  1893. case C_GT: ShowMessage("C_GT"); break;
  1894. case C_GE: ShowMessage("C_GE"); break;
  1895. case C_LT: ShowMessage("C_LT"); break;
  1896. case C_LE: ShowMessage("C_LE"); break;
  1897. case C_AND: ShowMessage("C_AND"); break;
  1898. case C_OR: ShowMessage("C_OR"); break;
  1899. case C_XOR: ShowMessage("C_XOR"); break;
  1900. case C_LAND: ShowMessage("C_LAND"); break;
  1901. case C_LOR: ShowMessage("C_LOR"); break;
  1902. case C_R_SHIFT: ShowMessage("C_R_SHIFT"); break;
  1903. case C_L_SHIFT: ShowMessage("C_L_SHIFT"); break;
  1904. case C_NEG: ShowMessage("C_NEG"); break;
  1905. case C_NOT: ShowMessage("C_NOT"); break;
  1906. case C_LNOT: ShowMessage("C_LNOT"); break;
  1907. case C_NOP: ShowMessage("C_NOP"); break;
  1908. case C_OP3: ShowMessage("C_OP3"); break;
  1909. case C_STR:
  1910. j = strlen(script_buf + i);
  1911. ShowMessage("C_STR %s",script_buf + i);
  1912. i+= j+1;
  1913. break;
  1914. default:
  1915. ShowMessage("unknown");
  1916. }
  1917. ShowMessage("\n");
  1918. }
  1919. }
  1920. #endif
  1921. CREATE(code,struct script_code,1);
  1922. code->script_buf = script_buf;
  1923. code->script_size = script_size;
  1924. code->script_vars = NULL;
  1925. return code;
  1926. }
  1927. //
  1928. // Script state
  1929. //
  1930. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1931. /// Returns the player attached to this script, identified by the rid.
  1932. /// If there is no player attached, the script is terminated.
  1933. TBL_PC *script_rid2sd(struct script_state *st)
  1934. {
  1935. TBL_PC *sd=map_id2sd(st->rid);
  1936. if(!sd){
  1937. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1938. script_reportsrc(st);
  1939. st->state = END;
  1940. }
  1941. return sd;
  1942. }
  1943. /// Dereferences a variable/constant, replacing it with a copy of the value.
  1944. ///
  1945. /// @param st Script state
  1946. /// @param data Variable/constant
  1947. void get_val(struct script_state* st, struct script_data* data)
  1948. {
  1949. char* name;
  1950. char prefix;
  1951. char postfix;
  1952. TBL_PC* sd = NULL;
  1953. if( !data_isreference(data) )
  1954. return;// not a variable/constant
  1955. name = reference_getname(data);
  1956. prefix = name[0];
  1957. postfix = name[strlen(name) - 1];
  1958. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1959. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1960. {
  1961. sd = script_rid2sd(st);
  1962. if( sd == NULL )
  1963. {// needs player attached
  1964. if( postfix == '$' )
  1965. {// string variable
  1966. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1967. data->type = C_CONSTSTR;
  1968. data->u.str = "";
  1969. }
  1970. else
  1971. {// integer variable
  1972. ShowWarning("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1973. data->type = C_INT;
  1974. data->u.num = 0;
  1975. }
  1976. return;
  1977. }
  1978. }
  1979. if( postfix == '$' )
  1980. {// string variable
  1981. switch( prefix )
  1982. {
  1983. case '@':
  1984. data->u.str = pc_readregstr(sd, data->u.num);
  1985. break;
  1986. case '$':
  1987. data->u.str = (char *)idb_get(mapregstr_db, data->u.num);
  1988. break;
  1989. case '#':
  1990. if( name[1] == '#' )
  1991. data->u.str = pc_readaccountreg2str(sd, name);// global
  1992. else
  1993. data->u.str = pc_readaccountregstr(sd, name);// local
  1994. break;
  1995. case '.':
  1996. {
  1997. struct linkdb_node** n =
  1998. data->ref ? data->ref:
  1999. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2000. &st->script->script_vars;// npc variable
  2001. data->u.str = linkdb_search(n, (void*)reference_getuid(data));
  2002. }
  2003. break;
  2004. default:
  2005. data->u.str = pc_readglobalreg_str(sd, name);
  2006. break;
  2007. }
  2008. if( data->u.str == NULL || data->u.str[0] == '\0' )
  2009. {// empty string
  2010. data->type = C_CONSTSTR;
  2011. data->u.str = "";
  2012. }
  2013. else
  2014. {// duplicate string
  2015. data->type = C_STR;
  2016. data->u.str = aStrdup(data->u.str);
  2017. }
  2018. }
  2019. else
  2020. {// integer variable
  2021. data->type = C_INT;
  2022. if( reference_toconstant(data) )
  2023. {
  2024. data->u.num = reference_getconstant(data);
  2025. }
  2026. else if( reference_toparam(data) )
  2027. {
  2028. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2029. }
  2030. else
  2031. switch( prefix )
  2032. {
  2033. case '@':
  2034. data->u.num = pc_readreg(sd, data->u.num);
  2035. break;
  2036. case '$':
  2037. data->u.num = (int)idb_get(mapreg_db, data->u.num);
  2038. break;
  2039. case '#':
  2040. if( name[1] == '#' )
  2041. data->u.num = pc_readaccountreg2(sd, name);// global
  2042. else
  2043. data->u.num = pc_readaccountreg(sd, name);// local
  2044. break;
  2045. case '.':
  2046. {
  2047. struct linkdb_node** n =
  2048. data->ref ? data->ref:
  2049. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2050. &st->script->script_vars;// npc variable
  2051. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2052. }
  2053. break;
  2054. default:
  2055. data->u.num = pc_readglobalreg(sd, name);
  2056. break;
  2057. }
  2058. }
  2059. return;
  2060. }
  2061. void push_val2(struct script_stack* stack, int type, int val, struct linkdb_node** ref);
  2062. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2063. /// The value is left in the top of the stack and needs to be removed manually.
  2064. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2065. {
  2066. struct script_data* data;
  2067. push_val2(st->stack, C_NAME, uid, ref);
  2068. data = script_getdatatop(st, -1);
  2069. get_val(st, data);
  2070. return (data->type == C_INT ? (void*)data->u.num : (void*)data->u.str);
  2071. }
  2072. /*==========================================
  2073. * Stores the value of a script variable
  2074. * Return value is 0 on fail, 1 on success.
  2075. *------------------------------------------*/
  2076. static int set_reg(struct script_state* st, TBL_PC* sd, int num, char* name, const void* value, struct linkdb_node** ref)
  2077. {
  2078. char prefix = name[0]; char postfix = name[strlen(name)-1];
  2079. if (postfix == '$') { // string variable
  2080. const char* str = (const char*)value;
  2081. switch (prefix) {
  2082. case '@':
  2083. return pc_setregstr(sd, num, str);
  2084. case '$':
  2085. return mapreg_setregstr(num, str);
  2086. case '#':
  2087. return (name[1] == '#') ?
  2088. pc_setaccountreg2str(sd, name, str) :
  2089. pc_setaccountregstr(sd, name, str);
  2090. case '.': {
  2091. char* p;
  2092. struct linkdb_node** n;
  2093. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2094. p = linkdb_erase(n, (void*)num);
  2095. if (p) aFree(p);
  2096. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2097. }
  2098. return 1;
  2099. default:
  2100. return pc_setglobalreg_str(sd, name, str);
  2101. }
  2102. } else { // integer variable
  2103. int val = (int)value;
  2104. if(str_data[num&0x00ffffff].type == C_PARAM)
  2105. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  2106. switch (prefix) {
  2107. case '@':
  2108. return pc_setreg(sd, num, val);
  2109. case '$':
  2110. return mapreg_setreg(num, val);
  2111. case '#':
  2112. return (name[1] == '#') ?
  2113. pc_setaccountreg2(sd, name, val) :
  2114. pc_setaccountreg(sd, name, val);
  2115. case '.': {
  2116. struct linkdb_node** n;
  2117. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2118. if (val == 0)
  2119. linkdb_erase(n, (void*)num);
  2120. else
  2121. linkdb_replace(n, (void*)num, (void*)val);
  2122. }
  2123. return 1;
  2124. default:
  2125. return pc_setglobalreg(sd, name, val);
  2126. }
  2127. }
  2128. }
  2129. int set_var(TBL_PC* sd, char* name, void* val)
  2130. {
  2131. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  2132. }
  2133. /// Converts the data to a string
  2134. const char* conv_str(struct script_state* st, struct script_data* data)
  2135. {
  2136. char* p;
  2137. get_val(st, data);
  2138. if( data_isstring(data) )
  2139. {// nothing to convert
  2140. }
  2141. else if( data_isint(data) )
  2142. {// int -> string
  2143. CREATE(p, char, ITEM_NAME_LENGTH);
  2144. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2145. p[ITEM_NAME_LENGTH-1] = '\0';
  2146. data->type = C_STR;
  2147. data->u.str = p;
  2148. }
  2149. else if( data_isreference(data) )
  2150. {// reference -> string
  2151. //##TODO when does this happen (check get_val) [FlavioJS]
  2152. data->type = C_CONSTSTR;
  2153. data->u.str = reference_getname(data);
  2154. }
  2155. else
  2156. {// unsupported data type
  2157. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2158. script_reportdata(data);
  2159. script_reportsrc(st);
  2160. data->type = C_CONSTSTR;
  2161. data->u.str = "";
  2162. }
  2163. return data->u.str;
  2164. }
  2165. /// Converts the data to an int
  2166. int conv_num(struct script_state* st, struct script_data* data)
  2167. {
  2168. char* p;
  2169. long num;
  2170. get_val(st, data);
  2171. if( data_isint(data) )
  2172. {// nothing to convert
  2173. }
  2174. else if( data_isstring(data) )
  2175. {// string -> int
  2176. // the result does not overflow or underflow, it is capped instead
  2177. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2178. p = data->u.str;
  2179. errno = 0;
  2180. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2181. if( errno == ERANGE
  2182. #if LONG_MAX > INT_MAX
  2183. || num < INT_MIN || num > INT_MAX
  2184. #endif
  2185. )
  2186. {
  2187. if( num <= INT_MIN )
  2188. {
  2189. num = INT_MIN;
  2190. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2191. }
  2192. else//if( num >= INT_MAX )
  2193. {
  2194. num = INT_MAX;
  2195. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2196. }
  2197. script_reportdata(data);
  2198. script_reportsrc(st);
  2199. }
  2200. if( data->type == C_STR )
  2201. aFree(p);
  2202. data->type = C_INT;
  2203. data->u.num = (int)num;
  2204. }
  2205. #if 0
  2206. // FIXME this function is being used to retrieve the position of labels and
  2207. // probably other stuff [FlavioJS]
  2208. else
  2209. {// unsupported data type
  2210. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2211. script_reportdata(data);
  2212. script_reportsrc(st);
  2213. data->type = C_INT;
  2214. data->u.num = 0;
  2215. }
  2216. #endif
  2217. return data->u.num;
  2218. }
  2219. //
  2220. // Stack operations
  2221. //
  2222. /// Increases the size of the stack
  2223. void stack_expand(struct script_stack* stack)
  2224. {
  2225. stack->sp_max += 64;
  2226. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2227. stack->sp_max * sizeof(stack->stack_data[0]) );
  2228. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2229. 64 * sizeof(stack->stack_data[0]) );
  2230. }
  2231. /// Pushes a value into the stack
  2232. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2233. /// Pushes a value into the stack (with reference)
  2234. void push_val2(struct script_stack* stack, int type, int val, struct linkdb_node** ref)
  2235. {
  2236. if( stack->sp >= stack->sp_max )
  2237. stack_expand(stack);
  2238. stack->stack_data[stack->sp].type = type;
  2239. stack->stack_data[stack->sp].u.num = val;
  2240. stack->stack_data[stack->sp].ref = ref;
  2241. stack->sp++;
  2242. }
  2243. /// Pushes a string into the stack
  2244. void push_str(struct script_stack* stack, int type, char* str)
  2245. {
  2246. if( stack->sp >= stack->sp_max )
  2247. stack_expand(stack);
  2248. stack->stack_data[stack->sp].type = type;
  2249. stack->stack_data[stack->sp].u.str = str;
  2250. stack->stack_data[stack->sp].ref = NULL;
  2251. stack->sp++;
  2252. }
  2253. /// Pushes a copy of the target position into the stack
  2254. void push_copy(struct script_stack* stack, int pos)
  2255. {
  2256. switch( stack->stack_data[pos].type )
  2257. {
  2258. case C_CONSTSTR:
  2259. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2260. break;
  2261. case C_STR:
  2262. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2263. break;
  2264. default:
  2265. push_val2(
  2266. stack,stack->stack_data[pos].type,
  2267. stack->stack_data[pos].u.num,
  2268. stack->stack_data[pos].ref
  2269. );
  2270. break;
  2271. }
  2272. }
  2273. /// Removes the values in indexes [start,end[ from the stack
  2274. void pop_stack(struct script_stack* stack, int start, int end)
  2275. {
  2276. struct script_data* data;
  2277. int i;
  2278. if( start < 0 )
  2279. start = 0;
  2280. if( end > stack->sp_max )
  2281. end = stack->sp_max;
  2282. if( start >= end )
  2283. return;// nothing to pop
  2284. // free stack elements
  2285. for( i = start; i < end; i++ )
  2286. {
  2287. data = &stack->stack_data[i];
  2288. if( data->type == C_STR )
  2289. aFree(data->u.str);
  2290. data->type = C_NOP;
  2291. }
  2292. // move the rest of the elements
  2293. if( stack->sp > end )
  2294. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2295. stack->sp -= end - start;
  2296. }
  2297. ///
  2298. ///
  2299. ///
  2300. /*==========================================
  2301. * スクリプト依存変数、関数依存変数の解放
  2302. *------------------------------------------*/
  2303. void script_free_vars(struct linkdb_node **node)
  2304. {
  2305. struct linkdb_node *n = *node;
  2306. while(n) {
  2307. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2308. char postfix = name[strlen(name)-1];
  2309. if( postfix == '$' ) {
  2310. // 文字型変数なので、データ削除
  2311. aFree(n->data);
  2312. }
  2313. n = n->next;
  2314. }
  2315. linkdb_final( node );
  2316. }
  2317. /*==========================================
  2318. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2319. *------------------------------------------*/
  2320. void script_free_stack(struct script_stack *stack)
  2321. {
  2322. int i;
  2323. for(i = 0; i < stack->sp; i++) {
  2324. if( stack->stack_data[i].type == C_STR ) {
  2325. aFree(stack->stack_data[i].u.str);
  2326. stack->stack_data[i].type = C_INT;
  2327. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2328. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2329. script_free_vars( n );
  2330. aFree( n );
  2331. }
  2332. }
  2333. script_free_vars( stack->var_function );
  2334. aFree(stack->var_function);
  2335. aFree(stack->stack_data);
  2336. aFree(stack);
  2337. }
  2338. void script_free_code(struct script_code* code)
  2339. {
  2340. script_free_vars( &code->script_vars );
  2341. aFree( code->script_buf );
  2342. aFree( code );
  2343. }
  2344. //
  2345. // 実行部main
  2346. //
  2347. /*==========================================
  2348. * コマンドの読み取り
  2349. *------------------------------------------*/
  2350. static int unget_com_data=-1;
  2351. int get_com(unsigned char *script,int *pos)
  2352. {
  2353. int i,j;
  2354. if(unget_com_data>=0){
  2355. i=unget_com_data;
  2356. unget_com_data=-1;
  2357. return i;
  2358. }
  2359. if(script[*pos]>=0x80){
  2360. return C_INT;
  2361. }
  2362. i=0; j=0;
  2363. while(script[*pos]>=0x40){
  2364. i=script[(*pos)++]<<j;
  2365. j+=6;
  2366. }
  2367. return i+(script[(*pos)++]<<j);
  2368. }
  2369. /*==========================================
  2370. * コマンドのプッシュバック
  2371. *------------------------------------------*/
  2372. void unget_com(int c)
  2373. {
  2374. if(unget_com_data!=-1)
  2375. ShowError("unget_com can back only 1 data\n");
  2376. unget_com_data=c;
  2377. }
  2378. /*==========================================
  2379. * 数値の所得
  2380. *------------------------------------------*/
  2381. int get_num(unsigned char *script,int *pos)
  2382. {
  2383. int i,j;
  2384. i=0; j=0;
  2385. while(script[*pos]>=0xc0){
  2386. i+=(script[(*pos)++]&0x7f)<<j;
  2387. j+=6;
  2388. }
  2389. return i+((script[(*pos)++]&0x7f)<<j);
  2390. }
  2391. /*==========================================
  2392. * スタックから値を取り出す
  2393. *------------------------------------------*/
  2394. int pop_val(struct script_state* st)
  2395. {
  2396. if(st->stack->sp<=0)
  2397. return 0;
  2398. st->stack->sp--;
  2399. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2400. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2401. return st->stack->stack_data[st->stack->sp].u.num;
  2402. return 0;
  2403. }
  2404. int isstr(struct script_data *c)
  2405. {
  2406. if( data_isstring(c) )
  2407. return 1;
  2408. else if( data_isreference(c) ) {
  2409. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2410. char postfix = p[strlen(p)-1];
  2411. return (postfix == '$');
  2412. }
  2413. return 0;
  2414. }
  2415. /// Ternary operators
  2416. /// test ? if_true : if_false
  2417. void op_3(struct script_state* st, int op)
  2418. {
  2419. struct script_data* data;
  2420. int flag = 0;
  2421. data = script_getdatatop(st, -3);
  2422. get_val(st, data);
  2423. if( data_isstring(data) )
  2424. flag = data->u.str[0];// "" -> false
  2425. else if( data_isint(data) )
  2426. flag = data->u.num;// 0 -> false
  2427. else
  2428. {
  2429. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2430. script_reportdata(data);
  2431. script_reportsrc(st);
  2432. script_removetop(st, -3, 0);
  2433. script_pushnil(st);
  2434. return;
  2435. }
  2436. if( flag )
  2437. script_pushcopytop(st, -2);
  2438. else
  2439. script_pushcopytop(st, -1);
  2440. script_removetop(st, -4, -1);
  2441. }
  2442. /// Binary string operators
  2443. /// s1 EQ s2 -> i
  2444. /// s1 NE s2 -> i
  2445. /// s1 GT s2 -> i
  2446. /// s1 GE s2 -> i
  2447. /// s1 LT s2 -> i
  2448. /// s1 LE s2 -> i
  2449. /// s1 ADD s2 -> s
  2450. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2451. {
  2452. int a = 0;
  2453. switch(op){
  2454. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2455. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2456. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2457. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2458. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2459. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2460. case C_ADD:
  2461. {
  2462. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2463. strcpy(buf, s1);
  2464. strcat(buf, s2);
  2465. script_pushstr(st, buf);
  2466. return;
  2467. }
  2468. default:
  2469. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2470. script_reportsrc(st);
  2471. script_pushnil(st);
  2472. st->state = END;
  2473. return;
  2474. }
  2475. script_pushint(st,a);
  2476. }
  2477. /// Binary number operators
  2478. /// i OP i -> i
  2479. void op_2num(struct script_state* st, int op, int i1, int i2)
  2480. {
  2481. int ret;
  2482. double ret_double;
  2483. switch( op )
  2484. {
  2485. case C_AND: ret = i1 & i2; break;
  2486. case C_OR: ret = i1 | i2; break;
  2487. case C_XOR: ret = i1 ^ i2; break;
  2488. case C_LAND: ret = (i1 && i2); break;
  2489. case C_LOR: ret = (i1 || i2); break;
  2490. case C_EQ: ret = (i1 == i2); break;
  2491. case C_NE: ret = (i1 != i2); break;
  2492. case C_GT: ret = (i1 > i2); break;
  2493. case C_GE: ret = (i1 >= i2); break;
  2494. case C_LT: ret = (i1 < i2); break;
  2495. case C_LE: ret = (i1 <= i2); break;
  2496. case C_R_SHIFT: ret = i1>>i2; break;
  2497. case C_L_SHIFT: ret = i1<<i2; break;
  2498. case C_DIV:
  2499. case C_MOD:
  2500. if( i2 == 0 )
  2501. {
  2502. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2503. script_reportsrc(st);
  2504. script_pushnil(st);
  2505. st->state = END;
  2506. return;
  2507. }
  2508. else if( op == C_DIV )
  2509. ret = i1 / i2;
  2510. else//if( op == C_MOD )
  2511. ret = i1 % i2;
  2512. break;
  2513. default:
  2514. switch( op )
  2515. {// operators that can overflow/underflow
  2516. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2517. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2518. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2519. default:
  2520. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2521. script_reportsrc(st);
  2522. script_pushnil(st);
  2523. return;
  2524. }
  2525. if( ret_double < (double)INT_MIN )
  2526. {
  2527. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2528. script_reportsrc(st);
  2529. ret = INT_MIN;
  2530. }
  2531. else if( ret_double > (double)INT_MAX )
  2532. {
  2533. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2534. script_reportsrc(st);
  2535. ret = INT_MAX;
  2536. }
  2537. }
  2538. script_pushint(st, ret);
  2539. }
  2540. /// Binary operators
  2541. void op_2(struct script_state *st, int op)
  2542. {
  2543. struct script_data* left;
  2544. struct script_data* right;
  2545. left = script_getdatatop(st, -2);
  2546. right = script_getdatatop(st, -1);
  2547. get_val(st, left);
  2548. get_val(st, right);
  2549. // automatic conversions
  2550. switch( op )
  2551. {
  2552. case C_ADD:
  2553. if( data_isint(left) && data_isstring(right) )
  2554. {// convert int-string to string-string
  2555. conv_str(st, left);
  2556. }
  2557. else if( data_isstring(left) && data_isint(right) )
  2558. {// convert string-int to string-string
  2559. conv_str(st, right);
  2560. }
  2561. break;
  2562. }
  2563. if( data_isstring(left) && data_isstring(right) )
  2564. {// ss => op_2str
  2565. op_2str(st, op, left->u.str, right->u.str);
  2566. script_removetop(st, -3, -1);// pop the two values before the top one
  2567. }
  2568. else if( data_isint(left) && data_isint(right) )
  2569. {// ii => op_2num
  2570. int i1 = left->u.num;
  2571. int i2 = right->u.num;
  2572. script_removetop(st, -2, 0);
  2573. op_2num(st, op, i1, i2);
  2574. }
  2575. else
  2576. {// invalid argument
  2577. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2578. script_reportdata(left);
  2579. script_reportdata(right);
  2580. script_reportsrc(st);
  2581. script_removetop(st, -2, 0);
  2582. script_pushnil(st);
  2583. st->state = END;
  2584. }
  2585. }
  2586. /// Unary operators
  2587. /// NEG i -> i
  2588. /// NOT i -> i
  2589. /// LNOT i -> i
  2590. void op_1(struct script_state* st, int op)
  2591. {
  2592. struct script_data* data;
  2593. int i1;
  2594. data = script_getdatatop(st, -1);
  2595. get_val(st, data);
  2596. if( !data_isint(data) )
  2597. {// not a number
  2598. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2599. script_reportdata(data);
  2600. script_reportsrc(st);
  2601. script_pushnil(st);
  2602. st->state = END;
  2603. return;
  2604. }
  2605. i1 = data->u.num;
  2606. script_removetop(st, -1, 0);
  2607. switch( op )
  2608. {
  2609. case C_NEG: i1 = -i1; break;
  2610. case C_NOT: i1 = ~i1; break;
  2611. case C_LNOT: i1 = !i1; break;
  2612. default:
  2613. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2614. script_reportsrc(st);
  2615. script_pushnil(st);
  2616. st->state = END;
  2617. return;
  2618. }
  2619. script_pushint(st, i1);
  2620. }
  2621. /*==========================================
  2622. * 関数の実行
  2623. *------------------------------------------*/
  2624. int run_func(struct script_state *st)
  2625. {
  2626. int i,start_sp,end_sp,func;
  2627. end_sp=st->stack->sp;
  2628. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2629. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2630. ShowError("function not found\n");
  2631. // st->stack->sp=0;
  2632. st->state=END;
  2633. script_reportsrc(st);
  2634. return 1;
  2635. }
  2636. start_sp=i-1;
  2637. st->start=i-1;
  2638. st->end=end_sp;
  2639. func=st->stack->stack_data[st->start].u.num;
  2640. #ifdef DEBUG_RUN
  2641. if(battle_config.etc_log) {
  2642. ShowDebug("run_func : %s? (%d(%d)) sp=%d (%d...%d)\n",str_buf+str_data[func].str, func, str_data[func].type, st->stack->sp, st->start, st->end);
  2643. ShowDebug("stack dump :");
  2644. for(i=0;i<end_sp;i++){
  2645. switch(st->stack->stack_data[i].type){
  2646. case C_INT:
  2647. ShowMessage(" int(%d)",st->stack->stack_data[i].u.num);
  2648. break;
  2649. case C_NAME:
  2650. ShowMessage(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2651. break;
  2652. case C_ARG:
  2653. ShowMessage(" arg");
  2654. break;
  2655. case C_POS:
  2656. ShowMessage(" pos(%d)",st->stack->stack_data[i].u.num);
  2657. break;
  2658. case C_STR:
  2659. ShowMessage(" str(%s)",st->stack->stack_data[i].u.str);
  2660. break;
  2661. case C_CONSTSTR:
  2662. ShowMessage(" cstr(%s)",st->stack->stack_data[i].u.str);
  2663. break;
  2664. default:
  2665. ShowMessage(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2666. }
  2667. }
  2668. ShowMessage("\n");
  2669. }
  2670. #endif
  2671. if(str_data[func].type!=C_FUNC ){
  2672. ShowError("run_func: '"CL_WHITE"%s"CL_RESET"' (type %d) is not function and command!\n", str_buf+str_data[func].str, str_data[func].type);
  2673. // st->stack->sp=0;
  2674. st->state=END;
  2675. script_reportsrc(st);
  2676. return 1;
  2677. }
  2678. #ifdef DEBUG_RUN
  2679. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2680. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2681. );
  2682. #endif
  2683. if(str_data[func].func){
  2684. if (str_data[func].func(st)) //Report error
  2685. script_reportsrc(st);
  2686. } else {
  2687. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2688. script_pushint(st,0);
  2689. script_reportsrc(st);
  2690. }
  2691. // Stack's datum are used when re-run functions [Eoe]
  2692. if(st->state != RERUNLINE) {
  2693. pop_stack(st->stack,start_sp,end_sp);
  2694. }
  2695. if(st->state==RETFUNC){
  2696. // ユーザー定義関数からの復帰
  2697. int olddefsp=st->stack->defsp;
  2698. int i;
  2699. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2700. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2701. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2702. st->state=END;
  2703. script_reportsrc(st);
  2704. return 1;
  2705. }
  2706. script_free_vars( st->stack->var_function );
  2707. aFree(st->stack->var_function);
  2708. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2709. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2710. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2711. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2712. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2713. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2714. st->state=GOTO;
  2715. }
  2716. return 0;
  2717. }
  2718. /*==========================================
  2719. * スクリプトの実行
  2720. *------------------------------------------*/
  2721. void run_script_main(struct script_state *st);
  2722. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2723. {
  2724. struct script_state *st;
  2725. TBL_PC *sd=NULL;
  2726. if(rootscript==NULL || pos<0)
  2727. return;
  2728. if (rid) sd = map_id2sd(rid);
  2729. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2730. //Resume script.
  2731. st = sd->st;
  2732. } else {
  2733. st = aCalloc(sizeof(struct script_state), 1);
  2734. // the script is different, make new script_state and stack
  2735. st->stack = aMalloc (sizeof(struct script_stack));
  2736. st->stack->sp=0;
  2737. st->stack->sp_max=64;
  2738. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2739. st->stack->defsp = st->stack->sp;
  2740. st->stack->var_function = aCalloc(1, sizeof(struct linkdb_node*));
  2741. st->state = RUN;
  2742. st->script = rootscript;
  2743. }
  2744. st->pos = pos;
  2745. st->rid = rid;
  2746. st->oid = oid;
  2747. st->sleep.timer = INVALID_TIMER;
  2748. run_script_main(st);
  2749. }
  2750. void script_stop_sleeptimers(int id)
  2751. {
  2752. struct script_state* st;
  2753. for(;;)
  2754. {
  2755. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2756. if( st == NULL )
  2757. break; // no more sleep timers
  2758. if( st->sleep.timer != INVALID_TIMER )
  2759. delete_timer(st->sleep.timer, run_script_timer);
  2760. script_free_stack(st->stack);
  2761. aFree(st);
  2762. }
  2763. }
  2764. /*==========================================
  2765. * 指定ノードをsleep_dbから削除
  2766. *------------------------------------------*/
  2767. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2768. {
  2769. struct linkdb_node *retnode;
  2770. if( n == NULL)
  2771. return NULL;
  2772. if( n->prev == NULL )
  2773. sleep_db = n->next;
  2774. else
  2775. n->prev->next = n->next;
  2776. if( n->next )
  2777. n->next->prev = n->prev;
  2778. retnode = n->next;
  2779. aFree( n );
  2780. return retnode; // 次のノードを返す
  2781. }
  2782. /*==========================================
  2783. * sleep用タイマー関数
  2784. *------------------------------------------*/
  2785. int run_script_timer(int tid, unsigned int tick, int id, int data)
  2786. {
  2787. struct script_state *st = (struct script_state *)data;
  2788. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2789. TBL_PC *sd = map_id2sd(st->rid);
  2790. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2791. { //Character mismatch. Cancel execution.
  2792. st->rid = 0;
  2793. st->state = END;
  2794. }
  2795. while( node && st->sleep.timer != -1 ) {
  2796. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2797. script_erase_sleepdb(node);
  2798. st->sleep.timer = -1;
  2799. break;
  2800. }
  2801. node = node->next;
  2802. }
  2803. if(st->state != RERUNLINE)
  2804. st->sleep.tick = 0;
  2805. run_script_main(st);
  2806. return 0;
  2807. }
  2808. /*==========================================
  2809. * スクリプトの実行メイン部分
  2810. *------------------------------------------*/
  2811. void run_script_main(struct script_state *st)
  2812. {
  2813. int c;
  2814. int cmdcount=script_config.check_cmdcount;
  2815. int gotocount=script_config.check_gotocount;
  2816. TBL_PC *sd;
  2817. //For backing up purposes
  2818. struct script_state *bk_st = NULL;
  2819. int bk_npcid = 0;
  2820. struct script_stack *stack=st->stack;
  2821. sd = st->rid?map_id2sd(st->rid):NULL;
  2822. if(sd){
  2823. if(sd->st != st){
  2824. bk_st = sd->st;
  2825. bk_npcid = sd->npc_id;
  2826. }
  2827. sd->st = st;
  2828. sd->npc_id = st->oid;
  2829. }
  2830. if(st->state == RERUNLINE) {
  2831. st->state = RUN;
  2832. run_func(st);
  2833. if(st->state == GOTO)
  2834. st->state = RUN;
  2835. } else if(st->state != END)
  2836. st->state = RUN;
  2837. while(st->state == RUN){
  2838. c= get_com(st->script->script_buf,&st->pos);
  2839. switch(c){
  2840. case C_EOL:
  2841. if( stack->sp != stack->defsp )
  2842. {
  2843. if( stack->sp > stack->defsp )
  2844. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2845. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2846. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2847. } else
  2848. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2849. stack->sp = stack->defsp;
  2850. }
  2851. break;
  2852. case C_INT:
  2853. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2854. break;
  2855. case C_POS:
  2856. case C_NAME:
  2857. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2858. st->pos+=3;
  2859. break;
  2860. case C_ARG:
  2861. push_val(stack,c,0);
  2862. break;
  2863. case C_STR:
  2864. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2865. while(st->script->script_buf[st->pos++]);
  2866. break;
  2867. case C_FUNC:
  2868. run_func(st);
  2869. if(st->state==GOTO){
  2870. st->state = RUN;
  2871. if( gotocount>0 && (--gotocount)<=0 ){
  2872. ShowError("run_script: infinity loop !\n");
  2873. script_reportsrc(st);
  2874. st->state=END;
  2875. }
  2876. }
  2877. break;
  2878. case C_NEG:
  2879. case C_NOT:
  2880. case C_LNOT:
  2881. op_1(st ,c);
  2882. break;
  2883. case C_ADD:
  2884. case C_SUB:
  2885. case C_MUL:
  2886. case C_DIV:
  2887. case C_MOD:
  2888. case C_EQ:
  2889. case C_NE:
  2890. case C_GT:
  2891. case C_GE:
  2892. case C_LT:
  2893. case C_LE:
  2894. case C_AND:
  2895. case C_OR:
  2896. case C_XOR:
  2897. case C_LAND:
  2898. case C_LOR:
  2899. case C_R_SHIFT:
  2900. case C_L_SHIFT:
  2901. op_2(st, c);
  2902. break;
  2903. case C_OP3:
  2904. op_3(st, c);
  2905. break;
  2906. case C_NOP:
  2907. st->state=END;
  2908. break;
  2909. default:
  2910. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2911. st->state=END;
  2912. break;
  2913. }
  2914. if( cmdcount>0 && (--cmdcount)<=0 ){
  2915. ShowError("run_script: infinity loop !\n");
  2916. script_reportsrc(st);
  2917. st->state=END;
  2918. }
  2919. }
  2920. if(st->sleep.tick > 0) {
  2921. //Delay execution
  2922. st->sleep.charid = sd?sd->status.char_id:0;
  2923. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2924. run_script_timer, st->sleep.charid, (int)st);
  2925. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2926. //Restore previous script
  2927. if (sd) {
  2928. sd->st = bk_st;
  2929. sd->npc_id = bk_npcid;
  2930. bk_st = NULL; //Remove tag for removal.
  2931. }
  2932. }
  2933. else if(st->state != END && sd){
  2934. //Resume later (st is already attached to player).
  2935. if(bk_st) {
  2936. ShowWarning("Unable to restore stack! Double continuation!\n");
  2937. //Report BOTH scripts to see if that can help somehow.
  2938. ShowDebug("Previous script (lost):\n");
  2939. script_reportsrc(bk_st);
  2940. ShowDebug("Current script:\n");
  2941. script_reportsrc(st);
  2942. }
  2943. } else {
  2944. //Dispose of script.
  2945. if (sd)
  2946. { //Restore previous stack and save char.
  2947. if(sd->state.using_fake_npc){
  2948. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2949. sd->state.using_fake_npc = 0;
  2950. }
  2951. //Restore previous script if any.
  2952. sd->st = bk_st;
  2953. sd->npc_id = bk_npcid;
  2954. if (!bk_st)
  2955. npc_event_dequeue(sd);
  2956. else
  2957. bk_st = NULL; //Remove tag for removal.
  2958. if (sd->state.reg_dirty&2)
  2959. intif_saveregistry(sd,2);
  2960. if (sd->state.reg_dirty&1)
  2961. intif_saveregistry(sd,1);
  2962. }
  2963. st->pos = -1;
  2964. script_free_stack (st->stack);
  2965. aFree(st);
  2966. }
  2967. if (bk_st)
  2968. { //Remove previous script
  2969. bk_st->pos = -1;
  2970. script_free_stack(bk_st->stack);
  2971. aFree(bk_st);
  2972. bk_st = NULL;
  2973. }
  2974. }
  2975. /*==========================================
  2976. * マップ変数の変更
  2977. *------------------------------------------*/
  2978. int mapreg_setreg(int num, int val)
  2979. {
  2980. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2981. int i = num >> 24;
  2982. char* name = str_buf + str_data[num&0x00ffffff].str;
  2983. if( val != 0 ) {
  2984. if(idb_put(mapreg_db,num,(void*)val))
  2985. ;
  2986. else if(name[1] != '@') {
  2987. char tmp_str[32*2+1];
  2988. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  2989. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%d')", mapregsql_db, mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, tmp_str, i, val) )
  2990. Sql_ShowDebug(mmysql_handle);
  2991. }
  2992. } else { // val == 0
  2993. if(name[1] != '@') { // Remove from database because it is unused.
  2994. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_varname, name, mapregsql_db_index, i) )
  2995. Sql_ShowDebug(mmysql_handle);
  2996. }
  2997. idb_remove(mapreg_db,num);
  2998. }
  2999. #else
  3000. if(val != 0)
  3001. idb_put(mapreg_db,num,(void*)val);
  3002. else
  3003. idb_remove(mapreg_db,num);
  3004. #endif
  3005. mapreg_dirty = 1;
  3006. return 1;
  3007. }
  3008. /*==========================================
  3009. * 文字列型マップ変数の変更
  3010. *------------------------------------------*/
  3011. int mapreg_setregstr(int num, const char* str)
  3012. {
  3013. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  3014. int i = num >> 24;
  3015. char* name = str_buf + str_data[num&0x00ffffff].str;
  3016. if( str==NULL || *str==0 ) {
  3017. if(name[1] != '@') {
  3018. if( SQL_ERROR == Sql_Query(mmysql_handle, "DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3019. Sql_ShowDebug(mmysql_handle);
  3020. }
  3021. idb_remove(mapregstr_db,num);
  3022. mapreg_dirty = 1;
  3023. return 1;
  3024. }
  3025. if (idb_put(mapregstr_db,num, aStrdup(str)))
  3026. ;
  3027. else if(name[1] != '@') { //put returned null, so we must insert.
  3028. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  3029. char tmp_str[32*2+1];
  3030. char tmp_str2[255*2+1];
  3031. Sql_EscapeStringLen(mmysql_handle, tmp_str, name, strnlen(name, 32));
  3032. Sql_EscapeStringLen(mmysql_handle, tmp_str2, str, strnlen(str, 255));
  3033. if( SQL_ERROR == Sql_Query(mmysql_handle, "INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%s')", mapregsql_db, mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, tmp_str, i, tmp_str2) )
  3034. Sql_ShowDebug(mmysql_handle);
  3035. }
  3036. #else
  3037. if( str==NULL || *str==0 )
  3038. idb_remove(mapregstr_db,num);
  3039. else
  3040. idb_put(mapregstr_db,num,aStrdup(str));
  3041. #endif
  3042. mapreg_dirty = 1;
  3043. return 1;
  3044. }
  3045. /*==========================================
  3046. * 永続的マップ変数の読み込み
  3047. *------------------------------------------*/
  3048. static int script_load_mapreg(void)
  3049. {
  3050. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3051. FILE* fp;
  3052. char line[1024];
  3053. if( (fp=fopen(mapreg_txt,"rt")) == NULL )
  3054. return -1;
  3055. while(fgets(line,sizeof(line),fp))
  3056. {
  3057. char buf1[256],buf2[1024];
  3058. char* p;
  3059. int n,v,s,i;
  3060. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  3061. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  3062. continue;
  3063. if( buf1[strlen(buf1)-1] == '$' ) {
  3064. if( sscanf(line + n, "%[^\n\r]", buf2) != 1 ) {
  3065. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3066. continue;
  3067. }
  3068. p = aStrdup(buf2);
  3069. s = add_str(buf1);
  3070. idb_put(mapregstr_db, (i<<24)|s, p);
  3071. } else {
  3072. if( sscanf(line + n, "%d", &v) != 1 ) {
  3073. ShowError("%s: %s broken data !\n", mapreg_txt, buf1);
  3074. continue;
  3075. }
  3076. s = add_str(buf1);
  3077. idb_put(mapreg_db, (i<<24)|s, (void*)v);
  3078. }
  3079. }
  3080. fclose(fp);
  3081. mapreg_dirty = 0;
  3082. return 0;
  3083. #else
  3084. /*
  3085. 0 1 2
  3086. +-------------------------+
  3087. | varname | index | value |
  3088. +-------------------------+
  3089. */
  3090. SqlStmt* stmt = SqlStmt_Malloc(mmysql_handle);
  3091. char varname[32+1];
  3092. int index;
  3093. char value[255+1];
  3094. uint32 length;
  3095. if ( SQL_ERROR == SqlStmt_Prepare(stmt, "SELECT `%s`, `%s`, `%s` FROM `%s`", mapregsql_db_varname, mapregsql_db_index, mapregsql_db_value, mapregsql_db)
  3096. || SQL_ERROR == SqlStmt_Execute(stmt)
  3097. ) {
  3098. SqlStmt_ShowDebug(stmt);
  3099. SqlStmt_Free(stmt);
  3100. return -1;
  3101. }
  3102. SqlStmt_BindColumn(stmt, 0, SQLDT_STRING, &varname[0], 32, &length, NULL);
  3103. SqlStmt_BindColumn(stmt, 1, SQLDT_INT, &index, 0, NULL, NULL);
  3104. SqlStmt_BindColumn(stmt, 2, SQLDT_STRING, &value[0], 255, NULL, NULL);
  3105. while ( SQL_SUCCESS == SqlStmt_NextRow(stmt) )
  3106. {
  3107. if( varname[length-1] == '$' ) {
  3108. int s = add_str(varname);
  3109. int i = index;
  3110. char* p = aStrdup(value);
  3111. idb_put(mapregstr_db, (i<<24)|s, p);
  3112. } else {
  3113. int s = add_str(varname);
  3114. int i = index;
  3115. int v = atoi(value);
  3116. idb_put(mapreg_db, (i<<24)|s, (void *)v);
  3117. }
  3118. }
  3119. SqlStmt_Free(stmt);
  3120. mapreg_dirty = 0;
  3121. return 0;
  3122. #endif /* TXT_ONLY */
  3123. }
  3124. /*==========================================
  3125. * 永続的マップ変数の書き込み
  3126. *------------------------------------------*/
  3127. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  3128. {
  3129. int num=key.i&0x00ffffff, i=key.i>>24;
  3130. char *name=str_buf+str_data[num].str;
  3131. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3132. FILE *fp=va_arg(ap,FILE*);
  3133. if( name[1]!='@' ) {
  3134. if(i==0)
  3135. fprintf(fp,"%s\t%d\n", name, (int)data);
  3136. else
  3137. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  3138. }
  3139. #else
  3140. if ( name[1] != '@') {
  3141. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `%s`='%d' WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_value, (int)data, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3142. Sql_ShowDebug(mmysql_handle);
  3143. }
  3144. #endif
  3145. return 0;
  3146. }
  3147. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  3148. {
  3149. int num=key.i&0x00ffffff, i=key.i>>24;
  3150. char *name=str_buf+str_data[num].str;
  3151. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3152. FILE *fp=va_arg(ap,FILE*);
  3153. if( name[1]!='@' ) {
  3154. if(i==0)
  3155. fprintf(fp,"%s\t%s\n", name, (char *)data);
  3156. else
  3157. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  3158. }
  3159. #else
  3160. if ( name[1] != '@') {
  3161. char tmp_str2[2*255+1];
  3162. Sql_EscapeStringLen(mmysql_handle, tmp_str2, (char*)data, safestrnlen((char*)data, 255));
  3163. if( SQL_ERROR == Sql_Query(mmysql_handle, "UPDATE `%s` SET `%s`='%s' WHERE `%s`='%s' AND `%s`='%d'", mapregsql_db, mapregsql_db_value, tmp_str2, mapregsql_db_varname, name, mapregsql_db_index, i) )
  3164. Sql_ShowDebug(mmysql_handle);
  3165. }
  3166. #endif
  3167. return 0;
  3168. }
  3169. static int script_save_mapreg(void)
  3170. {
  3171. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3172. FILE *fp;
  3173. int lock;
  3174. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  3175. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  3176. return -1;
  3177. }
  3178. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  3179. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  3180. lock_fclose(fp,mapreg_txt,&lock);
  3181. #else
  3182. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub);
  3183. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  3184. #endif
  3185. mapreg_dirty=0;
  3186. return 0;
  3187. }
  3188. static int script_autosave_mapreg(int tid,unsigned int tick,int id,int data)
  3189. {
  3190. if(mapreg_dirty)
  3191. if (script_save_mapreg() == -1)
  3192. ShowError("Failed to save the mapreg data!\n");
  3193. return 0;
  3194. }
  3195. int script_config_read(char *cfgName)
  3196. {
  3197. int i;
  3198. char line[1024],w1[1024],w2[1024];
  3199. FILE *fp;
  3200. fp=fopen(cfgName,"r");
  3201. if(fp==NULL){
  3202. ShowError("file not found: [%s]\n", cfgName);
  3203. return 1;
  3204. }
  3205. while(fgets(line, sizeof(line), fp))
  3206. {
  3207. if(line[0] == '/' && line[1] == '/')
  3208. continue;
  3209. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3210. if(i!=2)
  3211. continue;
  3212. if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3213. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3214. }
  3215. else if(strcmpi(w1,"check_cmdcount")==0) {
  3216. script_config.check_cmdcount = config_switch(w2);
  3217. }
  3218. else if(strcmpi(w1,"check_gotocount")==0) {
  3219. script_config.check_gotocount = config_switch(w2);
  3220. }
  3221. else if(strcmpi(w1,"input_min_value")==0) {
  3222. script_config.input_min_value = config_switch(w2);
  3223. }
  3224. else if(strcmpi(w1,"input_max_value")==0) {
  3225. script_config.input_max_value = config_switch(w2);
  3226. }
  3227. else if(strcmpi(w1,"import")==0){
  3228. script_config_read(w2);
  3229. }
  3230. }
  3231. fclose(fp);
  3232. return 0;
  3233. }
  3234. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3235. {
  3236. struct script_code *code = (struct script_code *)data;
  3237. if(code){
  3238. script_free_vars( &code->script_vars );
  3239. aFree( code->script_buf );
  3240. aFree( code );
  3241. }
  3242. return 0;
  3243. }
  3244. /*==========================================
  3245. * 終了
  3246. *------------------------------------------*/
  3247. int do_final_script()
  3248. {
  3249. #ifdef DEBUG_HASH
  3250. if (battle_config.etc_log)
  3251. {
  3252. FILE *fp = fopen("hash_dump.txt","wt");
  3253. if(fp) {
  3254. int i,count[SCRIPT_HASH_SIZE];
  3255. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3256. int n=0;
  3257. int min=INT_MAX,max=0,zero=0;
  3258. double mean=0.0f;
  3259. double median=0.0f;
  3260. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3261. memset(count, 0, sizeof(count));
  3262. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3263. fprintf(fp,"---------------------------------------------------------------\n");
  3264. for(i=LABEL_START; i<str_num; i++) {
  3265. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3266. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3267. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3268. ++count[h];
  3269. }
  3270. fprintf(fp,"--------------------\n\n");
  3271. memset(count2, 0, sizeof(count2));
  3272. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3273. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3274. if(min > count[i])
  3275. min = count[i]; // minimun count of collision
  3276. if(max < count[i])
  3277. max = count[i]; // maximun count of collision
  3278. if(count[i] == 0)
  3279. zero++;
  3280. ++count2[count[i]];
  3281. }
  3282. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3283. for( i=min; i <= max; ++i ){
  3284. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3285. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3286. }
  3287. for( i=min; i <= max; ++i ){
  3288. n += count2[i];
  3289. if( n*2 >= SCRIPT_HASH_SIZE )
  3290. {
  3291. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3292. median = (i+i+1)/2.0f;
  3293. else
  3294. median = i;
  3295. break;
  3296. }
  3297. }
  3298. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3299. fclose(fp);
  3300. }
  3301. }
  3302. #endif
  3303. if(mapreg_dirty>=0)
  3304. script_save_mapreg();
  3305. mapreg_db->destroy(mapreg_db,NULL);
  3306. mapregstr_db->destroy(mapregstr_db,NULL);
  3307. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3308. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3309. if(sleep_db) {
  3310. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3311. while(n) {
  3312. struct script_state *st = (struct script_state *)n->data;
  3313. script_free_stack(st->stack);
  3314. aFree(st);
  3315. n = n->next;
  3316. }
  3317. linkdb_final(&sleep_db);
  3318. }
  3319. if (str_data)
  3320. aFree(str_data);
  3321. if (str_buf)
  3322. aFree(str_buf);
  3323. return 0;
  3324. }
  3325. /*==========================================
  3326. * 初期化
  3327. *------------------------------------------*/
  3328. int do_init_script()
  3329. {
  3330. mapreg_db= idb_alloc(DB_OPT_BASE);
  3331. mapregstr_db=idb_alloc(DB_OPT_RELEASE_DATA);
  3332. userfunc_db=strdb_alloc(DB_OPT_DUP_KEY,0);
  3333. scriptlabel_db=strdb_alloc(DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA,50);
  3334. script_load_mapreg();
  3335. add_timer_func_list(script_autosave_mapreg, "script_autosave_mapreg");
  3336. add_timer_interval(gettick() + MAPREG_AUTOSAVE_INTERVAL, script_autosave_mapreg, 0, 0, MAPREG_AUTOSAVE_INTERVAL);
  3337. return 0;
  3338. }
  3339. int script_reload()
  3340. {
  3341. if(mapreg_dirty>=0)
  3342. script_save_mapreg();
  3343. mapreg_db->clear(mapreg_db, NULL);
  3344. mapregstr_db->clear(mapregstr_db, NULL);
  3345. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3346. scriptlabel_db->clear(scriptlabel_db, NULL);
  3347. if(sleep_db) {
  3348. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3349. while(n) {
  3350. struct script_state *st = (struct script_state *)n->data;
  3351. if( st->sleep.timer != INVALID_TIMER )
  3352. delete_timer(st->sleep.timer, run_script_timer);
  3353. script_free_stack(st->stack);
  3354. aFree(st);
  3355. n = n->next;
  3356. }
  3357. linkdb_final(&sleep_db);
  3358. }
  3359. script_load_mapreg();
  3360. return 0;
  3361. }
  3362. //-----------------------------------------------------------------------------
  3363. // buildin functions
  3364. //
  3365. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3366. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3367. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3368. /////////////////////////////////////////////////////////////////////
  3369. // NPC interaction
  3370. //
  3371. /// Appends a message to the npc dialog.
  3372. /// If a dialog doesn't exist yet, one is created.
  3373. ///
  3374. /// mes "<message>";
  3375. BUILDIN_FUNC(mes)
  3376. {
  3377. TBL_PC* sd = script_rid2sd(st);
  3378. if( sd == NULL )
  3379. return 0;
  3380. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  3381. return 0;
  3382. }
  3383. /// Displays the button 'next' in the npc dialog.
  3384. /// The dialog text is cleared and the script continues when the button is pressed.
  3385. ///
  3386. /// next;
  3387. BUILDIN_FUNC(next)
  3388. {
  3389. TBL_PC* sd;
  3390. sd = script_rid2sd(st);
  3391. if( sd == NULL )
  3392. return 0;
  3393. st->state = STOP;
  3394. clif_scriptnext(sd, st->oid);
  3395. return 0;
  3396. }
  3397. /// Ends the script and displays the button 'close' on the npc dialog.
  3398. /// The dialog is closed when the button is pressed.
  3399. ///
  3400. /// close;
  3401. BUILDIN_FUNC(close)
  3402. {
  3403. TBL_PC* sd;
  3404. sd = script_rid2sd(st);
  3405. if( sd == NULL )
  3406. return 0;
  3407. st->state = END;
  3408. clif_scriptclose(sd, st->oid);
  3409. return 0;
  3410. }
  3411. /// Displays the button 'close' on the npc dialog.
  3412. /// The dialog is closed and the script continues when the button is pressed.
  3413. ///
  3414. /// close2;
  3415. BUILDIN_FUNC(close2)
  3416. {
  3417. TBL_PC* sd;
  3418. sd = script_rid2sd(st);
  3419. if( sd == NULL )
  3420. return 0;
  3421. st->state = STOP;
  3422. clif_scriptclose(sd, st->oid);
  3423. return 0;
  3424. }
  3425. /// Counts the number of valid and total number of options in 'str'
  3426. /// If max_count > 0 the counting stops when that valid option is reached
  3427. /// total is incremented for each option (NULL is supported)
  3428. static int menu_countoptions(const char* str, int max_count, int* total)
  3429. {
  3430. int count = 0;
  3431. int bogus_total;
  3432. if( total == NULL )
  3433. total = &bogus_total;
  3434. ++(*total);
  3435. // initial empty options
  3436. while( *str == ':' )
  3437. {
  3438. ++str;
  3439. ++(*total);
  3440. }
  3441. // count menu options
  3442. while( *str != '\0' )
  3443. {
  3444. ++count;
  3445. --max_count;
  3446. if( max_count == 0 )
  3447. break;
  3448. while( *str != ':' && *str != '\0' )
  3449. ++str;
  3450. while( *str == ':' )
  3451. {
  3452. ++str;
  3453. ++(*total);
  3454. }
  3455. }
  3456. return count;
  3457. }
  3458. /// Displays a menu with options and goes to the target label.
  3459. /// The script is stopped if cancel is pressed.
  3460. /// Options with no text are not displayed in the client.
  3461. ///
  3462. /// Options can be grouped together, separated by the character ':' in the text:
  3463. /// ex: menu "A:B:C",L_target;
  3464. /// All these options go to the specified target label.
  3465. ///
  3466. /// The index of the selected option is put in the variable @menu.
  3467. /// Indexes start with 1 and are consistent with grouped and empty options.
  3468. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  3469. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  3470. ///
  3471. /// NOTE: the client closes the npc dialog when cancel is pressed
  3472. ///
  3473. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  3474. BUILDIN_FUNC(menu)
  3475. {
  3476. int i;
  3477. const char* text;
  3478. TBL_PC* sd;
  3479. sd = script_rid2sd(st);
  3480. if( sd == NULL )
  3481. return 0;
  3482. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  3483. if( sd->state.menu_or_input == 0 )
  3484. {
  3485. struct StringBuf buf;
  3486. struct script_data* data;
  3487. if( script_lastdata(st) % 2 == 0 )
  3488. {// argument count is not even (1st argument is at index 2)
  3489. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  3490. st->state = END;
  3491. return 1;
  3492. }
  3493. StringBuf_Init(&buf);
  3494. for( i = 2, sd->npc_menu = 0; i < script_lastdata(st); i += 2 )
  3495. {
  3496. // menu options
  3497. text = script_getstr(st, i);
  3498. // target label
  3499. data = script_getdata(st, i+1);
  3500. if( !data_islabel(data) )
  3501. {// not a label
  3502. StringBuf_Destroy(&buf);
  3503. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  3504. script_reportdata(data);
  3505. st->state = END;
  3506. return 1;
  3507. }
  3508. // append option(s)
  3509. if( text[0] == '\0' )
  3510. continue;// empty string, ignore
  3511. if( sd->npc_menu > 0 )
  3512. StringBuf_AppendStr(&buf, ":");
  3513. StringBuf_AppendStr(&buf, text);
  3514. sd->npc_menu += menu_countoptions(text, 0, NULL);
  3515. }
  3516. st->state = RERUNLINE;
  3517. sd->state.menu_or_input = 1;
  3518. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3519. StringBuf_Destroy(&buf);
  3520. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  3521. }
  3522. else if( sd->npc_menu == 0xff )
  3523. {// Cancel was pressed
  3524. sd->state.menu_or_input = 0;
  3525. st->state = END;
  3526. }
  3527. else
  3528. {// goto target label
  3529. int menu = 0;
  3530. sd->state.menu_or_input = 0;
  3531. if( sd->npc_menu <= 0 )
  3532. {
  3533. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  3534. st->state = END;
  3535. return 1;
  3536. }
  3537. // get target label
  3538. for( i = 2; i < script_lastdata(st); i += 2 )
  3539. {
  3540. text = script_getstr(st, i);
  3541. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3542. if( sd->npc_menu <= 0 )
  3543. break;// entry found
  3544. }
  3545. if( sd->npc_menu > 0 )
  3546. {// Invalid selection
  3547. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  3548. st->state = END;
  3549. return 1;
  3550. }
  3551. if( !data_islabel(script_getdata(st, i + 1)) )
  3552. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  3553. ShowError("script:menu: unexpected data in label argument\n");
  3554. script_reportdata(script_getdata(st, i + 1));
  3555. st->state = END;
  3556. return 1;
  3557. }
  3558. pc_setreg(sd, add_str("@menu"), menu);
  3559. st->pos = script_getnum(st, i + 1);
  3560. st->state = GOTO;
  3561. }
  3562. return 0;
  3563. }
  3564. /// Displays a menu with options and returns the selected option.
  3565. /// Behaves like 'menu' without the target labels.
  3566. ///
  3567. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  3568. ///
  3569. /// @see menu
  3570. BUILDIN_FUNC(select)
  3571. {
  3572. int i;
  3573. const char* text;
  3574. TBL_PC* sd;
  3575. sd = script_rid2sd(st);
  3576. if( sd == NULL )
  3577. return 0;
  3578. if( sd->state.menu_or_input == 0 )
  3579. {
  3580. struct StringBuf buf;
  3581. StringBuf_Init(&buf);
  3582. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  3583. {
  3584. text = script_getstr(st, i);
  3585. if( sd->npc_menu > 0 )
  3586. StringBuf_AppendStr(&buf, ":");
  3587. StringBuf_AppendStr(&buf, text);
  3588. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  3589. }
  3590. st->state = RERUNLINE;
  3591. sd->state.menu_or_input = 1;
  3592. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3593. StringBuf_Destroy(&buf);
  3594. }
  3595. else if( sd->npc_menu == 0xff )
  3596. {// Cancel was pressed
  3597. sd->state.menu_or_input = 0;
  3598. st->state = END;
  3599. }
  3600. else
  3601. {// return selected option
  3602. int menu = 0;
  3603. sd->state.menu_or_input = 0;
  3604. for( i = 2; i <= script_lastdata(st); ++i )
  3605. {
  3606. text = script_getstr(st, i);
  3607. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3608. if( sd->npc_menu <= 0 )
  3609. break;// entry found
  3610. }
  3611. pc_setreg(sd, add_str("@menu"), menu);
  3612. script_pushint(st, menu);
  3613. }
  3614. return 0;
  3615. }
  3616. /// Displays a menu with options and returns the selected option.
  3617. /// Behaves like 'menu' without the target labels, except when cancel is
  3618. /// pressed.
  3619. /// When cancel is pressed, the script continues and 255 is returned.
  3620. ///
  3621. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  3622. ///
  3623. /// @see menu
  3624. BUILDIN_FUNC(prompt)
  3625. {
  3626. int i;
  3627. const char *text;
  3628. TBL_PC* sd;
  3629. sd = script_rid2sd(st);
  3630. if( sd == NULL )
  3631. return 0;
  3632. if( sd->state.menu_or_input == 0 )
  3633. {
  3634. struct StringBuf buf;
  3635. StringBuf_Init(&buf);
  3636. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  3637. {
  3638. text = script_getstr(st, i);
  3639. if( sd->npc_menu > 0 )
  3640. StringBuf_AppendStr(&buf, ":");
  3641. StringBuf_AppendStr(&buf, text);
  3642. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  3643. }
  3644. st->state = RERUNLINE;
  3645. sd->state.menu_or_input = 1;
  3646. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  3647. StringBuf_Destroy(&buf);
  3648. }
  3649. else if( sd->npc_menu == 0xff )
  3650. {// Cancel was pressed
  3651. sd->state.menu_or_input = 0;
  3652. pc_setreg(sd, add_str("@menu"), 0xff);
  3653. script_pushint(st, 0xff);
  3654. }
  3655. else
  3656. {// return selected option
  3657. int menu = 0;
  3658. sd->state.menu_or_input = 0;
  3659. for( i = 2; i <= script_lastdata(st); ++i )
  3660. {
  3661. text = script_getstr(st, i);
  3662. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  3663. if( sd->npc_menu <= 0 )
  3664. break;// entry found
  3665. }
  3666. pc_setreg(sd, add_str("@menu"), menu);
  3667. script_pushint(st, menu);
  3668. }
  3669. return 0;
  3670. }
  3671. /////////////////////////////////////////////////////////////////////
  3672. // ...
  3673. //
  3674. /// Jumps to the target script label.
  3675. ///
  3676. /// goto <label>;
  3677. BUILDIN_FUNC(goto)
  3678. {
  3679. if( !data_islabel(script_getdata(st,2)) )
  3680. {
  3681. ShowError("script:goto: not a label\n");
  3682. script_reportdata(script_getdata(st,2));
  3683. st->state = END;
  3684. return 1;
  3685. }
  3686. st->pos = script_getnum(st,2);
  3687. st->state = GOTO;
  3688. return 0;
  3689. }
  3690. /*==========================================
  3691. * ユーザー定義関数の呼び出し
  3692. *------------------------------------------*/
  3693. BUILDIN_FUNC(callfunc)
  3694. {
  3695. int i, j;
  3696. struct linkdb_node** oldval;
  3697. struct script_code *scr, *oldscr;
  3698. const char* str = script_getstr(st,2);
  3699. scr = strdb_get(userfunc_db, str);
  3700. if( !scr )
  3701. {
  3702. ShowError("script:callfunc: function not found! [%s]\n", str);
  3703. st->state = END;
  3704. return 1;
  3705. }
  3706. for( i = st->start+3, j = 0; i < st->end; i++, j++ )
  3707. push_copy(st->stack,i);
  3708. script_pushint(st,j); // 引数の数をプッシュ
  3709. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3710. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3711. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3712. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3713. oldscr = st->script;
  3714. oldval = st->stack->var_function;
  3715. st->pos = 0;
  3716. st->script = scr;
  3717. st->stack->defsp = st->start+5+j;
  3718. st->state = GOTO;
  3719. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3720. // ' 変数の引き継ぎ
  3721. for( i = 0; i < j; i++ )
  3722. {
  3723. struct script_data* s = script_getdatatop(st, -6-i);
  3724. if( data_isreference(s) && !s->ref )
  3725. {
  3726. char* name = str_buf + str_data[s->u.num&0x00ffffff].str;
  3727. // '@ 変数の引き継ぎ
  3728. if( name[0] == '.' && name[1] == '@' ) {
  3729. s->ref = oldval;
  3730. } else if( name[0] == '.' ) {
  3731. s->ref = &oldscr->script_vars;
  3732. }
  3733. }
  3734. }
  3735. return 0;
  3736. }
  3737. /*==========================================
  3738. * サブルーティンの呼び出し
  3739. *------------------------------------------*/
  3740. BUILDIN_FUNC(callsub)
  3741. {
  3742. int pos=script_getnum(st,2);
  3743. int i,j;
  3744. if(!data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2))) {
  3745. ShowError("script:callsub: argument is not a label\n");
  3746. script_reportdata(script_getdata(st,2));
  3747. st->state=END;
  3748. return 1;
  3749. } else {
  3750. struct linkdb_node **oldval = st->stack->var_function;
  3751. for(i=st->start+3,j=0;i<st->end;i++,j++)
  3752. push_copy(st->stack,i);
  3753. script_pushint(st,j); // 引数の数をプッシュ
  3754. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  3755. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  3756. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  3757. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  3758. st->pos=pos;
  3759. st->stack->defsp=st->start+5+j;
  3760. st->state=GOTO;
  3761. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  3762. // ' 変数の引き継ぎ
  3763. for(i = 0; i < j; i++) {
  3764. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  3765. if( data_isreference(s) && !s->ref ) {
  3766. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  3767. // '@ 変数の引き継ぎ
  3768. if( name[0] == '.' && name[1] == '@' ) {
  3769. s->ref = oldval;
  3770. }
  3771. }
  3772. }
  3773. }
  3774. return 0;
  3775. }
  3776. /// Retrieves an argument provided to callfunc/callsub.
  3777. /// If the argument doesn't exist
  3778. ///
  3779. /// getarg(<index>{,<default_value>}) -> <value>
  3780. BUILDIN_FUNC(getarg)
  3781. {
  3782. int idx;
  3783. int count;
  3784. int stsp;
  3785. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  3786. {
  3787. ShowError("script:getarg: no callfunc or callsub!\n");
  3788. st->state = END;
  3789. return 1;
  3790. }
  3791. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  3792. stsp = st->stack->defsp - count - 5;
  3793. idx = script_getnum(st,2);
  3794. if( idx < count )
  3795. push_copy(st->stack, stsp + idx);
  3796. else if( script_hasdata(st,3) )
  3797. script_pushcopy(st, 3);
  3798. else
  3799. {
  3800. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  3801. st->state = END;
  3802. return 1;
  3803. }
  3804. return 0;
  3805. }
  3806. /// Returns from the current function, optionaly returning a value from the functions.
  3807. /// Don't use outside script functions.
  3808. ///
  3809. /// return;
  3810. /// return <value>;
  3811. BUILDIN_FUNC(return)
  3812. {
  3813. if( script_hasdata(st,2) )
  3814. {// return value
  3815. struct script_data* data;
  3816. script_pushcopy(st, 2);
  3817. data = script_getdatatop(st, -1);
  3818. if( data_isreference(data) )
  3819. {
  3820. char* name = reference_getname(data);
  3821. if( name[0] == '.' && name[1] == '@' )
  3822. {// temporary script variable, convert to value
  3823. get_val(st, data);
  3824. if( data_isstring(data) )
  3825. {// duplicate the string
  3826. data->type = C_STR;
  3827. data->u.str = aStrdup(data->u.str);
  3828. }
  3829. }
  3830. else if( name[0] == '.' && !data->ref )
  3831. {// script variable, link to current script
  3832. data->ref = &st->script->script_vars;
  3833. }
  3834. }
  3835. }
  3836. else
  3837. {// no return value
  3838. script_pushnil(st);
  3839. }
  3840. st->state = RETFUNC;
  3841. return 0;
  3842. }
  3843. /// Returns a random number from 0 to <range>-1.
  3844. /// Or returns a random number from <min> to <max>.
  3845. /// If <min> is greater than <max>, their numbers are switched.
  3846. /// rand(<range>) -> <int>
  3847. /// rand(<min>,<max>) -> <int>
  3848. BUILDIN_FUNC(rand)
  3849. {
  3850. int range;
  3851. int min;
  3852. int max;
  3853. if( script_hasdata(st,3) )
  3854. {// min,max
  3855. min = script_getnum(st,2);
  3856. max = script_getnum(st,3);
  3857. if( max < min )
  3858. swap(min, max);
  3859. range = max - min + 1;
  3860. }
  3861. else
  3862. {// range
  3863. min = 0;
  3864. range = script_getnum(st,2);
  3865. }
  3866. if( range <= 1 )
  3867. script_pushint(st, min);
  3868. else
  3869. script_pushint(st, rand()%range + min);
  3870. return 0;
  3871. }
  3872. /*==========================================
  3873. *
  3874. *------------------------------------------*/
  3875. BUILDIN_FUNC(warp)
  3876. {
  3877. int ret;
  3878. int x,y;
  3879. const char* str;
  3880. TBL_PC* sd;
  3881. sd = script_rid2sd(st);
  3882. if( sd == NULL )
  3883. return 0;
  3884. str = script_getstr(st,2);
  3885. x = script_getnum(st,3);
  3886. y = script_getnum(st,4);
  3887. if(strcmp(str,"Random")==0)
  3888. ret = pc_randomwarp(sd,3);
  3889. else if(strcmp(str,"SavePoint")==0 || strcmp(str,"Save")==0)
  3890. ret = pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  3891. else
  3892. ret = pc_setpos(sd,mapindex_name2id(str),x,y,0);
  3893. if( ret ) {
  3894. ShowError("buildin_warp: moving player '%s' to \"%s\",%d,%d failed.\n", sd->status.name, str, x, y);
  3895. script_reportsrc(st);
  3896. }
  3897. return 0;
  3898. }
  3899. /*==========================================
  3900. * エリア指定ワープ
  3901. *------------------------------------------*/
  3902. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  3903. {
  3904. int x,y;
  3905. unsigned int map;
  3906. map=va_arg(ap, unsigned int);
  3907. x=va_arg(ap,int);
  3908. y=va_arg(ap,int);
  3909. if(map == 0)
  3910. pc_randomwarp((TBL_PC *)bl,3);
  3911. else
  3912. pc_setpos((TBL_PC *)bl,map,x,y,0);
  3913. return 0;
  3914. }
  3915. BUILDIN_FUNC(areawarp)
  3916. {
  3917. int x,y,m;
  3918. unsigned int index;
  3919. const char *str;
  3920. const char *mapname;
  3921. int x0,y0,x1,y1;
  3922. mapname=script_getstr(st,2);
  3923. x0=script_getnum(st,3);
  3924. y0=script_getnum(st,4);
  3925. x1=script_getnum(st,5);
  3926. y1=script_getnum(st,6);
  3927. str=script_getstr(st,7);
  3928. x=script_getnum(st,8);
  3929. y=script_getnum(st,9);
  3930. if( (m=map_mapname2mapid(mapname))< 0)
  3931. return 0;
  3932. if(strcmp(str,"Random")==0)
  3933. index = 0;
  3934. else if(!(index=mapindex_name2id(str)))
  3935. return 0;
  3936. map_foreachinarea(buildin_areawarp_sub,
  3937. m,x0,y0,x1,y1,BL_PC, index,x,y );
  3938. return 0;
  3939. }
  3940. /*==========================================
  3941. * warpchar [LuzZza]
  3942. * Useful for warp one player from
  3943. * another player npc-session.
  3944. * Using: warpchar "mapname",x,y,Char_ID;
  3945. *------------------------------------------*/
  3946. BUILDIN_FUNC(warpchar)
  3947. {
  3948. int x,y,a;
  3949. const char *str;
  3950. TBL_PC *sd;
  3951. str=script_getstr(st,2);
  3952. x=script_getnum(st,3);
  3953. y=script_getnum(st,4);
  3954. a=script_getnum(st,5);
  3955. sd = map_charid2sd(a);
  3956. if( sd == NULL )
  3957. return 0;
  3958. if(strcmp(str, "Random") == 0)
  3959. pc_randomwarp(sd, 3);
  3960. else
  3961. if(strcmp(str, "SavePoint") == 0)
  3962. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  3963. else
  3964. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  3965. return 0;
  3966. }
  3967. /*==========================================
  3968. * Warpparty - [Fredzilla]
  3969. * Syntax: warpparty "mapname",x,y,Party_ID;
  3970. *------------------------------------------*/
  3971. BUILDIN_FUNC(warpparty)
  3972. {
  3973. int x,y;
  3974. const char *str;
  3975. int p_id;
  3976. int i;
  3977. unsigned short mapindex;
  3978. TBL_PC *pl_sd;
  3979. struct party_data *p=NULL;
  3980. str=script_getstr(st,2);
  3981. x=script_getnum(st,3);
  3982. y=script_getnum(st,4);
  3983. p_id=script_getnum(st,5);
  3984. if(p_id < 1)
  3985. return 0;
  3986. p = party_search(p_id);
  3987. if (!p)
  3988. return 0;
  3989. if(strcmp(str,"Random")==0)
  3990. {
  3991. for (i = 0; i < MAX_PARTY; i++)
  3992. {
  3993. if ((pl_sd = p->data[i].sd))
  3994. {
  3995. if(map[pl_sd->bl.m].flag.nowarp)
  3996. continue;
  3997. pc_randomwarp(pl_sd,3);
  3998. }
  3999. }
  4000. }
  4001. else if(strcmp(str,"SavePointAll")==0)
  4002. {
  4003. for (i = 0; i < MAX_PARTY; i++)
  4004. {
  4005. if ((pl_sd = p->data[i].sd))
  4006. {
  4007. if(map[pl_sd->bl.m].flag.noreturn)
  4008. continue;
  4009. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4010. }
  4011. }
  4012. }
  4013. else if(strcmp(str,"SavePoint")==0)
  4014. {
  4015. pl_sd=script_rid2sd(st);
  4016. if (!pl_sd) return 0;
  4017. mapindex=pl_sd->status.save_point.map;
  4018. x=pl_sd->status.save_point.x;
  4019. y=pl_sd->status.save_point.y;
  4020. for (i = 0; i < MAX_PARTY; i++)
  4021. {
  4022. if ((pl_sd = p->data[i].sd))
  4023. {
  4024. if(map[pl_sd->bl.m].flag.noreturn)
  4025. continue;
  4026. pc_setpos(pl_sd,mapindex,x,y,3);
  4027. }
  4028. }
  4029. }
  4030. else if(strcmp(str,"Leader")==0)
  4031. {
  4032. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4033. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4034. return 0;
  4035. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  4036. return 0;
  4037. mapindex = p->data[i].sd->mapindex;
  4038. x = p->data[i].sd->bl.x;
  4039. y = p->data[i].sd->bl.y;
  4040. for (i = 0; i < MAX_PARTY; i++)
  4041. {
  4042. pl_sd = p->data[i].sd;
  4043. if (!pl_sd)
  4044. continue;
  4045. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4046. continue;
  4047. pc_setpos(pl_sd,mapindex,x,y,3);
  4048. }
  4049. }
  4050. else
  4051. {
  4052. mapindex = mapindex_name2id(str);
  4053. if (!mapindex) //Show source of npc error.
  4054. return 1;
  4055. for (i = 0; i < MAX_PARTY; i++)
  4056. {
  4057. if ((pl_sd = p->data[i].sd))
  4058. {
  4059. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4060. continue;
  4061. pc_setpos(pl_sd,mapindex,x,y,3);
  4062. }
  4063. }
  4064. }
  4065. return 0;
  4066. }
  4067. /*==========================================
  4068. * Warpguild - [Fredzilla]
  4069. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4070. *------------------------------------------*/
  4071. BUILDIN_FUNC(warpguild)
  4072. {
  4073. TBL_PC *sd;
  4074. TBL_PC *pl_sd;
  4075. struct guild* g;
  4076. struct s_mapiterator* iter;
  4077. int type;
  4078. const char* str = script_getstr(st,2);
  4079. int x = script_getnum(st,3);
  4080. int y = script_getnum(st,4);
  4081. int gid = script_getnum(st,5);
  4082. sd=script_rid2sd(st);
  4083. if( sd == NULL )
  4084. return 0;
  4085. g = guild_search(gid);
  4086. if( g == NULL )
  4087. return 0;
  4088. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4089. return 0;
  4090. type = ( strcmp(str,"Random")==0 ) ? 0
  4091. : ( strcmp(str,"SavePointAll")==0 ) ? 1
  4092. : ( strcmp(str,"SavePoint")==0 ) ? 2
  4093. : 3;
  4094. iter = mapit_getallusers();
  4095. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  4096. {
  4097. if( pl_sd->status.guild_id != gid )
  4098. continue;
  4099. switch( type )
  4100. {
  4101. case 0: // Random
  4102. if(!map[pl_sd->bl.m].flag.nowarp)
  4103. pc_randomwarp(pl_sd,3);
  4104. break;
  4105. case 1: // SavePointAll
  4106. if(!map[pl_sd->bl.m].flag.noreturn)
  4107. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4108. break;
  4109. case 2: // SavePoint
  4110. if(!map[pl_sd->bl.m].flag.noreturn)
  4111. pc_setpos(pl_sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  4112. break;
  4113. case 3: // m,x,y
  4114. if(!map[pl_sd->bl.m].flag.noreturn && !map[pl_sd->bl.m].flag.nowarp)
  4115. pc_setpos(pl_sd,mapindex_name2id(str),x,y,3);
  4116. break;
  4117. }
  4118. }
  4119. mapit_free(iter);
  4120. return 0;
  4121. }
  4122. /*==========================================
  4123. *
  4124. *------------------------------------------*/
  4125. BUILDIN_FUNC(heal)
  4126. {
  4127. TBL_PC *sd;
  4128. int hp,sp;
  4129. sd = script_rid2sd(st);
  4130. if (!sd) return 0;
  4131. hp=script_getnum(st,2);
  4132. sp=script_getnum(st,3);
  4133. status_heal(&sd->bl, hp, sp, 1);
  4134. return 0;
  4135. }
  4136. /*==========================================
  4137. *
  4138. *------------------------------------------*/
  4139. BUILDIN_FUNC(itemheal)
  4140. {
  4141. TBL_PC *sd;
  4142. int hp,sp;
  4143. hp=script_getnum(st,2);
  4144. sp=script_getnum(st,3);
  4145. if(potion_flag==1) {
  4146. potion_hp = hp;
  4147. potion_sp = sp;
  4148. return 0;
  4149. }
  4150. sd = script_rid2sd(st);
  4151. if (!sd) return 0;
  4152. pc_itemheal(sd,sd->itemid,hp,sp);
  4153. return 0;
  4154. }
  4155. /*==========================================
  4156. *
  4157. *------------------------------------------*/
  4158. BUILDIN_FUNC(percentheal)
  4159. {
  4160. int hp,sp;
  4161. TBL_PC* sd;
  4162. hp=script_getnum(st,2);
  4163. sp=script_getnum(st,3);
  4164. if(potion_flag==1) {
  4165. potion_per_hp = hp;
  4166. potion_per_sp = sp;
  4167. return 0;
  4168. }
  4169. sd = script_rid2sd(st);
  4170. if( sd == NULL )
  4171. return 0;
  4172. pc_percentheal(sd,hp,sp);
  4173. return 0;
  4174. }
  4175. /*==========================================
  4176. *
  4177. *------------------------------------------*/
  4178. BUILDIN_FUNC(jobchange)
  4179. {
  4180. int job, upper=-1;
  4181. job=script_getnum(st,2);
  4182. if( script_hasdata(st,3) )
  4183. upper=script_getnum(st,3);
  4184. if (pcdb_checkid(job))
  4185. {
  4186. TBL_PC* sd;
  4187. sd = script_rid2sd(st);
  4188. if( sd == NULL )
  4189. return 0;
  4190. pc_jobchange(sd, job, upper);
  4191. if(use_irc && irc_announce_jobchange_flag)
  4192. irc_announce_jobchange(sd);
  4193. }
  4194. return 0;
  4195. }
  4196. /*==========================================
  4197. *
  4198. *------------------------------------------*/
  4199. BUILDIN_FUNC(jobname)
  4200. {
  4201. int class_=script_getnum(st,2);
  4202. script_pushconststr(st,job_name(class_));
  4203. return 0;
  4204. }
  4205. /// Get input from the player.
  4206. /// For numeric inputs the value is capped to the range [min,max]. Returns 1 if
  4207. /// the value was higher than 'max', -1 if lower than 'min' and 0 otherwise.
  4208. /// For string inputs it returns 1 if the string was longer than 'max', -1 is
  4209. /// shorter than 'min' and 0 otherwise.
  4210. ///
  4211. /// input(<var>{,<min>{,<max>}}) -> <int>
  4212. BUILDIN_FUNC(input)
  4213. {
  4214. TBL_PC* sd;
  4215. struct script_data* data;
  4216. int uid;
  4217. char* name;
  4218. int min;
  4219. int max;
  4220. sd = script_rid2sd(st);
  4221. if( sd == NULL )
  4222. return 0;
  4223. data = script_getdata(st,2);
  4224. if( !data_isreference(data) ){
  4225. ShowError("script:input: not a variable\n");
  4226. script_reportdata(data);
  4227. st->state = END;
  4228. return 1;
  4229. }
  4230. uid = reference_getuid(data);
  4231. name = reference_getname(data);
  4232. min = (script_hasdata(st,3) ? script_getnum(st,3) : script_config.input_min_value);
  4233. max = (script_hasdata(st,4) ? script_getnum(st,4) : script_config.input_max_value);
  4234. if( !sd->state.menu_or_input )
  4235. { // first invocation, display npc input box
  4236. sd->state.menu_or_input = 1;
  4237. st->state = RERUNLINE;
  4238. if( is_string_variable(name) )
  4239. clif_scriptinputstr(sd,st->oid);
  4240. else
  4241. clif_scriptinput(sd,st->oid);
  4242. }
  4243. else
  4244. { // take received text/value and store it in the designated variable
  4245. sd->state.menu_or_input = 0;
  4246. if( is_string_variable(name) )
  4247. {
  4248. size_t len = strlen(sd->npc_str);
  4249. set_reg(st, sd, uid, name, (void*)sd->npc_str, script_getref(st,2));
  4250. script_pushint(st, (len > max ? 1 : len < min ? -1 : 0));
  4251. }
  4252. else
  4253. {
  4254. int amount = sd->npc_amount;
  4255. set_reg(st, sd, uid, name, (void*)cap_value(amount,min,max), script_getref(st,2));
  4256. script_pushint(st, (amount > max ? 1 : amount < min ? -1 : 0));
  4257. }
  4258. }
  4259. return 0;
  4260. }
  4261. /*==========================================
  4262. * 変数設定
  4263. *------------------------------------------*/
  4264. BUILDIN_FUNC(set)
  4265. {
  4266. TBL_PC *sd=NULL;
  4267. int num=st->stack->stack_data[st->start+2].u.num;
  4268. char *name=str_buf+str_data[num&0x00ffffff].str;
  4269. char prefix=*name;
  4270. char postfix=name[strlen(name)-1];
  4271. if( !data_isreference(script_getdata(st,2)) ){
  4272. ShowError("script:set: not a variable\n");
  4273. script_reportdata(script_getdata(st,2));
  4274. st->state = END;
  4275. return 1;
  4276. }
  4277. if(not_server_variable(prefix))
  4278. {
  4279. sd=script_rid2sd(st);
  4280. if( sd == NULL )
  4281. {
  4282. ShowError("script:set: no player attached for player variable '%s'\n", name);
  4283. return 0;
  4284. }
  4285. }
  4286. if( postfix=='$' ){
  4287. // 文字列
  4288. const char *str = script_getstr(st,3);
  4289. set_reg(st,sd,num,name,(void*)str,script_getref(st,2));
  4290. }else{
  4291. // 数値
  4292. int val = script_getnum(st,3);
  4293. set_reg(st,sd,num,name,(void*)val,script_getref(st,2));
  4294. }
  4295. return 0;
  4296. }
  4297. /////////////////////////////////////////////////////////////////////
  4298. /// Array variables
  4299. ///
  4300. /// Returns the size of the specified array
  4301. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  4302. {
  4303. int32 ret = idx;
  4304. if( isstring )
  4305. {
  4306. for( ; idx < 128; ++idx )
  4307. {
  4308. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  4309. if( str && *str )
  4310. ret = idx + 1;
  4311. script_removetop(st, -1, 0);
  4312. }
  4313. }
  4314. else
  4315. {
  4316. for( ; idx < 128; ++idx )
  4317. {
  4318. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  4319. if( num )
  4320. ret = idx + 1;
  4321. script_removetop(st, -1, 0);
  4322. }
  4323. }
  4324. return ret;
  4325. }
  4326. /// Sets values of an array, from the starting index.
  4327. /// ex: setarray arr[1],1,2,3;
  4328. ///
  4329. /// setarray <array variable>,<value1>{,<value2>...};
  4330. BUILDIN_FUNC(setarray)
  4331. {
  4332. struct script_data* data;
  4333. char* name;
  4334. int32 start;
  4335. int32 end;
  4336. int32 id;
  4337. int32 i;
  4338. TBL_PC* sd = NULL;
  4339. data = script_getdata(st, 2);
  4340. if( !data_isreference(data) )
  4341. {
  4342. ShowError("script:setarray: not a variable\n");
  4343. script_reportdata(data);
  4344. st->state = END;
  4345. return 1;// not a variable
  4346. }
  4347. id = reference_getid(data);
  4348. start = reference_getindex(data);
  4349. name = reference_getname(data);
  4350. if( not_array_variable(*name) )
  4351. {
  4352. ShowError("script:setarray: illegal scope\n");
  4353. script_reportdata(data);
  4354. st->state = END;
  4355. return 1;// not supported
  4356. }
  4357. if( not_server_variable(*name) )
  4358. {
  4359. sd = script_rid2sd(st);
  4360. if( sd == NULL )
  4361. return 0;// no player attached
  4362. }
  4363. end = start + script_lastdata(st) - 2;
  4364. if( end > 127 )
  4365. end = 127;
  4366. if( is_string_variable(name) )
  4367. {// string array
  4368. for( i = 3; start < end; ++start, ++i )
  4369. {
  4370. void* v = (void*)script_getstr(st,i);
  4371. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4372. }
  4373. }
  4374. else
  4375. {// int array
  4376. for( i = 3; start < end; ++start, ++i )
  4377. {
  4378. void* v = (void*)script_getnum(st,i);
  4379. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4380. }
  4381. }
  4382. return 0;
  4383. }
  4384. /// Sets count values of an array, from the starting index.
  4385. /// ex: cleararray arr[0],0,1;
  4386. ///
  4387. /// cleararray <array variable>,<value>,<count>;
  4388. BUILDIN_FUNC(cleararray)
  4389. {
  4390. struct script_data* data;
  4391. char* name;
  4392. int32 start;
  4393. int32 end;
  4394. int32 id;
  4395. void* v;
  4396. TBL_PC* sd = NULL;
  4397. data = script_getdata(st, 2);
  4398. if( !data_isreference(data) )
  4399. {
  4400. ShowError("script:cleararray: not a variable\n");
  4401. script_reportdata(data);
  4402. st->state = END;
  4403. return 1;// not a variable
  4404. }
  4405. id = reference_getid(data);
  4406. start = reference_getindex(data);
  4407. name = reference_getname(data);
  4408. if( not_array_variable(*name) )
  4409. {
  4410. ShowError("script:cleararray: illegal scope\n");
  4411. script_reportdata(data);
  4412. st->state = END;
  4413. return 1;// not supported
  4414. }
  4415. if( not_server_variable(*name) )
  4416. {
  4417. sd = script_rid2sd(st);
  4418. if( sd == NULL )
  4419. return 0;// no player attached
  4420. }
  4421. if( is_string_variable(name) )
  4422. v = (void*)script_getstr(st, 3);
  4423. else
  4424. v = (void*)script_getnum(st, 3);
  4425. end = start + script_getnum(st, 4);
  4426. if( end > 127 )
  4427. end = 127;
  4428. for( ; start <= end; ++start )
  4429. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  4430. return 0;
  4431. }
  4432. /// Copies data from one array to another.
  4433. /// ex: copyarray arr[0],arr[2],2;
  4434. ///
  4435. /// copyarray <destination array variable>,<source array variable>,<count>;
  4436. BUILDIN_FUNC(copyarray)
  4437. {
  4438. struct script_data* data1;
  4439. struct script_data* data2;
  4440. char* name1;
  4441. char* name2;
  4442. int32 idx1;
  4443. int32 idx2;
  4444. int32 id1;
  4445. int32 id2;
  4446. void* v;
  4447. int32 i;
  4448. int32 count;
  4449. TBL_PC* sd = NULL;
  4450. data1 = script_getdata(st, 2);
  4451. data2 = script_getdata(st, 3);
  4452. if( !data_isreference(data1) || !data_isreference(data2) )
  4453. {
  4454. ShowError("script:copyarray: not a variable\n");
  4455. script_reportdata(data1);
  4456. script_reportdata(data2);
  4457. st->state = END;
  4458. return 1;// not a variable
  4459. }
  4460. id1 = reference_getid(data1);
  4461. id2 = reference_getid(data2);
  4462. idx1 = reference_getindex(data1);
  4463. idx2 = reference_getindex(data2);
  4464. name1 = reference_getname(data1);
  4465. name2 = reference_getname(data2);
  4466. if( not_array_variable(*name1) || not_array_variable(*name2) )
  4467. {
  4468. ShowError("script:copyarray: illegal scope\n");
  4469. script_reportdata(data1);
  4470. script_reportdata(data2);
  4471. st->state = END;
  4472. return 1;// not supported
  4473. }
  4474. if( is_string_variable(name1) != is_string_variable(name2) )
  4475. {
  4476. ShowError("script:copyarray: type mismatch\n");
  4477. script_reportdata(data1);
  4478. script_reportdata(data2);
  4479. st->state = END;
  4480. return 1;// data type mismatch
  4481. }
  4482. if( not_server_variable(*name1) || not_server_variable(*name2) )
  4483. {
  4484. sd = script_rid2sd(st);
  4485. if( sd == NULL )
  4486. return 0;// no player attached
  4487. }
  4488. count = script_getnum(st, 4);
  4489. if( count > 128 - idx1 )
  4490. count = 128 - idx1;
  4491. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  4492. return 0;// nothing to copy
  4493. if( id1 == id2 && idx1 > idx2 )
  4494. {// destination might be overlapping the source - copy in reverse order
  4495. for( i = count - 1; i >= 0; --i )
  4496. {
  4497. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4498. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4499. script_removetop(st, -1, 0);
  4500. }
  4501. }
  4502. else
  4503. {// normal copy
  4504. for( i = 0; i < count; ++i )
  4505. {
  4506. if( idx2 + i < 128 )
  4507. {
  4508. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  4509. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  4510. script_removetop(st, -1, 0);
  4511. }
  4512. else// out of range - assume ""/0
  4513. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, (is_string_variable(name1)?(void*)"":(void*)0), reference_getref(data1));
  4514. }
  4515. }
  4516. return 0;
  4517. }
  4518. /// Returns the size of the array.
  4519. /// Assumes that everything before the starting index exists.
  4520. /// ex: getarraysize(arr[3])
  4521. ///
  4522. /// getarraysize(<array variable>) -> <int>
  4523. BUILDIN_FUNC(getarraysize)
  4524. {
  4525. struct script_data* data;
  4526. char* name;
  4527. data = script_getdata(st, 2);
  4528. if( !data_isreference(data) )
  4529. {
  4530. ShowError("script:getarraysize: not a variable\n");
  4531. script_reportdata(data);
  4532. script_pushnil(st);
  4533. st->state = END;
  4534. return 1;// not a variable
  4535. }
  4536. name = reference_getname(data);
  4537. if( not_array_variable(*name) )
  4538. {
  4539. ShowError("script:getarraysize: illegal scope\n");
  4540. script_reportdata(data);
  4541. script_pushnil(st);
  4542. st->state = END;
  4543. return 1;// not supported
  4544. }
  4545. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  4546. return 0;
  4547. }
  4548. /// Deletes count or all the elements in an array, from the starting index.
  4549. /// ex: deletearray arr[4],2;
  4550. ///
  4551. /// deletearray <array variable>;
  4552. /// deletearray <array variable>,<count>;
  4553. BUILDIN_FUNC(deletearray)
  4554. {
  4555. struct script_data* data;
  4556. char* name;
  4557. int start;
  4558. int end;
  4559. int id;
  4560. TBL_PC *sd = NULL;
  4561. data = script_getdata(st, 2);
  4562. if( !data_isreference(data) )
  4563. {
  4564. ShowError("script:deletearray: not a variable\n");
  4565. script_reportdata(data);
  4566. st->state = END;
  4567. return 1;// not a variable
  4568. }
  4569. id = reference_getid(data);
  4570. start = reference_getindex(data);
  4571. name = reference_getname(data);
  4572. if( not_array_variable(*name) )
  4573. {
  4574. ShowError("script:deletearray: illegal scope\n");
  4575. script_reportdata(data);
  4576. st->state = END;
  4577. return 1;// not supported
  4578. }
  4579. if( not_server_variable(*name) )
  4580. {
  4581. sd = script_rid2sd(st);
  4582. if( sd == NULL )
  4583. return 0;// no player attached
  4584. }
  4585. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  4586. if( start >= end )
  4587. return 0;// nothing to free
  4588. if( script_hasdata(st,3) )
  4589. {
  4590. int count = script_getnum(st, 3);
  4591. if( count > end - start )
  4592. count = end - start;
  4593. if( count <= 0 )
  4594. return 0;// nothing to free
  4595. // move rest of the elements backward
  4596. for( ; start + count < end; ++start )
  4597. {
  4598. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  4599. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  4600. script_removetop(st, -1, 0);
  4601. }
  4602. }
  4603. // clear the rest of the array
  4604. if( is_string_variable(name) )
  4605. {
  4606. for( ; start < end; ++start )
  4607. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  4608. }
  4609. else
  4610. {
  4611. for( ; start < end; ++start )
  4612. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  4613. }
  4614. return 0;
  4615. }
  4616. /// Returns a reference to the target index of the array variable.
  4617. /// Equivalent to var[index].
  4618. ///
  4619. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  4620. BUILDIN_FUNC(getelementofarray)
  4621. {
  4622. struct script_data* data;
  4623. char* name;
  4624. int32 id;
  4625. int i;
  4626. data = script_getdata(st, 2);
  4627. if( !data_isreference(data) )
  4628. {
  4629. ShowError("script:getelementofarray: not a variable\n");
  4630. script_reportdata(data);
  4631. script_pushnil(st);
  4632. st->state = END;
  4633. return 1;// not a variable
  4634. }
  4635. id = reference_getid(data);
  4636. name = reference_getname(data);
  4637. if( not_array_variable(*name) )
  4638. {
  4639. ShowError("script:getelementofarray: illegal scope\n");
  4640. script_reportdata(data);
  4641. script_pushnil(st);
  4642. st->state = END;
  4643. return 1;// not supported
  4644. }
  4645. i = script_getnum(st, 3);
  4646. if( i < 0 || i >= 128 )
  4647. {
  4648. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  4649. script_reportdata(data);
  4650. script_pushnil(st);
  4651. st->state = END;
  4652. return 1;// out of range
  4653. }
  4654. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  4655. return 0;
  4656. }
  4657. /////////////////////////////////////////////////////////////////////
  4658. /// ...
  4659. ///
  4660. /*==========================================
  4661. *
  4662. *------------------------------------------*/
  4663. BUILDIN_FUNC(setlook)
  4664. {
  4665. int type,val;
  4666. TBL_PC* sd;
  4667. type=script_getnum(st,2);
  4668. val=script_getnum(st,3);
  4669. sd = script_rid2sd(st);
  4670. if( sd == NULL )
  4671. return 0;
  4672. pc_changelook(sd,type,val);
  4673. return 0;
  4674. }
  4675. /*==========================================
  4676. *
  4677. *------------------------------------------*/
  4678. BUILDIN_FUNC(cutin)
  4679. {
  4680. TBL_PC* sd;
  4681. sd = script_rid2sd(st);
  4682. if( sd == NULL )
  4683. return 0;
  4684. clif_cutin(sd,script_getstr(st,2),script_getnum(st,3));
  4685. return 0;
  4686. }
  4687. /*==========================================
  4688. *
  4689. *------------------------------------------*/
  4690. BUILDIN_FUNC(viewpoint)
  4691. {
  4692. int type,x,y,id,color;
  4693. TBL_PC* sd;
  4694. type=script_getnum(st,2);
  4695. x=script_getnum(st,3);
  4696. y=script_getnum(st,4);
  4697. id=script_getnum(st,5);
  4698. color=script_getnum(st,6);
  4699. sd = script_rid2sd(st);
  4700. if( sd == NULL )
  4701. return 0;
  4702. clif_viewpoint(sd,st->oid,type,x,y,id,color);
  4703. return 0;
  4704. }
  4705. /*==========================================
  4706. *
  4707. *------------------------------------------*/
  4708. BUILDIN_FUNC(countitem)
  4709. {
  4710. int nameid, i;
  4711. int count = 0;
  4712. struct script_data* data;
  4713. TBL_PC* sd = script_rid2sd(st);
  4714. if (!sd) {
  4715. script_pushint(st,0);
  4716. return 0;
  4717. }
  4718. data = script_getdata(st,2);
  4719. get_val(st,data);
  4720. if( data_isstring(data) ) {
  4721. const char* name = conv_str(st,data);
  4722. struct item_data* item_data;
  4723. if((item_data = itemdb_searchname(name)) != NULL)
  4724. nameid = item_data->nameid;
  4725. else
  4726. nameid = 0;
  4727. } else
  4728. nameid = conv_num(st,data);
  4729. if (nameid < 500) {
  4730. ShowError("wrong item ID : countitem(%i)\n", nameid);
  4731. script_reportsrc(st);
  4732. script_pushint(st,0);
  4733. return 1;
  4734. }
  4735. for(i = 0; i < MAX_INVENTORY; i++)
  4736. if(sd->status.inventory[i].nameid == nameid)
  4737. count += sd->status.inventory[i].amount;
  4738. script_pushint(st,count);
  4739. return 0;
  4740. }
  4741. /*==========================================
  4742. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  4743. * returns number of items that meet the conditions
  4744. *------------------------------------------*/
  4745. BUILDIN_FUNC(countitem2)
  4746. {
  4747. int nameid, iden, ref, attr, c1, c2, c3, c4;
  4748. int count = 0;
  4749. int i;
  4750. struct script_data* data;
  4751. TBL_PC* sd = script_rid2sd(st);
  4752. if (!sd) {
  4753. script_pushint(st,0);
  4754. return 0;
  4755. }
  4756. data = script_getdata(st,2);
  4757. get_val(st,data);
  4758. if( data_isstring(data) ) {
  4759. const char* name = conv_str(st,data);
  4760. struct item_data* item_data;
  4761. if((item_data = itemdb_searchname(name)) != NULL)
  4762. nameid = item_data->nameid;
  4763. else
  4764. nameid = 0;
  4765. } else
  4766. nameid = conv_num(st,data);
  4767. iden = script_getnum(st,3);
  4768. ref = script_getnum(st,4);
  4769. attr = script_getnum(st,5);
  4770. c1 = (short)script_getnum(st,6);
  4771. c2 = (short)script_getnum(st,7);
  4772. c3 = (short)script_getnum(st,8);
  4773. c4 = (short)script_getnum(st,9);
  4774. if (nameid < 500) {
  4775. ShowError("wrong item ID : countitem2(%i)\n", nameid);
  4776. script_pushint(st,0);
  4777. return 1;
  4778. }
  4779. for(i = 0; i < MAX_INVENTORY; i++)
  4780. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  4781. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  4782. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  4783. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  4784. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  4785. sd->status.inventory[i].card[3] == c4
  4786. )
  4787. count += sd->status.inventory[i].amount;
  4788. script_pushint(st,count);
  4789. return 0;
  4790. }
  4791. /*==========================================
  4792. * 重量チェック
  4793. *------------------------------------------*/
  4794. BUILDIN_FUNC(checkweight)
  4795. {
  4796. int nameid=0,amount,i;
  4797. unsigned long weight;
  4798. TBL_PC *sd;
  4799. struct script_data *data;
  4800. sd = script_rid2sd(st);
  4801. if( sd == NULL )
  4802. return 0;
  4803. data=script_getdata(st,2);
  4804. get_val(st,data);
  4805. if( data_isstring(data) ){
  4806. const char *name=conv_str(st,data);
  4807. struct item_data *item_data = itemdb_searchname(name);
  4808. if( item_data )
  4809. nameid=item_data->nameid;
  4810. }else
  4811. nameid=conv_num(st,data);
  4812. amount=script_getnum(st,3);
  4813. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  4814. script_pushint(st,0);
  4815. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  4816. return 1;
  4817. }
  4818. weight = itemdb_weight(nameid)*amount;
  4819. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  4820. script_pushint(st,0);
  4821. } else {
  4822. //Check if the inventory ain't full.
  4823. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  4824. i = pc_search_inventory(sd,0);
  4825. if (i >= 0) //Empty slot available.
  4826. script_pushint(st,1);
  4827. else //Inventory full
  4828. script_pushint(st,0);
  4829. }
  4830. return 0;
  4831. }
  4832. /*==========================================
  4833. * getitem <item id>,<amount>{,<character ID>};
  4834. * getitem "<item name>",<amount>{,<character ID>};
  4835. *------------------------------------------*/
  4836. BUILDIN_FUNC(getitem)
  4837. {
  4838. int nameid,amount,flag = 0;
  4839. struct item it;
  4840. TBL_PC *sd;
  4841. struct script_data *data;
  4842. data=script_getdata(st,2);
  4843. get_val(st,data);
  4844. if( data_isstring(data) )
  4845. {// "<item name>"
  4846. const char *name=conv_str(st,data);
  4847. struct item_data *item_data = itemdb_searchname(name);
  4848. if( item_data == NULL ){
  4849. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  4850. return 1; //No item created.
  4851. }
  4852. nameid=item_data->nameid;
  4853. } else if( data_isint(data) )
  4854. {// <item id>
  4855. nameid=conv_num(st,data);
  4856. //Violet Box, Blue Box, etc - random item pick
  4857. if( nameid < 0 ) {
  4858. nameid=itemdb_searchrandomid(-nameid);
  4859. flag = 1;
  4860. }
  4861. if( nameid <= 0 || !itemdb_exists(nameid) ){
  4862. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  4863. return 1; //No item created.
  4864. }
  4865. } else {
  4866. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  4867. return 1;
  4868. }
  4869. // <amount>
  4870. if( (amount=script_getnum(st,3)) <= 0)
  4871. return 0; //return if amount <=0, skip the useles iteration
  4872. memset(&it,0,sizeof(it));
  4873. it.nameid=nameid;
  4874. if(!flag)
  4875. it.identify=1;
  4876. else
  4877. it.identify=itemdb_isidentified(nameid);
  4878. if( script_hasdata(st,4) )
  4879. {// <character ID>
  4880. sd=map_id2sd(script_getnum(st,4));
  4881. } else
  4882. {// attached player
  4883. sd=script_rid2sd(st);
  4884. }
  4885. if( sd == NULL ) // no target
  4886. return 0;
  4887. if( pet_create_egg(sd, nameid) )
  4888. amount = 1; //This is a pet!
  4889. else if( (flag=pc_additem(sd,&it,amount)) ){
  4890. clif_additem(sd,0,0,flag);
  4891. if( pc_candrop(sd,&it) )
  4892. map_addflooritem(&it,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4893. }
  4894. //Logs items, got from (N)PC scripts [Lupus]
  4895. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  4896. log_pick_pc(sd, "N", nameid, amount, NULL);
  4897. return 0;
  4898. }
  4899. /*==========================================
  4900. *
  4901. *------------------------------------------*/
  4902. BUILDIN_FUNC(getitem2)
  4903. {
  4904. int nameid,amount,flag = 0;
  4905. int iden,ref,attr,c1,c2,c3,c4;
  4906. struct item_data *item_data;
  4907. struct item item_tmp;
  4908. TBL_PC *sd;
  4909. struct script_data *data;
  4910. sd = script_rid2sd(st);
  4911. if( sd == NULL )
  4912. return 0;
  4913. data=script_getdata(st,2);
  4914. get_val(st,data);
  4915. if( data_isstring(data) ){
  4916. const char *name=conv_str(st,data);
  4917. struct item_data *item_data = itemdb_searchname(name);
  4918. if( item_data )
  4919. nameid=item_data->nameid;
  4920. else
  4921. nameid=UNKNOWN_ITEM_ID;
  4922. }else
  4923. nameid=conv_num(st,data);
  4924. amount=script_getnum(st,3);
  4925. iden=script_getnum(st,4);
  4926. ref=script_getnum(st,5);
  4927. attr=script_getnum(st,6);
  4928. c1=script_getnum(st,7);
  4929. c2=script_getnum(st,8);
  4930. c3=script_getnum(st,9);
  4931. c4=script_getnum(st,10);
  4932. if( script_hasdata(st,11) ) //アイテムを指定したIDに渡す
  4933. sd=map_id2sd(script_getnum(st,11));
  4934. if(sd == NULL) //アイテムを渡す相手がいなかったらお帰り
  4935. return 0;
  4936. if(nameid<0) { // ランダム
  4937. nameid=itemdb_searchrandomid(-nameid);
  4938. flag = 1;
  4939. }
  4940. if(nameid > 0) {
  4941. memset(&item_tmp,0,sizeof(item_tmp));
  4942. item_data=itemdb_exists(nameid);
  4943. if (item_data == NULL)
  4944. return -1;
  4945. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  4946. if(ref > 10) ref = 10;
  4947. }
  4948. else if(item_data->type==IT_PETEGG) {
  4949. iden = 1;
  4950. ref = 0;
  4951. }
  4952. else {
  4953. iden = 1;
  4954. ref = attr = 0;
  4955. }
  4956. item_tmp.nameid=nameid;
  4957. if(!flag)
  4958. item_tmp.identify=iden;
  4959. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  4960. item_tmp.identify=0;
  4961. item_tmp.refine=ref;
  4962. item_tmp.attribute=attr;
  4963. item_tmp.card[0]=c1;
  4964. item_tmp.card[1]=c2;
  4965. item_tmp.card[2]=c3;
  4966. item_tmp.card[3]=c4;
  4967. if((flag = pc_additem(sd,&item_tmp,amount))) {
  4968. clif_additem(sd,0,0,flag);
  4969. map_addflooritem(&item_tmp,amount,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  4970. }
  4971. //Logs items, got from (N)PC scripts [Lupus]
  4972. if(log_config.enable_logs&0x40)
  4973. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  4974. }
  4975. return 0;
  4976. }
  4977. /*==========================================
  4978. * gets an item with someone's name inscribed [Skotlex]
  4979. * getinscribeditem item_num, character_name
  4980. * Returned Qty is always 1, only works on equip-able
  4981. * equipment
  4982. *------------------------------------------*/
  4983. BUILDIN_FUNC(getnameditem)
  4984. {
  4985. int nameid;
  4986. struct item item_tmp;
  4987. TBL_PC *sd, *tsd;
  4988. struct script_data *data;
  4989. sd = script_rid2sd(st);
  4990. if (sd == NULL)
  4991. { //Player not attached!
  4992. script_pushint(st,0);
  4993. return 0;
  4994. }
  4995. data=script_getdata(st,2);
  4996. get_val(st,data);
  4997. if( data_isstring(data) ){
  4998. const char *name=conv_str(st,data);
  4999. struct item_data *item_data = itemdb_searchname(name);
  5000. if( item_data == NULL)
  5001. { //Failed
  5002. script_pushint(st,0);
  5003. return 0;
  5004. }
  5005. nameid = item_data->nameid;
  5006. }else
  5007. nameid = conv_num(st,data);
  5008. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5009. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5010. script_pushint(st,0);
  5011. return 0;
  5012. }
  5013. data=script_getdata(st,3);
  5014. get_val(st,data);
  5015. if( data_isstring(data) ) //Char Name
  5016. tsd=map_nick2sd(conv_str(st,data));
  5017. else //Char Id was given
  5018. tsd=map_charid2sd(conv_num(st,data));
  5019. if( tsd == NULL )
  5020. { //Failed
  5021. script_pushint(st,0);
  5022. return 0;
  5023. }
  5024. memset(&item_tmp,0,sizeof(item_tmp));
  5025. item_tmp.nameid=nameid;
  5026. item_tmp.amount=1;
  5027. item_tmp.identify=1;
  5028. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5029. item_tmp.card[2]=tsd->status.char_id;
  5030. item_tmp.card[3]=tsd->status.char_id >> 16;
  5031. if(pc_additem(sd,&item_tmp,1)) {
  5032. script_pushint(st,0);
  5033. return 0; //Failed to add item, we will not drop if they don't fit
  5034. }
  5035. //Logs items, got from (N)PC scripts [Lupus]
  5036. if(log_config.enable_logs&0x40)
  5037. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5038. script_pushint(st,1);
  5039. return 0;
  5040. }
  5041. /*==========================================
  5042. * gets a random item ID from an item group [Skotlex]
  5043. * groupranditem group_num
  5044. *------------------------------------------*/
  5045. BUILDIN_FUNC(grouprandomitem)
  5046. {
  5047. int group;
  5048. group = script_getnum(st,2);
  5049. script_pushint(st,itemdb_searchrandomid(group));
  5050. return 0;
  5051. }
  5052. /*==========================================
  5053. *
  5054. *------------------------------------------*/
  5055. BUILDIN_FUNC(makeitem)
  5056. {
  5057. int nameid,amount,flag = 0;
  5058. int x,y,m;
  5059. const char *mapname;
  5060. struct item item_tmp;
  5061. struct script_data *data;
  5062. data=script_getdata(st,2);
  5063. get_val(st,data);
  5064. if( data_isstring(data) ){
  5065. const char *name=conv_str(st,data);
  5066. struct item_data *item_data = itemdb_searchname(name);
  5067. if( item_data )
  5068. nameid=item_data->nameid;
  5069. else
  5070. nameid=UNKNOWN_ITEM_ID;
  5071. }else
  5072. nameid=conv_num(st,data);
  5073. amount=script_getnum(st,3);
  5074. mapname =script_getstr(st,4);
  5075. x =script_getnum(st,5);
  5076. y =script_getnum(st,6);
  5077. if(strcmp(mapname,"this")==0)
  5078. {
  5079. TBL_PC *sd;
  5080. sd = script_rid2sd(st);
  5081. if (!sd) return 0; //Failed...
  5082. m=sd->bl.m;
  5083. } else
  5084. m=map_mapname2mapid(mapname);
  5085. if(nameid<0) { // ランダム
  5086. nameid=itemdb_searchrandomid(-nameid);
  5087. flag = 1;
  5088. }
  5089. if(nameid > 0) {
  5090. memset(&item_tmp,0,sizeof(item_tmp));
  5091. item_tmp.nameid=nameid;
  5092. if(!flag)
  5093. item_tmp.identify=1;
  5094. else
  5095. item_tmp.identify=itemdb_isidentified(nameid);
  5096. map_addflooritem(&item_tmp,amount,m,x,y,0,0,0,0);
  5097. }
  5098. return 0;
  5099. }
  5100. /*==========================================
  5101. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5102. *------------------------------------------*/
  5103. BUILDIN_FUNC(delitem)
  5104. {
  5105. int nameid=0,amount,i,important_item=0;
  5106. TBL_PC *sd;
  5107. struct script_data *data;
  5108. sd = script_rid2sd(st);
  5109. if( sd == NULL )
  5110. return 0;
  5111. data=script_getdata(st,2);
  5112. get_val(st,data);
  5113. if( data_isstring(data) ){
  5114. const char *name=conv_str(st,data);
  5115. struct item_data *item_data = itemdb_searchname(name);
  5116. //nameid=UNKNOWN_ITEM_ID;
  5117. if( item_data )
  5118. nameid=item_data->nameid;
  5119. }else
  5120. nameid=conv_num(st,data);
  5121. amount=script_getnum(st,3);
  5122. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5123. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5124. return 0;
  5125. }
  5126. //1st pass
  5127. //here we won't delete items with CARDS, named items but we count them
  5128. for(i=0;i<MAX_INVENTORY;i++){
  5129. //we don't delete wrong item or equipped item
  5130. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5131. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5132. continue;
  5133. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5134. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5135. sd->status.inventory[i].card[0] == CARD0_PET)
  5136. {
  5137. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5138. continue; //pet couldn't be sent for deletion.
  5139. } else
  5140. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5141. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5142. sd->status.inventory[i].card[0] ||
  5143. sd->status.inventory[i].refine) {
  5144. //this is important item, count it (except for pet eggs)
  5145. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5146. important_item++;
  5147. continue;
  5148. }
  5149. if(sd->status.inventory[i].amount>=amount){
  5150. //Logs items, got from (N)PC scripts [Lupus]
  5151. if(log_config.enable_logs&0x40)
  5152. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5153. pc_delitem(sd,i,amount,0);
  5154. return 0; //we deleted exact amount of items. now exit
  5155. } else {
  5156. amount-=sd->status.inventory[i].amount;
  5157. //Logs items, got from (N)PC scripts [Lupus]
  5158. if(log_config.enable_logs&0x40) {
  5159. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5160. }
  5161. //Logs
  5162. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5163. }
  5164. }
  5165. //2nd pass
  5166. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5167. if (important_item>0 && amount>0)
  5168. for(i=0;i<MAX_INVENTORY;i++){
  5169. //we don't delete wrong item
  5170. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5171. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5172. continue;
  5173. if(sd->status.inventory[i].amount>=amount){
  5174. //Logs items, got from (N)PC scripts [Lupus]
  5175. if(log_config.enable_logs&0x40)
  5176. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5177. pc_delitem(sd,i,amount,0);
  5178. return 0; //we deleted exact amount of items. now exit
  5179. } else {
  5180. amount-=sd->status.inventory[i].amount;
  5181. //Logs items, got from (N)PC scripts [Lupus]
  5182. if(log_config.enable_logs&0x40)
  5183. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5184. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5185. }
  5186. }
  5187. return 0;
  5188. }
  5189. /*==========================================
  5190. * advanced version of delitem [modified by Mihilion]
  5191. *------------------------------------------*/
  5192. BUILDIN_FUNC(delitem2)
  5193. {
  5194. int nameid=0,amount,i=0;
  5195. int iden,ref,attr,c1,c2,c3,c4;
  5196. TBL_PC *sd;
  5197. struct script_data *data;
  5198. sd = script_rid2sd(st);
  5199. if( sd == NULL )
  5200. return 0;
  5201. data=script_getdata(st,2);
  5202. get_val(st,data);
  5203. if( data_isstring(data) ){
  5204. const char *name=conv_str(st,data);
  5205. struct item_data *item_data = itemdb_searchname(name);
  5206. //nameid=UNKNOWN_ITEM_ID;
  5207. if( item_data )
  5208. nameid=item_data->nameid;
  5209. }else
  5210. nameid=conv_num(st,data);
  5211. amount=script_getnum(st,3);
  5212. iden=script_getnum(st,4);
  5213. ref=script_getnum(st,5);
  5214. attr=script_getnum(st,6);
  5215. c1=script_getnum(st,7);
  5216. c2=script_getnum(st,8);
  5217. c3=script_getnum(st,9);
  5218. c4=script_getnum(st,10);
  5219. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5220. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5221. return 0;
  5222. }
  5223. for(i=0;i<MAX_INVENTORY;i++){
  5224. //we don't delete wrong item or equipped item
  5225. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5226. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5227. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5228. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5229. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5230. sd->status.inventory[i].card[3]!=c4)
  5231. continue;
  5232. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5233. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5234. {
  5235. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5236. continue; //Failed to send delete the pet.
  5237. }
  5238. if(sd->status.inventory[i].amount>=amount){
  5239. //Logs items, got from (N)PC scripts [Lupus]
  5240. if(log_config.enable_logs&0x40)
  5241. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5242. pc_delitem(sd,i,amount,0);
  5243. return 0; //we deleted exact amount of items. now exit
  5244. } else {
  5245. amount-=sd->status.inventory[i].amount;
  5246. //Logs items, got from (N)PC scripts [Lupus]
  5247. if(log_config.enable_logs&0x40)
  5248. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5249. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5250. }
  5251. }
  5252. return 0;
  5253. }
  5254. /*==========================================
  5255. * Enables/Disables use of items while in an NPC [Skotlex]
  5256. *------------------------------------------*/
  5257. BUILDIN_FUNC(enableitemuse)
  5258. {
  5259. TBL_PC *sd;
  5260. sd=script_rid2sd(st);
  5261. if (sd)
  5262. sd->npc_item_flag = st->oid;
  5263. return 0;
  5264. }
  5265. BUILDIN_FUNC(disableitemuse)
  5266. {
  5267. TBL_PC *sd;
  5268. sd=script_rid2sd(st);
  5269. if (sd)
  5270. sd->npc_item_flag = 0;
  5271. return 0;
  5272. }
  5273. /*==========================================
  5274. *キャラ関係のパラメータ取得
  5275. *------------------------------------------*/
  5276. BUILDIN_FUNC(readparam)
  5277. {
  5278. int type;
  5279. TBL_PC *sd;
  5280. type=script_getnum(st,2);
  5281. if( script_hasdata(st,3) )
  5282. sd=map_nick2sd(script_getstr(st,3));
  5283. else
  5284. sd=script_rid2sd(st);
  5285. if(sd==NULL){
  5286. script_pushint(st,-1);
  5287. return 0;
  5288. }
  5289. script_pushint(st,pc_readparam(sd,type));
  5290. return 0;
  5291. }
  5292. /*==========================================
  5293. *キャラ関係のID取得
  5294. *------------------------------------------*/
  5295. BUILDIN_FUNC(getcharid)
  5296. {
  5297. int num;
  5298. TBL_PC *sd;
  5299. num = script_getnum(st,2);
  5300. if( script_hasdata(st,3) )
  5301. sd=map_nick2sd(script_getstr(st,3));
  5302. else
  5303. sd=script_rid2sd(st);
  5304. if(sd==NULL){
  5305. script_pushint(st,0); //return 0, according docs
  5306. return 0;
  5307. }
  5308. switch( num ) {
  5309. case 0: script_pushint(st,sd->status.char_id); break;
  5310. case 1: script_pushint(st,sd->status.party_id); break;
  5311. case 2: script_pushint(st,sd->status.guild_id); break;
  5312. case 3: script_pushint(st,sd->status.account_id); break;
  5313. default:
  5314. ShowError("buildin_getcharid: invalid parameter (%d).\n", num);
  5315. script_pushint(st,0);
  5316. break;
  5317. }
  5318. return 0;
  5319. }
  5320. /*==========================================
  5321. *指定IDのPT名取得
  5322. *------------------------------------------*/
  5323. char *buildin_getpartyname_sub(int party_id)
  5324. {
  5325. struct party_data *p;
  5326. p=party_search(party_id);
  5327. if(p!=NULL){
  5328. char *buf;
  5329. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5330. memcpy(buf, p->party.name, NAME_LENGTH);
  5331. buf[NAME_LENGTH-1] = '\0';
  5332. return buf;
  5333. }
  5334. return 0;
  5335. }
  5336. BUILDIN_FUNC(getpartyname)
  5337. {
  5338. char *name;
  5339. int party_id;
  5340. party_id=script_getnum(st,2);
  5341. name=buildin_getpartyname_sub(party_id);
  5342. if(name != NULL)
  5343. script_pushstr(st,name);
  5344. else
  5345. script_pushconststr(st,"null");
  5346. return 0;
  5347. }
  5348. /*==========================================
  5349. *指定IDのPT人数とメンバーID取得
  5350. *------------------------------------------*/
  5351. BUILDIN_FUNC(getpartymember)
  5352. {
  5353. struct party_data *p;
  5354. int i,j=0,type=0;
  5355. p=party_search(script_getnum(st,2));
  5356. if( script_hasdata(st,3) )
  5357. type=script_getnum(st,3);
  5358. if(p!=NULL){
  5359. for(i=0;i<MAX_PARTY;i++){
  5360. if(p->party.member[i].account_id){
  5361. switch (type) {
  5362. case 2:
  5363. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  5364. break;
  5365. case 1:
  5366. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  5367. break;
  5368. default:
  5369. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  5370. }
  5371. j++;
  5372. }
  5373. }
  5374. }
  5375. mapreg_setreg(add_str("$@partymembercount"),j);
  5376. return 0;
  5377. }
  5378. /*==========================================
  5379. * Retrieves party leader. if flag is specified,
  5380. * return some of the leader data. Otherwise, return name.
  5381. *------------------------------------------*/
  5382. BUILDIN_FUNC(getpartyleader)
  5383. {
  5384. int party_id, type = 0, i=0;
  5385. struct party_data *p;
  5386. party_id=script_getnum(st,2);
  5387. if( script_hasdata(st,3) )
  5388. type=script_getnum(st,3);
  5389. p=party_search(party_id);
  5390. if (p) //Search leader
  5391. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  5392. if (!p || i == MAX_PARTY) { //leader not found
  5393. if (type)
  5394. script_pushint(st,-1);
  5395. else
  5396. script_pushconststr(st,"null");
  5397. return 0;
  5398. }
  5399. switch (type) {
  5400. case 1: script_pushint(st,p->party.member[i].account_id); break;
  5401. case 2: script_pushint(st,p->party.member[i].char_id); break;
  5402. case 3: script_pushint(st,p->party.member[i].class_); break;
  5403. case 4: script_pushstrcopy(st,mapindex_id2name(p->party.member[i].map)); break;
  5404. case 5: script_pushint(st,p->party.member[i].lv); break;
  5405. default: script_pushstrcopy(st,p->party.member[i].name); break;
  5406. }
  5407. return 0;
  5408. }
  5409. /*==========================================
  5410. *指定IDのギルド名取得
  5411. *------------------------------------------*/
  5412. char *buildin_getguildname_sub(int guild_id)
  5413. {
  5414. struct guild *g=NULL;
  5415. g=guild_search(guild_id);
  5416. if(g!=NULL){
  5417. char *buf;
  5418. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5419. memcpy(buf, g->name, NAME_LENGTH);
  5420. buf[NAME_LENGTH-1] = '\0';
  5421. return buf;
  5422. }
  5423. return NULL;
  5424. }
  5425. BUILDIN_FUNC(getguildname)
  5426. {
  5427. char *name;
  5428. int guild_id=script_getnum(st,2);
  5429. name=buildin_getguildname_sub(guild_id);
  5430. if(name != NULL)
  5431. script_pushstr(st,name);
  5432. else
  5433. script_pushconststr(st,"null");
  5434. return 0;
  5435. }
  5436. /*==========================================
  5437. *指定IDのGuildMaster名取得
  5438. *------------------------------------------*/
  5439. char *buildin_getguildmaster_sub(int guild_id)
  5440. {
  5441. struct guild *g=NULL;
  5442. g=guild_search(guild_id);
  5443. if(g!=NULL){
  5444. char *buf;
  5445. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  5446. memcpy(buf, g->master, NAME_LENGTH);
  5447. buf[NAME_LENGTH-1] = '\0';
  5448. return buf;
  5449. }
  5450. return 0;
  5451. }
  5452. BUILDIN_FUNC(getguildmaster)
  5453. {
  5454. char *master;
  5455. int guild_id=script_getnum(st,2);
  5456. master=buildin_getguildmaster_sub(guild_id);
  5457. if(master!=0)
  5458. script_pushstr(st,master);
  5459. else
  5460. script_pushconststr(st,"null");
  5461. return 0;
  5462. }
  5463. BUILDIN_FUNC(getguildmasterid)
  5464. {
  5465. char *master;
  5466. TBL_PC *sd=NULL;
  5467. int guild_id=script_getnum(st,2);
  5468. master=buildin_getguildmaster_sub(guild_id);
  5469. if(master!=0){
  5470. if((sd=map_nick2sd(master)) == NULL){
  5471. script_pushint(st,0);
  5472. return 0;
  5473. }
  5474. script_pushint(st,sd->status.char_id);
  5475. }else{
  5476. script_pushint(st,0);
  5477. }
  5478. return 0;
  5479. }
  5480. /*==========================================
  5481. * キャラクタの名前
  5482. *------------------------------------------*/
  5483. BUILDIN_FUNC(strcharinfo)
  5484. {
  5485. TBL_PC *sd;
  5486. int num;
  5487. char *buf;
  5488. sd=script_rid2sd(st);
  5489. if (!sd) { //Avoid crashing....
  5490. script_pushconststr(st,"");
  5491. return 0;
  5492. }
  5493. num=script_getnum(st,2);
  5494. switch(num){
  5495. case 0:
  5496. script_pushstrcopy(st,sd->status.name);
  5497. break;
  5498. case 1:
  5499. buf=buildin_getpartyname_sub(sd->status.party_id);
  5500. if(buf!=0)
  5501. script_pushstr(st,buf);
  5502. else
  5503. script_pushconststr(st,"");
  5504. break;
  5505. case 2:
  5506. buf=buildin_getguildname_sub(sd->status.guild_id);
  5507. if(buf != NULL)
  5508. script_pushstr(st,buf);
  5509. else
  5510. script_pushconststr(st,"");
  5511. break;
  5512. default:
  5513. ShowWarning("buildin_strcharinfo: unknown parameter.");
  5514. script_pushconststr(st,"");
  5515. break;
  5516. }
  5517. return 0;
  5518. }
  5519. /*==========================================
  5520. * 呼び出し元のNPC情報を取得する
  5521. *------------------------------------------*/
  5522. BUILDIN_FUNC(strnpcinfo)
  5523. {
  5524. TBL_NPC* nd;
  5525. int num;
  5526. char *buf,*name=NULL;
  5527. nd = map_id2nd(st->oid);
  5528. if (!nd) {
  5529. script_pushconststr(st, "");
  5530. return 0;
  5531. }
  5532. num = script_getnum(st,2);
  5533. switch(num){
  5534. case 0: // display name
  5535. name = aStrdup(nd->name);
  5536. break;
  5537. case 1: // visible part of display name name
  5538. if((buf = strchr(nd->name,'#')) != NULL)
  5539. {
  5540. name = aStrdup(nd->name);
  5541. name[buf - nd->name] = 0;
  5542. }
  5543. break;
  5544. case 2: // # fragment
  5545. if((buf = strchr(nd->name,'#')) != NULL)
  5546. name = aStrdup(buf+1);
  5547. break;
  5548. case 3: // unique name
  5549. name = aStrdup(nd->exname);
  5550. break;
  5551. }
  5552. if(name)
  5553. script_pushstr(st, name);
  5554. else
  5555. script_pushconststr(st, "");
  5556. return 0;
  5557. }
  5558. 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};
  5559. /*==========================================
  5560. * GetEquipID(Pos); Pos: 1-10
  5561. *------------------------------------------*/
  5562. BUILDIN_FUNC(getequipid)
  5563. {
  5564. int i=-1,num;
  5565. TBL_PC *sd;
  5566. struct item_data* item;
  5567. sd=script_rid2sd(st);
  5568. if(sd == NULL)
  5569. return 0;
  5570. num=script_getnum(st,2);
  5571. if (num > 0 && num <= ARRAYLENGTH(equip))
  5572. i=pc_checkequip(sd,equip[num-1]);
  5573. if(i >= 0){
  5574. item=sd->inventory_data[i];
  5575. if(item)
  5576. script_pushint(st,item->nameid);
  5577. else
  5578. script_pushint(st,0);
  5579. }else{
  5580. script_pushint(st,-1);
  5581. }
  5582. return 0;
  5583. }
  5584. /*==========================================
  5585. * 装備名文字列(精錬メニュー用)
  5586. *------------------------------------------*/
  5587. BUILDIN_FUNC(getequipname)
  5588. {
  5589. int i=-1,num;
  5590. TBL_PC *sd;
  5591. struct item_data* item;
  5592. char *buf;
  5593. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  5594. sd = script_rid2sd(st);
  5595. if( sd == NULL )
  5596. return 0;
  5597. buf=(char *)aMallocA(64*sizeof(char));
  5598. num=script_getnum(st,2);
  5599. if (num > 0 && num <= ARRAYLENGTH(equip))
  5600. i=pc_checkequip(sd,equip[num-1]);
  5601. if(i >= 0){
  5602. item=sd->inventory_data[i];
  5603. if(item)
  5604. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  5605. else
  5606. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5607. }else{
  5608. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  5609. }
  5610. script_pushstr(st,buf);
  5611. return 0;
  5612. }
  5613. /*==========================================
  5614. * getbrokenid [Valaris]
  5615. *------------------------------------------*/
  5616. BUILDIN_FUNC(getbrokenid)
  5617. {
  5618. int i,num,id=0,brokencounter=0;
  5619. TBL_PC *sd;
  5620. sd = script_rid2sd(st);
  5621. if( sd == NULL )
  5622. return 0;
  5623. num=script_getnum(st,2);
  5624. for(i=0; i<MAX_INVENTORY; i++) {
  5625. if(sd->status.inventory[i].attribute==1){
  5626. brokencounter++;
  5627. if(num==brokencounter){
  5628. id=sd->status.inventory[i].nameid;
  5629. break;
  5630. }
  5631. }
  5632. }
  5633. script_pushint(st,id);
  5634. return 0;
  5635. }
  5636. /*==========================================
  5637. * repair [Valaris]
  5638. *------------------------------------------*/
  5639. BUILDIN_FUNC(repair)
  5640. {
  5641. int i,num;
  5642. int repaircounter=0;
  5643. TBL_PC *sd;
  5644. sd = script_rid2sd(st);
  5645. if( sd == NULL )
  5646. return 0;
  5647. num=script_getnum(st,2);
  5648. for(i=0; i<MAX_INVENTORY; i++) {
  5649. if(sd->status.inventory[i].attribute==1){
  5650. repaircounter++;
  5651. if(num==repaircounter){
  5652. sd->status.inventory[i].attribute=0;
  5653. clif_equiplist(sd);
  5654. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  5655. clif_misceffect(&sd->bl, 3);
  5656. break;
  5657. }
  5658. }
  5659. }
  5660. return 0;
  5661. }
  5662. /*==========================================
  5663. * 装備チェック
  5664. *------------------------------------------*/
  5665. BUILDIN_FUNC(getequipisequiped)
  5666. {
  5667. int i=-1,num;
  5668. TBL_PC *sd;
  5669. num=script_getnum(st,2);
  5670. sd = script_rid2sd(st);
  5671. if( sd == NULL )
  5672. return 0;
  5673. if (num > 0 && num <= ARRAYLENGTH(equip))
  5674. i=pc_checkequip(sd,equip[num-1]);
  5675. if(i >= 0)
  5676. script_pushint(st,1);
  5677. else
  5678. script_pushint(st,0);
  5679. return 0;
  5680. }
  5681. /*==========================================
  5682. * 装備品精錬可能チェック
  5683. *------------------------------------------*/
  5684. BUILDIN_FUNC(getequipisenableref)
  5685. {
  5686. int i=-1,num;
  5687. TBL_PC *sd;
  5688. num=script_getnum(st,2);
  5689. sd = script_rid2sd(st);
  5690. if( sd == NULL )
  5691. return 0;
  5692. if (num > 0 && num <= ARRAYLENGTH(equip))
  5693. i=pc_checkequip(sd,equip[num-1]);
  5694. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  5695. {
  5696. script_pushint(st,1);
  5697. } else {
  5698. script_pushint(st,0);
  5699. }
  5700. return 0;
  5701. }
  5702. /*==========================================
  5703. * 装備品鑑定チェック
  5704. *------------------------------------------*/
  5705. BUILDIN_FUNC(getequipisidentify)
  5706. {
  5707. int i=-1,num;
  5708. TBL_PC *sd;
  5709. num=script_getnum(st,2);
  5710. sd = script_rid2sd(st);
  5711. if( sd == NULL )
  5712. return 0;
  5713. if (num > 0 && num <= ARRAYLENGTH(equip))
  5714. i=pc_checkequip(sd,equip[num-1]);
  5715. if(i >= 0)
  5716. script_pushint(st,sd->status.inventory[i].identify);
  5717. else
  5718. script_pushint(st,0);
  5719. return 0;
  5720. }
  5721. /*==========================================
  5722. * 装備品精錬度
  5723. *------------------------------------------*/
  5724. BUILDIN_FUNC(getequiprefinerycnt)
  5725. {
  5726. int i=-1,num;
  5727. TBL_PC *sd;
  5728. num=script_getnum(st,2);
  5729. sd = script_rid2sd(st);
  5730. if( sd == NULL )
  5731. return 0;
  5732. if (num > 0 && num <= ARRAYLENGTH(equip))
  5733. i=pc_checkequip(sd,equip[num-1]);
  5734. if(i >= 0)
  5735. script_pushint(st,sd->status.inventory[i].refine);
  5736. else
  5737. script_pushint(st,0);
  5738. return 0;
  5739. }
  5740. /*==========================================
  5741. * 装備品武器LV
  5742. *------------------------------------------*/
  5743. BUILDIN_FUNC(getequipweaponlv)
  5744. {
  5745. int i=-1,num;
  5746. TBL_PC *sd;
  5747. num=script_getnum(st,2);
  5748. sd = script_rid2sd(st);
  5749. if( sd == NULL )
  5750. return 0;
  5751. if (num > 0 && num <= ARRAYLENGTH(equip))
  5752. i=pc_checkequip(sd,equip[num-1]);
  5753. if(i >= 0 && sd->inventory_data[i])
  5754. script_pushint(st,sd->inventory_data[i]->wlv);
  5755. else
  5756. script_pushint(st,0);
  5757. return 0;
  5758. }
  5759. /*==========================================
  5760. * 装備品精錬成功率
  5761. *------------------------------------------*/
  5762. BUILDIN_FUNC(getequippercentrefinery)
  5763. {
  5764. int i=-1,num;
  5765. TBL_PC *sd;
  5766. num=script_getnum(st,2);
  5767. sd = script_rid2sd(st);
  5768. if( sd == NULL )
  5769. return 0;
  5770. if (num > 0 && num <= ARRAYLENGTH(equip))
  5771. i=pc_checkequip(sd,equip[num-1]);
  5772. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  5773. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  5774. else
  5775. script_pushint(st,0);
  5776. return 0;
  5777. }
  5778. /*==========================================
  5779. * 精錬成功
  5780. *------------------------------------------*/
  5781. BUILDIN_FUNC(successrefitem)
  5782. {
  5783. int i=-1,num,ep;
  5784. TBL_PC *sd;
  5785. num=script_getnum(st,2);
  5786. sd = script_rid2sd(st);
  5787. if( sd == NULL )
  5788. return 0;
  5789. if (num > 0 && num <= ARRAYLENGTH(equip))
  5790. i=pc_checkequip(sd,equip[num-1]);
  5791. if(i >= 0) {
  5792. ep=sd->status.inventory[i].equip;
  5793. //Logs items, got from (N)PC scripts [Lupus]
  5794. if(log_config.enable_logs&0x40)
  5795. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5796. sd->status.inventory[i].refine++;
  5797. pc_unequipitem(sd,i,2);
  5798. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  5799. clif_delitem(sd,i,1);
  5800. //Logs items, got from (N)PC scripts [Lupus]
  5801. if(log_config.enable_logs&0x40)
  5802. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  5803. clif_additem(sd,i,1,0);
  5804. pc_equipitem(sd,i,ep);
  5805. clif_misceffect(&sd->bl,3);
  5806. if(sd->status.inventory[i].refine == MAX_REFINE &&
  5807. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  5808. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  5809. ){ // Fame point system [DracoRPG]
  5810. switch (sd->inventory_data[i]->wlv){
  5811. case 1:
  5812. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  5813. break;
  5814. case 2:
  5815. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  5816. break;
  5817. case 3:
  5818. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  5819. break;
  5820. }
  5821. }
  5822. }
  5823. return 0;
  5824. }
  5825. /*==========================================
  5826. * 精錬失敗
  5827. *------------------------------------------*/
  5828. BUILDIN_FUNC(failedrefitem)
  5829. {
  5830. int i=-1,num;
  5831. TBL_PC *sd;
  5832. num=script_getnum(st,2);
  5833. sd = script_rid2sd(st);
  5834. if( sd == NULL )
  5835. return 0;
  5836. if (num > 0 && num <= ARRAYLENGTH(equip))
  5837. i=pc_checkequip(sd,equip[num-1]);
  5838. if(i >= 0) {
  5839. //Logs items, got from (N)PC scripts [Lupus]
  5840. if(log_config.enable_logs&0x40)
  5841. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  5842. sd->status.inventory[i].refine = 0;
  5843. pc_unequipitem(sd,i,3);
  5844. // 精錬失敗エフェクトのパケット
  5845. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  5846. pc_delitem(sd,i,1,0);
  5847. // 他の人にも失敗を通知
  5848. clif_misceffect(&sd->bl,2);
  5849. }
  5850. return 0;
  5851. }
  5852. /*==========================================
  5853. *
  5854. *------------------------------------------*/
  5855. BUILDIN_FUNC(statusup)
  5856. {
  5857. int type;
  5858. TBL_PC *sd;
  5859. type=script_getnum(st,2);
  5860. sd = script_rid2sd(st);
  5861. if( sd == NULL )
  5862. return 0;
  5863. pc_statusup(sd,type);
  5864. return 0;
  5865. }
  5866. /*==========================================
  5867. *
  5868. *------------------------------------------*/
  5869. BUILDIN_FUNC(statusup2)
  5870. {
  5871. int type,val;
  5872. TBL_PC *sd;
  5873. type=script_getnum(st,2);
  5874. val=script_getnum(st,3);
  5875. sd = script_rid2sd(st);
  5876. if( sd == NULL )
  5877. return 0;
  5878. pc_statusup2(sd,type,val);
  5879. return 0;
  5880. }
  5881. /// See 'doc/item_bonus.txt'
  5882. ///
  5883. /// bonus <bonus type>,<val1>;
  5884. /// bonus2 <bonus type>,<val1>,<val2>;
  5885. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  5886. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  5887. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  5888. BUILDIN_FUNC(bonus)
  5889. {
  5890. int type;
  5891. int type2;
  5892. int type3;
  5893. int type4;
  5894. int type5;
  5895. int val;
  5896. TBL_PC* sd;
  5897. sd = script_rid2sd(st);
  5898. if( sd == NULL )
  5899. return 0; // no player attached
  5900. type = script_getnum(st,2);
  5901. switch( script_lastdata(st) ){
  5902. case 3:
  5903. val = script_getnum(st,3);
  5904. pc_bonus(sd, type, val);
  5905. break;
  5906. case 4:
  5907. type2 = script_getnum(st,3);
  5908. val = script_getnum(st,4);
  5909. pc_bonus2(sd, type, type2, val);
  5910. break;
  5911. case 5:
  5912. type2 = script_getnum(st,3);
  5913. type3 = script_getnum(st,4);
  5914. val = script_getnum(st,5);
  5915. pc_bonus3(sd, type, type2, type3, val);
  5916. break;
  5917. case 6:
  5918. type2 = script_getnum(st,3);
  5919. type3 = script_getnum(st,4);
  5920. type4 = script_getnum(st,5);
  5921. val = script_getnum(st,6);
  5922. pc_bonus4(sd, type, type2, type3, type4, val);
  5923. break;
  5924. case 7:
  5925. type2 = script_getnum(st,3);
  5926. type3 = script_getnum(st,4);
  5927. type4 = script_getnum(st,5);
  5928. type5 = script_getnum(st,6);
  5929. val = script_getnum(st,7);
  5930. pc_bonus5(sd, type, type2, type3, type4, type5, val);
  5931. break;
  5932. default:
  5933. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 2));
  5934. }
  5935. return 0;
  5936. }
  5937. /// Bonus script that has a chance of being executed on attack.
  5938. BUILDIN_FUNC(bonusautoscript)
  5939. {
  5940. int rate, flag = 0;
  5941. const char *str;
  5942. struct script_code *script;
  5943. TBL_PC* sd;
  5944. sd = script_rid2sd(st);
  5945. if( sd == NULL )
  5946. return 0;// no player attached, report source
  5947. str = script_getstr(st,2);
  5948. rate = script_getnum(st,3);
  5949. if( script_hasdata(st,4) )
  5950. flag = script_getnum(st,4);
  5951. script = parse_script(str, "autoscript bonus", 0, 0);
  5952. if (!script)
  5953. return 1;
  5954. if (!pc_autoscript_add(sd->autoscript, ARRAYLENGTH(sd->autoscript), rate, flag, script))
  5955. {
  5956. script_free_code(script);
  5957. return 1;
  5958. }
  5959. return 0;
  5960. }
  5961. /// Bonus script that has a chance of being executed when attacked.
  5962. BUILDIN_FUNC(bonusautoscript2)
  5963. {
  5964. int rate, flag = 0;
  5965. const char *str;
  5966. struct script_code *script;
  5967. TBL_PC* sd;
  5968. sd = script_rid2sd(st);
  5969. if( sd == NULL )
  5970. return 0;// no player attached, report source
  5971. str = script_getstr(st,2);
  5972. rate = script_getnum(st,3);
  5973. if( script_hasdata(st,4) )
  5974. flag = script_getnum(st,4);
  5975. script = parse_script(str, "autoscript2 bonus", 0, 0);
  5976. if (!script)
  5977. return 1;
  5978. if (!pc_autoscript_add(sd->autoscript2, ARRAYLENGTH(sd->autoscript2), rate, flag, script))
  5979. {
  5980. script_free_code(script);
  5981. return 1;
  5982. }
  5983. return 0;
  5984. }
  5985. /// Changes the level of a player skill.
  5986. /// <flag> defaults to 1
  5987. /// <flag>=0 : set the level of the skill
  5988. /// <flag>=1 : set the temporary level of the skill
  5989. /// <flag>=2 : add to the level of the skill
  5990. ///
  5991. /// skill <skill id>,<level>,<flag>
  5992. /// skill <skill id>,<level>
  5993. BUILDIN_FUNC(skill)
  5994. {
  5995. int id;
  5996. int level;
  5997. int flag = 1;
  5998. TBL_PC* sd;
  5999. sd = script_rid2sd(st);
  6000. if( sd == NULL )
  6001. return 0;// no player attached, report source
  6002. id = script_getnum(st,2);
  6003. level = script_getnum(st,3);
  6004. if( script_hasdata(st,4) )
  6005. flag = script_getnum(st,4);
  6006. pc_skill(sd, id, level, flag);
  6007. return 0;
  6008. }
  6009. /// Changes the level of a player skill.
  6010. /// like skill, but <flag> defaults to 2
  6011. ///
  6012. /// addtoskill <skill id>,<amount>,<flag>
  6013. /// addtoskill <skill id>,<amount>
  6014. ///
  6015. /// @see skill
  6016. BUILDIN_FUNC(addtoskill)
  6017. {
  6018. int id;
  6019. int level;
  6020. int flag = 2;
  6021. TBL_PC* sd;
  6022. sd = script_rid2sd(st);
  6023. if( sd == NULL )
  6024. return 0;// no player attached, report source
  6025. id = script_getnum(st,2);
  6026. level = script_getnum(st,3);
  6027. if( script_hasdata(st,4) )
  6028. flag = script_getnum(st,4);
  6029. pc_skill(sd, id, level, flag);
  6030. return 0;
  6031. }
  6032. /// Increases the level of a guild skill.
  6033. ///
  6034. /// guildskill <skill id>,<amount>;
  6035. BUILDIN_FUNC(guildskill)
  6036. {
  6037. int id;
  6038. int level;
  6039. TBL_PC* sd;
  6040. int i;
  6041. sd = script_rid2sd(st);
  6042. if( sd == NULL )
  6043. return 0;// no player attached, report source
  6044. id = script_getnum(st,2);
  6045. level = script_getnum(st,3);
  6046. for( i=0; i < level; i++ )
  6047. guild_skillup(sd, id);
  6048. return 0;
  6049. }
  6050. /// Returns the level of the player skill.
  6051. ///
  6052. /// getskilllv(<skill id>) -> <level>
  6053. BUILDIN_FUNC(getskilllv)
  6054. {
  6055. int id;
  6056. TBL_PC* sd;
  6057. sd = script_rid2sd(st);
  6058. if( sd == NULL )
  6059. return 0;// no player attached, report source
  6060. id = script_getnum(st,2);
  6061. script_pushint(st, pc_checkskill(sd,id));
  6062. return 0;
  6063. }
  6064. /// Returns the level of the guild skill.
  6065. ///
  6066. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6067. BUILDIN_FUNC(getgdskilllv)
  6068. {
  6069. int guild_id;
  6070. int skill_id;
  6071. struct guild* g;
  6072. guild_id = script_getnum(st,2);
  6073. skill_id = script_getnum(st,3);
  6074. g = guild_search(guild_id);
  6075. if( g == NULL )
  6076. script_pushint(st, -1);
  6077. else
  6078. script_pushint(st, guild_checkskill(g,skill_id));
  6079. return 0;
  6080. }
  6081. /// Returns the 'basic_skill_check' setting.
  6082. /// This config determines if the server checks the skill level of NV_BASIC
  6083. /// before allowing the basic actions.
  6084. ///
  6085. /// basicskillcheck() -> <bool>
  6086. BUILDIN_FUNC(basicskillcheck)
  6087. {
  6088. script_pushint(st, battle_config.basic_skill_check);
  6089. return 0;
  6090. }
  6091. /// Returns the GM level of the player.
  6092. ///
  6093. /// getgmlevel() -> <level>
  6094. BUILDIN_FUNC(getgmlevel)
  6095. {
  6096. TBL_PC* sd;
  6097. sd = script_rid2sd(st);
  6098. if( sd == NULL )
  6099. return 0;// no player attached, report source
  6100. script_pushint(st, pc_isGM(sd));
  6101. return 0;
  6102. }
  6103. /// Terminates the execution of this script instance.
  6104. ///
  6105. /// end
  6106. BUILDIN_FUNC(end)
  6107. {
  6108. st->state = END;
  6109. return 0;
  6110. }
  6111. /// Checks if the player has that effect state (option).
  6112. ///
  6113. /// checkoption(<option>) -> <bool>
  6114. BUILDIN_FUNC(checkoption)
  6115. {
  6116. int option;
  6117. TBL_PC* sd;
  6118. sd = script_rid2sd(st);
  6119. if( sd == NULL )
  6120. return 0;// no player attached, report source
  6121. option = script_getnum(st,2);
  6122. if( sd->sc.option&option )
  6123. script_pushint(st, 1);
  6124. else
  6125. script_pushint(st, 0);
  6126. return 0;
  6127. }
  6128. /// Checks if the player is in that body state (opt1).
  6129. ///
  6130. /// checkoption1(<opt1>) -> <bool>
  6131. BUILDIN_FUNC(checkoption1)
  6132. {
  6133. int opt1;
  6134. TBL_PC* sd;
  6135. sd = script_rid2sd(st);
  6136. if( sd == NULL )
  6137. return 0;// no player attached, report source
  6138. opt1 = script_getnum(st,2);
  6139. if( sd->sc.opt1 == opt1 )
  6140. script_pushint(st, 1);
  6141. else
  6142. script_pushint(st, 0);
  6143. return 0;
  6144. }
  6145. /// Checks if the player has that health state (opt2).
  6146. ///
  6147. /// checkoption2(<opt2>) -> <bool>
  6148. BUILDIN_FUNC(checkoption2)
  6149. {
  6150. int opt2;
  6151. TBL_PC* sd;
  6152. sd = script_rid2sd(st);
  6153. if( sd == NULL )
  6154. return 0;// no player attached, report source
  6155. opt2 = script_getnum(st,2);
  6156. if( sd->sc.opt2&opt2 )
  6157. script_pushint(st, 1);
  6158. else
  6159. script_pushint(st, 0);
  6160. return 0;
  6161. }
  6162. /// Changes the effect state (option) of the player.
  6163. /// <flag> defaults to 1
  6164. /// <flag>=0 : removes the option
  6165. /// <flag>=other : adds the option
  6166. ///
  6167. /// setoption <option>,<flag>;
  6168. /// setoption <option>;
  6169. BUILDIN_FUNC(setoption)
  6170. {
  6171. int option;
  6172. int flag = 1;
  6173. TBL_PC* sd;
  6174. sd = script_rid2sd(st);
  6175. if( sd == NULL )
  6176. return 0;// no player attached, report source
  6177. option = script_getnum(st,2);
  6178. if( script_hasdata(st,3) )
  6179. flag = script_getnum(st,3);
  6180. else if( !option ){// Request to remove everything.
  6181. flag = 0;
  6182. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6183. }
  6184. if( flag ){// Add option
  6185. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6186. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6187. pc_setoption(sd, sd->sc.option|option);
  6188. } else// Remove option
  6189. pc_setoption(sd, sd->sc.option&~option);
  6190. return 0;
  6191. }
  6192. /// Returns if the player has a cart.
  6193. ///
  6194. /// checkcart() -> <bool>
  6195. ///
  6196. /// @author Valaris
  6197. BUILDIN_FUNC(checkcart)
  6198. {
  6199. TBL_PC* sd;
  6200. sd = script_rid2sd(st);
  6201. if( sd == NULL )
  6202. return 0;// no player attached, report source
  6203. if( pc_iscarton(sd) )
  6204. script_pushint(st, 1);
  6205. else
  6206. script_pushint(st, 0);
  6207. return 0;
  6208. }
  6209. /// Sets the cart of the player.
  6210. /// <type> defaults to 1
  6211. /// <type>=0 : removes the cart
  6212. /// <type>=1 : Normal cart
  6213. /// <type>=2 : Wooden cart
  6214. /// <type>=3 : Covered cart with flowers and ferns
  6215. /// <type>=4 : Wooden cart with a Panda doll on the back
  6216. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6217. ///
  6218. /// setcart <type>;
  6219. /// setcart;
  6220. BUILDIN_FUNC(setcart)
  6221. {
  6222. int type = 1;
  6223. TBL_PC* sd;
  6224. sd = script_rid2sd(st);
  6225. if( sd == NULL )
  6226. return 0;// no player attached, report source
  6227. if( script_hasdata(st,2) )
  6228. type = script_getnum(st,2);
  6229. pc_setcart(sd, type);
  6230. return 0;
  6231. }
  6232. /// Returns if the player has a falcon.
  6233. ///
  6234. /// checkfalcon() -> <bool>
  6235. ///
  6236. /// @author Valaris
  6237. BUILDIN_FUNC(checkfalcon)
  6238. {
  6239. TBL_PC* sd;
  6240. sd = script_rid2sd(st);
  6241. if( sd == NULL )
  6242. return 0;// no player attached, report source
  6243. if( pc_isfalcon(sd) )
  6244. script_pushint(st, 1);
  6245. else
  6246. script_pushint(st, 0);
  6247. return 0;
  6248. }
  6249. /// Sets if the player has a falcon or not.
  6250. /// <flag> defaults to 1
  6251. ///
  6252. /// setfalcon <flag>;
  6253. /// setfalcon;
  6254. BUILDIN_FUNC(setfalcon)
  6255. {
  6256. int flag = 1;
  6257. TBL_PC* sd;
  6258. sd = script_rid2sd(st);
  6259. if( sd == NULL )
  6260. return 0;// no player attached, report source
  6261. if( script_hasdata(st,2) )
  6262. flag = script_getnum(st,2);
  6263. pc_setfalcon(sd, flag);
  6264. return 0;
  6265. }
  6266. /// Returns if the player is riding.
  6267. ///
  6268. /// checkriding() -> <bool>
  6269. ///
  6270. /// @author Valaris
  6271. BUILDIN_FUNC(checkriding)
  6272. {
  6273. TBL_PC* sd;
  6274. sd = script_rid2sd(st);
  6275. if( sd == NULL )
  6276. return 0;// no player attached, report source
  6277. if( pc_isriding(sd) )
  6278. script_pushint(st, 1);
  6279. else
  6280. script_pushint(st, 0);
  6281. return 0;
  6282. }
  6283. /// Sets if the player is riding.
  6284. /// <flag> defaults to 1
  6285. ///
  6286. /// setriding <flag>;
  6287. /// setriding;
  6288. BUILDIN_FUNC(setriding)
  6289. {
  6290. int flag = 1;
  6291. TBL_PC* sd;
  6292. sd = script_rid2sd(st);
  6293. if( sd == NULL )
  6294. return 0;// no player attached, report source
  6295. if( script_hasdata(st,2) )
  6296. flag = script_getnum(st,2);
  6297. pc_setriding(sd, flag);
  6298. return 0;
  6299. }
  6300. /// Sets the save point of the player.
  6301. ///
  6302. /// save "<map name>",<x>,<y>
  6303. /// savepoint "<map name>",<x>,<y>
  6304. BUILDIN_FUNC(savepoint)
  6305. {
  6306. int x;
  6307. int y;
  6308. short map;
  6309. const char* str;
  6310. TBL_PC* sd;
  6311. sd = script_rid2sd(st);
  6312. if( sd == NULL )
  6313. return 0;// no player attached, report source
  6314. str = script_getstr(st, 2);
  6315. x = script_getnum(st,3);
  6316. y = script_getnum(st,4);
  6317. map = mapindex_name2id(str);
  6318. if( map )
  6319. pc_setsavepoint(sd, map, x, y);
  6320. return 0;
  6321. }
  6322. /*==========================================
  6323. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6324. *------------------------------------------*/
  6325. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6326. {
  6327. int type;
  6328. time_t timer;
  6329. struct tm *t;
  6330. type=script_getnum(st,2);
  6331. switch(type){
  6332. case 2:
  6333. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6334. // from the system clock.)
  6335. script_pushint(st,(int)time(NULL));
  6336. break;
  6337. case 1:
  6338. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6339. time(&timer);
  6340. t=localtime(&timer);
  6341. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6342. break;
  6343. case 0:
  6344. default:
  6345. //type 0:(System Ticks)
  6346. script_pushint(st,gettick());
  6347. break;
  6348. }
  6349. return 0;
  6350. }
  6351. /*==========================================
  6352. * GetTime(Type);
  6353. * 1: Sec 2: Min 3: Hour
  6354. * 4: WeekDay 5: MonthDay 6: Month
  6355. * 7: Year
  6356. *------------------------------------------*/
  6357. BUILDIN_FUNC(gettime) /* Asgard Version */
  6358. {
  6359. int type;
  6360. time_t timer;
  6361. struct tm *t;
  6362. type=script_getnum(st,2);
  6363. time(&timer);
  6364. t=localtime(&timer);
  6365. switch(type){
  6366. case 1://Sec(0~59)
  6367. script_pushint(st,t->tm_sec);
  6368. break;
  6369. case 2://Min(0~59)
  6370. script_pushint(st,t->tm_min);
  6371. break;
  6372. case 3://Hour(0~23)
  6373. script_pushint(st,t->tm_hour);
  6374. break;
  6375. case 4://WeekDay(0~6)
  6376. script_pushint(st,t->tm_wday);
  6377. break;
  6378. case 5://MonthDay(01~31)
  6379. script_pushint(st,t->tm_mday);
  6380. break;
  6381. case 6://Month(01~12)
  6382. script_pushint(st,t->tm_mon+1);
  6383. break;
  6384. case 7://Year(20xx)
  6385. script_pushint(st,t->tm_year+1900);
  6386. break;
  6387. case 8://Year Day(01~366)
  6388. script_pushint(st,t->tm_yday+1);
  6389. break;
  6390. default://(format error)
  6391. script_pushint(st,-1);
  6392. break;
  6393. }
  6394. return 0;
  6395. }
  6396. /*==========================================
  6397. * GetTimeStr("TimeFMT", Length);
  6398. *------------------------------------------*/
  6399. BUILDIN_FUNC(gettimestr)
  6400. {
  6401. char *tmpstr;
  6402. const char *fmtstr;
  6403. int maxlen;
  6404. time_t now = time(NULL);
  6405. fmtstr=script_getstr(st,2);
  6406. maxlen=script_getnum(st,3);
  6407. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6408. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6409. tmpstr[maxlen]='\0';
  6410. script_pushstr(st,tmpstr);
  6411. return 0;
  6412. }
  6413. /*==========================================
  6414. * カプラ倉庫を開く
  6415. *------------------------------------------*/
  6416. BUILDIN_FUNC(openstorage)
  6417. {
  6418. TBL_PC* sd;
  6419. sd = script_rid2sd(st);
  6420. if( sd == NULL )
  6421. return 0;
  6422. storage_storageopen(sd);
  6423. return 0;
  6424. }
  6425. BUILDIN_FUNC(guildopenstorage)
  6426. {
  6427. TBL_PC* sd;
  6428. int ret;
  6429. sd = script_rid2sd(st);
  6430. if( sd == NULL )
  6431. return 0;
  6432. ret = storage_guild_storageopen(sd);
  6433. script_pushint(st,ret);
  6434. return 0;
  6435. }
  6436. /*==========================================
  6437. * アイテムによるスキル発動
  6438. *------------------------------------------*/
  6439. BUILDIN_FUNC(itemskill)
  6440. {
  6441. int id,lv;
  6442. TBL_PC *sd=script_rid2sd(st);
  6443. id=script_getnum(st,2);
  6444. lv=script_getnum(st,3);
  6445. if(!sd || sd->ud.skilltimer != -1)
  6446. return 0;
  6447. sd->skillitem=id;
  6448. sd->skillitemlv=lv;
  6449. clif_item_skill(sd,id,lv);
  6450. return 0;
  6451. }
  6452. /*==========================================
  6453. * アイテム作成
  6454. *------------------------------------------*/
  6455. BUILDIN_FUNC(produce)
  6456. {
  6457. int trigger;
  6458. TBL_PC* sd;
  6459. sd = script_rid2sd(st);
  6460. if( sd == NULL )
  6461. return 0;
  6462. trigger=script_getnum(st,2);
  6463. clif_skill_produce_mix_list(sd, trigger);
  6464. return 0;
  6465. }
  6466. /*==========================================
  6467. * NPCでペット作る
  6468. *------------------------------------------*/
  6469. BUILDIN_FUNC(makepet)
  6470. {
  6471. TBL_PC* sd;
  6472. int id,pet_id;
  6473. id=script_getnum(st,2);
  6474. sd = script_rid2sd(st);
  6475. if( sd == NULL )
  6476. return 0;
  6477. pet_id = search_petDB_index(id, PET_CLASS);
  6478. if (pet_id < 0)
  6479. pet_id = search_petDB_index(id, PET_EGG);
  6480. if (pet_id >= 0 && sd) {
  6481. sd->catch_target_class = pet_db[pet_id].class_;
  6482. intif_create_pet(
  6483. sd->status.account_id, sd->status.char_id,
  6484. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  6485. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  6486. 100, 0, 1, pet_db[pet_id].jname);
  6487. }
  6488. return 0;
  6489. }
  6490. /*==========================================
  6491. * NPCで経験値上げる
  6492. *------------------------------------------*/
  6493. BUILDIN_FUNC(getexp)
  6494. {
  6495. TBL_PC* sd;
  6496. int base=0,job=0;
  6497. double bonus;
  6498. sd = script_rid2sd(st);
  6499. if( sd == NULL )
  6500. return 0;
  6501. base=script_getnum(st,2);
  6502. job =script_getnum(st,3);
  6503. if(base<0 || job<0)
  6504. return 0;
  6505. // bonus for npc-given exp
  6506. bonus = battle_config.quest_exp_rate / 100.;
  6507. base = (int) cap_value(base * bonus, 0, INT_MAX);
  6508. job = (int) cap_value(job * bonus, 0, INT_MAX);
  6509. pc_gainexp(sd, NULL, base, job);
  6510. return 0;
  6511. }
  6512. /*==========================================
  6513. * Gain guild exp [Celest]
  6514. *------------------------------------------*/
  6515. BUILDIN_FUNC(guildgetexp)
  6516. {
  6517. TBL_PC* sd;
  6518. int exp;
  6519. sd = script_rid2sd(st);
  6520. if( sd == NULL )
  6521. return 0;
  6522. exp = script_getnum(st,2);
  6523. if(exp < 0)
  6524. return 0;
  6525. if(sd && sd->status.guild_id > 0)
  6526. guild_getexp (sd, exp);
  6527. return 0;
  6528. }
  6529. /*==========================================
  6530. * Changes the guild master of a guild [Skotlex]
  6531. *------------------------------------------*/
  6532. BUILDIN_FUNC(guildchangegm)
  6533. {
  6534. TBL_PC *sd;
  6535. int guild_id;
  6536. const char *name;
  6537. guild_id = script_getnum(st,2);
  6538. name = script_getstr(st,3);
  6539. sd=map_nick2sd(name);
  6540. if (!sd)
  6541. script_pushint(st,0);
  6542. else
  6543. script_pushint(st,guild_gm_change(guild_id, sd));
  6544. return 0;
  6545. }
  6546. /*==========================================
  6547. * モンスター発生
  6548. *------------------------------------------*/
  6549. BUILDIN_FUNC(monster)
  6550. {
  6551. const char* map = script_getstr(st,2);
  6552. int x = script_getnum(st,3);
  6553. int y = script_getnum(st,4);
  6554. const char* str = script_getstr(st,5);
  6555. int class_ = script_getnum(st,6);
  6556. int amount = script_getnum(st,7);
  6557. const char* event = "";
  6558. struct map_session_data* sd;
  6559. int m;
  6560. if( script_hasdata(st,8) )
  6561. {
  6562. event = script_getstr(st,8);
  6563. check_event(st, event);
  6564. }
  6565. if (class_ >= 0 && !mobdb_checkid(class_)) {
  6566. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  6567. return 1;
  6568. }
  6569. sd = map_id2sd(st->rid);
  6570. if( sd && strcmp(map,"this") == 0 )
  6571. m = sd->bl.m;
  6572. else
  6573. m = map_mapname2mapid(map);
  6574. mob_once_spawn(sd,m,x,y,str,class_,amount,event);
  6575. return 0;
  6576. }
  6577. /*==========================================
  6578. * モンスター発生
  6579. *------------------------------------------*/
  6580. BUILDIN_FUNC(areamonster)
  6581. {
  6582. const char* map = script_getstr(st,2);
  6583. int x0 = script_getnum(st,3);
  6584. int y0 = script_getnum(st,4);
  6585. int x1 = script_getnum(st,5);
  6586. int y1 = script_getnum(st,6);
  6587. const char* str = script_getstr(st,7);
  6588. int class_ = script_getnum(st,8);
  6589. int amount = script_getnum(st,9);
  6590. const char* event = "";
  6591. struct map_session_data* sd;
  6592. int m;
  6593. if( script_hasdata(st,10) )
  6594. {
  6595. event = script_getstr(st,10);
  6596. check_event(st, event);
  6597. }
  6598. sd = map_id2sd(st->rid);
  6599. if( sd && strcmp(map,"this") == 0 )
  6600. m = sd->bl.m;
  6601. else
  6602. m = map_mapname2mapid(map);
  6603. mob_once_spawn_area(sd,m,x0,y0,x1,y1,str,class_,amount,event);
  6604. return 0;
  6605. }
  6606. /*==========================================
  6607. * モンスター削除
  6608. *------------------------------------------*/
  6609. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  6610. {
  6611. TBL_MOB* md = (TBL_MOB*)bl;
  6612. char *event=va_arg(ap,char *);
  6613. int allflag=va_arg(ap,int);
  6614. if(!allflag){
  6615. if(strcmp(event,md->npc_event)==0)
  6616. status_kill(bl);
  6617. }else{
  6618. if(!md->spawn)
  6619. status_kill(bl);
  6620. }
  6621. return 0;
  6622. }
  6623. BUILDIN_FUNC(killmonster)
  6624. {
  6625. const char *mapname,*event;
  6626. int m,allflag=0;
  6627. mapname=script_getstr(st,2);
  6628. event=script_getstr(st,3);
  6629. if(strcmp(event,"All")==0)
  6630. allflag = 1;
  6631. else
  6632. check_event(st, event);
  6633. if( (m=map_mapname2mapid(mapname))<0 )
  6634. return 0;
  6635. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  6636. return 0;
  6637. }
  6638. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  6639. {
  6640. status_kill(bl);
  6641. return 0;
  6642. }
  6643. BUILDIN_FUNC(killmonsterall)
  6644. {
  6645. const char *mapname;
  6646. int m;
  6647. mapname=script_getstr(st,2);
  6648. if( (m=map_mapname2mapid(mapname))<0 )
  6649. return 0;
  6650. map_foreachinmap(buildin_killmonsterall_sub,
  6651. m,BL_MOB);
  6652. return 0;
  6653. }
  6654. /*==========================================
  6655. * Creates a clone of a player.
  6656. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  6657. *------------------------------------------*/
  6658. BUILDIN_FUNC(clone)
  6659. {
  6660. TBL_PC *sd, *msd=NULL;
  6661. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  6662. unsigned int duration = 0;
  6663. const char *map,*event="";
  6664. map=script_getstr(st,2);
  6665. x=script_getnum(st,3);
  6666. y=script_getnum(st,4);
  6667. event=script_getstr(st,5);
  6668. char_id=script_getnum(st,6);
  6669. if( script_hasdata(st,7) )
  6670. master_id=script_getnum(st,7);
  6671. if( script_hasdata(st,8) )
  6672. mode=script_getnum(st,8);
  6673. if( script_hasdata(st,9) )
  6674. flag=script_getnum(st,9);
  6675. if( script_hasdata(st,10) )
  6676. duration=script_getnum(st,10);
  6677. check_event(st, event);
  6678. m = map_mapname2mapid(map);
  6679. if (m < 0) return 0;
  6680. sd = map_charid2sd(char_id);
  6681. if (master_id) {
  6682. msd = map_charid2sd(master_id);
  6683. if (msd)
  6684. master_id = msd->bl.id;
  6685. else
  6686. master_id = 0;
  6687. }
  6688. if (sd) //Return ID of newly crafted clone.
  6689. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  6690. else //Failed to create clone.
  6691. script_pushint(st,0);
  6692. return 0;
  6693. }
  6694. /*==========================================
  6695. * イベント実行
  6696. *------------------------------------------*/
  6697. BUILDIN_FUNC(doevent)
  6698. {
  6699. const char* event = script_getstr(st,2);
  6700. check_event(st, event);
  6701. npc_event(map_id2sd(st->rid),event,0);
  6702. return 0;
  6703. }
  6704. /*==========================================
  6705. * NPC主体イベント実行
  6706. *------------------------------------------*/
  6707. BUILDIN_FUNC(donpcevent)
  6708. {
  6709. const char* event = script_getstr(st,2);
  6710. check_event(st, event);
  6711. npc_event_do(event);
  6712. return 0;
  6713. }
  6714. /// for Aegis compatibility
  6715. /// basically a specialized 'donpcevent', with the event specified as two arguments instead of one
  6716. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  6717. {
  6718. const char* npc = script_getstr(st,2);
  6719. const char* command = script_getstr(st,3);
  6720. char event[51];
  6721. snprintf(event, 51, "%s::OnCommand%s", npc, command);
  6722. check_event(st, event);
  6723. npc_event_do(event);
  6724. return 0;
  6725. }
  6726. /*==========================================
  6727. * イベントタイマー追加
  6728. *------------------------------------------*/
  6729. BUILDIN_FUNC(addtimer)
  6730. {
  6731. int tick = script_getnum(st,2);
  6732. const char* event = script_getstr(st, 3);
  6733. TBL_PC* sd;
  6734. check_event(st, event);
  6735. sd = script_rid2sd(st);
  6736. if( sd == NULL )
  6737. return 0;
  6738. pc_addeventtimer(sd,tick,event);
  6739. return 0;
  6740. }
  6741. /*==========================================
  6742. * イベントタイマー削除
  6743. *------------------------------------------*/
  6744. BUILDIN_FUNC(deltimer)
  6745. {
  6746. const char *event;
  6747. TBL_PC* sd;
  6748. event=script_getstr(st, 2);
  6749. sd = script_rid2sd(st);
  6750. if( sd == NULL )
  6751. return 0;
  6752. check_event(st, event);
  6753. pc_deleventtimer(sd,event);
  6754. return 0;
  6755. }
  6756. /*==========================================
  6757. * イベントタイマーのカウント値追加
  6758. *------------------------------------------*/
  6759. BUILDIN_FUNC(addtimercount)
  6760. {
  6761. const char *event;
  6762. int tick;
  6763. TBL_PC* sd;
  6764. event=script_getstr(st, 2);
  6765. tick=script_getnum(st,3);
  6766. sd = script_rid2sd(st);
  6767. if( sd == NULL )
  6768. return 0;
  6769. check_event(st, event);
  6770. pc_addeventtimercount(sd,event,tick);
  6771. return 0;
  6772. }
  6773. /*==========================================
  6774. * NPCタイマー初期化
  6775. *------------------------------------------*/
  6776. BUILDIN_FUNC(initnpctimer)
  6777. {
  6778. struct npc_data *nd;
  6779. int flag = 0;
  6780. if( script_hasdata(st,3) )
  6781. { //Two arguments: NPC name and attach flag.
  6782. nd = npc_name2id(script_getstr(st, 2));
  6783. flag = script_getnum(st,3);
  6784. } else
  6785. if( script_hasdata(st,2) )
  6786. { //Check if argument is numeric (flag) or string (npc name)
  6787. struct script_data *data;
  6788. data = script_getdata(st,2);
  6789. get_val(st,data);
  6790. if( data_isstring(data) ) //NPC name
  6791. nd = npc_name2id(script_getstr(st, 2));
  6792. else if( data_isint(data) ) { //Flag
  6793. nd = (struct npc_data *)map_id2bl(st->oid);
  6794. flag = script_getnum(st,3);
  6795. } else {
  6796. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6797. return 1;
  6798. }
  6799. } else
  6800. nd=(struct npc_data *)map_id2bl(st->oid);
  6801. if (!nd) return 0;
  6802. if (flag) { //Attach
  6803. TBL_PC* sd = script_rid2sd(st);
  6804. if( sd == NULL )
  6805. return 0;
  6806. nd->u.scr.rid = sd->bl.id;
  6807. }
  6808. npc_settimerevent_tick(nd,0);
  6809. npc_timerevent_start(nd, st->rid);
  6810. return 0;
  6811. }
  6812. /*==========================================
  6813. * NPCタイマー開始
  6814. *------------------------------------------*/
  6815. BUILDIN_FUNC(startnpctimer)
  6816. {
  6817. struct npc_data *nd;
  6818. int flag = 0;
  6819. if( script_hasdata(st,3) )
  6820. { //Two arguments: NPC name and attach flag.
  6821. nd = npc_name2id(script_getstr(st, 2));
  6822. flag = script_getnum(st,3);
  6823. } else
  6824. if( script_hasdata(st,2) )
  6825. { //Check if argument is numeric (flag) or string (npc name)
  6826. struct script_data *data;
  6827. data = script_getdata(st,2);
  6828. get_val(st,data);
  6829. if( data_isstring(data) ) //NPC name
  6830. nd = npc_name2id(script_getstr(st, 2));
  6831. else if( data_isint(data) ) { //Flag
  6832. nd = (struct npc_data *)map_id2bl(st->oid);
  6833. flag = script_getnum(st,3);
  6834. } else {
  6835. ShowError("startnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6836. return 1;
  6837. }
  6838. } else
  6839. nd=(struct npc_data *)map_id2bl(st->oid);
  6840. if (!nd) return 0;
  6841. if (flag) { //Attach
  6842. TBL_PC* sd = script_rid2sd(st);
  6843. if( sd == NULL )
  6844. return 0;
  6845. nd->u.scr.rid = sd->bl.id;
  6846. }
  6847. npc_timerevent_start(nd, st->rid);
  6848. return 0;
  6849. }
  6850. /*==========================================
  6851. * NPCタイマー停止
  6852. *------------------------------------------*/
  6853. BUILDIN_FUNC(stopnpctimer)
  6854. {
  6855. struct npc_data *nd;
  6856. int flag = 0;
  6857. if( script_hasdata(st,3) )
  6858. { //Two arguments: NPC name and attach flag.
  6859. nd = npc_name2id(script_getstr(st, 2));
  6860. flag = script_getnum(st,3);
  6861. } else
  6862. if( script_hasdata(st,2) )
  6863. { //Check if argument is numeric (flag) or string (npc name)
  6864. struct script_data *data;
  6865. data = script_getdata(st,2);
  6866. get_val(st,data);
  6867. if( data_isstring(data) ) //NPC name
  6868. nd = npc_name2id(script_getstr(st, 2));
  6869. else if( data_isint(data) ) { //Flag
  6870. nd = (struct npc_data *)map_id2bl(st->oid);
  6871. flag = script_getnum(st,3);
  6872. } else {
  6873. ShowError("stopnpctimer: invalid argument type #1 (needs be int or string)).\n");
  6874. return 1;
  6875. }
  6876. } else
  6877. nd=(struct npc_data *)map_id2bl(st->oid);
  6878. if (!nd) return 0;
  6879. if (flag) //Detach
  6880. nd->u.scr.rid = 0;
  6881. npc_timerevent_stop(nd);
  6882. return 0;
  6883. }
  6884. /*==========================================
  6885. * NPCタイマー情報所得
  6886. *------------------------------------------*/
  6887. BUILDIN_FUNC(getnpctimer)
  6888. {
  6889. struct npc_data *nd;
  6890. TBL_PC *sd;
  6891. int type=script_getnum(st,2);
  6892. int val=0;
  6893. if( script_hasdata(st,3) )
  6894. nd = npc_name2id(script_getstr(st,3));
  6895. else
  6896. nd = (struct npc_data *)map_id2bl(st->oid);
  6897. if (!nd || nd->bl.type != BL_NPC)
  6898. {
  6899. script_pushint(st,0);
  6900. ShowError("getnpctimer: Invalid NPC\n");
  6901. return 1;
  6902. }
  6903. switch(type){
  6904. case 0: val=npc_gettimerevent_tick(nd); break;
  6905. case 1:
  6906. if (nd->u.scr.rid) {
  6907. sd = map_id2sd(nd->u.scr.rid);
  6908. if (!sd) {
  6909. ShowError("buildin_getnpctimer: Attached player not found!\n");
  6910. break;
  6911. }
  6912. val = (sd->npc_timer_id != -1);
  6913. } else
  6914. val= (nd->u.scr.timerid !=-1);
  6915. break;
  6916. case 2: val= nd->u.scr.timeramount; break;
  6917. }
  6918. script_pushint(st,val);
  6919. return 0;
  6920. }
  6921. /*==========================================
  6922. * NPCタイマー値設定
  6923. *------------------------------------------*/
  6924. BUILDIN_FUNC(setnpctimer)
  6925. {
  6926. int tick;
  6927. struct npc_data *nd;
  6928. tick=script_getnum(st,2);
  6929. if( script_hasdata(st,3) )
  6930. nd=npc_name2id(script_getstr(st,3));
  6931. else
  6932. nd=(struct npc_data *)map_id2bl(st->oid);
  6933. npc_settimerevent_tick(nd,tick);
  6934. return 0;
  6935. }
  6936. /*==========================================
  6937. * attaches the player rid to the timer [Celest]
  6938. *------------------------------------------*/
  6939. BUILDIN_FUNC(attachnpctimer)
  6940. {
  6941. TBL_PC *sd;
  6942. struct npc_data *nd;
  6943. nd=(struct npc_data *)map_id2bl(st->oid);
  6944. if( script_hasdata(st,2) )
  6945. sd=map_nick2sd(script_getstr(st,2));
  6946. else
  6947. sd = script_rid2sd(st);
  6948. if (sd==NULL)
  6949. return 0;
  6950. nd->u.scr.rid = sd->bl.id;
  6951. return 0;
  6952. }
  6953. /*==========================================
  6954. * detaches a player rid from the timer [Celest]
  6955. *------------------------------------------*/
  6956. BUILDIN_FUNC(detachnpctimer)
  6957. {
  6958. struct npc_data *nd;
  6959. if( script_hasdata(st,2) )
  6960. nd=npc_name2id(script_getstr(st,2));
  6961. else
  6962. nd=(struct npc_data *)map_id2bl(st->oid);
  6963. nd->u.scr.rid = 0;
  6964. return 0;
  6965. }
  6966. /*==========================================
  6967. * To avoid "player not attached" script errors, this function is provided,
  6968. * it checks if there is a player attached to the current script. [Skotlex]
  6969. * If no, returns 0, if yes, returns the account_id of the attached player.
  6970. *------------------------------------------*/
  6971. BUILDIN_FUNC(playerattached)
  6972. {
  6973. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  6974. script_pushint(st,0);
  6975. else
  6976. script_pushint(st,st->rid);
  6977. return 0;
  6978. }
  6979. /*==========================================
  6980. * 天の声アナウンス
  6981. *------------------------------------------*/
  6982. BUILDIN_FUNC(announce)
  6983. {
  6984. const char *str, *color=NULL;
  6985. int flag;
  6986. str=script_getstr(st,2);
  6987. flag=script_getnum(st,3);
  6988. if (script_hasdata(st,4))
  6989. color=script_getstr(st,4);
  6990. if(flag&0x0f){
  6991. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  6992. (struct block_list *)script_rid2sd(st);
  6993. if( bl == NULL )
  6994. return 0;
  6995. if (color)
  6996. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  6997. else
  6998. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  6999. }else {
  7000. if (color)
  7001. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  7002. else
  7003. intif_GMmessage(str,(int)strlen(str)+1,flag);
  7004. }
  7005. return 0;
  7006. }
  7007. /*==========================================
  7008. * 天の声アナウンス(特定マップ)
  7009. *------------------------------------------*/
  7010. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  7011. {
  7012. char *str, *color;
  7013. int len,flag;
  7014. str=va_arg(ap,char *);
  7015. len=va_arg(ap,int);
  7016. flag=va_arg(ap,int);
  7017. color=va_arg(ap,char *);
  7018. if (color)
  7019. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  7020. else
  7021. clif_GMmessage(bl,str,len,flag|3);
  7022. return 0;
  7023. }
  7024. BUILDIN_FUNC(mapannounce)
  7025. {
  7026. const char *mapname,*str, *color=NULL;
  7027. int flag,m;
  7028. mapname=script_getstr(st,2);
  7029. str=script_getstr(st,3);
  7030. flag=script_getnum(st,4);
  7031. if (script_hasdata(st,5))
  7032. color=script_getstr(st,5);
  7033. if( (m=map_mapname2mapid(mapname))<0 )
  7034. return 0;
  7035. map_foreachinmap(buildin_mapannounce_sub,
  7036. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7037. return 0;
  7038. }
  7039. /*==========================================
  7040. * 天の声アナウンス(特定エリア)
  7041. *------------------------------------------*/
  7042. BUILDIN_FUNC(areaannounce)
  7043. {
  7044. const char *map,*str,*color=NULL;
  7045. int flag,m;
  7046. int x0,y0,x1,y1;
  7047. map=script_getstr(st,2);
  7048. x0=script_getnum(st,3);
  7049. y0=script_getnum(st,4);
  7050. x1=script_getnum(st,5);
  7051. y1=script_getnum(st,6);
  7052. str=script_getstr(st,7);
  7053. flag=script_getnum(st,8);
  7054. if (script_hasdata(st,9))
  7055. color=script_getstr(st,9);
  7056. if( (m=map_mapname2mapid(map))<0 )
  7057. return 0;
  7058. map_foreachinarea(buildin_mapannounce_sub,
  7059. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7060. return 0;
  7061. }
  7062. /*==========================================
  7063. * ユーザー数所得
  7064. *------------------------------------------*/
  7065. BUILDIN_FUNC(getusers)
  7066. {
  7067. int flag=script_getnum(st,2);
  7068. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7069. int val=0;
  7070. switch(flag&0x07){
  7071. case 0: val=map[bl->m].users; break;
  7072. case 1: val=map_getusers(); break;
  7073. }
  7074. script_pushint(st,val);
  7075. return 0;
  7076. }
  7077. /*==========================================
  7078. * Works like @WHO - displays all online users names in window
  7079. *------------------------------------------*/
  7080. BUILDIN_FUNC(getusersname)
  7081. {
  7082. TBL_PC *sd, *pl_sd;
  7083. int disp_num=1;
  7084. struct s_mapiterator* iter;
  7085. sd = script_rid2sd(st);
  7086. if (!sd) return 0;
  7087. iter = mapit_getallusers();
  7088. for( pl_sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); pl_sd = (TBL_PC*)mapit_next(iter) )
  7089. {
  7090. if( battle_config.hide_GM_session && pc_isGM(pl_sd) )
  7091. continue; // skip hidden GMs
  7092. if((disp_num++)%10==0)
  7093. clif_scriptnext(sd,st->oid);
  7094. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7095. }
  7096. mapit_free(iter);
  7097. return 0;
  7098. }
  7099. /*==========================================
  7100. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7101. *------------------------------------------*/
  7102. BUILDIN_FUNC(getmapguildusers)
  7103. {
  7104. const char *str;
  7105. int m, gid;
  7106. int i=0,c=0;
  7107. struct guild *g = NULL;
  7108. str=script_getstr(st,2);
  7109. gid=script_getnum(st,3);
  7110. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7111. script_pushint(st,-1);
  7112. return 0;
  7113. }
  7114. g = guild_search(gid);
  7115. if (g){
  7116. for(i = 0; i < g->max_member; i++)
  7117. {
  7118. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7119. c++;
  7120. }
  7121. }
  7122. script_pushint(st,c);
  7123. return 0;
  7124. }
  7125. /*==========================================
  7126. * マップ指定ユーザー数所得
  7127. *------------------------------------------*/
  7128. BUILDIN_FUNC(getmapusers)
  7129. {
  7130. const char *str;
  7131. int m;
  7132. str=script_getstr(st,2);
  7133. if( (m=map_mapname2mapid(str))< 0){
  7134. script_pushint(st,-1);
  7135. return 0;
  7136. }
  7137. script_pushint(st,map[m].users);
  7138. return 0;
  7139. }
  7140. /*==========================================
  7141. * エリア指定ユーザー数所得
  7142. *------------------------------------------*/
  7143. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7144. {
  7145. int *users=va_arg(ap,int *);
  7146. (*users)++;
  7147. return 0;
  7148. }
  7149. BUILDIN_FUNC(getareausers)
  7150. {
  7151. const char *str;
  7152. int m,x0,y0,x1,y1,users=0;
  7153. str=script_getstr(st,2);
  7154. x0=script_getnum(st,3);
  7155. y0=script_getnum(st,4);
  7156. x1=script_getnum(st,5);
  7157. y1=script_getnum(st,6);
  7158. if( (m=map_mapname2mapid(str))< 0){
  7159. script_pushint(st,-1);
  7160. return 0;
  7161. }
  7162. map_foreachinarea(buildin_getareausers_sub,
  7163. m,x0,y0,x1,y1,BL_PC,&users);
  7164. script_pushint(st,users);
  7165. return 0;
  7166. }
  7167. /*==========================================
  7168. * エリア指定ドロップアイテム数所得
  7169. *------------------------------------------*/
  7170. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7171. {
  7172. int item=va_arg(ap,int);
  7173. int *amount=va_arg(ap,int *);
  7174. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7175. if(drop->item_data.nameid==item)
  7176. (*amount)+=drop->item_data.amount;
  7177. return 0;
  7178. }
  7179. BUILDIN_FUNC(getareadropitem)
  7180. {
  7181. const char *str;
  7182. int m,x0,y0,x1,y1,item,amount=0;
  7183. struct script_data *data;
  7184. str=script_getstr(st,2);
  7185. x0=script_getnum(st,3);
  7186. y0=script_getnum(st,4);
  7187. x1=script_getnum(st,5);
  7188. y1=script_getnum(st,6);
  7189. data=script_getdata(st,7);
  7190. get_val(st,data);
  7191. if( data_isstring(data) ){
  7192. const char *name=conv_str(st,data);
  7193. struct item_data *item_data = itemdb_searchname(name);
  7194. item=UNKNOWN_ITEM_ID;
  7195. if( item_data )
  7196. item=item_data->nameid;
  7197. }else
  7198. item=conv_num(st,data);
  7199. if( (m=map_mapname2mapid(str))< 0){
  7200. script_pushint(st,-1);
  7201. return 0;
  7202. }
  7203. map_foreachinarea(buildin_getareadropitem_sub,
  7204. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7205. script_pushint(st,amount);
  7206. return 0;
  7207. }
  7208. /*==========================================
  7209. * NPCの有効化
  7210. *------------------------------------------*/
  7211. BUILDIN_FUNC(enablenpc)
  7212. {
  7213. const char *str;
  7214. str=script_getstr(st,2);
  7215. npc_enable(str,1);
  7216. return 0;
  7217. }
  7218. /*==========================================
  7219. * NPCの無効化
  7220. *------------------------------------------*/
  7221. BUILDIN_FUNC(disablenpc)
  7222. {
  7223. const char *str;
  7224. str=script_getstr(st,2);
  7225. npc_enable(str,0);
  7226. return 0;
  7227. }
  7228. /*==========================================
  7229. * 隠れているNPCの表示
  7230. *------------------------------------------*/
  7231. BUILDIN_FUNC(hideoffnpc)
  7232. {
  7233. const char *str;
  7234. str=script_getstr(st,2);
  7235. npc_enable(str,2);
  7236. return 0;
  7237. }
  7238. /*==========================================
  7239. * NPCをハイディング
  7240. *------------------------------------------*/
  7241. BUILDIN_FUNC(hideonnpc)
  7242. {
  7243. const char *str;
  7244. str=script_getstr(st,2);
  7245. npc_enable(str,4);
  7246. return 0;
  7247. }
  7248. /// Starts a status effect on the target unit or on the attached player.
  7249. ///
  7250. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7251. BUILDIN_FUNC(sc_start)
  7252. {
  7253. struct block_list* bl;
  7254. int type;
  7255. int tick;
  7256. int val1;
  7257. int val4 = 0;
  7258. type = script_getnum(st,2);
  7259. tick = script_getnum(st,3);
  7260. val1 = script_getnum(st,4);
  7261. if( script_hasdata(st,5) )
  7262. bl = map_id2bl(script_getnum(st,5));
  7263. else
  7264. bl = map_id2bl(st->rid);
  7265. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7266. {// When there isn't a duration specified, try to get it from the skill_db
  7267. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7268. }
  7269. if( potion_flag == 1 && potion_target )
  7270. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7271. bl = map_id2bl(potion_target);
  7272. tick /= 2;// Thrown potions only last half.
  7273. val4 = 1;// Mark that this was a thrown sc_effect
  7274. }
  7275. if( bl )
  7276. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 2);
  7277. return 0;
  7278. }
  7279. /// Starts a status effect on the target unit or on the attached player.
  7280. ///
  7281. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7282. BUILDIN_FUNC(sc_start2)
  7283. {
  7284. struct block_list* bl;
  7285. int type;
  7286. int tick;
  7287. int val1;
  7288. int val4 = 0;
  7289. int rate;
  7290. type = script_getnum(st,2);
  7291. tick = script_getnum(st,3);
  7292. val1 = script_getnum(st,4);
  7293. rate = script_getnum(st,5);
  7294. if( script_hasdata(st,6) )
  7295. bl = map_id2bl(script_getnum(st,6));
  7296. else
  7297. bl = map_id2bl(st->rid);
  7298. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7299. {// When there isn't a duration specified, try to get it from the skill_db
  7300. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7301. }
  7302. if( potion_flag == 1 && potion_target )
  7303. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7304. bl = map_id2bl(potion_target);
  7305. tick /= 2;// Thrown potions only last half.
  7306. val4 = 1;// Mark that this was a thrown sc_effect
  7307. }
  7308. if( bl )
  7309. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 2);
  7310. return 0;
  7311. }
  7312. /// Starts a status effect on the target unit or on the attached player.
  7313. ///
  7314. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7315. BUILDIN_FUNC(sc_start4)
  7316. {
  7317. struct block_list* bl;
  7318. int type;
  7319. int tick;
  7320. int val1;
  7321. int val2;
  7322. int val3;
  7323. int val4;
  7324. type = script_getnum(st,2);
  7325. tick = script_getnum(st,3);
  7326. val1 = script_getnum(st,4);
  7327. val2 = script_getnum(st,5);
  7328. val3 = script_getnum(st,6);
  7329. val4 = script_getnum(st,7);
  7330. if( script_hasdata(st,8) )
  7331. bl = map_id2bl(script_getnum(st,8));
  7332. else
  7333. bl = map_id2bl(st->rid);
  7334. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7335. {// When there isn't a duration specified, try to get it from the skill_db
  7336. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7337. }
  7338. if( potion_flag == 1 && potion_target )
  7339. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7340. bl = map_id2bl(potion_target);
  7341. tick /= 2;// Thrown potions only last half.
  7342. }
  7343. if( bl )
  7344. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 2);
  7345. return 0;
  7346. }
  7347. /// Ends one or all status effects on the target unit or on the attached player.
  7348. ///
  7349. /// sc_end <effect_id>{,<unit_id>};
  7350. BUILDIN_FUNC(sc_end)
  7351. {
  7352. struct block_list* bl;
  7353. int type;
  7354. type = script_getnum(st,2);
  7355. if( script_hasdata(st,3) )
  7356. bl = map_id2bl(script_getnum(st,3));
  7357. else
  7358. bl = map_id2bl(st->rid);
  7359. if( potion_flag==1 && potion_target )
  7360. {//##TODO how does this work [FlavioJS]
  7361. bl = map_id2bl(potion_target);
  7362. }
  7363. if( !bl ) return 0;
  7364. if( type >= 0 && type < SC_MAX )
  7365. {
  7366. struct status_change *sc = status_get_sc(bl);
  7367. struct status_change_entry *sce = sc?sc->data[type]:NULL;
  7368. if (!sce) return 0;
  7369. //This should help status_change_end force disabling the SC in case it has no limit.
  7370. sce->val1 = sce->val2 = sce->val3 = sce->val4 = 0;
  7371. status_change_end(bl, type, INVALID_TIMER);
  7372. } else
  7373. status_change_clear(bl, 2);// remove all effects
  7374. return 0;
  7375. }
  7376. /*==========================================
  7377. * 状態異常耐性を計算した確率を返す
  7378. *------------------------------------------*/
  7379. BUILDIN_FUNC(getscrate)
  7380. {
  7381. struct block_list *bl;
  7382. int type,rate;
  7383. type=script_getnum(st,2);
  7384. rate=script_getnum(st,3);
  7385. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7386. bl = map_id2bl(script_getnum(st,4));
  7387. else
  7388. bl = map_id2bl(st->rid);
  7389. if (bl)
  7390. rate = status_get_sc_def(bl,type, 10000, 10000, 0);
  7391. script_pushint(st,rate);
  7392. return 0;
  7393. }
  7394. /*==========================================
  7395. *
  7396. *------------------------------------------*/
  7397. BUILDIN_FUNC(debugmes)
  7398. {
  7399. const char *str;
  7400. str=script_getstr(st,2);
  7401. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7402. return 0;
  7403. }
  7404. /*==========================================
  7405. *捕獲アイテム使用
  7406. *------------------------------------------*/
  7407. BUILDIN_FUNC(catchpet)
  7408. {
  7409. int pet_id;
  7410. TBL_PC *sd;
  7411. pet_id= script_getnum(st,2);
  7412. sd=script_rid2sd(st);
  7413. if( sd == NULL )
  7414. return 0;
  7415. pet_catch_process1(sd,pet_id);
  7416. return 0;
  7417. }
  7418. /*==========================================
  7419. * [orn]
  7420. *------------------------------------------*/
  7421. BUILDIN_FUNC(homunculus_evolution)
  7422. {
  7423. TBL_PC *sd;
  7424. sd=script_rid2sd(st);
  7425. if( sd == NULL )
  7426. return 0;
  7427. if(merc_is_hom_active(sd->hd))
  7428. {
  7429. if (sd->hd->homunculus.intimacy > 91000)
  7430. merc_hom_evolution(sd->hd);
  7431. else
  7432. clif_emotion(&sd->hd->bl, 4) ; //swt
  7433. }
  7434. return 0;
  7435. }
  7436. // [Zephyrus]
  7437. BUILDIN_FUNC(homunculus_shuffle)
  7438. {
  7439. TBL_PC *sd;
  7440. sd=script_rid2sd(st);
  7441. if( sd == NULL )
  7442. return 0;
  7443. if(merc_is_hom_active(sd->hd))
  7444. merc_hom_shuffle(sd->hd);
  7445. return 0;
  7446. }
  7447. //These two functions bring the eA MAPID_* class functionality to scripts.
  7448. BUILDIN_FUNC(eaclass)
  7449. {
  7450. int class_;
  7451. if( script_hasdata(st,2) )
  7452. class_ = script_getnum(st,2);
  7453. else {
  7454. TBL_PC *sd;
  7455. sd=script_rid2sd(st);
  7456. if (!sd) {
  7457. script_pushint(st,-1);
  7458. return 0;
  7459. }
  7460. class_ = sd->status.class_;
  7461. }
  7462. script_pushint(st,pc_jobid2mapid(class_));
  7463. return 0;
  7464. }
  7465. BUILDIN_FUNC(roclass)
  7466. {
  7467. int class_ =script_getnum(st,2);
  7468. int sex;
  7469. if( script_hasdata(st,3) )
  7470. sex = script_getnum(st,3);
  7471. else {
  7472. TBL_PC *sd;
  7473. if (st->rid && (sd=script_rid2sd(st)))
  7474. sex = sd->status.sex;
  7475. else
  7476. sex = 1; //Just use male when not found.
  7477. }
  7478. script_pushint(st,pc_mapid2jobid(class_, sex));
  7479. return 0;
  7480. }
  7481. /*==========================================
  7482. *携帯卵孵化機使用
  7483. *------------------------------------------*/
  7484. BUILDIN_FUNC(birthpet)
  7485. {
  7486. TBL_PC *sd;
  7487. sd=script_rid2sd(st);
  7488. if( sd == NULL )
  7489. return 0;
  7490. clif_sendegg(sd);
  7491. return 0;
  7492. }
  7493. /*==========================================
  7494. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7495. *------------------------------------------*/
  7496. BUILDIN_FUNC(resetlvl)
  7497. {
  7498. TBL_PC *sd;
  7499. int type=script_getnum(st,2);
  7500. sd=script_rid2sd(st);
  7501. if( sd == NULL )
  7502. return 0;
  7503. pc_resetlvl(sd,type);
  7504. return 0;
  7505. }
  7506. /*==========================================
  7507. * ステータスリセット
  7508. *------------------------------------------*/
  7509. BUILDIN_FUNC(resetstatus)
  7510. {
  7511. TBL_PC *sd;
  7512. sd=script_rid2sd(st);
  7513. pc_resetstate(sd);
  7514. return 0;
  7515. }
  7516. /*==========================================
  7517. * script command resetskill
  7518. *------------------------------------------*/
  7519. BUILDIN_FUNC(resetskill)
  7520. {
  7521. TBL_PC *sd;
  7522. sd=script_rid2sd(st);
  7523. pc_resetskill(sd,1);
  7524. return 0;
  7525. }
  7526. /*==========================================
  7527. * Counts total amount of skill points.
  7528. *------------------------------------------*/
  7529. BUILDIN_FUNC(skillpointcount)
  7530. {
  7531. TBL_PC *sd;
  7532. sd=script_rid2sd(st);
  7533. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  7534. return 0;
  7535. }
  7536. /*==========================================
  7537. *
  7538. *------------------------------------------*/
  7539. BUILDIN_FUNC(changebase)
  7540. {
  7541. TBL_PC *sd=NULL;
  7542. int vclass;
  7543. if( script_hasdata(st,3) )
  7544. sd=map_id2sd(script_getnum(st,3));
  7545. else
  7546. sd=script_rid2sd(st);
  7547. if(sd == NULL)
  7548. return 0;
  7549. vclass = script_getnum(st,2);
  7550. if(vclass == JOB_WEDDING)
  7551. {
  7552. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  7553. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  7554. )
  7555. return 0;
  7556. }
  7557. if(!sd->disguise && vclass != sd->vd.class_) {
  7558. status_set_viewdata(&sd->bl, vclass);
  7559. //Updated client view. Base, Weapon and Cloth Colors.
  7560. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  7561. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  7562. if (sd->vd.cloth_color)
  7563. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  7564. }
  7565. return 0;
  7566. }
  7567. /*==========================================
  7568. * 性別変換
  7569. *------------------------------------------*/
  7570. BUILDIN_FUNC(changesex)
  7571. {
  7572. TBL_PC *sd = NULL;
  7573. sd = script_rid2sd(st);
  7574. if (sd->status.sex == 0) {
  7575. sd->status.sex = 1;
  7576. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7577. sd->status.class_ -= 1;
  7578. } else if (sd->status.sex == 1) {
  7579. sd->status.sex = 0;
  7580. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  7581. sd->status.class_ += 1;
  7582. }
  7583. chrif_char_ask_name(-1, sd->status.name, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex
  7584. chrif_save(sd,0);
  7585. return 0;
  7586. }
  7587. /*==========================================
  7588. * Works like 'announce' but outputs in the common chat window
  7589. *------------------------------------------*/
  7590. BUILDIN_FUNC(globalmes)
  7591. {
  7592. struct block_list *bl = map_id2bl(st->oid);
  7593. struct npc_data *nd = (struct npc_data *)bl;
  7594. const char *name=NULL,*mes;
  7595. mes=script_getstr(st,2); // メッセージの取得
  7596. if(mes==NULL) return 0;
  7597. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  7598. name=script_getstr(st,3);
  7599. } else {
  7600. name=nd->name;
  7601. }
  7602. npc_globalmessage(name,mes); // グローバルメッセージ送信
  7603. return 0;
  7604. }
  7605. /////////////////////////////////////////////////////////////////////
  7606. // NPC waiting room (chat room)
  7607. //
  7608. /// Creates a waiting room (chat room) for this npc.
  7609. ///
  7610. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  7611. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  7612. /// waitingroom "<title>",<limit>,"<event>";
  7613. /// waitingroom "<title>",<limit>;
  7614. BUILDIN_FUNC(waitingroom)
  7615. {
  7616. struct npc_data* nd;
  7617. const char* title;
  7618. const char* ev = "";
  7619. int limit;
  7620. int trigger = 0;
  7621. int pub = 1;
  7622. title = script_getstr(st, 2);
  7623. limit = script_getnum(st, 3);
  7624. if( script_hasdata(st,5) )
  7625. {
  7626. struct script_data* last = script_getdata(st, 5);
  7627. get_val(st, last);
  7628. if( data_isstring(last) )
  7629. {// ,<trigger>,"<event>"
  7630. trigger = script_getnum(st, 4);
  7631. ev = script_getstr(st, 5);
  7632. }
  7633. else
  7634. {// ,"<event>",<trigger>
  7635. ev = script_getstr(st, 4);
  7636. trigger = script_getnum(st,5);
  7637. }
  7638. }
  7639. else if( script_hasdata(st,4) )
  7640. {// ,"<event>"
  7641. ev = script_getstr(st, 4);
  7642. trigger = limit;
  7643. }
  7644. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  7645. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  7646. return 0;
  7647. }
  7648. /// Removes the waiting room of the current or target npc.
  7649. ///
  7650. /// delwaitingroom "<npc_name>";
  7651. /// delwaitingroom;
  7652. BUILDIN_FUNC(delwaitingroom)
  7653. {
  7654. struct npc_data* nd;
  7655. if( script_hasdata(st,2) )
  7656. nd = npc_name2id(script_getstr(st, 2));
  7657. else
  7658. nd = (struct npc_data *)map_id2bl(st->oid);
  7659. if( nd != NULL )
  7660. chat_deletenpcchat(nd);
  7661. return 0;
  7662. }
  7663. /// Kicks all the players from the waiting room of the current or target npc.
  7664. ///
  7665. /// kickwaitingroomall "<npc_name>";
  7666. /// kickwaitingroomall;
  7667. BUILDIN_FUNC(waitingroomkickall)
  7668. {
  7669. struct npc_data* nd;
  7670. struct chat_data* cd;
  7671. if( script_hasdata(st,2) )
  7672. nd = npc_name2id(script_getstr(st,2));
  7673. else
  7674. nd = (struct npc_data *)map_id2bl(st->oid);
  7675. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7676. chat_npckickall(cd);
  7677. return 0;
  7678. }
  7679. /// Enables the waiting room event of the current or target npc.
  7680. ///
  7681. /// enablewaitingroomevent "<npc_name>";
  7682. /// enablewaitingroomevent;
  7683. BUILDIN_FUNC(enablewaitingroomevent)
  7684. {
  7685. struct npc_data* nd;
  7686. struct chat_data* cd;
  7687. if( script_hasdata(st,2) )
  7688. nd = npc_name2id(script_getstr(st, 2));
  7689. else
  7690. nd = (struct npc_data *)map_id2bl(st->oid);
  7691. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7692. chat_enableevent(cd);
  7693. return 0;
  7694. }
  7695. /// Disables the waiting room event of the current or target npc.
  7696. ///
  7697. /// disablewaitingroomevent "<npc_name>";
  7698. /// disablewaitingroomevent;
  7699. BUILDIN_FUNC(disablewaitingroomevent)
  7700. {
  7701. struct npc_data *nd;
  7702. struct chat_data *cd;
  7703. if( script_hasdata(st,2) )
  7704. nd = npc_name2id(script_getstr(st, 2));
  7705. else
  7706. nd = (struct npc_data *)map_id2bl(st->oid);
  7707. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  7708. chat_disableevent(cd);
  7709. return 0;
  7710. }
  7711. /// Returns info on the waiting room of the current or target npc.
  7712. /// Returns -1 if the type unknown
  7713. /// <type>=0 : current number of users
  7714. /// <type>=1 : maximum number of users allowed
  7715. /// <type>=2 : the number of users that trigger the event
  7716. /// <type>=3 : if the trigger is disabled
  7717. /// <type>=4 : the title of the waiting room
  7718. /// <type>=5 : the password of the waiting room
  7719. /// <type>=16 : the name of the waiting room event
  7720. /// <type>=32 : if the waiting room is full
  7721. /// <type>=33 : if there are enough users to trigger the event
  7722. ///
  7723. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  7724. /// getwaitingroomstate(<type>) -> <info>
  7725. BUILDIN_FUNC(getwaitingroomstate)
  7726. {
  7727. struct npc_data *nd;
  7728. struct chat_data *cd;
  7729. int type;
  7730. type = script_getnum(st,2);
  7731. if( script_hasdata(st,3) )
  7732. nd = npc_name2id(script_getstr(st, 3));
  7733. else
  7734. nd = (struct npc_data *)map_id2bl(st->oid);
  7735. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7736. {
  7737. script_pushint(st, -1);
  7738. return 0;
  7739. }
  7740. switch(type)
  7741. {
  7742. case 0: script_pushint(st, cd->users); break;
  7743. case 1: script_pushint(st, cd->limit); break;
  7744. case 2: script_pushint(st, cd->trigger&0x7f); break;
  7745. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  7746. case 4: script_pushstrcopy(st, cd->title); break;
  7747. case 5: script_pushstrcopy(st, cd->pass); break;
  7748. case 16: script_pushstrcopy(st, cd->npc_event);break;
  7749. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  7750. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  7751. default: script_pushint(st, -1); break;
  7752. }
  7753. return 0;
  7754. }
  7755. /// Warps the trigger or target amount of players to the target map and position.
  7756. /// Players are automatically removed from the waiting room.
  7757. /// Those waiting the longest will get warped first.
  7758. /// The target map can be "Random" for a random position in the current map,
  7759. /// and "SavePoint" for the savepoint map+position.
  7760. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  7761. ///
  7762. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  7763. /// The total number of teleported players is put into $@warpwaitingpcnum
  7764. ///
  7765. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  7766. /// warpwaitingpc "<map name>",<x>,<y>;
  7767. BUILDIN_FUNC(warpwaitingpc)
  7768. {
  7769. int x;
  7770. int y;
  7771. int i;
  7772. int n;
  7773. const char* map_name;
  7774. struct npc_data* nd;
  7775. struct chat_data* cd;
  7776. TBL_PC* sd;
  7777. nd = (struct npc_data *)map_id2bl(st->oid);
  7778. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  7779. return 0;
  7780. map_name = script_getstr(st,2);
  7781. x = script_getnum(st,3);
  7782. y = script_getnum(st,4);
  7783. n = cd->trigger&0x7f;
  7784. if( script_hasdata(st,5) )
  7785. n = script_getnum(st,5);
  7786. for( i = 0; i < n && cd->users > 0; i++ )
  7787. {
  7788. sd = cd->usersd[0];
  7789. if( sd == NULL )
  7790. {
  7791. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  7792. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  7793. continue;// Broken npc chat room?
  7794. }
  7795. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  7796. if( strcmp(map_name,"Random") == 0 )
  7797. pc_randomwarp(sd,3);
  7798. else if( strcmp(map_name,"SavePoint") == 0 )
  7799. {
  7800. if( map[sd->bl.m].flag.noteleport )
  7801. return 0;// can't teleport on this map
  7802. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  7803. }
  7804. else
  7805. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  7806. }
  7807. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  7808. return 0;
  7809. }
  7810. /////////////////////////////////////////////////////////////////////
  7811. // ...
  7812. //
  7813. /*==========================================
  7814. * RIDのアタッチ
  7815. *------------------------------------------*/
  7816. BUILDIN_FUNC(attachrid)
  7817. {
  7818. st->rid=script_getnum(st,2);
  7819. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  7820. return 0;
  7821. }
  7822. /*==========================================
  7823. * RIDのデタッチ
  7824. *------------------------------------------*/
  7825. BUILDIN_FUNC(detachrid)
  7826. {
  7827. st->rid=0;
  7828. return 0;
  7829. }
  7830. /*==========================================
  7831. * 存在チェック
  7832. *------------------------------------------*/
  7833. BUILDIN_FUNC(isloggedin)
  7834. {
  7835. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  7836. if (script_hasdata(st,3) && sd &&
  7837. sd->status.char_id != script_getnum(st,3))
  7838. sd = NULL;
  7839. push_val(st->stack,C_INT,sd!=NULL);
  7840. return 0;
  7841. }
  7842. /*==========================================
  7843. *
  7844. *------------------------------------------*/
  7845. BUILDIN_FUNC(setmapflagnosave)
  7846. {
  7847. int m,x,y;
  7848. unsigned short mapindex;
  7849. const char *str,*str2;
  7850. str=script_getstr(st,2);
  7851. str2=script_getstr(st,3);
  7852. x=script_getnum(st,4);
  7853. y=script_getnum(st,5);
  7854. m = map_mapname2mapid(str);
  7855. mapindex = mapindex_name2id(str2);
  7856. if(m >= 0 && mapindex) {
  7857. map[m].flag.nosave=1;
  7858. map[m].save.map=mapindex;
  7859. map[m].save.x=x;
  7860. map[m].save.y=y;
  7861. }
  7862. return 0;
  7863. }
  7864. BUILDIN_FUNC(getmapflag)
  7865. {
  7866. int m,i;
  7867. const char *str;
  7868. str=script_getstr(st,2);
  7869. i=script_getnum(st,3);
  7870. m = map_mapname2mapid(str);
  7871. if(m >= 0) {
  7872. switch(i) {
  7873. case MF_NOMEMO: script_pushint(st,map[m].flag.nomemo); break;
  7874. case MF_NOTELEPORT: script_pushint(st,map[m].flag.noteleport); break;
  7875. case MF_NOBRANCH: script_pushint(st,map[m].flag.nobranch); break;
  7876. case MF_NOPENALTY: script_pushint(st,map[m].flag.noexppenalty); break;
  7877. case MF_NOZENYPENALTY: script_pushint(st,map[m].flag.nozenypenalty); break;
  7878. case MF_PVP: script_pushint(st,map[m].flag.pvp); break;
  7879. case MF_PVP_NOPARTY: script_pushint(st,map[m].flag.pvp_noparty); break;
  7880. case MF_PVP_NOGUILD: script_pushint(st,map[m].flag.pvp_noguild); break;
  7881. case MF_GVG: script_pushint(st,map[m].flag.gvg); break;
  7882. case MF_GVG_NOPARTY: script_pushint(st,map[m].flag.gvg_noparty); break;
  7883. case MF_GVG_DUNGEON: script_pushint(st,map[m].flag.gvg_dungeon); break;
  7884. case MF_GVG_CASTLE: script_pushint(st,map[m].flag.gvg_castle); break;
  7885. case MF_NOTRADE: script_pushint(st,map[m].flag.notrade); break;
  7886. case MF_NODROP: script_pushint(st,map[m].flag.nodrop); break;
  7887. case MF_NOSKILL: script_pushint(st,map[m].flag.noskill); break;
  7888. case MF_NOWARP: script_pushint(st,map[m].flag.nowarp); break;
  7889. case MF_NOICEWALL: script_pushint(st,map[m].flag.noicewall); break;
  7890. case MF_SNOW: script_pushint(st,map[m].flag.snow); break;
  7891. case MF_CLOUDS: script_pushint(st,map[m].flag.clouds); break;
  7892. case MF_CLOUDS2: script_pushint(st,map[m].flag.clouds2); break;
  7893. case MF_FOG: script_pushint(st,map[m].flag.fog); break;
  7894. case MF_FIREWORKS: script_pushint(st,map[m].flag.fireworks); break;
  7895. case MF_SAKURA: script_pushint(st,map[m].flag.sakura); break;
  7896. case MF_LEAVES: script_pushint(st,map[m].flag.leaves); break;
  7897. case MF_RAIN: script_pushint(st,map[m].flag.rain); break;
  7898. case MF_INDOORS: script_pushint(st,map[m].flag.indoors); break;
  7899. case MF_NIGHTENABLED: script_pushint(st,map[m].flag.nightenabled); break;
  7900. case MF_NOGO: script_pushint(st,map[m].flag.nogo); break;
  7901. case MF_NOBASEEXP: script_pushint(st,map[m].flag.nobaseexp); break;
  7902. case MF_NOJOBEXP: script_pushint(st,map[m].flag.nojobexp); break;
  7903. case MF_NOMOBLOOT: script_pushint(st,map[m].flag.nomobloot); break;
  7904. case MF_NOMVPLOOT: script_pushint(st,map[m].flag.nomvploot); break;
  7905. case MF_NORETURN: script_pushint(st,map[m].flag.noreturn); break;
  7906. case MF_NOWARPTO: script_pushint(st,map[m].flag.nowarpto); break;
  7907. case MF_NIGHTMAREDROP: script_pushint(st,map[m].flag.pvp_nightmaredrop); break;
  7908. case MF_RESTRICTED: script_pushint(st,map[m].flag.restricted); break;
  7909. case MF_NOCOMMAND: script_pushint(st,map[m].nocommand); break;
  7910. case MF_JEXP: script_pushint(st,map[m].jexp); break;
  7911. case MF_BEXP: script_pushint(st,map[m].bexp); break;
  7912. case MF_NOVENDING: script_pushint(st,map[m].flag.novending); break;
  7913. case MF_LOADEVENT: script_pushint(st,map[m].flag.loadevent); break;
  7914. case MF_NOCHAT: script_pushint(st,map[m].flag.nochat); break;
  7915. case MF_PARTYLOCK: script_pushint(st,map[m].flag.partylock); break;
  7916. case MF_GUILDLOCK: script_pushint(st,map[m].flag.guildlock); break;
  7917. }
  7918. }
  7919. return 0;
  7920. }
  7921. BUILDIN_FUNC(setmapflag)
  7922. {
  7923. int m,i;
  7924. const char *str;
  7925. const char *val=NULL;
  7926. str=script_getstr(st,2);
  7927. i=script_getnum(st,3);
  7928. if(script_hasdata(st,4)){
  7929. val=script_getstr(st,4);
  7930. }
  7931. m = map_mapname2mapid(str);
  7932. if(m >= 0) {
  7933. switch(i) {
  7934. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  7935. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  7936. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  7937. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  7938. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  7939. case MF_PVP: map[m].flag.pvp=1; break;
  7940. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  7941. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  7942. case MF_GVG: map[m].flag.gvg=1; break;
  7943. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  7944. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  7945. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  7946. case MF_NOTRADE: map[m].flag.notrade=1; break;
  7947. case MF_NODROP: map[m].flag.nodrop=1; break;
  7948. case MF_NOSKILL: map[m].flag.noskill=1; break;
  7949. case MF_NOWARP: map[m].flag.nowarp=1; break;
  7950. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  7951. case MF_SNOW: map[m].flag.snow=1; break;
  7952. case MF_CLOUDS: map[m].flag.clouds=1; break;
  7953. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  7954. case MF_FOG: map[m].flag.fog=1; break;
  7955. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  7956. case MF_SAKURA: map[m].flag.sakura=1; break;
  7957. case MF_LEAVES: map[m].flag.leaves=1; break;
  7958. case MF_RAIN: map[m].flag.rain=1; break;
  7959. case MF_INDOORS: map[m].flag.indoors=1; break;
  7960. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  7961. case MF_NOGO: map[m].flag.nogo=1; break;
  7962. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  7963. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  7964. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  7965. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  7966. case MF_NORETURN: map[m].flag.noreturn=1; break;
  7967. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  7968. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  7969. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  7970. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  7971. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7972. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  7973. case MF_NOVENDING: map[m].flag.novending=1; break;
  7974. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  7975. case MF_NOCHAT: map[m].flag.nochat=1; break;
  7976. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  7977. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  7978. }
  7979. }
  7980. return 0;
  7981. }
  7982. BUILDIN_FUNC(removemapflag)
  7983. {
  7984. int m,i;
  7985. const char *str;
  7986. str=script_getstr(st,2);
  7987. i=script_getnum(st,3);
  7988. m = map_mapname2mapid(str);
  7989. if(m >= 0) {
  7990. switch(i) {
  7991. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  7992. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  7993. case MF_NOSAVE: map[m].flag.nosave=0; break;
  7994. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  7995. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  7996. case MF_PVP: map[m].flag.pvp=0; break;
  7997. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  7998. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  7999. case MF_GVG: map[m].flag.gvg=0; break;
  8000. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  8001. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  8002. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8003. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8004. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8005. case MF_NODROP: map[m].flag.nodrop=0; break;
  8006. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8007. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8008. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8009. case MF_SNOW: map[m].flag.snow=0; break;
  8010. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8011. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8012. case MF_FOG: map[m].flag.fog=0; break;
  8013. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8014. case MF_SAKURA: map[m].flag.sakura=0; break;
  8015. case MF_LEAVES: map[m].flag.leaves=0; break;
  8016. case MF_RAIN: map[m].flag.rain=0; break;
  8017. case MF_INDOORS: map[m].flag.indoors=0; break;
  8018. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8019. case MF_NOGO: map[m].flag.nogo=0; break;
  8020. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8021. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8022. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8023. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8024. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8025. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8026. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8027. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8028. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8029. case MF_JEXP: map[m].jexp=100; break;
  8030. case MF_BEXP: map[m].bexp=100; break;
  8031. case MF_NOVENDING: map[m].flag.novending=0; break;
  8032. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8033. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8034. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8035. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8036. }
  8037. }
  8038. return 0;
  8039. }
  8040. BUILDIN_FUNC(pvpon)
  8041. {
  8042. int m;
  8043. const char *str;
  8044. TBL_PC* sd = NULL;
  8045. struct s_mapiterator* iter;
  8046. str = script_getstr(st,2);
  8047. m = map_mapname2mapid(str);
  8048. if( m < 0 || map[m].flag.pvp )
  8049. return 0; // nothing to do
  8050. map[m].flag.pvp = 1;
  8051. clif_send0199(m,1);
  8052. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8053. return 0;
  8054. iter = mapit_getallusers();
  8055. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  8056. {
  8057. if( sd->bl.m != m || sd->pvp_timer != -1 )
  8058. continue; // not applicable
  8059. sd->pvp_timer = add_timer(gettick()+200,pc_calc_pvprank_timer,sd->bl.id,0);
  8060. sd->pvp_rank = 0;
  8061. sd->pvp_lastusers = 0;
  8062. sd->pvp_point = 5;
  8063. sd->pvp_won = 0;
  8064. sd->pvp_lost = 0;
  8065. }
  8066. mapit_free(iter);
  8067. return 0;
  8068. }
  8069. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap)
  8070. {
  8071. TBL_PC* sd = (TBL_PC*)bl;
  8072. clif_pvpset(sd, 0, 0, 2);
  8073. if (sd->pvp_timer != -1) {
  8074. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8075. sd->pvp_timer = -1;
  8076. }
  8077. return 0;
  8078. }
  8079. BUILDIN_FUNC(pvpoff)
  8080. {
  8081. int m;
  8082. const char *str;
  8083. str=script_getstr(st,2);
  8084. m = map_mapname2mapid(str);
  8085. if(m < 0 || !map[m].flag.pvp)
  8086. return 0; //fixed Lupus
  8087. map[m].flag.pvp = 0;
  8088. clif_send0199(m,0);
  8089. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8090. return 0;
  8091. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8092. return 0;
  8093. }
  8094. BUILDIN_FUNC(gvgon)
  8095. {
  8096. int m;
  8097. const char *str;
  8098. str=script_getstr(st,2);
  8099. m = map_mapname2mapid(str);
  8100. if(m >= 0 && !map[m].flag.gvg) {
  8101. map[m].flag.gvg = 1;
  8102. clif_send0199(m,3);
  8103. }
  8104. return 0;
  8105. }
  8106. BUILDIN_FUNC(gvgoff)
  8107. {
  8108. int m;
  8109. const char *str;
  8110. str=script_getstr(st,2);
  8111. m = map_mapname2mapid(str);
  8112. if(m >= 0 && map[m].flag.gvg) {
  8113. map[m].flag.gvg = 0;
  8114. clif_send0199(m,0);
  8115. }
  8116. return 0;
  8117. }
  8118. /*==========================================
  8119. * Shows an emoticon on top of the player/npc
  8120. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8121. *------------------------------------------*/
  8122. //Optional second parameter added by [Skotlex]
  8123. BUILDIN_FUNC(emotion)
  8124. {
  8125. int type;
  8126. int player=0;
  8127. type=script_getnum(st,2);
  8128. if(type < 0 || type > 100)
  8129. return 0;
  8130. if( script_hasdata(st,3) )
  8131. player=script_getnum(st,3);
  8132. if (player) {
  8133. TBL_PC *sd = script_rid2sd(st);
  8134. if (sd)
  8135. clif_emotion(&sd->bl,type);
  8136. } else
  8137. clif_emotion(map_id2bl(st->oid),type);
  8138. return 0;
  8139. }
  8140. static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
  8141. {
  8142. int m=va_arg(ap,int);
  8143. int g_id=va_arg(ap,int);
  8144. int flag=va_arg(ap,int);
  8145. TBL_PC *sd = (TBL_PC*)data;
  8146. if(!sd || sd->bl.m != m)
  8147. return 0;
  8148. if(
  8149. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8150. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8151. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8152. )
  8153. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8154. return 1;
  8155. }
  8156. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8157. {
  8158. struct mob_data *md=(struct mob_data *)bl;
  8159. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8160. status_kill(bl);
  8161. return 0;
  8162. }
  8163. BUILDIN_FUNC(maprespawnguildid)
  8164. {
  8165. const char *mapname=script_getstr(st,2);
  8166. int g_id=script_getnum(st,3);
  8167. int flag=script_getnum(st,4);
  8168. int m=map_mapname2mapid(mapname);
  8169. if(m == -1)
  8170. return 0;
  8171. //Catch ALL players (in case some are 'between maps' on execution time)
  8172. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8173. if (flag&4) //Remove script mobs.
  8174. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8175. return 0;
  8176. }
  8177. BUILDIN_FUNC(agitstart)
  8178. {
  8179. if(agit_flag==1) return 0; // Agit already Start.
  8180. agit_flag=1;
  8181. guild_agit_start();
  8182. return 0;
  8183. }
  8184. BUILDIN_FUNC(agitend)
  8185. {
  8186. if(agit_flag==0) return 0; // Agit already End.
  8187. agit_flag=0;
  8188. guild_agit_end();
  8189. return 0;
  8190. }
  8191. /*==========================================
  8192. * Returns whether woe is on or off. // choice script
  8193. *------------------------------------------*/
  8194. BUILDIN_FUNC(agitcheck)
  8195. {
  8196. script_pushint(st,agit_flag);
  8197. return 0;
  8198. }
  8199. BUILDIN_FUNC(flagemblem)
  8200. {
  8201. int g_id=script_getnum(st,2);
  8202. if(g_id < 0) return 0;
  8203. // ShowMessage("Script.c: [FlagEmblem] GuildID=%d, Emblem=%d.\n", g->guild_id, g->emblem_id);
  8204. ((struct npc_data *)map_id2bl(st->oid))->u.scr.guild_id = g_id;
  8205. return 0;
  8206. }
  8207. BUILDIN_FUNC(getcastlename)
  8208. {
  8209. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8210. struct guild_castle* gc = guild_mapname2gc(mapname);
  8211. char* name = (gc) ? gc->castle_name : "";
  8212. script_pushstrcopy(st,name);
  8213. return 0;
  8214. }
  8215. BUILDIN_FUNC(getcastledata)
  8216. {
  8217. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8218. int index = script_getnum(st,3);
  8219. struct guild_castle* gc = guild_mapname2gc(mapname);
  8220. if(script_hasdata(st,4) && index==0 && gc) {
  8221. const char* event = script_getstr(st,4);
  8222. check_event(st, event);
  8223. guild_addcastleinfoevent(gc->castle_id,17,event);
  8224. }
  8225. if(gc){
  8226. switch(index){
  8227. case 0: {
  8228. int i;
  8229. for(i=1;i<18;i++) // Initialize[AgitInit]
  8230. guild_castledataload(gc->castle_id,i);
  8231. } break;
  8232. case 1:
  8233. script_pushint(st,gc->guild_id); break;
  8234. case 2:
  8235. script_pushint(st,gc->economy); break;
  8236. case 3:
  8237. script_pushint(st,gc->defense); break;
  8238. case 4:
  8239. script_pushint(st,gc->triggerE); break;
  8240. case 5:
  8241. script_pushint(st,gc->triggerD); break;
  8242. case 6:
  8243. script_pushint(st,gc->nextTime); break;
  8244. case 7:
  8245. script_pushint(st,gc->payTime); break;
  8246. case 8:
  8247. script_pushint(st,gc->createTime); break;
  8248. case 9:
  8249. script_pushint(st,gc->visibleC); break;
  8250. case 10:
  8251. case 11:
  8252. case 12:
  8253. case 13:
  8254. case 14:
  8255. case 15:
  8256. case 16:
  8257. case 17:
  8258. script_pushint(st,gc->guardian[index-10].visible); break;
  8259. default:
  8260. script_pushint(st,0); break;
  8261. }
  8262. return 0;
  8263. }
  8264. script_pushint(st,0);
  8265. return 0;
  8266. }
  8267. BUILDIN_FUNC(setcastledata)
  8268. {
  8269. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8270. int index = script_getnum(st,3);
  8271. int value = script_getnum(st,4);
  8272. struct guild_castle* gc = guild_mapname2gc(mapname);
  8273. if(gc) {
  8274. // Save Data byself First
  8275. switch(index){
  8276. case 1:
  8277. gc->guild_id = value; break;
  8278. case 2:
  8279. gc->economy = value; break;
  8280. case 3:
  8281. gc->defense = value; break;
  8282. case 4:
  8283. gc->triggerE = value; break;
  8284. case 5:
  8285. gc->triggerD = value; break;
  8286. case 6:
  8287. gc->nextTime = value; break;
  8288. case 7:
  8289. gc->payTime = value; break;
  8290. case 8:
  8291. gc->createTime = value; break;
  8292. case 9:
  8293. gc->visibleC = value; break;
  8294. case 10:
  8295. case 11:
  8296. case 12:
  8297. case 13:
  8298. case 14:
  8299. case 15:
  8300. case 16:
  8301. case 17:
  8302. gc->guardian[index-10].visible = value; break;
  8303. default:
  8304. return 0;
  8305. }
  8306. guild_castledatasave(gc->castle_id,index,value);
  8307. }
  8308. return 0;
  8309. }
  8310. /* =====================================================================
  8311. * ギルド情報を要求する
  8312. * ---------------------------------------------------------------------*/
  8313. BUILDIN_FUNC(requestguildinfo)
  8314. {
  8315. int guild_id=script_getnum(st,2);
  8316. const char *event=NULL;
  8317. if( script_hasdata(st,3) ){
  8318. event=script_getstr(st,3);
  8319. check_event(st, event);
  8320. }
  8321. if(guild_id>0)
  8322. guild_npc_request_info(guild_id,event);
  8323. return 0;
  8324. }
  8325. /// Returns the number of cards that have been compounded onto the specified equipped item.
  8326. /// getequipcardcnt(<equipment slot>);
  8327. BUILDIN_FUNC(getequipcardcnt)
  8328. {
  8329. int i=-1,j,num;
  8330. TBL_PC *sd;
  8331. int count;
  8332. num=script_getnum(st,2);
  8333. sd=script_rid2sd(st);
  8334. if (num > 0 && num <= ARRAYLENGTH(equip))
  8335. i=pc_checkequip(sd,equip[num-1]);
  8336. if (i < 0) {
  8337. script_pushint(st,0);
  8338. return 0;
  8339. }
  8340. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8341. {
  8342. script_pushint(st,0);
  8343. return 0;
  8344. }
  8345. count = 0;
  8346. for( j = 0; j < MAX_SLOTS; j++ )
  8347. if( sd->status.inventory[i].card[j] && itemdb_type(sd->status.inventory[i].card[j]) == IT_CARD )
  8348. count++;
  8349. script_pushint(st,count);
  8350. return 0;
  8351. }
  8352. /// Removes all cards from the item found in the specified equipment slot of the invoking character,
  8353. /// and give them to the character. If any cards were removed in this manner, it will also show a success effect.
  8354. /// successremovecards <slot>;
  8355. BUILDIN_FUNC(successremovecards)
  8356. {
  8357. int i=-1,j,c,cardflag=0;
  8358. TBL_PC* sd = script_rid2sd(st);
  8359. int num = script_getnum(st,2);
  8360. if (num > 0 && num <= ARRAYLENGTH(equip))
  8361. i=pc_checkequip(sd,equip[num-1]);
  8362. if (i < 0) {
  8363. return 0;
  8364. }
  8365. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8366. return 0;
  8367. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8368. {
  8369. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8370. {// extract this card from the item
  8371. int flag;
  8372. struct item item_tmp;
  8373. cardflag = 1;
  8374. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8375. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8376. item_tmp.attribute=0;
  8377. for (j = 0; j < MAX_SLOTS; j++)
  8378. item_tmp.card[j]=0;
  8379. //Logs items, got from (N)PC scripts [Lupus]
  8380. if(log_config.enable_logs&0x40)
  8381. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8382. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8383. clif_additem(sd,0,0,flag);
  8384. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8385. }
  8386. }
  8387. }
  8388. if(cardflag == 1)
  8389. { // カードを取り除いたアイテム所得
  8390. int flag;
  8391. struct item item_tmp;
  8392. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8393. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8394. item_tmp.attribute=sd->status.inventory[i].attribute;
  8395. for (j = 0; j < MAX_SLOTS; j++)
  8396. item_tmp.card[j]=0;
  8397. //Logs items, got from (N)PC scripts [Lupus]
  8398. if(log_config.enable_logs&0x40)
  8399. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8400. pc_delitem(sd,i,1,0);
  8401. //Logs items, got from (N)PC scripts [Lupus]
  8402. if(log_config.enable_logs&0x40)
  8403. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8404. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8405. clif_additem(sd,0,0,flag);
  8406. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8407. }
  8408. clif_misceffect(&sd->bl,3);
  8409. }
  8410. return 0;
  8411. }
  8412. /// Removes all cards from the item found in the specified equipment slot of the invoking character.
  8413. /// failedremovecards <slot>, <type>;
  8414. /// <type>=0 : will destroy both the item and the cards.
  8415. /// <type>=1 : will keep the item, but destroy the cards.
  8416. /// <type>=2 : will keep the cards, but destroy the item.
  8417. /// <type>=? : will just display the failure effect.
  8418. BUILDIN_FUNC(failedremovecards)
  8419. {
  8420. int i=-1,j,c,cardflag=0;
  8421. TBL_PC* sd = script_rid2sd(st);
  8422. int num = script_getnum(st,2);
  8423. int typefail = script_getnum(st,3);
  8424. if (num > 0 && num <= ARRAYLENGTH(equip))
  8425. i=pc_checkequip(sd,equip[num-1]);
  8426. if (i < 0)
  8427. return 0;
  8428. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8429. return 0;
  8430. for( c = MAX_SLOTS - 1; c >= 0; --c )
  8431. {
  8432. if( sd->status.inventory[i].card[c] && itemdb_type(sd->status.inventory[i].card[c]) == IT_CARD )
  8433. {
  8434. cardflag = 1;
  8435. if(typefail == 2)
  8436. {// add cards to inventory, clear
  8437. int flag;
  8438. struct item item_tmp;
  8439. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c];
  8440. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8441. item_tmp.attribute=0;
  8442. for (j = 0; j < MAX_SLOTS; j++)
  8443. item_tmp.card[j]=0;
  8444. //Logs items, got from (N)PC scripts [Lupus]
  8445. if(log_config.enable_logs&0x40)
  8446. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8447. if((flag=pc_additem(sd,&item_tmp,1))){
  8448. clif_additem(sd,0,0,flag);
  8449. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8450. }
  8451. }
  8452. }
  8453. }
  8454. if(cardflag == 1)
  8455. {
  8456. if(typefail == 0 || typefail == 2){ // 武具損失
  8457. //Logs items, got from (N)PC scripts [Lupus]
  8458. if(log_config.enable_logs&0x40)
  8459. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8460. pc_delitem(sd,i,1,0);
  8461. }
  8462. if(typefail == 1){ // カードのみ損失(武具を返す)
  8463. int flag;
  8464. struct item item_tmp;
  8465. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8466. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8467. item_tmp.attribute=sd->status.inventory[i].attribute;
  8468. //Logs items, got from (N)PC scripts [Lupus]
  8469. if(log_config.enable_logs&0x40)
  8470. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8471. for (j = 0; j < MAX_SLOTS; j++)
  8472. item_tmp.card[j]=0;
  8473. pc_delitem(sd,i,1,0);
  8474. //Logs items, got from (N)PC scripts [Lupus]
  8475. if(log_config.enable_logs&0x40)
  8476. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8477. if((flag=pc_additem(sd,&item_tmp,1))){
  8478. clif_additem(sd,0,0,flag);
  8479. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0);
  8480. }
  8481. }
  8482. clif_misceffect(&sd->bl,2);
  8483. }
  8484. return 0;
  8485. }
  8486. /* ================================================================
  8487. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8488. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8489. * improved by [Lance]
  8490. * ================================================================*/
  8491. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8492. {
  8493. int x,y,m,check_val=0,check_ID=0,i=0;
  8494. struct guild *g = NULL;
  8495. struct party_data *p = NULL;
  8496. const char *str;
  8497. const char *mapname;
  8498. unsigned int index;
  8499. mapname=script_getstr(st,2);
  8500. str=script_getstr(st,3);
  8501. x=script_getnum(st,4);
  8502. y=script_getnum(st,5);
  8503. if(script_hasdata(st,7)){
  8504. check_val=script_getnum(st,6);
  8505. check_ID=script_getnum(st,7);
  8506. }
  8507. if((m=map_mapname2mapid(mapname))< 0)
  8508. return 0;
  8509. if(!(index=mapindex_name2id(str)))
  8510. return 0;
  8511. switch(check_val){
  8512. case 1:
  8513. g = guild_search(check_ID);
  8514. if (g){
  8515. for( i=0; i < g->max_member; i++)
  8516. {
  8517. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  8518. pc_setpos(g->member[i].sd,index,x,y,3);
  8519. }
  8520. }
  8521. }
  8522. break;
  8523. case 2:
  8524. p = party_search(check_ID);
  8525. if(p){
  8526. for(i=0;i<MAX_PARTY; i++){
  8527. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  8528. pc_setpos(p->data[i].sd,index,x,y,3);
  8529. }
  8530. }
  8531. }
  8532. break;
  8533. default:
  8534. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  8535. break;
  8536. }
  8537. return 0;
  8538. }
  8539. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  8540. {
  8541. char *event=va_arg(ap,char *);
  8542. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  8543. return 1;
  8544. return 0;
  8545. }
  8546. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  8547. {
  8548. const char *mapname,*event;
  8549. int m;
  8550. mapname=script_getstr(st,2);
  8551. event=script_getstr(st,3);
  8552. check_event(st, event);
  8553. if( (m=map_mapname2mapid(mapname))<0 ) {
  8554. script_pushint(st,-1);
  8555. return 0;
  8556. }
  8557. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  8558. return 0;
  8559. }
  8560. BUILDIN_FUNC(marriage)
  8561. {
  8562. const char *partner=script_getstr(st,2);
  8563. TBL_PC *sd=script_rid2sd(st);
  8564. TBL_PC *p_sd=map_nick2sd(partner);
  8565. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  8566. script_pushint(st,0);
  8567. return 0;
  8568. }
  8569. script_pushint(st,1);
  8570. return 0;
  8571. }
  8572. BUILDIN_FUNC(wedding_effect)
  8573. {
  8574. TBL_PC *sd=script_rid2sd(st);
  8575. struct block_list *bl;
  8576. if(sd==NULL) {
  8577. bl=map_id2bl(st->oid);
  8578. } else
  8579. bl=&sd->bl;
  8580. clif_wedding_effect(bl);
  8581. return 0;
  8582. }
  8583. BUILDIN_FUNC(divorce)
  8584. {
  8585. TBL_PC *sd=script_rid2sd(st);
  8586. if(sd==NULL || pc_divorce(sd) < 0){
  8587. script_pushint(st,0);
  8588. return 0;
  8589. }
  8590. script_pushint(st,1);
  8591. return 0;
  8592. }
  8593. BUILDIN_FUNC(ispartneron)
  8594. {
  8595. TBL_PC *sd=script_rid2sd(st);
  8596. TBL_PC *p_sd=NULL;
  8597. if(sd==NULL || !pc_ismarried(sd) ||
  8598. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8599. script_pushint(st,0);
  8600. return 0;
  8601. }
  8602. script_pushint(st,1);
  8603. return 0;
  8604. }
  8605. BUILDIN_FUNC(getpartnerid)
  8606. {
  8607. TBL_PC *sd=script_rid2sd(st);
  8608. if (sd == NULL) {
  8609. script_pushint(st,0);
  8610. return 0;
  8611. }
  8612. script_pushint(st,sd->status.partner_id);
  8613. return 0;
  8614. }
  8615. BUILDIN_FUNC(getchildid)
  8616. {
  8617. TBL_PC *sd=script_rid2sd(st);
  8618. if (sd == NULL) {
  8619. script_pushint(st,0);
  8620. return 0;
  8621. }
  8622. script_pushint(st,sd->status.child);
  8623. return 0;
  8624. }
  8625. BUILDIN_FUNC(getmotherid)
  8626. {
  8627. TBL_PC *sd=script_rid2sd(st);
  8628. if (sd == NULL) {
  8629. script_pushint(st,0);
  8630. return 0;
  8631. }
  8632. script_pushint(st,sd->status.mother);
  8633. return 0;
  8634. }
  8635. BUILDIN_FUNC(getfatherid)
  8636. {
  8637. TBL_PC *sd=script_rid2sd(st);
  8638. if (sd == NULL) {
  8639. script_pushint(st,0);
  8640. return 0;
  8641. }
  8642. script_pushint(st,sd->status.father);
  8643. return 0;
  8644. }
  8645. BUILDIN_FUNC(warppartner)
  8646. {
  8647. int x,y;
  8648. unsigned short mapindex;
  8649. const char *str;
  8650. TBL_PC *sd=script_rid2sd(st);
  8651. TBL_PC *p_sd=NULL;
  8652. if(sd==NULL || !pc_ismarried(sd) ||
  8653. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  8654. script_pushint(st,0);
  8655. return 0;
  8656. }
  8657. str=script_getstr(st,2);
  8658. x=script_getnum(st,3);
  8659. y=script_getnum(st,4);
  8660. mapindex = mapindex_name2id(str);
  8661. if (mapindex) {
  8662. pc_setpos(p_sd,mapindex,x,y,0);
  8663. script_pushint(st,1);
  8664. } else
  8665. script_pushint(st,0);
  8666. return 0;
  8667. }
  8668. /*================================================
  8669. * Script for Displaying MOB Information [Valaris]
  8670. *------------------------------------------------*/
  8671. BUILDIN_FUNC(strmobinfo)
  8672. {
  8673. int num=script_getnum(st,2);
  8674. int class_=script_getnum(st,3);
  8675. if(!mobdb_checkid(class_))
  8676. {
  8677. script_pushint(st,0);
  8678. return 0;
  8679. }
  8680. switch (num) {
  8681. case 1: script_pushstrcopy(st,mob_db(class_)->name); break;
  8682. case 2: script_pushstrcopy(st,mob_db(class_)->jname); break;
  8683. case 3: script_pushint(st,mob_db(class_)->lv); break;
  8684. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  8685. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  8686. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  8687. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  8688. default:
  8689. script_pushint(st,0);
  8690. break;
  8691. }
  8692. return 0;
  8693. }
  8694. /*==========================================
  8695. * Summon guardians [Valaris]
  8696. * guardian "<map name>",<x>,<y>,"<name to show>",<mob id>,{,"<event label>"}{,<guardian index>};
  8697. *------------------------------------------*/
  8698. BUILDIN_FUNC(guardian)
  8699. {
  8700. int class_=0,x=0,y=0,guardian=0;
  8701. const char *str,*map,*evt="";
  8702. struct script_data *data;
  8703. map =script_getstr(st,2);
  8704. x =script_getnum(st,3);
  8705. y =script_getnum(st,4);
  8706. str =script_getstr(st,5);
  8707. class_=script_getnum(st,6);
  8708. if( script_hasdata(st,8) )
  8709. {// "<event label>",<guardian index>
  8710. evt=script_getstr(st,7);
  8711. guardian=script_getnum(st,8);
  8712. } else if( script_hasdata(st,7) ){
  8713. data=script_getdata(st,7);
  8714. get_val(st,data);
  8715. if( data_isstring(data) )
  8716. {// "<event label>"
  8717. evt=script_getstr(st,7);
  8718. } else if( data_isint(data) )
  8719. {// <guardian index>
  8720. guardian=script_getnum(st,7);
  8721. } else {
  8722. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  8723. script_reportdata(data);
  8724. return 1;
  8725. }
  8726. }
  8727. check_event(st, evt);
  8728. mob_spawn_guardian(map,x,y,str,class_,evt,guardian);
  8729. return 0;
  8730. }
  8731. /// Retrieves various information about the specified guardian.
  8732. ///
  8733. /// guardianinfo("<map_name>", <index>, <type>) -> <value>
  8734. /// type: 0 - whether it is deployed or not
  8735. /// 1 - maximum hp
  8736. /// 2 - current hp
  8737. ///
  8738. BUILDIN_FUNC(guardianinfo)
  8739. {
  8740. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8741. int id = script_getnum(st,3);
  8742. int type = script_getnum(st,4);
  8743. struct guild_castle* gc = guild_mapname2gc(mapname);
  8744. struct mob_data* gd;
  8745. if( gc == NULL || id < 0 || id >= MAX_GUARDIANS )
  8746. {
  8747. script_pushint(st,-1);
  8748. return 0;
  8749. }
  8750. if( type == 0 )
  8751. script_pushint(st, gc->guardian[id].visible);
  8752. else
  8753. if( !gc->guardian[id].visible )
  8754. script_pushint(st,-1);
  8755. else
  8756. if( (gd = map_id2md(gc->guardian[id].id)) == NULL )
  8757. script_pushint(st,-1);
  8758. else
  8759. {
  8760. if ( type == 1 ) script_pushint(st,gd->status.max_hp);
  8761. else if( type == 2 ) script_pushint(st,gd->status.hp);
  8762. else
  8763. script_pushint(st,-1);
  8764. }
  8765. return 0;
  8766. }
  8767. /*==========================================
  8768. * IDからItem名
  8769. *------------------------------------------*/
  8770. BUILDIN_FUNC(getitemname)
  8771. {
  8772. int item_id=0;
  8773. struct item_data *i_data;
  8774. char *item_name;
  8775. struct script_data *data;
  8776. data=script_getdata(st,2);
  8777. get_val(st,data);
  8778. if( data_isstring(data) ){
  8779. const char *name=conv_str(st,data);
  8780. struct item_data *item_data = itemdb_searchname(name);
  8781. if( item_data )
  8782. item_id=item_data->nameid;
  8783. }else
  8784. item_id=conv_num(st,data);
  8785. i_data = itemdb_exists(item_id);
  8786. if (i_data == NULL)
  8787. {
  8788. script_pushconststr(st,"null");
  8789. return 0;
  8790. }
  8791. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  8792. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  8793. script_pushstr(st,item_name);
  8794. return 0;
  8795. }
  8796. /*==========================================
  8797. * Returns number of slots an item has. [Skotlex]
  8798. *------------------------------------------*/
  8799. BUILDIN_FUNC(getitemslots)
  8800. {
  8801. int item_id;
  8802. struct item_data *i_data;
  8803. item_id=script_getnum(st,2);
  8804. i_data = itemdb_exists(item_id);
  8805. if (i_data)
  8806. script_pushint(st,i_data->slot);
  8807. else
  8808. script_pushint(st,-1);
  8809. return 0;
  8810. }
  8811. /*==========================================
  8812. * Returns some values of an item [Lupus]
  8813. * Price, Weight, etc...
  8814. getiteminfo(itemID,n), where n
  8815. 0 value_buy;
  8816. 1 value_sell;
  8817. 2 type;
  8818. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8819. if = 0, then monsters don't drop it at all (rare or a quest item)
  8820. if = 10000, then this item is sold in NPC shops only
  8821. 4 sex;
  8822. 5 equip;
  8823. 6 weight;
  8824. 7 atk;
  8825. 8 def;
  8826. 9 range;
  8827. 10 slot;
  8828. 11 look;
  8829. 12 elv;
  8830. 13 wlv;
  8831. 14 view id
  8832. *------------------------------------------*/
  8833. BUILDIN_FUNC(getiteminfo)
  8834. {
  8835. int item_id,n;
  8836. int *item_arr;
  8837. struct item_data *i_data;
  8838. item_id = script_getnum(st,2);
  8839. n = script_getnum(st,3);
  8840. i_data = itemdb_exists(item_id);
  8841. if (i_data && n>=0 && n<14) {
  8842. item_arr = (int*)&i_data->value_buy;
  8843. script_pushint(st,item_arr[n]);
  8844. } else
  8845. script_pushint(st,-1);
  8846. return 0;
  8847. }
  8848. /*==========================================
  8849. * Set some values of an item [Lupus]
  8850. * Price, Weight, etc...
  8851. setiteminfo(itemID,n,Value), where n
  8852. 0 value_buy;
  8853. 1 value_sell;
  8854. 2 type;
  8855. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  8856. if = 0, then monsters don't drop it at all (rare or a quest item)
  8857. if = 10000, then this item is sold in NPC shops only
  8858. 4 sex;
  8859. 5 equip;
  8860. 6 weight;
  8861. 7 atk;
  8862. 8 def;
  8863. 9 range;
  8864. 10 slot;
  8865. 11 look;
  8866. 12 elv;
  8867. 13 wlv;
  8868. 14 view id
  8869. * Returns Value or -1 if the wrong field's been set
  8870. *------------------------------------------*/
  8871. BUILDIN_FUNC(setiteminfo)
  8872. {
  8873. int item_id,n,value;
  8874. int *item_arr;
  8875. struct item_data *i_data;
  8876. item_id = script_getnum(st,2);
  8877. n = script_getnum(st,3);
  8878. value = script_getnum(st,4);
  8879. i_data = itemdb_exists(item_id);
  8880. if (i_data && n>=0 && n<14) {
  8881. item_arr = (int*)&i_data->value_buy;
  8882. item_arr[n] = value;
  8883. script_pushint(st,value);
  8884. } else
  8885. script_pushint(st,-1);
  8886. return 0;
  8887. }
  8888. /*==========================================
  8889. * Returns value from equipped item slot n [Lupus]
  8890. getequipcardid(num,slot)
  8891. where
  8892. num = eqip position slot
  8893. slot = 0,1,2,3 (Card Slot N)
  8894. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  8895. it's useful when you want to check item cards or if it's signed
  8896. Useful for such quests as "Sign this refined item with players name" etc
  8897. Hat[0] +4 -> Player's Hat[0] +4
  8898. *------------------------------------------*/
  8899. BUILDIN_FUNC(getequipcardid)
  8900. {
  8901. int i=-1,num,slot;
  8902. TBL_PC *sd;
  8903. num=script_getnum(st,2);
  8904. slot=script_getnum(st,3);
  8905. sd=script_rid2sd(st);
  8906. if (num > 0 && num <= ARRAYLENGTH(equip))
  8907. i=pc_checkequip(sd,equip[num-1]);
  8908. if(i >= 0 && slot>=0 && slot<4)
  8909. script_pushint(st,sd->status.inventory[i].card[slot]);
  8910. else
  8911. script_pushint(st,0);
  8912. return 0;
  8913. }
  8914. /*==========================================
  8915. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  8916. *------------------------------------------*/
  8917. BUILDIN_FUNC(petskillbonus)
  8918. {
  8919. struct pet_data *pd;
  8920. TBL_PC *sd=script_rid2sd(st);
  8921. if(sd==NULL || sd->pd==NULL)
  8922. return 0;
  8923. pd=sd->pd;
  8924. if (pd->bonus)
  8925. { //Clear previous bonus
  8926. if (pd->bonus->timer != -1)
  8927. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  8928. } else //init
  8929. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  8930. pd->bonus->type=script_getnum(st,2);
  8931. pd->bonus->val=script_getnum(st,3);
  8932. pd->bonus->duration=script_getnum(st,4);
  8933. pd->bonus->delay=script_getnum(st,5);
  8934. if (pd->state.skillbonus == 1)
  8935. pd->state.skillbonus=0; // waiting state
  8936. // wait for timer to start
  8937. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  8938. pd->bonus->timer=-1;
  8939. else
  8940. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  8941. return 0;
  8942. }
  8943. /*==========================================
  8944. * pet looting [Valaris] //Rewritten by [Skotlex]
  8945. *------------------------------------------*/
  8946. BUILDIN_FUNC(petloot)
  8947. {
  8948. int max;
  8949. struct pet_data *pd;
  8950. TBL_PC *sd=script_rid2sd(st);
  8951. if(sd==NULL || sd->pd==NULL)
  8952. return 0;
  8953. max=script_getnum(st,2);
  8954. if(max < 1)
  8955. max = 1; //Let'em loot at least 1 item.
  8956. else if (max > MAX_PETLOOT_SIZE)
  8957. max = MAX_PETLOOT_SIZE;
  8958. pd = sd->pd;
  8959. if (pd->loot != NULL)
  8960. { //Release whatever was there already and reallocate memory
  8961. pet_lootitem_drop(pd, pd->msd);
  8962. aFree(pd->loot->item);
  8963. }
  8964. else
  8965. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  8966. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  8967. pd->loot->max=max;
  8968. pd->loot->count = 0;
  8969. pd->loot->weight = 0;
  8970. return 0;
  8971. }
  8972. /*==========================================
  8973. * PCの所持品情報読み取り
  8974. *------------------------------------------*/
  8975. BUILDIN_FUNC(getinventorylist)
  8976. {
  8977. TBL_PC *sd=script_rid2sd(st);
  8978. char card_var[NAME_LENGTH];
  8979. int i,j=0,k;
  8980. if(!sd) return 0;
  8981. for(i=0;i<MAX_INVENTORY;i++){
  8982. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  8983. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  8984. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  8985. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  8986. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  8987. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  8988. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  8989. for (k = 0; k < MAX_SLOTS; k++)
  8990. {
  8991. sprintf(card_var, "@inventorylist_card%d",k+1);
  8992. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  8993. }
  8994. j++;
  8995. }
  8996. }
  8997. pc_setreg(sd,add_str("@inventorylist_count"),j);
  8998. return 0;
  8999. }
  9000. BUILDIN_FUNC(getskilllist)
  9001. {
  9002. TBL_PC *sd=script_rid2sd(st);
  9003. int i,j=0;
  9004. if(!sd) return 0;
  9005. for(i=0;i<MAX_SKILL;i++){
  9006. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9007. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9008. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9009. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9010. j++;
  9011. }
  9012. }
  9013. pc_setreg(sd,add_str("@skilllist_count"),j);
  9014. return 0;
  9015. }
  9016. BUILDIN_FUNC(clearitem)
  9017. {
  9018. TBL_PC *sd=script_rid2sd(st);
  9019. int i;
  9020. if(sd==NULL) return 0;
  9021. for (i=0; i<MAX_INVENTORY; i++) {
  9022. if (sd->status.inventory[i].amount) {
  9023. //Logs items, got from (N)PC scripts [Lupus]
  9024. if(log_config.enable_logs&0x40)
  9025. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9026. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9027. }
  9028. }
  9029. return 0;
  9030. }
  9031. /*==========================================
  9032. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9033. *------------------------------------------*/
  9034. BUILDIN_FUNC(disguise)
  9035. {
  9036. int id;
  9037. TBL_PC* sd = script_rid2sd(st);
  9038. if (sd == NULL) return 0;
  9039. id = script_getnum(st,2);
  9040. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9041. pc_disguise(sd, id);
  9042. script_pushint(st,id);
  9043. } else
  9044. script_pushint(st,0);
  9045. return 0;
  9046. }
  9047. /*==========================================
  9048. * Undisguise Player (returns 1 if success, 0 on fail)
  9049. *------------------------------------------*/
  9050. BUILDIN_FUNC(undisguise)
  9051. {
  9052. TBL_PC* sd = script_rid2sd(st);
  9053. if (sd == NULL) return 0;
  9054. if (sd->disguise) {
  9055. pc_disguise(sd, 0);
  9056. script_pushint(st,0);
  9057. } else {
  9058. script_pushint(st,1);
  9059. }
  9060. return 0;
  9061. }
  9062. /*==========================================
  9063. * NPCクラスチェンジ
  9064. * classは変わりたいclass
  9065. * typeは通常0なのかな?
  9066. *------------------------------------------*/
  9067. BUILDIN_FUNC(classchange)
  9068. {
  9069. int _class,type;
  9070. struct block_list *bl=map_id2bl(st->oid);
  9071. if(bl==NULL) return 0;
  9072. _class=script_getnum(st,2);
  9073. type=script_getnum(st,3);
  9074. clif_class_change(bl,_class,type);
  9075. return 0;
  9076. }
  9077. /*==========================================
  9078. * NPCから発生するエフェクト
  9079. *------------------------------------------*/
  9080. BUILDIN_FUNC(misceffect)
  9081. {
  9082. int type;
  9083. type=script_getnum(st,2);
  9084. if(st->oid && st->oid != fake_nd->bl.id) {
  9085. struct block_list *bl = map_id2bl(st->oid);
  9086. if (bl)
  9087. clif_misceffect2(bl,type);
  9088. } else{
  9089. TBL_PC *sd=script_rid2sd(st);
  9090. if(sd)
  9091. clif_misceffect2(&sd->bl,type);
  9092. }
  9093. return 0;
  9094. }
  9095. /*==========================================
  9096. * サウンドエフェクト
  9097. *------------------------------------------*/
  9098. BUILDIN_FUNC(soundeffect)
  9099. {
  9100. TBL_PC* sd = script_rid2sd(st);
  9101. const char* name = script_getstr(st,2);
  9102. int type = script_getnum(st,3);
  9103. if(sd)
  9104. {
  9105. if(!st->rid)
  9106. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9107. else
  9108. clif_soundeffect(sd,&sd->bl,name,type);
  9109. }
  9110. return 0;
  9111. }
  9112. int soundeffect_sub(struct block_list* bl,va_list ap)
  9113. {
  9114. char* name = va_arg(ap,char*);
  9115. int type = va_arg(ap,int);
  9116. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9117. return 0;
  9118. }
  9119. /*==========================================
  9120. * Play a sound effect (.wav) on multiple clients
  9121. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9122. *------------------------------------------*/
  9123. BUILDIN_FUNC(soundeffectall)
  9124. {
  9125. struct block_list* bl;
  9126. const char* name;
  9127. int type;
  9128. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9129. if (!bl)
  9130. return 0;
  9131. name = script_getstr(st,2);
  9132. type = script_getnum(st,3);
  9133. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9134. if(!script_hasdata(st,4))
  9135. { // area around
  9136. clif_soundeffectall(bl, name, type, AREA);
  9137. }
  9138. else
  9139. if(!script_hasdata(st,5))
  9140. { // entire map
  9141. const char* map = script_getstr(st,4);
  9142. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9143. }
  9144. else
  9145. if(script_hasdata(st,9))
  9146. { // specified part of map
  9147. const char* map = script_getstr(st,5);
  9148. int x0 = script_getnum(st,6);
  9149. int y0 = script_getnum(st,7);
  9150. int x1 = script_getnum(st,8);
  9151. int y1 = script_getnum(st,9);
  9152. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9153. }
  9154. else
  9155. {
  9156. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9157. }
  9158. return 0;
  9159. }
  9160. /*==========================================
  9161. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9162. *------------------------------------------*/
  9163. BUILDIN_FUNC(petrecovery)
  9164. {
  9165. struct pet_data *pd;
  9166. TBL_PC *sd=script_rid2sd(st);
  9167. if(sd==NULL || sd->pd==NULL)
  9168. return 0;
  9169. pd=sd->pd;
  9170. if (pd->recovery)
  9171. { //Halt previous bonus
  9172. if (pd->recovery->timer != -1)
  9173. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9174. } else //Init
  9175. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9176. pd->recovery->type=script_getnum(st,2);
  9177. pd->recovery->delay=script_getnum(st,3);
  9178. pd->recovery->timer=-1;
  9179. return 0;
  9180. }
  9181. /*==========================================
  9182. * pet healing [Valaris] //Rewritten by [Skotlex]
  9183. *------------------------------------------*/
  9184. BUILDIN_FUNC(petheal)
  9185. {
  9186. struct pet_data *pd;
  9187. TBL_PC *sd=script_rid2sd(st);
  9188. if(sd==NULL || sd->pd==NULL)
  9189. return 0;
  9190. pd=sd->pd;
  9191. if (pd->s_skill)
  9192. { //Clear previous skill
  9193. if (pd->s_skill->timer != -1)
  9194. {
  9195. if (pd->s_skill->id)
  9196. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9197. else
  9198. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9199. }
  9200. } else //init memory
  9201. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9202. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9203. //Use the lv as the amount to heal
  9204. pd->s_skill->lv=script_getnum(st,2);
  9205. pd->s_skill->delay=script_getnum(st,3);
  9206. pd->s_skill->hp=script_getnum(st,4);
  9207. pd->s_skill->sp=script_getnum(st,5);
  9208. //Use delay as initial offset to avoid skill/heal exploits
  9209. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9210. pd->s_skill->timer=-1;
  9211. else
  9212. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9213. return 0;
  9214. }
  9215. /*==========================================
  9216. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9217. *------------------------------------------*/
  9218. BUILDIN_FUNC(petskillattack)
  9219. {
  9220. struct pet_data *pd;
  9221. TBL_PC *sd=script_rid2sd(st);
  9222. if(sd==NULL || sd->pd==NULL)
  9223. return 0;
  9224. pd=sd->pd;
  9225. if (pd->a_skill == NULL)
  9226. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9227. pd->a_skill->id=script_getnum(st,2);
  9228. pd->a_skill->lv=script_getnum(st,3);
  9229. pd->a_skill->div_ = 0;
  9230. pd->a_skill->rate=script_getnum(st,4);
  9231. pd->a_skill->bonusrate=script_getnum(st,5);
  9232. return 0;
  9233. }
  9234. /*==========================================
  9235. * pet attack skills [Valaris]
  9236. *------------------------------------------*/
  9237. BUILDIN_FUNC(petskillattack2)
  9238. {
  9239. struct pet_data *pd;
  9240. TBL_PC *sd=script_rid2sd(st);
  9241. if(sd==NULL || sd->pd==NULL)
  9242. return 0;
  9243. pd=sd->pd;
  9244. if (pd->a_skill == NULL)
  9245. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9246. pd->a_skill->id=script_getnum(st,2);
  9247. pd->a_skill->lv=script_getnum(st,3);
  9248. pd->a_skill->div_ = script_getnum(st,4);
  9249. pd->a_skill->rate=script_getnum(st,5);
  9250. pd->a_skill->bonusrate=script_getnum(st,6);
  9251. return 0;
  9252. }
  9253. /*==========================================
  9254. * pet support skills [Skotlex]
  9255. *------------------------------------------*/
  9256. BUILDIN_FUNC(petskillsupport)
  9257. {
  9258. struct pet_data *pd;
  9259. TBL_PC *sd=script_rid2sd(st);
  9260. if(sd==NULL || sd->pd==NULL)
  9261. return 0;
  9262. pd=sd->pd;
  9263. if (pd->s_skill)
  9264. { //Clear previous skill
  9265. if (pd->s_skill->timer != -1)
  9266. {
  9267. if (pd->s_skill->id)
  9268. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9269. else
  9270. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9271. }
  9272. } else //init memory
  9273. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9274. pd->s_skill->id=script_getnum(st,2);
  9275. pd->s_skill->lv=script_getnum(st,3);
  9276. pd->s_skill->delay=script_getnum(st,4);
  9277. pd->s_skill->hp=script_getnum(st,5);
  9278. pd->s_skill->sp=script_getnum(st,6);
  9279. //Use delay as initial offset to avoid skill/heal exploits
  9280. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9281. pd->s_skill->timer=-1;
  9282. else
  9283. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9284. return 0;
  9285. }
  9286. /*==========================================
  9287. * Scripted skill effects [Celest]
  9288. *------------------------------------------*/
  9289. BUILDIN_FUNC(skilleffect)
  9290. {
  9291. TBL_PC *sd;
  9292. int skillid=script_getnum(st,2);
  9293. int skilllv=script_getnum(st,3);
  9294. sd=script_rid2sd(st);
  9295. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9296. return 0;
  9297. }
  9298. /*==========================================
  9299. * NPC skill effects [Valaris]
  9300. *------------------------------------------*/
  9301. BUILDIN_FUNC(npcskilleffect)
  9302. {
  9303. struct block_list *bl= map_id2bl(st->oid);
  9304. int skillid=script_getnum(st,2);
  9305. int skilllv=script_getnum(st,3);
  9306. int x=script_getnum(st,4);
  9307. int y=script_getnum(st,5);
  9308. if (bl)
  9309. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9310. return 0;
  9311. }
  9312. /*==========================================
  9313. * Special effects [Valaris]
  9314. *------------------------------------------*/
  9315. BUILDIN_FUNC(specialeffect)
  9316. {
  9317. struct block_list *bl=map_id2bl(st->oid);
  9318. if(bl==NULL)
  9319. return 0;
  9320. clif_specialeffect(bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9321. return 0;
  9322. }
  9323. BUILDIN_FUNC(specialeffect2)
  9324. {
  9325. TBL_PC *sd=script_rid2sd(st);
  9326. if(sd==NULL)
  9327. return 0;
  9328. clif_specialeffect(&sd->bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9329. return 0;
  9330. }
  9331. /*==========================================
  9332. * Nude [Valaris]
  9333. *------------------------------------------*/
  9334. BUILDIN_FUNC(nude)
  9335. {
  9336. TBL_PC *sd=script_rid2sd(st);
  9337. int i,calcflag=0;
  9338. if(sd==NULL)
  9339. return 0;
  9340. for(i=0;i<11;i++)
  9341. if(sd->equip_index[i] >= 0) {
  9342. if(!calcflag)
  9343. calcflag=1;
  9344. pc_unequipitem(sd,sd->equip_index[i],2);
  9345. }
  9346. if(calcflag)
  9347. status_calc_pc(sd,0);
  9348. return 0;
  9349. }
  9350. /*==========================================
  9351. * gmcommand [MouseJstr]
  9352. *
  9353. * suggested on the forums...
  9354. * splitted into atcommand & charcommand by [Skotlex]
  9355. *------------------------------------------*/
  9356. BUILDIN_FUNC(atcommand)
  9357. {
  9358. TBL_PC dummy_sd;
  9359. TBL_PC* sd;
  9360. int fd;
  9361. const char* cmd;
  9362. cmd = script_getstr(st,2);
  9363. if (st->rid) {
  9364. sd = script_rid2sd(st);
  9365. fd = sd->fd;
  9366. } else { //Use a dummy character.
  9367. sd = &dummy_sd;
  9368. fd = 0;
  9369. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9370. if (st->oid)
  9371. {
  9372. struct block_list* bl = map_id2bl(st->oid);
  9373. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9374. if (bl->type == BL_NPC)
  9375. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9376. }
  9377. }
  9378. // compatibility with previous implementation (deprecated!)
  9379. if(cmd[0] != atcommand_symbol)
  9380. {
  9381. cmd += strlen(sd->status.name);
  9382. while(*cmd != atcommand_symbol && *cmd != 0)
  9383. cmd++;
  9384. }
  9385. is_atcommand_sub(fd, sd, cmd, 99);
  9386. return 0;
  9387. }
  9388. BUILDIN_FUNC(charcommand)
  9389. {
  9390. TBL_PC dummy_sd;
  9391. TBL_PC* sd;
  9392. int fd;
  9393. const char* cmd;
  9394. cmd = script_getstr(st,2);
  9395. if (st->rid) {
  9396. sd = script_rid2sd(st);
  9397. fd = sd->fd;
  9398. } else { //Use a dummy character.
  9399. sd = &dummy_sd;
  9400. fd = 0;
  9401. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9402. if (st->oid)
  9403. {
  9404. struct block_list* bl = map_id2bl(st->oid);
  9405. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9406. if (bl->type == BL_NPC)
  9407. safestrncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9408. }
  9409. }
  9410. // compatibility with previous implementation (deprecated!)
  9411. if(cmd[0] != charcommand_symbol)
  9412. {
  9413. cmd += strlen(sd->status.name);
  9414. while(*cmd != charcommand_symbol && *cmd != 0)
  9415. cmd++;
  9416. }
  9417. is_charcommand_sub(fd, sd, cmd, 99);
  9418. return 0;
  9419. }
  9420. /*==========================================
  9421. * Displays a message for the player only (like system messages like "you got an apple" )
  9422. *------------------------------------------*/
  9423. BUILDIN_FUNC(dispbottom)
  9424. {
  9425. TBL_PC *sd=script_rid2sd(st);
  9426. const char *message;
  9427. message=script_getstr(st,2);
  9428. if(sd)
  9429. clif_disp_onlyself(sd,message,(int)strlen(message));
  9430. return 0;
  9431. }
  9432. /*==========================================
  9433. * All The Players Full Recovery
  9434. * (HP/SP full restore and resurrect if need)
  9435. *------------------------------------------*/
  9436. BUILDIN_FUNC(recovery)
  9437. {
  9438. TBL_PC* sd;
  9439. struct s_mapiterator* iter;
  9440. iter = mapit_getallusers();
  9441. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  9442. {
  9443. if(pc_isdead(sd))
  9444. status_revive(&sd->bl, 100, 100);
  9445. else
  9446. status_percent_heal(&sd->bl, 100, 100);
  9447. clif_displaymessage(sd->fd,"You have been recovered!");
  9448. }
  9449. mapit_free(iter);
  9450. return 0;
  9451. }
  9452. /*==========================================
  9453. * Get your pet info: getpetinfo(n)
  9454. * n -> 0:pet_id 1:pet_class 2:pet_name
  9455. * 3:friendly 4:hungry, 5: rename flag.
  9456. *------------------------------------------*/
  9457. BUILDIN_FUNC(getpetinfo)
  9458. {
  9459. TBL_PC *sd=script_rid2sd(st);
  9460. TBL_PET *pd;
  9461. int type=script_getnum(st,2);
  9462. if(!sd || !sd->pd) {
  9463. if (type == 2)
  9464. script_pushconststr(st,"null");
  9465. else
  9466. script_pushint(st,0);
  9467. return 0;
  9468. }
  9469. pd = sd->pd;
  9470. switch(type){
  9471. case 0: script_pushint(st,pd->pet.pet_id); break;
  9472. case 1: script_pushint(st,pd->pet.class_); break;
  9473. case 2: script_pushstrcopy(st,pd->pet.name); break;
  9474. case 3: script_pushint(st,pd->pet.intimate); break;
  9475. case 4: script_pushint(st,pd->pet.hungry); break;
  9476. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9477. default:
  9478. script_pushint(st,0);
  9479. break;
  9480. }
  9481. return 0;
  9482. }
  9483. /*==========================================
  9484. * Get your homunculus info: gethominfo(n)
  9485. * n -> 0:hom_id 1:class 2:name
  9486. * 3:friendly 4:hungry, 5: rename flag.
  9487. * 6: level
  9488. *------------------------------------------*/
  9489. BUILDIN_FUNC(gethominfo)
  9490. {
  9491. TBL_PC *sd=script_rid2sd(st);
  9492. TBL_HOM *hd;
  9493. int type=script_getnum(st,2);
  9494. hd = sd?sd->hd:NULL;
  9495. if(!merc_is_hom_active(hd))
  9496. {
  9497. if (type == 2)
  9498. script_pushconststr(st,"null");
  9499. else
  9500. script_pushint(st,0);
  9501. return 0;
  9502. }
  9503. switch(type){
  9504. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9505. case 1: script_pushint(st,hd->homunculus.class_); break;
  9506. case 2: script_pushstrcopy(st,hd->homunculus.name); break;
  9507. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  9508. case 4: script_pushint(st,hd->homunculus.hunger); break;
  9509. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  9510. case 6: script_pushint(st,hd->homunculus.level); break;
  9511. default:
  9512. script_pushint(st,0);
  9513. break;
  9514. }
  9515. return 0;
  9516. }
  9517. /*==========================================
  9518. * Shows wether your inventory(and equips) contain
  9519. selected card or not.
  9520. checkequipedcard(4001);
  9521. *------------------------------------------*/
  9522. BUILDIN_FUNC(checkequipedcard)
  9523. {
  9524. TBL_PC *sd=script_rid2sd(st);
  9525. int n,i,c=0;
  9526. c=script_getnum(st,2);
  9527. if(sd){
  9528. for(i=0;i<MAX_INVENTORY;i++){
  9529. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  9530. for(n=0;n<MAX_SLOTS;n++){
  9531. if(sd->status.inventory[i].card[n]==c){
  9532. script_pushint(st,1);
  9533. return 0;
  9534. }
  9535. }
  9536. }
  9537. }
  9538. }
  9539. script_pushint(st,0);
  9540. return 0;
  9541. }
  9542. BUILDIN_FUNC(jump_zero)
  9543. {
  9544. int sel;
  9545. sel=script_getnum(st,2);
  9546. if(!sel) {
  9547. int pos;
  9548. if( !data_islabel(script_getdata(st,3)) ){
  9549. ShowError("script: jump_zero: not label !\n");
  9550. st->state=END;
  9551. return 1;
  9552. }
  9553. pos=script_getnum(st,3);
  9554. st->pos=pos;
  9555. st->state=GOTO;
  9556. // printf("script: jump_zero: jumpto : %d\n",pos);
  9557. } else {
  9558. // printf("script: jump_zero: fail\n");
  9559. }
  9560. return 0;
  9561. }
  9562. /*==========================================
  9563. * GetMapMobs
  9564. returns mob counts on a set map:
  9565. e.g. GetMapMobs("prontera")
  9566. use "this" - for player's map
  9567. *------------------------------------------*/
  9568. BUILDIN_FUNC(getmapmobs)
  9569. {
  9570. const char *str=NULL;
  9571. int m=-1,bx,by;
  9572. int count=0;
  9573. struct block_list *bl;
  9574. str=script_getstr(st,2);
  9575. if(strcmp(str,"this")==0){
  9576. TBL_PC *sd=script_rid2sd(st);
  9577. if(sd)
  9578. m=sd->bl.m;
  9579. else{
  9580. script_pushint(st,-1);
  9581. return 0;
  9582. }
  9583. }else
  9584. m=map_mapname2mapid(str);
  9585. if(m < 0){
  9586. script_pushint(st,-1);
  9587. return 0;
  9588. }
  9589. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++)
  9590. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++)
  9591. for( bl = map[m].block_mob[bx+by*map[m].bxs] ; bl != NULL ; bl = bl->next )
  9592. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  9593. count++;
  9594. script_pushint(st,count);
  9595. return 0;
  9596. }
  9597. /*==========================================
  9598. * movenpc [MouseJstr]
  9599. *------------------------------------------*/
  9600. BUILDIN_FUNC(movenpc)
  9601. {
  9602. TBL_NPC *nd = NULL;
  9603. const char *npc;
  9604. int x,y;
  9605. npc = script_getstr(st,2);
  9606. x = script_getnum(st,3);
  9607. y = script_getnum(st,4);
  9608. if ((nd = npc_name2id(npc)) == NULL)
  9609. return -1;
  9610. npc_movenpc(nd, x, y);
  9611. return 0;
  9612. }
  9613. /*==========================================
  9614. * message [MouseJstr]
  9615. *------------------------------------------*/
  9616. BUILDIN_FUNC(message)
  9617. {
  9618. TBL_PC *sd;
  9619. const char *msg,*player;
  9620. TBL_PC *pl_sd = NULL;
  9621. sd = script_rid2sd(st);
  9622. player = script_getstr(st,2);
  9623. msg = script_getstr(st,3);
  9624. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  9625. return 0;
  9626. clif_displaymessage(pl_sd->fd, msg);
  9627. return 0;
  9628. }
  9629. /*==========================================
  9630. * npctalk (sends message to surrounding area)
  9631. *------------------------------------------*/
  9632. BUILDIN_FUNC(npctalk)
  9633. {
  9634. const char* str;
  9635. char message[255];
  9636. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  9637. str = script_getstr(st,2);
  9638. if(nd) {
  9639. memcpy(message, nd->name, NAME_LENGTH);
  9640. strtok(message, "#"); // discard extra name identifier if present
  9641. strcat(message, " : ");
  9642. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  9643. clif_message(&(nd->bl), message);
  9644. }
  9645. return 0;
  9646. }
  9647. // change npc walkspeed [Valaris]
  9648. BUILDIN_FUNC(npcspeed)
  9649. {
  9650. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9651. int x=0;
  9652. x=script_getnum(st,2);
  9653. if(nd) {
  9654. nd->speed=x;
  9655. }
  9656. return 0;
  9657. }
  9658. // make an npc walk to a position [Valaris]
  9659. BUILDIN_FUNC(npcwalkto)
  9660. {
  9661. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9662. int x=0,y=0;
  9663. x=script_getnum(st,2);
  9664. y=script_getnum(st,3);
  9665. if(nd) {
  9666. unit_walktoxy(&nd->bl,x,y,0);
  9667. }
  9668. return 0;
  9669. }
  9670. // stop an npc's movement [Valaris]
  9671. BUILDIN_FUNC(npcstop)
  9672. {
  9673. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  9674. if(nd) {
  9675. unit_stop_walking(&nd->bl,1);
  9676. }
  9677. return 0;
  9678. }
  9679. /*==========================================
  9680. * getlook char info. getlook(arg)
  9681. *------------------------------------------*/
  9682. BUILDIN_FUNC(getlook)
  9683. {
  9684. int type,val;
  9685. TBL_PC *sd;
  9686. sd=script_rid2sd(st);
  9687. type=script_getnum(st,2);
  9688. val=-1;
  9689. switch(type) {
  9690. case LOOK_HAIR: val=sd->status.hair; break; //1
  9691. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  9692. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  9693. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  9694. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  9695. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  9696. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  9697. case LOOK_SHIELD: val=sd->status.shield; break; //8
  9698. case LOOK_SHOES: break; //9
  9699. }
  9700. script_pushint(st,val);
  9701. return 0;
  9702. }
  9703. /*==========================================
  9704. * get char save point. argument: 0- map name, 1- x, 2- y
  9705. *------------------------------------------*/
  9706. BUILDIN_FUNC(getsavepoint)
  9707. {
  9708. TBL_PC* sd;
  9709. int type;
  9710. sd = script_rid2sd(st);
  9711. if (sd == NULL) {
  9712. script_pushint(st,0);
  9713. return 0;
  9714. }
  9715. type = script_getnum(st,2);
  9716. switch(type) {
  9717. case 0: script_pushstrcopy(st,mapindex_id2name(sd->status.save_point.map)); break;
  9718. case 1: script_pushint(st,sd->status.save_point.x); break;
  9719. case 2: script_pushint(st,sd->status.save_point.y); break;
  9720. default:
  9721. script_pushint(st,0);
  9722. break;
  9723. }
  9724. return 0;
  9725. }
  9726. /*==========================================
  9727. * Get position for char/npc/pet/mob objects. Added by Lorky
  9728. *
  9729. * int getMapXY(MapName$,MapX,MapY,type,[CharName$]);
  9730. * where type:
  9731. * MapName$ - String variable for output map name
  9732. * MapX - Integer variable for output coord X
  9733. * MapY - Integer variable for output coord Y
  9734. * type - type of object
  9735. * 0 - Character coord
  9736. * 1 - NPC coord
  9737. * 2 - Pet coord
  9738. * 3 - Mob coord (not released)
  9739. * 4 - Homun coord
  9740. * CharName$ - Name object. If miss or "this" the current object
  9741. *
  9742. * Return:
  9743. * 0 - success
  9744. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  9745. *------------------------------------------*/
  9746. BUILDIN_FUNC(getmapxy)
  9747. {
  9748. struct block_list *bl = NULL;
  9749. TBL_PC *sd=NULL;
  9750. int num;
  9751. char *name;
  9752. char prefix;
  9753. int x,y,type;
  9754. char mapname[MAP_NAME_LENGTH];
  9755. memset(mapname, 0, sizeof(mapname));
  9756. if( !data_isreference(script_getdata(st,2)) ){
  9757. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  9758. script_pushint(st,-1);
  9759. return 1;
  9760. }
  9761. if( !data_isreference(script_getdata(st,3)) ){
  9762. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  9763. script_pushint(st,-1);
  9764. return 1;
  9765. }
  9766. if( !data_isreference(script_getdata(st,4)) ){
  9767. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  9768. script_pushint(st,-1);
  9769. return 1;
  9770. }
  9771. // Possible needly check function parameters on C_STR,C_INT,C_INT
  9772. type=script_getnum(st,5);
  9773. switch (type){
  9774. case 0: //Get Character Position
  9775. if( script_hasdata(st,6) )
  9776. sd=map_nick2sd(script_getstr(st,6));
  9777. else
  9778. sd=script_rid2sd(st);
  9779. if (sd)
  9780. bl = &sd->bl;
  9781. break;
  9782. case 1: //Get NPC Position
  9783. if( script_hasdata(st,6) )
  9784. {
  9785. struct npc_data *nd;
  9786. nd=npc_name2id(script_getstr(st,6));
  9787. if (nd)
  9788. bl = &nd->bl;
  9789. } else //In case the origin is not an npc?
  9790. bl=map_id2bl(st->oid);
  9791. break;
  9792. case 2: //Get Pet Position
  9793. if(script_hasdata(st,6))
  9794. sd=map_nick2sd(script_getstr(st,6));
  9795. else
  9796. sd=script_rid2sd(st);
  9797. if (sd && sd->pd)
  9798. bl = &sd->pd->bl;
  9799. break;
  9800. case 3: //Get Mob Position
  9801. break; //Not supported?
  9802. case 4: //Get Homun 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->hd)
  9808. bl = &sd->hd->bl;
  9809. break;
  9810. }
  9811. if (!bl) { //No object found.
  9812. script_pushint(st,-1);
  9813. return 0;
  9814. }
  9815. x= bl->x;
  9816. y= bl->y;
  9817. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  9818. //Set MapName$
  9819. num=st->stack->stack_data[st->start+2].u.num;
  9820. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9821. prefix=*name;
  9822. if(not_server_variable(prefix))
  9823. sd=script_rid2sd(st);
  9824. else
  9825. sd=NULL;
  9826. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  9827. //Set MapX
  9828. num=st->stack->stack_data[st->start+3].u.num;
  9829. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9830. prefix=*name;
  9831. if(not_server_variable(prefix))
  9832. sd=script_rid2sd(st);
  9833. else
  9834. sd=NULL;
  9835. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  9836. //Set MapY
  9837. num=st->stack->stack_data[st->start+4].u.num;
  9838. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  9839. prefix=*name;
  9840. if(not_server_variable(prefix))
  9841. sd=script_rid2sd(st);
  9842. else
  9843. sd=NULL;
  9844. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  9845. //Return Success value
  9846. script_pushint(st,0);
  9847. return 0;
  9848. }
  9849. /*==========================================
  9850. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  9851. *------------------------------------------*/
  9852. BUILDIN_FUNC(logmes)
  9853. {
  9854. const char *str;
  9855. if (log_config.npc <= 0 ) return 0;
  9856. str = script_getstr(st,2);
  9857. log_npc(script_rid2sd(st),str);
  9858. return 0;
  9859. }
  9860. BUILDIN_FUNC(summon)
  9861. {
  9862. int _class, timeout=0;
  9863. const char *str,*event="";
  9864. TBL_PC *sd;
  9865. struct mob_data *md;
  9866. int tick = gettick();
  9867. sd=script_rid2sd(st);
  9868. if (!sd) return 0;
  9869. str =script_getstr(st,2);
  9870. _class=script_getnum(st,3);
  9871. if( script_hasdata(st,4) )
  9872. timeout=script_getnum(st,4);
  9873. if( script_hasdata(st,5) ){
  9874. event=script_getstr(st,5);
  9875. check_event(st, event);
  9876. }
  9877. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  9878. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  9879. if (md) {
  9880. md->master_id=sd->bl.id;
  9881. md->special_state.ai=1;
  9882. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  9883. mob_spawn (md); //Now it is ready for spawning.
  9884. clif_misceffect2(&md->bl,344);
  9885. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  9886. }
  9887. return 0;
  9888. }
  9889. /*==========================================
  9890. * Checks whether it is daytime/nighttime
  9891. *------------------------------------------*/
  9892. BUILDIN_FUNC(isnight)
  9893. {
  9894. script_pushint(st,(night_flag == 1));
  9895. return 0;
  9896. }
  9897. BUILDIN_FUNC(isday)
  9898. {
  9899. script_pushint(st,(night_flag == 0));
  9900. return 0;
  9901. }
  9902. /*================================================
  9903. * Check whether another item/card has been
  9904. * equipped - used for 2/15's cards patch [celest]
  9905. *------------------------------------------------*/
  9906. // leave this here, just in case
  9907. #if 0
  9908. BUILDIN_FUNC(isequipped)
  9909. {
  9910. TBL_PC *sd;
  9911. int i, j, k, id = 1;
  9912. int ret = -1;
  9913. sd = script_rid2sd(st);
  9914. for (i=0; id!=0; i++) {
  9915. int flag = 0;
  9916. FETCH (i+2, id) else id = 0;
  9917. if (id <= 0)
  9918. continue;
  9919. for (j=0; j<EQI_MAX; j++) {
  9920. int index;
  9921. index = sd->equip_index[j];
  9922. if(index < 0) continue;
  9923. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9924. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9925. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9926. if(!sd->inventory_data[index])
  9927. continue;
  9928. if(itemdb_type(id) != IT_CARD) { //Non card
  9929. if (sd->inventory_data[index]->nameid != id)
  9930. continue;
  9931. flag = 1;
  9932. break;
  9933. } else { //Card
  9934. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9935. continue;
  9936. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9937. if (sd->status.inventory[index].card[k] == id)
  9938. {
  9939. flag = 1;
  9940. break;
  9941. }
  9942. }
  9943. }
  9944. if (flag) break;
  9945. }
  9946. if (ret == -1)
  9947. ret = flag;
  9948. else
  9949. ret &= flag;
  9950. if (!ret) break;
  9951. }
  9952. script_pushint(st,ret);
  9953. return 0;
  9954. }
  9955. #endif
  9956. /*================================================
  9957. * Check how many items/cards in the list are
  9958. * equipped - used for 2/15's cards patch [celest]
  9959. *------------------------------------------------*/
  9960. BUILDIN_FUNC(isequippedcnt)
  9961. {
  9962. TBL_PC *sd;
  9963. int i, j, k, id = 1;
  9964. int ret = 0;
  9965. sd = script_rid2sd(st);
  9966. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  9967. script_pushint(st,0);
  9968. return 0;
  9969. }
  9970. for (i=0; id!=0; i++) {
  9971. FETCH (i+2, id) else id = 0;
  9972. if (id <= 0)
  9973. continue;
  9974. for (j=0; j<EQI_MAX; j++) {
  9975. int index;
  9976. index = sd->equip_index[j];
  9977. if(index < 0) continue;
  9978. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  9979. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  9980. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  9981. if(!sd->inventory_data[index])
  9982. continue;
  9983. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  9984. if (sd->inventory_data[index]->nameid == id)
  9985. ret+= sd->status.inventory[index].amount;
  9986. } else { //Count cards.
  9987. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  9988. continue; //No cards
  9989. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  9990. if (sd->status.inventory[index].card[k] == id)
  9991. ret++; //[Lupus]
  9992. }
  9993. }
  9994. }
  9995. }
  9996. script_pushint(st,ret);
  9997. return 0;
  9998. }
  9999. /*================================================
  10000. * Check whether another card has been
  10001. * equipped - used for 2/15's cards patch [celest]
  10002. * -- Items checked cannot be reused in another
  10003. * card set to prevent exploits
  10004. *------------------------------------------------*/
  10005. BUILDIN_FUNC(isequipped)
  10006. {
  10007. TBL_PC *sd;
  10008. int i, j, k, id = 1;
  10009. int index, flag;
  10010. int ret = -1;
  10011. //Original hash to reverse it when full check fails.
  10012. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10013. sd = script_rid2sd(st);
  10014. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10015. script_pushint(st,0);
  10016. return 0;
  10017. }
  10018. setitem_hash = sd->setitem_hash;
  10019. setitem_hash2 = sd->setitem_hash2;
  10020. for (i=0; id!=0; i++)
  10021. {
  10022. FETCH (i+2, id) else id = 0;
  10023. if (id <= 0)
  10024. continue;
  10025. flag = 0;
  10026. for (j=0; j<EQI_MAX; j++)
  10027. {
  10028. index = sd->equip_index[j];
  10029. if(index < 0) continue;
  10030. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10031. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10032. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10033. if(!sd->inventory_data[index])
  10034. continue;
  10035. if (itemdb_type(id) != IT_CARD) {
  10036. if (sd->inventory_data[index]->nameid != id)
  10037. continue;
  10038. flag = 1;
  10039. break;
  10040. } else { //Cards
  10041. if (sd->inventory_data[index]->slot == 0 ||
  10042. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10043. continue;
  10044. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10045. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10046. unsigned int hash = 0;
  10047. if (sd->status.inventory[index].card[k] != id)
  10048. continue;
  10049. hash = 1<<((j<5?j:j-5)*4 + k);
  10050. // check if card is already used by another set
  10051. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10052. continue;
  10053. // We have found a match
  10054. flag = 1;
  10055. // Set hash so this card cannot be used by another
  10056. if (j<5)
  10057. sd->setitem_hash |= hash;
  10058. else
  10059. sd->setitem_hash2 |= hash;
  10060. break;
  10061. }
  10062. }
  10063. if (flag) break; //Card found
  10064. }
  10065. if (ret == -1)
  10066. ret = flag;
  10067. else
  10068. ret &= flag;
  10069. if (!ret) break;
  10070. }
  10071. if (!ret)
  10072. { //When check fails, restore original hash values. [Skotlex]
  10073. sd->setitem_hash = setitem_hash;
  10074. sd->setitem_hash2 = setitem_hash2;
  10075. }
  10076. script_pushint(st,ret);
  10077. return 0;
  10078. }
  10079. /*================================================
  10080. * Check how many given inserted cards in the CURRENT
  10081. * weapon - used for 2/15's cards patch [Lupus]
  10082. *------------------------------------------------*/
  10083. BUILDIN_FUNC(cardscnt)
  10084. {
  10085. TBL_PC *sd;
  10086. int i, k, id = 1;
  10087. int ret = 0;
  10088. int index;
  10089. sd = script_rid2sd(st);
  10090. for (i=0; id!=0; i++) {
  10091. FETCH (i+2, id) else id = 0;
  10092. if (id <= 0)
  10093. continue;
  10094. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10095. if(index < 0) continue;
  10096. if(!sd->inventory_data[index])
  10097. continue;
  10098. if(itemdb_type(id) != IT_CARD) {
  10099. if (sd->inventory_data[index]->nameid == id)
  10100. ret+= sd->status.inventory[index].amount;
  10101. } else {
  10102. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10103. continue;
  10104. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10105. if (sd->status.inventory[index].card[k] == id)
  10106. ret++;
  10107. }
  10108. }
  10109. }
  10110. script_pushint(st,ret);
  10111. // script_pushint(st,current_equip_item_index);
  10112. return 0;
  10113. }
  10114. /*=======================================================
  10115. * Returns the refined number of the current item, or an
  10116. * item with inventory index specified
  10117. *-------------------------------------------------------*/
  10118. BUILDIN_FUNC(getrefine)
  10119. {
  10120. TBL_PC *sd;
  10121. if ((sd = script_rid2sd(st))!= NULL)
  10122. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10123. else
  10124. script_pushint(st,0);
  10125. return 0;
  10126. }
  10127. /*=======================================================
  10128. * Allows 2 Parents to adopt a character as a Baby
  10129. *-------------------------------------------------------*/
  10130. BUILDIN_FUNC(adopt)
  10131. {
  10132. int ret;
  10133. const char *parent1 = script_getstr(st,2);
  10134. const char *parent2 = script_getstr(st,3);
  10135. const char *child = script_getstr(st,4);
  10136. TBL_PC *p1_sd = map_nick2sd(parent1);
  10137. TBL_PC *p2_sd = map_nick2sd(parent2);
  10138. TBL_PC *c_sd = map_nick2sd(child);
  10139. if (!p1_sd || !p2_sd || !c_sd ||
  10140. p1_sd->status.base_level < 70 ||
  10141. p2_sd->status.base_level < 70)
  10142. return 0;
  10143. ret = pc_adoption(p1_sd, p2_sd, c_sd);
  10144. script_pushint(st,ret);
  10145. return 0;
  10146. }
  10147. /*=======================================================
  10148. * Day/Night controls
  10149. *-------------------------------------------------------*/
  10150. BUILDIN_FUNC(night)
  10151. {
  10152. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10153. return 0;
  10154. }
  10155. BUILDIN_FUNC(day)
  10156. {
  10157. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10158. return 0;
  10159. }
  10160. //=======================================================
  10161. // Unequip [Spectre]
  10162. //-------------------------------------------------------
  10163. BUILDIN_FUNC(unequip)
  10164. {
  10165. int i;
  10166. size_t num;
  10167. TBL_PC *sd;
  10168. num = script_getnum(st,2);
  10169. sd = script_rid2sd(st);
  10170. if( sd != NULL && num >= 1 && num <= ARRAYLENGTH(equip) )
  10171. {
  10172. i = pc_checkequip(sd,equip[num-1]);
  10173. if (i >= 0)
  10174. pc_unequipitem(sd,i,2);
  10175. return 0;
  10176. }
  10177. return 0;
  10178. }
  10179. BUILDIN_FUNC(equip)
  10180. {
  10181. int nameid=0,i;
  10182. TBL_PC *sd;
  10183. struct item_data *item_data;
  10184. sd = script_rid2sd(st);
  10185. nameid=script_getnum(st,2);
  10186. if((item_data = itemdb_exists(nameid)) == NULL)
  10187. {
  10188. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10189. return 1;
  10190. }
  10191. ARR_FIND( 0, MAX_INVENTORY, i, sd->status.inventory[i].nameid == nameid );
  10192. if( i < MAX_INVENTORY )
  10193. pc_equipitem(sd,i,item_data->equip);
  10194. return 0;
  10195. }
  10196. BUILDIN_FUNC(autoequip)
  10197. {
  10198. int nameid, flag;
  10199. struct item_data *item_data;
  10200. nameid=script_getnum(st,2);
  10201. flag=script_getnum(st,3);
  10202. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10203. item_data->flag.autoequip = flag>0?1:0;
  10204. }
  10205. return 0;
  10206. }
  10207. BUILDIN_FUNC(setbattleflag)
  10208. {
  10209. const char *flag, *value;
  10210. flag = script_getstr(st,2);
  10211. value = script_getstr(st,3);
  10212. if (battle_set_value(flag, value) == 0)
  10213. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10214. else
  10215. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10216. return 0;
  10217. }
  10218. BUILDIN_FUNC(getbattleflag)
  10219. {
  10220. const char *flag;
  10221. flag = script_getstr(st,2);
  10222. script_pushint(st,battle_get_value(flag));
  10223. return 0;
  10224. }
  10225. //=======================================================
  10226. // strlen [Valaris]
  10227. //-------------------------------------------------------
  10228. BUILDIN_FUNC(getstrlen)
  10229. {
  10230. const char *str = script_getstr(st,2);
  10231. int len = (str) ? (int)strlen(str) : 0;
  10232. script_pushint(st,len);
  10233. return 0;
  10234. }
  10235. //=======================================================
  10236. // isalpha [Valaris]
  10237. //-------------------------------------------------------
  10238. BUILDIN_FUNC(charisalpha)
  10239. {
  10240. const char *str=script_getstr(st,2);
  10241. int pos=script_getnum(st,3);
  10242. int val = ( str && pos >= 0 && (unsigned int)pos < strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10243. script_pushint(st,val);
  10244. return 0;
  10245. }
  10246. /// Changes the display name and/or display class of the npc.
  10247. /// Returns 0 is successful, 1 if the npc does not exist.
  10248. ///
  10249. /// setnpcdisplay("<npc name>", "<new display name>", <new class id>) -> <int>
  10250. /// setnpcdisplay("<npc name>", "<new display name>") -> <int>
  10251. /// setnpcdisplay("<npc name>", <new class id>) -> <int>
  10252. BUILDIN_FUNC(setnpcdisplay)
  10253. {
  10254. const char* name;
  10255. const char* newname = NULL;
  10256. int class_ = -1;
  10257. struct script_data* data;
  10258. struct npc_data* nd;
  10259. name = script_getstr(st,2);
  10260. data = script_getdata(st,3);
  10261. get_val(st, data);
  10262. if( script_hasdata(st,4) )
  10263. {
  10264. newname = conv_str(st,data);
  10265. class_ = script_getnum(st,4);
  10266. }
  10267. else if( data_isstring(data) )
  10268. {
  10269. newname = conv_str(st,data);
  10270. }
  10271. else if( data_isint(data) )
  10272. {
  10273. class_ = conv_num(st,data);
  10274. }
  10275. else
  10276. {
  10277. ShowError("script:setnpcdisplay: expected a string or number\n");
  10278. script_reportdata(data);
  10279. return 1;
  10280. }
  10281. nd = npc_name2id(name);
  10282. if( nd == NULL )
  10283. {// not found
  10284. script_pushint(st,1);
  10285. return 0;
  10286. }
  10287. // update npc
  10288. if( newname )
  10289. npc_setdisplayname(nd, newname);
  10290. if( class_ != -1 )
  10291. npc_setclass(nd, class_);
  10292. script_pushint(st,0);
  10293. return 0;
  10294. }
  10295. BUILDIN_FUNC(atoi)
  10296. {
  10297. const char *value;
  10298. value = script_getstr(st,2);
  10299. script_pushint(st,atoi(value));
  10300. return 0;
  10301. }
  10302. // case-insensitive substring search [lordalfa]
  10303. BUILDIN_FUNC(compare)
  10304. {
  10305. const char *message;
  10306. const char *cmpstring;
  10307. message = script_getstr(st,2);
  10308. cmpstring = script_getstr(st,3);
  10309. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10310. return 0;
  10311. }
  10312. // [zBuffer] List of mathematics commands --->
  10313. BUILDIN_FUNC(sqrt)
  10314. {
  10315. double i, a;
  10316. i = script_getnum(st,2);
  10317. a = sqrt(i);
  10318. script_pushint(st,(int)a);
  10319. return 0;
  10320. }
  10321. BUILDIN_FUNC(pow)
  10322. {
  10323. double i, a, b;
  10324. a = script_getnum(st,2);
  10325. b = script_getnum(st,3);
  10326. i = pow(a,b);
  10327. script_pushint(st,(int)i);
  10328. return 0;
  10329. }
  10330. BUILDIN_FUNC(distance)
  10331. {
  10332. int x0, y0, x1, y1;
  10333. x0 = script_getnum(st,2);
  10334. y0 = script_getnum(st,3);
  10335. x1 = script_getnum(st,4);
  10336. y1 = script_getnum(st,5);
  10337. script_pushint(st,distance_xy(x0,y0,x1,y1));
  10338. return 0;
  10339. }
  10340. BUILDIN_FUNC(checkcell)
  10341. {
  10342. int m;
  10343. const char *map = script_getstr(st, 2);
  10344. m = mapindex_name2id(map);
  10345. if(m){
  10346. script_pushint(st,map_getcell(m, script_getnum(st,3), script_getnum(st,4),script_getnum(st,5)));
  10347. } else {
  10348. script_pushint(st,0);
  10349. }
  10350. return 0;
  10351. }
  10352. // <--- [zBuffer] List of mathematics commands
  10353. // [zBuffer] List of dynamic var commands --->
  10354. //FIXME: some other functions are using this private function
  10355. void setd_sub(struct script_state *st, TBL_PC *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10356. {
  10357. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10358. return;
  10359. }
  10360. BUILDIN_FUNC(setd)
  10361. {
  10362. TBL_PC *sd=NULL;
  10363. char varname[100];
  10364. const char *value, *buffer;
  10365. int elem;
  10366. buffer = script_getstr(st, 2);
  10367. value = script_getstr(st, 3);
  10368. if(sscanf(buffer, "%99[^[][%d]", varname, &elem) < 2)
  10369. elem = 0;
  10370. if( not_server_variable(*varname) )
  10371. {
  10372. sd = script_rid2sd(st);
  10373. if( sd == NULL )
  10374. {
  10375. ShowError("script:setd: no player attached for player variable '%s'\n", buffer);
  10376. return 0;
  10377. }
  10378. }
  10379. if(varname[strlen(varname)-1] != '$') {
  10380. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10381. } else {
  10382. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10383. }
  10384. return 0;
  10385. }
  10386. #ifndef TXT_ONLY
  10387. int buildin_query_sql_sub(struct script_state* st, Sql* handle)
  10388. {
  10389. int i, j;
  10390. TBL_PC* sd = NULL;
  10391. const char* query;
  10392. struct script_data* data;
  10393. char* name;
  10394. int max_rows = 128;// maximum number of rows
  10395. int num_vars;
  10396. int num_cols;
  10397. // check target variables
  10398. for( i = 3; script_hasdata(st,i); ++i )
  10399. {
  10400. data = script_getdata(st, i);
  10401. if( data_isreference(data) && reference_tovariable(data) )
  10402. {// it's a variable
  10403. name = reference_getname(data);
  10404. if( not_server_variable(*name) && sd == NULL )
  10405. {// requires a player
  10406. sd = script_rid2sd(st);
  10407. if( sd == NULL )
  10408. {// no player attached
  10409. script_reportdata(data);
  10410. st->state = END;
  10411. return 1;
  10412. }
  10413. }
  10414. if( not_array_variable(*name) )
  10415. max_rows = 1;// not an array, limit to one row
  10416. }
  10417. else
  10418. {
  10419. ShowError("script:query_sql: not a variable\n");
  10420. script_reportdata(data);
  10421. st->state = END;
  10422. return 1;
  10423. }
  10424. }
  10425. num_vars = i - 3;
  10426. // Execute the query
  10427. query = script_getstr(st,2);
  10428. if( SQL_ERROR == Sql_QueryStr(handle, query) )
  10429. {
  10430. Sql_ShowDebug(handle);
  10431. script_pushint(st, 0);
  10432. return 1;
  10433. }
  10434. if( Sql_NumRows(handle) == 0 )
  10435. {// No data received
  10436. Sql_FreeResult(handle);
  10437. script_pushint(st, 0);
  10438. return 0;
  10439. }
  10440. // Count the number of columns to store
  10441. num_cols = Sql_NumColumns(handle);
  10442. if( num_vars < num_cols )
  10443. {
  10444. ShowWarning("script:query_sql: Too many columns, discarding last %u columns.\n", (unsigned int)(num_cols-num_vars));
  10445. script_reportsrc(st);
  10446. }
  10447. else if( num_vars > num_cols )
  10448. {
  10449. ShowWarning("script:query_sql: Too many variables (%u extra).\n", (unsigned int)(num_vars-num_cols));
  10450. script_reportsrc(st);
  10451. }
  10452. // Store data
  10453. for( i = 0; i < max_rows && SQL_SUCCESS == Sql_NextRow(handle); ++i )
  10454. {
  10455. for( j = 0; j < num_vars; ++j )
  10456. {
  10457. char* str = NULL;
  10458. if( j < num_cols )
  10459. Sql_GetData(handle, j, &str, NULL);
  10460. data = script_getdata(st, j+3);
  10461. name = reference_getname(data);
  10462. if( is_string_variable(name) )
  10463. setd_sub(st, sd, name, i, (void *)(str?str:""), reference_getref(data));
  10464. else
  10465. setd_sub(st, sd, name, i, (void *)(str?atoi(str):0), reference_getref(data));
  10466. }
  10467. }
  10468. if( i == max_rows && max_rows < Sql_NumRows(handle) )
  10469. {
  10470. ShowWarning("script:query_sql: Only %d/%u rows have been stored.\n", max_rows, (unsigned int)Sql_NumRows(handle));
  10471. script_reportsrc(st);
  10472. }
  10473. // Free data
  10474. Sql_FreeResult(handle);
  10475. script_pushint(st, i);
  10476. return 0;
  10477. }
  10478. #endif
  10479. BUILDIN_FUNC(query_sql)
  10480. {
  10481. #ifndef TXT_ONLY
  10482. return buildin_query_sql_sub(st, mmysql_handle);
  10483. #else
  10484. //for TXT version, we always return -1
  10485. script_pushint(st,-1);
  10486. return 0;
  10487. #endif
  10488. }
  10489. BUILDIN_FUNC(query_logsql)
  10490. {
  10491. #ifndef TXT_ONLY
  10492. return buildin_query_sql_sub(st, logmysql_handle);
  10493. #else
  10494. //for TXT version, we always return -1
  10495. script_pushint(st,-1);
  10496. return 0;
  10497. #endif
  10498. }
  10499. //Allows escaping of a given string.
  10500. BUILDIN_FUNC(escape_sql)
  10501. {
  10502. const char *str;
  10503. char *esc_str;
  10504. size_t len;
  10505. str = script_getstr(st,2);
  10506. len = strlen(str);
  10507. esc_str = aMallocA(len*2+1);
  10508. #if defined(TXT_ONLY)
  10509. jstrescapecpy(esc_str, str);
  10510. #else
  10511. Sql_EscapeStringLen(mmysql_handle, esc_str, str, len);
  10512. #endif
  10513. script_pushstr(st, esc_str);
  10514. return 0;
  10515. }
  10516. BUILDIN_FUNC(getd)
  10517. {
  10518. char varname[100];
  10519. const char *buffer;
  10520. int elem;
  10521. buffer = script_getstr(st, 2);
  10522. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10523. elem = 0;
  10524. // Push the 'pointer' so it's more flexible [Lance]
  10525. push_val(st->stack, C_NAME, (elem<<24) | add_str(varname));
  10526. return 0;
  10527. }
  10528. // <--- [zBuffer] List of dynamic var commands
  10529. // Pet stat [Lance]
  10530. BUILDIN_FUNC(petstat)
  10531. {
  10532. TBL_PC *sd = NULL;
  10533. struct pet_data *pd;
  10534. int flag = script_getnum(st,2);
  10535. sd = script_rid2sd(st);
  10536. if(!sd || !sd->status.pet_id || !sd->pd){
  10537. if(flag == 2)
  10538. script_pushconststr(st, "");
  10539. else
  10540. script_pushint(st,0);
  10541. return 0;
  10542. }
  10543. pd = sd->pd;
  10544. switch(flag){
  10545. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10546. case 2: script_pushstrcopy(st, pd->pet.name); break;
  10547. case 3: script_pushint(st,(int)pd->pet.level); break;
  10548. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10549. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10550. default:
  10551. script_pushint(st,0);
  10552. break;
  10553. }
  10554. return 0;
  10555. }
  10556. BUILDIN_FUNC(callshop)
  10557. {
  10558. TBL_PC *sd = NULL;
  10559. struct npc_data *nd;
  10560. const char *shopname;
  10561. int flag = 0;
  10562. sd = script_rid2sd(st);
  10563. if (!sd) {
  10564. script_pushint(st,0);
  10565. return 0;
  10566. }
  10567. shopname = script_getstr(st, 2);
  10568. if( script_hasdata(st,3) )
  10569. flag = script_getnum(st,3);
  10570. nd = npc_name2id(shopname);
  10571. if (!nd || nd->bl.type!=BL_NPC || nd->subtype!=SHOP) {
  10572. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)\n", shopname);
  10573. script_pushint(st,0);
  10574. return 1;
  10575. }
  10576. switch (flag) {
  10577. case 1: //Buy window
  10578. npc_buysellsel(sd,nd->bl.id,0);
  10579. break;
  10580. case 2: //Sell window
  10581. npc_buysellsel(sd,nd->bl.id,1);
  10582. break;
  10583. default: //Show menu
  10584. clif_npcbuysell(sd,nd->bl.id);
  10585. break;
  10586. }
  10587. sd->npc_shopid = nd->bl.id;
  10588. script_pushint(st,1);
  10589. return 0;
  10590. }
  10591. BUILDIN_FUNC(npcshopitem)
  10592. {
  10593. const char* npcname = script_getstr(st, 2);
  10594. struct npc_data* nd = npc_name2id(npcname);
  10595. int n, i;
  10596. int amount;
  10597. if( !nd || nd->subtype != SHOP )
  10598. { //Not found.
  10599. script_pushint(st,0);
  10600. return 0;
  10601. }
  10602. // get the count of new entries
  10603. amount = (script_lastdata(st)-2)/2;
  10604. // generate new shop item list
  10605. RECREATE(nd->u.shop.shop_item, struct npc_item_list, amount);
  10606. for( n = 0, i = 3; n < amount; n++, i+=2 )
  10607. {
  10608. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10609. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10610. }
  10611. nd->u.shop.count = n;
  10612. script_pushint(st,1);
  10613. return 0;
  10614. }
  10615. BUILDIN_FUNC(npcshopadditem)
  10616. {
  10617. const char* npcname = script_getstr(st,2);
  10618. struct npc_data* nd = npc_name2id(npcname);
  10619. int n, i;
  10620. int amount;
  10621. if( !nd || nd->subtype != SHOP )
  10622. { //Not found.
  10623. script_pushint(st,0);
  10624. return 0;
  10625. }
  10626. // get the count of new entries
  10627. amount = (script_lastdata(st)-2)/2;
  10628. // append new items to existing shop item list
  10629. RECREATE(nd->u.shop.shop_item, struct npc_item_list, nd->u.shop.count+amount);
  10630. for( n = nd->u.shop.count, i = 3; n < nd->u.shop.count+amount; n++, i+=2 )
  10631. {
  10632. nd->u.shop.shop_item[n].nameid = script_getnum(st,i);
  10633. nd->u.shop.shop_item[n].value = script_getnum(st,i+1);
  10634. }
  10635. nd->u.shop.count = n;
  10636. script_pushint(st,1);
  10637. return 0;
  10638. }
  10639. BUILDIN_FUNC(npcshopdelitem)
  10640. {
  10641. const char* npcname = script_getstr(st,2);
  10642. struct npc_data* nd = npc_name2id(npcname);
  10643. int n, i;
  10644. int amount;
  10645. int size;
  10646. if( !nd || nd->subtype != SHOP )
  10647. { //Not found.
  10648. script_pushint(st,0);
  10649. return 0;
  10650. }
  10651. amount = script_lastdata(st)-2;
  10652. size = nd->u.shop.count;
  10653. // remove specified items from the shop item list
  10654. for( i = 3; i < 3 + amount; i++ )
  10655. {
  10656. ARR_FIND( 0, size, n, nd->u.shop.shop_item[n].nameid == script_getnum(st,i) );
  10657. if( n < size )
  10658. {
  10659. memmove(&nd->u.shop.shop_item[n], &nd->u.shop.shop_item[n+1], sizeof(nd->u.shop.shop_item[0])*(size-n));
  10660. size--;
  10661. }
  10662. }
  10663. RECREATE(nd->u.shop.shop_item, struct npc_item_list, size);
  10664. nd->u.shop.count = size;
  10665. script_pushint(st,1);
  10666. return 0;
  10667. }
  10668. //Sets a script to attach to a shop npc.
  10669. BUILDIN_FUNC(npcshopattach)
  10670. {
  10671. const char* npcname = script_getstr(st,2);
  10672. struct npc_data* nd = npc_name2id(npcname);
  10673. int flag = 1;
  10674. if( script_hasdata(st,3) )
  10675. flag = script_getnum(st,3);
  10676. if( !nd || nd->subtype != SHOP )
  10677. { //Not found.
  10678. script_pushint(st,0);
  10679. return 0;
  10680. }
  10681. if (flag)
  10682. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  10683. else
  10684. nd->master_nd = NULL;
  10685. script_pushint(st,1);
  10686. return 0;
  10687. }
  10688. /*==========================================
  10689. * Returns some values of an item [Lupus]
  10690. * Price, Weight, etc...
  10691. setitemscript(itemID,"{new item bonus script}",[n]);
  10692. Where n:
  10693. 0 - script
  10694. 1 - Equip script
  10695. 2 - Unequip script
  10696. *------------------------------------------*/
  10697. BUILDIN_FUNC(setitemscript)
  10698. {
  10699. int item_id,n=0;
  10700. const char *script;
  10701. struct item_data *i_data;
  10702. struct script_code **dstscript;
  10703. item_id = script_getnum(st,2);
  10704. script = script_getstr(st,3);
  10705. if( script_hasdata(st,4) )
  10706. n=script_getnum(st,4);
  10707. i_data = itemdb_exists(item_id);
  10708. if (!i_data || script==NULL || script[0]!='{') {
  10709. script_pushint(st,0);
  10710. return 0;
  10711. }
  10712. switch (n) {
  10713. case 2:
  10714. dstscript = &i_data->unequip_script;
  10715. break;
  10716. case 1:
  10717. dstscript = &i_data->equip_script;
  10718. break;
  10719. default:
  10720. dstscript = &i_data->script;
  10721. break;
  10722. }
  10723. if(*dstscript)
  10724. script_free_code(*dstscript);
  10725. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  10726. script_pushint(st,1);
  10727. return 0;
  10728. }
  10729. /* Work In Progress [Lupus]
  10730. BUILDIN_FUNC(addmonsterdrop)
  10731. {
  10732. int class_,item_id,chance;
  10733. class_=script_getnum(st,2);
  10734. item_id=script_getnum(st,3);
  10735. chance=script_getnum(st,4);
  10736. if(class_>1000 && item_id>500 && chance>0) {
  10737. script_pushint(st,1);
  10738. } else {
  10739. script_pushint(st,0);
  10740. }
  10741. }
  10742. BUILDIN_FUNC(delmonsterdrop)
  10743. {
  10744. int class_,item_id;
  10745. class_=script_getnum(st,2);
  10746. item_id=script_getnum(st,3);
  10747. if(class_>1000 && item_id>500) {
  10748. script_pushint(st,1);
  10749. } else {
  10750. script_pushint(st,0);
  10751. }
  10752. }
  10753. */
  10754. /*==========================================
  10755. * Returns some values of a monster [Lupus]
  10756. * Name, Level, race, size, etc...
  10757. getmonsterinfo(monsterID,queryIndex);
  10758. *------------------------------------------*/
  10759. BUILDIN_FUNC(getmonsterinfo)
  10760. {
  10761. struct mob_db *mob;
  10762. int mob_id;
  10763. mob_id = script_getnum(st,2);
  10764. if (!mobdb_checkid(mob_id)) {
  10765. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i\n", mob_id);
  10766. if ( !script_getnum(st,3) ) //requested a string
  10767. script_pushconststr(st,"null");
  10768. else
  10769. script_pushint(st,-1);
  10770. return -1;
  10771. }
  10772. mob = mob_db(mob_id);
  10773. switch ( script_getnum(st,3) ) {
  10774. case 0: script_pushstrcopy(st,mob->jname); break;
  10775. case 1: script_pushint(st,mob->lv); break;
  10776. case 2: script_pushint(st,mob->status.max_hp); break;
  10777. case 3: script_pushint(st,mob->base_exp); break;
  10778. case 4: script_pushint(st,mob->job_exp); break;
  10779. case 5: script_pushint(st,mob->status.rhw.atk); break;
  10780. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  10781. case 7: script_pushint(st,mob->status.def); break;
  10782. case 8: script_pushint(st,mob->status.mdef); break;
  10783. case 9: script_pushint(st,mob->status.str); break;
  10784. case 10: script_pushint(st,mob->status.agi); break;
  10785. case 11: script_pushint(st,mob->status.vit); break;
  10786. case 12: script_pushint(st,mob->status.int_); break;
  10787. case 13: script_pushint(st,mob->status.dex); break;
  10788. case 14: script_pushint(st,mob->status.luk); break;
  10789. case 15: script_pushint(st,mob->status.rhw.range); break;
  10790. case 16: script_pushint(st,mob->range2); break;
  10791. case 17: script_pushint(st,mob->range3); break;
  10792. case 18: script_pushint(st,mob->status.size); break;
  10793. case 19: script_pushint(st,mob->status.race); break;
  10794. case 20: script_pushint(st,mob->status.def_ele); break;
  10795. case 21: script_pushint(st,mob->status.mode); break;
  10796. default: script_pushint(st,-1); //wrong Index
  10797. }
  10798. return 0;
  10799. }
  10800. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  10801. {
  10802. TBL_PC *sd = NULL;
  10803. if(script_hasdata(st,2))
  10804. sd = map_nick2sd(script_getstr(st,2));
  10805. else
  10806. sd = script_rid2sd(st);
  10807. if(sd)
  10808. script_pushint(st,(sd->vender_id != 0));
  10809. else
  10810. script_pushint(st,0);
  10811. return 0;
  10812. }
  10813. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  10814. {
  10815. TBL_PC *sd = NULL;
  10816. if(script_hasdata(st,2))
  10817. sd = map_nick2sd(script_getstr(st,2));
  10818. else
  10819. sd = script_rid2sd(st);
  10820. if(sd)
  10821. script_pushint(st,(sd->chatID != 0));
  10822. else
  10823. script_pushint(st,0);
  10824. return 0;
  10825. }
  10826. int axtoi(const char *hexStg)
  10827. {
  10828. int n = 0; // position in string
  10829. int m = 0; // position in digit[] to shift
  10830. int count; // loop index
  10831. int intValue = 0; // integer value of hex string
  10832. int digit[11]; // hold values to convert
  10833. while (n < 10) {
  10834. if (hexStg[n]=='\0')
  10835. break;
  10836. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  10837. digit[n] = hexStg[n] & 0x0f; //convert to int
  10838. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  10839. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10840. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  10841. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  10842. else break;
  10843. n++;
  10844. }
  10845. count = n;
  10846. m = n - 1;
  10847. n = 0;
  10848. while(n < count) {
  10849. // digit[n] is value of hex digit at position n
  10850. // (m << 2) is the number of positions to shift
  10851. // OR the bits into return value
  10852. intValue = intValue | (digit[n] << (m << 2));
  10853. m--; // adjust the position to set
  10854. n++; // next digit to process
  10855. }
  10856. return (intValue);
  10857. }
  10858. // [Lance] Hex string to integer converter
  10859. BUILDIN_FUNC(axtoi)
  10860. {
  10861. const char *hex = script_getstr(st,2);
  10862. script_pushint(st,axtoi(hex));
  10863. return 0;
  10864. }
  10865. // [zBuffer] List of player cont commands --->
  10866. BUILDIN_FUNC(rid2name)
  10867. {
  10868. struct block_list *bl = NULL;
  10869. int rid = script_getnum(st,2);
  10870. if((bl = map_id2bl(rid)))
  10871. {
  10872. switch(bl->type) {
  10873. case BL_MOB: script_pushstrcopy(st,((TBL_MOB*)bl)->name); break;
  10874. case BL_PC: script_pushstrcopy(st,((TBL_PC*)bl)->status.name); break;
  10875. case BL_NPC: script_pushstrcopy(st,((TBL_NPC*)bl)->exname); break;
  10876. case BL_PET: script_pushstrcopy(st,((TBL_PET*)bl)->pet.name); break;
  10877. case BL_HOM: script_pushstrcopy(st,((TBL_HOM*)bl)->homunculus.name); break;
  10878. default:
  10879. ShowError("buildin_rid2name: BL type unknown.\n");
  10880. script_pushconststr(st,"");
  10881. break;
  10882. }
  10883. } else {
  10884. ShowError("buildin_rid2name: invalid RID\n");
  10885. script_pushconststr(st,"(null)");
  10886. }
  10887. return 0;
  10888. }
  10889. BUILDIN_FUNC(pcblockmove)
  10890. {
  10891. int id, flag;
  10892. TBL_PC *sd = NULL;
  10893. id = script_getnum(st,2);
  10894. flag = script_getnum(st,3);
  10895. if(id)
  10896. sd = map_id2sd(id);
  10897. else
  10898. sd = script_rid2sd(st);
  10899. if(sd)
  10900. sd->state.blockedmove = flag > 0;
  10901. return 0;
  10902. }
  10903. BUILDIN_FUNC(pcfollow)
  10904. {
  10905. int id, targetid;
  10906. TBL_PC *sd = NULL;
  10907. id = script_getnum(st,2);
  10908. targetid = script_getnum(st,3);
  10909. if(id)
  10910. sd = map_id2sd(id);
  10911. else
  10912. sd = script_rid2sd(st);
  10913. if(sd)
  10914. pc_follow(sd, targetid);
  10915. return 0;
  10916. }
  10917. BUILDIN_FUNC(pcstopfollow)
  10918. {
  10919. int id;
  10920. TBL_PC *sd = NULL;
  10921. id = script_getnum(st,2);
  10922. if(id)
  10923. sd = map_id2sd(id);
  10924. else
  10925. sd = script_rid2sd(st);
  10926. if(sd)
  10927. pc_stop_following(sd);
  10928. return 0;
  10929. }
  10930. // <--- [zBuffer] List of player cont commands
  10931. // [zBuffer] List of mob control commands --->
  10932. //## TODO always return if the request/whatever was successfull [FlavioJS]
  10933. BUILDIN_FUNC(mobspawn)
  10934. {
  10935. int class_,x,y,id;
  10936. const char *str,*map;
  10937. // Who?
  10938. str =script_getstr(st,2);
  10939. // What?
  10940. class_ =script_getnum(st,3);
  10941. // Where?
  10942. map =script_getstr(st,4);
  10943. x =script_getnum(st,5);
  10944. y =script_getnum(st,6);
  10945. id = mob_once_spawn(map_id2sd(st->rid),map_mapname2mapid(map),x,y,str,class_,1,"");
  10946. script_pushint(st,id);
  10947. return 0;
  10948. }
  10949. BUILDIN_FUNC(mobremove)
  10950. {
  10951. int id;
  10952. struct block_list *bl = NULL;
  10953. id = script_getnum(st,2);
  10954. bl = map_id2bl(id);
  10955. if (bl && bl->type == BL_MOB)
  10956. unit_free(bl,0);
  10957. return 0;
  10958. }
  10959. BUILDIN_FUNC(getmobdata)
  10960. {
  10961. int num, id;
  10962. char *name;
  10963. struct mob_data *md = NULL;
  10964. TBL_PC *sd = st->rid?map_id2sd(st->rid):NULL;
  10965. id = script_getnum(st,2);
  10966. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || !data_isreference(script_getdata(st,3)) ){
  10967. ShowWarning("buildin_getmobdata: Error in argument!\n");
  10968. return -1;
  10969. }
  10970. num=st->stack->stack_data[st->start+3].u.num;
  10971. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10972. setd_sub(st,sd,name,0,(void *)(int)md->class_,script_getref(st,3));
  10973. setd_sub(st,sd,name,1,(void *)(int)md->level,script_getref(st,3));
  10974. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,script_getref(st,3));
  10975. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,script_getref(st,3));
  10976. setd_sub(st,sd,name,4,(void *)(int)md->master_id,script_getref(st,3));
  10977. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,script_getref(st,3));
  10978. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,script_getref(st,3));
  10979. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,script_getref(st,3));
  10980. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,script_getref(st,3));
  10981. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,script_getref(st,3));
  10982. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,script_getref(st,3));
  10983. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,script_getref(st,3));
  10984. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,script_getref(st,3));
  10985. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,script_getref(st,3));
  10986. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,script_getref(st,3));
  10987. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,script_getref(st,3));
  10988. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,script_getref(st,3));
  10989. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,script_getref(st,3));
  10990. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,script_getref(st,3));
  10991. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,script_getref(st,3));
  10992. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,script_getref(st,3));
  10993. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,script_getref(st,3));
  10994. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,script_getref(st,3));
  10995. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,script_getref(st,3));
  10996. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,script_getref(st,3));
  10997. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,script_getref(st,3));
  10998. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, script_getref(st,3));
  10999. return 0;
  11000. }
  11001. /// Changes the data of a mob
  11002. ///
  11003. /// setmobdata <mob unit id>,<type>,<value>;
  11004. BUILDIN_FUNC(setmobdata)
  11005. {
  11006. struct block_list* mob_bl;
  11007. mob_bl = map_id2bl(script_getnum(st,2));
  11008. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11009. {
  11010. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11011. int type;
  11012. int value;
  11013. type = script_getnum(st,3);
  11014. value = script_getnum(st,4);
  11015. switch( type )
  11016. {
  11017. case 0: md->class_ = (short)value; break;
  11018. case 1: md->level = (unsigned short)value; break;
  11019. case 2: md->status.hp = (unsigned int)value; break;
  11020. case 3: md->status.max_hp = (unsigned int)value; break;
  11021. case 4: md->master_id = value; break;
  11022. case 5: md->bl.m = (short)value; break;
  11023. case 6: md->bl.x = (short)value; break;
  11024. case 7: md->bl.y = (short)value; break;
  11025. case 8: md->status.speed = (unsigned short)value; break;
  11026. case 9: md->status.mode = (unsigned short)value; break;
  11027. case 10: md->special_state.ai = (unsigned int)value; break;
  11028. case 11: md->sc.option = (unsigned short)value; break;
  11029. case 12: md->vd->sex = (char)value; break;
  11030. case 13: md->vd->class_ = (unsigned short)value; break;
  11031. case 14: md->vd->hair_style = (unsigned short)value; break;
  11032. case 15: md->vd->hair_color = (unsigned short)value; break;
  11033. case 16: md->vd->head_bottom = (unsigned short)value; break;
  11034. case 17: md->vd->head_mid = (unsigned short)value; break;
  11035. case 18: md->vd->head_top = (unsigned short)value; break;
  11036. case 19: md->vd->cloth_color = (unsigned short)value; break;
  11037. case 20: md->vd->shield = (unsigned short)value; break;
  11038. case 21: md->vd->weapon = (unsigned short)value; break;
  11039. case 22: md->vd->shield = (unsigned short)value; break;
  11040. case 23: md->ud.dir = (unsigned char)value; break;
  11041. case 24: md->state.killer = value > 0 ? 1 : 0; break;
  11042. case 25: md->callback_flag = (short)value; break;
  11043. case 26: md->state.no_random_walk = value > 0 ? 1 : 0; break;
  11044. default:
  11045. ShowError("script:setmobdata: unknown data identifier %d\n", type);
  11046. return 1;
  11047. }
  11048. }
  11049. return 0;
  11050. }
  11051. /// Makes the mob assist the target unit as a slave
  11052. ///
  11053. /// mobassist <mob unit id>,"<player name>";
  11054. /// mobassist <mob unit id>,<target id>;
  11055. BUILDIN_FUNC(mobassist)
  11056. {
  11057. struct block_list* mob_bl;
  11058. // get mob
  11059. mob_bl = map_id2bl(script_getnum(st,2));
  11060. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11061. {
  11062. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11063. struct block_list* target_bl = NULL;
  11064. struct script_data* data;
  11065. // get target
  11066. data = script_getdata(st, 3);
  11067. get_val(st, data);
  11068. if( data_isstring(data) )
  11069. {
  11070. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11071. if( sd != NULL )
  11072. target_bl = &sd->bl;
  11073. }
  11074. if( target_bl == NULL )
  11075. target_bl = map_id2bl(conv_num(st, data));
  11076. // set mob as slave
  11077. if( target_bl != NULL )
  11078. {
  11079. struct unit_data* ud;
  11080. md->master_id = target_bl->id;
  11081. md->state.killer = 1;
  11082. mob_convertslave(md);
  11083. ud = unit_bl2ud(mob_bl);
  11084. if( ud != NULL )
  11085. {
  11086. if( ud->target != 0 )
  11087. md->target_id = ud->target;
  11088. else if( ud->skilltarget != 0 )
  11089. md->target_id = ud->skilltarget;
  11090. if( md->target_id != 0 )
  11091. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  11092. }
  11093. }
  11094. }
  11095. return 0;
  11096. }
  11097. /// Attaches the current npc or the target npc to the mob unit
  11098. ///
  11099. /// mobattach <mob unit id>{,"<npc name>"};
  11100. BUILDIN_FUNC(mobattach)
  11101. {
  11102. struct block_list* mob_bl;
  11103. mob_bl = map_id2bl(script_getnum(st,2));
  11104. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11105. {
  11106. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11107. TBL_NPC* nd = NULL;
  11108. if( script_hasdata(st,3) )
  11109. nd = npc_name2id(script_getstr(st, 3));
  11110. else
  11111. {
  11112. struct block_list* npc_bl = map_id2bl(st->oid);
  11113. if( npc_bl != NULL && npc_bl->type == BL_NPC )
  11114. nd = (TBL_NPC*)npc_bl;
  11115. }
  11116. if( nd != NULL )
  11117. md->nd = nd;
  11118. }
  11119. return 0;
  11120. }
  11121. /// Makes the unit walk to target position or map
  11122. /// Returns if it was successfull
  11123. ///
  11124. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11125. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11126. BUILDIN_FUNC(unitwalk)
  11127. {
  11128. struct block_list* bl;
  11129. bl = map_id2bl(script_getnum(st,2));
  11130. if( bl == NULL )
  11131. {
  11132. script_pushint(st, 0);
  11133. }
  11134. else if( script_hasdata(st,4) )
  11135. {
  11136. int x = script_getnum(st,3);
  11137. int y = script_getnum(st,4);
  11138. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11139. }
  11140. else
  11141. {
  11142. int map_id = script_getnum(st,3);
  11143. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11144. }
  11145. return 0;
  11146. }
  11147. /// Kills the unit
  11148. ///
  11149. /// unitkill <unit_id>;
  11150. BUILDIN_FUNC(unitkill)
  11151. {
  11152. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11153. if( bl != NULL )
  11154. status_kill(bl);
  11155. return 0;
  11156. }
  11157. /// Warps the unit to the target position in the target map
  11158. /// Returns if it was successfull
  11159. ///
  11160. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11161. BUILDIN_FUNC(unitwarp)
  11162. {
  11163. int unit_id;
  11164. int map;
  11165. short x;
  11166. short y;
  11167. struct block_list* bl;
  11168. unit_id = script_getnum(st,2);
  11169. map = map_mapname2mapid(script_getstr(st, 3));
  11170. x = (short)script_getnum(st,4);
  11171. y = (short)script_getnum(st,5);
  11172. bl = map_id2bl(unit_id);
  11173. if( map >= 0 && bl != NULL )
  11174. script_pushint(st, unit_warp(bl,map,x,y,0));
  11175. else
  11176. script_pushint(st, 0);
  11177. return 0;
  11178. }
  11179. /// Makes the unit attack the target.
  11180. /// If the unit is a player and <action type> is not 0, it does a continuous
  11181. /// attack instead of a single attack.
  11182. /// Returns if the request was successfull.
  11183. ///
  11184. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11185. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11186. BUILDIN_FUNC(unitattack)
  11187. {
  11188. struct block_list* unit_bl;
  11189. struct block_list* target_bl = NULL;
  11190. struct script_data* data;
  11191. int actiontype = 0;
  11192. // get unit
  11193. unit_bl = map_id2bl(script_getnum(st,2));
  11194. if( unit_bl == NULL ) {
  11195. script_pushint(st, 0);
  11196. return 0;
  11197. }
  11198. data = script_getdata(st, 3);
  11199. get_val(st, data);
  11200. if( data_isstring(data) )
  11201. {
  11202. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11203. if( sd != NULL )
  11204. target_bl = &sd->bl;
  11205. } else
  11206. target_bl = map_id2bl(conv_num(st, data));
  11207. // request the attack
  11208. if( target_bl == NULL )
  11209. {
  11210. script_pushint(st, 0);
  11211. return 0;
  11212. }
  11213. // get actiontype
  11214. if( script_hasdata(st,4) )
  11215. actiontype = script_getnum(st,4);
  11216. switch( unit_bl->type )
  11217. {
  11218. case BL_PC:
  11219. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11220. script_pushint(st, 1);
  11221. return 0;
  11222. case BL_MOB:
  11223. ((TBL_MOB *)unit_bl)->state.killer = 1;
  11224. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11225. break;
  11226. case BL_PET:
  11227. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11228. break;
  11229. default:
  11230. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11231. script_pushint(st, 0);
  11232. return 1;
  11233. }
  11234. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11235. return 0;
  11236. }
  11237. /// Makes the unit stop attacking and moving
  11238. ///
  11239. /// unitstop <unit_id>;
  11240. BUILDIN_FUNC(unitstop)
  11241. {
  11242. int unit_id;
  11243. struct block_list* bl;
  11244. unit_id = script_getnum(st,2);
  11245. bl = map_id2bl(unit_id);
  11246. if( bl != NULL )
  11247. {
  11248. unit_stop_attack(bl);
  11249. unit_stop_walking(bl,0);
  11250. if( bl->type == BL_MOB )
  11251. ((TBL_MOB*)bl)->target_id = 0;
  11252. }
  11253. return 0;
  11254. }
  11255. /// Makes the unit say the message
  11256. ///
  11257. /// unittalk <unit_id>,"<message>";
  11258. BUILDIN_FUNC(unittalk)
  11259. {
  11260. int unit_id;
  11261. const char* message;
  11262. struct block_list* bl;
  11263. unit_id = script_getnum(st,2);
  11264. message = script_getstr(st, 3);
  11265. bl = map_id2bl(unit_id);
  11266. if( bl != NULL )
  11267. {
  11268. struct StringBuf sbuf;
  11269. StringBuf_Init(&sbuf);
  11270. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11271. clif_message(bl, StringBuf_Value(&sbuf));
  11272. if( bl->type == BL_PC )
  11273. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11274. StringBuf_Destroy(&sbuf);
  11275. }
  11276. return 0;
  11277. }
  11278. /// Makes the unit do an emotion
  11279. ///
  11280. /// unitemote <unit_id>,<emotion>;
  11281. ///
  11282. /// @see e_* in const.txt
  11283. BUILDIN_FUNC(unitemote)
  11284. {
  11285. int unit_id;
  11286. int emotion;
  11287. struct block_list* bl;
  11288. unit_id = script_getnum(st,2);
  11289. emotion = script_getnum(st,3);
  11290. bl = map_id2bl(unit_id);
  11291. if( bl != NULL )
  11292. clif_emotion(bl, emotion);
  11293. return 0;
  11294. }
  11295. /// Makes the unit cast the skill on the target or self if no target is specified
  11296. ///
  11297. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11298. BUILDIN_FUNC(unitskilluseid)
  11299. {
  11300. int unit_id;
  11301. int skill_id;
  11302. int skill_lv;
  11303. int target_id;
  11304. struct block_list* bl;
  11305. unit_id = script_getnum(st,2);
  11306. skill_id = script_getnum(st,3);
  11307. skill_lv = script_getnum(st,4);
  11308. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11309. bl = map_id2bl(unit_id);
  11310. if( bl != NULL )
  11311. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11312. return 0;
  11313. }
  11314. /// Makes the unit cast the skill on the target position.
  11315. ///
  11316. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11317. BUILDIN_FUNC(unitskillusepos)
  11318. {
  11319. int unit_id;
  11320. int skill_id;
  11321. int skill_lv;
  11322. int skill_x;
  11323. int skill_y;
  11324. struct block_list* bl;
  11325. unit_id = script_getnum(st,2);
  11326. skill_id = script_getnum(st,3);
  11327. skill_lv = script_getnum(st,4);
  11328. skill_x = script_getnum(st,5);
  11329. skill_y = script_getnum(st,6);
  11330. bl = map_id2bl(unit_id);
  11331. if( bl != NULL )
  11332. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11333. return 0;
  11334. }
  11335. // <--- [zBuffer] List of mob control commands
  11336. /// Pauses the execution of the script, detaching the player
  11337. ///
  11338. /// sleep <mili seconds>;
  11339. BUILDIN_FUNC(sleep)
  11340. {
  11341. int ticks;
  11342. TBL_PC* sd;
  11343. ticks = script_getnum(st,2);
  11344. sd = map_id2sd(st->rid);
  11345. // detach the player
  11346. if( sd && sd->npc_id == st->oid )
  11347. {
  11348. sd->npc_id = 0;
  11349. }
  11350. st->rid = 0;
  11351. if( ticks <= 0 )
  11352. {// do nothing
  11353. }
  11354. else if( st->sleep.tick == 0 )
  11355. {// sleep for the target amount of time
  11356. st->state = RERUNLINE;
  11357. st->sleep.tick = ticks;
  11358. }
  11359. else
  11360. {// sleep time is over
  11361. st->sleep.tick = 0;
  11362. }
  11363. return 0;
  11364. }
  11365. /// Pauses the execution of the script, keeping the player attached
  11366. /// Returns if a player is still attached
  11367. ///
  11368. /// sleep2(<mili secconds>) -> <bool>
  11369. BUILDIN_FUNC(sleep2)
  11370. {
  11371. int ticks;
  11372. ticks = script_getnum(st,2);
  11373. if( ticks <= 0 )
  11374. {// do nothing
  11375. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11376. }
  11377. else if( !st->sleep.tick )
  11378. {// sleep for the target amount of time
  11379. st->state = RERUNLINE;
  11380. st->sleep.tick = ticks;
  11381. }
  11382. else
  11383. {// sleep time is over
  11384. st->sleep.tick = 0;
  11385. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11386. }
  11387. return 0;
  11388. }
  11389. /// Awakes all the sleep timers of the target npc
  11390. ///
  11391. /// awake "<npc name>";
  11392. BUILDIN_FUNC(awake)
  11393. {
  11394. struct npc_data* nd;
  11395. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11396. nd = npc_name2id(script_getstr(st, 2));
  11397. if( nd == NULL )
  11398. return 0;
  11399. while( node )
  11400. {
  11401. if( (int)node->key == nd->bl.id )
  11402. {// sleep timer for the npc
  11403. struct script_state* tst = (struct script_state*)node->data;
  11404. TBL_PC* sd = map_id2sd(tst->rid);
  11405. if( tst->sleep.timer == -1 )
  11406. {// already awake ???
  11407. node = node->next;
  11408. continue;
  11409. }
  11410. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11411. {// char not online anymore / another char of the same account is online - Cancel execution
  11412. tst->state = END;
  11413. tst->rid = 0;
  11414. }
  11415. delete_timer(tst->sleep.timer, run_script_timer);
  11416. node = script_erase_sleepdb(node);
  11417. tst->sleep.timer = -1;
  11418. tst->sleep.tick = 0;
  11419. run_script_main(tst);
  11420. }
  11421. else
  11422. {
  11423. node = node->next;
  11424. }
  11425. }
  11426. return 0;
  11427. }
  11428. /// Returns a reference to a variable of the target NPC.
  11429. /// Returns 0 if an error occurs.
  11430. ///
  11431. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11432. BUILDIN_FUNC(getvariableofnpc)
  11433. {
  11434. struct script_data* data;
  11435. char* name;
  11436. struct npc_data* nd;
  11437. data = script_getdata(st,2);
  11438. if( !data_isreference(data) )
  11439. {// Not a reference (aka varaible name)
  11440. ShowError("script:getvariableofnpc: not a variable\n");
  11441. script_reportdata(data);
  11442. script_pushnil(st);
  11443. st->state = END;
  11444. return 1;
  11445. }
  11446. name = reference_getname(data);
  11447. if( *name != '.' || name[1] == '@' )
  11448. {// not a npc variable
  11449. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11450. script_reportdata(data);
  11451. script_pushnil(st);
  11452. st->state = END;
  11453. return 1;
  11454. }
  11455. nd = npc_name2id(script_getstr(st,3));
  11456. if( nd == NULL || nd->subtype != SCRIPT || nd->u.scr.script == NULL )
  11457. {// NPC not found or has no script
  11458. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11459. script_pushnil(st);
  11460. st->state = END;
  11461. return 1;
  11462. }
  11463. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11464. return 0;
  11465. }
  11466. /// Opens a warp portal.
  11467. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11468. ///
  11469. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11470. ///
  11471. /// @author blackhole89
  11472. BUILDIN_FUNC(warpportal)
  11473. {
  11474. int spx;
  11475. int spy;
  11476. unsigned short mapindex;
  11477. int tpx;
  11478. int tpy;
  11479. struct skill_unit_group* group;
  11480. struct block_list* bl;
  11481. bl = map_id2bl(st->oid);
  11482. if( bl == NULL )
  11483. {
  11484. ShowError("script:warpportal: npc is needed\n");
  11485. return 1;
  11486. }
  11487. spx = script_getnum(st,2);
  11488. spy = script_getnum(st,3);
  11489. mapindex = mapindex_name2id(script_getstr(st, 4));
  11490. tpx = script_getnum(st,5);
  11491. tpy = script_getnum(st,6);
  11492. if( mapindex == 0 )
  11493. return 0;// map not found
  11494. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 0);
  11495. if( group == NULL )
  11496. return 0;// failed
  11497. group->val2 = (tpx<<16) | tpy;
  11498. group->val3 = mapindex;
  11499. return 0;
  11500. }
  11501. BUILDIN_FUNC(openmail)
  11502. {
  11503. TBL_PC* sd;
  11504. sd = script_rid2sd(st);
  11505. if( sd == NULL )
  11506. return 0;
  11507. #ifndef TXT_ONLY
  11508. mail_openmail(sd);
  11509. #endif
  11510. return 0;
  11511. }
  11512. /// Modifies flags of cells in the specified area.
  11513. ///
  11514. /// setcell "<map name>",<x1>,<y1>,<x2>,<y2>,<type>,<flag>;
  11515. ///
  11516. /// @see cell_* constants in const.txt for the types
  11517. BUILDIN_FUNC(setcell)
  11518. {
  11519. int m = map_mapname2mapid(script_getstr(st,2));
  11520. int x1 = script_getnum(st,3);
  11521. int y1 = script_getnum(st,4);
  11522. int x2 = script_getnum(st,5);
  11523. int y2 = script_getnum(st,6);
  11524. cell_t type = (cell_t)script_getnum(st,7);
  11525. bool flag = (bool)script_getnum(st,8);
  11526. int x,y;
  11527. if( x1 > x2 ) swap(x1,x2);
  11528. if( y1 > y2 ) swap(y1,y2);
  11529. for( y = y1; y <= y2; ++y )
  11530. for( x = x1; x <= x2; ++x )
  11531. map_setcell(m, x, y, type, flag);
  11532. return 0;
  11533. }
  11534. // declarations that were supposed to be exported from npc_chat.c
  11535. #ifdef PCRE_SUPPORT
  11536. BUILDIN_FUNC(defpattern);
  11537. BUILDIN_FUNC(activatepset);
  11538. BUILDIN_FUNC(deactivatepset);
  11539. BUILDIN_FUNC(deletepset);
  11540. #endif
  11541. struct script_function buildin_func[] = {
  11542. // NPC interaction
  11543. BUILDIN_DEF(mes,"s"),
  11544. BUILDIN_DEF(next,""),
  11545. BUILDIN_DEF(close,""),
  11546. BUILDIN_DEF(close2,""),
  11547. BUILDIN_DEF(menu,"sl*"),
  11548. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  11549. BUILDIN_DEF(prompt,"s*"),
  11550. //
  11551. BUILDIN_DEF(goto,"l"),
  11552. BUILDIN_DEF(callsub,"i*"),
  11553. BUILDIN_DEF(callfunc,"s*"),
  11554. BUILDIN_DEF(return,"?"),
  11555. BUILDIN_DEF(getarg,"i?"),
  11556. BUILDIN_DEF(jobchange,"i*"),
  11557. BUILDIN_DEF(jobname,"i"),
  11558. BUILDIN_DEF(input,"v??"),
  11559. BUILDIN_DEF(warp,"sii"),
  11560. BUILDIN_DEF(areawarp,"siiiisii"),
  11561. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  11562. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  11563. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  11564. BUILDIN_DEF(setlook,"ii"),
  11565. BUILDIN_DEF(set,"ii"),
  11566. BUILDIN_DEF(setarray,"rv*"),
  11567. BUILDIN_DEF(cleararray,"rvi"),
  11568. BUILDIN_DEF(copyarray,"rri"),
  11569. BUILDIN_DEF(getarraysize,"r"),
  11570. BUILDIN_DEF(deletearray,"r?"),
  11571. BUILDIN_DEF(getelementofarray,"ri"),
  11572. BUILDIN_DEF(getitem,"vi?"),
  11573. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  11574. BUILDIN_DEF(getnameditem,"is"),
  11575. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  11576. BUILDIN_DEF(makeitem,"iisii"),
  11577. BUILDIN_DEF(delitem,"ii"),
  11578. BUILDIN_DEF(delitem2,"iiiiiiiii"),
  11579. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  11580. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  11581. BUILDIN_DEF(cutin,"si"),
  11582. BUILDIN_DEF(viewpoint,"iiiii"),
  11583. BUILDIN_DEF(heal,"ii"),
  11584. BUILDIN_DEF(itemheal,"ii"),
  11585. BUILDIN_DEF(percentheal,"ii"),
  11586. BUILDIN_DEF(rand,"i?"),
  11587. BUILDIN_DEF(countitem,"i"),
  11588. BUILDIN_DEF(countitem2,"iiiiiiii"),
  11589. BUILDIN_DEF(checkweight,"ii"),
  11590. BUILDIN_DEF(readparam,"i*"),
  11591. BUILDIN_DEF(getcharid,"i*"),
  11592. BUILDIN_DEF(getpartyname,"i"),
  11593. BUILDIN_DEF(getpartymember,"i*"),
  11594. BUILDIN_DEF(getpartyleader,"i?"),
  11595. BUILDIN_DEF(getguildname,"i"),
  11596. BUILDIN_DEF(getguildmaster,"i"),
  11597. BUILDIN_DEF(getguildmasterid,"i"),
  11598. BUILDIN_DEF(strcharinfo,"i"),
  11599. BUILDIN_DEF(strnpcinfo,"i"),
  11600. BUILDIN_DEF(getequipid,"i"),
  11601. BUILDIN_DEF(getequipname,"i"),
  11602. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  11603. BUILDIN_DEF(repair,"i"), // [Valaris]
  11604. BUILDIN_DEF(getequipisequiped,"i"),
  11605. BUILDIN_DEF(getequipisenableref,"i"),
  11606. BUILDIN_DEF(getequipisidentify,"i"),
  11607. BUILDIN_DEF(getequiprefinerycnt,"i"),
  11608. BUILDIN_DEF(getequipweaponlv,"i"),
  11609. BUILDIN_DEF(getequippercentrefinery,"i"),
  11610. BUILDIN_DEF(successrefitem,"i"),
  11611. BUILDIN_DEF(failedrefitem,"i"),
  11612. BUILDIN_DEF(statusup,"i"),
  11613. BUILDIN_DEF(statusup2,"ii"),
  11614. BUILDIN_DEF(bonus,"ii"),
  11615. BUILDIN_DEF2(bonus,"bonus2","iii"),
  11616. BUILDIN_DEF2(bonus,"bonus3","iiii"),
  11617. BUILDIN_DEF2(bonus,"bonus4","iiiii"),
  11618. BUILDIN_DEF2(bonus,"bonus5","iiiiii"),
  11619. BUILDIN_DEF(bonusautoscript,"si?"),
  11620. BUILDIN_DEF(bonusautoscript2,"si?"),
  11621. BUILDIN_DEF(skill,"ii?"),
  11622. BUILDIN_DEF(addtoskill,"ii?"), // [Valaris]
  11623. BUILDIN_DEF(guildskill,"ii"),
  11624. BUILDIN_DEF(getskilllv,"i"),
  11625. BUILDIN_DEF(getgdskilllv,"ii"),
  11626. BUILDIN_DEF(basicskillcheck,""),
  11627. BUILDIN_DEF(getgmlevel,""),
  11628. BUILDIN_DEF(end,""),
  11629. BUILDIN_DEF(checkoption,"i"),
  11630. BUILDIN_DEF(setoption,"i?"),
  11631. BUILDIN_DEF(setcart,"?"),
  11632. BUILDIN_DEF(checkcart,""),
  11633. BUILDIN_DEF(setfalcon,"?"),
  11634. BUILDIN_DEF(checkfalcon,""),
  11635. BUILDIN_DEF(setriding,"?"),
  11636. BUILDIN_DEF(checkriding,""),
  11637. BUILDIN_DEF2(savepoint,"save","sii"),
  11638. BUILDIN_DEF(savepoint,"sii"),
  11639. BUILDIN_DEF(gettimetick,"i"),
  11640. BUILDIN_DEF(gettime,"i"),
  11641. BUILDIN_DEF(gettimestr,"si"),
  11642. BUILDIN_DEF(openstorage,""),
  11643. BUILDIN_DEF(guildopenstorage,"*"),
  11644. BUILDIN_DEF(itemskill,"ii"),
  11645. BUILDIN_DEF(produce,"i"),
  11646. BUILDIN_DEF(monster,"siisii*"),
  11647. BUILDIN_DEF(areamonster,"siiiisii*"),
  11648. BUILDIN_DEF(killmonster,"ss"),
  11649. BUILDIN_DEF(killmonsterall,"s"),
  11650. BUILDIN_DEF(clone,"siisi*"),
  11651. BUILDIN_DEF(doevent,"s"),
  11652. BUILDIN_DEF(donpcevent,"s"),
  11653. BUILDIN_DEF(cmdothernpc,"ss"),
  11654. BUILDIN_DEF(addtimer,"is"),
  11655. BUILDIN_DEF(deltimer,"s"),
  11656. BUILDIN_DEF(addtimercount,"si"),
  11657. BUILDIN_DEF(initnpctimer,"??"),
  11658. BUILDIN_DEF(stopnpctimer,"??"),
  11659. BUILDIN_DEF(startnpctimer,"??"),
  11660. BUILDIN_DEF(setnpctimer,"i?"),
  11661. BUILDIN_DEF(getnpctimer,"i?"),
  11662. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  11663. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  11664. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  11665. BUILDIN_DEF(announce,"si*"),
  11666. BUILDIN_DEF(mapannounce,"ssi*"),
  11667. BUILDIN_DEF(areaannounce,"siiiisi*"),
  11668. BUILDIN_DEF(getusers,"i"),
  11669. BUILDIN_DEF(getmapguildusers,"si"),
  11670. BUILDIN_DEF(getmapusers,"s"),
  11671. BUILDIN_DEF(getareausers,"siiii"),
  11672. BUILDIN_DEF(getareadropitem,"siiiii"),
  11673. BUILDIN_DEF(enablenpc,"s"),
  11674. BUILDIN_DEF(disablenpc,"s"),
  11675. BUILDIN_DEF(hideoffnpc,"s"),
  11676. BUILDIN_DEF(hideonnpc,"s"),
  11677. BUILDIN_DEF(sc_start,"iii?"),
  11678. BUILDIN_DEF(sc_start2,"iiii?"),
  11679. BUILDIN_DEF(sc_start4,"iiiiii?"),
  11680. BUILDIN_DEF(sc_end,"i?"),
  11681. BUILDIN_DEF(getscrate,"ii*"),
  11682. BUILDIN_DEF(debugmes,"s"),
  11683. BUILDIN_DEF2(catchpet,"pet","i"),
  11684. BUILDIN_DEF2(birthpet,"bpet",""),
  11685. BUILDIN_DEF(resetlvl,"i"),
  11686. BUILDIN_DEF(resetstatus,""),
  11687. BUILDIN_DEF(resetskill,""),
  11688. BUILDIN_DEF(skillpointcount,""),
  11689. BUILDIN_DEF(changebase,"i"),
  11690. BUILDIN_DEF(changesex,""),
  11691. BUILDIN_DEF(waitingroom,"si??"),
  11692. BUILDIN_DEF(delwaitingroom,"?"),
  11693. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  11694. BUILDIN_DEF(enablewaitingroomevent,"?"),
  11695. BUILDIN_DEF(disablewaitingroomevent,"?"),
  11696. BUILDIN_DEF2(enablewaitingroomevent,"enablearena",""), // Added by RoVeRT
  11697. BUILDIN_DEF2(disablewaitingroomevent,"disablearena",""), // Added by RoVeRT
  11698. BUILDIN_DEF(getwaitingroomstate,"i?"),
  11699. BUILDIN_DEF(warpwaitingpc,"sii?"),
  11700. BUILDIN_DEF(attachrid,"i"),
  11701. BUILDIN_DEF(detachrid,""),
  11702. BUILDIN_DEF(isloggedin,"i?"),
  11703. BUILDIN_DEF(setmapflagnosave,"ssii"),
  11704. BUILDIN_DEF(getmapflag,"si"),
  11705. BUILDIN_DEF(setmapflag,"si*"),
  11706. BUILDIN_DEF(removemapflag,"si"),
  11707. BUILDIN_DEF(pvpon,"s"),
  11708. BUILDIN_DEF(pvpoff,"s"),
  11709. BUILDIN_DEF(gvgon,"s"),
  11710. BUILDIN_DEF(gvgoff,"s"),
  11711. BUILDIN_DEF(emotion,"i*"),
  11712. BUILDIN_DEF(maprespawnguildid,"sii"),
  11713. BUILDIN_DEF(agitstart,""), // <Agit>
  11714. BUILDIN_DEF(agitend,""),
  11715. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  11716. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  11717. BUILDIN_DEF(getcastlename,"s"),
  11718. BUILDIN_DEF(getcastledata,"si*"),
  11719. BUILDIN_DEF(setcastledata,"sii"),
  11720. BUILDIN_DEF(requestguildinfo,"i*"),
  11721. BUILDIN_DEF(getequipcardcnt,"i"),
  11722. BUILDIN_DEF(successremovecards,"i"),
  11723. BUILDIN_DEF(failedremovecards,"ii"),
  11724. BUILDIN_DEF(marriage,"s"),
  11725. BUILDIN_DEF2(wedding_effect,"wedding",""),
  11726. BUILDIN_DEF(divorce,""),
  11727. BUILDIN_DEF(ispartneron,""),
  11728. BUILDIN_DEF(getpartnerid,""),
  11729. BUILDIN_DEF(getchildid,""),
  11730. BUILDIN_DEF(getmotherid,""),
  11731. BUILDIN_DEF(getfatherid,""),
  11732. BUILDIN_DEF(warppartner,"sii"),
  11733. BUILDIN_DEF(getitemname,"i"),
  11734. BUILDIN_DEF(getitemslots,"i"),
  11735. BUILDIN_DEF(makepet,"i"),
  11736. BUILDIN_DEF(getexp,"ii"),
  11737. BUILDIN_DEF(getinventorylist,""),
  11738. BUILDIN_DEF(getskilllist,""),
  11739. BUILDIN_DEF(clearitem,""),
  11740. BUILDIN_DEF(classchange,"ii"),
  11741. BUILDIN_DEF(misceffect,"i"),
  11742. BUILDIN_DEF(soundeffect,"si"),
  11743. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  11744. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  11745. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  11746. BUILDIN_DEF(guardianinfo,"sii"), // display guardian data [Valaris]
  11747. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  11748. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  11749. BUILDIN_DEF(petloot,"i"), // [Valaris]
  11750. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  11751. BUILDIN_DEF(petskillattack,"iiii"), // [Skotlex]
  11752. BUILDIN_DEF(petskillattack2,"iiiii"), // [Valaris]
  11753. BUILDIN_DEF(petskillsupport,"iiiii"), // [Skotlex]
  11754. BUILDIN_DEF(skilleffect,"ii"), // skill effect [Celest]
  11755. BUILDIN_DEF(npcskilleffect,"iiii"), // npc skill effect [Valaris]
  11756. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  11757. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  11758. BUILDIN_DEF(nude,""), // nude command [Valaris]
  11759. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  11760. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  11761. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  11762. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  11763. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  11764. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  11765. BUILDIN_DEF(mobcount,"ss"),
  11766. BUILDIN_DEF(getlook,"i"),
  11767. BUILDIN_DEF(getsavepoint,"i"),
  11768. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  11769. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  11770. BUILDIN_DEF(npcstop,""), // [Valaris]
  11771. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  11772. BUILDIN_DEF(checkoption1,"i"),
  11773. BUILDIN_DEF(checkoption2,"i"),
  11774. BUILDIN_DEF(guildgetexp,"i"),
  11775. BUILDIN_DEF(guildchangegm,"is"),
  11776. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  11777. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  11778. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  11779. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  11780. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  11781. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  11782. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  11783. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  11784. BUILDIN_DEF(adopt,"sss"), // allows 2 parents to adopt a child
  11785. BUILDIN_DEF(night,""), // sets the server to night time
  11786. BUILDIN_DEF(day,""), // sets the server to day time
  11787. #ifdef PCRE_SUPPORT
  11788. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  11789. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  11790. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  11791. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  11792. #endif
  11793. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  11794. BUILDIN_DEF(getusersname,"*"),
  11795. BUILDIN_DEF(recovery,""),
  11796. BUILDIN_DEF(getpetinfo,"i"),
  11797. BUILDIN_DEF(gethominfo,"i"),
  11798. BUILDIN_DEF(checkequipedcard,"i"),
  11799. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  11800. BUILDIN_DEF(globalmes,"s*"),
  11801. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  11802. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  11803. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  11804. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  11805. BUILDIN_DEF(setnpcdisplay,"sv?"),
  11806. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  11807. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  11808. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  11809. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  11810. // [zBuffer] List of mathematics commands --->
  11811. BUILDIN_DEF(sqrt,"i"),
  11812. BUILDIN_DEF(pow,"ii"),
  11813. BUILDIN_DEF(distance,"iiii"),
  11814. BUILDIN_DEF(checkcell,"siii"),
  11815. // <--- [zBuffer] List of mathematics commands
  11816. // [zBuffer] List of dynamic var commands --->
  11817. BUILDIN_DEF(getd,"*"),
  11818. BUILDIN_DEF(setd,"*"),
  11819. // <--- [zBuffer] List of dynamic var commands
  11820. BUILDIN_DEF(petstat,"i"),
  11821. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  11822. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  11823. BUILDIN_DEF(npcshopadditem,"sii*"),
  11824. BUILDIN_DEF(npcshopdelitem,"si*"),
  11825. BUILDIN_DEF(npcshopattach,"s?"),
  11826. BUILDIN_DEF(equip,"i"),
  11827. BUILDIN_DEF(autoequip,"ii"),
  11828. BUILDIN_DEF(setbattleflag,"ss"),
  11829. BUILDIN_DEF(getbattleflag,"s"),
  11830. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  11831. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  11832. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  11833. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  11834. BUILDIN_DEF(axtoi,"s"),
  11835. BUILDIN_DEF(query_sql,"s*"),
  11836. BUILDIN_DEF(query_logsql,"s*"),
  11837. BUILDIN_DEF(escape_sql,"s"),
  11838. BUILDIN_DEF(atoi,"s"),
  11839. // [zBuffer] List of player cont commands --->
  11840. BUILDIN_DEF(rid2name,"i"),
  11841. BUILDIN_DEF(pcfollow,"ii"),
  11842. BUILDIN_DEF(pcstopfollow,"i"),
  11843. BUILDIN_DEF(pcblockmove,"ii"),
  11844. // <--- [zBuffer] List of player cont commands
  11845. // [zBuffer] List of mob control commands --->
  11846. BUILDIN_DEF(mobspawn,"*"),
  11847. BUILDIN_DEF(mobremove,"i"),
  11848. BUILDIN_DEF(getmobdata,"i*"),
  11849. BUILDIN_DEF(setmobdata,"iii"),
  11850. BUILDIN_DEF(mobassist,"i?"),
  11851. BUILDIN_DEF(mobattach,"i?"),
  11852. BUILDIN_DEF(unitwalk,"ii?"),
  11853. BUILDIN_DEF(unitkill,"i"),
  11854. BUILDIN_DEF(unitwarp,"isii"),
  11855. BUILDIN_DEF(unitattack,"iv?"),
  11856. BUILDIN_DEF(unitstop,"i"),
  11857. BUILDIN_DEF(unittalk,"is"),
  11858. BUILDIN_DEF(unitemote,"ii"),
  11859. BUILDIN_DEF(unitskilluseid,"iii?"), // originally by Qamera [Celest]
  11860. BUILDIN_DEF(unitskillusepos,"iiiii"), // [Celest]
  11861. // <--- [zBuffer] List of mob control commands
  11862. BUILDIN_DEF(sleep,"i"),
  11863. BUILDIN_DEF(sleep2,"i"),
  11864. BUILDIN_DEF(awake,"s"),
  11865. BUILDIN_DEF(getvariableofnpc,"rs"),
  11866. BUILDIN_DEF(warpportal,"iisii"),
  11867. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  11868. BUILDIN_DEF2(homunculus_shuffle,"homshuffle",""), //[Zephyrus]
  11869. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  11870. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  11871. BUILDIN_DEF(checkvending,"*"),
  11872. BUILDIN_DEF(checkchatting,"*"),
  11873. BUILDIN_DEF(openmail,""),
  11874. BUILDIN_DEF(setcell,"siiiiii"),
  11875. {NULL,NULL,NULL},
  11876. };