script.c 330 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364
  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 "clif.h"
  18. #include "chrif.h"
  19. #include "itemdb.h"
  20. #include "pc.h"
  21. #include "status.h"
  22. #include "storage.h"
  23. #include "mob.h"
  24. #include "npc.h"
  25. #include "pet.h"
  26. #include "mercenary.h" //[orn]
  27. #include "intif.h"
  28. #include "skill.h"
  29. #include "chat.h"
  30. #include "battle.h"
  31. #include "party.h"
  32. #include "guild.h"
  33. #include "atcommand.h"
  34. #include "charcommand.h"
  35. #include "log.h"
  36. #include "unit.h"
  37. #include "irc.h"
  38. #include "pet.h"
  39. #include "script.h"
  40. #include <stdio.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <math.h>
  44. #ifndef WIN32
  45. #include <sys/time.h>
  46. #endif
  47. #include <time.h>
  48. #include <setjmp.h>
  49. #include <errno.h>
  50. ///////////////////////////////////////////////////////////////////////////////
  51. //## TODO possible enhancements: [FlavioJS]
  52. // - 'callfunc' supporting labels in the current npc "::LabelName"
  53. // - 'callfunc' supporting labels in other npcs "NpcName::LabelName"
  54. // - 'function FuncName;' function declarations reverting to global functions
  55. // if local label isn't found
  56. // - join callfunc and callsub's functionality
  57. //
  58. // struct script_state* st;
  59. //
  60. /// Returns the script_data at the target index
  61. #define script_getdata(st,i) ( &((st)->stack->stack_data[(st)->start + (i)]) )
  62. /// Returns if the stack contains data at the target index
  63. #define script_hasdata(st,i) ( (st)->end > (st)->start + (i) )
  64. /// Returns the index of the last data in the stack
  65. #define script_lastdata(st) ( (st)->end - (st)->start - 1 )
  66. /// Pushes an int into the stack
  67. #define script_pushint(st,val) push_val((st)->stack, C_INT, (val))
  68. #define script_pushstr(st,val) push_str((st)->stack, C_STR, (val))
  69. #define script_pushconststr(st,val) push_str((st)->stack, C_CONSTSTR, (val))
  70. /// Pushes a nil into the stack
  71. #define script_pushnil(st) push_val((st)->stack, C_NOP, 0)
  72. /// Pushes a copy of the data in the target index
  73. #define script_pushcopy(st,i) push_copy((st)->stack, (st)->start + (i))
  74. #define script_getnum(st,val) conv_num(st, script_getdata(st,val))
  75. #define script_getstr(st,val) conv_str(st, script_getdata(st,val))
  76. #define script_getref(st,val) ( script_getdata(st,val)->ref )
  77. // Note: "top" functions/defines use indexes relative to the top of the stack
  78. // -1 is the index of the data at the top
  79. /// Returns the script_data at the target index relative to the top of the stack
  80. #define script_getdatatop(st,i) ( &((st)->stack->stack_data[(st)->stack->sp + (i)]) )
  81. /// Pushes a copy of the data in the target index relative to the top of the stack
  82. #define script_pushcopytop(st,i) push_copy((st)->stack, (st)->stack->sp + (i))
  83. /// Removes the range of values [start,end[ relative to the top of the stack
  84. #define script_removetop(st,start,end) ( pop_stack((st)->stack, ((st)->stack->sp + (start)), (st)->stack->sp + (end)) )
  85. //
  86. // struct script_data* data;
  87. //
  88. /// Returns if the script data is a string
  89. #define data_isstring(data) ( (data)->type == C_STR || (data)->type == C_CONSTSTR )
  90. /// Returns if the script data is an int
  91. #define data_isint(data) ( (data)->type == C_INT )
  92. /// Returns if the script data is a reference
  93. #define data_isreference(data) ( (data)->type == C_NAME )
  94. /// Returns if the script data is a label
  95. #define data_islabel(data) ( (data)->type == C_POS )
  96. /// Returns if the script data is an internal script function label
  97. #define data_isfunclabel(data) ( (data)->type == C_USERFUNC_POS )
  98. /// Returns if this is a reference to a constant
  99. #define reference_toconstant(data) ( str_data[reference_getid(data)].type == C_INT )
  100. /// Returns if this a reference to a param
  101. #define reference_toparam(data) ( str_data[reference_getid(data)].type == C_PARAM )
  102. /// Returns if this a reference to a variable
  103. //##TODO confirm it's C_NAME [FlavioJS]
  104. #define reference_tovariable(data) ( str_data[reference_getid(data)].type == C_NAME )
  105. /// Returns the unique id of the reference (id and index)
  106. #define reference_getuid(data) ( (data)->u.num )
  107. /// Returns the id of the reference
  108. #define reference_getid(data) ( (int32)(reference_getuid(data) & 0x00ffffff) )
  109. /// Returns the array index of the reference
  110. #define reference_getindex(data) ( (int32)(((uint32)(reference_getuid(data) & 0xff000000)) >> 24) )
  111. /// Returns the name of the reference
  112. #define reference_getname(data) ( str_buf + str_data[reference_getid(data)].str )
  113. /// Returns the linked list of uid-value pairs of the reference (can be NULL)
  114. #define reference_getref(data) ( (data)->ref )
  115. /// Returns the value of the constant
  116. #define reference_getconstant(data) ( str_data[reference_getid(data)].val )
  117. /// Returns the type of param
  118. #define reference_getparamtype(data) ( str_data[reference_getid(data)].val )
  119. /// Composes the uid of a reference from the id and the index
  120. #define reference_uid(id,idx) ( (int32)((((uint32)(id)) & 0x00ffffff) | (((uint32)(idx)) << 24)) )
  121. #define FETCH(n, t) \
  122. if( script_hasdata(st,n) ) \
  123. (t)=script_getnum(st,n);
  124. #define SCRIPT_BLOCK_SIZE 512
  125. enum { LABEL_NEXTLINE=1,LABEL_START };
  126. static unsigned char * script_buf = NULL;
  127. static int script_pos,script_size;
  128. #define GETVALUE(buf,i) ((int)MakeDWord(MakeWord((buf)[i],(buf)[i+1]),MakeWord((buf)[i+2],0)))
  129. #define SETVALUE(buf,i,n) ((buf)[i]=GetByte(n,0),(buf)[i+1]=GetByte(n,1),(buf)[i+2]=GetByte(n,2))
  130. #define GETSTRING(off) (str_buf+(off))
  131. static char *str_buf;
  132. static int str_pos,str_size;
  133. static struct str_data_struct {
  134. int type;
  135. int str;
  136. int backpatch;
  137. int label;
  138. int (*func)(struct script_state *st);
  139. int val;
  140. int next;
  141. } *str_data = NULL;
  142. int str_num=LABEL_START,str_data_size;
  143. // Using a prime number for SCRIPT_HASH_SIZE should give better distributions
  144. #define SCRIPT_HASH_SIZE 1021
  145. int str_hash[SCRIPT_HASH_SIZE];
  146. //#define SCRIPT_HASH_DJB2
  147. #define SCRIPT_HASH_SDBM
  148. //#define SCRIPT_HASH_ELF
  149. //#define SCRIPT_HASH_PJW
  150. static struct dbt *mapreg_db=NULL;
  151. static struct dbt *mapregstr_db=NULL;
  152. static int mapreg_dirty=-1;
  153. char mapreg_txt[256]="save/mapreg.txt";
  154. #define MAPREG_AUTOSAVE_INTERVAL (300*1000)
  155. static struct dbt *scriptlabel_db=NULL;
  156. static struct dbt *userfunc_db=NULL;
  157. static int parse_options=0;
  158. struct dbt* script_get_label_db(){ return scriptlabel_db; }
  159. struct dbt* script_get_userfunc_db(){ return userfunc_db; }
  160. struct Script_Config script_config;
  161. static jmp_buf error_jump;
  162. static char* error_msg;
  163. static const char* error_pos;
  164. static int error_report; // if the error should produce output
  165. // for advanced scripting support ( nested if, switch, while, for, do-while, function, etc )
  166. // [Eoe / jA 1080, 1081, 1094, 1164]
  167. enum curly_type {
  168. TYPE_NULL = 0,
  169. TYPE_IF,
  170. TYPE_SWITCH,
  171. TYPE_WHILE,
  172. TYPE_FOR,
  173. TYPE_DO,
  174. TYPE_USERFUNC,
  175. TYPE_ARGLIST // function argument list
  176. };
  177. #define ARGLIST_UNDEFINED 0
  178. #define ARGLIST_NO_PAREN 1
  179. #define ARGLIST_PAREN 2
  180. static struct {
  181. struct {
  182. int type;
  183. int index;
  184. int count;
  185. int flag;
  186. struct linkdb_node *case_label;
  187. } curly[256]; // 右カッコの情報
  188. int curly_count; // 右カッコの数
  189. int index; // スクリプト内で使用した構文の数
  190. } syntax;
  191. const char* parse_curly_close(const char* p);
  192. const char* parse_syntax_close(const char* p);
  193. const char* parse_syntax_close_sub(const char* p,int* flag);
  194. const char* parse_syntax(const char* p);
  195. static int parse_syntax_for_flag = 0;
  196. extern int current_equip_item_index; //for New CARS Scripts. It contains Inventory Index of the EQUIP_SCRIPT caller item. [Lupus]
  197. int potion_flag=0; //For use on Alchemist improved potions/Potion Pitcher. [Skotlex]
  198. int potion_hp=0, potion_per_hp=0, potion_sp=0, potion_per_sp=0;
  199. int potion_target=0;
  200. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  201. // [zBuffer] SQL Mapreg Saving/Loading Database Declaration
  202. char mapregsql_db[32] = "mapreg";
  203. char mapregsql_db_varname[32] = "varname";
  204. char mapregsql_db_index[32] = "index";
  205. char mapregsql_db_value[32] = "value";
  206. char tmp_sql[65535];
  207. // --------------------------------------------------------
  208. #endif
  209. int get_com(unsigned char *script,int *pos);
  210. int get_num(unsigned char *script,int *pos);
  211. extern struct script_function {
  212. int (*func)(struct script_state *st);
  213. const char *name;
  214. const char *arg;
  215. } buildin_func[];
  216. static struct linkdb_node *sleep_db;
  217. #define not_server_variable(prefix) ( (prefix) != '$' && (prefix) != '.')
  218. #define not_array_variable(prefix) ( (prefix) != '$' && (prefix) != '@' && (prefix) != '.' )
  219. #define is_string_variable(name) ( (name)[strlen(name) - 1] == '$' )
  220. /*==========================================
  221. * ローカルプロトタイプ宣言 (必要な物のみ)
  222. *------------------------------------------*/
  223. const char* parse_subexpr(const char* p,int limit);
  224. void push_val(struct script_stack *stack,int type,int val);
  225. int run_func(struct script_state *st);
  226. int mapreg_setreg(int num,int val);
  227. int mapreg_setregstr(int num,const char *str);
  228. enum c_op {
  229. C_NOP, // end of script/no value (nil)
  230. C_POS,
  231. C_INT, // number
  232. C_PARAM, // parameter variable (see pc_readparam/pc_setparam)
  233. C_FUNC, // buildin function call
  234. C_STR, // string (free'd automatically)
  235. C_CONSTSTR, // string (not free'd)
  236. C_ARG, // start of argument list
  237. C_NAME,
  238. C_EOL, // end of line (extra stack values are cleared)
  239. C_RETINFO,
  240. C_USERFUNC, // internal script function
  241. C_USERFUNC_POS, // internal script function label
  242. // operators
  243. C_OP3, // a ? b : c
  244. C_LOR, // a || b
  245. C_LAND, // a && b
  246. C_LE, // a <= b
  247. C_LT, // a < b
  248. C_GE, // a >= b
  249. C_GT, // a > b
  250. C_EQ, // a == b
  251. C_NE, // a != b
  252. C_XOR, // a ^ b
  253. C_OR, // a | b
  254. C_AND, // a & b
  255. C_ADD, // a + b
  256. C_SUB, // a - b
  257. C_MUL, // a * b
  258. C_DIV, // a / b
  259. C_MOD, // a % b
  260. C_NEG, // - a
  261. C_LNOT, // ! a
  262. C_NOT, // ~ a
  263. C_R_SHIFT, // a >> b
  264. C_L_SHIFT // a << b
  265. };
  266. enum {
  267. MF_NOMEMO, //0
  268. MF_NOTELEPORT,
  269. MF_NOSAVE,
  270. MF_NOBRANCH,
  271. MF_NOPENALTY,
  272. MF_NOZENYPENALTY,
  273. MF_PVP,
  274. MF_PVP_NOPARTY,
  275. MF_PVP_NOGUILD,
  276. MF_GVG,
  277. MF_GVG_NOPARTY, //10
  278. MF_NOTRADE,
  279. MF_NOSKILL,
  280. MF_NOWARP,
  281. MF_PARTYLOCK,
  282. MF_NOICEWALL,
  283. MF_SNOW,
  284. MF_FOG,
  285. MF_SAKURA,
  286. MF_LEAVES,
  287. MF_RAIN, //20
  288. MF_INDOORS,
  289. MF_NOGO,
  290. MF_CLOUDS,
  291. MF_CLOUDS2,
  292. MF_FIREWORKS,
  293. MF_GVG_CASTLE,
  294. MF_GVG_DUNGEON,
  295. MF_NIGHTENABLED,
  296. MF_NOBASEEXP,
  297. MF_NOJOBEXP, //30
  298. MF_NOMOBLOOT,
  299. MF_NOMVPLOOT,
  300. MF_NORETURN,
  301. MF_NOWARPTO,
  302. MF_NIGHTMAREDROP,
  303. MF_RESTRICTED,
  304. MF_NOCOMMAND,
  305. MF_NODROP,
  306. MF_JEXP,
  307. MF_BEXP, //40
  308. MF_NOVENDING,
  309. MF_LOADEVENT,
  310. MF_NOCHAT,
  311. MF_NOEXPPENALTY,
  312. MF_GUILDLOCK
  313. };
  314. const char* script_op2name(int op)
  315. {
  316. #define RETURN_OP_NAME(type) case type: return #type
  317. switch( op )
  318. {
  319. RETURN_OP_NAME(C_NOP);
  320. RETURN_OP_NAME(C_POS);
  321. RETURN_OP_NAME(C_INT);
  322. RETURN_OP_NAME(C_PARAM);
  323. RETURN_OP_NAME(C_FUNC);
  324. RETURN_OP_NAME(C_STR);
  325. RETURN_OP_NAME(C_CONSTSTR);
  326. RETURN_OP_NAME(C_ARG);
  327. RETURN_OP_NAME(C_NAME);
  328. RETURN_OP_NAME(C_EOL);
  329. RETURN_OP_NAME(C_RETINFO);
  330. RETURN_OP_NAME(C_USERFUNC);
  331. RETURN_OP_NAME(C_USERFUNC_POS);
  332. // operators
  333. RETURN_OP_NAME(C_OP3);
  334. RETURN_OP_NAME(C_LOR);
  335. RETURN_OP_NAME(C_LAND);
  336. RETURN_OP_NAME(C_LE);
  337. RETURN_OP_NAME(C_LT);
  338. RETURN_OP_NAME(C_GE);
  339. RETURN_OP_NAME(C_GT);
  340. RETURN_OP_NAME(C_EQ);
  341. RETURN_OP_NAME(C_NE);
  342. RETURN_OP_NAME(C_XOR);
  343. RETURN_OP_NAME(C_OR);
  344. RETURN_OP_NAME(C_AND);
  345. RETURN_OP_NAME(C_ADD);
  346. RETURN_OP_NAME(C_SUB);
  347. RETURN_OP_NAME(C_MUL);
  348. RETURN_OP_NAME(C_DIV);
  349. RETURN_OP_NAME(C_MOD);
  350. RETURN_OP_NAME(C_NEG);
  351. RETURN_OP_NAME(C_LNOT);
  352. RETURN_OP_NAME(C_NOT);
  353. RETURN_OP_NAME(C_R_SHIFT);
  354. RETURN_OP_NAME(C_L_SHIFT);
  355. default:
  356. ShowDebug("script_op2name: unexpected op=%d\n", op);
  357. return "???";
  358. }
  359. #undef RETURN_OP_NAME
  360. }
  361. /// Reports on the console the src of a script error.
  362. static void script_reportsrc(struct script_state *st)
  363. {
  364. struct block_list* bl;
  365. if( st->oid == 0 )
  366. return; //Can't report source.
  367. bl = map_id2bl(st->oid);
  368. if( bl == NULL )
  369. return;
  370. switch( bl->type )
  371. {
  372. case BL_NPC:
  373. if( bl->m >= 0 )
  374. ShowDebug("Source (NPC): %s at %s (%d,%d)\n", ((struct npc_data *)bl)->name, map[bl->m].name, bl->x, bl->y);
  375. else
  376. ShowDebug("Source (NPC): %s (invisible/not on a map)\n", ((struct npc_data *)bl)->name);
  377. break;
  378. default:
  379. if( bl->m >= 0 )
  380. 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);
  381. else
  382. ShowDebug("Source (Non-NPC type %d): name %s (invisible/not on a map)\n", bl->type, status_get_name(bl));
  383. break;
  384. }
  385. }
  386. /// Reports on the console information about the script data.
  387. static void script_reportdata(struct script_data* data)
  388. {
  389. if( data == NULL )
  390. return;
  391. switch( data->type )
  392. {
  393. case C_NOP:// no value
  394. ShowDebug("Data: nothing (nil)\n");
  395. break;
  396. case C_INT:// number
  397. ShowDebug("Data: number value=%d\n", data->u.num);
  398. break;
  399. case C_STR:
  400. case C_CONSTSTR:// string
  401. ShowDebug("Data: string value=\"%s\"\n", data->u.str);
  402. break;
  403. case C_NAME:// reference
  404. if( reference_tovariable(data) )
  405. {// variable
  406. char* name = reference_getname(data);
  407. if( not_array_variable(*name) )
  408. ShowDebug("Data: variable name='%s'\n", name);
  409. else
  410. ShowDebug("Data: variable name='%s' index=%d\n", name, reference_getindex(data));
  411. }
  412. else if( reference_toconstant(data) )
  413. {// constant
  414. ShowDebug("Data: constant name='%s' value=%d\n", reference_getname(data), reference_getconstant(data));
  415. }
  416. else if( reference_toparam(data) )
  417. {// param
  418. ShowDebug("Data: param name='%s' type=%d\n", reference_getname(data), reference_getparamtype(data));
  419. }
  420. else
  421. {// ???
  422. ShowDebug("Data: reference name='%s' type=%s\n", reference_getname(data), script_op2name(data->type));
  423. }
  424. break;
  425. case C_POS:// label
  426. ShowDebug("Data: label pos=%d\n", data->u.num);
  427. break;
  428. default:
  429. ShowDebug("Data: %s\n", script_op2name(data->type));
  430. break;
  431. }
  432. }
  433. /*==========================================
  434. * エラーメッセージ出力
  435. *------------------------------------------*/
  436. static void disp_error_message2(const char *mes,const char *pos,int report)
  437. {
  438. error_msg = aStrdup(mes);
  439. error_pos = pos;
  440. error_report = report;
  441. longjmp( error_jump, 1 );
  442. }
  443. #define disp_error_message(mes,pos) disp_error_message2(mes,pos,1)
  444. /// Checks event parameter validity
  445. static void check_event(struct script_state *st, const char *evt)
  446. {
  447. if( evt != NULL && *evt != '\0' && !stristr(evt,"::On") ){
  448. ShowError("NPC event parameter deprecated! Please use 'NPCNAME::OnEVENT' instead of '%s'.\n",evt);
  449. script_reportsrc(st);
  450. }
  451. }
  452. /*==========================================
  453. * 文字列のハッシュを計算
  454. *------------------------------------------*/
  455. #define calc_hash(x) (calc_hash2(x)%SCRIPT_HASH_SIZE)
  456. static unsigned int calc_hash2(const char* p)
  457. {
  458. #if defined(SCRIPT_HASH_DJB2)
  459. unsigned int h = 5381;
  460. while( *p ) // hash*33 + c
  461. h = ( h << 5 ) + h + ((unsigned char)TOLOWER(*p++));
  462. return h;
  463. #elif defined(SCRIPT_HASH_SDBM)
  464. unsigned int h = 0;
  465. while( *p )
  466. h = ( h << 6 ) + ( h << 16 ) - h + ((unsigned char)TOLOWER(*p++));
  467. return h;
  468. #elif defined(SCRIPT_HASH_ELF)
  469. unsigned int h = 0;
  470. unsigned int g;
  471. while( *p ){ // UNIX ELF hash
  472. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  473. g = h & 0xF0000000;
  474. if( g )
  475. h ^= g >> 24;
  476. h &= ~g;
  477. }
  478. return h;
  479. #elif defined(SCRIPT_HASH_PJW)
  480. unsigned int h = 0;
  481. unsigned int g;
  482. while( *p ){
  483. h = ( h << 4 ) + ((unsigned char)TOLOWER(*p++));
  484. g = h & 0xF0000000;
  485. if( g )
  486. {
  487. h ^= g>>24;
  488. h ^= g;
  489. }
  490. }
  491. return h;
  492. #else
  493. unsigned int h = 0;
  494. while( *p ){
  495. h = ( h << 1 ) + ( h >> 3 ) + ( h >> 5 ) + ( h >> 8 );
  496. h += (unsigned char)TOLOWER(*p++);
  497. }
  498. return h;
  499. #endif
  500. }
  501. /*==========================================
  502. * str_dataの中に名前があるか検索する
  503. *------------------------------------------*/
  504. // 既存のであれば番号、無ければ-1
  505. static int search_str(const char *p)
  506. {
  507. int i;
  508. i=str_hash[calc_hash(p)];
  509. while(i){
  510. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  511. return i;
  512. }
  513. i=str_data[i].next;
  514. }
  515. return -1;
  516. }
  517. /*==========================================
  518. * str_dataに名前を登録
  519. *------------------------------------------*/
  520. // 既存のであれば番号、無ければ登録して新規番号
  521. int add_str(const char* p)
  522. {
  523. int i;
  524. int len;
  525. i=calc_hash(p);
  526. if(str_hash[i]==0){
  527. str_hash[i]=str_num;
  528. } else {
  529. i=str_hash[i];
  530. for(;;){
  531. if(strcasecmp(str_buf+str_data[i].str,p)==0){
  532. return i;
  533. }
  534. if(str_data[i].next==0)
  535. break;
  536. i=str_data[i].next;
  537. }
  538. str_data[i].next=str_num;
  539. }
  540. if(str_num>=str_data_size){
  541. str_data_size+=128;
  542. RECREATE(str_data,struct str_data_struct,str_data_size);
  543. memset(str_data + (str_data_size - 128), '\0', 128);
  544. }
  545. len=(int)strlen(p);
  546. while(str_pos+len+1>=str_size){
  547. str_size+=256;
  548. RECREATE(str_buf,char,str_size);
  549. memset(str_buf + (str_size - 256), '\0', 256);
  550. }
  551. memcpy(str_buf+str_pos,p,len+1);
  552. str_data[str_num].type=C_NOP;
  553. str_data[str_num].str=str_pos;
  554. str_data[str_num].next=0;
  555. str_data[str_num].func=NULL;
  556. str_data[str_num].backpatch=-1;
  557. str_data[str_num].label=-1;
  558. str_pos+=len+1;
  559. return str_num++;
  560. }
  561. /*==========================================
  562. * スクリプトバッファサイズの確認と拡張
  563. *------------------------------------------*/
  564. static void expand_script_buf(void)
  565. {
  566. script_size+=SCRIPT_BLOCK_SIZE;
  567. RECREATE(script_buf,unsigned char,script_size);
  568. }
  569. /*==========================================
  570. * スクリプトバッファに1バイト書き込む
  571. *------------------------------------------*/
  572. #define add_scriptb(a) if( script_pos+1>=script_size ) expand_script_buf(); script_buf[script_pos++]=(uint8)(a)
  573. #if 0
  574. static void add_scriptb(int a)
  575. {
  576. expand_script_buf();
  577. script_buf[script_pos++]=a;
  578. }
  579. #endif
  580. /*==========================================
  581. * スクリプトバッファにデータタイプを書き込む
  582. *------------------------------------------*/
  583. static void add_scriptc(int a)
  584. {
  585. while(a>=0x40){
  586. add_scriptb((a&0x3f)|0x40);
  587. a=(a-0x40)>>6;
  588. }
  589. add_scriptb(a&0x3f);
  590. }
  591. /*==========================================
  592. * スクリプトバッファに整数を書き込む
  593. *------------------------------------------*/
  594. static void add_scripti(int a)
  595. {
  596. while(a>=0x40){
  597. add_scriptb(a|0xc0);
  598. a=(a-0x40)>>6;
  599. }
  600. add_scriptb(a|0x80);
  601. }
  602. /*==========================================
  603. * スクリプトバッファにラベル/変数/関数を書き込む
  604. *------------------------------------------*/
  605. // 最大16Mまで
  606. static void add_scriptl(int l)
  607. {
  608. int backpatch = str_data[l].backpatch;
  609. switch(str_data[l].type){
  610. case C_POS:
  611. case C_USERFUNC_POS:
  612. add_scriptc(C_POS);
  613. add_scriptb(str_data[l].label);
  614. add_scriptb(str_data[l].label>>8);
  615. add_scriptb(str_data[l].label>>16);
  616. break;
  617. case C_NOP:
  618. case C_USERFUNC:
  619. // ラベルの可能性があるのでbackpatch用データ埋め込み
  620. add_scriptc(C_NAME);
  621. str_data[l].backpatch=script_pos;
  622. add_scriptb(backpatch);
  623. add_scriptb(backpatch>>8);
  624. add_scriptb(backpatch>>16);
  625. break;
  626. case C_INT:
  627. add_scripti(abs(str_data[l].val));
  628. if(str_data[l].val < 0) //Notice that this is negative, from jA (Rayce)
  629. add_scriptc(C_NEG);
  630. break;
  631. default:
  632. // もう他の用途と確定してるので数字をそのまま
  633. add_scriptc(C_NAME);
  634. add_scriptb(l);
  635. add_scriptb(l>>8);
  636. add_scriptb(l>>16);
  637. break;
  638. }
  639. }
  640. /*==========================================
  641. * ラベルを解決する
  642. *------------------------------------------*/
  643. void set_label(int l,int pos, const char* script_pos)
  644. {
  645. int i,next;
  646. if(str_data[l].type==C_INT || str_data[l].type==C_PARAM)
  647. { //Prevent overwriting constants values and parameters [Skotlex]
  648. disp_error_message("set_label: invalid label name",script_pos);
  649. return;
  650. }
  651. if(str_data[l].label!=-1){
  652. disp_error_message("set_label: dup label ",script_pos);
  653. return;
  654. }
  655. str_data[l].type=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  656. str_data[l].label=pos;
  657. for(i=str_data[l].backpatch;i>=0 && i!=0x00ffffff;){
  658. next=GETVALUE(script_buf,i);
  659. script_buf[i-1]=(str_data[l].type == C_USERFUNC ? C_USERFUNC_POS : C_POS);
  660. SETVALUE(script_buf,i,pos);
  661. i=next;
  662. }
  663. }
  664. /// Skips spaces and/or comments.
  665. static
  666. const char* skip_space(const char* p)
  667. {
  668. for(;;)
  669. {
  670. while( ISSPACE(*p) )
  671. ++p;
  672. if( *p == '/' && p[1] == '/' )
  673. {// line comment
  674. while(*p && *p!='\n')
  675. ++p;
  676. }
  677. else if( *p == '/' && p[1] == '*' )
  678. {// block comment
  679. p += 2;
  680. for(;;)
  681. {
  682. if( *p == '\0' )
  683. disp_error_message("script:skip_space: end of file while parsing block comment. expected "CL_BOLD"*/"CL_NORM, p);
  684. if( *p == '*' || p[1] == '/' )
  685. {// end of block comment
  686. p += 2;
  687. break;
  688. }
  689. ++p;
  690. }
  691. }
  692. else
  693. break;
  694. }
  695. return p;
  696. }
  697. /// Skips a word.
  698. /// A word consists of undercores and/or alfanumeric characters,
  699. /// and valid variable prefixes/postfixes.
  700. static
  701. const char* skip_word(const char* p)
  702. {
  703. // prefix
  704. switch( *p )
  705. {
  706. case '@':// temporary char variable
  707. ++p; break;
  708. case '#':// account variable
  709. p += ( p[1] == '#' ? 2 : 1 ); break;
  710. case '.':// npc variable
  711. p += ( p[1] == '@' ? 2 : 1 ); break;
  712. case '$':// global variable
  713. p += ( p[1] == '@' ? 2 : 1 ); break;
  714. }
  715. while( ISALNUM(*p) || *p == '_' )
  716. ++p;
  717. // postfix
  718. if( *p == '$' )// string
  719. p++;
  720. return p;
  721. }
  722. /// Adds a word to str_data.
  723. /// @see skip_word
  724. /// @see add_str
  725. static
  726. int add_word(const char* p)
  727. {
  728. char* word;
  729. int len;
  730. int i;
  731. // Check for a word
  732. len = skip_word(p) - p;
  733. if( len == 0 )
  734. disp_error_message("script:add_word: invalid word. A word consists of undercores and/or alfanumeric characters, and valid variable prefixes/postfixes.", p);
  735. // Duplicate the word
  736. CREATE(word, char, len+1);
  737. memcpy(word, p, len);
  738. word[len] = 0;
  739. // add the word
  740. i = add_str(word);
  741. aFree(word);
  742. return i;
  743. }
  744. /// Parses a function call.
  745. /// The argument list can have parenthesis or not.
  746. /// The number of arguments is checked.
  747. static
  748. const char* parse_callfunc(const char* p, int require_paren)
  749. {
  750. const char* p2;
  751. const char* arg=NULL;
  752. int func;
  753. func = add_word(p);
  754. if( str_data[func].type == C_FUNC ){
  755. // buildin function
  756. add_scriptl(func);
  757. add_scriptc(C_ARG);
  758. arg = buildin_func[str_data[func].val].arg;
  759. } else if( str_data[func].type == C_USERFUNC || str_data[func].type == C_USERFUNC_POS ){
  760. // script defined function
  761. int callsub = search_str("callsub");
  762. add_scriptl(callsub);
  763. add_scriptc(C_ARG);
  764. add_scriptl(func);
  765. arg = buildin_func[str_data[callsub].val].arg;
  766. if( *arg == 0 )
  767. disp_error_message("parse_callfunc: callsub has no arguments, please review it's definition",p);
  768. if( *arg != '*' )
  769. ++arg; // count func as argument
  770. } else
  771. disp_error_message("parse_line: expect command, missing function name or calling undeclared function",p);
  772. p = skip_word(p);
  773. p = skip_space(p);
  774. syntax.curly[syntax.curly_count].type = TYPE_ARGLIST;
  775. syntax.curly[syntax.curly_count].count = 0;
  776. if( *p == ';' )
  777. {// <func name> ';'
  778. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  779. } else if( *p == '(' && *(p2=skip_space(p+1)) == ')' )
  780. {// <func name> '(' ')'
  781. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  782. p = p2;
  783. /*
  784. } else if( 0 && require_paren && *p != '(' )
  785. {// <func name>
  786. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  787. */
  788. } else
  789. {// <func name> <arg list>
  790. if( require_paren ){
  791. if( *p != '(' )
  792. disp_error_message("need '('",p);
  793. ++p; // skip '('
  794. syntax.curly[syntax.curly_count].flag = ARGLIST_PAREN;
  795. } else if( *p == '(' ){
  796. syntax.curly[syntax.curly_count].flag = ARGLIST_UNDEFINED;
  797. } else {
  798. syntax.curly[syntax.curly_count].flag = ARGLIST_NO_PAREN;
  799. }
  800. ++syntax.curly_count;
  801. while( *arg ) {
  802. p2=parse_subexpr(p,-1);
  803. if( p == p2 )
  804. break; // not an argument
  805. if( *arg != '*' )
  806. ++arg; // next argument
  807. p=skip_space(p2);
  808. if( *arg == 0 || *p != ',' )
  809. break; // no more arguments
  810. ++p; // skip comma
  811. }
  812. --syntax.curly_count;
  813. }
  814. if( *arg && *arg != '?' && *arg != '*' )
  815. disp_error_message2("parse_callfunc: not enough arguments, expected ','", p, script_config.warn_func_mismatch_paramnum);
  816. if( syntax.curly[syntax.curly_count].type != TYPE_ARGLIST )
  817. disp_error_message("parse_callfunc: DEBUG last curly is not an argument list",p);
  818. if( syntax.curly[syntax.curly_count].flag == ARGLIST_PAREN ){
  819. if( *p != ')' )
  820. disp_error_message("parse_callfunc: expected ')' to close argument list",p);
  821. ++p;
  822. }
  823. add_scriptc(C_FUNC);
  824. return p;
  825. }
  826. /*==========================================
  827. * 項の解析
  828. *------------------------------------------*/
  829. const char* parse_simpleexpr(const char *p)
  830. {
  831. int i;
  832. p=skip_space(p);
  833. #ifdef DEBUG_FUNCIN
  834. if(battle_config.etc_log)
  835. ShowDebug("parse_simpleexpr %s\n",p);
  836. #endif
  837. if(*p==';' || *p==',')
  838. disp_error_message("parse_simpleexpr: unexpected expr end",p);
  839. if(*p=='('){
  840. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST )
  841. ++syntax.curly[i].count;
  842. p=parse_subexpr(p+1,-1);
  843. p=skip_space(p);
  844. if( (i=syntax.curly_count-1) >= 0 && syntax.curly[i].type == TYPE_ARGLIST &&
  845. syntax.curly[i].flag == ARGLIST_UNDEFINED && --syntax.curly[i].count == 0
  846. ){
  847. if( *p == ',' ){
  848. syntax.curly[i].flag = ARGLIST_PAREN;
  849. return p;
  850. } else
  851. syntax.curly[i].flag = ARGLIST_NO_PAREN;
  852. }
  853. if( *p != ')' )
  854. disp_error_message("parse_simpleexpr: unmatch ')'",p);
  855. ++p;
  856. } else if(ISDIGIT(*p) || ((*p=='-' || *p=='+') && ISDIGIT(p[1]))){
  857. char *np;
  858. i=strtoul(p,&np,0);
  859. add_scripti(i);
  860. p=np;
  861. } else if(*p=='"'){
  862. add_scriptc(C_STR);
  863. p++;
  864. while( *p && *p != '"' ){
  865. if( (unsigned char)p[-1] <= 0x7e && *p == '\\' )
  866. p++;
  867. else if( *p == '\n' )
  868. disp_error_message("parse_simpleexpr: unexpected newline @ string",p);
  869. add_scriptb(*p++);
  870. }
  871. if(!*p)
  872. disp_error_message("parse_simpleexpr: unexpected eof @ string",p);
  873. add_scriptb(0);
  874. p++; //'"'
  875. } else {
  876. int l;
  877. // label , register , function etc
  878. if(skip_word(p)==p)
  879. disp_error_message("parse_simpleexpr: unexpected character",p);
  880. l=add_word(p);
  881. if( str_data[l].type == C_FUNC || str_data[l].type == C_USERFUNC || str_data[l].type == C_USERFUNC_POS)
  882. return parse_callfunc(p,1);
  883. p=skip_word(p);
  884. if( *p == '[' ){
  885. // array(name[i] => getelementofarray(name,i) )
  886. add_scriptl(search_str("getelementofarray"));
  887. add_scriptc(C_ARG);
  888. add_scriptl(l);
  889. p=parse_subexpr(p+1,-1);
  890. p=skip_space(p);
  891. if( *p != ']' )
  892. disp_error_message("parse_simpleexpr: unmatch ']'",p);
  893. ++p;
  894. add_scriptc(C_FUNC);
  895. }else
  896. add_scriptl(l);
  897. }
  898. #ifdef DEBUG_FUNCIN
  899. if(battle_config.etc_log)
  900. ShowDebug("parse_simpleexpr end %s\n",p);
  901. #endif
  902. return p;
  903. }
  904. /*==========================================
  905. * 式の解析
  906. *------------------------------------------*/
  907. const char* parse_subexpr(const char* p,int limit)
  908. {
  909. int op,opl,len;
  910. const char* tmpp;
  911. #ifdef DEBUG_FUNCIN
  912. if(battle_config.etc_log)
  913. ShowDebug("parse_subexpr %s\n",p);
  914. #endif
  915. p=skip_space(p);
  916. if(*p=='-'){
  917. tmpp=skip_space(p+1);
  918. if(*tmpp==';' || *tmpp==','){
  919. add_scriptl(LABEL_NEXTLINE);
  920. p++;
  921. return p;
  922. }
  923. }
  924. tmpp=p;
  925. if((op=C_NEG,*p=='-') || (op=C_LNOT,*p=='!') || (op=C_NOT,*p=='~')){
  926. p=parse_subexpr(p+1,10);
  927. add_scriptc(op);
  928. } else
  929. p=parse_simpleexpr(p);
  930. p=skip_space(p);
  931. while((
  932. (op=C_OP3,opl=0,len=1,*p=='?') ||
  933. (op=C_ADD,opl=8,len=1,*p=='+') ||
  934. (op=C_SUB,opl=8,len=1,*p=='-') ||
  935. (op=C_MUL,opl=9,len=1,*p=='*') ||
  936. (op=C_DIV,opl=9,len=1,*p=='/') ||
  937. (op=C_MOD,opl=9,len=1,*p=='%') ||
  938. (op=C_LAND,opl=2,len=2,*p=='&' && p[1]=='&') ||
  939. (op=C_AND,opl=6,len=1,*p=='&') ||
  940. (op=C_LOR,opl=1,len=2,*p=='|' && p[1]=='|') ||
  941. (op=C_OR,opl=5,len=1,*p=='|') ||
  942. (op=C_XOR,opl=4,len=1,*p=='^') ||
  943. (op=C_EQ,opl=3,len=2,*p=='=' && p[1]=='=') ||
  944. (op=C_NE,opl=3,len=2,*p=='!' && p[1]=='=') ||
  945. (op=C_R_SHIFT,opl=7,len=2,*p=='>' && p[1]=='>') ||
  946. (op=C_GE,opl=3,len=2,*p=='>' && p[1]=='=') ||
  947. (op=C_GT,opl=3,len=1,*p=='>') ||
  948. (op=C_L_SHIFT,opl=7,len=2,*p=='<' && p[1]=='<') ||
  949. (op=C_LE,opl=3,len=2,*p=='<' && p[1]=='=') ||
  950. (op=C_LT,opl=3,len=1,*p=='<')) && opl>limit){
  951. p+=len;
  952. if(op == C_OP3) {
  953. p=parse_subexpr(p,-1);
  954. p=skip_space(p);
  955. if( *(p++) != ':')
  956. disp_error_message("parse_subexpr: need ':'", p-1);
  957. p=parse_subexpr(p,-1);
  958. } else {
  959. p=parse_subexpr(p,opl);
  960. }
  961. add_scriptc(op);
  962. p=skip_space(p);
  963. }
  964. #ifdef DEBUG_FUNCIN
  965. if(battle_config.etc_log)
  966. ShowDebug("parse_subexpr end %s\n",p);
  967. #endif
  968. return p; /* return first untreated operator */
  969. }
  970. /*==========================================
  971. * 式の評価
  972. *------------------------------------------*/
  973. const char* parse_expr(const char *p)
  974. {
  975. #ifdef DEBUG_FUNCIN
  976. if(battle_config.etc_log)
  977. ShowDebug("parse_expr %s\n",p);
  978. #endif
  979. switch(*p){
  980. case ')': case ';': case ':': case '[': case ']':
  981. case '}':
  982. disp_error_message("parse_expr: unexpected char",p);
  983. }
  984. p=parse_subexpr(p,-1);
  985. #ifdef DEBUG_FUNCIN
  986. if(battle_config.etc_log)
  987. ShowDebug("parse_expr end %s\n",p);
  988. #endif
  989. return p;
  990. }
  991. /*==========================================
  992. * 行の解析
  993. *------------------------------------------*/
  994. const char* parse_line(const char* p)
  995. {
  996. const char* p2;
  997. p=skip_space(p);
  998. if(*p==';') {
  999. // if(); for(); while(); のために閉じ判定
  1000. p = parse_syntax_close(p);
  1001. return p+1;
  1002. }
  1003. if(*p==')' && parse_syntax_for_flag)
  1004. return p+1;
  1005. p = skip_space(p);
  1006. if(p[0] == '{') {
  1007. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1008. syntax.curly[syntax.curly_count].count = -1;
  1009. syntax.curly[syntax.curly_count].index = -1;
  1010. syntax.curly_count++;
  1011. return p + 1;
  1012. } else if(p[0] == '}') {
  1013. return parse_curly_close(p);
  1014. }
  1015. // 構文関連の処理
  1016. p2 = parse_syntax(p);
  1017. if(p2 != NULL)
  1018. return p2;
  1019. p = parse_callfunc(p,0);
  1020. p = skip_space(p);
  1021. if(parse_syntax_for_flag) {
  1022. if( *p != ')' )
  1023. disp_error_message("parse_line: need ')'",p);
  1024. } else {
  1025. if( *p != ';' )
  1026. disp_error_message("parse_line: need ';'",p);
  1027. }
  1028. // if, for , while の閉じ判定
  1029. p = parse_syntax_close(p+1);
  1030. return p;
  1031. }
  1032. // { ... } の閉じ処理
  1033. const char* parse_curly_close(const char* p)
  1034. {
  1035. if(syntax.curly_count <= 0) {
  1036. disp_error_message("parse_curly_close: unexpected string",p);
  1037. return p + 1;
  1038. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_NULL) {
  1039. syntax.curly_count--;
  1040. // if, for , while の閉じ判定
  1041. p = parse_syntax_close(p + 1);
  1042. return p;
  1043. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_SWITCH) {
  1044. // switch() 閉じ判定
  1045. int pos = syntax.curly_count-1;
  1046. char label[256];
  1047. int l;
  1048. // 一時変数を消す
  1049. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1050. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1051. parse_line(label);
  1052. syntax.curly_count--;
  1053. // 無条件で終了ポインタに移動
  1054. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1055. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1056. parse_line(label);
  1057. syntax.curly_count--;
  1058. // 現在地のラベルを付ける
  1059. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1060. l=add_str(label);
  1061. set_label(l,script_pos, p);
  1062. if(syntax.curly[pos].flag) {
  1063. // default が存在する
  1064. sprintf(label,"goto __SW%x_DEF;",syntax.curly[pos].index);
  1065. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1066. parse_line(label);
  1067. syntax.curly_count--;
  1068. }
  1069. // 終了ラベルを付ける
  1070. sprintf(label,"__SW%x_FIN",syntax.curly[pos].index);
  1071. l=add_str(label);
  1072. set_label(l,script_pos, p);
  1073. linkdb_final(&syntax.curly[pos].case_label); // free the list of case label
  1074. syntax.curly_count--;
  1075. return p+1;
  1076. } else {
  1077. disp_error_message("parse_curly_close: unexpected string",p);
  1078. return p + 1;
  1079. }
  1080. }
  1081. // 構文関連の処理
  1082. // break, case, continue, default, do, for, function,
  1083. // if, switch, while をこの内部で処理します。
  1084. const char* parse_syntax(const char* p)
  1085. {
  1086. const char *p2 = skip_word(p);
  1087. switch(*p) {
  1088. case 'B':
  1089. case 'b':
  1090. if(p2 - p == 5 && !strncasecmp(p,"break",5)) {
  1091. // break の処理
  1092. char label[256];
  1093. int pos = syntax.curly_count - 1;
  1094. while(pos >= 0) {
  1095. if(syntax.curly[pos].type == TYPE_DO) {
  1096. sprintf(label,"goto __DO%x_FIN;",syntax.curly[pos].index);
  1097. break;
  1098. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1099. sprintf(label,"goto __FR%x_FIN;",syntax.curly[pos].index);
  1100. break;
  1101. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1102. sprintf(label,"goto __WL%x_FIN;",syntax.curly[pos].index);
  1103. break;
  1104. } else if(syntax.curly[pos].type == TYPE_SWITCH) {
  1105. sprintf(label,"goto __SW%x_FIN;",syntax.curly[pos].index);
  1106. break;
  1107. }
  1108. pos--;
  1109. }
  1110. if(pos < 0) {
  1111. disp_error_message("parse_syntax: unexpected 'break'",p);
  1112. } else {
  1113. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1114. parse_line(label);
  1115. syntax.curly_count--;
  1116. }
  1117. p = skip_space(p2);
  1118. if(*p != ';') {
  1119. disp_error_message("parse_syntax: need ';'",p);
  1120. }
  1121. p++;
  1122. // if, for , while の閉じ判定
  1123. p = parse_syntax_close(p + 1);
  1124. return p;
  1125. }
  1126. break;
  1127. case 'c':
  1128. case 'C':
  1129. if(p2 - p == 4 && !strncasecmp(p,"case",4)) {
  1130. // case の処理
  1131. int pos = syntax.curly_count-1;
  1132. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1133. disp_error_message("parse_syntax: unexpected 'case' ",p);
  1134. return p+1;
  1135. } else {
  1136. char label[256];
  1137. int l,v;
  1138. char *np;
  1139. if(syntax.curly[pos].count != 1) {
  1140. // FALLTHRU 用のジャンプ
  1141. sprintf(label,"goto __SW%x_%xJ;",syntax.curly[pos].index,syntax.curly[pos].count);
  1142. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1143. parse_line(label);
  1144. syntax.curly_count--;
  1145. // 現在地のラベルを付ける
  1146. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1147. l=add_str(label);
  1148. set_label(l,script_pos, p);
  1149. }
  1150. // switch 判定文
  1151. p = skip_space(p2);
  1152. if(p == p2) {
  1153. disp_error_message("parse_syntax: expect space ' '",p);
  1154. }
  1155. // check whether case label is integer or not
  1156. v = strtol(p,&np,0);
  1157. if(np == p) { //Check for constants
  1158. p2 = skip_word(p);
  1159. v = p2-p; // length of word at p2
  1160. memcpy(label,p,v);
  1161. label[v]='\0';
  1162. v = search_str(label);
  1163. if (v < 0 || str_data[v].type != C_INT)
  1164. disp_error_message("parse_syntax: 'case' label not integer",p);
  1165. v = str_data[v].val;
  1166. p = skip_word(p);
  1167. } else { //Numeric value
  1168. if((*p == '-' || *p == '+') && ISDIGIT(p[1])) // pre-skip because '-' can not skip_word
  1169. p++;
  1170. p = skip_word(p);
  1171. if(np != p)
  1172. disp_error_message("parse_syntax: 'case' label not integer",np);
  1173. }
  1174. p = skip_space(p);
  1175. if(*p != ':')
  1176. disp_error_message("parse_syntax: expect ':'",p);
  1177. sprintf(label,"if(%d != $@__SW%x_VAL) goto __SW%x_%x;",
  1178. v,syntax.curly[pos].index,syntax.curly[pos].index,syntax.curly[pos].count+1);
  1179. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1180. // 2回parse しないとダメ
  1181. p2 = parse_line(label);
  1182. parse_line(p2);
  1183. syntax.curly_count--;
  1184. if(syntax.curly[pos].count != 1) {
  1185. // FALLTHRU 終了後のラベル
  1186. sprintf(label,"__SW%x_%xJ",syntax.curly[pos].index,syntax.curly[pos].count);
  1187. l=add_str(label);
  1188. set_label(l,script_pos,p);
  1189. }
  1190. // check duplication of case label [Rayce]
  1191. if(linkdb_search(&syntax.curly[pos].case_label, (void*)v) != NULL)
  1192. disp_error_message("parse_syntax: dup 'case'",p);
  1193. linkdb_insert(&syntax.curly[pos].case_label, (void*)v, (void*)1);
  1194. sprintf(label,"set $@__SW%x_VAL,0;",syntax.curly[pos].index);
  1195. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1196. parse_line(label);
  1197. syntax.curly_count--;
  1198. syntax.curly[pos].count++;
  1199. }
  1200. return p + 1;
  1201. } else if(p2 - p == 8 && !strncasecmp(p,"continue",8)) {
  1202. // continue の処理
  1203. char label[256];
  1204. int pos = syntax.curly_count - 1;
  1205. while(pos >= 0) {
  1206. if(syntax.curly[pos].type == TYPE_DO) {
  1207. sprintf(label,"goto __DO%x_NXT;",syntax.curly[pos].index);
  1208. syntax.curly[pos].flag = 1; // continue 用のリンク張るフラグ
  1209. break;
  1210. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1211. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1212. break;
  1213. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1214. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1215. break;
  1216. }
  1217. pos--;
  1218. }
  1219. if(pos < 0) {
  1220. disp_error_message("parse_syntax: unexpected 'continue'",p);
  1221. } else {
  1222. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1223. parse_line(label);
  1224. syntax.curly_count--;
  1225. }
  1226. p = skip_space(p2);
  1227. if(*p != ';')
  1228. disp_error_message("parse_syntax: need ';'",p);
  1229. p++;
  1230. // if, for , while の閉じ判定
  1231. p = parse_syntax_close(p + 1);
  1232. return p;
  1233. }
  1234. break;
  1235. case 'd':
  1236. case 'D':
  1237. if(p2 - p == 7 && !strncasecmp(p,"default",7)) {
  1238. // switch - default の処理
  1239. int pos = syntax.curly_count-1;
  1240. if(pos < 0 || syntax.curly[pos].type != TYPE_SWITCH) {
  1241. disp_error_message("parse_syntax: unexpected 'default'",p);
  1242. } else if(syntax.curly[pos].flag) {
  1243. disp_error_message("parse_syntax: dup 'default'",p);
  1244. } else {
  1245. char label[256];
  1246. int l;
  1247. // 現在地のラベルを付ける
  1248. p = skip_space(p2);
  1249. if(*p != ':') {
  1250. disp_error_message("parse_syntax: need ':'",p);
  1251. }
  1252. sprintf(label,"__SW%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1253. l=add_str(label);
  1254. set_label(l,script_pos,p);
  1255. // 無条件で次のリンクに飛ばす
  1256. sprintf(label,"goto __SW%x_%x;",syntax.curly[pos].index,syntax.curly[pos].count+1);
  1257. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1258. parse_line(label);
  1259. syntax.curly_count--;
  1260. // default のラベルを付ける
  1261. sprintf(label,"__SW%x_DEF",syntax.curly[pos].index);
  1262. l=add_str(label);
  1263. set_label(l,script_pos,p);
  1264. syntax.curly[syntax.curly_count - 1].flag = 1;
  1265. syntax.curly[pos].count++;
  1266. }
  1267. return p + 1;
  1268. } else if(p2 - p == 2 && !strncasecmp(p,"do",2)) {
  1269. int l;
  1270. char label[256];
  1271. p=skip_space(p2);
  1272. syntax.curly[syntax.curly_count].type = TYPE_DO;
  1273. syntax.curly[syntax.curly_count].count = 1;
  1274. syntax.curly[syntax.curly_count].index = syntax.index++;
  1275. syntax.curly[syntax.curly_count].flag = 0;
  1276. // 現在地のラベル形成する
  1277. sprintf(label,"__DO%x_BGN",syntax.curly[syntax.curly_count].index);
  1278. l=add_str(label);
  1279. set_label(l,script_pos,p);
  1280. syntax.curly_count++;
  1281. return p;
  1282. }
  1283. break;
  1284. case 'f':
  1285. case 'F':
  1286. if(p2 - p == 3 && !strncasecmp(p,"for",3)) {
  1287. int l;
  1288. char label[256];
  1289. int pos = syntax.curly_count;
  1290. syntax.curly[syntax.curly_count].type = TYPE_FOR;
  1291. syntax.curly[syntax.curly_count].count = 1;
  1292. syntax.curly[syntax.curly_count].index = syntax.index++;
  1293. syntax.curly[syntax.curly_count].flag = 0;
  1294. syntax.curly_count++;
  1295. p=skip_space(p2);
  1296. if(*p != '(')
  1297. disp_error_message("parse_syntax: need '('",p);
  1298. p++;
  1299. // 初期化文を実行する
  1300. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1301. p=parse_line(p);
  1302. syntax.curly_count--;
  1303. // 条件判断開始のラベル形成する
  1304. sprintf(label,"__FR%x_J",syntax.curly[pos].index);
  1305. l=add_str(label);
  1306. set_label(l,script_pos,p);
  1307. p=skip_space(p);
  1308. if(*p == ';') {
  1309. // for(;;) のパターンなので必ず真
  1310. ;
  1311. } else {
  1312. // 条件が偽なら終了地点に飛ばす
  1313. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1314. add_scriptl(add_str("jump_zero"));
  1315. add_scriptc(C_ARG);
  1316. p=parse_expr(p);
  1317. p=skip_space(p);
  1318. add_scriptl(add_str(label));
  1319. add_scriptc(C_FUNC);
  1320. }
  1321. if(*p != ';')
  1322. disp_error_message("parse_syntax: need ';'",p);
  1323. p++;
  1324. // ループ開始に飛ばす
  1325. sprintf(label,"goto __FR%x_BGN;",syntax.curly[pos].index);
  1326. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1327. parse_line(label);
  1328. syntax.curly_count--;
  1329. // 次のループへのラベル形成する
  1330. sprintf(label,"__FR%x_NXT",syntax.curly[pos].index);
  1331. l=add_str(label);
  1332. set_label(l,script_pos,p);
  1333. // 次のループに入る時の処理
  1334. // for 最後の ')' を ';' として扱うフラグ
  1335. parse_syntax_for_flag = 1;
  1336. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1337. p=parse_line(p);
  1338. syntax.curly_count--;
  1339. parse_syntax_for_flag = 0;
  1340. // 条件判定処理に飛ばす
  1341. sprintf(label,"goto __FR%x_J;",syntax.curly[pos].index);
  1342. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1343. parse_line(label);
  1344. syntax.curly_count--;
  1345. // ループ開始のラベル付け
  1346. sprintf(label,"__FR%x_BGN",syntax.curly[pos].index);
  1347. l=add_str(label);
  1348. set_label(l,script_pos,p);
  1349. return p;
  1350. }
  1351. else if( p2 - p == 8 && strncasecmp(p,"function",8) == 0 )
  1352. {// internal script function
  1353. const char *func_name;
  1354. func_name = skip_space(p2);
  1355. p = skip_word(func_name);
  1356. if( p == func_name )
  1357. disp_error_message("parse_syntax:function: function name is missing or invalid", p);
  1358. if( *skip_space(p) == ';' )
  1359. {// function <name> ;
  1360. // function declaration - just register the name
  1361. int l;
  1362. l = add_word(func_name);
  1363. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1364. str_data[l].type = C_USERFUNC;
  1365. return skip_space(p) + 1;
  1366. }
  1367. else
  1368. {// function <name> <line/block of code>
  1369. char label[256];
  1370. int l;
  1371. syntax.curly[syntax.curly_count].type = TYPE_USERFUNC;
  1372. syntax.curly[syntax.curly_count].count = 1;
  1373. syntax.curly[syntax.curly_count].index = syntax.index++;
  1374. syntax.curly[syntax.curly_count].flag = 0;
  1375. ++syntax.curly_count;
  1376. // Jump over the function code
  1377. sprintf(label, "goto __FN%x_FIN;", syntax.curly[syntax.curly_count-1].index);
  1378. syntax.curly[syntax.curly_count].type = TYPE_NULL;
  1379. ++syntax.curly_count;
  1380. parse_line(label);
  1381. --syntax.curly_count;
  1382. // Set the position of the function (label)
  1383. l=add_word(func_name);
  1384. if( str_data[l].type == C_NOP )//## ??? [FlavioJS]
  1385. str_data[l].type = C_USERFUNC;
  1386. set_label(l, script_pos, p);
  1387. if( parse_options&SCRIPT_USE_LABEL_DB )
  1388. strdb_put(scriptlabel_db, GETSTRING(str_data[l].str), (void*)script_pos);
  1389. return skip_space(p);
  1390. }
  1391. }
  1392. break;
  1393. case 'i':
  1394. case 'I':
  1395. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1396. // if() の処理
  1397. char label[256];
  1398. p=skip_space(p2);
  1399. if(*p != '(') { //Prevent if this {} non-c syntax. from Rayce (jA)
  1400. disp_error_message("need '('",p);
  1401. }
  1402. syntax.curly[syntax.curly_count].type = TYPE_IF;
  1403. syntax.curly[syntax.curly_count].count = 1;
  1404. syntax.curly[syntax.curly_count].index = syntax.index++;
  1405. syntax.curly[syntax.curly_count].flag = 0;
  1406. sprintf(label,"__IF%x_%x",syntax.curly[syntax.curly_count].index,syntax.curly[syntax.curly_count].count);
  1407. syntax.curly_count++;
  1408. add_scriptl(add_str("jump_zero"));
  1409. add_scriptc(C_ARG);
  1410. p=parse_expr(p);
  1411. p=skip_space(p);
  1412. add_scriptl(add_str(label));
  1413. add_scriptc(C_FUNC);
  1414. return p;
  1415. }
  1416. break;
  1417. case 's':
  1418. case 'S':
  1419. if(p2 - p == 6 && !strncasecmp(p,"switch",6)) {
  1420. // switch() の処理
  1421. char label[256];
  1422. p=skip_space(p2);
  1423. if(*p != '(') {
  1424. disp_error_message("need '('",p);
  1425. }
  1426. syntax.curly[syntax.curly_count].type = TYPE_SWITCH;
  1427. syntax.curly[syntax.curly_count].count = 1;
  1428. syntax.curly[syntax.curly_count].index = syntax.index++;
  1429. syntax.curly[syntax.curly_count].flag = 0;
  1430. sprintf(label,"$@__SW%x_VAL",syntax.curly[syntax.curly_count].index);
  1431. syntax.curly_count++;
  1432. add_scriptl(add_str("set"));
  1433. add_scriptc(C_ARG);
  1434. add_scriptl(add_str(label));
  1435. p=parse_expr(p);
  1436. p=skip_space(p);
  1437. if(*p != '{') {
  1438. disp_error_message("parse_syntax: need '{'",p);
  1439. }
  1440. add_scriptc(C_FUNC);
  1441. return p + 1;
  1442. }
  1443. break;
  1444. case 'w':
  1445. case 'W':
  1446. if(p2 - p == 5 && !strncasecmp(p,"while",5)) {
  1447. int l;
  1448. char label[256];
  1449. p=skip_space(p2);
  1450. if(*p != '(') {
  1451. disp_error_message("need '('",p);
  1452. }
  1453. syntax.curly[syntax.curly_count].type = TYPE_WHILE;
  1454. syntax.curly[syntax.curly_count].count = 1;
  1455. syntax.curly[syntax.curly_count].index = syntax.index++;
  1456. syntax.curly[syntax.curly_count].flag = 0;
  1457. // 条件判断開始のラベル形成する
  1458. sprintf(label,"__WL%x_NXT",syntax.curly[syntax.curly_count].index);
  1459. l=add_str(label);
  1460. set_label(l,script_pos,p);
  1461. // 条件が偽なら終了地点に飛ばす
  1462. sprintf(label,"__WL%x_FIN",syntax.curly[syntax.curly_count].index);
  1463. syntax.curly_count++;
  1464. add_scriptl(add_str("jump_zero"));
  1465. add_scriptc(C_ARG);
  1466. p=parse_expr(p);
  1467. p=skip_space(p);
  1468. add_scriptl(add_str(label));
  1469. add_scriptc(C_FUNC);
  1470. return p;
  1471. }
  1472. break;
  1473. }
  1474. return NULL;
  1475. }
  1476. const char* parse_syntax_close(const char *p) {
  1477. // if(...) for(...) hoge(); のように、1度閉じられたら再度閉じられるか確認する
  1478. int flag;
  1479. do {
  1480. p = parse_syntax_close_sub(p,&flag);
  1481. } while(flag);
  1482. return p;
  1483. }
  1484. // if, for , while , do の閉じ判定
  1485. // flag == 1 : 閉じられた
  1486. // flag == 0 : 閉じられない
  1487. const char* parse_syntax_close_sub(const char* p,int* flag)
  1488. {
  1489. char label[256];
  1490. int pos = syntax.curly_count - 1;
  1491. int l;
  1492. *flag = 1;
  1493. if(syntax.curly_count <= 0) {
  1494. *flag = 0;
  1495. return p;
  1496. } else if(syntax.curly[pos].type == TYPE_IF) {
  1497. const char *bp = p;
  1498. const char *p2;
  1499. // if 最終場所へ飛ばす
  1500. sprintf(label,"goto __IF%x_FIN;",syntax.curly[pos].index);
  1501. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1502. parse_line(label);
  1503. syntax.curly_count--;
  1504. // 現在地のラベルを付ける
  1505. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1506. l=add_str(label);
  1507. set_label(l,script_pos,p);
  1508. syntax.curly[pos].count++;
  1509. p = skip_space(p);
  1510. p2 = skip_word(p);
  1511. if(!syntax.curly[pos].flag && p2 - p == 4 && !strncasecmp(p,"else",4)) {
  1512. // else or else - if
  1513. p = skip_space(p2);
  1514. p2 = skip_word(p);
  1515. if(p2 - p == 2 && !strncasecmp(p,"if",2)) {
  1516. // else - if
  1517. p=skip_space(p2);
  1518. if(*p != '(') {
  1519. disp_error_message("need '('",p);
  1520. }
  1521. sprintf(label,"__IF%x_%x",syntax.curly[pos].index,syntax.curly[pos].count);
  1522. add_scriptl(add_str("jump_zero"));
  1523. add_scriptc(C_ARG);
  1524. p=parse_expr(p);
  1525. p=skip_space(p);
  1526. add_scriptl(add_str(label));
  1527. add_scriptc(C_FUNC);
  1528. *flag = 0;
  1529. return p;
  1530. } else {
  1531. // else
  1532. if(!syntax.curly[pos].flag) {
  1533. syntax.curly[pos].flag = 1;
  1534. *flag = 0;
  1535. return p;
  1536. }
  1537. }
  1538. }
  1539. // if 閉じ
  1540. syntax.curly_count--;
  1541. // 最終地のラベルを付ける
  1542. sprintf(label,"__IF%x_FIN",syntax.curly[pos].index);
  1543. l=add_str(label);
  1544. set_label(l,script_pos,p);
  1545. if(syntax.curly[pos].flag == 1) {
  1546. // このifに対するelseじゃないのでポインタの位置は同じ
  1547. return bp;
  1548. }
  1549. return p;
  1550. } else if(syntax.curly[pos].type == TYPE_DO) {
  1551. int l;
  1552. char label[256];
  1553. const char *p2;
  1554. if(syntax.curly[pos].flag) {
  1555. // 現在地のラベル形成する(continue でここに来る)
  1556. sprintf(label,"__DO%x_NXT",syntax.curly[pos].index);
  1557. l=add_str(label);
  1558. set_label(l,script_pos,p);
  1559. }
  1560. // 条件が偽なら終了地点に飛ばす
  1561. p = skip_space(p);
  1562. p2 = skip_word(p);
  1563. if(p2 - p != 5 || strncasecmp(p,"while",5))
  1564. disp_error_message("parse_syntax: need 'while'",p);
  1565. p = skip_space(p2);
  1566. if(*p != '(') {
  1567. disp_error_message("need '('",p);
  1568. }
  1569. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1570. add_scriptl(add_str("jump_zero"));
  1571. add_scriptc(C_ARG);
  1572. p=parse_expr(p);
  1573. p=skip_space(p);
  1574. add_scriptl(add_str(label));
  1575. add_scriptc(C_FUNC);
  1576. // 開始地点に飛ばす
  1577. sprintf(label,"goto __DO%x_BGN;",syntax.curly[pos].index);
  1578. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1579. parse_line(label);
  1580. syntax.curly_count--;
  1581. // 条件終了地点のラベル形成する
  1582. sprintf(label,"__DO%x_FIN",syntax.curly[pos].index);
  1583. l=add_str(label);
  1584. set_label(l,script_pos,p);
  1585. p = skip_space(p);
  1586. if(*p != ';') {
  1587. disp_error_message("parse_syntax: need ';'",p);
  1588. return p+1;
  1589. }
  1590. p++;
  1591. syntax.curly_count--;
  1592. return p;
  1593. } else if(syntax.curly[pos].type == TYPE_FOR) {
  1594. // 次のループに飛ばす
  1595. sprintf(label,"goto __FR%x_NXT;",syntax.curly[pos].index);
  1596. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1597. parse_line(label);
  1598. syntax.curly_count--;
  1599. // for 終了のラベル付け
  1600. sprintf(label,"__FR%x_FIN",syntax.curly[pos].index);
  1601. l=add_str(label);
  1602. set_label(l,script_pos,p);
  1603. syntax.curly_count--;
  1604. return p;
  1605. } else if(syntax.curly[pos].type == TYPE_WHILE) {
  1606. // while 条件判断へ飛ばす
  1607. sprintf(label,"goto __WL%x_NXT;",syntax.curly[pos].index);
  1608. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1609. parse_line(label);
  1610. syntax.curly_count--;
  1611. // while 終了のラベル付け
  1612. sprintf(label,"__WL%x_FIN",syntax.curly[pos].index);
  1613. l=add_str(label);
  1614. set_label(l,script_pos,p);
  1615. syntax.curly_count--;
  1616. return p;
  1617. } else if(syntax.curly[syntax.curly_count-1].type == TYPE_USERFUNC) {
  1618. int pos = syntax.curly_count-1;
  1619. char label[256];
  1620. int l;
  1621. // 戻す
  1622. sprintf(label,"return;");
  1623. syntax.curly[syntax.curly_count++].type = TYPE_NULL;
  1624. parse_line(label);
  1625. syntax.curly_count--;
  1626. // 現在地のラベルを付ける
  1627. sprintf(label,"__FN%x_FIN",syntax.curly[pos].index);
  1628. l=add_str(label);
  1629. set_label(l,script_pos,p);
  1630. syntax.curly_count--;
  1631. return p + 1;
  1632. } else {
  1633. *flag = 0;
  1634. return p;
  1635. }
  1636. }
  1637. /*==========================================
  1638. * 組み込み関数の追加
  1639. *------------------------------------------*/
  1640. static void add_buildin_func(void)
  1641. {
  1642. int i,n;
  1643. const char* p;
  1644. for( i=0; buildin_func[i].func; i++ ){
  1645. // arg must follow the pattern: (v|s|i|r|l)*\?*\*?
  1646. // 'v' - value (either string or int or reference)
  1647. // 's' - string
  1648. // 'i' - int
  1649. // 'r' - reference (of a variable)
  1650. // 'l' - label
  1651. // '?' - one optional parameter
  1652. // '*' - unknown number of optional parameters
  1653. p=buildin_func[i].arg;
  1654. while( *p == 'v' || *p == 's' || *p == 'i' || *p == 'r' || *p == 'l' ) ++p;
  1655. while( *p == '?' ) ++p;
  1656. if( *p == '*' ) ++p;
  1657. if( *p != 0){
  1658. ShowWarning("add_buildin_func: ignoring function \"%s\" with invalid arg \"%s\".\n", buildin_func[i].name, buildin_func[i].arg);
  1659. } else if( *skip_word(buildin_func[i].name) != 0 ){
  1660. ShowWarning("add_buildin_func: ignoring function with invalid name \"%s\" (must be a word).\n", buildin_func[i].name);
  1661. } else {
  1662. n=add_str(buildin_func[i].name);
  1663. str_data[n].type=C_FUNC;
  1664. str_data[n].val=i;
  1665. str_data[n].func=buildin_func[i].func;
  1666. }
  1667. }
  1668. }
  1669. /*==========================================
  1670. * 定数データベースの読み込み
  1671. *------------------------------------------*/
  1672. static void read_constdb(void)
  1673. {
  1674. FILE *fp;
  1675. char line[1024],name[1024],val[1024];
  1676. int n,type;
  1677. sprintf(line, "%s/const.txt", db_path);
  1678. fp=fopen(line, "r");
  1679. if(fp==NULL){
  1680. ShowError("can't read %s\n", line);
  1681. return ;
  1682. }
  1683. while(fgets(line, sizeof(line), fp))
  1684. {
  1685. if(line[0]=='/' && line[1]=='/')
  1686. continue;
  1687. type=0;
  1688. if(sscanf(line,"%[A-Za-z0-9_],%[-0-9xXA-Fa-f],%d",name,val,&type)>=2 ||
  1689. sscanf(line,"%[A-Za-z0-9_] %[-0-9xXA-Fa-f] %d",name,val,&type)>=2){
  1690. n=add_str(name);
  1691. if(type==0)
  1692. str_data[n].type=C_INT;
  1693. else
  1694. str_data[n].type=C_PARAM;
  1695. str_data[n].val= (int)strtol(val,NULL,0);
  1696. }
  1697. }
  1698. fclose(fp);
  1699. }
  1700. /*==========================================
  1701. * エラー表示
  1702. *------------------------------------------*/
  1703. const char* script_print_line( const char *p, const char *mark, int line )
  1704. {
  1705. int i;
  1706. if( p == NULL || !p[0] ) return NULL;
  1707. if( line < 0 )
  1708. printf("*% 5d : ", -line);
  1709. else
  1710. printf(" % 5d : ", line);
  1711. for(i=0;p[i] && p[i] != '\n';i++){
  1712. if(p + i != mark)
  1713. printf("%c",p[i]);
  1714. else
  1715. printf("\'%c\'",p[i]);
  1716. }
  1717. printf("\n");
  1718. return p+i+(p[i] == '\n' ? 1 : 0);
  1719. }
  1720. void script_error(const char *src,const char *file,int start_line, const char *error_msg, const char *error_pos)
  1721. {
  1722. // エラーが発生した行を求める
  1723. int j;
  1724. int line = start_line;
  1725. const char *p;
  1726. const char *linestart[5] = { NULL, NULL, NULL, NULL, NULL };
  1727. for(p=src;p && *p;line++){
  1728. char *lineend=strchr(p,'\n');
  1729. if(lineend==NULL || error_pos<lineend){
  1730. break;
  1731. }
  1732. for( j = 0; j < 4; j++ ) {
  1733. linestart[j] = linestart[j+1];
  1734. }
  1735. linestart[4] = p;
  1736. p=lineend+1;
  1737. }
  1738. printf("\a\n");
  1739. printf("script error on %s line %d\n", file, line);
  1740. printf(" %s\n", error_msg);
  1741. for(j = 0; j < 5; j++ ) {
  1742. script_print_line( linestart[j], NULL, line + j - 5);
  1743. }
  1744. p = script_print_line( p, error_pos, -line);
  1745. for(j = 0; j < 5; j++) {
  1746. p = script_print_line( p, NULL, line + j + 1 );
  1747. }
  1748. }
  1749. /*==========================================
  1750. * スクリプトの解析
  1751. *------------------------------------------*/
  1752. struct script_code* parse_script(const char *src,const char *file,int line,int options)
  1753. {
  1754. const char *p,*tmpp;
  1755. int i;
  1756. struct script_code *code;
  1757. static int first=1;
  1758. memset(&syntax,0,sizeof(syntax));
  1759. if(first){
  1760. add_buildin_func();
  1761. read_constdb();
  1762. }
  1763. first=0;
  1764. script_buf=(unsigned char *)aMalloc(SCRIPT_BLOCK_SIZE*sizeof(unsigned char));
  1765. script_pos=0;
  1766. script_size=SCRIPT_BLOCK_SIZE;
  1767. str_data[LABEL_NEXTLINE].type=C_NOP;
  1768. str_data[LABEL_NEXTLINE].backpatch=-1;
  1769. str_data[LABEL_NEXTLINE].label=-1;
  1770. for(i=LABEL_START;i<str_num;i++){
  1771. if(
  1772. str_data[i].type==C_POS || str_data[i].type==C_NAME ||
  1773. str_data[i].type==C_USERFUNC || str_data[i].type == C_USERFUNC_POS
  1774. ){
  1775. str_data[i].type=C_NOP;
  1776. str_data[i].backpatch=-1;
  1777. str_data[i].label=-1;
  1778. }
  1779. }
  1780. // who called parse_script is responsible for clearing the database after using it, but just in case... lets clear it here
  1781. if( options&SCRIPT_USE_LABEL_DB )
  1782. scriptlabel_db->clear(scriptlabel_db, NULL);
  1783. parse_options = options;
  1784. if( setjmp( error_jump ) != 0 ) {
  1785. //Restore program state when script has problems. [from jA]
  1786. int i;
  1787. const int size = sizeof(syntax.curly)/sizeof(syntax.curly[0]);
  1788. if( error_report )
  1789. script_error(src,file,line,error_msg,error_pos);
  1790. aFree( error_msg );
  1791. aFree( script_buf );
  1792. script_pos = 0;
  1793. script_size = 0;
  1794. script_buf = NULL;
  1795. for(i=LABEL_START;i<str_num;i++)
  1796. if(str_data[i].type == C_NOP) str_data[i].type = C_NAME;
  1797. for(i=0; i<size; i++)
  1798. linkdb_final(&syntax.curly[i].case_label);
  1799. return NULL;
  1800. }
  1801. parse_syntax_for_flag=0;
  1802. p=src;
  1803. p=skip_space(p);
  1804. if(*p!='{'){
  1805. disp_error_message("not found '{'",p);
  1806. }
  1807. p++;
  1808. p = skip_space(p);
  1809. if (p && *p == '}') {
  1810. // an empty function, just return
  1811. aFree( script_buf );
  1812. script_pos = 0;
  1813. script_size = 0;
  1814. script_buf = NULL;
  1815. return NULL;
  1816. }
  1817. while (p && *p && (*p!='}' || syntax.curly_count != 0)) {
  1818. p=skip_space(p);
  1819. // labelだけ特殊処理
  1820. tmpp=skip_space(skip_word(p));
  1821. if(*tmpp==':' && !(!strncasecmp(p,"default:",8) && p + 7 == tmpp)){
  1822. i=add_word(p);
  1823. set_label(i,script_pos,p);
  1824. if( parse_options&SCRIPT_USE_LABEL_DB )
  1825. strdb_put(scriptlabel_db, GETSTRING(str_data[i].str), (void*)script_pos);
  1826. p=tmpp+1;
  1827. continue;
  1828. }
  1829. // 他は全部一緒くた
  1830. p=parse_line(p);
  1831. p=skip_space(p);
  1832. add_scriptc(C_EOL);
  1833. set_label(LABEL_NEXTLINE,script_pos,p);
  1834. str_data[LABEL_NEXTLINE].type=C_NOP;
  1835. str_data[LABEL_NEXTLINE].backpatch=-1;
  1836. str_data[LABEL_NEXTLINE].label=-1;
  1837. }
  1838. add_scriptc(C_NOP);
  1839. script_size = script_pos;
  1840. RECREATE(script_buf,unsigned char,script_pos);
  1841. // 未解決のラベルを解決
  1842. for(i=LABEL_START;i<str_num;i++){
  1843. if(str_data[i].type==C_NOP){
  1844. int j,next;
  1845. str_data[i].type=C_NAME;
  1846. str_data[i].label=i;
  1847. for(j=str_data[i].backpatch;j>=0 && j!=0x00ffffff;){
  1848. next=GETVALUE(script_buf,j);
  1849. SETVALUE(script_buf,j,i);
  1850. j=next;
  1851. }
  1852. }
  1853. }
  1854. #ifdef DEBUG_DISP
  1855. for(i=0;i<script_pos;i++){
  1856. if((i&15)==0) printf("%04x : ",i);
  1857. printf("%02x ",script_buf[i]);
  1858. if((i&15)==15) printf("\n");
  1859. }
  1860. printf("\n");
  1861. #endif
  1862. #ifdef DEBUG_DISASM
  1863. {
  1864. int i = 0,j;
  1865. while(i < script_pos) {
  1866. printf("%06x ",i);
  1867. j = i;
  1868. switch(get_com(script_buf,&i)) {
  1869. case C_EOL: printf("C_EOL"); break;
  1870. case C_INT: printf("C_INT %d",get_num(script_buf,&i)); break;
  1871. case C_POS:
  1872. printf("C_POS 0x%06x",*(int*)(script_buf+i)&0xffffff);
  1873. i += 3;
  1874. break;
  1875. case C_NAME:
  1876. j = (*(int*)(script_buf+i)&0xffffff);
  1877. printf("C_NAME %s",j == 0xffffff ? "?? unknown ??" : str_buf + str_data[j].str);
  1878. i += 3;
  1879. break;
  1880. case C_ARG: printf("C_ARG"); break;
  1881. case C_FUNC: printf("C_FUNC"); break;
  1882. case C_ADD: printf("C_ADD"); break;
  1883. case C_SUB: printf("C_SUB"); break;
  1884. case C_MUL: printf("C_MUL"); break;
  1885. case C_DIV: printf("C_DIV"); break;
  1886. case C_MOD: printf("C_MOD"); break;
  1887. case C_EQ: printf("C_EQ"); break;
  1888. case C_NE: printf("C_NE"); break;
  1889. case C_GT: printf("C_GT"); break;
  1890. case C_GE: printf("C_GE"); break;
  1891. case C_LT: printf("C_LT"); break;
  1892. case C_LE: printf("C_LE"); break;
  1893. case C_AND: printf("C_AND"); break;
  1894. case C_OR: printf("C_OR"); break;
  1895. case C_XOR: printf("C_XOR"); break;
  1896. case C_LAND: printf("C_LAND"); break;
  1897. case C_LOR: printf("C_LOR"); break;
  1898. case C_R_SHIFT: printf("C_R_SHIFT"); break;
  1899. case C_L_SHIFT: printf("C_L_SHIFT"); break;
  1900. case C_NEG: printf("C_NEG"); break;
  1901. case C_NOT: printf("C_NOT"); break;
  1902. case C_LNOT: printf("C_LNOT"); break;
  1903. case C_NOP: printf("C_NOP"); break;
  1904. case C_OP3: printf("C_OP3"); break;
  1905. case C_STR:
  1906. j = strlen(script_buf + i);
  1907. printf("C_STR %s",script_buf + i);
  1908. i+= j+1;
  1909. break;
  1910. default:
  1911. printf("unknown");
  1912. }
  1913. printf("\n");
  1914. }
  1915. }
  1916. #endif
  1917. CREATE(code,struct script_code,1);
  1918. code->script_buf = script_buf;
  1919. code->script_size = script_size;
  1920. code->script_vars = NULL;
  1921. return code;
  1922. }
  1923. //
  1924. // Script state
  1925. //
  1926. enum {RUN = 0,STOP,END,RERUNLINE,GOTO,RETFUNC};
  1927. /// Returns the player attached to this script, identified by the rid.
  1928. /// If there is no player attached, the script is terminated.
  1929. TBL_PC *script_rid2sd(struct script_state *st)
  1930. {
  1931. TBL_PC *sd=map_id2sd(st->rid);
  1932. if(!sd){
  1933. ShowError("script_rid2sd: fatal error ! player not attached!\n");
  1934. st->state = END;
  1935. }
  1936. return sd;
  1937. }
  1938. /// Retrieves the value of a script data
  1939. int get_val(struct script_state* st, struct script_data* data)
  1940. {
  1941. char* name;
  1942. char prefix;
  1943. char postfix;
  1944. TBL_PC* sd = NULL;
  1945. if( !data_isreference(data) )
  1946. return 0;// not a variable
  1947. name = reference_getname(data);
  1948. prefix = name[0];
  1949. postfix = name[strlen(name) - 1];
  1950. //##TODO use reference_tovariable(data) when it's confirmed that it works [FlavioJS]
  1951. if( !reference_toconstant(data) && not_server_variable(prefix) )
  1952. {
  1953. sd = script_rid2sd(st);
  1954. if( sd == NULL )
  1955. {// needs player attached
  1956. if( postfix == '$' )
  1957. {// string variable
  1958. ShowError("script:get_val: cannot access player variable '%s', defaulting to \"\"\n", name);
  1959. data->type = C_CONSTSTR;
  1960. data->u.str = "";
  1961. }
  1962. else
  1963. {// integer variable
  1964. ShowError("script:get_val: cannot access player variable '%s', defaulting to 0\n", name);
  1965. data->type = C_INT;
  1966. data->u.num = 0;
  1967. }
  1968. return 0;
  1969. }
  1970. }
  1971. if( postfix == '$' )
  1972. {// string variable
  1973. data->type = C_CONSTSTR;
  1974. switch( prefix )
  1975. {
  1976. case '@':
  1977. data->u.str = pc_readregstr(sd, data->u.num);
  1978. break;
  1979. case '$':
  1980. data->u.str = (char *)idb_get(mapregstr_db, data->u.num);
  1981. break;
  1982. case '#':
  1983. if( name[1] == '#' )
  1984. data->u.str = pc_readaccountreg2str(sd, name);// global
  1985. else
  1986. data->u.str = pc_readaccountregstr(sd, name);// local
  1987. break;
  1988. case '.':
  1989. {
  1990. struct linkdb_node** n =
  1991. data->ref ? data->ref:
  1992. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  1993. &st->script->script_vars;// npc variable
  1994. data->u.str = linkdb_search(n, (void*)reference_getuid(data));
  1995. }
  1996. break;
  1997. default:
  1998. data->u.str = pc_readglobalreg_str(sd, name);
  1999. break;
  2000. }
  2001. if( data->u.str == NULL )
  2002. data->u.str = "";
  2003. }
  2004. else
  2005. {// integer variable
  2006. data->type = C_INT;
  2007. if( reference_toconstant(data) )
  2008. {
  2009. data->u.num = reference_getconstant(data);
  2010. }
  2011. else if( reference_toparam(data) )
  2012. {
  2013. data->u.num = pc_readparam(sd, reference_getparamtype(data));
  2014. }
  2015. else
  2016. switch( prefix )
  2017. {
  2018. case '@':
  2019. data->u.num = pc_readreg(sd, data->u.num);
  2020. break;
  2021. case '$':
  2022. data->u.num = (int)idb_get(mapreg_db, data->u.num);
  2023. break;
  2024. case '#':
  2025. if( name[1] == '#' )
  2026. data->u.num = pc_readaccountreg2(sd, name);// global
  2027. else
  2028. data->u.num = pc_readaccountreg(sd, name);// local
  2029. break;
  2030. case '.':
  2031. {
  2032. struct linkdb_node** n =
  2033. data->ref ? data->ref:
  2034. name[1] == '@' ? st->stack->var_function:// instance/scope variable
  2035. &st->script->script_vars;// npc variable
  2036. data->u.num = (int)linkdb_search(n, (void*)reference_getuid(data));
  2037. }
  2038. break;
  2039. default:
  2040. data->u.num = pc_readglobalreg(sd, name);
  2041. break;
  2042. }
  2043. }
  2044. return 0;
  2045. }
  2046. /// Retrieves the value of a reference identified by uid (variable, constant, param)
  2047. void* get_val2(struct script_state* st, int uid, struct linkdb_node** ref)
  2048. {
  2049. struct script_data data;
  2050. data.type = C_NAME;
  2051. data.u.num = uid;
  2052. data.ref = ref;
  2053. get_val(st, &data);
  2054. return (data.type == C_INT ? (void*)data.u.num : (void*)data.u.str);
  2055. }
  2056. /*==========================================
  2057. * Stores the value of a script variable
  2058. * Return value is 0 on fail, 1 on success.
  2059. *------------------------------------------*/
  2060. static int set_reg(struct script_state* st, TBL_PC* sd, int num, char* name, void* value, struct linkdb_node** ref)
  2061. {
  2062. char prefix = name[0]; char postfix = name[strlen(name)-1];
  2063. if (postfix == '$') { // string variable
  2064. char* str = (char*)value;
  2065. switch (prefix) {
  2066. case '@':
  2067. return pc_setregstr(sd, num, str);
  2068. case '$':
  2069. return mapreg_setregstr(num, str);
  2070. case '#':
  2071. return (name[1] == '#') ?
  2072. pc_setaccountreg2str(sd, name, str) :
  2073. pc_setaccountregstr(sd, name, str);
  2074. case '.': {
  2075. char* p;
  2076. struct linkdb_node** n;
  2077. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2078. p = linkdb_search(n, (void*)num);
  2079. if (p) {
  2080. linkdb_erase(n, (void*)num);
  2081. aFree(p);
  2082. }
  2083. if (str[0]) linkdb_insert(n, (void*)num, aStrdup(str));
  2084. }
  2085. return 1;
  2086. default:
  2087. return pc_setglobalreg_str(sd, name, str);
  2088. }
  2089. } else { // integer variable
  2090. int val = (int)value;
  2091. if(str_data[num&0x00ffffff].type == C_PARAM)
  2092. return pc_setparam(sd, str_data[num&0x00ffffff].val, val);
  2093. switch (prefix) {
  2094. case '@':
  2095. return pc_setreg(sd, num, val);
  2096. case '$':
  2097. return mapreg_setreg(num, val);
  2098. case '#':
  2099. return (name[1] == '#') ?
  2100. pc_setaccountreg2(sd, name, val) :
  2101. pc_setaccountreg(sd, name, val);
  2102. case '.': {
  2103. struct linkdb_node** n;
  2104. n = (ref) ? ref : (name[1] == '@') ? st->stack->var_function : &st->script->script_vars;
  2105. if (val == 0)
  2106. linkdb_erase(n, (void*)num);
  2107. else
  2108. linkdb_replace(n, (void*)num, (void*)val);
  2109. }
  2110. return 1;
  2111. default:
  2112. return pc_setglobalreg(sd, name, val);
  2113. }
  2114. }
  2115. }
  2116. int set_var(TBL_PC* sd, char* name, void* val)
  2117. {
  2118. return set_reg(NULL, sd, add_str(name), name, val, NULL);
  2119. }
  2120. /// Converts the data to a string
  2121. const char* conv_str(struct script_state* st, struct script_data* data)
  2122. {
  2123. char* p;
  2124. get_val(st, data);
  2125. if( data_isstring(data) )
  2126. {// nothing to convert
  2127. }
  2128. else if( data_isint(data) )
  2129. {// int -> string
  2130. CREATE(p, char, ITEM_NAME_LENGTH);
  2131. snprintf(p, ITEM_NAME_LENGTH, "%d", data->u.num);
  2132. p[ITEM_NAME_LENGTH-1] = '\0';
  2133. data->type = C_STR;
  2134. data->u.str = p;
  2135. }
  2136. else if( data_isreference(data) )
  2137. {// reference -> string
  2138. //##TODO when does this happen (check get_val) [FlavioJS]
  2139. data->type = C_CONSTSTR;
  2140. data->u.str = reference_getname(data);
  2141. }
  2142. else
  2143. {// unsupported data type
  2144. ShowError("script:conv_str: cannot convert to string, defaulting to \"\"\n");
  2145. script_reportdata(data);
  2146. script_reportsrc(st);
  2147. data->type = C_CONSTSTR;
  2148. data->u.str = "";
  2149. }
  2150. return data->u.str;
  2151. }
  2152. /// Converts the data to an int
  2153. int conv_num(struct script_state* st, struct script_data* data)
  2154. {
  2155. char* p;
  2156. long num;
  2157. get_val(st, data);
  2158. if( data_isint(data) )
  2159. {// nothing to convert
  2160. }
  2161. else if( data_isstring(data) )
  2162. {// string -> int
  2163. // the result does not overflow or underflow, it is capped instead
  2164. // ex: 999999999999 is capped to INT_MAX (2147483647)
  2165. p = data->u.str;
  2166. errno = 0;
  2167. num = strtol(data->u.str, NULL, 10);// change radix to 0 to support octal numbers "o377" and hex numbers "0xFF"
  2168. if( errno == ERANGE
  2169. #if LONG_MAX > INT_MAX
  2170. || num < INT_MIN || num > INT_MAX
  2171. #endif
  2172. )
  2173. {
  2174. if( num <= INT_MIN )
  2175. {
  2176. num = INT_MIN;
  2177. ShowError("script:conv_num: underflow detected, capping to %ld\n", num);
  2178. }
  2179. else//if( num >= INT_MAX )
  2180. {
  2181. num = INT_MAX;
  2182. ShowError("script:conv_num: overflow detected, capping to %ld\n", num);
  2183. }
  2184. script_reportdata(data);
  2185. script_reportsrc(st);
  2186. }
  2187. if( data->type == C_STR )
  2188. aFree(p);
  2189. data->type = C_INT;
  2190. data->u.num = (int)num;
  2191. }
  2192. #if 0
  2193. // FIXME this function is being used to retrieve the position of labels and
  2194. // probably other stuff [FlavioJS]
  2195. else
  2196. {// unsupported data type
  2197. ShowError("script:conv_num: cannot convert to number, defaulting to 0\n");
  2198. script_reportdata(data);
  2199. script_reportsrc(st);
  2200. data->type = C_INT;
  2201. data->u.num = 0;
  2202. }
  2203. #endif
  2204. return data->u.num;
  2205. }
  2206. //
  2207. // Stack operations
  2208. //
  2209. /// Increases the size of the stack
  2210. void stack_expand(struct script_stack* stack)
  2211. {
  2212. stack->sp_max += 64;
  2213. stack->stack_data = (struct script_data*)aRealloc(stack->stack_data,
  2214. stack->sp_max * sizeof(stack->stack_data[0]) );
  2215. memset(stack->stack_data + (stack->sp_max - 64), 0,
  2216. 64 * sizeof(stack->stack_data[0]) );
  2217. }
  2218. /// Pushes a value into the stack
  2219. #define push_val(stack,type,val) push_val2(stack, type, val, NULL)
  2220. /// Pushes a value into the stack (with reference)
  2221. void push_val2(struct script_stack* stack, int type, int val, struct linkdb_node** ref)
  2222. {
  2223. if( stack->sp >= stack->sp_max )
  2224. stack_expand(stack);
  2225. stack->stack_data[stack->sp].type = type;
  2226. stack->stack_data[stack->sp].u.num = val;
  2227. stack->stack_data[stack->sp].ref = ref;
  2228. stack->sp++;
  2229. }
  2230. /// Pushes a string into the stack
  2231. void push_str(struct script_stack* stack, int type, char* str)
  2232. {
  2233. if( stack->sp >= stack->sp_max )
  2234. stack_expand(stack);
  2235. stack->stack_data[stack->sp].type = type;
  2236. stack->stack_data[stack->sp].u.str = str;
  2237. stack->stack_data[stack->sp].ref = NULL;
  2238. stack->sp++;
  2239. }
  2240. /// Pushes a copy of the target position into the stack
  2241. void push_copy(struct script_stack* stack, int pos)
  2242. {
  2243. switch( stack->stack_data[pos].type )
  2244. {
  2245. case C_CONSTSTR:
  2246. push_str(stack, C_CONSTSTR, stack->stack_data[pos].u.str);
  2247. break;
  2248. case C_STR:
  2249. push_str(stack, C_STR, aStrdup(stack->stack_data[pos].u.str));
  2250. break;
  2251. default:
  2252. push_val2(
  2253. stack,stack->stack_data[pos].type,
  2254. stack->stack_data[pos].u.num,
  2255. stack->stack_data[pos].ref
  2256. );
  2257. break;
  2258. }
  2259. }
  2260. /// Removes the values in indexes [start,end[ from the stack
  2261. void pop_stack(struct script_stack* stack, int start, int end)
  2262. {
  2263. struct script_data* data;
  2264. int i;
  2265. if( start < 0 )
  2266. start = 0;
  2267. if( end > stack->sp_max )
  2268. end = stack->sp_max;
  2269. if( start >= end )
  2270. return;// nothing to pop
  2271. // free stack elements
  2272. for( i = start; i < end; i++ )
  2273. {
  2274. data = &stack->stack_data[i];
  2275. if( data->type == C_STR )
  2276. aFree(data->u.str);
  2277. data->type = C_NOP;
  2278. }
  2279. // move the rest of the elements
  2280. if( stack->sp > end )
  2281. memmove(&stack->stack_data[start], &stack->stack_data[end], sizeof(stack->stack_data[0])*(stack->sp - end));
  2282. stack->sp -= end - start;
  2283. }
  2284. ///
  2285. ///
  2286. ///
  2287. /*==========================================
  2288. * スクリプト依存変数、関数依存変数の解放
  2289. *------------------------------------------*/
  2290. void script_free_vars(struct linkdb_node **node)
  2291. {
  2292. struct linkdb_node *n = *node;
  2293. while(n) {
  2294. char *name = str_buf + str_data[(int)(n->key)&0x00ffffff].str;
  2295. char postfix = name[strlen(name)-1];
  2296. if( postfix == '$' ) {
  2297. // 文字型変数なので、データ削除
  2298. aFree(n->data);
  2299. }
  2300. n = n->next;
  2301. }
  2302. linkdb_final( node );
  2303. }
  2304. /*==========================================
  2305. * Free's the whole stack. Invoked when clearing a character. [Skotlex]
  2306. *------------------------------------------*/
  2307. void script_free_stack(struct script_stack *stack)
  2308. {
  2309. int i;
  2310. for(i = 0; i < stack->sp; i++) {
  2311. if( stack->stack_data[i].type == C_STR ) {
  2312. aFree(stack->stack_data[i].u.str);
  2313. stack->stack_data[i].type = C_INT;
  2314. } else if( i > 0 && stack->stack_data[i].type == C_RETINFO ) {
  2315. struct linkdb_node** n = (struct linkdb_node**)stack->stack_data[i-1].u.num;
  2316. script_free_vars( n );
  2317. aFree( n );
  2318. }
  2319. }
  2320. script_free_vars( stack->var_function );
  2321. aFree(stack->var_function);
  2322. aFree(stack->stack_data);
  2323. aFree(stack);
  2324. }
  2325. void script_free_code(struct script_code* code)
  2326. {
  2327. script_free_vars( &code->script_vars );
  2328. aFree( code->script_buf );
  2329. aFree( code );
  2330. }
  2331. //
  2332. // 実行部main
  2333. //
  2334. /*==========================================
  2335. * コマンドの読み取り
  2336. *------------------------------------------*/
  2337. static int unget_com_data=-1;
  2338. int get_com(unsigned char *script,int *pos)
  2339. {
  2340. int i,j;
  2341. if(unget_com_data>=0){
  2342. i=unget_com_data;
  2343. unget_com_data=-1;
  2344. return i;
  2345. }
  2346. if(script[*pos]>=0x80){
  2347. return C_INT;
  2348. }
  2349. i=0; j=0;
  2350. while(script[*pos]>=0x40){
  2351. i=script[(*pos)++]<<j;
  2352. j+=6;
  2353. }
  2354. return i+(script[(*pos)++]<<j);
  2355. }
  2356. /*==========================================
  2357. * コマンドのプッシュバック
  2358. *------------------------------------------*/
  2359. void unget_com(int c)
  2360. {
  2361. if(unget_com_data!=-1){
  2362. if(battle_config.error_log)
  2363. ShowError("unget_com can back only 1 data\n");
  2364. }
  2365. unget_com_data=c;
  2366. }
  2367. /*==========================================
  2368. * 数値の所得
  2369. *------------------------------------------*/
  2370. int get_num(unsigned char *script,int *pos)
  2371. {
  2372. int i,j;
  2373. i=0; j=0;
  2374. while(script[*pos]>=0xc0){
  2375. i+=(script[(*pos)++]&0x7f)<<j;
  2376. j+=6;
  2377. }
  2378. return i+((script[(*pos)++]&0x7f)<<j);
  2379. }
  2380. /*==========================================
  2381. * スタックから値を取り出す
  2382. *------------------------------------------*/
  2383. int pop_val(struct script_state* st)
  2384. {
  2385. if(st->stack->sp<=0)
  2386. return 0;
  2387. st->stack->sp--;
  2388. get_val(st,&(st->stack->stack_data[st->stack->sp]));
  2389. if(st->stack->stack_data[st->stack->sp].type==C_INT)
  2390. return st->stack->stack_data[st->stack->sp].u.num;
  2391. return 0;
  2392. }
  2393. int isstr(struct script_data *c)
  2394. {
  2395. if( data_isstring(c) )
  2396. return 1;
  2397. else if( data_isreference(c) ) {
  2398. char *p = str_buf + str_data[c->u.num & 0xffffff].str;
  2399. char postfix = p[strlen(p)-1];
  2400. return (postfix == '$');
  2401. }
  2402. return 0;
  2403. }
  2404. /// Ternary operators
  2405. /// test ? if_true : if_false
  2406. void op_3(struct script_state* st, int op)
  2407. {
  2408. struct script_data* data;
  2409. int flag = 0;
  2410. data = script_getdatatop(st, -3);
  2411. get_val(st, data);
  2412. if( data_isstring(data) )
  2413. flag = data->u.str[0];// "" -> false
  2414. else if( data_isint(data) )
  2415. flag = data->u.num;// 0 -> false
  2416. else
  2417. {
  2418. ShowError("script:op_3: invalid data for the ternary operator test\n");
  2419. script_reportdata(data);
  2420. script_reportsrc(st);
  2421. script_removetop(st, -3, 0);
  2422. script_pushnil(st);
  2423. return;
  2424. }
  2425. if( flag )
  2426. script_pushcopytop(st, -2);
  2427. else
  2428. script_pushcopytop(st, -1);
  2429. script_removetop(st, -4, -1);
  2430. }
  2431. /// Binary string operators
  2432. /// s1 EQ s2 -> i
  2433. /// s1 NE s2 -> i
  2434. /// s1 GT s2 -> i
  2435. /// s1 GE s2 -> i
  2436. /// s1 LT s2 -> i
  2437. /// s1 LE s2 -> i
  2438. /// s1 ADD s2 -> s
  2439. void op_2str(struct script_state* st, int op, const char* s1, const char* s2)
  2440. {
  2441. int a = 0;
  2442. switch(op){
  2443. case C_EQ: a = (strcmp(s1,s2) == 0); break;
  2444. case C_NE: a = (strcmp(s1,s2) != 0); break;
  2445. case C_GT: a = (strcmp(s1,s2) > 0); break;
  2446. case C_GE: a = (strcmp(s1,s2) >= 0); break;
  2447. case C_LT: a = (strcmp(s1,s2) < 0); break;
  2448. case C_LE: a = (strcmp(s1,s2) <= 0); break;
  2449. case C_ADD:
  2450. {
  2451. char* buf = (char *)aMallocA((strlen(s1)+strlen(s2)+1)*sizeof(char));
  2452. strcpy(buf, s1);
  2453. strcat(buf, s2);
  2454. script_pushstr(st, buf);
  2455. return;
  2456. }
  2457. default:
  2458. ShowError("script:op2_str: unexpected string operator %s\n", script_op2name(op));
  2459. script_reportsrc(st);
  2460. script_pushnil(st);
  2461. st->state = END;
  2462. return;
  2463. }
  2464. script_pushint(st,a);
  2465. }
  2466. /// Binary number operators
  2467. /// i OP i -> i
  2468. void op_2num(struct script_state* st, int op, int i1, int i2)
  2469. {
  2470. int ret;
  2471. double ret_double;
  2472. switch( op )
  2473. {
  2474. case C_AND: ret = i1 & i2; break;
  2475. case C_OR: ret = i1 | i2; break;
  2476. case C_XOR: ret = i1 ^ i2; break;
  2477. case C_LAND: ret = (i1 && i2); break;
  2478. case C_LOR: ret = (i1 || i2); break;
  2479. case C_EQ: ret = (i1 == i2); break;
  2480. case C_NE: ret = (i1 != i2); break;
  2481. case C_GT: ret = (i1 > i2); break;
  2482. case C_GE: ret = (i1 >= i2); break;
  2483. case C_LT: ret = (i1 < i2); break;
  2484. case C_LE: ret = (i1 <= i2); break;
  2485. case C_R_SHIFT: ret = i1>>i2; break;
  2486. case C_L_SHIFT: ret = i1<<i2; break;
  2487. case C_DIV:
  2488. case C_MOD:
  2489. if( i2 == 0 )
  2490. {
  2491. ShowError("script:op_2num: division by zero detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2492. script_reportsrc(st);
  2493. script_pushnil(st);
  2494. st->state = END;
  2495. return;
  2496. }
  2497. else if( op == C_DIV )
  2498. ret = i1 / i2;
  2499. else//if( op == C_MOD )
  2500. ret = i1 % i2;
  2501. break;
  2502. default:
  2503. switch( op )
  2504. {// operators that can overflow/underflow
  2505. case C_ADD: ret = i1 + i2; ret_double = (double)i1 + (double)i2; break;
  2506. case C_SUB: ret = i1 - i2; ret_double = (double)i1 - (double)i2; break;
  2507. case C_MUL: ret = i1 * i2; ret_double = (double)i1 * (double)i2; break;
  2508. default:
  2509. ShowError("script:op_2num: unexpected number operator %s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2510. script_reportsrc(st);
  2511. script_pushnil(st);
  2512. return;
  2513. }
  2514. if( ret_double < (double)INT_MIN )
  2515. {
  2516. ShowWarning("script:op_2num: underflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2517. script_reportsrc(st);
  2518. ret = INT_MIN;
  2519. }
  2520. else if( ret_double > (double)INT_MAX )
  2521. {
  2522. ShowWarning("script:op_2num: overflow detected op=%s i1=%d i2=%d\n", script_op2name(op), i1, i2);
  2523. script_reportsrc(st);
  2524. ret = INT_MAX;
  2525. }
  2526. }
  2527. script_pushint(st, ret);
  2528. }
  2529. /// Binary operators
  2530. void op_2(struct script_state *st, int op)
  2531. {
  2532. struct script_data* left;
  2533. struct script_data* right;
  2534. left = script_getdatatop(st, -2);
  2535. right = script_getdatatop(st, -1);
  2536. get_val(st, left);
  2537. get_val(st, right);
  2538. // automatic conversions
  2539. switch( op )
  2540. {
  2541. case C_ADD:
  2542. if( data_isint(left) && data_isstring(right) )
  2543. {// convert int-string to string-string
  2544. conv_str(st, left);
  2545. }
  2546. else if( data_isstring(left) && data_isint(right) )
  2547. {// convert string-int to string-string
  2548. conv_str(st, right);
  2549. }
  2550. break;
  2551. }
  2552. if( data_isstring(left) && data_isstring(right) )
  2553. {// ss => op_2str
  2554. op_2str(st, op, left->u.str, right->u.str);
  2555. script_removetop(st, -3, -1);// pop the two values before the top one
  2556. }
  2557. else if( data_isint(left) && data_isint(right) )
  2558. {// ii => op_2num
  2559. int i1 = left->u.num;
  2560. int i2 = right->u.num;
  2561. script_removetop(st, -2, 0);
  2562. op_2num(st, op, i1, i2);
  2563. }
  2564. else
  2565. {// invalid argument
  2566. ShowError("script:op_2: invalid data for operator %s\n", script_op2name(op));
  2567. script_reportdata(left);
  2568. script_reportdata(right);
  2569. script_reportsrc(st);
  2570. script_removetop(st, -2, 0);
  2571. script_pushnil(st);
  2572. st->state = END;
  2573. }
  2574. }
  2575. /// Unary operators
  2576. /// NEG i -> i
  2577. /// NOT i -> i
  2578. /// LNOT i -> i
  2579. void op_1(struct script_state* st, int op)
  2580. {
  2581. struct script_data* data;
  2582. int i1;
  2583. data = script_getdatatop(st, -1);
  2584. get_val(st, data);
  2585. if( !data_isint(data) )
  2586. {// not a number
  2587. ShowError("script:op_1: argument is not a number (op=%s)\n", script_op2name(op));
  2588. script_reportdata(data);
  2589. script_reportsrc(st);
  2590. script_pushnil(st);
  2591. st->state = END;
  2592. return;
  2593. }
  2594. i1 = data->u.num;
  2595. script_removetop(st, -1, 0);
  2596. switch( op )
  2597. {
  2598. case C_NEG: i1 = -i1; break;
  2599. case C_NOT: i1 = ~i1; break;
  2600. case C_LNOT: i1 = !i1; break;
  2601. default:
  2602. ShowError("script:op_1: unexpected operator %s i1=%d\n", script_op2name(op), i1);
  2603. script_reportsrc(st);
  2604. script_pushnil(st);
  2605. st->state = END;
  2606. return;
  2607. }
  2608. script_pushint(st, i1);
  2609. }
  2610. /*==========================================
  2611. * 関数の実行
  2612. *------------------------------------------*/
  2613. int run_func(struct script_state *st)
  2614. {
  2615. int i,start_sp,end_sp,func;
  2616. end_sp=st->stack->sp;
  2617. for(i=end_sp-1;i>=0 && st->stack->stack_data[i].type!=C_ARG;i--);
  2618. if(i<=0){ //Crash fix when missing "push_val" causes current pointer to become -1. from Rayce (jA)
  2619. if(battle_config.error_log)
  2620. ShowError("function not found\n");
  2621. // st->stack->sp=0;
  2622. st->state=END;
  2623. script_reportsrc(st);
  2624. return 1;
  2625. }
  2626. start_sp=i-1;
  2627. st->start=i-1;
  2628. st->end=end_sp;
  2629. func=st->stack->stack_data[st->start].u.num;
  2630. #ifdef DEBUG_RUN
  2631. if(battle_config.etc_log) {
  2632. 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);
  2633. ShowDebug("stack dump :");
  2634. for(i=0;i<end_sp;i++){
  2635. switch(st->stack->stack_data[i].type){
  2636. case C_INT:
  2637. printf(" int(%d)",st->stack->stack_data[i].u.num);
  2638. break;
  2639. case C_NAME:
  2640. printf(" name(%s)",str_buf+str_data[st->stack->stack_data[i].u.num & 0xffffff].str);
  2641. break;
  2642. case C_ARG:
  2643. printf(" arg");
  2644. break;
  2645. case C_POS:
  2646. printf(" pos(%d)",st->stack->stack_data[i].u.num);
  2647. break;
  2648. case C_STR:
  2649. printf(" str(%s)",st->stack->stack_data[i].u.str);
  2650. break;
  2651. case C_CONSTSTR:
  2652. printf(" cstr(%s)",st->stack->stack_data[i].u.str);
  2653. break;
  2654. default:
  2655. printf(" etc(%d,%d)",st->stack->stack_data[i].type,st->stack->stack_data[i].u.num);
  2656. }
  2657. }
  2658. printf("\n");
  2659. }
  2660. #endif
  2661. if(str_data[func].type!=C_FUNC ){
  2662. ShowMessage ("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);
  2663. // st->stack->sp=0;
  2664. st->state=END;
  2665. script_reportsrc(st);
  2666. return 1;
  2667. }
  2668. #ifdef DEBUG_RUN
  2669. ShowDebug("run_func : %s (func_no : %d , func_type : %d pos : 0x%x)\n",
  2670. str_buf+str_data[func].str,func,str_data[func].type,st->pos
  2671. );
  2672. #endif
  2673. if(str_data[func].func){
  2674. if (str_data[func].func(st)) //Report error
  2675. script_reportsrc(st);
  2676. } else {
  2677. if(battle_config.error_log)
  2678. ShowError("run_func : %s? (%d(%d))\n",str_buf+str_data[func].str,func,str_data[func].type);
  2679. script_pushint(st,0);
  2680. script_reportsrc(st);
  2681. }
  2682. // Stack's datum are used when re-run functions [Eoe]
  2683. if(st->state != RERUNLINE) {
  2684. pop_stack(st->stack,start_sp,end_sp);
  2685. }
  2686. if(st->state==RETFUNC){
  2687. // ユーザー定義関数からの復帰
  2688. int olddefsp=st->stack->defsp;
  2689. int i;
  2690. pop_stack(st->stack,st->stack->defsp,start_sp); // 復帰に邪魔なスタック削除
  2691. if(st->stack->defsp<5 || st->stack->stack_data[st->stack->defsp-1].type!=C_RETINFO){
  2692. ShowWarning("script:run_func(return) return without callfunc or callsub!\n");
  2693. st->state=END;
  2694. script_reportsrc(st);
  2695. return 1;
  2696. }
  2697. script_free_vars( st->stack->var_function );
  2698. aFree(st->stack->var_function);
  2699. i = conv_num(st,& (st->stack->stack_data[st->stack->defsp-5])); // 引数の数所得
  2700. st->pos=conv_num(st,& (st->stack->stack_data[st->stack->defsp-1])); // スクリプト位置の復元
  2701. st->script=(struct script_code*)conv_num(st,& (st->stack->stack_data[st->stack->defsp-3])); // スクリプトを復元
  2702. st->stack->var_function = (struct linkdb_node**)st->stack->stack_data[st->stack->defsp-2].u.num; // 関数依存変数
  2703. st->stack->defsp=conv_num(st,& (st->stack->stack_data[st->stack->defsp-4])); // 基準スタックポインタを復元
  2704. pop_stack(st->stack,olddefsp-5-i,olddefsp); // 要らなくなったスタック(引数と復帰用データ)削除
  2705. st->state=GOTO;
  2706. }
  2707. return 0;
  2708. }
  2709. /*==========================================
  2710. * スクリプトの実行
  2711. *------------------------------------------*/
  2712. void run_script_main(struct script_state *st);
  2713. void run_script(struct script_code *rootscript,int pos,int rid,int oid)
  2714. {
  2715. struct script_state *st;
  2716. TBL_PC *sd=NULL;
  2717. if(rootscript==NULL || pos<0)
  2718. return;
  2719. if (rid) sd = map_id2sd(rid);
  2720. if (sd && sd->st && sd->st->scriptroot == rootscript && sd->st->pos == pos){
  2721. //Resume script.
  2722. st = sd->st;
  2723. } else {
  2724. st = aCalloc(sizeof(struct script_state), 1);
  2725. // the script is different, make new script_state and stack
  2726. st->stack = aMalloc (sizeof(struct script_stack));
  2727. st->stack->sp=0;
  2728. st->stack->sp_max=64;
  2729. st->stack->stack_data = (struct script_data *)aCalloc(st->stack->sp_max,sizeof(st->stack->stack_data[0]));
  2730. st->stack->defsp = st->stack->sp;
  2731. st->stack->var_function = aCalloc(1, sizeof(struct linkdb_node*));
  2732. st->state = RUN;
  2733. st->script = rootscript;
  2734. }
  2735. st->pos = pos;
  2736. st->rid = rid;
  2737. st->oid = oid;
  2738. st->sleep.timer = INVALID_TIMER;
  2739. run_script_main(st);
  2740. }
  2741. void script_stop_sleeptimers(int id)
  2742. {
  2743. struct script_state* st;
  2744. for(;;)
  2745. {
  2746. st = (struct script_state*)linkdb_erase(&sleep_db,(void*)id);
  2747. if( st == NULL )
  2748. break; // no more sleep timers
  2749. if( st->sleep.timer != INVALID_TIMER )
  2750. delete_timer(st->sleep.timer, run_script_timer);
  2751. script_free_stack(st->stack);
  2752. aFree(st);
  2753. }
  2754. }
  2755. /*==========================================
  2756. * 指定ノードをsleep_dbから削除
  2757. *------------------------------------------*/
  2758. struct linkdb_node* script_erase_sleepdb(struct linkdb_node *n)
  2759. {
  2760. struct linkdb_node *retnode;
  2761. if( n == NULL)
  2762. return NULL;
  2763. if( n->prev == NULL )
  2764. sleep_db = n->next;
  2765. else
  2766. n->prev->next = n->next;
  2767. if( n->next )
  2768. n->next->prev = n->prev;
  2769. retnode = n->next;
  2770. aFree( n );
  2771. return retnode; // 次のノードを返す
  2772. }
  2773. /*==========================================
  2774. * sleep用タイマー関数
  2775. *------------------------------------------*/
  2776. int run_script_timer(int tid, unsigned int tick, int id, int data)
  2777. {
  2778. struct script_state *st = (struct script_state *)data;
  2779. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  2780. TBL_PC *sd = map_id2sd(st->rid);
  2781. if((sd && sd->status.char_id != id) || (st->rid && !sd))
  2782. { //Character mismatch. Cancel execution.
  2783. st->rid = 0;
  2784. st->state = END;
  2785. }
  2786. while( node && st->sleep.timer != -1 ) {
  2787. if( (int)node->key == st->oid && ((struct script_state *)node->data)->sleep.timer == st->sleep.timer ) {
  2788. script_erase_sleepdb(node);
  2789. st->sleep.timer = -1;
  2790. break;
  2791. }
  2792. node = node->next;
  2793. }
  2794. if(st->state != RERUNLINE)
  2795. st->sleep.tick = 0;
  2796. run_script_main(st);
  2797. return 0;
  2798. }
  2799. /*==========================================
  2800. * スクリプトの実行メイン部分
  2801. *------------------------------------------*/
  2802. void run_script_main(struct script_state *st)
  2803. {
  2804. int c;
  2805. int cmdcount=script_config.check_cmdcount;
  2806. int gotocount=script_config.check_gotocount;
  2807. TBL_PC *sd;
  2808. //For backing up purposes
  2809. struct script_state *bk_st = NULL;
  2810. int bk_npcid = 0;
  2811. struct script_stack *stack=st->stack;
  2812. sd = st->rid?map_id2sd(st->rid):NULL;
  2813. if(sd){
  2814. if(sd->st != st){
  2815. bk_st = sd->st;
  2816. bk_npcid = sd->npc_id;
  2817. }
  2818. sd->st = st;
  2819. sd->npc_id = st->oid;
  2820. }
  2821. if(st->state == RERUNLINE) {
  2822. st->state = RUN;
  2823. run_func(st);
  2824. if(st->state == GOTO)
  2825. st->state = RUN;
  2826. } else if(st->state != END)
  2827. st->state = RUN;
  2828. while(st->state == RUN){
  2829. c= get_com(st->script->script_buf,&st->pos);
  2830. switch(c){
  2831. case C_EOL:
  2832. if( stack->sp != stack->defsp )
  2833. {
  2834. if( stack->sp > stack->defsp )
  2835. { //sp > defsp is valid in cases when you invoke functions and don't use the returned value. [Skotlex]
  2836. //Since sp is supposed to be defsp in these cases, we could assume the extra stack elements are unneeded.
  2837. pop_stack(stack, stack->defsp, stack->sp); //Clear out the unused stack-section.
  2838. } else if( battle_config.error_log )
  2839. ShowError("script:run_script_main: unexpected stack position stack.sp(%d) != default(%d)\n", stack->sp, stack->defsp);
  2840. stack->sp = stack->defsp;
  2841. }
  2842. break;
  2843. case C_INT:
  2844. push_val(stack,C_INT,get_num(st->script->script_buf,&st->pos));
  2845. break;
  2846. case C_POS:
  2847. case C_NAME:
  2848. push_val(stack,c,GETVALUE(st->script->script_buf,st->pos));
  2849. st->pos+=3;
  2850. break;
  2851. case C_ARG:
  2852. push_val(stack,c,0);
  2853. break;
  2854. case C_STR:
  2855. push_str(stack,C_CONSTSTR,(char*)(st->script->script_buf+st->pos));
  2856. while(st->script->script_buf[st->pos++]);
  2857. break;
  2858. case C_FUNC:
  2859. run_func(st);
  2860. if(st->state==GOTO){
  2861. st->state = RUN;
  2862. if( gotocount>0 && (--gotocount)<=0 ){
  2863. ShowError("run_script: infinity loop !\n");
  2864. script_reportsrc(st);
  2865. st->state=END;
  2866. }
  2867. }
  2868. break;
  2869. case C_NEG:
  2870. case C_NOT:
  2871. case C_LNOT:
  2872. op_1(st ,c);
  2873. break;
  2874. case C_ADD:
  2875. case C_SUB:
  2876. case C_MUL:
  2877. case C_DIV:
  2878. case C_MOD:
  2879. case C_EQ:
  2880. case C_NE:
  2881. case C_GT:
  2882. case C_GE:
  2883. case C_LT:
  2884. case C_LE:
  2885. case C_AND:
  2886. case C_OR:
  2887. case C_XOR:
  2888. case C_LAND:
  2889. case C_LOR:
  2890. case C_R_SHIFT:
  2891. case C_L_SHIFT:
  2892. op_2(st, c);
  2893. break;
  2894. case C_OP3:
  2895. op_3(st, c);
  2896. break;
  2897. case C_NOP:
  2898. st->state=END;
  2899. break;
  2900. default:
  2901. if(battle_config.error_log)
  2902. ShowError("unknown command : %d @ %d\n",c,st->pos);
  2903. st->state=END;
  2904. break;
  2905. }
  2906. if( cmdcount>0 && (--cmdcount)<=0 ){
  2907. ShowError("run_script: infinity loop !\n");
  2908. script_reportsrc(st);
  2909. st->state=END;
  2910. }
  2911. }
  2912. if(st->sleep.tick > 0) {
  2913. //Delay execution
  2914. st->sleep.charid = sd?sd->status.char_id:0;
  2915. st->sleep.timer = add_timer(gettick()+st->sleep.tick,
  2916. run_script_timer, st->sleep.charid, (int)st);
  2917. linkdb_insert(&sleep_db, (void*)st->oid, st);
  2918. //Restore previous script
  2919. if (sd) {
  2920. sd->st = bk_st;
  2921. sd->npc_id = bk_npcid;
  2922. bk_st = NULL; //Remove tag for removal.
  2923. }
  2924. }
  2925. else if(st->state != END && sd){
  2926. //Resume later (st is already attached to player).
  2927. if(bk_st) {
  2928. ShowWarning("Unable to restore stack! Double continuation!\n");
  2929. //Report BOTH scripts to see if that can help somehow.
  2930. ShowDebug("Previous script (lost):\n");
  2931. script_reportsrc(bk_st);
  2932. ShowDebug("Current script:\n");
  2933. script_reportsrc(st);
  2934. }
  2935. } else {
  2936. //Dispose of script.
  2937. if (sd)
  2938. { //Restore previous stack and save char.
  2939. if(sd->state.using_fake_npc){
  2940. clif_clearunit_single(sd->npc_id, 0, sd->fd);
  2941. sd->state.using_fake_npc = 0;
  2942. }
  2943. //Restore previous script if any.
  2944. sd->st = bk_st;
  2945. sd->npc_id = bk_npcid;
  2946. if (!bk_st)
  2947. npc_event_dequeue(sd);
  2948. else
  2949. bk_st = NULL; //Remove tag for removal.
  2950. if (sd->state.reg_dirty&2)
  2951. intif_saveregistry(sd,2);
  2952. if (sd->state.reg_dirty&1)
  2953. intif_saveregistry(sd,1);
  2954. }
  2955. st->pos = -1;
  2956. script_free_stack (st->stack);
  2957. aFree(st);
  2958. }
  2959. if (bk_st)
  2960. { //Remove previous script
  2961. bk_st->pos = -1;
  2962. script_free_stack(bk_st->stack);
  2963. aFree(bk_st);
  2964. bk_st = NULL;
  2965. }
  2966. }
  2967. /*==========================================
  2968. * マップ変数の変更
  2969. *------------------------------------------*/
  2970. int mapreg_setreg(int num,int val)
  2971. {
  2972. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2973. int i=num>>24;
  2974. char *name=str_buf+str_data[num&0x00ffffff].str;
  2975. char tmp_str[64];
  2976. #endif
  2977. if(val!=0) {
  2978. if(idb_put(mapreg_db,num,(void*)val))
  2979. ;
  2980. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2981. else if(name[1] != '@') {
  2982. sprintf(tmp_sql,"INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%d')",mapregsql_db,mapregsql_db_varname,mapregsql_db_index,mapregsql_db_value,jstrescapecpy(tmp_str,name),i,val);
  2983. if(mysql_query(&mmysql_handle,tmp_sql)){
  2984. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2985. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2986. }
  2987. }
  2988. #endif
  2989. } else { // [zBuffer]
  2990. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  2991. if(name[1] != '@') { // Remove from database because it is unused.
  2992. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_varname,name,mapregsql_db_index,i);
  2993. if(mysql_query(&mmysql_handle,tmp_sql)){
  2994. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  2995. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  2996. }
  2997. }
  2998. #endif
  2999. idb_remove(mapreg_db,num);
  3000. }
  3001. mapreg_dirty=1;
  3002. return 1;
  3003. }
  3004. /*==========================================
  3005. * 文字列型マップ変数の変更
  3006. *------------------------------------------*/
  3007. int mapreg_setregstr(int num,const char *str)
  3008. {
  3009. char *p;
  3010. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  3011. char tmp_str[64];
  3012. char tmp_str2[512];
  3013. int i=num>>24; // [zBuffer]
  3014. char *name=str_buf+str_data[num&0x00ffffff].str;
  3015. #endif
  3016. if( str==NULL || *str==0 ){
  3017. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  3018. if(name[1] != '@') {
  3019. sprintf(tmp_sql,"DELETE FROM `%s` WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_varname,name,mapregsql_db_index,i);
  3020. if(mysql_query(&mmysql_handle,tmp_sql)){
  3021. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  3022. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3023. }
  3024. }
  3025. #endif
  3026. idb_remove(mapregstr_db,num);
  3027. mapreg_dirty=1;
  3028. return 1;
  3029. }
  3030. p=(char *)aMallocA((strlen(str)+1)*sizeof(char));
  3031. strcpy(p,str);
  3032. if (idb_put(mapregstr_db,num,p))
  3033. ;
  3034. #if !defined(TXT_ONLY) && defined(MAPREGSQL)
  3035. else if(name[1] != '@'){ //put returned null, so we must insert.
  3036. // Someone is causing a database size infinite increase here without name[1] != '@' [Lance]
  3037. sprintf(tmp_sql,"INSERT INTO `%s`(`%s`,`%s`,`%s`) VALUES ('%s','%d','%s')",mapregsql_db,mapregsql_db_varname,mapregsql_db_index,mapregsql_db_value,jstrescapecpy(tmp_str,name),i,jstrescapecpy(tmp_str2,p));
  3038. if(mysql_query(&mmysql_handle,tmp_sql)){
  3039. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  3040. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3041. }
  3042. }
  3043. #endif
  3044. mapreg_dirty=1;
  3045. return 1;
  3046. }
  3047. /*==========================================
  3048. * 永続的マップ変数の読み込み
  3049. *------------------------------------------*/
  3050. static int script_load_mapreg(void)
  3051. {
  3052. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3053. FILE *fp;
  3054. char line[1024];
  3055. if( (fp=fopen(mapreg_txt,"rt"))==NULL )
  3056. return -1;
  3057. while(fgets(line,sizeof(line),fp))
  3058. {
  3059. char buf1[256],buf2[1024],*p;
  3060. int n,v,s,i;
  3061. if( sscanf(line,"%255[^,],%d\t%n",buf1,&i,&n)!=2 &&
  3062. (i=0,sscanf(line,"%[^\t]\t%n",buf1,&n)!=1) )
  3063. continue;
  3064. if( buf1[strlen(buf1)-1]=='$' ){
  3065. if( sscanf(line+n,"%[^\n\r]",buf2)!=1 ){
  3066. ShowError("%s: %s broken data !\n",mapreg_txt,buf1);
  3067. continue;
  3068. }
  3069. p=(char *)aMallocA((strlen(buf2) + 1)*sizeof(char));
  3070. strcpy(p,buf2);
  3071. s= add_str(buf1);
  3072. idb_put(mapregstr_db,(i<<24)|s,p);
  3073. }else{
  3074. if( sscanf(line+n,"%d",&v)!=1 ){
  3075. ShowError("%s: %s broken data !\n",mapreg_txt,buf1);
  3076. continue;
  3077. }
  3078. s= add_str(buf1);
  3079. idb_put(mapreg_db,(i<<24)|s,(void*)v);
  3080. }
  3081. }
  3082. fclose(fp);
  3083. mapreg_dirty=0;
  3084. return 0;
  3085. #else
  3086. // SQL mapreg code start [zBuffer]
  3087. /*
  3088. 0 1 2
  3089. +-------------------------+
  3090. | varname | index | value |
  3091. +-------------------------+
  3092. */
  3093. unsigned int perfomance = (unsigned int)time(NULL);
  3094. sprintf(tmp_sql,"SELECT * FROM `%s`",mapregsql_db);
  3095. ShowInfo("Querying script_load_mapreg ...\n");
  3096. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  3097. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  3098. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3099. return -1;
  3100. }
  3101. ShowInfo("Success! Returning results ...\n");
  3102. sql_res = mysql_store_result(&mmysql_handle);
  3103. if (sql_res) {
  3104. while ((sql_row = mysql_fetch_row(sql_res))) {
  3105. char buf1[33], *p = NULL;
  3106. int i,v,s;
  3107. strcpy(buf1,sql_row[0]);
  3108. if( buf1[strlen(buf1)-1]=='$' ){
  3109. i = atoi(sql_row[1]);
  3110. p=(char *)aMallocA((strlen(sql_row[2]) + 1)*sizeof(char));
  3111. strcpy(p,sql_row[2]);
  3112. s= add_str(buf1);
  3113. idb_put(mapregstr_db,(i<<24)|s,p);
  3114. }else{
  3115. s= add_str(buf1);
  3116. v= atoi(sql_row[2]);
  3117. i = atoi(sql_row[1]);
  3118. idb_put(mapreg_db,(i<<24)|s,(void *)v);
  3119. }
  3120. }
  3121. }
  3122. ShowInfo("Freeing results...\n");
  3123. mysql_free_result(sql_res);
  3124. mapreg_dirty=0;
  3125. perfomance = (((unsigned int)time(NULL)) - perfomance);
  3126. ShowInfo("SQL Mapreg Loading Completed Under %d Seconds.\n",perfomance);
  3127. return 0;
  3128. #endif /* TXT_ONLY */
  3129. }
  3130. /*==========================================
  3131. * 永続的マップ変数の書き込み
  3132. *------------------------------------------*/
  3133. static int script_save_mapreg_intsub(DBKey key,void *data,va_list ap)
  3134. {
  3135. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3136. FILE *fp=va_arg(ap,FILE*);
  3137. int num=key.i&0x00ffffff, i=key.i>>24;
  3138. char *name=str_buf+str_data[num].str;
  3139. if( name[1]!='@' ){
  3140. if(i==0)
  3141. fprintf(fp,"%s\t%d\n", name, (int)data);
  3142. else
  3143. fprintf(fp,"%s,%d\t%d\n", name, i, (int)data);
  3144. }
  3145. return 0;
  3146. #else
  3147. int num=key.i&0x00ffffff, i=key.i>>24; // [zBuffer]
  3148. char *name=str_buf+str_data[num].str;
  3149. if ( name[1] != '@') {
  3150. sprintf(tmp_sql,"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);
  3151. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  3152. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  3153. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3154. }
  3155. }
  3156. return 0;
  3157. #endif
  3158. }
  3159. static int script_save_mapreg_strsub(DBKey key,void *data,va_list ap)
  3160. {
  3161. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3162. FILE *fp=va_arg(ap,FILE*);
  3163. int num=key.i&0x00ffffff, i=key.i>>24;
  3164. char *name=str_buf+str_data[num].str;
  3165. if( name[1]!='@' ){
  3166. if(i==0)
  3167. fprintf(fp,"%s\t%s\n", name, (char *)data);
  3168. else
  3169. fprintf(fp,"%s,%d\t%s\n", name, i, (char *)data);
  3170. }
  3171. return 0;
  3172. #else
  3173. char tmp_str2[512];
  3174. int num=key.i&0x00ffffff, i=key.i>>24;
  3175. char *name=str_buf+str_data[num].str;
  3176. if ( name[1] != '@') {
  3177. sprintf(tmp_sql,"UPDATE `%s` SET `%s`='%s' WHERE `%s`='%s' AND `%s`='%d'",mapregsql_db,mapregsql_db_value,jstrescapecpy(tmp_str2,(char *)data),mapregsql_db_varname,name,mapregsql_db_index,i);
  3178. if(mysql_query(&mmysql_handle, tmp_sql) ) {
  3179. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  3180. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  3181. }
  3182. }
  3183. return 0;
  3184. #endif
  3185. }
  3186. static int script_save_mapreg(void)
  3187. {
  3188. #if defined(TXT_ONLY) || !defined(MAPREGSQL)
  3189. FILE *fp;
  3190. int lock;
  3191. if( (fp=lock_fopen(mapreg_txt,&lock))==NULL ) {
  3192. ShowError("script_save_mapreg: Unable to lock-open file [%s]\n",mapreg_txt);
  3193. return -1;
  3194. }
  3195. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub,fp);
  3196. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub,fp);
  3197. lock_fclose(fp,mapreg_txt,&lock);
  3198. #else
  3199. unsigned int perfomance = (unsigned int)time(NULL);
  3200. mapreg_db->foreach(mapreg_db,script_save_mapreg_intsub); // [zBuffer]
  3201. mapregstr_db->foreach(mapregstr_db,script_save_mapreg_strsub);
  3202. perfomance = ((unsigned int)time(NULL) - perfomance);
  3203. if(perfomance > 2)
  3204. ShowWarning("Slow Query: MapregSQL Saving @ %d second(s).\n", perfomance);
  3205. #endif
  3206. mapreg_dirty=0;
  3207. return 0;
  3208. }
  3209. static int script_autosave_mapreg(int tid,unsigned int tick,int id,int data)
  3210. {
  3211. if(mapreg_dirty)
  3212. if (script_save_mapreg() == -1)
  3213. ShowError("Failed to save the mapreg data!\n");
  3214. return 0;
  3215. }
  3216. int script_config_read_sub(char *cfgName)
  3217. {
  3218. int i;
  3219. char line[1024],w1[1024],w2[1024];
  3220. FILE *fp;
  3221. fp=fopen(cfgName,"r");
  3222. if(fp==NULL){
  3223. ShowError("file not found: [%s]\n", cfgName);
  3224. return 1;
  3225. }
  3226. while(fgets(line, sizeof(line), fp))
  3227. {
  3228. if(line[0] == '/' && line[1] == '/')
  3229. continue;
  3230. i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2);
  3231. if(i!=2)
  3232. continue;
  3233. if(strcmpi(w1,"verbose_mode")==0) {
  3234. script_config.verbose_mode = config_switch(w2);
  3235. }
  3236. else if(strcmpi(w1,"warn_func_mismatch_paramnum")==0) {
  3237. script_config.warn_func_mismatch_paramnum = config_switch(w2);
  3238. }
  3239. else if(strcmpi(w1,"check_cmdcount")==0) {
  3240. script_config.check_cmdcount = config_switch(w2);
  3241. }
  3242. else if(strcmpi(w1,"check_gotocount")==0) {
  3243. script_config.check_gotocount = config_switch(w2);
  3244. }
  3245. else if(strcmpi(w1,"event_script_type")==0) {
  3246. script_config.event_script_type = config_switch(w2);
  3247. }
  3248. else if(strcmpi(w1,"event_requires_trigger")==0) {
  3249. script_config.event_requires_trigger = config_switch(w2);
  3250. }
  3251. else if(strcmpi(w1,"die_event_name")==0) {
  3252. strncpy(script_config.die_event_name, w2, NAME_LENGTH-1);
  3253. if (strlen(script_config.die_event_name) != strlen(w2))
  3254. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.die_event_name);
  3255. }
  3256. else if(strcmpi(w1,"kill_pc_event_name")==0) {
  3257. strncpy(script_config.kill_pc_event_name, w2, NAME_LENGTH-1);
  3258. if (strlen(script_config.kill_pc_event_name) != strlen(w2))
  3259. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_pc_event_name);
  3260. }
  3261. else if(strcmpi(w1,"kill_mob_event_name")==0) {
  3262. strncpy(script_config.kill_mob_event_name, w2, NAME_LENGTH-1);
  3263. if (strlen(script_config.kill_mob_event_name) != strlen(w2))
  3264. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.kill_mob_event_name);
  3265. }
  3266. else if(strcmpi(w1,"login_event_name")==0) {
  3267. strncpy(script_config.login_event_name, w2, NAME_LENGTH-1);
  3268. if (strlen(script_config.login_event_name) != strlen(w2))
  3269. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.login_event_name);
  3270. }
  3271. else if(strcmpi(w1,"logout_event_name")==0) {
  3272. strncpy(script_config.logout_event_name, w2, NAME_LENGTH-1);
  3273. if (strlen(script_config.logout_event_name) != strlen(w2))
  3274. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.logout_event_name);
  3275. }
  3276. else if(strcmpi(w1,"loadmap_event_name")==0) {
  3277. strncpy(script_config.loadmap_event_name, w2, NAME_LENGTH-1);
  3278. if (strlen(script_config.loadmap_event_name) != strlen(w2))
  3279. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.loadmap_event_name);
  3280. }
  3281. else if(strcmpi(w1,"baselvup_event_name")==0) {
  3282. strncpy(script_config.baselvup_event_name, w2, NAME_LENGTH-1);
  3283. if (strlen(script_config.baselvup_event_name) != strlen(w2))
  3284. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.baselvup_event_name);
  3285. }
  3286. else if(strcmpi(w1,"joblvup_event_name")==0) {
  3287. strncpy(script_config.joblvup_event_name, w2, NAME_LENGTH-1);
  3288. if (strlen(script_config.joblvup_event_name) != strlen(w2))
  3289. ShowWarning("script_config_read: Event label truncated (max length is 23 chars): %d\n", script_config.joblvup_event_name);
  3290. }
  3291. else if(strcmpi(w1,"import")==0){
  3292. script_config_read_sub(w2);
  3293. }
  3294. }
  3295. fclose(fp);
  3296. return 0;
  3297. }
  3298. int script_config_read(char *cfgName)
  3299. { //Script related variables should be initialized once! [Skotlex]
  3300. memset (&script_config, 0, sizeof(script_config));
  3301. script_config.verbose_mode = 0;
  3302. script_config.warn_func_mismatch_paramnum = 1;
  3303. script_config.check_cmdcount = 65535;
  3304. script_config.check_gotocount = 2048;
  3305. script_config.event_script_type = 0;
  3306. script_config.event_requires_trigger = 1;
  3307. return script_config_read_sub(cfgName);
  3308. }
  3309. static int do_final_userfunc_sub (DBKey key,void *data,va_list ap)
  3310. {
  3311. struct script_code *code = (struct script_code *)data;
  3312. if(code){
  3313. script_free_vars( &code->script_vars );
  3314. aFree( code->script_buf );
  3315. }
  3316. return 0;
  3317. }
  3318. /*==========================================
  3319. * 終了
  3320. *------------------------------------------*/
  3321. int do_final_script()
  3322. {
  3323. #ifdef DEBUG_HASH
  3324. if (battle_config.etc_log)
  3325. {
  3326. FILE *fp = fopen("hash_dump.txt","wt");
  3327. if(fp) {
  3328. int i,count[SCRIPT_HASH_SIZE];
  3329. int count2[SCRIPT_HASH_SIZE]; // number of buckets with a certain number of items
  3330. int n=0;
  3331. int min=INT_MAX,max=0,zero=0;
  3332. double mean=0.0f;
  3333. double median=0.0f;
  3334. ShowNotice("Dumping script str hash information to hash_dump.txt\n");
  3335. memset(count, 0, sizeof(count));
  3336. fprintf(fp,"num : calced_val -> hash : data_name\n");
  3337. fprintf(fp,"---------------------------------------------------------------\n");
  3338. for(i=LABEL_START; i<str_num; i++) {
  3339. unsigned int h2=calc_hash2(str_buf+str_data[i].str);
  3340. unsigned int h =h2%SCRIPT_HASH_SIZE;
  3341. fprintf(fp,"%04d: %10u -> %3u : %s\n",i,h2,h,str_buf+str_data[i].str);
  3342. ++count[h];
  3343. }
  3344. fprintf(fp,"--------------------\n\n");
  3345. memset(count2, 0, sizeof(count2));
  3346. for(i=0; i<SCRIPT_HASH_SIZE; i++) {
  3347. fprintf(fp," hash %3d = %d\n",i,count[i]);
  3348. if(min > count[i])
  3349. min = count[i]; // minimun count of collision
  3350. if(max < count[i])
  3351. max = count[i]; // maximun count of collision
  3352. if(count[i] == 0)
  3353. zero++;
  3354. ++count2[count[i]];
  3355. }
  3356. fprintf(fp,"\n--------------------\n items : buckets\n--------------------\n");
  3357. for( i=min; i <= max; ++i ){
  3358. fprintf(fp," %5d : %7d\n",i,count2[i]);
  3359. mean += 1.0f*i*count2[i]/SCRIPT_HASH_SIZE; // Note: this will always result in <nr labels>/<nr buckets>
  3360. }
  3361. for( i=min; i <= max; ++i ){
  3362. n += count2[i];
  3363. if( n*2 >= SCRIPT_HASH_SIZE )
  3364. {
  3365. if( SCRIPT_HASH_SIZE%2 == 0 && SCRIPT_HASH_SIZE/2 == n )
  3366. median = (i+i+1)/2.0f;
  3367. else
  3368. median = i;
  3369. break;
  3370. }
  3371. }
  3372. fprintf(fp,"--------------------\n min = %d, max = %d, zero = %d\n mean = %lf, median = %lf\n",min,max,zero,mean,median);
  3373. fclose(fp);
  3374. }
  3375. }
  3376. #endif
  3377. if(mapreg_dirty>=0)
  3378. script_save_mapreg();
  3379. mapreg_db->destroy(mapreg_db,NULL);
  3380. mapregstr_db->destroy(mapregstr_db,NULL);
  3381. scriptlabel_db->destroy(scriptlabel_db,NULL);
  3382. userfunc_db->destroy(userfunc_db,do_final_userfunc_sub);
  3383. if(sleep_db) {
  3384. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3385. while(n) {
  3386. struct script_state *st = (struct script_state *)n->data;
  3387. script_free_stack(st->stack);
  3388. aFree(st);
  3389. n = n->next;
  3390. }
  3391. linkdb_final(&sleep_db);
  3392. }
  3393. if (str_data)
  3394. aFree(str_data);
  3395. if (str_buf)
  3396. aFree(str_buf);
  3397. return 0;
  3398. }
  3399. /*==========================================
  3400. * 初期化
  3401. *------------------------------------------*/
  3402. int do_init_script()
  3403. {
  3404. mapreg_db= db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_BASE,sizeof(int));
  3405. mapregstr_db=db_alloc(__FILE__,__LINE__,DB_INT,DB_OPT_RELEASE_DATA,sizeof(int));
  3406. userfunc_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_RELEASE_BOTH,50);
  3407. scriptlabel_db=db_alloc(__FILE__,__LINE__,DB_STRING,DB_OPT_DUP_KEY|DB_OPT_ALLOW_NULL_DATA,50);
  3408. script_load_mapreg();
  3409. add_timer_func_list(script_autosave_mapreg,"script_autosave_mapreg");
  3410. add_timer_interval(gettick()+MAPREG_AUTOSAVE_INTERVAL,
  3411. script_autosave_mapreg,0,0,MAPREG_AUTOSAVE_INTERVAL);
  3412. return 0;
  3413. }
  3414. int script_reload()
  3415. {
  3416. if(mapreg_dirty>=0)
  3417. script_save_mapreg();
  3418. mapreg_db->clear(mapreg_db, NULL);
  3419. mapregstr_db->clear(mapregstr_db, NULL);
  3420. userfunc_db->clear(userfunc_db,do_final_userfunc_sub);
  3421. scriptlabel_db->clear(scriptlabel_db, NULL);
  3422. if(sleep_db) {
  3423. struct linkdb_node *n = (struct linkdb_node *)sleep_db;
  3424. while(n) {
  3425. struct script_state *st = (struct script_state *)n->data;
  3426. if( st->sleep.timer != INVALID_TIMER )
  3427. delete_timer(st->sleep.timer, run_script_timer);
  3428. script_free_stack(st->stack);
  3429. aFree(st);
  3430. n = n->next;
  3431. }
  3432. linkdb_final(&sleep_db);
  3433. }
  3434. script_load_mapreg();
  3435. return 0;
  3436. }
  3437. //-----------------------------------------------------------------------------
  3438. // buildin functions
  3439. //
  3440. #define BUILDIN_DEF(x,args) { buildin_ ## x , #x , args }
  3441. #define BUILDIN_DEF2(x,x2,args) { buildin_ ## x , x2 , args }
  3442. #define BUILDIN_FUNC(x) int buildin_ ## x (struct script_state* st)
  3443. BUILDIN_FUNC(mes);
  3444. BUILDIN_FUNC(goto);
  3445. BUILDIN_FUNC(callsub);
  3446. BUILDIN_FUNC(callfunc);
  3447. BUILDIN_FUNC(return);
  3448. BUILDIN_FUNC(getarg);
  3449. BUILDIN_FUNC(next);
  3450. BUILDIN_FUNC(close);
  3451. BUILDIN_FUNC(close2);
  3452. BUILDIN_FUNC(menu);
  3453. BUILDIN_FUNC(rand);
  3454. BUILDIN_FUNC(warp);
  3455. BUILDIN_FUNC(areawarp);
  3456. BUILDIN_FUNC(warpchar); // [LuzZza]
  3457. BUILDIN_FUNC(warpparty); //[Fredzilla]
  3458. BUILDIN_FUNC(warpguild); //[Fredzilla]
  3459. BUILDIN_FUNC(heal);
  3460. BUILDIN_FUNC(itemheal);
  3461. BUILDIN_FUNC(percentheal);
  3462. BUILDIN_FUNC(jobchange);
  3463. BUILDIN_FUNC(jobname);
  3464. BUILDIN_FUNC(input);
  3465. BUILDIN_FUNC(setlook);
  3466. BUILDIN_FUNC(set);
  3467. BUILDIN_FUNC(setarray);
  3468. BUILDIN_FUNC(cleararray);
  3469. BUILDIN_FUNC(copyarray);
  3470. BUILDIN_FUNC(getarraysize);
  3471. BUILDIN_FUNC(deletearray);
  3472. BUILDIN_FUNC(getelementofarray);
  3473. BUILDIN_FUNC(getitem);
  3474. BUILDIN_FUNC(getitem2);
  3475. BUILDIN_FUNC(getnameditem);
  3476. BUILDIN_FUNC(grouprandomitem);
  3477. BUILDIN_FUNC(makeitem);
  3478. BUILDIN_FUNC(delitem);
  3479. BUILDIN_FUNC(delitem2);
  3480. BUILDIN_FUNC(enableitemuse);
  3481. BUILDIN_FUNC(disableitemuse);
  3482. BUILDIN_FUNC(viewpoint);
  3483. BUILDIN_FUNC(countitem);
  3484. BUILDIN_FUNC(countitem2);
  3485. BUILDIN_FUNC(checkweight);
  3486. BUILDIN_FUNC(readparam);
  3487. BUILDIN_FUNC(getcharid);
  3488. BUILDIN_FUNC(getpartyname);
  3489. BUILDIN_FUNC(getpartymember);
  3490. BUILDIN_FUNC(getpartyleader);
  3491. BUILDIN_FUNC(getguildname);
  3492. BUILDIN_FUNC(getguildmaster);
  3493. BUILDIN_FUNC(getguildmasterid);
  3494. BUILDIN_FUNC(strcharinfo);
  3495. BUILDIN_FUNC(getequipid);
  3496. BUILDIN_FUNC(getequipname);
  3497. BUILDIN_FUNC(getbrokenid); // [Valaris]
  3498. BUILDIN_FUNC(repair); // [Valaris]
  3499. BUILDIN_FUNC(getequipisequiped);
  3500. BUILDIN_FUNC(getequipisenableref);
  3501. BUILDIN_FUNC(getequipisidentify);
  3502. BUILDIN_FUNC(getequiprefinerycnt);
  3503. BUILDIN_FUNC(getequipweaponlv);
  3504. BUILDIN_FUNC(getequippercentrefinery);
  3505. BUILDIN_FUNC(successrefitem);
  3506. BUILDIN_FUNC(failedrefitem);
  3507. BUILDIN_FUNC(cutin);
  3508. BUILDIN_FUNC(statusup);
  3509. BUILDIN_FUNC(statusup2);
  3510. BUILDIN_FUNC(bonus);
  3511. BUILDIN_FUNC(bonus2);
  3512. BUILDIN_FUNC(bonus3);
  3513. BUILDIN_FUNC(bonus4);
  3514. BUILDIN_FUNC(bonus5);
  3515. BUILDIN_FUNC(skill);
  3516. BUILDIN_FUNC(addtoskill); // [Valaris]
  3517. BUILDIN_FUNC(guildskill);
  3518. BUILDIN_FUNC(getskilllv);
  3519. BUILDIN_FUNC(getgdskilllv);
  3520. BUILDIN_FUNC(basicskillcheck);
  3521. BUILDIN_FUNC(getgmlevel);
  3522. BUILDIN_FUNC(end);
  3523. BUILDIN_FUNC(checkoption);
  3524. BUILDIN_FUNC(setoption);
  3525. BUILDIN_FUNC(setcart);
  3526. BUILDIN_FUNC(checkcart); // check cart [Valaris]
  3527. BUILDIN_FUNC(setfalcon);
  3528. BUILDIN_FUNC(checkfalcon); // check falcon [Valaris]
  3529. BUILDIN_FUNC(setriding);
  3530. BUILDIN_FUNC(checkriding); // check for pecopeco [Valaris]
  3531. BUILDIN_FUNC(savepoint);
  3532. BUILDIN_FUNC(gettimetick);
  3533. BUILDIN_FUNC(gettime);
  3534. BUILDIN_FUNC(gettimestr);
  3535. BUILDIN_FUNC(openstorage);
  3536. BUILDIN_FUNC(guildopenstorage);
  3537. BUILDIN_FUNC(itemskill);
  3538. BUILDIN_FUNC(produce);
  3539. BUILDIN_FUNC(monster);
  3540. BUILDIN_FUNC(areamonster);
  3541. BUILDIN_FUNC(killmonster);
  3542. BUILDIN_FUNC(killmonsterall);
  3543. BUILDIN_FUNC(clone);
  3544. BUILDIN_FUNC(doevent);
  3545. BUILDIN_FUNC(donpcevent);
  3546. BUILDIN_FUNC(addtimer);
  3547. BUILDIN_FUNC(deltimer);
  3548. BUILDIN_FUNC(addtimercount);
  3549. BUILDIN_FUNC(initnpctimer);
  3550. BUILDIN_FUNC(stopnpctimer);
  3551. BUILDIN_FUNC(startnpctimer);
  3552. BUILDIN_FUNC(setnpctimer);
  3553. BUILDIN_FUNC(getnpctimer);
  3554. BUILDIN_FUNC(attachnpctimer); // [celest]
  3555. BUILDIN_FUNC(detachnpctimer); // [celest]
  3556. BUILDIN_FUNC(playerattached); // [Skotlex]
  3557. BUILDIN_FUNC(announce);
  3558. BUILDIN_FUNC(mapannounce);
  3559. BUILDIN_FUNC(areaannounce);
  3560. BUILDIN_FUNC(getusers);
  3561. BUILDIN_FUNC(getmapguildusers);
  3562. BUILDIN_FUNC(getmapusers);
  3563. BUILDIN_FUNC(getareausers);
  3564. BUILDIN_FUNC(getareadropitem);
  3565. BUILDIN_FUNC(enablenpc);
  3566. BUILDIN_FUNC(disablenpc);
  3567. BUILDIN_FUNC(enablearena); // Added by RoVeRT
  3568. BUILDIN_FUNC(disablearena); // Added by RoVeRT
  3569. BUILDIN_FUNC(hideoffnpc);
  3570. BUILDIN_FUNC(hideonnpc);
  3571. BUILDIN_FUNC(sc_start);
  3572. BUILDIN_FUNC(sc_start2);
  3573. BUILDIN_FUNC(sc_start4);
  3574. BUILDIN_FUNC(sc_end);
  3575. BUILDIN_FUNC(getscrate);
  3576. BUILDIN_FUNC(debugmes);
  3577. BUILDIN_FUNC(catchpet);
  3578. BUILDIN_FUNC(birthpet);
  3579. BUILDIN_FUNC(resetlvl);
  3580. BUILDIN_FUNC(resetstatus);
  3581. BUILDIN_FUNC(resetskill);
  3582. BUILDIN_FUNC(skillpointcount);
  3583. BUILDIN_FUNC(changebase);
  3584. BUILDIN_FUNC(changesex);
  3585. BUILDIN_FUNC(waitingroom);
  3586. BUILDIN_FUNC(delwaitingroom);
  3587. BUILDIN_FUNC(waitingroomkickall);
  3588. BUILDIN_FUNC(enablewaitingroomevent);
  3589. BUILDIN_FUNC(disablewaitingroomevent);
  3590. BUILDIN_FUNC(getwaitingroomstate);
  3591. BUILDIN_FUNC(warpwaitingpc);
  3592. BUILDIN_FUNC(attachrid);
  3593. BUILDIN_FUNC(detachrid);
  3594. BUILDIN_FUNC(isloggedin);
  3595. BUILDIN_FUNC(setmapflagnosave);
  3596. BUILDIN_FUNC(setmapflag);
  3597. BUILDIN_FUNC(removemapflag);
  3598. BUILDIN_FUNC(pvpon);
  3599. BUILDIN_FUNC(pvpoff);
  3600. BUILDIN_FUNC(gvgon);
  3601. BUILDIN_FUNC(gvgoff);
  3602. BUILDIN_FUNC(emotion);
  3603. BUILDIN_FUNC(maprespawnguildid);
  3604. BUILDIN_FUNC(agitstart); // <Agit>
  3605. BUILDIN_FUNC(agitend);
  3606. BUILDIN_FUNC(agitcheck); // <Agitcheck>
  3607. BUILDIN_FUNC(flagemblem); // Flag Emblem
  3608. BUILDIN_FUNC(getcastlename);
  3609. BUILDIN_FUNC(getcastledata);
  3610. BUILDIN_FUNC(setcastledata);
  3611. BUILDIN_FUNC(requestguildinfo);
  3612. BUILDIN_FUNC(getequipcardcnt);
  3613. BUILDIN_FUNC(successremovecards);
  3614. BUILDIN_FUNC(failedremovecards);
  3615. BUILDIN_FUNC(marriage);
  3616. BUILDIN_FUNC(wedding_effect);
  3617. BUILDIN_FUNC(divorce);
  3618. BUILDIN_FUNC(ispartneron); // MouseJstr
  3619. BUILDIN_FUNC(getpartnerid); // MouseJstr
  3620. BUILDIN_FUNC(getchildid); // Skotlex
  3621. BUILDIN_FUNC(getmotherid); // Lupus
  3622. BUILDIN_FUNC(getfatherid); // Lupus
  3623. BUILDIN_FUNC(warppartner); // MouseJstr
  3624. BUILDIN_FUNC(getitemname);
  3625. BUILDIN_FUNC(getitemslots);
  3626. BUILDIN_FUNC(makepet);
  3627. BUILDIN_FUNC(getexp);
  3628. BUILDIN_FUNC(getinventorylist);
  3629. BUILDIN_FUNC(getskilllist);
  3630. BUILDIN_FUNC(clearitem);
  3631. BUILDIN_FUNC(classchange);
  3632. BUILDIN_FUNC(misceffect);
  3633. BUILDIN_FUNC(soundeffect);
  3634. BUILDIN_FUNC(soundeffectall);
  3635. BUILDIN_FUNC(setcastledata);
  3636. BUILDIN_FUNC(mapwarp);
  3637. BUILDIN_FUNC(inittimer);
  3638. BUILDIN_FUNC(stoptimer);
  3639. BUILDIN_FUNC(cmdothernpc);
  3640. BUILDIN_FUNC(mobcount);
  3641. BUILDIN_FUNC(strmobinfo); // Script for displaying mob info [Valaris]
  3642. BUILDIN_FUNC(guardian); // Script for displaying mob info [Valaris]
  3643. BUILDIN_FUNC(guardianinfo); // Script for displaying mob info [Valaris]
  3644. BUILDIN_FUNC(petskillbonus); // petskillbonus [Valaris]
  3645. BUILDIN_FUNC(petrecovery); // pet skill for curing status [Valaris]
  3646. BUILDIN_FUNC(petloot); // pet looting [Valaris]
  3647. BUILDIN_FUNC(petheal); // pet healing [Valaris]
  3648. //BUILDIN_FUNC(petmag); // pet magnificat [Valaris]
  3649. BUILDIN_FUNC(petskillattack); // pet skill attacks [Skotlex]
  3650. BUILDIN_FUNC(petskillattack2); // pet skill attacks [Skotlex]
  3651. BUILDIN_FUNC(petskillsupport); // pet support skill [Valaris]
  3652. BUILDIN_FUNC(skilleffect); // skill effects [Celest]
  3653. BUILDIN_FUNC(npcskilleffect); // skill effects for npcs [Valaris]
  3654. BUILDIN_FUNC(specialeffect); // special effect script [Valaris]
  3655. BUILDIN_FUNC(specialeffect2); // special effect script [Valaris]
  3656. BUILDIN_FUNC(nude); // nude [Valaris]
  3657. BUILDIN_FUNC(atcommand); // [MouseJstr]
  3658. BUILDIN_FUNC(charcommand); // [MouseJstr]
  3659. BUILDIN_FUNC(movenpc); // [MouseJstr]
  3660. BUILDIN_FUNC(message); // [MouseJstr]
  3661. BUILDIN_FUNC(npctalk); // [Valaris]
  3662. BUILDIN_FUNC(getlook); //Lorky [Lupus]
  3663. BUILDIN_FUNC(getsavepoint); //Lorky [Lupus]
  3664. BUILDIN_FUNC(npcspeed); // [Valaris]
  3665. BUILDIN_FUNC(npcwalkto); // [Valaris]
  3666. BUILDIN_FUNC(npcstop); // [Valaris]
  3667. BUILDIN_FUNC(getmapxy); //get map position for player/npc/pet/mob by Lorky [Lupus]
  3668. BUILDIN_FUNC(checkoption1); // [celest]
  3669. BUILDIN_FUNC(checkoption2); // [celest]
  3670. BUILDIN_FUNC(guildgetexp); // [celest]
  3671. BUILDIN_FUNC(guildchangegm); // [Skotlex]
  3672. BUILDIN_FUNC(logmes); // [Lupus]
  3673. BUILDIN_FUNC(summon); // [celest]
  3674. BUILDIN_FUNC(isnight); // [celest]
  3675. BUILDIN_FUNC(isday); // [celest]
  3676. BUILDIN_FUNC(isequipped); // [celest]
  3677. BUILDIN_FUNC(isequippedcnt); // [celest]
  3678. BUILDIN_FUNC(cardscnt); // [Lupus]
  3679. BUILDIN_FUNC(getrefine); // [celest]
  3680. BUILDIN_FUNC(adopt);
  3681. BUILDIN_FUNC(night);
  3682. BUILDIN_FUNC(day);
  3683. BUILDIN_FUNC(getusersname); //jA commands added [Lupus]
  3684. BUILDIN_FUNC(dispbottom);
  3685. BUILDIN_FUNC(recovery);
  3686. BUILDIN_FUNC(getpetinfo);
  3687. BUILDIN_FUNC(checkequipedcard);
  3688. BUILDIN_FUNC(globalmes);
  3689. BUILDIN_FUNC(jump_zero);
  3690. BUILDIN_FUNC(select);
  3691. BUILDIN_FUNC(prompt);
  3692. BUILDIN_FUNC(getmapmobs); //jA addition end
  3693. BUILDIN_FUNC(unequip); // unequip [Spectre]
  3694. BUILDIN_FUNC(getstrlen); //strlen [valaris]
  3695. BUILDIN_FUNC(charisalpha);//isalpha [valaris]
  3696. BUILDIN_FUNC(fakenpcname); // [Lance]
  3697. BUILDIN_FUNC(compare); // Lordalfa, to bring strstr to Scripting Engine
  3698. BUILDIN_FUNC(getiteminfo); //[Lupus] returns Items Buy / sell Price, etc info
  3699. BUILDIN_FUNC(setiteminfo); //[Lupus] set Items Buy / sell Price, etc info
  3700. BUILDIN_FUNC(getequipcardid); //[Lupus] returns card id from quipped item card slot N
  3701. // [zBuffer] List of mathematics commands --->
  3702. BUILDIN_FUNC(sqrt);
  3703. BUILDIN_FUNC(pow);
  3704. BUILDIN_FUNC(distance);
  3705. BUILDIN_FUNC(checkcell);
  3706. // <--- [zBuffer] List of mathematics commands
  3707. // [zBuffer] List of dynamic var commands --->
  3708. BUILDIN_FUNC(getd);
  3709. BUILDIN_FUNC(setd);
  3710. // <--- [zBuffer] List of dynamic var commands
  3711. BUILDIN_FUNC(petstat); // [Lance] Pet Stat Rq: Dubby
  3712. BUILDIN_FUNC(callshop); // [Skotlex]
  3713. BUILDIN_FUNC(npcshopitem); // [Lance]
  3714. BUILDIN_FUNC(npcshopadditem);
  3715. BUILDIN_FUNC(npcshopdelitem);
  3716. BUILDIN_FUNC(npcshopattach);
  3717. BUILDIN_FUNC(equip);
  3718. BUILDIN_FUNC(autoequip);
  3719. BUILDIN_FUNC(setbattleflag);
  3720. BUILDIN_FUNC(getbattleflag);
  3721. BUILDIN_FUNC(query_sql);
  3722. BUILDIN_FUNC(escape_sql);
  3723. BUILDIN_FUNC(atoi);
  3724. BUILDIN_FUNC(axtoi);
  3725. // [zBuffer] List of player cont commands --->
  3726. BUILDIN_FUNC(rid2name);
  3727. BUILDIN_FUNC(pcfollow);
  3728. BUILDIN_FUNC(pcstopfollow);
  3729. BUILDIN_FUNC(pcblockmove);
  3730. // <--- [zBuffer] List of player cont commands
  3731. // [zBuffer] List of mob control commands --->
  3732. BUILDIN_FUNC(mobspawn);
  3733. BUILDIN_FUNC(mobremove);
  3734. BUILDIN_FUNC(getmobdata);
  3735. BUILDIN_FUNC(setmobdata);
  3736. BUILDIN_FUNC(mobassist);
  3737. BUILDIN_FUNC(mobattach);
  3738. BUILDIN_FUNC(unitwalk);
  3739. BUILDIN_FUNC(unitkill);
  3740. BUILDIN_FUNC(unitwarp);
  3741. BUILDIN_FUNC(unitattack);
  3742. BUILDIN_FUNC(unitstop);
  3743. BUILDIN_FUNC(unittalk);
  3744. BUILDIN_FUNC(unitemote);
  3745. BUILDIN_FUNC(unitskilluseid); // originally by Qamera [celest]
  3746. BUILDIN_FUNC(unitskillusepos); // originally by Qamera [celest]
  3747. // <--- [zBuffer] List of mob control commands
  3748. BUILDIN_FUNC(sleep);
  3749. BUILDIN_FUNC(sleep2);
  3750. BUILDIN_FUNC(awake);
  3751. BUILDIN_FUNC(getvariableofnpc);
  3752. BUILDIN_FUNC(warpportal);
  3753. BUILDIN_FUNC(homunculus_evolution) ; //[orn]
  3754. BUILDIN_FUNC(eaclass);
  3755. BUILDIN_FUNC(roclass);
  3756. BUILDIN_FUNC(setitemscript);
  3757. BUILDIN_FUNC(disguise);
  3758. BUILDIN_FUNC(undisguise);
  3759. BUILDIN_FUNC(getmonsterinfo); // [Lupus]
  3760. BUILDIN_FUNC(checkvending); // check vending [Nab4]
  3761. BUILDIN_FUNC(checkchatting); // check chatting [Marka]
  3762. #ifdef PCRE_SUPPORT
  3763. BUILDIN_FUNC(defpattern); // MouseJstr
  3764. BUILDIN_FUNC(activatepset); // MouseJstr
  3765. BUILDIN_FUNC(deactivatepset); // MouseJstr
  3766. BUILDIN_FUNC(deletepset); // MouseJstr
  3767. #endif
  3768. struct script_function buildin_func[] = {
  3769. // NPC interaction
  3770. BUILDIN_DEF(mes,"s"),
  3771. BUILDIN_DEF(next,""),
  3772. BUILDIN_DEF(close,""),
  3773. BUILDIN_DEF(close2,""),
  3774. BUILDIN_DEF(menu,"sl*"),
  3775. BUILDIN_DEF(select,"s*"), //for future jA script compatibility
  3776. BUILDIN_DEF(prompt,"s*"),
  3777. //
  3778. BUILDIN_DEF(goto,"l"),
  3779. BUILDIN_DEF(callsub,"i*"),
  3780. BUILDIN_DEF(callfunc,"s*"),
  3781. BUILDIN_DEF(return,"?"),
  3782. BUILDIN_DEF(getarg,"i?"),
  3783. BUILDIN_DEF(jobchange,"i*"),
  3784. BUILDIN_DEF(jobname,"i"),
  3785. BUILDIN_DEF(input,"v"),
  3786. BUILDIN_DEF(warp,"sii"),
  3787. BUILDIN_DEF(areawarp,"siiiisii"),
  3788. BUILDIN_DEF(warpchar,"siii"), // [LuzZza]
  3789. BUILDIN_DEF(warpparty,"siii"), // [Fredzilla]
  3790. BUILDIN_DEF(warpguild,"siii"), // [Fredzilla]
  3791. BUILDIN_DEF(setlook,"ii"),
  3792. BUILDIN_DEF(set,"ii"),
  3793. BUILDIN_DEF(setarray,"rv*"),
  3794. BUILDIN_DEF(cleararray,"rvi"),
  3795. BUILDIN_DEF(copyarray,"rri"),
  3796. BUILDIN_DEF(getarraysize,"r"),
  3797. BUILDIN_DEF(deletearray,"r?"),
  3798. BUILDIN_DEF(getelementofarray,"ri"),
  3799. BUILDIN_DEF(getitem,"vi?"),
  3800. BUILDIN_DEF(getitem2,"iiiiiiiii*"),
  3801. BUILDIN_DEF(getnameditem,"is"),
  3802. BUILDIN_DEF2(grouprandomitem,"groupranditem","i"),
  3803. BUILDIN_DEF(makeitem,"iisii"),
  3804. BUILDIN_DEF(delitem,"ii"),
  3805. BUILDIN_DEF(delitem2,"iiiiiiiii"),
  3806. BUILDIN_DEF2(enableitemuse,"enable_items",""),
  3807. BUILDIN_DEF2(disableitemuse,"disable_items",""),
  3808. BUILDIN_DEF(cutin,"si"),
  3809. BUILDIN_DEF(viewpoint,"iiiii"),
  3810. BUILDIN_DEF(heal,"ii"),
  3811. BUILDIN_DEF(itemheal,"ii"),
  3812. BUILDIN_DEF(percentheal,"ii"),
  3813. BUILDIN_DEF(rand,"i?"),
  3814. BUILDIN_DEF(countitem,"i"),
  3815. BUILDIN_DEF(countitem2,"iiiiiiii"),
  3816. BUILDIN_DEF(checkweight,"ii"),
  3817. BUILDIN_DEF(readparam,"i*"),
  3818. BUILDIN_DEF(getcharid,"i*"),
  3819. BUILDIN_DEF(getpartyname,"i"),
  3820. BUILDIN_DEF(getpartymember,"i*"),
  3821. BUILDIN_DEF(getpartyleader,"i?"),
  3822. BUILDIN_DEF(getguildname,"i"),
  3823. BUILDIN_DEF(getguildmaster,"i"),
  3824. BUILDIN_DEF(getguildmasterid,"i"),
  3825. BUILDIN_DEF(strcharinfo,"i"),
  3826. BUILDIN_DEF(getequipid,"i"),
  3827. BUILDIN_DEF(getequipname,"i"),
  3828. BUILDIN_DEF(getbrokenid,"i"), // [Valaris]
  3829. BUILDIN_DEF(repair,"i"), // [Valaris]
  3830. BUILDIN_DEF(getequipisequiped,"i"),
  3831. BUILDIN_DEF(getequipisenableref,"i"),
  3832. BUILDIN_DEF(getequipisidentify,"i"),
  3833. BUILDIN_DEF(getequiprefinerycnt,"i"),
  3834. BUILDIN_DEF(getequipweaponlv,"i"),
  3835. BUILDIN_DEF(getequippercentrefinery,"i"),
  3836. BUILDIN_DEF(successrefitem,"i"),
  3837. BUILDIN_DEF(failedrefitem,"i"),
  3838. BUILDIN_DEF(statusup,"i"),
  3839. BUILDIN_DEF(statusup2,"ii"),
  3840. BUILDIN_DEF(bonus,"ii"),
  3841. BUILDIN_DEF2(bonus,"bonus2","iii"),
  3842. BUILDIN_DEF2(bonus,"bonus3","iiii"),
  3843. BUILDIN_DEF2(bonus,"bonus4","iiiii"),
  3844. BUILDIN_DEF2(bonus,"bonus5","iiiiii"),
  3845. BUILDIN_DEF(skill,"ii?"),
  3846. BUILDIN_DEF(addtoskill,"ii?"), // [Valaris]
  3847. BUILDIN_DEF(guildskill,"ii"),
  3848. BUILDIN_DEF(getskilllv,"i"),
  3849. BUILDIN_DEF(getgdskilllv,"ii"),
  3850. BUILDIN_DEF(basicskillcheck,""),
  3851. BUILDIN_DEF(getgmlevel,""),
  3852. BUILDIN_DEF(end,""),
  3853. // BUILDIN_DEF2(end,"break",""), this might confuse advanced scripting support [Eoe]
  3854. BUILDIN_DEF(checkoption,"i"),
  3855. BUILDIN_DEF(setoption,"i?"),
  3856. BUILDIN_DEF(setcart,"?"),
  3857. BUILDIN_DEF(checkcart,""),
  3858. BUILDIN_DEF(setfalcon,"?"),
  3859. BUILDIN_DEF(checkfalcon,""),
  3860. BUILDIN_DEF(setriding,"?"),
  3861. BUILDIN_DEF(checkriding,""),
  3862. BUILDIN_DEF2(savepoint,"save","sii"),
  3863. BUILDIN_DEF(savepoint,"sii"),
  3864. BUILDIN_DEF(gettimetick,"i"),
  3865. BUILDIN_DEF(gettime,"i"),
  3866. BUILDIN_DEF(gettimestr,"si"),
  3867. BUILDIN_DEF(openstorage,""),
  3868. BUILDIN_DEF(guildopenstorage,"*"),
  3869. BUILDIN_DEF(itemskill,"ii"),
  3870. BUILDIN_DEF(produce,"i"),
  3871. BUILDIN_DEF(monster,"siisii*"),
  3872. BUILDIN_DEF(areamonster,"siiiisii*"),
  3873. BUILDIN_DEF(killmonster,"ss"),
  3874. BUILDIN_DEF(killmonsterall,"s"),
  3875. BUILDIN_DEF(clone,"siisi*"),
  3876. BUILDIN_DEF(doevent,"s"),
  3877. BUILDIN_DEF(donpcevent,"s"),
  3878. BUILDIN_DEF(addtimer,"is"),
  3879. BUILDIN_DEF(deltimer,"s"),
  3880. BUILDIN_DEF(addtimercount,"si"),
  3881. BUILDIN_DEF(initnpctimer,"??"),
  3882. BUILDIN_DEF(stopnpctimer,"??"),
  3883. BUILDIN_DEF(startnpctimer,"??"),
  3884. BUILDIN_DEF(setnpctimer,"i?"),
  3885. BUILDIN_DEF(getnpctimer,"i?"),
  3886. BUILDIN_DEF(attachnpctimer,"?"), // attached the player id to the npc timer [Celest]
  3887. BUILDIN_DEF(detachnpctimer,"?"), // detached the player id from the npc timer [Celest]
  3888. BUILDIN_DEF(playerattached,""), // returns id of the current attached player. [Skotlex]
  3889. BUILDIN_DEF(announce,"si*"),
  3890. BUILDIN_DEF(mapannounce,"ssi*"),
  3891. BUILDIN_DEF(areaannounce,"siiiisi*"),
  3892. BUILDIN_DEF(getusers,"i"),
  3893. BUILDIN_DEF(getmapguildusers,"si"),
  3894. BUILDIN_DEF(getmapusers,"s"),
  3895. BUILDIN_DEF(getareausers,"siiii"),
  3896. BUILDIN_DEF(getareadropitem,"siiiii"),
  3897. BUILDIN_DEF(enablenpc,"s"),
  3898. BUILDIN_DEF(disablenpc,"s"),
  3899. BUILDIN_DEF(enablearena,""), // Added by RoVeRT
  3900. BUILDIN_DEF(disablearena,""), // Added by RoVeRT
  3901. BUILDIN_DEF(hideoffnpc,"s"),
  3902. BUILDIN_DEF(hideonnpc,"s"),
  3903. BUILDIN_DEF(sc_start,"iii?"),
  3904. BUILDIN_DEF(sc_start2,"iiii?"),
  3905. BUILDIN_DEF(sc_start4,"iiiiii?"),
  3906. BUILDIN_DEF(sc_end,"i?"),
  3907. BUILDIN_DEF(getscrate,"ii*"),
  3908. BUILDIN_DEF(debugmes,"s"),
  3909. BUILDIN_DEF2(catchpet,"pet","i"),
  3910. BUILDIN_DEF2(birthpet,"bpet",""),
  3911. BUILDIN_DEF(resetlvl,"i"),
  3912. BUILDIN_DEF(resetstatus,""),
  3913. BUILDIN_DEF(resetskill,""),
  3914. BUILDIN_DEF(skillpointcount,""),
  3915. BUILDIN_DEF(changebase,"i"),
  3916. BUILDIN_DEF(changesex,""),
  3917. BUILDIN_DEF(waitingroom,"si??"),
  3918. BUILDIN_DEF(delwaitingroom,"?"),
  3919. BUILDIN_DEF2(waitingroomkickall,"kickwaitingroomall","?"),
  3920. BUILDIN_DEF(enablewaitingroomevent,"?"),
  3921. BUILDIN_DEF(disablewaitingroomevent,"?"),
  3922. BUILDIN_DEF(getwaitingroomstate,"i?"),
  3923. BUILDIN_DEF(warpwaitingpc,"sii?"),
  3924. BUILDIN_DEF(attachrid,"i"),
  3925. BUILDIN_DEF(detachrid,""),
  3926. BUILDIN_DEF(isloggedin,"i?"),
  3927. BUILDIN_DEF(setmapflagnosave,"ssii"),
  3928. BUILDIN_DEF(setmapflag,"si*"),
  3929. BUILDIN_DEF(removemapflag,"si"),
  3930. BUILDIN_DEF(pvpon,"s"),
  3931. BUILDIN_DEF(pvpoff,"s"),
  3932. BUILDIN_DEF(gvgon,"s"),
  3933. BUILDIN_DEF(gvgoff,"s"),
  3934. BUILDIN_DEF(emotion,"i*"),
  3935. BUILDIN_DEF(maprespawnguildid,"sii"),
  3936. BUILDIN_DEF(agitstart,""), // <Agit>
  3937. BUILDIN_DEF(agitend,""),
  3938. BUILDIN_DEF(agitcheck,""), // <Agitcheck>
  3939. BUILDIN_DEF(flagemblem,"i"), // Flag Emblem
  3940. BUILDIN_DEF(getcastlename,"s"),
  3941. BUILDIN_DEF(getcastledata,"si*"),
  3942. BUILDIN_DEF(setcastledata,"sii"),
  3943. BUILDIN_DEF(requestguildinfo,"i*"),
  3944. BUILDIN_DEF(getequipcardcnt,"i"),
  3945. BUILDIN_DEF(successremovecards,"i"),
  3946. BUILDIN_DEF(failedremovecards,"ii"),
  3947. BUILDIN_DEF(marriage,"s"),
  3948. BUILDIN_DEF2(wedding_effect,"wedding",""),
  3949. BUILDIN_DEF(divorce,""),
  3950. BUILDIN_DEF(ispartneron,""),
  3951. BUILDIN_DEF(getpartnerid,""),
  3952. BUILDIN_DEF(getchildid,""),
  3953. BUILDIN_DEF(getmotherid,""),
  3954. BUILDIN_DEF(getfatherid,""),
  3955. BUILDIN_DEF(warppartner,"sii"),
  3956. BUILDIN_DEF(getitemname,"i"),
  3957. BUILDIN_DEF(getitemslots,"i"),
  3958. BUILDIN_DEF(makepet,"i"),
  3959. BUILDIN_DEF(getexp,"ii"),
  3960. BUILDIN_DEF(getinventorylist,""),
  3961. BUILDIN_DEF(getskilllist,""),
  3962. BUILDIN_DEF(clearitem,""),
  3963. BUILDIN_DEF(classchange,"ii"),
  3964. BUILDIN_DEF(misceffect,"i"),
  3965. BUILDIN_DEF(soundeffect,"si"),
  3966. BUILDIN_DEF(soundeffectall,"si*"), // SoundEffectAll [Codemaster]
  3967. BUILDIN_DEF(strmobinfo,"ii"), // display mob data [Valaris]
  3968. BUILDIN_DEF(guardian,"siisi??"), // summon guardians
  3969. BUILDIN_DEF(guardianinfo,"i"), // display guardian data [Valaris]
  3970. BUILDIN_DEF(petskillbonus,"iiii"), // [Valaris]
  3971. BUILDIN_DEF(petrecovery,"ii"), // [Valaris]
  3972. BUILDIN_DEF(petloot,"i"), // [Valaris]
  3973. BUILDIN_DEF(petheal,"iiii"), // [Valaris]
  3974. // BUILDIN_DEF(petmag,"iiii"), // [Valaris]
  3975. BUILDIN_DEF(petskillattack,"iiii"), // [Skotlex]
  3976. BUILDIN_DEF(petskillattack2,"iiiii"), // [Valaris]
  3977. BUILDIN_DEF(petskillsupport,"iiiii"), // [Skotlex]
  3978. BUILDIN_DEF(skilleffect,"ii"), // skill effect [Celest]
  3979. BUILDIN_DEF(npcskilleffect,"iiii"), // npc skill effect [Valaris]
  3980. BUILDIN_DEF(specialeffect,"i*"), // npc skill effect [Valaris]
  3981. BUILDIN_DEF(specialeffect2,"i*"), // skill effect on players[Valaris]
  3982. BUILDIN_DEF(nude,""), // nude command [Valaris]
  3983. BUILDIN_DEF(mapwarp,"ssii*"), // Added by RoVeRT
  3984. BUILDIN_DEF(inittimer,""),
  3985. BUILDIN_DEF(stoptimer,""),
  3986. BUILDIN_DEF(cmdothernpc,"ss"),
  3987. BUILDIN_DEF(atcommand,"*"), // [MouseJstr]
  3988. BUILDIN_DEF(charcommand,"*"), // [MouseJstr]
  3989. BUILDIN_DEF(movenpc,"sii"), // [MouseJstr]
  3990. BUILDIN_DEF(message,"s*"), // [MouseJstr]
  3991. BUILDIN_DEF(npctalk,"*"), // [Valaris]
  3992. BUILDIN_DEF(mobcount,"ss"),
  3993. BUILDIN_DEF(getlook,"i"),
  3994. BUILDIN_DEF(getsavepoint,"i"),
  3995. BUILDIN_DEF(npcspeed,"i"), // [Valaris]
  3996. BUILDIN_DEF(npcwalkto,"ii"), // [Valaris]
  3997. BUILDIN_DEF(npcstop,""), // [Valaris]
  3998. BUILDIN_DEF(getmapxy,"siii*"), //by Lorky [Lupus]
  3999. BUILDIN_DEF(checkoption1,"i"),
  4000. BUILDIN_DEF(checkoption2,"i"),
  4001. BUILDIN_DEF(guildgetexp,"i"),
  4002. BUILDIN_DEF(guildchangegm,"is"),
  4003. BUILDIN_DEF(logmes,"s"), //this command actls as MES but rints info into LOG file either SQL/TXT [Lupus]
  4004. BUILDIN_DEF(summon,"si*"), // summons a slave monster [Celest]
  4005. BUILDIN_DEF(isnight,""), // check whether it is night time [Celest]
  4006. BUILDIN_DEF(isday,""), // check whether it is day time [Celest]
  4007. BUILDIN_DEF(isequipped,"i*"), // check whether another item/card has been equipped [Celest]
  4008. BUILDIN_DEF(isequippedcnt,"i*"), // check how many items/cards are being equipped [Celest]
  4009. BUILDIN_DEF(cardscnt,"i*"), // check how many items/cards are being equipped in the same arm [Lupus]
  4010. BUILDIN_DEF(getrefine,"*"), // returns the refined number of the current item, or an item with index specified [celest]
  4011. BUILDIN_DEF(adopt,"sss"), // allows 2 parents to adopt a child
  4012. BUILDIN_DEF(night,""), // sets the server to night time
  4013. BUILDIN_DEF(day,""), // sets the server to day time
  4014. #ifdef PCRE_SUPPORT
  4015. BUILDIN_DEF(defpattern,"iss"), // Define pattern to listen for [MouseJstr]
  4016. BUILDIN_DEF(activatepset,"i"), // Activate a pattern set [MouseJstr]
  4017. BUILDIN_DEF(deactivatepset,"i"), // Deactive a pattern set [MouseJstr]
  4018. BUILDIN_DEF(deletepset,"i"), // Delete a pattern set [MouseJstr]
  4019. #endif
  4020. BUILDIN_DEF(dispbottom,"s"), //added from jA [Lupus]
  4021. BUILDIN_DEF(getusersname,"*"),
  4022. BUILDIN_DEF(recovery,""),
  4023. BUILDIN_DEF(getpetinfo,"i"),
  4024. BUILDIN_DEF(checkequipedcard,"i"),
  4025. BUILDIN_DEF(jump_zero,"ii"), //for future jA script compatibility
  4026. BUILDIN_DEF(globalmes,"s*"),
  4027. BUILDIN_DEF(getmapmobs,"s"), //end jA addition
  4028. BUILDIN_DEF(unequip,"i"), // unequip command [Spectre]
  4029. BUILDIN_DEF(getstrlen,"s"), //strlen [Valaris]
  4030. BUILDIN_DEF(charisalpha,"si"), //isalpha [Valaris]
  4031. BUILDIN_DEF(fakenpcname,"ssi"), // [Lance]
  4032. BUILDIN_DEF(compare,"ss"), // Lordalfa - To bring strstr to scripting Engine.
  4033. BUILDIN_DEF(getiteminfo,"ii"), //[Lupus] returns Items Buy / sell Price, etc info
  4034. BUILDIN_DEF(setiteminfo,"iii"), //[Lupus] set Items Buy / sell Price, etc info
  4035. BUILDIN_DEF(getequipcardid,"ii"), //[Lupus] returns CARD ID or other info from CARD slot N of equipped item
  4036. // [zBuffer] List of mathematics commands --->
  4037. BUILDIN_DEF(sqrt,"i"),
  4038. BUILDIN_DEF(pow,"ii"),
  4039. BUILDIN_DEF(distance,"iiii"),
  4040. BUILDIN_DEF(checkcell,"siii"),
  4041. // <--- [zBuffer] List of mathematics commands
  4042. // [zBuffer] List of dynamic var commands --->
  4043. BUILDIN_DEF(getd,"*"),
  4044. BUILDIN_DEF(setd,"*"),
  4045. // <--- [zBuffer] List of dynamic var commands
  4046. BUILDIN_DEF(petstat,"i"),
  4047. BUILDIN_DEF(callshop,"si"), // [Skotlex]
  4048. BUILDIN_DEF(npcshopitem,"sii*"), // [Lance]
  4049. BUILDIN_DEF(npcshopadditem,"sii*"),
  4050. BUILDIN_DEF(npcshopdelitem,"si*"),
  4051. BUILDIN_DEF(npcshopattach,"s?"),
  4052. BUILDIN_DEF(equip,"i"),
  4053. BUILDIN_DEF(autoequip,"ii"),
  4054. BUILDIN_DEF(setbattleflag,"ss"),
  4055. BUILDIN_DEF(getbattleflag,"s"),
  4056. BUILDIN_DEF(setitemscript,"is*"), //Set NEW item bonus script. Lupus
  4057. BUILDIN_DEF(disguise,"i"), //disguise player. Lupus
  4058. BUILDIN_DEF(undisguise,"*"), //undisguise player. Lupus
  4059. BUILDIN_DEF(getmonsterinfo,"ii"), //Lupus
  4060. BUILDIN_DEF(axtoi,"s"),
  4061. BUILDIN_DEF(query_sql,"s*"),
  4062. BUILDIN_DEF(escape_sql,"s"),
  4063. BUILDIN_DEF(atoi,"s"),
  4064. // [zBuffer] List of player cont commands --->
  4065. BUILDIN_DEF(rid2name,"i"),
  4066. BUILDIN_DEF(pcfollow,"ii"),
  4067. BUILDIN_DEF(pcstopfollow,"i"),
  4068. BUILDIN_DEF(pcblockmove,"ii"),
  4069. // <--- [zBuffer] List of player cont commands
  4070. // [zBuffer] List of mob control commands --->
  4071. BUILDIN_DEF(mobspawn,"*"),
  4072. BUILDIN_DEF(mobremove,"i"),
  4073. BUILDIN_DEF(getmobdata,"i*"),
  4074. BUILDIN_DEF(setmobdata,"iii"),
  4075. BUILDIN_DEF(mobassist,"i?"),
  4076. BUILDIN_DEF(mobattach,"i?"),
  4077. BUILDIN_DEF(unitwalk,"ii?"),
  4078. BUILDIN_DEF(unitkill,"i"),
  4079. BUILDIN_DEF(unitwarp,"isii"),
  4080. BUILDIN_DEF(unitattack,"iv?"),
  4081. BUILDIN_DEF(unitstop,"i"),
  4082. BUILDIN_DEF(unittalk,"is"),
  4083. BUILDIN_DEF(unitemote,"ii"),
  4084. BUILDIN_DEF(unitskilluseid,"iii?"), // originally by Qamera [Celest]
  4085. BUILDIN_DEF(unitskillusepos,"iiiii"), // [Celest]
  4086. // <--- [zBuffer] List of mob control commands
  4087. BUILDIN_DEF(sleep,"i"),
  4088. BUILDIN_DEF(sleep2,"i"),
  4089. BUILDIN_DEF(awake,"s"),
  4090. BUILDIN_DEF(getvariableofnpc,"rs"),
  4091. BUILDIN_DEF(warpportal,"iisii"),
  4092. BUILDIN_DEF2(homunculus_evolution,"homevolution",""), //[orn]
  4093. BUILDIN_DEF(eaclass,"*"), //[Skotlex]
  4094. BUILDIN_DEF(roclass,"i*"), //[Skotlex]
  4095. BUILDIN_DEF(checkvending,"*"),
  4096. BUILDIN_DEF(checkchatting,"*"),
  4097. {NULL,NULL,NULL},
  4098. };
  4099. /////////////////////////////////////////////////////////////////////
  4100. // NPC interaction
  4101. //
  4102. /// Appends a message to the npc dialog.
  4103. /// If a dialog doesn't exist yet, one is created.
  4104. ///
  4105. /// mes "<message>";
  4106. BUILDIN_FUNC(mes)
  4107. {
  4108. TBL_PC* sd = script_rid2sd(st);
  4109. if( sd == NULL )
  4110. return 1;
  4111. clif_scriptmes(sd, st->oid, script_getstr(st, 2));
  4112. return 0;
  4113. }
  4114. /// Displays the button 'next' in the npc dialog.
  4115. /// The dialog text is cleared and the script continues when the button is pressed.
  4116. ///
  4117. /// next;
  4118. BUILDIN_FUNC(next)
  4119. {
  4120. TBL_PC* sd;
  4121. sd = script_rid2sd(st);
  4122. if( sd == NULL )
  4123. return 1;
  4124. st->state = STOP;
  4125. clif_scriptnext(sd, st->oid);
  4126. return 0;
  4127. }
  4128. /// Ends the script and displays the button 'close' on the npc dialog.
  4129. /// The dialog is closed when the button is pressed.
  4130. ///
  4131. /// close;
  4132. BUILDIN_FUNC(close)
  4133. {
  4134. TBL_PC* sd;
  4135. sd = script_rid2sd(st);
  4136. if( sd == NULL )
  4137. return 1;
  4138. st->state = END;
  4139. clif_scriptclose(sd, st->oid);
  4140. return 0;
  4141. }
  4142. /// Displays the button 'close' on the npc dialog.
  4143. /// The dialog is closed and the script continues when the button is pressed.
  4144. ///
  4145. /// close2;
  4146. BUILDIN_FUNC(close2)
  4147. {
  4148. TBL_PC* sd;
  4149. sd = script_rid2sd(st);
  4150. if( sd == NULL )
  4151. return 1;
  4152. st->state = STOP;
  4153. clif_scriptclose(sd, st->oid);
  4154. return 0;
  4155. }
  4156. /// Counts the number of valid and total number of options in 'str'
  4157. /// If max_count > 0 the counting stops when that valid option is reached
  4158. /// total is incremented for each option (NULL is supported)
  4159. static int menu_countoptions(const char* str, int max_count, int* total)
  4160. {
  4161. int count = 0;
  4162. int bogus_total;
  4163. if( total == NULL )
  4164. total = &bogus_total;
  4165. ++(*total);
  4166. // initial empty options
  4167. while( *str == ':' )
  4168. {
  4169. ++str;
  4170. ++(*total);
  4171. }
  4172. // count menu options
  4173. while( *str != '\0' )
  4174. {
  4175. ++count;
  4176. --max_count;
  4177. if( max_count == 0 )
  4178. break;
  4179. while( *str != ':' && *str != '\0' )
  4180. ++str;
  4181. while( *str == ':' )
  4182. {
  4183. ++str;
  4184. ++(*total);
  4185. }
  4186. }
  4187. return count;
  4188. }
  4189. /// Displays a menu with options and goes to the target label.
  4190. /// The script is stopped if cancel is pressed.
  4191. /// Options with no text are not displayed in the client.
  4192. ///
  4193. /// Options can be grouped together, separated by the character ':' in the text:
  4194. /// ex: menu "A:B:C",L_target;
  4195. /// All these options go to the specified target label.
  4196. ///
  4197. /// The index of the selected option is put in the variable @menu.
  4198. /// Indexes start with 1 and are consistent with grouped and empty options.
  4199. /// ex: menu "A::B",-,"",L_Impossible,"C",-;
  4200. /// // displays "A", "B" and "C", corresponding to indexes 1, 3 and 5
  4201. ///
  4202. /// NOTE: the client closes the npc dialog when cancel is pressed
  4203. ///
  4204. /// menu "<option_text>",<target_label>{,"<option_text>",<target_label>,...};
  4205. BUILDIN_FUNC(menu)
  4206. {
  4207. int i;
  4208. const char* text;
  4209. TBL_PC* sd;
  4210. sd = script_rid2sd(st);
  4211. if( sd == NULL )
  4212. return 1;
  4213. // TODO detect multiple scripts waiting for input at the same time, and what to do when that happens
  4214. if( sd->state.menu_or_input == 0 )
  4215. {
  4216. struct StringBuf buf;
  4217. struct script_data* data;
  4218. if( script_lastdata(st) % 2 == 0 )
  4219. {// argument count is not even (1st argument is at index 2)
  4220. ShowError("script:menu: illegal number of arguments (%d).\n", (script_lastdata(st) - 1));
  4221. st->state = END;
  4222. return 1;
  4223. }
  4224. StringBuf_Init(&buf);
  4225. for( i = 2, sd->npc_menu = 0; i < script_lastdata(st); i += 2 )
  4226. {
  4227. // menu options
  4228. text = script_getstr(st, i);
  4229. // target label
  4230. data = script_getdata(st, i+1);
  4231. if( !data_islabel(data) )
  4232. {// not a label
  4233. StringBuf_Destroy(&buf);
  4234. ShowError("script:menu: argument #%d (from 1) is not a label or label not found.\n", i);
  4235. script_reportdata(data);
  4236. st->state = END;
  4237. return 1;
  4238. }
  4239. // append option(s)
  4240. if( text[0] == '\0' )
  4241. continue;// empty string, ignore
  4242. if( sd->npc_menu > 0 )
  4243. StringBuf_AppendStr(&buf, ":");
  4244. StringBuf_AppendStr(&buf, text);
  4245. sd->npc_menu += menu_countoptions(text, 0, NULL);
  4246. }
  4247. st->state = RERUNLINE;
  4248. sd->state.menu_or_input = 1;
  4249. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4250. StringBuf_Destroy(&buf);
  4251. //TODO what's the maximum number of options that can be displayed and/or received? -> give warning
  4252. }
  4253. else if( sd->npc_menu == 0xff )
  4254. {// Cancel was pressed
  4255. sd->state.menu_or_input = 0;
  4256. st->state = END;
  4257. }
  4258. else
  4259. {// goto target label
  4260. int menu = 0;
  4261. sd->state.menu_or_input = 0;
  4262. if( sd->npc_menu <= 0 )
  4263. {
  4264. ShowDebug("script:menu: unexpected selection (%d)\n", sd->npc_menu);
  4265. st->state = END;
  4266. return 1;
  4267. }
  4268. // get target label
  4269. for( i = 2; i < script_lastdata(st); i += 2 )
  4270. {
  4271. text = script_getstr(st, i);
  4272. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4273. if( sd->npc_menu <= 0 )
  4274. break;// entry found
  4275. }
  4276. if( sd->npc_menu > 0 )
  4277. {// Invalid selection
  4278. ShowDebug("script:menu: selection is out of range (%d pairs are missing?) - please report this\n", sd->npc_menu);
  4279. st->state = END;
  4280. return 1;
  4281. }
  4282. if( !data_islabel(script_getdata(st, i + 1)) )
  4283. {// TODO remove this temporary crash-prevention code (fallback for multiple scripts requesting user input)
  4284. ShowError("script:menu: unexpected data in label argument\n");
  4285. script_reportdata(script_getdata(st, i + 1));
  4286. st->state = END;
  4287. return 1;
  4288. }
  4289. pc_setreg(sd, add_str("@menu"), menu);
  4290. st->pos = script_getnum(st, i + 1);
  4291. st->state = GOTO;
  4292. }
  4293. return 0;
  4294. }
  4295. /// Displays a menu with options and returns the selected option.
  4296. /// Behaves like 'menu' without the target labels.
  4297. ///
  4298. /// select(<option_text>{,<option_text>,...}) -> <selected_option>
  4299. ///
  4300. /// @see menu
  4301. BUILDIN_FUNC(select)
  4302. {
  4303. int i;
  4304. const char* text;
  4305. TBL_PC* sd;
  4306. sd = script_rid2sd(st);
  4307. if( sd == NULL )
  4308. return 1;
  4309. if( sd->state.menu_or_input == 0 )
  4310. {
  4311. struct StringBuf buf;
  4312. StringBuf_Init(&buf);
  4313. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  4314. {
  4315. text = script_getstr(st, i);
  4316. if( sd->npc_menu > 0 )
  4317. StringBuf_AppendStr(&buf, ":");
  4318. StringBuf_AppendStr(&buf, text);
  4319. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  4320. }
  4321. st->state = RERUNLINE;
  4322. sd->state.menu_or_input = 1;
  4323. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4324. StringBuf_Destroy(&buf);
  4325. }
  4326. else if( sd->npc_menu == 0xff )
  4327. {// Cancel was pressed
  4328. sd->state.menu_or_input = 0;
  4329. st->state = END;
  4330. }
  4331. else
  4332. {// return selected option
  4333. int menu = 0;
  4334. sd->state.menu_or_input = 0;
  4335. for( i = 2; i <= script_lastdata(st); ++i )
  4336. {
  4337. text = script_getstr(st, i);
  4338. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4339. if( sd->npc_menu <= 0 )
  4340. break;// entry found
  4341. }
  4342. pc_setreg(sd, add_str("@menu"), menu);
  4343. script_pushint(st, menu);
  4344. }
  4345. return 0;
  4346. }
  4347. /// Displays a menu with options and returns the selected option.
  4348. /// Behaves like 'menu' without the target labels, except when cancel is
  4349. /// pressed.
  4350. /// When cancel is pressed, the script continues and 255 is returned.
  4351. ///
  4352. /// prompt(<option_text>{,<option_text>,...}) -> <selected_option>
  4353. ///
  4354. /// @see menu
  4355. BUILDIN_FUNC(prompt)
  4356. {
  4357. int i;
  4358. const char *text;
  4359. TBL_PC* sd;
  4360. sd = script_rid2sd(st);
  4361. if( sd == NULL )
  4362. return 1;
  4363. if( sd->state.menu_or_input == 0 )
  4364. {
  4365. struct StringBuf buf;
  4366. StringBuf_Init(&buf);
  4367. for( i = 2, sd->npc_menu = 0; i <= script_lastdata(st); ++i )
  4368. {
  4369. text = script_getstr(st, i);
  4370. if( sd->npc_menu > 0 )
  4371. StringBuf_AppendStr(&buf, ":");
  4372. StringBuf_AppendStr(&buf, text);
  4373. sd ->npc_menu += menu_countoptions(text, 0, NULL);
  4374. }
  4375. st->state = RERUNLINE;
  4376. sd->state.menu_or_input = 1;
  4377. clif_scriptmenu(sd, st->oid, StringBuf_Value(&buf));
  4378. StringBuf_Destroy(&buf);
  4379. }
  4380. else if( sd->npc_menu == 0xff )
  4381. {// Cancel was pressed
  4382. sd->state.menu_or_input = 0;
  4383. pc_setreg(sd, add_str("@menu"), 0xff);
  4384. script_pushint(st, 0xff);
  4385. }
  4386. else
  4387. {// return selected option
  4388. int menu = 0;
  4389. sd->state.menu_or_input = 0;
  4390. for( i = 2; i <= script_lastdata(st); ++i )
  4391. {
  4392. text = script_getstr(st, i);
  4393. sd->npc_menu -= menu_countoptions(text, sd->npc_menu, &menu);
  4394. if( sd->npc_menu <= 0 )
  4395. break;// entry found
  4396. }
  4397. pc_setreg(sd, add_str("@menu"), menu);
  4398. script_pushint(st, menu);
  4399. }
  4400. return 0;
  4401. }
  4402. /////////////////////////////////////////////////////////////////////
  4403. // ...
  4404. //
  4405. /// Jumps to the target script label.
  4406. ///
  4407. /// goto <label>;
  4408. BUILDIN_FUNC(goto)
  4409. {
  4410. if( !data_islabel(script_getdata(st,2)) )
  4411. {
  4412. ShowError("script:goto: not a label\n");
  4413. script_reportdata(script_getdata(st,2));
  4414. st->state = END;
  4415. return 1;
  4416. }
  4417. st->pos = script_getnum(st,2);
  4418. st->state = GOTO;
  4419. return 0;
  4420. }
  4421. /*==========================================
  4422. * ユーザー定義関数の呼び出し
  4423. *------------------------------------------*/
  4424. BUILDIN_FUNC(callfunc)
  4425. {
  4426. struct script_code *scr, *oldscr;
  4427. const char *str=script_getstr(st,2);
  4428. if( (scr = strdb_get(userfunc_db, str)) ){
  4429. int i,j;
  4430. struct linkdb_node **oldval = st->stack->var_function;
  4431. for(i=st->start+3,j=0;i<st->end;i++,j++)
  4432. push_copy(st->stack,i);
  4433. script_pushint(st,j); // 引数の数をプッシュ
  4434. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  4435. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  4436. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  4437. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  4438. oldscr = st->script;
  4439. st->pos=0;
  4440. st->script=scr;
  4441. st->stack->defsp=st->start+5+j;
  4442. st->state=GOTO;
  4443. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  4444. // ' 変数の引き継ぎ
  4445. for(i = 0; i < j; i++) {
  4446. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  4447. if( data_isreference(s) && !s->ref ) {
  4448. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  4449. // '@ 変数の引き継ぎ
  4450. if( name[0] == '.' && name[1] == '@' ) {
  4451. s->ref = oldval;
  4452. } else if( name[0] == '.' ) {
  4453. s->ref = &oldscr->script_vars;
  4454. }
  4455. }
  4456. }
  4457. }else{
  4458. ShowError("script:callfunc: function not found! [%s]\n",str);
  4459. st->state=END;
  4460. return 1;
  4461. }
  4462. return 0;
  4463. }
  4464. /*==========================================
  4465. * サブルーティンの呼び出し
  4466. *------------------------------------------*/
  4467. BUILDIN_FUNC(callsub)
  4468. {
  4469. int pos=script_getnum(st,2);
  4470. int i,j;
  4471. if(!data_islabel(script_getdata(st,2)) && !data_isfunclabel(script_getdata(st,2))) {
  4472. ShowError("script:callsub: argument is not a label\n");
  4473. script_reportdata(script_getdata(st,2));
  4474. st->state=END;
  4475. return 1;
  4476. } else {
  4477. struct linkdb_node **oldval = st->stack->var_function;
  4478. for(i=st->start+3,j=0;i<st->end;i++,j++)
  4479. push_copy(st->stack,i);
  4480. script_pushint(st,j); // 引数の数をプッシュ
  4481. script_pushint(st,st->stack->defsp); // 現在の基準スタックポインタをプッシュ
  4482. script_pushint(st,(int)st->script); // 現在のスクリプトをプッシュ
  4483. script_pushint(st,(int)st->stack->var_function); // 現在の関数依存変数をプッシュ
  4484. push_val(st->stack,C_RETINFO,st->pos); // 現在のスクリプト位置をプッシュ
  4485. st->pos=pos;
  4486. st->stack->defsp=st->start+5+j;
  4487. st->state=GOTO;
  4488. st->stack->var_function = (struct linkdb_node**)aCalloc(1, sizeof(struct linkdb_node*));
  4489. // ' 変数の引き継ぎ
  4490. for(i = 0; i < j; i++) {
  4491. struct script_data *s = &st->stack->stack_data[st->stack->sp-6-i];
  4492. if( data_isreference(s) && !s->ref ) {
  4493. char *name = str_buf+str_data[s->u.num&0x00ffffff].str;
  4494. // '@ 変数の引き継ぎ
  4495. if( name[0] == '.' && name[1] == '@' ) {
  4496. s->ref = oldval;
  4497. }
  4498. }
  4499. }
  4500. }
  4501. return 0;
  4502. }
  4503. /// Retrieves an argument provided to callfunc/callsub.
  4504. /// If the argument doesn't exist
  4505. ///
  4506. /// getarg(<index>{,<default_value>}) -> <value>
  4507. BUILDIN_FUNC(getarg)
  4508. {
  4509. int idx;
  4510. int count;
  4511. int stsp;
  4512. if( st->stack->defsp < 5 || st->stack->stack_data[st->stack->defsp - 1].type != C_RETINFO )
  4513. {
  4514. ShowError("script:getarg: no callfunc or callsub!\n");
  4515. st->state = END;
  4516. return 1;
  4517. }
  4518. count = conv_num(st, &(st->stack->stack_data[st->stack->defsp - 5]));
  4519. stsp = st->stack->defsp - count - 5;
  4520. idx = script_getnum(st,2);
  4521. if( idx < count )
  4522. push_copy(st->stack, stsp + idx);
  4523. else if( script_hasdata(st,3) )
  4524. script_pushcopy(st, 3);
  4525. else
  4526. {
  4527. ShowError("script:getarg: index (idx=%d) out of range (count=%d) and no default value found\n", idx, count);
  4528. st->state = END;
  4529. return 1;
  4530. }
  4531. return 0;
  4532. }
  4533. /// Returns from the current function, optionaly returning a value from the functions.
  4534. /// Don't use outside script functions.
  4535. ///
  4536. /// return;
  4537. /// return <value>;
  4538. BUILDIN_FUNC(return)
  4539. {
  4540. if( script_hasdata(st,2) )
  4541. {// return value
  4542. struct script_data* data;
  4543. script_pushcopy(st, 2);
  4544. data = script_getdatatop(st, -1);
  4545. if( data_isreference(data) )
  4546. {
  4547. char* name = reference_getname(data);
  4548. if( name[0] == '.' && name[1] == '@' )
  4549. {// temporary script variable, convert to value
  4550. get_val(st, data);
  4551. if( data_isstring(data) )
  4552. {// duplicate the string
  4553. data->type = C_STR;
  4554. data->u.str = aStrdup(data->u.str);
  4555. }
  4556. }
  4557. else if( name[0] == '.' && !data->ref )
  4558. {// script variable, link to current script
  4559. data->ref = &st->script->script_vars;
  4560. }
  4561. }
  4562. }
  4563. else
  4564. {// no return value
  4565. script_pushnil(st);
  4566. }
  4567. st->state = RETFUNC;
  4568. return 0;
  4569. }
  4570. /// Returns a random number from 0 to <range>-1.
  4571. /// Or returns a random number from <min> to <max>.
  4572. /// If <min> is greater than <max>, their numbers are switched.
  4573. /// rand(<range>) -> <int>
  4574. /// rand(<min>,<max>) -> <int>
  4575. BUILDIN_FUNC(rand)
  4576. {
  4577. int range;
  4578. int min;
  4579. int max;
  4580. if( script_hasdata(st,3) )
  4581. {// min,max
  4582. min = script_getnum(st,2);
  4583. max = script_getnum(st,3);
  4584. if( max < min )
  4585. swap(min, max);
  4586. range = max - min + 1;
  4587. }
  4588. else
  4589. {// range
  4590. min = 0;
  4591. range = script_getnum(st,2);
  4592. }
  4593. if( range <= 1 )
  4594. script_pushint(st, min);
  4595. else
  4596. script_pushint(st, rand()%range + min);
  4597. return 0;
  4598. }
  4599. /*==========================================
  4600. *
  4601. *------------------------------------------*/
  4602. BUILDIN_FUNC(warp)
  4603. {
  4604. int x,y;
  4605. const char *str;
  4606. TBL_PC *sd=script_rid2sd(st);
  4607. nullpo_retr(0, sd);
  4608. str=script_getstr(st,2);
  4609. x=script_getnum(st,3);
  4610. y=script_getnum(st,4);
  4611. if(strcmp(str,"Random")==0)
  4612. pc_randomwarp(sd,3);
  4613. else if(strcmp(str,"SavePoint")==0){
  4614. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  4615. }else if(strcmp(str,"Save")==0){
  4616. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  4617. }else
  4618. pc_setpos(sd,mapindex_name2id(str),x,y,0);
  4619. return 0;
  4620. }
  4621. /*==========================================
  4622. * エリア指定ワープ
  4623. *------------------------------------------*/
  4624. static int buildin_areawarp_sub(struct block_list *bl,va_list ap)
  4625. {
  4626. int x,y;
  4627. unsigned int map;
  4628. map=va_arg(ap, unsigned int);
  4629. x=va_arg(ap,int);
  4630. y=va_arg(ap,int);
  4631. if(map == 0)
  4632. pc_randomwarp((TBL_PC *)bl,3);
  4633. else
  4634. pc_setpos((TBL_PC *)bl,map,x,y,0);
  4635. return 0;
  4636. }
  4637. BUILDIN_FUNC(areawarp)
  4638. {
  4639. int x,y,m;
  4640. unsigned int index;
  4641. const char *str;
  4642. const char *mapname;
  4643. int x0,y0,x1,y1;
  4644. mapname=script_getstr(st,2);
  4645. x0=script_getnum(st,3);
  4646. y0=script_getnum(st,4);
  4647. x1=script_getnum(st,5);
  4648. y1=script_getnum(st,6);
  4649. str=script_getstr(st,7);
  4650. x=script_getnum(st,8);
  4651. y=script_getnum(st,9);
  4652. if( (m=map_mapname2mapid(mapname))< 0)
  4653. return 0;
  4654. if(strcmp(str,"Random")==0)
  4655. index = 0;
  4656. else if(!(index=mapindex_name2id(str)))
  4657. return 0;
  4658. map_foreachinarea(buildin_areawarp_sub,
  4659. m,x0,y0,x1,y1,BL_PC, index,x,y );
  4660. return 0;
  4661. }
  4662. /*==========================================
  4663. * warpchar [LuzZza]
  4664. * Useful for warp one player from
  4665. * another player npc-session.
  4666. * Using: warpchar "mapname",x,y,Char_ID;
  4667. *------------------------------------------*/
  4668. BUILDIN_FUNC(warpchar)
  4669. {
  4670. int x,y,a,i;
  4671. const char *str;
  4672. TBL_PC *sd, **pl_allsd;
  4673. int users;
  4674. str=script_getstr(st,2);
  4675. x=script_getnum(st,3);
  4676. y=script_getnum(st,4);
  4677. a=script_getnum(st,5);
  4678. pl_allsd = map_getallusers(&users);
  4679. for(i=0; i<users; i++) {
  4680. sd = pl_allsd[i];
  4681. if(sd->status.char_id == a) {
  4682. if(strcmp(str, "Random") == 0)
  4683. pc_randomwarp(sd, 3);
  4684. else if(strcmp(str, "SavePoint") == 0)
  4685. pc_setpos(sd, sd->status.save_point.map,sd->status.save_point.x, sd->status.save_point.y, 3);
  4686. else
  4687. pc_setpos(sd, mapindex_name2id(str), x, y, 3);
  4688. }
  4689. }
  4690. return 0;
  4691. }
  4692. /*==========================================
  4693. * Warpparty - [Fredzilla]
  4694. * Syntax: warpparty "mapname",x,y,Party_ID;
  4695. *------------------------------------------*/
  4696. BUILDIN_FUNC(warpparty)
  4697. {
  4698. int x,y;
  4699. const char *str;
  4700. int p_id;
  4701. int i;
  4702. unsigned short mapindex;
  4703. TBL_PC *pl_sd;
  4704. struct party_data *p=NULL;
  4705. str=script_getstr(st,2);
  4706. x=script_getnum(st,3);
  4707. y=script_getnum(st,4);
  4708. p_id=script_getnum(st,5);
  4709. if(p_id < 1)
  4710. return 0;
  4711. p = party_search(p_id);
  4712. if (!p)
  4713. return 0;
  4714. if(strcmp(str,"Random")==0)
  4715. {
  4716. for (i = 0; i < MAX_PARTY; i++)
  4717. {
  4718. if ((pl_sd = p->data[i].sd))
  4719. {
  4720. if(map[pl_sd->bl.m].flag.nowarp)
  4721. continue;
  4722. pc_randomwarp(pl_sd,3);
  4723. }
  4724. }
  4725. }
  4726. else if(strcmp(str,"SavePointAll")==0)
  4727. {
  4728. for (i = 0; i < MAX_PARTY; i++)
  4729. {
  4730. if ((pl_sd = p->data[i].sd))
  4731. {
  4732. if(map[pl_sd->bl.m].flag.noreturn)
  4733. continue;
  4734. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4735. }
  4736. }
  4737. }
  4738. else if(strcmp(str,"SavePoint")==0)
  4739. {
  4740. pl_sd=script_rid2sd(st);
  4741. if (!pl_sd) return 0;
  4742. mapindex=pl_sd->status.save_point.map;
  4743. x=pl_sd->status.save_point.x;
  4744. y=pl_sd->status.save_point.y;
  4745. for (i = 0; i < MAX_PARTY; i++)
  4746. {
  4747. if ((pl_sd = p->data[i].sd))
  4748. {
  4749. if(map[pl_sd->bl.m].flag.noreturn)
  4750. continue;
  4751. pc_setpos(pl_sd,mapindex,x,y,3);
  4752. }
  4753. }
  4754. }
  4755. else if(strcmp(str,"Leader")==0)
  4756. {
  4757. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  4758. if (i == MAX_PARTY || !p->data[i].sd) //Leader not found / not online
  4759. return 0;
  4760. if(map[p->data[i].sd->bl.m].flag.nowarpto)
  4761. return 0;
  4762. mapindex = p->data[i].sd->mapindex;
  4763. x = p->data[i].sd->bl.x;
  4764. y = p->data[i].sd->bl.y;
  4765. for (i = 0; i < MAX_PARTY; i++)
  4766. {
  4767. pl_sd = p->data[i].sd;
  4768. if (!pl_sd)
  4769. continue;
  4770. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4771. continue;
  4772. pc_setpos(pl_sd,mapindex,x,y,3);
  4773. }
  4774. }
  4775. else
  4776. {
  4777. mapindex = mapindex_name2id(str);
  4778. if (!mapindex) //Show source of npc error.
  4779. return 1;
  4780. for (i = 0; i < MAX_PARTY; i++)
  4781. {
  4782. if ((pl_sd = p->data[i].sd))
  4783. {
  4784. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4785. continue;
  4786. pc_setpos(pl_sd,mapindex,x,y,3);
  4787. }
  4788. }
  4789. }
  4790. return 0;
  4791. }
  4792. /*==========================================
  4793. * Warpguild - [Fredzilla]
  4794. * Syntax: warpguild "mapname",x,y,Guild_ID;
  4795. *------------------------------------------*/
  4796. BUILDIN_FUNC(warpguild)
  4797. {
  4798. int x,y;
  4799. unsigned short mapindex;
  4800. const char *str;
  4801. int g;
  4802. int i;
  4803. TBL_PC *pl_sd, **pl_allsd;
  4804. int users;
  4805. TBL_PC *sd;
  4806. str=script_getstr(st,2);
  4807. x=script_getnum(st,3);
  4808. y=script_getnum(st,4);
  4809. g=script_getnum(st,5);
  4810. sd=script_rid2sd(st);
  4811. if(map[sd->bl.m].flag.noreturn || map[sd->bl.m].flag.nowarpto)
  4812. return 0;
  4813. if(g < 1)
  4814. return 0;
  4815. pl_allsd = map_getallusers(&users);
  4816. if(strcmp(str,"Random")==0)
  4817. {
  4818. for (i = 0; i < users; i++)
  4819. {
  4820. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4821. {
  4822. if(map[pl_sd->bl.m].flag.nowarp)
  4823. continue;
  4824. pc_randomwarp(pl_sd,3);
  4825. }
  4826. }
  4827. }
  4828. else if(strcmp(str,"SavePointAll")==0)
  4829. {
  4830. if(map[sd->bl.m].flag.noreturn)
  4831. return 0;
  4832. for (i = 0; i < users; i++)
  4833. {
  4834. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4835. {
  4836. if(map[pl_sd->bl.m].flag.noreturn)
  4837. continue;
  4838. pc_setpos(pl_sd,pl_sd->status.save_point.map,pl_sd->status.save_point.x,pl_sd->status.save_point.y,3);
  4839. }
  4840. }
  4841. }
  4842. else if(strcmp(str,"SavePoint")==0)
  4843. {
  4844. if(map[sd->bl.m].flag.noreturn)
  4845. return 0;
  4846. mapindex=sd->status.save_point.map;
  4847. x=sd->status.save_point.x;
  4848. y=sd->status.save_point.y;
  4849. for (i = 0; i < users; i++)
  4850. {
  4851. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4852. {
  4853. if(map[pl_sd->bl.m].flag.noreturn)
  4854. continue;
  4855. pc_setpos(pl_sd,mapindex,x,y,3);
  4856. }
  4857. }
  4858. }
  4859. else
  4860. {
  4861. mapindex = mapindex_name2id(str);
  4862. for (i = 0; i < users; i++)
  4863. {
  4864. if ((pl_sd = pl_allsd[i]) && pl_sd->status.guild_id == g)
  4865. {
  4866. if(map[pl_sd->bl.m].flag.noreturn || map[pl_sd->bl.m].flag.nowarp)
  4867. continue;
  4868. pc_setpos(pl_sd,mapindex,x,y,3);
  4869. }
  4870. }
  4871. }
  4872. return 0;
  4873. }
  4874. /*==========================================
  4875. *
  4876. *------------------------------------------*/
  4877. BUILDIN_FUNC(heal)
  4878. {
  4879. TBL_PC *sd;
  4880. int hp,sp;
  4881. sd = script_rid2sd(st);
  4882. if (!sd) return 0;
  4883. hp=script_getnum(st,2);
  4884. sp=script_getnum(st,3);
  4885. status_heal(&sd->bl, hp, sp, 1);
  4886. return 0;
  4887. }
  4888. /*==========================================
  4889. *
  4890. *------------------------------------------*/
  4891. BUILDIN_FUNC(itemheal)
  4892. {
  4893. TBL_PC *sd;
  4894. int hp,sp;
  4895. hp=script_getnum(st,2);
  4896. sp=script_getnum(st,3);
  4897. if(potion_flag==1) {
  4898. potion_hp = hp;
  4899. potion_sp = sp;
  4900. return 0;
  4901. }
  4902. sd = script_rid2sd(st);
  4903. if (!sd) return 0;
  4904. pc_itemheal(sd,sd->itemid,hp,sp);
  4905. return 0;
  4906. }
  4907. /*==========================================
  4908. *
  4909. *------------------------------------------*/
  4910. BUILDIN_FUNC(percentheal)
  4911. {
  4912. int hp,sp;
  4913. hp=script_getnum(st,2);
  4914. sp=script_getnum(st,3);
  4915. if(potion_flag==1) {
  4916. potion_per_hp = hp;
  4917. potion_per_sp = sp;
  4918. return 0;
  4919. }
  4920. pc_percentheal(script_rid2sd(st),hp,sp);
  4921. return 0;
  4922. }
  4923. /*==========================================
  4924. *
  4925. *------------------------------------------*/
  4926. BUILDIN_FUNC(jobchange)
  4927. {
  4928. int job, upper=-1;
  4929. job=script_getnum(st,2);
  4930. if( script_hasdata(st,3) )
  4931. upper=script_getnum(st,3);
  4932. if ((job >= 0 && job < MAX_PC_CLASS))
  4933. {
  4934. pc_jobchange(script_rid2sd(st),job, upper);
  4935. if(use_irc && irc_announce_jobchange_flag)
  4936. irc_announce_jobchange(script_rid2sd(st));
  4937. }
  4938. return 0;
  4939. }
  4940. /*==========================================
  4941. *
  4942. *------------------------------------------*/
  4943. BUILDIN_FUNC(jobname)
  4944. {
  4945. int class_=script_getnum(st,2);
  4946. script_pushconststr(st,job_name(class_));
  4947. return 0;
  4948. }
  4949. /*==========================================
  4950. *
  4951. *------------------------------------------*/
  4952. BUILDIN_FUNC(input)
  4953. {
  4954. TBL_PC *sd = script_rid2sd(st);
  4955. struct script_data *data = script_getdata(st,2);
  4956. int num = data->u.num;
  4957. char *name=str_buf+str_data[num&0x00ffffff].str;
  4958. char postfix = name[strlen(name)-1];
  4959. if (!sd) return 1;
  4960. if( !data_isreference(data) ){
  4961. ShowError("script:input: not a variable\n");
  4962. script_reportdata(data);
  4963. return 1;
  4964. }
  4965. if(sd->state.menu_or_input){
  4966. sd->state.menu_or_input=0;
  4967. if( postfix=='$' )
  4968. {
  4969. set_reg(st,sd,num,name,(void*)sd->npc_str,
  4970. script_getref(st,2));
  4971. return 0;
  4972. }
  4973. // Yor, Lupus & Fritz have messed with this.
  4974. // Basicly it prevents negative input since most scripts do not account for them.
  4975. sd->npc_amount = cap_value(sd->npc_amount, 0, battle_config.vending_max_value);
  4976. set_reg(st,sd,num,name,(void*)sd->npc_amount,
  4977. script_getref(st,2));
  4978. return 0;
  4979. }
  4980. //state.menu_or_input = 0
  4981. st->state=RERUNLINE;
  4982. if( postfix=='$' )
  4983. clif_scriptinputstr(sd,st->oid);
  4984. else
  4985. clif_scriptinput(sd,st->oid);
  4986. sd->state.menu_or_input=1;
  4987. return 0;
  4988. }
  4989. /*==========================================
  4990. * 変数設定
  4991. *------------------------------------------*/
  4992. BUILDIN_FUNC(set)
  4993. {
  4994. TBL_PC *sd=NULL;
  4995. int num=st->stack->stack_data[st->start+2].u.num;
  4996. char *name=str_buf+str_data[num&0x00ffffff].str;
  4997. char prefix=*name;
  4998. char postfix=name[strlen(name)-1];
  4999. if( !data_isreference(script_getdata(st,2)) ){
  5000. ShowError("script:set: not a variable\n");
  5001. script_reportdata(script_getdata(st,2));
  5002. st->state = END;
  5003. return 1;
  5004. }
  5005. if(not_server_variable(prefix))
  5006. sd=script_rid2sd(st);
  5007. if( postfix=='$' ){
  5008. // 文字列
  5009. const char *str = script_getstr(st,3);
  5010. set_reg(st,sd,num,name,(void*)str,script_getref(st,2));
  5011. }else{
  5012. // 数値
  5013. int val = script_getnum(st,3);
  5014. set_reg(st,sd,num,name,(void*)val,script_getref(st,2));
  5015. }
  5016. return 0;
  5017. }
  5018. /////////////////////////////////////////////////////////////////////
  5019. /// Array variables
  5020. ///
  5021. /// Returns the size of the specified array
  5022. static int32 getarraysize(struct script_state* st, int32 id, int32 idx, int isstring, struct linkdb_node** ref)
  5023. {
  5024. int32 ret = idx;
  5025. if( isstring )
  5026. {
  5027. for( ; idx < 128; ++idx )
  5028. {
  5029. char* str = (char*)get_val2(st, reference_uid(id, idx), ref);
  5030. if( str && *str )
  5031. ret = idx + 1;
  5032. }
  5033. }
  5034. else
  5035. {
  5036. for( ; idx < 128; ++idx )
  5037. {
  5038. int32 num = (int32)get_val2(st, reference_uid(id, idx), ref);
  5039. if( num )
  5040. ret = idx + 1;
  5041. }
  5042. }
  5043. return ret;
  5044. }
  5045. /// Sets values of an array, from the starting index.
  5046. /// ex: setarray arr[1],1,2,3;
  5047. ///
  5048. /// setarray <array variable>,<value1>{,<value2>...};
  5049. BUILDIN_FUNC(setarray)
  5050. {
  5051. struct script_data* data;
  5052. char* name;
  5053. int32 start;
  5054. int32 end;
  5055. int32 id;
  5056. int32 i;
  5057. TBL_PC* sd = NULL;
  5058. data = script_getdata(st, 2);
  5059. if( !data_isreference(data) )
  5060. {
  5061. ShowError("script:setarray: not a variable\n");
  5062. script_reportdata(data);
  5063. st->state = END;
  5064. return 1;// not a variable
  5065. }
  5066. id = reference_getid(data);
  5067. start = reference_getindex(data);
  5068. name = reference_getname(data);
  5069. if( not_array_variable(*name) )
  5070. {
  5071. ShowError("script:setarray: illegal scope\n");
  5072. script_reportdata(data);
  5073. st->state = END;
  5074. return 1;// not supported
  5075. }
  5076. if( not_server_variable(*name) )
  5077. {
  5078. sd = script_rid2sd(st);
  5079. if( sd == NULL )
  5080. return 1;// no player attached
  5081. }
  5082. end = start + script_lastdata(st) - 2;
  5083. if( end > 127 )
  5084. end = 127;
  5085. if( is_string_variable(name) )
  5086. {// string array
  5087. for( i = 3; start < end; ++start, ++i )
  5088. {
  5089. void* v = (void*)script_getstr(st,i);
  5090. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5091. }
  5092. }
  5093. else
  5094. {// int array
  5095. for( i = 3; start < end; ++start, ++i )
  5096. {
  5097. void* v = (void*)script_getnum(st,i);
  5098. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5099. }
  5100. }
  5101. return 0;
  5102. }
  5103. /// Sets count values of an array, from the starting index.
  5104. /// ex: cleararray arr[0],0,1;
  5105. ///
  5106. /// cleararray <array variable>,<value>,<count>;
  5107. BUILDIN_FUNC(cleararray)
  5108. {
  5109. struct script_data* data;
  5110. char* name;
  5111. int32 start;
  5112. int32 end;
  5113. int32 id;
  5114. void* v;
  5115. TBL_PC* sd = NULL;
  5116. data = script_getdata(st, 2);
  5117. if( !data_isreference(data) )
  5118. {
  5119. ShowError("script:cleararray: not a variable\n");
  5120. script_reportdata(data);
  5121. st->state = END;
  5122. return 1;// not a variable
  5123. }
  5124. id = reference_getid(data);
  5125. start = reference_getindex(data);
  5126. name = reference_getname(data);
  5127. if( not_array_variable(*name) )
  5128. {
  5129. ShowError("script:cleararray: illegal scope\n");
  5130. script_reportdata(data);
  5131. st->state = END;
  5132. return 1;// not supported
  5133. }
  5134. if( not_server_variable(*name) )
  5135. {
  5136. sd = script_rid2sd(st);
  5137. if( sd == NULL )
  5138. return 1;// no player attached
  5139. }
  5140. if( is_string_variable(name) )
  5141. v = (void*)script_getstr(st, 3);
  5142. else
  5143. v = (void*)script_getnum(st, 3);
  5144. end = start + script_getnum(st, 4);
  5145. if( end > 127 )
  5146. end = 127;
  5147. for( ; start < end; ++start )
  5148. set_reg(st, sd, reference_uid(id, start), name, v, script_getref(st,2));
  5149. return 0;
  5150. }
  5151. /// Copies data from one array to another.
  5152. /// ex: copyarray arr[0],arr[2],2;
  5153. ///
  5154. /// copyarray <destination array variable>,<source array variable>,<count>;
  5155. BUILDIN_FUNC(copyarray)
  5156. {
  5157. struct script_data* data1;
  5158. struct script_data* data2;
  5159. char* name1;
  5160. char* name2;
  5161. int32 idx1;
  5162. int32 idx2;
  5163. int32 id1;
  5164. int32 id2;
  5165. void* v;
  5166. int32 i;
  5167. int32 count;
  5168. TBL_PC* sd = NULL;
  5169. data1 = script_getdata(st, 2);
  5170. data2 = script_getdata(st, 3);
  5171. if( !data_isreference(data1) || !data_isreference(data2) )
  5172. {
  5173. ShowError("script:copyarray: not a variable\n");
  5174. script_reportdata(data1);
  5175. script_reportdata(data2);
  5176. st->state = END;
  5177. return 1;// not a variable
  5178. }
  5179. id1 = reference_getid(data1);
  5180. id2 = reference_getid(data2);
  5181. idx1 = reference_getindex(data1);
  5182. idx2 = reference_getindex(data2);
  5183. name1 = reference_getname(data1);
  5184. name2 = reference_getname(data2);
  5185. if( not_array_variable(*name1) || not_array_variable(*name2) )
  5186. {
  5187. ShowError("script:copyarray: illegal scope\n");
  5188. script_reportdata(data1);
  5189. script_reportdata(data2);
  5190. st->state = END;
  5191. return 1;// not supported
  5192. }
  5193. if( is_string_variable(name1) != is_string_variable(name2) )
  5194. {
  5195. ShowError("script:copyarray: type mismatch\n");
  5196. script_reportdata(data1);
  5197. script_reportdata(data2);
  5198. st->state = END;
  5199. return 1;// data type mismatch
  5200. }
  5201. if( not_server_variable(*name1) || not_server_variable(*name2) )
  5202. {
  5203. sd = script_rid2sd(st);
  5204. if( sd == NULL )
  5205. return 1;// no player attached
  5206. }
  5207. count = script_getnum(st, 4);
  5208. if( count > 128 - idx1 )
  5209. count = 128 - idx1;
  5210. if( count <= 0 || (id1 == id2 && idx1 == idx2) )
  5211. return 0;// nothing to copy
  5212. if( id1 == id2 && idx1 > idx2 )
  5213. {// destination might be overlapping the source - copy in reverse order
  5214. for( i = count - 1; i >= 0; --i )
  5215. {
  5216. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5217. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5218. }
  5219. }
  5220. else
  5221. {// normal copy
  5222. for( i = 0; i < count; ++i )
  5223. {
  5224. if( idx2 + i < 128 )
  5225. v = get_val2(st, reference_uid(id2, idx2 + i), reference_getref(data2));
  5226. else// out of range - assume ""/0
  5227. v = (void*)(is_string_variable(name1) ? "" : 0);
  5228. set_reg(st, sd, reference_uid(id1, idx1 + i), name1, v, reference_getref(data1));
  5229. }
  5230. }
  5231. return 0;
  5232. }
  5233. /// Returns the size of the array.
  5234. /// Assumes that everything before the starting index exists.
  5235. /// ex: getarraysize(arr[3])
  5236. ///
  5237. /// getarraysize(<array variable>) -> <int>
  5238. BUILDIN_FUNC(getarraysize)
  5239. {
  5240. struct script_data* data;
  5241. char* name;
  5242. data = script_getdata(st, 2);
  5243. if( !data_isreference(data) )
  5244. {
  5245. ShowError("script:getarraysize: not a variable\n");
  5246. script_reportdata(data);
  5247. script_pushnil(st);
  5248. st->state = END;
  5249. return 1;// not a variable
  5250. }
  5251. name = reference_getname(data);
  5252. if( not_array_variable(*name) )
  5253. {
  5254. ShowError("script:getarraysize: illegal scope\n");
  5255. script_reportdata(data);
  5256. script_pushnil(st);
  5257. st->state = END;
  5258. return 1;// not supported
  5259. }
  5260. script_pushint(st, getarraysize(st, reference_getid(data), reference_getindex(data), is_string_variable(name), reference_getref(data)));
  5261. return 0;
  5262. }
  5263. /// Deletes count or all the elements in an array, from the starting index.
  5264. /// ex: deletearray arr[4],2;
  5265. ///
  5266. /// deletearray <array variable>;
  5267. /// deletearray <array variable>,<count>;
  5268. BUILDIN_FUNC(deletearray)
  5269. {
  5270. struct script_data* data;
  5271. char* name;
  5272. int start;
  5273. int end;
  5274. int id;
  5275. TBL_PC *sd = NULL;
  5276. data = script_getdata(st, 2);
  5277. if( !data_isreference(data) )
  5278. {
  5279. ShowError("script:deletearray: not a variable\n");
  5280. script_reportdata(data);
  5281. st->state = END;
  5282. return 1;// not a variable
  5283. }
  5284. id = reference_getid(data);
  5285. start = reference_getindex(data);
  5286. name = reference_getname(data);
  5287. if( not_array_variable(*name) )
  5288. {
  5289. ShowError("script:deletearray: illegal scope\n");
  5290. script_reportdata(data);
  5291. st->state = END;
  5292. return 1;// not supported
  5293. }
  5294. if( not_server_variable(*name) )
  5295. {
  5296. sd = script_rid2sd(st);
  5297. if( sd == NULL )
  5298. return 1;// no player attached
  5299. }
  5300. end = getarraysize(st, id, start, is_string_variable(name), reference_getref(data));
  5301. if( start >= end )
  5302. return 0;// nothing to free
  5303. if( script_hasdata(st,3) )
  5304. {
  5305. int count = script_getnum(st, 3);
  5306. if( count > end - start )
  5307. count = end - start;
  5308. if( count <= 0 )
  5309. return 0;// nothing to free
  5310. // move rest of the elements backward
  5311. for( ; start + count < end; ++start )
  5312. {
  5313. void* v = get_val2(st, reference_uid(id, start + count), reference_getref(data));
  5314. set_reg(st, sd, reference_uid(id, start), name, v, reference_getref(data));
  5315. }
  5316. }
  5317. // clear the rest of the array
  5318. if( is_string_variable(name) )
  5319. {
  5320. for( ; start < end; ++start )
  5321. set_reg(st, sd, reference_uid(id, start), name, (void *)"", reference_getref(data));
  5322. }
  5323. else
  5324. {
  5325. for( ; start < end; ++start )
  5326. set_reg(st, sd, reference_uid(id, start), name, (void*)0, reference_getref(data));
  5327. }
  5328. return 0;
  5329. }
  5330. /// Returns a reference to the target index of the array variable.
  5331. /// Equivalent to var[index].
  5332. ///
  5333. /// getelementofarray(<array variable>,<index>) -> <variable reference>
  5334. BUILDIN_FUNC(getelementofarray)
  5335. {
  5336. struct script_data* data;
  5337. char* name;
  5338. int32 id;
  5339. int i;
  5340. data = script_getdata(st, 2);
  5341. if( !data_isreference(data) )
  5342. {
  5343. ShowError("script:getelementofarray: not a variable\n");
  5344. script_reportdata(data);
  5345. script_pushnil(st);
  5346. st->state = END;
  5347. return 1;// not a variable
  5348. }
  5349. id = reference_getid(data);
  5350. name = reference_getname(data);
  5351. if( not_array_variable(*name) )
  5352. {
  5353. ShowError("script:getelementofarray: illegal scope\n");
  5354. script_reportdata(data);
  5355. script_pushnil(st);
  5356. st->state = END;
  5357. return 1;// not supported
  5358. }
  5359. i = script_getnum(st, 3);
  5360. if( i < 0 || i >= 128 )
  5361. {
  5362. ShowWarning("script:getelementofarray: index out of range (%d)\n", i);
  5363. script_reportdata(data);
  5364. script_pushnil(st);
  5365. st->state = END;
  5366. return 1;// out of range
  5367. }
  5368. push_val2(st->stack, C_NAME, reference_uid(id, i), reference_getref(data));
  5369. return 0;
  5370. }
  5371. /////////////////////////////////////////////////////////////////////
  5372. /// ...
  5373. ///
  5374. /*==========================================
  5375. *
  5376. *------------------------------------------*/
  5377. BUILDIN_FUNC(setlook)
  5378. {
  5379. int type,val;
  5380. type=script_getnum(st,2);
  5381. val=script_getnum(st,3);
  5382. pc_changelook(script_rid2sd(st),type,val);
  5383. return 0;
  5384. }
  5385. /*==========================================
  5386. *
  5387. *------------------------------------------*/
  5388. BUILDIN_FUNC(cutin)
  5389. {
  5390. clif_cutin(script_rid2sd(st),script_getstr(st,2),script_getnum(st,3));
  5391. return 0;
  5392. }
  5393. /*==========================================
  5394. *
  5395. *------------------------------------------*/
  5396. BUILDIN_FUNC(viewpoint)
  5397. {
  5398. int type,x,y,id,color;
  5399. type=script_getnum(st,2);
  5400. x=script_getnum(st,3);
  5401. y=script_getnum(st,4);
  5402. id=script_getnum(st,5);
  5403. color=script_getnum(st,6);
  5404. clif_viewpoint(script_rid2sd(st),st->oid,type,x,y,id,color);
  5405. return 0;
  5406. }
  5407. /*==========================================
  5408. *
  5409. *------------------------------------------*/
  5410. BUILDIN_FUNC(countitem)
  5411. {
  5412. int nameid, i;
  5413. int count = 0;
  5414. struct script_data* data;
  5415. TBL_PC* sd = script_rid2sd(st);
  5416. if (!sd) {
  5417. script_pushint(st,0);
  5418. return 0;
  5419. }
  5420. data = script_getdata(st,2);
  5421. get_val(st,data);
  5422. if( data_isstring(data) ) {
  5423. const char* name = conv_str(st,data);
  5424. struct item_data* item_data;
  5425. if((item_data = itemdb_searchname(name)) != NULL)
  5426. nameid = item_data->nameid;
  5427. else
  5428. nameid = 0;
  5429. } else
  5430. nameid = conv_num(st,data);
  5431. if (nameid < 500) {
  5432. if(battle_config.error_log) ShowError("wrong item ID : countitem(%i)\n", nameid);
  5433. script_pushint(st,0);
  5434. return 1;
  5435. }
  5436. for(i = 0; i < MAX_INVENTORY; i++)
  5437. if(sd->status.inventory[i].nameid == nameid)
  5438. count += sd->status.inventory[i].amount;
  5439. script_pushint(st,count);
  5440. return 0;
  5441. }
  5442. /*==========================================
  5443. * countitem2(nameID,Identified,Refine,Attribute,Card0,Card1,Card2,Card3) [Lupus]
  5444. * returns number of items that meet the conditions
  5445. *------------------------------------------*/
  5446. BUILDIN_FUNC(countitem2)
  5447. {
  5448. int nameid, iden, ref, attr, c1, c2, c3, c4;
  5449. int count = 0;
  5450. int i;
  5451. struct script_data* data;
  5452. TBL_PC* sd = script_rid2sd(st);
  5453. if (!sd) {
  5454. script_pushint(st,0);
  5455. return 0;
  5456. }
  5457. data = script_getdata(st,2);
  5458. get_val(st,data);
  5459. if( data_isstring(data) ) {
  5460. const char* name = conv_str(st,data);
  5461. struct item_data* item_data;
  5462. if((item_data = itemdb_searchname(name)) != NULL)
  5463. nameid = item_data->nameid;
  5464. else
  5465. nameid = 0;
  5466. } else
  5467. nameid = conv_num(st,data);
  5468. iden = script_getnum(st,3);
  5469. ref = script_getnum(st,4);
  5470. attr = script_getnum(st,5);
  5471. c1 = (short)script_getnum(st,6);
  5472. c2 = (short)script_getnum(st,7);
  5473. c3 = (short)script_getnum(st,8);
  5474. c4 = (short)script_getnum(st,9);
  5475. if (nameid < 500) {
  5476. if(battle_config.error_log) ShowError("wrong item ID : countitem2(%i)\n", nameid);
  5477. script_pushint(st,0);
  5478. return 1;
  5479. }
  5480. for(i = 0; i < MAX_INVENTORY; i++)
  5481. if (sd->status.inventory[i].nameid > 0 && sd->inventory_data[i] != NULL &&
  5482. sd->status.inventory[i].amount > 0 && sd->status.inventory[i].nameid == nameid &&
  5483. sd->status.inventory[i].identify == iden && sd->status.inventory[i].refine == ref &&
  5484. sd->status.inventory[i].attribute == attr && sd->status.inventory[i].card[0] == c1 &&
  5485. sd->status.inventory[i].card[1] == c2 && sd->status.inventory[i].card[2] == c3 &&
  5486. sd->status.inventory[i].card[3] == c4
  5487. )
  5488. count += sd->status.inventory[i].amount;
  5489. script_pushint(st,count);
  5490. return 0;
  5491. }
  5492. /*==========================================
  5493. * 重量チェック
  5494. *------------------------------------------*/
  5495. BUILDIN_FUNC(checkweight)
  5496. {
  5497. int nameid=0,amount,i;
  5498. unsigned long weight;
  5499. TBL_PC *sd;
  5500. struct script_data *data;
  5501. sd = script_rid2sd(st);
  5502. data=script_getdata(st,2);
  5503. get_val(st,data);
  5504. if( data_isstring(data) ){
  5505. const char *name=conv_str(st,data);
  5506. struct item_data *item_data = itemdb_searchname(name);
  5507. if( item_data )
  5508. nameid=item_data->nameid;
  5509. }else
  5510. nameid=conv_num(st,data);
  5511. amount=script_getnum(st,3);
  5512. if ( amount<=0 || nameid<500 ) { //if get wrong item ID or amount<=0, don't count weight of non existing items
  5513. script_pushint(st,0);
  5514. ShowError("buildin_checkweight: Wrong item ID or amount.\n");
  5515. return 1;
  5516. }
  5517. weight = itemdb_weight(nameid)*amount;
  5518. if(amount > MAX_AMOUNT || weight + sd->weight > sd->max_weight){
  5519. script_pushint(st,0);
  5520. } else {
  5521. //Check if the inventory ain't full.
  5522. //TODO: Currently does not checks if you can just stack it on top of another item you already have....
  5523. i = pc_search_inventory(sd,0);
  5524. if (i >= 0) //Empty slot available.
  5525. script_pushint(st,1);
  5526. else //Inventory full
  5527. script_pushint(st,0);
  5528. }
  5529. return 0;
  5530. }
  5531. /*==========================================
  5532. * getitem <item id>,<amount>{,<character ID>};
  5533. * getitem "<item name>",<amount>{,<character ID>};
  5534. *------------------------------------------*/
  5535. BUILDIN_FUNC(getitem)
  5536. {
  5537. int nameid,amount,flag = 0;
  5538. struct item it;
  5539. TBL_PC *sd;
  5540. struct script_data *data;
  5541. data=script_getdata(st,2);
  5542. get_val(st,data);
  5543. if( data_isstring(data) )
  5544. {// "<item name>"
  5545. const char *name=conv_str(st,data);
  5546. struct item_data *item_data = itemdb_searchname(name);
  5547. if( item_data == NULL ){
  5548. ShowError("buildin_getitem: Nonexistant item %s requested.\n", name);
  5549. return 1; //No item created.
  5550. }
  5551. nameid=item_data->nameid;
  5552. } else if( data_isint(data) )
  5553. {// <item id>
  5554. nameid=conv_num(st,data);
  5555. //Violet Box, Blue Box, etc - random item pick
  5556. if( nameid < 0 ) {
  5557. nameid=itemdb_searchrandomid(-nameid);
  5558. flag = 1;
  5559. }
  5560. if( nameid <= 0 || !itemdb_exists(nameid) ){
  5561. ShowError("buildin_getitem: Nonexistant item %d requested.\n", nameid);
  5562. return 1; //No item created.
  5563. }
  5564. } else {
  5565. ShowError("buildin_getitem: invalid data type for argument #1 (%d).", data->type);
  5566. return 1;
  5567. }
  5568. // <amount>
  5569. if( (amount=script_getnum(st,3)) <= 0)
  5570. return 0; //return if amount <=0, skip the useles iteration
  5571. memset(&it,0,sizeof(it));
  5572. it.nameid=nameid;
  5573. if(!flag)
  5574. it.identify=1;
  5575. else
  5576. it.identify=itemdb_isidentified(nameid);
  5577. if( script_hasdata(st,4) )
  5578. {// <character ID>
  5579. sd=map_id2sd(script_getnum(st,4));
  5580. } else
  5581. {// attached player
  5582. sd=script_rid2sd(st);
  5583. }
  5584. if( sd == NULL ) // no target
  5585. return 0;
  5586. if( pet_create_egg(sd, nameid) )
  5587. amount = 1; //This is a pet!
  5588. else if( (flag=pc_additem(sd,&it,amount)) ){
  5589. clif_additem(sd,0,0,flag);
  5590. if( pc_candrop(sd,&it) )
  5591. map_addflooritem(&it,amount,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  5592. }
  5593. //Logs items, got from (N)PC scripts [Lupus]
  5594. if(log_config.enable_logs&LOG_SCRIPT_TRANSACTIONS)
  5595. log_pick_pc(sd, "N", nameid, amount, NULL);
  5596. return 0;
  5597. }
  5598. /*==========================================
  5599. *
  5600. *------------------------------------------*/
  5601. BUILDIN_FUNC(getitem2)
  5602. {
  5603. int nameid,amount,flag = 0;
  5604. int iden,ref,attr,c1,c2,c3,c4;
  5605. struct item_data *item_data;
  5606. struct item item_tmp;
  5607. TBL_PC *sd;
  5608. struct script_data *data;
  5609. sd = script_rid2sd(st);
  5610. data=script_getdata(st,2);
  5611. get_val(st,data);
  5612. if( data_isstring(data) ){
  5613. const char *name=conv_str(st,data);
  5614. struct item_data *item_data = itemdb_searchname(name);
  5615. if( item_data )
  5616. nameid=item_data->nameid;
  5617. else
  5618. nameid=UNKNOWN_ITEM_ID;
  5619. }else
  5620. nameid=conv_num(st,data);
  5621. amount=script_getnum(st,3);
  5622. iden=script_getnum(st,4);
  5623. ref=script_getnum(st,5);
  5624. attr=script_getnum(st,6);
  5625. c1=script_getnum(st,7);
  5626. c2=script_getnum(st,8);
  5627. c3=script_getnum(st,9);
  5628. c4=script_getnum(st,10);
  5629. if( script_hasdata(st,11) ) //アイテムを指定したIDに渡す
  5630. sd=map_id2sd(script_getnum(st,11));
  5631. if(sd == NULL) //アイテムを渡す相手がいなかったらお帰り
  5632. return 0;
  5633. if(nameid<0) { // ランダム
  5634. nameid=itemdb_searchrandomid(-nameid);
  5635. flag = 1;
  5636. }
  5637. if(nameid > 0) {
  5638. memset(&item_tmp,0,sizeof(item_tmp));
  5639. item_data=itemdb_exists(nameid);
  5640. if (item_data == NULL)
  5641. return -1;
  5642. if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR){
  5643. if(ref > 10) ref = 10;
  5644. }
  5645. else if(item_data->type==IT_PETEGG) {
  5646. iden = 1;
  5647. ref = 0;
  5648. }
  5649. else {
  5650. iden = 1;
  5651. ref = attr = 0;
  5652. }
  5653. item_tmp.nameid=nameid;
  5654. if(!flag)
  5655. item_tmp.identify=iden;
  5656. else if(item_data->type==IT_WEAPON || item_data->type==IT_ARMOR)
  5657. item_tmp.identify=0;
  5658. item_tmp.refine=ref;
  5659. item_tmp.attribute=attr;
  5660. item_tmp.card[0]=c1;
  5661. item_tmp.card[1]=c2;
  5662. item_tmp.card[2]=c3;
  5663. item_tmp.card[3]=c4;
  5664. if((flag = pc_additem(sd,&item_tmp,amount))) {
  5665. clif_additem(sd,0,0,flag);
  5666. map_addflooritem(&item_tmp,amount,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  5667. }
  5668. //Logs items, got from (N)PC scripts [Lupus]
  5669. if(log_config.enable_logs&0x40)
  5670. log_pick_pc(sd, "N", nameid, amount, &item_tmp);
  5671. }
  5672. return 0;
  5673. }
  5674. /*==========================================
  5675. * gets an item with someone's name inscribed [Skotlex]
  5676. * getinscribeditem item_num, character_name
  5677. * Returned Qty is always 1, only works on equip-able
  5678. * equipment
  5679. *------------------------------------------*/
  5680. BUILDIN_FUNC(getnameditem)
  5681. {
  5682. int nameid;
  5683. struct item item_tmp;
  5684. TBL_PC *sd, *tsd;
  5685. struct script_data *data;
  5686. sd = script_rid2sd(st);
  5687. if (sd == NULL)
  5688. { //Player not attached!
  5689. script_pushint(st,0);
  5690. return 0;
  5691. }
  5692. data=script_getdata(st,2);
  5693. get_val(st,data);
  5694. if( data_isstring(data) ){
  5695. const char *name=conv_str(st,data);
  5696. struct item_data *item_data = itemdb_searchname(name);
  5697. if( item_data == NULL)
  5698. { //Failed
  5699. script_pushint(st,0);
  5700. return 0;
  5701. }
  5702. nameid = item_data->nameid;
  5703. }else
  5704. nameid = conv_num(st,data);
  5705. if(!itemdb_exists(nameid)/* || itemdb_isstackable(nameid)*/)
  5706. { //Even though named stackable items "could" be risky, they are required for certain quests.
  5707. script_pushint(st,0);
  5708. return 0;
  5709. }
  5710. data=script_getdata(st,3);
  5711. get_val(st,data);
  5712. if( data_isstring(data) ) //Char Name
  5713. tsd=map_nick2sd(conv_str(st,data));
  5714. else //Char Id was given
  5715. tsd=map_charid2sd(conv_num(st,data));
  5716. if( tsd == NULL )
  5717. { //Failed
  5718. script_pushint(st,0);
  5719. return 0;
  5720. }
  5721. memset(&item_tmp,0,sizeof(item_tmp));
  5722. item_tmp.nameid=nameid;
  5723. item_tmp.amount=1;
  5724. item_tmp.identify=1;
  5725. item_tmp.card[0]=CARD0_CREATE; //we don't use 255! because for example SIGNED WEAPON shouldn't get TOP10 BS Fame bonus [Lupus]
  5726. item_tmp.card[2]=tsd->status.char_id;
  5727. item_tmp.card[3]=tsd->status.char_id >> 16;
  5728. if(pc_additem(sd,&item_tmp,1)) {
  5729. script_pushint(st,0);
  5730. return 0; //Failed to add item, we will not drop if they don't fit
  5731. }
  5732. //Logs items, got from (N)PC scripts [Lupus]
  5733. if(log_config.enable_logs&0x40)
  5734. log_pick_pc(sd, "N", item_tmp.nameid, item_tmp.amount, &item_tmp);
  5735. script_pushint(st,1);
  5736. return 0;
  5737. }
  5738. /*==========================================
  5739. * gets a random item ID from an item group [Skotlex]
  5740. * groupranditem group_num
  5741. *------------------------------------------*/
  5742. BUILDIN_FUNC(grouprandomitem)
  5743. {
  5744. int group;
  5745. group = script_getnum(st,2);
  5746. script_pushint(st,itemdb_searchrandomid(group));
  5747. return 0;
  5748. }
  5749. /*==========================================
  5750. *
  5751. *------------------------------------------*/
  5752. BUILDIN_FUNC(makeitem)
  5753. {
  5754. int nameid,amount,flag = 0;
  5755. int x,y,m;
  5756. const char *mapname;
  5757. struct item item_tmp;
  5758. struct script_data *data;
  5759. data=script_getdata(st,2);
  5760. get_val(st,data);
  5761. if( data_isstring(data) ){
  5762. const char *name=conv_str(st,data);
  5763. struct item_data *item_data = itemdb_searchname(name);
  5764. if( item_data )
  5765. nameid=item_data->nameid;
  5766. else
  5767. nameid=UNKNOWN_ITEM_ID;
  5768. }else
  5769. nameid=conv_num(st,data);
  5770. amount=script_getnum(st,3);
  5771. mapname =script_getstr(st,4);
  5772. x =script_getnum(st,5);
  5773. y =script_getnum(st,6);
  5774. if(strcmp(mapname,"this")==0)
  5775. {
  5776. TBL_PC *sd;
  5777. sd = script_rid2sd(st);
  5778. if (!sd) return 0; //Failed...
  5779. m=sd->bl.m;
  5780. } else
  5781. m=map_mapname2mapid(mapname);
  5782. if(nameid<0) { // ランダム
  5783. nameid=itemdb_searchrandomid(-nameid);
  5784. flag = 1;
  5785. }
  5786. if(nameid > 0) {
  5787. memset(&item_tmp,0,sizeof(item_tmp));
  5788. item_tmp.nameid=nameid;
  5789. if(!flag)
  5790. item_tmp.identify=1;
  5791. else
  5792. item_tmp.identify=itemdb_isidentified(nameid);
  5793. map_addflooritem(&item_tmp,amount,m,x,y,NULL,NULL,NULL,0);
  5794. }
  5795. return 0;
  5796. }
  5797. /*==========================================
  5798. * script DELITEM command (fixed 2 bugs by Lupus, added deletion priority by Lupus)
  5799. *------------------------------------------*/
  5800. BUILDIN_FUNC(delitem)
  5801. {
  5802. int nameid=0,amount,i,important_item=0;
  5803. TBL_PC *sd;
  5804. struct script_data *data;
  5805. sd = script_rid2sd(st);
  5806. data=script_getdata(st,2);
  5807. get_val(st,data);
  5808. if( data_isstring(data) ){
  5809. const char *name=conv_str(st,data);
  5810. struct item_data *item_data = itemdb_searchname(name);
  5811. //nameid=UNKNOWN_ITEM_ID;
  5812. if( item_data )
  5813. nameid=item_data->nameid;
  5814. }else
  5815. nameid=conv_num(st,data);
  5816. amount=script_getnum(st,3);
  5817. if (nameid<500 || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5818. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5819. return 0;
  5820. }
  5821. //1st pass
  5822. //here we won't delete items with CARDS, named items but we count them
  5823. for(i=0;i<MAX_INVENTORY;i++){
  5824. //we don't delete wrong item or equipped item
  5825. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5826. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid)
  5827. continue;
  5828. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5829. if(sd->inventory_data[i]->type==IT_PETEGG &&
  5830. sd->status.inventory[i].card[0] == CARD0_PET)
  5831. {
  5832. if (!intif_delete_petdata(MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5833. continue; //pet couldn't be sent for deletion.
  5834. } else
  5835. //is this item important? does it have cards? or Player's name? or Refined/Upgraded
  5836. if(itemdb_isspecial(sd->status.inventory[i].card[0]) ||
  5837. sd->status.inventory[i].card[0] ||
  5838. sd->status.inventory[i].refine) {
  5839. //this is important item, count it (except for pet eggs)
  5840. if(sd->status.inventory[i].card[0] != CARD0_PET)
  5841. important_item++;
  5842. continue;
  5843. }
  5844. if(sd->status.inventory[i].amount>=amount){
  5845. //Logs items, got from (N)PC scripts [Lupus]
  5846. if(log_config.enable_logs&0x40)
  5847. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5848. pc_delitem(sd,i,amount,0);
  5849. return 0; //we deleted exact amount of items. now exit
  5850. } else {
  5851. amount-=sd->status.inventory[i].amount;
  5852. //Logs items, got from (N)PC scripts [Lupus]
  5853. if(log_config.enable_logs&0x40) {
  5854. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5855. }
  5856. //Logs
  5857. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5858. }
  5859. }
  5860. //2nd pass
  5861. //now if there WERE items with CARDs/REFINED/NAMED... and if we still have to delete some items. we'll delete them finally
  5862. if (important_item>0 && amount>0)
  5863. for(i=0;i<MAX_INVENTORY;i++){
  5864. //we don't delete wrong item
  5865. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5866. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid )
  5867. continue;
  5868. if(sd->status.inventory[i].amount>=amount){
  5869. //Logs items, got from (N)PC scripts [Lupus]
  5870. if(log_config.enable_logs&0x40)
  5871. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5872. pc_delitem(sd,i,amount,0);
  5873. return 0; //we deleted exact amount of items. now exit
  5874. } else {
  5875. amount-=sd->status.inventory[i].amount;
  5876. //Logs items, got from (N)PC scripts [Lupus]
  5877. if(log_config.enable_logs&0x40)
  5878. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5879. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5880. }
  5881. }
  5882. return 0;
  5883. }
  5884. /*==========================================
  5885. * advanced version of delitem [modified by Mihilion]
  5886. *------------------------------------------*/
  5887. BUILDIN_FUNC(delitem2)
  5888. {
  5889. int nameid=0,amount,i=0;
  5890. int iden,ref,attr,c1,c2,c3,c4;
  5891. TBL_PC *sd;
  5892. struct script_data *data;
  5893. sd = script_rid2sd(st);
  5894. data=script_getdata(st,2);
  5895. get_val(st,data);
  5896. if( data_isstring(data) ){
  5897. const char *name=conv_str(st,data);
  5898. struct item_data *item_data = itemdb_searchname(name);
  5899. //nameid=UNKNOWN_ITEM_ID;
  5900. if( item_data )
  5901. nameid=item_data->nameid;
  5902. }else
  5903. nameid=conv_num(st,data);
  5904. amount=script_getnum(st,3);
  5905. iden=script_getnum(st,4);
  5906. ref=script_getnum(st,5);
  5907. attr=script_getnum(st,6);
  5908. c1=script_getnum(st,7);
  5909. c2=script_getnum(st,8);
  5910. c3=script_getnum(st,9);
  5911. c4=script_getnum(st,10);
  5912. if (!itemdb_exists(nameid) || amount<=0 ) {//by Lupus. Don't run FOR if u got wrong item ID or amount<=0
  5913. //eprintf("wrong item ID or amount<=0 : delitem %i,\n",nameid,amount);
  5914. return 0;
  5915. }
  5916. for(i=0;i<MAX_INVENTORY;i++){
  5917. //we don't delete wrong item or equipped item
  5918. if(sd->status.inventory[i].nameid<=0 || sd->inventory_data[i] == NULL ||
  5919. sd->status.inventory[i].amount<=0 || sd->status.inventory[i].nameid!=nameid ||
  5920. sd->status.inventory[i].identify!=iden || sd->status.inventory[i].refine!=ref ||
  5921. sd->status.inventory[i].attribute!=attr || sd->status.inventory[i].card[0]!=c1 ||
  5922. sd->status.inventory[i].card[1]!=c2 || sd->status.inventory[i].card[2]!=c3 ||
  5923. sd->status.inventory[i].card[3]!=c4)
  5924. continue;
  5925. //1 egg uses 1 cell in the inventory. so it's ok to delete 1 pet / per cycle
  5926. if(sd->inventory_data[i]->type==IT_PETEGG && sd->status.inventory[i].card[0] == CARD0_PET)
  5927. {
  5928. if (!intif_delete_petdata( MakeDWord(sd->status.inventory[i].card[1], sd->status.inventory[i].card[2])))
  5929. continue; //Failed to send delete the pet.
  5930. }
  5931. if(sd->status.inventory[i].amount>=amount){
  5932. //Logs items, got from (N)PC scripts [Lupus]
  5933. if(log_config.enable_logs&0x40)
  5934. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -amount, &sd->status.inventory[i]);
  5935. pc_delitem(sd,i,amount,0);
  5936. return 0; //we deleted exact amount of items. now exit
  5937. } else {
  5938. amount-=sd->status.inventory[i].amount;
  5939. //Logs items, got from (N)PC scripts [Lupus]
  5940. if(log_config.enable_logs&0x40)
  5941. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  5942. pc_delitem(sd,i,sd->status.inventory[i].amount,0);
  5943. }
  5944. }
  5945. return 0;
  5946. }
  5947. /*==========================================
  5948. * Enables/Disables use of items while in an NPC [Skotlex]
  5949. *------------------------------------------*/
  5950. BUILDIN_FUNC(enableitemuse)
  5951. {
  5952. TBL_PC *sd;
  5953. sd=script_rid2sd(st);
  5954. if (sd)
  5955. sd->npc_item_flag = st->oid;
  5956. return 0;
  5957. }
  5958. BUILDIN_FUNC(disableitemuse)
  5959. {
  5960. TBL_PC *sd;
  5961. sd=script_rid2sd(st);
  5962. if (sd)
  5963. sd->npc_item_flag = 0;
  5964. return 0;
  5965. }
  5966. /*==========================================
  5967. *キャラ関係のパラメータ取得
  5968. *------------------------------------------*/
  5969. BUILDIN_FUNC(readparam)
  5970. {
  5971. int type;
  5972. TBL_PC *sd;
  5973. type=script_getnum(st,2);
  5974. if( script_hasdata(st,3) )
  5975. sd=map_nick2sd(script_getstr(st,3));
  5976. else
  5977. sd=script_rid2sd(st);
  5978. if(sd==NULL){
  5979. script_pushint(st,-1);
  5980. return 0;
  5981. }
  5982. script_pushint(st,pc_readparam(sd,type));
  5983. return 0;
  5984. }
  5985. /*==========================================
  5986. *キャラ関係のID取得
  5987. *------------------------------------------*/
  5988. BUILDIN_FUNC(getcharid)
  5989. {
  5990. int num;
  5991. TBL_PC *sd;
  5992. num=script_getnum(st,2);
  5993. if( script_hasdata(st,3) )
  5994. sd=map_nick2sd(script_getstr(st,3));
  5995. else
  5996. sd=script_rid2sd(st);
  5997. if(sd==NULL || num<0 || num>3){
  5998. script_pushint(st,0); //return 0, according docs
  5999. return 0;
  6000. }
  6001. switch (num) {
  6002. case 0: script_pushint(st,sd->status.char_id); break;
  6003. case 1: script_pushint(st,sd->status.party_id); break;
  6004. case 2: script_pushint(st,sd->status.guild_id); break;
  6005. case 3: script_pushint(st,sd->status.account_id); break;
  6006. default:
  6007. ShowError("buildin_getcharid: invalid .\n");
  6008. script_pushconststr(st,"");
  6009. break;
  6010. }
  6011. return 0;
  6012. }
  6013. /*==========================================
  6014. *指定IDのPT名取得
  6015. *------------------------------------------*/
  6016. char *buildin_getpartyname_sub(int party_id)
  6017. {
  6018. struct party_data *p;
  6019. p=party_search(party_id);
  6020. if(p!=NULL){
  6021. char *buf;
  6022. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  6023. memcpy(buf, p->party.name, NAME_LENGTH);
  6024. buf[NAME_LENGTH-1] = '\0';
  6025. return buf;
  6026. }
  6027. return 0;
  6028. }
  6029. BUILDIN_FUNC(getpartyname)
  6030. {
  6031. char *name;
  6032. int party_id;
  6033. party_id=script_getnum(st,2);
  6034. name=buildin_getpartyname_sub(party_id);
  6035. if(name != NULL)
  6036. script_pushstr(st,name);
  6037. else
  6038. script_pushconststr(st,"null");
  6039. return 0;
  6040. }
  6041. /*==========================================
  6042. *指定IDのPT人数とメンバーID取得
  6043. *------------------------------------------*/
  6044. BUILDIN_FUNC(getpartymember)
  6045. {
  6046. struct party_data *p;
  6047. int i,j=0,type=0;
  6048. p=party_search(script_getnum(st,2));
  6049. if( script_hasdata(st,3) )
  6050. type=script_getnum(st,3);
  6051. if(p!=NULL){
  6052. for(i=0;i<MAX_PARTY;i++){
  6053. if(p->party.member[i].account_id){
  6054. switch (type) {
  6055. case 2:
  6056. mapreg_setreg(add_str("$@partymemberaid")+(j<<24),p->party.member[i].account_id);
  6057. break;
  6058. case 1:
  6059. mapreg_setreg(add_str("$@partymembercid")+(j<<24),p->party.member[i].char_id);
  6060. break;
  6061. default:
  6062. mapreg_setregstr(add_str("$@partymembername$")+(j<<24),p->party.member[i].name);
  6063. }
  6064. j++;
  6065. }
  6066. }
  6067. }
  6068. mapreg_setreg(add_str("$@partymembercount"),j);
  6069. return 0;
  6070. }
  6071. /*==========================================
  6072. * Retrieves party leader. if flag is specified,
  6073. * return some of the leader data. Otherwise, return name.
  6074. *------------------------------------------*/
  6075. BUILDIN_FUNC(getpartyleader)
  6076. {
  6077. int party_id, type = 0, i=0;
  6078. struct party_data *p;
  6079. party_id=script_getnum(st,2);
  6080. if( script_hasdata(st,3) )
  6081. type=script_getnum(st,3);
  6082. p=party_search(party_id);
  6083. if (p) //Search leader
  6084. for(i = 0; i < MAX_PARTY && !p->party.member[i].leader; i++);
  6085. if (!p || i == MAX_PARTY) { //leader not found
  6086. if (type)
  6087. script_pushint(st,-1);
  6088. else
  6089. script_pushconststr(st,"null");
  6090. return 0;
  6091. }
  6092. switch (type) {
  6093. case 1: script_pushint(st,p->party.member[i].account_id); break;
  6094. case 2: script_pushint(st,p->party.member[i].char_id); break;
  6095. case 3: script_pushint(st,p->party.member[i].class_); break;
  6096. case 4: script_pushstr(st,aStrdup(mapindex_id2name(p->party.member[i].map))); break;
  6097. case 5: script_pushint(st,p->party.member[i].lv); break;
  6098. default: script_pushstr(st,aStrdup(p->party.member[i].name)); break;
  6099. }
  6100. return 0;
  6101. }
  6102. /*==========================================
  6103. *指定IDのギルド名取得
  6104. *------------------------------------------*/
  6105. char *buildin_getguildname_sub(int guild_id)
  6106. {
  6107. struct guild *g=NULL;
  6108. g=guild_search(guild_id);
  6109. if(g!=NULL){
  6110. char *buf;
  6111. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  6112. memcpy(buf, g->name, NAME_LENGTH);
  6113. buf[NAME_LENGTH-1] = '\0';
  6114. return buf;
  6115. }
  6116. return NULL;
  6117. }
  6118. BUILDIN_FUNC(getguildname)
  6119. {
  6120. char *name;
  6121. int guild_id=script_getnum(st,2);
  6122. name=buildin_getguildname_sub(guild_id);
  6123. if(name != NULL)
  6124. script_pushstr(st,name);
  6125. else
  6126. script_pushconststr(st,"null");
  6127. return 0;
  6128. }
  6129. /*==========================================
  6130. *指定IDのGuildMaster名取得
  6131. *------------------------------------------*/
  6132. char *buildin_getguildmaster_sub(int guild_id)
  6133. {
  6134. struct guild *g=NULL;
  6135. g=guild_search(guild_id);
  6136. if(g!=NULL){
  6137. char *buf;
  6138. buf=(char *)aMallocA(NAME_LENGTH*sizeof(char));
  6139. memcpy(buf, g->master, NAME_LENGTH);
  6140. buf[NAME_LENGTH-1] = '\0';
  6141. return buf;
  6142. }
  6143. return 0;
  6144. }
  6145. BUILDIN_FUNC(getguildmaster)
  6146. {
  6147. char *master;
  6148. int guild_id=script_getnum(st,2);
  6149. master=buildin_getguildmaster_sub(guild_id);
  6150. if(master!=0)
  6151. script_pushstr(st,master);
  6152. else
  6153. script_pushconststr(st,"null");
  6154. return 0;
  6155. }
  6156. BUILDIN_FUNC(getguildmasterid)
  6157. {
  6158. char *master;
  6159. TBL_PC *sd=NULL;
  6160. int guild_id=script_getnum(st,2);
  6161. master=buildin_getguildmaster_sub(guild_id);
  6162. if(master!=0){
  6163. if((sd=map_nick2sd(master)) == NULL){
  6164. script_pushint(st,0);
  6165. return 0;
  6166. }
  6167. script_pushint(st,sd->status.char_id);
  6168. }else{
  6169. script_pushint(st,0);
  6170. }
  6171. return 0;
  6172. }
  6173. /*==========================================
  6174. * キャラクタの名前
  6175. *------------------------------------------*/
  6176. BUILDIN_FUNC(strcharinfo)
  6177. {
  6178. TBL_PC *sd;
  6179. int num;
  6180. char *buf;
  6181. sd=script_rid2sd(st);
  6182. if (!sd) { //Avoid crashing....
  6183. script_pushconststr(st,"");
  6184. return 0;
  6185. }
  6186. num=script_getnum(st,2);
  6187. switch(num){
  6188. case 0:
  6189. script_pushstr(st,aStrdup(sd->status.name));
  6190. break;
  6191. case 1:
  6192. buf=buildin_getpartyname_sub(sd->status.party_id);
  6193. if(buf!=0)
  6194. script_pushstr(st,buf);
  6195. else
  6196. script_pushconststr(st,"");
  6197. break;
  6198. case 2:
  6199. buf=buildin_getguildname_sub(sd->status.guild_id);
  6200. if(buf != NULL)
  6201. script_pushstr(st,buf);
  6202. else
  6203. script_pushconststr(st,"");
  6204. break;
  6205. default:
  6206. ShowWarning("buildin_strcharinfo: unknown parameter.");
  6207. script_pushconststr(st,"");
  6208. break;
  6209. }
  6210. return 0;
  6211. }
  6212. 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};
  6213. /*==========================================
  6214. * GetEquipID(Pos); Pos: 1-10
  6215. *------------------------------------------*/
  6216. BUILDIN_FUNC(getequipid)
  6217. {
  6218. int i,num;
  6219. TBL_PC *sd;
  6220. struct item_data* item;
  6221. sd=script_rid2sd(st);
  6222. if(sd == NULL)
  6223. {
  6224. ShowError("getequipid: sd == NULL\n");
  6225. return 0;
  6226. }
  6227. num=script_getnum(st,2);
  6228. i=pc_checkequip(sd,equip[num-1]);
  6229. if(i >= 0){
  6230. item=sd->inventory_data[i];
  6231. if(item)
  6232. script_pushint(st,item->nameid);
  6233. else
  6234. script_pushint(st,0);
  6235. }else{
  6236. script_pushint(st,-1);
  6237. }
  6238. return 0;
  6239. }
  6240. /*==========================================
  6241. * 装備名文字列(精錬メニュー用)
  6242. *------------------------------------------*/
  6243. BUILDIN_FUNC(getequipname)
  6244. {
  6245. int i,num;
  6246. TBL_PC *sd;
  6247. struct item_data* item;
  6248. char *buf;
  6249. static char pos[11][100] = {"Head","Body","Left hand","Right hand","Robe","Shoes","Accessory 1","Accessory 2","Head 2","Head 3","Not Equipped"};
  6250. buf=(char *)aMallocA(64*sizeof(char));
  6251. sd=script_rid2sd(st);
  6252. num=script_getnum(st,2);
  6253. i=pc_checkequip(sd,equip[num-1]);
  6254. if(i >= 0){
  6255. item=sd->inventory_data[i];
  6256. if(item)
  6257. sprintf(buf,"%s-[%s]",pos[num-1],item->jname);
  6258. else
  6259. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  6260. }else{
  6261. sprintf(buf,"%s-[%s]",pos[num-1],pos[10]);
  6262. }
  6263. script_pushstr(st,buf);
  6264. return 0;
  6265. }
  6266. /*==========================================
  6267. * getbrokenid [Valaris]
  6268. *------------------------------------------*/
  6269. BUILDIN_FUNC(getbrokenid)
  6270. {
  6271. int i,num,id=0,brokencounter=0;
  6272. TBL_PC *sd;
  6273. sd=script_rid2sd(st);
  6274. num=script_getnum(st,2);
  6275. for(i=0; i<MAX_INVENTORY; i++) {
  6276. if(sd->status.inventory[i].attribute==1){
  6277. brokencounter++;
  6278. if(num==brokencounter){
  6279. id=sd->status.inventory[i].nameid;
  6280. break;
  6281. }
  6282. }
  6283. }
  6284. script_pushint(st,id);
  6285. return 0;
  6286. }
  6287. /*==========================================
  6288. * repair [Valaris]
  6289. *------------------------------------------*/
  6290. BUILDIN_FUNC(repair)
  6291. {
  6292. int i,num;
  6293. int repaircounter=0;
  6294. TBL_PC *sd;
  6295. sd=script_rid2sd(st);
  6296. num=script_getnum(st,2);
  6297. for(i=0; i<MAX_INVENTORY; i++) {
  6298. if(sd->status.inventory[i].attribute==1){
  6299. repaircounter++;
  6300. if(num==repaircounter){
  6301. sd->status.inventory[i].attribute=0;
  6302. clif_equiplist(sd);
  6303. clif_produceeffect(sd, 0, sd->status.inventory[i].nameid);
  6304. clif_misceffect(&sd->bl, 3);
  6305. break;
  6306. }
  6307. }
  6308. }
  6309. return 0;
  6310. }
  6311. /*==========================================
  6312. * 装備チェック
  6313. *------------------------------------------*/
  6314. BUILDIN_FUNC(getequipisequiped)
  6315. {
  6316. int i,num;
  6317. TBL_PC *sd;
  6318. num=script_getnum(st,2);
  6319. sd=script_rid2sd(st);
  6320. if ((num - 1) >= (sizeof(equip) / sizeof(equip[0])))
  6321. i = -1;
  6322. else
  6323. i=pc_checkequip(sd,equip[num-1]);
  6324. if(i >= 0)
  6325. script_pushint(st,1);
  6326. else
  6327. script_pushint(st,0);
  6328. return 0;
  6329. }
  6330. /*==========================================
  6331. * 装備品精錬可能チェック
  6332. *------------------------------------------*/
  6333. BUILDIN_FUNC(getequipisenableref)
  6334. {
  6335. int i,num;
  6336. TBL_PC *sd;
  6337. num=script_getnum(st,2);
  6338. sd=script_rid2sd(st);
  6339. i=pc_checkequip(sd,equip[num-1]);
  6340. if(i >= 0 && sd->inventory_data[i] && !sd->inventory_data[i]->flag.no_refine)
  6341. {
  6342. script_pushint(st,1);
  6343. } else {
  6344. script_pushint(st,0);
  6345. }
  6346. return 0;
  6347. }
  6348. /*==========================================
  6349. * 装備品鑑定チェック
  6350. *------------------------------------------*/
  6351. BUILDIN_FUNC(getequipisidentify)
  6352. {
  6353. int i,num;
  6354. TBL_PC *sd;
  6355. num=script_getnum(st,2);
  6356. sd=script_rid2sd(st);
  6357. i=pc_checkequip(sd,equip[num-1]);
  6358. if(i >= 0)
  6359. script_pushint(st,sd->status.inventory[i].identify);
  6360. else
  6361. script_pushint(st,0);
  6362. return 0;
  6363. }
  6364. /*==========================================
  6365. * 装備品精錬度
  6366. *------------------------------------------*/
  6367. BUILDIN_FUNC(getequiprefinerycnt)
  6368. {
  6369. int i,num;
  6370. TBL_PC *sd;
  6371. num=script_getnum(st,2);
  6372. sd=script_rid2sd(st);
  6373. i=pc_checkequip(sd,equip[num-1]);
  6374. if(i >= 0)
  6375. script_pushint(st,sd->status.inventory[i].refine);
  6376. else
  6377. script_pushint(st,0);
  6378. return 0;
  6379. }
  6380. /*==========================================
  6381. * 装備品武器LV
  6382. *------------------------------------------*/
  6383. BUILDIN_FUNC(getequipweaponlv)
  6384. {
  6385. int i,num;
  6386. TBL_PC *sd;
  6387. num=script_getnum(st,2);
  6388. sd=script_rid2sd(st);
  6389. i=pc_checkequip(sd,equip[num-1]);
  6390. if(i >= 0 && sd->inventory_data[i])
  6391. script_pushint(st,sd->inventory_data[i]->wlv);
  6392. else
  6393. script_pushint(st,0);
  6394. return 0;
  6395. }
  6396. /*==========================================
  6397. * 装備品精錬成功率
  6398. *------------------------------------------*/
  6399. BUILDIN_FUNC(getequippercentrefinery)
  6400. {
  6401. int i,num;
  6402. TBL_PC *sd;
  6403. num=script_getnum(st,2);
  6404. sd=script_rid2sd(st);
  6405. i=pc_checkequip(sd,equip[num-1]);
  6406. if(i >= 0 && sd->status.inventory[i].nameid && sd->status.inventory[i].refine < MAX_REFINE)
  6407. script_pushint(st,percentrefinery[itemdb_wlv(sd->status.inventory[i].nameid)][(int)sd->status.inventory[i].refine]);
  6408. else
  6409. script_pushint(st,0);
  6410. return 0;
  6411. }
  6412. /*==========================================
  6413. * 精錬成功
  6414. *------------------------------------------*/
  6415. BUILDIN_FUNC(successrefitem)
  6416. {
  6417. int i,num,ep;
  6418. TBL_PC *sd;
  6419. num=script_getnum(st,2);
  6420. sd=script_rid2sd(st);
  6421. i=pc_checkequip(sd,equip[num-1]);
  6422. if(i >= 0) {
  6423. ep=sd->status.inventory[i].equip;
  6424. //Logs items, got from (N)PC scripts [Lupus]
  6425. if(log_config.enable_logs&0x40)
  6426. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6427. sd->status.inventory[i].refine++;
  6428. pc_unequipitem(sd,i,2);
  6429. clif_refine(sd->fd,0,i,sd->status.inventory[i].refine);
  6430. clif_delitem(sd,i,1);
  6431. //Logs items, got from (N)PC scripts [Lupus]
  6432. if(log_config.enable_logs&0x40)
  6433. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, 1, &sd->status.inventory[i]);
  6434. clif_additem(sd,i,1,0);
  6435. pc_equipitem(sd,i,ep);
  6436. clif_misceffect(&sd->bl,3);
  6437. if(sd->status.inventory[i].refine == MAX_REFINE &&
  6438. sd->status.inventory[i].card[0] == CARD0_FORGE &&
  6439. sd->status.char_id == (int)MakeDWord(sd->status.inventory[i].card[2],sd->status.inventory[i].card[3])
  6440. ){ // Fame point system [DracoRPG]
  6441. switch (sd->inventory_data[i]->wlv){
  6442. case 1:
  6443. pc_addfame(sd,1); // Success to refine to +10 a lv1 weapon you forged = +1 fame point
  6444. break;
  6445. case 2:
  6446. pc_addfame(sd,25); // Success to refine to +10 a lv2 weapon you forged = +25 fame point
  6447. break;
  6448. case 3:
  6449. pc_addfame(sd,1000); // Success to refine to +10 a lv3 weapon you forged = +1000 fame point
  6450. break;
  6451. }
  6452. }
  6453. }
  6454. return 0;
  6455. }
  6456. /*==========================================
  6457. * 精錬失敗
  6458. *------------------------------------------*/
  6459. BUILDIN_FUNC(failedrefitem)
  6460. {
  6461. int i,num;
  6462. TBL_PC *sd;
  6463. num=script_getnum(st,2);
  6464. sd=script_rid2sd(st);
  6465. i=pc_checkequip(sd,equip[num-1]);
  6466. if(i >= 0) {
  6467. //Logs items, got from (N)PC scripts [Lupus]
  6468. if(log_config.enable_logs&0x40)
  6469. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  6470. sd->status.inventory[i].refine = 0;
  6471. pc_unequipitem(sd,i,3);
  6472. // 精錬失敗エフェクトのパケット
  6473. clif_refine(sd->fd,1,i,sd->status.inventory[i].refine);
  6474. pc_delitem(sd,i,1,0);
  6475. // 他の人にも失敗を通知
  6476. clif_misceffect(&sd->bl,2);
  6477. }
  6478. return 0;
  6479. }
  6480. /*==========================================
  6481. *
  6482. *------------------------------------------*/
  6483. BUILDIN_FUNC(statusup)
  6484. {
  6485. int type;
  6486. TBL_PC *sd;
  6487. type=script_getnum(st,2);
  6488. sd=script_rid2sd(st);
  6489. pc_statusup(sd,type);
  6490. return 0;
  6491. }
  6492. /*==========================================
  6493. *
  6494. *------------------------------------------*/
  6495. BUILDIN_FUNC(statusup2)
  6496. {
  6497. int type,val;
  6498. TBL_PC *sd;
  6499. type=script_getnum(st,2);
  6500. val=script_getnum(st,3);
  6501. sd=script_rid2sd(st);
  6502. pc_statusup2(sd,type,val);
  6503. return 0;
  6504. }
  6505. /// See 'doc/item_bonus.txt'
  6506. ///
  6507. /// bonus <bonus type>,<val1>;
  6508. /// bonus2 <bonus type>,<val1>,<val2>;
  6509. /// bonus3 <bonus type>,<val1>,<val2>,<val3>;
  6510. /// bonus4 <bonus type>,<val1>,<val2>,<val3>,<val4>;
  6511. /// bonus5 <bonus type>,<val1>,<val2>,<val3>,<val4>,<val5>;
  6512. BUILDIN_FUNC(bonus)
  6513. {
  6514. int type;
  6515. int type2;
  6516. int type3;
  6517. int type4;
  6518. int type5;
  6519. int val;
  6520. TBL_PC* sd;
  6521. sd = script_rid2sd(st);
  6522. if( sd == NULL )
  6523. return 1; // no player attached
  6524. type = script_getnum(st,2);
  6525. switch( script_lastdata(st) ){
  6526. case 3:
  6527. val = script_getnum(st,3);
  6528. pc_bonus(sd, type, val);
  6529. break;
  6530. case 4:
  6531. type2 = script_getnum(st,3);
  6532. val = script_getnum(st,4);
  6533. pc_bonus2(sd, type, type2, val);
  6534. break;
  6535. case 5:
  6536. type2 = script_getnum(st,3);
  6537. type3 = script_getnum(st,4);
  6538. val = script_getnum(st,5);
  6539. pc_bonus3(sd, type, type2, type3, val);
  6540. break;
  6541. case 6:
  6542. type2 = script_getnum(st,3);
  6543. type3 = script_getnum(st,4);
  6544. type4 = script_getnum(st,5);
  6545. val = script_getnum(st,6);
  6546. pc_bonus4(sd, type, type2, type3, type4, val);
  6547. break;
  6548. case 7:
  6549. type2 = script_getnum(st,3);
  6550. type3 = script_getnum(st,4);
  6551. type4 = script_getnum(st,5);
  6552. type5 = script_getnum(st,6);
  6553. val = script_getnum(st,7);
  6554. pc_bonus5(sd, type, type2, type3, type4, type5, val);
  6555. break;
  6556. default:
  6557. ShowDebug("buildin_bonus: unexpected number of arguments (%d)\n", (script_lastdata(st) - 2));
  6558. }
  6559. return 0;
  6560. }
  6561. /// Changes the level of a player skill.
  6562. /// <flag> defaults to 1
  6563. /// <flag>=0 : set the level of the skill
  6564. /// <flag>=1 : set the temporary level of the skill
  6565. /// <flag>=2 : add to the level of the skill
  6566. ///
  6567. /// skill <skill id>,<level>,<flag>
  6568. /// skill <skill id>,<level>
  6569. BUILDIN_FUNC(skill)
  6570. {
  6571. int id;
  6572. int level;
  6573. int flag = 1;
  6574. TBL_PC* sd;
  6575. sd = script_rid2sd(st);
  6576. if( sd == NULL )
  6577. return 1;// no player attached, report source
  6578. id = script_getnum(st,2);
  6579. level = script_getnum(st,3);
  6580. if( script_hasdata(st,4) )
  6581. flag = script_getnum(st,4);
  6582. pc_skill(sd, id, level, flag);
  6583. return 0;
  6584. }
  6585. /// Changes the level of a player skill.
  6586. /// like skill, but <flag> defaults to 2
  6587. ///
  6588. /// addtoskill <skill id>,<amount>,<flag>
  6589. /// addtoskill <skill id>,<amount>
  6590. ///
  6591. /// @see skill
  6592. BUILDIN_FUNC(addtoskill)
  6593. {
  6594. int id;
  6595. int level;
  6596. int flag = 2;
  6597. TBL_PC* sd;
  6598. sd = script_rid2sd(st);
  6599. if( sd == NULL )
  6600. return 1;// no player attached, report source
  6601. id = script_getnum(st,2);
  6602. level = script_getnum(st,3);
  6603. if( script_hasdata(st,4) )
  6604. flag = script_getnum(st,4);
  6605. pc_skill(sd, id, level, flag);
  6606. return 0;
  6607. }
  6608. /// Increases the level of a guild skill.
  6609. ///
  6610. /// guildskill <skill id>,<amount>;
  6611. BUILDIN_FUNC(guildskill)
  6612. {
  6613. int id;
  6614. int level;
  6615. TBL_PC* sd;
  6616. int i;
  6617. sd = script_rid2sd(st);
  6618. if( sd == NULL )
  6619. return 1;// no player attached, report source
  6620. id = script_getnum(st,2);
  6621. level = script_getnum(st,3);
  6622. for( i=0; i < level; i++ )
  6623. guild_skillup(sd, id);
  6624. return 0;
  6625. }
  6626. /// Returns the level of the player skill.
  6627. ///
  6628. /// getskilllv(<skill id>) -> <level>
  6629. BUILDIN_FUNC(getskilllv)
  6630. {
  6631. int id;
  6632. TBL_PC* sd;
  6633. sd = script_rid2sd(st);
  6634. if( sd == NULL )
  6635. return 1;// no player attached, report source
  6636. id = script_getnum(st,2);
  6637. script_pushint(st, pc_checkskill(sd,id));
  6638. return 0;
  6639. }
  6640. /// Returns the level of the guild skill.
  6641. ///
  6642. /// getgdskilllv(<guild id>,<skill id>) -> <level>
  6643. BUILDIN_FUNC(getgdskilllv)
  6644. {
  6645. int guild_id;
  6646. int skill_id;
  6647. struct guild* g;
  6648. guild_id = script_getnum(st,2);
  6649. skill_id = script_getnum(st,3);
  6650. g = guild_search(guild_id);
  6651. if( g == NULL )
  6652. script_pushint(st, -1);
  6653. else
  6654. script_pushint(st, guild_checkskill(g,skill_id));
  6655. return 0;
  6656. }
  6657. /// Returns the 'basic_skill_check' setting.
  6658. /// This config determines if the server checks the skill level of NV_BASIC
  6659. /// before allowing the basic actions.
  6660. ///
  6661. /// basicskillcheck() -> <bool>
  6662. BUILDIN_FUNC(basicskillcheck)
  6663. {
  6664. script_pushint(st, battle_config.basic_skill_check);
  6665. return 0;
  6666. }
  6667. /// Returns the GM level of the player.
  6668. ///
  6669. /// getgmlevel() -> <level>
  6670. BUILDIN_FUNC(getgmlevel)
  6671. {
  6672. TBL_PC* sd;
  6673. sd = script_rid2sd(st);
  6674. if( sd == NULL )
  6675. return 1;// no player attached, report source
  6676. script_pushint(st, pc_isGM(sd));
  6677. return 0;
  6678. }
  6679. /// Terminates the execution of this script instance.
  6680. ///
  6681. /// end
  6682. BUILDIN_FUNC(end)
  6683. {
  6684. st->state = END;
  6685. return 0;
  6686. }
  6687. /// Checks if the player has that effect state (option).
  6688. ///
  6689. /// checkoption(<option>) -> <bool>
  6690. BUILDIN_FUNC(checkoption)
  6691. {
  6692. int option;
  6693. TBL_PC* sd;
  6694. sd = script_rid2sd(st);
  6695. if( sd == NULL )
  6696. return 1;// no player attached, report source
  6697. option = script_getnum(st,2);
  6698. if( sd->sc.option&option )
  6699. script_pushint(st, 1);
  6700. else
  6701. script_pushint(st, 0);
  6702. return 0;
  6703. }
  6704. /// Checks if the player is in that body state (opt1).
  6705. ///
  6706. /// checkoption1(<opt1>) -> <bool>
  6707. BUILDIN_FUNC(checkoption1)
  6708. {
  6709. int opt1;
  6710. TBL_PC* sd;
  6711. sd = script_rid2sd(st);
  6712. if( sd == NULL )
  6713. return 1;// no player attached, report source
  6714. opt1 = script_getnum(st,2);
  6715. if( sd->sc.opt1 == opt1 )
  6716. script_pushint(st, 1);
  6717. else
  6718. script_pushint(st, 0);
  6719. return 0;
  6720. }
  6721. /// Checks if the player has that health state (opt2).
  6722. ///
  6723. /// checkoption2(<opt2>) -> <bool>
  6724. BUILDIN_FUNC(checkoption2)
  6725. {
  6726. int opt2;
  6727. TBL_PC* sd;
  6728. sd = script_rid2sd(st);
  6729. if( sd == NULL )
  6730. return 1;// no player attached, report source
  6731. opt2 = script_getnum(st,2);
  6732. if( sd->sc.opt2&opt2 )
  6733. script_pushint(st, 1);
  6734. else
  6735. script_pushint(st, 0);
  6736. return 0;
  6737. }
  6738. /// Changes the effect state (option) of the player.
  6739. /// <flag> defaults to 1
  6740. /// <flag>=0 : removes the option
  6741. /// <flag>=other : adds the option
  6742. ///
  6743. /// setoption <option>,<flag>;
  6744. /// setoption <option>;
  6745. BUILDIN_FUNC(setoption)
  6746. {
  6747. int option;
  6748. int flag = 1;
  6749. TBL_PC* sd;
  6750. sd = script_rid2sd(st);
  6751. if( sd == NULL )
  6752. return 1;// no player attached, report source
  6753. option = script_getnum(st,2);
  6754. if( script_hasdata(st,3) )
  6755. flag = script_getnum(st,3);
  6756. else if( !option ){// Request to remove everything.
  6757. flag = 0;
  6758. option = OPTION_CART|OPTION_FALCON|OPTION_RIDING;
  6759. }
  6760. if( flag ){// Add option
  6761. if( option&OPTION_WEDDING && !battle_config.wedding_modifydisplay )
  6762. option &= ~OPTION_WEDDING;// Do not show the wedding sprites
  6763. pc_setoption(sd, sd->sc.option|option);
  6764. } else// Remove option
  6765. pc_setoption(sd, sd->sc.option&~option);
  6766. return 0;
  6767. }
  6768. /// Returns if the player has a cart.
  6769. ///
  6770. /// checkcart() -> <bool>
  6771. ///
  6772. /// @author Valaris
  6773. BUILDIN_FUNC(checkcart)
  6774. {
  6775. TBL_PC* sd;
  6776. sd = script_rid2sd(st);
  6777. if( sd == NULL )
  6778. return 1;// no player attached, report source
  6779. if( pc_iscarton(sd) )
  6780. script_pushint(st, 1);
  6781. else
  6782. script_pushint(st, 0);
  6783. return 0;
  6784. }
  6785. /// Sets the cart of the player.
  6786. /// <type> defaults to 1
  6787. /// <type>=0 : removes the cart
  6788. /// <type>=1 : Normal cart
  6789. /// <type>=2 : Wooden cart
  6790. /// <type>=3 : Covered cart with flowers and ferns
  6791. /// <type>=4 : Wooden cart with a Panda doll on the back
  6792. /// <type>=5 : Normal cart with bigger wheels, a roof and a banner on the back
  6793. ///
  6794. /// setcart <type>;
  6795. /// setcart;
  6796. BUILDIN_FUNC(setcart)
  6797. {
  6798. int type = 1;
  6799. TBL_PC* sd;
  6800. sd = script_rid2sd(st);
  6801. if( sd == NULL )
  6802. return 1;// no player attached, report source
  6803. if( script_hasdata(st,2) )
  6804. type = script_getnum(st,2);
  6805. pc_setcart(sd, type);
  6806. return 0;
  6807. }
  6808. /// Returns if the player has a falcon.
  6809. ///
  6810. /// checkfalcon() -> <bool>
  6811. ///
  6812. /// @author Valaris
  6813. BUILDIN_FUNC(checkfalcon)
  6814. {
  6815. TBL_PC* sd;
  6816. sd = script_rid2sd(st);
  6817. if( sd == NULL )
  6818. return 1;// no player attached, report source
  6819. if( pc_isfalcon(sd) )
  6820. script_pushint(st, 1);
  6821. else
  6822. script_pushint(st, 0);
  6823. return 0;
  6824. }
  6825. /// Sets if the player has a falcon or not.
  6826. /// <flag> defaults to 1
  6827. ///
  6828. /// setfalcon <flag>;
  6829. /// setfalcon;
  6830. BUILDIN_FUNC(setfalcon)
  6831. {
  6832. int flag = 1;
  6833. TBL_PC* sd;
  6834. sd = script_rid2sd(st);
  6835. if( sd == NULL )
  6836. return 1;// no player attached, report source
  6837. if( script_hasdata(st,2) )
  6838. flag = script_getnum(st,2);
  6839. pc_setfalcon(sd, flag);
  6840. return 0;
  6841. }
  6842. /// Returns if the player is riding.
  6843. ///
  6844. /// checkriding() -> <bool>
  6845. ///
  6846. /// @author Valaris
  6847. BUILDIN_FUNC(checkriding)
  6848. {
  6849. TBL_PC* sd;
  6850. sd = script_rid2sd(st);
  6851. if( sd == NULL )
  6852. return 1;// no player attached, report source
  6853. if( pc_isriding(sd) )
  6854. script_pushint(st, 1);
  6855. else
  6856. script_pushint(st, 0);
  6857. return 0;
  6858. }
  6859. /// Sets if the player is riding.
  6860. /// <flag> defaults to 1
  6861. ///
  6862. /// setriding <flag>;
  6863. /// setriding;
  6864. BUILDIN_FUNC(setriding)
  6865. {
  6866. int flag = 1;
  6867. TBL_PC* sd;
  6868. sd = script_rid2sd(st);
  6869. if( sd == NULL )
  6870. return 1;// no player attached, report source
  6871. if( script_hasdata(st,2) )
  6872. flag = script_getnum(st,2);
  6873. pc_setriding(sd, flag);
  6874. return 0;
  6875. }
  6876. /// Sets the save point of the player.
  6877. ///
  6878. /// save "<map name>",<x>,<y>
  6879. /// savepoint "<map name>",<x>,<y>
  6880. BUILDIN_FUNC(savepoint)
  6881. {
  6882. int x;
  6883. int y;
  6884. short map;
  6885. const char* str;
  6886. TBL_PC* sd;
  6887. sd = script_rid2sd(st);
  6888. if( sd == NULL )
  6889. return 1;// no player attached, report source
  6890. str = script_getstr(st, 2);
  6891. x = script_getnum(st,3);
  6892. y = script_getnum(st,4);
  6893. map = mapindex_name2id(str);
  6894. if( map )
  6895. pc_setsavepoint(sd, map, x, y);
  6896. return 0;
  6897. }
  6898. /*==========================================
  6899. * GetTimeTick(0: System Tick, 1: Time Second Tick)
  6900. *------------------------------------------*/
  6901. BUILDIN_FUNC(gettimetick) /* Asgard Version */
  6902. {
  6903. int type;
  6904. time_t timer;
  6905. struct tm *t;
  6906. type=script_getnum(st,2);
  6907. switch(type){
  6908. case 2:
  6909. //type 2:(Get the number of seconds elapsed since 00:00 hours, Jan 1, 1970 UTC
  6910. // from the system clock.)
  6911. script_pushint(st,(int)time(NULL));
  6912. break;
  6913. case 1:
  6914. //type 1:(Second Ticks: 0-86399, 00:00:00-23:59:59)
  6915. time(&timer);
  6916. t=localtime(&timer);
  6917. script_pushint(st,((t->tm_hour)*3600+(t->tm_min)*60+t->tm_sec));
  6918. break;
  6919. case 0:
  6920. default:
  6921. //type 0:(System Ticks)
  6922. script_pushint(st,gettick());
  6923. break;
  6924. }
  6925. return 0;
  6926. }
  6927. /*==========================================
  6928. * GetTime(Type);
  6929. * 1: Sec 2: Min 3: Hour
  6930. * 4: WeekDay 5: MonthDay 6: Month
  6931. * 7: Year
  6932. *------------------------------------------*/
  6933. BUILDIN_FUNC(gettime) /* Asgard Version */
  6934. {
  6935. int type;
  6936. time_t timer;
  6937. struct tm *t;
  6938. type=script_getnum(st,2);
  6939. time(&timer);
  6940. t=localtime(&timer);
  6941. switch(type){
  6942. case 1://Sec(0~59)
  6943. script_pushint(st,t->tm_sec);
  6944. break;
  6945. case 2://Min(0~59)
  6946. script_pushint(st,t->tm_min);
  6947. break;
  6948. case 3://Hour(0~23)
  6949. script_pushint(st,t->tm_hour);
  6950. break;
  6951. case 4://WeekDay(0~6)
  6952. script_pushint(st,t->tm_wday);
  6953. break;
  6954. case 5://MonthDay(01~31)
  6955. script_pushint(st,t->tm_mday);
  6956. break;
  6957. case 6://Month(01~12)
  6958. script_pushint(st,t->tm_mon+1);
  6959. break;
  6960. case 7://Year(20xx)
  6961. script_pushint(st,t->tm_year+1900);
  6962. break;
  6963. case 8://Year Day(01~366)
  6964. script_pushint(st,t->tm_yday+1);
  6965. break;
  6966. default://(format error)
  6967. script_pushint(st,-1);
  6968. break;
  6969. }
  6970. return 0;
  6971. }
  6972. /*==========================================
  6973. * GetTimeStr("TimeFMT", Length);
  6974. *------------------------------------------*/
  6975. BUILDIN_FUNC(gettimestr)
  6976. {
  6977. char *tmpstr;
  6978. const char *fmtstr;
  6979. int maxlen;
  6980. time_t now = time(NULL);
  6981. fmtstr=script_getstr(st,2);
  6982. maxlen=script_getnum(st,3);
  6983. tmpstr=(char *)aMallocA((maxlen+1)*sizeof(char));
  6984. strftime(tmpstr,maxlen,fmtstr,localtime(&now));
  6985. tmpstr[maxlen]='\0';
  6986. script_pushstr(st,tmpstr);
  6987. return 0;
  6988. }
  6989. /*==========================================
  6990. * カプラ倉庫を開く
  6991. *------------------------------------------*/
  6992. BUILDIN_FUNC(openstorage)
  6993. {
  6994. storage_storageopen(script_rid2sd(st));
  6995. return 0;
  6996. }
  6997. BUILDIN_FUNC(guildopenstorage)
  6998. {
  6999. TBL_PC *sd=script_rid2sd(st);
  7000. int ret;
  7001. ret = storage_guild_storageopen(sd);
  7002. script_pushint(st,ret);
  7003. return 0;
  7004. }
  7005. /*==========================================
  7006. * アイテムによるスキル発動
  7007. *------------------------------------------*/
  7008. BUILDIN_FUNC(itemskill)
  7009. {
  7010. int id,lv;
  7011. TBL_PC *sd=script_rid2sd(st);
  7012. id=script_getnum(st,2);
  7013. lv=script_getnum(st,3);
  7014. if(!sd || sd->ud.skilltimer != -1)
  7015. return 0;
  7016. sd->skillitem=id;
  7017. sd->skillitemlv=lv;
  7018. clif_item_skill(sd,id,lv);
  7019. return 0;
  7020. }
  7021. /*==========================================
  7022. * アイテム作成
  7023. *------------------------------------------*/
  7024. BUILDIN_FUNC(produce)
  7025. {
  7026. int trigger;
  7027. TBL_PC *sd=script_rid2sd(st);
  7028. trigger=script_getnum(st,2);
  7029. clif_skill_produce_mix_list(sd, trigger);
  7030. return 0;
  7031. }
  7032. /*==========================================
  7033. * NPCでペット作る
  7034. *------------------------------------------*/
  7035. BUILDIN_FUNC(makepet)
  7036. {
  7037. TBL_PC *sd = script_rid2sd(st);
  7038. int id,pet_id;
  7039. id=script_getnum(st,2);
  7040. pet_id = search_petDB_index(id, PET_CLASS);
  7041. if (pet_id < 0)
  7042. pet_id = search_petDB_index(id, PET_EGG);
  7043. if (pet_id >= 0 && sd) {
  7044. sd->catch_target_class = pet_db[pet_id].class_;
  7045. intif_create_pet(
  7046. sd->status.account_id, sd->status.char_id,
  7047. (short)pet_db[pet_id].class_, (short)mob_db(pet_db[pet_id].class_)->lv,
  7048. (short)pet_db[pet_id].EggID, 0, (short)pet_db[pet_id].intimate,
  7049. 100, 0, 1, pet_db[pet_id].jname);
  7050. }
  7051. return 0;
  7052. }
  7053. /*==========================================
  7054. * NPCで経験値上げる
  7055. *------------------------------------------*/
  7056. BUILDIN_FUNC(getexp)
  7057. {
  7058. TBL_PC *sd = script_rid2sd(st);
  7059. int base=0,job=0;
  7060. double bonus;
  7061. nullpo_retr(0, sd);
  7062. base=script_getnum(st,2);
  7063. job =script_getnum(st,3);
  7064. if(base<0 || job<0)
  7065. return 0;
  7066. // bonus for npc-given exp
  7067. bonus = battle_config.quest_exp_rate / 100.;
  7068. base = (int) cap_value(base * bonus, 0, INT_MAX);
  7069. job = (int) cap_value(job * bonus, 0, INT_MAX);
  7070. pc_gainexp(sd, NULL, base, job);
  7071. return 0;
  7072. }
  7073. /*==========================================
  7074. * Gain guild exp [Celest]
  7075. *------------------------------------------*/
  7076. BUILDIN_FUNC(guildgetexp)
  7077. {
  7078. TBL_PC *sd = script_rid2sd(st);
  7079. int exp;
  7080. exp = script_getnum(st,2);
  7081. if(exp < 0)
  7082. return 0;
  7083. if(sd && sd->status.guild_id > 0)
  7084. guild_getexp (sd, exp);
  7085. return 0;
  7086. }
  7087. /*==========================================
  7088. * Changes the guild master of a guild [Skotlex]
  7089. *------------------------------------------*/
  7090. BUILDIN_FUNC(guildchangegm)
  7091. {
  7092. TBL_PC *sd;
  7093. int guild_id;
  7094. const char *name;
  7095. guild_id = script_getnum(st,2);
  7096. name = script_getstr(st,3);
  7097. sd=map_nick2sd(name);
  7098. if (!sd)
  7099. script_pushint(st,0);
  7100. else
  7101. script_pushint(st,guild_gm_change(guild_id, sd));
  7102. return 0;
  7103. }
  7104. /*==========================================
  7105. * モンスター発生
  7106. *------------------------------------------*/
  7107. BUILDIN_FUNC(monster)
  7108. {
  7109. int class_,amount,x,y;
  7110. const char *str,*map,*event="";
  7111. map =script_getstr(st,2);
  7112. x =script_getnum(st,3);
  7113. y =script_getnum(st,4);
  7114. str =script_getstr(st,5);
  7115. class_=script_getnum(st,6);
  7116. amount=script_getnum(st,7);
  7117. if( script_hasdata(st,8) ){
  7118. event=script_getstr(st,8);
  7119. check_event(st, event);
  7120. }
  7121. if (class_ >= 0 && !mobdb_checkid(class_)) {
  7122. ShowWarning("buildin_monster: Attempted to spawn non-existing monster class %d\n", class_);
  7123. return 1;
  7124. }
  7125. mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,amount,event);
  7126. return 0;
  7127. }
  7128. /*==========================================
  7129. * モンスター発生
  7130. *------------------------------------------*/
  7131. BUILDIN_FUNC(areamonster)
  7132. {
  7133. int class_,amount,x0,y0,x1,y1;
  7134. const char *str,*map,*event="";
  7135. map =script_getstr(st,2);
  7136. x0 =script_getnum(st,3);
  7137. y0 =script_getnum(st,4);
  7138. x1 =script_getnum(st,5);
  7139. y1 =script_getnum(st,6);
  7140. str =script_getstr(st,7);
  7141. class_=script_getnum(st,8);
  7142. amount=script_getnum(st,9);
  7143. if( script_hasdata(st,10) ){
  7144. event=script_getstr(st,10);
  7145. check_event(st, event);
  7146. }
  7147. mob_once_spawn_area(map_id2sd(st->rid),map,x0,y0,x1,y1,str,class_,amount,event);
  7148. return 0;
  7149. }
  7150. /*==========================================
  7151. * モンスター削除
  7152. *------------------------------------------*/
  7153. static int buildin_killmonster_sub(struct block_list *bl,va_list ap)
  7154. {
  7155. TBL_MOB* md = (TBL_MOB*)bl;
  7156. char *event=va_arg(ap,char *);
  7157. int allflag=va_arg(ap,int);
  7158. if(!allflag){
  7159. if(strcmp(event,md->npc_event)==0)
  7160. status_kill(bl);
  7161. }else{
  7162. if(!md->spawn)
  7163. status_kill(bl);
  7164. }
  7165. return 0;
  7166. }
  7167. BUILDIN_FUNC(killmonster)
  7168. {
  7169. const char *mapname,*event;
  7170. int m,allflag=0;
  7171. mapname=script_getstr(st,2);
  7172. event=script_getstr(st,3);
  7173. if(strcmp(event,"All")==0)
  7174. allflag = 1;
  7175. else
  7176. check_event(st, event);
  7177. if( (m=map_mapname2mapid(mapname))<0 )
  7178. return 0;
  7179. map_foreachinmap(buildin_killmonster_sub, m, BL_MOB, event ,allflag);
  7180. return 0;
  7181. }
  7182. static int buildin_killmonsterall_sub(struct block_list *bl,va_list ap)
  7183. {
  7184. status_kill(bl);
  7185. return 0;
  7186. }
  7187. BUILDIN_FUNC(killmonsterall)
  7188. {
  7189. const char *mapname;
  7190. int m;
  7191. mapname=script_getstr(st,2);
  7192. if( (m=map_mapname2mapid(mapname))<0 )
  7193. return 0;
  7194. map_foreachinmap(buildin_killmonsterall_sub,
  7195. m,BL_MOB);
  7196. return 0;
  7197. }
  7198. /*==========================================
  7199. * Creates a clone of a player.
  7200. * clone map, x, y, event, char_id, master_id, mode, flag, duration
  7201. *------------------------------------------*/
  7202. BUILDIN_FUNC(clone)
  7203. {
  7204. TBL_PC *sd, *msd=NULL;
  7205. int char_id,master_id=0,x,y, mode = 0, flag = 0, m;
  7206. unsigned int duration = 0;
  7207. const char *map,*event="";
  7208. map=script_getstr(st,2);
  7209. x=script_getnum(st,3);
  7210. y=script_getnum(st,4);
  7211. event=script_getstr(st,5);
  7212. char_id=script_getnum(st,6);
  7213. if( script_hasdata(st,7) )
  7214. master_id=script_getnum(st,7);
  7215. if( script_hasdata(st,8) )
  7216. mode=script_getnum(st,8);
  7217. if( script_hasdata(st,9) )
  7218. flag=script_getnum(st,9);
  7219. if( script_hasdata(st,10) )
  7220. duration=script_getnum(st,10);
  7221. check_event(st, event);
  7222. m = map_mapname2mapid(map);
  7223. if (m < 0) return 0;
  7224. sd = map_charid2sd(char_id);
  7225. if (master_id) {
  7226. msd = map_charid2sd(master_id);
  7227. if (msd)
  7228. master_id = msd->bl.id;
  7229. else
  7230. master_id = 0;
  7231. }
  7232. if (sd) //Return ID of newly crafted clone.
  7233. script_pushint(st,mob_clone_spawn(sd, m, x, y, event, master_id, mode, flag, 1000*duration));
  7234. else //Failed to create clone.
  7235. script_pushint(st,0);
  7236. return 0;
  7237. }
  7238. /*==========================================
  7239. * イベント実行
  7240. *------------------------------------------*/
  7241. BUILDIN_FUNC(doevent)
  7242. {
  7243. const char *event;
  7244. event=script_getstr(st,2);
  7245. check_event(st, event);
  7246. npc_event(map_id2sd(st->rid),event,0);
  7247. return 0;
  7248. }
  7249. /*==========================================
  7250. * NPC主体イベント実行
  7251. *------------------------------------------*/
  7252. BUILDIN_FUNC(donpcevent)
  7253. {
  7254. const char *event;
  7255. event=script_getstr(st,2);
  7256. check_event(st, event);
  7257. npc_event_do(event);
  7258. return 0;
  7259. }
  7260. /*==========================================
  7261. * イベントタイマー追加
  7262. *------------------------------------------*/
  7263. BUILDIN_FUNC(addtimer)
  7264. {
  7265. const char *event;
  7266. int tick;
  7267. tick=script_getnum(st,2);
  7268. event=script_getstr(st, 3);
  7269. check_event(st, event);
  7270. pc_addeventtimer(script_rid2sd(st),tick,event);
  7271. return 0;
  7272. }
  7273. /*==========================================
  7274. * イベントタイマー削除
  7275. *------------------------------------------*/
  7276. BUILDIN_FUNC(deltimer)
  7277. {
  7278. const char *event;
  7279. event=script_getstr(st, 2);
  7280. check_event(st, event);
  7281. pc_deleventtimer(script_rid2sd(st),event);
  7282. return 0;
  7283. }
  7284. /*==========================================
  7285. * イベントタイマーのカウント値追加
  7286. *------------------------------------------*/
  7287. BUILDIN_FUNC(addtimercount)
  7288. {
  7289. const char *event;
  7290. int tick;
  7291. event=script_getstr(st, 2);
  7292. tick=script_getnum(st,3);
  7293. check_event(st, event);
  7294. pc_addeventtimercount(script_rid2sd(st),event,tick);
  7295. return 0;
  7296. }
  7297. /*==========================================
  7298. * NPCタイマー初期化
  7299. *------------------------------------------*/
  7300. BUILDIN_FUNC(initnpctimer)
  7301. {
  7302. struct npc_data *nd;
  7303. int flag = 0;
  7304. if( script_hasdata(st,3) )
  7305. { //Two arguments: NPC name and attach flag.
  7306. nd = npc_name2id(script_getstr(st, 2));
  7307. flag = script_getnum(st,3);
  7308. } else
  7309. if( script_hasdata(st,2) )
  7310. { //Check if argument is numeric (flag) or string (npc name)
  7311. struct script_data *data;
  7312. data = script_getdata(st,2);
  7313. get_val(st,data);
  7314. if( data_isstring(data) ) //NPC name
  7315. nd = npc_name2id(script_getstr(st, 2));
  7316. else if( data_isint(data) ) { //Flag
  7317. nd = (struct npc_data *)map_id2bl(st->oid);
  7318. flag = script_getnum(st,3);
  7319. } else {
  7320. ShowError("initnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7321. return 1;
  7322. }
  7323. } else
  7324. nd=(struct npc_data *)map_id2bl(st->oid);
  7325. if (!nd) return 0;
  7326. if (flag) { //Attach
  7327. TBL_PC* sd = script_rid2sd(st);
  7328. if (sd) nd->u.scr.rid = sd->bl.id;
  7329. }
  7330. npc_settimerevent_tick(nd,0);
  7331. npc_timerevent_start(nd, st->rid);
  7332. return 0;
  7333. }
  7334. /*==========================================
  7335. * NPCタイマー開始
  7336. *------------------------------------------*/
  7337. BUILDIN_FUNC(startnpctimer)
  7338. {
  7339. struct npc_data *nd;
  7340. int flag = 0;
  7341. if( script_hasdata(st,3) )
  7342. { //Two arguments: NPC name and attach flag.
  7343. nd = npc_name2id(script_getstr(st, 2));
  7344. flag = script_getnum(st,3);
  7345. } else
  7346. if( script_hasdata(st,2) )
  7347. { //Check if argument is numeric (flag) or string (npc name)
  7348. struct script_data *data;
  7349. data = script_getdata(st,2);
  7350. get_val(st,data);
  7351. if( data_isstring(data) ) //NPC name
  7352. nd = npc_name2id(script_getstr(st, 2));
  7353. else if( data_isint(data) ) { //Flag
  7354. nd = (struct npc_data *)map_id2bl(st->oid);
  7355. flag = script_getnum(st,3);
  7356. } else {
  7357. ShowError("startnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7358. return 1;
  7359. }
  7360. } else
  7361. nd=(struct npc_data *)map_id2bl(st->oid);
  7362. if (!nd) return 0;
  7363. if (flag) { //Attach
  7364. TBL_PC* sd = script_rid2sd(st);
  7365. if (sd) nd->u.scr.rid = sd->bl.id;
  7366. }
  7367. npc_timerevent_start(nd, st->rid);
  7368. return 0;
  7369. }
  7370. /*==========================================
  7371. * NPCタイマー停止
  7372. *------------------------------------------*/
  7373. BUILDIN_FUNC(stopnpctimer)
  7374. {
  7375. struct npc_data *nd;
  7376. int flag = 0;
  7377. if( script_hasdata(st,3) )
  7378. { //Two arguments: NPC name and attach flag.
  7379. nd = npc_name2id(script_getstr(st, 2));
  7380. flag = script_getnum(st,3);
  7381. } else
  7382. if( script_hasdata(st,2) )
  7383. { //Check if argument is numeric (flag) or string (npc name)
  7384. struct script_data *data;
  7385. data = script_getdata(st,2);
  7386. get_val(st,data);
  7387. if( data_isstring(data) ) //NPC name
  7388. nd = npc_name2id(script_getstr(st, 2));
  7389. else if( data_isint(data) ) { //Flag
  7390. nd = (struct npc_data *)map_id2bl(st->oid);
  7391. flag = script_getnum(st,3);
  7392. } else {
  7393. ShowError("stopnpctimer: invalid argument type #1 (needs be int or string)).\n");
  7394. return 1;
  7395. }
  7396. } else
  7397. nd=(struct npc_data *)map_id2bl(st->oid);
  7398. if (!nd) return 0;
  7399. if (flag) //Detach
  7400. nd->u.scr.rid = 0;
  7401. npc_timerevent_stop(nd);
  7402. return 0;
  7403. }
  7404. /*==========================================
  7405. * NPCタイマー情報所得
  7406. *------------------------------------------*/
  7407. BUILDIN_FUNC(getnpctimer)
  7408. {
  7409. struct npc_data *nd;
  7410. TBL_PC *sd;
  7411. int type=script_getnum(st,2);
  7412. int val=0;
  7413. if( script_hasdata(st,3) )
  7414. nd = npc_name2id(script_getstr(st,3));
  7415. else
  7416. nd = (struct npc_data *)map_id2bl(st->oid);
  7417. if (!nd || nd->bl.type != BL_NPC)
  7418. {
  7419. script_pushint(st,0);
  7420. if (battle_config.error_log)
  7421. ShowError("getnpctimer: Invalid NPC\n");
  7422. return 1;
  7423. }
  7424. switch(type){
  7425. case 0: val=npc_gettimerevent_tick(nd); break;
  7426. case 1:
  7427. if (nd->u.scr.rid) {
  7428. sd = map_id2sd(nd->u.scr.rid);
  7429. if (!sd) {
  7430. if(battle_config.error_log)
  7431. ShowError("buildin_getnpctimer: Attached player not found!\n");
  7432. break;
  7433. }
  7434. val = (sd->npc_timer_id != -1);
  7435. } else
  7436. val= (nd->u.scr.timerid !=-1);
  7437. break;
  7438. case 2: val= nd->u.scr.timeramount; break;
  7439. }
  7440. script_pushint(st,val);
  7441. return 0;
  7442. }
  7443. /*==========================================
  7444. * NPCタイマー値設定
  7445. *------------------------------------------*/
  7446. BUILDIN_FUNC(setnpctimer)
  7447. {
  7448. int tick;
  7449. struct npc_data *nd;
  7450. tick=script_getnum(st,2);
  7451. if( script_hasdata(st,3) )
  7452. nd=npc_name2id(script_getstr(st,3));
  7453. else
  7454. nd=(struct npc_data *)map_id2bl(st->oid);
  7455. npc_settimerevent_tick(nd,tick);
  7456. return 0;
  7457. }
  7458. /*==========================================
  7459. * attaches the player rid to the timer [Celest]
  7460. *------------------------------------------*/
  7461. BUILDIN_FUNC(attachnpctimer)
  7462. {
  7463. TBL_PC *sd;
  7464. struct npc_data *nd;
  7465. nd=(struct npc_data *)map_id2bl(st->oid);
  7466. if( script_hasdata(st,2) )
  7467. sd=map_nick2sd(script_getstr(st,2));
  7468. else
  7469. sd = script_rid2sd(st);
  7470. if (sd==NULL)
  7471. return 0;
  7472. nd->u.scr.rid = sd->bl.id;
  7473. return 0;
  7474. }
  7475. /*==========================================
  7476. * detaches a player rid from the timer [Celest]
  7477. *------------------------------------------*/
  7478. BUILDIN_FUNC(detachnpctimer)
  7479. {
  7480. struct npc_data *nd;
  7481. if( script_hasdata(st,2) )
  7482. nd=npc_name2id(script_getstr(st,2));
  7483. else
  7484. nd=(struct npc_data *)map_id2bl(st->oid);
  7485. nd->u.scr.rid = 0;
  7486. return 0;
  7487. }
  7488. /*==========================================
  7489. * To avoid "player not attached" script errors, this function is provided,
  7490. * it checks if there is a player attached to the current script. [Skotlex]
  7491. * If no, returns 0, if yes, returns the account_id of the attached player.
  7492. *------------------------------------------*/
  7493. BUILDIN_FUNC(playerattached)
  7494. {
  7495. if(st->rid == 0 || map_id2sd(st->rid) == NULL)
  7496. script_pushint(st,0);
  7497. else
  7498. script_pushint(st,st->rid);
  7499. return 0;
  7500. }
  7501. /*==========================================
  7502. * 天の声アナウンス
  7503. *------------------------------------------*/
  7504. BUILDIN_FUNC(announce)
  7505. {
  7506. const char *str, *color=NULL;
  7507. int flag;
  7508. str=script_getstr(st,2);
  7509. flag=script_getnum(st,3);
  7510. if (script_hasdata(st,4))
  7511. color=script_getstr(st,4);
  7512. if(flag&0x0f){
  7513. struct block_list *bl=(flag&0x08)? map_id2bl(st->oid) :
  7514. (struct block_list *)script_rid2sd(st);
  7515. if (color)
  7516. clif_announce(bl,str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0),flag);
  7517. else
  7518. clif_GMmessage(bl,str,(int)strlen(str)+1,flag);
  7519. }else {
  7520. if (color)
  7521. intif_announce(str,(int)strlen(str)+1, strtol(color, (char **)NULL, 0), flag);
  7522. else
  7523. intif_GMmessage(str,(int)strlen(str)+1,flag);
  7524. }
  7525. return 0;
  7526. }
  7527. /*==========================================
  7528. * 天の声アナウンス(特定マップ)
  7529. *------------------------------------------*/
  7530. static int buildin_mapannounce_sub(struct block_list *bl,va_list ap)
  7531. {
  7532. char *str, *color;
  7533. int len,flag;
  7534. str=va_arg(ap,char *);
  7535. len=va_arg(ap,int);
  7536. flag=va_arg(ap,int);
  7537. color=va_arg(ap,char *);
  7538. if (color)
  7539. clif_announce(bl,str,len, strtol(color, (char **)NULL, 0), flag|3);
  7540. else
  7541. clif_GMmessage(bl,str,len,flag|3);
  7542. return 0;
  7543. }
  7544. BUILDIN_FUNC(mapannounce)
  7545. {
  7546. const char *mapname,*str, *color=NULL;
  7547. int flag,m;
  7548. mapname=script_getstr(st,2);
  7549. str=script_getstr(st,3);
  7550. flag=script_getnum(st,4);
  7551. if (script_hasdata(st,5))
  7552. color=script_getstr(st,5);
  7553. if( (m=map_mapname2mapid(mapname))<0 )
  7554. return 0;
  7555. map_foreachinmap(buildin_mapannounce_sub,
  7556. m, BL_PC, str,strlen(str)+1,flag&0x10, color);
  7557. return 0;
  7558. }
  7559. /*==========================================
  7560. * 天の声アナウンス(特定エリア)
  7561. *------------------------------------------*/
  7562. BUILDIN_FUNC(areaannounce)
  7563. {
  7564. const char *map,*str,*color=NULL;
  7565. int flag,m;
  7566. int x0,y0,x1,y1;
  7567. map=script_getstr(st,2);
  7568. x0=script_getnum(st,3);
  7569. y0=script_getnum(st,4);
  7570. x1=script_getnum(st,5);
  7571. y1=script_getnum(st,6);
  7572. str=script_getstr(st,7);
  7573. flag=script_getnum(st,8);
  7574. if (script_hasdata(st,9))
  7575. color=script_getstr(st,9);
  7576. if( (m=map_mapname2mapid(map))<0 )
  7577. return 0;
  7578. map_foreachinarea(buildin_mapannounce_sub,
  7579. m,x0,y0,x1,y1,BL_PC, str,strlen(str)+1,flag&0x10, color);
  7580. return 0;
  7581. }
  7582. /*==========================================
  7583. * ユーザー数所得
  7584. *------------------------------------------*/
  7585. BUILDIN_FUNC(getusers)
  7586. {
  7587. int flag=script_getnum(st,2);
  7588. struct block_list *bl=map_id2bl((flag&0x08)?st->oid:st->rid);
  7589. int val=0;
  7590. switch(flag&0x07){
  7591. case 0: val=map[bl->m].users; break;
  7592. case 1: val=map_getusers(); break;
  7593. }
  7594. script_pushint(st,val);
  7595. return 0;
  7596. }
  7597. /*==========================================
  7598. * Works like @WHO - displays all online users names in window
  7599. *------------------------------------------*/
  7600. BUILDIN_FUNC(getusersname)
  7601. {
  7602. TBL_PC *sd, *pl_sd = NULL, **pl_allsd;
  7603. int i=0,disp_num=1, users;
  7604. sd = script_rid2sd(st);
  7605. if (!sd) return 0;
  7606. pl_allsd = map_getallusers(&users);
  7607. for (i=0;i<users;i++)
  7608. {
  7609. pl_sd = pl_allsd[i];
  7610. if( !(battle_config.hide_GM_session && pc_isGM(pl_sd)) )
  7611. {
  7612. if((disp_num++)%10==0)
  7613. clif_scriptnext(sd,st->oid);
  7614. clif_scriptmes(sd,st->oid,pl_sd->status.name);
  7615. }
  7616. }
  7617. return 0;
  7618. }
  7619. /*==========================================
  7620. * getmapguildusers("mapname",guild ID) Returns the number guild members present on a map [Reddozen]
  7621. *------------------------------------------*/
  7622. BUILDIN_FUNC(getmapguildusers)
  7623. {
  7624. const char *str;
  7625. int m, gid;
  7626. int i=0,c=0;
  7627. struct guild *g = NULL;
  7628. str=script_getstr(st,2);
  7629. gid=script_getnum(st,3);
  7630. if ((m = map_mapname2mapid(str)) < 0) { // map id on this server (m == -1 if not in actual map-server)
  7631. script_pushint(st,-1);
  7632. return 0;
  7633. }
  7634. g = guild_search(gid);
  7635. if (g){
  7636. for(i = 0; i < g->max_member; i++)
  7637. {
  7638. if (g->member[i].sd && g->member[i].sd->bl.m == m)
  7639. c++;
  7640. }
  7641. }
  7642. script_pushint(st,c);
  7643. return 0;
  7644. }
  7645. /*==========================================
  7646. * マップ指定ユーザー数所得
  7647. *------------------------------------------*/
  7648. BUILDIN_FUNC(getmapusers)
  7649. {
  7650. const char *str;
  7651. int m;
  7652. str=script_getstr(st,2);
  7653. if( (m=map_mapname2mapid(str))< 0){
  7654. script_pushint(st,-1);
  7655. return 0;
  7656. }
  7657. script_pushint(st,map[m].users);
  7658. return 0;
  7659. }
  7660. /*==========================================
  7661. * エリア指定ユーザー数所得
  7662. *------------------------------------------*/
  7663. static int buildin_getareausers_sub(struct block_list *bl,va_list ap)
  7664. {
  7665. int *users=va_arg(ap,int *);
  7666. (*users)++;
  7667. return 0;
  7668. }
  7669. BUILDIN_FUNC(getareausers)
  7670. {
  7671. const char *str;
  7672. int m,x0,y0,x1,y1,users=0;
  7673. str=script_getstr(st,2);
  7674. x0=script_getnum(st,3);
  7675. y0=script_getnum(st,4);
  7676. x1=script_getnum(st,5);
  7677. y1=script_getnum(st,6);
  7678. if( (m=map_mapname2mapid(str))< 0){
  7679. script_pushint(st,-1);
  7680. return 0;
  7681. }
  7682. map_foreachinarea(buildin_getareausers_sub,
  7683. m,x0,y0,x1,y1,BL_PC,&users);
  7684. script_pushint(st,users);
  7685. return 0;
  7686. }
  7687. /*==========================================
  7688. * エリア指定ドロップアイテム数所得
  7689. *------------------------------------------*/
  7690. static int buildin_getareadropitem_sub(struct block_list *bl,va_list ap)
  7691. {
  7692. int item=va_arg(ap,int);
  7693. int *amount=va_arg(ap,int *);
  7694. struct flooritem_data *drop=(struct flooritem_data *)bl;
  7695. if(drop->item_data.nameid==item)
  7696. (*amount)+=drop->item_data.amount;
  7697. return 0;
  7698. }
  7699. BUILDIN_FUNC(getareadropitem)
  7700. {
  7701. const char *str;
  7702. int m,x0,y0,x1,y1,item,amount=0;
  7703. struct script_data *data;
  7704. str=script_getstr(st,2);
  7705. x0=script_getnum(st,3);
  7706. y0=script_getnum(st,4);
  7707. x1=script_getnum(st,5);
  7708. y1=script_getnum(st,6);
  7709. data=script_getdata(st,7);
  7710. get_val(st,data);
  7711. if( data_isstring(data) ){
  7712. const char *name=conv_str(st,data);
  7713. struct item_data *item_data = itemdb_searchname(name);
  7714. item=UNKNOWN_ITEM_ID;
  7715. if( item_data )
  7716. item=item_data->nameid;
  7717. }else
  7718. item=conv_num(st,data);
  7719. if( (m=map_mapname2mapid(str))< 0){
  7720. script_pushint(st,-1);
  7721. return 0;
  7722. }
  7723. map_foreachinarea(buildin_getareadropitem_sub,
  7724. m,x0,y0,x1,y1,BL_ITEM,item,&amount);
  7725. script_pushint(st,amount);
  7726. return 0;
  7727. }
  7728. /*==========================================
  7729. * NPCの有効化
  7730. *------------------------------------------*/
  7731. BUILDIN_FUNC(enablenpc)
  7732. {
  7733. const char *str;
  7734. str=script_getstr(st,2);
  7735. npc_enable(str,1);
  7736. return 0;
  7737. }
  7738. /*==========================================
  7739. * NPCの無効化
  7740. *------------------------------------------*/
  7741. BUILDIN_FUNC(disablenpc)
  7742. {
  7743. const char *str;
  7744. str=script_getstr(st,2);
  7745. npc_enable(str,0);
  7746. return 0;
  7747. }
  7748. /*==========================================
  7749. * 隠れているNPCの表示
  7750. *------------------------------------------*/
  7751. BUILDIN_FUNC(hideoffnpc)
  7752. {
  7753. const char *str;
  7754. str=script_getstr(st,2);
  7755. npc_enable(str,2);
  7756. return 0;
  7757. }
  7758. /*==========================================
  7759. * NPCをハイディング
  7760. *------------------------------------------*/
  7761. BUILDIN_FUNC(hideonnpc)
  7762. {
  7763. const char *str;
  7764. str=script_getstr(st,2);
  7765. npc_enable(str,4);
  7766. return 0;
  7767. }
  7768. /// Starts a status effect on the target unit or on the attached player.
  7769. ///
  7770. /// sc_start <effect_id>,<duration>,<val1>{,<unit_id>};
  7771. BUILDIN_FUNC(sc_start)
  7772. {
  7773. struct block_list* bl;
  7774. int type;
  7775. int tick;
  7776. int val1;
  7777. int val4 = 0;
  7778. type = script_getnum(st,2);
  7779. tick = script_getnum(st,3);
  7780. val1 = script_getnum(st,4);
  7781. if( script_hasdata(st,5) )
  7782. bl = map_id2bl(script_getnum(st,5));
  7783. else
  7784. bl = map_id2bl(st->rid);
  7785. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7786. {// When there isn't a duration specified, try to get it from the skill_db
  7787. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7788. }
  7789. if( potion_flag == 1 && potion_target )
  7790. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7791. bl = map_id2bl(potion_target);
  7792. tick /= 2;// Thrown potions only last half.
  7793. val4 = 1;// Mark that this was a thrown sc_effect
  7794. }
  7795. if( bl )
  7796. status_change_start(bl, type, 10000, val1, 0, 0, val4, tick, 1|2|8);
  7797. return 0;
  7798. }
  7799. /// Starts a status effect on the target unit or on the attached player.
  7800. ///
  7801. /// sc_start2 <effect_id>,<duration>,<val1>,<percent chance>{,<unit_id>};
  7802. BUILDIN_FUNC(sc_start2)
  7803. {
  7804. struct block_list* bl;
  7805. int type;
  7806. int tick;
  7807. int val1;
  7808. int val4 = 0;
  7809. int rate;
  7810. type = script_getnum(st,2);
  7811. tick = script_getnum(st,3);
  7812. val1 = script_getnum(st,4);
  7813. rate = script_getnum(st,5);
  7814. if( script_hasdata(st,6) )
  7815. bl = map_id2bl(script_getnum(st,6));
  7816. else
  7817. bl = map_id2bl(st->rid);
  7818. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7819. {// When there isn't a duration specified, try to get it from the skill_db
  7820. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7821. }
  7822. if( potion_flag == 1 && potion_target )
  7823. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7824. bl = map_id2bl(potion_target);
  7825. tick /= 2;// Thrown potions only last half.
  7826. val4 = 1;// Mark that this was a thrown sc_effect
  7827. }
  7828. if( bl )
  7829. status_change_start(bl, type, rate, val1, 0, 0, val4, tick, 1|2|8);
  7830. return 0;
  7831. }
  7832. /// Starts a status effect on the target unit or on the attached player.
  7833. ///
  7834. /// sc_start4 <effect_id>,<duration>,<val1>,<val2>,<val3>,<val4>{,<unit_id>};
  7835. BUILDIN_FUNC(sc_start4)
  7836. {
  7837. struct block_list* bl;
  7838. int type;
  7839. int tick;
  7840. int val1;
  7841. int val2;
  7842. int val3;
  7843. int val4;
  7844. type = script_getnum(st,2);
  7845. tick = script_getnum(st,3);
  7846. val1 = script_getnum(st,4);
  7847. val2 = script_getnum(st,5);
  7848. val3 = script_getnum(st,6);
  7849. val4 = script_getnum(st,7);
  7850. if( script_hasdata(st,8) )
  7851. bl = map_id2bl(script_getnum(st,8));
  7852. else
  7853. bl = map_id2bl(st->rid);
  7854. if( tick == 0 && val1 > 0 && type >= 0 && type < SC_MAX && StatusSkillChangeTable[type] != 0 )
  7855. {// When there isn't a duration specified, try to get it from the skill_db
  7856. tick = skill_get_time(StatusSkillChangeTable[type], val1);
  7857. }
  7858. if( potion_flag == 1 && potion_target )
  7859. { //skill.c set the flags before running the script, this must be a potion-pitched effect.
  7860. bl = map_id2bl(potion_target);
  7861. tick /= 2;// Thrown potions only last half.
  7862. }
  7863. if( bl )
  7864. status_change_start(bl, type, 10000, val1, val2, val3, val4, tick, 1|2|8);
  7865. return 0;
  7866. }
  7867. /// Ends one or all status effects on the target unit or on the attached player.
  7868. ///
  7869. /// sc_end <effect_id>{,<unit_id>};
  7870. BUILDIN_FUNC(sc_end)
  7871. {
  7872. struct block_list* bl;
  7873. int type;
  7874. type = script_getnum(st,2);
  7875. if( script_hasdata(st,3) )
  7876. bl = map_id2bl(script_getnum(st,3));
  7877. else
  7878. bl = map_id2bl(st->rid);
  7879. if( potion_flag==1 && potion_target )
  7880. {//##TODO how does this work [FlavioJS]
  7881. bl = map_id2bl(potion_target);
  7882. }
  7883. if( bl )
  7884. {
  7885. if( type >= 0 )
  7886. status_change_end(bl, type, INVALID_TIMER);
  7887. else
  7888. status_change_clear(bl, 2);// remove all effects
  7889. }
  7890. return 0;
  7891. }
  7892. /*==========================================
  7893. * 状態異常耐性を計算した確率を返す
  7894. *------------------------------------------*/
  7895. BUILDIN_FUNC(getscrate)
  7896. {
  7897. struct block_list *bl;
  7898. int type,rate;
  7899. type=script_getnum(st,2);
  7900. rate=script_getnum(st,3);
  7901. if( script_hasdata(st,4) ) //指定したキャラの耐性を計算する
  7902. bl = map_id2bl(script_getnum(st,4));
  7903. else
  7904. bl = map_id2bl(st->rid);
  7905. if (bl)
  7906. rate = status_get_sc_def(bl,type, 10000, 10000, 0);
  7907. script_pushint(st,rate);
  7908. return 0;
  7909. }
  7910. /*==========================================
  7911. *
  7912. *------------------------------------------*/
  7913. BUILDIN_FUNC(debugmes)
  7914. {
  7915. const char *str;
  7916. str=script_getstr(st,2);
  7917. ShowDebug("script debug : %d %d : %s\n",st->rid,st->oid,str);
  7918. return 0;
  7919. }
  7920. /*==========================================
  7921. *捕獲アイテム使用
  7922. *------------------------------------------*/
  7923. BUILDIN_FUNC(catchpet)
  7924. {
  7925. int pet_id;
  7926. TBL_PC *sd;
  7927. pet_id= script_getnum(st,2);
  7928. sd=script_rid2sd(st);
  7929. pet_catch_process1(sd,pet_id);
  7930. return 0;
  7931. }
  7932. /*==========================================
  7933. * [orn]
  7934. *------------------------------------------*/
  7935. BUILDIN_FUNC(homunculus_evolution)
  7936. {
  7937. TBL_PC *sd;
  7938. sd=script_rid2sd(st);
  7939. if(merc_is_hom_active(sd->hd))
  7940. {
  7941. if (sd->hd->homunculus.intimacy > 91000)
  7942. merc_hom_evolution(sd->hd);
  7943. else
  7944. clif_emotion(&sd->hd->bl, 4) ; //swt
  7945. }
  7946. return 0;
  7947. }
  7948. //These two functions bring the eA MAPID_* class functionality to scripts.
  7949. BUILDIN_FUNC(eaclass)
  7950. {
  7951. int class_;
  7952. if( script_hasdata(st,2) )
  7953. class_ = script_getnum(st,2);
  7954. else {
  7955. TBL_PC *sd;
  7956. sd=script_rid2sd(st);
  7957. if (!sd) {
  7958. script_pushint(st,-1);
  7959. return 0;
  7960. }
  7961. class_ = sd->status.class_;
  7962. }
  7963. script_pushint(st,pc_jobid2mapid(class_));
  7964. return 0;
  7965. }
  7966. BUILDIN_FUNC(roclass)
  7967. {
  7968. int class_ =script_getnum(st,2);
  7969. int sex;
  7970. if( script_hasdata(st,3) )
  7971. sex = script_getnum(st,3);
  7972. else {
  7973. TBL_PC *sd;
  7974. if (st->rid && (sd=script_rid2sd(st)))
  7975. sex = sd->status.sex;
  7976. else
  7977. sex = 1; //Just use male when not found.
  7978. }
  7979. script_pushint(st,pc_mapid2jobid(class_, sex));
  7980. return 0;
  7981. }
  7982. /*==========================================
  7983. *携帯卵孵化機使用
  7984. *------------------------------------------*/
  7985. BUILDIN_FUNC(birthpet)
  7986. {
  7987. TBL_PC *sd;
  7988. sd=script_rid2sd(st);
  7989. clif_sendegg(sd);
  7990. return 0;
  7991. }
  7992. /*==========================================
  7993. * Added - AppleGirl For Advanced Classes, (Updated for Cleaner Script Purposes)
  7994. *------------------------------------------*/
  7995. BUILDIN_FUNC(resetlvl)
  7996. {
  7997. TBL_PC *sd;
  7998. int type=script_getnum(st,2);
  7999. sd=script_rid2sd(st);
  8000. pc_resetlvl(sd,type);
  8001. return 0;
  8002. }
  8003. /*==========================================
  8004. * ステータスリセット
  8005. *------------------------------------------*/
  8006. BUILDIN_FUNC(resetstatus)
  8007. {
  8008. TBL_PC *sd;
  8009. sd=script_rid2sd(st);
  8010. pc_resetstate(sd);
  8011. return 0;
  8012. }
  8013. /*==========================================
  8014. * script command resetskill
  8015. *------------------------------------------*/
  8016. BUILDIN_FUNC(resetskill)
  8017. {
  8018. TBL_PC *sd;
  8019. sd=script_rid2sd(st);
  8020. pc_resetskill(sd,1);
  8021. return 0;
  8022. }
  8023. /*==========================================
  8024. * Counts total amount of skill points.
  8025. *------------------------------------------*/
  8026. BUILDIN_FUNC(skillpointcount)
  8027. {
  8028. TBL_PC *sd;
  8029. sd=script_rid2sd(st);
  8030. script_pushint(st,sd->status.skill_point + pc_resetskill(sd,2));
  8031. return 0;
  8032. }
  8033. /*==========================================
  8034. *
  8035. *------------------------------------------*/
  8036. BUILDIN_FUNC(changebase)
  8037. {
  8038. TBL_PC *sd=NULL;
  8039. int vclass;
  8040. if( script_hasdata(st,3) )
  8041. sd=map_id2sd(script_getnum(st,3));
  8042. else
  8043. sd=script_rid2sd(st);
  8044. if(sd == NULL)
  8045. return 0;
  8046. vclass = script_getnum(st,2);
  8047. if(vclass == JOB_WEDDING)
  8048. {
  8049. if (!battle_config.wedding_modifydisplay || //Do not show the wedding sprites
  8050. sd->class_&JOBL_BABY //Baby classes screw up when showing wedding sprites. [Skotlex] They don't seem to anymore.
  8051. )
  8052. return 0;
  8053. }
  8054. if(!sd->disguise && vclass != sd->vd.class_) {
  8055. status_set_viewdata(&sd->bl, vclass);
  8056. //Updated client view. Base, Weapon and Cloth Colors.
  8057. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_);
  8058. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  8059. if (sd->vd.cloth_color)
  8060. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  8061. }
  8062. return 0;
  8063. }
  8064. /*==========================================
  8065. * 性別変換
  8066. *------------------------------------------*/
  8067. BUILDIN_FUNC(changesex)
  8068. {
  8069. TBL_PC *sd = NULL;
  8070. sd = script_rid2sd(st);
  8071. if (sd->status.sex == 0) {
  8072. sd->status.sex = 1;
  8073. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  8074. sd->status.class_ -= 1;
  8075. } else if (sd->status.sex == 1) {
  8076. sd->status.sex = 0;
  8077. if ((sd->class_&MAPID_UPPERMASK) == MAPID_BARDDANCER)
  8078. sd->status.class_ += 1;
  8079. }
  8080. chrif_char_ask_name(-1, sd->status.name, 5, 0, 0, 0, 0, 0, 0); // type: 5 - changesex
  8081. chrif_save(sd,0);
  8082. return 0;
  8083. }
  8084. /*==========================================
  8085. * Works like 'announce' but outputs in the common chat window
  8086. *------------------------------------------*/
  8087. BUILDIN_FUNC(globalmes)
  8088. {
  8089. struct block_list *bl = map_id2bl(st->oid);
  8090. struct npc_data *nd = (struct npc_data *)bl;
  8091. const char *name=NULL,*mes;
  8092. mes=script_getstr(st,2); // メッセージの取得
  8093. if(mes==NULL) return 0;
  8094. if(script_hasdata(st,3)){ // NPC名の取得(123#456)
  8095. name=script_getstr(st,3);
  8096. } else {
  8097. name=nd->name;
  8098. }
  8099. npc_globalmessage(name,mes); // グローバルメッセージ送信
  8100. return 0;
  8101. }
  8102. /////////////////////////////////////////////////////////////////////
  8103. // NPC waiting room (chat room)
  8104. //
  8105. /// Creates a waiting room (chat room) for this npc.
  8106. ///
  8107. /// waitingroom "<title>",<limit>,<trigger>,"<event>";
  8108. /// waitingroom "<title>",<limit>,"<event>",<trigger>;
  8109. /// waitingroom "<title>",<limit>,"<event>";
  8110. /// waitingroom "<title>",<limit>;
  8111. BUILDIN_FUNC(waitingroom)
  8112. {
  8113. struct npc_data* nd;
  8114. const char* title;
  8115. const char* ev = "";
  8116. int limit;
  8117. int trigger = 0;
  8118. int pub = 1;
  8119. title = script_getstr(st, 2);
  8120. limit = script_getnum(st, 3);
  8121. if( script_hasdata(st,5) )
  8122. {
  8123. struct script_data* last = script_getdata(st, 5);
  8124. get_val(st, last);
  8125. if( data_isstring(last) )
  8126. {// ,<trigger>,"<event>"
  8127. trigger = script_getnum(st, 4);
  8128. ev = script_getstr(st, 5);
  8129. }
  8130. else
  8131. {// ,"<event>",<trigger>
  8132. ev = script_getstr(st, 4);
  8133. trigger = script_getnum(st,5);
  8134. }
  8135. }
  8136. else if( script_hasdata(st,4) )
  8137. {// ,"<event>"
  8138. ev = script_getstr(st, 4);
  8139. trigger = limit;
  8140. }
  8141. if( (nd=(struct npc_data *)map_id2bl(st->oid)) != NULL )
  8142. chat_createnpcchat(nd, title, limit, pub, trigger, ev);
  8143. return 0;
  8144. }
  8145. /// Removes the waiting room of the current or target npc.
  8146. ///
  8147. /// delwaitingroom "<npc_name>";
  8148. /// delwaitingroom;
  8149. BUILDIN_FUNC(delwaitingroom)
  8150. {
  8151. struct npc_data* nd;
  8152. if( script_hasdata(st,2) )
  8153. nd = npc_name2id(script_getstr(st, 2));
  8154. else
  8155. nd = (struct npc_data *)map_id2bl(st->oid);
  8156. if( nd != NULL )
  8157. chat_deletenpcchat(nd);
  8158. return 0;
  8159. }
  8160. /// Kicks all the players from the waiting room of the current or target npc.
  8161. ///
  8162. /// kickwaitingroomall "<npc_name>";
  8163. /// kickwaitingroomall;
  8164. BUILDIN_FUNC(waitingroomkickall)
  8165. {
  8166. struct npc_data* nd;
  8167. struct chat_data* cd;
  8168. if( script_hasdata(st,2) )
  8169. nd = npc_name2id(script_getstr(st,2));
  8170. else
  8171. nd = (struct npc_data *)map_id2bl(st->oid);
  8172. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8173. chat_npckickall(cd);
  8174. return 0;
  8175. }
  8176. /// Enables the waiting room event of the current or target npc.
  8177. ///
  8178. /// enablewaitingroomevent "<npc_name>";
  8179. /// enablewaitingroomevent;
  8180. BUILDIN_FUNC(enablewaitingroomevent)
  8181. {
  8182. struct npc_data* nd;
  8183. struct chat_data* cd;
  8184. if( script_hasdata(st,2) )
  8185. nd = npc_name2id(script_getstr(st, 2));
  8186. else
  8187. nd = (struct npc_data *)map_id2bl(st->oid);
  8188. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8189. chat_enableevent(cd);
  8190. return 0;
  8191. }
  8192. /// Disables the waiting room event of the current or target npc.
  8193. ///
  8194. /// disablewaitingroomevent "<npc_name>";
  8195. /// disablewaitingroomevent;
  8196. BUILDIN_FUNC(disablewaitingroomevent)
  8197. {
  8198. struct npc_data *nd;
  8199. struct chat_data *cd;
  8200. if( script_hasdata(st,2) )
  8201. nd = npc_name2id(script_getstr(st, 2));
  8202. else
  8203. nd = (struct npc_data *)map_id2bl(st->oid);
  8204. if( nd != NULL && (cd=(struct chat_data *)map_id2bl(nd->chat_id)) != NULL )
  8205. chat_disableevent(cd);
  8206. return 0;
  8207. }
  8208. /// Returns info on the waiting room of the current or target npc.
  8209. /// Returns -1 if the type unknown
  8210. /// <type>=0 : current number of users
  8211. /// <type>=1 : maximum number of users allowed
  8212. /// <type>=2 : the number of users that trigger the event
  8213. /// <type>=3 : if the trigger is disabled
  8214. /// <type>=4 : the title of the waiting room
  8215. /// <type>=5 : the password of the waiting room
  8216. /// <type>=16 : the name of the waiting room event
  8217. /// <type>=32 : if the waiting room is full
  8218. /// <type>=33 : if there are enough users to trigger the event
  8219. ///
  8220. /// getwaitingroomstate(<type>,"<npc_name>") -> <info>
  8221. /// getwaitingroomstate(<type>) -> <info>
  8222. BUILDIN_FUNC(getwaitingroomstate)
  8223. {
  8224. struct npc_data *nd;
  8225. struct chat_data *cd;
  8226. int type;
  8227. type = script_getnum(st,2);
  8228. if( script_hasdata(st,3) )
  8229. nd = npc_name2id(script_getstr(st, 3));
  8230. else
  8231. nd = (struct npc_data *)map_id2bl(st->oid);
  8232. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8233. {
  8234. script_pushint(st, -1);
  8235. return 0;
  8236. }
  8237. switch(type)
  8238. {
  8239. case 0: script_pushint(st, cd->users); break;
  8240. case 1: script_pushint(st, cd->limit); break;
  8241. case 2: script_pushint(st, cd->trigger&0x7f); break;
  8242. case 3: script_pushint(st, ((cd->trigger&0x80)!=0)); break;
  8243. case 4: script_pushconststr(st, cd->title); break;
  8244. case 5: script_pushconststr(st, cd->pass); break;
  8245. case 16: script_pushconststr(st, cd->npc_event);break;
  8246. case 32: script_pushint(st, (cd->users >= cd->limit)); break;
  8247. case 33: script_pushint(st, (cd->users >= cd->trigger)); break;
  8248. default: script_pushint(st, -1); break;
  8249. }
  8250. return 0;
  8251. }
  8252. /// Warps the trigger or target amount of players to the target map and position.
  8253. /// Players are automatically removed from the waiting room.
  8254. /// Those waiting the longest will get warped first.
  8255. /// The target map can be "Random" for a random position in the current map,
  8256. /// and "SavePoint" for the savepoint map+position.
  8257. /// The map flag noteleport of the current map is only considered when teleporting to the savepoint.
  8258. ///
  8259. /// The id's of the teleported players are put into the array $@warpwaitingpc[]
  8260. /// The total number of teleported players is put into $@warpwaitingpcnum
  8261. ///
  8262. /// warpwaitingpc "<map name>",<x>,<y>,<number of players>;
  8263. /// warpwaitingpc "<map name>",<x>,<y>;
  8264. BUILDIN_FUNC(warpwaitingpc)
  8265. {
  8266. int x;
  8267. int y;
  8268. int i;
  8269. int n;
  8270. const char* map_name;
  8271. struct npc_data* nd;
  8272. struct chat_data* cd;
  8273. TBL_PC* sd;
  8274. nd = (struct npc_data *)map_id2bl(st->oid);
  8275. if( nd == NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id)) == NULL )
  8276. return 0;
  8277. map_name = script_getstr(st,2);
  8278. x = script_getnum(st,3);
  8279. y = script_getnum(st,4);
  8280. n = cd->trigger&0x7f;
  8281. if( script_hasdata(st,5) )
  8282. n = script_getnum(st,5);
  8283. for( i = 0; i < n && cd->users > 0; i++ )
  8284. {
  8285. sd = cd->usersd[0];
  8286. if( sd == NULL )
  8287. {
  8288. ShowDebug("script:warpwaitingpc: no user in chat room position 0 (cd->users=%d,%d/%d)\n", cd->users, i, n);
  8289. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), 0);
  8290. continue;// Broken npc chat room?
  8291. }
  8292. mapreg_setreg(add_str("$@warpwaitingpc")+(i<<24), sd->bl.id);
  8293. if( strcmp(map_name,"Random") == 0 )
  8294. pc_randomwarp(sd,3);
  8295. else if( strcmp(map_name,"SavePoint") == 0 )
  8296. {
  8297. if( map[sd->bl.m].flag.noteleport )
  8298. return 0;// can't teleport on this map
  8299. pc_setpos(sd, sd->status.save_point.map, sd->status.save_point.x, sd->status.save_point.y, 3);
  8300. }
  8301. else
  8302. pc_setpos(sd, mapindex_name2id(map_name), x, y, 0);
  8303. }
  8304. mapreg_setreg(add_str("$@warpwaitingpcnum"), i);
  8305. return 0;
  8306. }
  8307. /////////////////////////////////////////////////////////////////////
  8308. // ...
  8309. //
  8310. /// TODO what is this suposed to do?
  8311. ///
  8312. /// @author RoVeRT
  8313. BUILDIN_FUNC(enablearena)
  8314. {
  8315. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  8316. struct chat_data *cd;
  8317. if(nd==NULL || (cd=(struct chat_data *)map_id2bl(nd->chat_id))==NULL)
  8318. return 0;
  8319. npc_enable(nd->name, 1);
  8320. nd->arenaflag = 1;
  8321. if( cd->users >= cd->trigger && cd->npc_event[0] )
  8322. npc_timer_event(cd->npc_event);
  8323. return 0;
  8324. }
  8325. /// TODO what is this suposed to do?
  8326. ///
  8327. /// @author RoVeRT
  8328. BUILDIN_FUNC(disablearena)
  8329. {
  8330. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  8331. nd->arenaflag=0;
  8332. return 0;
  8333. }
  8334. /////////////////////////////////////////////////////////////////////
  8335. // ...
  8336. //
  8337. /*==========================================
  8338. * RIDのアタッチ
  8339. *------------------------------------------*/
  8340. BUILDIN_FUNC(attachrid)
  8341. {
  8342. st->rid=script_getnum(st,2);
  8343. script_pushint(st,(map_id2sd(st->rid)!=NULL));
  8344. return 0;
  8345. }
  8346. /*==========================================
  8347. * RIDのデタッチ
  8348. *------------------------------------------*/
  8349. BUILDIN_FUNC(detachrid)
  8350. {
  8351. st->rid=0;
  8352. return 0;
  8353. }
  8354. /*==========================================
  8355. * 存在チェック
  8356. *------------------------------------------*/
  8357. BUILDIN_FUNC(isloggedin)
  8358. {
  8359. TBL_PC* sd = map_id2sd(script_getnum(st,2));
  8360. if (script_hasdata(st,3) && sd &&
  8361. sd->status.char_id != script_getnum(st,3))
  8362. sd = NULL;
  8363. push_val(st->stack,C_INT,sd!=NULL);
  8364. return 0;
  8365. }
  8366. /*==========================================
  8367. *
  8368. *------------------------------------------*/
  8369. BUILDIN_FUNC(setmapflagnosave)
  8370. {
  8371. int m,x,y;
  8372. unsigned short mapindex;
  8373. const char *str,*str2;
  8374. str=script_getstr(st,2);
  8375. str2=script_getstr(st,3);
  8376. x=script_getnum(st,4);
  8377. y=script_getnum(st,5);
  8378. m = map_mapname2mapid(str);
  8379. mapindex = mapindex_name2id(str2);
  8380. if(m >= 0 && mapindex) {
  8381. map[m].flag.nosave=1;
  8382. map[m].save.map=mapindex;
  8383. map[m].save.x=x;
  8384. map[m].save.y=y;
  8385. }
  8386. return 0;
  8387. }
  8388. BUILDIN_FUNC(setmapflag)
  8389. {
  8390. int m,i;
  8391. const char *str;
  8392. const char *val=NULL;
  8393. str=script_getstr(st,2);
  8394. i=script_getnum(st,3);
  8395. if(script_hasdata(st,4)){
  8396. val=script_getstr(st,4);
  8397. }
  8398. m = map_mapname2mapid(str);
  8399. if(m >= 0) {
  8400. switch(i) {
  8401. case MF_NOMEMO: map[m].flag.nomemo=1; break;
  8402. case MF_NOTELEPORT: map[m].flag.noteleport=1; break;
  8403. case MF_NOBRANCH: map[m].flag.nobranch=1; break;
  8404. case MF_NOPENALTY: map[m].flag.noexppenalty=1; map[m].flag.nozenypenalty=1; break;
  8405. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=1; break;
  8406. case MF_PVP: map[m].flag.pvp=1; break;
  8407. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=1; break;
  8408. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=1; break;
  8409. case MF_GVG: map[m].flag.gvg=1; break;
  8410. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=1; break;
  8411. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=1; break;
  8412. case MF_GVG_CASTLE: map[m].flag.gvg_castle=1; break;
  8413. case MF_NOTRADE: map[m].flag.notrade=1; break;
  8414. case MF_NODROP: map[m].flag.nodrop=1; break;
  8415. case MF_NOSKILL: map[m].flag.noskill=1; break;
  8416. case MF_NOWARP: map[m].flag.nowarp=1; break;
  8417. case MF_NOICEWALL: map[m].flag.noicewall=1; break;
  8418. case MF_SNOW: map[m].flag.snow=1; break;
  8419. case MF_CLOUDS: map[m].flag.clouds=1; break;
  8420. case MF_CLOUDS2: map[m].flag.clouds2=1; break;
  8421. case MF_FOG: map[m].flag.fog=1; break;
  8422. case MF_FIREWORKS: map[m].flag.fireworks=1; break;
  8423. case MF_SAKURA: map[m].flag.sakura=1; break;
  8424. case MF_LEAVES: map[m].flag.leaves=1; break;
  8425. case MF_RAIN: map[m].flag.rain=1; break;
  8426. case MF_INDOORS: map[m].flag.indoors=1; break;
  8427. case MF_NIGHTENABLED: map[m].flag.nightenabled=1; break;
  8428. case MF_NOGO: map[m].flag.nogo=1; break;
  8429. case MF_NOBASEEXP: map[m].flag.nobaseexp=1; break;
  8430. case MF_NOJOBEXP: map[m].flag.nojobexp=1; break;
  8431. case MF_NOMOBLOOT: map[m].flag.nomobloot=1; break;
  8432. case MF_NOMVPLOOT: map[m].flag.nomvploot=1; break;
  8433. case MF_NORETURN: map[m].flag.noreturn=1; break;
  8434. case MF_NOWARPTO: map[m].flag.nowarpto=1; break;
  8435. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=1; break;
  8436. case MF_RESTRICTED: map[m].flag.restricted=1; break;
  8437. case MF_NOCOMMAND: map[m].nocommand = (!val || atoi(val) <= 0) ? 100 : atoi(val); break;
  8438. case MF_JEXP: map[m].jexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8439. case MF_BEXP: map[m].bexp = (!val || atoi(val) < 0) ? 100 : atoi(val); break;
  8440. case MF_NOVENDING: map[m].flag.novending=1; break;
  8441. case MF_LOADEVENT: map[m].flag.loadevent=1; break;
  8442. case MF_NOCHAT: map[m].flag.nochat=1; break;
  8443. case MF_PARTYLOCK: map[m].flag.partylock=1; break;
  8444. case MF_GUILDLOCK: map[m].flag.guildlock=1; break;
  8445. }
  8446. }
  8447. return 0;
  8448. }
  8449. BUILDIN_FUNC(removemapflag)
  8450. {
  8451. int m,i;
  8452. const char *str;
  8453. str=script_getstr(st,2);
  8454. i=script_getnum(st,3);
  8455. m = map_mapname2mapid(str);
  8456. if(m >= 0) {
  8457. switch(i) {
  8458. case MF_NOMEMO: map[m].flag.nomemo=0; break;
  8459. case MF_NOTELEPORT: map[m].flag.noteleport=0; break;
  8460. case MF_NOSAVE: map[m].flag.nosave=0; break;
  8461. case MF_NOBRANCH: map[m].flag.nobranch=0; break;
  8462. case MF_NOPENALTY: map[m].flag.noexppenalty=0; map[m].flag.nozenypenalty=0; break;
  8463. case MF_PVP: map[m].flag.pvp=0; break;
  8464. case MF_PVP_NOPARTY: map[m].flag.pvp_noparty=0; break;
  8465. case MF_PVP_NOGUILD: map[m].flag.pvp_noguild=0; break;
  8466. case MF_GVG: map[m].flag.gvg=0; break;
  8467. case MF_GVG_NOPARTY: map[m].flag.gvg_noparty=0; break;
  8468. case MF_GVG_DUNGEON: map[m].flag.gvg_dungeon=0; break;
  8469. case MF_GVG_CASTLE: map[m].flag.gvg_castle=0; break;
  8470. case MF_NOZENYPENALTY: map[m].flag.nozenypenalty=0; break;
  8471. case MF_NOTRADE: map[m].flag.notrade=0; break;
  8472. case MF_NODROP: map[m].flag.nodrop=0; break;
  8473. case MF_NOSKILL: map[m].flag.noskill=0; break;
  8474. case MF_NOWARP: map[m].flag.nowarp=0; break;
  8475. case MF_NOICEWALL: map[m].flag.noicewall=0; break;
  8476. case MF_SNOW: map[m].flag.snow=0; break;
  8477. case MF_CLOUDS: map[m].flag.clouds=0; break;
  8478. case MF_CLOUDS2: map[m].flag.clouds2=0; break;
  8479. case MF_FOG: map[m].flag.fog=0; break;
  8480. case MF_FIREWORKS: map[m].flag.fireworks=0; break;
  8481. case MF_SAKURA: map[m].flag.sakura=0; break;
  8482. case MF_LEAVES: map[m].flag.leaves=0; break;
  8483. case MF_RAIN: map[m].flag.rain=0; break;
  8484. case MF_INDOORS: map[m].flag.indoors=0; break;
  8485. case MF_NIGHTENABLED: map[m].flag.nightenabled=0; break;
  8486. case MF_NOGO: map[m].flag.nogo=0; break;
  8487. case MF_NOBASEEXP: map[m].flag.nobaseexp=0; break;
  8488. case MF_NOJOBEXP: map[m].flag.nojobexp=0; break;
  8489. case MF_NOMOBLOOT: map[m].flag.nomobloot=0; break;
  8490. case MF_NOMVPLOOT: map[m].flag.nomvploot=0; break;
  8491. case MF_NORETURN: map[m].flag.noreturn=0; break;
  8492. case MF_NOWARPTO: map[m].flag.nowarpto=0; break;
  8493. case MF_NIGHTMAREDROP: map[m].flag.pvp_nightmaredrop=0; break;
  8494. case MF_RESTRICTED: map[m].flag.restricted=0; break;
  8495. case MF_NOCOMMAND: map[m].nocommand=0; break;
  8496. case MF_JEXP: map[m].jexp=100; break;
  8497. case MF_BEXP: map[m].bexp=100; break;
  8498. case MF_NOVENDING: map[m].flag.novending=0; break;
  8499. case MF_LOADEVENT: map[m].flag.loadevent=0; break;
  8500. case MF_NOCHAT: map[m].flag.nochat=0; break;
  8501. case MF_PARTYLOCK: map[m].flag.partylock=0; break;
  8502. case MF_GUILDLOCK: map[m].flag.guildlock=0; break;
  8503. }
  8504. }
  8505. return 0;
  8506. }
  8507. BUILDIN_FUNC(pvpon)
  8508. {
  8509. int m,i,users;
  8510. const char *str;
  8511. TBL_PC *pl_sd=NULL, **pl_allsd;
  8512. str=script_getstr(st,2);
  8513. m = map_mapname2mapid(str);
  8514. if(m >= 0 && !map[m].flag.pvp) {
  8515. map[m].flag.pvp = 1;
  8516. clif_send0199(m,1);
  8517. if(battle_config.pk_mode) // disable ranking functions if pk_mode is on [Valaris]
  8518. return 0;
  8519. pl_allsd = map_getallusers(&users);
  8520. for(i=0;i<users;i++)
  8521. {
  8522. if ((pl_sd = pl_allsd[i]) && m == pl_sd->bl.m && pl_sd->pvp_timer == -1)
  8523. {
  8524. pl_sd->pvp_timer=add_timer(gettick()+200,pc_calc_pvprank_timer,pl_sd->bl.id,0);
  8525. pl_sd->pvp_rank=0;
  8526. pl_sd->pvp_lastusers=0;
  8527. pl_sd->pvp_point=5;
  8528. pl_sd->pvp_won = 0;
  8529. pl_sd->pvp_lost = 0;
  8530. }
  8531. }
  8532. }
  8533. return 0;
  8534. }
  8535. static int buildin_pvpoff_sub(struct block_list *bl,va_list ap) {
  8536. TBL_PC* sd = (TBL_PC*)bl;
  8537. clif_pvpset(sd, 0, 0, 2);
  8538. if (sd->pvp_timer != UINT_MAX) {
  8539. delete_timer(sd->pvp_timer, pc_calc_pvprank_timer);
  8540. sd->pvp_timer = UINT_MAX;
  8541. }
  8542. return 0;
  8543. }
  8544. BUILDIN_FUNC(pvpoff)
  8545. {
  8546. int m;
  8547. const char *str;
  8548. str=script_getstr(st,2);
  8549. m = map_mapname2mapid(str);
  8550. if(m < 0 || !map[m].flag.pvp)
  8551. return 0; //fixed Lupus
  8552. map[m].flag.pvp = 0;
  8553. clif_send0199(m,0);
  8554. if(battle_config.pk_mode) // disable ranking options if pk_mode is on [Valaris]
  8555. return 0;
  8556. map_foreachinmap(buildin_pvpoff_sub, m, BL_PC);
  8557. return 0;
  8558. }
  8559. BUILDIN_FUNC(gvgon)
  8560. {
  8561. int m;
  8562. const char *str;
  8563. str=script_getstr(st,2);
  8564. m = map_mapname2mapid(str);
  8565. if(m >= 0 && !map[m].flag.gvg) {
  8566. map[m].flag.gvg = 1;
  8567. clif_send0199(m,3);
  8568. }
  8569. return 0;
  8570. }
  8571. BUILDIN_FUNC(gvgoff)
  8572. {
  8573. int m;
  8574. const char *str;
  8575. str=script_getstr(st,2);
  8576. m = map_mapname2mapid(str);
  8577. if(m >= 0 && map[m].flag.gvg) {
  8578. map[m].flag.gvg = 0;
  8579. clif_send0199(m,0);
  8580. }
  8581. return 0;
  8582. }
  8583. /*==========================================
  8584. * Shows an emoticon on top of the player/npc
  8585. * emotion emotion#, <target: 0 - NPC, 1 - PC>
  8586. *------------------------------------------*/
  8587. //Optional second parameter added by [Skotlex]
  8588. BUILDIN_FUNC(emotion)
  8589. {
  8590. int type;
  8591. int player=0;
  8592. type=script_getnum(st,2);
  8593. if(type < 0 || type > 100)
  8594. return 0;
  8595. if( script_hasdata(st,3) )
  8596. player=script_getnum(st,3);
  8597. if (player) {
  8598. TBL_PC *sd = script_rid2sd(st);
  8599. if (sd)
  8600. clif_emotion(&sd->bl,type);
  8601. } else
  8602. clif_emotion(map_id2bl(st->oid),type);
  8603. return 0;
  8604. }
  8605. static int buildin_maprespawnguildid_sub_pc(DBKey key, void *data, va_list ap)
  8606. {
  8607. int m=va_arg(ap,int);
  8608. int g_id=va_arg(ap,int);
  8609. int flag=va_arg(ap,int);
  8610. TBL_PC *sd = (TBL_PC*)data;
  8611. if(!sd || sd->bl.m != m)
  8612. return 0;
  8613. if(
  8614. (sd->status.guild_id == g_id && flag&1) || //Warp out owners
  8615. (sd->status.guild_id != g_id && flag&2) || //Warp out outsiders
  8616. (sd->status.guild_id == 0) // Warp out players not in guild [Valaris]
  8617. )
  8618. pc_setpos(sd,sd->status.save_point.map,sd->status.save_point.x,sd->status.save_point.y,3);
  8619. return 1;
  8620. }
  8621. static int buildin_maprespawnguildid_sub_mob(struct block_list *bl,va_list ap)
  8622. {
  8623. struct mob_data *md=(struct mob_data *)bl;
  8624. if(!md->guardian_data && md->class_ != MOBID_EMPERIUM)
  8625. status_kill(bl);
  8626. return 0;
  8627. }
  8628. BUILDIN_FUNC(maprespawnguildid)
  8629. {
  8630. const char *mapname=script_getstr(st,2);
  8631. int g_id=script_getnum(st,3);
  8632. int flag=script_getnum(st,4);
  8633. int m=map_mapname2mapid(mapname);
  8634. if(m == -1)
  8635. return 0;
  8636. //Catch ALL players (in case some are 'between maps' on execution time)
  8637. map_foreachpc(buildin_maprespawnguildid_sub_pc,m,g_id,flag);
  8638. if (flag&4) //Remove script mobs.
  8639. map_foreachinmap(buildin_maprespawnguildid_sub_mob,m,BL_MOB);
  8640. return 0;
  8641. }
  8642. BUILDIN_FUNC(agitstart)
  8643. {
  8644. if(agit_flag==1) return 0; // Agit already Start.
  8645. agit_flag=1;
  8646. guild_agit_start();
  8647. return 0;
  8648. }
  8649. BUILDIN_FUNC(agitend)
  8650. {
  8651. if(agit_flag==0) return 0; // Agit already End.
  8652. agit_flag=0;
  8653. guild_agit_end();
  8654. return 0;
  8655. }
  8656. /*==========================================
  8657. * Returns whether woe is on or off. // choice script
  8658. *------------------------------------------*/
  8659. BUILDIN_FUNC(agitcheck)
  8660. {
  8661. script_pushint(st,agit_flag);
  8662. return 0;
  8663. }
  8664. BUILDIN_FUNC(flagemblem)
  8665. {
  8666. int g_id=script_getnum(st,2);
  8667. if(g_id < 0) return 0;
  8668. // printf("Script.c: [FlagEmblem] GuildID=%d, Emblem=%d.\n", g->guild_id, g->emblem_id);
  8669. ((struct npc_data *)map_id2bl(st->oid))->u.scr.guild_id = g_id;
  8670. return 0;
  8671. }
  8672. BUILDIN_FUNC(getcastlename)
  8673. {
  8674. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8675. struct guild_castle* gc = guild_mapname2gc(mapname);
  8676. char* name = (gc) ? gc->castle_name : "";
  8677. script_pushconststr(st,name);
  8678. return 0;
  8679. }
  8680. BUILDIN_FUNC(getcastledata)
  8681. {
  8682. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8683. int index = script_getnum(st,3);
  8684. struct guild_castle* gc = guild_mapname2gc(mapname);
  8685. if(script_hasdata(st,4) && index==0 && gc) {
  8686. const char* event = script_getstr(st,4);
  8687. check_event(st, event);
  8688. guild_addcastleinfoevent(gc->castle_id,17,event);
  8689. }
  8690. if(gc){
  8691. switch(index){
  8692. case 0: {
  8693. int i;
  8694. for(i=1;i<26;i++) // Initialize[AgitInit]
  8695. guild_castledataload(gc->castle_id,i);
  8696. } break;
  8697. case 1:
  8698. script_pushint(st,gc->guild_id); break;
  8699. case 2:
  8700. script_pushint(st,gc->economy); break;
  8701. case 3:
  8702. script_pushint(st,gc->defense); break;
  8703. case 4:
  8704. script_pushint(st,gc->triggerE); break;
  8705. case 5:
  8706. script_pushint(st,gc->triggerD); break;
  8707. case 6:
  8708. script_pushint(st,gc->nextTime); break;
  8709. case 7:
  8710. script_pushint(st,gc->payTime); break;
  8711. case 8:
  8712. script_pushint(st,gc->createTime); break;
  8713. case 9:
  8714. script_pushint(st,gc->visibleC); break;
  8715. case 10:
  8716. case 11:
  8717. case 12:
  8718. case 13:
  8719. case 14:
  8720. case 15:
  8721. case 16:
  8722. case 17:
  8723. script_pushint(st,gc->guardian[index-10].visible); break;
  8724. case 18:
  8725. case 19:
  8726. case 20:
  8727. case 21:
  8728. case 22:
  8729. case 23:
  8730. case 24:
  8731. case 25:
  8732. script_pushint(st,gc->guardian[index-18].hp); break;
  8733. default:
  8734. script_pushint(st,0); break;
  8735. }
  8736. return 0;
  8737. }
  8738. script_pushint(st,0);
  8739. return 0;
  8740. }
  8741. BUILDIN_FUNC(setcastledata)
  8742. {
  8743. const char* mapname = mapindex_getmapname(script_getstr(st,2),NULL);
  8744. int index = script_getnum(st,3);
  8745. int value = script_getnum(st,4);
  8746. struct guild_castle* gc = guild_mapname2gc(mapname);
  8747. if(gc) {
  8748. // Save Data byself First
  8749. switch(index){
  8750. case 1:
  8751. gc->guild_id = value; break;
  8752. case 2:
  8753. gc->economy = value; break;
  8754. case 3:
  8755. gc->defense = value; break;
  8756. case 4:
  8757. gc->triggerE = value; break;
  8758. case 5:
  8759. gc->triggerD = value; break;
  8760. case 6:
  8761. gc->nextTime = value; break;
  8762. case 7:
  8763. gc->payTime = value; break;
  8764. case 8:
  8765. gc->createTime = value; break;
  8766. case 9:
  8767. gc->visibleC = value; break;
  8768. case 10:
  8769. case 11:
  8770. case 12:
  8771. case 13:
  8772. case 14:
  8773. case 15:
  8774. case 16:
  8775. case 17:
  8776. gc->guardian[index-10].visible = value; break;
  8777. case 18:
  8778. case 19:
  8779. case 20:
  8780. case 21:
  8781. case 22:
  8782. case 23:
  8783. case 24:
  8784. case 25:
  8785. gc->guardian[index-18].hp = value;
  8786. if (gc->guardian[index-18].id)
  8787. { //Update this mob's HP.
  8788. struct block_list *bl = map_id2bl(gc->guardian[index-18].id);
  8789. if (!bl)
  8790. { //Wrong target?
  8791. gc->guardian[index-18].id = 0;
  8792. break;
  8793. }
  8794. if (value < 1) {
  8795. status_kill(bl);
  8796. break;
  8797. }
  8798. status_set_hp(bl, value, 0);
  8799. }
  8800. break;
  8801. default:
  8802. return 0;
  8803. }
  8804. guild_castledatasave(gc->castle_id,index,value);
  8805. }
  8806. return 0;
  8807. }
  8808. /* =====================================================================
  8809. * ギルド情報を要求する
  8810. * ---------------------------------------------------------------------*/
  8811. BUILDIN_FUNC(requestguildinfo)
  8812. {
  8813. int guild_id=script_getnum(st,2);
  8814. const char *event=NULL;
  8815. if( script_hasdata(st,3) ){
  8816. event=script_getstr(st,3);
  8817. check_event(st, event);
  8818. }
  8819. if(guild_id>0)
  8820. guild_npc_request_info(guild_id,event);
  8821. return 0;
  8822. }
  8823. /* =====================================================================
  8824. * カードの数を得る
  8825. * ---------------------------------------------------------------------*/
  8826. BUILDIN_FUNC(getequipcardcnt)
  8827. {
  8828. int i,num;
  8829. TBL_PC *sd;
  8830. int c=MAX_SLOTS;
  8831. num=script_getnum(st,2);
  8832. sd=script_rid2sd(st);
  8833. i=pc_checkequip(sd,equip[num-1]);
  8834. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8835. {
  8836. script_pushint(st,0);
  8837. return 0;
  8838. }
  8839. do{
  8840. if(sd->status.inventory[i].card[c-1] &&
  8841. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8842. script_pushint(st,(c));
  8843. return 0;
  8844. }
  8845. }while(c--);
  8846. script_pushint(st,0);
  8847. return 0;
  8848. }
  8849. /* ================================================================
  8850. * カード取り外し成功
  8851. * ----------------------------------------------------------------*/
  8852. BUILDIN_FUNC(successremovecards)
  8853. {
  8854. int i,j,num,cardflag=0,flag;
  8855. TBL_PC *sd;
  8856. struct item item_tmp;
  8857. int c=MAX_SLOTS;
  8858. num=script_getnum(st,2);
  8859. sd=script_rid2sd(st);
  8860. i=pc_checkequip(sd,equip[num-1]);
  8861. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8862. return 0;
  8863. do{
  8864. if(sd->status.inventory[i].card[c-1] &&
  8865. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8866. cardflag = 1;
  8867. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8868. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8869. item_tmp.attribute=0;
  8870. for (j = 0; j < MAX_SLOTS; j++)
  8871. item_tmp.card[j]=0;
  8872. //Logs items, got from (N)PC scripts [Lupus]
  8873. if(log_config.enable_logs&0x40)
  8874. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8875. if((flag=pc_additem(sd,&item_tmp,1))){ // 持てないならドロップ
  8876. clif_additem(sd,0,0,flag);
  8877. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8878. }
  8879. }
  8880. }while(c--);
  8881. if(cardflag == 1){ // カードを取り除いたアイテム所得
  8882. flag=0;
  8883. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8884. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8885. item_tmp.attribute=sd->status.inventory[i].attribute;
  8886. //Logs items, got from (N)PC scripts [Lupus]
  8887. if(log_config.enable_logs&0x40)
  8888. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8889. for (j = 0; j < MAX_SLOTS; j++)
  8890. item_tmp.card[j]=0;
  8891. pc_delitem(sd,i,1,0);
  8892. //Logs items, got from (N)PC scripts [Lupus]
  8893. if(log_config.enable_logs&0x40)
  8894. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8895. if((flag=pc_additem(sd,&item_tmp,1))){ // もてないならドロップ
  8896. clif_additem(sd,0,0,flag);
  8897. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8898. }
  8899. clif_misceffect(&sd->bl,3);
  8900. return 0;
  8901. }
  8902. return 0;
  8903. }
  8904. /* ================================================================
  8905. * カード取り外し失敗 slot,type
  8906. * type=0: 両方損失、1:カード損失、2:武具損失、3:損失無し
  8907. * ----------------------------------------------------------------*/
  8908. BUILDIN_FUNC(failedremovecards)
  8909. {
  8910. int i,j,num,cardflag=0,flag,typefail;
  8911. TBL_PC *sd;
  8912. struct item item_tmp;
  8913. int c=MAX_SLOTS;
  8914. num=script_getnum(st,2);
  8915. typefail=script_getnum(st,3);
  8916. sd=script_rid2sd(st);
  8917. i=pc_checkequip(sd,equip[num-1]);
  8918. if(itemdb_isspecial(sd->status.inventory[i].card[0]))
  8919. return 0;
  8920. do{
  8921. if(sd->status.inventory[i].card[c-1] &&
  8922. itemdb_type(sd->status.inventory[i].card[c-1]) == IT_CARD){ // [Celest]
  8923. cardflag = 1;
  8924. if(typefail == 2){ // 武具のみ損失なら、カードは受け取らせる
  8925. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].card[c-1];
  8926. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=0;
  8927. item_tmp.attribute=0;
  8928. for (j = 0; j < MAX_SLOTS; j++)
  8929. item_tmp.card[j]=0;
  8930. //Logs items, got from (N)PC scripts [Lupus]
  8931. if(log_config.enable_logs&0x40)
  8932. log_pick_pc(sd, "N", item_tmp.nameid, 1, NULL);
  8933. if((flag=pc_additem(sd,&item_tmp,1))){
  8934. clif_additem(sd,0,0,flag);
  8935. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8936. }
  8937. }
  8938. }
  8939. }while(c--);
  8940. if(cardflag == 1){
  8941. if(typefail == 0 || typefail == 2){ // 武具損失
  8942. //Logs items, got from (N)PC scripts [Lupus]
  8943. if(log_config.enable_logs&0x40)
  8944. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8945. pc_delitem(sd,i,1,0);
  8946. clif_misceffect(&sd->bl,2);
  8947. return 0;
  8948. }
  8949. if(typefail == 1){ // カードのみ損失(武具を返す)
  8950. flag=0;
  8951. item_tmp.id=0,item_tmp.nameid=sd->status.inventory[i].nameid;
  8952. item_tmp.equip=0,item_tmp.identify=1,item_tmp.refine=sd->status.inventory[i].refine;
  8953. item_tmp.attribute=sd->status.inventory[i].attribute;
  8954. //Logs items, got from (N)PC scripts [Lupus]
  8955. if(log_config.enable_logs&0x40)
  8956. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -1, &sd->status.inventory[i]);
  8957. for (j = 0; j < MAX_SLOTS; j++)
  8958. item_tmp.card[j]=0;
  8959. pc_delitem(sd,i,1,0);
  8960. //Logs items, got from (N)PC scripts [Lupus]
  8961. if(log_config.enable_logs&0x40)
  8962. log_pick_pc(sd, "N", item_tmp.nameid, 1, &item_tmp);
  8963. if((flag=pc_additem(sd,&item_tmp,1))){
  8964. clif_additem(sd,0,0,flag);
  8965. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,NULL,NULL,NULL,0);
  8966. }
  8967. }
  8968. clif_misceffect(&sd->bl,2);
  8969. return 0;
  8970. }
  8971. return 0;
  8972. }
  8973. /* ================================================================
  8974. * mapwarp "<from map>","<to map>",<x>,<y>,<type>,<ID for Type>;
  8975. * type: 0=everyone, 1=guild, 2=party; [Reddozen]
  8976. * improved by [Lance]
  8977. * ================================================================*/
  8978. BUILDIN_FUNC(mapwarp) // Added by RoVeRT
  8979. {
  8980. int x,y,m,check_val=0,check_ID=0,i=0;
  8981. struct guild *g = NULL;
  8982. struct party_data *p = NULL;
  8983. const char *str;
  8984. const char *mapname;
  8985. unsigned int index;
  8986. mapname=script_getstr(st,2);
  8987. str=script_getstr(st,3);
  8988. x=script_getnum(st,4);
  8989. y=script_getnum(st,5);
  8990. if(script_hasdata(st,7)){
  8991. check_val=script_getnum(st,6);
  8992. check_ID=script_getnum(st,7);
  8993. }
  8994. if((m=map_mapname2mapid(mapname))< 0)
  8995. return 0;
  8996. if(!(index=mapindex_name2id(str)))
  8997. return 0;
  8998. switch(check_val){
  8999. case 1:
  9000. g = guild_search(check_ID);
  9001. if (g){
  9002. for( i=0; i < g->max_member; i++)
  9003. {
  9004. if(g->member[i].sd && g->member[i].sd->bl.m==m){
  9005. pc_setpos(g->member[i].sd,index,x,y,3);
  9006. }
  9007. }
  9008. }
  9009. break;
  9010. case 2:
  9011. p = party_search(check_ID);
  9012. if(p){
  9013. for(i=0;i<MAX_PARTY; i++){
  9014. if(p->data[i].sd && p->data[i].sd->bl.m == m){
  9015. pc_setpos(p->data[i].sd,index,x,y,3);
  9016. }
  9017. }
  9018. }
  9019. break;
  9020. default:
  9021. map_foreachinmap(buildin_areawarp_sub,m,BL_PC,index,x,y);
  9022. break;
  9023. }
  9024. return 0;
  9025. }
  9026. BUILDIN_FUNC(cmdothernpc) // Added by RoVeRT
  9027. {
  9028. const char *npc,*command;
  9029. npc=script_getstr(st,2);
  9030. command=script_getstr(st,3);
  9031. npc_command(map_id2sd(st->rid),npc,command);
  9032. return 0;
  9033. }
  9034. BUILDIN_FUNC(inittimer) // Added by RoVeRT
  9035. {
  9036. // struct npc_data *nd=(struct npc_data*)map_id2bl(st->oid);
  9037. // nd->lastaction=nd->timer=gettick();
  9038. npc_do_ontimer(st->oid, 1);
  9039. return 0;
  9040. }
  9041. BUILDIN_FUNC(stoptimer) // Added by RoVeRT
  9042. {
  9043. // struct npc_data *nd=(struct npc_data*)map_id2bl(st->oid);
  9044. // nd->lastaction=nd->timer=-1;
  9045. npc_do_ontimer(st->oid, 0);
  9046. return 0;
  9047. }
  9048. static int buildin_mobcount_sub(struct block_list *bl,va_list ap) // Added by RoVeRT
  9049. {
  9050. char *event=va_arg(ap,char *);
  9051. if(strcmp(event,((struct mob_data *)bl)->npc_event)==0)
  9052. return 1;
  9053. return 0;
  9054. }
  9055. BUILDIN_FUNC(mobcount) // Added by RoVeRT
  9056. {
  9057. const char *mapname,*event;
  9058. int m;
  9059. mapname=script_getstr(st,2);
  9060. event=script_getstr(st,3);
  9061. check_event(st, event);
  9062. if( (m=map_mapname2mapid(mapname))<0 ) {
  9063. script_pushint(st,-1);
  9064. return 0;
  9065. }
  9066. script_pushint(st,map_foreachinmap(buildin_mobcount_sub, m, BL_MOB, event));
  9067. return 0;
  9068. }
  9069. BUILDIN_FUNC(marriage)
  9070. {
  9071. const char *partner=script_getstr(st,2);
  9072. TBL_PC *sd=script_rid2sd(st);
  9073. TBL_PC *p_sd=map_nick2sd(partner);
  9074. if(sd==NULL || p_sd==NULL || pc_marriage(sd,p_sd) < 0){
  9075. script_pushint(st,0);
  9076. return 0;
  9077. }
  9078. script_pushint(st,1);
  9079. return 0;
  9080. }
  9081. BUILDIN_FUNC(wedding_effect)
  9082. {
  9083. TBL_PC *sd=script_rid2sd(st);
  9084. struct block_list *bl;
  9085. if(sd==NULL) {
  9086. bl=map_id2bl(st->oid);
  9087. } else
  9088. bl=&sd->bl;
  9089. clif_wedding_effect(bl);
  9090. return 0;
  9091. }
  9092. BUILDIN_FUNC(divorce)
  9093. {
  9094. TBL_PC *sd=script_rid2sd(st);
  9095. if(sd==NULL || pc_divorce(sd) < 0){
  9096. script_pushint(st,0);
  9097. return 0;
  9098. }
  9099. script_pushint(st,1);
  9100. return 0;
  9101. }
  9102. BUILDIN_FUNC(ispartneron)
  9103. {
  9104. TBL_PC *sd=script_rid2sd(st);
  9105. TBL_PC *p_sd=NULL;
  9106. if(sd==NULL || !pc_ismarried(sd) ||
  9107. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9108. script_pushint(st,0);
  9109. return 0;
  9110. }
  9111. script_pushint(st,1);
  9112. return 0;
  9113. }
  9114. BUILDIN_FUNC(getpartnerid)
  9115. {
  9116. TBL_PC *sd=script_rid2sd(st);
  9117. if (sd == NULL) {
  9118. script_pushint(st,0);
  9119. return 0;
  9120. }
  9121. script_pushint(st,sd->status.partner_id);
  9122. return 0;
  9123. }
  9124. BUILDIN_FUNC(getchildid)
  9125. {
  9126. TBL_PC *sd=script_rid2sd(st);
  9127. if (sd == NULL) {
  9128. script_pushint(st,0);
  9129. return 0;
  9130. }
  9131. script_pushint(st,sd->status.child);
  9132. return 0;
  9133. }
  9134. BUILDIN_FUNC(getmotherid)
  9135. {
  9136. TBL_PC *sd=script_rid2sd(st);
  9137. if (sd == NULL) {
  9138. script_pushint(st,0);
  9139. return 0;
  9140. }
  9141. script_pushint(st,sd->status.mother);
  9142. return 0;
  9143. }
  9144. BUILDIN_FUNC(getfatherid)
  9145. {
  9146. TBL_PC *sd=script_rid2sd(st);
  9147. if (sd == NULL) {
  9148. script_pushint(st,0);
  9149. return 0;
  9150. }
  9151. script_pushint(st,sd->status.father);
  9152. return 0;
  9153. }
  9154. BUILDIN_FUNC(warppartner)
  9155. {
  9156. int x,y;
  9157. unsigned short mapindex;
  9158. const char *str;
  9159. TBL_PC *sd=script_rid2sd(st);
  9160. TBL_PC *p_sd=NULL;
  9161. if(sd==NULL || !pc_ismarried(sd) ||
  9162. (p_sd=map_charid2sd(sd->status.partner_id)) == NULL) {
  9163. script_pushint(st,0);
  9164. return 0;
  9165. }
  9166. str=script_getstr(st,2);
  9167. x=script_getnum(st,3);
  9168. y=script_getnum(st,4);
  9169. mapindex = mapindex_name2id(str);
  9170. if (mapindex) {
  9171. pc_setpos(p_sd,mapindex,x,y,0);
  9172. script_pushint(st,1);
  9173. } else
  9174. script_pushint(st,0);
  9175. return 0;
  9176. }
  9177. /*================================================
  9178. * Script for Displaying MOB Information [Valaris]
  9179. *------------------------------------------------*/
  9180. BUILDIN_FUNC(strmobinfo)
  9181. {
  9182. int num=script_getnum(st,2);
  9183. int class_=script_getnum(st,3);
  9184. if(!mobdb_checkid(class_))
  9185. {
  9186. script_pushint(st,0);
  9187. return 0;
  9188. }
  9189. switch (num) {
  9190. case 1: script_pushconststr(st,mob_db(class_)->name); break;
  9191. case 2: script_pushconststr(st,mob_db(class_)->jname); break;
  9192. case 3: script_pushint(st,mob_db(class_)->lv); break;
  9193. case 4: script_pushint(st,mob_db(class_)->status.max_hp); break;
  9194. case 5: script_pushint(st,mob_db(class_)->status.max_sp); break;
  9195. case 6: script_pushint(st,mob_db(class_)->base_exp); break;
  9196. case 7: script_pushint(st,mob_db(class_)->job_exp); break;
  9197. default:
  9198. script_pushint(st,0);
  9199. break;
  9200. }
  9201. return 0;
  9202. }
  9203. /*==========================================
  9204. * Summon guardians [Valaris]
  9205. * guardian "<map name>",<x>,<y>,"<name to show>",<mob id>,{,"<event label>"}{,<guardian index>};
  9206. *------------------------------------------*/
  9207. BUILDIN_FUNC(guardian)
  9208. {
  9209. int class_=0,x=0,y=0,guardian=0;
  9210. const char *str,*map,*evt="";
  9211. struct script_data *data;
  9212. map =script_getstr(st,2);
  9213. x =script_getnum(st,3);
  9214. y =script_getnum(st,4);
  9215. str =script_getstr(st,5);
  9216. class_=script_getnum(st,6);
  9217. if( script_hasdata(st,8) )
  9218. {// "<event label>",<guardian index>
  9219. evt=script_getstr(st,7);
  9220. guardian=script_getnum(st,8);
  9221. } else if( script_hasdata(st,7) ){
  9222. data=script_getdata(st,7);
  9223. get_val(st,data);
  9224. if( data_isstring(data) )
  9225. {// "<event label>"
  9226. evt=script_getstr(st,7);
  9227. } else if( data_isint(data) )
  9228. {// <guardian index>
  9229. guardian=script_getnum(st,7);
  9230. } else {
  9231. ShowError("script:guardian: invalid data type for argument #6 (from 1)\n");
  9232. script_reportdata(data);
  9233. return 1;
  9234. }
  9235. }
  9236. check_event(st, evt);
  9237. mob_spawn_guardian(map,x,y,str,class_,evt,guardian);
  9238. return 0;
  9239. }
  9240. /*================================================
  9241. * Script for Displaying Guardian Info [Valaris]
  9242. *------------------------------------------------*/
  9243. BUILDIN_FUNC(guardianinfo)
  9244. {
  9245. int guardian=script_getnum(st,2);
  9246. TBL_PC *sd=script_rid2sd(st);
  9247. struct guild_castle *gc=guild_mapname2gc(map[sd->bl.m].name);
  9248. if (guardian < 0 || guardian >= MAX_GUARDIANS || gc==NULL)
  9249. {
  9250. script_pushint(st,-1);
  9251. return 0;
  9252. }
  9253. if(gc->guardian[guardian].visible)
  9254. script_pushint(st,gc->guardian[guardian].hp);
  9255. else script_pushint(st,-1);
  9256. return 0;
  9257. }
  9258. /*==========================================
  9259. * IDからItem名
  9260. *------------------------------------------*/
  9261. BUILDIN_FUNC(getitemname)
  9262. {
  9263. int item_id=0;
  9264. struct item_data *i_data;
  9265. char *item_name;
  9266. struct script_data *data;
  9267. data=script_getdata(st,2);
  9268. get_val(st,data);
  9269. if( data_isstring(data) ){
  9270. const char *name=conv_str(st,data);
  9271. struct item_data *item_data = itemdb_searchname(name);
  9272. if( item_data )
  9273. item_id=item_data->nameid;
  9274. }else
  9275. item_id=conv_num(st,data);
  9276. i_data = itemdb_exists(item_id);
  9277. if (i_data == NULL)
  9278. {
  9279. script_pushconststr(st,"null");
  9280. return 0;
  9281. }
  9282. item_name=(char *)aMallocA(ITEM_NAME_LENGTH*sizeof(char));
  9283. memcpy(item_name, i_data->jname, ITEM_NAME_LENGTH);
  9284. script_pushstr(st,item_name);
  9285. return 0;
  9286. }
  9287. /*==========================================
  9288. * Returns number of slots an item has. [Skotlex]
  9289. *------------------------------------------*/
  9290. BUILDIN_FUNC(getitemslots)
  9291. {
  9292. int item_id;
  9293. struct item_data *i_data;
  9294. item_id=script_getnum(st,2);
  9295. i_data = itemdb_exists(item_id);
  9296. if (i_data)
  9297. script_pushint(st,i_data->slot);
  9298. else
  9299. script_pushint(st,-1);
  9300. return 0;
  9301. }
  9302. /*==========================================
  9303. * Returns some values of an item [Lupus]
  9304. * Price, Weight, etc...
  9305. getiteminfo(itemID,n), where n
  9306. 0 value_buy;
  9307. 1 value_sell;
  9308. 2 type;
  9309. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9310. if = 0, then monsters don't drop it at all (rare or a quest item)
  9311. if = 10000, then this item is sold in NPC shops only
  9312. 4 sex;
  9313. 5 equip;
  9314. 6 weight;
  9315. 7 atk;
  9316. 8 def;
  9317. 9 range;
  9318. 10 slot;
  9319. 11 look;
  9320. 12 elv;
  9321. 13 wlv;
  9322. 14 view id
  9323. *------------------------------------------*/
  9324. BUILDIN_FUNC(getiteminfo)
  9325. {
  9326. int item_id,n;
  9327. int *item_arr;
  9328. struct item_data *i_data;
  9329. item_id = script_getnum(st,2);
  9330. n = script_getnum(st,3);
  9331. i_data = itemdb_exists(item_id);
  9332. if (i_data && n>=0 && n<14) {
  9333. item_arr = (int*)&i_data->value_buy;
  9334. script_pushint(st,item_arr[n]);
  9335. } else
  9336. script_pushint(st,-1);
  9337. return 0;
  9338. }
  9339. /*==========================================
  9340. * Set some values of an item [Lupus]
  9341. * Price, Weight, etc...
  9342. setiteminfo(itemID,n,Value), where n
  9343. 0 value_buy;
  9344. 1 value_sell;
  9345. 2 type;
  9346. 3 maxchance = Max drop chance of this item e.g. 1 = 0.01% , etc..
  9347. if = 0, then monsters don't drop it at all (rare or a quest item)
  9348. if = 10000, then this item is sold in NPC shops only
  9349. 4 sex;
  9350. 5 equip;
  9351. 6 weight;
  9352. 7 atk;
  9353. 8 def;
  9354. 9 range;
  9355. 10 slot;
  9356. 11 look;
  9357. 12 elv;
  9358. 13 wlv;
  9359. 14 view id
  9360. * Returns Value or -1 if the wrong field's been set
  9361. *------------------------------------------*/
  9362. BUILDIN_FUNC(setiteminfo)
  9363. {
  9364. int item_id,n,value;
  9365. int *item_arr;
  9366. struct item_data *i_data;
  9367. item_id = script_getnum(st,2);
  9368. n = script_getnum(st,3);
  9369. value = script_getnum(st,4);
  9370. i_data = itemdb_exists(item_id);
  9371. if (i_data && n>=0 && n<14) {
  9372. item_arr = (int*)&i_data->value_buy;
  9373. item_arr[n] = value;
  9374. script_pushint(st,value);
  9375. } else
  9376. script_pushint(st,-1);
  9377. return 0;
  9378. }
  9379. /*==========================================
  9380. * Returns value from equipped item slot n [Lupus]
  9381. getequipcardid(num,slot)
  9382. where
  9383. num = eqip position slot
  9384. slot = 0,1,2,3 (Card Slot N)
  9385. This func returns CARD ID, 255,254,-255 (for card 0, if the item is produced)
  9386. it's useful when you want to check item cards or if it's signed
  9387. Useful for such quests as "Sign this refined item with players name" etc
  9388. Hat[0] +4 -> Player's Hat[0] +4
  9389. *------------------------------------------*/
  9390. BUILDIN_FUNC(getequipcardid)
  9391. {
  9392. int i,num,slot;
  9393. TBL_PC *sd;
  9394. num=script_getnum(st,2);
  9395. slot=script_getnum(st,3);
  9396. sd=script_rid2sd(st);
  9397. i=pc_checkequip(sd,equip[num-1]);
  9398. if(i >= 0 && slot>=0 && slot<4)
  9399. script_pushint(st,sd->status.inventory[i].card[slot]);
  9400. else
  9401. script_pushint(st,0);
  9402. return 0;
  9403. }
  9404. /*==========================================
  9405. * petskillbonus [Valaris] //Rewritten by [Skotlex]
  9406. *------------------------------------------*/
  9407. BUILDIN_FUNC(petskillbonus)
  9408. {
  9409. struct pet_data *pd;
  9410. TBL_PC *sd=script_rid2sd(st);
  9411. if(sd==NULL || sd->pd==NULL)
  9412. return 0;
  9413. pd=sd->pd;
  9414. if (pd->bonus)
  9415. { //Clear previous bonus
  9416. if (pd->bonus->timer != -1)
  9417. delete_timer(pd->bonus->timer, pet_skill_bonus_timer);
  9418. } else //init
  9419. pd->bonus = (struct pet_bonus *) aMalloc(sizeof(struct pet_bonus));
  9420. pd->bonus->type=script_getnum(st,2);
  9421. pd->bonus->val=script_getnum(st,3);
  9422. pd->bonus->duration=script_getnum(st,4);
  9423. pd->bonus->delay=script_getnum(st,5);
  9424. if (pd->state.skillbonus == 1)
  9425. pd->state.skillbonus=0; // waiting state
  9426. // wait for timer to start
  9427. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9428. pd->bonus->timer=-1;
  9429. else
  9430. pd->bonus->timer=add_timer(gettick()+pd->bonus->delay*1000, pet_skill_bonus_timer, sd->bl.id, 0);
  9431. return 0;
  9432. }
  9433. /*==========================================
  9434. * pet looting [Valaris] //Rewritten by [Skotlex]
  9435. *------------------------------------------*/
  9436. BUILDIN_FUNC(petloot)
  9437. {
  9438. int max;
  9439. struct pet_data *pd;
  9440. TBL_PC *sd=script_rid2sd(st);
  9441. if(sd==NULL || sd->pd==NULL)
  9442. return 0;
  9443. max=script_getnum(st,2);
  9444. if(max < 1)
  9445. max = 1; //Let'em loot at least 1 item.
  9446. else if (max > MAX_PETLOOT_SIZE)
  9447. max = MAX_PETLOOT_SIZE;
  9448. pd = sd->pd;
  9449. if (pd->loot != NULL)
  9450. { //Release whatever was there already and reallocate memory
  9451. pet_lootitem_drop(pd, pd->msd);
  9452. aFree(pd->loot->item);
  9453. }
  9454. else
  9455. pd->loot = (struct pet_loot *)aMalloc(sizeof(struct pet_loot));
  9456. pd->loot->item = (struct item *)aCalloc(max,sizeof(struct item));
  9457. pd->loot->max=max;
  9458. pd->loot->count = 0;
  9459. pd->loot->weight = 0;
  9460. return 0;
  9461. }
  9462. /*==========================================
  9463. * PCの所持品情報読み取り
  9464. *------------------------------------------*/
  9465. BUILDIN_FUNC(getinventorylist)
  9466. {
  9467. TBL_PC *sd=script_rid2sd(st);
  9468. char card_var[NAME_LENGTH];
  9469. int i,j=0,k;
  9470. if(!sd) return 0;
  9471. for(i=0;i<MAX_INVENTORY;i++){
  9472. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount > 0){
  9473. pc_setreg(sd,add_str("@inventorylist_id")+(j<<24),sd->status.inventory[i].nameid);
  9474. pc_setreg(sd,add_str("@inventorylist_amount")+(j<<24),sd->status.inventory[i].amount);
  9475. pc_setreg(sd,add_str("@inventorylist_equip")+(j<<24),sd->status.inventory[i].equip);
  9476. pc_setreg(sd,add_str("@inventorylist_refine")+(j<<24),sd->status.inventory[i].refine);
  9477. pc_setreg(sd,add_str("@inventorylist_identify")+(j<<24),sd->status.inventory[i].identify);
  9478. pc_setreg(sd,add_str("@inventorylist_attribute")+(j<<24),sd->status.inventory[i].attribute);
  9479. for (k = 0; k < MAX_SLOTS; k++)
  9480. {
  9481. sprintf(card_var, "@inventorylist_card%d",k+1);
  9482. pc_setreg(sd,add_str(card_var)+(j<<24),sd->status.inventory[i].card[k]);
  9483. }
  9484. j++;
  9485. }
  9486. }
  9487. pc_setreg(sd,add_str("@inventorylist_count"),j);
  9488. return 0;
  9489. }
  9490. BUILDIN_FUNC(getskilllist)
  9491. {
  9492. TBL_PC *sd=script_rid2sd(st);
  9493. int i,j=0;
  9494. if(!sd) return 0;
  9495. for(i=0;i<MAX_SKILL;i++){
  9496. if(sd->status.skill[i].id > 0 && sd->status.skill[i].lv > 0){
  9497. pc_setreg(sd,add_str("@skilllist_id")+(j<<24),sd->status.skill[i].id);
  9498. pc_setreg(sd,add_str("@skilllist_lv")+(j<<24),sd->status.skill[i].lv);
  9499. pc_setreg(sd,add_str("@skilllist_flag")+(j<<24),sd->status.skill[i].flag);
  9500. j++;
  9501. }
  9502. }
  9503. pc_setreg(sd,add_str("@skilllist_count"),j);
  9504. return 0;
  9505. }
  9506. BUILDIN_FUNC(clearitem)
  9507. {
  9508. TBL_PC *sd=script_rid2sd(st);
  9509. int i;
  9510. if(sd==NULL) return 0;
  9511. for (i=0; i<MAX_INVENTORY; i++) {
  9512. if (sd->status.inventory[i].amount) {
  9513. //Logs items, got from (N)PC scripts [Lupus]
  9514. if(log_config.enable_logs&0x40)
  9515. log_pick_pc(sd, "N", sd->status.inventory[i].nameid, -sd->status.inventory[i].amount, &sd->status.inventory[i]);
  9516. pc_delitem(sd, i, sd->status.inventory[i].amount, 0);
  9517. }
  9518. }
  9519. return 0;
  9520. }
  9521. /*==========================================
  9522. * Disguise Player (returns Mob/NPC ID if success, 0 on fail)
  9523. *------------------------------------------*/
  9524. BUILDIN_FUNC(disguise)
  9525. {
  9526. int id;
  9527. TBL_PC* sd = script_rid2sd(st);
  9528. if (sd == NULL) return 0;
  9529. id = script_getnum(st,2);
  9530. if (mobdb_checkid(id) || npcdb_checkid(id)) {
  9531. pc_disguise(sd, id);
  9532. script_pushint(st,id);
  9533. } else
  9534. script_pushint(st,0);
  9535. return 0;
  9536. }
  9537. /*==========================================
  9538. * Undisguise Player (returns 1 if success, 0 on fail)
  9539. *------------------------------------------*/
  9540. BUILDIN_FUNC(undisguise)
  9541. {
  9542. TBL_PC* sd = script_rid2sd(st);
  9543. if (sd == NULL) return 0;
  9544. if (sd->disguise) {
  9545. pc_disguise(sd, 0);
  9546. script_pushint(st,0);
  9547. } else {
  9548. script_pushint(st,1);
  9549. }
  9550. return 0;
  9551. }
  9552. /*==========================================
  9553. * NPCクラスチェンジ
  9554. * classは変わりたいclass
  9555. * typeは通常0なのかな?
  9556. *------------------------------------------*/
  9557. BUILDIN_FUNC(classchange)
  9558. {
  9559. int _class,type;
  9560. struct block_list *bl=map_id2bl(st->oid);
  9561. if(bl==NULL) return 0;
  9562. _class=script_getnum(st,2);
  9563. type=script_getnum(st,3);
  9564. clif_class_change(bl,_class,type);
  9565. return 0;
  9566. }
  9567. /*==========================================
  9568. * NPCから発生するエフェクト
  9569. *------------------------------------------*/
  9570. BUILDIN_FUNC(misceffect)
  9571. {
  9572. int type;
  9573. type=script_getnum(st,2);
  9574. if(st->oid && st->oid != fake_nd->bl.id) {
  9575. struct block_list *bl = map_id2bl(st->oid);
  9576. if (bl)
  9577. clif_misceffect2(bl,type);
  9578. } else{
  9579. TBL_PC *sd=script_rid2sd(st);
  9580. if(sd)
  9581. clif_misceffect2(&sd->bl,type);
  9582. }
  9583. return 0;
  9584. }
  9585. /*==========================================
  9586. * サウンドエフェクト
  9587. *------------------------------------------*/
  9588. BUILDIN_FUNC(soundeffect)
  9589. {
  9590. TBL_PC* sd = script_rid2sd(st);
  9591. const char* name = script_getstr(st,2);
  9592. int type = script_getnum(st,3);
  9593. if(sd)
  9594. {
  9595. if(!st->rid)
  9596. clif_soundeffect(sd,map_id2bl(st->oid),name,type);
  9597. else
  9598. clif_soundeffect(sd,&sd->bl,name,type);
  9599. }
  9600. return 0;
  9601. }
  9602. int soundeffect_sub(struct block_list* bl,va_list ap)
  9603. {
  9604. char* name = va_arg(ap,char*);
  9605. int type = va_arg(ap,int);
  9606. clif_soundeffect((TBL_PC *)bl, bl, name, type);
  9607. return 0;
  9608. }
  9609. /*==========================================
  9610. * Play a sound effect (.wav) on multiple clients
  9611. * soundeffectall "<filepath>",<type>{,"<map name>"}{,<x0>,<y0>,<x1>,<y1>};
  9612. *------------------------------------------*/
  9613. BUILDIN_FUNC(soundeffectall)
  9614. {
  9615. struct block_list* bl;
  9616. const char* name;
  9617. int type;
  9618. bl = (st->rid) ? &(script_rid2sd(st)->bl) : map_id2bl(st->oid);
  9619. if (!bl)
  9620. return 0;
  9621. name = script_getstr(st,2);
  9622. type = script_getnum(st,3);
  9623. //FIXME: enumerating map squares (map_foreach) is slower than enumerating the list of online players (map_foreachpc?) [ultramage]
  9624. if(!script_hasdata(st,4))
  9625. { // area around
  9626. clif_soundeffectall(bl, name, type, AREA);
  9627. }
  9628. else
  9629. if(!script_hasdata(st,5))
  9630. { // entire map
  9631. const char* map = script_getstr(st,4);
  9632. map_foreachinmap(soundeffect_sub, map_mapname2mapid(map), BL_PC, name, type);
  9633. }
  9634. else
  9635. if(script_hasdata(st,9))
  9636. { // specified part of map
  9637. const char* map = script_getstr(st,5);
  9638. int x0 = script_getnum(st,6);
  9639. int y0 = script_getnum(st,7);
  9640. int x1 = script_getnum(st,8);
  9641. int y1 = script_getnum(st,9);
  9642. map_foreachinarea(soundeffect_sub, map_mapname2mapid(map), x0, y0, x1, y1, BL_PC, name, type);
  9643. }
  9644. else
  9645. {
  9646. ShowError("buildin_soundeffectall: insufficient arguments for specific area broadcast.\n");
  9647. }
  9648. return 0;
  9649. }
  9650. /*==========================================
  9651. * pet status recovery [Valaris] / Rewritten by [Skotlex]
  9652. *------------------------------------------*/
  9653. BUILDIN_FUNC(petrecovery)
  9654. {
  9655. struct pet_data *pd;
  9656. TBL_PC *sd=script_rid2sd(st);
  9657. if(sd==NULL || sd->pd==NULL)
  9658. return 0;
  9659. pd=sd->pd;
  9660. if (pd->recovery)
  9661. { //Halt previous bonus
  9662. if (pd->recovery->timer != -1)
  9663. delete_timer(pd->recovery->timer, pet_recovery_timer);
  9664. } else //Init
  9665. pd->recovery = (struct pet_recovery *)aMalloc(sizeof(struct pet_recovery));
  9666. pd->recovery->type=script_getnum(st,2);
  9667. pd->recovery->delay=script_getnum(st,3);
  9668. pd->recovery->timer=-1;
  9669. return 0;
  9670. }
  9671. /*==========================================
  9672. * pet healing [Valaris] //Rewritten by [Skotlex]
  9673. *------------------------------------------*/
  9674. BUILDIN_FUNC(petheal)
  9675. {
  9676. struct pet_data *pd;
  9677. TBL_PC *sd=script_rid2sd(st);
  9678. if(sd==NULL || sd->pd==NULL)
  9679. return 0;
  9680. pd=sd->pd;
  9681. if (pd->s_skill)
  9682. { //Clear previous skill
  9683. if (pd->s_skill->timer != -1)
  9684. {
  9685. if (pd->s_skill->id)
  9686. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9687. else
  9688. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9689. }
  9690. } else //init memory
  9691. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9692. pd->s_skill->id=0; //This id identifies that it IS petheal rather than pet_skillsupport
  9693. //Use the lv as the amount to heal
  9694. pd->s_skill->lv=script_getnum(st,2);
  9695. pd->s_skill->delay=script_getnum(st,3);
  9696. pd->s_skill->hp=script_getnum(st,4);
  9697. pd->s_skill->sp=script_getnum(st,5);
  9698. //Use delay as initial offset to avoid skill/heal exploits
  9699. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9700. pd->s_skill->timer=-1;
  9701. else
  9702. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_heal_timer,sd->bl.id,0);
  9703. return 0;
  9704. }
  9705. /*==========================================
  9706. * pet attack skills [Valaris] //Rewritten by [Skotlex]
  9707. *------------------------------------------*/
  9708. BUILDIN_FUNC(petskillattack)
  9709. {
  9710. struct pet_data *pd;
  9711. TBL_PC *sd=script_rid2sd(st);
  9712. if(sd==NULL || sd->pd==NULL)
  9713. return 0;
  9714. pd=sd->pd;
  9715. if (pd->a_skill == NULL)
  9716. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9717. pd->a_skill->id=script_getnum(st,2);
  9718. pd->a_skill->lv=script_getnum(st,3);
  9719. pd->a_skill->div_ = 0;
  9720. pd->a_skill->rate=script_getnum(st,4);
  9721. pd->a_skill->bonusrate=script_getnum(st,5);
  9722. return 0;
  9723. }
  9724. /*==========================================
  9725. * pet attack skills [Valaris]
  9726. *------------------------------------------*/
  9727. BUILDIN_FUNC(petskillattack2)
  9728. {
  9729. struct pet_data *pd;
  9730. TBL_PC *sd=script_rid2sd(st);
  9731. if(sd==NULL || sd->pd==NULL)
  9732. return 0;
  9733. pd=sd->pd;
  9734. if (pd->a_skill == NULL)
  9735. pd->a_skill = (struct pet_skill_attack *)aMalloc(sizeof(struct pet_skill_attack));
  9736. pd->a_skill->id=script_getnum(st,2);
  9737. pd->a_skill->lv=script_getnum(st,3);
  9738. pd->a_skill->div_ = script_getnum(st,4);
  9739. pd->a_skill->rate=script_getnum(st,5);
  9740. pd->a_skill->bonusrate=script_getnum(st,6);
  9741. return 0;
  9742. }
  9743. /*==========================================
  9744. * pet support skills [Skotlex]
  9745. *------------------------------------------*/
  9746. BUILDIN_FUNC(petskillsupport)
  9747. {
  9748. struct pet_data *pd;
  9749. TBL_PC *sd=script_rid2sd(st);
  9750. if(sd==NULL || sd->pd==NULL)
  9751. return 0;
  9752. pd=sd->pd;
  9753. if (pd->s_skill)
  9754. { //Clear previous skill
  9755. if (pd->s_skill->timer != -1)
  9756. {
  9757. if (pd->s_skill->id)
  9758. delete_timer(pd->s_skill->timer, pet_skill_support_timer);
  9759. else
  9760. delete_timer(pd->s_skill->timer, pet_heal_timer);
  9761. }
  9762. } else //init memory
  9763. pd->s_skill = (struct pet_skill_support *) aMalloc(sizeof(struct pet_skill_support));
  9764. pd->s_skill->id=script_getnum(st,2);
  9765. pd->s_skill->lv=script_getnum(st,3);
  9766. pd->s_skill->delay=script_getnum(st,4);
  9767. pd->s_skill->hp=script_getnum(st,5);
  9768. pd->s_skill->sp=script_getnum(st,6);
  9769. //Use delay as initial offset to avoid skill/heal exploits
  9770. if (battle_config.pet_equip_required && pd->pet.equip == 0)
  9771. pd->s_skill->timer=-1;
  9772. else
  9773. pd->s_skill->timer=add_timer(gettick()+pd->s_skill->delay*1000,pet_skill_support_timer,sd->bl.id,0);
  9774. return 0;
  9775. }
  9776. /*==========================================
  9777. * Scripted skill effects [Celest]
  9778. *------------------------------------------*/
  9779. BUILDIN_FUNC(skilleffect)
  9780. {
  9781. TBL_PC *sd;
  9782. int skillid=script_getnum(st,2);
  9783. int skilllv=script_getnum(st,3);
  9784. sd=script_rid2sd(st);
  9785. clif_skill_nodamage(&sd->bl,&sd->bl,skillid,skilllv,1);
  9786. return 0;
  9787. }
  9788. /*==========================================
  9789. * NPC skill effects [Valaris]
  9790. *------------------------------------------*/
  9791. BUILDIN_FUNC(npcskilleffect)
  9792. {
  9793. struct block_list *bl= map_id2bl(st->oid);
  9794. int skillid=script_getnum(st,2);
  9795. int skilllv=script_getnum(st,3);
  9796. int x=script_getnum(st,4);
  9797. int y=script_getnum(st,5);
  9798. if (bl)
  9799. clif_skill_poseffect(bl,skillid,skilllv,x,y,gettick());
  9800. return 0;
  9801. }
  9802. /*==========================================
  9803. * Special effects [Valaris]
  9804. *------------------------------------------*/
  9805. BUILDIN_FUNC(specialeffect)
  9806. {
  9807. struct block_list *bl=map_id2bl(st->oid);
  9808. if(bl==NULL)
  9809. return 0;
  9810. clif_specialeffect(bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9811. return 0;
  9812. }
  9813. BUILDIN_FUNC(specialeffect2)
  9814. {
  9815. TBL_PC *sd=script_rid2sd(st);
  9816. if(sd==NULL)
  9817. return 0;
  9818. clif_specialeffect(&sd->bl,script_getnum(st,2), (script_hasdata(st,3)?script_getnum(st,3):AREA));
  9819. return 0;
  9820. }
  9821. /*==========================================
  9822. * Nude [Valaris]
  9823. *------------------------------------------*/
  9824. BUILDIN_FUNC(nude)
  9825. {
  9826. TBL_PC *sd=script_rid2sd(st);
  9827. int i,calcflag=0;
  9828. if(sd==NULL)
  9829. return 0;
  9830. for(i=0;i<11;i++)
  9831. if(sd->equip_index[i] >= 0) {
  9832. if(!calcflag)
  9833. calcflag=1;
  9834. pc_unequipitem(sd,sd->equip_index[i],2);
  9835. }
  9836. if(calcflag)
  9837. status_calc_pc(sd,0);
  9838. return 0;
  9839. }
  9840. /*==========================================
  9841. * gmcommand [MouseJstr]
  9842. *
  9843. * suggested on the forums...
  9844. * splitted into atcommand & charcommand by [Skotlex]
  9845. *------------------------------------------*/
  9846. BUILDIN_FUNC(atcommand)
  9847. {
  9848. TBL_PC *sd=NULL;
  9849. const char *cmd;
  9850. cmd = script_getstr(st,2);
  9851. if (st->rid)
  9852. sd = script_rid2sd(st);
  9853. if (sd){
  9854. if(cmd[0] != atcommand_symbol){
  9855. cmd += strlen(sd->status.name);
  9856. while(*cmd != atcommand_symbol && *cmd != 0)
  9857. cmd++;
  9858. }
  9859. is_atcommand_sub(sd->fd, sd, cmd, 99);
  9860. } else { //Use a dummy character.
  9861. TBL_PC dummy_sd;
  9862. struct block_list *bl = NULL;
  9863. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9864. if (st->oid) bl = map_id2bl(st->oid);
  9865. if (bl) {
  9866. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9867. if (bl->type == BL_NPC)
  9868. strncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9869. }
  9870. if(cmd[0] != atcommand_symbol){
  9871. cmd += strlen(dummy_sd.status.name);
  9872. while(*cmd != atcommand_symbol && *cmd != 0)
  9873. cmd++;
  9874. }
  9875. is_atcommand_sub(0, &dummy_sd, cmd, 99);
  9876. }
  9877. return 0;
  9878. }
  9879. BUILDIN_FUNC(charcommand)
  9880. {
  9881. TBL_PC *sd=NULL;
  9882. const char *cmd;
  9883. cmd = script_getstr(st,2);
  9884. if (st->rid)
  9885. sd = script_rid2sd(st);
  9886. if (sd){
  9887. if(cmd[0] != charcommand_symbol){
  9888. cmd += strlen(sd->status.name);
  9889. while(*cmd != charcommand_symbol && *cmd != 0)
  9890. cmd++;
  9891. }
  9892. is_charcommand_sub(sd->fd, sd, cmd,99);
  9893. } else { //Use a dummy character.
  9894. TBL_PC dummy_sd;
  9895. struct block_list *bl = NULL;
  9896. memset(&dummy_sd, 0, sizeof(TBL_PC));
  9897. if (st->oid) bl = map_id2bl(st->oid);
  9898. if (bl) {
  9899. memcpy(&dummy_sd.bl, bl, sizeof(struct block_list));
  9900. if (bl->type == BL_NPC)
  9901. strncpy(dummy_sd.status.name, ((TBL_NPC*)bl)->name, NAME_LENGTH);
  9902. }
  9903. if(cmd[0] != charcommand_symbol){
  9904. cmd += strlen(dummy_sd.status.name);
  9905. while(*cmd != charcommand_symbol && *cmd != 0)
  9906. cmd++;
  9907. }
  9908. is_charcommand_sub(0, &dummy_sd, cmd, 99);
  9909. }
  9910. return 0;
  9911. }
  9912. /*==========================================
  9913. * Displays a message for the player only (like system messages like "you got an apple" )
  9914. *------------------------------------------*/
  9915. BUILDIN_FUNC(dispbottom)
  9916. {
  9917. TBL_PC *sd=script_rid2sd(st);
  9918. const char *message;
  9919. message=script_getstr(st,2);
  9920. if(sd)
  9921. clif_disp_onlyself(sd,message,(int)strlen(message));
  9922. return 0;
  9923. }
  9924. /*==========================================
  9925. * All The Players Full Recovery
  9926. * (HP/SP full restore and resurrect if need)
  9927. *------------------------------------------*/
  9928. BUILDIN_FUNC(recovery)
  9929. {
  9930. TBL_PC *sd, **all_sd;
  9931. int i = 0, users;
  9932. all_sd = map_getallusers(&users);
  9933. for (i = 0; i < users; i++)
  9934. {
  9935. sd = all_sd[i];
  9936. if(pc_isdead(sd))
  9937. status_revive(&sd->bl, 100, 100);
  9938. else
  9939. status_percent_heal(&sd->bl, 100, 100);
  9940. clif_displaymessage(sd->fd,"You have been recovered!");
  9941. }
  9942. return 0;
  9943. }
  9944. /*==========================================
  9945. * Get your pet info: getpetinfo(n)
  9946. * n -> 0:pet_id 1:pet_class 2:pet_name
  9947. * 3:friendly 4:hungry, 5: rename flag.
  9948. *------------------------------------------*/
  9949. BUILDIN_FUNC(getpetinfo)
  9950. {
  9951. TBL_PC *sd=script_rid2sd(st);
  9952. TBL_PET *pd;
  9953. int type=script_getnum(st,2);
  9954. if(!sd || !sd->pd) {
  9955. if (type == 2)
  9956. script_pushconststr(st,"null");
  9957. else
  9958. script_pushint(st,0);
  9959. return 0;
  9960. }
  9961. pd = sd->pd;
  9962. switch(type){
  9963. case 0: script_pushint(st,pd->pet.pet_id); break;
  9964. case 1: script_pushint(st,pd->pet.class_); break;
  9965. case 2: script_pushstr(st,aStrdup(pd->pet.name)); break;
  9966. case 3: script_pushint(st,pd->pet.intimate); break;
  9967. case 4: script_pushint(st,pd->pet.hungry); break;
  9968. case 5: script_pushint(st,pd->pet.rename_flag); break;
  9969. default:
  9970. script_pushint(st,0);
  9971. break;
  9972. }
  9973. return 0;
  9974. }
  9975. /*==========================================
  9976. * Get your homunculus info: gethominfo(n)
  9977. * n -> 0:hom_id 1:class 2:name
  9978. * 3:friendly 4:hungry, 5: rename flag.
  9979. * 6: level
  9980. *------------------------------------------*/
  9981. BUILDIN_FUNC(gethominfo)
  9982. {
  9983. TBL_PC *sd=script_rid2sd(st);
  9984. TBL_HOM *hd;
  9985. int type=script_getnum(st,2);
  9986. hd = sd?sd->hd:NULL;
  9987. if(!merc_is_hom_active(hd))
  9988. {
  9989. if (type == 2)
  9990. script_pushconststr(st,"null");
  9991. else
  9992. script_pushint(st,0);
  9993. return 0;
  9994. }
  9995. switch(type){
  9996. case 0: script_pushint(st,hd->homunculus.hom_id); break;
  9997. case 1: script_pushint(st,hd->homunculus.class_); break;
  9998. case 2: script_pushstr(st,aStrdup(hd->homunculus.name)); break;
  9999. case 3: script_pushint(st,hd->homunculus.intimacy); break;
  10000. case 4: script_pushint(st,hd->homunculus.hunger); break;
  10001. case 5: script_pushint(st,hd->homunculus.rename_flag); break;
  10002. case 6: script_pushint(st,hd->homunculus.level); break;
  10003. default:
  10004. script_pushint(st,0);
  10005. break;
  10006. }
  10007. return 0;
  10008. }
  10009. /*==========================================
  10010. * Shows wether your inventory(and equips) contain
  10011. selected card or not.
  10012. checkequipedcard(4001);
  10013. *------------------------------------------*/
  10014. BUILDIN_FUNC(checkequipedcard)
  10015. {
  10016. TBL_PC *sd=script_rid2sd(st);
  10017. int n,i,c=0;
  10018. c=script_getnum(st,2);
  10019. if(sd){
  10020. for(i=0;i<MAX_INVENTORY;i++){
  10021. if(sd->status.inventory[i].nameid > 0 && sd->status.inventory[i].amount){
  10022. for(n=0;n<MAX_SLOTS;n++){
  10023. if(sd->status.inventory[i].card[n]==c){
  10024. script_pushint(st,1);
  10025. return 0;
  10026. }
  10027. }
  10028. }
  10029. }
  10030. }
  10031. script_pushint(st,0);
  10032. return 0;
  10033. }
  10034. BUILDIN_FUNC(jump_zero)
  10035. {
  10036. int sel;
  10037. sel=script_getnum(st,2);
  10038. if(!sel) {
  10039. int pos;
  10040. if( !data_islabel(script_getdata(st,3)) ){
  10041. ShowError("script: jump_zero: not label !\n");
  10042. st->state=END;
  10043. return 1;
  10044. }
  10045. pos=script_getnum(st,3);
  10046. st->pos=pos;
  10047. st->state=GOTO;
  10048. // printf("script: jump_zero: jumpto : %d\n",pos);
  10049. } else {
  10050. // printf("script: jump_zero: fail\n");
  10051. }
  10052. return 0;
  10053. }
  10054. /*==========================================
  10055. * GetMapMobs
  10056. returns mob counts on a set map:
  10057. e.g. GetMapMobs("prontera")
  10058. use "this" - for player's map
  10059. *------------------------------------------*/
  10060. BUILDIN_FUNC(getmapmobs)
  10061. {
  10062. const char *str=NULL;
  10063. int m=-1,bx,by,i;
  10064. int count=0,c;
  10065. struct block_list *bl;
  10066. str=script_getstr(st,2);
  10067. if(strcmp(str,"this")==0){
  10068. TBL_PC *sd=script_rid2sd(st);
  10069. if(sd)
  10070. m=sd->bl.m;
  10071. else{
  10072. script_pushint(st,-1);
  10073. return 0;
  10074. }
  10075. }else
  10076. m=map_mapname2mapid(str);
  10077. if(m < 0){
  10078. script_pushint(st,-1);
  10079. return 0;
  10080. }
  10081. for(by=0;by<=(map[m].ys-1)/BLOCK_SIZE;by++){
  10082. for(bx=0;bx<=(map[m].xs-1)/BLOCK_SIZE;bx++){
  10083. bl = map[m].block_mob[bx+by*map[m].bxs];
  10084. c = map[m].block_mob_count[bx+by*map[m].bxs];
  10085. for(i=0;i<c && bl;i++,bl=bl->next){
  10086. if(bl->x>=0 && bl->x<=map[m].xs-1 && bl->y>=0 && bl->y<=map[m].ys-1)
  10087. count++;
  10088. }
  10089. }
  10090. }
  10091. script_pushint(st,count);
  10092. return 0;
  10093. }
  10094. /*==========================================
  10095. * movenpc [MouseJstr]
  10096. *------------------------------------------*/
  10097. BUILDIN_FUNC(movenpc)
  10098. {
  10099. TBL_NPC *nd = NULL;
  10100. const char *npc;
  10101. int x,y;
  10102. npc = script_getstr(st,2);
  10103. x = script_getnum(st,3);
  10104. y = script_getnum(st,4);
  10105. if ((nd = npc_name2id(npc)) == NULL)
  10106. return -1;
  10107. npc_movenpc(nd, x, y);
  10108. return 0;
  10109. }
  10110. /*==========================================
  10111. * message [MouseJstr]
  10112. *------------------------------------------*/
  10113. BUILDIN_FUNC(message)
  10114. {
  10115. TBL_PC *sd;
  10116. const char *msg,*player;
  10117. TBL_PC *pl_sd = NULL;
  10118. sd = script_rid2sd(st);
  10119. player = script_getstr(st,2);
  10120. msg = script_getstr(st,3);
  10121. if((pl_sd=map_nick2sd((char *) player)) == NULL)
  10122. return 0;
  10123. clif_displaymessage(pl_sd->fd, msg);
  10124. return 0;
  10125. }
  10126. /*==========================================
  10127. * npctalk (sends message to surrounding area)
  10128. *------------------------------------------*/
  10129. BUILDIN_FUNC(npctalk)
  10130. {
  10131. const char* str;
  10132. char message[255];
  10133. struct npc_data* nd = (struct npc_data *)map_id2bl(st->oid);
  10134. str = script_getstr(st,2);
  10135. if(nd) {
  10136. memcpy(message, nd->name, NAME_LENGTH);
  10137. strtok(message, "#"); // discard extra name identifier if present
  10138. strcat(message, " : ");
  10139. strncat(message, str, 254); //Prevent overflow possibility. [Skotlex]
  10140. clif_message(&(nd->bl), message);
  10141. }
  10142. return 0;
  10143. }
  10144. // change npc walkspeed [Valaris]
  10145. BUILDIN_FUNC(npcspeed)
  10146. {
  10147. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10148. int x=0;
  10149. x=script_getnum(st,2);
  10150. if(nd) {
  10151. nd->speed=x;
  10152. }
  10153. return 0;
  10154. }
  10155. // make an npc walk to a position [Valaris]
  10156. BUILDIN_FUNC(npcwalkto)
  10157. {
  10158. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10159. int x=0,y=0;
  10160. x=script_getnum(st,2);
  10161. y=script_getnum(st,3);
  10162. if(nd) {
  10163. unit_walktoxy(&nd->bl,x,y,0);
  10164. }
  10165. return 0;
  10166. }
  10167. // stop an npc's movement [Valaris]
  10168. BUILDIN_FUNC(npcstop)
  10169. {
  10170. struct npc_data *nd=(struct npc_data *)map_id2bl(st->oid);
  10171. if(nd) {
  10172. unit_stop_walking(&nd->bl,1);
  10173. }
  10174. return 0;
  10175. }
  10176. /*==========================================
  10177. * getlook char info. getlook(arg)
  10178. *------------------------------------------*/
  10179. BUILDIN_FUNC(getlook)
  10180. {
  10181. int type,val;
  10182. TBL_PC *sd;
  10183. sd=script_rid2sd(st);
  10184. type=script_getnum(st,2);
  10185. val=-1;
  10186. switch(type) {
  10187. case LOOK_HAIR: val=sd->status.hair; break; //1
  10188. case LOOK_WEAPON: val=sd->status.weapon; break; //2
  10189. case LOOK_HEAD_BOTTOM: val=sd->status.head_bottom; break; //3
  10190. case LOOK_HEAD_TOP: val=sd->status.head_top; break; //4
  10191. case LOOK_HEAD_MID: val=sd->status.head_mid; break; //5
  10192. case LOOK_HAIR_COLOR: val=sd->status.hair_color; break; //6
  10193. case LOOK_CLOTHES_COLOR: val=sd->status.clothes_color; break; //7
  10194. case LOOK_SHIELD: val=sd->status.shield; break; //8
  10195. case LOOK_SHOES: break; //9
  10196. }
  10197. script_pushint(st,val);
  10198. return 0;
  10199. }
  10200. /*==========================================
  10201. * get char save point. argument: 0- map name, 1- x, 2- y
  10202. *------------------------------------------*/
  10203. BUILDIN_FUNC(getsavepoint)
  10204. {
  10205. TBL_PC* sd;
  10206. int type;
  10207. sd = script_rid2sd(st);
  10208. if (sd == NULL) {
  10209. script_pushint(st,0);
  10210. return 0;
  10211. }
  10212. type = script_getnum(st,2);
  10213. switch(type) {
  10214. case 0: script_pushstr(st,aStrdup(mapindex_id2name(sd->status.save_point.map))); break;
  10215. case 1: script_pushint(st,sd->status.save_point.x); break;
  10216. case 2: script_pushint(st,sd->status.save_point.y); break;
  10217. default:
  10218. script_pushint(st,0);
  10219. break;
  10220. }
  10221. return 0;
  10222. }
  10223. /*==========================================
  10224. * Get position for char/npc/pet/mob objects. Added by Lorky
  10225. *
  10226. * int getMapXY(MapName$,MaxX,MapY,type,[CharName$]);
  10227. * where type:
  10228. * MapName$ - String variable for output map name
  10229. * MapX - Integer variable for output coord X
  10230. * MapY - Integer variable for output coord Y
  10231. * type - type of object
  10232. * 0 - Character coord
  10233. * 1 - NPC coord
  10234. * 2 - Pet coord
  10235. * 3 - Mob coord (not released)
  10236. * 4 - Homun coord
  10237. * CharName$ - Name object. If miss or "this" the current object
  10238. *
  10239. * Return:
  10240. * 0 - success
  10241. * -1 - some error, MapName$,MapX,MapY contains unknown value.
  10242. *------------------------------------------*/
  10243. BUILDIN_FUNC(getmapxy)
  10244. {
  10245. struct block_list *bl = NULL;
  10246. TBL_PC *sd=NULL;
  10247. int num;
  10248. char *name;
  10249. char prefix;
  10250. int x,y,type;
  10251. char mapname[MAP_NAME_LENGTH];
  10252. memset(mapname, 0, sizeof(mapname));
  10253. if( !data_isreference(script_getdata(st,2)) ){
  10254. ShowWarning("script: buildin_getmapxy: not mapname variable\n");
  10255. script_pushint(st,-1);
  10256. return 1;
  10257. }
  10258. if( !data_isreference(script_getdata(st,3)) ){
  10259. ShowWarning("script: buildin_getmapxy: not mapx variable\n");
  10260. script_pushint(st,-1);
  10261. return 1;
  10262. }
  10263. if( !data_isreference(script_getdata(st,4)) ){
  10264. ShowWarning("script: buildin_getmapxy: not mapy variable\n");
  10265. script_pushint(st,-1);
  10266. return 1;
  10267. }
  10268. // Possible needly check function parameters on C_STR,C_INT,C_INT
  10269. type=script_getnum(st,5);
  10270. switch (type){
  10271. case 0: //Get Character Position
  10272. if( script_hasdata(st,6) )
  10273. sd=map_nick2sd(script_getstr(st,6));
  10274. else
  10275. sd=script_rid2sd(st);
  10276. if (sd)
  10277. bl = &sd->bl;
  10278. break;
  10279. case 1: //Get NPC Position
  10280. if( script_hasdata(st,6) )
  10281. {
  10282. struct npc_data *nd;
  10283. nd=npc_name2id(script_getstr(st,6));
  10284. if (nd)
  10285. bl = &nd->bl;
  10286. } else //In case the origin is not an npc?
  10287. bl=map_id2bl(st->oid);
  10288. break;
  10289. case 2: //Get Pet Position
  10290. if(script_hasdata(st,6))
  10291. sd=map_nick2sd(script_getstr(st,6));
  10292. else
  10293. sd=script_rid2sd(st);
  10294. if (sd && sd->pd)
  10295. bl = &sd->pd->bl;
  10296. break;
  10297. case 3: //Get Mob Position
  10298. break; //Not supported?
  10299. case 4: //Get Homun Position
  10300. if(script_hasdata(st,6))
  10301. sd=map_nick2sd(script_getstr(st,6));
  10302. else
  10303. sd=script_rid2sd(st);
  10304. if (sd && sd->hd)
  10305. bl = &sd->hd->bl;
  10306. break;
  10307. }
  10308. if (!bl) { //No object found.
  10309. script_pushint(st,-1);
  10310. return 0;
  10311. }
  10312. x= bl->x;
  10313. y= bl->y;
  10314. memcpy(mapname, map[bl->m].name, MAP_NAME_LENGTH);
  10315. //Set MapName$
  10316. num=st->stack->stack_data[st->start+2].u.num;
  10317. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10318. prefix=*name;
  10319. if(not_server_variable(prefix))
  10320. sd=script_rid2sd(st);
  10321. else
  10322. sd=NULL;
  10323. set_reg(st,sd,num,name,(void*)mapname,script_getref(st,2));
  10324. //Set MapX
  10325. num=st->stack->stack_data[st->start+3].u.num;
  10326. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10327. prefix=*name;
  10328. if(not_server_variable(prefix))
  10329. sd=script_rid2sd(st);
  10330. else
  10331. sd=NULL;
  10332. set_reg(st,sd,num,name,(void*)x,script_getref(st,3));
  10333. //Set MapY
  10334. num=st->stack->stack_data[st->start+4].u.num;
  10335. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10336. prefix=*name;
  10337. if(not_server_variable(prefix))
  10338. sd=script_rid2sd(st);
  10339. else
  10340. sd=NULL;
  10341. set_reg(st,sd,num,name,(void*)y,script_getref(st,4));
  10342. //Return Success value
  10343. script_pushint(st,0);
  10344. return 0;
  10345. }
  10346. /*==========================================
  10347. * Allows player to write NPC logs (i.e. Bank NPC, etc) [Lupus]
  10348. *------------------------------------------*/
  10349. BUILDIN_FUNC(logmes)
  10350. {
  10351. const char *str;
  10352. if (log_config.npc <= 0 ) return 0;
  10353. str = script_getstr(st,2);
  10354. log_npc(script_rid2sd(st),str);
  10355. return 0;
  10356. }
  10357. BUILDIN_FUNC(summon)
  10358. {
  10359. int _class, timeout=0;
  10360. const char *str,*event="";
  10361. TBL_PC *sd;
  10362. struct mob_data *md;
  10363. int tick = gettick();
  10364. sd=script_rid2sd(st);
  10365. if (!sd) return 0;
  10366. str =script_getstr(st,2);
  10367. _class=script_getnum(st,3);
  10368. if( script_hasdata(st,4) )
  10369. timeout=script_getnum(st,4);
  10370. if( script_hasdata(st,5) ){
  10371. event=script_getstr(st,5);
  10372. check_event(st, event);
  10373. }
  10374. clif_skill_poseffect(&sd->bl,AM_CALLHOMUN,1,sd->bl.x,sd->bl.y,tick);
  10375. md = mob_once_spawn_sub(&sd->bl, sd->bl.m, sd->bl.x, sd->bl.y, str, _class, event);
  10376. if (md) {
  10377. md->master_id=sd->bl.id;
  10378. md->special_state.ai=1;
  10379. md->deletetimer = add_timer(tick+(timeout>0?timeout*1000:60000),mob_timer_delete,md->bl.id,0);
  10380. mob_spawn (md); //Now it is ready for spawning.
  10381. clif_misceffect2(&md->bl,344);
  10382. sc_start4(&md->bl, SC_MODECHANGE, 100, 1, 0, MD_AGGRESSIVE, 0, 60000);
  10383. }
  10384. return 0;
  10385. }
  10386. /*==========================================
  10387. * Checks whether it is daytime/nighttime
  10388. *------------------------------------------*/
  10389. BUILDIN_FUNC(isnight)
  10390. {
  10391. script_pushint(st,(night_flag == 1));
  10392. return 0;
  10393. }
  10394. BUILDIN_FUNC(isday)
  10395. {
  10396. script_pushint(st,(night_flag == 0));
  10397. return 0;
  10398. }
  10399. /*================================================
  10400. * Check whether another item/card has been
  10401. * equipped - used for 2/15's cards patch [celest]
  10402. *------------------------------------------------*/
  10403. // leave this here, just in case
  10404. #if 0
  10405. BUILDIN_FUNC(isequipped)
  10406. {
  10407. TBL_PC *sd;
  10408. int i, j, k, id = 1;
  10409. int ret = -1;
  10410. sd = script_rid2sd(st);
  10411. for (i=0; id!=0; i++) {
  10412. int flag = 0;
  10413. FETCH (i+2, id) else id = 0;
  10414. if (id <= 0)
  10415. continue;
  10416. for (j=0; j<EQI_MAX; j++) {
  10417. int index;
  10418. index = sd->equip_index[j];
  10419. if(index < 0) continue;
  10420. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10421. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10422. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10423. if(!sd->inventory_data[index])
  10424. continue;
  10425. if(itemdb_type(id) != IT_CARD) { //Non card
  10426. if (sd->inventory_data[index]->nameid != id)
  10427. continue;
  10428. flag = 1;
  10429. break;
  10430. } else { //Card
  10431. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10432. continue;
  10433. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10434. if (sd->status.inventory[index].card[k] == id)
  10435. {
  10436. flag = 1;
  10437. break;
  10438. }
  10439. }
  10440. }
  10441. if (flag) break;
  10442. }
  10443. if (ret == -1)
  10444. ret = flag;
  10445. else
  10446. ret &= flag;
  10447. if (!ret) break;
  10448. }
  10449. script_pushint(st,ret);
  10450. return 0;
  10451. }
  10452. #endif
  10453. /*================================================
  10454. * Check how many items/cards in the list are
  10455. * equipped - used for 2/15's cards patch [celest]
  10456. *------------------------------------------------*/
  10457. BUILDIN_FUNC(isequippedcnt)
  10458. {
  10459. TBL_PC *sd;
  10460. int i, j, k, id = 1;
  10461. int ret = 0;
  10462. sd = script_rid2sd(st);
  10463. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10464. script_pushint(st,0);
  10465. return 0;
  10466. }
  10467. for (i=0; id!=0; i++) {
  10468. FETCH (i+2, id) else id = 0;
  10469. if (id <= 0)
  10470. continue;
  10471. for (j=0; j<EQI_MAX; j++) {
  10472. int index;
  10473. index = sd->equip_index[j];
  10474. if(index < 0) continue;
  10475. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10476. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10477. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10478. if(!sd->inventory_data[index])
  10479. continue;
  10480. if (itemdb_type(id) != IT_CARD) { //No card. Count amount in inventory.
  10481. if (sd->inventory_data[index]->nameid == id)
  10482. ret+= sd->status.inventory[index].amount;
  10483. } else { //Count cards.
  10484. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10485. continue; //No cards
  10486. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10487. if (sd->status.inventory[index].card[k] == id)
  10488. ret++; //[Lupus]
  10489. }
  10490. }
  10491. }
  10492. }
  10493. script_pushint(st,ret);
  10494. return 0;
  10495. }
  10496. /*================================================
  10497. * Check whether another card has been
  10498. * equipped - used for 2/15's cards patch [celest]
  10499. * -- Items checked cannot be reused in another
  10500. * card set to prevent exploits
  10501. *------------------------------------------------*/
  10502. BUILDIN_FUNC(isequipped)
  10503. {
  10504. TBL_PC *sd;
  10505. int i, j, k, id = 1;
  10506. int index, flag;
  10507. int ret = -1;
  10508. //Original hash to reverse it when full check fails.
  10509. unsigned int setitem_hash = 0, setitem_hash2 = 0;
  10510. sd = script_rid2sd(st);
  10511. if (!sd) { //If the player is not attached it is a script error anyway... but better prevent the map server from crashing...
  10512. script_pushint(st,0);
  10513. return 0;
  10514. }
  10515. setitem_hash = sd->setitem_hash;
  10516. setitem_hash2 = sd->setitem_hash2;
  10517. for (i=0; id!=0; i++)
  10518. {
  10519. FETCH (i+2, id) else id = 0;
  10520. if (id <= 0)
  10521. continue;
  10522. flag = 0;
  10523. for (j=0; j<EQI_MAX; j++)
  10524. {
  10525. index = sd->equip_index[j];
  10526. if(index < 0) continue;
  10527. if(j == EQI_HAND_R && sd->equip_index[EQI_HAND_L] == index) continue;
  10528. if(j == EQI_HEAD_MID && sd->equip_index[EQI_HEAD_LOW] == index) continue;
  10529. if(j == EQI_HEAD_TOP && (sd->equip_index[EQI_HEAD_MID] == index || sd->equip_index[EQI_HEAD_LOW] == index)) continue;
  10530. if(!sd->inventory_data[index])
  10531. continue;
  10532. if (itemdb_type(id) != IT_CARD) {
  10533. if (sd->inventory_data[index]->nameid != id)
  10534. continue;
  10535. flag = 1;
  10536. break;
  10537. } else { //Cards
  10538. if (sd->inventory_data[index]->slot == 0 ||
  10539. itemdb_isspecial(sd->status.inventory[index].card[0]))
  10540. continue;
  10541. for (k = 0; k < sd->inventory_data[index]->slot; k++)
  10542. { //New hash system which should support up to 4 slots on any equipment. [Skotlex]
  10543. unsigned int hash = 0;
  10544. if (sd->status.inventory[index].card[k] != id)
  10545. continue;
  10546. hash = 1<<((j<5?j:j-5)*4 + k);
  10547. // check if card is already used by another set
  10548. if ((j<5?sd->setitem_hash:sd->setitem_hash2) & hash)
  10549. continue;
  10550. // We have found a match
  10551. flag = 1;
  10552. // Set hash so this card cannot be used by another
  10553. if (j<5)
  10554. sd->setitem_hash |= hash;
  10555. else
  10556. sd->setitem_hash2 |= hash;
  10557. break;
  10558. }
  10559. }
  10560. if (flag) break; //Card found
  10561. }
  10562. if (ret == -1)
  10563. ret = flag;
  10564. else
  10565. ret &= flag;
  10566. if (!ret) break;
  10567. }
  10568. if (!ret)
  10569. { //When check fails, restore original hash values. [Skotlex]
  10570. sd->setitem_hash = setitem_hash;
  10571. sd->setitem_hash2 = setitem_hash2;
  10572. }
  10573. script_pushint(st,ret);
  10574. return 0;
  10575. }
  10576. /*================================================
  10577. * Check how many given inserted cards in the CURRENT
  10578. * weapon - used for 2/15's cards patch [Lupus]
  10579. *------------------------------------------------*/
  10580. BUILDIN_FUNC(cardscnt)
  10581. {
  10582. TBL_PC *sd;
  10583. int i, k, id = 1;
  10584. int ret = 0;
  10585. int index;
  10586. sd = script_rid2sd(st);
  10587. for (i=0; id!=0; i++) {
  10588. FETCH (i+2, id) else id = 0;
  10589. if (id <= 0)
  10590. continue;
  10591. index = current_equip_item_index; //we get CURRENT WEAPON inventory index from status.c [Lupus]
  10592. if(index < 0) continue;
  10593. if(!sd->inventory_data[index])
  10594. continue;
  10595. if(itemdb_type(id) != IT_CARD) {
  10596. if (sd->inventory_data[index]->nameid == id)
  10597. ret+= sd->status.inventory[index].amount;
  10598. } else {
  10599. if (itemdb_isspecial(sd->status.inventory[index].card[0]))
  10600. continue;
  10601. for(k=0; k<sd->inventory_data[index]->slot; k++) {
  10602. if (sd->status.inventory[index].card[k] == id)
  10603. ret++;
  10604. }
  10605. }
  10606. }
  10607. script_pushint(st,ret);
  10608. // script_pushint(st,current_equip_item_index);
  10609. return 0;
  10610. }
  10611. /*=======================================================
  10612. * Returns the refined number of the current item, or an
  10613. * item with inventory index specified
  10614. *-------------------------------------------------------*/
  10615. BUILDIN_FUNC(getrefine)
  10616. {
  10617. TBL_PC *sd;
  10618. if ((sd = script_rid2sd(st))!= NULL)
  10619. script_pushint(st,sd->status.inventory[current_equip_item_index].refine);
  10620. else
  10621. script_pushint(st,0);
  10622. return 0;
  10623. }
  10624. /*=======================================================
  10625. * Allows 2 Parents to adopt a character as a Baby
  10626. *-------------------------------------------------------*/
  10627. BUILDIN_FUNC(adopt)
  10628. {
  10629. int ret;
  10630. const char *parent1 = script_getstr(st,2);
  10631. const char *parent2 = script_getstr(st,3);
  10632. const char *child = script_getstr(st,4);
  10633. TBL_PC *p1_sd = map_nick2sd(parent1);
  10634. TBL_PC *p2_sd = map_nick2sd(parent2);
  10635. TBL_PC *c_sd = map_nick2sd(child);
  10636. if (!p1_sd || !p2_sd || !c_sd ||
  10637. p1_sd->status.base_level < 70 ||
  10638. p2_sd->status.base_level < 70)
  10639. return 0;
  10640. ret = pc_adoption(p1_sd, p2_sd, c_sd);
  10641. script_pushint(st,ret);
  10642. return 0;
  10643. }
  10644. /*=======================================================
  10645. * Day/Night controls
  10646. *-------------------------------------------------------*/
  10647. BUILDIN_FUNC(night)
  10648. {
  10649. if (night_flag != 1) map_night_timer(night_timer_tid, 0, 0, 1);
  10650. return 0;
  10651. }
  10652. BUILDIN_FUNC(day)
  10653. {
  10654. if (night_flag != 0) map_day_timer(day_timer_tid, 0, 0, 1);
  10655. return 0;
  10656. }
  10657. //=======================================================
  10658. // Unequip [Spectre]
  10659. //-------------------------------------------------------
  10660. BUILDIN_FUNC(unequip)
  10661. {
  10662. int i;
  10663. size_t num;
  10664. TBL_PC *sd;
  10665. num = script_getnum(st,2) - 1;
  10666. sd=script_rid2sd(st);
  10667. if(sd!=NULL && num<10)
  10668. {
  10669. i=pc_checkequip(sd,equip[num]);
  10670. pc_unequipitem(sd,i,2);
  10671. return 0;
  10672. }
  10673. return 0;
  10674. }
  10675. BUILDIN_FUNC(equip)
  10676. {
  10677. int nameid=0,i;
  10678. TBL_PC *sd;
  10679. struct item_data *item_data;
  10680. sd = script_rid2sd(st);
  10681. nameid=script_getnum(st,2);
  10682. if((item_data = itemdb_exists(nameid)) == NULL)
  10683. {
  10684. if(battle_config.error_log)
  10685. ShowError("wrong item ID : equipitem(%i)\n",nameid);
  10686. return 1;
  10687. }
  10688. for(i=0;i<MAX_INVENTORY && sd->status.inventory[i].nameid!=nameid;i++);
  10689. if(i==MAX_INVENTORY) return 0;
  10690. pc_equipitem(sd,i,item_data->equip);
  10691. return 0;
  10692. }
  10693. BUILDIN_FUNC(autoequip)
  10694. {
  10695. int nameid, flag;
  10696. struct item_data *item_data;
  10697. nameid=script_getnum(st,2);
  10698. flag=script_getnum(st,3);
  10699. if(nameid>=500 && (item_data = itemdb_search(nameid)) != NULL){
  10700. item_data->flag.autoequip = flag>0?1:0;
  10701. }
  10702. return 0;
  10703. }
  10704. BUILDIN_FUNC(setbattleflag)
  10705. {
  10706. const char *flag, *value;
  10707. flag = script_getstr(st,2);
  10708. value = script_getstr(st,3);
  10709. if (battle_set_value(flag, value) == 0)
  10710. ShowWarning("buildin_setbattleflag: unknown battle_config flag '%s'\n",flag);
  10711. else
  10712. ShowInfo("buildin_setbattleflag: battle_config flag '%s' is now set to '%s'.\n",flag,value);
  10713. return 0;
  10714. }
  10715. BUILDIN_FUNC(getbattleflag)
  10716. {
  10717. const char *flag;
  10718. flag = script_getstr(st,2);
  10719. script_pushint(st,battle_get_value(flag));
  10720. return 0;
  10721. }
  10722. //=======================================================
  10723. // strlen [Valaris]
  10724. //-------------------------------------------------------
  10725. BUILDIN_FUNC(getstrlen)
  10726. {
  10727. const char *str = script_getstr(st,2);
  10728. int len = (str) ? (int)strlen(str) : 0;
  10729. script_pushint(st,len);
  10730. return 0;
  10731. }
  10732. //=======================================================
  10733. // isalpha [Valaris]
  10734. //-------------------------------------------------------
  10735. BUILDIN_FUNC(charisalpha)
  10736. {
  10737. const char *str=script_getstr(st,2);
  10738. int pos=script_getnum(st,3);
  10739. int val = ( str && pos>0 && (unsigned int)pos<strlen(str) ) ? ISALPHA( str[pos] ) : 0;
  10740. script_pushint(st,val);
  10741. return 0;
  10742. }
  10743. // [Lance]
  10744. BUILDIN_FUNC(fakenpcname)
  10745. {
  10746. const char *name;
  10747. const char *newname;
  10748. int look;
  10749. name = script_getstr(st,2);
  10750. newname = script_getstr(st,3);
  10751. look = script_getnum(st,4);
  10752. if(look > 32767 || look < -32768) {
  10753. ShowError("buildin_fakenpcname: Invalid look value %d\n",look);
  10754. return 1; // Safety measure to prevent runtime errors
  10755. }
  10756. npc_changename(name,newname,(short)look);
  10757. return 0;
  10758. }
  10759. BUILDIN_FUNC(atoi)
  10760. {
  10761. const char *value;
  10762. value = script_getstr(st,2);
  10763. script_pushint(st,atoi(value));
  10764. return 0;
  10765. }
  10766. // case-insensitive substring search [lordalfa]
  10767. BUILDIN_FUNC(compare)
  10768. {
  10769. const char *message;
  10770. const char *cmpstring;
  10771. message = script_getstr(st,2);
  10772. cmpstring = script_getstr(st,3);
  10773. script_pushint(st,(stristr(message,cmpstring) != NULL));
  10774. return 0;
  10775. }
  10776. // [zBuffer] List of mathematics commands --->
  10777. BUILDIN_FUNC(sqrt)
  10778. {
  10779. double i, a;
  10780. i = script_getnum(st,2);
  10781. a = sqrt(i);
  10782. script_pushint(st,(int)a);
  10783. return 0;
  10784. }
  10785. BUILDIN_FUNC(pow)
  10786. {
  10787. double i, a, b;
  10788. a = script_getnum(st,2);
  10789. b = script_getnum(st,3);
  10790. i = pow(a,b);
  10791. script_pushint(st,(int)i);
  10792. return 0;
  10793. }
  10794. BUILDIN_FUNC(distance)
  10795. {
  10796. int x0, y0, x1, y1;
  10797. x0 = script_getnum(st,2);
  10798. y0 = script_getnum(st,3);
  10799. x1 = script_getnum(st,4);
  10800. y1 = script_getnum(st,5);
  10801. script_pushint(st,distance(x0-x1, y0-y1));
  10802. return 0;
  10803. }
  10804. BUILDIN_FUNC(checkcell)
  10805. {
  10806. int m;
  10807. const char *map = script_getstr(st, 2);
  10808. m = mapindex_name2id(map);
  10809. if(m){
  10810. script_pushint(st,map_getcell(m, script_getnum(st,3), script_getnum(st,4),script_getnum(st,5)));
  10811. } else {
  10812. script_pushint(st,0);
  10813. }
  10814. return 0;
  10815. }
  10816. // <--- [zBuffer] List of mathematics commands
  10817. // [zBuffer] List of dynamic var commands --->
  10818. void setd_sub(struct script_state *st, TBL_PC *sd, char *varname, int elem, void *value, struct linkdb_node **ref)
  10819. {
  10820. set_reg(st, sd, add_str(varname)+(elem<<24), varname, value, ref);
  10821. return;
  10822. }
  10823. BUILDIN_FUNC(setd)
  10824. {
  10825. TBL_PC *sd=NULL;
  10826. char varname[100];
  10827. const char *value, *buffer;
  10828. int elem;
  10829. buffer = script_getstr(st, 2);
  10830. value = script_getstr(st, 3);
  10831. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10832. elem = 0;
  10833. if(st->rid)
  10834. sd = script_rid2sd(st);
  10835. if(varname[strlen(varname)-1] != '$') {
  10836. setd_sub(st,sd, varname, elem, (void *)atoi(value),NULL);
  10837. } else {
  10838. setd_sub(st,sd, varname, elem, (void *)value,NULL);
  10839. }
  10840. return 0;
  10841. }
  10842. BUILDIN_FUNC(query_sql)
  10843. {
  10844. #ifndef TXT_ONLY
  10845. char *name = NULL;
  10846. const char *query;
  10847. int num, i = 0,j, nb_rows;
  10848. struct { char * dst_var_name; char type; } row[32];
  10849. TBL_PC *sd = (st->rid)? script_rid2sd(st) : NULL;
  10850. query = script_getstr(st,2);
  10851. strcpy(tmp_sql, query);
  10852. if(mysql_query(&mmysql_handle,tmp_sql)){
  10853. ShowSQL("DB error - %s\n",mysql_error(&mmysql_handle));
  10854. ShowDebug("at %s:%d - %s\n", __FILE__,__LINE__,tmp_sql);
  10855. script_pushint(st,0);
  10856. return 1;
  10857. }
  10858. // If some data was returned
  10859. if((sql_res = mysql_store_result(&mmysql_handle))){
  10860. // Count the number of rows to store
  10861. nb_rows = mysql_num_fields(sql_res);
  10862. // Can't store more row than variable
  10863. if (nb_rows > st->end - (st->start+3))
  10864. nb_rows = st->end - (st->start+3);
  10865. if (!nb_rows)
  10866. {
  10867. script_pushint(st,0);
  10868. return 0; // Nothing to store
  10869. }
  10870. if (nb_rows > 32)
  10871. {
  10872. ShowWarning("buildin_query_sql: too many rows!\n");
  10873. script_pushint(st,0);
  10874. return 1;
  10875. }
  10876. memset(row, 0, sizeof(row));
  10877. // Verify argument types
  10878. for(j=0; j < nb_rows; j++)
  10879. {
  10880. if(!data_isreference(script_getdata(st, 3+j))){
  10881. ShowWarning("buildin_query_sql: Parameter %d is not a variable!\n", j);
  10882. script_pushint(st,0);
  10883. return 0;
  10884. } else {
  10885. // Store type of variable (string = 0/int = 1)
  10886. num=st->stack->stack_data[st->start+3+j].u.num;
  10887. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  10888. if(name[strlen(name)-1] != '$') {
  10889. row[j].type = 1;
  10890. }
  10891. row[j].dst_var_name = name;
  10892. }
  10893. }
  10894. // Store data
  10895. while(i<128 && (sql_row = mysql_fetch_row(sql_res))){
  10896. for(j=0; j < nb_rows; j++)
  10897. {
  10898. if (row[j].type == 1)
  10899. setd_sub(st,sd, row[j].dst_var_name, i, (void *)atoi(sql_row[j]),script_getref(st,3+j));
  10900. else
  10901. setd_sub(st,sd, row[j].dst_var_name, i, (void *)sql_row[j],script_getref(st,3+j));
  10902. }
  10903. i++;
  10904. }
  10905. // Free data
  10906. mysql_free_result(sql_res);
  10907. }
  10908. script_pushint(st,i);
  10909. #else
  10910. //for TXT version, we always return -1
  10911. script_pushint(st,-1);
  10912. #endif
  10913. return 0;
  10914. }
  10915. //Allows escaping of a given string.
  10916. BUILDIN_FUNC(escape_sql)
  10917. {
  10918. const char *query;
  10919. char *t_query;
  10920. query = script_getstr(st,2);
  10921. t_query = aMallocA((strlen(query)*2+1)*sizeof(char));
  10922. jstrescapecpy(t_query,query);
  10923. script_pushstr(st,t_query);
  10924. return 0;
  10925. }
  10926. BUILDIN_FUNC(getd)
  10927. {
  10928. char varname[100];
  10929. const char *buffer;
  10930. //struct script_data dat;
  10931. int elem;
  10932. buffer = script_getstr(st, 2);
  10933. if(sscanf(buffer, "%[^[][%d]", varname, &elem) < 2)
  10934. elem = 0;
  10935. // Push the 'pointer' so it's more flexible [Lance]
  10936. push_val(st->stack,C_NAME,
  10937. (elem<<24) | add_str(varname));
  10938. return 0;
  10939. }
  10940. // <--- [zBuffer] List of dynamic var commands
  10941. // Pet stat [Lance]
  10942. BUILDIN_FUNC(petstat)
  10943. {
  10944. TBL_PC *sd = NULL;
  10945. struct pet_data *pd;
  10946. int flag = script_getnum(st,2);
  10947. sd = script_rid2sd(st);
  10948. if(!sd || !sd->status.pet_id || !sd->pd){
  10949. if(flag == 2)
  10950. script_pushconststr(st, "");
  10951. else
  10952. script_pushint(st,0);
  10953. return 0;
  10954. }
  10955. pd = sd->pd;
  10956. switch(flag){
  10957. case 1: script_pushint(st,(int)pd->pet.class_); break;
  10958. case 2: script_pushstr(st, aStrdup(pd->pet.name)); break;
  10959. case 3: script_pushint(st,(int)pd->pet.level); break;
  10960. case 4: script_pushint(st,(int)pd->pet.hungry); break;
  10961. case 5: script_pushint(st,(int)pd->pet.intimate); break;
  10962. default:
  10963. script_pushint(st,0);
  10964. break;
  10965. }
  10966. return 0;
  10967. }
  10968. BUILDIN_FUNC(callshop)
  10969. {
  10970. TBL_PC *sd = NULL;
  10971. struct npc_data *nd;
  10972. const char *shopname;
  10973. int flag = 0;
  10974. sd = script_rid2sd(st);
  10975. if (!sd) {
  10976. script_pushint(st,0);
  10977. return 0;
  10978. }
  10979. shopname = script_getstr(st, 2);
  10980. if( script_hasdata(st,3) )
  10981. flag = script_getnum(st,3);
  10982. nd = npc_name2id(shopname);
  10983. if (!nd || nd->bl.type!=BL_NPC || nd->bl.subtype!=SHOP) {
  10984. ShowError("buildin_callshop: Shop [%s] not found (or NPC is not shop type)", shopname);
  10985. script_pushint(st,0);
  10986. return 1;
  10987. }
  10988. switch (flag) {
  10989. case 1: //Buy window
  10990. npc_buysellsel(sd,nd->bl.id,0);
  10991. break;
  10992. case 2: //Sell window
  10993. npc_buysellsel(sd,nd->bl.id,1);
  10994. break;
  10995. default: //Show menu
  10996. clif_npcbuysell(sd,nd->bl.id);
  10997. break;
  10998. }
  10999. sd->npc_shopid = nd->bl.id;
  11000. script_pushint(st,1);
  11001. return 0;
  11002. }
  11003. #ifndef MAX_SHOPITEM
  11004. #define MAX_SHOPITEM 100
  11005. #endif
  11006. BUILDIN_FUNC(npcshopitem)
  11007. {
  11008. struct npc_data *nd= NULL;
  11009. int n = 0;
  11010. int i = 3;
  11011. int amount;
  11012. const char* npcname = script_getstr(st, 2);
  11013. nd = npc_name2id(npcname);
  11014. if(!nd || nd->bl.subtype!=SHOP)
  11015. { //Not found.
  11016. script_pushint(st,0);
  11017. return 0;
  11018. }
  11019. // st->end - 2 = nameid + value # ... / 2 = number of items ... + 1 to end it
  11020. amount = ((st->end-2)/2)+1;
  11021. //Reinsert as realloc could change the pointer address.
  11022. map_deliddb(&nd->bl);
  11023. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  11024. sizeof(nd->u.shop_item[0]) * amount);
  11025. map_addiddb(&nd->bl);
  11026. // Reset sell list.
  11027. memset(nd->u.shop_item, 0, sizeof(nd->u.shop_item[0]) * amount);
  11028. n = 0;
  11029. while (script_hasdata(st,i)) {
  11030. nd->u.shop_item[n].nameid = script_getnum(st,i);
  11031. i++;
  11032. nd->u.shop_item[n].value = script_getnum(st,i);
  11033. i++;
  11034. n++;
  11035. }
  11036. return 0;
  11037. }
  11038. BUILDIN_FUNC(npcshopadditem)
  11039. {
  11040. struct npc_data *nd=NULL;
  11041. int n = 0;
  11042. int i = 3;
  11043. int amount;
  11044. const char* npcname = script_getstr(st, 2);
  11045. nd = npc_name2id(npcname);
  11046. if (!nd || nd->bl.subtype!=SHOP)
  11047. { //Not found.
  11048. script_pushint(st,0);
  11049. return 0;
  11050. }
  11051. amount = ((st->end-2)/2)+1;
  11052. while (nd->u.shop_item[n].nameid && n < MAX_SHOPITEM)
  11053. n++;
  11054. //Reinsert as realloc could change the pointer address.
  11055. map_deliddb(&nd->bl);
  11056. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  11057. sizeof(nd->u.shop_item[0]) * (amount+n));
  11058. map_addiddb(&nd->bl);
  11059. while(script_hasdata(st,i)){
  11060. nd->u.shop_item[n].nameid = script_getnum(st,i);
  11061. i++;
  11062. nd->u.shop_item[n].value = script_getnum(st,i);
  11063. i++;
  11064. n++;
  11065. }
  11066. // Marks the last of our stuff..
  11067. nd->u.shop_item[n].value = 0;
  11068. nd->u.shop_item[n].nameid = 0;
  11069. script_pushint(st,1);
  11070. return 0;
  11071. }
  11072. BUILDIN_FUNC(npcshopdelitem)
  11073. {
  11074. struct npc_data *nd=NULL;
  11075. int n=0;
  11076. int i=3;
  11077. int size = 0;
  11078. const char* npcname = script_getstr(st, 2);
  11079. nd = npc_name2id(npcname);
  11080. if (!nd || nd->bl.subtype!=SHOP)
  11081. { //Not found.
  11082. script_pushint(st,0);
  11083. return 0;
  11084. }
  11085. while (nd->u.shop_item[size].nameid)
  11086. size++;
  11087. while (script_hasdata(st,i)) {
  11088. for(n=0;nd->u.shop_item[n].nameid && n < MAX_SHOPITEM;n++) {
  11089. if (nd->u.shop_item[n].nameid == script_getnum(st,i)) {
  11090. // We're moving 1 extra empty block. Junk data is eliminated later.
  11091. memmove(&nd->u.shop_item[n], &nd->u.shop_item[n+1], sizeof(nd->u.shop_item[0])*(size-n));
  11092. }
  11093. }
  11094. i++;
  11095. }
  11096. size = 0;
  11097. while (nd->u.shop_item[size].nameid)
  11098. size++;
  11099. //Reinsert as realloc could change the pointer address.
  11100. map_deliddb(&nd->bl);
  11101. nd = (struct npc_data *)aRealloc(nd,sizeof(struct npc_data) +
  11102. sizeof(nd->u.shop_item[0]) * (size+1));
  11103. map_addiddb(&nd->bl);
  11104. script_pushint(st,1);
  11105. return 0;
  11106. }
  11107. //Sets a script to attach to an npc.
  11108. BUILDIN_FUNC(npcshopattach)
  11109. {
  11110. struct npc_data *nd=NULL;
  11111. const char* npcname = script_getstr(st, 2);
  11112. int flag = 1;
  11113. if( script_hasdata(st,3) )
  11114. flag = script_getnum(st,3);
  11115. nd = npc_name2id(npcname);
  11116. if (!nd || nd->bl.subtype!=SHOP)
  11117. { //Not found.
  11118. script_pushint(st,0);
  11119. return 0;
  11120. }
  11121. if (flag)
  11122. nd->master_nd = ((struct npc_data *)map_id2bl(st->oid));
  11123. else
  11124. nd->master_nd = NULL;
  11125. script_pushint(st,1);
  11126. return 0;
  11127. }
  11128. /*==========================================
  11129. * Returns some values of an item [Lupus]
  11130. * Price, Weight, etc...
  11131. setitemscript(itemID,"{new item bonus script}",[n]);
  11132. Where n:
  11133. 0 - script
  11134. 1 - Equip script
  11135. 2 - Unequip script
  11136. *------------------------------------------*/
  11137. BUILDIN_FUNC(setitemscript)
  11138. {
  11139. int item_id,n=0;
  11140. const char *script;
  11141. struct item_data *i_data;
  11142. struct script_code **dstscript;
  11143. item_id = script_getnum(st,2);
  11144. script = script_getstr(st,3);
  11145. if( script_hasdata(st,4) )
  11146. n=script_getnum(st,4);
  11147. i_data = itemdb_exists(item_id);
  11148. if (!i_data || script==NULL || script[0]!='{') {
  11149. script_pushint(st,0);
  11150. return 0;
  11151. }
  11152. switch (n) {
  11153. case 2:
  11154. dstscript = &i_data->unequip_script;
  11155. break;
  11156. case 1:
  11157. dstscript = &i_data->equip_script;
  11158. break;
  11159. default:
  11160. dstscript = &i_data->script;
  11161. break;
  11162. }
  11163. if(*dstscript)
  11164. script_free_code(*dstscript);
  11165. *dstscript = parse_script(script, "script_setitemscript", 0, 0);
  11166. script_pushint(st,1);
  11167. return 0;
  11168. }
  11169. /* Work In Progress [Lupus]
  11170. BUILDIN_FUNC(addmonsterdrop)
  11171. {
  11172. int class_,item_id,chance;
  11173. class_=script_getnum(st,2);
  11174. item_id=script_getnum(st,3);
  11175. chance=script_getnum(st,4);
  11176. if(class_>1000 && item_id>500 && chance>0) {
  11177. script_pushint(st,1);
  11178. } else {
  11179. script_pushint(st,0);
  11180. }
  11181. }
  11182. BUILDIN_FUNC(delmonsterdrop)
  11183. {
  11184. int class_,item_id;
  11185. class_=script_getnum(st,2);
  11186. item_id=script_getnum(st,3);
  11187. if(class_>1000 && item_id>500) {
  11188. script_pushint(st,1);
  11189. } else {
  11190. script_pushint(st,0);
  11191. }
  11192. }
  11193. */
  11194. /*==========================================
  11195. * Returns some values of a monster [Lupus]
  11196. * Name, Level, race, size, etc...
  11197. getmonsterinfo(monsterID,queryIndex);
  11198. *------------------------------------------*/
  11199. BUILDIN_FUNC(getmonsterinfo)
  11200. {
  11201. struct mob_db *mob;
  11202. int mob_id;
  11203. mob_id = script_getnum(st,2);
  11204. if (!mobdb_checkid(mob_id)) {
  11205. ShowError("buildin_getmonsterinfo: Wrong Monster ID: %i", mob_id);
  11206. script_pushint(st,-1);
  11207. return -1;
  11208. }
  11209. mob = mob_db(mob_id);
  11210. switch ( script_getnum(st,3) ) {
  11211. case 0: script_pushconststr(st,mob->jname); break;
  11212. case 1: script_pushint(st,mob->lv); break;
  11213. case 2: script_pushint(st,mob->status.max_hp); break;
  11214. case 3: script_pushint(st,mob->base_exp); break;
  11215. case 4: script_pushint(st,mob->job_exp); break;
  11216. case 5: script_pushint(st,mob->status.rhw.atk); break;
  11217. case 6: script_pushint(st,mob->status.rhw.atk2); break;
  11218. case 7: script_pushint(st,mob->status.def); break;
  11219. case 8: script_pushint(st,mob->status.mdef); break;
  11220. case 9: script_pushint(st,mob->status.str); break;
  11221. case 10: script_pushint(st,mob->status.agi); break;
  11222. case 11: script_pushint(st,mob->status.vit); break;
  11223. case 12: script_pushint(st,mob->status.int_); break;
  11224. case 13: script_pushint(st,mob->status.dex); break;
  11225. case 14: script_pushint(st,mob->status.luk); break;
  11226. case 15: script_pushint(st,mob->status.rhw.range); break;
  11227. case 16: script_pushint(st,mob->range2); break;
  11228. case 17: script_pushint(st,mob->range3); break;
  11229. case 18: script_pushint(st,mob->status.size); break;
  11230. case 19: script_pushint(st,mob->status.race); break;
  11231. case 20: script_pushint(st,mob->status.def_ele); break;
  11232. case 21: script_pushint(st,mob->status.mode); break;
  11233. default: script_pushint(st,-1); //wrong Index
  11234. }
  11235. return 0;
  11236. }
  11237. BUILDIN_FUNC(checkvending) // check vending [Nab4]
  11238. {
  11239. TBL_PC *sd = NULL;
  11240. if(script_hasdata(st,2))
  11241. sd = map_nick2sd(script_getstr(st,2));
  11242. else
  11243. sd = script_rid2sd(st);
  11244. if(sd)
  11245. script_pushint(st,(sd->vender_id != 0));
  11246. else
  11247. script_pushint(st,0);
  11248. return 0;
  11249. }
  11250. BUILDIN_FUNC(checkchatting) // check chatting [Marka]
  11251. {
  11252. TBL_PC *sd = NULL;
  11253. if(script_hasdata(st,2))
  11254. sd = map_nick2sd(script_getstr(st,2));
  11255. else
  11256. sd = script_rid2sd(st);
  11257. if(sd)
  11258. script_pushint(st,(sd->chatID != 0));
  11259. else
  11260. script_pushint(st,0);
  11261. return 0;
  11262. }
  11263. int axtoi(const char *hexStg)
  11264. {
  11265. int n = 0; // position in string
  11266. int m = 0; // position in digit[] to shift
  11267. int count; // loop index
  11268. int intValue = 0; // integer value of hex string
  11269. int digit[11]; // hold values to convert
  11270. while (n < 10) {
  11271. if (hexStg[n]=='\0')
  11272. break;
  11273. if (hexStg[n] > 0x29 && hexStg[n] < 0x40 ) //if 0 to 9
  11274. digit[n] = hexStg[n] & 0x0f; //convert to int
  11275. else if (hexStg[n] >='a' && hexStg[n] <= 'f') //if a to f
  11276. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11277. else if (hexStg[n] >='A' && hexStg[n] <= 'F') //if A to F
  11278. digit[n] = (hexStg[n] & 0x0f) + 9; //convert to int
  11279. else break;
  11280. n++;
  11281. }
  11282. count = n;
  11283. m = n - 1;
  11284. n = 0;
  11285. while(n < count) {
  11286. // digit[n] is value of hex digit at position n
  11287. // (m << 2) is the number of positions to shift
  11288. // OR the bits into return value
  11289. intValue = intValue | (digit[n] << (m << 2));
  11290. m--; // adjust the position to set
  11291. n++; // next digit to process
  11292. }
  11293. return (intValue);
  11294. }
  11295. // [Lance] Hex string to integer converter
  11296. BUILDIN_FUNC(axtoi)
  11297. {
  11298. const char *hex = script_getstr(st,2);
  11299. script_pushint(st,axtoi(hex));
  11300. return 0;
  11301. }
  11302. // [zBuffer] List of player cont commands --->
  11303. BUILDIN_FUNC(rid2name)
  11304. {
  11305. struct block_list *bl = NULL;
  11306. int rid = script_getnum(st,2);
  11307. if((bl = map_id2bl(rid)))
  11308. {
  11309. switch(bl->type) {
  11310. case BL_MOB: script_pushconststr(st,((TBL_MOB*)bl)->name); break;
  11311. case BL_PC: script_pushconststr(st,((TBL_PC*)bl)->status.name); break;
  11312. case BL_NPC: script_pushconststr(st,((TBL_NPC*)bl)->exname); break;
  11313. case BL_PET: script_pushconststr(st,((TBL_PET*)bl)->pet.name); break;
  11314. case BL_HOM: script_pushconststr(st,((TBL_HOM*)bl)->homunculus.name); break;
  11315. default:
  11316. ShowError("buildin_rid2name: BL type unknown.\n");
  11317. script_pushconststr(st,"");
  11318. break;
  11319. }
  11320. } else {
  11321. ShowError("buildin_rid2name: invalid RID\n");
  11322. script_pushconststr(st,"(null)");
  11323. }
  11324. return 0;
  11325. }
  11326. BUILDIN_FUNC(pcblockmove)
  11327. {
  11328. int id, flag;
  11329. TBL_PC *sd = NULL;
  11330. id = script_getnum(st,2);
  11331. flag = script_getnum(st,3);
  11332. if(id)
  11333. sd = map_id2sd(id);
  11334. else
  11335. sd = script_rid2sd(st);
  11336. if(sd)
  11337. sd->state.blockedmove = flag > 0;
  11338. return 0;
  11339. }
  11340. BUILDIN_FUNC(pcfollow)
  11341. {
  11342. int id, targetid;
  11343. TBL_PC *sd = NULL;
  11344. id = script_getnum(st,2);
  11345. targetid = script_getnum(st,3);
  11346. if(id)
  11347. sd = map_id2sd(id);
  11348. else
  11349. sd = script_rid2sd(st);
  11350. if(sd)
  11351. pc_follow(sd, targetid);
  11352. return 0;
  11353. }
  11354. BUILDIN_FUNC(pcstopfollow)
  11355. {
  11356. int id;
  11357. TBL_PC *sd = NULL;
  11358. id = script_getnum(st,2);
  11359. if(id)
  11360. sd = map_id2sd(id);
  11361. else
  11362. sd = script_rid2sd(st);
  11363. if(sd)
  11364. pc_stop_following(sd);
  11365. return 0;
  11366. }
  11367. // <--- [zBuffer] List of player cont commands
  11368. // [zBuffer] List of mob control commands --->
  11369. //## TODO always return if the request/whatever was successfull [FlavioJS]
  11370. BUILDIN_FUNC(mobspawn)
  11371. {
  11372. int class_,x,y,id;
  11373. const char *str,*map;
  11374. // Who?
  11375. str =script_getstr(st,2);
  11376. // What?
  11377. class_ =script_getnum(st,3);
  11378. // Where?
  11379. map =script_getstr(st,4);
  11380. x =script_getnum(st,5);
  11381. y =script_getnum(st,6);
  11382. id = mob_once_spawn(map_id2sd(st->rid),map,x,y,str,class_,1,"");
  11383. script_pushint(st,id);
  11384. return 0;
  11385. }
  11386. BUILDIN_FUNC(mobremove)
  11387. {
  11388. int id;
  11389. struct block_list *bl = NULL;
  11390. id = script_getnum(st,2);
  11391. bl = map_id2bl(id);
  11392. if (bl && bl->type == BL_MOB)
  11393. unit_free(bl,0);
  11394. return 0;
  11395. }
  11396. BUILDIN_FUNC(getmobdata)
  11397. {
  11398. int num, id;
  11399. char *name;
  11400. struct mob_data *md = NULL;
  11401. TBL_PC *sd = st->rid?map_id2sd(st->rid):NULL;
  11402. id = script_getnum(st,2);
  11403. if(!(md = (struct mob_data *)map_id2bl(id)) || md->bl.type != BL_MOB || !data_isreference(script_getdata(st,3)) ){
  11404. ShowWarning("buildin_getmobdata: Error in argument!\n");
  11405. return -1;
  11406. }
  11407. num=st->stack->stack_data[st->start+3].u.num;
  11408. name=(char *)(str_buf+str_data[num&0x00ffffff].str);
  11409. setd_sub(st,sd,name,0,(void *)(int)md->class_,script_getref(st,3));
  11410. setd_sub(st,sd,name,1,(void *)(int)md->level,script_getref(st,3));
  11411. setd_sub(st,sd,name,2,(void *)(int)md->status.hp,script_getref(st,3));
  11412. setd_sub(st,sd,name,3,(void *)(int)md->status.max_hp,script_getref(st,3));
  11413. setd_sub(st,sd,name,4,(void *)(int)md->master_id,script_getref(st,3));
  11414. setd_sub(st,sd,name,5,(void *)(int)md->bl.m,script_getref(st,3));
  11415. setd_sub(st,sd,name,6,(void *)(int)md->bl.x,script_getref(st,3));
  11416. setd_sub(st,sd,name,7,(void *)(int)md->bl.y,script_getref(st,3));
  11417. setd_sub(st,sd,name,8,(void *)(int)md->status.speed,script_getref(st,3));
  11418. setd_sub(st,sd,name,9,(void *)(int)md->status.mode,script_getref(st,3));
  11419. setd_sub(st,sd,name,10,(void *)(int)md->special_state.ai,script_getref(st,3));
  11420. setd_sub(st,sd,name,11,(void *)(int)md->sc.option,script_getref(st,3));
  11421. setd_sub(st,sd,name,12,(void *)(int)md->vd->sex,script_getref(st,3));
  11422. setd_sub(st,sd,name,13,(void *)(int)md->vd->class_,script_getref(st,3));
  11423. setd_sub(st,sd,name,14,(void *)(int)md->vd->hair_style,script_getref(st,3));
  11424. setd_sub(st,sd,name,15,(void *)(int)md->vd->hair_color,script_getref(st,3));
  11425. setd_sub(st,sd,name,16,(void *)(int)md->vd->head_bottom,script_getref(st,3));
  11426. setd_sub(st,sd,name,17,(void *)(int)md->vd->head_mid,script_getref(st,3));
  11427. setd_sub(st,sd,name,18,(void *)(int)md->vd->head_top,script_getref(st,3));
  11428. setd_sub(st,sd,name,19,(void *)(int)md->vd->cloth_color,script_getref(st,3));
  11429. setd_sub(st,sd,name,20,(void *)(int)md->vd->shield,script_getref(st,3));
  11430. setd_sub(st,sd,name,21,(void *)(int)md->vd->weapon,script_getref(st,3));
  11431. setd_sub(st,sd,name,22,(void *)(int)md->vd->shield,script_getref(st,3));
  11432. setd_sub(st,sd,name,23,(void *)(int)md->ud.dir,script_getref(st,3));
  11433. setd_sub(st,sd,name,24,(void *)(int)md->state.killer,script_getref(st,3));
  11434. setd_sub(st,sd,name,25,(void *)(int)md->callback_flag,script_getref(st,3));
  11435. setd_sub(st,sd,name,26,(void *)(int)md->state.no_random_walk, script_getref(st,3));
  11436. return 0;
  11437. }
  11438. /// Changes the data of a mob
  11439. ///
  11440. /// setmobdata <mob unit id>,<type>,<value>;
  11441. BUILDIN_FUNC(setmobdata)
  11442. {
  11443. struct block_list* mob_bl;
  11444. mob_bl = map_id2bl(script_getnum(st,2));
  11445. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11446. {
  11447. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11448. int type;
  11449. int value;
  11450. type = script_getnum(st,3);
  11451. value = script_getnum(st,4);
  11452. switch( type )
  11453. {
  11454. case 0: md->class_ = (short)value; break;
  11455. case 1: md->level = (unsigned short)value; break;
  11456. case 2: md->status.hp = (unsigned int)value; break;
  11457. case 3: md->status.max_hp = (unsigned int)value; break;
  11458. case 4: md->master_id = value; break;
  11459. case 5: md->bl.m = (short)value; break;
  11460. case 6: md->bl.x = (short)value; break;
  11461. case 7: md->bl.y = (short)value; break;
  11462. case 8: md->status.speed = (unsigned short)value; break;
  11463. case 9: md->status.mode = (unsigned short)value; break;
  11464. case 10: md->special_state.ai = (unsigned int)value; break;
  11465. case 11: md->sc.option = (unsigned short)value; break;
  11466. case 12: md->vd->sex = (char)value; break;
  11467. case 13: md->vd->class_ = (unsigned short)value; break;
  11468. case 14: md->vd->hair_style = (unsigned short)value; break;
  11469. case 15: md->vd->hair_color = (unsigned short)value; break;
  11470. case 16: md->vd->head_bottom = (unsigned short)value; break;
  11471. case 17: md->vd->head_mid = (unsigned short)value; break;
  11472. case 18: md->vd->head_top = (unsigned short)value; break;
  11473. case 19: md->vd->cloth_color = (unsigned short)value; break;
  11474. case 20: md->vd->shield = (unsigned short)value; break;
  11475. case 21: md->vd->weapon = (unsigned short)value; break;
  11476. case 22: md->vd->shield = (unsigned short)value; break;
  11477. case 23: md->ud.dir = (unsigned char)value; break;
  11478. case 24: md->state.killer = value > 0 ? 1 : 0; break;
  11479. case 25: md->callback_flag = (short)value; break;
  11480. case 26: md->state.no_random_walk = value > 0 ? 1 : 0; break;
  11481. default:
  11482. ShowError("script:setmobdata: unknown data identifier %d\n", type);
  11483. return 1;
  11484. }
  11485. }
  11486. return 0;
  11487. }
  11488. /// Makes the mob assist the target unit as a slave
  11489. ///
  11490. /// mobassist <mob unit id>,"<player name>";
  11491. /// mobassist <mob unit id>,<target id>;
  11492. BUILDIN_FUNC(mobassist)
  11493. {
  11494. struct block_list* mob_bl;
  11495. // get mob
  11496. mob_bl = map_id2bl(script_getnum(st,2));
  11497. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11498. {
  11499. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11500. struct block_list* target_bl = NULL;
  11501. struct script_data* data;
  11502. // get target
  11503. data = script_getdata(st, 3);
  11504. get_val(st, data);
  11505. if( data_isstring(data) )
  11506. {
  11507. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11508. if( sd != NULL )
  11509. target_bl = &sd->bl;
  11510. }
  11511. if( target_bl == NULL )
  11512. target_bl = map_id2bl(conv_num(st, data));
  11513. // set mob as slave
  11514. if( target_bl != NULL )
  11515. {
  11516. struct unit_data* ud;
  11517. md->master_id = target_bl->id;
  11518. md->state.killer = 1;
  11519. mob_convertslave(md);
  11520. ud = unit_bl2ud(mob_bl);
  11521. if( ud != NULL )
  11522. {
  11523. if( ud->target != 0 )
  11524. md->target_id = ud->target;
  11525. else if( ud->skilltarget != 0 )
  11526. md->target_id = ud->skilltarget;
  11527. if( md->target_id != 0 )
  11528. unit_walktobl(&md->bl, map_id2bl(md->target_id), 65025, 2);
  11529. }
  11530. }
  11531. }
  11532. return 0;
  11533. }
  11534. /// Attaches the current npc or the target npc to the mob unit
  11535. ///
  11536. /// mobattach <mob unit id>{,"<npc name>"};
  11537. BUILDIN_FUNC(mobattach)
  11538. {
  11539. struct block_list* mob_bl;
  11540. mob_bl = map_id2bl(script_getnum(st,2));
  11541. if( mob_bl != NULL && mob_bl->type == BL_MOB )
  11542. {
  11543. TBL_MOB* md = (TBL_MOB*)mob_bl;
  11544. TBL_NPC* nd = NULL;
  11545. if( script_hasdata(st,3) )
  11546. nd = npc_name2id(script_getstr(st, 3));
  11547. else
  11548. {
  11549. struct block_list* npc_bl = map_id2bl(st->oid);
  11550. if( npc_bl != NULL && npc_bl->type == BL_NPC )
  11551. nd = (TBL_NPC*)npc_bl;
  11552. }
  11553. if( nd != NULL )
  11554. md->nd = nd;
  11555. }
  11556. return 0;
  11557. }
  11558. /// Makes the unit walk to target position or map
  11559. /// Returns if it was successfull
  11560. ///
  11561. /// unitwalk(<unit_id>,<x>,<y>) -> <bool>
  11562. /// unitwalk(<unit_id>,<map_id>) -> <bool>
  11563. BUILDIN_FUNC(unitwalk)
  11564. {
  11565. struct block_list* bl;
  11566. bl = map_id2bl(script_getnum(st,2));
  11567. if( bl == NULL )
  11568. {
  11569. script_pushint(st, 0);
  11570. }
  11571. else if( script_hasdata(st,4) )
  11572. {
  11573. int x = script_getnum(st,3);
  11574. int y = script_getnum(st,4);
  11575. script_pushint(st, unit_walktoxy(bl,x,y,0));// We'll use harder calculations.
  11576. }
  11577. else
  11578. {
  11579. int map_id = script_getnum(st,3);
  11580. script_pushint(st, unit_walktobl(bl,map_id2bl(map_id),65025,1));
  11581. }
  11582. return 0;
  11583. }
  11584. /// Kills the unit
  11585. ///
  11586. /// unitkill <unit_id>;
  11587. BUILDIN_FUNC(unitkill)
  11588. {
  11589. struct block_list* bl = map_id2bl(script_getnum(st,2));
  11590. if( bl != NULL )
  11591. status_kill(bl);
  11592. return 0;
  11593. }
  11594. /// Warps the unit to the target position in the target map
  11595. /// Returns if it was successfull
  11596. ///
  11597. /// unitwarp(<unit_id>,"<map name>",<x>,<y>) -> <bool>
  11598. BUILDIN_FUNC(unitwarp)
  11599. {
  11600. int unit_id;
  11601. int map;
  11602. short x;
  11603. short y;
  11604. struct block_list* bl;
  11605. unit_id = script_getnum(st,2);
  11606. map = map_mapname2mapid(script_getstr(st, 3));
  11607. x = (short)script_getnum(st,4);
  11608. y = (short)script_getnum(st,5);
  11609. bl = map_id2bl(unit_id);
  11610. if( map >= 0 && bl != NULL )
  11611. script_pushint(st, unit_warp(bl,map,x,y,0));
  11612. else
  11613. script_pushint(st, 0);
  11614. return 0;
  11615. }
  11616. /// Makes the unit attack the target.
  11617. /// If the unit is a player and <action type> is not 0, it does a continuous
  11618. /// attack instead of a single attack.
  11619. /// Returns if the request was successfull.
  11620. ///
  11621. /// unitattack(<unit_id>,"<target name>"{,<action type>}) -> <bool>
  11622. /// unitattack(<unit_id>,<target_id>{,<action type>}) -> <bool>
  11623. BUILDIN_FUNC(unitattack)
  11624. {
  11625. struct block_list* unit_bl;
  11626. struct block_list* target_bl = NULL;
  11627. struct script_data* data;
  11628. int actiontype = 0;
  11629. // get unit
  11630. unit_bl = map_id2bl(script_getnum(st,2));
  11631. if( unit_bl == NULL ) {
  11632. script_pushint(st, 0);
  11633. return 0;
  11634. }
  11635. data = script_getdata(st, 3);
  11636. get_val(st, data);
  11637. if( data_isstring(data) )
  11638. {
  11639. TBL_PC* sd = map_nick2sd(conv_str(st, data));
  11640. if( sd != NULL )
  11641. target_bl = &sd->bl;
  11642. } else
  11643. target_bl = map_id2bl(conv_num(st, data));
  11644. // request the attack
  11645. if( target_bl == NULL )
  11646. {
  11647. script_pushint(st, 0);
  11648. return 0;
  11649. }
  11650. // get actiontype
  11651. if( script_hasdata(st,4) )
  11652. actiontype = script_getnum(st,4);
  11653. switch( unit_bl->type )
  11654. {
  11655. case BL_PC:
  11656. clif_parse_ActionRequest_sub(((TBL_PC *)unit_bl), actiontype > 0 ? 0x07 : 0x00, target_bl->id, gettick());
  11657. script_pushint(st, 1);
  11658. return 0;
  11659. case BL_MOB:
  11660. ((TBL_MOB *)unit_bl)->state.killer = 1;
  11661. ((TBL_MOB *)unit_bl)->target_id = target_bl->id;
  11662. break;
  11663. case BL_PET:
  11664. ((TBL_PET *)unit_bl)->target_id = target_bl->id;
  11665. break;
  11666. default:
  11667. ShowError("script:unitattack: unsupported source unit type %d\n", unit_bl->type);
  11668. script_pushint(st, 0);
  11669. return 1;
  11670. }
  11671. script_pushint(st, unit_walktobl(unit_bl, target_bl, 65025, 2));
  11672. return 0;
  11673. }
  11674. /// Makes the unit stop attacking and moving
  11675. ///
  11676. /// unitstop <unit_id>;
  11677. BUILDIN_FUNC(unitstop)
  11678. {
  11679. int unit_id;
  11680. struct block_list* bl;
  11681. unit_id = script_getnum(st,2);
  11682. bl = map_id2bl(unit_id);
  11683. if( bl != NULL )
  11684. {
  11685. unit_stop_attack(bl);
  11686. unit_stop_walking(bl,0);
  11687. if( bl->type == BL_MOB )
  11688. ((TBL_MOB*)bl)->target_id = 0;
  11689. }
  11690. return 0;
  11691. }
  11692. /// Makes the unit say the message
  11693. ///
  11694. /// unittalk <unit_id>,"<message>";
  11695. BUILDIN_FUNC(unittalk)
  11696. {
  11697. int unit_id;
  11698. const char* message;
  11699. struct block_list* bl;
  11700. unit_id = script_getnum(st,2);
  11701. message = script_getstr(st, 3);
  11702. bl = map_id2bl(unit_id);
  11703. if( bl != NULL )
  11704. {
  11705. struct StringBuf sbuf;
  11706. StringBuf_Init(&sbuf);
  11707. StringBuf_Printf(&sbuf, "%s : %s", status_get_name(bl), message);
  11708. clif_message(bl, StringBuf_Value(&sbuf));
  11709. if( bl->type == BL_PC )
  11710. clif_displaymessage(((TBL_PC*)bl)->fd, StringBuf_Value(&sbuf));
  11711. StringBuf_Destroy(&sbuf);
  11712. }
  11713. return 0;
  11714. }
  11715. /// Makes the unit do an emotion
  11716. ///
  11717. /// unitemote <unit_id>,<emotion>;
  11718. ///
  11719. /// @see e_* in const.txt
  11720. BUILDIN_FUNC(unitemote)
  11721. {
  11722. int unit_id;
  11723. int emotion;
  11724. struct block_list* bl;
  11725. unit_id = script_getnum(st,2);
  11726. emotion = script_getnum(st,3);
  11727. bl = map_id2bl(unit_id);
  11728. if( bl != NULL )
  11729. clif_emotion(bl, emotion);
  11730. return 0;
  11731. }
  11732. /// Makes the unit cast the skill on the target or self if no target is specified
  11733. ///
  11734. /// unitskilluseid <unit_id>,<skill_id>,<skill_lv>{,<target_id>};
  11735. BUILDIN_FUNC(unitskilluseid)
  11736. {
  11737. int unit_id;
  11738. int skill_id;
  11739. int skill_lv;
  11740. int target_id;
  11741. struct block_list* bl;
  11742. unit_id = script_getnum(st,2);
  11743. skill_id = script_getnum(st,3);
  11744. skill_lv = script_getnum(st,4);
  11745. target_id = ( script_hasdata(st,5) ? script_getnum(st,5) : unit_id );
  11746. bl = map_id2bl(unit_id);
  11747. if( bl != NULL )
  11748. unit_skilluse_id(bl, target_id, skill_id, skill_lv);
  11749. return 0;
  11750. }
  11751. /// Makes the unit cast the skill on the target position.
  11752. ///
  11753. /// unitskillusepos <unit_id>,<skill_id>,<skill_lv>,<target_x>,<target_y>;
  11754. BUILDIN_FUNC(unitskillusepos)
  11755. {
  11756. int unit_id;
  11757. int skill_id;
  11758. int skill_lv;
  11759. int skill_x;
  11760. int skill_y;
  11761. struct block_list* bl;
  11762. unit_id = script_getnum(st,2);
  11763. skill_id = script_getnum(st,3);
  11764. skill_lv = script_getnum(st,4);
  11765. skill_x = script_getnum(st,5);
  11766. skill_y = script_getnum(st,6);
  11767. bl = map_id2bl(unit_id);
  11768. if( bl != NULL )
  11769. unit_skilluse_pos(bl, skill_x, skill_y, skill_id, skill_lv);
  11770. return 0;
  11771. }
  11772. // <--- [zBuffer] List of mob control commands
  11773. /// Pauses the execution of the script, detaching the player
  11774. ///
  11775. /// sleep <mili seconds>;
  11776. BUILDIN_FUNC(sleep)
  11777. {
  11778. int ticks;
  11779. TBL_PC* sd;
  11780. ticks = script_getnum(st,2);
  11781. sd = map_id2sd(st->rid);
  11782. // detach the player
  11783. if( sd && sd->npc_id == st->oid )
  11784. {
  11785. sd->npc_id = 0;
  11786. }
  11787. st->rid = 0;
  11788. if( ticks <= 0 )
  11789. {// do nothing
  11790. }
  11791. else if( st->sleep.tick == 0 )
  11792. {// sleep for the target amount of time
  11793. st->state = RERUNLINE;
  11794. st->sleep.tick = ticks;
  11795. }
  11796. else
  11797. {// sleep time is over
  11798. st->sleep.tick = 0;
  11799. }
  11800. return 0;
  11801. }
  11802. /// Pauses the execution of the script, keeping the player attached
  11803. /// Returns if a player is still attached
  11804. ///
  11805. /// sleep2(<mili secconds>) -> <bool>
  11806. BUILDIN_FUNC(sleep2)
  11807. {
  11808. int ticks;
  11809. ticks = script_getnum(st,2);
  11810. if( ticks <= 0 )
  11811. {// do nothing
  11812. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11813. }
  11814. else if( !st->sleep.tick )
  11815. {// sleep for the target amount of time
  11816. st->state = RERUNLINE;
  11817. st->sleep.tick = ticks;
  11818. }
  11819. else
  11820. {// sleep time is over
  11821. st->sleep.tick = 0;
  11822. script_pushint(st, (map_id2sd(st->rid)!=NULL));
  11823. }
  11824. return 0;
  11825. }
  11826. /// Awakes all the sleep timers of the target npc
  11827. ///
  11828. /// awake "<npc name>";
  11829. BUILDIN_FUNC(awake)
  11830. {
  11831. struct npc_data* nd;
  11832. struct linkdb_node *node = (struct linkdb_node *)sleep_db;
  11833. nd = npc_name2id(script_getstr(st, 2));
  11834. if( nd == NULL )
  11835. return 0;
  11836. while( node )
  11837. {
  11838. if( (int)node->key == nd->bl.id )
  11839. {// sleep timer for the npc
  11840. struct script_state* tst = (struct script_state*)node->data;
  11841. TBL_PC* sd = map_id2sd(tst->rid);
  11842. if( tst->sleep.timer == -1 )
  11843. {// already awake ???
  11844. node = node->next;
  11845. continue;
  11846. }
  11847. if( (sd && sd->status.char_id != tst->sleep.charid) || (tst->rid && !sd))
  11848. {// char not online anymore / another char of the same account is online - Cancel execution
  11849. tst->state = END;
  11850. tst->rid = 0;
  11851. }
  11852. delete_timer(tst->sleep.timer, run_script_timer);
  11853. node = script_erase_sleepdb(node);
  11854. tst->sleep.timer = -1;
  11855. tst->sleep.tick = 0;
  11856. run_script_main(tst);
  11857. }
  11858. else
  11859. {
  11860. node = node->next;
  11861. }
  11862. }
  11863. return 0;
  11864. }
  11865. /// Returns a reference to a variable of the target NPC.
  11866. /// Returns 0 if an error occurs.
  11867. ///
  11868. /// getvariableofnpc(<variable>, "<npc name>") -> <reference>
  11869. BUILDIN_FUNC(getvariableofnpc)
  11870. {
  11871. struct script_data* data;
  11872. char* name;
  11873. struct npc_data* nd;
  11874. data = script_getdata(st,2);
  11875. if( !data_isreference(data) )
  11876. {// Not a reference (aka varaible name)
  11877. ShowError("script:getvariableofnpc: not a variable\n");
  11878. script_reportdata(data);
  11879. script_pushnil(st);
  11880. st->state = END;
  11881. return 1;
  11882. }
  11883. name = reference_getname(data);
  11884. if( *name != '.' || name[1] == '@' )
  11885. {// not a npc variable
  11886. ShowError("script:getvariableofnpc: invalid scope (not npc variable)\n");
  11887. script_reportdata(data);
  11888. script_pushnil(st);
  11889. st->state = END;
  11890. return 1;
  11891. }
  11892. nd = npc_name2id(script_getstr(st,3));
  11893. if( nd == NULL || nd->bl.subtype != SCRIPT || nd->u.scr.script == NULL )
  11894. {// NPC not found or has no script
  11895. ShowError("script:getvariableofnpc: can't find npc %s\n", script_getstr(st,3));
  11896. script_pushnil(st);
  11897. st->state = END;
  11898. return 1;
  11899. }
  11900. push_val2(st->stack, C_NAME, reference_getuid(data), &nd->u.scr.script->script_vars );
  11901. return 0;
  11902. }
  11903. /// Opens a warp portal.
  11904. /// Has no "portal opening" effect/sound, it opens the portal immediately.
  11905. ///
  11906. /// warpportal <source x>,<source y>,"<target map>",<target x>,<target y>;
  11907. ///
  11908. /// @author blackhole89
  11909. BUILDIN_FUNC(warpportal)
  11910. {
  11911. int spx;
  11912. int spy;
  11913. unsigned short mapindex;
  11914. int tpx;
  11915. int tpy;
  11916. struct skill_unit_group* group;
  11917. struct block_list* bl;
  11918. bl = map_id2bl(st->oid);
  11919. if( bl == NULL )
  11920. {
  11921. ShowError("script:warpportal: npc is needed\n");
  11922. return 1;
  11923. }
  11924. spx = script_getnum(st,2);
  11925. spy = script_getnum(st,3);
  11926. mapindex = mapindex_name2id(script_getstr(st, 4));
  11927. tpx = script_getnum(st,5);
  11928. tpy = script_getnum(st,6);
  11929. if( mapindex == 0 )
  11930. return 0;// map not found
  11931. group = skill_unitsetting(bl, AL_WARP, 4, spx, spy, 1);
  11932. if( group == NULL )
  11933. return 0;// failed
  11934. group->val2 = (tpx<<16) | tpy;
  11935. group->val3 = mapindex;
  11936. return 0;
  11937. }