pc.cpp 480 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #include "pc.hpp"
  4. #include <cmath>
  5. #include <cstdlib>
  6. #include <map>
  7. #ifdef MAP_GENERATOR
  8. #include <fstream>
  9. #include <iostream>
  10. #include <nlohmann/json.hpp>
  11. #endif
  12. #include <common/cbasetypes.hpp>
  13. #include <common/core.hpp> // get_svn_revision()
  14. #include <common/database.hpp>
  15. #include <common/ers.hpp> // ers_destroy
  16. #include <common/grfio.hpp>
  17. #include <common/malloc.hpp>
  18. #include <common/mmo.hpp> //NAME_LENGTH
  19. #include <common/nullpo.hpp>
  20. #include <common/random.hpp>
  21. #include <common/showmsg.hpp>
  22. #include <common/socket.hpp> // session[]
  23. #include <common/strlib.hpp> // safestrncpy()
  24. #include <common/timer.hpp>
  25. #include <common/utilities.hpp>
  26. #include <common/utils.hpp>
  27. #include "achievement.hpp"
  28. #include "atcommand.hpp" // get_atcommand_level()
  29. #include "battle.hpp" // battle_config
  30. #include "battleground.hpp"
  31. #include "buyingstore.hpp" // struct s_buyingstore
  32. #include "channel.hpp"
  33. #include "chat.hpp"
  34. #include "chrif.hpp"
  35. #include "clan.hpp"
  36. #include "clif.hpp"
  37. #include "date.hpp" // is_day_of_*()
  38. #include "duel.hpp"
  39. #include "elemental.hpp"
  40. #include "guild.hpp"
  41. #include "homunculus.hpp"
  42. #include "instance.hpp"
  43. #include "intif.hpp"
  44. #include "itemdb.hpp" // MAX_ITEMGROUP
  45. #include "log.hpp"
  46. #include "map.hpp"
  47. #include "mercenary.hpp"
  48. #include "mob.hpp"
  49. #include "npc.hpp"
  50. #include "party.hpp" // party_search()
  51. #include "pc_groups.hpp"
  52. #include "pet.hpp" // pet_unlocktarget()
  53. #include "quest.hpp"
  54. #include "skill.hpp" // skill_isCopyable()
  55. #include "script.hpp" // struct script_reg, struct script_regstr
  56. #include "searchstore.hpp" // struct s_search_store_info
  57. #include "status.hpp" // OPTION_*, struct weapon_atk
  58. #include "storage.hpp"
  59. #include "unit.hpp" // unit_stop_attack(), unit_stop_walking()
  60. #include "vending.hpp" // struct s_vending
  61. using namespace rathena;
  62. JobDatabase job_db;
  63. CaptchaDatabase captcha_db;
  64. const char *macro_allowed_answer_chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  65. int32 pc_split_atoui(char* str, uint32* val, char sep, int32 max);
  66. static inline bool pc_attendance_rewarded_today( map_session_data* sd );
  67. #define PVP_CALCRANK_INTERVAL 1000 // PVP calculation interval
  68. PlayerStatPointDatabase statpoint_db;
  69. SkillTreeDatabase skill_tree_db;
  70. // timer for night.day implementation
  71. int32 day_timer_tid = INVALID_TIMER;
  72. int32 night_timer_tid = INVALID_TIMER;
  73. struct eri *pc_sc_display_ers = nullptr;
  74. struct eri *num_reg_ers;
  75. struct eri *str_reg_ers;
  76. int32 pc_expiration_tid = INVALID_TIMER;
  77. struct fame_list smith_fame_list[MAX_FAME_LIST];
  78. struct fame_list chemist_fame_list[MAX_FAME_LIST];
  79. struct fame_list taekwon_fame_list[MAX_FAME_LIST];
  80. const std::string AttendanceDatabase::getDefaultLocation(){
  81. return std::string(db_path) + "/attendance.yml";
  82. }
  83. /**
  84. * Reads and parses an entry from the attendance_db.
  85. * @param node: YAML node containing the entry.
  86. * @return count of successfully parsed rows
  87. */
  88. uint64 AttendanceDatabase::parseBodyNode(const ryml::NodeRef& node){
  89. uint32 start;
  90. if( !this->asUInt32( node, "Start", start ) ){
  91. return 0;
  92. }
  93. std::shared_ptr<s_attendance_period> attendance_period = this->find( start );
  94. bool exists = attendance_period != nullptr;
  95. if( !exists ){
  96. if( !this->nodeExists( node, "End" ) ){
  97. this->invalidWarning( node, "Node \"End\" is missing.\n" );
  98. return 0;
  99. }
  100. if( !this->nodeExists( node, "Rewards" ) ){
  101. this->invalidWarning( node, "Node \"Rewards\" is missing.\n" );
  102. return 0;
  103. }
  104. attendance_period = std::make_shared<s_attendance_period>();
  105. attendance_period->start = start;
  106. }
  107. // If it does not exist yet, we need to check it for sure
  108. bool requiresCollisionDetection = !exists;
  109. if( this->nodeExists( node, "End" ) ){
  110. uint32 end;
  111. if( !this->asUInt32( node, "End", end ) ){
  112. return 0;
  113. }
  114. // If the period is outdated already, we do not even bother parsing
  115. if( end < date_get( DT_YYYYMMDD ) ){
  116. this->invalidWarning( node, "Node \"End\" date %u has already passed, skipping.\n", end );
  117. return 0;
  118. }
  119. if( !exists || attendance_period->end != end ){
  120. requiresCollisionDetection = true;
  121. attendance_period->end = end;
  122. }
  123. }
  124. // Collision detection
  125. if( requiresCollisionDetection ){
  126. bool collision = false;
  127. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : *this ){
  128. std::shared_ptr<s_attendance_period> period = pair.second;
  129. if( exists && period->start == attendance_period->start ){
  130. // Dont compare to yourself
  131. continue;
  132. }
  133. // Check if start is inside another period
  134. if( period->start <= attendance_period->start && start <= period->end ){
  135. this->invalidWarning( node, "Node \"Start\" period %u intersects with period %u-%u, skipping.\n", attendance_period->start, period->start, period->end );
  136. collision = true;
  137. break;
  138. }
  139. // Check if end is inside another period
  140. if( period->start <= attendance_period->end && attendance_period->end <= period->end ){
  141. this->invalidWarning( node, "Node \"End\" period %u intersects with period %u-%u.\n", attendance_period->start, period->start, period->end );
  142. collision = true;
  143. break;
  144. }
  145. }
  146. if( collision ){
  147. return 0;
  148. }
  149. }
  150. if( this->nodeExists( node, "Rewards" ) ){
  151. const auto& rewardsNode = node["Rewards"];
  152. for( const auto& rewardNode : rewardsNode ){
  153. uint32 day;
  154. if( !this->asUInt32( rewardNode, "Day", day ) ){
  155. continue;
  156. }
  157. day -= 1;
  158. std::shared_ptr<s_attendance_reward> reward = util::map_find( attendance_period->rewards, day );
  159. bool reward_exists = reward != nullptr;
  160. if( !reward_exists ){
  161. if( !this->nodeExists( rewardNode, "ItemId" ) ){
  162. this->invalidWarning( rewardNode, "Node \"ItemId\" is missing.\n" );
  163. return 0;
  164. }
  165. reward = std::make_shared<s_attendance_reward>();
  166. }
  167. if( this->nodeExists( rewardNode, "ItemId" ) ){
  168. t_itemid item_id;
  169. if( !this->asUInt32( rewardNode, "ItemId", item_id ) ){
  170. continue;
  171. }
  172. if( !item_db.exists( item_id ) ){
  173. ShowError( "pc_attendance_load: Unknown item ID %u for day %d.\n", item_id, day + 1 );
  174. continue;
  175. }
  176. reward->item_id = item_id;
  177. }
  178. if( this->nodeExists( rewardNode, "Amount" ) ){
  179. uint16 amount;
  180. if( !this->asUInt16( rewardNode, "Amount", amount ) ){
  181. continue;
  182. }
  183. if( amount == 0 ){
  184. ShowWarning( "pc_attendance_load: Invalid reward count %hu for day %d. Defaulting to 1...\n", amount, day + 1 );
  185. amount = 1;
  186. }else if( amount > MAX_AMOUNT ){
  187. ShowError( "pc_attendance_load: Reward count %hu above maximum %hu for day %d. Defaulting to %hu...\n", amount, MAX_AMOUNT, day + 1, MAX_AMOUNT );
  188. amount = MAX_AMOUNT;
  189. }
  190. reward->amount = amount;
  191. }else{
  192. if( !reward_exists ){
  193. reward->amount = 1;
  194. }
  195. }
  196. if( !reward_exists ){
  197. attendance_period->rewards[day] = reward;
  198. }
  199. }
  200. bool missing_day = false;
  201. for( int32 day = 0; day < attendance_period->rewards.size(); day++ ){
  202. if( attendance_period->rewards.find( day ) == attendance_period->rewards.end() ){
  203. ShowError( "pc_attendance_load: Reward for day %d is missing.\n", day + 1 );
  204. missing_day = true;
  205. break;
  206. }
  207. }
  208. if( missing_day ){
  209. return 0;
  210. }
  211. }
  212. if( !exists ){
  213. this->put( start, attendance_period );
  214. }
  215. return 1;
  216. }
  217. AttendanceDatabase attendance_db;
  218. const std::string ReputationDatabase::getDefaultLocation(){
  219. return std::string( db_path ) + "/reputation.yml";
  220. }
  221. uint64 ReputationDatabase::parseBodyNode( const ryml::NodeRef& node ){
  222. int64 id;
  223. if( !this->asInt64( node, "Id", id ) ){
  224. return 0;
  225. }
  226. std::shared_ptr<s_reputation> reputation = this->find( id );
  227. bool exists = reputation != nullptr;
  228. if( !exists ){
  229. if( !this->nodesExist( node, { "Name", "Variable" } ) ){
  230. return 0;
  231. }
  232. reputation = std::make_shared<s_reputation>();
  233. reputation->id = id;
  234. }
  235. if( this->nodeExists( node, "Name" ) ){
  236. std::string name;
  237. if( !this->asString( node, "Name", name ) ){
  238. return 0;
  239. }
  240. reputation->name = name;
  241. }
  242. if( this->nodeExists( node, "Variable" ) ){
  243. std::string variable;
  244. if( !this->asString( node, "Variable", variable ) ){
  245. return 0;
  246. }
  247. if( variable.length() > 32 ){
  248. this->invalidWarning( node, "Variable name \"%s\" exceeds maximum length 32.\n", variable.c_str() );
  249. return 0;
  250. }
  251. reputation->variable = variable;
  252. }
  253. if( this->nodeExists( node, "Minimum" ) ){
  254. int64 minimum;
  255. if( !this->asInt64( node, "Minimum", minimum ) ){
  256. return 0;
  257. }
  258. reputation->minimum = minimum;
  259. }else{
  260. if( !exists ){
  261. reputation->minimum = INT64_MIN;
  262. }
  263. }
  264. if( this->nodeExists( node, "Maximum" ) ){
  265. int64 maximum;
  266. if( !this->asInt64( node, "Maximum", maximum ) ){
  267. return 0;
  268. }
  269. reputation->maximum = maximum;
  270. }else{
  271. if( !exists ){
  272. reputation->maximum = INT64_MIN;
  273. }
  274. }
  275. #ifdef MAP_GENERATOR
  276. if (this->nodeExists(node, "Visibility")) {
  277. std::string visibility;
  278. if (!this->asString(node, "Visibility", visibility)) {
  279. return 0;
  280. }
  281. if (visibility == "Always")
  282. reputation->visibility = s_reputation::e_visibility::ALWAYS;
  283. else if (visibility == "Never")
  284. reputation->visibility = s_reputation::e_visibility::NEVER;
  285. else if (visibility == "Exist")
  286. reputation->visibility = s_reputation::e_visibility::EXIST;
  287. else {
  288. this->invalidWarning(node, "Visibility \"%s\" unknown.\n", visibility.c_str());
  289. return 0;
  290. }
  291. } else {
  292. if (!exists) {
  293. reputation->visibility = s_reputation::e_visibility::ALWAYS;
  294. }
  295. }
  296. #endif
  297. if( !exists ){
  298. this->put( id, reputation );
  299. }
  300. return 1;
  301. }
  302. ReputationDatabase reputation_db;
  303. const std::string ReputationGroupDatabase::getDefaultLocation() {
  304. return std::string(db_path) + "/reputation_group.yml";
  305. }
  306. uint64 ReputationGroupDatabase::parseBodyNode(const ryml::NodeRef& node) {
  307. int64 id;
  308. if (!this->asInt64(node, "Id", id)) {
  309. return 0;
  310. }
  311. std::shared_ptr<s_reputationgroup> group = this->find(id);
  312. bool exists = group != nullptr;
  313. if (!exists) {
  314. if (!this->nodesExist(node, {"Name", "ReputeList"})) {
  315. return 0;
  316. }
  317. group = std::make_shared<s_reputationgroup>();
  318. group->id = id;
  319. }
  320. if (this->nodeExists(node, "Name")) {
  321. std::string name;
  322. if (!this->asString(node, "Name", name)) {
  323. return 0;
  324. }
  325. group->name = name;
  326. }
  327. if (this->nodeExists(node, "ScriptName")) {
  328. std::string script_name;
  329. if (!this->asString(node, "ScriptName", script_name)) {
  330. return 0;
  331. }
  332. group->script_name = script_name;
  333. }
  334. if (this->nodeExists(node, "ReputeList")) {
  335. const auto& reputelist = node[c4::to_csubstr("ReputeList")];
  336. for (const auto& repute : reputelist) {
  337. int64 repute_id;
  338. try {
  339. repute >> repute_id;
  340. } catch (std::runtime_error const&) {
  341. this->invalidWarning(node, "Value \"%s\" cannot be parsed as int64.\n", repute.val().str);
  342. continue;
  343. }
  344. if (!reputation_db.find(repute_id)) {
  345. this->invalidWarning(node, "Reputation id %lld does not exist in reputation_db!\n", repute_id);
  346. continue;
  347. }
  348. group->reputations.push_back(repute_id);
  349. }
  350. }
  351. if (!exists) {
  352. this->put(id, group);
  353. }
  354. return 1;
  355. }
  356. ReputationGroupDatabase reputationgroup_db;
  357. void pc_reputation_generate() {
  358. #ifdef MAP_GENERATOR
  359. const std::string filePrefix = "generated/clientside/data/contentdata/";
  360. auto reputeInfo = nlohmann::json::object();
  361. for (const auto& pair : reputation_db) {
  362. auto id = pair.first;
  363. auto rep = pair.second;
  364. nlohmann::json node;
  365. switch (rep->visibility) {
  366. case s_reputation::e_visibility::ALWAYS:
  367. node["Invisible"] = "VISIBLE_TRUE";
  368. break;
  369. case s_reputation::e_visibility::NEVER:
  370. node["Invisible"] = "VISIBLE_FALSE";
  371. break;
  372. case s_reputation::e_visibility::EXIST:
  373. node["Invisible"] = "VISIBLE_EXIST";
  374. break;
  375. }
  376. node["MaxPoint_Negative"] = std::abs(rep->minimum);
  377. node["MaxPoint_Positive"] = std::abs(rep->maximum);
  378. node["Name"] = rep->name;
  379. reputeInfo[std::to_string(id)] = node;
  380. }
  381. auto j = nlohmann::json::object();
  382. j["reputeInfo"] = reputeInfo;
  383. // std::cout << j.dump(2) << "\n";
  384. auto bson = nlohmann::json::to_bson(j);
  385. auto reputation_file = std::ofstream(filePrefix + "./reputeinfodata.bson", std::ios::binary);
  386. if (!reputation_file) {
  387. ShowError("Failed to create reputation file.\n");
  388. ShowError("Maybe the file directory \"%s\" does not exist?\n", filePrefix.c_str());
  389. ShowInfo("Create the directory and rerun map-server-generator\n");
  390. exit(1);
  391. }
  392. reputation_file.write((const char *)&bson[0], bson.size());
  393. auto reputeGroupInfo = nlohmann::json::object();
  394. for (const auto& pair : reputationgroup_db) {
  395. auto id = pair.first;
  396. auto group = pair.second;
  397. nlohmann::json node;
  398. node["ID"] = group->script_name;
  399. node["Name"] = group->name;
  400. node["ReputeList"] = group->reputations;
  401. reputeGroupInfo[std::to_string(id)] = node;
  402. }
  403. j = nlohmann::json::object();
  404. j["ReputeGroup"] = reputeGroupInfo;
  405. // std::cout << j.dump(2) << "\n";
  406. bson = nlohmann::json::to_bson(j);
  407. auto reputation_group_file = std::ofstream(filePrefix + "./reputegroupdata.bson", std::ios::binary);
  408. if (!reputation_group_file) {
  409. ShowError("Failed to create reputation group file.\n");
  410. ShowError("Maybe the file directory \"%s\" does not exist?\n", filePrefix.c_str());
  411. ShowInfo("Create the directory and rerun map-server-generator\n");
  412. exit(1);
  413. }
  414. reputation_group_file.write((const char *)&bson[0], bson.size());
  415. #endif
  416. }
  417. const std::string PenaltyDatabase::getDefaultLocation(){
  418. return std::string( db_path ) + "/level_penalty.yml";
  419. }
  420. uint64 PenaltyDatabase::parseBodyNode(const ryml::NodeRef& node){
  421. std::string type_constant;
  422. if( !this->asString( node, "Type", type_constant ) ){
  423. return 0;
  424. }
  425. int64 constant_value;
  426. if( !script_get_constant( ( "PENALTY_" + type_constant ).c_str(), &constant_value ) ){
  427. this->invalidWarning( node["Type"], "Unknown penalty type \"%s\".\n", type_constant.c_str() );
  428. return 0;
  429. }
  430. if( constant_value < PENALTY_NONE || constant_value > PENALTY_MAX ){
  431. this->invalidWarning( node["Type"], "Invalid penalty type \"%s\".\n", type_constant.c_str() );
  432. return 0;
  433. }
  434. e_penalty_type type = static_cast<e_penalty_type>( constant_value );
  435. std::shared_ptr<s_penalty> penalty = this->find( type );
  436. bool exists = penalty != nullptr;
  437. if( !exists ){
  438. penalty = std::make_shared<s_penalty>();
  439. penalty->type = type;
  440. for( int32 i = 0, max = ARRAYLENGTH( penalty->rate ); i < max; i++ ){
  441. penalty->rate[i] = UINT16_MAX;
  442. }
  443. }
  444. if( this->nodeExists( node, "LevelDifferences" ) ){
  445. for( const auto& levelNode : node["LevelDifferences"] ){
  446. if( !this->nodesExist( levelNode, { "Difference", "Rate" } ) ){
  447. return 0;
  448. }
  449. int32 difference;
  450. if( !this->asInt32( levelNode, "Difference", difference ) ){
  451. return 0;
  452. }
  453. if( std::abs( difference ) > MAX_LEVEL ){
  454. this->invalidWarning( levelNode["Difference"], "Level difference %d is bigger than maximum level %d.\n", difference, MAX_LEVEL );
  455. return 0;
  456. }
  457. uint16 rate;
  458. if( !this->asUInt16Rate( levelNode, "Rate", rate ) ){
  459. return 0;
  460. }
  461. penalty->rate[difference + MAX_LEVEL - 1] = rate;
  462. }
  463. }
  464. if( !exists ){
  465. this->put( type, penalty );
  466. }
  467. return 1;
  468. }
  469. void PenaltyDatabase::loadingFinished(){
  470. for( const auto& pair : *this ){
  471. for( int32 i = MAX_LEVEL - 1, max = ARRAYLENGTH( pair.second->rate ), last_rate = 100; i < max; i++ ){
  472. uint16 rate = pair.second->rate[i];
  473. // Check if it has been defined
  474. if( rate == UINT16_MAX ){
  475. pair.second->rate[i] = last_rate;
  476. }else{
  477. last_rate = rate;
  478. }
  479. }
  480. for( int32 i = MAX_LEVEL - 1, last_rate = 100; i >= 0; i-- ){
  481. uint16 rate = pair.second->rate[i];
  482. // Check if it has been defined
  483. if( rate == UINT16_MAX ){
  484. pair.second->rate[i] = last_rate;
  485. }else{
  486. last_rate = rate;
  487. }
  488. }
  489. }
  490. TypesafeYamlDatabase::loadingFinished();
  491. }
  492. PenaltyDatabase penalty_db;
  493. #define MOTD_LINE_SIZE 128
  494. static char motd_text[MOTD_LINE_SIZE][CHAT_SIZE_MAX]; // Message of the day buffer [Valaris]
  495. bool reg_load;
  496. /**
  497. * Translation table from athena equip index to aegis bitmask
  498. */
  499. uint32 equip_bitmask[EQI_MAX] = {
  500. EQP_ACC_L, // EQI_ACC_L
  501. EQP_ACC_R, // EQI_ACC_R
  502. EQP_SHOES, // EQI_SHOES
  503. EQP_GARMENT, // EQI_GARMENT
  504. EQP_HEAD_LOW, // EQI_HEAD_LOW
  505. EQP_HEAD_MID, // EQI_HEAD_MID
  506. EQP_HEAD_TOP, // EQI_HEAD_TOP
  507. EQP_ARMOR, // EQI_ARMOR
  508. EQP_HAND_L, // EQI_HAND_L
  509. EQP_HAND_R, // EQI_HAND_R
  510. EQP_COSTUME_HEAD_TOP, // EQI_COSTUME_HEAD_TOP
  511. EQP_COSTUME_HEAD_MID, // EQI_COSTUME_HEAD_MID
  512. EQP_COSTUME_HEAD_LOW, // EQI_COSTUME_HEAD_LOW
  513. EQP_COSTUME_GARMENT, // EQI_COSTUME_GARMENT
  514. EQP_AMMO, // EQI_AMMO
  515. EQP_SHADOW_ARMOR, // EQI_SHADOW_ARMOR
  516. EQP_SHADOW_WEAPON, // EQI_SHADOW_WEAPON
  517. EQP_SHADOW_SHIELD, // EQI_SHADOW_SHIELD
  518. EQP_SHADOW_SHOES, // EQI_SHADOW_SHOES
  519. EQP_SHADOW_ACC_R, // EQI_SHADOW_ACC_R
  520. EQP_SHADOW_ACC_L // EQI_SHADOW_ACC_L
  521. };
  522. //Links related info to the sd->hate_mob[]/sd->feel_map[] entries
  523. const struct sg_data sg_info[MAX_PC_FEELHATE] = {
  524. { SG_SUN_ANGER, SG_SUN_BLESS, SG_SUN_COMFORT, "PC_FEEL_SUN", "PC_HATE_MOB_SUN", is_day_of_sun },
  525. { SG_MOON_ANGER, SG_MOON_BLESS, SG_MOON_COMFORT, "PC_FEEL_MOON", "PC_HATE_MOB_MOON", is_day_of_moon },
  526. { SG_STAR_ANGER, SG_STAR_BLESS, SG_STAR_COMFORT, "PC_FEEL_STAR", "PC_HATE_MOB_STAR", is_day_of_star }
  527. };
  528. void pc_set_reg_load( bool val ){
  529. reg_load = val;
  530. }
  531. /**
  532. * Item Cool Down Delay Saving
  533. * Struct item_cd is not a member of map_session_data
  534. * to keep cooldowns in memory between player log-ins.
  535. * All cooldowns are reset when server is restarted.
  536. **/
  537. DBMap* itemcd_db = nullptr; // char_id -> struct item_cd
  538. struct item_cd {
  539. t_tick tick[MAX_ITEMDELAYS]; //tick
  540. t_itemid nameid[MAX_ITEMDELAYS]; //item id
  541. };
  542. /**
  543. * Converts a class to its array index for CLASS_COUNT defined arrays.
  544. * Note that it does not do a validity check for speed purposes, where parsing
  545. * player input make sure to use a pcdb_checkid first!
  546. * @param class_ Job ID see enum e_job
  547. * @return Class Index
  548. */
  549. int32 pc_class2idx(int32 class_) {
  550. if (class_ >= JOB_NOVICE_HIGH)
  551. return class_- JOB_NOVICE_HIGH+JOB_MAX_BASIC;
  552. return class_;
  553. }
  554. /**
  555. * Get player's group ID
  556. * @param sd
  557. * @return Group ID
  558. */
  559. int32 pc_get_group_id(map_session_data *sd) {
  560. return sd->group_id;
  561. }
  562. /** Get player's group Level
  563. * @param sd
  564. * @return Group Level
  565. */
  566. int32 pc_get_group_level(map_session_data *sd) {
  567. return sd->group->level;
  568. }
  569. /**
  570. * Remove a player from queue after timeout
  571. * @param tid: Timer ID
  572. * @param tick: Timer
  573. * @param id: ID
  574. * @return 0 on success or 1 otherwise
  575. */
  576. static TIMER_FUNC(pc_on_expire_active)
  577. {
  578. map_session_data *sd = (map_session_data *)data;
  579. nullpo_retr(1, sd);
  580. sd->tid_queue_active = INVALID_TIMER;
  581. bg_queue_leave(sd);
  582. clif_bg_queue_entry_init(sd);
  583. return 0;
  584. }
  585. /**
  586. * Function used to set timer externally
  587. * @param sd: Player data
  588. */
  589. void pc_set_bg_queue_timer(map_session_data *sd) {
  590. nullpo_retv(sd);
  591. if (sd->tid_queue_active != INVALID_TIMER) {
  592. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  593. sd->tid_queue_active = INVALID_TIMER;
  594. }
  595. sd->tid_queue_active = add_timer(gettick() + 20000, pc_on_expire_active, 0, (intptr_t)sd);
  596. }
  597. /**
  598. * Function used to delete timer externally
  599. * @param sd: Player data
  600. */
  601. void pc_delete_bg_queue_timer(map_session_data *sd) {
  602. nullpo_retv(sd);
  603. if (sd->tid_queue_active != INVALID_TIMER) {
  604. delete_timer(sd->tid_queue_active, pc_on_expire_active);
  605. sd->tid_queue_active = INVALID_TIMER;
  606. }
  607. }
  608. static TIMER_FUNC(pc_invincible_timer){
  609. map_session_data *sd;
  610. if( (sd=(map_session_data *)map_id2sd(id)) == nullptr || sd->bl.type!=BL_PC )
  611. return 1;
  612. if(sd->invincible_timer != tid){
  613. ShowError("invincible_timer %d != %d\n",sd->invincible_timer,tid);
  614. return 0;
  615. }
  616. sd->invincible_timer = INVALID_TIMER;
  617. skill_unit_move(&sd->bl,tick,1);
  618. return 0;
  619. }
  620. void pc_setinvincibletimer(map_session_data* sd, int32 val) {
  621. nullpo_retv(sd);
  622. if( sd->invincible_timer != INVALID_TIMER )
  623. delete_timer(sd->invincible_timer,pc_invincible_timer);
  624. sd->invincible_timer = add_timer(gettick()+val,pc_invincible_timer,sd->bl.id,0);
  625. }
  626. void pc_delinvincibletimer(map_session_data* sd)
  627. {
  628. nullpo_retv(sd);
  629. if( sd->invincible_timer != INVALID_TIMER )
  630. {
  631. delete_timer(sd->invincible_timer,pc_invincible_timer);
  632. sd->invincible_timer = INVALID_TIMER;
  633. skill_unit_move(&sd->bl,gettick(),1);
  634. }
  635. }
  636. static TIMER_FUNC(pc_spiritball_timer){
  637. map_session_data *sd;
  638. int32 i;
  639. if( (sd=(map_session_data *)map_id2sd(id)) == nullptr || sd->bl.type!=BL_PC )
  640. return 1;
  641. if( sd->spiritball <= 0 )
  642. {
  643. ShowError("pc_spiritball_timer: %d spiritball's available. (aid=%d cid=%d tid=%d)\n", sd->spiritball, sd->status.account_id, sd->status.char_id, tid);
  644. sd->spiritball = 0;
  645. return 0;
  646. }
  647. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == tid);
  648. if( i == sd->spiritball )
  649. {
  650. ShowError("pc_spiritball_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  651. return 0;
  652. }
  653. sd->spiritball--;
  654. if( i != sd->spiritball )
  655. memmove(sd->spirit_timer+i, sd->spirit_timer+i+1, (sd->spiritball-i)*sizeof(int32));
  656. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  657. clif_spiritball(&sd->bl);
  658. return 0;
  659. }
  660. /**
  661. * Adds a spiritball to player for 'interval' ms
  662. * @param sd
  663. * @param interval
  664. * @param max
  665. */
  666. void pc_addspiritball(map_session_data *sd,int32 interval,int32 max)
  667. {
  668. int32 tid;
  669. uint8 i;
  670. nullpo_retv(sd);
  671. if(max > MAX_SPIRITBALL)
  672. max = MAX_SPIRITBALL;
  673. if(sd->spiritball < 0)
  674. sd->spiritball = 0;
  675. if( sd->spiritball && sd->spiritball >= max )
  676. {
  677. if(sd->spirit_timer[0] != INVALID_TIMER)
  678. delete_timer(sd->spirit_timer[0],pc_spiritball_timer);
  679. sd->spiritball--;
  680. if( sd->spiritball != 0 )
  681. memmove(sd->spirit_timer+0, sd->spirit_timer+1, (sd->spiritball)*sizeof(int32));
  682. sd->spirit_timer[sd->spiritball] = INVALID_TIMER;
  683. }
  684. tid = add_timer(gettick()+interval, pc_spiritball_timer, sd->bl.id, 0);
  685. ARR_FIND(0, sd->spiritball, i, sd->spirit_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spirit_timer[i])->tick) < 0);
  686. if( i != sd->spiritball )
  687. memmove(sd->spirit_timer+i+1, sd->spirit_timer+i, (sd->spiritball-i)*sizeof(int32));
  688. sd->spirit_timer[i] = tid;
  689. sd->spiritball++;
  690. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  691. clif_millenniumshield( sd->bl, sd->spiritball );
  692. else
  693. clif_spiritball(&sd->bl);
  694. }
  695. /**
  696. * Removes number of spiritball from player
  697. * @param sd
  698. * @param count
  699. * @param type 1 = doesn't give client effect
  700. */
  701. void pc_delspiritball(map_session_data *sd,int32 count,int32 type)
  702. {
  703. uint8 i;
  704. nullpo_retv(sd);
  705. if(sd->spiritball <= 0) {
  706. sd->spiritball = 0;
  707. return;
  708. }
  709. if(count == 0)
  710. return;
  711. if(count > sd->spiritball)
  712. count = sd->spiritball;
  713. sd->spiritball -= count;
  714. if(count > MAX_SPIRITBALL)
  715. count = MAX_SPIRITBALL;
  716. for(i=0;i<count;i++) {
  717. if(sd->spirit_timer[i] != INVALID_TIMER) {
  718. delete_timer(sd->spirit_timer[i],pc_spiritball_timer);
  719. sd->spirit_timer[i] = INVALID_TIMER;
  720. }
  721. }
  722. for(i=count;i<MAX_SPIRITBALL;i++) {
  723. sd->spirit_timer[i-count] = sd->spirit_timer[i];
  724. sd->spirit_timer[i] = INVALID_TIMER;
  725. }
  726. if(!type) {
  727. if( (sd->class_&MAPID_THIRDMASK) == MAPID_ROYAL_GUARD )
  728. clif_millenniumshield( sd->bl, sd->spiritball );
  729. else
  730. clif_spiritball(&sd->bl);
  731. }
  732. }
  733. /**
  734. * Adds a soulball to player
  735. * @param sd: Player data
  736. * @param number: Amount to give
  737. * @param max: Max amount of soulballs
  738. */
  739. void pc_addsoulball( map_session_data& sd, int32 number, int32 max ) {
  740. status_change *sc = status_get_sc(&sd.bl);
  741. // Save previous soulball number
  742. int32 val = sd.soulball;
  743. if (sc == nullptr || sc->getSCE(SC_SOULENERGY) == nullptr)
  744. sd.soulball = 0;
  745. max = min(max, MAX_SOUL_BALL);
  746. sd.soulball += number;
  747. sd.soulball = cap_value(sd.soulball, 0, max);
  748. // Refresh the SC_SOULENERGY timer (which is 10 minutes by default) if the number has changed. The soul is removed when the timer expire.
  749. if (sd.soulball != val)
  750. sc_start( &sd.bl, &sd.bl, SC_SOULENERGY, 100, sd.soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  751. clif_soulball(&sd);
  752. }
  753. /**
  754. * Removes number of soulball from player
  755. * @param sd: Player data
  756. * @param count: Amount to remove
  757. * @param no_client_effect: true = doesn't give client effect
  758. */
  759. void pc_delsoulball( map_session_data& sd, int32 count, bool no_client_effect ) {
  760. if (count <= 0)
  761. return;
  762. status_change *sc = status_get_sc(&sd.bl);
  763. if (sd.soulball <= 0)
  764. sd.soulball = 0;
  765. else {
  766. sd.soulball -= cap_value(count, 0, sd.soulball);
  767. if (sd.soulball == 0)
  768. status_change_end(&sd.bl, SC_SOULENERGY);
  769. else
  770. sc_start( &sd.bl, &sd.bl, SC_SOULENERGY, 100, sd.soulball, skill_get_time2(SP_SOULCOLLECT, 1)); // refresh val1 and status MATK
  771. }
  772. if (!no_client_effect)
  773. clif_soulball(&sd);
  774. }
  775. /**
  776. * Adds servantballs to a player
  777. * @param sd: Player data
  778. * @param amount: Amount to add
  779. */
  780. void pc_addservantball( map_session_data& sd, int32 count ){
  781. sd.servantball = cap_value( sd.servantball + count, 0, MAX_SERVANTBALL );
  782. clif_servantball( sd );
  783. }
  784. /**
  785. * Removes number of servantballs from player
  786. * @param sd: Player data
  787. * @param count: Amount to remove
  788. */
  789. void pc_delservantball( map_session_data& sd, int32 count ){
  790. sd.servantball = cap_value( sd.servantball - count, 0, MAX_SERVANTBALL );
  791. clif_servantball( sd );
  792. }
  793. /**
  794. * Adds abyssballs to a player
  795. * @param sd: Player data
  796. * @param amount: Amount to add
  797. */
  798. void pc_addabyssball( map_session_data& sd, int32 count ){
  799. sd.abyssball = cap_value( sd.abyssball + count, 0, MAX_ABYSSBALL );
  800. clif_abyssball( sd );
  801. }
  802. /**
  803. * Removes number of abyssballs from player
  804. * @param sd: Player data
  805. * @param count: Amount to remove
  806. */
  807. void pc_delabyssball( map_session_data& sd, int32 count ){
  808. sd.abyssball = cap_value( sd.abyssball - count, 0, MAX_ABYSSBALL );
  809. clif_abyssball( sd );
  810. }
  811. /**
  812. * Increases a player's fame points and displays a notice to him
  813. * @param sd Player
  814. * @param count Fame point
  815. * @return true: on success, false: on error
  816. */
  817. bool pc_addfame(map_session_data &sd, int32 count)
  818. {
  819. enum e_rank ranktype;
  820. switch(sd.class_&MAPID_UPPERMASK){
  821. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  822. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  823. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  824. default:
  825. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd.status.class_), sd.status.class_ );
  826. return false;
  827. }
  828. sd.status.fame = cap_value( sd.status.fame + count, 0, MAX_FAME );
  829. clif_update_rankingpoint(sd, ranktype, count);
  830. chrif_updatefamelist(sd, ranktype);
  831. return true;
  832. }
  833. /**
  834. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  835. * @param sd
  836. * @param job Job use enum e_mapid
  837. * @return Rank
  838. */
  839. unsigned char pc_famerank(uint32 char_id, int32 job)
  840. {
  841. uint8 i;
  842. switch(job){
  843. case MAPID_BLACKSMITH: // Blacksmith
  844. for(i = 0; i < MAX_FAME_LIST; i++){
  845. if(smith_fame_list[i].id == char_id)
  846. return i + 1;
  847. }
  848. break;
  849. case MAPID_ALCHEMIST: // Alchemist
  850. for(i = 0; i < MAX_FAME_LIST; i++){
  851. if(chemist_fame_list[i].id == char_id)
  852. return i + 1;
  853. }
  854. break;
  855. case MAPID_TAEKWON: // Taekwon
  856. for(i = 0; i < MAX_FAME_LIST; i++){
  857. if(taekwon_fame_list[i].id == char_id)
  858. return i + 1;
  859. }
  860. break;
  861. }
  862. return 0;
  863. }
  864. /**
  865. * Restart player's HP & SP value
  866. * @param sd
  867. * @param type Restart type: 1 - Normal Resurection
  868. */
  869. void pc_setrestartvalue(map_session_data *sd, char type) {
  870. struct status_data *status, *b_status;
  871. nullpo_retv(sd);
  872. b_status = &sd->base_status;
  873. status = &sd->battle_status;
  874. if (type&1) { //Normal resurrection
  875. status->hp = 1; //Otherwise status_heal may fail if dead.
  876. status_heal(&sd->bl, b_status->hp, 0, 1);
  877. if( status->sp < b_status->sp )
  878. status_set_sp(&sd->bl, b_status->sp, 1);
  879. } else { //Just for saving on the char-server (with values as if respawned)
  880. sd->status.hp = b_status->hp;
  881. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  882. sd->status.ap = (status->ap < b_status->ap)?b_status->ap:status->ap;
  883. }
  884. }
  885. /*==========================================
  886. Rental System
  887. *------------------------------------------*/
  888. /**
  889. * Ends a rental and removes the item/effect
  890. * @param tid: Tick ID
  891. * @param tick: Timer
  892. * @param id: Timer ID
  893. * @param data: Data
  894. * @return false - failure, true - success
  895. */
  896. TIMER_FUNC(pc_inventory_rental_end){
  897. map_session_data *sd = map_id2sd(id);
  898. if( sd == nullptr )
  899. return 0;
  900. if( tid != sd->rental_timer ) {
  901. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  902. return 0;
  903. }
  904. pc_inventory_rentals(sd);
  905. return 1;
  906. }
  907. /**
  908. * Removes the rental timer from the player
  909. * @param sd: Player data
  910. */
  911. void pc_inventory_rental_clear(map_session_data *sd)
  912. {
  913. if( sd->rental_timer != INVALID_TIMER ) {
  914. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  915. sd->rental_timer = INVALID_TIMER;
  916. }
  917. }
  918. /**
  919. * Check for items in the player's inventory that are rental type
  920. * @param sd: Player data
  921. */
  922. void pc_inventory_rentals(map_session_data *sd)
  923. {
  924. int32 i, c = 0;
  925. uint32 next_tick = UINT_MAX;
  926. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  927. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  928. continue; // Nothing here
  929. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  930. continue;
  931. if( sd->inventory.u.items_inventory[i].expire_time <= time(nullptr) ) {
  932. if (sd->inventory_data[i]->unequip_script)
  933. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  934. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  935. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  936. } else {
  937. uint32 expire_tick = (uint32)(sd->inventory.u.items_inventory[i].expire_time - time(nullptr));
  938. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int32)expire_tick);
  939. next_tick = umin(expire_tick * 1000U, next_tick);
  940. c++;
  941. }
  942. }
  943. if( c > 0 ) // min(next_tick,3600000) 1 hour each timer to keep announcing to the owner, and to avoid a but with rental time > 15 days
  944. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  945. else
  946. sd->rental_timer = INVALID_TIMER;
  947. }
  948. /**
  949. * Add a rental item to the player and adjusts the rental timer appropriately
  950. * @param sd: Player data
  951. * @param seconds: Rental time
  952. */
  953. void pc_inventory_rental_add(map_session_data *sd, uint32 seconds)
  954. {
  955. t_tick tick = seconds * 1000;
  956. if( sd == nullptr )
  957. return;
  958. if( sd->rental_timer != INVALID_TIMER ) {
  959. const struct TimerData * td;
  960. td = get_timer(sd->rental_timer);
  961. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  962. pc_inventory_rental_clear(sd);
  963. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  964. }
  965. } else
  966. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  967. }
  968. /**
  969. * Check if the player can sell the current item
  970. * @param sd: map_session_data of the player
  971. * @param item: struct of the checking item
  972. * @param shoptype: NPC's sub type see enum npc_subtype
  973. * @return bool 'true' is sellable, 'false' otherwise
  974. */
  975. bool pc_can_sell_item(map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  976. if (sd == nullptr || item == nullptr)
  977. return false;
  978. if (!pc_can_give_items(sd))
  979. return false;
  980. if (item->equip > 0 || item->amount < 0)
  981. return false;
  982. if (battle_config.hide_fav_sell && item->favorite != 0)
  983. return false; //Cannot sell favs (optional config)
  984. if (!battle_config.rental_transaction && item->expire_time)
  985. return false; // Cannot Sell Rental Items
  986. if( item->equipSwitch ){
  987. return false;
  988. }
  989. if (itemdb_ishatched_egg(item))
  990. return false;
  991. switch (shoptype) {
  992. case NPCTYPE_SHOP:
  993. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  994. item->bound != BOUND_GUILD ||
  995. (sd->guild && sd->status.char_id == sd->guild->guild.member[0].char_id) ||
  996. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  997. ))
  998. return true;
  999. break;
  1000. case NPCTYPE_ITEMSHOP:
  1001. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  1002. item->bound != BOUND_GUILD ||
  1003. (sd->guild && sd->status.char_id == sd->guild->guild.member[0].char_id) ||
  1004. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1005. ))
  1006. return true;
  1007. else if (!item->bound) {
  1008. struct item_data *itd = itemdb_search(item->nameid);
  1009. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  1010. return true;
  1011. }
  1012. break;
  1013. }
  1014. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  1015. return false;
  1016. if (item->bound && !pc_can_give_bounded_items(sd))
  1017. return false; // Don't allow sale of bound items
  1018. return true;
  1019. }
  1020. /**
  1021. * Determines if player can give / drop / trade / vend items
  1022. */
  1023. bool pc_can_give_items(map_session_data *sd)
  1024. {
  1025. return (pc_has_permission(sd, PC_PERM_TRADE) || pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL));
  1026. }
  1027. /**
  1028. * Determines if player can give / drop / trade / vend bounded items
  1029. */
  1030. bool pc_can_give_bounded_items(map_session_data *sd)
  1031. {
  1032. return (pc_has_permission(sd, PC_PERM_TRADE_BOUNDED) || pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL));
  1033. }
  1034. /**
  1035. * Determine if an item in a player's inventory is tradeable based on several merits.
  1036. * Checks for item_trade, bound, and rental restrictions.
  1037. * @param sd: Player data
  1038. * @param index: Item inventory index
  1039. * @return True if the item can be traded or false otherwise
  1040. */
  1041. bool pc_can_trade_item(map_session_data *sd, int32 index) {
  1042. if (sd && index >= 0) {
  1043. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  1044. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  1045. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  1046. );
  1047. }
  1048. return false;
  1049. }
  1050. /*==========================================
  1051. * Prepares character for saving.
  1052. * @param sd
  1053. *------------------------------------------*/
  1054. void pc_makesavestatus(map_session_data *sd) {
  1055. nullpo_retv(sd);
  1056. if(!battle_config.save_clothcolor)
  1057. sd->status.clothes_color = 0;
  1058. if(!battle_config.save_body_style)
  1059. sd->status.body = 0;
  1060. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  1061. //status change load/saving. [Skotlex]
  1062. #ifdef NEW_CARTS
  1063. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1064. #else
  1065. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1066. #endif
  1067. // Mark last point as not instance related by default
  1068. sd->status.last_point_instanceid = 0;
  1069. if (sd->sc.getSCE(SC_JAILED)) { //When Jailed, do not move last point.
  1070. if(pc_isdead(sd)){
  1071. pc_setrestartvalue(sd, 0);
  1072. } else {
  1073. sd->status.hp = sd->battle_status.hp;
  1074. sd->status.sp = sd->battle_status.sp;
  1075. sd->status.ap = sd->battle_status.ap;
  1076. }
  1077. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1078. sd->status.last_point.x = sd->bl.x;
  1079. sd->status.last_point.y = sd->bl.y;
  1080. return;
  1081. }
  1082. if( pc_isdead( sd ) ){
  1083. pc_setrestartvalue( sd, 0 );
  1084. // Return to save point
  1085. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1086. sd->status.last_point.x = sd->status.save_point.x;
  1087. sd->status.last_point.y = sd->status.save_point.y;
  1088. }else{
  1089. sd->status.hp = sd->battle_status.hp;
  1090. sd->status.sp = sd->battle_status.sp;
  1091. sd->status.ap = sd->battle_status.ap;
  1092. struct map_data* mapdata = map_getmapdata( sd->bl.m );
  1093. // If saving is not allowed on the map, we return the player to the designated point
  1094. if( mapdata->getMapFlag(MF_NOSAVE) ){
  1095. // The map has a specific return point
  1096. if( mapdata->save.map ){
  1097. safestrncpy( sd->status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd->status.last_point.map ) );
  1098. sd->status.last_point.x = mapdata->save.x;
  1099. sd->status.last_point.y = mapdata->save.y;
  1100. // Return the user to his save point
  1101. }else{
  1102. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1103. sd->status.last_point.x = sd->status.save_point.x;
  1104. sd->status.last_point.y = sd->status.save_point.y;
  1105. }
  1106. // If the user is on a instance map, special handling is needed
  1107. }else if( mapdata->instance_id ){
  1108. if( battle_config.instance_allow_reconnect ){
  1109. // Store the original mapname
  1110. struct map_data* mapdata_source = map_getmapdata( mapdata->instance_src_map );
  1111. mapindex_getmapname( mapindex_id2name( mapdata_source->index ), sd->status.last_point.map );
  1112. sd->status.last_point.x = sd->bl.x;
  1113. sd->status.last_point.y = sd->bl.y;
  1114. sd->status.last_point_instanceid = mapdata->instance_id;
  1115. }else{
  1116. // Return the user to his save point
  1117. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1118. sd->status.last_point.x = sd->status.save_point.x;
  1119. sd->status.last_point.y = sd->status.save_point.y;
  1120. }
  1121. }else{
  1122. // Save normally
  1123. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1124. sd->status.last_point.x = sd->bl.x;
  1125. sd->status.last_point.y = sd->bl.y;
  1126. }
  1127. }
  1128. }
  1129. /*==========================================
  1130. * Off init ? Connection?
  1131. *------------------------------------------*/
  1132. void pc_setnewpc(map_session_data *sd, uint32 account_id, uint32 char_id, int32 login_id1, t_tick client_tick, int32 sex, int32 fd) {
  1133. nullpo_retv(sd);
  1134. sd->bl.id = account_id;
  1135. sd->status.account_id = account_id;
  1136. sd->status.char_id = char_id;
  1137. sd->status.sex = sex;
  1138. sd->login_id1 = login_id1;
  1139. sd->login_id2 = 0; // at this point, we can not know the value :(
  1140. sd->client_tick = client_tick;
  1141. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  1142. sd->bl.type = BL_PC;
  1143. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  1144. sd->canlog_tick = gettick();
  1145. //Required to prevent homunculus copuing a base speed of 0.
  1146. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  1147. }
  1148. /**
  1149. * Get equip point for an equip
  1150. * @param sd
  1151. * @param id
  1152. */
  1153. int32 pc_equippoint_sub(map_session_data *sd,struct item_data* id){
  1154. int32 ep = 0;
  1155. nullpo_ret(sd);
  1156. nullpo_ret(id);
  1157. if (!itemdb_isequip2(id))
  1158. return 0; //Not equippable by players.
  1159. ep = id->equip;
  1160. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  1161. if(pc_checkskill(sd,AS_LEFT) > 0 || (sd->class_&MAPID_UPPERMASK) == MAPID_ASSASSIN || (sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO) { //Kagerou and Oboro can dual wield daggers. [Rytech]
  1162. if (ep == EQP_WEAPON)
  1163. return EQP_ARMS;
  1164. if (ep == EQP_SHADOW_WEAPON)
  1165. return EQP_SHADOW_ARMS;
  1166. }
  1167. }
  1168. return ep;
  1169. }
  1170. /**
  1171. * Get equip point for an equip
  1172. * @param sd
  1173. * @param n Equip index in inventory
  1174. */
  1175. int32 pc_equippoint(map_session_data *sd,int32 n){
  1176. nullpo_ret(sd);
  1177. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  1178. }
  1179. /**
  1180. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  1181. * @param sd : player session
  1182. * @return 0 sucess, 1:invalid sd
  1183. */
  1184. void pc_setinventorydata(map_session_data *sd)
  1185. {
  1186. uint8 i;
  1187. nullpo_retv(sd);
  1188. for(i = 0; i < MAX_INVENTORY; i++) {
  1189. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  1190. sd->inventory_data[i] = id?itemdb_search(id):nullptr;
  1191. }
  1192. }
  1193. /**
  1194. * 'Calculates' weapon type
  1195. * @param sd : Player
  1196. */
  1197. void pc_calcweapontype(map_session_data *sd)
  1198. {
  1199. nullpo_retv(sd);
  1200. // single-hand
  1201. if(sd->weapontype2 == W_FIST) {
  1202. sd->status.weapon = sd->weapontype1;
  1203. return;
  1204. }
  1205. if(sd->weapontype1 == W_FIST) {
  1206. sd->status.weapon = sd->weapontype2;
  1207. return;
  1208. }
  1209. // dual-wield
  1210. sd->status.weapon = W_FIST;
  1211. switch (sd->weapontype1){
  1212. case W_DAGGER:
  1213. switch (sd->weapontype2) {
  1214. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  1215. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  1216. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  1217. }
  1218. break;
  1219. case W_1HSWORD:
  1220. switch (sd->weapontype2) {
  1221. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  1222. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  1223. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  1224. }
  1225. break;
  1226. case W_1HAXE:
  1227. switch (sd->weapontype2) {
  1228. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  1229. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  1230. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  1231. }
  1232. }
  1233. // unknown, default to right hand type
  1234. if (!sd->status.weapon)
  1235. sd->status.weapon = sd->weapontype1;
  1236. }
  1237. /**
  1238. * Set equip index
  1239. * @param sd : Player
  1240. */
  1241. void pc_setequipindex(map_session_data *sd)
  1242. {
  1243. uint16 i;
  1244. nullpo_retv(sd);
  1245. for (i = 0; i < EQI_MAX; i++){
  1246. sd->equip_index[i] = -1;
  1247. sd->equip_switch_index[i] = -1;
  1248. }
  1249. for (i = 0; i < MAX_INVENTORY; i++) {
  1250. if (sd->inventory.u.items_inventory[i].nameid == 0)
  1251. continue;
  1252. if (sd->inventory.u.items_inventory[i].equip) {
  1253. uint8 j;
  1254. for (j = 0; j < EQI_MAX; j++)
  1255. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  1256. sd->equip_index[j] = i;
  1257. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  1258. if (sd->inventory_data[i])
  1259. sd->weapontype1 = sd->inventory_data[i]->subtype;
  1260. else
  1261. sd->weapontype1 = W_FIST;
  1262. }
  1263. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  1264. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  1265. sd->weapontype2 = sd->inventory_data[i]->subtype;
  1266. else
  1267. sd->weapontype2 = W_FIST;
  1268. }
  1269. }
  1270. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  1271. for (uint8 j = 0; j < EQI_MAX; j++) {
  1272. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1273. sd->equip_switch_index[j] = i;
  1274. }
  1275. }
  1276. }
  1277. }
  1278. pc_calcweapontype(sd);
  1279. }
  1280. //static int32 pc_isAllowedCardOn(map_session_data *sd,int32 s,int32 eqindex,int32 flag)
  1281. //{
  1282. // int32 i;
  1283. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1284. // std::shared_ptr<item_data> data;
  1285. //
  1286. // //Crafted/made/hatched items.
  1287. // if (itemdb_isspecial(item->card[0]))
  1288. // return 1;
  1289. //
  1290. // /* scan for enchant armor gems */
  1291. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1292. // s = MAX_SLOTS - 1;
  1293. //
  1294. // ARR_FIND( 0, s, i, item->card[i] && (data = item_db.find(item->card[i])) != nullptr && data->flag.no_equip&flag );
  1295. // return( i < s ) ? 0 : 1;
  1296. //}
  1297. /**
  1298. * Check if an item is equiped by player
  1299. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1300. * @param sd : player session
  1301. * @param nameid : itemid
  1302. * @return 1:yes, 0:no
  1303. */
  1304. bool pc_isequipped(map_session_data *sd, t_itemid nameid)
  1305. {
  1306. uint8 i;
  1307. for( i = 0; i < EQI_MAX; i++ )
  1308. {
  1309. short index = sd->equip_index[i], j;
  1310. if( index < 0 )
  1311. continue;
  1312. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1313. continue;
  1314. if( !sd->inventory_data[index] )
  1315. continue;
  1316. if( sd->inventory_data[index]->nameid == nameid )
  1317. return true;
  1318. for( j = 0; j < MAX_SLOTS; j++ ){
  1319. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1320. return true;
  1321. }
  1322. }
  1323. return false;
  1324. }
  1325. /**
  1326. * Check adoption rules
  1327. * @param p1_sd: Player 1
  1328. * @param p2_sd: Player 2
  1329. * @param b_sd: Player that will be adopted
  1330. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1331. * ADOPT_ALREADY_ADOPTED - Already adopted
  1332. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1333. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1334. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1335. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1336. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1337. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1338. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1339. */
  1340. enum adopt_responses pc_try_adopt(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1341. {
  1342. if( !p1_sd || !p2_sd || !b_sd )
  1343. return ADOPT_CHARACTER_NOT_FOUND;
  1344. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1345. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1346. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1347. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1348. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1349. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1350. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1351. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1352. // Parents need to have their ring equipped
  1353. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1354. return ADOPT_EQUIP_RINGS;
  1355. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1356. return ADOPT_EQUIP_RINGS;
  1357. // Already adopted a baby
  1358. if( p1_sd->status.child || p2_sd->status.child ) {
  1359. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1360. return ADOPT_MORE_CHILDREN;
  1361. }
  1362. // Parents need at least lvl 70 to adopt
  1363. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1364. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1365. return ADOPT_LEVEL_70;
  1366. }
  1367. if( b_sd->status.partner_id ) {
  1368. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1369. return ADOPT_MARRIED;
  1370. }
  1371. if( !( ( b_sd->status.class_ >= JOB_NOVICE && b_sd->status.class_ <= JOB_THIEF ) || b_sd->status.class_ == JOB_SUPER_NOVICE || b_sd->status.class_ == JOB_SUPER_NOVICE_E ) )
  1372. return ADOPT_NOT_NOVICE;
  1373. return ADOPT_ALLOWED;
  1374. }
  1375. /*==========================================
  1376. * Adoption Process
  1377. *------------------------------------------*/
  1378. bool pc_adoption(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1379. {
  1380. int32 job, joblevel;
  1381. t_exp jobexp;
  1382. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1383. return false;
  1384. // Preserve current job levels and progress
  1385. joblevel = b_sd->status.job_level;
  1386. jobexp = b_sd->status.job_exp;
  1387. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1388. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1389. { // Success, proceed to configure parents and baby skills
  1390. p1_sd->status.child = b_sd->status.char_id;
  1391. p2_sd->status.child = b_sd->status.char_id;
  1392. b_sd->status.father = p1_sd->status.char_id;
  1393. b_sd->status.mother = p2_sd->status.char_id;
  1394. // Restore progress
  1395. b_sd->status.job_level = joblevel;
  1396. clif_updatestatus(*b_sd, SP_JOBLEVEL);
  1397. b_sd->status.job_exp = jobexp;
  1398. clif_updatestatus(*b_sd, SP_JOBEXP);
  1399. // Baby Skills
  1400. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1401. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1402. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1403. // Parents Skills
  1404. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1405. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1406. chrif_save(p1_sd, CSAVE_NORMAL);
  1407. chrif_save(p2_sd, CSAVE_NORMAL);
  1408. chrif_save(b_sd, CSAVE_NORMAL);
  1409. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1410. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1411. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1412. return true;
  1413. }
  1414. return false; // Job Change Fail
  1415. }
  1416. static bool pc_job_can_use_item( map_session_data* sd, struct item_data* item ){
  1417. nullpo_retr( false, sd );
  1418. nullpo_retr( false, item );
  1419. // Calculate the required bit to check
  1420. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1421. size_t index;
  1422. // 2-1
  1423. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1424. index = 1;
  1425. // 2-2
  1426. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1427. index = 2;
  1428. // Basejob
  1429. }else{
  1430. index = 0;
  1431. }
  1432. return ( item->class_base[index] & job ) != 0;
  1433. }
  1434. /*==========================================
  1435. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1436. Returns:
  1437. false : Cannot use/equip
  1438. true : Can use/equip
  1439. * Credits:
  1440. [Inkfish] for first idea
  1441. [Haru] for third-classes extension
  1442. [Cydh] finishing :D
  1443. *------------------------------------------*/
  1444. static bool pc_isItemClass (map_session_data *sd, struct item_data* item) {
  1445. while (1) {
  1446. if (item->class_upper&ITEMJ_NORMAL && !(sd->class_&(JOBL_UPPER|JOBL_BABY|JOBL_THIRD|JOBL_FOURTH))) //normal classes (no upper, no baby, no third, no fourth)
  1447. break;
  1448. #ifndef RENEWAL
  1449. //allow third classes to use trans. class items
  1450. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1451. break;
  1452. //third-baby classes can use same item too
  1453. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1454. break;
  1455. //don't need to decide specific rules for third-classes?
  1456. //items for third classes can be used for all third classes
  1457. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1458. break;
  1459. #else
  1460. //trans. classes (exl. third-trans.)
  1461. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1462. break;
  1463. //baby classes (exl. third-baby)
  1464. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1465. break;
  1466. //third classes (exl. third-trans. and baby-third and fourth)
  1467. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) && !(sd->class_&JOBL_FOURTH))
  1468. break;
  1469. //trans-third classes (exl. fourth)
  1470. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_FOURTH))
  1471. break;
  1472. //third-baby classes (exl. fourth)
  1473. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY && !(sd->class_&JOBL_FOURTH))
  1474. break;
  1475. //fourth classes
  1476. if (item->class_upper&ITEMJ_FOURTH && sd->class_&JOBL_FOURTH)
  1477. break;
  1478. #endif
  1479. return false;
  1480. }
  1481. return true;
  1482. }
  1483. /*=================================================
  1484. * Checks if the player can equip the item at index n in inventory.
  1485. * @param sd
  1486. * @param n Item index in inventory
  1487. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1488. *------------------------------------------------*/
  1489. uint8 pc_isequip(map_session_data *sd,int32 n)
  1490. {
  1491. struct item_data *item;
  1492. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1493. item = sd->inventory_data[n];
  1494. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1495. return ITEM_EQUIP_ACK_OK;
  1496. if(item == nullptr)
  1497. return ITEM_EQUIP_ACK_FAIL;
  1498. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_SUPERNOVICE) {
  1499. //Spirit of Super Novice equip bonuses. [Skotlex]
  1500. if (sd->status.base_level >= 90 && item->equip & EQP_HELM)
  1501. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1502. if (sd->status.base_level >= 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1503. switch (item->subtype) { //In weapons, the look determines type of weapon.
  1504. case W_DAGGER: //All level 4 - Daggers
  1505. case W_1HSWORD: //All level 4 - 1H Swords
  1506. case W_1HAXE: //All level 4 - 1H Axes
  1507. case W_MACE: //All level 4 - 1H Maces
  1508. case W_STAFF: //All level 4 - 1H Staves
  1509. case W_2HSTAFF: //All level 4 - 2H Staves
  1510. return ITEM_EQUIP_ACK_OK;
  1511. }
  1512. }
  1513. if(item->elv && sd->status.base_level < static_cast<uint32>(item->elv))
  1514. return ITEM_EQUIP_ACK_FAILLEVEL;
  1515. if(item->elvmax && sd->status.base_level > static_cast<uint32>(item->elvmax))
  1516. return ITEM_EQUIP_ACK_FAILLEVEL;
  1517. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1518. return ITEM_EQUIP_ACK_FAIL;
  1519. // Broken equip
  1520. if (sd->inventory.u.items_inventory[n].attribute == 1)
  1521. return ITEM_EQUIP_ACK_FAIL;
  1522. //fail to equip if item is restricted
  1523. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1524. return ITEM_EQUIP_ACK_FAIL;
  1525. if (item->equip&EQP_AMMO) {
  1526. switch (item->subtype) {
  1527. case AMMO_ARROW:
  1528. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1529. clif_msg(sd, MSI_FAIL_NEED_EQUIPPED_BOW);
  1530. return ITEM_EQUIP_ACK_FAIL;
  1531. }
  1532. break;
  1533. case AMMO_DAGGER:
  1534. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1535. return ITEM_EQUIP_ACK_FAIL;
  1536. break;
  1537. case AMMO_BULLET:
  1538. case AMMO_SHELL:
  1539. if (battle_config.ammo_check_weapon && sd->status.weapon != W_REVOLVER && sd->status.weapon != W_RIFLE && sd->status.weapon != W_GATLING && sd->status.weapon != W_SHOTGUN
  1540. #ifdef RENEWAL
  1541. && sd->status.weapon != W_GRENADE
  1542. #endif
  1543. ) {
  1544. clif_msg(sd, MSI_WRONG_BULLET);
  1545. return ITEM_EQUIP_ACK_FAIL;
  1546. }
  1547. break;
  1548. #ifndef RENEWAL
  1549. case AMMO_GRENADE:
  1550. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1551. clif_msg(sd, MSI_WRONG_BULLET);
  1552. return ITEM_EQUIP_ACK_FAIL;
  1553. }
  1554. break;
  1555. #endif
  1556. case AMMO_CANNONBALL:
  1557. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1558. clif_msg(sd, MSI_USESKILL_FAIL_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1559. return ITEM_EQUIP_ACK_FAIL;
  1560. }
  1561. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1562. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1563. clif_msg(sd, MSI_USESKILL_FAIL_CART); // Only available when cart is mounted.
  1564. return ITEM_EQUIP_ACK_FAIL;
  1565. }
  1566. break;
  1567. }
  1568. }
  1569. if (!sd->sc.empty()) {
  1570. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.getSCE(SC_STRIPWEAPON)) // Also works with left-hand weapons [DracoRPG]
  1571. return ITEM_EQUIP_ACK_FAIL;
  1572. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.getSCE(SC_STRIPSHIELD))
  1573. return ITEM_EQUIP_ACK_FAIL;
  1574. if(item->equip & EQP_ARMOR && sd->sc.getSCE(SC_STRIPARMOR))
  1575. return ITEM_EQUIP_ACK_FAIL;
  1576. if(item->equip & EQP_HEAD_TOP && sd->sc.getSCE(SC_STRIPHELM))
  1577. return ITEM_EQUIP_ACK_FAIL;
  1578. if(item->equip & EQP_ACC && sd->sc.getSCE(SC__STRIPACCESSORY))
  1579. return ITEM_EQUIP_ACK_FAIL;
  1580. if (item->equip & EQP_ARMS && sd->sc.getSCE(SC__WEAKNESS))
  1581. return ITEM_EQUIP_ACK_FAIL;
  1582. if (item->equip & EQP_SHADOW_GEAR && sd->sc.getSCE(SC_SHADOW_STRIP))
  1583. return ITEM_EQUIP_ACK_FAIL;
  1584. if(item->equip && (sd->sc.getSCE(SC_KYOUGAKU) || sd->sc.getSCE(SC_SUHIDE)))
  1585. return ITEM_EQUIP_ACK_FAIL;
  1586. }
  1587. //Not equipable by class. [Skotlex]
  1588. if (!pc_job_can_use_item(sd,item))
  1589. return ITEM_EQUIP_ACK_FAIL;
  1590. if (!pc_isItemClass(sd, item))
  1591. return ITEM_EQUIP_ACK_FAIL;
  1592. return ITEM_EQUIP_ACK_OK;
  1593. }
  1594. /**
  1595. * Performs some special modifications to a player's last point location,
  1596. * if the map has a nosave mapflag or if the map was an instance.
  1597. * @param sd: Player data
  1598. * @return True if the player should be returned to his savepoint or false if not
  1599. */
  1600. bool pc_lastpoint_special( map_session_data& sd ){
  1601. int16 mapid = map_mapname2mapid( sd.status.last_point.map );
  1602. // Should not happen because otherwise the char-server would have sent the player to another map-server
  1603. if( mapid < 0 ){
  1604. // Return the player to his savepoint
  1605. return true;
  1606. }
  1607. struct map_data* mapdata = map_getmapdata( mapid );
  1608. if( mapdata == nullptr ){
  1609. // Return the player to his savepoint
  1610. return true;
  1611. }
  1612. if (strcmpi(sd.status.last_point.map, MAP_JAIL) == 0) {
  1613. // Don't return jailed player to save point.
  1614. return false;
  1615. }
  1616. // Maybe since the player's logout the nosave mapflag was added to the map
  1617. if( mapdata->getMapFlag(MF_NOSAVE) ){
  1618. // The map has a specific return point
  1619. if( mapdata->save.map ){
  1620. safestrncpy( sd.status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd.status.last_point.map ) );
  1621. sd.status.last_point.x = mapdata->save.x;
  1622. sd.status.last_point.y = mapdata->save.y;
  1623. return false;
  1624. }else{
  1625. // Return the user to his save point
  1626. return true;
  1627. }
  1628. }
  1629. // Check if the last point was an instance
  1630. if( sd.status.last_point_instanceid == 0 ){
  1631. // Nothing to do
  1632. return false;
  1633. }
  1634. // Check if returning to the instance is allowed in general
  1635. if( !battle_config.instance_allow_reconnect ){
  1636. // Return the player to his savepoint
  1637. return true;
  1638. }
  1639. std::shared_ptr<s_instance_data> instance_data = util::umap_find( instances, sd.status.last_point_instanceid );
  1640. if( instance_data == nullptr ){
  1641. // Instance does not exist anymore, return the player to his savepoint
  1642. return true;
  1643. }
  1644. switch( instance_data->mode ){
  1645. case IM_NONE:
  1646. // Cannot validate
  1647. break;
  1648. case IM_CHAR:
  1649. if( sd.status.char_id != instance_data->owner_id ){
  1650. // It is a character bound instance and the player is not the owner, return the player to his savepoint
  1651. return true;
  1652. }
  1653. break;
  1654. case IM_PARTY:
  1655. if( sd.status.party_id != instance_data->owner_id ){
  1656. // It is a party bound instance and the player is not in the party, return the player to his savepoint
  1657. return true;
  1658. }
  1659. break;
  1660. case IM_GUILD:
  1661. if( sd.status.guild_id != instance_data->owner_id ){
  1662. // It is a guild bound instance and the player is not in the guild, return the player to his savepoint
  1663. return true;
  1664. }
  1665. break;
  1666. case IM_CLAN:
  1667. if( sd.status.clan_id != instance_data->owner_id ){
  1668. // It is a clan bound instance and the player is not in the clan, return the player to his savepoint
  1669. return true;
  1670. }
  1671. break;
  1672. }
  1673. int16 imapid = instance_mapid( mapid, sd.status.last_point_instanceid );
  1674. if( imapid < 0 ){
  1675. // Instance does not contain this map anymore, return the player to his savepoint
  1676. return true;
  1677. }
  1678. // Overwrite the stored "source mapname" with the real mapname
  1679. instance_generate_mapname( mapid, sd.status.last_point_instanceid, sd.status.last_point.map );
  1680. // X/Y coordinates are already correct and do not need to be modified
  1681. // Dont warp the player back to his savepoint
  1682. return false;
  1683. }
  1684. /*==========================================
  1685. * No problem with the session id
  1686. * set the status that has been sent from char server
  1687. *------------------------------------------*/
  1688. bool pc_authok(map_session_data *sd, uint32 login_id2, time_t expiration_time, int32 group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1689. {
  1690. t_tick tick = gettick();
  1691. uint32 ip = session[sd->fd]->client_addr;
  1692. sd->login_id2 = login_id2;
  1693. sd->group_id = group_id;
  1694. /* load user permissions */
  1695. pc_group_pc_load(sd);
  1696. memcpy(&sd->status, st, sizeof(*st));
  1697. if (st->sex != sd->status.sex) {
  1698. clif_authfail_fd(sd->fd, 0);
  1699. return false;
  1700. }
  1701. //Set the map-server used job id. [Skotlex]
  1702. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1703. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1704. ShowError("pc_authok: Invalid class %d for player %s (%d:%d). Class was changed to novice.\n", sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id);
  1705. sd->status.class_ = JOB_NOVICE;
  1706. sd->class_ = MAPID_NOVICE;
  1707. } else
  1708. sd->class_ = class_;
  1709. // Checks and fixes to character status data, that are required
  1710. // in case of configuration change or stuff, which cannot be
  1711. // checked on char-server.
  1712. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1713. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1714. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1715. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1716. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1717. sd->state.connect_new = 1;
  1718. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1719. sd->invincible_timer = INVALID_TIMER;
  1720. sd->npc_timer_id = INVALID_TIMER;
  1721. sd->pvp_timer = INVALID_TIMER;
  1722. sd->expiration_tid = INVALID_TIMER;
  1723. sd->autotrade_tid = INVALID_TIMER;
  1724. sd->respawn_tid = INVALID_TIMER;
  1725. sd->tid_queue_active = INVALID_TIMER;
  1726. sd->macro_detect.timer = INVALID_TIMER;
  1727. sd->skill_keep_using.tid = INVALID_TIMER;
  1728. sd->skill_keep_using.skill_id = 0;
  1729. sd->skill_keep_using.level = 0;
  1730. sd->skill_keep_using.target = 0;
  1731. #ifdef SECURE_NPCTIMEOUT
  1732. // Initialize to defaults/expected
  1733. sd->npc_idle_timer = INVALID_TIMER;
  1734. sd->npc_idle_tick = tick;
  1735. sd->npc_idle_type = NPCT_INPUT;
  1736. sd->state.ignoretimeout = false;
  1737. #endif
  1738. sd->canuseitem_tick = tick;
  1739. sd->canusecashfood_tick = tick;
  1740. sd->canequip_tick = tick;
  1741. sd->cantalk_tick = tick;
  1742. sd->canskill_tick = tick;
  1743. sd->cansendmail_tick = tick;
  1744. sd->idletime = last_tick;
  1745. sd->regen.tick.hp = tick;
  1746. sd->regen.tick.sp = tick;
  1747. for(int32 i = 0; i < MAX_SPIRITBALL; i++)
  1748. sd->spirit_timer[i] = INVALID_TIMER;
  1749. if (battle_config.item_auto_get)
  1750. sd->state.autoloot = 10000;
  1751. if (battle_config.disp_experience)
  1752. sd->state.showexp = 1;
  1753. if (battle_config.disp_zeny)
  1754. sd->state.showzeny = 1;
  1755. #ifdef VIP_ENABLE
  1756. if (!battle_config.vip_disp_rate)
  1757. sd->vip.disableshowrate = 1;
  1758. #endif
  1759. if (!(battle_config.display_skill_fail&2))
  1760. sd->state.showdelay = 1;
  1761. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1762. memset(&sd->cart, 0, sizeof(struct s_storage));
  1763. memset(&sd->storage, 0, sizeof(struct s_storage));
  1764. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1765. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1766. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1767. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1768. sd->status.option &= ~OPTION_INVISIBLE;
  1769. }
  1770. status_change_init(&sd->bl);
  1771. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1772. unit_dataset(&sd->bl);
  1773. sd->guild_x = -1;
  1774. sd->guild_y = -1;
  1775. sd->delayed_damage = 0;
  1776. // Event Timers
  1777. for( int32 i = 0; i < MAX_EVENTTIMER; i++ )
  1778. sd->eventtimer[i] = INVALID_TIMER;
  1779. // Rental Timer
  1780. sd->rental_timer = INVALID_TIMER;
  1781. for( int32 i = 0; i < 3; i++ )
  1782. sd->hate_mob[i] = -1;
  1783. sd->quest_log = nullptr;
  1784. sd->num_quests = 0;
  1785. sd->avail_quests = 0;
  1786. sd->save_quest = false;
  1787. sd->count_rewarp = 0;
  1788. sd->mail.pending_weight = 0;
  1789. sd->mail.pending_zeny = 0;
  1790. sd->mail.pending_slots = 0;
  1791. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1792. sd->regs.arrays = nullptr;
  1793. sd->vars_dirty = false;
  1794. sd->vars_ok = false;
  1795. sd->vars_received = 0x0;
  1796. // Check if the player's last point requires special handling and if conditions apply to return the player to his savepoint
  1797. if( pc_lastpoint_special( *sd ) ){
  1798. // The player should be warped back to his savepoint
  1799. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1800. sd->status.last_point.x = sd->status.save_point.x;
  1801. sd->status.last_point.y = sd->status.save_point.y;
  1802. }
  1803. //warp player
  1804. enum e_setpos setpos_result = pc_setpos( sd, mapindex_name2id( sd->status.last_point.map ), sd->status.last_point.x, sd->status.last_point.y, CLR_OUTSIGHT );
  1805. if( setpos_result != SETPOS_OK ){
  1806. ShowError( "Last_point_map %s not found (error code %d)\n", sd->status.last_point.map, setpos_result );
  1807. // try warping to a default map instead (church graveyard)
  1808. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1809. // if we fail again
  1810. clif_authfail_fd(sd->fd, 0);
  1811. return false;
  1812. }
  1813. }
  1814. clif_inventory_expansion_info( sd );
  1815. clif_authok(sd);
  1816. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1817. sd->die_counter=-1;
  1818. //display login notice
  1819. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1820. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1821. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1822. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1823. sd->status.name, sd->status.account_id, sd->status.char_id,
  1824. CONVIP(ip), sd->group_id);
  1825. // Send friends list
  1826. clif_friendslist_send( *sd );
  1827. if( !changing_mapservers ) {
  1828. if (battle_config.display_version == 1)
  1829. pc_show_version(sd);
  1830. // Message of the Day [Valaris]
  1831. for(int32 i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1832. if (battle_config.motd_type)
  1833. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1834. else
  1835. clif_displaymessage(sd->fd, motd_text[i]);
  1836. }
  1837. if (expiration_time != 0)
  1838. sd->expiration_time = expiration_time;
  1839. /**
  1840. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1841. **/
  1842. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  1843. }
  1844. pc_validate_skill(sd);
  1845. /* [Ind] */
  1846. sd->sc_display = nullptr;
  1847. sd->sc_display_count = 0;
  1848. // Player has not yet received the CashShop list
  1849. sd->status.cashshop_sent = false;
  1850. sd->last_addeditem_index = -1;
  1851. sd->bonus_script.head = nullptr;
  1852. sd->bonus_script.count = 0;
  1853. // Initialize BG queue
  1854. sd->bg_queue_id = 0;
  1855. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1856. sd->hatEffects = {};
  1857. #endif
  1858. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1859. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1860. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1861. clif_updatestatus(*sd, SP_BASEEXP);
  1862. }
  1863. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1864. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1865. clif_updatestatus(*sd, SP_JOBEXP);
  1866. }
  1867. // Request all registries (auth is considered completed whence they arrive)
  1868. intif_request_registry(sd,7);
  1869. return true;
  1870. }
  1871. /*==========================================
  1872. * Closes a connection because it failed to be authenticated from the char server.
  1873. *------------------------------------------*/
  1874. void pc_authfail(map_session_data *sd)
  1875. {
  1876. clif_authfail_fd(sd->fd, 0);
  1877. return;
  1878. }
  1879. /**
  1880. * Player register a bl as hatred
  1881. * @param sd : player session
  1882. * @param pos : hate position [0;2]
  1883. * @param bl : target bl
  1884. * @return false:failed, true:success
  1885. */
  1886. bool pc_set_hate_mob(map_session_data *sd, int32 pos, struct block_list *bl)
  1887. {
  1888. int32 class_;
  1889. if (!sd || !bl || pos < 0 || pos > 2)
  1890. return false;
  1891. if (sd->hate_mob[pos] != -1)
  1892. { //Can't change hate targets.
  1893. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1894. return false;
  1895. }
  1896. class_ = status_get_class(bl);
  1897. if (!pcdb_checkid(class_)) {
  1898. uint32 max_hp = status_get_max_hp(bl);
  1899. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1900. return false;
  1901. if (pos != status_get_size(bl))
  1902. return false; //Wrong size
  1903. }
  1904. sd->hate_mob[pos] = class_;
  1905. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1906. clif_hate_info(sd, pos, class_, 1);
  1907. return true;
  1908. }
  1909. /*==========================================
  1910. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1911. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1912. * See intif_parse_StorageReceived() for item operations [lighta]
  1913. *------------------------------------------*/
  1914. void pc_reg_received(map_session_data *sd)
  1915. {
  1916. uint8 i;
  1917. sd->vars_ok = true;
  1918. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1919. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1920. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1921. sd->die_counter = static_cast<int32>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1922. sd->langtype = static_cast<int32>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1923. if (msg_checklangtype(sd->langtype,true) < 0)
  1924. sd->langtype = 0; //invalid langtype reset to default
  1925. // Cash shop
  1926. sd->cashPoints = static_cast<int32>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1927. sd->kafraPoints = static_cast<int32>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1928. // Cooking Exp
  1929. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1930. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1931. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1932. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1933. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1934. }
  1935. if (battle_config.feature_banking)
  1936. sd->bank_vault = static_cast<int32>(pc_readreg2(sd, BANK_VAULT_VAR));
  1937. if (battle_config.feature_roulette) {
  1938. sd->roulette_point.bronze = static_cast<int32>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1939. sd->roulette_point.silver = static_cast<int32>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1940. sd->roulette_point.gold = static_cast<int32>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1941. }
  1942. sd->roulette.prizeIdx = -1;
  1943. //SG map and mob read [Komurka]
  1944. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1945. uint16 j;
  1946. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1947. sd->feel_map[i].index = j;
  1948. sd->feel_map[i].m = map_mapindex2mapid(j);
  1949. } else {
  1950. sd->feel_map[i].index = 0;
  1951. sd->feel_map[i].m = -1;
  1952. }
  1953. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1954. }
  1955. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1956. uint16 skid = static_cast<uint16>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1957. sd->cloneskill_idx = skill_get_index(skid);
  1958. if (sd->cloneskill_idx > 0) {
  1959. sd->status.skill[sd->cloneskill_idx].id = skid;
  1960. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1961. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1962. sd->status.skill[sd->cloneskill_idx].lv = i;
  1963. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1964. }
  1965. }
  1966. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1967. uint16 skid = static_cast<uint16>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1968. sd->reproduceskill_idx = skill_get_index(skid);
  1969. if (sd->reproduceskill_idx > 0) {
  1970. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1971. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1972. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1973. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1974. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1975. }
  1976. }
  1977. //Weird... maybe registries were reloaded?
  1978. if (sd->state.active)
  1979. return;
  1980. sd->state.active = 1;
  1981. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1982. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1983. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1984. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1985. // Restore IM_CHAR instance to the player
  1986. for (const auto &instance : instances) {
  1987. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1988. sd->instance_id = instance.first;
  1989. break;
  1990. }
  1991. }
  1992. if (sd->status.party_id > 0)
  1993. party_member_joined( *sd );
  1994. if (sd->status.guild_id > 0)
  1995. guild_member_joined(sd);
  1996. if (sd->status.clan_id > 0)
  1997. clan_member_joined( *sd );
  1998. #if !( PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410 )
  1999. // Before those clients you could send out the instance info even when the client was still loading the map, afterwards you need to send it later
  2000. clif_instance_info( *sd );
  2001. #endif
  2002. // pet
  2003. if (sd->status.pet_id > 0)
  2004. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  2005. // Homunculus [albator]
  2006. if( sd->status.hom_id > 0 )
  2007. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  2008. if( sd->status.mer_id > 0 )
  2009. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  2010. if( sd->status.ele_id > 0 )
  2011. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  2012. map_addiddb(&sd->bl);
  2013. map_delnickdb(sd->status.char_id, sd->status.name);
  2014. if (!chrif_auth_finished(sd))
  2015. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  2016. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  2017. chrif_bsdata_request(sd->status.char_id);
  2018. #ifdef VIP_ENABLE
  2019. sd->vip.time = 0;
  2020. sd->vip.enabled = 0;
  2021. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  2022. #endif
  2023. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  2024. intif_request_questlog(sd);
  2025. if (battle_config.feature_achievement) {
  2026. sd->achievement_data.total_score = 0;
  2027. sd->achievement_data.level = 0;
  2028. sd->achievement_data.save = false;
  2029. sd->achievement_data.count = 0;
  2030. sd->achievement_data.incompleteCount = 0;
  2031. sd->achievement_data.achievements = nullptr;
  2032. intif_request_achievements(sd->status.char_id);
  2033. }
  2034. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  2035. sd->state.connect_new = 1;
  2036. clif_parse_LoadEndAck(sd->fd, sd);
  2037. if( pc_isinvisible( sd ) ){
  2038. sd->vd.class_ = JT_INVISIBLE;
  2039. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  2040. // decrement the number of pvp players on the map
  2041. map_getmapdata( sd->bl.m )->users_pvp--;
  2042. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  2043. // unregister the player for ranking
  2044. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  2045. sd->pvp_timer = INVALID_TIMER;
  2046. }
  2047. clif_changeoption( &sd->bl );
  2048. }
  2049. }
  2050. channel_autojoin(sd);
  2051. }
  2052. static int32 pc_calc_skillpoint(map_session_data* sd)
  2053. {
  2054. uint16 i, skill_point = 0;
  2055. nullpo_ret(sd);
  2056. for(i = 1; i < MAX_SKILL; i++) {
  2057. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  2058. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  2059. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  2060. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  2061. )
  2062. {
  2063. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  2064. skill_point += sd->status.skill[i].lv;
  2065. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  2066. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  2067. }
  2068. }
  2069. }
  2070. return skill_point;
  2071. }
  2072. static bool pc_grant_allskills(map_session_data *sd, bool addlv) {
  2073. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  2074. return false;
  2075. /**
  2076. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  2077. * skill tree and since they have no icons they'll give resource errors
  2078. * Get ALL skills except npc/guild ones. [Skotlex]
  2079. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  2080. **/
  2081. for (const auto &skill : skill_db) {
  2082. uint16 skill_id = skill.second->nameid;
  2083. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  2084. continue;
  2085. switch (skill_id) {
  2086. case SM_SELFPROVOKE:
  2087. case AB_DUPLELIGHT_MELEE:
  2088. case AB_DUPLELIGHT_MAGIC:
  2089. case WL_CHAINLIGHTNING_ATK:
  2090. case WL_TETRAVORTEX_FIRE:
  2091. case WL_TETRAVORTEX_WATER:
  2092. case WL_TETRAVORTEX_WIND:
  2093. case WL_TETRAVORTEX_GROUND:
  2094. case WL_SUMMON_ATK_FIRE:
  2095. case WL_SUMMON_ATK_WIND:
  2096. case WL_SUMMON_ATK_WATER:
  2097. case WL_SUMMON_ATK_GROUND:
  2098. case LG_OVERBRAND_BRANDISH:
  2099. case LG_OVERBRAND_PLUSATK:
  2100. case WM_SEVERE_RAINSTORM_MELEE:
  2101. case RL_R_TRIP_PLUSATK:
  2102. case SG_DEVIL:
  2103. case MO_TRIPLEATTACK:
  2104. case RG_SNATCHER:
  2105. continue;
  2106. default:
  2107. {
  2108. uint8 lv = (uint8)skill_get_max(skill_id);
  2109. if (lv > 0) {
  2110. uint16 idx = skill_get_index(skill_id);
  2111. sd->status.skill[idx].id = skill_id;
  2112. if (addlv)
  2113. sd->status.skill[idx].lv = lv;
  2114. }
  2115. }
  2116. break;
  2117. }
  2118. }
  2119. return true;
  2120. }
  2121. /*==========================================
  2122. * Calculation of skill level.
  2123. * @param sd
  2124. *------------------------------------------*/
  2125. void pc_calc_skilltree(map_session_data *sd)
  2126. {
  2127. nullpo_retv(sd);
  2128. uint64 job = pc_calc_skilltree_normalize_job(sd);
  2129. int32 class_ = pc_mapid2jobid(job, sd->status.sex);
  2130. if( class_ == -1 )
  2131. { //Unable to normalize job??
  2132. ShowError( "pc_calc_skilltree: Unable to normalize job %s(%d) for character %s (%d:%d)\n", job_name( sd->status.class_ ), sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id );
  2133. return;
  2134. }
  2135. uint16 job_id = class_;
  2136. class_ = pc_class2idx(class_);
  2137. for (const auto &skill : skill_db) {
  2138. uint16 skill_id = skill.second->nameid;
  2139. uint16 idx = skill_get_index(skill_id);
  2140. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  2141. sd->status.skill[idx].id = 0; //First clear skills.
  2142. /* permanent skills that must be re-checked */
  2143. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  2144. if (skill_id == 0) {
  2145. sd->status.skill[idx].id = 0;
  2146. sd->status.skill[idx].lv = 0;
  2147. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2148. continue;
  2149. }
  2150. switch (skill_id) {
  2151. case NV_TRICKDEAD:
  2152. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  2153. sd->status.skill[idx].id = 0;
  2154. sd->status.skill[idx].lv = 0;
  2155. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2156. }
  2157. break;
  2158. }
  2159. }
  2160. }
  2161. for (const auto &skill : skill_db) {
  2162. uint16 idx = skill_get_index(skill.second->nameid);
  2163. // Restore original level of skills after deleting earned skills.
  2164. if( sd->status.skill[idx].flag != SKILL_FLAG_PERMANENT && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED ) {
  2165. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2166. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2167. }
  2168. }
  2169. // Removes Taekwon Ranker skill bonus
  2170. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  2171. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  2172. if (tree != nullptr && !tree->skills.empty()) {
  2173. for (const auto &it : tree->skills) {
  2174. uint16 sk_idx = skill_get_index(it.first);
  2175. if (sk_idx == 0)
  2176. continue;
  2177. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  2178. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  2179. continue;
  2180. sd->status.skill[sk_idx].id = 0;
  2181. }
  2182. }
  2183. }
  2184. }
  2185. // Grant all skills
  2186. pc_grant_allskills(sd, false);
  2187. int32 flag;
  2188. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2189. do {
  2190. flag = 0;
  2191. if (tree == nullptr || tree->skills.empty())
  2192. break;
  2193. for (const auto &skillsit : tree->skills) {
  2194. bool fail = false;
  2195. uint16 skid = skillsit.first;
  2196. uint16 sk_idx = skill_get_index(skid);
  2197. if (sd->status.skill[sk_idx].id)
  2198. continue; //Skill already known.
  2199. if (!battle_config.skillfree) {
  2200. // Checking required skills
  2201. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2202. if (entry != nullptr && !entry->need.empty()) {
  2203. for (const auto &it : entry->need) {
  2204. uint16 sk_need_id = it.first;
  2205. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2206. if (sk_need_idx > 0) {
  2207. uint16 sk_need = sk_need_id;
  2208. if (sd->status.skill[sk_need_idx].id == 0 || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_PLAGIARIZED)
  2209. sk_need = 0; //Not learned.
  2210. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  2211. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2212. else
  2213. sk_need = pc_checkskill(sd,sk_need_id);
  2214. if (sk_need < it.second) {
  2215. fail = true;
  2216. break;
  2217. }
  2218. }
  2219. }
  2220. }
  2221. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  2222. int32 c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2223. c_ = pc_class2idx(c_);
  2224. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  2225. fail = true; // base level requirement wasn't satisfied
  2226. }
  2227. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  2228. int32 c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2229. c_ = pc_class2idx(c_);
  2230. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  2231. fail = true; // job level requirement wasn't satisfied
  2232. }
  2233. }
  2234. if (!fail) {
  2235. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2236. if (!sd->status.skill[sk_idx].lv && (
  2237. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2238. skill->inf2[INF2_ISWEDDING] ||
  2239. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2240. ))
  2241. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  2242. sd->status.skill[sk_idx].id = skid;
  2243. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  2244. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  2245. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  2246. }
  2247. flag = 1; // skill list has changed, perform another pass
  2248. }
  2249. }
  2250. } while(flag);
  2251. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  2252. uint16 skid = 0;
  2253. /* Taekwon Ranker Bonus Skill Tree
  2254. ============================================
  2255. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  2256. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  2257. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  2258. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2259. if (tree != nullptr && !tree->skills.empty()) {
  2260. for (const auto &it : tree->skills) {
  2261. skid = it.first;
  2262. uint16 sk_idx = skill_get_index(skid);
  2263. if (sk_idx == 0)
  2264. continue;
  2265. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  2266. continue; //Do not include Quest/Wedding skills.
  2267. if( sd->status.skill[sk_idx].id == 0 ) {
  2268. sd->status.skill[sk_idx].id = skid;
  2269. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  2270. } else if( skid != NV_BASIC )
  2271. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  2272. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  2273. }
  2274. }
  2275. }
  2276. // Enable Bard/Dancer spirit linked skills.
  2277. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_BARDDANCER) {
  2278. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  2279. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  2280. { BA_POEMBRAGI, DC_FORTUNEKISS },
  2281. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  2282. for (const auto &skill : linked_skills) {
  2283. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  2284. continue;
  2285. // Tag it as a non-savable, non-uppable, bonus skill
  2286. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  2287. }
  2288. }
  2289. }
  2290. //Checks if you can learn a new skill after having leveled up a skill.
  2291. static void pc_check_skilltree(map_session_data *sd)
  2292. {
  2293. int32 flag = 0;
  2294. if (battle_config.skillfree)
  2295. return; //Function serves no purpose if this is set
  2296. int32 c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  2297. if (c == -1) { //Unable to normalize job??
  2298. ShowError("pc_check_skilltree: Unable to normalize job %d for character %s (%d:%d)\n", sd->status.class_, sd->status.name, sd->status.account_id, sd->status.char_id);
  2299. return;
  2300. }
  2301. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  2302. if (tree == nullptr || tree->skills.empty())
  2303. return;
  2304. do {
  2305. flag = 0;
  2306. for (const auto &skillsit : tree->skills) {
  2307. uint16 skid = skillsit.first;
  2308. uint16 sk_idx = skill_get_index(skid);
  2309. bool fail = false;
  2310. if (sd->status.skill[sk_idx].id) //Already learned
  2311. continue;
  2312. // Checking required skills
  2313. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2314. if (entry != nullptr && !entry->need.empty()) {
  2315. for (const auto &it : entry->need) {
  2316. uint16 sk_need_id = it.first;
  2317. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2318. if (sk_need_id > 0) {
  2319. short sk_need = sk_need_id;
  2320. if (sd->status.skill[sk_need_idx].id == 0 || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[sk_need_idx].flag == SKILL_FLAG_PLAGIARIZED)
  2321. sk_need = 0; //Not learned.
  2322. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  2323. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2324. else
  2325. sk_need = pc_checkskill(sd,sk_need_id);
  2326. if (sk_need < it.second) {
  2327. fail = true;
  2328. break;
  2329. }
  2330. }
  2331. }
  2332. }
  2333. if( fail )
  2334. continue;
  2335. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2336. continue;
  2337. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2338. if( !sd->status.skill[sk_idx].lv && (
  2339. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2340. skill->inf2[INF2_ISWEDDING] ||
  2341. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2342. ) )
  2343. continue; //Cannot be learned via normal means.
  2344. sd->status.skill[sk_idx].id = skid;
  2345. flag = 1;
  2346. }
  2347. } while(flag);
  2348. }
  2349. // Make sure all the skills are in the correct condition
  2350. // before persisting to the backend.. [MouseJstr]
  2351. void pc_clean_skilltree(map_session_data *sd)
  2352. {
  2353. uint16 i;
  2354. for (i = 0; i < MAX_SKILL; i++){
  2355. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2356. sd->status.skill[i].id = 0;
  2357. sd->status.skill[i].lv = 0;
  2358. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2359. }
  2360. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2361. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2362. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2363. }
  2364. }
  2365. }
  2366. uint64 pc_calc_skilltree_normalize_job_sub( map_session_data *sd ){
  2367. int32 skill_point = pc_calc_skillpoint( sd );
  2368. if( sd->class_ & MAPID_SUMMONER ){
  2369. // Novice's skill points for basic skill.
  2370. std::shared_ptr<s_job_info> summoner_job = job_db.find( JOB_SUMMONER );
  2371. int32 summoner_skills = summoner_job->max_job_level - 1;
  2372. if( skill_point < summoner_skills ){
  2373. return MAPID_SUMMONER;
  2374. }
  2375. skill_point -= summoner_skills;
  2376. }else{
  2377. // Novice's skill points for basic skill.
  2378. std::shared_ptr<s_job_info> novice_job = job_db.find( JOB_NOVICE );
  2379. int32 novice_skills = novice_job->max_job_level - 1;
  2380. if( skill_point < novice_skills ){
  2381. return MAPID_NOVICE;
  2382. }
  2383. skill_point -= novice_skills;
  2384. }
  2385. // 1st Class Job LV Check
  2386. if( sd->class_ & JOBL_2 && ( sd->class_ & MAPID_UPPERMASK ) != MAPID_SUPER_NOVICE ){
  2387. uint64 mapid_1st = sd->class_ & MAPID_BASEMASK;
  2388. int32 class_1st = pc_mapid2jobid( mapid_1st, sd->status.sex );
  2389. if( !sd->change_level_2nd ){
  2390. if( class_1st >= JOB_NOVICE ){
  2391. sd->change_level_2nd = job_db.find( class_1st )->max_job_level;
  2392. }else{
  2393. sd->change_level_2nd = 0;
  2394. }
  2395. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  2396. }
  2397. if( class_1st > 0 && skill_point < ( sd->change_level_2nd - 1 ) ){
  2398. return mapid_1st;
  2399. }
  2400. skill_point -= ( sd->change_level_2nd - 1 );
  2401. }
  2402. // 2nd Class Job LV Check
  2403. if( sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E ){
  2404. uint64 mapid_2nd = sd->class_ & MAPID_UPPERMASK;
  2405. int32 class_2nd = pc_mapid2jobid( mapid_2nd, sd->status.sex );
  2406. if( !sd->change_level_3rd ){
  2407. if( class_2nd >= JOB_NOVICE ){
  2408. sd->change_level_3rd = job_db.find( class_2nd )->max_job_level;
  2409. }else{
  2410. sd->change_level_3rd = 0;
  2411. }
  2412. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  2413. }
  2414. if( class_2nd > 0 && skill_point < ( sd->change_level_3rd - 1 ) ){
  2415. return mapid_2nd;
  2416. }
  2417. skill_point -= ( sd->change_level_3rd - 1 );
  2418. }
  2419. // 3rd Class Job LV Check
  2420. if( sd->class_ & JOBL_FOURTH ){
  2421. uint64 mapid_3rd = sd->class_ & MAPID_THIRDMASK | JOBL_THIRD;
  2422. int32 class_3rd = pc_mapid2jobid( mapid_3rd, sd->status.sex );
  2423. if( !sd->change_level_4th ){
  2424. if( class_3rd >= JOB_NOVICE ){
  2425. sd->change_level_4th = job_db.find( class_3rd )->max_job_level;
  2426. }else{
  2427. sd->change_level_4th = 0;
  2428. }
  2429. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ), sd->change_level_4th );
  2430. }
  2431. if( class_3rd > 0 && skill_point < ( sd->change_level_4th - 1 ) ){
  2432. return mapid_3rd;
  2433. }
  2434. skill_point -= ( sd->change_level_4th - 1 );
  2435. }
  2436. return sd->class_;
  2437. }
  2438. uint64 pc_calc_skilltree_normalize_job( map_session_data *sd ){
  2439. if( !battle_config.skillup_limit || pc_has_permission( sd, PC_PERM_ALL_SKILL ) ){
  2440. return sd->class_;
  2441. }
  2442. uint64 c = pc_calc_skilltree_normalize_job_sub( sd );
  2443. // Special Masks
  2444. if( sd->class_&JOBL_UPPER ){
  2445. if( pc_mapid2jobid( c | JOBL_UPPER, sd->status.sex ) >= JOB_NOVICE ){
  2446. c |= JOBL_UPPER;// Rebirth Job
  2447. }
  2448. }
  2449. if( sd->class_&JOBL_BABY ){
  2450. if( pc_mapid2jobid( c | JOBL_BABY, sd->status.sex ) >= JOB_NOVICE ){
  2451. c |= JOBL_BABY;// Baby Job
  2452. }
  2453. }
  2454. return c;
  2455. }
  2456. /*==========================================
  2457. * Updates the weight status
  2458. *------------------------------------------
  2459. * 1: overweight 50% for pre-renewal and 70% for renewal
  2460. * 2: overweight 90%
  2461. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2462. */
  2463. void pc_updateweightstatus(map_session_data *sd)
  2464. {
  2465. int32 old_overweight;
  2466. int32 new_overweight;
  2467. nullpo_retv(sd);
  2468. old_overweight = (sd->sc.getSCE(SC_WEIGHT90)) ? 2 : (sd->sc.getSCE(SC_WEIGHT50)) ? 1 : 0;
  2469. #ifdef RENEWAL
  2470. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2471. #else
  2472. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2473. #endif
  2474. if( old_overweight == new_overweight )
  2475. return; // no change
  2476. // stop old status change
  2477. if( old_overweight == 1 )
  2478. status_change_end(&sd->bl, SC_WEIGHT50);
  2479. else if( old_overweight == 2 )
  2480. status_change_end(&sd->bl, SC_WEIGHT90);
  2481. // start new status change
  2482. if( new_overweight == 1 )
  2483. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2484. else if( new_overweight == 2 )
  2485. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2486. // update overweight status
  2487. sd->regen.state.overweight = new_overweight;
  2488. }
  2489. int32 pc_disguise(map_session_data *sd, int32 class_)
  2490. {
  2491. if (!class_ && !sd->disguise)
  2492. return 0;
  2493. if (class_ && sd->disguise == class_)
  2494. return 0;
  2495. if(pc_isinvisible(sd))
  2496. { //Character is invisible. Stealth class-change. [Skotlex]
  2497. sd->disguise = class_; //viewdata is set on uncloaking.
  2498. return 2;
  2499. }
  2500. if (sd->bl.prev != nullptr) {
  2501. unit_stop_walking( &sd->bl, USW_NONE );
  2502. clif_clearunit_area( sd->bl, CLR_OUTSIGHT );
  2503. }
  2504. if (!class_) {
  2505. sd->disguise = 0;
  2506. class_ = sd->status.class_;
  2507. } else
  2508. sd->disguise=class_;
  2509. status_set_viewdata(&sd->bl, class_);
  2510. clif_changeoption(&sd->bl);
  2511. if (sd->bl.prev != nullptr) {
  2512. clif_spawn(&sd->bl);
  2513. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2514. { //It seems the cart info is lost on undisguise.
  2515. clif_cartlist(sd);
  2516. clif_updatestatus(*sd,SP_CARTINFO);
  2517. }
  2518. if (sd->chatID) {
  2519. chat_data* cd = map_id2cd( sd->chatID );
  2520. if( cd != nullptr ){
  2521. clif_dispchat( *cd );
  2522. }
  2523. }
  2524. }
  2525. return 1;
  2526. }
  2527. /// Show error message
  2528. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2529. /// Check for valid Element, break & show error message if invalid Element
  2530. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2531. /// Check for valid Race, break & show error message if invalid Race
  2532. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2533. /// Check for valid Race2, break & show error message if invalid Race2
  2534. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2535. /// Check for valid Class, break & show error message if invalid Class
  2536. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2537. /// Check for valid Size, break & show error message if invalid Size
  2538. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2539. /// Check for valid SC, break & show error message if invalid SC
  2540. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2541. /**
  2542. * Add auto spell bonus for player while attacking/attacked
  2543. * @param spell: Spell array
  2544. * @param id: Skill to cast
  2545. * @param lv: Skill level
  2546. * @param rate: Success chance
  2547. * @param battle_flag: Battle flag
  2548. * @param card_id: Used to prevent card stacking
  2549. * @param flag: Flags used for extra arguments
  2550. * &0: forces the skill to be casted on self, rather than on the target
  2551. * &1: forces the skill to be casted on target, rather than self
  2552. * &2: random skill level in [1..lv] is chosen
  2553. */
  2554. static void pc_bonus_autospell(std::vector<s_autospell> &spell, uint16 id, uint16 lv, short rate, short battle_flag, t_itemid card_id, uint8 flag)
  2555. {
  2556. if (spell.size() == MAX_PC_BONUS) {
  2557. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2558. return;
  2559. }
  2560. if (!rate)
  2561. return;
  2562. if (!(battle_flag&BF_RANGEMASK))
  2563. battle_flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2564. if (!(battle_flag&BF_WEAPONMASK))
  2565. battle_flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2566. if (!(battle_flag&BF_SKILLMASK)) {
  2567. if (battle_flag&(BF_MAGIC | BF_MISC))
  2568. battle_flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2569. if (battle_flag&BF_WEAPON)
  2570. battle_flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2571. }
  2572. for (auto &it : spell) {
  2573. if ((it.card_id == card_id || it.rate < 0 || rate < 0) && it.id == id && it.lv == lv && it.battle_flag == battle_flag && it.flag == flag) {
  2574. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2575. return;
  2576. it.rate = util::safe_addition_cap(it.rate, rate, (short)1000);
  2577. return;
  2578. }
  2579. }
  2580. struct s_autospell entry = {};
  2581. entry.id = id;
  2582. entry.lv = lv;
  2583. entry.rate = cap_value(rate, -1000, 1000);
  2584. entry.battle_flag = battle_flag;
  2585. entry.card_id = card_id;
  2586. entry.flag = flag;
  2587. spell.push_back(entry);
  2588. }
  2589. /**
  2590. * Add auto spell bonus for player while using skills
  2591. * @param spell: Spell array
  2592. * @param src_skill: Trigger skill
  2593. * @param id: Support or target type
  2594. * @param lv: Skill level
  2595. * @param rate: Success chance
  2596. * @param card_id: Used to prevent card stacking
  2597. * @param flag: Flags used for extra arguments
  2598. * &1: forces the skill to be casted on self, rather than on the target of skill
  2599. * &2: random skill level in [1..lv] is chosen
  2600. */
  2601. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, uint16 src_skill, uint16 id, uint16 lv, short rate, t_itemid card_id, uint8 flag)
  2602. {
  2603. if (spell.size() == MAX_PC_BONUS) {
  2604. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2605. return;
  2606. }
  2607. if (!rate)
  2608. return;
  2609. struct s_autospell entry = {};
  2610. entry.trigger_skill = src_skill;
  2611. entry.id = id;
  2612. entry.lv = lv;
  2613. entry.rate = cap_value(rate, -1000, 1000);
  2614. entry.card_id = card_id;
  2615. entry.flag = flag;
  2616. spell.push_back(entry);
  2617. }
  2618. /**
  2619. * Add inflict effect bonus for player while attacking/attacked
  2620. * @param effect: Effect array
  2621. * @param sc: SC/Effect type
  2622. * @param rate: Success chance
  2623. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2624. * @param flag: Target flag
  2625. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2626. */
  2627. static void pc_bonus_addeff(std::vector<s_addeffect> &effect, enum sc_type sc, int32 rate, short arrow_rate, unsigned char flag, uint32 duration)
  2628. {
  2629. if (effect.size() == MAX_PC_BONUS) {
  2630. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2631. return;
  2632. }
  2633. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2634. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2635. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2636. flag |= ATF_TARGET; //Default target: enemy.
  2637. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2638. flag |= ATF_WEAPON; //Default type: weapon.
  2639. if (!duration)
  2640. duration = (uint32)skill_get_time2(status_db.getSkill(sc), 7);
  2641. for (auto &it : effect) {
  2642. if (it.sc == sc && it.flag == flag) {
  2643. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2644. it.arrow_rate = util::safe_addition_cap(it.arrow_rate, arrow_rate, (short)SHRT_MAX);
  2645. it.duration = umax(it.duration, duration);
  2646. return;
  2647. }
  2648. }
  2649. struct s_addeffect entry = {};
  2650. entry.sc = sc;
  2651. entry.rate = rate;
  2652. entry.arrow_rate = arrow_rate;
  2653. entry.flag = flag;
  2654. entry.duration = duration;
  2655. effect.push_back(entry);
  2656. }
  2657. /**
  2658. * Add inflict effect bonus for player while attacking using skill
  2659. * @param effect: Effect array
  2660. * @param sc: SC/Effect type
  2661. * @param rate: Success chance
  2662. * @param skill_id: Skill to cast
  2663. * @param target: Target type
  2664. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2665. */
  2666. static void pc_bonus_addeff_onskill(std::vector<s_addeffectonskill> &effect, enum sc_type sc, int32 rate, short skill_id, unsigned char target, uint32 duration)
  2667. {
  2668. if (effect.size() == MAX_PC_BONUS) {
  2669. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2670. return;
  2671. }
  2672. if (!duration)
  2673. duration = (uint32)skill_get_time2(status_db.getSkill(sc), 7);
  2674. for (auto &it : effect) {
  2675. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2676. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2677. it.duration = umax(it.duration, duration);
  2678. return;
  2679. }
  2680. }
  2681. struct s_addeffectonskill entry = {};
  2682. entry.sc = sc;
  2683. entry.rate = rate;
  2684. entry.skill_id = skill_id;
  2685. entry.target = target;
  2686. entry.duration = duration;
  2687. effect.push_back(entry);
  2688. }
  2689. /**
  2690. * Adjust/add drop rate modifier for player
  2691. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2692. * @param nameid: item id that will be dropped
  2693. * @param group: group id
  2694. * @param class_: target class
  2695. * @param race: target race. if < 0, means monster_id
  2696. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2697. */
  2698. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int32 class_, short race, int32 rate)
  2699. {
  2700. if (!nameid && !group) {
  2701. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2702. return;
  2703. }
  2704. if (nameid && !item_db.exists(nameid)) {
  2705. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2706. return;
  2707. }
  2708. if (group && !itemdb_group.exists(group)) {
  2709. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2710. return;
  2711. }
  2712. if (drop.size() == MAX_PC_BONUS) {
  2713. ShowWarning("pc_bonus_item_drop: Reached max (%d) number of added drops per character! (nameid: %u group: %d class_: %d race: %d rate: %d)\n", MAX_PC_BONUS, nameid, group, class_, race, rate);
  2714. return;
  2715. }
  2716. //Apply config rate adjustment settings.
  2717. if (rate >= 0) { //Absolute drop.
  2718. if (battle_config.item_rate_adddrop != 100)
  2719. rate = rate*battle_config.item_rate_adddrop/100;
  2720. if (rate < battle_config.item_drop_adddrop_min)
  2721. rate = battle_config.item_drop_adddrop_min;
  2722. else if (rate > battle_config.item_drop_adddrop_max)
  2723. rate = battle_config.item_drop_adddrop_max;
  2724. } else { //Relative drop, max/min limits are applied at drop time.
  2725. if (battle_config.item_rate_adddrop != 100)
  2726. rate = rate*battle_config.item_rate_adddrop/100;
  2727. if (rate > -1)
  2728. rate = -1;
  2729. }
  2730. for (auto &it : drop) {
  2731. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2732. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2733. it.rate = util::safe_addition_cap(it.rate, rate, 10000);
  2734. return;
  2735. }
  2736. }
  2737. struct s_add_drop entry = {};
  2738. entry.nameid = nameid;
  2739. entry.group = group;
  2740. entry.race = race;
  2741. entry.class_ = class_;
  2742. entry.rate = cap_value(rate, -10000, 10000);
  2743. drop.push_back(entry);
  2744. }
  2745. s_autobonus::~s_autobonus(){
  2746. if( this->active != INVALID_TIMER ){
  2747. delete_timer( this->active, pc_endautobonus );
  2748. this->active = INVALID_TIMER;
  2749. }
  2750. if( this->bonus_script != nullptr ){
  2751. aFree( this->bonus_script );
  2752. this->bonus_script = nullptr;
  2753. }
  2754. if( this->other_script != nullptr ){
  2755. aFree( this->other_script );
  2756. this->other_script = nullptr;
  2757. }
  2758. }
  2759. /**
  2760. * Add autobonus to player when attacking/attacked
  2761. * @param bonus: Bonus array
  2762. * @param script: Script to execute
  2763. * @param rate: Success chance
  2764. * @param dur: Duration
  2765. * @param flag: Battle flag/skill
  2766. * @param other_script: Secondary script to execute
  2767. * @param pos: Item equip position
  2768. * @param onskill: Skill used to trigger autobonus
  2769. * @return True on success or false otherwise
  2770. */
  2771. bool pc_addautobonus(std::vector<std::shared_ptr<s_autobonus>> &bonus, const char *script, short rate, uint32 dur, uint16 flag, const char *other_script, uint32 pos, bool onskill){
  2772. // Check if the same bonus already exists
  2773. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2774. // Compare based on position and bonus script
  2775. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2776. return false;
  2777. }
  2778. }
  2779. if (bonus.size() == MAX_PC_BONUS) {
  2780. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2781. return false;
  2782. }
  2783. if (!onskill) {
  2784. if (!(flag&BF_RANGEMASK))
  2785. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2786. if (!(flag&BF_WEAPONMASK))
  2787. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2788. if (!(flag&BF_SKILLMASK)) {
  2789. if (flag&(BF_MAGIC | BF_MISC))
  2790. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2791. if (flag&BF_WEAPON)
  2792. flag |= BF_NORMAL | BF_SKILL;
  2793. }
  2794. }
  2795. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2796. if (rate < -10000 || rate > 10000)
  2797. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2798. entry->rate = cap_value(rate, -10000, 10000);
  2799. entry->duration = dur;
  2800. entry->active = INVALID_TIMER;
  2801. entry->atk_type = flag;
  2802. entry->pos = pos;
  2803. entry->bonus_script = aStrdup(script);
  2804. entry->other_script = (other_script ? aStrdup(other_script) : nullptr);
  2805. bonus.push_back(entry);
  2806. return true;
  2807. }
  2808. /**
  2809. * Remove an autobonus from player
  2810. * @param sd: Player data
  2811. * @param bonus: Autobonus array
  2812. * @param restore: Run script on clearing or not
  2813. */
  2814. void pc_delautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2815. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2816. while( it != bonus.end() ){
  2817. std::shared_ptr<s_autobonus> b = *it;
  2818. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2819. uint32 equip_pos_idx = 0;
  2820. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2821. for (uint8 j = 0; j < EQI_MAX; j++) {
  2822. if (sd.equip_index[j] >= 0)
  2823. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2824. }
  2825. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2826. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2827. }else{
  2828. // Not all required items equipped anymore
  2829. restore = false;
  2830. }
  2831. }
  2832. if( restore ){
  2833. it++;
  2834. continue;
  2835. }
  2836. it = bonus.erase(it);
  2837. }
  2838. }
  2839. /**
  2840. * Execute autobonus on player
  2841. * @param sd: Player data
  2842. * @param autobonus: Autobonus to run
  2843. */
  2844. void pc_exeautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2845. {
  2846. if (autobonus->active != INVALID_TIMER)
  2847. delete_timer(autobonus->active, pc_endautobonus);
  2848. if( autobonus->other_script )
  2849. {
  2850. int32 j;
  2851. uint32 equip_pos_idx = 0;
  2852. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2853. for(j = 0; j < EQI_MAX; j++) {
  2854. if(sd.equip_index[j] >= 0)
  2855. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2856. }
  2857. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2858. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2859. }
  2860. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2861. status_calc_pc(&sd,SCO_FORCE);
  2862. }
  2863. /**
  2864. * Remove autobonus timer from player
  2865. */
  2866. TIMER_FUNC(pc_endautobonus){
  2867. map_session_data *sd = map_id2sd(id);
  2868. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2869. nullpo_ret(sd);
  2870. nullpo_ret(bonus);
  2871. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2872. if( autobonus->active == tid ){
  2873. autobonus->active = INVALID_TIMER;
  2874. break;
  2875. }
  2876. }
  2877. status_calc_pc(sd,SCO_FORCE);
  2878. return 0;
  2879. }
  2880. /**
  2881. * Add element bonus to player when attacking
  2882. * @param sd: Player data
  2883. * @param ele: Element to adjust
  2884. * @param rate: Success chance
  2885. * @param flag: Battle flag
  2886. */
  2887. static void pc_bonus_addele(map_session_data* sd, unsigned char ele, short rate, short flag)
  2888. {
  2889. nullpo_retv(sd);
  2890. struct weapon_data *wd = (sd->state.lr_flag != LR_FLAG_NONE ? &sd->left_weapon : &sd->right_weapon);
  2891. if (wd->addele2.size() == MAX_PC_BONUS) {
  2892. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2893. return;
  2894. }
  2895. if (!(flag&BF_RANGEMASK))
  2896. flag |= BF_SHORT | BF_LONG;
  2897. if (!(flag&BF_WEAPONMASK))
  2898. flag |= BF_WEAPON;
  2899. if (!(flag&BF_SKILLMASK)) {
  2900. if (flag&(BF_MAGIC | BF_MISC))
  2901. flag |= BF_SKILL;
  2902. if (flag&BF_WEAPON)
  2903. flag |= BF_NORMAL | BF_SKILL;
  2904. }
  2905. for (auto &it : wd->addele2) {
  2906. if (it.ele == ele && it.flag == flag) {
  2907. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2908. return;
  2909. }
  2910. }
  2911. struct s_addele2 entry = {};
  2912. entry.ele = ele;
  2913. entry.rate = cap_value(rate, -10000, 10000);
  2914. entry.flag = flag;
  2915. wd->addele2.push_back(entry);
  2916. }
  2917. /**
  2918. * Reduce element bonus to player when attacking
  2919. * @param sd: Player data
  2920. * @param ele: Element to adjust
  2921. * @param rate: Success chance
  2922. * @param flag: Battle flag
  2923. */
  2924. static void pc_bonus_subele(map_session_data* sd, unsigned char ele, short rate, short flag)
  2925. {
  2926. nullpo_retv(sd);
  2927. if (sd->subele2.size() == MAX_PC_BONUS) {
  2928. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2929. return;
  2930. }
  2931. if (!(flag&BF_RANGEMASK))
  2932. flag |= BF_SHORT | BF_LONG;
  2933. if (!(flag&BF_WEAPONMASK))
  2934. flag |= BF_WEAPON;
  2935. if (!(flag&BF_SKILLMASK)) {
  2936. if (flag&(BF_MAGIC | BF_MISC))
  2937. flag |= BF_SKILL;
  2938. if (flag&BF_WEAPON)
  2939. flag |= BF_NORMAL | BF_SKILL;
  2940. }
  2941. for (auto &it : sd->subele2) {
  2942. if (it.ele == ele && it.flag == flag) {
  2943. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2944. return;
  2945. }
  2946. }
  2947. struct s_addele2 entry = {};
  2948. entry.ele = ele;
  2949. entry.rate = cap_value(rate, -10000, 10000);
  2950. entry.flag = flag;
  2951. sd->subele2.push_back(entry);
  2952. }
  2953. /**
  2954. * Adjust race damage to target when attacking
  2955. * @param sd: Player data
  2956. * @param race: Race to adjust
  2957. * @param rate: Success chance
  2958. * @param flag: Battle flag
  2959. */
  2960. static void pc_bonus_subrace(map_session_data* sd, unsigned char race, short rate, short flag)
  2961. {
  2962. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2963. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2964. return;
  2965. }
  2966. if (!(flag&BF_RANGEMASK))
  2967. flag |= BF_SHORT | BF_LONG;
  2968. if (!(flag&BF_WEAPONMASK))
  2969. flag |= BF_WEAPON;
  2970. if (!(flag&BF_SKILLMASK)) {
  2971. if (flag&(BF_MAGIC | BF_MISC))
  2972. flag |= BF_SKILL;
  2973. if (flag&BF_WEAPON)
  2974. flag |= BF_NORMAL | BF_SKILL;
  2975. }
  2976. for (auto &it : sd->subrace3) {
  2977. if (it.race == race && it.flag == flag) {
  2978. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2979. return;
  2980. }
  2981. }
  2982. struct s_addrace2 entry = {};
  2983. entry.race = race;
  2984. entry.rate = cap_value(rate, -10000, 10000);
  2985. entry.flag = flag;
  2986. sd->subrace3.push_back(entry);
  2987. }
  2988. /**
  2989. * General item bonus for player
  2990. * @param bonus: Bonus array
  2991. * @param id: Key
  2992. * @param val: Value
  2993. * @param cap_rate: If Value is a rate value that needs to be capped
  2994. */
  2995. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int32 val, bool cap_rate)
  2996. {
  2997. for (auto &it : bonus) {
  2998. if (it.id == id) {
  2999. if (cap_rate)
  3000. it.val = util::safe_addition_cap(it.val, val, 10000);
  3001. else
  3002. it.val += val;
  3003. return;
  3004. }
  3005. }
  3006. struct s_item_bonus entry = {};
  3007. entry.id = id;
  3008. entry.val = cap_rate ? cap_value(val, -10000, 10000) : val;
  3009. bonus.push_back(entry);
  3010. }
  3011. /**
  3012. * Remove HP/SP to player when attacking
  3013. * @param bonus: Bonus array
  3014. * @param rate: Success chance
  3015. * @param per: Percentage of HP/SP to vanish
  3016. * @param flag: Battle flag
  3017. */
  3018. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int32 flag) {
  3019. if (bonus.size() == MAX_PC_BONUS) {
  3020. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  3021. return;
  3022. }
  3023. if (!(flag&BF_RANGEMASK))
  3024. flag |= BF_SHORT | BF_LONG;
  3025. if (!(flag&BF_WEAPONMASK))
  3026. flag |= BF_WEAPON;
  3027. if (!(flag&BF_SKILLMASK)) {
  3028. if (flag&(BF_MAGIC | BF_MISC))
  3029. flag |= BF_SKILL;
  3030. if (flag&BF_WEAPON)
  3031. flag |= BF_NORMAL | BF_SKILL;
  3032. }
  3033. for (auto &it : bonus) {
  3034. if (it.flag == flag) {
  3035. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  3036. it.per += per;
  3037. return;
  3038. }
  3039. }
  3040. struct s_vanish_bonus entry = {};
  3041. entry.rate = cap_value(rate, -10000, 10000);
  3042. entry.per = per;
  3043. entry.flag = flag;
  3044. bonus.push_back(entry);
  3045. }
  3046. /*==========================================
  3047. * Add a bonus(type) to player sd
  3048. * format: bonus bBonusName,val;
  3049. * @param sd
  3050. * @param type Bonus type used by bBonusName
  3051. * @param val Value that usually for rate or fixed value
  3052. *------------------------------------------*/
  3053. void pc_bonus(map_session_data *sd,int32 type,int32 val)
  3054. {
  3055. struct status_data *status;
  3056. int32 bonus;
  3057. nullpo_retv(sd);
  3058. status = &sd->base_status;
  3059. switch(type){
  3060. case SP_STR:
  3061. case SP_AGI:
  3062. case SP_VIT:
  3063. case SP_INT:
  3064. case SP_DEX:
  3065. case SP_LUK:
  3066. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3067. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  3068. break;
  3069. case SP_POW:
  3070. case SP_STA:
  3071. case SP_WIS:
  3072. case SP_SPL:
  3073. case SP_CON:
  3074. case SP_CRT:
  3075. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3076. sd->indexed_bonus.param_bonus[type - SP_POW + PARAM_POW] += val;
  3077. break;
  3078. case SP_ATK1:
  3079. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3080. bonus = status->rhw.atk + val;
  3081. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  3082. }
  3083. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3084. bonus = status->lhw.atk + val;
  3085. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  3086. }
  3087. break;
  3088. case SP_ATK2:
  3089. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3090. bonus = status->rhw.atk2 + val;
  3091. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3092. }
  3093. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3094. bonus = status->lhw.atk2 + val;
  3095. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3096. }
  3097. break;
  3098. case SP_BASE_ATK:
  3099. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3100. #ifdef RENEWAL
  3101. bonus = sd->bonus.eatk + val;
  3102. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3103. #else
  3104. bonus = status->batk + val;
  3105. status->batk = cap_value(bonus, 0, USHRT_MAX);
  3106. #endif
  3107. }
  3108. break;
  3109. case SP_DEF1:
  3110. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3111. bonus = status->def + val;
  3112. #ifdef RENEWAL
  3113. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3114. #else
  3115. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3116. #endif
  3117. }
  3118. break;
  3119. case SP_DEF2:
  3120. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3121. bonus = status->def2 + val;
  3122. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3123. }
  3124. break;
  3125. case SP_MDEF1:
  3126. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3127. bonus = status->mdef + val;
  3128. #ifdef RENEWAL
  3129. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3130. #else
  3131. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3132. #endif
  3133. if( sd->state.lr_flag == LR_FLAG_SHIELD ) {//Shield, used for royal guard
  3134. sd->bonus.shieldmdef += bonus;
  3135. }
  3136. }
  3137. break;
  3138. case SP_MDEF2:
  3139. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3140. bonus = status->mdef2 + val;
  3141. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3142. }
  3143. break;
  3144. case SP_HIT:
  3145. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3146. bonus = status->hit + val;
  3147. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3148. } else
  3149. sd->bonus.arrow_hit+=val;
  3150. break;
  3151. case SP_FLEE1:
  3152. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3153. bonus = status->flee + val;
  3154. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3155. }
  3156. break;
  3157. case SP_FLEE2:
  3158. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3159. bonus = status->flee2 + val*10;
  3160. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3161. }
  3162. break;
  3163. case SP_CRITICAL:
  3164. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3165. bonus = status->cri + val*10;
  3166. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3167. } else
  3168. sd->bonus.arrow_cri += val*10;
  3169. break;
  3170. case SP_PATK:
  3171. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3172. bonus = status->patk + val;
  3173. status->patk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3174. }
  3175. break;
  3176. case SP_SMATK:
  3177. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3178. bonus = status->smatk + val;
  3179. status->smatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3180. }
  3181. break;
  3182. case SP_RES:
  3183. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3184. bonus = status->res + val;
  3185. status->res = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3186. }
  3187. break;
  3188. case SP_MRES:
  3189. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3190. bonus = status->mres + val;
  3191. status->mres = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3192. }
  3193. break;
  3194. case SP_HPLUS:
  3195. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3196. bonus = status->hplus + val;
  3197. status->hplus = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3198. }
  3199. break;
  3200. case SP_CRATE:
  3201. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3202. bonus = status->crate + val;
  3203. status->crate = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3204. }
  3205. break;
  3206. case SP_ATKELE:
  3207. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  3208. switch (sd->state.lr_flag)
  3209. {
  3210. case LR_FLAG_ARROW:
  3211. switch (sd->status.weapon) {
  3212. case W_BOW:
  3213. case W_REVOLVER:
  3214. case W_RIFLE:
  3215. case W_GATLING:
  3216. case W_SHOTGUN:
  3217. case W_GRENADE:
  3218. //Become weapon element.
  3219. status->rhw.ele=val;
  3220. break;
  3221. default: //Become arrow element.
  3222. sd->bonus.arrow_ele=val;
  3223. break;
  3224. }
  3225. break;
  3226. case LR_FLAG_WEAPON:
  3227. status->lhw.ele=val;
  3228. break;
  3229. default:
  3230. status->rhw.ele=val;
  3231. break;
  3232. }
  3233. break;
  3234. case SP_DEFELE:
  3235. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  3236. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3237. status->def_ele=val;
  3238. break;
  3239. case SP_MAXHP:
  3240. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3241. break;
  3242. sd->bonus.hp += val;
  3243. break;
  3244. case SP_MAXSP:
  3245. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3246. break;
  3247. sd->bonus.sp += val;
  3248. break;
  3249. case SP_MAXAP:
  3250. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3251. break;
  3252. sd->bonus.ap += val;
  3253. break;
  3254. case SP_MAXHPRATE:
  3255. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3256. sd->hprate+=val;
  3257. break;
  3258. case SP_MAXSPRATE:
  3259. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3260. sd->sprate+=val;
  3261. break;
  3262. case SP_MAXAPRATE:
  3263. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3264. sd->aprate += val;
  3265. break;
  3266. case SP_SPRATE:
  3267. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3268. sd->dsprate+=val;
  3269. break;
  3270. case SP_ATTACKRANGE:
  3271. switch (sd->state.lr_flag) {
  3272. case LR_FLAG_ARROW:
  3273. switch (sd->status.weapon) {
  3274. case W_BOW:
  3275. case W_REVOLVER:
  3276. case W_RIFLE:
  3277. case W_GATLING:
  3278. case W_SHOTGUN:
  3279. case W_GRENADE:
  3280. status->rhw.range += val;
  3281. }
  3282. break;
  3283. case LR_FLAG_WEAPON:
  3284. status->lhw.range += val;
  3285. break;
  3286. default:
  3287. status->rhw.range += val;
  3288. break;
  3289. }
  3290. break;
  3291. case SP_SPEED_RATE: //Non stackable increase
  3292. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3293. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  3294. break;
  3295. case SP_SPEED_ADDRATE: //Stackable increase
  3296. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3297. sd->bonus.speed_add_rate -= val;
  3298. break;
  3299. case SP_ASPD: //Raw increase
  3300. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3301. sd->bonus.aspd_add -= 10*val;
  3302. break;
  3303. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  3304. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3305. #ifndef RENEWAL_ASPD
  3306. status->aspd_rate -= 10*val;
  3307. #else
  3308. status->aspd_rate2 += val;
  3309. #endif
  3310. break;
  3311. case SP_HP_RECOV_RATE:
  3312. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3313. sd->hprecov_rate += val;
  3314. break;
  3315. case SP_SP_RECOV_RATE:
  3316. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3317. sd->sprecov_rate += val;
  3318. break;
  3319. case SP_CRITICAL_DEF:
  3320. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3321. sd->bonus.critical_def += val;
  3322. break;
  3323. case SP_NEAR_ATK_DEF:
  3324. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3325. sd->bonus.near_attack_def_rate += val;
  3326. break;
  3327. case SP_LONG_ATK_DEF:
  3328. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3329. sd->bonus.long_attack_def_rate += val;
  3330. break;
  3331. case SP_DOUBLE_RATE:
  3332. if (sd->state.lr_flag == LR_FLAG_NONE && sd->bonus.double_rate < val)
  3333. sd->bonus.double_rate = val;
  3334. break;
  3335. case SP_DOUBLE_ADD_RATE:
  3336. if (sd->state.lr_flag == LR_FLAG_NONE)
  3337. sd->bonus.double_add_rate += val;
  3338. break;
  3339. case SP_MATK_RATE:
  3340. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3341. sd->matk_rate += val;
  3342. break;
  3343. case SP_IGNORE_DEF_ELE:
  3344. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  3345. if (sd->state.lr_flag == LR_FLAG_NONE)
  3346. sd->right_weapon.ignore_def_ele |= 1<<val;
  3347. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3348. sd->left_weapon.ignore_def_ele |= 1<<val;
  3349. break;
  3350. case SP_IGNORE_DEF_RACE:
  3351. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  3352. if (sd->state.lr_flag == LR_FLAG_NONE)
  3353. sd->right_weapon.ignore_def_race |= 1<<val;
  3354. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3355. sd->left_weapon.ignore_def_race |= 1<<val;
  3356. break;
  3357. case SP_IGNORE_DEF_CLASS:
  3358. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  3359. if (sd->state.lr_flag == LR_FLAG_NONE)
  3360. sd->right_weapon.ignore_def_class |= 1<<val;
  3361. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3362. sd->left_weapon.ignore_def_class |= 1<<val;
  3363. break;
  3364. case SP_ATK_RATE:
  3365. #ifdef RENEWAL
  3366. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3367. sd->bonus.atk_rate += val;
  3368. #else
  3369. ShowError( "pc_bonus: %s is not supported in Pre-Renewal mode.\n", QUOTE( SP_ATK_RATE ) );
  3370. #endif
  3371. break;
  3372. case SP_MAGIC_ATK_DEF:
  3373. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3374. sd->bonus.magic_def_rate += val;
  3375. break;
  3376. case SP_MISC_ATK_DEF:
  3377. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3378. sd->bonus.misc_def_rate += val;
  3379. break;
  3380. case SP_IGNORE_MDEF_ELE:
  3381. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3382. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3383. sd->bonus.ignore_mdef_ele |= 1<<val;
  3384. break;
  3385. case SP_IGNORE_MDEF_RACE:
  3386. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3387. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3388. sd->bonus.ignore_mdef_race |= 1<<val;
  3389. break;
  3390. case SP_PERFECT_HIT_RATE:
  3391. if (sd->state.lr_flag != LR_FLAG_ARROW && sd->bonus.perfect_hit < val)
  3392. sd->bonus.perfect_hit = val;
  3393. break;
  3394. case SP_PERFECT_HIT_ADD_RATE:
  3395. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3396. sd->bonus.perfect_hit_add += val;
  3397. break;
  3398. case SP_CRITICAL_RATE:
  3399. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3400. sd->critical_rate+=val;
  3401. break;
  3402. case SP_DEF_RATIO_ATK_ELE:
  3403. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3404. if (sd->state.lr_flag == LR_FLAG_NONE)
  3405. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3406. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3407. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3408. break;
  3409. case SP_DEF_RATIO_ATK_RACE:
  3410. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3411. if (sd->state.lr_flag == LR_FLAG_NONE)
  3412. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3413. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3414. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3415. break;
  3416. case SP_DEF_RATIO_ATK_CLASS:
  3417. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3418. if (sd->state.lr_flag == LR_FLAG_NONE)
  3419. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3420. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3421. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3422. break;
  3423. case SP_HIT_RATE:
  3424. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3425. sd->hit_rate += val;
  3426. break;
  3427. case SP_FLEE_RATE:
  3428. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3429. sd->flee_rate += val;
  3430. break;
  3431. case SP_FLEE2_RATE:
  3432. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3433. sd->flee2_rate += val;
  3434. break;
  3435. case SP_DEF_RATE:
  3436. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3437. sd->def_rate += val;
  3438. break;
  3439. case SP_DEF2_RATE:
  3440. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3441. sd->def2_rate += val;
  3442. break;
  3443. case SP_MDEF_RATE:
  3444. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3445. sd->mdef_rate += val;
  3446. break;
  3447. case SP_MDEF2_RATE:
  3448. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3449. sd->mdef2_rate += val;
  3450. break;
  3451. case SP_PATK_RATE:
  3452. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3453. sd->patk_rate += val;
  3454. break;
  3455. case SP_SMATK_RATE:
  3456. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3457. sd->smatk_rate += val;
  3458. break;
  3459. case SP_RES_RATE:
  3460. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3461. sd->res_rate += val;
  3462. break;
  3463. case SP_MRES_RATE:
  3464. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3465. sd->mres_rate += val;
  3466. break;
  3467. case SP_HPLUS_RATE:
  3468. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3469. sd->hplus_rate += val;
  3470. break;
  3471. case SP_CRATE_RATE:
  3472. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3473. sd->crate_rate += val;
  3474. break;
  3475. case SP_RESTART_FULL_RECOVER:
  3476. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3477. sd->special_state.restart_full_recover = 1;
  3478. break;
  3479. case SP_NO_CASTCANCEL:
  3480. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3481. sd->special_state.no_castcancel = 1;
  3482. break;
  3483. case SP_NO_CASTCANCEL2:
  3484. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3485. sd->special_state.no_castcancel2 = 1;
  3486. break;
  3487. case SP_NO_SIZEFIX:
  3488. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3489. sd->special_state.no_sizefix = 1;
  3490. break;
  3491. case SP_NO_MAGIC_DAMAGE:
  3492. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3493. break;
  3494. val+= sd->special_state.no_magic_damage;
  3495. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3496. break;
  3497. case SP_NO_WEAPON_DAMAGE:
  3498. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3499. break;
  3500. val+= sd->special_state.no_weapon_damage;
  3501. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3502. break;
  3503. case SP_NO_MISC_DAMAGE:
  3504. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3505. break;
  3506. val+= sd->special_state.no_misc_damage;
  3507. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3508. break;
  3509. case SP_NO_GEMSTONE:
  3510. if (sd->state.lr_flag != LR_FLAG_ARROW && sd->special_state.no_gemstone != 2)
  3511. sd->special_state.no_gemstone = 1;
  3512. break;
  3513. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3514. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3515. sd->special_state.intravision = 1;
  3516. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3517. }
  3518. break;
  3519. case SP_NO_KNOCKBACK:
  3520. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3521. sd->special_state.no_knockback = 1;
  3522. break;
  3523. case SP_SPLASH_RANGE:
  3524. if(sd->bonus.splash_range < val)
  3525. sd->bonus.splash_range = val;
  3526. break;
  3527. case SP_SPLASH_ADD_RANGE:
  3528. sd->bonus.splash_add_range += val;
  3529. break;
  3530. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3531. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3532. sd->bonus.short_weapon_damage_return += val;
  3533. break;
  3534. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3535. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3536. sd->bonus.long_weapon_damage_return += val;
  3537. break;
  3538. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3539. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3540. sd->bonus.magic_damage_return += val;
  3541. break;
  3542. case SP_REDUCE_DAMAGE_RETURN:
  3543. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3544. sd->bonus.reduce_damage_return += val;
  3545. break;
  3546. case SP_ALL_STATS: // [Valaris]
  3547. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3548. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3549. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3550. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3551. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3552. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3553. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3554. }
  3555. break;
  3556. case SP_ALL_TRAIT_STATS:
  3557. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3558. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3559. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3560. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3561. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3562. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3563. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3564. }
  3565. break;
  3566. case SP_AGI_VIT: // [Valaris]
  3567. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3568. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3569. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3570. }
  3571. break;
  3572. case SP_AGI_DEX_STR: // [Valaris]
  3573. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3574. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3575. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3576. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3577. }
  3578. break;
  3579. case SP_PERFECT_HIDE: // [Valaris]
  3580. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3581. sd->special_state.perfect_hiding=1;
  3582. break;
  3583. case SP_UNBREAKABLE:
  3584. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3585. sd->bonus.unbreakable += val;
  3586. break;
  3587. case SP_UNBREAKABLE_WEAPON:
  3588. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3589. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3590. break;
  3591. case SP_UNBREAKABLE_ARMOR:
  3592. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3593. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3594. break;
  3595. case SP_UNBREAKABLE_HELM:
  3596. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3597. sd->bonus.unbreakable_equip |= EQP_HELM;
  3598. break;
  3599. case SP_UNBREAKABLE_SHIELD:
  3600. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3601. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3602. break;
  3603. case SP_UNBREAKABLE_GARMENT:
  3604. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3605. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3606. break;
  3607. case SP_UNBREAKABLE_SHOES:
  3608. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3609. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3610. break;
  3611. case SP_CLASSCHANGE: // [Valaris]
  3612. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3613. sd->bonus.classchange=val;
  3614. break;
  3615. case SP_SHORT_ATK_RATE:
  3616. if (sd->state.lr_flag != LR_FLAG_ARROW) //[Lupus] it should stack, too. As any other cards rate bonuses
  3617. sd->bonus.short_attack_atk_rate+=val;
  3618. break;
  3619. case SP_LONG_ATK_RATE:
  3620. if (sd->state.lr_flag != LR_FLAG_ARROW) //[Lupus] it should stack, too. As any other cards rate bonuses
  3621. sd->bonus.long_attack_atk_rate+=val;
  3622. break;
  3623. case SP_BREAK_WEAPON_RATE:
  3624. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3625. sd->bonus.break_weapon_rate+=val;
  3626. break;
  3627. case SP_BREAK_ARMOR_RATE:
  3628. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3629. sd->bonus.break_armor_rate+=val;
  3630. break;
  3631. case SP_ADD_STEAL_RATE:
  3632. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3633. sd->bonus.add_steal_rate+=val;
  3634. break;
  3635. case SP_DELAYRATE:
  3636. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3637. sd->bonus.delayrate -= val;
  3638. break;
  3639. case SP_CRIT_ATK_RATE:
  3640. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3641. sd->bonus.crit_atk_rate += val;
  3642. break;
  3643. case SP_CRIT_DEF_RATE:
  3644. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3645. sd->bonus.crit_def_rate += val;
  3646. break;
  3647. case SP_NO_REGEN:
  3648. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3649. sd->regen.state.block|=val;
  3650. break;
  3651. case SP_UNSTRIPABLE_WEAPON:
  3652. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3653. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3654. break;
  3655. case SP_UNSTRIPABLE:
  3656. case SP_UNSTRIPABLE_ARMOR:
  3657. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3658. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3659. break;
  3660. case SP_UNSTRIPABLE_HELM:
  3661. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3662. sd->bonus.unstripable_equip |= EQP_HELM;
  3663. break;
  3664. case SP_UNSTRIPABLE_SHIELD:
  3665. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3666. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3667. break;
  3668. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3669. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3670. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3671. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3672. } else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3673. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3674. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3675. }
  3676. break;
  3677. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3678. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3679. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3680. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3681. } else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3682. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3683. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3684. }
  3685. break;
  3686. case SP_SP_GAIN_VALUE:
  3687. if (sd->state.lr_flag == LR_FLAG_NONE)
  3688. sd->bonus.sp_gain_value += val;
  3689. break;
  3690. case SP_HP_GAIN_VALUE:
  3691. if (sd->state.lr_flag == LR_FLAG_NONE)
  3692. sd->bonus.hp_gain_value += val;
  3693. break;
  3694. case SP_LONG_SP_GAIN_VALUE:
  3695. if (sd->state.lr_flag == LR_FLAG_NONE)
  3696. sd->bonus.long_sp_gain_value += val;
  3697. break;
  3698. case SP_LONG_HP_GAIN_VALUE:
  3699. if (sd->state.lr_flag == LR_FLAG_NONE)
  3700. sd->bonus.long_hp_gain_value += val;
  3701. break;
  3702. case SP_MAGIC_SP_GAIN_VALUE:
  3703. if (sd->state.lr_flag == LR_FLAG_NONE)
  3704. sd->bonus.magic_sp_gain_value += val;
  3705. break;
  3706. case SP_MAGIC_HP_GAIN_VALUE:
  3707. if (sd->state.lr_flag == LR_FLAG_NONE)
  3708. sd->bonus.magic_hp_gain_value += val;
  3709. break;
  3710. case SP_ADD_HEAL_RATE:
  3711. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3712. sd->bonus.add_heal_rate += val;
  3713. break;
  3714. case SP_ADD_HEAL2_RATE:
  3715. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3716. sd->bonus.add_heal2_rate += val;
  3717. break;
  3718. case SP_ADD_ITEM_HEAL_RATE:
  3719. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3720. sd->bonus.itemhealrate2 += val;
  3721. break;
  3722. case SP_EMATK:
  3723. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3724. sd->bonus.ematk += val;
  3725. break;
  3726. case SP_ADD_VARIABLECAST:
  3727. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3728. sd->bonus.add_varcast += val;
  3729. break;
  3730. #ifdef RENEWAL_CAST
  3731. case SP_FIXCASTRATE:
  3732. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3733. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3734. break;
  3735. case SP_ADD_FIXEDCAST:
  3736. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3737. sd->bonus.add_fixcast += val;
  3738. break;
  3739. case SP_CASTRATE:
  3740. case SP_VARCASTRATE:
  3741. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3742. sd->bonus.varcastrate -= val;
  3743. break;
  3744. #else
  3745. case SP_ADD_FIXEDCAST:
  3746. case SP_FIXCASTRATE:
  3747. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3748. break;
  3749. case SP_VARCASTRATE:
  3750. case SP_CASTRATE:
  3751. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3752. sd->castrate += val;
  3753. break;
  3754. #endif
  3755. case SP_ADDMAXWEIGHT:
  3756. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3757. sd->add_max_weight += val;
  3758. break;
  3759. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3760. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3761. break;
  3762. case SP_ABSORB_DMG_MAXHP2:
  3763. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3764. break;
  3765. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3766. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3767. sd->bonus.critical_rangeatk += val*10;
  3768. else
  3769. sd->bonus.arrow_cri += val*10;
  3770. break;
  3771. case SP_WEAPON_ATK_RATE:
  3772. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3773. sd->bonus.weapon_atk_rate += val;
  3774. break;
  3775. case SP_WEAPON_MATK_RATE:
  3776. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3777. sd->bonus.weapon_matk_rate += val;
  3778. break;
  3779. case SP_NO_MADO_FUEL:
  3780. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3781. sd->special_state.no_mado_fuel = 1;
  3782. break;
  3783. case SP_NO_WALK_DELAY:
  3784. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3785. sd->special_state.no_walk_delay = 1;
  3786. break;
  3787. case SP_ADD_ITEM_SPHEAL_RATE:
  3788. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3789. sd->bonus.itemsphealrate2 += val;
  3790. break;
  3791. default:
  3792. if (current_equip_combo_pos > 0) {
  3793. ShowWarning("pc_bonus: unknown bonus type %d %d in a combo with item #%u\n", type, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  3794. }
  3795. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3796. ShowWarning("pc_bonus: unknown bonus type %d %d in item #%u\n", type, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  3797. }
  3798. else {
  3799. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3800. }
  3801. break;
  3802. }
  3803. }
  3804. /*==========================================
  3805. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3806. * format: bonus2 bBonusName,type2,val;
  3807. * @param sd
  3808. * @param type Bonus type used by bBonusName
  3809. * @param type2
  3810. * @param val Value that usually for rate or fixed value
  3811. *------------------------------------------*/
  3812. void pc_bonus2(map_session_data *sd,int32 type,int32 type2,int32 val)
  3813. {
  3814. nullpo_retv(sd);
  3815. switch(type){
  3816. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3817. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3818. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3819. sd->right_weapon.addele[type2]+=val;
  3820. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3821. sd->left_weapon.addele[type2]+=val;
  3822. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3823. sd->indexed_bonus.arrow_addele[type2]+=val;
  3824. break;
  3825. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3826. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3827. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3828. sd->right_weapon.addrace[type2]+=val;
  3829. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3830. sd->left_weapon.addrace[type2]+=val;
  3831. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3832. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3833. break;
  3834. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3835. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3836. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3837. sd->right_weapon.addclass[type2]+=val;
  3838. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3839. sd->left_weapon.addclass[type2]+=val;
  3840. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3841. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3842. break;
  3843. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3844. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3845. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3846. sd->right_weapon.addsize[type2]+=val;
  3847. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3848. sd->left_weapon.addsize[type2]+=val;
  3849. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3850. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3851. break;
  3852. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3853. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3854. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3855. sd->indexed_bonus.subele_script[type2] += val;
  3856. break;
  3857. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3858. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3859. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3860. sd->indexed_bonus.subrace[type2]+=val;
  3861. break;
  3862. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3863. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3864. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3865. sd->indexed_bonus.subclass[type2]+=val;
  3866. break;
  3867. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3868. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3869. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != LR_FLAG_ARROW ? val : 0, sd->state.lr_flag == LR_FLAG_ARROW ? val : 0, 0, 0);
  3870. break;
  3871. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3872. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3873. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != LR_FLAG_ARROW ? val : 0, sd->state.lr_flag == LR_FLAG_ARROW ? val : 0, ATF_SELF, 0);
  3874. break;
  3875. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3876. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3877. break;
  3878. if (sd->reseff.size() == MAX_PC_BONUS) {
  3879. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3880. break;
  3881. }
  3882. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3883. break;
  3884. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3885. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3886. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3887. sd->indexed_bonus.magic_addele_script[type2] += val;
  3888. break;
  3889. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3890. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3891. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3892. sd->indexed_bonus.magic_addrace[type2]+=val;
  3893. break;
  3894. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3895. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3896. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3897. sd->indexed_bonus.magic_addclass[type2]+=val;
  3898. break;
  3899. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3900. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3901. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3902. sd->indexed_bonus.magic_addsize[type2]+=val;
  3903. break;
  3904. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3905. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3906. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3907. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3908. break;
  3909. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3910. struct weapon_data *wd = (sd->state.lr_flag != LR_FLAG_NONE ? &sd->left_weapon : &sd->right_weapon);
  3911. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3912. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3913. break;
  3914. }
  3915. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3916. }
  3917. break;
  3918. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3919. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3920. break;
  3921. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3922. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3923. break;
  3924. }
  3925. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3926. break;
  3927. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3928. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3929. break;
  3930. if (sd->add_def.size() == MAX_PC_BONUS) {
  3931. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3932. break;
  3933. }
  3934. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3935. break;
  3936. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3937. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3938. break;
  3939. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3940. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3941. break;
  3942. }
  3943. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3944. break;
  3945. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3946. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3947. sd->right_weapon.hp_drain_rate.rate += type2;
  3948. sd->right_weapon.hp_drain_rate.per += val;
  3949. }
  3950. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3951. sd->left_weapon.hp_drain_rate.rate += type2;
  3952. sd->left_weapon.hp_drain_rate.per += val;
  3953. }
  3954. break;
  3955. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3956. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3957. sd->right_weapon.sp_drain_rate.rate += type2;
  3958. sd->right_weapon.sp_drain_rate.per += val;
  3959. }
  3960. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3961. sd->left_weapon.sp_drain_rate.rate += type2;
  3962. sd->left_weapon.sp_drain_rate.per += val;
  3963. }
  3964. break;
  3965. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3966. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3967. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3968. }
  3969. break;
  3970. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3971. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3972. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3973. }
  3974. break;
  3975. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3976. if (sd->state.lr_flag != LR_FLAG_ARROW && sd->bonus.get_zeny_rate < val) {
  3977. sd->bonus.get_zeny_rate = val;
  3978. sd->bonus.get_zeny_num = type2;
  3979. }
  3980. break;
  3981. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3982. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3983. sd->bonus.get_zeny_rate += val;
  3984. sd->bonus.get_zeny_num += type2;
  3985. }
  3986. break;
  3987. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3988. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3989. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3990. break;
  3991. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3992. sd->special_state.bonus_coma = 1;
  3993. break;
  3994. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3995. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3996. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3997. break;
  3998. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3999. sd->special_state.bonus_coma = 1;
  4000. break;
  4001. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  4002. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  4003. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4004. break;
  4005. sd->indexed_bonus.weapon_coma_class[type2] += val;
  4006. sd->special_state.bonus_coma = 1;
  4007. break;
  4008. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  4009. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4010. sd->indexed_bonus.weapon_atk[type2]+=val;
  4011. break;
  4012. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  4013. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4014. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  4015. break;
  4016. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  4017. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  4018. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4019. sd->indexed_bonus.critaddrace[type2] += val*10;
  4020. break;
  4021. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  4022. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4023. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4024. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  4025. break;
  4026. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  4027. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4028. break;
  4029. if (sd->skillatk.size() == MAX_PC_BONUS) {
  4030. ShowWarning("pc_bonus2: SP_SKILL_ATK: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4031. break;
  4032. }
  4033. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  4034. break;
  4035. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  4036. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4037. break;
  4038. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4039. ShowWarning("pc_bonus2: SP_SKILL_HEAL: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4040. break;
  4041. }
  4042. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  4043. break;
  4044. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  4045. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4046. break;
  4047. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4048. ShowWarning("pc_bonus2: SP_SKILL_HEAL2: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4049. break;
  4050. }
  4051. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  4052. break;
  4053. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  4054. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4055. break;
  4056. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4057. ShowWarning("pc_bonus2: SP_ADD_SKILL_BLOW: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  4058. break;
  4059. }
  4060. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  4061. break;
  4062. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  4063. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4064. sd->hp_loss.value = type2;
  4065. sd->hp_loss.rate = val;
  4066. }
  4067. break;
  4068. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  4069. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4070. sd->hp_regen.value = type2;
  4071. sd->hp_regen.rate = val;
  4072. }
  4073. break;
  4074. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  4075. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4076. sd->percent_hp_regen.value = type2;
  4077. sd->percent_hp_regen.rate = val;
  4078. }
  4079. break;
  4080. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  4081. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4082. sd->percent_sp_regen.value = type2;
  4083. sd->percent_sp_regen.rate = val;
  4084. }
  4085. break;
  4086. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  4087. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  4088. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4089. sd->right_weapon.addrace2[type2] += val;
  4090. else
  4091. sd->left_weapon.addrace2[type2] += val;
  4092. break;
  4093. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  4094. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  4095. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4096. sd->indexed_bonus.subsize[type2]+=val;
  4097. break;
  4098. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  4099. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  4100. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4101. sd->indexed_bonus.weapon_subsize[type2] += val;
  4102. break;
  4103. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  4104. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  4105. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4106. sd->indexed_bonus.magic_subsize[type2]+=val;
  4107. break;
  4108. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  4109. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  4110. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4111. sd->indexed_bonus.subrace2[type2]+=val;
  4112. break;
  4113. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  4114. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4115. break;
  4116. if( !item_db.exists( type2 ) ){
  4117. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  4118. break;
  4119. }
  4120. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  4121. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4122. break;
  4123. }
  4124. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  4125. break;
  4126. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  4127. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4128. break;
  4129. if (!type2 || !itemdb_group.exists(type2)) {
  4130. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  4131. break;
  4132. }
  4133. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  4134. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4135. break;
  4136. }
  4137. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  4138. break;
  4139. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  4140. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  4141. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4142. sd->indexed_bonus.expaddrace[type2]+=val;
  4143. break;
  4144. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  4145. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  4146. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4147. sd->indexed_bonus.expaddclass[type2]+=val;
  4148. break;
  4149. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  4150. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  4151. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4152. sd->indexed_bonus.sp_gain_race[type2]+=val;
  4153. break;
  4154. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  4155. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4156. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  4157. break;
  4158. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  4159. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4160. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  4161. break;
  4162. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  4163. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4164. sd->sp_loss.value = type2;
  4165. sd->sp_loss.rate = val;
  4166. }
  4167. break;
  4168. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  4169. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4170. sd->sp_regen.value = type2;
  4171. sd->sp_regen.rate = val;
  4172. }
  4173. break;
  4174. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  4175. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  4176. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4177. sd->right_weapon.hp_drain_race[type2] += val;
  4178. }
  4179. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4180. sd->left_weapon.hp_drain_race[type2] += val;
  4181. }
  4182. break;
  4183. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  4184. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  4185. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4186. sd->right_weapon.sp_drain_race[type2] += val;
  4187. }
  4188. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4189. sd->left_weapon.sp_drain_race[type2] += val;
  4190. }
  4191. break;
  4192. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  4193. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  4194. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4195. sd->right_weapon.hp_drain_class[type2] += val;
  4196. }
  4197. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4198. sd->left_weapon.hp_drain_class[type2] += val;
  4199. }
  4200. break;
  4201. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  4202. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  4203. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4204. sd->right_weapon.sp_drain_class[type2] += val;
  4205. }
  4206. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4207. sd->left_weapon.sp_drain_class[type2] += val;
  4208. }
  4209. break;
  4210. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  4211. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  4212. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4213. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  4214. break;
  4215. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  4216. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  4217. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4218. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  4219. break;
  4220. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  4221. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  4222. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4223. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  4224. break;
  4225. case SP_SP_IGNORE_RES_RACE_RATE: // bonus2 bIgnoreResRaceRate,r,n;
  4226. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_RES_RACE_RATE);
  4227. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4228. sd->indexed_bonus.ignore_res_by_race[type2] += val;
  4229. break;
  4230. case SP_SP_IGNORE_MRES_RACE_RATE: // bonus2 bIgnoreMResRaceRate,r,n;
  4231. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_MRES_RACE_RATE);
  4232. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4233. sd->indexed_bonus.ignore_mres_by_race[type2] += val;
  4234. break;
  4235. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  4236. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  4237. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4238. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  4239. break;
  4240. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  4241. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4242. break;
  4243. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  4244. ShowWarning("pc_bonus2: SP_SKILL_USE_SP_RATE: Reached max (%d) number of skills per character, bonus skill %d (+%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4245. break;
  4246. }
  4247. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  4248. break;
  4249. case SP_SKILL_DELAY:
  4250. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4251. break;
  4252. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  4253. ShowWarning("pc_bonus2: SP_SKILL_DELAY: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  4254. break;
  4255. }
  4256. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  4257. break;
  4258. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  4259. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4260. break;
  4261. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  4262. ShowWarning("pc_bonus2: SP_SKILL_COOLDOWN: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  4263. break;
  4264. }
  4265. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  4266. break;
  4267. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  4268. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4269. break;
  4270. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  4271. ShowWarning("pc_bonus2: SP_SKILL_VARIABLECAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  4272. break;
  4273. }
  4274. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  4275. break;
  4276. #ifdef RENEWAL_CAST
  4277. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4278. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4279. break;
  4280. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  4281. ShowWarning("pc_bonus2: SP_SKILL_FIXEDCAST: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  4282. break;
  4283. }
  4284. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  4285. break;
  4286. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4287. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4288. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4289. break;
  4290. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  4291. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4292. break;
  4293. }
  4294. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  4295. break;
  4296. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4297. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4298. break;
  4299. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  4300. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4301. break;
  4302. }
  4303. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  4304. break;
  4305. #else
  4306. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4307. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4308. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  4309. break;
  4310. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4311. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4312. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4313. break;
  4314. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4315. ShowWarning("pc_bonus2: %s: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", (type == SP_CASTRATE) ? "SP_CASTRATE" : "SP_VARCASTRATE", MAX_PC_BONUS, type2, val);
  4316. break;
  4317. }
  4318. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  4319. break;
  4320. #endif
  4321. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  4322. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4323. break;
  4324. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  4325. ShowWarning("pc_bonus2: SP_SKILL_USE_SP: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  4326. break;
  4327. }
  4328. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4329. break;
  4330. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4331. if (sd->subskill.size() == MAX_PC_BONUS) {
  4332. ShowWarning("pc_bonus2: SP_SUB_SKILL: Reached max (%d) number of skills per character, bonus skill %d (%d) lost.\n", MAX_PC_BONUS, type2, val);
  4333. break;
  4334. }
  4335. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4336. break;
  4337. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4338. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4339. sd->indexed_bonus.subdefele[type2] += val;
  4340. break;
  4341. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4342. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4343. sd->indexed_bonus.coma_class[type2] += val;
  4344. sd->special_state.bonus_coma = 1;
  4345. break;
  4346. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4347. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4348. sd->indexed_bonus.coma_race[type2] += val;
  4349. sd->special_state.bonus_coma = 1;
  4350. break;
  4351. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4352. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4353. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4354. sd->indexed_bonus.magic_addrace2[type2] += val;
  4355. break;
  4356. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4357. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4358. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4359. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4360. break;
  4361. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4362. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4363. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4364. sd->indexed_bonus.dropaddrace[type2] += val;
  4365. break;
  4366. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4367. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4368. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4369. sd->indexed_bonus.dropaddclass[type2] += val;
  4370. break;
  4371. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4372. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4373. sd->indexed_bonus.magic_subdefele[type2] += val;
  4374. break;
  4375. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4376. if( sd->state.lr_flag == LR_FLAG_ARROW ){
  4377. break;
  4378. }
  4379. if( !item_db.exists( type2 ) ){
  4380. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4381. break;
  4382. }
  4383. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4384. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4385. break;
  4386. }
  4387. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4388. break;
  4389. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4390. if( sd->state.lr_flag == LR_FLAG_ARROW ){
  4391. break;
  4392. }
  4393. if( !type2 || !itemdb_group.exists( type2 ) ){
  4394. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4395. break;
  4396. }
  4397. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4398. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4399. break;
  4400. }
  4401. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4402. break;
  4403. default:
  4404. if (current_equip_combo_pos > 0) {
  4405. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in a combo with item #%u\n", type, type2, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4406. }
  4407. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4408. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in item #%u\n", type, type2, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4409. }
  4410. else {
  4411. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4412. }
  4413. break;
  4414. }
  4415. }
  4416. /**
  4417. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4418. * @param sd
  4419. * @param type Bonus type used by bBonusName
  4420. * @param type2
  4421. * @param type3
  4422. * @param val Value that usually for rate or fixed value
  4423. */
  4424. void pc_bonus3(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 val)
  4425. {
  4426. nullpo_retv(sd);
  4427. switch(type){
  4428. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4429. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4430. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4431. break;
  4432. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  4433. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4434. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  4435. break;
  4436. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4437. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4438. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4439. break;
  4440. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4441. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4442. {
  4443. int32 target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4444. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4445. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4446. }
  4447. break;
  4448. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4449. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4450. {
  4451. int32 target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4452. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4453. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4454. }
  4455. break;
  4456. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4457. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4458. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4459. break;
  4460. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4461. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4462. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4463. break;
  4464. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4465. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4466. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != LR_FLAG_ARROW ? type3 : 0, sd->state.lr_flag == LR_FLAG_ARROW ? type3 : 0, val, 0);
  4467. break;
  4468. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4469. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4470. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4471. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4472. break;
  4473. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4474. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4475. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4476. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4477. break;
  4478. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4479. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4480. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4481. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4482. break;
  4483. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4484. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4485. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4486. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4487. break;
  4488. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4489. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4490. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4491. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4492. break;
  4493. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4494. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4495. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4496. sd->sp_vanish_race[type2].rate += type3;
  4497. sd->sp_vanish_race[type2].per += val;
  4498. }
  4499. break;
  4500. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4501. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4502. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4503. sd->hp_vanish_race[type2].rate += type3;
  4504. sd->hp_vanish_race[type2].per += val;
  4505. }
  4506. break;
  4507. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4508. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4509. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4510. }
  4511. break;
  4512. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4513. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4514. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4515. }
  4516. break;
  4517. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4518. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4519. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4520. break;
  4521. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4522. //if (type3 > sd->norecover_state_race[type2].rate) {
  4523. // sd->norecover_state_race[type2].rate = type3;
  4524. // sd->norecover_state_race[type2].tick = val;
  4525. // break;
  4526. //}
  4527. sd->norecover_state_race[type2].rate = type3;
  4528. sd->norecover_state_race[type2].tick = val;
  4529. break;
  4530. default:
  4531. if (current_equip_combo_pos > 0) {
  4532. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in a combo with item #%u\n", type, type2, type3, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4533. }
  4534. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4535. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in item #%u\n", type, type2, type3, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4536. }
  4537. else {
  4538. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4539. }
  4540. break;
  4541. }
  4542. }
  4543. /**
  4544. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4545. * @param sd
  4546. * @param type Bonus type used by bBonusName
  4547. * @param type2
  4548. * @param type3
  4549. * @param type4
  4550. * @param val Value that usually for rate or fixed value
  4551. */
  4552. void pc_bonus4(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 type4,int32 val)
  4553. {
  4554. nullpo_retv(sd);
  4555. switch(type){
  4556. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4557. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4558. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4559. break;
  4560. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4561. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4562. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4563. break;
  4564. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4565. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4566. {
  4567. int32 target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4568. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4569. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4570. }
  4571. break;
  4572. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4573. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4574. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != LR_FLAG_ARROW ? type3 : 0, sd->state.lr_flag == LR_FLAG_ARROW ? type3 : 0, type4, val);
  4575. break;
  4576. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4577. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4578. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4579. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4580. break;
  4581. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4582. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4583. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4584. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4585. break;
  4586. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4587. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4588. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4589. break;
  4590. sd->def_set_race[type2].rate = type3;
  4591. sd->def_set_race[type2].tick = type4;
  4592. sd->def_set_race[type2].value = val;
  4593. break;
  4594. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4595. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4596. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4597. break;
  4598. sd->mdef_set_race[type2].rate = type3;
  4599. sd->mdef_set_race[type2].tick = type4;
  4600. sd->mdef_set_race[type2].value = val;
  4601. break;
  4602. default:
  4603. if (current_equip_combo_pos > 0) {
  4604. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in a combo with item #%u\n", type, type2, type3, type4, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4605. }
  4606. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4607. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in item #%u\n", type, type2, type3, type4, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4608. }
  4609. else {
  4610. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4611. }
  4612. break;
  4613. }
  4614. }
  4615. /**
  4616. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4617. * @param sd
  4618. * @param type Bonus type used by bBonusName
  4619. * @param type2
  4620. * @param type3
  4621. * @param type4
  4622. * @param val Value that usually for rate or fixed value
  4623. */
  4624. void pc_bonus5(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 type4,int32 type5,int32 val)
  4625. {
  4626. nullpo_retv(sd);
  4627. switch(type){
  4628. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4629. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4630. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4631. break;
  4632. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4633. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4634. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4635. break;
  4636. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4637. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4638. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4639. break;
  4640. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4641. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4642. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4643. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4644. break;
  4645. default:
  4646. if (current_equip_combo_pos > 0) {
  4647. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in a combo with item #%u\n", type, type2, type3, type4, type5, val, sd->inventory_data[pc_checkequip( sd, current_equip_combo_pos )]->nameid);
  4648. }
  4649. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4650. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in item #%u\n", type, type2, type3, type4, type5, val, current_equip_card_id ? current_equip_card_id : sd->inventory_data[current_equip_item_index]->nameid);
  4651. }
  4652. else {
  4653. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4654. }
  4655. break;
  4656. }
  4657. }
  4658. /*==========================================
  4659. * Grants a player a given skill. Flag values are:
  4660. * 0 - Grant permanent skill to be bound to skill tree
  4661. * 1 - Grant an item skill (temporary)
  4662. * 2 - Like 1, except the level granted can stack with previously learned level.
  4663. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4664. *------------------------------------------*/
  4665. bool pc_skill(map_session_data* sd, uint16 skill_id, int32 level, enum e_addskill_type type) {
  4666. uint16 idx = 0;
  4667. nullpo_ret(sd);
  4668. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4669. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4670. return false;
  4671. }
  4672. if (level > MAX_SKILL_LEVEL) {
  4673. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4674. return false;
  4675. }
  4676. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4677. ShowWarning("pc_skill: Skill level bonus %d too high. Max lv supported is %d. Curr lv is %d. Set to max level.\n", level, MAX_SKILL_LEVEL, sd->status.skill[idx].lv);
  4678. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4679. }
  4680. switch (type) {
  4681. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4682. sd->status.skill[idx].id = skill_id;
  4683. sd->status.skill[idx].lv = level;
  4684. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4685. if (level == 0) { //Remove skill.
  4686. sd->status.skill[idx].id = 0;
  4687. clif_deleteskill(*sd,skill_id);
  4688. } else
  4689. clif_addskill(*sd,skill_id);
  4690. if (!skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20) //Only recalculate for passive skills.
  4691. status_calc_pc(sd, SCO_NONE);
  4692. break;
  4693. case ADDSKILL_TEMP: //Item bonus skill.
  4694. if (sd->status.skill[idx].id != 0) {
  4695. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4696. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4697. } else {
  4698. sd->status.skill[idx].id = skill_id;
  4699. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4700. }
  4701. sd->status.skill[idx].lv = max(sd->status.skill[idx].lv, level);
  4702. break;
  4703. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4704. if (sd->status.skill[idx].id != 0) {
  4705. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4706. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4707. } else {
  4708. sd->status.skill[idx].id = skill_id;
  4709. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4710. }
  4711. sd->status.skill[idx].lv += level;
  4712. break;
  4713. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4714. sd->status.skill[idx].id = skill_id;
  4715. sd->status.skill[idx].lv = level;
  4716. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4717. if (level == 0) { //Remove skill.
  4718. sd->status.skill[idx].id = 0;
  4719. clif_deleteskill(*sd,skill_id);
  4720. } else
  4721. clif_addskill(*sd,skill_id);
  4722. if (!skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20) //Only recalculate for passive skills.
  4723. status_calc_pc(sd, SCO_NONE);
  4724. break;
  4725. default:
  4726. return false;
  4727. }
  4728. return true;
  4729. }
  4730. /**
  4731. * Set's a player's plagiarized skill.
  4732. * @param sd: Player
  4733. * @param skill_id: Skill to be plagiarized
  4734. * @param skill_lv: Skill level to be plagiarized
  4735. * @return True on success or false otherwise
  4736. */
  4737. bool pc_skill_plagiarism(map_session_data &sd, uint16 skill_id, uint16 skill_lv)
  4738. {
  4739. skill_id = skill_dummy2skill_id(skill_id);
  4740. uint16 idx = skill_get_index(skill_id);
  4741. // Use skill index, avoiding out-of-bound array [Cydh]
  4742. if (idx == 0) {
  4743. ShowWarning("pc_skill_plagiarism: invalid skill idx 0 for skill %d.\n", skill_id);
  4744. return false;
  4745. }
  4746. skill_lv = cap_value(skill_lv, 1, skill_get_max(skill_id));
  4747. int32 type = skill_isCopyable(&sd, skill_id);
  4748. if (type == 1) {
  4749. pc_skill_plagiarism_reset(sd, type);
  4750. sd.cloneskill_idx = idx;
  4751. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), skill_id);
  4752. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), skill_lv);
  4753. } else if (type == 2) {
  4754. pc_skill_plagiarism_reset(sd, type);
  4755. sd.reproduceskill_idx = idx;
  4756. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), skill_id);
  4757. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), skill_lv);
  4758. } else {
  4759. ShowWarning("pc_skill_plagiarism: skill %d is not copyable.\n", skill_id);
  4760. return false;
  4761. }
  4762. sd.status.skill[idx].id = skill_id;
  4763. sd.status.skill[idx].lv = static_cast<uint8>(skill_lv);
  4764. sd.status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
  4765. clif_addskill(sd, skill_id);
  4766. return true;
  4767. }
  4768. /**
  4769. * Clear plagiarized skills from a player.
  4770. * @param sd: Player
  4771. * @param type: 1 for Plagiarism or 2 for Reproduce
  4772. * @return True on success or false otherwise
  4773. */
  4774. bool pc_skill_plagiarism_reset(map_session_data &sd, uint8 type)
  4775. {
  4776. uint16 idx;
  4777. if (type == 1)
  4778. idx = sd.cloneskill_idx;
  4779. else if (type == 2)
  4780. idx = sd.reproduceskill_idx;
  4781. else {
  4782. ShowError("pc_skill_plagiarism_reset: Unknown type %d.\n", type);
  4783. return false;
  4784. }
  4785. if (sd.status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED) {
  4786. uint16 skill_id = sd.status.skill[idx].id;
  4787. sd.status.skill[idx].id = 0;
  4788. sd.status.skill[idx].lv = 0;
  4789. sd.status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4790. clif_deleteskill(sd, skill_id);
  4791. if (type == 1) {
  4792. sd.cloneskill_idx = 0;
  4793. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  4794. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  4795. }
  4796. else if (type == 2) {
  4797. sd.reproduceskill_idx = 0;
  4798. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), 0);
  4799. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  4800. }
  4801. }
  4802. return true;
  4803. }
  4804. /*==========================================
  4805. * Append a card to an item ?
  4806. *------------------------------------------*/
  4807. int32 pc_insert_card(map_session_data* sd, int32 idx_card, int32 idx_equip)
  4808. {
  4809. nullpo_ret(sd);
  4810. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4811. return 0;
  4812. }
  4813. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4814. return 0;
  4815. }
  4816. int32 i;
  4817. t_itemid nameid;
  4818. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4819. struct item_data* item_card = sd->inventory_data[idx_card];
  4820. if(item_eq == nullptr)
  4821. return 0; //Invalid item index.
  4822. if(item_card == nullptr)
  4823. return 0; //Invalid card index.
  4824. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4825. return 0; // target item missing
  4826. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4827. return 0; // target card missing
  4828. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4829. return 0; // only weapons and armor are allowed
  4830. if( item_card->type != IT_CARD )
  4831. return 0; // must be a card
  4832. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4833. return 0; // target must be identified
  4834. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4835. return 0; // card slots reserved for other purposes
  4836. if( (item_eq->equip & item_card->equip) == 0 )
  4837. return 0; // card cannot be compounded on this item type
  4838. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4839. return 0; // attempted to place shield card on left-hand weapon.
  4840. if( item_eq->type == IT_ARMOR && (item_card->equip & EQP_ACC) && ((item_card->equip & EQP_ACC) != EQP_ACC) && ((item_eq->equip & EQP_ACC) != (item_card->equip & EQP_ACC)) )
  4841. return 0; // specific accessory-card can only be inserted to specific accessory.
  4842. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4843. return 0; // item must be unequipped
  4844. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4845. if( i == item_eq->slots )
  4846. return 0; // no free slots
  4847. // remember the card id to insert
  4848. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4849. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4850. {// failed
  4851. clif_insert_card( *sd, idx_equip, idx_card, true );
  4852. }
  4853. else
  4854. {// success
  4855. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4856. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4857. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4858. clif_insert_card( *sd, idx_equip, idx_card, false );
  4859. }
  4860. return 0;
  4861. }
  4862. /**
  4863. * Returns the count of unidentified items with the option to identify too.
  4864. * @param sd: Player data
  4865. * @param identify_item: Whether or not to identify any unidentified items
  4866. * @return Unidentified items count
  4867. */
  4868. int32 pc_identifyall(map_session_data *sd, bool identify_item)
  4869. {
  4870. int32 unidentified_count = 0;
  4871. for (int32 i = 0; i < MAX_INVENTORY; i++) {
  4872. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4873. if (identify_item == true) {
  4874. sd->inventory.u.items_inventory[i].identify = 1;
  4875. clif_item_identified( *sd, i, false );
  4876. }
  4877. unidentified_count++;
  4878. }
  4879. }
  4880. return unidentified_count;
  4881. }
  4882. //
  4883. // Items
  4884. //
  4885. /*==========================================
  4886. * Update buying value by skills
  4887. *------------------------------------------*/
  4888. int32 pc_modifybuyvalue(map_session_data *sd,int32 orig_value)
  4889. {
  4890. int32 skill,val = orig_value,rate1 = 0,rate2 = 0;
  4891. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4892. rate1 = 5+skill*2-((skill==10)? 1:0);
  4893. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4894. rate2 = 5+skill*4;
  4895. if(rate1 < rate2) rate1 = rate2;
  4896. if(rate1)
  4897. val = (int32)((double)orig_value*(double)(100-rate1)/100.);
  4898. if(val < battle_config.min_shop_buy)
  4899. val = battle_config.min_shop_buy;
  4900. return val;
  4901. }
  4902. /*==========================================
  4903. * Update selling value by skills
  4904. *------------------------------------------*/
  4905. int32 pc_modifysellvalue(map_session_data *sd,int32 orig_value)
  4906. {
  4907. int32 skill,val = orig_value,rate = 0;
  4908. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4909. rate = 5+skill*2-((skill==10)? 1:0);
  4910. if(rate)
  4911. val = (int32)((double)orig_value*(double)(100+rate)/100.);
  4912. if (val < battle_config.min_shop_sell)
  4913. val = battle_config.min_shop_sell;
  4914. return val;
  4915. }
  4916. /*==========================================
  4917. * Checking if we have enough place on inventory for new item
  4918. * Make sure to take 30k as limit (for client I guess)
  4919. * @param sd
  4920. * @param nameid
  4921. * @param amount
  4922. * @return e_chkitem_result
  4923. *------------------------------------------*/
  4924. char pc_checkadditem(map_session_data *sd, t_itemid nameid, int32 amount)
  4925. {
  4926. int32 i;
  4927. struct item_data* data;
  4928. nullpo_ret(sd);
  4929. if(amount > MAX_AMOUNT)
  4930. return CHKADDITEM_OVERAMOUNT;
  4931. data = itemdb_search(nameid);
  4932. if(!itemdb_isstackable2(data))
  4933. return CHKADDITEM_NEW;
  4934. if( data->stack.inventory && amount > data->stack.amount )
  4935. return CHKADDITEM_OVERAMOUNT;
  4936. if (data->flag.guid)
  4937. return CHKADDITEM_NEW;
  4938. for(i=0;i<MAX_INVENTORY;i++){
  4939. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4940. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4941. if( amount > MAX_AMOUNT - sd->inventory.u.items_inventory[i].amount || ( data->stack.inventory && amount > data->stack.amount - sd->inventory.u.items_inventory[i].amount ) )
  4942. return CHKADDITEM_OVERAMOUNT;
  4943. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4944. if( i >= sd->status.inventory_slots ){
  4945. return CHKADDITEM_OVERAMOUNT;
  4946. }
  4947. return CHKADDITEM_EXIST;
  4948. }
  4949. }
  4950. return CHKADDITEM_NEW;
  4951. }
  4952. /*==========================================
  4953. * Return number of available place in inventory
  4954. * Each non stackable item will reduce place by 1
  4955. * @param sd
  4956. * @return Number of empty slots
  4957. *------------------------------------------*/
  4958. uint8 pc_inventoryblank(map_session_data *sd)
  4959. {
  4960. uint16 i;
  4961. uint8 b;
  4962. nullpo_ret(sd);
  4963. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4964. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4965. b++;
  4966. }
  4967. return b;
  4968. }
  4969. /**
  4970. * Attempts to remove zeny from player
  4971. * @param sd: Player
  4972. * @param zeny: Zeny removed
  4973. * @param type: Log type
  4974. * @param log_charid: (optional) From who to log (if not needed, use 0)
  4975. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4976. */
  4977. char pc_payzeny(map_session_data *sd, int32 zeny, enum e_log_pick_type type, uint32 log_charid)
  4978. {
  4979. nullpo_retr(2,sd);
  4980. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4981. if( zeny < 0 )
  4982. {
  4983. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4984. return 1;
  4985. }
  4986. if( sd->status.zeny < zeny )
  4987. return 1; //Not enough.
  4988. sd->status.zeny -= zeny;
  4989. clif_updatestatus(*sd,SP_ZENY);
  4990. log_zeny(*sd, type, log_charid, -zeny);
  4991. if( zeny > 0 && sd->state.showzeny ) {
  4992. char output[255];
  4993. sprintf(output, "Removed %dz.", zeny);
  4994. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4995. }
  4996. return 0;
  4997. }
  4998. /**
  4999. * Attempts to give zeny to player
  5000. * @param sd: Player
  5001. * @param type: Log type
  5002. * @param log_charid: (optional) From who to log (if not needed, use 0)
  5003. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  5004. */
  5005. char pc_getzeny(map_session_data *sd, int32 zeny, enum e_log_pick_type type, uint32 log_charid)
  5006. {
  5007. nullpo_retr(-1,sd);
  5008. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  5009. if( zeny < 0 )
  5010. {
  5011. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  5012. return 1;
  5013. }
  5014. if( zeny > MAX_ZENY - sd->status.zeny )
  5015. zeny = MAX_ZENY - sd->status.zeny;
  5016. sd->status.zeny += zeny;
  5017. clif_updatestatus(*sd,SP_ZENY);
  5018. log_zeny(*sd, type, log_charid, zeny);
  5019. if( zeny > 0 && sd->state.showzeny ) {
  5020. char output[255];
  5021. sprintf(output, "Gained %dz.", zeny);
  5022. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5023. }
  5024. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  5025. return 0;
  5026. }
  5027. /**
  5028. * Attempts to remove Cash Points from player
  5029. * @param sd: Player
  5030. * @param price: Total points (cash + kafra) the player has to pay
  5031. * @param points: Kafra points the player has to pay
  5032. * @param type: Log type
  5033. * @return -1: Not enough points, otherwise success (cash+points)
  5034. */
  5035. int32 pc_paycash(map_session_data *sd, int32 price, int32 points, e_log_pick_type type)
  5036. {
  5037. int32 cash;
  5038. nullpo_retr(-1,sd);
  5039. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5040. cash = price-points;
  5041. if( sd->cashPoints < cash || sd->kafraPoints < points )
  5042. {
  5043. ShowError("pc_paycash: Not enough points (cash=%d, kafra=%d) to cover the price (cash=%d, kafra=%d) (account_id=%d, char_id=%d).\n", sd->cashPoints, sd->kafraPoints, cash, points, sd->status.account_id, sd->status.char_id);
  5044. return -1;
  5045. }
  5046. if( cash ){
  5047. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  5048. sd->cashPoints -= cash;
  5049. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  5050. }
  5051. if( points ){
  5052. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  5053. sd->kafraPoints -= points;
  5054. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  5055. }
  5056. if( battle_config.cashshop_show_points )
  5057. {
  5058. char output[CHAT_SIZE_MAX];
  5059. sprintf(output, msg_txt(sd,504), points, cash, sd->kafraPoints, sd->cashPoints); // Used %d kafra points and %d cash points. %d kafra and %d cash points remaining.
  5060. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5061. }
  5062. return cash+points;
  5063. }
  5064. /**
  5065. * Attempts to give Cash Points to player
  5066. * @param sd: Player
  5067. * @param cash: Cash points the player gets
  5068. * @param points: Kafra points the player gets
  5069. * @param type: Log type
  5070. * @return -1: Error, otherwise success (cash or points)
  5071. */
  5072. int32 pc_getcash(map_session_data *sd, int32 cash, int32 points, e_log_pick_type type)
  5073. {
  5074. char output[CHAT_SIZE_MAX];
  5075. nullpo_retr(-1,sd);
  5076. cash = cap_value(cash, 0, MAX_CASHPOINT); //prevent command UB
  5077. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5078. if( cash > 0 )
  5079. {
  5080. if( cash > MAX_CASHPOINT-sd->cashPoints )
  5081. {
  5082. ShowWarning("pc_getcash: Cash point overflow (cash=%d, have cash=%d, account_id=%d, char_id=%d).\n", cash, sd->cashPoints, sd->status.account_id, sd->status.char_id);
  5083. cash = MAX_CASHPOINT-sd->cashPoints;
  5084. }
  5085. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  5086. sd->cashPoints += cash;
  5087. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  5088. if( battle_config.cashshop_show_points )
  5089. {
  5090. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  5091. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5092. }
  5093. return cash;
  5094. }
  5095. if( points > 0 )
  5096. {
  5097. if( points > MAX_KAFRAPOINT-sd->kafraPoints )
  5098. {
  5099. ShowWarning("pc_getcash: Kafra point overflow (points=%d, have points=%d, account_id=%d, char_id=%d).\n", points, sd->kafraPoints, sd->status.account_id, sd->status.char_id);
  5100. points = MAX_KAFRAPOINT-sd->kafraPoints;
  5101. }
  5102. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  5103. sd->kafraPoints += points;
  5104. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  5105. if( battle_config.cashshop_show_points )
  5106. {
  5107. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  5108. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5109. }
  5110. return points;
  5111. }
  5112. return -1; //shouldn't happen but just in case
  5113. }
  5114. /**
  5115. * Searching a specified itemid in inventory and return his stored index
  5116. * @param sd Player
  5117. * @param nameid Find this Item!
  5118. * @return Stored index in inventory, or -1 if not found.
  5119. **/
  5120. short pc_search_inventory(map_session_data *sd, t_itemid nameid) {
  5121. short i;
  5122. nullpo_retr(-1, sd);
  5123. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  5124. return ( i < MAX_INVENTORY ) ? i : -1;
  5125. }
  5126. /** Attempt to add a new item to player inventory
  5127. * @param sd
  5128. * @param item_data
  5129. * @param amount
  5130. * @param log_type
  5131. * @return
  5132. * 0 = success
  5133. * 1 = invalid itemid not found or negative amount
  5134. * 2 = overweight
  5135. * 3 = ?
  5136. * 4 = no free place found
  5137. * 5 = max amount reached
  5138. * 6 = ?
  5139. * 7 = stack limitation
  5140. */
  5141. enum e_additem_result pc_additem(map_session_data *sd,struct item *item,int32 amount,e_log_pick_type log_type) {
  5142. struct item_data *id;
  5143. int16 i;
  5144. uint32 w;
  5145. nullpo_retr(ADDITEM_INVALID, sd);
  5146. nullpo_retr(ADDITEM_INVALID, item);
  5147. if( item->nameid == 0 || amount <= 0 )
  5148. return ADDITEM_INVALID;
  5149. if( amount > MAX_AMOUNT )
  5150. return ADDITEM_OVERAMOUNT;
  5151. id = itemdb_search(item->nameid);
  5152. if( id->stack.inventory && amount > id->stack.amount )
  5153. {// item stack limitation
  5154. return ADDITEM_STACKLIMIT;
  5155. }
  5156. w = id->weight*amount;
  5157. if(sd->weight + w > sd->max_weight)
  5158. return ADDITEM_OVERWEIGHT;
  5159. if (id->flag.guid && !item->unique_id)
  5160. item->unique_id = pc_generate_unique_id(sd);
  5161. // Stackable | Non Rental
  5162. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  5163. for( i = 0; i < MAX_INVENTORY; i++ ) {
  5164. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  5165. sd->inventory.u.items_inventory[i].bound == item->bound &&
  5166. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  5167. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  5168. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  5169. if( amount > MAX_AMOUNT - sd->inventory.u.items_inventory[i].amount || ( id->stack.inventory && amount > id->stack.amount - sd->inventory.u.items_inventory[i].amount ) )
  5170. return ADDITEM_OVERAMOUNT;
  5171. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  5172. if( i >= sd->status.inventory_slots ){
  5173. return ADDITEM_OVERAMOUNT;
  5174. }
  5175. sd->inventory.u.items_inventory[i].amount += amount;
  5176. clif_additem(sd,i,amount,0);
  5177. break;
  5178. }
  5179. }
  5180. }else{
  5181. i = MAX_INVENTORY;
  5182. }
  5183. if (i >= MAX_INVENTORY) {
  5184. i = pc_search_inventory(sd,0);
  5185. if( i < 0 )
  5186. return ADDITEM_OVERITEM;
  5187. if( i >= sd->status.inventory_slots ){
  5188. return ADDITEM_OVERITEM;
  5189. }
  5190. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  5191. // clear equip and favorite fields first, just in case
  5192. if( item->equip )
  5193. sd->inventory.u.items_inventory[i].equip = 0;
  5194. if( item->favorite != 0 )
  5195. sd->inventory.u.items_inventory[i].favorite = 0;
  5196. if( item->equipSwitch )
  5197. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  5198. sd->inventory.u.items_inventory[i].amount = amount;
  5199. sd->inventory_data[i] = id;
  5200. sd->last_addeditem_index = i;
  5201. if (!itemdb_isstackable2(id) || id->flag.guid)
  5202. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  5203. clif_additem(sd,i,amount,0);
  5204. }
  5205. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  5206. sd->weight += w;
  5207. clif_updatestatus(*sd,SP_WEIGHT);
  5208. //Auto-equip
  5209. if(id->flag.autoequip)
  5210. pc_equipitem(sd, i, id->equip);
  5211. /* rental item check */
  5212. if( item->expire_time ) {
  5213. if( time(nullptr) > item->expire_time ) {
  5214. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  5215. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  5216. } else {
  5217. uint32 seconds = (uint32)( item->expire_time - time(nullptr) );
  5218. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  5219. pc_inventory_rental_add(sd, seconds);
  5220. }
  5221. }
  5222. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  5223. pc_show_questinfo(sd);
  5224. return ADDITEM_SUCCESS;
  5225. }
  5226. /*==========================================
  5227. * Remove an item at index n from inventory by amount.
  5228. * @param sd
  5229. * @param n Item index in inventory
  5230. * @param amount
  5231. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  5232. * @param reason Delete reason
  5233. * @param log_type e_log_pick_type
  5234. * @return 1 - invalid itemid or negative amount; 0 - Success
  5235. *------------------------------------------*/
  5236. char pc_delitem(map_session_data *sd,int32 n,int32 amount,int32 type, short reason, e_log_pick_type log_type)
  5237. {
  5238. nullpo_retr(1, sd);
  5239. if(n < 0 || sd->inventory.u.items_inventory[n].nameid == 0 || amount <= 0 || sd->inventory.u.items_inventory[n].amount<amount || sd->inventory_data[n] == nullptr)
  5240. return 1;
  5241. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  5242. sd->inventory.u.items_inventory[n].amount -= amount;
  5243. sd->weight -= sd->inventory_data[n]->weight*amount ;
  5244. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  5245. if(sd->inventory.u.items_inventory[n].equip)
  5246. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  5247. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  5248. sd->inventory_data[n] = nullptr;
  5249. }
  5250. if(!(type&1))
  5251. clif_delitem( *sd, n, amount, reason );
  5252. if(!(type&2))
  5253. clif_updatestatus(*sd,SP_WEIGHT);
  5254. pc_show_questinfo(sd);
  5255. return 0;
  5256. }
  5257. /*==========================================
  5258. * Attempt to drop an item.
  5259. * @param sd
  5260. * @param n Item index in inventory
  5261. * @param amount Amount of item
  5262. * @return False = fail; True = success
  5263. *------------------------------------------*/
  5264. bool pc_dropitem(map_session_data *sd,int32 n,int32 amount)
  5265. {
  5266. nullpo_retr(1, sd);
  5267. if(n < 0 || n >= MAX_INVENTORY)
  5268. return false;
  5269. if(amount <= 0)
  5270. return false;
  5271. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  5272. sd->inventory.u.items_inventory[n].amount <= 0 ||
  5273. sd->inventory.u.items_inventory[n].amount < amount ||
  5274. sd->state.trading || sd->state.vending ||
  5275. !sd->inventory_data[n] //pc_delitem would fail on this case.
  5276. )
  5277. return false;
  5278. if( sd->inventory.u.items_inventory[n].equipSwitch )
  5279. return false;
  5280. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  5281. {
  5282. clif_displaymessage (sd->fd, msg_txt(sd,271));
  5283. return false; //Can't drop items in nodrop mapflag maps.
  5284. }
  5285. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  5286. {
  5287. clif_displaymessage (sd->fd, msg_txt(sd,263));
  5288. return false;
  5289. }
  5290. // Bypass drop restriction in map_addflooritem because we've already checked it above
  5291. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2|4, 0,
  5292. false, DIR_MAX, battle_config.item_stacking?BL_NUL:BL_ITEM))
  5293. {
  5294. return false;
  5295. }
  5296. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  5297. clif_dropitem( *sd, n, amount );
  5298. return true;
  5299. }
  5300. /*==========================================
  5301. * Attempt to pick up an item.
  5302. * @param sd
  5303. * @param fitem Item that will be picked
  5304. * @return False = fail; True = success
  5305. *------------------------------------------*/
  5306. bool pc_takeitem(map_session_data *sd,struct flooritem_data *fitem)
  5307. {
  5308. int32 flag = 0;
  5309. t_tick tick = gettick();
  5310. struct party_data *p = nullptr;
  5311. nullpo_ret(sd);
  5312. nullpo_ret(fitem);
  5313. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  5314. return false; // Distance is too far
  5315. if (sd->sc.cant.pickup)
  5316. return false;
  5317. if (sd->status.party_id)
  5318. p = party_search(sd->status.party_id);
  5319. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  5320. map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  5321. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  5322. if (!(p && p->party.item&1 &&
  5323. first_sd && first_sd->status.party_id == sd->status.party_id
  5324. ))
  5325. return false;
  5326. }
  5327. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  5328. map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  5329. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  5330. if (!(p && p->party.item&1 &&
  5331. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5332. (second_sd && second_sd->status.party_id == sd->status.party_id))
  5333. ))
  5334. return false;
  5335. }
  5336. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  5337. map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  5338. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  5339. if(!(p && p->party.item&1 &&
  5340. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5341. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  5342. (third_sd && third_sd->status.party_id == sd->status.party_id))
  5343. ))
  5344. return false;
  5345. }
  5346. }
  5347. }
  5348. }
  5349. //This function takes care of giving the item to whoever should have it, considering party-share options.
  5350. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  5351. clif_additem(sd,0,0,flag);
  5352. return true;
  5353. }
  5354. //Display pickup animation.
  5355. unit_stop_attack( &sd->bl );
  5356. clif_takeitem(sd->bl,fitem->bl);
  5357. if (fitem->mob_id &&
  5358. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  5359. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  5360. )
  5361. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  5362. map_clearflooritem(&fitem->bl);
  5363. return true;
  5364. }
  5365. /*==========================================
  5366. * Check if item is usable.
  5367. * Return:
  5368. * false = no
  5369. * true = yes
  5370. *------------------------------------------*/
  5371. bool pc_isUseitem(map_session_data *sd,int32 n)
  5372. {
  5373. struct item_data *item;
  5374. t_itemid nameid;
  5375. nullpo_ret(sd);
  5376. item = sd->inventory_data[n];
  5377. nameid = sd->inventory.u.items_inventory[n].nameid;
  5378. if( item == nullptr )
  5379. return false;
  5380. //Not consumable item
  5381. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  5382. return false;
  5383. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  5384. return true;
  5385. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5386. if(mapdata->getMapFlag(MF_NOITEMCONSUMPTION)) //consumable but mapflag prevent it
  5387. return false;
  5388. //Prevent mass item usage. [Skotlex]
  5389. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  5390. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  5391. )
  5392. return false;
  5393. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  5394. clif_msg(sd, MSI_CANT_USE_WHEN_SITDOWN);
  5395. return false; // You cannot use this item while sitting.
  5396. }
  5397. if (sd->state.storage_flag && item->type != IT_CASH) {
  5398. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  5399. return false; // You cannot use this item while storage is open.
  5400. }
  5401. if (item->flag.dead_branch && (mapdata->getMapFlag(MF_NOBRANCH) || mapdata_flag_gvg2(mapdata)))
  5402. return false;
  5403. if( itemdb_group.item_exists( IG_MF_NOTELEPORT, nameid ) ){
  5404. if( ( mapdata->getMapFlag(MF_NOTELEPORT) || mapdata_flag_gvg2( mapdata ) ) ){
  5405. clif_skill_teleportmessage( *sd, NOTIFY_MAPINFO_CANT_TP );
  5406. return false;
  5407. }
  5408. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5409. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5410. return false;
  5411. }
  5412. }
  5413. if( itemdb_group.item_exists( IG_MF_NORETURN, nameid ) ){
  5414. if( mapdata->getMapFlag(MF_NORETURN) ){
  5415. return false;
  5416. }
  5417. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5418. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5419. return false;
  5420. }
  5421. }
  5422. if( itemdb_group.item_exists( IG_GIANT_FLY_WING, nameid ) ){
  5423. struct party_data *pd = party_search( sd->status.party_id );
  5424. if( pd ){
  5425. int32 i;
  5426. ARR_FIND( 0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader );
  5427. // User is not party leader
  5428. if( i == MAX_PARTY ){
  5429. clif_msg( sd, MSI_CANNOT_PARTYCALL);
  5430. return false;
  5431. }
  5432. ARR_FIND( 0, MAX_PARTY, i, pd->data[i].sd && pd->data[i].sd != sd && pd->data[i].sd->bl.m == sd->bl.m && !pc_isdead( pd->data[i].sd ) );
  5433. // No party members found on same map
  5434. if( i == MAX_PARTY ){
  5435. clif_msg( sd, MSI_NO_PARTYMEM_ON_THISMAP );
  5436. return false;
  5437. }
  5438. }else{
  5439. clif_msg( sd, MSI_CANNOT_PARTYCALL);
  5440. return false;
  5441. }
  5442. }
  5443. switch( nameid ) {
  5444. case ITEMID_MERCENARY_RED_POTION:
  5445. case ITEMID_MERCENARY_BLUE_POTION:
  5446. case ITEMID_M_CENTER_POTION:
  5447. case ITEMID_M_AWAKENING_POTION:
  5448. case ITEMID_M_BERSERK_POTION:
  5449. if( sd->md == nullptr || sd->md->db == nullptr )
  5450. return false;
  5451. if( sd->md->sc.cant.consume )
  5452. return false;
  5453. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5454. return false;
  5455. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5456. return false;
  5457. break;
  5458. case ITEMID_NEURALIZER:
  5459. if( !mapdata->getMapFlag(MF_RESET) )
  5460. return false;
  5461. break;
  5462. }
  5463. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != nullptr )
  5464. return false; // Mercenary Scrolls
  5465. // Safe check type cash disappear when overweight [Napster]
  5466. if( item->flag.group || item->type == IT_CASH ){
  5467. #ifdef RENEWAL
  5468. // Check if the player is not overweighted
  5469. // In Renewal the limit is 70% weight and gives the same error message
  5470. if (pc_is70overweight(sd)) {
  5471. clif_msg_color(sd, MSI_PICKUP_FAILED_ITEMCREATE, color_table[COLOR_RED]);
  5472. return 0;
  5473. }
  5474. #else
  5475. // Check if the player is not overweighted
  5476. // In Pre-Renewal the limit is 50% weight and gives a specific error message
  5477. if (pc_is50overweight(sd)) {
  5478. clif_msg_color(sd, MSI_CANT_GET_ITEM_BECAUSE_WEIGHT, color_table[COLOR_RED]);
  5479. return 0;
  5480. }
  5481. #endif
  5482. // Check if the player has enough free spaces in the inventory
  5483. // Official servers use 10 as the minimum amount of slots required to get the items
  5484. // The <= is intentional, as in official servers you actually need an extra empty slot
  5485. // TODO: Count the items the player will get and check for the actual inventory space required std::max<size_t>( count, 10 )
  5486. if (pc_inventoryblank(sd) <= 10) {
  5487. #ifdef RENEWAL
  5488. clif_msg_color(sd, MSI_PICKUP_FAILED_ITEMCREATE, color_table[COLOR_RED]);
  5489. #else
  5490. clif_msg_color(sd, MSI_CANT_GET_ITEM_BECAUSE_COUNT, color_table[COLOR_RED]);
  5491. #endif
  5492. return 0;
  5493. }
  5494. }
  5495. //Gender check
  5496. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5497. return false;
  5498. //Required level check
  5499. if(item->elv && sd->status.base_level < (uint32)item->elv)
  5500. return false;
  5501. if(item->elvmax && sd->status.base_level > (uint32)item->elvmax)
  5502. return false;
  5503. //Not equipable by class. [Skotlex]
  5504. if (!pc_job_can_use_item(sd,item))
  5505. return false;
  5506. if (sd->sc.cant.consume)
  5507. return false;
  5508. if (!pc_isItemClass(sd,item))
  5509. return false;
  5510. //Dead Branch items
  5511. if( item->flag.dead_branch )
  5512. log_branch(sd);
  5513. return true;
  5514. }
  5515. /*==========================================
  5516. * Last checks to use an item.
  5517. * Return:
  5518. * 0 = fail
  5519. * 1 = success
  5520. *------------------------------------------*/
  5521. int32 pc_useitem(map_session_data *sd,int32 n)
  5522. {
  5523. t_tick tick = gettick();
  5524. int32 amount;
  5525. t_itemid nameid;
  5526. struct script_code *script;
  5527. struct item item;
  5528. struct item_data *id;
  5529. nullpo_ret(sd);
  5530. if (sd->state.mail_writing)
  5531. return 0;
  5532. if (sd->npc_id) {
  5533. if (sd->progressbar.npc_id) {
  5534. clif_progressbar_abort(sd);
  5535. return 0; // First item use attempt cancels the progress bar
  5536. }
  5537. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5538. #ifdef RENEWAL
  5539. clif_msg( sd, MSI_BUSY);
  5540. #endif
  5541. return 0;
  5542. }
  5543. }
  5544. item = sd->inventory.u.items_inventory[n];
  5545. id = sd->inventory_data[n];
  5546. if (item.nameid == 0 || item.amount <= 0)
  5547. return 0;
  5548. if( !pc_isUseitem(sd,n) )
  5549. return 0;
  5550. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5551. nameid = id->nameid;
  5552. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5553. return 0;
  5554. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5555. if( id->flag.delay_consume > 0 ) {
  5556. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.getSCE(SC_ALL_RIDING) )
  5557. return 0;
  5558. else if( pc_issit(sd) )
  5559. return 0;
  5560. }
  5561. //Since most delay-consume items involve using a "skill-type" target cursor,
  5562. //perform a skill-use check before going through. [Skotlex]
  5563. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5564. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5565. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5566. return 0;
  5567. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5568. return 0;
  5569. /* on restricted maps the item is consumed but the effect is not used */
  5570. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5571. clif_msg(sd, MSI_IMPOSSIBLE_USEITEM_AREA); // This item cannot be used within this area
  5572. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5573. clif_useitemack(sd,n,item.amount-1,true);
  5574. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5575. }
  5576. return 0;/* regardless, effect is not run */
  5577. }
  5578. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5579. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5580. return 0;
  5581. }
  5582. sd->itemid = item.nameid;
  5583. sd->itemindex = n;
  5584. amount = item.amount;
  5585. script = id->script;
  5586. //Check if the item is to be consumed immediately [Skotlex]
  5587. if (id->flag.delay_consume > 0)
  5588. clif_useitemack(sd, n, amount, true);
  5589. else
  5590. {
  5591. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5592. {
  5593. clif_useitemack(sd, n, amount - 1, true);
  5594. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5595. }
  5596. else
  5597. clif_useitemack(sd, n, 0, false);
  5598. }
  5599. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5600. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5601. //Update item use time.
  5602. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5603. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5604. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5605. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5606. potion_flag = 0;
  5607. return 1;
  5608. }
  5609. /**
  5610. * Add item on cart for given index.
  5611. * @param sd
  5612. * @param item
  5613. * @param amount
  5614. * @param log_type
  5615. * @return See pc.hpp::e_additem_result
  5616. */
  5617. enum e_additem_result pc_cart_additem(map_session_data *sd,struct item *item,int32 amount,e_log_pick_type log_type)
  5618. {
  5619. struct item_data *data;
  5620. int32 i,w;
  5621. nullpo_retr(ADDITEM_INVALID, sd);
  5622. nullpo_retr(ADDITEM_INVALID, item);
  5623. if(item->nameid == 0 || amount <= 0)
  5624. return ADDITEM_INVALID;
  5625. if (itemdb_ishatched_egg(item))
  5626. return ADDITEM_INVALID;
  5627. data = itemdb_search(item->nameid);
  5628. if( data->stack.cart && amount > data->stack.amount )
  5629. {// item stack limitation
  5630. return ADDITEM_STACKLIMIT;
  5631. }
  5632. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5633. { // Check item trade restrictions [Skotlex]
  5634. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5635. return ADDITEM_INVALID;
  5636. }
  5637. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5638. return ADDITEM_OVERWEIGHT;
  5639. i = MAX_CART;
  5640. if( itemdb_isstackable2(data) && !item->expire_time )
  5641. {
  5642. for (i = 0; i < MAX_CART; i++) {
  5643. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5644. && sd->cart.u.items_cart[i].bound == item->bound
  5645. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5646. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5647. )
  5648. break;
  5649. }
  5650. }
  5651. if( i < MAX_CART )
  5652. {// item already in cart, stack it
  5653. if( amount > MAX_AMOUNT - sd->cart.u.items_cart[i].amount || ( data->stack.cart && amount > data->stack.amount - sd->cart.u.items_cart[i].amount ) )
  5654. return ADDITEM_OVERAMOUNT; // no slot
  5655. sd->cart.u.items_cart[i].amount += amount;
  5656. clif_cart_additem(sd,i,amount);
  5657. }
  5658. else
  5659. {// item not stackable or not present, add it
  5660. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5661. if( i == MAX_CART )
  5662. return ADDITEM_OVERAMOUNT; // no slot
  5663. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5664. sd->cart.u.items_cart[i].id = 0;
  5665. sd->cart.u.items_cart[i].amount = amount;
  5666. sd->cart_num++;
  5667. clif_cart_additem(sd,i,amount);
  5668. }
  5669. sd->cart.u.items_cart[i].favorite = 0; // clear
  5670. sd->cart.u.items_cart[i].equipSwitch = 0;
  5671. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5672. sd->cart_weight += w;
  5673. clif_updatestatus(*sd,SP_CARTINFO);
  5674. return ADDITEM_SUCCESS;
  5675. }
  5676. /*==========================================
  5677. * Delete item on cart for given index.
  5678. *------------------------------------------*/
  5679. void pc_cart_delitem(map_session_data *sd,int32 n,int32 amount,int32 type,e_log_pick_type log_type)
  5680. {
  5681. nullpo_retv(sd);
  5682. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5683. sd->cart.u.items_cart[n].amount < amount)
  5684. return;
  5685. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5686. sd->cart.u.items_cart[n].amount -= amount;
  5687. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5688. if(sd->cart.u.items_cart[n].amount <= 0) {
  5689. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5690. sd->cart_num--;
  5691. }
  5692. if(!type) {
  5693. clif_cart_delitem( *sd, n, amount );
  5694. clif_updatestatus(*sd,SP_CARTINFO);
  5695. }
  5696. }
  5697. /*==========================================
  5698. * Transfer item from inventory to cart.
  5699. *------------------------------------------*/
  5700. void pc_putitemtocart(map_session_data *sd,int32 idx,int32 amount)
  5701. {
  5702. nullpo_retv(sd);
  5703. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5704. return;
  5705. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5706. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5707. return;
  5708. if( item_data->equipSwitch ){
  5709. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  5710. return;
  5711. }
  5712. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5713. if (flag == ADDITEM_SUCCESS)
  5714. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5715. else {
  5716. if (flag == ADDITEM_OVERAMOUNT)
  5717. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_COUNT );
  5718. else
  5719. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_WEIGHT );
  5720. clif_additem(sd, idx, amount, 0);
  5721. clif_delitem( *sd, idx, amount, 0 );
  5722. }
  5723. }
  5724. /*==========================================
  5725. * Get number of item in cart.
  5726. * Return:
  5727. -1 = itemid not found or no amount found
  5728. x = remaining itemid on cart after get
  5729. *------------------------------------------*/
  5730. int32 pc_cartitem_amount(map_session_data* sd, int32 idx, int32 amount)
  5731. {
  5732. struct item* item_data;
  5733. nullpo_retr(-1, sd);
  5734. item_data = &sd->cart.u.items_cart[idx];
  5735. if( item_data->nameid == 0 || item_data->amount == 0 )
  5736. return -1;
  5737. return item_data->amount - amount;
  5738. }
  5739. /*==========================================
  5740. * Retrieve an item at index idx from cart.
  5741. *------------------------------------------*/
  5742. bool pc_getitemfromcart(map_session_data *sd,int32 idx,int32 amount)
  5743. {
  5744. nullpo_retr(1, sd);
  5745. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5746. return false;
  5747. struct item *item_data=&sd->cart.u.items_cart[idx];
  5748. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5749. return false;
  5750. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5751. if (flag == ADDITEM_SUCCESS)
  5752. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5753. else {
  5754. clif_cart_delitem( *sd, idx, amount );
  5755. clif_additem(sd, idx, amount, flag);
  5756. clif_cart_additem(sd, idx, amount);
  5757. }
  5758. return true;
  5759. }
  5760. /*==========================================
  5761. * Bound Item Check
  5762. * Type:
  5763. * 1 Account Bound
  5764. * 2 Guild Bound
  5765. * 3 Party Bound
  5766. * 4 Character Bound
  5767. *------------------------------------------*/
  5768. int32 pc_bound_chk(TBL_PC *sd,enum bound_type type,int32 *idxlist)
  5769. {
  5770. int32 i = 0, j = 0;
  5771. for(i = 0; i < MAX_INVENTORY; i++) {
  5772. if(sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].amount > 0 && sd->inventory.u.items_inventory[i].bound == type) {
  5773. idxlist[j] = i;
  5774. j++;
  5775. }
  5776. }
  5777. return j;
  5778. }
  5779. /*==========================================
  5780. * Display item stolen msg to player sd
  5781. *------------------------------------------*/
  5782. int32 pc_show_steal(struct block_list *bl,va_list ap)
  5783. {
  5784. map_session_data *sd;
  5785. t_itemid itemid;
  5786. char output[100];
  5787. sd=va_arg(ap,map_session_data *);
  5788. itemid=va_arg(ap,int32);
  5789. std::shared_ptr<item_data> id = item_db.find(itemid);
  5790. if(id == nullptr)
  5791. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5792. else
  5793. sprintf(output,"%s stole %s.",sd->status.name,id->ename.c_str());
  5794. clif_displaymessage( ((map_session_data *)bl)->fd, output);
  5795. return 0;
  5796. }
  5797. /**
  5798. * Steal an item from bl (mob).
  5799. * @param sd: Player data
  5800. * @param bl: Object to steal from
  5801. * @param skill_lv: Level of skill used
  5802. * @return True on success or false otherwise
  5803. */
  5804. bool pc_steal_item(map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5805. {
  5806. int32 i;
  5807. t_itemid itemid;
  5808. double rate;
  5809. unsigned char flag = 0;
  5810. struct mob_data *md;
  5811. if(!sd || !bl || bl->type!=BL_MOB)
  5812. return false;
  5813. md = (TBL_MOB *)bl;
  5814. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5815. return false;
  5816. status_data* sd_status = status_get_status_data(sd->bl);
  5817. status_data* md_status = status_get_status_data(*bl);
  5818. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5819. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5820. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5821. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5822. ) { //Can't steal from
  5823. md->state.steal_flag = UCHAR_MAX;
  5824. return false;
  5825. }
  5826. // base skill success chance (percentual)
  5827. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5828. rate += sd->bonus.add_steal_rate;
  5829. if( rate < 1
  5830. #ifdef RENEWAL
  5831. || rnd()%100 >= rate
  5832. #endif
  5833. )
  5834. return false;
  5835. // Try dropping one item, in the order from first to last possible slot.
  5836. // Droprate is affected by the skill success rate.
  5837. for( i = 0; i < MAX_MOB_DROP; i++ )
  5838. if( item_db.exists(md->db->dropitem[i].nameid) && !md->db->dropitem[i].steal_protected && rnd() % 10000 < md->db->dropitem[i].rate
  5839. #ifndef RENEWAL
  5840. * rate/100.
  5841. #endif
  5842. )
  5843. break;
  5844. if( i == MAX_MOB_DROP )
  5845. return false;
  5846. itemid = md->db->dropitem[i].nameid;
  5847. struct item tmp_item = {};
  5848. tmp_item.nameid = itemid;
  5849. tmp_item.amount = 1;
  5850. tmp_item.identify = itemdb_isidentified(itemid);
  5851. if( battle_config.skill_steal_random_options ){
  5852. mob_setdropitem_option( tmp_item, md->db->dropitem[i] );
  5853. }
  5854. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5855. //TODO: Should we disable stealing when the item you stole couldn't be added to your inventory? Perhaps players will figure out a way to exploit this behaviour otherwise?
  5856. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5857. if(flag) { //Failed to steal due to overweight
  5858. clif_additem(sd,0,0,flag);
  5859. return false;
  5860. }
  5861. if(battle_config.show_steal_in_same_party)
  5862. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5863. //Logs items, Stolen from mobs [Lupus]
  5864. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5865. //A Rare Steal Global Announce by Lupus
  5866. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5867. struct item_data *i_data;
  5868. char message[128];
  5869. i_data = itemdb_search(itemid);
  5870. sprintf (message, msg_txt(sd,542), (sd->status.name[0])?sd->status.name :"GM", md->db->jname.c_str(), i_data->ename.c_str(), (float)md->db->dropitem[i].rate / 100);
  5871. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5872. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5873. }
  5874. return true;
  5875. }
  5876. /*==========================================
  5877. * Stole zeny from bl (mob)
  5878. * return
  5879. * 0 = fail
  5880. * 1 = success
  5881. *------------------------------------------*/
  5882. int32 pc_steal_coin(map_session_data *sd,struct block_list *target)
  5883. {
  5884. int32 rate, target_lv;
  5885. struct mob_data *md;
  5886. if(!sd || !target || target->type != BL_MOB)
  5887. return 0;
  5888. md = (TBL_MOB*)target;
  5889. target_lv = status_get_lv(target);
  5890. if (md->state.steal_coin_flag || md->sc.getSCE(SC_STONE) || md->sc.getSCE(SC_FREEZE) || md->sc.getSCE(SC_HANDICAPSTATE_FROSTBITE) ||
  5891. md->sc.getSCE(SC_HANDICAPSTATE_SWOONING) || md->sc.getSCE(SC_HANDICAPSTATE_LIGHTNINGSTRIKE) || md->sc.getSCE(SC_HANDICAPSTATE_CRYSTALLIZATION) ||
  5892. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5893. return 0;
  5894. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5895. if(rnd()%1000 < rate)
  5896. {
  5897. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5898. int32 amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5899. pc_getzeny(sd, amount, LOG_TYPE_STEAL);
  5900. md->state.steal_coin_flag = 1;
  5901. return 1;
  5902. }
  5903. return 0;
  5904. }
  5905. /*==========================================
  5906. * Set's a player position.
  5907. * @param sd
  5908. * @param mapindex
  5909. * @param x
  5910. * @param y
  5911. * @param clrtype
  5912. * @return SETPOS_OK Success
  5913. * SETPOS_MAPINDEX Invalid map index
  5914. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5915. * SETPOS_AUTOTRADE Player is in autotrade state
  5916. *------------------------------------------*/
  5917. enum e_setpos pc_setpos(map_session_data* sd, uint16 mapindex, int32 x, int32 y, clr_type clrtype)
  5918. {
  5919. nullpo_retr(SETPOS_OK,sd);
  5920. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5921. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5922. return SETPOS_MAPINDEX;
  5923. }
  5924. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5925. return SETPOS_AUTOTRADE;
  5926. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5927. pc_setstand(sd, true);
  5928. pc_setrestartvalue(sd,1);
  5929. }
  5930. int16 m = map_mapindex2mapid(mapindex);
  5931. struct map_data *mapdata = map_getmapdata(m);
  5932. status_change *sc = status_get_sc(&sd->bl);
  5933. sd->state.changemap = (sd->mapindex != mapindex);
  5934. sd->state.warping = 1;
  5935. sd->state.workinprogress = WIP_DISABLE_NONE;
  5936. sd->state.mail_writing = false;
  5937. sd->state.refineui_open = false;
  5938. if( sd->state.changemap ) { // Misc map-changing settings
  5939. int32 curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5940. if (curr_map_instance_id != new_map_instance_id) {
  5941. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5942. instance_delusers(curr_map_instance_id);
  5943. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5944. }
  5945. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5946. instance_addusers(new_map_instance_id);
  5947. }
  5948. if (sd->bg_id && mapdata && !mapdata->getMapFlag(MF_BATTLEGROUND)) // Moving to a map that isn't a Battlegrounds
  5949. bg_team_leave(sd, false, true);
  5950. sd->state.pmap = sd->bl.m;
  5951. if (sc != nullptr && !sc->empty()) { // Cancel some map related stuff.
  5952. if (sc->cant.warp)
  5953. return SETPOS_MAPINDEX; // You may not get out!
  5954. for (const auto &it : status_db) {
  5955. if (sc->getSCE(it.first)) {
  5956. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5957. status_change_end(&sd->bl, static_cast<sc_type>(it.first));
  5958. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5959. status_change_entry *sce = sd->sc.getSCE(it.first);
  5960. if (sce->timer != INVALID_TIMER)
  5961. delete_timer(sce->timer, status_change_timer);
  5962. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5963. }
  5964. }
  5965. }
  5966. }
  5967. for(int32 i = 0; i < EQI_MAX; i++ ) {
  5968. if( sd->equip_index[i] >= 0 )
  5969. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  5970. pc_unequipitem(sd,sd->equip_index[i],2);
  5971. }
  5972. }
  5973. if (battle_config.clear_unit_onwarp&BL_PC)
  5974. skill_clear_unitgroup(&sd->bl);
  5975. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5976. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5977. }
  5978. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5979. guild_send_dot_remove(sd);
  5980. bg_send_dot_remove(sd);
  5981. if (sd->regen.state.gc)
  5982. sd->regen.state.gc = 0;
  5983. if (mapdata) {
  5984. // make sure vending is allowed here
  5985. if (sd->state.vending && mapdata->getMapFlag(MF_NOVENDING)) {
  5986. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5987. vending_closevending(sd);
  5988. }
  5989. // make sure buyingstore is allowed here
  5990. if (sd->state.buyingstore && mapdata->getMapFlag(MF_NOBUYINGSTORE)) {
  5991. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5992. buyingstore_close(sd);
  5993. }
  5994. }
  5995. channel_pcquit(sd,4); //quit map chan
  5996. // Remove Cloaked NPCs on map change
  5997. sd->cloaked_npc.clear();
  5998. }
  5999. if( m < 0 )
  6000. {
  6001. uint32 ip;
  6002. uint16 port;
  6003. struct script_state *st;
  6004. //if can't find any map-servers, just abort setting position.
  6005. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  6006. return SETPOS_NO_MAPSERVER;
  6007. if (sd->npc_id){
  6008. npc_event_dequeue(sd,false);
  6009. st = sd->st;
  6010. }else{
  6011. st = nullptr;
  6012. }
  6013. if (sd->bg_id) // Switching map servers, remove from bg
  6014. bg_team_leave(sd, false, true);
  6015. if (sd->state.vending) // Stop vending
  6016. vending_closevending(sd);
  6017. if (sd->state.buyingstore) // Stop buyingstore
  6018. buyingstore_close(sd);
  6019. npc_script_event( *sd, NPCE_LOGOUT );
  6020. //remove from map, THEN change x/y coordinates
  6021. unit_remove_map_pc(sd,clrtype);
  6022. sd->mapindex = mapindex;
  6023. sd->bl.x=x;
  6024. sd->bl.y=y;
  6025. pc_clean_skilltree(sd);
  6026. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  6027. chrif_changemapserver(sd, ip, (short)port);
  6028. //Free session data from this map server [Kevin]
  6029. unit_free_pc(sd);
  6030. if( st ){
  6031. // Has to be done here, because otherwise unit_free_pc will free the stack already
  6032. st->state = END;
  6033. }
  6034. return SETPOS_OK;
  6035. }
  6036. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  6037. {
  6038. ShowError("pc_setpos: attempt to place player '%s' (%d:%d) on invalid coordinates (%s-%d,%d)\n", sd->status.name, sd->status.account_id, sd->status.char_id, mapindex_id2name(mapindex),x,y);
  6039. x = y = 0; // make it random
  6040. }
  6041. if( x == 0 && y == 0 ) { // pick a random walkable cell
  6042. int32 c=0;
  6043. do {
  6044. x = rnd()%(mapdata->xs-2)+1;
  6045. y = rnd()%(mapdata->ys-2)+1;
  6046. c++;
  6047. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  6048. ShowError("pc_setpos: couldn't found a valid coordinates for player '%s' (%d:%d) on (%s), preventing warp\n", sd->status.name, sd->status.account_id, sd->status.char_id, mapindex_id2name(mapindex));
  6049. return SETPOS_OK; //preventing warp
  6050. //break; //allow warp anyway
  6051. }
  6052. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  6053. }
  6054. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  6055. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  6056. vending_closevending(sd);
  6057. }
  6058. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  6059. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  6060. buyingstore_close(sd);
  6061. }
  6062. if(sd->bl.prev != nullptr){
  6063. unit_remove_map_pc(sd,clrtype);
  6064. clif_changemap( *sd, m, x, y );
  6065. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  6066. sd->state.rewarp = 1;
  6067. sd->mapindex = mapindex;
  6068. sd->bl.m = m;
  6069. sd->bl.x = sd->ud.to_x = x;
  6070. sd->bl.y = sd->ud.to_y = y;
  6071. if( sd->status.guild_id > 0 && mapdata->getMapFlag(MF_GVG_CASTLE) )
  6072. { // Increased guild castle regen [Valaris]
  6073. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  6074. if(gc && gc->guild_id == sd->status.guild_id)
  6075. sd->regen.state.gc = 1;
  6076. }
  6077. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  6078. {
  6079. sd->pd->bl.m = m;
  6080. sd->pd->bl.x = sd->pd->ud.to_x = x;
  6081. sd->pd->bl.y = sd->pd->ud.to_y = y;
  6082. sd->pd->ud.dir = sd->ud.dir;
  6083. }
  6084. if( hom_is_active(sd->hd) )
  6085. {
  6086. if (sd->state.changemap)
  6087. status_db.removeByStatusFlag(&sd->hd->bl, { SCF_REMOVEFROMHOMONMAPWARP });
  6088. else
  6089. status_db.removeByStatusFlag(&sd->hd->bl, { SCF_REMOVEFROMHOMONWARP });
  6090. if (battle_config.hom_delay_reset_warp) {
  6091. skill_blockhomun_clear(*sd->hd);
  6092. }
  6093. sd->hd->bl.m = m;
  6094. sd->hd->bl.x = sd->hd->ud.to_x = x;
  6095. sd->hd->bl.y = sd->hd->ud.to_y = y;
  6096. sd->hd->ud.dir = sd->ud.dir;
  6097. }
  6098. if( sd->md )
  6099. {
  6100. sd->md->bl.m = m;
  6101. sd->md->bl.x = sd->md->ud.to_x = x;
  6102. sd->md->bl.y = sd->md->ud.to_y = y;
  6103. sd->md->ud.dir = sd->ud.dir;
  6104. }
  6105. if( sd->ed ) {
  6106. sd->ed->bl.m = m;
  6107. sd->ed->bl.x = sd->ed->ud.to_x = x;
  6108. sd->ed->bl.y = sd->ed->ud.to_y = y;
  6109. sd->ed->ud.dir = sd->ud.dir;
  6110. }
  6111. pc_cell_basilica(sd);
  6112. //check if we gonna be rewarped [lighta]
  6113. if(npc_check_areanpc(1,m,x,y,0)){
  6114. sd->count_rewarp++;
  6115. }
  6116. else
  6117. sd->count_rewarp = 0;
  6118. if (sd->state.vending)
  6119. vending_update(*sd);
  6120. if (sd->state.buyingstore)
  6121. buyingstore_update(*sd);
  6122. return SETPOS_OK;
  6123. }
  6124. enum e_setpos pc_setpos_savepoint( map_session_data& sd, clr_type clrtype ){
  6125. struct map_data *mapdata = map_getmapdata( sd.bl.m );
  6126. if( mapdata != nullptr && mapdata->getMapFlag(MF_NOSAVE) && mapdata->save.map ){
  6127. return pc_setpos( &sd, mapdata->save.map, mapdata->save.x, mapdata->save.y, clrtype );
  6128. }else{
  6129. return pc_setpos( &sd, mapindex_name2id( sd.status.save_point.map ), sd.status.save_point.x, sd.status.save_point.y, clrtype );
  6130. }
  6131. }
  6132. /*==========================================
  6133. * Warp player sd to random location on current map.
  6134. * May fail if no walkable cell found (1000 attempts).
  6135. * Return:
  6136. * 0 = Success
  6137. * 1,2,3 = Fail
  6138. *------------------------------------------*/
  6139. char pc_randomwarp(map_session_data *sd, clr_type type, bool ignore_mapflag)
  6140. {
  6141. int16 x,y,i=0;
  6142. nullpo_ret(sd);
  6143. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6144. if (mapdata->getMapFlag(MF_NOTELEPORT) && !ignore_mapflag) //Teleport forbidden
  6145. return 3;
  6146. int32 edge = battle_config.map_edge_size;
  6147. do {
  6148. x = rnd_value<int16>(edge, mapdata->xs - edge - 1);
  6149. y = rnd_value<int16>(edge, mapdata->ys - edge - 1);
  6150. } while((map_getcell(sd->bl.m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,sd->bl.m,x,y,1))) && (i++) < 1000);
  6151. if (i < 1000)
  6152. return pc_setpos(sd,mapdata->index,x,y,type);
  6153. return 3;
  6154. }
  6155. /*==========================================
  6156. * Records a memo point at sd's current position
  6157. * pos - entry to replace, (-1: shift oldest entry out)
  6158. *------------------------------------------*/
  6159. bool pc_memo(map_session_data* sd, int32 pos)
  6160. {
  6161. int32 skill;
  6162. nullpo_ret(sd);
  6163. // check mapflags
  6164. if( sd->bl.m >= 0 && (map_getmapflag(sd->bl.m, MF_NOMEMO) || map_getmapflag(sd->bl.m, MF_NOWARPTO)) && !pc_has_permission(sd, PC_PERM_WARP_ANYWHERE) ) {
  6165. clif_skill_teleportmessage( *sd, NOTIFY_MAPINFO_CANT_MEMO ); // "Saved point cannot be memorized."
  6166. return false;
  6167. }
  6168. // check inputs
  6169. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  6170. return false; // invalid input
  6171. // check required skill level
  6172. skill = pc_checkskill(sd, AL_WARP);
  6173. if( skill < 1 ) {
  6174. clif_skill_memomessage( *sd, WARPPOINT_NOT_LEARNED ); // "You haven't learned Warp."
  6175. return false;
  6176. }
  6177. if( skill < 2 || skill - 2 < pos ) {
  6178. clif_skill_memomessage( *sd, WARPPOINT_LOW_LEVEL ); // "Skill Level is not high enough."
  6179. return false;
  6180. }
  6181. if( pos == -1 )
  6182. {
  6183. uint8 i;
  6184. const char* mapname = map_mapid2mapname( sd->bl.m );
  6185. // prevent memo-ing the same map multiple times
  6186. ARR_FIND( 0, MAX_MEMOPOINTS, i, strncmp( sd->status.memo_point[i].map, mapname, sizeof( sd->status.memo_point[i].map ) ) == 0 );
  6187. memmove( &sd->status.memo_point[1], &sd->status.memo_point[0], ( u8min( i, MAX_MEMOPOINTS - 1 ) ) * sizeof( struct s_point_str ) );
  6188. pos = 0;
  6189. }
  6190. if( map_getmapdata(sd->bl.m)->instance_id ) {
  6191. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  6192. return false;
  6193. }
  6194. safestrncpy( sd->status.memo_point[pos].map, map_mapid2mapname( sd->bl.m ), sizeof( sd->status.memo_point[pos].map ) );
  6195. sd->status.memo_point[pos].x = sd->bl.x;
  6196. sd->status.memo_point[pos].y = sd->bl.y;
  6197. clif_skill_memomessage( *sd, WARPPOINT_SUCCESS );
  6198. return true;
  6199. }
  6200. //
  6201. // Skills
  6202. //
  6203. /**
  6204. * Get the skill current cooldown for player.
  6205. * (get the db base cooldown for skill + player specific cooldown)
  6206. * @param sd : player pointer
  6207. * @param id : skill id
  6208. * @param lv : skill lv
  6209. * @return player skill cooldown
  6210. */
  6211. int32 pc_get_skillcooldown(map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  6212. if (skill_id == SJ_NOVAEXPLOSING) {
  6213. status_change *sc = status_get_sc(&sd->bl);
  6214. if (sc && sc->getSCE(SC_DIMENSION))
  6215. return 0;
  6216. }
  6217. int32 cooldown = skill_get_cooldown(skill_id, skill_lv);
  6218. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  6219. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  6220. for (auto &it : sd->skillcooldown) {
  6221. if (it.id == skill_id) {
  6222. cooldown += it.val;
  6223. break;
  6224. }
  6225. }
  6226. return max(0, cooldown);
  6227. }
  6228. /*==========================================
  6229. * Return player sd skill_lv learned for given skill
  6230. *------------------------------------------*/
  6231. uint8 pc_checkskill(map_session_data *sd, uint16 skill_id)
  6232. {
  6233. uint16 idx = 0;
  6234. if (sd == nullptr)
  6235. return 0;
  6236. #ifdef RENEWAL
  6237. if ((idx = skill_get_index(skill_id)) == 0) {
  6238. #else
  6239. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  6240. if( skill_id >= RK_ENCHANTBLADE ){
  6241. // Silently fail for now -> future update planned
  6242. return 0;
  6243. }
  6244. #endif
  6245. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  6246. return 0;
  6247. }
  6248. if (SKILL_CHK_GUILD(skill_id) ) {
  6249. if (sd->status.guild_id>0 && sd->guild)
  6250. return guild_checkskill(sd->guild->guild,skill_id);
  6251. return 0;
  6252. }
  6253. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  6254. }
  6255. /**
  6256. * Returns the flag of the given skill (when learned).
  6257. * @param sd: Player data
  6258. * @param skill_id: Skill to lookup
  6259. * @return Skill flag type
  6260. */
  6261. e_skill_flag pc_checkskill_flag(map_session_data &sd, uint16 skill_id) {
  6262. uint16 idx;
  6263. #ifdef RENEWAL
  6264. if ((idx = skill_get_index(skill_id)) == 0) {
  6265. #else
  6266. if ((idx = skill_db.get_index(skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__)) == 0) {
  6267. if (skill_id >= RK_ENCHANTBLADE) {
  6268. // Silently fail for now -> future update planned
  6269. return SKILL_FLAG_NONE;
  6270. }
  6271. #endif
  6272. ShowError("pc_checkskill_flag: Invalid skill id %d (char_id=%d).\n", skill_id, sd.status.char_id);
  6273. return SKILL_FLAG_NONE;
  6274. }
  6275. return (sd.status.skill[idx].id == skill_id && sd.status.skill[idx].lv > 0) ? static_cast<e_skill_flag>(sd.status.skill[idx].flag) : SKILL_FLAG_NONE;
  6276. }
  6277. /**
  6278. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  6279. * @param sd: Player data
  6280. * @param type: Summoner Power Type
  6281. * @return Skill points invested
  6282. */
  6283. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  6284. if (sd == nullptr)
  6285. return 0;
  6286. uint8 count = 0;
  6287. switch (type) {
  6288. case SUMMONER_POWER_SEA:
  6289. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  6290. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  6291. break;
  6292. case SUMMONER_POWER_LAND:
  6293. count = pc_checkskill(sd, SU_SV_STEMSPEAR) + pc_checkskill(sd, SU_CN_POWDERING) + pc_checkskill(sd, SU_CN_METEOR) + pc_checkskill(sd, SU_SV_ROOTTWIST) +
  6294. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  6295. break;
  6296. case SUMMONER_POWER_LIFE:
  6297. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  6298. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  6299. break;
  6300. }
  6301. return count;
  6302. }
  6303. /**
  6304. * Checks for Imperial Guard's passive skills.
  6305. * @param sd: Player data
  6306. * @param flag:
  6307. * Flag&1 = IG_SHIELD_MASTERY
  6308. * Flag&2 = IG_SPEAR_SWORD_M
  6309. */
  6310. uint8 pc_checkskill_imperial_guard(map_session_data *sd, short flag)
  6311. {
  6312. nullpo_retr(0, sd);
  6313. uint8 count = 0;
  6314. if (flag&1 && sd->status.shield > 0)
  6315. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  6316. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  6317. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  6318. return count;
  6319. }
  6320. /**
  6321. * Check if we still have the correct weapon to continue the skill (actually status)
  6322. * If not ending it
  6323. * @param sd
  6324. * @return 0:error, 1:check done
  6325. */
  6326. static void pc_checkallowskill(map_session_data *sd)
  6327. {
  6328. nullpo_retv(sd);
  6329. if(sd->sc.empty())
  6330. return;
  6331. for (const auto &it : status_db) {
  6332. sc_type status = it.second->type;
  6333. std::bitset<SCF_MAX> flag = it.second->flag;
  6334. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  6335. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  6336. continue;
  6337. if (sd->sc.getSCE(status) && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  6338. status_change_end(&sd->bl, status);
  6339. }
  6340. if (flag[SCF_REQUIRENOWEAPON]) {
  6341. if (sd->sc.getSCE(status) && sd->status.weapon)
  6342. status_change_end( &sd->bl, status );
  6343. }
  6344. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  6345. if (sd->sc.getSCE(status) && sd->status.shield <= 0)
  6346. status_change_end(&sd->bl, status);
  6347. }
  6348. }
  6349. }
  6350. /*==========================================
  6351. * Return equipped index of item on player sd at pos
  6352. * Return
  6353. * -1 : Nothing equipped
  6354. * idx : (this index could be used in inventory to found item_data)
  6355. *------------------------------------------*/
  6356. short pc_checkequip(map_session_data *sd,int32 pos, bool checkall)
  6357. {
  6358. uint8 i;
  6359. nullpo_retr(-1, sd);
  6360. for(i=0;i<EQI_MAX;i++){
  6361. if(pos & equip_bitmask[i]){
  6362. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  6363. // Check all if any match is found
  6364. continue;
  6365. }
  6366. return sd->equip_index[i];
  6367. }
  6368. }
  6369. return -1;
  6370. }
  6371. /*==========================================
  6372. * Check if sd has nameid equipped somewhere
  6373. * @sd : the player session
  6374. * @nameid : id of the item to check
  6375. * @min : : see pc.hpp enum equip_index from ? to @max
  6376. * @max : see pc.hpp enum equip_index for @min to ?
  6377. * -return true,false
  6378. *------------------------------------------*/
  6379. bool pc_checkequip2(map_session_data *sd, t_itemid nameid, int32 min, int32 max)
  6380. {
  6381. int32 i;
  6382. for(i = min; i < max; i++) {
  6383. if(equip_bitmask[i]) {
  6384. int32 idx = sd->equip_index[i];
  6385. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  6386. return true;
  6387. }
  6388. }
  6389. return false;
  6390. }
  6391. /*==========================================
  6392. * Convert's from the client's lame Job ID system
  6393. * to the map server's 'makes sense' system. [Skotlex]
  6394. *------------------------------------------*/
  6395. uint64 pc_jobid2mapid(uint16 b_class)
  6396. {
  6397. switch(b_class)
  6398. {
  6399. //Novice And 1-1 Jobs
  6400. case JOB_NOVICE: return MAPID_NOVICE;
  6401. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  6402. case JOB_MAGE: return MAPID_MAGE;
  6403. case JOB_ARCHER: return MAPID_ARCHER;
  6404. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  6405. case JOB_MERCHANT: return MAPID_MERCHANT;
  6406. case JOB_THIEF: return MAPID_THIEF;
  6407. case JOB_TAEKWON: return MAPID_TAEKWON;
  6408. case JOB_WEDDING: return MAPID_WEDDING;
  6409. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  6410. case JOB_NINJA: return MAPID_NINJA;
  6411. case JOB_XMAS: return MAPID_XMAS;
  6412. case JOB_SUMMER: return MAPID_SUMMER;
  6413. case JOB_HANBOK: return MAPID_HANBOK;
  6414. case JOB_GANGSI: return MAPID_GANGSI;
  6415. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  6416. case JOB_SUMMER2: return MAPID_SUMMER2;
  6417. //2-1 Jobs
  6418. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  6419. case JOB_KNIGHT: return MAPID_KNIGHT;
  6420. case JOB_WIZARD: return MAPID_WIZARD;
  6421. case JOB_HUNTER: return MAPID_HUNTER;
  6422. case JOB_PRIEST: return MAPID_PRIEST;
  6423. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  6424. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  6425. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  6426. case JOB_KAGEROU:
  6427. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  6428. case JOB_REBELLION: return MAPID_REBELLION;
  6429. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  6430. //2-2 Jobs
  6431. case JOB_CRUSADER: return MAPID_CRUSADER;
  6432. case JOB_SAGE: return MAPID_SAGE;
  6433. case JOB_BARD:
  6434. case JOB_DANCER: return MAPID_BARDDANCER;
  6435. case JOB_MONK: return MAPID_MONK;
  6436. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  6437. case JOB_ROGUE: return MAPID_ROGUE;
  6438. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  6439. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  6440. //Trans Novice And Trans 1-1 Jobs
  6441. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  6442. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  6443. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  6444. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  6445. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  6446. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  6447. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  6448. //Trans 2-1 Jobs
  6449. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  6450. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  6451. case JOB_SNIPER: return MAPID_SNIPER;
  6452. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  6453. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6454. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6455. //Trans 2-2 Jobs
  6456. case JOB_PALADIN: return MAPID_PALADIN;
  6457. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6458. case JOB_CLOWN:
  6459. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6460. case JOB_CHAMPION: return MAPID_CHAMPION;
  6461. case JOB_CREATOR: return MAPID_CREATOR;
  6462. case JOB_STALKER: return MAPID_STALKER;
  6463. //Baby Novice And Baby 1-1 Jobs
  6464. case JOB_BABY: return MAPID_BABY;
  6465. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6466. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6467. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6468. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6469. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6470. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6471. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6472. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6473. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6474. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6475. //Baby 2-1 Jobs
  6476. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6477. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6478. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6479. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6480. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6481. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6482. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6483. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6484. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6485. case JOB_BABY_KAGEROU:
  6486. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6487. //Baby 2-2 Jobs
  6488. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6489. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6490. case JOB_BABY_BARD:
  6491. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6492. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6493. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6494. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6495. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6496. //3-1 Jobs
  6497. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6498. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6499. case JOB_WARLOCK: return MAPID_WARLOCK;
  6500. case JOB_RANGER: return MAPID_RANGER;
  6501. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6502. case JOB_MECHANIC: return MAPID_MECHANIC;
  6503. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6504. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6505. //3-2 Jobs
  6506. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6507. case JOB_SORCERER: return MAPID_SORCERER;
  6508. case JOB_MINSTREL:
  6509. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6510. case JOB_SURA: return MAPID_SURA;
  6511. case JOB_GENETIC: return MAPID_GENETIC;
  6512. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6513. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6514. //Trans 3-1 Jobs
  6515. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6516. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6517. case JOB_RANGER_T: return MAPID_RANGER_T;
  6518. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6519. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6520. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6521. //Trans 3-2 Jobs
  6522. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6523. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6524. case JOB_MINSTREL_T:
  6525. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6526. case JOB_SURA_T: return MAPID_SURA_T;
  6527. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6528. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6529. //Baby 3-1 Jobs
  6530. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6531. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6532. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6533. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6534. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6535. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6536. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6537. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6538. //Baby 3-2 Jobs
  6539. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6540. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6541. case JOB_BABY_MINSTREL:
  6542. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6543. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6544. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6545. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6546. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6547. //Doram Jobs
  6548. case JOB_SUMMONER: return MAPID_SUMMONER;
  6549. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6550. //4-1 Jobs
  6551. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6552. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6553. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6554. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6555. case JOB_CARDINAL: return MAPID_CARDINAL;
  6556. case JOB_MEISTER: return MAPID_MEISTER;
  6557. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6558. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6559. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6560. case JOB_SHINKIRO:
  6561. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6562. //4-2 Jobs
  6563. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6564. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6565. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6566. case JOB_TROUBADOUR:
  6567. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6568. case JOB_BIOLO: return MAPID_BIOLO;
  6569. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6570. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6571. //Unknown
  6572. default:
  6573. return -1;
  6574. }
  6575. }
  6576. //Reverts the map-style class id to the client-style one.
  6577. int32 pc_mapid2jobid(uint64 class_, int32 sex)
  6578. {
  6579. switch(class_) {
  6580. //Novice And 1-1 Jobs
  6581. case MAPID_NOVICE: return JOB_NOVICE;
  6582. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6583. case MAPID_MAGE: return JOB_MAGE;
  6584. case MAPID_ARCHER: return JOB_ARCHER;
  6585. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6586. case MAPID_MERCHANT: return JOB_MERCHANT;
  6587. case MAPID_THIEF: return JOB_THIEF;
  6588. case MAPID_TAEKWON: return JOB_TAEKWON;
  6589. case MAPID_WEDDING: return JOB_WEDDING;
  6590. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6591. case MAPID_NINJA: return JOB_NINJA;
  6592. case MAPID_XMAS: return JOB_XMAS;
  6593. case MAPID_SUMMER: return JOB_SUMMER;
  6594. case MAPID_HANBOK: return JOB_HANBOK;
  6595. case MAPID_GANGSI: return JOB_GANGSI;
  6596. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6597. case MAPID_SUMMER2: return JOB_SUMMER2;
  6598. //2-1 Jobs
  6599. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6600. case MAPID_KNIGHT: return JOB_KNIGHT;
  6601. case MAPID_WIZARD: return JOB_WIZARD;
  6602. case MAPID_HUNTER: return JOB_HUNTER;
  6603. case MAPID_PRIEST: return JOB_PRIEST;
  6604. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6605. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6606. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6607. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6608. case MAPID_REBELLION: return JOB_REBELLION;
  6609. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6610. //2-2 Jobs
  6611. case MAPID_CRUSADER: return JOB_CRUSADER;
  6612. case MAPID_SAGE: return JOB_SAGE;
  6613. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6614. case MAPID_MONK: return JOB_MONK;
  6615. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6616. case MAPID_ROGUE: return JOB_ROGUE;
  6617. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6618. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6619. //Trans Novice And Trans 2-1 Jobs
  6620. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6621. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6622. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6623. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6624. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6625. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6626. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6627. //Trans 2-1 Jobs
  6628. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6629. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6630. case MAPID_SNIPER: return JOB_SNIPER;
  6631. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6632. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6633. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6634. //Trans 2-2 Jobs
  6635. case MAPID_PALADIN: return JOB_PALADIN;
  6636. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6637. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6638. case MAPID_CHAMPION: return JOB_CHAMPION;
  6639. case MAPID_CREATOR: return JOB_CREATOR;
  6640. case MAPID_STALKER: return JOB_STALKER;
  6641. //Baby Novice And Baby 1-1 Jobs
  6642. case MAPID_BABY: return JOB_BABY;
  6643. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6644. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6645. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6646. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6647. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6648. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6649. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6650. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6651. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6652. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6653. //Baby 2-1 Jobs
  6654. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6655. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6656. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6657. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6658. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6659. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6660. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6661. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6662. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6663. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6664. //Baby 2-2 Jobs
  6665. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6666. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6667. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6668. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6669. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6670. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6671. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6672. //3-1 Jobs
  6673. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6674. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6675. case MAPID_WARLOCK: return JOB_WARLOCK;
  6676. case MAPID_RANGER: return JOB_RANGER;
  6677. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6678. case MAPID_MECHANIC: return JOB_MECHANIC;
  6679. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6680. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6681. //3-2 Jobs
  6682. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6683. case MAPID_SORCERER: return JOB_SORCERER;
  6684. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6685. case MAPID_SURA: return JOB_SURA;
  6686. case MAPID_GENETIC: return JOB_GENETIC;
  6687. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6688. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6689. //Trans 3-1 Jobs
  6690. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6691. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6692. case MAPID_RANGER_T: return JOB_RANGER_T;
  6693. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6694. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6695. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6696. //Trans 3-2 Jobs
  6697. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6698. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6699. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6700. case MAPID_SURA_T: return JOB_SURA_T;
  6701. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6702. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6703. //Baby 3-1 Jobs
  6704. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6705. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6706. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6707. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6708. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6709. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6710. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6711. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6712. //Baby 3-2 Jobs
  6713. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6714. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6715. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6716. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6717. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6718. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6719. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6720. //Doram Jobs
  6721. case MAPID_SUMMONER: return JOB_SUMMONER;
  6722. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6723. //4-1 Jobs
  6724. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6725. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6726. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6727. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6728. case MAPID_CARDINAL: return JOB_CARDINAL;
  6729. case MAPID_MEISTER: return JOB_MEISTER;
  6730. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6731. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6732. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6733. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6734. //4-2 Jobs
  6735. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6736. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6737. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6738. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6739. case MAPID_BIOLO: return JOB_BIOLO;
  6740. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6741. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6742. //Unknown
  6743. default:
  6744. return -1;
  6745. }
  6746. }
  6747. /*====================================================
  6748. * This function return the name of the job (by [Yor])
  6749. *----------------------------------------------------*/
  6750. const char* job_name(int32 class_)
  6751. {
  6752. switch (class_) {
  6753. case JOB_NOVICE:
  6754. case JOB_SWORDMAN:
  6755. case JOB_MAGE:
  6756. case JOB_ARCHER:
  6757. case JOB_ACOLYTE:
  6758. case JOB_MERCHANT:
  6759. case JOB_THIEF:
  6760. return msg_txt(nullptr,550 - JOB_NOVICE+class_);
  6761. case JOB_KNIGHT:
  6762. case JOB_PRIEST:
  6763. case JOB_WIZARD:
  6764. case JOB_BLACKSMITH:
  6765. case JOB_HUNTER:
  6766. case JOB_ASSASSIN:
  6767. return msg_txt(nullptr,557 - JOB_KNIGHT+class_);
  6768. case JOB_KNIGHT2:
  6769. return msg_txt(nullptr,557);
  6770. case JOB_CRUSADER:
  6771. case JOB_MONK:
  6772. case JOB_SAGE:
  6773. case JOB_ROGUE:
  6774. case JOB_ALCHEMIST:
  6775. case JOB_BARD:
  6776. case JOB_DANCER:
  6777. return msg_txt(nullptr,563 - JOB_CRUSADER+class_);
  6778. case JOB_CRUSADER2:
  6779. return msg_txt(nullptr,563);
  6780. case JOB_WEDDING:
  6781. case JOB_SUPER_NOVICE:
  6782. case JOB_GUNSLINGER:
  6783. case JOB_NINJA:
  6784. case JOB_XMAS:
  6785. return msg_txt(nullptr,570 - JOB_WEDDING+class_);
  6786. case JOB_SUMMER:
  6787. case JOB_SUMMER2:
  6788. return msg_txt(nullptr,621);
  6789. case JOB_HANBOK:
  6790. return msg_txt(nullptr,694);
  6791. case JOB_OKTOBERFEST:
  6792. return msg_txt(nullptr,696);
  6793. case JOB_NOVICE_HIGH:
  6794. case JOB_SWORDMAN_HIGH:
  6795. case JOB_MAGE_HIGH:
  6796. case JOB_ARCHER_HIGH:
  6797. case JOB_ACOLYTE_HIGH:
  6798. case JOB_MERCHANT_HIGH:
  6799. case JOB_THIEF_HIGH:
  6800. return msg_txt(nullptr,575 - JOB_NOVICE_HIGH+class_);
  6801. case JOB_LORD_KNIGHT:
  6802. case JOB_HIGH_PRIEST:
  6803. case JOB_HIGH_WIZARD:
  6804. case JOB_WHITESMITH:
  6805. case JOB_SNIPER:
  6806. case JOB_ASSASSIN_CROSS:
  6807. return msg_txt(nullptr,582 - JOB_LORD_KNIGHT+class_);
  6808. case JOB_LORD_KNIGHT2:
  6809. return msg_txt(nullptr,582);
  6810. case JOB_PALADIN:
  6811. case JOB_CHAMPION:
  6812. case JOB_PROFESSOR:
  6813. case JOB_STALKER:
  6814. case JOB_CREATOR:
  6815. case JOB_CLOWN:
  6816. case JOB_GYPSY:
  6817. return msg_txt(nullptr,588 - JOB_PALADIN + class_);
  6818. case JOB_PALADIN2:
  6819. return msg_txt(nullptr,588);
  6820. case JOB_BABY:
  6821. case JOB_BABY_SWORDMAN:
  6822. case JOB_BABY_MAGE:
  6823. case JOB_BABY_ARCHER:
  6824. case JOB_BABY_ACOLYTE:
  6825. case JOB_BABY_MERCHANT:
  6826. case JOB_BABY_THIEF:
  6827. return msg_txt(nullptr,595 - JOB_BABY + class_);
  6828. case JOB_BABY_KNIGHT:
  6829. case JOB_BABY_PRIEST:
  6830. case JOB_BABY_WIZARD:
  6831. case JOB_BABY_BLACKSMITH:
  6832. case JOB_BABY_HUNTER:
  6833. case JOB_BABY_ASSASSIN:
  6834. return msg_txt(nullptr,602 - JOB_BABY_KNIGHT + class_);
  6835. case JOB_BABY_KNIGHT2:
  6836. return msg_txt(nullptr,602);
  6837. case JOB_BABY_CRUSADER:
  6838. case JOB_BABY_MONK:
  6839. case JOB_BABY_SAGE:
  6840. case JOB_BABY_ROGUE:
  6841. case JOB_BABY_ALCHEMIST:
  6842. case JOB_BABY_BARD:
  6843. case JOB_BABY_DANCER:
  6844. return msg_txt(nullptr,608 - JOB_BABY_CRUSADER + class_);
  6845. case JOB_BABY_CRUSADER2:
  6846. return msg_txt(nullptr,608);
  6847. case JOB_SUPER_BABY:
  6848. return msg_txt(nullptr,615);
  6849. case JOB_TAEKWON:
  6850. return msg_txt(nullptr,616);
  6851. case JOB_STAR_GLADIATOR:
  6852. case JOB_STAR_GLADIATOR2:
  6853. return msg_txt(nullptr,617);
  6854. case JOB_SOUL_LINKER:
  6855. return msg_txt(nullptr,618);
  6856. case JOB_GANGSI:
  6857. case JOB_DEATH_KNIGHT:
  6858. case JOB_DARK_COLLECTOR:
  6859. return msg_txt(nullptr,622 - JOB_GANGSI+class_);
  6860. case JOB_RUNE_KNIGHT:
  6861. case JOB_WARLOCK:
  6862. case JOB_RANGER:
  6863. case JOB_ARCH_BISHOP:
  6864. case JOB_MECHANIC:
  6865. case JOB_GUILLOTINE_CROSS:
  6866. return msg_txt(nullptr,625 - JOB_RUNE_KNIGHT+class_);
  6867. case JOB_RUNE_KNIGHT_T:
  6868. case JOB_WARLOCK_T:
  6869. case JOB_RANGER_T:
  6870. case JOB_ARCH_BISHOP_T:
  6871. case JOB_MECHANIC_T:
  6872. case JOB_GUILLOTINE_CROSS_T:
  6873. return msg_txt(nullptr,681 - JOB_RUNE_KNIGHT_T+class_);
  6874. case JOB_ROYAL_GUARD:
  6875. case JOB_SORCERER:
  6876. case JOB_MINSTREL:
  6877. case JOB_WANDERER:
  6878. case JOB_SURA:
  6879. case JOB_GENETIC:
  6880. case JOB_SHADOW_CHASER:
  6881. return msg_txt(nullptr,631 - JOB_ROYAL_GUARD+class_);
  6882. case JOB_ROYAL_GUARD_T:
  6883. case JOB_SORCERER_T:
  6884. case JOB_MINSTREL_T:
  6885. case JOB_WANDERER_T:
  6886. case JOB_SURA_T:
  6887. case JOB_GENETIC_T:
  6888. case JOB_SHADOW_CHASER_T:
  6889. return msg_txt(nullptr,687 - JOB_ROYAL_GUARD_T+class_);
  6890. case JOB_RUNE_KNIGHT2:
  6891. case JOB_RUNE_KNIGHT_T2:
  6892. return msg_txt(nullptr,625);
  6893. case JOB_ROYAL_GUARD2:
  6894. case JOB_ROYAL_GUARD_T2:
  6895. return msg_txt(nullptr,631);
  6896. case JOB_RANGER2:
  6897. case JOB_RANGER_T2:
  6898. return msg_txt(nullptr,627);
  6899. case JOB_MECHANIC2:
  6900. case JOB_MECHANIC_T2:
  6901. return msg_txt(nullptr,629);
  6902. case JOB_BABY_RUNE_KNIGHT:
  6903. case JOB_BABY_WARLOCK:
  6904. case JOB_BABY_RANGER:
  6905. case JOB_BABY_ARCH_BISHOP:
  6906. case JOB_BABY_MECHANIC:
  6907. case JOB_BABY_GUILLOTINE_CROSS:
  6908. case JOB_BABY_ROYAL_GUARD:
  6909. case JOB_BABY_SORCERER:
  6910. case JOB_BABY_MINSTREL:
  6911. case JOB_BABY_WANDERER:
  6912. case JOB_BABY_SURA:
  6913. case JOB_BABY_GENETIC:
  6914. case JOB_BABY_SHADOW_CHASER:
  6915. return msg_txt(nullptr,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6916. case JOB_BABY_RUNE_KNIGHT2:
  6917. return msg_txt(nullptr,638);
  6918. case JOB_BABY_ROYAL_GUARD2:
  6919. return msg_txt(nullptr,644);
  6920. case JOB_BABY_RANGER2:
  6921. return msg_txt(nullptr,640);
  6922. case JOB_BABY_MECHANIC2:
  6923. return msg_txt(nullptr,642);
  6924. case JOB_SUPER_NOVICE_E:
  6925. case JOB_SUPER_BABY_E:
  6926. return msg_txt(nullptr,651 - JOB_SUPER_NOVICE_E+class_);
  6927. case JOB_KAGEROU:
  6928. case JOB_OBORO:
  6929. return msg_txt(nullptr,653 - JOB_KAGEROU+class_);
  6930. case JOB_REBELLION:
  6931. return msg_txt(nullptr,695);
  6932. case JOB_SUMMONER:
  6933. return msg_txt(nullptr,697);
  6934. case JOB_BABY_SUMMONER:
  6935. return msg_txt(nullptr,698);
  6936. case JOB_BABY_NINJA:
  6937. return msg_txt(nullptr,699);
  6938. case JOB_BABY_KAGEROU:
  6939. case JOB_BABY_OBORO:
  6940. case JOB_BABY_TAEKWON:
  6941. case JOB_BABY_STAR_GLADIATOR:
  6942. case JOB_BABY_SOUL_LINKER:
  6943. case JOB_BABY_GUNSLINGER:
  6944. case JOB_BABY_REBELLION:
  6945. return msg_txt(nullptr,753 - JOB_BABY_KAGEROU+class_);
  6946. case JOB_BABY_STAR_GLADIATOR2:
  6947. return msg_txt(nullptr,756);
  6948. case JOB_STAR_EMPEROR:
  6949. case JOB_SOUL_REAPER:
  6950. case JOB_BABY_STAR_EMPEROR:
  6951. case JOB_BABY_SOUL_REAPER:
  6952. return msg_txt(nullptr,782 - JOB_STAR_EMPEROR + class_);
  6953. case JOB_STAR_EMPEROR2:
  6954. return msg_txt(nullptr,782);
  6955. case JOB_BABY_STAR_EMPEROR2:
  6956. return msg_txt(nullptr,784);
  6957. case JOB_DRAGON_KNIGHT:
  6958. case JOB_MEISTER:
  6959. case JOB_SHADOW_CROSS:
  6960. case JOB_ARCH_MAGE:
  6961. case JOB_CARDINAL:
  6962. case JOB_WINDHAWK:
  6963. case JOB_IMPERIAL_GUARD:
  6964. case JOB_BIOLO:
  6965. case JOB_ABYSS_CHASER:
  6966. case JOB_ELEMENTAL_MASTER:
  6967. case JOB_INQUISITOR:
  6968. case JOB_TROUBADOUR:
  6969. case JOB_TROUVERE:
  6970. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6971. case JOB_WINDHAWK2:
  6972. return msg_txt( nullptr, 805);
  6973. case JOB_MEISTER2:
  6974. return msg_txt( nullptr, 801 );
  6975. case JOB_DRAGON_KNIGHT2:
  6976. return msg_txt( nullptr, 800 );
  6977. case JOB_IMPERIAL_GUARD2:
  6978. return msg_txt( nullptr, 806 );
  6979. case JOB_SKY_EMPEROR:
  6980. case JOB_SOUL_ASCETIC:
  6981. case JOB_SHINKIRO:
  6982. case JOB_SHIRANUI:
  6983. case JOB_NIGHT_WATCH:
  6984. case JOB_HYPER_NOVICE:
  6985. case JOB_SPIRIT_HANDLER:
  6986. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6987. case JOB_SKY_EMPEROR2:
  6988. return msg_txt( nullptr, 813 );
  6989. default:
  6990. return msg_txt(nullptr,655);
  6991. }
  6992. }
  6993. /*====================================================
  6994. * Timered function to make id follow a target.
  6995. * @id = bl.id (player only atm)
  6996. * target is define in sd->followtarget (bl.id)
  6997. * used by pc_follow
  6998. *----------------------------------------------------*/
  6999. TIMER_FUNC(pc_follow_timer){
  7000. map_session_data *sd;
  7001. struct block_list *tbl;
  7002. sd = map_id2sd(id);
  7003. nullpo_ret(sd);
  7004. if (sd->followtimer != tid){
  7005. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  7006. sd->followtimer = INVALID_TIMER;
  7007. return 0;
  7008. }
  7009. sd->followtimer = INVALID_TIMER;
  7010. tbl = map_id2bl(sd->followtarget);
  7011. if (tbl == nullptr || pc_isdead(sd))
  7012. {
  7013. pc_stop_following(sd);
  7014. return 0;
  7015. }
  7016. // either player or target is currently detached from map blocks (could be teleporting),
  7017. // but still connected to this map, so we'll just increment the timer and check back later
  7018. if (sd->bl.prev != nullptr && tbl->prev != nullptr &&
  7019. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  7020. {
  7021. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, nullptr, nullptr)) {
  7022. if (!check_distance_bl(&sd->bl, tbl, 5))
  7023. unit_walktobl(&sd->bl, tbl, 5, 0);
  7024. } else
  7025. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  7026. }
  7027. sd->followtimer = add_timer(
  7028. tick + 1000, // increase time a bit to loosen up map's load
  7029. pc_follow_timer, sd->bl.id, 0);
  7030. return 0;
  7031. }
  7032. int32 pc_stop_following (map_session_data *sd)
  7033. {
  7034. nullpo_ret(sd);
  7035. if (sd->followtimer != INVALID_TIMER) {
  7036. delete_timer(sd->followtimer,pc_follow_timer);
  7037. sd->followtimer = INVALID_TIMER;
  7038. }
  7039. sd->followtarget = -1;
  7040. sd->ud.target_to = 0;
  7041. unit_stop_walking( &sd->bl, USW_FIXPOS );
  7042. return 0;
  7043. }
  7044. int32 pc_follow(map_session_data *sd,int32 target_id)
  7045. {
  7046. struct block_list *bl = map_id2bl(target_id);
  7047. if (bl == nullptr /*|| bl->type != BL_PC*/)
  7048. return 1;
  7049. if (sd->followtimer != INVALID_TIMER)
  7050. pc_stop_following(sd);
  7051. sd->followtarget = target_id;
  7052. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7053. return 0;
  7054. }
  7055. int32 pc_checkbaselevelup(map_session_data *sd) {
  7056. t_exp next = pc_nextbaseexp(sd);
  7057. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  7058. return 0;
  7059. uint32 base_level = sd->status.base_level;
  7060. do {
  7061. sd->status.base_exp -= next;
  7062. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7063. if( ( !battle_config.multi_level_up || ( battle_config.multi_level_up_base > 0 && sd->status.base_level >= battle_config.multi_level_up_base ) ) && sd->status.base_exp > next-1 )
  7064. sd->status.base_exp = next-1;
  7065. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  7066. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  7067. sd->status.base_level++;
  7068. if( pc_is_maxbaselv(sd) ){
  7069. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  7070. break;
  7071. }
  7072. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  7073. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  7074. status_calc_pet(sd->pd,SCO_NONE);
  7075. clif_updatestatus(*sd,SP_STATUSPOINT);
  7076. clif_updatestatus(*sd,SP_TRAITPOINT);
  7077. clif_updatestatus(*sd,SP_BASELEVEL);
  7078. clif_updatestatus(*sd,SP_BASEEXP);
  7079. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  7080. status_calc_pc(sd,SCO_FORCE);
  7081. status_percent_heal(&sd->bl,100,100);
  7082. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7083. for (const auto &status : status_db) {
  7084. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  7085. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, 120000); //All buffs with super novice flag for 2 minutes
  7086. }
  7087. if (sd->state.snovice_dead_flag)
  7088. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  7089. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  7090. for (const auto &status : status_db) {
  7091. if (status.second->flag[SCF_TAEKWONANGEL])
  7092. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  7093. }
  7094. }
  7095. clif_misceffect( sd->bl, NOTIFYEFFECT_BASE_LEVEL_UP );
  7096. npc_script_event( *sd, NPCE_BASELVUP );
  7097. if(sd->status.party_id)
  7098. party_send_levelup(sd);
  7099. pc_baselevelchanged(sd);
  7100. for (; base_level <= sd->status.base_level; base_level++) {
  7101. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  7102. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  7103. }
  7104. return 1;
  7105. }
  7106. void pc_baselevelchanged(map_session_data *sd) {
  7107. uint8 i;
  7108. for( i = 0; i < EQI_MAX; i++ ) {
  7109. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  7110. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (uint32)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  7111. pc_unequipitem(sd, sd->equip_index[i], 3);
  7112. }
  7113. }
  7114. pc_show_questinfo(sd);
  7115. }
  7116. int32 pc_checkjoblevelup(map_session_data *sd)
  7117. {
  7118. t_exp next = pc_nextjobexp(sd);
  7119. nullpo_ret(sd);
  7120. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  7121. return 0;
  7122. uint32 job_level = sd->status.job_level;
  7123. do {
  7124. sd->status.job_exp -= next;
  7125. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7126. if( ( !battle_config.multi_level_up || ( battle_config.multi_level_up_job > 0 && sd->status.job_level >= battle_config.multi_level_up_job ) ) && sd->status.job_exp > next-1 )
  7127. sd->status.job_exp = next-1;
  7128. sd->status.job_level ++;
  7129. sd->status.skill_point ++;
  7130. if( pc_is_maxjoblv(sd) ){
  7131. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  7132. break;
  7133. }
  7134. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  7135. clif_updatestatus(*sd,SP_JOBLEVEL);
  7136. clif_updatestatus(*sd,SP_JOBEXP);
  7137. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  7138. clif_updatestatus(*sd,SP_SKILLPOINT);
  7139. status_calc_pc(sd,SCO_FORCE);
  7140. clif_misceffect( sd->bl, NOTIFYEFFECT_JOB_LEVEL_UP );
  7141. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7142. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7143. npc_script_event( *sd, NPCE_JOBLVUP );
  7144. for (; job_level <= sd->status.job_level; job_level++)
  7145. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  7146. pc_show_questinfo(sd);
  7147. return 1;
  7148. }
  7149. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  7150. * @param sd Player
  7151. * @param base_exp Base EXP before peronal bonuses
  7152. * @param job_exp Job EXP before peronal bonuses
  7153. * @param src Block list that affecting the exp calculation
  7154. */
  7155. static void pc_calcexp(map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  7156. {
  7157. int32 bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  7158. if (src) {
  7159. status_data* status = status_get_status_data(*src);
  7160. if( sd->indexed_bonus.expaddrace[status->race] )
  7161. bonus += sd->indexed_bonus.expaddrace[status->race];
  7162. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  7163. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  7164. if( sd->indexed_bonus.expaddclass[status->class_] )
  7165. bonus += sd->indexed_bonus.expaddclass[status->class_];
  7166. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  7167. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  7168. if (battle_config.pk_mode &&
  7169. (int32)(status_get_lv(src) - sd->status.base_level) >= 20)
  7170. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  7171. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  7172. vip_bonus_base = battle_config.vip_base_exp_increase;
  7173. vip_bonus_job = battle_config.vip_job_exp_increase;
  7174. }
  7175. }
  7176. // Give EXPBOOST for quests even if src is nullptr.
  7177. if (sd->sc.getSCE(SC_EXPBOOST)) {
  7178. bonus += sd->sc.getSCE(SC_EXPBOOST)->val1;
  7179. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  7180. bonus += (sd->sc.getSCE(SC_EXPBOOST)->val1 / battle_config.vip_bm_increase);
  7181. }
  7182. if (sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND))
  7183. bonus += sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)->val1;
  7184. if (*base_exp) {
  7185. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  7186. *base_exp = cap_value(exp, 1, MAX_EXP);
  7187. }
  7188. // Give JEXPBOOST for quests even if src is nullptr.
  7189. if (sd->sc.getSCE(SC_JEXPBOOST))
  7190. bonus += sd->sc.getSCE(SC_JEXPBOOST)->val1;
  7191. if (sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)) // Increase Jexp as well
  7192. bonus += sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)->val1;
  7193. if (*job_exp) {
  7194. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  7195. *job_exp = cap_value(exp, 1, MAX_EXP);
  7196. }
  7197. return;
  7198. }
  7199. /**
  7200. * Show EXP gained by player in percentage by @showexp
  7201. * @param sd Player
  7202. * @param base_exp Base EXP gained/loss
  7203. * @param next_base_exp Base EXP needed for next base level
  7204. * @param job_exp Job EXP gained/loss
  7205. * @param next_job_exp Job EXP needed for next job level
  7206. * @param lost True:EXP penalty, lose EXP
  7207. **/
  7208. void pc_gainexp_disp(map_session_data *sd, t_exp base_exp, t_exp next_base_exp, t_exp job_exp, t_exp next_job_exp, bool lost) {
  7209. char output[CHAT_SIZE_MAX];
  7210. nullpo_retv(sd);
  7211. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  7212. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  7213. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  7214. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  7215. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  7216. }
  7217. /**
  7218. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  7219. * @param sd Player
  7220. * @param src EXP source
  7221. * @param base_exp Base EXP gained
  7222. * @param base_exp Job EXP gained
  7223. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  7224. * @return
  7225. **/
  7226. void pc_gainexp(map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  7227. {
  7228. t_exp nextb = 0, nextj = 0;
  7229. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  7230. nullpo_retv(sd);
  7231. if(sd->bl.prev == nullptr || pc_isdead(sd))
  7232. return;
  7233. if (!(exp_flag&2)) {
  7234. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  7235. return; // no exp on pvp maps
  7236. if (sd->status.guild_id>0)
  7237. base_exp -= guild_payexp(sd,base_exp);
  7238. }
  7239. flag = ((base_exp) ? 1 : 0) |
  7240. ((job_exp) ? 2 : 0) |
  7241. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  7242. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  7243. if (!(exp_flag&2))
  7244. pc_calcexp(sd, &base_exp, &job_exp, src);
  7245. nextb = pc_nextbaseexp(sd);
  7246. nextj = pc_nextjobexp(sd);
  7247. if (flag&4){
  7248. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  7249. base_exp = 0;
  7250. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  7251. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  7252. }
  7253. if (flag&8){
  7254. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  7255. job_exp = 0;
  7256. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  7257. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  7258. }
  7259. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  7260. //Note that this value should never be greater than the original
  7261. //therefore no overflow checks are needed. [Skotlex]
  7262. if (nextb > 0) {
  7263. float nextbp = (float) base_exp / (float) nextb;
  7264. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  7265. base_exp = (uint32)(battle_config.max_exp_gain_rate/1000.*nextb);
  7266. }
  7267. if (nextj > 0) {
  7268. float nextjp = (float) job_exp / (float) nextj;
  7269. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  7270. job_exp = (uint32)(battle_config.max_exp_gain_rate/1000.*nextj);
  7271. }
  7272. }
  7273. // Give EXP for Base Level
  7274. if (base_exp) {
  7275. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  7276. if (!pc_checkbaselevelup(sd))
  7277. clif_updatestatus(*sd,SP_BASEEXP);
  7278. }
  7279. // Give EXP for Job Level
  7280. if (job_exp) {
  7281. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  7282. if (!pc_checkjoblevelup(sd))
  7283. clif_updatestatus(*sd,SP_JOBEXP);
  7284. }
  7285. if (flag&1)
  7286. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  7287. if (flag&2)
  7288. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  7289. if (sd->state.showexp && (base_exp || job_exp))
  7290. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  7291. }
  7292. /**
  7293. * Lost Base/Job EXP from a player
  7294. * @param sd Player
  7295. * @param base_exp Base EXP lost
  7296. * @param job_exp Job EXP lost
  7297. **/
  7298. void pc_lostexp(map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  7299. nullpo_retv(sd);
  7300. if (base_exp) {
  7301. base_exp = u64min(sd->status.base_exp, base_exp);
  7302. sd->status.base_exp -= base_exp;
  7303. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  7304. clif_updatestatus(*sd, SP_BASEEXP);
  7305. }
  7306. if (job_exp) {
  7307. job_exp = u64min(sd->status.job_exp, job_exp);
  7308. sd->status.job_exp -= job_exp;
  7309. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  7310. clif_updatestatus(*sd, SP_JOBEXP);
  7311. }
  7312. if (sd->state.showexp && (base_exp || job_exp))
  7313. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  7314. }
  7315. /**
  7316. * Returns max base level for this character's class.
  7317. * @param job_id: Player's class
  7318. * @return Max Base Level
  7319. */
  7320. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  7321. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7322. return job ? job->max_base_level : 0;
  7323. }
  7324. /**
  7325. * Returns max base level for this character.
  7326. * @param sd Player
  7327. * @return Max Base Level
  7328. **/
  7329. uint32 pc_maxbaselv(map_session_data *sd){
  7330. return job_db.get_maxBaseLv(sd->status.class_);
  7331. }
  7332. /**
  7333. * Returns max job level for this character's class.
  7334. * @param job_id: Player's class
  7335. * @return Max Job Level
  7336. */
  7337. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  7338. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7339. return job ? job->max_job_level : 0;
  7340. }
  7341. /**
  7342. * Returns max job level for this character.
  7343. * @param sd Player
  7344. * @return Max Job Level
  7345. **/
  7346. uint32 pc_maxjoblv(map_session_data *sd){
  7347. return job_db.get_maxJobLv(sd->status.class_);
  7348. }
  7349. /**
  7350. * Check if player is reached max base level
  7351. * @param sd
  7352. * @return True if reached max level
  7353. **/
  7354. bool pc_is_maxbaselv(map_session_data *sd) {
  7355. nullpo_retr(false, sd);
  7356. return (sd->status.base_level >= pc_maxbaselv(sd));
  7357. }
  7358. /**
  7359. * Check if player is reached max base level
  7360. * @param sd
  7361. * @return True if reached max level
  7362. **/
  7363. bool pc_is_maxjoblv(map_session_data *sd) {
  7364. nullpo_retr(false, sd);
  7365. return (sd->status.job_level >= pc_maxjoblv(sd));
  7366. }
  7367. /**
  7368. * Returns base experience for this character's class.
  7369. * @param job_id: Player's class
  7370. * @param level: Player's level
  7371. * @return Base EXP
  7372. */
  7373. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  7374. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7375. return job ? job->base_exp[level - 1] : 0;
  7376. }
  7377. /**
  7378. * Base exp needed for player to level up.
  7379. * @param sd
  7380. * @return Base EXP needed for next base level
  7381. **/
  7382. t_exp pc_nextbaseexp(map_session_data *sd){
  7383. nullpo_ret(sd);
  7384. if (sd->status.base_level == 0) // Is this something that possible?
  7385. return 0;
  7386. if (pc_is_maxbaselv(sd))
  7387. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  7388. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  7389. }
  7390. /**
  7391. * Returns job experience for this character's class.
  7392. * @param job_id: Player's class
  7393. * @param level: Player's level
  7394. * @return Job EXP
  7395. */
  7396. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  7397. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7398. return job ? job->job_exp[level - 1] : 0;
  7399. }
  7400. /**
  7401. * Job exp needed for player to level up.
  7402. * @param sd
  7403. * @return Job EXP needed for next job level
  7404. **/
  7405. t_exp pc_nextjobexp(map_session_data *sd){
  7406. nullpo_ret(sd);
  7407. if (sd->status.job_level == 0) // Is this something that possible?
  7408. return 0;
  7409. if (pc_is_maxjoblv(sd))
  7410. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  7411. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  7412. }
  7413. /**
  7414. * Returns max weight base for this character's class.
  7415. * @param job_id: Player's class
  7416. * @return Max weight base
  7417. */
  7418. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  7419. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7420. return job ? job->max_weight_base : 0;
  7421. }
  7422. /// Returns the value of the specified stat.
  7423. int32 pc_getstat(map_session_data *sd, int32 type)
  7424. {
  7425. nullpo_retr(-1, sd);
  7426. switch( type ) {
  7427. case SP_STR: return sd->status.str;
  7428. case SP_AGI: return sd->status.agi;
  7429. case SP_VIT: return sd->status.vit;
  7430. case SP_INT: return sd->status.int_;
  7431. case SP_DEX: return sd->status.dex;
  7432. case SP_LUK: return sd->status.luk;
  7433. case SP_POW: return sd->status.pow;
  7434. case SP_STA: return sd->status.sta;
  7435. case SP_WIS: return sd->status.wis;
  7436. case SP_SPL: return sd->status.spl;
  7437. case SP_CON: return sd->status.con;
  7438. case SP_CRT: return sd->status.crt;
  7439. default:
  7440. return -1;
  7441. }
  7442. }
  7443. /// Sets the specified stat to the specified value.
  7444. /// Returns the new value.
  7445. int32 pc_setstat(map_session_data* sd, int32 type, int32 val)
  7446. {
  7447. nullpo_retr(-1, sd);
  7448. switch( type ) {
  7449. case SP_STR: sd->status.str = val; break;
  7450. case SP_AGI: sd->status.agi = val; break;
  7451. case SP_VIT: sd->status.vit = val; break;
  7452. case SP_INT: sd->status.int_ = val; break;
  7453. case SP_DEX: sd->status.dex = val; break;
  7454. case SP_LUK: sd->status.luk = val; break;
  7455. case SP_POW: sd->status.pow = val; break;
  7456. case SP_STA: sd->status.sta = val; break;
  7457. case SP_WIS: sd->status.wis = val; break;
  7458. case SP_SPL: sd->status.spl = val; break;
  7459. case SP_CON: sd->status.con = val; break;
  7460. case SP_CRT: sd->status.crt = val; break;
  7461. default:
  7462. return -1;
  7463. }
  7464. return val;
  7465. }
  7466. /**
  7467. * Gets the total number of status points at the provided level.
  7468. * @param level: Player base level.
  7469. * @return Total number of status points at specific base level.
  7470. */
  7471. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7472. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7473. if( entry != nullptr ){
  7474. return entry->statpoints;
  7475. }else{
  7476. return 0;
  7477. }
  7478. }
  7479. /**
  7480. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7481. * @param level: Player base level.
  7482. * @param table: Use table value or formula.
  7483. * @return Status points at specific base level.
  7484. */
  7485. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7486. uint32 next_level = this->get_table_point( level + 1 );
  7487. uint32 current_level = this->get_table_point( level );
  7488. if( next_level > current_level ){
  7489. return next_level - current_level;
  7490. }else{
  7491. return 0;
  7492. }
  7493. }
  7494. /**
  7495. * Gets the total number of trait points at the provided level.
  7496. * @param level: Player base level.
  7497. * @return Total number of trait points at specific base level.
  7498. */
  7499. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7500. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7501. if( entry != nullptr ){
  7502. return entry->traitpoints;
  7503. }else{
  7504. return 0;
  7505. }
  7506. }
  7507. /**
  7508. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7509. * @param level: Player base level.
  7510. * @param table: Use table value or formula.
  7511. * @return Trait points at specific base level.
  7512. */
  7513. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7514. uint32 next_level = this->get_trait_table_point( level + 1 );
  7515. uint32 current_level = this->get_trait_table_point( level );
  7516. if( next_level > current_level ){
  7517. return next_level - current_level;
  7518. }else{
  7519. return 0;
  7520. }
  7521. }
  7522. #ifdef RENEWAL_STAT
  7523. /// Renewal status point cost formula
  7524. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7525. #else
  7526. /// Pre-Renewal status point cost formula
  7527. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7528. #endif
  7529. /// Returns the number of stat points needed to change the specified stat by val.
  7530. /// If val is negative, returns the number of stat points that would be needed to
  7531. /// raise the specified stat from (current value - val) to current value.
  7532. int32 pc_need_status_point(map_session_data* sd, int32 type, int32 val)
  7533. {
  7534. int32 low, high, sp = 0, max = 0;
  7535. if ( val == 0 )
  7536. return 0;
  7537. low = pc_getstat(sd,type);
  7538. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7539. if ( low >= max && val > 0 )
  7540. return 0; // Official servers show '0' when max is reached
  7541. high = low + val;
  7542. if ( val < 0 )
  7543. std::swap(low, high);
  7544. for ( ; low < high; low++ )
  7545. sp += PC_STATUS_POINT_COST(low);
  7546. return sp;
  7547. }
  7548. /**
  7549. * Returns the value the specified stat can be increased by with the current
  7550. * amount of available status points for the current character's class.
  7551. *
  7552. * @param sd The target character.
  7553. * @param type Stat to verify.
  7554. * @return Maximum value the stat could grow by.
  7555. */
  7556. int32 pc_maxparameterincrease(map_session_data* sd, int32 type)
  7557. {
  7558. int32 base, final_val, status_points, max_param;
  7559. nullpo_ret(sd);
  7560. base = final_val = pc_getstat(sd, type);
  7561. status_points = sd->status.status_point;
  7562. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7563. while (final_val <= max_param && status_points >= 0) {
  7564. status_points -= PC_STATUS_POINT_COST(final_val);
  7565. final_val++;
  7566. }
  7567. final_val--;
  7568. return (final_val > base ? final_val-base : 0);
  7569. }
  7570. /**
  7571. * Raises a stat by the specified amount.
  7572. *
  7573. * Obeys max_parameter limits.
  7574. * Subtracts status points according to the cost of the increased stat points.
  7575. *
  7576. * @param sd The target character.
  7577. * @param type The stat to change (see enum _sp)
  7578. * @param increase The stat increase (strictly positive) amount.
  7579. * @retval true if the stat was increased by any amount.
  7580. * @retval false if there were no changes.
  7581. */
  7582. bool pc_statusup(map_session_data* sd, int32 type, int32 increase)
  7583. {
  7584. int32 max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7585. nullpo_ret(sd);
  7586. // check conditions
  7587. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7588. clif_statusupack( *sd, type, false );
  7589. return false;
  7590. }
  7591. // check limits
  7592. current = pc_getstat(sd, type);
  7593. max_increase = pc_maxparameterincrease(sd, type);
  7594. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7595. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7596. clif_statusupack( *sd, type, false );
  7597. return false;
  7598. }
  7599. // check status points
  7600. needed_points = pc_need_status_point(sd, type, increase);
  7601. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7602. clif_statusupack( *sd, type, false );
  7603. return false;
  7604. }
  7605. // set new values
  7606. final_value = pc_setstat(sd, type, current + increase);
  7607. sd->status.status_point -= needed_points;
  7608. status_calc_pc(sd,SCO_NONE);
  7609. // update increase cost indicator
  7610. clif_updatestatus(*sd, static_cast<_sp>( SP_USTR + type-SP_STR ) );
  7611. // update statpoint count
  7612. clif_updatestatus(*sd, SP_STATUSPOINT);
  7613. // update stat value
  7614. clif_statusupack( *sd, type, true, final_value );
  7615. if( final_value > 255 )
  7616. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7617. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7618. return true;
  7619. }
  7620. /**
  7621. * Raises a stat by the specified amount.
  7622. *
  7623. * Obeys max_parameter limits.
  7624. * Does not subtract status points for the cost of the modified stat points.
  7625. *
  7626. * @param sd The target character.
  7627. * @param type The stat to change (see enum _sp)
  7628. * @param val The stat increase (or decrease) amount.
  7629. * @return the stat increase amount.
  7630. * @retval 0 if no changes were made.
  7631. */
  7632. int32 pc_statusup2(map_session_data* sd, int32 type, int32 val)
  7633. {
  7634. int32 max, need;
  7635. nullpo_ret(sd);
  7636. if( type < SP_STR || type > SP_LUK )
  7637. {
  7638. clif_statusupack( *sd, type, false );
  7639. return 0;
  7640. }
  7641. need = pc_need_status_point(sd,type,1);
  7642. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7643. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7644. status_calc_pc(sd,SCO_NONE);
  7645. // update increase cost indicator
  7646. if( need != pc_need_status_point(sd,type,1) )
  7647. clif_updatestatus(*sd, static_cast<_sp>( SP_USTR + type-SP_STR ) );
  7648. // update stat value
  7649. clif_statusupack( *sd, type, true, val );
  7650. if( val > 255 )
  7651. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7652. return val;
  7653. }
  7654. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7655. /// If val is negative, returns the number of trait stat points that would be needed to
  7656. /// raise the specified trait stat from (current value - val) to current value.
  7657. int32 pc_need_trait_point(map_session_data* sd, int32 type, int32 val)
  7658. {
  7659. nullpo_retr(0, sd);
  7660. if (val == 0 || type < SP_POW || type > SP_CRT)
  7661. return 0;
  7662. int32 low = pc_getstat(sd, type);
  7663. int32 max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7664. if (low >= max && val > 0)
  7665. return 0; // Official servers show '0' when max is reached
  7666. int32 high = low + val, sp = 0;
  7667. if (val < 0)
  7668. std::swap(low, high);
  7669. for (; low < high; low++)
  7670. sp += 1;
  7671. return sp;
  7672. }
  7673. /**
  7674. * Returns the value the specified trait stat can be increased by with the current
  7675. * amount of available trait status points for the current character's class.
  7676. *
  7677. * @param sd The target character.
  7678. * @param type Trait stat to verify.
  7679. * @return Maximum value the stat could grow by.
  7680. */
  7681. int32 pc_maxtraitparameterincrease(map_session_data* sd, int32 type)
  7682. {
  7683. nullpo_ret(sd);
  7684. if( type < SP_POW || type > SP_CRT ){
  7685. return 0;
  7686. }
  7687. int32 base, final_val = pc_getstat(sd, type);
  7688. int32 trait_points = sd->status.trait_point;
  7689. int32 max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7690. base = final_val;
  7691. while (final_val <= max_param && trait_points >= 0) {
  7692. trait_points -= 1;
  7693. final_val++;
  7694. }
  7695. final_val--;
  7696. return (final_val > base ? final_val - base : 0);
  7697. }
  7698. /**
  7699. * Raises a trait stat by the specified amount.
  7700. *
  7701. * Obeys max_traitparameter limits.
  7702. * Subtracts trait status points according to the cost of the increased trait stat points.
  7703. *
  7704. * @param sd The target character.
  7705. * @param type The stat to change (see enum _sp)
  7706. * @param increase The stat increase (strictly positive) amount.
  7707. * @retval true if the trait stat was increased by any amount.
  7708. * @retval false if there were no changes.
  7709. */
  7710. bool pc_traitstatusup(map_session_data* sd, int32 type, int32 increase)
  7711. {
  7712. nullpo_ret(sd);
  7713. // check conditions
  7714. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7715. clif_statusupack( *sd, type, false );
  7716. return false;
  7717. }
  7718. // check limits
  7719. int32 current = pc_getstat(sd, type);
  7720. int32 max_increase = pc_maxtraitparameterincrease(sd, type);
  7721. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7722. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7723. clif_statusupack( *sd, type, false );
  7724. return false;
  7725. }
  7726. // check status points
  7727. int32 needed_points = pc_need_trait_point(sd, type, increase);
  7728. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7729. clif_statusupack( *sd, type, false );
  7730. return false;
  7731. }
  7732. // set new values
  7733. int32 final_value = pc_setstat(sd, type, current + increase);
  7734. sd->status.trait_point -= needed_points;
  7735. status_calc_pc(sd, SCO_NONE);
  7736. // update increase cost indicator
  7737. clif_updatestatus(*sd, static_cast<_sp>( SP_UPOW + type - SP_POW ) );
  7738. // update statpoint count
  7739. clif_updatestatus(*sd, SP_TRAITPOINT);
  7740. // update stat value
  7741. clif_statusupack( *sd, type, true, final_value );
  7742. if (final_value > 255)
  7743. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7744. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7745. return true;
  7746. }
  7747. /**
  7748. * Raises a trait stat by the specified amount.
  7749. *
  7750. * Obeys max_trait_parameter limits.
  7751. * Does not subtract status points for the cost of the modified stat points.
  7752. *
  7753. * @param sd The target character.
  7754. * @param type The stat to change (see enum _sp)
  7755. * @param val The stat increase (or decrease) amount.
  7756. * @return the stat increase amount.
  7757. * @retval 0 if no changes were made.
  7758. */
  7759. int32 pc_traitstatusup2(map_session_data* sd, int32 type, int32 val)
  7760. {
  7761. nullpo_ret(sd);
  7762. if (type < SP_POW || type > SP_CRT) {
  7763. clif_statusupack( *sd, type, false );
  7764. return 0;
  7765. }
  7766. int32 need = pc_need_trait_point(sd, type, 1);
  7767. int32 max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7768. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7769. status_calc_pc(sd, SCO_NONE);
  7770. // update increase cost indicator
  7771. if (need != pc_need_trait_point(sd, type, 1))
  7772. clif_updatestatus(*sd, static_cast<_sp>( SP_UPOW + type - SP_POW ) );
  7773. // update stat value
  7774. clif_statusupack( *sd, type, true, val );
  7775. if (val > 255)
  7776. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7777. return val;
  7778. }
  7779. /*==========================================
  7780. * Update skill_lv for player sd
  7781. * Skill point allocation
  7782. *------------------------------------------*/
  7783. void pc_skillup(map_session_data *sd,uint16 skill_id)
  7784. {
  7785. uint16 idx = skill_get_index(skill_id);
  7786. nullpo_retv(sd);
  7787. if (!idx) {
  7788. if (skill_id)
  7789. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7790. return;
  7791. }
  7792. // Level up guild skill
  7793. if (SKILL_CHK_GUILD(skill_id)) {
  7794. guild_skillup(sd, skill_id);
  7795. return;
  7796. }
  7797. // Level up homunculus skill
  7798. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7799. hom_skillup(sd->hd, skill_id);
  7800. return;
  7801. }
  7802. else {
  7803. if( sd->status.skill_point > 0 &&
  7804. sd->status.skill[idx].id &&
  7805. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7806. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7807. {
  7808. sd->status.skill[idx].lv++;
  7809. sd->status.skill_point--;
  7810. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7811. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7812. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7813. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7814. else
  7815. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7816. uint16 lv = sd->status.skill[idx].lv;
  7817. int32 range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7818. bool upgradable = ( lv < skill_tree_get_max( sd->status.skill[idx].id, sd->status.class_ ) );
  7819. clif_skillup( *sd, skill_id, lv, range, upgradable );
  7820. clif_updatestatus(*sd,SP_SKILLPOINT);
  7821. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7822. clif_updatestatus(*sd,SP_CARTINFO);
  7823. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7824. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7825. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7826. clif_skillinfoblock(sd);
  7827. }
  7828. //else
  7829. // ShowDebug("Skill Level up failed. ID:%d idx:%d (CID=%d. AID=%d)\n", skill_id, idx, sd->status.char_id, sd->status.account_id);
  7830. }
  7831. }
  7832. /*==========================================
  7833. * /allskill
  7834. *------------------------------------------*/
  7835. int32 pc_allskillup(map_session_data *sd)
  7836. {
  7837. int32 i;
  7838. nullpo_ret(sd);
  7839. for (i = 0; i < MAX_SKILL; i++) {
  7840. if (sd->status.skill[i].flag != SKILL_FLAG_PERMANENT && sd->status.skill[i].flag != SKILL_FLAG_PERM_GRANTED && sd->status.skill[i].flag != SKILL_FLAG_PLAGIARIZED) {
  7841. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7842. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7843. if (sd->status.skill[i].lv == 0)
  7844. sd->status.skill[i].id = 0;
  7845. }
  7846. }
  7847. if (!pc_grant_allskills(sd, true)) {
  7848. uint16 sk_id;
  7849. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7850. if (tree != nullptr && !tree->skills.empty()) {
  7851. for (const auto &skillsit : tree->skills) {
  7852. sk_id = skillsit.first;
  7853. uint16 sk_idx = skill_get_index(sk_id);
  7854. if (sk_idx == 0)
  7855. continue;
  7856. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7857. if (
  7858. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7859. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7860. sk_id == SG_DEVIL
  7861. )
  7862. continue; //Cannot be learned normally.
  7863. sd->status.skill[sk_idx].id = sk_id;
  7864. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7865. }
  7866. }
  7867. }
  7868. status_calc_pc(sd,SCO_NONE);
  7869. //Required because if you could level up all skills previously,
  7870. //the update will not be sent as only the lv variable changes.
  7871. clif_skillinfoblock(sd);
  7872. return 0;
  7873. }
  7874. /*==========================================
  7875. * /resetlvl
  7876. *------------------------------------------*/
  7877. int32 pc_resetlvl(map_session_data* sd,int32 type)
  7878. {
  7879. int32 i;
  7880. nullpo_ret(sd);
  7881. if (type != 3) //Also reset skills
  7882. pc_resetskill(sd, 0);
  7883. if(type == 1){
  7884. sd->status.skill_point=0;
  7885. sd->status.trait_point = 0;
  7886. sd->status.base_level=1;
  7887. sd->status.job_level=1;
  7888. sd->status.base_exp=0;
  7889. sd->status.job_exp=0;
  7890. if(sd->sc.option !=0)
  7891. sd->sc.option = 0;
  7892. sd->status.str=1;
  7893. sd->status.agi=1;
  7894. sd->status.vit=1;
  7895. sd->status.int_=1;
  7896. sd->status.dex=1;
  7897. sd->status.luk=1;
  7898. sd->status.pow=0;
  7899. sd->status.sta=0;
  7900. sd->status.wis=0;
  7901. sd->status.spl=0;
  7902. sd->status.con=0;
  7903. sd->status.crt=0;
  7904. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7905. sd->status.status_point=100; // not 88 [celest]
  7906. // give platinum skills upon changing
  7907. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7908. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7909. }
  7910. }
  7911. if(type == 2){
  7912. sd->status.skill_point=0;
  7913. sd->status.base_level=1;
  7914. sd->status.job_level=1;
  7915. sd->status.base_exp=0;
  7916. sd->status.job_exp=0;
  7917. }
  7918. if(type == 3){
  7919. sd->status.base_level=1;
  7920. sd->status.base_exp=0;
  7921. }
  7922. if(type == 4){
  7923. sd->status.job_level=1;
  7924. sd->status.job_exp=0;
  7925. }
  7926. clif_updatestatus(*sd,SP_STATUSPOINT);
  7927. clif_updatestatus(*sd,SP_TRAITPOINT);
  7928. clif_updatestatus(*sd,SP_STR);
  7929. clif_updatestatus(*sd,SP_AGI);
  7930. clif_updatestatus(*sd,SP_VIT);
  7931. clif_updatestatus(*sd,SP_INT);
  7932. clif_updatestatus(*sd,SP_DEX);
  7933. clif_updatestatus(*sd,SP_LUK);
  7934. clif_updatestatus(*sd,SP_POW);
  7935. clif_updatestatus(*sd,SP_STA);
  7936. clif_updatestatus(*sd,SP_WIS);
  7937. clif_updatestatus(*sd,SP_SPL);
  7938. clif_updatestatus(*sd,SP_CON);
  7939. clif_updatestatus(*sd,SP_CRT);
  7940. clif_updatestatus(*sd,SP_BASELEVEL);
  7941. clif_updatestatus(*sd,SP_JOBLEVEL);
  7942. clif_updatestatus(*sd,SP_STATUSPOINT);
  7943. clif_updatestatus(*sd,SP_BASEEXP);
  7944. clif_updatestatus(*sd,SP_JOBEXP);
  7945. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  7946. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  7947. clif_updatestatus(*sd,SP_SKILLPOINT);
  7948. clif_updatestatus(*sd,SP_USTR); // Updates needed stat points - Valaris
  7949. clif_updatestatus(*sd,SP_UAGI);
  7950. clif_updatestatus(*sd,SP_UVIT);
  7951. clif_updatestatus(*sd,SP_UINT);
  7952. clif_updatestatus(*sd,SP_UDEX);
  7953. clif_updatestatus(*sd,SP_ULUK); // End Addition
  7954. clif_updatestatus(*sd,SP_UPOW);
  7955. clif_updatestatus(*sd,SP_USTA);
  7956. clif_updatestatus(*sd,SP_UWIS);
  7957. clif_updatestatus(*sd,SP_USPL);
  7958. clif_updatestatus(*sd,SP_UCON);
  7959. clif_updatestatus(*sd,SP_UCRT);
  7960. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7961. if(sd->equip_index[i] >= 0)
  7962. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  7963. pc_unequipitem(sd,sd->equip_index[i],2);
  7964. }
  7965. }
  7966. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7967. party_send_levelup(sd);
  7968. status_calc_pc(sd, SCO_FORCE);
  7969. clif_skillinfoblock(sd);
  7970. return 0;
  7971. }
  7972. /*==========================================
  7973. * /resetstate
  7974. *------------------------------------------*/
  7975. int32 pc_resetstate(map_session_data* sd)
  7976. {
  7977. nullpo_ret(sd);
  7978. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7979. ShowError( "pc_resetstate: Capping the Level to %d to reset the stats of %d:%d, the base level (%d) is greater than the max level supported.\n",
  7980. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7981. sd->status.base_level = pc_maxbaselv( sd );
  7982. clif_updatestatus( *sd, SP_BASELEVEL );
  7983. }
  7984. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7985. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7986. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7987. sd->status.status_point += battle_config.transcendent_status_points;
  7988. }
  7989. if ((sd->class_&JOBL_FOURTH) != 0) {
  7990. sd->status.trait_point += battle_config.trait_points_job_change;
  7991. }
  7992. pc_setstat(sd, SP_STR, 1);
  7993. pc_setstat(sd, SP_AGI, 1);
  7994. pc_setstat(sd, SP_VIT, 1);
  7995. pc_setstat(sd, SP_INT, 1);
  7996. pc_setstat(sd, SP_DEX, 1);
  7997. pc_setstat(sd, SP_LUK, 1);
  7998. pc_setstat(sd, SP_POW, 0);
  7999. pc_setstat(sd, SP_STA, 0);
  8000. pc_setstat(sd, SP_WIS, 0);
  8001. pc_setstat(sd, SP_SPL, 0);
  8002. pc_setstat(sd, SP_CON, 0);
  8003. pc_setstat(sd, SP_CRT, 0);
  8004. clif_updatestatus(*sd,SP_STR);
  8005. clif_updatestatus(*sd,SP_AGI);
  8006. clif_updatestatus(*sd,SP_VIT);
  8007. clif_updatestatus(*sd,SP_INT);
  8008. clif_updatestatus(*sd,SP_DEX);
  8009. clif_updatestatus(*sd,SP_LUK);
  8010. clif_updatestatus(*sd,SP_POW);
  8011. clif_updatestatus(*sd,SP_STA);
  8012. clif_updatestatus(*sd,SP_WIS);
  8013. clif_updatestatus(*sd,SP_SPL);
  8014. clif_updatestatus(*sd,SP_CON);
  8015. clif_updatestatus(*sd,SP_CRT);
  8016. clif_updatestatus(*sd,SP_USTR); // Updates needed stat points - Valaris
  8017. clif_updatestatus(*sd,SP_UAGI);
  8018. clif_updatestatus(*sd,SP_UVIT);
  8019. clif_updatestatus(*sd,SP_UINT);
  8020. clif_updatestatus(*sd,SP_UDEX);
  8021. clif_updatestatus(*sd,SP_ULUK); // End Addition
  8022. clif_updatestatus(*sd,SP_UPOW);
  8023. clif_updatestatus(*sd,SP_USTA);
  8024. clif_updatestatus(*sd,SP_UWIS);
  8025. clif_updatestatus(*sd,SP_USPL);
  8026. clif_updatestatus(*sd,SP_UCON);
  8027. clif_updatestatus(*sd,SP_UCRT);
  8028. clif_updatestatus(*sd,SP_STATUSPOINT);
  8029. clif_updatestatus(*sd,SP_TRAITPOINT);
  8030. if( sd->mission_mobid ) { //bugreport:2200
  8031. sd->mission_mobid = 0;
  8032. sd->mission_count = 0;
  8033. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  8034. }
  8035. status_calc_pc(sd, SCO_NONE);
  8036. return 1;
  8037. }
  8038. /*==========================================
  8039. * /resetskill
  8040. * if flag&1, perform block resync and status_calc call.
  8041. * if flag&2, just count total amount of skill points used by player, do not really reset.
  8042. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  8043. *------------------------------------------*/
  8044. int32 pc_resetskill(map_session_data* sd, int32 flag)
  8045. {
  8046. int32 i, skill_point=0;
  8047. nullpo_ret(sd);
  8048. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  8049. return 0;
  8050. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  8051. /**
  8052. * It has been confirmed on official servers that when you reset skills with a ranked Taekwon your skills are not reset (because you have all of them anyway)
  8053. **/
  8054. if( pc_is_taekwon_ranker(sd) )
  8055. return 0;
  8056. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  8057. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  8058. i = sd->sc.option;
  8059. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  8060. i &= ~OPTION_RIDING;
  8061. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  8062. i &= ~OPTION_FALCON;
  8063. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  8064. i &= ~OPTION_DRAGON;
  8065. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  8066. i &= ~OPTION_WUG;
  8067. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  8068. i &= ~OPTION_WUGRIDER;
  8069. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  8070. i &= ~OPTION_MADOGEAR;
  8071. #ifndef NEW_CARTS
  8072. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  8073. i &= ~OPTION_CART;
  8074. #else
  8075. if( sd->sc.getSCE(SC_PUSH_CART) )
  8076. pc_setcart(sd, 0);
  8077. #endif
  8078. if( i != sd->sc.option )
  8079. pc_setoption(sd, i);
  8080. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  8081. hom_vaporize(sd, HOM_ST_ACTIVE);
  8082. if (sd->sc.getSCE(SC_SPRITEMABLE) && pc_checkskill(sd, SU_SPRITEMABLE))
  8083. status_change_end(&sd->bl, SC_SPRITEMABLE);
  8084. if (sd->sc.getSCE(SC_SOULATTACK) && pc_checkskill(sd, SU_SOULATTACK))
  8085. status_change_end(&sd->bl, SC_SOULATTACK);
  8086. }
  8087. for (const auto &skill : skill_db) {
  8088. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  8089. uint8 lv = sd->status.skill[idx].lv;
  8090. if (lv == 0 || skill_id == 0)
  8091. continue;
  8092. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  8093. continue;
  8094. // Don't reset trick dead if not a novice/baby
  8095. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8096. {
  8097. sd->status.skill[idx].lv = 0;
  8098. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8099. continue;
  8100. }
  8101. // do not reset basic skill
  8102. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8103. continue;
  8104. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  8105. continue;
  8106. if( flag&4 && !skill_ischangesex(skill_id) )
  8107. continue;
  8108. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  8109. { //Only handle quest skills in a special way when you can't learn them manually
  8110. if( battle_config.quest_skill_reset && !(flag&2) )
  8111. { //Wipe them
  8112. sd->status.skill[idx].lv = 0;
  8113. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8114. }
  8115. continue;
  8116. }
  8117. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  8118. skill_point += lv;
  8119. else
  8120. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  8121. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  8122. if( !(flag&2) )
  8123. {// reset
  8124. sd->status.skill[idx].lv = 0;
  8125. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8126. }
  8127. }
  8128. if( flag&2 || !skill_point ) return skill_point;
  8129. sd->status.skill_point += skill_point;
  8130. if (flag&1) {
  8131. clif_updatestatus(*sd,SP_SKILLPOINT);
  8132. clif_skillinfoblock(sd);
  8133. status_calc_pc(sd, SCO_FORCE);
  8134. }
  8135. return skill_point;
  8136. }
  8137. /*==========================================
  8138. * /resetfeel [Komurka]
  8139. *------------------------------------------*/
  8140. int32 pc_resetfeel(map_session_data* sd)
  8141. {
  8142. int32 i;
  8143. nullpo_ret(sd);
  8144. for (i=0; i<MAX_PC_FEELHATE; i++)
  8145. {
  8146. sd->feel_map[i].m = -1;
  8147. sd->feel_map[i].index = 0;
  8148. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  8149. }
  8150. return 0;
  8151. }
  8152. int32 pc_resethate(map_session_data* sd)
  8153. {
  8154. int32 i;
  8155. nullpo_ret(sd);
  8156. for (i=0; i<MAX_PC_FEELHATE; i++)
  8157. {
  8158. sd->hate_mob[i] = -1;
  8159. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  8160. }
  8161. return 0;
  8162. }
  8163. int32 pc_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8164. {
  8165. int32 bonus = 0;
  8166. nullpo_ret(sd);
  8167. skill_id = skill_dummy2skill_id(skill_id);
  8168. for (auto &it : sd->skillatk) {
  8169. if (it.id == skill_id) {
  8170. bonus += it.val;
  8171. break;
  8172. }
  8173. }
  8174. return bonus;
  8175. }
  8176. int32 pc_sub_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8177. {
  8178. int32 bonus = 0;
  8179. nullpo_ret(sd);
  8180. skill_id = skill_dummy2skill_id(skill_id);
  8181. for (auto &it : sd->subskill) {
  8182. if (it.id == skill_id) {
  8183. bonus += it.val;
  8184. break;
  8185. }
  8186. }
  8187. return bonus;
  8188. }
  8189. int32 pc_skillheal_bonus(map_session_data *sd, uint16 skill_id) {
  8190. int32 bonus = sd->bonus.add_heal_rate;
  8191. nullpo_ret(sd);
  8192. skill_id = skill_dummy2skill_id(skill_id);
  8193. if( bonus ) {
  8194. switch( skill_id ) {
  8195. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  8196. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  8197. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  8198. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  8199. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  8200. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  8201. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  8202. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  8203. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  8204. }
  8205. }
  8206. for (auto &it : sd->skillheal) {
  8207. if (it.id == skill_id) {
  8208. bonus += it.val;
  8209. break;
  8210. }
  8211. }
  8212. return bonus;
  8213. }
  8214. int32 pc_skillheal2_bonus(map_session_data *sd, uint16 skill_id) {
  8215. int32 bonus = sd->bonus.add_heal2_rate;
  8216. skill_id = skill_dummy2skill_id(skill_id);
  8217. for (auto &it : sd->skillheal2) {
  8218. if (it.id == skill_id) {
  8219. bonus += it.val;
  8220. break;
  8221. }
  8222. }
  8223. return bonus;
  8224. }
  8225. void pc_respawn(map_session_data* sd, clr_type clrtype)
  8226. {
  8227. if( !pc_isdead(sd) )
  8228. return; // not applicable
  8229. if( sd->bg_id && bg_member_respawn(sd) )
  8230. return; // member revived by battleground
  8231. pc_setstand(sd, true);
  8232. pc_setrestartvalue(sd,3);
  8233. if( pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, clrtype ) != SETPOS_OK ){
  8234. clif_resurrection( sd->bl ); //If warping fails, send a normal stand up packet.
  8235. }
  8236. }
  8237. static TIMER_FUNC(pc_respawn_timer){
  8238. map_session_data *sd = map_id2sd(id);
  8239. if( sd != nullptr )
  8240. {
  8241. sd->pvp_point=0;
  8242. sd->respawn_tid = INVALID_TIMER;
  8243. pc_respawn(sd,CLR_OUTSIGHT);
  8244. }
  8245. return 0;
  8246. }
  8247. /*==========================================
  8248. * Invoked when a player has received damage
  8249. *------------------------------------------*/
  8250. void pc_damage(map_session_data *sd,struct block_list *src,uint32 hp, uint32 sp, uint32 ap)
  8251. {
  8252. if (ap) clif_updatestatus(*sd,SP_AP);
  8253. if (sp) clif_updatestatus(*sd,SP_SP);
  8254. if (hp) clif_updatestatus(*sd,SP_HP);
  8255. else return;
  8256. if (!src)
  8257. return;
  8258. if( pc_issit(sd) ) {
  8259. pc_setstand(sd, true);
  8260. skill_sit(sd,0);
  8261. }
  8262. if (sd->progressbar.npc_id)
  8263. clif_progressbar_abort(sd);
  8264. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  8265. pet_target_check(sd->pd,src,1);
  8266. if( sd->status.ele_id > 0 )
  8267. elemental_set_target(sd,src);
  8268. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  8269. sd->canlog_tick = gettick();
  8270. }
  8271. TIMER_FUNC(pc_close_npc_timer){
  8272. map_session_data* sd = map_id2sd( id );
  8273. if( sd != nullptr ){
  8274. pc_close_npc( sd, static_cast<int>( data ) );
  8275. }
  8276. return 0;
  8277. }
  8278. /**
  8279. * Method to properly close a NPC for player and clear anything related.
  8280. * @param sd: Player attached
  8281. * @param flag: Method of closure
  8282. * 1: Produce a close button and end the NPC
  8283. * 2: End the NPC (best for no dialog windows)
  8284. */
  8285. void pc_close_npc(map_session_data *sd,int32 flag)
  8286. {
  8287. nullpo_retv(sd);
  8288. if (sd->npc_id || sd->npc_shopid) {
  8289. if (sd->state.using_fake_npc) {
  8290. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, *sd );
  8291. sd->state.using_fake_npc = 0;
  8292. }
  8293. if (sd->st) {
  8294. if(sd->st->state == RUN){ //wait ending code execution
  8295. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  8296. return;
  8297. }
  8298. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  8299. sd->st->mes_active = 0;
  8300. }
  8301. sd->state.menu_or_input = 0;
  8302. sd->npc_menu = 0;
  8303. sd->npc_shopid = 0;
  8304. #ifdef SECURE_NPCTIMEOUT
  8305. if( sd->npc_idle_timer != INVALID_TIMER ){
  8306. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  8307. sd->npc_idle_timer = INVALID_TIMER;
  8308. }
  8309. #endif
  8310. if (sd->st) {
  8311. if (sd->st->state == CLOSE) {
  8312. clif_scriptclose( *sd, sd->npc_id );
  8313. clif_cutin( *sd, "", 255); // Force to end cutin [Haydrich]
  8314. sd->st->state = END; // Force to end now
  8315. }
  8316. if (sd->st->state == END) { // free attached scripts that are waiting
  8317. script_free_state(sd->st);
  8318. sd->st = nullptr;
  8319. sd->npc_id = 0;
  8320. }
  8321. }
  8322. }
  8323. }
  8324. /*==========================================
  8325. * Invoked when a player has negative current hp
  8326. *------------------------------------------*/
  8327. int32 pc_dead(map_session_data *sd,struct block_list *src)
  8328. {
  8329. int32 i=0,k=0;
  8330. t_tick tick = gettick();
  8331. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8332. // Activate Steel body if a super novice dies at 99+% exp [celest]
  8333. // Super Novices have no kill or die functions attached when saved by their angel
  8334. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  8335. t_exp exp = pc_nextbaseexp(sd);
  8336. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  8337. sd->state.snovice_dead_flag = 1;
  8338. pc_setrestartvalue(sd,1);
  8339. status_percent_heal(&sd->bl, 100, 100);
  8340. clif_resurrection( sd->bl );
  8341. if(battle_config.pc_invincible_time)
  8342. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8343. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  8344. if(mapdata_flag_gvg2(mapdata))
  8345. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  8346. return 0;
  8347. }
  8348. }
  8349. for(k = 0; k < MAX_DEVOTION; k++) {
  8350. if (sd->devotion[k]){
  8351. map_session_data *devsd = map_id2sd(sd->devotion[k]);
  8352. if (devsd)
  8353. status_change_end(&devsd->bl, SC_DEVOTION);
  8354. sd->devotion[k] = 0;
  8355. }
  8356. }
  8357. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  8358. if (sd->stellar_mark[k]) {
  8359. map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  8360. if (smarksd)
  8361. status_change_end(&smarksd->bl, SC_FLASHKICK);
  8362. sd->stellar_mark[k] = 0;
  8363. }
  8364. }
  8365. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  8366. if (sd->united_soul[k]) {
  8367. map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  8368. if (usoulsd)
  8369. status_change_end(&usoulsd->bl, SC_SOULUNITY);
  8370. sd->united_soul[k] = 0;
  8371. }
  8372. }
  8373. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  8374. if (sd->servant_sign[k]) {
  8375. map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  8376. if (ssignsd)
  8377. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN);
  8378. sd->servant_sign[k] = 0;
  8379. }
  8380. }
  8381. if(sd->shadowform_id) { //if we were target of shadowform
  8382. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM);
  8383. sd->shadowform_id = 0; //should be remove on status end anyway
  8384. }
  8385. if(sd->status.pet_id > 0 && sd->pd) {
  8386. struct pet_data *pd = sd->pd;
  8387. if( !mapdata->getMapFlag(MF_NOEXPPENALTY) ) {
  8388. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  8389. clif_send_petdata( sd, *sd->pd, CHANGESTATEPET_INTIMACY );
  8390. }
  8391. if( sd->pd->target_id ) // Unlock all targets...
  8392. pet_unlocktarget(sd->pd);
  8393. }
  8394. if (hom_is_active(sd->hd) && battle_config.homunculus_auto_vapor && get_percentage(sd->hd->battle_status.hp, sd->hd->battle_status.max_hp) >= battle_config.homunculus_auto_vapor)
  8395. hom_vaporize(sd, HOM_ST_ACTIVE);
  8396. if( sd->md )
  8397. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  8398. if( sd->ed )
  8399. elemental_delete(sd->ed);
  8400. // Leave duel if you die [LuzZza]
  8401. if(battle_config.duel_autoleave_when_die) {
  8402. if(sd->duel_group > 0)
  8403. duel_leave(sd->duel_group, sd);
  8404. if(sd->duel_invite > 0)
  8405. duel_reject(sd->duel_invite, sd);
  8406. }
  8407. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  8408. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  8409. sd->skill_keep_using.tid = INVALID_TIMER;
  8410. }
  8411. pc_close_npc(sd,2); //close npc if we were using one
  8412. /* e.g. not killed thru pc_damage */
  8413. if( pc_issit(sd) ) {
  8414. clif_status_load(&sd->bl,EFST_SIT,0);
  8415. }
  8416. pc_setdead(sd);
  8417. clif_party_dead( *sd );
  8418. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  8419. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  8420. if (battle_config.loose_ap_on_death == 1)
  8421. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  8422. //Reset menu skills/item skills
  8423. if ((sd->skillitem) != 0)
  8424. sd->skillitem = sd->skillitemlv = 0;
  8425. if ((sd->menuskill_id) != 0)
  8426. sd->menuskill_id = sd->menuskill_val = 0;
  8427. //Reset ticks.
  8428. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  8429. if ( sd->spiritball !=0 )
  8430. pc_delspiritball(sd,sd->spiritball,0);
  8431. if (sd->soulball != 0)
  8432. pc_delsoulball( *sd, sd->soulball );
  8433. if (sd->servantball != 0)
  8434. pc_delservantball( *sd, sd->servantball );
  8435. if (sd->abyssball != 0)
  8436. pc_delabyssball( *sd, sd->abyssball );
  8437. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  8438. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  8439. if (src)
  8440. switch (src->type) {
  8441. case BL_MOB:
  8442. {
  8443. struct mob_data *md=(struct mob_data *)src;
  8444. if(md->target_id==sd->bl.id)
  8445. mob_unlocktarget(md,tick);
  8446. if(battle_config.mobs_level_up && md->status.hp &&
  8447. (uint32)md->level < pc_maxbaselv(sd) &&
  8448. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  8449. ) { // monster level up [Valaris]
  8450. clif_misceffect( md->bl, NOTIFYEFFECT_BASE_LEVEL_UP );
  8451. md->level++;
  8452. status_calc_mob(md, SCO_NONE);
  8453. status_percent_heal(src,10,0);
  8454. if( battle_config.show_mob_info&4 )
  8455. {// update name with new level
  8456. clif_name_area(&md->bl);
  8457. }
  8458. }
  8459. src = battle_get_master(src); // Maybe Player Summon
  8460. }
  8461. break;
  8462. case BL_PET: //Pass on to master...
  8463. case BL_HOM:
  8464. case BL_MER:
  8465. src = battle_get_master(src);
  8466. break;
  8467. }
  8468. if (src && src->type == BL_PC) {
  8469. map_session_data *ssd = (map_session_data *)src;
  8470. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8471. npc_script_event( *ssd, NPCE_KILLPC );
  8472. if (battle_config.pk_mode&2) {
  8473. ssd->status.manner -= 5;
  8474. if(ssd->status.manner < 0)
  8475. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8476. #if 0
  8477. // PK/Karma system code (not enabled yet) [celest]
  8478. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8479. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8480. // karma going down = more 'good' / more honourable.
  8481. // The Karma System way...
  8482. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8483. sd->status.karma--;
  8484. ssd->status.karma--;
  8485. }
  8486. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8487. ssd->status.karma++;
  8488. // or the PK System way...
  8489. if (sd->status.karma > 0) // player killed is dishonourable?
  8490. ssd->status.karma--; // honour points earned
  8491. sd->status.karma++; // honour points lost
  8492. // To-do: Receive exp on certain occasions
  8493. #endif
  8494. }
  8495. }
  8496. if(battle_config.bone_drop==2
  8497. || (battle_config.bone_drop==1 && mapdata->getMapFlag(MF_PVP)))
  8498. {
  8499. struct item item_tmp;
  8500. memset(&item_tmp,0,sizeof(item_tmp));
  8501. item_tmp.nameid=ITEMID_SKULL_;
  8502. item_tmp.identify=1;
  8503. item_tmp.card[0]=CARD0_CREATE;
  8504. item_tmp.card[1]=0;
  8505. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8506. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8507. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,4,0);
  8508. }
  8509. //Remove bonus_script when dead
  8510. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8511. // changed penalty options, added death by player if pk_mode [Valaris]
  8512. if(battle_config.death_penalty_type
  8513. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8514. && !sd->sc.cant.deathpenalty
  8515. && !mapdata->getMapFlag(MF_NOEXPPENALTY) && !mapdata_flag_gvg2(mapdata))
  8516. {
  8517. t_exp base_penalty = 0;
  8518. t_exp job_penalty = 0;
  8519. uint32 zeny_penalty = 0;
  8520. if (pc_isvip(sd)) { // EXP penalty for VIP
  8521. base_penalty = battle_config.vip_exp_penalty_base;
  8522. job_penalty = battle_config.vip_exp_penalty_job;
  8523. zeny_penalty = battle_config.vip_zeny_penalty;
  8524. } else {
  8525. base_penalty = battle_config.death_penalty_base;
  8526. job_penalty = battle_config.death_penalty_job;
  8527. zeny_penalty = battle_config.zeny_penalty;
  8528. }
  8529. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8530. switch (battle_config.death_penalty_type) {
  8531. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8532. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8533. }
  8534. if (base_penalty){ //recheck after altering to speedup
  8535. if (battle_config.pk_mode && src && src->type==BL_PC)
  8536. base_penalty *= 2;
  8537. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8538. }
  8539. }
  8540. else
  8541. base_penalty = 0;
  8542. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8543. switch (battle_config.death_penalty_type) {
  8544. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8545. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8546. }
  8547. if (job_penalty) {
  8548. if (battle_config.pk_mode && src && src->type==BL_PC)
  8549. job_penalty *= 2;
  8550. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8551. }
  8552. }
  8553. else
  8554. job_penalty = 0;
  8555. if (base_penalty || job_penalty) {
  8556. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8557. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8558. pc_lostexp(sd, base_penalty, job_penalty);
  8559. }
  8560. if( zeny_penalty > 0 && !mapdata->getMapFlag(MF_NOZENYPENALTY)) {
  8561. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8562. if(zeny_penalty)
  8563. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER);
  8564. }
  8565. }
  8566. if( mapdata->getMapFlag(MF_PVP_NIGHTMAREDROP) ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8567. for (const auto &it : mapdata->drop_list) {
  8568. int32 id = it.drop_id, per = it.drop_per;
  8569. enum e_nightmare_drop_type type = it.drop_type;
  8570. if(id == 0)
  8571. continue;
  8572. if(id == -1){
  8573. int32 eq_num=0,eq_n[MAX_INVENTORY];
  8574. memset(eq_n,0,sizeof(eq_n));
  8575. for(i=0;i<MAX_INVENTORY;i++) {
  8576. if( sd->inventory.u.items_inventory[i].nameid == 0 ){
  8577. continue;
  8578. }
  8579. if( !pc_candrop( sd, &sd->inventory.u.items_inventory[i] ) ){
  8580. continue;
  8581. }
  8582. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8583. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8584. || type == NMDT_ALL)
  8585. {
  8586. eq_n[eq_num++] = i;
  8587. }
  8588. }
  8589. if(eq_num > 0){
  8590. if(rnd()%10000 < per) {
  8591. int32 n = eq_n[rnd() % eq_num];
  8592. if(sd->inventory.u.items_inventory[n].equip)
  8593. pc_unequipitem(sd,n,3);
  8594. pc_dropitem(sd,n,1);
  8595. }
  8596. }
  8597. }
  8598. else if(id > 0) {
  8599. for(i=0;i<MAX_INVENTORY;i++){
  8600. if(sd->inventory.u.items_inventory[i].nameid == id
  8601. && pc_candrop( sd, &sd->inventory.u.items_inventory[i] )
  8602. && rnd()%10000 < per
  8603. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8604. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8605. || type == NMDT_ALL) ){
  8606. if(sd->inventory.u.items_inventory[i].equip)
  8607. pc_unequipitem(sd,i,3);
  8608. pc_dropitem(sd,i,1);
  8609. break;
  8610. }
  8611. }
  8612. }
  8613. }
  8614. }
  8615. // pvp
  8616. // disable certain pvp functions on pk_mode [Valaris]
  8617. if( !battle_config.pk_mode && mapdata->getMapFlag(MF_PVP) && !mapdata->getMapFlag(MF_PVP_NOCALCRANK) ) {
  8618. sd->pvp_point -= 5;
  8619. sd->pvp_lost++;
  8620. if( src && src->type == BL_PC ) {
  8621. map_session_data *ssd = (map_session_data *)src;
  8622. ssd->pvp_point++;
  8623. ssd->pvp_won++;
  8624. }
  8625. if( sd->pvp_point < 0 ) {
  8626. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8627. return 1|8;
  8628. }
  8629. }
  8630. //GvG
  8631. if( mapdata_flag_gvg2(mapdata) ) {
  8632. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8633. return 1|8;
  8634. }
  8635. else if( sd->bg_id ) {
  8636. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8637. if (bg) {
  8638. if (bg->cemetery.map > 0) { // Respawn by BG
  8639. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8640. return 1|8;
  8641. }
  8642. }
  8643. }
  8644. //Reset "can log out" tick.
  8645. if( battle_config.prevent_logout )
  8646. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8647. return 1;
  8648. }
  8649. void pc_revive(map_session_data *sd,uint32 hp, uint32 sp, uint32 ap) {
  8650. if(hp) clif_updatestatus(*sd,SP_HP);
  8651. if(sp) clif_updatestatus(*sd,SP_SP);
  8652. if(ap) clif_updatestatus(*sd,SP_AP);
  8653. pc_setstand(sd, true);
  8654. if(battle_config.pc_invincible_time > 0)
  8655. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8656. if (sd->state.gmaster_flag && sd->guild) {
  8657. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  8658. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  8659. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  8660. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  8661. }
  8662. }
  8663. bool pc_revive_item(map_session_data *sd) {
  8664. nullpo_retr(false, sd);
  8665. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8666. return false;
  8667. if (sd->sc.getSCE(SC_HELLPOWER)) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8668. return false;
  8669. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8670. uint8 hp = 100, sp = 100;
  8671. if (item_position < 0) {
  8672. if (sd->sc.getSCE(SC_LIGHT_OF_REGENE)) {
  8673. hp = sd->sc.getSCE(SC_LIGHT_OF_REGENE)->val2;
  8674. sp = 0;
  8675. }
  8676. else
  8677. return false;
  8678. }
  8679. if (!status_revive(&sd->bl, hp, sp))
  8680. return false;
  8681. if (item_position < 0)
  8682. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE);
  8683. else
  8684. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8685. clif_skill_nodamage(&sd->bl, sd->bl, ALL_RESURRECTION, 4);
  8686. return true;
  8687. }
  8688. // script
  8689. //
  8690. /*==========================================
  8691. * script reading pc status registry
  8692. *------------------------------------------*/
  8693. int64 pc_readparam(map_session_data* sd,int64 type)
  8694. {
  8695. int64 val = 0;
  8696. nullpo_ret(sd);
  8697. switch(type) {
  8698. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8699. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8700. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8701. case SP_ZENY: val = sd->status.zeny; break;
  8702. case SP_BASELEVEL: val = sd->status.base_level; break;
  8703. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8704. case SP_CLASS: val = sd->status.class_; break;
  8705. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8706. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8707. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8708. case SP_SEX: val = sd->status.sex; break;
  8709. case SP_WEIGHT: val = sd->weight; break;
  8710. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8711. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8712. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8713. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8714. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8715. case SP_HP: val = sd->battle_status.hp; break;
  8716. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8717. case SP_SP: val = sd->battle_status.sp; break;
  8718. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8719. case SP_AP: val = sd->battle_status.ap; break;
  8720. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8721. case SP_STR: val = sd->status.str; break;
  8722. case SP_AGI: val = sd->status.agi; break;
  8723. case SP_VIT: val = sd->status.vit; break;
  8724. case SP_INT: val = sd->status.int_; break;
  8725. case SP_DEX: val = sd->status.dex; break;
  8726. case SP_LUK: val = sd->status.luk; break;
  8727. case SP_POW: val = sd->status.pow; break;
  8728. case SP_STA: val = sd->status.sta; break;
  8729. case SP_WIS: val = sd->status.wis; break;
  8730. case SP_SPL: val = sd->status.spl; break;
  8731. case SP_CON: val = sd->status.con; break;
  8732. case SP_CRT: val = sd->status.crt; break;
  8733. case SP_KARMA: val = sd->status.karma; break;
  8734. case SP_MANNER: val = sd->status.manner; break;
  8735. case SP_FAME: val = sd->status.fame; break;
  8736. case SP_KILLERRID: val = sd->killerrid; break;
  8737. case SP_KILLEDRID: val = sd->killedrid; break;
  8738. case SP_KILLEDGID: val = sd->killedgid; break;
  8739. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8740. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8741. case SP_CHARRENAME: val = sd->status.rename; break;
  8742. case SP_CHARFONT: val = sd->status.font; break;
  8743. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8744. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8745. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8746. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8747. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8748. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8749. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8750. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8751. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8752. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8753. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  8754. case SP_BASE_ATK:
  8755. #ifdef RENEWAL
  8756. val = sd->bonus.eatk;
  8757. #else
  8758. val = sd->battle_status.batk;
  8759. #endif
  8760. break;
  8761. case SP_DEF1: val = sd->battle_status.def; break;
  8762. case SP_DEF2: val = sd->battle_status.def2; break;
  8763. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8764. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8765. case SP_HIT: val = sd->battle_status.hit; break;
  8766. case SP_FLEE1: val = sd->battle_status.flee; break;
  8767. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8768. case SP_PATK: val = sd->battle_status.patk; break;
  8769. case SP_SMATK: val = sd->battle_status.smatk; break;
  8770. case SP_RES: val = sd->battle_status.res; break;
  8771. case SP_MRES: val = sd->battle_status.mres; break;
  8772. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8773. case SP_CRATE: val = sd->battle_status.crate; break;
  8774. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8775. case SP_MAXHPRATE: val = sd->hprate; break;
  8776. case SP_MAXSPRATE: val = sd->sprate; break;
  8777. case SP_MAXAPRATE: val = sd->aprate; break;
  8778. case SP_SPRATE: val = sd->dsprate; break;
  8779. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8780. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8781. case SP_ASPD_RATE:
  8782. #ifndef RENEWAL_ASPD
  8783. val = sd->battle_status.aspd_rate;
  8784. #else
  8785. val = sd->battle_status.aspd_rate2;
  8786. #endif
  8787. break;
  8788. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8789. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8790. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8791. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8792. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8793. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8794. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8795. case SP_MATK_RATE: val = sd->matk_rate; break;
  8796. case SP_ATK_RATE:
  8797. #ifdef RENEWAL
  8798. val = sd->bonus.atk_rate;
  8799. #else
  8800. ShowError( "pc_readparam: %s is not supported in Pre-Renewal mode.\n", QUOTE( SP_ATK_RATE ) );
  8801. #endif
  8802. break;
  8803. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8804. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8805. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8806. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8807. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8808. case SP_HIT_RATE: val = sd->hit_rate; break;
  8809. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8810. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8811. case SP_DEF_RATE: val = sd->def_rate; break;
  8812. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8813. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8814. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8815. case SP_PATK_RATE: val = sd->patk_rate; break;
  8816. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8817. case SP_RES_RATE: val = sd->res_rate; break;
  8818. case SP_MRES_RATE: val = sd->mres_rate; break;
  8819. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8820. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8821. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8822. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8823. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8824. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8825. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8826. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8827. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8828. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8829. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8830. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8831. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8832. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8833. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8834. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8835. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8836. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8837. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8838. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8839. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8840. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8841. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8842. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8843. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8844. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8845. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8846. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8847. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8848. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8849. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8850. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8851. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8852. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8853. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8854. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8855. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8856. case SP_UNSTRIPABLE:
  8857. case SP_UNSTRIPABLE_ARMOR:
  8858. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8859. break;
  8860. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8861. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8862. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8863. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8864. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8865. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8866. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8867. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8868. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8869. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8870. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8871. case SP_EMATK: val = sd->bonus.ematk; break;
  8872. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8873. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8874. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8875. case SP_CASTRATE:
  8876. case SP_VARCASTRATE:
  8877. #ifdef RENEWAL_CAST
  8878. val = sd->bonus.varcastrate; break;
  8879. #else
  8880. val = sd->castrate; break;
  8881. #endif
  8882. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8883. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8884. default:
  8885. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8886. return -1;
  8887. }
  8888. return val;
  8889. }
  8890. /*==========================================
  8891. * script set pc status registry
  8892. *------------------------------------------*/
  8893. bool pc_setparam(map_session_data *sd,int64 type,int64 val_tmp)
  8894. {
  8895. nullpo_retr(false,sd);
  8896. int32 val = static_cast<uint32>(val_tmp);
  8897. switch(type){
  8898. case SP_BASELEVEL:
  8899. if (val > pc_maxbaselv(sd)) //Capping to max
  8900. val = pc_maxbaselv(sd);
  8901. if (val > sd->status.base_level) {
  8902. for( int32 i = 0; i < (int32)( val - sd->status.base_level ); i++ ){
  8903. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8904. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8905. }
  8906. }
  8907. sd->status.base_level = val;
  8908. sd->status.base_exp = 0;
  8909. // clif_updatestatus(*sd, SP_BASELEVEL); // Gets updated at the bottom
  8910. clif_updatestatus(*sd, SP_NEXTBASEEXP);
  8911. clif_updatestatus(*sd, SP_STATUSPOINT);
  8912. clif_updatestatus(*sd, SP_TRAITPOINT);
  8913. clif_updatestatus(*sd, SP_BASEEXP);
  8914. status_calc_pc(sd, SCO_FORCE);
  8915. if(sd->status.party_id)
  8916. party_send_levelup(sd);
  8917. break;
  8918. case SP_JOBLEVEL:
  8919. if (val >= sd->status.job_level) {
  8920. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8921. sd->status.skill_point += val - sd->status.job_level;
  8922. clif_updatestatus(*sd, SP_SKILLPOINT);
  8923. }
  8924. sd->status.job_level = val;
  8925. sd->status.job_exp = 0;
  8926. // clif_updatestatus(*sd, SP_JOBLEVEL); // Gets updated at the bottom
  8927. clif_updatestatus(*sd, SP_NEXTJOBEXP);
  8928. clif_updatestatus(*sd, SP_JOBEXP);
  8929. status_calc_pc(sd, SCO_FORCE);
  8930. break;
  8931. case SP_SKILLPOINT:
  8932. sd->status.skill_point = val;
  8933. break;
  8934. case SP_STATUSPOINT:
  8935. sd->status.status_point = val;
  8936. break;
  8937. case SP_TRAITPOINT:
  8938. sd->status.trait_point = val;
  8939. break;
  8940. case SP_ZENY:
  8941. if( val < 0 )
  8942. return false;// can't set negative zeny
  8943. log_zeny(*sd, LOG_TYPE_SCRIPT, sd->status.char_id, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8944. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8945. break;
  8946. case SP_BASEEXP:
  8947. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8948. if (val_tmp < sd->status.base_exp) // Lost
  8949. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8950. else // Gained
  8951. pc_gainexp(sd, nullptr, val_tmp - sd->status.base_exp, 0, 2);
  8952. return true;
  8953. case SP_JOBEXP:
  8954. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8955. if (val_tmp < sd->status.job_exp) // Lost
  8956. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8957. else // Gained
  8958. pc_gainexp(sd, nullptr, 0, val_tmp - sd->status.job_exp, 2);
  8959. return true;
  8960. case SP_SEX:
  8961. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8962. break;
  8963. case SP_WEIGHT:
  8964. sd->weight = val;
  8965. break;
  8966. case SP_MAXWEIGHT:
  8967. sd->max_weight = val;
  8968. break;
  8969. case SP_HP:
  8970. sd->battle_status.hp = cap_value(val, 1, (int32)sd->battle_status.max_hp);
  8971. break;
  8972. case SP_MAXHP:
  8973. if (sd->status.base_level < 100)
  8974. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8975. else if (sd->status.base_level < 151)
  8976. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8977. else
  8978. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8979. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8980. {
  8981. sd->battle_status.hp = sd->battle_status.max_hp;
  8982. clif_updatestatus(*sd, SP_HP);
  8983. }
  8984. break;
  8985. case SP_SP:
  8986. sd->battle_status.sp = cap_value(val, 0, (int32)sd->battle_status.max_sp);
  8987. break;
  8988. case SP_MAXSP:
  8989. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8990. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8991. {
  8992. sd->battle_status.sp = sd->battle_status.max_sp;
  8993. clif_updatestatus(*sd, SP_SP);
  8994. }
  8995. break;
  8996. case SP_AP:
  8997. sd->battle_status.ap = cap_value(val, 0, (int32)sd->battle_status.max_ap);
  8998. break;
  8999. case SP_MAXAP:
  9000. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  9001. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  9002. sd->battle_status.ap = sd->battle_status.max_ap;
  9003. clif_updatestatus(*sd, SP_AP);
  9004. }
  9005. break;
  9006. case SP_STR:
  9007. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  9008. break;
  9009. case SP_AGI:
  9010. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  9011. break;
  9012. case SP_VIT:
  9013. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  9014. break;
  9015. case SP_INT:
  9016. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  9017. break;
  9018. case SP_DEX:
  9019. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  9020. break;
  9021. case SP_LUK:
  9022. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  9023. break;
  9024. case SP_POW:
  9025. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  9026. break;
  9027. case SP_STA:
  9028. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  9029. break;
  9030. case SP_WIS:
  9031. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  9032. break;
  9033. case SP_SPL:
  9034. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  9035. break;
  9036. case SP_CON:
  9037. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  9038. break;
  9039. case SP_CRT:
  9040. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  9041. break;
  9042. case SP_KARMA:
  9043. sd->status.karma = val;
  9044. break;
  9045. case SP_MANNER:
  9046. sd->status.manner = val;
  9047. if( val < 0 )
  9048. sc_start(nullptr, &sd->bl, SC_NOCHAT, 100, 0, 0);
  9049. else {
  9050. status_change_end(&sd->bl, SC_NOCHAT);
  9051. clif_manner_message(sd, 5);
  9052. }
  9053. return true; // status_change_start/status_change_end already sends packets warning the client
  9054. case SP_FAME:
  9055. sd->status.fame = val;
  9056. break;
  9057. case SP_KILLERRID:
  9058. sd->killerrid = val;
  9059. return true;
  9060. case SP_KILLEDRID:
  9061. sd->killedrid = val;
  9062. return true;
  9063. case SP_KILLEDGID:
  9064. sd->killedgid = val;
  9065. return true;
  9066. case SP_CHARMOVE:
  9067. sd->status.character_moves = val;
  9068. return true;
  9069. case SP_CHARRENAME:
  9070. sd->status.rename = val;
  9071. return true;
  9072. case SP_CHARFONT:
  9073. sd->status.font = val;
  9074. clif_font(sd);
  9075. return true;
  9076. case SP_BANK_VAULT:
  9077. if (val < 0)
  9078. return false;
  9079. log_zeny(*sd, LOG_TYPE_BANK, sd->status.char_id, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  9080. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  9081. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  9082. return true;
  9083. case SP_ROULETTE_BRONZE:
  9084. sd->roulette_point.bronze = val;
  9085. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  9086. return true;
  9087. case SP_ROULETTE_SILVER:
  9088. sd->roulette_point.silver = val;
  9089. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  9090. return true;
  9091. case SP_ROULETTE_GOLD:
  9092. sd->roulette_point.gold = val;
  9093. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  9094. return true;
  9095. case SP_CASHPOINTS:
  9096. if (val < 0)
  9097. return false;
  9098. if (!sd->state.connect_new)
  9099. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_CASHPOINT)));
  9100. sd->cashPoints = cap_value(val, 0, MAX_CASHPOINT);
  9101. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  9102. return true;
  9103. case SP_KAFRAPOINTS:
  9104. if (val < 0)
  9105. return false;
  9106. if (!sd->state.connect_new)
  9107. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_KAFRAPOINT)));
  9108. sd->kafraPoints = cap_value(val, 0, MAX_KAFRAPOINT);
  9109. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  9110. return true;
  9111. case SP_PCDIECOUNTER:
  9112. if (val < 0)
  9113. return false;
  9114. if (sd->die_counter == val)
  9115. return true;
  9116. sd->die_counter = val;
  9117. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  9118. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  9119. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  9120. return true;
  9121. case SP_COOKMASTERY:
  9122. if (sd->cook_mastery == val)
  9123. return true;
  9124. val = cap_value(val, 0, 1999);
  9125. sd->cook_mastery = val;
  9126. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  9127. return true;
  9128. default:
  9129. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  9130. return false;
  9131. }
  9132. clif_updatestatus(*sd,static_cast<_sp>(type));
  9133. return true;
  9134. }
  9135. /*==========================================
  9136. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  9137. *------------------------------------------*/
  9138. void pc_heal(map_session_data *sd,uint32 hp,uint32 sp, uint32 ap, int32 type)
  9139. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  9140. nullpo_retv(sd);
  9141. if (type&2) {
  9142. if (hp || type&4) {
  9143. clif_heal( *sd, SP_HP, hp );
  9144. clif_update_hp(*sd);
  9145. }
  9146. if (sp)
  9147. clif_heal( *sd, SP_SP, sp );
  9148. if (ap)
  9149. clif_heal( *sd, SP_AP, ap );
  9150. } else {
  9151. if(hp)
  9152. clif_updatestatus(*sd,SP_HP);
  9153. if(sp)
  9154. clif_updatestatus(*sd,SP_SP);
  9155. if (ap)
  9156. clif_updatestatus(*sd,SP_AP);
  9157. }
  9158. return;
  9159. }
  9160. /**
  9161. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  9162. * @param sd: Player data
  9163. * @param itemid: Item ID
  9164. * @param hp: HP to heal
  9165. * @param sp: SP to heal
  9166. * @return Amount healed to an object
  9167. */
  9168. int32 pc_itemheal(map_session_data *sd, t_itemid itemid, int32 hp, int32 sp)
  9169. {
  9170. int32 bonus, tmp, penalty = 0;
  9171. if (hp) {
  9172. bonus = 100 + (sd->battle_status.vit * 2) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9173. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9174. if (potion_flag == 2) {
  9175. bonus += bonus * 50 / 100;
  9176. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_ROGUE)
  9177. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  9178. }
  9179. //All item bonuses.
  9180. bonus += sd->bonus.itemhealrate2;
  9181. //Item Group bonuses
  9182. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  9183. //Individual item bonuses.
  9184. for(const auto &it : sd->itemhealrate) {
  9185. if (it.id == itemid) {
  9186. bonus += bonus * it.val / 100;
  9187. break;
  9188. }
  9189. }
  9190. // Recovery Potion
  9191. if (sd->sc.getSCE(SC_INCHEALRATE))
  9192. bonus += bonus * sd->sc.getSCE(SC_INCHEALRATE)->val1 / 100;
  9193. tmp = hp * bonus / 100; // Overflow check
  9194. if (bonus != 100 && tmp > hp)
  9195. hp = tmp;
  9196. }
  9197. if (sp) {
  9198. bonus = 100 + (sd->battle_status.int_ * 2) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9199. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9200. if (potion_flag == 2)
  9201. bonus += bonus * 50 / 100;
  9202. // All item bonuses.
  9203. bonus += sd->bonus.itemsphealrate2;
  9204. // Item Group bonuses
  9205. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  9206. // Individual item bonuses.
  9207. for( const auto &it : sd->itemsphealrate ){
  9208. if( it.id == itemid ){
  9209. bonus += bonus * it.val / 100;
  9210. break;
  9211. }
  9212. }
  9213. tmp = sp * bonus / 100; // Overflow check
  9214. if (bonus != 100 && tmp > sp)
  9215. sp = tmp;
  9216. }
  9217. if (!sd->sc.empty()) {
  9218. // Critical Wound and Death Hurt stack
  9219. if (sd->sc.getSCE(SC_CRITICALWOUND))
  9220. penalty += sd->sc.getSCE(SC_CRITICALWOUND)->val2;
  9221. if (sd->sc.getSCE(SC_DEATHHURT) && sd->sc.getSCE(SC_DEATHHURT)->val3 == 1)
  9222. penalty += 20;
  9223. if (sd->sc.getSCE(SC_NORECOVER_STATE))
  9224. penalty = 100;
  9225. if (sd->sc.getSCE(SC_VITALITYACTIVATION))
  9226. hp += hp / 2; // 1.5 times
  9227. if (sd->sc.getSCE(SC_WATER_INSIGNIA) && sd->sc.getSCE(SC_WATER_INSIGNIA)->val1 == 2) {
  9228. hp += hp / 10;
  9229. sp += sp / 10;
  9230. }
  9231. #ifdef RENEWAL
  9232. if (sd->sc.getSCE(SC_APPLEIDUN))
  9233. hp += sd->sc.getSCE(SC_APPLEIDUN)->val3 / 100;
  9234. #endif
  9235. if (penalty > 0) {
  9236. hp -= hp * penalty / 100;
  9237. sp -= sp * penalty / 100;
  9238. }
  9239. #ifdef RENEWAL
  9240. if (sd->sc.getSCE(SC_EXTREMITYFIST))
  9241. sp = 0;
  9242. #endif
  9243. if (sd->sc.getSCE(SC_BITESCAR))
  9244. hp = 0;
  9245. }
  9246. return status_heal(&sd->bl, hp, sp, 1);
  9247. }
  9248. /*==========================================
  9249. * HP/SP Recovery
  9250. * Heal player hp nad/or sp by rate
  9251. *------------------------------------------*/
  9252. int32 pc_percentheal(map_session_data *sd,int32 hp,int32 sp)
  9253. {
  9254. nullpo_ret(sd);
  9255. if (hp > 100) hp = 100;
  9256. else if (hp <-100) hp = -100;
  9257. if (sp > 100) sp = 100;
  9258. else if (sp <-100) sp = -100;
  9259. if(hp >= 0 && sp >= 0) //Heal
  9260. return status_percent_heal(&sd->bl, hp, sp);
  9261. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  9262. return status_percent_damage(nullptr, &sd->bl, hp, sp, hp==-100);
  9263. //Crossed signs
  9264. if(hp) {
  9265. if(hp > 0)
  9266. status_percent_heal(&sd->bl, hp, 0);
  9267. else
  9268. status_percent_damage(nullptr, &sd->bl, hp, 0, hp==-100);
  9269. }
  9270. if(sp) {
  9271. if(sp > 0)
  9272. status_percent_heal(&sd->bl, 0, sp);
  9273. else
  9274. status_percent_damage(nullptr, &sd->bl, 0, sp, false);
  9275. }
  9276. return 0;
  9277. }
  9278. static int32 jobchange_killclone(struct block_list *bl, va_list ap)
  9279. {
  9280. struct mob_data *md;
  9281. int32 flag;
  9282. md = (struct mob_data *)bl;
  9283. nullpo_ret(md);
  9284. flag = va_arg(ap, int32);
  9285. if (md->master_id && md->special_state.clone && md->master_id == flag)
  9286. status_kill(&md->bl);
  9287. return 1;
  9288. }
  9289. /**
  9290. * Called when player changes job
  9291. * Rewrote to make it tidider [Celest]
  9292. * @param sd
  9293. * @param job JOB ID. See enum e_job
  9294. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  9295. * @return True if success, false if failed
  9296. **/
  9297. bool pc_jobchange(map_session_data *sd,int32 job, char upper)
  9298. {
  9299. int32 i, fame_flag = 0;
  9300. nullpo_retr(false,sd);
  9301. if (job < 0)
  9302. return false;
  9303. //Normalize job.
  9304. uint64 b_class = pc_jobid2mapid(job);
  9305. if (b_class == -1)
  9306. return false;
  9307. switch (upper) {
  9308. case 1:
  9309. b_class|= JOBL_UPPER;
  9310. break;
  9311. case 2:
  9312. b_class|= JOBL_BABY;
  9313. break;
  9314. }
  9315. //This will automatically adjust bard/dancer classes to the correct gender
  9316. //That is, if you try to jobchange into dancer, it will turn you to bard.
  9317. job = pc_mapid2jobid(b_class, sd->status.sex);
  9318. if (job == -1)
  9319. return false;
  9320. if ((uint16)b_class == sd->class_)
  9321. return false; //Nothing to change.
  9322. // If the job does not exist in the job db, dont allow changing to it
  9323. if( !job_db.exists( job ) ){
  9324. return false;
  9325. }
  9326. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  9327. // Changing to 4th job
  9328. sd->change_level_4th = sd->status.job_level;
  9329. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  9330. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  9331. // changing from 2nd to 3rd job
  9332. sd->change_level_3rd = sd->status.job_level;
  9333. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  9334. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  9335. // changing from 1st to 2nd job
  9336. sd->change_level_2nd = sd->status.job_level;
  9337. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  9338. }
  9339. if(sd->cloneskill_idx > 0) {
  9340. pc_skill_plagiarism_reset(*sd, 1);
  9341. }
  9342. if(sd->reproduceskill_idx > 0) {
  9343. pc_skill_plagiarism_reset(*sd, 2);
  9344. }
  9345. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  9346. status_db.changeSkillTree(sd);
  9347. }
  9348. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  9349. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  9350. pc_resetfeel(sd);
  9351. pc_resethate(sd);
  9352. }
  9353. // Reset body style to 0 before changing job to avoid
  9354. // errors since not every job has a alternate outfit.
  9355. sd->status.body = 0;
  9356. clif_changelook(&sd->bl,LOOK_BODY2,0);
  9357. sd->status.class_ = job;
  9358. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  9359. uint64 previous_class = sd->class_;
  9360. sd->class_ = (uint16)b_class;
  9361. sd->status.job_level=1;
  9362. sd->status.job_exp=0;
  9363. if (sd->status.base_level > pc_maxbaselv(sd)) {
  9364. sd->status.base_level = pc_maxbaselv(sd);
  9365. sd->status.base_exp=0;
  9366. pc_resetstate(sd);
  9367. clif_updatestatus(*sd,SP_STATUSPOINT);
  9368. clif_updatestatus(*sd,SP_TRAITPOINT);
  9369. clif_updatestatus(*sd,SP_BASELEVEL);
  9370. clif_updatestatus(*sd,SP_BASEEXP);
  9371. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  9372. }
  9373. // Give or reduce transcendent status points
  9374. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  9375. sd->status.status_point += battle_config.transcendent_status_points;
  9376. clif_updatestatus(*sd,SP_STATUSPOINT);
  9377. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  9378. if( sd->status.status_point < battle_config.transcendent_status_points ){
  9379. // The player already used his bonus points, so we have to reset his status points
  9380. pc_resetstate(sd);
  9381. }else{
  9382. sd->status.status_point -= battle_config.transcendent_status_points;
  9383. clif_updatestatus(*sd,SP_STATUSPOINT);
  9384. }
  9385. }
  9386. // Give or reduce trait status points
  9387. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  9388. sd->status.trait_point += battle_config.trait_points_job_change;
  9389. clif_updatestatus(*sd, SP_TRAITPOINT);
  9390. clif_updatestatus(*sd, SP_UPOW);
  9391. clif_updatestatus(*sd, SP_USTA);
  9392. clif_updatestatus(*sd, SP_UWIS);
  9393. clif_updatestatus(*sd, SP_USPL);
  9394. clif_updatestatus(*sd, SP_UCON);
  9395. clif_updatestatus(*sd, SP_UCRT);
  9396. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  9397. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  9398. // Player may have already used the trait status points. Force a reset.
  9399. pc_resetstate(sd);
  9400. } else {
  9401. sd->status.trait_point = 0;
  9402. clif_updatestatus(*sd, SP_TRAITPOINT);
  9403. clif_updatestatus(*sd, SP_UPOW);
  9404. clif_updatestatus(*sd, SP_USTA);
  9405. clif_updatestatus(*sd, SP_UWIS);
  9406. clif_updatestatus(*sd, SP_USPL);
  9407. clif_updatestatus(*sd, SP_UCON);
  9408. clif_updatestatus(*sd, SP_UCRT);
  9409. }
  9410. }
  9411. clif_updatestatus(*sd,SP_JOBLEVEL);
  9412. clif_updatestatus(*sd,SP_JOBEXP);
  9413. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  9414. for(i=0;i<EQI_MAX;i++) {
  9415. if(sd->equip_index[i] >= 0)
  9416. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  9417. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  9418. }
  9419. }
  9420. //Change look, if disguised, you need to undisguise
  9421. //to correctly calculate new job sprite without
  9422. if (sd->disguise)
  9423. pc_disguise(sd, 0);
  9424. status_set_viewdata(&sd->bl, job);
  9425. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  9426. #if PACKETVER >= 20151001
  9427. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  9428. #endif
  9429. if(sd->vd.cloth_color)
  9430. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9431. /*
  9432. if(sd->vd.body_style)
  9433. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9434. */
  9435. //Update skill tree.
  9436. pc_calc_skilltree(sd);
  9437. clif_skillinfoblock(sd);
  9438. if (sd->ed)
  9439. elemental_delete(sd->ed);
  9440. if (sd->state.vending)
  9441. vending_closevending(sd);
  9442. if (sd->state.buyingstore)
  9443. buyingstore_close(sd);
  9444. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  9445. //Remove peco/cart/falcon
  9446. i = sd->sc.option;
  9447. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  9448. i&=~OPTION_RIDING;
  9449. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9450. i&=~OPTION_FALCON;
  9451. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9452. i&=~OPTION_DRAGON;
  9453. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9454. i&=~OPTION_WUGRIDER;
  9455. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9456. i&=~OPTION_WUG;
  9457. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9458. i&=~OPTION_MADOGEAR;
  9459. #ifndef NEW_CARTS
  9460. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9461. i&=~OPTION_CART;
  9462. #else
  9463. if( sd->sc.getSCE(SC_PUSH_CART) && !pc_checkskill(sd, MC_PUSHCART) )
  9464. pc_setcart(sd, 0);
  9465. #endif
  9466. if(i != sd->sc.option)
  9467. pc_setoption(sd, i);
  9468. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9469. hom_vaporize(sd, HOM_ST_ACTIVE);
  9470. if (sd->sc.getSCE(SC_SPRITEMABLE) && !pc_checkskill(sd, SU_SPRITEMABLE))
  9471. status_change_end(&sd->bl, SC_SPRITEMABLE);
  9472. if (sd->sc.getSCE(SC_SOULATTACK) && !pc_checkskill(sd, SU_SOULATTACK))
  9473. status_change_end(&sd->bl, SC_SOULATTACK);
  9474. if( sd->sc.getSCE(SC_SPIRIT) ){
  9475. status_change_end( &sd->bl, SC_SPIRIT );
  9476. }
  9477. if(sd->status.manner < 0){
  9478. clif_changemanner( *sd );
  9479. clif_updatestatus(*sd,SP_MANNER);
  9480. }
  9481. status_calc_pc(sd,SCO_FORCE);
  9482. pc_checkallowskill(sd);
  9483. pc_equiplookall(sd);
  9484. pc_show_questinfo(sd);
  9485. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9486. if( sd->status.party_id ){
  9487. struct party_data* p;
  9488. if( ( p = party_search( sd->status.party_id ) ) != nullptr ){
  9489. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9490. if( i < MAX_PARTY ){
  9491. p->party.member[i].class_ = sd->status.class_;
  9492. clif_party_job_and_level( *sd );
  9493. }
  9494. }
  9495. }
  9496. chrif_save(sd, CSAVE_NORMAL);
  9497. //if you were previously famous, not anymore.
  9498. if (fame_flag)
  9499. chrif_buildfamelist();
  9500. else if (sd->status.fame > 0) {
  9501. //It may be that now they are famous?
  9502. switch (sd->class_&MAPID_UPPERMASK) {
  9503. case MAPID_BLACKSMITH:
  9504. case MAPID_ALCHEMIST:
  9505. case MAPID_TAEKWON:
  9506. chrif_buildfamelist();
  9507. break;
  9508. }
  9509. }
  9510. return true;
  9511. }
  9512. /*==========================================
  9513. * Tell client player sd has change equipement
  9514. *------------------------------------------*/
  9515. void pc_equiplookall(map_session_data *sd)
  9516. {
  9517. nullpo_retv(sd);
  9518. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9519. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9520. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9521. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9522. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9523. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9524. }
  9525. /*==========================================
  9526. * Tell client player sd has change look (hair,equip...)
  9527. *------------------------------------------*/
  9528. void pc_changelook(map_session_data *sd,int32 type,int32 val) {
  9529. nullpo_retv(sd);
  9530. switch(type) {
  9531. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9532. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9533. if (sd->status.hair != val) {
  9534. sd->status.hair = val;
  9535. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9536. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9537. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9538. }
  9539. break;
  9540. case LOOK_WEAPON:
  9541. sd->status.weapon = val;
  9542. break;
  9543. case LOOK_HEAD_BOTTOM:
  9544. sd->status.head_bottom = val;
  9545. sd->setlook_head_bottom = val;
  9546. break;
  9547. case LOOK_HEAD_TOP:
  9548. sd->status.head_top = val;
  9549. sd->setlook_head_top = val;
  9550. break;
  9551. case LOOK_HEAD_MID:
  9552. sd->status.head_mid = val;
  9553. sd->setlook_head_mid = val;
  9554. break;
  9555. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9556. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9557. if (sd->status.hair_color != val) {
  9558. sd->status.hair_color = val;
  9559. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9560. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9561. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9562. }
  9563. break;
  9564. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9565. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9566. sd->status.clothes_color = val;
  9567. break;
  9568. case LOOK_SHIELD:
  9569. sd->status.shield = val;
  9570. break;
  9571. case LOOK_SHOES:
  9572. break;
  9573. case LOOK_ROBE:
  9574. sd->status.robe = val;
  9575. sd->setlook_robe = val;
  9576. break;
  9577. case LOOK_BODY2:
  9578. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9579. sd->status.body = val;
  9580. break;
  9581. }
  9582. clif_changelook(&sd->bl, type, val);
  9583. }
  9584. /*==========================================
  9585. * Give an option (type) to player (sd) and display it to client
  9586. *------------------------------------------*/
  9587. void pc_setoption(map_session_data *sd,int32 type, int32 subtype)
  9588. {
  9589. int32 p_type, new_look=0;
  9590. nullpo_retv(sd);
  9591. p_type = sd->sc.option;
  9592. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9593. sd->sc.option=type;
  9594. clif_changeoption(&sd->bl);
  9595. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9596. { // Mounting
  9597. clif_status_load(&sd->bl,EFST_RIDING,1);
  9598. status_calc_pc(sd,SCO_NONE);
  9599. }
  9600. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9601. { // Dismount
  9602. clif_status_load(&sd->bl,EFST_RIDING,0);
  9603. status_calc_pc(sd,SCO_NONE);
  9604. }
  9605. #ifndef NEW_CARTS
  9606. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9607. clif_cartlist(sd);
  9608. clif_updatestatus(*sd, SP_CARTINFO);
  9609. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9610. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9611. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9612. clif_clearcart(sd->fd);
  9613. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9614. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9615. }
  9616. #endif
  9617. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9618. clif_status_load(&sd->bl,EFST_FALCON,1);
  9619. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9620. clif_status_load(&sd->bl,EFST_FALCON,0);
  9621. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9622. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9623. status_calc_pc(sd,SCO_NONE);
  9624. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9625. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9626. status_calc_pc(sd,SCO_NONE);
  9627. }
  9628. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9629. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9630. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9631. status_change_end(&sd->bl, SC_MADOGEAR);
  9632. }
  9633. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9634. new_look = JOB_STAR_GLADIATOR2;
  9635. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9636. new_look = -1;
  9637. if (sd->disguise || !new_look)
  9638. return; //Disguises break sprite changes
  9639. if (new_look < 0) { //Restore normal look.
  9640. status_set_viewdata(&sd->bl, sd->status.class_);
  9641. new_look = sd->vd.class_;
  9642. }
  9643. // Stop attacking on new view change (to prevent wedding/santa attacks).
  9644. unit_stop_attack( &sd->bl );
  9645. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9646. if (sd->vd.cloth_color)
  9647. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9648. if( sd->vd.body_style )
  9649. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9650. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9651. }
  9652. /**
  9653. * Give player a cart
  9654. * @param sd Player
  9655. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9656. **/
  9657. bool pc_setcart(map_session_data *sd,int32 type) {
  9658. #ifndef NEW_CARTS
  9659. int32 cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9660. int32 option;
  9661. #endif
  9662. nullpo_retr(false,sd);
  9663. if( type < 0 || type > MAX_CARTS )
  9664. return false;// Never trust the values sent by the client! [Skotlex]
  9665. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9666. return false;// Push cart is required
  9667. #ifdef NEW_CARTS
  9668. switch( type ) {
  9669. case 0:
  9670. if( !sd->sc.getSCE(SC_PUSH_CART) )
  9671. return 0;
  9672. status_change_end(&sd->bl,SC_PUSH_CART);
  9673. clif_clearcart(sd->fd);
  9674. break;
  9675. default:/* everything else is an allowed ID so we can move on */
  9676. if( !sd->sc.getSCE(SC_PUSH_CART) ) { /* first time, so fill cart data */
  9677. clif_cartlist(sd);
  9678. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9679. }
  9680. clif_updatestatus(*sd, SP_CARTINFO);
  9681. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9682. break;
  9683. }
  9684. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9685. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9686. #else
  9687. // Update option
  9688. option = sd->sc.option;
  9689. option &= ~OPTION_CART;// clear cart bits
  9690. option |= cart[type]; // set cart
  9691. pc_setoption(sd, option);
  9692. #endif
  9693. return true;
  9694. }
  9695. /*==========================================
  9696. * Give player a falcon
  9697. *------------------------------------------*/
  9698. void pc_setfalcon(map_session_data* sd, int32 flag)
  9699. {
  9700. if( flag ){
  9701. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9702. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9703. } else if( pc_isfalcon(sd) ){
  9704. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9705. }
  9706. }
  9707. /*==========================================
  9708. * Set player riding
  9709. *------------------------------------------*/
  9710. void pc_setriding(map_session_data* sd, int32 flag)
  9711. {
  9712. if( sd->sc.getSCE(SC_ALL_RIDING) )
  9713. return;
  9714. if( flag ){
  9715. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9716. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9717. } else if( pc_isriding(sd) ){
  9718. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9719. }
  9720. }
  9721. /**
  9722. * Give player a mado
  9723. * @param sd: Player
  9724. * @param flag: Enable or disable mado
  9725. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9726. */
  9727. void pc_setmadogear(map_session_data *sd, bool flag, e_mado_type type)
  9728. {
  9729. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9730. return;
  9731. if (flag) {
  9732. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9733. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9734. } else if (pc_ismadogear(sd)) {
  9735. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9736. }
  9737. }
  9738. /*==========================================
  9739. * Check if player can drop an item
  9740. *------------------------------------------*/
  9741. bool pc_candrop(map_session_data *sd, struct item *item)
  9742. {
  9743. if (sd->sc.cant.drop)
  9744. return false;
  9745. if( item && itemdb_ishatched_egg(item) )
  9746. return false;
  9747. if (pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL)) // no restriction
  9748. return true;
  9749. if( !pc_can_give_items(sd) )
  9750. return false;
  9751. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  9752. return false;
  9753. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9754. }
  9755. /*==========================================
  9756. * Read '@type' variables (temporary numeric char reg)
  9757. *------------------------------------------*/
  9758. int64 pc_readreg(map_session_data* sd, int64 reg)
  9759. {
  9760. return i64db_i64get(sd->regs.vars, reg);
  9761. }
  9762. /*==========================================
  9763. * Set '@type' variables (temporary numeric char reg)
  9764. *------------------------------------------*/
  9765. bool pc_setreg(map_session_data* sd, int64 reg, int64 val)
  9766. {
  9767. uint32 index = script_getvaridx(reg);
  9768. nullpo_retr(false, sd);
  9769. if( val ) {
  9770. i64db_i64put(sd->regs.vars, reg, val);
  9771. if( index )
  9772. script_array_update(&sd->regs, reg, false);
  9773. } else {
  9774. i64db_remove(sd->regs.vars, reg);
  9775. if( index )
  9776. script_array_update(&sd->regs, reg, true);
  9777. }
  9778. return true;
  9779. }
  9780. /*==========================================
  9781. * Read '@type$' variables (temporary string char reg)
  9782. *------------------------------------------*/
  9783. char* pc_readregstr(map_session_data* sd, int64 reg)
  9784. {
  9785. struct script_reg_str *p = nullptr;
  9786. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9787. return p ? p->value : nullptr;
  9788. }
  9789. /*==========================================
  9790. * Set '@type$' variables (temporary string char reg)
  9791. *------------------------------------------*/
  9792. bool pc_setregstr(map_session_data* sd, int64 reg, const char* str)
  9793. {
  9794. struct script_reg_str *p = nullptr;
  9795. uint32 index = script_getvaridx(reg);
  9796. DBData prev;
  9797. nullpo_retr(false, sd);
  9798. if( str[0] ) {
  9799. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9800. p->value = aStrdup(str);
  9801. p->flag.type = 1;
  9802. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9803. p = (struct script_reg_str *)db_data2ptr(&prev);
  9804. if( p->value )
  9805. aFree(p->value);
  9806. ers_free(str_reg_ers, p);
  9807. } else {
  9808. if( index )
  9809. script_array_update(&sd->regs, reg, false);
  9810. }
  9811. } else {
  9812. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9813. p = (struct script_reg_str *)db_data2ptr(&prev);
  9814. if( p->value )
  9815. aFree(p->value);
  9816. ers_free(str_reg_ers, p);
  9817. if( index )
  9818. script_array_update(&sd->regs, reg, true);
  9819. }
  9820. }
  9821. return true;
  9822. }
  9823. /**
  9824. * Serves the following variable types:
  9825. * - 'type' (permanent numeric char reg)
  9826. * - '#type' (permanent numeric account reg)
  9827. * - '##type' (permanent numeric account reg2)
  9828. **/
  9829. int64 pc_readregistry(map_session_data *sd, int64 reg)
  9830. {
  9831. struct script_reg_num *p = nullptr;
  9832. if (!sd->vars_ok) {
  9833. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9834. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9835. //intif->request_registry(sd,type==3?4:type);
  9836. set_eof(sd->fd);
  9837. return 0;
  9838. }
  9839. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9840. return p ? p->value : 0;
  9841. }
  9842. /**
  9843. * Serves the following variable types:
  9844. * - 'type$' (permanent str char reg)
  9845. * - '#type$' (permanent str account reg)
  9846. * - '##type$' (permanent str account reg2)
  9847. **/
  9848. char* pc_readregistry_str(map_session_data *sd, int64 reg)
  9849. {
  9850. struct script_reg_str *p = nullptr;
  9851. if (!sd->vars_ok) {
  9852. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9853. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9854. //intif->request_registry(sd,type==3?4:type);
  9855. set_eof(sd->fd);
  9856. return nullptr;
  9857. }
  9858. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9859. return p ? p->value : nullptr;
  9860. }
  9861. /**
  9862. * Serves the following variable types:
  9863. * - 'type' (permanent numeric char reg)
  9864. * - '#type' (permanent numeric account reg)
  9865. * - '##type' (permanent numeric account reg2)
  9866. **/
  9867. bool pc_setregistry(map_session_data *sd, int64 reg, int64 val)
  9868. {
  9869. struct script_reg_num *p = nullptr;
  9870. const char *regname = get_str(script_getvarid(reg));
  9871. uint32 index = script_getvaridx(reg);
  9872. if ( !reg_load && !sd->vars_ok ) {
  9873. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9874. return false;
  9875. }
  9876. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9877. if( val ) {
  9878. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9879. script_array_update(&sd->regs, reg, false);
  9880. p->value = val;
  9881. } else {
  9882. p->value = 0;
  9883. if( index )
  9884. script_array_update(&sd->regs, reg, true);
  9885. }
  9886. if (!reg_load)
  9887. p->flag.update = 1;/* either way, it will require either delete or replace */
  9888. } else if( val ) {
  9889. DBData prev;
  9890. if( index )
  9891. script_array_update(&sd->regs, reg, false);
  9892. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9893. p->value = val;
  9894. if (!reg_load)
  9895. p->flag.update = 1;
  9896. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9897. p = (struct script_reg_num *)db_data2ptr(&prev);
  9898. ers_free(num_reg_ers, p);
  9899. }
  9900. }
  9901. if (!reg_load && p)
  9902. sd->vars_dirty = true;
  9903. return true;
  9904. }
  9905. /**
  9906. * Serves the following variable types:
  9907. * - 'type$' (permanent str char reg)
  9908. * - '#type$' (permanent str account reg)
  9909. * - '##type$' (permanent str account reg2)
  9910. **/
  9911. bool pc_setregistry_str(map_session_data *sd, int64 reg, const char *val)
  9912. {
  9913. struct script_reg_str *p = nullptr;
  9914. const char *regname = get_str(script_getvarid(reg));
  9915. uint32 index = script_getvaridx(reg);
  9916. size_t vlen = 0;
  9917. if (!reg_load && !sd->vars_ok) {
  9918. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9919. return false;
  9920. }
  9921. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9922. {
  9923. ShowError("pc_check_RegistryVariableLength: Variable value length is too long (aid: %d, cid: %d): '%s' sz=%zu\n", sd->status.account_id, sd->status.char_id, val, vlen);
  9924. return false;
  9925. }
  9926. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9927. if( val[0] ) {
  9928. if( p->value )
  9929. aFree(p->value);
  9930. else if ( index ) // an entry that was deleted, so we reset
  9931. script_array_update(&sd->regs, reg, false);
  9932. p->value = aStrdup(val);
  9933. } else {
  9934. if (p->value)
  9935. aFree(p->value);
  9936. p->value = nullptr;
  9937. if( index )
  9938. script_array_update(&sd->regs, reg, true);
  9939. }
  9940. if( !reg_load )
  9941. p->flag.update = 1; // either way, it will require either delete or replace
  9942. } else if( val[0] ) {
  9943. DBData prev;
  9944. if( index )
  9945. script_array_update(&sd->regs, reg, false);
  9946. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9947. p->value = aStrdup(val);
  9948. if( !reg_load )
  9949. p->flag.update = 1;
  9950. p->flag.type = 1;
  9951. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9952. p = (struct script_reg_str *)db_data2ptr(&prev);
  9953. if( p->value )
  9954. aFree(p->value);
  9955. ers_free(str_reg_ers, p);
  9956. }
  9957. }
  9958. if( !reg_load && p )
  9959. sd->vars_dirty = true;
  9960. return true;
  9961. }
  9962. /**
  9963. * Set value of player variable
  9964. * @param sd Player
  9965. * @param reg Variable name
  9966. * @param value
  9967. * @return True if success, false if failed.
  9968. **/
  9969. bool pc_setreg2(map_session_data *sd, const char *reg, int64 val) {
  9970. char prefix = reg[0];
  9971. nullpo_retr(false, sd);
  9972. if (reg[strlen(reg)-1] == '$') {
  9973. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9974. return false;
  9975. }
  9976. val = cap_value(val, INT_MIN, INT_MAX);
  9977. switch (prefix) {
  9978. case '.':
  9979. case '\'':
  9980. case '$':
  9981. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9982. return false;
  9983. case '@':
  9984. return pc_setreg(sd, add_str(reg), val);
  9985. case '#':
  9986. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9987. default:
  9988. return pc_setglobalreg(sd, add_str(reg), val);
  9989. }
  9990. return false;
  9991. }
  9992. /**
  9993. * Get value of player variable
  9994. * @param sd Player
  9995. * @param reg Variable name
  9996. * @return Variable value or 0 if failed.
  9997. **/
  9998. int64 pc_readreg2(map_session_data *sd, const char *reg) {
  9999. char prefix = reg[0];
  10000. nullpo_ret(sd);
  10001. if (reg[strlen(reg)-1] == '$') {
  10002. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  10003. return 0;
  10004. }
  10005. switch (prefix) {
  10006. case '.':
  10007. case '\'':
  10008. case '$':
  10009. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  10010. return 0;
  10011. case '@':
  10012. return pc_readreg(sd, add_str(reg));
  10013. case '#':
  10014. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  10015. default:
  10016. return pc_readglobalreg(sd, add_str(reg));
  10017. }
  10018. return 0;
  10019. }
  10020. /*==========================================
  10021. * Exec eventtimer for player sd (retrieved from map_session (id))
  10022. *------------------------------------------*/
  10023. static TIMER_FUNC(pc_eventtimer){
  10024. map_session_data *sd=map_id2sd(id);
  10025. char *p = (char *)data;
  10026. int32 i;
  10027. if(sd==nullptr)
  10028. return 0;
  10029. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  10030. if( i < MAX_EVENTTIMER )
  10031. {
  10032. sd->eventtimer[i] = INVALID_TIMER;
  10033. sd->eventcount--;
  10034. npc_event(sd,p,0);
  10035. }
  10036. else
  10037. ShowError("pc_eventtimer: no such event timer\n");
  10038. if (p) aFree(p);
  10039. return 0;
  10040. }
  10041. /*==========================================
  10042. * Add eventtimer for player sd ?
  10043. *------------------------------------------*/
  10044. bool pc_addeventtimer(map_session_data *sd,int32 tick,const char *name)
  10045. {
  10046. int32 i;
  10047. nullpo_retr(false,sd);
  10048. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  10049. if( i == MAX_EVENTTIMER )
  10050. return false;
  10051. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  10052. sd->eventcount++;
  10053. return true;
  10054. }
  10055. /*==========================================
  10056. * Del eventtimer for player sd ?
  10057. *------------------------------------------*/
  10058. bool pc_deleventtimer(map_session_data *sd,const char *name)
  10059. {
  10060. char* p = nullptr;
  10061. int32 i;
  10062. nullpo_retr(false,sd);
  10063. if (sd->eventcount == 0)
  10064. return false;
  10065. // find the named event timer
  10066. ARR_FIND( 0, MAX_EVENTTIMER, i,
  10067. sd->eventtimer[i] != INVALID_TIMER &&
  10068. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != nullptr &&
  10069. strcmp(p, name) == 0
  10070. );
  10071. if( i == MAX_EVENTTIMER )
  10072. return false; // not found
  10073. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10074. sd->eventtimer[i] = INVALID_TIMER;
  10075. if(sd->eventcount > 0)
  10076. sd->eventcount--;
  10077. aFree(p);
  10078. return true;
  10079. }
  10080. /*==========================================
  10081. * Update eventtimer count for player sd
  10082. *------------------------------------------*/
  10083. void pc_addeventtimercount(map_session_data *sd,const char *name,int32 tick)
  10084. {
  10085. int32 i;
  10086. nullpo_retv(sd);
  10087. for(i=0;i<MAX_EVENTTIMER;i++)
  10088. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  10089. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  10090. addtick_timer(sd->eventtimer[i],tick);
  10091. break;
  10092. }
  10093. }
  10094. /*==========================================
  10095. * Remove all eventtimer for player sd
  10096. *------------------------------------------*/
  10097. void pc_cleareventtimer(map_session_data *sd)
  10098. {
  10099. int32 i;
  10100. nullpo_retv(sd);
  10101. if (sd->eventcount == 0)
  10102. return;
  10103. for(i=0;i<MAX_EVENTTIMER;i++){
  10104. if( sd->eventtimer[i] != INVALID_TIMER ){
  10105. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  10106. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10107. sd->eventtimer[i] = INVALID_TIMER;
  10108. if(sd->eventcount > 0) //avoid looping to max val
  10109. sd->eventcount--;
  10110. if (p) aFree(p);
  10111. }
  10112. }
  10113. }
  10114. /**
  10115. * Called when an item with combo is worn
  10116. * @param sd: Player data
  10117. * @param data: Item data
  10118. * @return Number of succeeded combo(s)
  10119. */
  10120. static int32 pc_checkcombo(map_session_data *sd, item_data *data) {
  10121. int32 success = 0;
  10122. for (const auto &item_combo : data->combos) {
  10123. bool do_continue = false;
  10124. // Ensure this isn't a duplicate combo
  10125. for (const auto player_combo : sd->combos) {
  10126. if (player_combo->id == item_combo->id) {
  10127. do_continue = true;
  10128. break;
  10129. }
  10130. }
  10131. // Combo already equipped
  10132. if (do_continue)
  10133. continue;
  10134. size_t nb_itemCombo = item_combo->nameid.size();
  10135. if (nb_itemCombo < 2) // A combo with less then 2 item?
  10136. continue;
  10137. struct s_itemchk {
  10138. int32 idx;
  10139. t_itemid nameid, card[MAX_SLOTS];
  10140. s_itemchk() : idx(0), nameid(0), card() {};
  10141. };
  10142. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  10143. size_t j;
  10144. uint32 pos = 0;
  10145. for (j = 0; j < nb_itemCombo; j++) {
  10146. t_itemid id = item_combo->nameid[j];
  10147. bool found = false;
  10148. for (int16 k = 0; k < EQI_MAX; k++) {
  10149. short index = sd->equip_index[k];
  10150. if (index < 0)
  10151. continue;
  10152. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  10153. continue;
  10154. if (!sd->inventory_data[index])
  10155. continue;
  10156. if (itemdb_type(id) != IT_CARD) {
  10157. if (sd->inventory_data[index]->nameid != id)
  10158. continue;
  10159. if (j > 0) { // Check if this item not already used
  10160. do_continue = false;
  10161. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  10162. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  10163. do_continue = true;
  10164. break;
  10165. }
  10166. }
  10167. if (do_continue)
  10168. continue;
  10169. }
  10170. combo_idx[j].nameid = id;
  10171. combo_idx[j].idx = index;
  10172. pos |= sd->inventory.u.items_inventory[index].equip;
  10173. found = true;
  10174. break;
  10175. } else { // Cards and enchants
  10176. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  10177. continue;
  10178. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  10179. do_continue = false;
  10180. if (sd->inventory.u.items_inventory[index].card[z] != id)
  10181. continue;
  10182. if (j > 0) {
  10183. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  10184. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  10185. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  10186. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  10187. do_continue = true;
  10188. break;
  10189. }
  10190. }
  10191. }
  10192. }
  10193. }
  10194. if (do_continue)
  10195. continue;
  10196. combo_idx[j].nameid = id;
  10197. combo_idx[j].idx = index;
  10198. combo_idx[j].card[z] = id;
  10199. pos |= sd->inventory.u.items_inventory[index].equip;
  10200. found = true;
  10201. break;
  10202. }
  10203. }
  10204. }
  10205. if (!found)
  10206. break; // Unable to found all the IDs for this combo, return
  10207. }
  10208. // Broke out of the count loop without finding all IDs, move to the next combo
  10209. if (j < nb_itemCombo)
  10210. continue;
  10211. // All items in the combo are matching
  10212. auto entry = std::make_shared<s_combos>();
  10213. entry->bonus = item_combo->script;
  10214. entry->id = item_combo->id;
  10215. entry->pos = pos;
  10216. sd->combos.push_back(entry);
  10217. combo_idx.clear();
  10218. success++;
  10219. }
  10220. return success;
  10221. }
  10222. /**
  10223. * Called when an item with combo is removed
  10224. * @param sd: Player data
  10225. * @param data: Item data
  10226. * @return Number of removed combo(s)
  10227. */
  10228. static int32 pc_removecombo(map_session_data *sd, item_data *data ) {
  10229. if (sd->combos.empty())
  10230. return 0; // Nothing to do here, player has no combos
  10231. int32 retval = 0;
  10232. for (const auto &item_combo : data->combos) {
  10233. std::shared_ptr<s_combos> del_combo = nullptr;
  10234. // Check if this combo exists on this player
  10235. for (const auto &player_combo : sd->combos) {
  10236. if (player_combo->id == item_combo->id) {
  10237. del_combo = player_combo;
  10238. break;
  10239. }
  10240. }
  10241. // No match, skip this combo
  10242. if (del_combo == nullptr)
  10243. continue;
  10244. util::vector_erase_if_exists(sd->combos, del_combo);
  10245. retval++;
  10246. // Check if combo requirements still fit
  10247. if (pc_checkcombo(sd, data))
  10248. continue;
  10249. // It's empty, clear all the memory
  10250. if (sd->combos.empty()) {
  10251. sd->combos.clear();
  10252. return retval; // Return at this point as there are no more combos to check
  10253. }
  10254. }
  10255. return retval;
  10256. }
  10257. /**
  10258. * Load combo data(s) of player
  10259. * @param sd: Player data
  10260. * @return ret numbers of succeed combo
  10261. */
  10262. int32 pc_load_combo(map_session_data *sd) {
  10263. int32 ret = 0;
  10264. for (int16 i = 0; i < EQI_MAX; i++) {
  10265. item_data *id;
  10266. short idx = sd->equip_index[i];
  10267. if (idx < 0 || !(id = sd->inventory_data[idx]))
  10268. continue;
  10269. if (!id->combos.empty())
  10270. ret += pc_checkcombo(sd, id);
  10271. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  10272. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  10273. if (!sd->inventory.u.items_inventory[idx].card[j])
  10274. continue;
  10275. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[idx].card[j]);
  10276. if (data != nullptr) {
  10277. if (!data->combos.empty())
  10278. ret += pc_checkcombo(sd, data.get());
  10279. }
  10280. }
  10281. }
  10282. }
  10283. return ret;
  10284. }
  10285. /*==========================================
  10286. * Equip item on player sd at req_pos from inventory index n
  10287. * return: false - fail; true - success
  10288. *------------------------------------------*/
  10289. bool pc_equipitem(map_session_data *sd,short n,int32 req_pos,bool equipswitch)
  10290. {
  10291. int32 i, pos, flag = 0, iflag;
  10292. struct item_data *id;
  10293. short* equip_index;
  10294. nullpo_retr(false,sd);
  10295. if( n < 0 || n >= MAX_INVENTORY ) {
  10296. if( equipswitch ){
  10297. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10298. }else{
  10299. // Does not deserve an answer... [Lemongrass]
  10300. //clif_equipitemack( sd, ITEM_EQUIP_ACK_FAIL, n );
  10301. }
  10302. return false;
  10303. }
  10304. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  10305. if( equipswitch ){
  10306. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10307. }else{
  10308. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n );
  10309. }
  10310. return false;
  10311. }
  10312. if (!(id = sd->inventory_data[n]))
  10313. return false;
  10314. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  10315. if(battle_config.battle_log && !equipswitch)
  10316. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  10317. uint8 res = pc_isequip( sd, n );
  10318. if( res != ITEM_EQUIP_ACK_OK ){
  10319. if( equipswitch ){
  10320. clif_equipswitch_add( sd, n, req_pos, res );
  10321. }else{
  10322. clif_equipitemack( *sd, res, n ); // fail
  10323. }
  10324. return false;
  10325. }
  10326. if( equipswitch && id->type == IT_AMMO ){
  10327. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10328. return false;
  10329. }
  10330. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0) {
  10331. if( equipswitch ){
  10332. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10333. }else{
  10334. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); // fail
  10335. }
  10336. return false;
  10337. }
  10338. if( !sd->sc.empty() && (sd->sc.cant.equip || (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) {
  10339. if( equipswitch ){
  10340. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10341. }else{
  10342. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); //Fail
  10343. }
  10344. return false;
  10345. }
  10346. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  10347. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  10348. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  10349. clif_notify_bindOnEquip( *sd, n );
  10350. }
  10351. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  10352. pos = req_pos&EQP_ACC;
  10353. if (pos == EQP_ACC) //User specified both slots.
  10354. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  10355. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  10356. if (!sd->inventory.u.items_inventory[n].card[i])
  10357. continue;
  10358. std::shared_ptr<item_data> card_data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10359. if (card_data) {
  10360. int32 card_pos = card_data->equip;
  10361. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  10362. pos = card_pos; // Use the card's equip position
  10363. break;
  10364. }
  10365. }
  10366. }
  10367. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  10368. pos = (req_pos&EQP_ARMS);
  10369. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  10370. #ifdef RENEWAL
  10371. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  10372. #else
  10373. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  10374. #endif
  10375. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  10376. pos = req_pos&EQP_SHADOW_ACC;
  10377. if (pos == EQP_SHADOW_ACC)
  10378. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  10379. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  10380. pos = (req_pos&EQP_SHADOW_ARMS);
  10381. if( pos == EQP_SHADOW_ARMS )
  10382. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  10383. }
  10384. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  10385. //Update skill-block range database when weapon range changes. [Skotlex]
  10386. i = equip_index[EQI_HAND_R];
  10387. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  10388. flag = 1;
  10389. else
  10390. flag = id->range != sd->inventory_data[i]->range;
  10391. }
  10392. if( equipswitch ){
  10393. for( i = 0; i < EQI_MAX; i++ ){
  10394. if( pos&equip_bitmask[i] ){
  10395. // If there was already an item assigned to this slot
  10396. if( sd->equip_switch_index[i] >= 0 ){
  10397. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  10398. }
  10399. // Assign the new index to it
  10400. sd->equip_switch_index[i] = n;
  10401. }
  10402. }
  10403. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  10404. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  10405. return true;
  10406. }else{
  10407. for(i=0;i<EQI_MAX;i++) {
  10408. if(pos & equip_bitmask[i]) {
  10409. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  10410. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  10411. sd->equip_index[i] = n;
  10412. }
  10413. }
  10414. pc_equipswitch_remove(sd, n);
  10415. if(pos==EQP_AMMO) {
  10416. clif_arrowequip( *sd );
  10417. clif_arrow_fail( *sd, ARROWFAIL_SUCCESS );
  10418. }
  10419. else
  10420. clif_equipitemack( *sd, ITEM_EQUIP_ACK_OK, n, pos );
  10421. sd->inventory.u.items_inventory[n].equip = pos;
  10422. }
  10423. if(pos & EQP_HAND_R) {
  10424. if(id)
  10425. sd->weapontype1 = id->subtype;
  10426. else
  10427. sd->weapontype1 = W_FIST;
  10428. pc_calcweapontype(sd);
  10429. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10430. }
  10431. if(pos & EQP_HAND_L) {
  10432. if(id) {
  10433. if(id->type == IT_WEAPON) {
  10434. sd->status.shield = W_FIST;
  10435. sd->weapontype2 = id->subtype;
  10436. }
  10437. else
  10438. if(id->type == IT_ARMOR) {
  10439. sd->status.shield = id->look;
  10440. sd->weapontype2 = W_FIST;
  10441. }
  10442. }
  10443. else
  10444. sd->status.shield = sd->weapontype2 = W_FIST;
  10445. pc_calcweapontype(sd);
  10446. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10447. }
  10448. if(pos & EQP_SHOES)
  10449. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10450. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  10451. short idx = sd->equip_index[EQI_AMMO];
  10452. if (idx >= 0) {
  10453. switch (sd->inventory_data[idx]->subtype) {
  10454. case AMMO_ARROW:
  10455. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  10456. pc_unequipitem(sd, idx, 2 | 4);
  10457. break;
  10458. case AMMO_BULLET:
  10459. case AMMO_SHELL:
  10460. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10461. #ifdef RENEWAL
  10462. && id->subtype != W_GRENADE
  10463. #endif
  10464. )
  10465. pc_unequipitem(sd, idx, 2 | 4);
  10466. break;
  10467. #ifndef RENEWAL
  10468. case AMMO_GRENADE:
  10469. if (id->subtype != W_GRENADE)
  10470. pc_unequipitem(sd, idx, 2 | 4);
  10471. break;
  10472. #endif
  10473. }
  10474. }
  10475. }
  10476. pc_set_costume_view(sd);
  10477. pc_checkallowskill(sd); //Check if status changes should be halted.
  10478. iflag = sd->npc_item_flag;
  10479. // Check for combos (MUST be before status_calc_pc)
  10480. if (!id->combos.empty())
  10481. pc_checkcombo(sd, id);
  10482. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10483. ; // No cards
  10484. else {
  10485. for (i = 0; i < MAX_SLOTS; i++) {
  10486. if (!sd->inventory.u.items_inventory[n].card[i])
  10487. continue;
  10488. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10489. if (data != nullptr) {
  10490. if (!data->combos.empty())
  10491. pc_checkcombo(sd, data.get());
  10492. }
  10493. }
  10494. }
  10495. status_calc_pc(sd,SCO_NONE);
  10496. if (flag) //Update skill data
  10497. clif_skillinfoblock(sd);
  10498. //OnEquip script [Skotlex]
  10499. if (id) {
  10500. current_equip_item_index = n;
  10501. current_equip_card_id = 0;
  10502. //only run the script if item isn't restricted
  10503. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  10504. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10505. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10506. ; //No cards
  10507. else {
  10508. for( i = 0; i < MAX_SLOTS; i++ ) {
  10509. if (!sd->inventory.u.items_inventory[n].card[i])
  10510. continue;
  10511. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10512. if ( data != nullptr ) {
  10513. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data.get(), sd->bl.m))) {
  10514. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10515. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10516. }
  10517. }
  10518. current_equip_card_id = 0;
  10519. }
  10520. }
  10521. current_equip_item_index = -1;
  10522. }
  10523. sd->npc_item_flag = iflag;
  10524. return true;
  10525. }
  10526. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int32 position ){
  10527. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10528. while( it != bonus.end() ){
  10529. std::shared_ptr<s_autobonus> b = *it;
  10530. if( ( b->pos & position ) != b->pos ){
  10531. it++;
  10532. continue;
  10533. }
  10534. it = bonus.erase( it );
  10535. }
  10536. }
  10537. /**
  10538. * Recalculate player status on unequip
  10539. * @param sd: Player data
  10540. * @param n: Item inventory index
  10541. * @param flag: Whether to recalculate a player's status or not
  10542. * @return True on success or false on failure
  10543. */
  10544. static void pc_unequipitem_sub(map_session_data *sd, int32 n, int32 flag) {
  10545. int32 i, iflag;
  10546. bool status_calc = false;
  10547. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10548. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10549. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10550. sd->inventory.u.items_inventory[n].equip = 0;
  10551. if (!(flag & 4))
  10552. pc_checkallowskill(sd);
  10553. iflag = sd->npc_item_flag;
  10554. // Check for combos (MUST be before status_calc_pc)
  10555. if (sd->inventory_data[n]) {
  10556. if (!sd->inventory_data[n]->combos.empty()) {
  10557. if (pc_removecombo(sd, sd->inventory_data[n]))
  10558. status_calc = true;
  10559. }
  10560. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10561. ; // No cards
  10562. else {
  10563. for (i = 0; i < MAX_SLOTS; i++) {
  10564. if (!sd->inventory.u.items_inventory[n].card[i])
  10565. continue;
  10566. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10567. if (data != nullptr) {
  10568. if (!data->combos.empty()) {
  10569. if (pc_removecombo(sd, data.get()))
  10570. status_calc = true;
  10571. }
  10572. }
  10573. }
  10574. }
  10575. }
  10576. if (flag & 1 || status_calc) {
  10577. pc_checkallowskill(sd);
  10578. status_calc_pc(sd, SCO_FORCE);
  10579. }
  10580. if (sd->sc.getSCE(SC_SIGNUMCRUCIS) && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10581. status_change_end(&sd->bl, SC_SIGNUMCRUCIS);
  10582. //OnUnEquip script [Skotlex]
  10583. if (sd->inventory_data[n]) {
  10584. current_equip_item_index = n;
  10585. current_equip_card_id = 0;
  10586. if (sd->inventory_data[n]->unequip_script)
  10587. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10588. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10589. ; //No cards
  10590. else {
  10591. for (i = 0; i < MAX_SLOTS; i++) {
  10592. if (!sd->inventory.u.items_inventory[n].card[i])
  10593. continue;
  10594. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10595. if (data != nullptr) {
  10596. if (data->unequip_script) {
  10597. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10598. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10599. }
  10600. }
  10601. current_equip_card_id = 0;
  10602. }
  10603. }
  10604. current_equip_item_index = -1;
  10605. }
  10606. sd->npc_item_flag = iflag;
  10607. }
  10608. /**
  10609. * Called when attempting to unequip an item from a player
  10610. * @param sd: Player data
  10611. * @param n: Item inventory index
  10612. * @param flag: Type of unequip
  10613. * 0 - only unequip
  10614. * 1 - calculate status after unequipping
  10615. * 2 - force unequip
  10616. * 4 - unequip by switching equipment
  10617. * @return True on success or false on failure
  10618. */
  10619. bool pc_unequipitem(map_session_data *sd, int32 n, int32 flag) {
  10620. int32 i, pos;
  10621. nullpo_retr(false,sd);
  10622. if (n < 0 || n >= MAX_INVENTORY) {
  10623. clif_unequipitemack(*sd,0,0,false);
  10624. return false;
  10625. }
  10626. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10627. clif_unequipitemack(*sd,n,0,false);
  10628. return false; //Nothing to unequip
  10629. }
  10630. // status change that makes player cannot unequip equipment
  10631. if (!(flag&2) && !sd->sc.empty() &&( sd->sc.cant.unequip ||
  10632. (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10633. {
  10634. clif_unequipitemack(*sd,n,0,false);
  10635. return false;
  10636. }
  10637. if (battle_config.battle_log)
  10638. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10639. for(i = 0; i < EQI_MAX; i++) {
  10640. if (pos & equip_bitmask[i])
  10641. sd->equip_index[i] = -1;
  10642. }
  10643. if(pos & EQP_HAND_R) {
  10644. sd->weapontype1 = W_FIST;
  10645. sd->status.weapon = sd->weapontype2;
  10646. pc_calcweapontype(sd);
  10647. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10648. if( !battle_config.dancing_weaponswitch_fix )
  10649. status_change_end(&sd->bl, SC_DANCING); // Unequipping => stop dancing.
  10650. #ifdef RENEWAL
  10651. if (battle_config.switch_remove_edp&2) {
  10652. #else
  10653. if (battle_config.switch_remove_edp&1) {
  10654. #endif
  10655. status_change_end(&sd->bl, SC_EDP);
  10656. }
  10657. }
  10658. if(pos & EQP_HAND_L) {
  10659. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10660. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10661. sd->status.shield = sd->weapontype2 = W_FIST;
  10662. pc_calcweapontype(sd);
  10663. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10664. }
  10665. if(pos & EQP_SHOES)
  10666. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10667. clif_unequipitemack(*sd,n,pos,true);
  10668. pc_set_costume_view(sd);
  10669. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10670. // On weapon change (right and left hand)
  10671. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10672. if (battle_config.ammo_unequip && !(flag & 4)) {
  10673. switch (sd->inventory_data[n]->subtype) {
  10674. case W_BOW:
  10675. case W_MUSICAL:
  10676. case W_WHIP:
  10677. case W_REVOLVER:
  10678. case W_RIFLE:
  10679. case W_GATLING:
  10680. case W_SHOTGUN:
  10681. case W_GRENADE: {
  10682. short idx = sd->equip_index[EQI_AMMO];
  10683. if (idx >= 0) {
  10684. sd->equip_index[EQI_AMMO] = -1;
  10685. clif_unequipitemack(*sd, idx, sd->inventory.u.items_inventory[idx].equip, true);
  10686. pc_unequipitem_sub(sd, idx, 0);
  10687. }
  10688. }
  10689. break;
  10690. }
  10691. }
  10692. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10693. }
  10694. // On armor change
  10695. if (pos & EQP_ARMOR) {
  10696. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10697. }
  10698. // On equipment change
  10699. #ifndef RENEWAL
  10700. if (!(flag&4))
  10701. status_change_end(&sd->bl, SC_CONCENTRATION);
  10702. #endif
  10703. // On ammo change
  10704. if (sd->inventory_data[n]->type == IT_AMMO && (sd->inventory_data[n]->nameid != ITEMID_SILVER_BULLET || sd->inventory_data[n]->nameid != ITEMID_PURIFICATION_BULLET || sd->inventory_data[n]->nameid != ITEMID_SILVER_BULLET_))
  10705. status_change_end(&sd->bl, SC_P_ALTER);
  10706. pc_unequipitem_sub(sd, n, flag);
  10707. return true;
  10708. }
  10709. int32 pc_equipswitch( map_session_data* sd, int32 index ){
  10710. // Get the target equip mask
  10711. int32 position = sd->inventory.u.items_inventory[index].equipSwitch;
  10712. // Get the currently equipped item
  10713. short equippedItem = pc_checkequip( sd, position, true );
  10714. // No item equipped at the target
  10715. if( equippedItem == -1 ){
  10716. // Remove it from the equip switch
  10717. pc_equipswitch_remove( sd, index );
  10718. pc_equipitem( sd, index, position );
  10719. return position;
  10720. }else{
  10721. std::map<int32, int32> unequipped;
  10722. int32 unequipped_position = 0;
  10723. // Unequip all items that interfere
  10724. for( int32 i = 0; i < EQI_MAX; i++ ){
  10725. int32 unequip_index = sd->equip_index[i];
  10726. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10727. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10728. // Store the unequipped index and position mask for later
  10729. unequipped[unequip_index] = unequip_item->equip;
  10730. // Keep the position for later
  10731. unequipped_position |= unequip_item->equip;
  10732. // Unequip the item
  10733. pc_unequipitem( sd, unequip_index, 0 );
  10734. }
  10735. }
  10736. int32 all_position = position | unequipped_position;
  10737. // Equip everything that is hit by the mask
  10738. for( int32 i = 0; i < EQI_MAX; i++ ){
  10739. int32 exchange_index = sd->equip_switch_index[i];
  10740. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10741. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10742. // Store the target position
  10743. int32 exchange_position = exchange_item->equipSwitch;
  10744. // Remove the item from equip switch
  10745. pc_equipswitch_remove( sd, exchange_index );
  10746. // Equip the item at the destinated position
  10747. pc_equipitem( sd, exchange_index, exchange_position );
  10748. }
  10749. }
  10750. // Place all unequipped items into the equip switch window
  10751. for( std::pair<int32, int32> pair : unequipped ){
  10752. int32 unequipped_index = pair.first;
  10753. int32 unequipped_position = pair.second;
  10754. // Rebuild the index cache
  10755. for( int32 i = 0; i < EQI_MAX; i++ ){
  10756. if( unequipped_position & equip_bitmask[i] ){
  10757. sd->equip_switch_index[i] = unequipped_index;
  10758. }
  10759. }
  10760. // Set the correct position mask
  10761. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10762. // Notify the client
  10763. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10764. }
  10765. return all_position;
  10766. }
  10767. }
  10768. void pc_equipswitch_remove( map_session_data* sd, int32 index ){
  10769. struct item* item = &sd->inventory.u.items_inventory[index];
  10770. if( !item->equipSwitch ){
  10771. return;
  10772. }
  10773. for( int32 i = 0; i < EQI_MAX; i++ ){
  10774. // If a match is found
  10775. if( sd->equip_switch_index[i] == index ){
  10776. // Remove it from the slot
  10777. sd->equip_switch_index[i] = -1;
  10778. }
  10779. }
  10780. // Send out one packet for all slots using the current item's mask
  10781. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10782. item->equipSwitch = 0;
  10783. }
  10784. /*==========================================
  10785. * Checking if player (sd) has an invalid item
  10786. * and is unequiped on map load (item_noequip)
  10787. *------------------------------------------*/
  10788. void pc_checkitem(map_session_data *sd) {
  10789. int32 i, calc_flag = 0;
  10790. struct item* it;
  10791. nullpo_retv(sd);
  10792. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10793. return;
  10794. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10795. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10796. it = &sd->inventory.u.items_inventory[i];
  10797. if( it->nameid == 0 )
  10798. continue;
  10799. if( !it->equip )
  10800. continue;
  10801. if( it->equip&~pc_equippoint(sd,i) ) {
  10802. pc_unequipitem(sd, i, 2);
  10803. calc_flag = 1;
  10804. continue;
  10805. }
  10806. if( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) {
  10807. pc_unequipitem(sd, i, 2);
  10808. calc_flag = 1;
  10809. continue;
  10810. }
  10811. }
  10812. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10813. it = &sd->inventory.u.items_inventory[i];
  10814. if( it->nameid == 0 )
  10815. continue;
  10816. if( !it->equipSwitch )
  10817. continue;
  10818. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10819. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10820. for( int32 j = 0; j < EQI_MAX; j++ ){
  10821. if( sd->equip_switch_index[j] == i ){
  10822. sd->equip_switch_index[j] = -1;
  10823. }
  10824. }
  10825. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10826. continue;
  10827. }
  10828. }
  10829. if( calc_flag && sd->state.active ) {
  10830. pc_checkallowskill(sd);
  10831. status_calc_pc(sd,SCO_NONE);
  10832. }
  10833. }
  10834. /*==========================================
  10835. * Checks for unavailable items and removes them.
  10836. * @param sd: Player data
  10837. * @param type Forced check:
  10838. * 1 - Inventory
  10839. * 2 - Cart
  10840. * 4 - Storage
  10841. *------------------------------------------*/
  10842. void pc_check_available_item(map_session_data *sd, uint8 type)
  10843. {
  10844. int32 i;
  10845. t_itemid nameid;
  10846. char output[256];
  10847. nullpo_retv(sd);
  10848. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10849. for(i = 0; i < MAX_INVENTORY; i++) {
  10850. nameid = sd->inventory.u.items_inventory[i].nameid;
  10851. if (!nameid)
  10852. continue;
  10853. if (!itemdb_available(nameid)) {
  10854. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10855. clif_displaymessage(sd->fd, output);
  10856. ShowWarning("Removed invalid/disabled item (ID: %u, amount: %d) from inventory (char_id: %d).\n", nameid, sd->inventory.u.items_inventory[i].amount, sd->status.char_id);
  10857. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10858. continue;
  10859. }
  10860. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10861. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10862. }
  10863. }
  10864. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10865. for(i = 0; i < MAX_CART; i++) {
  10866. nameid = sd->cart.u.items_cart[i].nameid;
  10867. if (!nameid)
  10868. continue;
  10869. if (!itemdb_available(nameid)) {
  10870. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10871. clif_displaymessage(sd->fd, output);
  10872. ShowWarning("Removed invalid/disabled item (ID: %u, amount: %d) from cart (char_id: %d).\n", nameid, sd->cart.u.items_cart[i].amount, sd->status.char_id);
  10873. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10874. continue;
  10875. }
  10876. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10877. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10878. }
  10879. }
  10880. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10881. for(i = 0; i < sd->storage.max_amount; i++) {
  10882. nameid = sd->storage.u.items_storage[i].nameid;
  10883. if (!nameid)
  10884. continue;
  10885. if (!itemdb_available(nameid)) {
  10886. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10887. clif_displaymessage(sd->fd, output);
  10888. ShowWarning("Removed invalid/disabled item (ID: %u, amount: %d) from storage (char_id: %d).\n", nameid, sd->storage.u.items_storage[i].amount, sd->status.char_id);
  10889. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10890. continue;
  10891. }
  10892. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10893. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10894. }
  10895. }
  10896. }
  10897. /*==========================================
  10898. * Update PVP rank for sd1 in cmp to sd2
  10899. *------------------------------------------*/
  10900. static int32 pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10901. {
  10902. map_session_data *sd1,*sd2;
  10903. sd1=(map_session_data *)bl;
  10904. sd2=va_arg(ap,map_session_data *);
  10905. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10906. {// cannot register pvp rank for hidden GMs
  10907. return 0;
  10908. }
  10909. if( sd1->pvp_point > sd2->pvp_point )
  10910. sd2->pvp_rank++;
  10911. return 0;
  10912. }
  10913. /*==========================================
  10914. * Calculate new rank beetween all present players (map_foreachinallarea)
  10915. * and display result
  10916. *------------------------------------------*/
  10917. int32 pc_calc_pvprank(map_session_data *sd)
  10918. {
  10919. int32 old = sd->pvp_rank;
  10920. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10921. sd->pvp_rank=1;
  10922. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10923. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10924. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10925. return sd->pvp_rank;
  10926. }
  10927. /*==========================================
  10928. * Calculate next sd ranking calculation from config
  10929. *------------------------------------------*/
  10930. TIMER_FUNC(pc_calc_pvprank_timer){
  10931. map_session_data *sd;
  10932. sd=map_id2sd(id);
  10933. if(sd==nullptr)
  10934. return 0;
  10935. sd->pvp_timer = INVALID_TIMER;
  10936. if( pc_isinvisible(sd) )
  10937. {// do not calculate the pvp rank for a hidden GM
  10938. return 0;
  10939. }
  10940. if( pc_calc_pvprank(sd) > 0 )
  10941. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10942. return 0;
  10943. }
  10944. /*==========================================
  10945. * Checking if sd is married
  10946. * Return:
  10947. * partner_id = yes
  10948. * 0 = no
  10949. *------------------------------------------*/
  10950. int32 pc_ismarried(map_session_data *sd)
  10951. {
  10952. if(sd == nullptr)
  10953. return -1;
  10954. if(sd->status.partner_id > 0)
  10955. return sd->status.partner_id;
  10956. else
  10957. return 0;
  10958. }
  10959. /*==========================================
  10960. * Marry player sd to player dstsd
  10961. * Return:
  10962. * false = fail
  10963. * true = success
  10964. *------------------------------------------*/
  10965. bool pc_marriage(map_session_data *sd,map_session_data *dstsd)
  10966. {
  10967. if(sd == nullptr || dstsd == nullptr ||
  10968. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10969. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10970. return false;
  10971. sd->status.partner_id = dstsd->status.char_id;
  10972. dstsd->status.partner_id = sd->status.char_id;
  10973. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10974. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10975. return true;
  10976. }
  10977. /*==========================================
  10978. * Divorce sd from its partner
  10979. * Return:
  10980. * false = fail
  10981. * true = success
  10982. *------------------------------------------*/
  10983. bool pc_divorce(map_session_data *sd)
  10984. {
  10985. map_session_data *p_sd;
  10986. int32 i;
  10987. if( sd == nullptr || !pc_ismarried(sd) )
  10988. return false;
  10989. if( !sd->status.partner_id )
  10990. return false; // Char is not married
  10991. if( (p_sd = map_charid2sd(sd->status.partner_id)) == nullptr )
  10992. { // Lets char server do the divorce
  10993. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10994. return false; // No char server connected
  10995. return true;
  10996. }
  10997. // Both players online, lets do the divorce manually
  10998. sd->status.partner_id = 0;
  10999. p_sd->status.partner_id = 0;
  11000. for( i = 0; i < MAX_INVENTORY; i++ )
  11001. {
  11002. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  11003. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  11004. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  11005. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  11006. }
  11007. clif_divorced(sd, p_sd->status.name);
  11008. clif_divorced(p_sd, sd->status.name);
  11009. return true;
  11010. }
  11011. /**
  11012. * Get the partner map_session_data of a player
  11013. * @param sd : the husband|wife session
  11014. * @return partner session or nullptr
  11015. */
  11016. map_session_data *pc_get_partner(map_session_data *sd){
  11017. if (!sd || !pc_ismarried(sd))
  11018. return nullptr;
  11019. return map_charid2sd(sd->status.partner_id);
  11020. }
  11021. /**
  11022. * Get the father map_session_data of a player
  11023. * @param sd : the baby session
  11024. * @return father session or nullptr
  11025. */
  11026. map_session_data *pc_get_father (map_session_data *sd){
  11027. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  11028. return nullptr;
  11029. return map_charid2sd(sd->status.father);
  11030. }
  11031. /**
  11032. * Get the mother map_session_data of a player
  11033. * @param sd : the baby session
  11034. * @return mother session or nullptr
  11035. */
  11036. map_session_data *pc_get_mother (map_session_data *sd){
  11037. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  11038. return nullptr;
  11039. return map_charid2sd(sd->status.mother);
  11040. }
  11041. /*==========================================
  11042. * Get sd children charid. (Need to be married)
  11043. *------------------------------------------*/
  11044. map_session_data *pc_get_child (map_session_data *sd)
  11045. {
  11046. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  11047. // charid2sd returns nullptr if not found
  11048. return nullptr;
  11049. return map_charid2sd(sd->status.child);
  11050. }
  11051. /*==========================================
  11052. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  11053. *------------------------------------------*/
  11054. void pc_bleeding (map_session_data *sd, t_tick diff_tick)
  11055. {
  11056. int32 hp = 0, sp = 0;
  11057. if( pc_isdead(sd) )
  11058. return;
  11059. if (sd->hp_loss.value) {
  11060. sd->hp_loss.tick += diff_tick;
  11061. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  11062. hp += sd->hp_loss.value;
  11063. sd->hp_loss.tick -= sd->hp_loss.rate;
  11064. }
  11065. if(hp >= sd->battle_status.hp)
  11066. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  11067. }
  11068. if (sd->sp_loss.value) {
  11069. sd->sp_loss.tick += diff_tick;
  11070. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  11071. sp += sd->sp_loss.value;
  11072. sd->sp_loss.tick -= sd->sp_loss.rate;
  11073. }
  11074. }
  11075. if (hp > 0 || sp > 0)
  11076. status_zap(&sd->bl, hp, sp);
  11077. }
  11078. //Character regen. Flag is used to know which types of regen can take place.
  11079. //&1: HP regen
  11080. //&2: SP regen
  11081. void pc_regen (map_session_data *sd, t_tick diff_tick)
  11082. {
  11083. int32 hp = 0, sp = 0;
  11084. if (sd->hp_regen.value) {
  11085. sd->hp_regen.tick += diff_tick;
  11086. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  11087. hp += sd->hp_regen.value;
  11088. sd->hp_regen.tick -= sd->hp_regen.rate;
  11089. }
  11090. }
  11091. if (sd->sp_regen.value) {
  11092. sd->sp_regen.tick += diff_tick;
  11093. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  11094. sp += sd->sp_regen.value;
  11095. sd->sp_regen.tick -= sd->sp_regen.rate;
  11096. }
  11097. }
  11098. if (sd->percent_hp_regen.value) {
  11099. sd->percent_hp_regen.tick += diff_tick;
  11100. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  11101. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  11102. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  11103. }
  11104. }
  11105. if (sd->percent_sp_regen.value) {
  11106. sd->percent_sp_regen.tick += diff_tick;
  11107. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  11108. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  11109. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  11110. }
  11111. }
  11112. if (hp > 0 || sp > 0)
  11113. status_heal(&sd->bl, hp, sp, 0);
  11114. }
  11115. /*==========================================
  11116. * Memo player sd savepoint. (map,x,y)
  11117. *------------------------------------------*/
  11118. void pc_setsavepoint(map_session_data *sd, short mapindex,int32 x,int32 y)
  11119. {
  11120. nullpo_retv(sd);
  11121. safestrncpy( sd->status.save_point.map, mapindex_id2name( mapindex ), sizeof( sd->status.save_point.map ) );
  11122. sd->status.save_point.x = x;
  11123. sd->status.save_point.y = y;
  11124. }
  11125. /*==========================================
  11126. * Save 1 player data at autosave interval
  11127. *------------------------------------------*/
  11128. static TIMER_FUNC(pc_autosave){
  11129. int32 interval;
  11130. struct s_mapiterator* iter;
  11131. map_session_data* sd;
  11132. static int32 last_save_id = 0, save_flag = 0;
  11133. if(save_flag == 2) //Someone was saved on last call, normal cycle
  11134. save_flag = 0;
  11135. else
  11136. save_flag = 1; //Noone was saved, so save first found char.
  11137. iter = mapit_getallusers();
  11138. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11139. {
  11140. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  11141. continue;
  11142. if(sd->bl.id == last_save_id && save_flag != 1) {
  11143. save_flag = 1;
  11144. continue;
  11145. }
  11146. if(save_flag != 1) //Not our turn to save yet.
  11147. continue;
  11148. //Save char.
  11149. last_save_id = sd->bl.id;
  11150. save_flag = 2;
  11151. if (pc_isvip(sd)) // Check if we're still VIP
  11152. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  11153. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  11154. break;
  11155. }
  11156. mapit_free(iter);
  11157. interval = autosave_interval/(map_usercount()+1);
  11158. if(interval < minsave_interval)
  11159. interval = minsave_interval;
  11160. add_timer(gettick()+interval,pc_autosave,0,0);
  11161. return 0;
  11162. }
  11163. static int32 pc_daynight_timer_sub(map_session_data *sd,va_list ap)
  11164. {
  11165. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  11166. { //Night/day state does not match.
  11167. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  11168. sd->state.night = night_flag;
  11169. return 1;
  11170. }
  11171. return 0;
  11172. }
  11173. /*================================================
  11174. * timer to do the day [Yor]
  11175. * data: 0 = called by timer, 1 = gmcommand/script
  11176. *------------------------------------------------*/
  11177. TIMER_FUNC(map_day_timer){
  11178. char tmp_soutput[1024];
  11179. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  11180. return 0;
  11181. if (!night_flag)
  11182. return 0; //Already day.
  11183. night_flag = 0; // 0=day, 1=night [Yor]
  11184. map_foreachpc(pc_daynight_timer_sub);
  11185. strcpy(tmp_soutput, (data == 0) ? msg_txt(nullptr,502) : msg_txt(nullptr,60)); // The day has arrived!
  11186. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11187. return 0;
  11188. }
  11189. /*================================================
  11190. * timer to do the night [Yor]
  11191. * data: 0 = called by timer, 1 = gmcommand/script
  11192. *------------------------------------------------*/
  11193. TIMER_FUNC(map_night_timer){
  11194. char tmp_soutput[1024];
  11195. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  11196. return 0;
  11197. if (night_flag)
  11198. return 0; //Already nigth.
  11199. night_flag = 1; // 0=day, 1=night [Yor]
  11200. map_foreachpc(pc_daynight_timer_sub);
  11201. strcpy(tmp_soutput, (data == 0) ? msg_txt(nullptr,503) : msg_txt(nullptr,59)); // The night has fallen...
  11202. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11203. return 0;
  11204. }
  11205. /**
  11206. * Attempt to stand up a player
  11207. * @param sd
  11208. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  11209. * @return True if success, Fals if failed
  11210. */
  11211. bool pc_setstand(map_session_data *sd, bool force){
  11212. nullpo_ret(sd);
  11213. // Cannot stand yet
  11214. // TODO: Move to SCS_NOSTAND [Cydh]
  11215. if (!force && (sd->sc.getSCE(SC_SITDOWN_FORCE) || sd->sc.getSCE(SC_BANANA_BOMB_SITDOWN)))
  11216. return false;
  11217. status_change_end(&sd->bl, SC_TENSIONRELAX);
  11218. clif_status_load(&sd->bl,EFST_SIT,0);
  11219. clif_standing(sd->bl); //Inform area PC is standing
  11220. //Reset sitting tick.
  11221. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  11222. if( pc_isdead( sd ) ){
  11223. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11224. clif_party_dead( *sd );
  11225. }else{
  11226. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11227. }
  11228. return true;
  11229. }
  11230. /**
  11231. * Calculate Overheat value
  11232. * @param sd: Player data
  11233. * @param heat: Amount of Heat to adjust
  11234. **/
  11235. void pc_overheat(map_session_data &sd, int16 heat) {
  11236. status_change_entry *sce = sd.sc.getSCE(SC_OVERHEAT_LIMITPOINT);
  11237. if (sce) {
  11238. sce->val1 += heat;
  11239. sce->val1 = cap_value(sce->val1, 0, 1000);
  11240. if (heat < 0 && sce->val1 == 0) { // Cooling device used.
  11241. status_change_end(&sd.bl, SC_OVERHEAT_LIMITPOINT);
  11242. status_change_end(&sd.bl, SC_OVERHEAT);
  11243. }
  11244. } else if (heat > 0)
  11245. sc_start(&sd.bl, &sd.bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  11246. }
  11247. /**
  11248. * Check if player is autolooting given itemID.
  11249. */
  11250. bool pc_isautolooting(map_session_data *sd, t_itemid nameid)
  11251. {
  11252. uint8 i = 0;
  11253. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  11254. return true;
  11255. if (!sd->state.autolooting)
  11256. return false;
  11257. if (sd->state.autolooting)
  11258. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  11259. return (i != AUTOLOOTITEM_SIZE);
  11260. }
  11261. /**
  11262. * Checks if player can use @/#command
  11263. * @param sd Player map session data
  11264. * @param command Command name without @/# and params
  11265. * @param type is it atcommand or charcommand
  11266. */
  11267. bool pc_can_use_command( map_session_data *sd, const char *command, AtCommandType type ){
  11268. return sd->group->can_use_command( command, type );
  11269. }
  11270. bool pc_has_permission( map_session_data* sd, e_pc_permission permission ){
  11271. return sd->permissions.test( permission );
  11272. }
  11273. /**
  11274. * Checks if commands used by a player should be logged
  11275. * according to their group setting.
  11276. * @param sd Player map session data
  11277. */
  11278. bool pc_should_log_commands( map_session_data *sd ){
  11279. return sd->group->log_commands;
  11280. }
  11281. /**
  11282. * Spirit Charm expiration timer.
  11283. * @see TimerFunc
  11284. */
  11285. static TIMER_FUNC(pc_spiritcharm_timer){
  11286. map_session_data *sd;
  11287. int32 i;
  11288. if ((sd = (map_session_data *)map_id2sd(id)) == nullptr || sd->bl.type != BL_PC)
  11289. return 1;
  11290. if (sd->spiritcharm <= 0) {
  11291. ShowError("pc_spiritcharm_timer: %d spiritcharm's available. (aid=%d cid=%d tid=%d)\n", sd->spiritcharm, sd->status.account_id, sd->status.char_id, tid);
  11292. sd->spiritcharm = 0;
  11293. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11294. return 0;
  11295. }
  11296. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  11297. if (i == sd->spiritcharm) {
  11298. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  11299. return 0;
  11300. }
  11301. sd->spiritcharm--;
  11302. if (i != sd->spiritcharm)
  11303. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int32));
  11304. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11305. if (sd->spiritcharm <= 0)
  11306. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11307. clif_spiritcharm( *sd );
  11308. return 0;
  11309. }
  11310. /**
  11311. * Adds a spirit charm.
  11312. * @param sd: Target character
  11313. * @param interval: Duration
  11314. * @param max: Maximum amount of charms to add
  11315. * @param type: Charm type (@see spirit_charm_types)
  11316. */
  11317. void pc_addspiritcharm(map_session_data *sd, int32 interval, int32 max, int32 type)
  11318. {
  11319. int32 tid, i;
  11320. nullpo_retv(sd);
  11321. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  11322. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  11323. if (max > MAX_SPIRITCHARM)
  11324. max = MAX_SPIRITCHARM;
  11325. if (sd->spiritcharm < 0)
  11326. sd->spiritcharm = 0;
  11327. if (sd->spiritcharm && sd->spiritcharm >= max) {
  11328. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  11329. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  11330. sd->spiritcharm--;
  11331. if (sd->spiritcharm != 0)
  11332. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int32));
  11333. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11334. }
  11335. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  11336. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == INVALID_TIMER || DIFF_TICK(get_timer(tid)->tick, get_timer(sd->spiritcharm_timer[i])->tick) < 0);
  11337. if (i != sd->spiritcharm)
  11338. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int32));
  11339. sd->spiritcharm_timer[i] = tid;
  11340. sd->spiritcharm++;
  11341. sd->spiritcharm_type = type;
  11342. clif_spiritcharm( *sd );
  11343. }
  11344. /**
  11345. * Removes one or more spirit charms.
  11346. * @param sd: The target character
  11347. * @param count: Amount of charms to remove
  11348. * @param type: Type of charm to remove
  11349. */
  11350. void pc_delspiritcharm(map_session_data *sd, int32 count, int32 type)
  11351. {
  11352. int32 i;
  11353. nullpo_retv(sd);
  11354. if (sd->spiritcharm_type != type)
  11355. return;
  11356. if (sd->spiritcharm <= 0) {
  11357. sd->spiritcharm = 0;
  11358. return;
  11359. }
  11360. if (count <= 0)
  11361. return;
  11362. if (count > sd->spiritcharm)
  11363. count = sd->spiritcharm;
  11364. sd->spiritcharm -= count;
  11365. if (count > MAX_SPIRITCHARM)
  11366. count = MAX_SPIRITCHARM;
  11367. for (i = 0; i < count; i++) {
  11368. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  11369. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  11370. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11371. }
  11372. }
  11373. for (i = count; i < MAX_SPIRITCHARM; i++) {
  11374. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  11375. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11376. }
  11377. if (sd->spiritcharm <= 0)
  11378. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11379. clif_spiritcharm( *sd );
  11380. }
  11381. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11382. /**
  11383. * Renewal EXP/Item Drop rate modifier based on level penalty
  11384. * @param level_diff: Monster and Player level difference
  11385. * @param mob_class: Monster class
  11386. * @param mode: Monster mode
  11387. * @param type: 1 - EXP, 2 - Item Drop
  11388. * @return Penalty rate
  11389. */
  11390. uint16 pc_level_penalty_mod( map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  11391. // No player was attached, we don't use any modifier (100 = rates are not touched)
  11392. if( sd == nullptr ){
  11393. return 100;
  11394. }
  11395. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  11396. return 100;
  11397. }
  11398. int32 monster_level;
  11399. if( md != nullptr ){
  11400. monster_level = md->level;
  11401. mob = md->db;
  11402. }else if( mob != nullptr ){
  11403. monster_level = mob->lv;
  11404. }else{
  11405. return 100;
  11406. }
  11407. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  11408. return 100;
  11409. }
  11410. int32 level_difference = monster_level - sd->status.base_level;
  11411. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  11412. if( penalty != nullptr ){
  11413. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  11414. }else{
  11415. return 100;
  11416. }
  11417. }
  11418. #endif
  11419. int32 pc_split_str(char *str,char **val,int32 num)
  11420. {
  11421. int32 i;
  11422. for (i=0; i<num && str; i++){
  11423. val[i] = str;
  11424. str = strchr(str,',');
  11425. if (str && i<num-1) //Do not remove a trailing comma.
  11426. *str++=0;
  11427. }
  11428. return i;
  11429. }
  11430. int32 pc_split_atoui(char* str, uint32* val, char sep, int32 max)
  11431. {
  11432. static int32 warning=0;
  11433. int32 i,j;
  11434. for (i=0; i<max; i++) {
  11435. double f;
  11436. if (!str) break;
  11437. f = atof(str);
  11438. if (f < 0)
  11439. val[i] = 0;
  11440. else if (f > UINT_MAX) {
  11441. val[i] = UINT_MAX;
  11442. if (!warning) {
  11443. warning = 1;
  11444. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  11445. }
  11446. } else
  11447. val[i] = (uint32)f;
  11448. str = strchr(str,sep);
  11449. if (str)
  11450. *str++=0;
  11451. }
  11452. //Zero up the remaining.
  11453. for(j=i; j < max; j++)
  11454. val[j] = 0;
  11455. return i;
  11456. }
  11457. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int32 class_, uint16 skill_id) {
  11458. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  11459. if (tree != nullptr)
  11460. return util::umap_find(tree->skills, skill_id);
  11461. return nullptr;
  11462. }
  11463. const std::string SkillTreeDatabase::getDefaultLocation() {
  11464. return std::string(db_path) + "/skill_tree.yml";
  11465. }
  11466. /**
  11467. * Reads and parses an entry from the skill_tree.
  11468. * @param node: YAML node containing the entry.
  11469. * @return count of successfully parsed rows
  11470. */
  11471. uint64 SkillTreeDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11472. std::string job_name;
  11473. if (!this->asString(node, "Job", job_name))
  11474. return 0;
  11475. int64 constant;
  11476. std::string job_name_constant = "JOB_" + job_name;
  11477. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11478. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  11479. return 0;
  11480. }
  11481. uint16 job_id = static_cast<uint16>(constant);
  11482. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  11483. bool exists = tree != nullptr;
  11484. if (!exists)
  11485. tree = std::make_shared<s_skill_tree>();
  11486. if (this->nodeExists(node, "Inherit")) {
  11487. const ryml::NodeRef& InheritNode = node["Inherit"];
  11488. for (const auto &Inheritit : InheritNode) {
  11489. std::string inheritname;
  11490. c4::from_chars(Inheritit.key(), &inheritname);
  11491. std::string inheritname_constant = "JOB_" + inheritname;
  11492. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11493. this->invalidWarning(InheritNode[Inheritit.key()], "Invalid job %s.\n", inheritname.c_str());
  11494. return 0;
  11495. }
  11496. bool active;
  11497. if (!this->asBool(InheritNode, inheritname, active))
  11498. return 0;
  11499. uint16 inherit_job = static_cast<uint16>(constant);
  11500. if (!active) {
  11501. if (exists)
  11502. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  11503. }
  11504. else {
  11505. if (!util::vector_exists(tree->inherit_job, inherit_job))
  11506. tree->inherit_job.push_back(inherit_job);
  11507. }
  11508. }
  11509. }
  11510. if (this->nodeExists(node, "Tree")) {
  11511. for (const auto &it : node["Tree"]) {
  11512. std::string skill_name;
  11513. if (!this->asString(it, "Name", skill_name))
  11514. return 0;
  11515. uint16 skill_id = skill_name2id(skill_name.c_str());
  11516. if (skill_id == 0) {
  11517. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  11518. return 0;
  11519. }
  11520. if (!skill_get_index(skill_id)) {
  11521. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  11522. return 0;
  11523. }
  11524. std::shared_ptr<s_skill_tree_entry> entry;
  11525. bool skill_exists = tree->skills.count(skill_id) > 0;
  11526. if (skill_exists)
  11527. entry = tree->skills[skill_id];
  11528. else
  11529. entry = std::make_shared<s_skill_tree_entry>();
  11530. entry->skill_id = skill_id;
  11531. uint16 max_lv;
  11532. if (!this->asUInt16(it, "MaxLevel", max_lv))
  11533. return 0;
  11534. if (max_lv > MAX_SKILL_LEVEL) {
  11535. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11536. return 0;
  11537. }
  11538. uint16 skill_lv_max = skill_get_max(skill_id);
  11539. if (max_lv > skill_lv_max) {
  11540. this->invalidWarning(it["MaxLevel"], "Skill %s's level %hu exceeds the skill's max level %hu. Capping skill level.\n", skill_name.c_str(), max_lv, skill_lv_max);
  11541. max_lv = skill_lv_max;
  11542. }
  11543. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11544. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11545. // this->invalidWarning(it["Name"], "Failed to erase %s, the skill doesn't exist in for job %s, skipping.\n", skill_name.c_str(), job_name.c_str());
  11546. // continue;
  11547. // }
  11548. entry->max_lv = max_lv;
  11549. if (this->nodeExists(it, "BaseLevel")) {
  11550. uint32 baselv;
  11551. if (!this->asUInt32(it, "BaseLevel", baselv))
  11552. return 0;
  11553. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11554. if (baselv > baselv_max) {
  11555. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11556. skill_id, baselv, job_name.c_str(), baselv_max);
  11557. baselv = baselv_max;
  11558. }
  11559. entry->baselv = baselv;
  11560. } else {
  11561. if (!skill_exists)
  11562. entry->baselv = 0;
  11563. }
  11564. if (this->nodeExists(it, "JobLevel")) {
  11565. uint32 joblv;
  11566. if (!this->asUInt32(it, "JobLevel", joblv))
  11567. return 0;
  11568. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11569. if (joblv > joblv_max) {
  11570. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11571. skill_id, joblv, job_name.c_str(), joblv_max);
  11572. joblv = joblv_max;
  11573. }
  11574. entry->joblv = joblv;
  11575. } else {
  11576. if (!skill_exists)
  11577. entry->joblv = 0;
  11578. }
  11579. if (this->nodeExists(it, "Requires")) {
  11580. for (const auto &Requiresit : it["Requires"]) {
  11581. if (!this->nodesExist(Requiresit, { "Name" }))
  11582. return 0;
  11583. std::string skill_name_req;
  11584. if (!this->asString(Requiresit, "Name", skill_name_req))
  11585. return 0;
  11586. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11587. if (skill_id_req == 0) {
  11588. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11589. return 0;
  11590. }
  11591. uint16 lv_req;
  11592. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11593. return 0;
  11594. if (lv_req > MAX_SKILL_LEVEL) {
  11595. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11596. return 0;
  11597. }
  11598. uint16 lv_req_max = skill_get_max(skill_id_req);
  11599. if (lv_req > lv_req_max) {
  11600. this->invalidWarning(it["MaxLevel"], "Required skill %s's level %hu exceeds the skill's max level %hu. Capping skill level.\n", skill_name.c_str(), lv_req, lv_req_max);
  11601. lv_req = lv_req_max;
  11602. }
  11603. if (lv_req == 0) {
  11604. if (entry->need.erase(skill_id_req) == 0)
  11605. this->invalidWarning(Requiresit["Name"], "Failed to erase %s, the skill doesn't exist in for job %s, skipping.\n", skill_name_req.c_str(), job_name.c_str());
  11606. continue;
  11607. }
  11608. entry->need[skill_id_req] = lv_req;
  11609. }
  11610. }
  11611. if (this->nodeExists(it, "Exclude")) {
  11612. bool exclude;
  11613. if (!this->asBool(it, "Exclude", exclude))
  11614. return 0;
  11615. entry->exclude_inherit = exclude;
  11616. } else {
  11617. if (!skill_exists)
  11618. entry->exclude_inherit = false;
  11619. }
  11620. if (!skill_exists)
  11621. tree->skills.insert({ skill_id, entry });
  11622. }
  11623. }
  11624. if (!exists)
  11625. this->put(job_id, tree);
  11626. return true;
  11627. }
  11628. void SkillTreeDatabase::loadingFinished() {
  11629. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11630. for (auto &data : *this) {
  11631. if (data.second->inherit_job.empty())
  11632. continue;
  11633. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11634. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11635. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11636. for (const auto &inherit_job : data.second->inherit_job) {
  11637. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11638. if (tree == nullptr || tree->skills.empty())
  11639. continue;
  11640. for (const auto &it : tree->skills) {
  11641. if (it.second->exclude_inherit)
  11642. continue;
  11643. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11644. continue;
  11645. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11646. skill_tree->skills[it.first] = it.second;
  11647. else
  11648. skill_tree->skills.insert({ it.first, it.second });
  11649. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11650. if (skill->baselv > baselv_max) {
  11651. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11652. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11653. skill->baselv = baselv_max;
  11654. }
  11655. if (skill->joblv > joblv_max) {
  11656. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11657. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11658. skill->joblv = joblv_max;
  11659. }
  11660. }
  11661. }
  11662. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11663. job_tree.insert({ data.first, skill_tree });
  11664. }
  11665. if (!job_tree.empty()) {
  11666. for (auto &data : *this) {
  11667. if (job_tree.count(data.first) == 0)
  11668. continue;
  11669. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11670. }
  11671. }
  11672. // remove skills with max_lv = 0
  11673. for (const auto &job : *this) {
  11674. if (job.second->skills.empty())
  11675. continue;
  11676. auto it = job.second->skills.begin();
  11677. while( it != job.second->skills.end() ){
  11678. if( it->second->max_lv == 0 ){
  11679. it = job.second->skills.erase( it );
  11680. }else{
  11681. it++;
  11682. }
  11683. }
  11684. }
  11685. TypesafeYamlDatabase::loadingFinished();
  11686. }
  11687. /**
  11688. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  11689. * @param level: Base level of player
  11690. * @param job_id: Job ID @see enum e_job
  11691. * @return base_hp
  11692. * @author [Cydh]
  11693. */
  11694. static uint32 pc_calc_basehp(uint16 level, uint16 job_id) {
  11695. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11696. double base_hp = 35 + level * (job->hp_increase / 100.);
  11697. #ifndef RENEWAL
  11698. if (level >= 10 && (job_id == JOB_NINJA || job_id == JOB_GUNSLINGER))
  11699. base_hp += 90;
  11700. #endif
  11701. for (uint16 i = 2; i <= level; i++)
  11702. base_hp += floor(((job->hp_factor / 100.) * i) + 0.5); //Don't have round()
  11703. if (job_id == JOB_SUMMONER || job_id == JOB_SPIRIT_HANDLER)
  11704. base_hp += floor((base_hp / 2) + 0.5);
  11705. return (uint32)base_hp;
  11706. }
  11707. /**
  11708. * Calculates base sp of player.
  11709. * @param level: Base level of player
  11710. * @param job_id: Job ID @see enum e_job
  11711. * @return base_sp
  11712. * @author [Playtester]
  11713. */
  11714. static uint32 pc_calc_basesp(uint16 level, uint16 job_id) {
  11715. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11716. double base_sp = 10 + floor(level * (job->sp_increase / 100.));
  11717. switch (job_id) {
  11718. case JOB_NINJA:
  11719. if (level >= 10)
  11720. base_sp -= 22;
  11721. else
  11722. base_sp = 11 + 3*level;
  11723. break;
  11724. case JOB_GUNSLINGER:
  11725. if (level > 10)
  11726. base_sp -= 18;
  11727. else
  11728. base_sp = 9 + 3*level;
  11729. break;
  11730. case JOB_SUMMONER:
  11731. case JOB_SPIRIT_HANDLER:
  11732. base_sp -= floor(base_sp / 2);
  11733. break;
  11734. }
  11735. return (uint32)base_sp;
  11736. }
  11737. const std::string JobDatabase::getDefaultLocation() {
  11738. return std::string(db_path) + "/job_stats.yml";
  11739. }
  11740. /**
  11741. * Reads and parses an entry from the job_db.
  11742. * @param node: YAML node containing the entry.
  11743. * @return count of successfully parsed rows
  11744. */
  11745. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11746. if (this->nodeExists(node, "Jobs")) {
  11747. const ryml::NodeRef& jobsNode = node["Jobs"];
  11748. for (const auto &jobit : jobsNode) {
  11749. std::string job_name;
  11750. c4::from_chars(jobit.key(), &job_name);
  11751. std::string job_name_constant = "JOB_" + job_name;
  11752. int64 job_id;
  11753. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11754. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11755. return 0;
  11756. }
  11757. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11758. bool exists = job != nullptr;
  11759. if (!exists) {
  11760. job = std::make_shared<s_job_info>();
  11761. job->job_bonus.resize(MAX_LEVEL);
  11762. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11763. job->base_hp.resize(MAX_LEVEL);
  11764. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11765. job->base_sp.resize(MAX_LEVEL);
  11766. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11767. job->base_ap.resize(MAX_LEVEL);
  11768. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11769. }
  11770. if (this->nodeExists(node, "MaxWeight")) {
  11771. uint32 weight;
  11772. if (!this->asUInt32(node, "MaxWeight", weight))
  11773. return 0;
  11774. job->max_weight_base = weight;
  11775. } else {
  11776. if (!exists)
  11777. job->max_weight_base = 20000;
  11778. }
  11779. if (this->nodeExists(node, "HpFactor")) {
  11780. uint32 hp;
  11781. if (!this->asUInt32(node, "HpFactor", hp))
  11782. return 0;
  11783. job->hp_factor = hp;
  11784. } else {
  11785. if (!exists)
  11786. job->hp_factor = 0;
  11787. }
  11788. if (this->nodeExists(node, "HpIncrease")) {
  11789. uint32 hp;
  11790. if (!this->asUInt32(node, "HpIncrease", hp))
  11791. return 0;
  11792. job->hp_increase = hp;
  11793. } else {
  11794. if (!exists)
  11795. job->hp_increase = 500;
  11796. }
  11797. if (this->nodeExists(node, "SpIncrease")) {
  11798. uint32 sp;
  11799. if (!this->asUInt32(node, "SpIncrease", sp))
  11800. return 0;
  11801. job->sp_increase = sp;
  11802. } else {
  11803. if (!exists)
  11804. job->sp_increase = 100;
  11805. }
  11806. if (this->nodeExists(node, "BaseASPD")) {
  11807. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11808. uint8 max = MAX_WEAPON_TYPE;
  11809. #ifdef RENEWAL // Renewal adds an extra column for shields
  11810. max += 1;
  11811. #endif
  11812. if (!exists) {
  11813. job->aspd_base.resize(max);
  11814. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11815. }
  11816. for (const auto &aspdit : aspdNode) {
  11817. std::string weapon;
  11818. c4::from_chars(aspdit.key(), &weapon);
  11819. std::string weapon_constant = "W_" + weapon;
  11820. int64 constant;
  11821. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11822. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11823. continue;
  11824. }
  11825. if (constant < W_FIST || constant >= max) {
  11826. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11827. continue;
  11828. }
  11829. int16 aspd;
  11830. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11831. return 0;
  11832. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11833. }
  11834. } else {
  11835. if (!exists) {
  11836. uint8 max = MAX_WEAPON_TYPE;
  11837. #ifdef RENEWAL // Renewal adds an extra column for shields
  11838. max += 1;
  11839. #endif
  11840. job->aspd_base.resize(max);
  11841. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11842. }
  11843. }
  11844. if (this->nodeExists(node, "MaxStats")) {
  11845. const ryml::NodeRef& statNode = node["MaxStats"];
  11846. for (const auto &statit : statNode) {
  11847. std::string stat;
  11848. c4::from_chars(statit.key(), &stat);
  11849. std::string stat_constant = "PARAM_" + stat;
  11850. int64 constant;
  11851. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11852. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11853. continue;
  11854. }
  11855. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11856. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11857. continue;
  11858. }
  11859. uint16 max;
  11860. if (!this->asUInt16(statNode, stat.c_str(), max))
  11861. return 0;
  11862. job->max_param[constant] = max;
  11863. }
  11864. }
  11865. if (this->nodeExists(node, "MaxBaseLevel")) {
  11866. uint16 level;
  11867. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11868. return 0;
  11869. if (level == 0 || level > MAX_LEVEL) {
  11870. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11871. level = MAX_LEVEL;
  11872. }
  11873. job->max_base_level = level;
  11874. } else {
  11875. if (!exists)
  11876. job->max_base_level = MAX_LEVEL;
  11877. }
  11878. if (this->nodeExists(node, "BaseExp")) {
  11879. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11880. uint16 level;
  11881. if (!this->asUInt16(bexpNode, "Level", level))
  11882. return 0;
  11883. if (level > job->max_base_level)
  11884. continue;
  11885. if (level == 0 || level > MAX_LEVEL) {
  11886. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11887. return 0;
  11888. }
  11889. if (this->nodeExists(bexpNode, "Exp")) {
  11890. t_exp exp;
  11891. if (!this->asUInt64(bexpNode, "Exp", exp))
  11892. return 0;
  11893. job->base_exp[level - 1] = exp;
  11894. }
  11895. }
  11896. }
  11897. if (this->nodeExists(node, "MaxJobLevel")) {
  11898. uint16 level;
  11899. if (!this->asUInt16(node, "MaxJobLevel", level))
  11900. return 0;
  11901. if (level == 0 || level > MAX_LEVEL) {
  11902. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11903. level = MAX_LEVEL;
  11904. }
  11905. job->max_job_level = level;
  11906. } else {
  11907. if (!exists)
  11908. job->max_job_level = MAX_LEVEL;
  11909. }
  11910. if (this->nodeExists(node, "JobExp")) {
  11911. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11912. uint16 level;
  11913. if (!this->asUInt16(jexpNode, "Level", level))
  11914. return 0;
  11915. if (level > job->max_job_level)
  11916. continue;
  11917. if (level == 0 || level > MAX_LEVEL) {
  11918. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11919. return 0;
  11920. }
  11921. if (this->nodeExists(jexpNode, "Exp")) {
  11922. t_exp exp;
  11923. if (!this->asUInt64(jexpNode, "Exp", exp))
  11924. return 0;
  11925. job->job_exp[level - 1] = exp;
  11926. }
  11927. }
  11928. }
  11929. if (this->nodeExists(node, "BonusStats")) {
  11930. const ryml::NodeRef& bonusNode = node["BonusStats"];
  11931. for (const ryml::NodeRef& levelNode : bonusNode) {
  11932. uint16 level;
  11933. if (!this->asUInt16(levelNode, "Level", level))
  11934. return 0;
  11935. if (level == 0 || level > MAX_LEVEL) {
  11936. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11937. return 0;
  11938. }
  11939. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  11940. if (this->nodeExists(levelNode, parameter_names[idx])) {
  11941. int16 change;
  11942. if (!this->asInt16(levelNode, parameter_names[idx], change))
  11943. return 0;
  11944. job->job_bonus[level - 1][idx] = change;
  11945. }
  11946. }
  11947. }
  11948. }
  11949. #ifdef HP_SP_TABLES
  11950. if (this->nodeExists(node, "BaseHp")) {
  11951. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  11952. uint16 level;
  11953. if (!this->asUInt16(bhpNode, "Level", level))
  11954. return 0;
  11955. if (level > job->max_base_level)
  11956. continue;
  11957. if (level == 0 || level > MAX_LEVEL) {
  11958. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11959. return 0;
  11960. }
  11961. if (this->nodeExists(bhpNode, "Hp")) {
  11962. uint32 points;
  11963. if (!this->asUInt32(bhpNode, "Hp", points))
  11964. return 0;
  11965. job->base_hp[level - 1] = points;
  11966. }
  11967. }
  11968. }
  11969. if (this->nodeExists(node, "BaseSp")) {
  11970. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  11971. uint16 level;
  11972. if (!this->asUInt16(bspNode, "Level", level))
  11973. return 0;
  11974. if (level > job->max_base_level)
  11975. continue;
  11976. if (level == 0 || level > MAX_LEVEL) {
  11977. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11978. return 0;
  11979. }
  11980. if (this->nodeExists(bspNode, "Sp")) {
  11981. uint32 points;
  11982. if (!this->asUInt32(bspNode, "Sp", points))
  11983. return 0;
  11984. job->base_sp[level - 1] = points;
  11985. }
  11986. }
  11987. }
  11988. if (this->nodeExists(node, "BaseAp")) {
  11989. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  11990. uint16 level;
  11991. if (!this->asUInt16(bapNode, "Level", level))
  11992. return 0;
  11993. if (level > job->max_base_level)
  11994. continue;
  11995. if (level == 0 || level > MAX_LEVEL) {
  11996. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11997. return 0;
  11998. }
  11999. if (this->nodeExists(bapNode, "Ap")) {
  12000. uint32 points;
  12001. if (!this->asUInt32(bapNode, "Ap", points))
  12002. return 0;
  12003. job->base_ap[level - 1] = points;
  12004. }
  12005. }
  12006. }
  12007. #endif
  12008. if (!exists)
  12009. this->put(static_cast<uint16>(job_id), job);
  12010. }
  12011. }
  12012. return 1;
  12013. }
  12014. void JobDatabase::loadingFinished() {
  12015. // Checking if all class have their data
  12016. for (auto &jobIt : *this) {
  12017. uint16 job_id = jobIt.first;
  12018. if (!pcdb_checkid(job_id))
  12019. continue;
  12020. if (job_id == JOB_WEDDING || job_id == JOB_XMAS || job_id == JOB_SUMMER || job_id == JOB_HANBOK || job_id == JOB_OKTOBERFEST || job_id == JOB_SUMMER2)
  12021. continue; // Classes that do not need exp tables.
  12022. std::shared_ptr<s_job_info> job = jobIt.second;
  12023. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  12024. if (!maxBaseLv)
  12025. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  12026. if (!maxJobLv)
  12027. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  12028. // Init and checking the empty value of Base HP/SP [Cydh]
  12029. if (job->base_hp.empty())
  12030. job->base_hp.resize(maxBaseLv);
  12031. for (uint16 j = 0; j < maxBaseLv; j++) {
  12032. if (job->base_hp[j] == 0)
  12033. job->base_hp[j] = pc_calc_basehp(j + 1, job_id);
  12034. }
  12035. if (job->base_sp.empty())
  12036. job->base_sp.resize(maxBaseLv);
  12037. for (uint16 j = 0; j < maxBaseLv; j++) {
  12038. if (job->base_sp[j] == 0)
  12039. job->base_sp[j] = pc_calc_basesp(j + 1, job_id);
  12040. }
  12041. // Resize to the maximum base level
  12042. if (job->base_hp.capacity() > maxBaseLv)
  12043. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  12044. if (job->base_sp.capacity() > maxBaseLv)
  12045. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  12046. if (job->base_ap.capacity() > maxBaseLv)
  12047. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  12048. // Resize to the maximum job level
  12049. if (job->job_bonus.capacity() > maxJobLv)
  12050. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  12051. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  12052. // Store total
  12053. int16 current = 0;
  12054. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  12055. // Add the bonus from this job level
  12056. current += job->job_bonus[job_level][parameter];
  12057. // Set the new total on this job level
  12058. job->job_bonus[job_level][parameter] = current;
  12059. }
  12060. }
  12061. uint64 class_ = pc_jobid2mapid( job_id );
  12062. // Set normal status limits
  12063. uint16 max = battle_config.max_parameter;
  12064. do{
  12065. // Always check babies first
  12066. if( class_ & JOBL_BABY ){
  12067. if( class_ & JOBL_THIRD ){
  12068. max = battle_config.max_baby_third_parameter;
  12069. break;
  12070. }else{
  12071. max = battle_config.max_baby_parameter;
  12072. break;
  12073. }
  12074. }
  12075. // Summoner
  12076. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  12077. max = battle_config.max_summoner_parameter;
  12078. break;
  12079. }
  12080. // Extended classes
  12081. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  12082. max = battle_config.max_extended_parameter;
  12083. break;
  12084. }
  12085. if( class_ & JOBL_FOURTH ){
  12086. max = battle_config.max_fourth_parameter;
  12087. break;
  12088. }
  12089. // 3rd class
  12090. if( class_ & JOBL_THIRD ){
  12091. // Transcendent
  12092. if( class_ & JOBL_UPPER ){
  12093. max = battle_config.max_third_trans_parameter;
  12094. break;
  12095. }else{
  12096. max = battle_config.max_third_parameter;
  12097. break;
  12098. }
  12099. }
  12100. // Transcendent
  12101. if( class_ & JOBL_UPPER ){
  12102. max = battle_config.max_trans_parameter;
  12103. break;
  12104. }
  12105. }while( false );
  12106. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  12107. // If it is not explicitly set in the database file
  12108. if( job->max_param[parameter] == 0 ){
  12109. job->max_param[parameter] = max;
  12110. }
  12111. }
  12112. // Set trait status limit
  12113. if( class_ & JOBL_FOURTH ){
  12114. max = battle_config.max_trait_parameter;
  12115. }else{
  12116. max = 0;
  12117. }
  12118. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  12119. // If it is not explicitly set in the database file
  12120. if( job->max_param[parameter] == 0 ){
  12121. job->max_param[parameter] = max;
  12122. }
  12123. }
  12124. }
  12125. TypesafeCachedYamlDatabase::loadingFinished();
  12126. }
  12127. /**
  12128. * Read job_noenter_map.txt
  12129. **/
  12130. static bool pc_readdb_job_noenter_map( char *str[], size_t columns, size_t current ){
  12131. int32 class_ = -1;
  12132. int64 class_tmp;
  12133. if (ISDIGIT(str[0][0])) {
  12134. class_ = atoi(str[0]);
  12135. } else {
  12136. if (!script_get_constant(str[0], &class_tmp)) {
  12137. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  12138. return false;
  12139. }
  12140. class_ = static_cast<int32>(class_tmp);
  12141. }
  12142. if (!pcdb_checkid(class_)) {
  12143. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  12144. return false;
  12145. }
  12146. std::shared_ptr<s_job_info> job = job_db.find(class_);
  12147. if (job == nullptr) {
  12148. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  12149. return false;
  12150. }
  12151. job->noenter_map.zone = atoi(str[1]);
  12152. job->noenter_map.group_lv = atoi(str[2]);
  12153. return true;
  12154. }
  12155. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  12156. return std::string(db_path) + "/statpoint.yml";
  12157. }
  12158. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  12159. uint16 level;
  12160. if (!this->asUInt16(node, "Level", level))
  12161. return 0;
  12162. if (level == 0) {
  12163. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  12164. return 0;
  12165. }
  12166. if (level > MAX_LEVEL) {
  12167. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  12168. return 0;
  12169. }
  12170. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12171. bool exists = entry != nullptr;
  12172. if( !exists ){
  12173. if( !this->nodesExist( node, { "Points" } ) ){
  12174. return 0;
  12175. }
  12176. entry = std::make_shared<s_statpoint_entry>();
  12177. entry->level = level;
  12178. }
  12179. if( this->nodeExists( node, "Points" ) ){
  12180. uint32 points;
  12181. if( !this->asUInt32( node, "Points", points ) ){
  12182. return 0;
  12183. }
  12184. entry->statpoints = points;
  12185. }
  12186. if( this->nodeExists( node, "TraitPoints" ) ){
  12187. uint32 traitpoints;
  12188. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  12189. return 0;
  12190. }
  12191. entry->traitpoints = traitpoints;
  12192. }else{
  12193. if( !exists ){
  12194. entry->traitpoints = 0;
  12195. }
  12196. }
  12197. if( !exists ){
  12198. this->put( level, entry );
  12199. }
  12200. return 1;
  12201. }
  12202. /**
  12203. * Generate the remaining parts of the db if necessary.
  12204. */
  12205. void PlayerStatPointDatabase::loadingFinished(){
  12206. const uint16 trait_start_level = 200;
  12207. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  12208. if( level_one == nullptr ){
  12209. if( battle_config.use_statpoint_table ){
  12210. ShowError( "Missing status points for Level 1\n" );
  12211. }
  12212. level_one = std::make_shared<s_statpoint_entry>();
  12213. level_one->level = 1;
  12214. level_one->statpoints = inter_config.start_status_points;
  12215. level_one->traitpoints = 0;
  12216. this->put( 1, level_one );
  12217. }else if( battle_config.use_statpoint_table ){
  12218. if( level_one->statpoints != inter_config.start_status_points ){
  12219. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, inter_config.start_status_points );
  12220. level_one->statpoints = inter_config.start_status_points;
  12221. }
  12222. }else{
  12223. level_one->statpoints = inter_config.start_status_points;
  12224. level_one->traitpoints = 0;
  12225. }
  12226. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  12227. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  12228. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12229. bool exists = entry != nullptr;
  12230. if( !exists ){
  12231. entry = std::make_shared<s_statpoint_entry>();
  12232. entry->level = level;
  12233. this->put( level, entry );
  12234. }
  12235. if( !battle_config.use_statpoint_table || !exists ){
  12236. if( battle_config.use_statpoint_table ){
  12237. ShowError("Missing status points for Level %hu\n", level);
  12238. }
  12239. if( level <= trait_start_level ){
  12240. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  12241. }else{
  12242. entry->statpoints = last_level->statpoints;
  12243. }
  12244. }
  12245. if( !battle_config.use_traitpoint_table || !exists ){
  12246. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  12247. ShowError( "Missing trait points for Level %hu\n", level );
  12248. }
  12249. if( level > trait_start_level ){
  12250. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  12251. }else{
  12252. entry->traitpoints = 0;
  12253. }
  12254. }
  12255. // Store it for next iteration
  12256. last_level = entry;
  12257. }
  12258. TypesafeCachedYamlDatabase::loadingFinished();
  12259. }
  12260. /*==========================================
  12261. * pc DB reading.
  12262. * job_stats.yml - Job values
  12263. * skill_tree.txt - skill tree for every class
  12264. * attr_fix.yml - elemental adjustment table
  12265. *------------------------------------------*/
  12266. void pc_readdb(void) {
  12267. int32 i, s = 1;
  12268. const char* dbsubpath[] = {
  12269. "",
  12270. "/" DBIMPORT,
  12271. //add other path here
  12272. };
  12273. //reset
  12274. job_db.clear(); // job_info table
  12275. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  12276. penalty_db.load();
  12277. #endif
  12278. statpoint_db.clear();
  12279. job_db.load();
  12280. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  12281. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  12282. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  12283. char* dbsubpath1 = (char*)aMalloc(n1+1);
  12284. char* dbsubpath2 = (char*)aMalloc(n2+1);
  12285. if(i==0) {
  12286. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12287. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  12288. }
  12289. else {
  12290. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12291. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  12292. }
  12293. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  12294. aFree(dbsubpath1);
  12295. aFree(dbsubpath2);
  12296. }
  12297. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  12298. skill_tree_db.reload();
  12299. statpoint_db.load();
  12300. }
  12301. // Read MOTD on startup. [Valaris]
  12302. int32 pc_read_motd(void)
  12303. {
  12304. FILE* fp;
  12305. // clear old MOTD
  12306. memset(motd_text, 0, sizeof(motd_text));
  12307. // read current MOTD
  12308. if( ( fp = fopen(motd_txt, "r") ) != nullptr )
  12309. {
  12310. uint32 entries = 0;
  12311. char buf[CHAT_SIZE_MAX];
  12312. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  12313. {
  12314. uint32 lines = 0;
  12315. size_t len;
  12316. lines++;
  12317. if( buf[0] == '/' && buf[1] == '/' )
  12318. continue;
  12319. len = strlen(buf);
  12320. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  12321. len--;
  12322. if( len ) {
  12323. char * ptr;
  12324. buf[len] = 0;
  12325. if( ( ptr = strstr(buf, " :") ) != nullptr && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  12326. ShowWarning("Found sequence '" CL_WHITE " :" CL_RESET "' on line '" CL_WHITE "%u" CL_RESET "' in '" CL_WHITE "%s" CL_RESET "'. This can cause newer clients to crash.\n", lines, motd_txt);
  12327. }
  12328. else {// empty line
  12329. buf[0] = ' ';
  12330. buf[1] = 0;
  12331. }
  12332. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  12333. entries++;
  12334. }
  12335. fclose(fp);
  12336. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  12337. }
  12338. else
  12339. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  12340. return 0;
  12341. }
  12342. void pc_itemcd_do(map_session_data *sd, bool load) {
  12343. int32 i,cursor = 0;
  12344. struct item_cd* cd = nullptr;
  12345. if( load ) {
  12346. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  12347. // no item cooldown is associated with this character
  12348. return;
  12349. }
  12350. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12351. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  12352. sd->item_delay[cursor].tick = cd->tick[i];
  12353. sd->item_delay[cursor].nameid = cd->nameid[i];
  12354. cursor++;
  12355. }
  12356. }
  12357. idb_remove(itemcd_db,sd->status.char_id);
  12358. } else {
  12359. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  12360. // create a new skill cooldown object for map storage
  12361. CREATE( cd, struct item_cd, 1 );
  12362. idb_put( itemcd_db, sd->status.char_id, cd );
  12363. }
  12364. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12365. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  12366. cd->tick[cursor] = sd->item_delay[i].tick;
  12367. cd->nameid[cursor] = sd->item_delay[i].nameid;
  12368. cursor++;
  12369. }
  12370. }
  12371. }
  12372. return;
  12373. }
  12374. /**
  12375. * Add item delay to player's item delay data
  12376. * @param sd Player
  12377. * @param id Item data
  12378. * @param tick Current tick
  12379. * @param n Item index in inventory
  12380. * @return 0: No delay, can consume item.
  12381. * 1: Has delay, cancel consumption.
  12382. **/
  12383. uint8 pc_itemcd_add(map_session_data *sd, struct item_data *id, t_tick tick, uint16 n) {
  12384. int32 i;
  12385. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  12386. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  12387. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  12388. if( i < MAX_ITEMDELAYS ) {
  12389. if( sd->item_delay[i].nameid ) {// found
  12390. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  12391. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  12392. char e_msg[CHAT_SIZE_MAX];
  12393. if( e_tick > 99 )
  12394. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  12395. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  12396. else
  12397. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  12398. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  12399. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  12400. return 1; // Delay has not expired yet
  12401. }
  12402. } else {// not yet used item (all slots are initially empty)
  12403. sd->item_delay[i].nameid = id->nameid;
  12404. }
  12405. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  12406. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  12407. } else {// should not happen
  12408. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  12409. }
  12410. //clean up used delays so we can give room for more
  12411. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12412. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  12413. sd->item_delay[i].tick = 0;
  12414. sd->item_delay[i].nameid = 0;
  12415. }
  12416. }
  12417. return 0;
  12418. }
  12419. /**
  12420. * Check if player has delay to reuse item
  12421. * @param sd Player
  12422. * @param id Item data
  12423. * @param tick Current tick
  12424. * @param n Item index in inventory
  12425. * @return 0: No delay, can consume item.
  12426. * 1: Has delay, cancel consumption.
  12427. **/
  12428. uint8 pc_itemcd_check(map_session_data *sd, struct item_data *id, t_tick tick, uint16 n) {
  12429. status_change *sc = nullptr;
  12430. nullpo_retr(0, sd);
  12431. nullpo_retr(0, id);
  12432. // Do normal delay assignment
  12433. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  12434. return pc_itemcd_add(sd, id, tick, n);
  12435. // Send reply of delay remains
  12436. if (sc->getSCE(id->delay.sc)) {
  12437. const struct TimerData *timer = get_timer(sc->getSCE(id->delay.sc)->timer);
  12438. clif_msg_value(sd, MSI_ITEM_REUSE_LIMIT_SECOND, (int32)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  12439. return 1;
  12440. }
  12441. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  12442. return 0;
  12443. }
  12444. /**
  12445. * Status change data arrived from char-server
  12446. * @param sd: Player data
  12447. */
  12448. void pc_scdata_received(map_session_data *sd) {
  12449. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12450. clif_weight_limit( sd );
  12451. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) && pc_attendance_counter(sd) < 200 ){
  12452. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12453. }
  12454. sd->state.pc_loaded = true;
  12455. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12456. sd->state.connect_new = 1;
  12457. clif_parse_LoadEndAck(sd->fd, sd);
  12458. }
  12459. if (pc_iscarton(sd)) {
  12460. sd->cart_weight_max = 0; // Force a client refesh
  12461. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12462. }
  12463. if (sd->sc.getSCE(SC_SOULENERGY))
  12464. sd->soulball = sd->sc.getSCE(SC_SOULENERGY)->val1;
  12465. }
  12466. /**
  12467. * Check player account expiration time and rental item expirations
  12468. * @param sd: Player data
  12469. */
  12470. void pc_check_expiration(map_session_data *sd) {
  12471. #ifndef ENABLE_SC_SAVING
  12472. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12473. #endif
  12474. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12475. time_t exp_time = sd->expiration_time;
  12476. char tmpstr[1024];
  12477. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12478. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12479. pc_expire_check(sd);
  12480. }
  12481. }
  12482. TIMER_FUNC(pc_expiration_timer){
  12483. map_session_data *sd = map_id2sd(id);
  12484. if( !sd ) return 0;
  12485. sd->expiration_tid = INVALID_TIMER;
  12486. if( sd->fd )
  12487. clif_authfail_fd(sd->fd,10);
  12488. map_quit(sd);
  12489. return 0;
  12490. }
  12491. TIMER_FUNC(pc_autotrade_timer){
  12492. map_session_data *sd = map_id2sd(id);
  12493. if (!sd)
  12494. return 0;
  12495. sd->autotrade_tid = INVALID_TIMER;
  12496. if (sd->state.autotrade&2)
  12497. vending_reopen(*sd);
  12498. if (sd->state.autotrade&4)
  12499. buyingstore_reopen(sd);
  12500. if (!sd->vender_id && !sd->buyer_id) {
  12501. sd->state.autotrade = 0;
  12502. map_quit(sd);
  12503. }
  12504. return 0;
  12505. }
  12506. /* this timer exists only when a character with a expire timer > 24h is online */
  12507. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12508. TIMER_FUNC(pc_global_expiration_timer){
  12509. struct s_mapiterator* iter;
  12510. map_session_data* sd;
  12511. iter = mapit_getallusers();
  12512. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12513. if( sd->expiration_time )
  12514. pc_expire_check(sd);
  12515. mapit_free(iter);
  12516. return 0;
  12517. }
  12518. void pc_expire_check(map_session_data *sd) {
  12519. /* ongoing timer */
  12520. if( sd->expiration_tid != INVALID_TIMER )
  12521. return;
  12522. /* not within the next 24h, enable the global check */
  12523. if( sd->expiration_time > (time(nullptr) + ((60 * 60) * 24)) ) {
  12524. /* global check not running, enable */
  12525. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12526. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12527. return;
  12528. }
  12529. sd->expiration_tid = add_timer(gettick() + (uint32)(sd->expiration_time - time(nullptr)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12530. }
  12531. /**
  12532. * Deposit some money to bank
  12533. * @param sd
  12534. * @param money Amount of money to deposit
  12535. **/
  12536. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(map_session_data *sd, int32 money) {
  12537. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12538. return BDA_ERROR;
  12539. }
  12540. uint32 limit_check = money + sd->bank_vault;
  12541. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12542. return BDA_OVERFLOW;
  12543. } else if ( money > sd->status.zeny ) {
  12544. return BDA_NO_MONEY;
  12545. }
  12546. if( pc_payzeny(sd, money, LOG_TYPE_BANK) )
  12547. return BDA_NO_MONEY;
  12548. sd->bank_vault += money;
  12549. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12550. if( save_settings&CHARSAVE_BANK )
  12551. chrif_save(sd, CSAVE_NORMAL);
  12552. return BDA_SUCCESS;
  12553. }
  12554. /**
  12555. * Withdraw money from bank
  12556. * @param sd
  12557. * @param money Amount of money that will be withdrawn
  12558. **/
  12559. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(map_session_data *sd, int32 money) {
  12560. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12561. return BWA_UNKNOWN_ERROR;
  12562. }
  12563. uint32 limit_check = money + sd->status.zeny;
  12564. if( money <= 0 ) {
  12565. return BWA_UNKNOWN_ERROR;
  12566. } else if ( money > sd->bank_vault ) {
  12567. return BWA_NO_MONEY;
  12568. } else if ( limit_check > MAX_ZENY ) {
  12569. /* no official response for this scenario exists. */
  12570. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12571. return BWA_UNKNOWN_ERROR;
  12572. }
  12573. if( pc_getzeny(sd,money, LOG_TYPE_BANK) )
  12574. return BWA_NO_MONEY;
  12575. sd->bank_vault -= money;
  12576. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12577. if( save_settings&CHARSAVE_BANK )
  12578. chrif_save(sd, CSAVE_NORMAL);
  12579. return BWA_SUCCESS;
  12580. }
  12581. /**
  12582. * Clear Crimson Marker data from caster
  12583. * @param sd: Player
  12584. **/
  12585. void pc_crimson_marker_clear(map_session_data *sd) {
  12586. uint8 i;
  12587. if (!sd)
  12588. return;
  12589. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12590. struct block_list *bl = nullptr;
  12591. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12592. status_change_end(bl,SC_C_MARKER);
  12593. sd->c_marker[i] = 0;
  12594. }
  12595. }
  12596. /**
  12597. * Show version to player
  12598. * @param sd: Player
  12599. **/
  12600. void pc_show_version(map_session_data *sd) {
  12601. const char* svn = get_svn_revision();
  12602. char buf[CHAT_SIZE_MAX];
  12603. if( svn[0] != UNKNOWN_VERSION )
  12604. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12605. else {
  12606. const char* git = get_git_hash();
  12607. if( git[0] != UNKNOWN_VERSION )
  12608. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12609. else
  12610. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12611. }
  12612. clif_displaymessage(sd->fd,buf);
  12613. }
  12614. /**
  12615. * Run bonus_script on player
  12616. * @param sd
  12617. * @author [Cydh]
  12618. **/
  12619. void pc_bonus_script(map_session_data *sd) {
  12620. t_tick now = gettick();
  12621. struct linkdb_node *node = nullptr, *next = nullptr;
  12622. if (!sd || !(node = sd->bonus_script.head))
  12623. return;
  12624. while (node) {
  12625. struct s_bonus_script_entry *entry = nullptr;
  12626. next = node->next;
  12627. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12628. // Only start timer for new bonus_script
  12629. if (entry->tid == INVALID_TIMER) {
  12630. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12631. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12632. entry->tick += now;
  12633. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12634. }
  12635. if (entry->script)
  12636. run_script(entry->script, 0, sd->bl.id, 0);
  12637. else
  12638. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12639. }
  12640. node = next;
  12641. }
  12642. }
  12643. /**
  12644. * Add bonus_script to player
  12645. * @param sd Player
  12646. * @param script_str Script string
  12647. * @param dur Duration in ms
  12648. * @param icon EFST
  12649. * @param flag Flags @see enum e_bonus_script_flags
  12650. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12651. * @return New created entry pointer or nullptr if failed or nullptr if duplicate fail
  12652. * @author [Cydh]
  12653. **/
  12654. struct s_bonus_script_entry *pc_bonus_script_add(map_session_data *sd, const char *script_str, t_tick dur, enum efst_type icon, uint16 flag, uint8 type) {
  12655. struct script_code *script = nullptr;
  12656. struct linkdb_node *node = nullptr;
  12657. struct s_bonus_script_entry *entry = nullptr;
  12658. if (!sd)
  12659. return nullptr;
  12660. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12661. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12662. return nullptr;
  12663. }
  12664. // Duplication checks
  12665. if ((node = sd->bonus_script.head)) {
  12666. while (node) {
  12667. entry = (struct s_bonus_script_entry *)node->data;
  12668. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12669. t_tick newdur = gettick() + dur;
  12670. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12671. settick_timer(entry->tid, newdur);
  12672. script_free_code(script);
  12673. return nullptr;
  12674. }
  12675. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12676. break;
  12677. else { // No duplicate bonus
  12678. script_free_code(script);
  12679. return nullptr;
  12680. }
  12681. }
  12682. node = node->next;
  12683. }
  12684. }
  12685. CREATE(entry, struct s_bonus_script_entry, 1);
  12686. entry->script_buf = StringBuf_Malloc();
  12687. StringBuf_AppendStr(entry->script_buf, script_str);
  12688. entry->tid = INVALID_TIMER;
  12689. entry->flag = flag;
  12690. entry->icon = icon;
  12691. entry->tick = dur; // Use duration first, on run change to expire time
  12692. entry->type = type;
  12693. entry->script = script;
  12694. sd->bonus_script.count++;
  12695. return entry;
  12696. }
  12697. /**
  12698. * Remove bonus_script data from player
  12699. * @param sd: Target player
  12700. * @param list: Bonus script entry from player
  12701. * @author [Cydh]
  12702. **/
  12703. void pc_bonus_script_free_entry(map_session_data *sd, struct s_bonus_script_entry *entry) {
  12704. if (entry->tid != INVALID_TIMER)
  12705. delete_timer(entry->tid, pc_bonus_script_timer);
  12706. if (entry->script)
  12707. script_free_code(entry->script);
  12708. if (entry->script_buf)
  12709. StringBuf_Free(entry->script_buf);
  12710. if (sd) {
  12711. if (entry->icon != EFST_BLANK)
  12712. clif_status_load(&sd->bl, entry->icon, 0);
  12713. if (sd->bonus_script.count > 0)
  12714. sd->bonus_script.count--;
  12715. }
  12716. aFree(entry);
  12717. }
  12718. /**
  12719. * Do final process if no entry left
  12720. * @param sd
  12721. **/
  12722. static void inline pc_bonus_script_check_final(map_session_data *sd) {
  12723. if (sd->bonus_script.count == 0) {
  12724. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12725. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12726. linkdb_final(&sd->bonus_script.head);
  12727. }
  12728. }
  12729. /**
  12730. * Timer for bonus_script
  12731. * @param tid
  12732. * @param tick
  12733. * @param id
  12734. * @param data
  12735. * @author [Cydh]
  12736. **/
  12737. TIMER_FUNC(pc_bonus_script_timer){
  12738. map_session_data *sd;
  12739. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12740. sd = map_id2sd(id);
  12741. if (!sd) {
  12742. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12743. return 0;
  12744. }
  12745. if (tid == INVALID_TIMER)
  12746. return 0;
  12747. if (!sd->bonus_script.head || entry == nullptr) {
  12748. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12749. return 0;
  12750. }
  12751. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12752. pc_bonus_script_free_entry(sd, entry);
  12753. pc_bonus_script_check_final(sd);
  12754. status_calc_pc(sd,SCO_NONE);
  12755. return 0;
  12756. }
  12757. /**
  12758. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12759. * @param sd: Target player
  12760. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12761. * @author [Cydh]
  12762. **/
  12763. void pc_bonus_script_clear(map_session_data *sd, uint32 flag) {
  12764. struct linkdb_node *node = nullptr;
  12765. uint16 count = 0;
  12766. if (!sd || !(node = sd->bonus_script.head))
  12767. return;
  12768. while (node) {
  12769. struct linkdb_node *next = node->next;
  12770. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12771. if (entry && (
  12772. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12773. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12774. (flag&entry->flag) || // Matched flag
  12775. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12776. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12777. )
  12778. )
  12779. {
  12780. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12781. pc_bonus_script_free_entry(sd, entry);
  12782. count++;
  12783. }
  12784. node = next;
  12785. }
  12786. pc_bonus_script_check_final(sd);
  12787. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12788. status_calc_pc(sd,SCO_NONE);
  12789. }
  12790. /** [Cydh]
  12791. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12792. * @param sd: Target player
  12793. */
  12794. void pc_cell_basilica(map_session_data *sd) {
  12795. nullpo_retv(sd);
  12796. #ifdef RENEWAL
  12797. enum sc_type type = SC_BASILICA_CELL;
  12798. #else
  12799. enum sc_type type = SC_BASILICA;
  12800. #endif
  12801. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12802. if (sd->sc.getSCE(type))
  12803. status_change_end(&sd->bl, type);
  12804. }
  12805. else if (!sd->sc.getSCE(type))
  12806. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12807. }
  12808. /** [Cydh]
  12809. * Get maximum specified parameter for specified class
  12810. * @param sd: Player data
  12811. * @param param: Max parameter to check
  12812. * @return max_param
  12813. */
  12814. uint16 pc_maxparameter(map_session_data *sd, e_params param) {
  12815. nullpo_retr(0, sd);
  12816. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12817. if( job == nullptr || param == PARAM_MAX ){
  12818. return 0;
  12819. }
  12820. return job->max_param[param];
  12821. }
  12822. /**
  12823. * Get max ASPD for player based on Class
  12824. * @param sd Player
  12825. * @return ASPD
  12826. */
  12827. short pc_maxaspd(map_session_data *sd) {
  12828. nullpo_ret(sd);
  12829. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12830. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12831. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12832. battle_config.max_aspd ));
  12833. }
  12834. /**
  12835. * Calculates total item-group related bonuses for the given item
  12836. * @param sd Player
  12837. * @param nameid Item ID
  12838. * @return Heal rate
  12839. **/
  12840. short pc_get_itemgroup_bonus(map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12841. if (bonuses.empty())
  12842. return 0;
  12843. short bonus = 0;
  12844. for (const auto &it : bonuses) {
  12845. uint16 group_id = it.id;
  12846. if (group_id == 0)
  12847. continue;
  12848. if (itemdb_group.item_exists(group_id, nameid))
  12849. bonus += it.val;
  12850. }
  12851. return bonus;
  12852. }
  12853. /**
  12854. * Calculates total item-group related bonuses for the given item group
  12855. * @param sd Player
  12856. * @param group_id Item Group ID
  12857. * @return Heal rate
  12858. **/
  12859. short pc_get_itemgroup_bonus_group(map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12860. if (bonuses.empty())
  12861. return 0;
  12862. for (const auto &it : bonuses) {
  12863. if (it.id == group_id)
  12864. return it.val;
  12865. }
  12866. return 0;
  12867. }
  12868. /**
  12869. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12870. * @param eqi Item EQI of enum equip_index
  12871. * @param *equip_index Player's equip_index[]
  12872. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12873. * @return True if item in same inventory index, False if doesn't
  12874. */
  12875. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  12876. if (index < 0 || index >= MAX_INVENTORY)
  12877. return true;
  12878. // Dual weapon checks
  12879. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12880. return true;
  12881. // Headgear with Mid & Low location
  12882. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12883. return true;
  12884. // Headgear with Top & Mid or Low location
  12885. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12886. return true;
  12887. // Headgear with Mid & Low location
  12888. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12889. return true;
  12890. // Headgear with Top & Mid or Low location
  12891. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12892. return true;
  12893. return false;
  12894. }
  12895. /**
  12896. * Generate Unique item ID for player
  12897. * @param sd : Player
  12898. * @return A generated Unique item ID
  12899. */
  12900. uint64 pc_generate_unique_id(map_session_data *sd) {
  12901. nullpo_ret(sd);
  12902. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12903. }
  12904. /**
  12905. * Validating skill from player after logged on
  12906. * @param sd
  12907. **/
  12908. void pc_validate_skill(map_session_data *sd) {
  12909. if (sd) {
  12910. uint16 i = 0, count = 0;
  12911. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  12912. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  12913. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  12914. for (i = 0; i < MAX_SKILL; i++) {
  12915. uint16 idx = 0;
  12916. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  12917. continue;
  12918. if ((idx = skill_get_index(tmp_skills[i].id))) {
  12919. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  12920. count++;
  12921. }
  12922. else
  12923. ShowWarning("pc_validate_skill: Removing invalid skill '%d' from player (AID=%d CID=%d).\n", tmp_skills[i].id, sd->status.account_id, sd->status.char_id);
  12924. }
  12925. }
  12926. }
  12927. /**
  12928. * Show available NPC Quest / Event Icon Check [Kisuka]
  12929. * @param sd Player
  12930. **/
  12931. void pc_show_questinfo(map_session_data *sd) {
  12932. #if PACKETVER >= 20090218
  12933. nullpo_retv(sd);
  12934. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12935. return;
  12936. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12937. nullpo_retv(mapdata);
  12938. if (mapdata->qi_npc.empty())
  12939. return;
  12940. if (mapdata->qi_npc.size() != sd->qi_display.size())
  12941. return; // init was not called yet
  12942. for (int32 i = 0; i < mapdata->qi_npc.size(); i++) {
  12943. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  12944. if (!nd || nd->qi_data.empty())
  12945. continue;
  12946. bool show = false;
  12947. for (auto &qi : nd->qi_data) {
  12948. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  12949. show = true;
  12950. // Check if need to be displayed
  12951. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  12952. sd->qi_display[i].is_active = true;
  12953. sd->qi_display[i].icon = qi->icon;
  12954. sd->qi_display[i].color = qi->color;
  12955. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  12956. }
  12957. break;
  12958. }
  12959. }
  12960. if (show == false) {
  12961. // Check if need to be hide
  12962. if (sd->qi_display[i].is_active) {
  12963. sd->qi_display[i].is_active = false;
  12964. sd->qi_display[i].icon = QTYPE_NONE;
  12965. sd->qi_display[i].color = QMARK_NONE;
  12966. #if PACKETVER >= 20120410
  12967. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  12968. #else
  12969. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  12970. #endif
  12971. }
  12972. }
  12973. }
  12974. #endif
  12975. }
  12976. /**
  12977. * Reinit the questinfo for player when changing map
  12978. * @param sd Player
  12979. **/
  12980. void pc_show_questinfo_reinit(map_session_data *sd) {
  12981. #if PACKETVER >= 20090218
  12982. nullpo_retv(sd);
  12983. sd->qi_display.clear();
  12984. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12985. return;
  12986. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12987. nullpo_retv(mapdata);
  12988. if (mapdata->qi_npc.empty())
  12989. return;
  12990. sd->qi_display.reserve( mapdata->qi_npc.size() );
  12991. for( int32 i = 0; i < mapdata->qi_npc.size(); i++ ){
  12992. sd->qi_display.push_back( s_qi_display() );
  12993. }
  12994. #endif
  12995. }
  12996. /**
  12997. * Check if a job is allowed to enter the map
  12998. * @param jobid Job ID see enum e_job or sd->status.class_
  12999. * @param m ID -an index- for direct indexing map[] array
  13000. * @return 1 if job is allowed, 0 otherwise
  13001. **/
  13002. bool pc_job_can_entermap(enum e_job jobid, int32 m, int32 group_lv) {
  13003. // Map is other map server.
  13004. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  13005. if (m < 0)
  13006. return true;
  13007. struct map_data *mapdata = map_getmapdata(m);
  13008. if (!mapdata->cell)
  13009. return false;
  13010. if (!pcdb_checkid(jobid))
  13011. return false;
  13012. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  13013. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  13014. return true;
  13015. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  13016. (job->noenter_map.zone&2 && mapdata->getMapFlag(MF_PVP)) || // PVP
  13017. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  13018. (job->noenter_map.zone&8 && mapdata->getMapFlag(MF_BATTLEGROUND)) || // Battleground
  13019. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  13020. (job->noenter_map.zone&(mapdata->zone) && mapdata->getMapFlag(MF_RESTRICTED)) // Zone restriction
  13021. )
  13022. return false;
  13023. return true;
  13024. }
  13025. /**
  13026. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  13027. * @param sd
  13028. **/
  13029. void pc_set_costume_view(map_session_data *sd) {
  13030. int32 i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  13031. struct item_data *id = nullptr;
  13032. nullpo_retv(sd);
  13033. head_low = sd->status.head_bottom;
  13034. head_mid = sd->status.head_mid;
  13035. head_top = sd->status.head_top;
  13036. robe = sd->status.robe;
  13037. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  13038. //Added check to prevent sending the same look on multiple slots ->
  13039. //causes client to redraw item on top of itself. (suggested by Lupus)
  13040. // Normal headgear checks
  13041. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13042. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  13043. sd->status.head_bottom = id->look;
  13044. else
  13045. sd->status.head_bottom = 0;
  13046. }
  13047. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13048. if (!(id->equip&(EQP_HEAD_TOP)))
  13049. sd->status.head_mid = id->look;
  13050. else
  13051. sd->status.head_mid = 0;
  13052. }
  13053. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13054. sd->status.head_top = id->look;
  13055. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13056. sd->status.robe = id->look;
  13057. // Costumes check
  13058. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  13059. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13060. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  13061. sd->status.head_bottom = id->look;
  13062. else
  13063. sd->status.head_bottom = 0;
  13064. }
  13065. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13066. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  13067. sd->status.head_mid = id->look;
  13068. else
  13069. sd->status.head_mid = 0;
  13070. }
  13071. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13072. sd->status.head_top = id->look;
  13073. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13074. sd->status.robe = id->look;
  13075. }
  13076. if (sd->setlook_head_bottom)
  13077. sd->status.head_bottom = sd->setlook_head_bottom;
  13078. if (sd->setlook_head_mid)
  13079. sd->status.head_mid = sd->setlook_head_mid;
  13080. if (sd->setlook_head_top)
  13081. sd->status.head_top = sd->setlook_head_top;
  13082. if (sd->setlook_robe)
  13083. sd->status.robe = sd->setlook_robe;
  13084. if (head_low != sd->status.head_bottom)
  13085. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  13086. if (head_mid != sd->status.head_mid)
  13087. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  13088. if (head_top != sd->status.head_top)
  13089. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  13090. if (robe != sd->status.robe)
  13091. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  13092. }
  13093. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  13094. uint32 date = date_get(DT_YYYYMMDD);
  13095. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  13096. std::shared_ptr<s_attendance_period> period = pair.second;
  13097. if( period->start <= date && period->end >= date ){
  13098. return period;
  13099. }
  13100. }
  13101. return nullptr;
  13102. }
  13103. bool pc_attendance_enabled(){
  13104. // Check if the attendance feature is disabled
  13105. if( !battle_config.feature_attendance ){
  13106. return false;
  13107. }
  13108. // Check if there is a running attendance period
  13109. return pc_attendance_period() != nullptr;
  13110. }
  13111. static inline bool pc_attendance_rewarded_today( map_session_data* sd ){
  13112. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  13113. }
  13114. int32 pc_attendance_counter( map_session_data* sd ){
  13115. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13116. // No running attendance period
  13117. if( period == nullptr ){
  13118. return 0;
  13119. }
  13120. // Get the counter for the current period
  13121. int32 counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13122. // Check if we have a remaining counter from a previous period
  13123. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  13124. // Reset the counter to zero
  13125. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  13126. return 0;
  13127. }
  13128. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  13129. }
  13130. void pc_attendance_claim_reward( map_session_data* sd ){
  13131. // If the user's group does not have the permission
  13132. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  13133. return;
  13134. }
  13135. // Check if the attendance feature is disabled
  13136. if( !pc_attendance_enabled() ){
  13137. return;
  13138. }
  13139. // Check if the user already got his reward today
  13140. if( pc_attendance_rewarded_today( sd ) ){
  13141. return;
  13142. }
  13143. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13144. attendance_counter += 1;
  13145. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13146. if( period == nullptr ){
  13147. return;
  13148. }
  13149. if( period->rewards.size() < attendance_counter ){
  13150. return;
  13151. }
  13152. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  13153. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  13154. if( save_settings&CHARSAVE_ATTENDANCE )
  13155. chrif_save(sd, CSAVE_NORMAL);
  13156. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  13157. struct mail_message msg;
  13158. memset( &msg, 0, sizeof( struct mail_message ) );
  13159. msg.dest_id = sd->status.char_id;
  13160. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  13161. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  13162. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  13163. msg.item[0].nameid = reward->item_id;
  13164. msg.item[0].amount = reward->amount;
  13165. msg.item[0].identify = 1;
  13166. msg.status = MAIL_NEW;
  13167. msg.type = MAIL_INBOX_NORMAL;
  13168. msg.timestamp = time(nullptr);
  13169. intif_Mail_send(0, &msg);
  13170. clif_attendence_response( sd, attendance_counter );
  13171. }
  13172. /**
  13173. * Send a player to jail and determine the location to send in jail.
  13174. * @param sd: Player data
  13175. * @param duration: Duration in minutes (default INT_MAX = infinite)
  13176. */
  13177. void pc_jail(map_session_data &sd, int32 duration) {
  13178. uint16 m_index = mapindex_name2id(MAP_JAIL);
  13179. int16 x, y;
  13180. switch (rnd() % 2) { // Jail Locations
  13181. case 0: // Jail #1
  13182. x = 49;
  13183. y = 75;
  13184. break;
  13185. default: // Default Jail
  13186. x = 24;
  13187. y = 75;
  13188. break;
  13189. }
  13190. duration = i32max(0, duration); // Can't be less than 0 seconds.
  13191. // If duration > 0 then triggered via jailfor which checks every minute.
  13192. // If duration == INT_MAX then triggered via jail for infinite duration.
  13193. // If duration == 0 then triggered via unjail and end status.
  13194. if (duration > 0)
  13195. sc_start4(nullptr, &sd.bl, SC_JAILED, 100, duration, m_index, x, y, 60000);
  13196. else
  13197. status_change_end(&sd.bl, SC_JAILED);
  13198. }
  13199. /**
  13200. * Determine the punishment type when failing macro checks.
  13201. * @param sd: Player data
  13202. * @param stype: Macro detection status type (for banning)
  13203. */
  13204. static void pc_macro_punishment(map_session_data &sd, e_macro_detect_status stype) {
  13205. int32 duration = 0;
  13206. // Determine if there's a unique duration
  13207. if (battle_config.macro_detection_punishment_time > 0) {
  13208. char time[13];
  13209. safesnprintf(time, 13, "+%dnm", battle_config.macro_detection_punishment_time);
  13210. duration = static_cast<int32>(solve_time(time));
  13211. }
  13212. // Delete the timer
  13213. if (sd.macro_detect.timer != INVALID_TIMER)
  13214. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13215. // Clear the macro detect data
  13216. sd.macro_detect = {};
  13217. sd.macro_detect.timer = INVALID_TIMER;
  13218. // Unblock all actions for the player
  13219. sd.state.block_action &= ~PCBLOCK_ALL;
  13220. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13221. if (battle_config.macro_detection_punishment == 0) { // Ban
  13222. clif_macro_detector_status(sd, stype);
  13223. chrif_req_login_operation(sd.macro_detect.reporter_aid, sd.status.name, (duration == 0 ? CHRIF_OP_LOGIN_BLOCK : CHRIF_OP_LOGIN_BAN), duration, 0, 0);
  13224. } else { // Jail
  13225. // Send success to close the window without closing the client
  13226. clif_macro_detector_status(sd, MCD_GOOD);
  13227. pc_jail(sd, (duration == 0 ? INT_MAX : duration / 60));
  13228. }
  13229. }
  13230. /**
  13231. * Save a captcha image to memory via /macro_register.
  13232. * @param sd: Player data
  13233. * @param image_size: Captcha image size
  13234. * @param captcha_answer: Answer to captcha
  13235. */
  13236. void pc_macro_captcha_register(map_session_data &sd, uint16 image_size, const char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13237. nullpo_retv(captcha_answer);
  13238. sd.captcha_upload.cd = nullptr;
  13239. sd.captcha_upload.upload_size = 0;
  13240. if (strlen(captcha_answer) < 4 || image_size == 0 || image_size > CAPTCHA_BMP_SIZE) {
  13241. clif_captcha_upload_request(sd); // Notify client of failure.
  13242. return;
  13243. }
  13244. std::shared_ptr<s_captcha_data> cd = std::make_shared<s_captcha_data>();
  13245. sd.captcha_upload.cd = cd;
  13246. cd->image_size = image_size;
  13247. safestrncpy(cd->captcha_answer, captcha_answer, sizeof(cd->captcha_answer));
  13248. memset(cd->image_data, 0, sizeof(cd->image_data));
  13249. // Request the image data from the client.
  13250. clif_captcha_upload_request(sd);
  13251. }
  13252. /**
  13253. * Save captcha image to server.
  13254. * @param sd: Player data
  13255. * @param captcha_key: Captcha ID
  13256. * @param upload_size: Captcha size
  13257. * @param upload_data: Image data
  13258. */
  13259. void pc_macro_captcha_register_upload(map_session_data &sd, uint16 upload_size, const char *upload_data) {
  13260. nullpo_retv(upload_data);
  13261. memcpy(&sd.captcha_upload.cd->image_data[sd.captcha_upload.upload_size], upload_data, upload_size);
  13262. sd.captcha_upload.upload_size += upload_size;
  13263. // Notify that the image finished uploading.
  13264. if (sd.captcha_upload.upload_size == sd.captcha_upload.cd->image_size) {
  13265. // Tell the client that the upload was finished
  13266. clif_captcha_upload_end(sd);
  13267. // Look for a free key
  13268. uint16 index;
  13269. for (index = 0; index < UINT16_MAX; index++) {
  13270. if (!captcha_db.exists(index)) {
  13271. break;
  13272. }
  13273. }
  13274. if (index == UINT16_MAX) {
  13275. // no free key found...
  13276. sd.captcha_upload.cd = nullptr;
  13277. sd.captcha_upload.upload_size = 0;
  13278. return;
  13279. }
  13280. captcha_db.put(index, sd.captcha_upload.cd);
  13281. sd.captcha_upload.cd = nullptr;
  13282. sd.captcha_upload.upload_size = 0;
  13283. // TODO: write YAML and BMP file?
  13284. }
  13285. }
  13286. /**
  13287. * Timer attached to target player with attempts to confirm captcha.
  13288. */
  13289. TIMER_FUNC(pc_macro_detector_timeout) {
  13290. map_session_data *sd = map_id2sd(id);
  13291. nullpo_ret(sd);
  13292. // Remove the current timer
  13293. sd->macro_detect.timer = INVALID_TIMER;
  13294. // Deduct an answering attempt
  13295. sd->macro_detect.retry -= 1;
  13296. if (sd->macro_detect.retry == 0) {
  13297. // All attempts have been exhausted, punish the user
  13298. pc_macro_punishment(*sd, MCD_TIMEOUT);
  13299. } else {
  13300. // Update the client
  13301. clif_macro_detector_request_show(*sd);
  13302. // Start a new timer
  13303. sd->macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd->bl.id, 0);
  13304. }
  13305. return 0;
  13306. }
  13307. /**
  13308. * Check player's captcha answer.
  13309. * @param sd: Player data
  13310. * @param captcha_answer: Captcha answer entered by player
  13311. */
  13312. void pc_macro_detector_process_answer(map_session_data &sd, const char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13313. nullpo_retv(captcha_answer);
  13314. const std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  13315. // Has no captcha request
  13316. if (cd == nullptr) {
  13317. return;
  13318. }
  13319. // Correct answer
  13320. if (strcmp(captcha_answer, cd->captcha_answer) == 0) {
  13321. // Delete the timer
  13322. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13323. // Clear the macro detect data
  13324. sd.macro_detect = {};
  13325. sd.macro_detect.timer = INVALID_TIMER;
  13326. // Unblock all actions for the player
  13327. sd.state.block_action &= ~PCBLOCK_ALL;
  13328. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13329. // Assign temporary macro variable to check failures
  13330. pc_setreg(&sd, add_str("@captcha_retries"), battle_config.macro_detection_retry - sd.macro_detect.retry);
  13331. // Grant bonuses via script
  13332. run_script(cd->bonus_script, 0, sd.bl.id, fake_nd->bl.id);
  13333. // Notify the client
  13334. clif_macro_detector_status(sd, MCD_GOOD);
  13335. } else {
  13336. // Deduct an answering attempt
  13337. sd.macro_detect.retry -= 1;
  13338. // All attempts have been exhausted, punish the user
  13339. if (sd.macro_detect.retry <= 0) {
  13340. pc_macro_punishment(sd, MCD_INCORRECT);
  13341. return;
  13342. }
  13343. // Incorrect response, update the client
  13344. clif_macro_detector_request_show(sd);
  13345. // Reset the timer
  13346. addtick_timer(sd.macro_detect.timer, gettick() + battle_config.macro_detection_timeout);
  13347. }
  13348. }
  13349. /**
  13350. * Determine if a player tries to log out during a captcha check.
  13351. * @param sd: Player data
  13352. */
  13353. void pc_macro_detector_disconnect(map_session_data &sd) {
  13354. // Delete the timeout timer
  13355. if (sd.macro_detect.timer != INVALID_TIMER) {
  13356. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13357. sd.macro_detect.timer = INVALID_TIMER;
  13358. }
  13359. // If the player disconnects before clearing the challenge the player is punished.
  13360. if (sd.macro_detect.retry != 0)
  13361. pc_macro_punishment(sd, MCD_TIMEOUT);
  13362. }
  13363. /**
  13364. * Save a list of players from an area select via /macro_detector.
  13365. */
  13366. int32 pc_macro_reporter_area_select_sub(block_list *bl, va_list ap) {
  13367. nullpo_retr(0, bl);
  13368. if (bl->type != BL_PC)
  13369. return 0;
  13370. std::vector<uint32> *aid_list = va_arg(ap, std::vector<uint32> *);
  13371. nullpo_ret(aid_list);
  13372. aid_list->push_back(bl->id);
  13373. return 0;
  13374. }
  13375. /**
  13376. * Area select via /macro_detector.
  13377. * @param sd: Player data
  13378. * @param x: X location
  13379. * @param y: Y location
  13380. * @param radius: Area
  13381. */
  13382. void pc_macro_reporter_area_select(map_session_data &sd, const int16 x, const int16 y, const int8 radius) {
  13383. std::vector<uint32> aid_list;
  13384. map_foreachinarea(pc_macro_reporter_area_select_sub, sd.bl.m, x - radius, y - radius, x + radius, y + radius, BL_PC, &aid_list);
  13385. clif_macro_reporter_select(sd, aid_list);
  13386. }
  13387. /**
  13388. * Send out captcha check to player.
  13389. * @param sd: Target player data
  13390. * @param reporter_account_id: Account ID of reporter
  13391. */
  13392. void pc_macro_reporter_process(map_session_data &sd, int32 reporter_account_id) {
  13393. if (captcha_db.empty())
  13394. return;
  13395. // Pick a random image from the database.
  13396. const std::shared_ptr<s_captcha_data> cd = captcha_db.random();
  13397. // Set macro detection data.
  13398. sd.macro_detect.cd = cd;
  13399. sd.macro_detect.reporter_aid = reporter_account_id;
  13400. sd.macro_detect.retry = battle_config.macro_detection_retry;
  13401. // Block all actions for the target player.
  13402. sd.state.block_action |= (PCBLOCK_ALL | PCBLOCK_IMMUNE);
  13403. // Open macro detect client side.
  13404. clif_macro_detector_request(sd);
  13405. // Start the timeout timer.
  13406. sd.macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd.bl.id, 0);
  13407. }
  13408. /**
  13409. * Parse a BMP image to memory.
  13410. * @param filepath: Image file location
  13411. * @param cd: Captcha data
  13412. */
  13413. bool pc_macro_read_captcha_db_loadbmp(const std::string &filepath, std::shared_ptr<s_captcha_data> cd) {
  13414. if (cd == nullptr)
  13415. return false;
  13416. FILE *fp = fopen(filepath.c_str(), "rb");
  13417. if (fp == nullptr) {
  13418. ShowError("%s: Failed to open file \"%s\"\n", __func__, filepath.c_str());
  13419. return false;
  13420. }
  13421. // Load the file data and verify magic
  13422. char bmp_data[CAPTCHA_BMP_SIZE];
  13423. if (fread(bmp_data, CAPTCHA_BMP_SIZE, 1, fp) != 1) {
  13424. ShowError("%s: Failed to read data from \"%s\"\n", __func__, filepath.c_str());
  13425. fclose(fp);
  13426. return false;
  13427. }
  13428. fclose(fp);
  13429. if (bmp_data[0] != 'B' || bmp_data[1] != 'M') {
  13430. ShowError("%s: Invalid BMP file header given at \"%s\"\n", __func__, filepath.c_str());
  13431. return false;
  13432. }
  13433. // Compress the data into the destination
  13434. unsigned long com_size = sizeof(cd->image_data);
  13435. encode_zip(cd->image_data, &com_size, bmp_data, CAPTCHA_BMP_SIZE);
  13436. cd->image_size = static_cast<int16>(com_size);
  13437. return true;
  13438. }
  13439. const std::string CaptchaDatabase::getDefaultLocation() {
  13440. return std::string(db_path) + "/captcha_db.yml";
  13441. }
  13442. /**
  13443. * Reads and parses an entry from the captcha_db.
  13444. * @param node: YAML node containing the entry.
  13445. * @return count of successfully parsed rows
  13446. */
  13447. uint64 CaptchaDatabase::parseBodyNode(const ryml::NodeRef &node) {
  13448. uint16 index;
  13449. if (!this->asUInt16(node, "Id", index))
  13450. return 0;
  13451. std::shared_ptr<s_captcha_data> cd = captcha_db.find(index);
  13452. bool exists = cd != nullptr;
  13453. if (!exists) {
  13454. if (!this->nodesExist(node, { "Filename", "Answer" }))
  13455. return 0;
  13456. cd = std::make_shared<s_captcha_data>();
  13457. cd->index = index;
  13458. }
  13459. if (this->nodeExists(node, "Filename")) {
  13460. std::string filename;
  13461. if (!this->asString(node, "Filename", filename))
  13462. return 0;
  13463. if (!pc_macro_read_captcha_db_loadbmp(filename, cd)) {
  13464. this->invalidWarning(node["Filename"], "Failed to parse BMP image, skipping...\n");
  13465. return 0;
  13466. }
  13467. }
  13468. if (this->nodeExists(node, "Answer")) {
  13469. std::string answer;
  13470. if (!this->asString(node, "Answer", answer))
  13471. return 0;
  13472. if (answer.length() < 4 || answer.length() > CAPTCHA_ANSWER_SIZE) {
  13473. this->invalidWarning(node["Answer"], "The captcha answer must be between 4~%d characters, skipping...", CAPTCHA_ANSWER_SIZE);
  13474. return 0;
  13475. }
  13476. safestrncpy(cd->captcha_answer, answer.c_str(), sizeof(cd->captcha_answer));
  13477. }
  13478. if (this->nodeExists(node, "Bonus")) {
  13479. std::string script;
  13480. if (!this->asString(node, "Bonus", script)) {
  13481. return 0;
  13482. }
  13483. if (cd->bonus_script) {
  13484. script_free_code(cd->bonus_script);
  13485. cd->bonus_script = nullptr;
  13486. }
  13487. cd->bonus_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Bonus"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13488. } else {
  13489. if (!exists)
  13490. cd->bonus_script = parse_script("specialeffect2 EF_BLESSING; sc_start SC_BLESSING,600000,10; specialeffect2 EF_INCAGILITY; sc_start SC_INCREASEAGI,600000,10;", "macro_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13491. }
  13492. if (!exists)
  13493. captcha_db.put(index, cd);
  13494. return 1;
  13495. }
  13496. /*==========================================
  13497. * pc Init/Terminate
  13498. *------------------------------------------*/
  13499. void do_final_pc(void) {
  13500. db_destroy(itemcd_db);
  13501. do_final_pc_groups();
  13502. ers_destroy(pc_sc_display_ers);
  13503. ers_destroy(num_reg_ers);
  13504. ers_destroy(str_reg_ers);
  13505. attendance_db.clear();
  13506. reputation_db.clear();
  13507. #ifdef MAP_GENERATOR
  13508. reputationgroup_db.clear();
  13509. #endif
  13510. penalty_db.clear();
  13511. captcha_db.clear();
  13512. }
  13513. void do_init_pc(void) {
  13514. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  13515. pc_readdb();
  13516. pc_read_motd(); // Read MOTD [Valaris]
  13517. attendance_db.load();
  13518. reputation_db.load();
  13519. #ifdef MAP_GENERATOR
  13520. reputationgroup_db.load();
  13521. #endif
  13522. captcha_db.load();
  13523. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  13524. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  13525. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  13526. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  13527. add_timer_func_list(pc_autosave, "pc_autosave");
  13528. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  13529. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  13530. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  13531. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  13532. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  13533. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  13534. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  13535. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  13536. add_timer_func_list(pc_macro_detector_timeout, "pc_macro_detector_timeout");
  13537. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  13538. // 0=day, 1=night [Yor]
  13539. night_flag = battle_config.night_at_start ? 1 : 0;
  13540. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  13541. int32 day_duration = battle_config.day_duration;
  13542. int32 night_duration = battle_config.night_duration;
  13543. // add night/day timer [Yor]
  13544. add_timer_func_list(map_day_timer, "map_day_timer");
  13545. add_timer_func_list(map_night_timer, "map_night_timer");
  13546. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  13547. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  13548. }
  13549. do_init_pc_groups();
  13550. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  13551. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13552. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13553. ers_chunk_size(pc_sc_display_ers, 150);
  13554. ers_chunk_size(num_reg_ers, 300);
  13555. ers_chunk_size(str_reg_ers, 50);
  13556. }