pc.cpp 483 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994
  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. int16 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_2HAXE: // All level 4 - 2H Axes (works, but low ASPD)
  1508. case W_MACE: //All level 4 - 1H Maces
  1509. case W_STAFF: //All level 4 - 1H Staves
  1510. case W_2HSTAFF: //All level 4 - 2H Staves
  1511. return ITEM_EQUIP_ACK_OK;
  1512. }
  1513. }
  1514. if(item->elv && sd->status.base_level < static_cast<uint32>(item->elv))
  1515. return ITEM_EQUIP_ACK_FAILLEVEL;
  1516. if(item->elvmax && sd->status.base_level > static_cast<uint32>(item->elvmax))
  1517. return ITEM_EQUIP_ACK_FAILLEVEL;
  1518. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1519. return ITEM_EQUIP_ACK_FAIL;
  1520. // Broken equip
  1521. if (sd->inventory.u.items_inventory[n].attribute == 1)
  1522. return ITEM_EQUIP_ACK_FAIL;
  1523. //fail to equip if item is restricted
  1524. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1525. return ITEM_EQUIP_ACK_FAIL;
  1526. if (item->equip&EQP_AMMO) {
  1527. switch (item->subtype) {
  1528. case AMMO_ARROW:
  1529. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1530. clif_msg( *sd, MSI_FAIL_NEED_EQUIPPED_BOW );
  1531. return ITEM_EQUIP_ACK_FAIL;
  1532. }
  1533. break;
  1534. case AMMO_DAGGER:
  1535. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1536. return ITEM_EQUIP_ACK_FAIL;
  1537. break;
  1538. case AMMO_BULLET:
  1539. case AMMO_SHELL:
  1540. 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
  1541. #ifdef RENEWAL
  1542. && sd->status.weapon != W_GRENADE
  1543. #endif
  1544. ) {
  1545. clif_msg( *sd, MSI_WRONG_BULLET );
  1546. return ITEM_EQUIP_ACK_FAIL;
  1547. }
  1548. break;
  1549. #ifndef RENEWAL
  1550. case AMMO_GRENADE:
  1551. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1552. clif_msg( *sd, MSI_WRONG_BULLET );
  1553. return ITEM_EQUIP_ACK_FAIL;
  1554. }
  1555. break;
  1556. #endif
  1557. case AMMO_CANNONBALL:
  1558. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1559. clif_msg( *sd, MSI_USESKILL_FAIL_MADOGEAR ); // Item can only be used when Mado Gear is mounted.
  1560. return ITEM_EQUIP_ACK_FAIL;
  1561. }
  1562. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1563. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1564. clif_msg( *sd, MSI_USESKILL_FAIL_CART ); // Only available when cart is mounted.
  1565. return ITEM_EQUIP_ACK_FAIL;
  1566. }
  1567. break;
  1568. }
  1569. }
  1570. if (!sd->sc.empty()) {
  1571. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.getSCE(SC_STRIPWEAPON)) // Also works with left-hand weapons [DracoRPG]
  1572. return ITEM_EQUIP_ACK_FAIL;
  1573. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.getSCE(SC_STRIPSHIELD))
  1574. return ITEM_EQUIP_ACK_FAIL;
  1575. if(item->equip & EQP_ARMOR && sd->sc.getSCE(SC_STRIPARMOR))
  1576. return ITEM_EQUIP_ACK_FAIL;
  1577. if(item->equip & EQP_HEAD_TOP && sd->sc.getSCE(SC_STRIPHELM))
  1578. return ITEM_EQUIP_ACK_FAIL;
  1579. if(item->equip & EQP_ACC && sd->sc.getSCE(SC__STRIPACCESSORY))
  1580. return ITEM_EQUIP_ACK_FAIL;
  1581. if (item->equip & EQP_ARMS && sd->sc.getSCE(SC__WEAKNESS))
  1582. return ITEM_EQUIP_ACK_FAIL;
  1583. if (item->equip & EQP_SHADOW_GEAR && sd->sc.getSCE(SC_SHADOW_STRIP))
  1584. return ITEM_EQUIP_ACK_FAIL;
  1585. if(item->equip && (sd->sc.getSCE(SC_KYOUGAKU) || sd->sc.getSCE(SC_SUHIDE)))
  1586. return ITEM_EQUIP_ACK_FAIL;
  1587. }
  1588. //Not equipable by class. [Skotlex]
  1589. if (!pc_job_can_use_item(sd,item))
  1590. return ITEM_EQUIP_ACK_FAIL;
  1591. if (!pc_isItemClass(sd, item))
  1592. return ITEM_EQUIP_ACK_FAIL;
  1593. return ITEM_EQUIP_ACK_OK;
  1594. }
  1595. /**
  1596. * Performs some special modifications to a player's last point location,
  1597. * if the map has a nosave mapflag or if the map was an instance.
  1598. * @param sd: Player data
  1599. * @return True if the player should be returned to his savepoint or false if not
  1600. */
  1601. bool pc_lastpoint_special( map_session_data& sd ){
  1602. int16 mapid = map_mapname2mapid( sd.status.last_point.map );
  1603. // Should not happen because otherwise the char-server would have sent the player to another map-server
  1604. if( mapid < 0 ){
  1605. // Return the player to his savepoint
  1606. return true;
  1607. }
  1608. struct map_data* mapdata = map_getmapdata( mapid );
  1609. if( mapdata == nullptr ){
  1610. // Return the player to his savepoint
  1611. return true;
  1612. }
  1613. if (strcmpi(sd.status.last_point.map, MAP_JAIL) == 0) {
  1614. // Don't return jailed player to save point.
  1615. return false;
  1616. }
  1617. // Maybe since the player's logout the nosave mapflag was added to the map
  1618. if( mapdata->getMapFlag(MF_NOSAVE) ){
  1619. // The map has a specific return point
  1620. if( mapdata->save.map ){
  1621. safestrncpy( sd.status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd.status.last_point.map ) );
  1622. sd.status.last_point.x = mapdata->save.x;
  1623. sd.status.last_point.y = mapdata->save.y;
  1624. return false;
  1625. }else{
  1626. // Return the user to his save point
  1627. return true;
  1628. }
  1629. }
  1630. // Check if the last point was an instance
  1631. if( sd.status.last_point_instanceid == 0 ){
  1632. // Nothing to do
  1633. return false;
  1634. }
  1635. // Check if returning to the instance is allowed in general
  1636. if( !battle_config.instance_allow_reconnect ){
  1637. // Return the player to his savepoint
  1638. return true;
  1639. }
  1640. std::shared_ptr<s_instance_data> instance_data = util::umap_find( instances, sd.status.last_point_instanceid );
  1641. if( instance_data == nullptr ){
  1642. // Instance does not exist anymore, return the player to his savepoint
  1643. return true;
  1644. }
  1645. switch( instance_data->mode ){
  1646. case IM_NONE:
  1647. // Cannot validate
  1648. break;
  1649. case IM_CHAR:
  1650. if( sd.status.char_id != instance_data->owner_id ){
  1651. // It is a character bound instance and the player is not the owner, return the player to his savepoint
  1652. return true;
  1653. }
  1654. break;
  1655. case IM_PARTY:
  1656. if( sd.status.party_id != instance_data->owner_id ){
  1657. // It is a party bound instance and the player is not in the party, return the player to his savepoint
  1658. return true;
  1659. }
  1660. break;
  1661. case IM_GUILD:
  1662. if( sd.status.guild_id != instance_data->owner_id ){
  1663. // It is a guild bound instance and the player is not in the guild, return the player to his savepoint
  1664. return true;
  1665. }
  1666. break;
  1667. case IM_CLAN:
  1668. if( sd.status.clan_id != instance_data->owner_id ){
  1669. // It is a clan bound instance and the player is not in the clan, return the player to his savepoint
  1670. return true;
  1671. }
  1672. break;
  1673. }
  1674. int16 imapid = instance_mapid( mapid, sd.status.last_point_instanceid );
  1675. if( imapid < 0 ){
  1676. // Instance does not contain this map anymore, return the player to his savepoint
  1677. return true;
  1678. }
  1679. // Overwrite the stored "source mapname" with the real mapname
  1680. instance_generate_mapname( mapid, sd.status.last_point_instanceid, sd.status.last_point.map );
  1681. // X/Y coordinates are already correct and do not need to be modified
  1682. // Dont warp the player back to his savepoint
  1683. return false;
  1684. }
  1685. /*==========================================
  1686. * No problem with the session id
  1687. * set the status that has been sent from char server
  1688. *------------------------------------------*/
  1689. bool pc_authok(map_session_data *sd, uint32 login_id2, time_t expiration_time, int32 group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1690. {
  1691. t_tick tick = gettick();
  1692. uint32 ip = session[sd->fd]->client_addr;
  1693. sd->login_id2 = login_id2;
  1694. sd->group_id = group_id;
  1695. /* load user permissions */
  1696. pc_group_pc_load(sd);
  1697. memcpy(&sd->status, st, sizeof(*st));
  1698. if (st->sex != sd->status.sex) {
  1699. clif_authfail_fd(sd->fd, 0);
  1700. return false;
  1701. }
  1702. //Set the map-server used job id. [Skotlex]
  1703. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1704. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1705. 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);
  1706. sd->status.class_ = JOB_NOVICE;
  1707. sd->class_ = MAPID_NOVICE;
  1708. } else
  1709. sd->class_ = class_;
  1710. // Checks and fixes to character status data, that are required
  1711. // in case of configuration change or stuff, which cannot be
  1712. // checked on char-server.
  1713. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1714. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1715. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1716. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1717. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1718. sd->state.connect_new = 1;
  1719. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1720. sd->invincible_timer = INVALID_TIMER;
  1721. sd->npc_timer_id = INVALID_TIMER;
  1722. sd->pvp_timer = INVALID_TIMER;
  1723. sd->expiration_tid = INVALID_TIMER;
  1724. sd->autotrade_tid = INVALID_TIMER;
  1725. sd->respawn_tid = INVALID_TIMER;
  1726. sd->tid_queue_active = INVALID_TIMER;
  1727. sd->macro_detect.timer = INVALID_TIMER;
  1728. sd->skill_keep_using.tid = INVALID_TIMER;
  1729. sd->skill_keep_using.skill_id = 0;
  1730. sd->skill_keep_using.level = 0;
  1731. sd->skill_keep_using.target = 0;
  1732. #ifdef SECURE_NPCTIMEOUT
  1733. // Initialize to defaults/expected
  1734. sd->npc_idle_timer = INVALID_TIMER;
  1735. sd->npc_idle_tick = tick;
  1736. sd->npc_idle_type = NPCT_INPUT;
  1737. sd->state.ignoretimeout = false;
  1738. #endif
  1739. sd->canuseitem_tick = tick;
  1740. sd->canusecashfood_tick = tick;
  1741. sd->canequip_tick = tick;
  1742. sd->cantalk_tick = tick;
  1743. sd->canskill_tick = tick;
  1744. sd->cansendmail_tick = tick;
  1745. sd->idletime = last_tick;
  1746. sd->regen.tick.hp = tick;
  1747. sd->regen.tick.sp = tick;
  1748. for(int32 i = 0; i < MAX_SPIRITBALL; i++)
  1749. sd->spirit_timer[i] = INVALID_TIMER;
  1750. if (battle_config.item_auto_get)
  1751. sd->state.autoloot = 10000;
  1752. if (battle_config.disp_experience)
  1753. sd->state.showexp = 1;
  1754. if (battle_config.disp_zeny)
  1755. sd->state.showzeny = 1;
  1756. #ifdef VIP_ENABLE
  1757. if (!battle_config.vip_disp_rate)
  1758. sd->vip.disableshowrate = 1;
  1759. #endif
  1760. if (!(battle_config.display_skill_fail&2))
  1761. sd->state.showdelay = 1;
  1762. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1763. memset(&sd->cart, 0, sizeof(struct s_storage));
  1764. memset(&sd->storage, 0, sizeof(struct s_storage));
  1765. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1766. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1767. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1768. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1769. sd->status.option &= ~OPTION_INVISIBLE;
  1770. }
  1771. status_change_init(&sd->bl);
  1772. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1773. unit_dataset(&sd->bl);
  1774. sd->guild_x = -1;
  1775. sd->guild_y = -1;
  1776. sd->delayed_damage = 0;
  1777. // Event Timers
  1778. for( int32 i = 0; i < MAX_EVENTTIMER; i++ )
  1779. sd->eventtimer[i] = INVALID_TIMER;
  1780. // Rental Timer
  1781. sd->rental_timer = INVALID_TIMER;
  1782. for( int32 i = 0; i < 3; i++ )
  1783. sd->hate_mob[i] = -1;
  1784. sd->quest_log = nullptr;
  1785. sd->num_quests = 0;
  1786. sd->avail_quests = 0;
  1787. sd->save_quest = false;
  1788. sd->count_rewarp = 0;
  1789. sd->mail.pending_weight = 0;
  1790. sd->mail.pending_zeny = 0;
  1791. sd->mail.pending_slots = 0;
  1792. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1793. sd->regs.arrays = nullptr;
  1794. sd->vars_dirty = false;
  1795. sd->vars_ok = false;
  1796. sd->vars_received = 0x0;
  1797. // Check if the player's last point requires special handling and if conditions apply to return the player to his savepoint
  1798. if( pc_lastpoint_special( *sd ) ){
  1799. // The player should be warped back to his savepoint
  1800. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1801. sd->status.last_point.x = sd->status.save_point.x;
  1802. sd->status.last_point.y = sd->status.save_point.y;
  1803. }
  1804. //warp player
  1805. 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 );
  1806. if( setpos_result != SETPOS_OK ){
  1807. ShowError( "Last_point_map %s not found (error code %d)\n", sd->status.last_point.map, setpos_result );
  1808. // try warping to a default map instead (church graveyard)
  1809. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1810. // if we fail again
  1811. clif_authfail_fd(sd->fd, 0);
  1812. return false;
  1813. }
  1814. }
  1815. clif_inventory_expansion_info( sd );
  1816. clif_authok(sd);
  1817. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1818. sd->die_counter=-1;
  1819. //display login notice
  1820. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1821. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1822. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1823. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1824. sd->status.name, sd->status.account_id, sd->status.char_id,
  1825. CONVIP(ip), sd->group_id);
  1826. // Send friends list
  1827. clif_friendslist_send( *sd );
  1828. if( !changing_mapservers ) {
  1829. if (battle_config.display_version == 1)
  1830. pc_show_version(sd);
  1831. // Message of the Day [Valaris]
  1832. for(int32 i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1833. if (battle_config.motd_type)
  1834. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1835. else
  1836. clif_displaymessage(sd->fd, motd_text[i]);
  1837. }
  1838. if (expiration_time != 0)
  1839. sd->expiration_time = expiration_time;
  1840. /**
  1841. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1842. **/
  1843. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  1844. }
  1845. pc_validate_skill(sd);
  1846. /* [Ind] */
  1847. sd->sc_display = nullptr;
  1848. sd->sc_display_count = 0;
  1849. // Player has not yet received the CashShop list
  1850. sd->status.cashshop_sent = false;
  1851. sd->last_addeditem_index = -1;
  1852. sd->bonus_script.head = nullptr;
  1853. sd->bonus_script.count = 0;
  1854. // Initialize BG queue
  1855. sd->bg_queue_id = 0;
  1856. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1857. sd->hatEffects = {};
  1858. #endif
  1859. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1860. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1861. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1862. clif_updatestatus(*sd, SP_BASEEXP);
  1863. }
  1864. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1865. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1866. clif_updatestatus(*sd, SP_JOBEXP);
  1867. }
  1868. // Request all registries (auth is considered completed whence they arrive)
  1869. intif_request_registry(sd,7);
  1870. return true;
  1871. }
  1872. /*==========================================
  1873. * Closes a connection because it failed to be authenticated from the char server.
  1874. *------------------------------------------*/
  1875. void pc_authfail(map_session_data *sd)
  1876. {
  1877. clif_authfail_fd(sd->fd, 0);
  1878. return;
  1879. }
  1880. /**
  1881. * Player register a bl as hatred
  1882. * @param sd : player session
  1883. * @param pos : hate position [0;2]
  1884. * @param bl : target bl
  1885. * @return false:failed, true:success
  1886. */
  1887. bool pc_set_hate_mob(map_session_data *sd, int32 pos, struct block_list *bl)
  1888. {
  1889. int32 class_;
  1890. if (!sd || !bl || pos < 0 || pos > 2)
  1891. return false;
  1892. if (sd->hate_mob[pos] != -1)
  1893. { //Can't change hate targets.
  1894. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1895. return false;
  1896. }
  1897. class_ = status_get_class(bl);
  1898. if (!pcdb_checkid(class_)) {
  1899. uint32 max_hp = status_get_max_hp(bl);
  1900. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1901. return false;
  1902. if (pos != status_get_size(bl))
  1903. return false; //Wrong size
  1904. }
  1905. sd->hate_mob[pos] = class_;
  1906. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1907. clif_hate_info(sd, pos, class_, 1);
  1908. return true;
  1909. }
  1910. /*==========================================
  1911. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1912. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1913. * See intif_parse_StorageReceived() for item operations [lighta]
  1914. *------------------------------------------*/
  1915. void pc_reg_received(map_session_data *sd)
  1916. {
  1917. uint8 i;
  1918. sd->vars_ok = true;
  1919. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1920. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1921. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1922. sd->die_counter = static_cast<int32>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1923. sd->langtype = static_cast<int32>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1924. if (msg_checklangtype(sd->langtype,true) < 0)
  1925. sd->langtype = 0; //invalid langtype reset to default
  1926. // Cash shop
  1927. sd->cashPoints = static_cast<int32>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1928. sd->kafraPoints = static_cast<int32>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1929. // Cooking Exp
  1930. sd->cook_mastery = static_cast<int16>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1931. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1932. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1933. sd->mission_mobid = static_cast<int16>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1934. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1935. }
  1936. if (battle_config.feature_banking)
  1937. sd->bank_vault = static_cast<int32>(pc_readreg2(sd, BANK_VAULT_VAR));
  1938. if (battle_config.feature_roulette) {
  1939. sd->roulette_point.bronze = static_cast<int32>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1940. sd->roulette_point.silver = static_cast<int32>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1941. sd->roulette_point.gold = static_cast<int32>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1942. }
  1943. sd->roulette.prizeIdx = -1;
  1944. //SG map and mob read [Komurka]
  1945. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1946. uint16 j;
  1947. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1948. sd->feel_map[i].index = j;
  1949. sd->feel_map[i].m = map_mapindex2mapid(j);
  1950. } else {
  1951. sd->feel_map[i].index = 0;
  1952. sd->feel_map[i].m = -1;
  1953. }
  1954. sd->hate_mob[i] = static_cast<int16>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1955. }
  1956. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1957. uint16 skid = static_cast<uint16>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1958. sd->cloneskill_idx = skill_get_index(skid);
  1959. if (sd->cloneskill_idx > 0) {
  1960. sd->status.skill[sd->cloneskill_idx].id = skid;
  1961. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1962. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1963. sd->status.skill[sd->cloneskill_idx].lv = i;
  1964. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1965. }
  1966. }
  1967. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1968. uint16 skid = static_cast<uint16>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1969. sd->reproduceskill_idx = skill_get_index(skid);
  1970. if (sd->reproduceskill_idx > 0) {
  1971. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1972. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1973. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1974. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1975. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1976. }
  1977. }
  1978. //Weird... maybe registries were reloaded?
  1979. if (sd->state.active)
  1980. return;
  1981. sd->state.active = 1;
  1982. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1983. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1984. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1985. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1986. // Restore IM_CHAR instance to the player
  1987. for (const auto &instance : instances) {
  1988. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1989. sd->instance_id = instance.first;
  1990. break;
  1991. }
  1992. }
  1993. if (sd->status.party_id > 0)
  1994. party_member_joined( *sd );
  1995. if (sd->status.guild_id > 0)
  1996. guild_member_joined(sd);
  1997. if (sd->status.clan_id > 0)
  1998. clan_member_joined( *sd );
  1999. #if !( PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410 )
  2000. // 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
  2001. clif_instance_info( *sd );
  2002. #endif
  2003. // pet
  2004. if (sd->status.pet_id > 0)
  2005. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  2006. // Homunculus [albator]
  2007. if( sd->status.hom_id > 0 )
  2008. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  2009. if( sd->status.mer_id > 0 )
  2010. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  2011. if( sd->status.ele_id > 0 )
  2012. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  2013. map_addiddb(&sd->bl);
  2014. map_delnickdb(sd->status.char_id, sd->status.name);
  2015. if (!chrif_auth_finished(sd))
  2016. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  2017. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  2018. chrif_bsdata_request(sd->status.char_id);
  2019. #ifdef VIP_ENABLE
  2020. sd->vip.time = 0;
  2021. sd->vip.enabled = 0;
  2022. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  2023. #endif
  2024. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  2025. intif_request_questlog(sd);
  2026. if (battle_config.feature_achievement) {
  2027. sd->achievement_data.total_score = 0;
  2028. sd->achievement_data.level = 0;
  2029. sd->achievement_data.save = false;
  2030. sd->achievement_data.count = 0;
  2031. sd->achievement_data.incompleteCount = 0;
  2032. sd->achievement_data.achievements = nullptr;
  2033. intif_request_achievements(sd->status.char_id);
  2034. }
  2035. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  2036. sd->state.connect_new = 1;
  2037. clif_parse_LoadEndAck(sd->fd, sd);
  2038. if( pc_isinvisible( sd ) ){
  2039. sd->vd.class_ = JT_INVISIBLE;
  2040. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  2041. // decrement the number of pvp players on the map
  2042. map_getmapdata( sd->bl.m )->users_pvp--;
  2043. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  2044. // unregister the player for ranking
  2045. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  2046. sd->pvp_timer = INVALID_TIMER;
  2047. }
  2048. clif_changeoption( &sd->bl );
  2049. }
  2050. }
  2051. channel_autojoin(sd);
  2052. }
  2053. static int32 pc_calc_skillpoint(map_session_data* sd)
  2054. {
  2055. uint16 i, skill_point = 0;
  2056. nullpo_ret(sd);
  2057. for(i = 1; i < MAX_SKILL; i++) {
  2058. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  2059. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  2060. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  2061. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  2062. )
  2063. {
  2064. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  2065. skill_point += sd->status.skill[i].lv;
  2066. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  2067. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  2068. }
  2069. }
  2070. }
  2071. return skill_point;
  2072. }
  2073. static bool pc_grant_allskills(map_session_data *sd, bool addlv) {
  2074. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  2075. return false;
  2076. /**
  2077. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  2078. * skill tree and since they have no icons they'll give resource errors
  2079. * Get ALL skills except npc/guild ones. [Skotlex]
  2080. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  2081. **/
  2082. for (const auto &skill : skill_db) {
  2083. uint16 skill_id = skill.second->nameid;
  2084. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  2085. continue;
  2086. switch (skill_id) {
  2087. case SM_SELFPROVOKE:
  2088. case AB_DUPLELIGHT_MELEE:
  2089. case AB_DUPLELIGHT_MAGIC:
  2090. case WL_CHAINLIGHTNING_ATK:
  2091. case WL_TETRAVORTEX_FIRE:
  2092. case WL_TETRAVORTEX_WATER:
  2093. case WL_TETRAVORTEX_WIND:
  2094. case WL_TETRAVORTEX_GROUND:
  2095. case WL_SUMMON_ATK_FIRE:
  2096. case WL_SUMMON_ATK_WIND:
  2097. case WL_SUMMON_ATK_WATER:
  2098. case WL_SUMMON_ATK_GROUND:
  2099. case LG_OVERBRAND_BRANDISH:
  2100. case LG_OVERBRAND_PLUSATK:
  2101. case WM_SEVERE_RAINSTORM_MELEE:
  2102. case RL_R_TRIP_PLUSATK:
  2103. case SG_DEVIL:
  2104. case MO_TRIPLEATTACK:
  2105. case RG_SNATCHER:
  2106. continue;
  2107. default:
  2108. {
  2109. uint8 lv = (uint8)skill_get_max(skill_id);
  2110. if (lv > 0) {
  2111. uint16 idx = skill_get_index(skill_id);
  2112. sd->status.skill[idx].id = skill_id;
  2113. if (addlv)
  2114. sd->status.skill[idx].lv = lv;
  2115. }
  2116. }
  2117. break;
  2118. }
  2119. }
  2120. return true;
  2121. }
  2122. /*==========================================
  2123. * Calculation of skill level.
  2124. * @param sd
  2125. *------------------------------------------*/
  2126. void pc_calc_skilltree(map_session_data *sd)
  2127. {
  2128. nullpo_retv(sd);
  2129. uint64 job = pc_calc_skilltree_normalize_job(sd);
  2130. int32 class_ = pc_mapid2jobid(job, sd->status.sex);
  2131. if( class_ == -1 )
  2132. { //Unable to normalize job??
  2133. 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 );
  2134. return;
  2135. }
  2136. uint16 job_id = class_;
  2137. class_ = pc_class2idx(class_);
  2138. for (const auto &skill : skill_db) {
  2139. uint16 skill_id = skill.second->nameid;
  2140. uint16 idx = skill_get_index(skill_id);
  2141. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  2142. sd->status.skill[idx].id = 0; //First clear skills.
  2143. /* permanent skills that must be re-checked */
  2144. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  2145. if (skill_id == 0) {
  2146. sd->status.skill[idx].id = 0;
  2147. sd->status.skill[idx].lv = 0;
  2148. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2149. continue;
  2150. }
  2151. switch (skill_id) {
  2152. case NV_TRICKDEAD:
  2153. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  2154. sd->status.skill[idx].id = 0;
  2155. sd->status.skill[idx].lv = 0;
  2156. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2157. }
  2158. break;
  2159. }
  2160. }
  2161. }
  2162. for (const auto &skill : skill_db) {
  2163. uint16 idx = skill_get_index(skill.second->nameid);
  2164. // Restore original level of skills after deleting earned skills.
  2165. 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 ) {
  2166. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2167. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2168. }
  2169. }
  2170. // Removes Taekwon Ranker skill bonus
  2171. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  2172. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  2173. if (tree != nullptr && !tree->skills.empty()) {
  2174. for (const auto &it : tree->skills) {
  2175. uint16 sk_idx = skill_get_index(it.first);
  2176. if (sk_idx == 0)
  2177. continue;
  2178. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  2179. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  2180. continue;
  2181. sd->status.skill[sk_idx].id = 0;
  2182. }
  2183. }
  2184. }
  2185. }
  2186. // Grant all skills
  2187. pc_grant_allskills(sd, false);
  2188. int32 flag;
  2189. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2190. do {
  2191. flag = 0;
  2192. if (tree == nullptr || tree->skills.empty())
  2193. break;
  2194. for (const auto &skillsit : tree->skills) {
  2195. bool fail = false;
  2196. uint16 skid = skillsit.first;
  2197. uint16 sk_idx = skill_get_index(skid);
  2198. if (sd->status.skill[sk_idx].id)
  2199. continue; //Skill already known.
  2200. if (!battle_config.skillfree) {
  2201. // Checking required skills
  2202. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2203. if (entry != nullptr && !entry->need.empty()) {
  2204. for (const auto &it : entry->need) {
  2205. uint16 sk_need_id = it.first;
  2206. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2207. if (sk_need_idx > 0) {
  2208. uint16 sk_need = sk_need_id;
  2209. 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)
  2210. sk_need = 0; //Not learned.
  2211. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  2212. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2213. else
  2214. sk_need = pc_checkskill(sd,sk_need_id);
  2215. if (sk_need < it.second) {
  2216. fail = true;
  2217. break;
  2218. }
  2219. }
  2220. }
  2221. }
  2222. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  2223. int32 c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2224. c_ = pc_class2idx(c_);
  2225. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  2226. fail = true; // base level requirement wasn't satisfied
  2227. }
  2228. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  2229. int32 c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2230. c_ = pc_class2idx(c_);
  2231. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  2232. fail = true; // job level requirement wasn't satisfied
  2233. }
  2234. }
  2235. if (!fail) {
  2236. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2237. if (!sd->status.skill[sk_idx].lv && (
  2238. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2239. skill->inf2[INF2_ISWEDDING] ||
  2240. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2241. ))
  2242. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  2243. sd->status.skill[sk_idx].id = skid;
  2244. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  2245. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  2246. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  2247. }
  2248. flag = 1; // skill list has changed, perform another pass
  2249. }
  2250. }
  2251. } while(flag);
  2252. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  2253. uint16 skid = 0;
  2254. /* Taekwon Ranker Bonus Skill Tree
  2255. ============================================
  2256. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  2257. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  2258. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  2259. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2260. if (tree != nullptr && !tree->skills.empty()) {
  2261. for (const auto &it : tree->skills) {
  2262. skid = it.first;
  2263. uint16 sk_idx = skill_get_index(skid);
  2264. if (sk_idx == 0)
  2265. continue;
  2266. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  2267. continue; //Do not include Quest/Wedding skills.
  2268. if( sd->status.skill[sk_idx].id == 0 ) {
  2269. sd->status.skill[sk_idx].id = skid;
  2270. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  2271. } else if( skid != NV_BASIC )
  2272. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  2273. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  2274. }
  2275. }
  2276. }
  2277. // Enable Bard/Dancer spirit linked skills.
  2278. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_BARDDANCER) {
  2279. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  2280. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  2281. { BA_POEMBRAGI, DC_FORTUNEKISS },
  2282. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  2283. for (const auto &skill : linked_skills) {
  2284. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  2285. continue;
  2286. // Tag it as a non-savable, non-uppable, bonus skill
  2287. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  2288. }
  2289. }
  2290. }
  2291. //Checks if you can learn a new skill after having leveled up a skill.
  2292. static void pc_check_skilltree(map_session_data *sd)
  2293. {
  2294. int32 flag = 0;
  2295. if (battle_config.skillfree)
  2296. return; //Function serves no purpose if this is set
  2297. int32 c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  2298. if (c == -1) { //Unable to normalize job??
  2299. 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);
  2300. return;
  2301. }
  2302. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  2303. if (tree == nullptr || tree->skills.empty())
  2304. return;
  2305. do {
  2306. flag = 0;
  2307. for (const auto &skillsit : tree->skills) {
  2308. uint16 skid = skillsit.first;
  2309. uint16 sk_idx = skill_get_index(skid);
  2310. bool fail = false;
  2311. if (sd->status.skill[sk_idx].id) //Already learned
  2312. continue;
  2313. // Checking required skills
  2314. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2315. if (entry != nullptr && !entry->need.empty()) {
  2316. for (const auto &it : entry->need) {
  2317. uint16 sk_need_id = it.first;
  2318. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2319. if (sk_need_id > 0) {
  2320. int16 sk_need = sk_need_id;
  2321. 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)
  2322. sk_need = 0; //Not learned.
  2323. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  2324. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2325. else
  2326. sk_need = pc_checkskill(sd,sk_need_id);
  2327. if (sk_need < it.second) {
  2328. fail = true;
  2329. break;
  2330. }
  2331. }
  2332. }
  2333. }
  2334. if( fail )
  2335. continue;
  2336. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2337. continue;
  2338. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2339. if( !sd->status.skill[sk_idx].lv && (
  2340. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2341. skill->inf2[INF2_ISWEDDING] ||
  2342. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2343. ) )
  2344. continue; //Cannot be learned via normal means.
  2345. sd->status.skill[sk_idx].id = skid;
  2346. flag = 1;
  2347. }
  2348. } while(flag);
  2349. }
  2350. // Make sure all the skills are in the correct condition
  2351. // before persisting to the backend.. [MouseJstr]
  2352. void pc_clean_skilltree(map_session_data *sd)
  2353. {
  2354. uint16 i;
  2355. for (i = 0; i < MAX_SKILL; i++){
  2356. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2357. sd->status.skill[i].id = 0;
  2358. sd->status.skill[i].lv = 0;
  2359. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2360. }
  2361. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2362. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2363. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2364. }
  2365. }
  2366. }
  2367. uint64 pc_calc_skilltree_normalize_job_sub( map_session_data *sd ){
  2368. int32 skill_point = pc_calc_skillpoint( sd );
  2369. if( sd->class_ & MAPID_SUMMONER ){
  2370. // Novice's skill points for basic skill.
  2371. std::shared_ptr<s_job_info> summoner_job = job_db.find( JOB_SUMMONER );
  2372. int32 summoner_skills = summoner_job->max_job_level - 1;
  2373. if( skill_point < summoner_skills ){
  2374. return MAPID_SUMMONER;
  2375. }
  2376. skill_point -= summoner_skills;
  2377. }else{
  2378. // Novice's skill points for basic skill.
  2379. std::shared_ptr<s_job_info> novice_job = job_db.find( JOB_NOVICE );
  2380. int32 novice_skills = novice_job->max_job_level - 1;
  2381. if( skill_point < novice_skills ){
  2382. return MAPID_NOVICE;
  2383. }
  2384. skill_point -= novice_skills;
  2385. }
  2386. // 1st Class Job LV Check
  2387. if( sd->class_ & JOBL_2 && ( sd->class_ & MAPID_UPPERMASK ) != MAPID_SUPER_NOVICE ){
  2388. uint64 mapid_1st = sd->class_ & MAPID_BASEMASK;
  2389. int32 class_1st = pc_mapid2jobid( mapid_1st, sd->status.sex );
  2390. if( !sd->change_level_2nd ){
  2391. if( class_1st >= JOB_NOVICE ){
  2392. sd->change_level_2nd = job_db.find( class_1st )->max_job_level;
  2393. }else{
  2394. sd->change_level_2nd = 0;
  2395. }
  2396. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  2397. }
  2398. if( class_1st > 0 && skill_point < ( sd->change_level_2nd - 1 ) ){
  2399. return mapid_1st;
  2400. }
  2401. skill_point -= ( sd->change_level_2nd - 1 );
  2402. }
  2403. // 2nd Class Job LV Check
  2404. if( sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E ){
  2405. uint64 mapid_2nd = sd->class_ & MAPID_UPPERMASK;
  2406. int32 class_2nd = pc_mapid2jobid( mapid_2nd, sd->status.sex );
  2407. if( !sd->change_level_3rd ){
  2408. if( class_2nd >= JOB_NOVICE ){
  2409. sd->change_level_3rd = job_db.find( class_2nd )->max_job_level;
  2410. }else{
  2411. sd->change_level_3rd = 0;
  2412. }
  2413. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  2414. }
  2415. if( class_2nd > 0 && skill_point < ( sd->change_level_3rd - 1 ) ){
  2416. return mapid_2nd;
  2417. }
  2418. skill_point -= ( sd->change_level_3rd - 1 );
  2419. }
  2420. // 3rd Class Job LV Check
  2421. if( sd->class_ & JOBL_FOURTH ){
  2422. uint64 mapid_3rd = sd->class_ & MAPID_THIRDMASK | JOBL_THIRD;
  2423. int32 class_3rd = pc_mapid2jobid( mapid_3rd, sd->status.sex );
  2424. if( !sd->change_level_4th ){
  2425. if( class_3rd >= JOB_NOVICE ){
  2426. sd->change_level_4th = job_db.find( class_3rd )->max_job_level;
  2427. }else{
  2428. sd->change_level_4th = 0;
  2429. }
  2430. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ), sd->change_level_4th );
  2431. }
  2432. if( class_3rd > 0 && skill_point < ( sd->change_level_4th - 1 ) ){
  2433. return mapid_3rd;
  2434. }
  2435. skill_point -= ( sd->change_level_4th - 1 );
  2436. }
  2437. return sd->class_;
  2438. }
  2439. uint64 pc_calc_skilltree_normalize_job( map_session_data *sd ){
  2440. if( !battle_config.skillup_limit || pc_has_permission( sd, PC_PERM_ALL_SKILL ) ){
  2441. return sd->class_;
  2442. }
  2443. uint64 c = pc_calc_skilltree_normalize_job_sub( sd );
  2444. // Special Masks
  2445. if( sd->class_&JOBL_UPPER ){
  2446. if( pc_mapid2jobid( c | JOBL_UPPER, sd->status.sex ) >= JOB_NOVICE ){
  2447. c |= JOBL_UPPER;// Rebirth Job
  2448. }
  2449. }
  2450. if( sd->class_&JOBL_BABY ){
  2451. if( pc_mapid2jobid( c | JOBL_BABY, sd->status.sex ) >= JOB_NOVICE ){
  2452. c |= JOBL_BABY;// Baby Job
  2453. }
  2454. }
  2455. return c;
  2456. }
  2457. /*==========================================
  2458. * Updates the weight status
  2459. *------------------------------------------
  2460. * 1: overweight 50% for pre-renewal and 70% for renewal
  2461. * 2: overweight 90%
  2462. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2463. */
  2464. void pc_updateweightstatus(map_session_data *sd)
  2465. {
  2466. int32 old_overweight;
  2467. int32 new_overweight;
  2468. nullpo_retv(sd);
  2469. old_overweight = (sd->sc.getSCE(SC_WEIGHT90)) ? 2 : (sd->sc.getSCE(SC_WEIGHT50)) ? 1 : 0;
  2470. #ifdef RENEWAL
  2471. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2472. #else
  2473. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2474. #endif
  2475. if( old_overweight == new_overweight )
  2476. return; // no change
  2477. // stop old status change
  2478. if( old_overweight == 1 )
  2479. status_change_end(&sd->bl, SC_WEIGHT50);
  2480. else if( old_overweight == 2 )
  2481. status_change_end(&sd->bl, SC_WEIGHT90);
  2482. // start new status change
  2483. if( new_overweight == 1 )
  2484. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2485. else if( new_overweight == 2 )
  2486. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2487. // update overweight status
  2488. sd->regen.state.overweight = new_overweight;
  2489. }
  2490. int32 pc_disguise(map_session_data *sd, int32 class_)
  2491. {
  2492. if (!class_ && !sd->disguise)
  2493. return 0;
  2494. if (class_ && sd->disguise == class_)
  2495. return 0;
  2496. if(pc_isinvisible(sd))
  2497. { //Character is invisible. Stealth class-change. [Skotlex]
  2498. sd->disguise = class_; //viewdata is set on uncloaking.
  2499. return 2;
  2500. }
  2501. if (sd->bl.prev != nullptr) {
  2502. unit_stop_walking( &sd->bl, USW_NONE );
  2503. clif_clearunit_area( sd->bl, CLR_OUTSIGHT );
  2504. }
  2505. if (!class_) {
  2506. sd->disguise = 0;
  2507. class_ = sd->status.class_;
  2508. } else
  2509. sd->disguise=class_;
  2510. status_set_viewdata(&sd->bl, class_);
  2511. clif_changeoption(&sd->bl);
  2512. if (sd->bl.prev != nullptr) {
  2513. clif_spawn(&sd->bl);
  2514. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2515. { //It seems the cart info is lost on undisguise.
  2516. clif_cartlist(sd);
  2517. clif_updatestatus(*sd,SP_CARTINFO);
  2518. }
  2519. if (sd->chatID) {
  2520. chat_data* cd = map_id2cd( sd->chatID );
  2521. if( cd != nullptr ){
  2522. clif_dispchat( *cd );
  2523. }
  2524. }
  2525. }
  2526. return 1;
  2527. }
  2528. /// Show error message
  2529. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2530. /// Check for valid Element, break & show error message if invalid Element
  2531. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2532. /// Check for valid Race, break & show error message if invalid Race
  2533. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2534. /// Check for valid Race2, break & show error message if invalid Race2
  2535. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2536. /// Check for valid Class, break & show error message if invalid Class
  2537. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2538. /// Check for valid Size, break & show error message if invalid Size
  2539. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2540. /// Check for valid SC, break & show error message if invalid SC
  2541. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2542. /**
  2543. * Add auto spell bonus for player while attacking/attacked
  2544. * @param spell: Spell array
  2545. * @param id: Skill to cast
  2546. * @param lv: Skill level
  2547. * @param rate: Success chance
  2548. * @param battle_flag: Battle flag
  2549. * @param card_id: Used to prevent card stacking
  2550. * @param flag: Flags used for extra arguments
  2551. * &0: forces the skill to be casted on self, rather than on the target
  2552. * &1: forces the skill to be casted on target, rather than self
  2553. * &2: random skill level in [1..lv] is chosen
  2554. */
  2555. static void pc_bonus_autospell(std::vector<s_autospell> &spell, uint16 id, uint16 lv, int16 rate, int16 battle_flag, t_itemid card_id, uint8 flag)
  2556. {
  2557. if (spell.size() == MAX_PC_BONUS) {
  2558. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2559. return;
  2560. }
  2561. if (!rate)
  2562. return;
  2563. if (!(battle_flag&BF_RANGEMASK))
  2564. battle_flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2565. if (!(battle_flag&BF_WEAPONMASK))
  2566. battle_flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2567. if (!(battle_flag&BF_SKILLMASK)) {
  2568. if (battle_flag&(BF_MAGIC | BF_MISC))
  2569. battle_flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2570. if (battle_flag&BF_WEAPON)
  2571. battle_flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2572. }
  2573. for (auto &it : spell) {
  2574. 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) {
  2575. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2576. return;
  2577. it.rate = util::safe_addition_cap(it.rate, rate, (int16)1000);
  2578. return;
  2579. }
  2580. }
  2581. struct s_autospell entry = {};
  2582. entry.id = id;
  2583. entry.lv = lv;
  2584. entry.rate = cap_value(rate, -1000, 1000);
  2585. entry.battle_flag = battle_flag;
  2586. entry.card_id = card_id;
  2587. entry.flag = flag;
  2588. spell.push_back(entry);
  2589. }
  2590. /**
  2591. * Add auto spell bonus for player while using skills
  2592. * @param spell: Spell array
  2593. * @param src_skill: Trigger skill
  2594. * @param id: Support or target type
  2595. * @param lv: Skill level
  2596. * @param rate: Success chance
  2597. * @param card_id: Used to prevent card stacking
  2598. * @param flag: Flags used for extra arguments
  2599. * &1: forces the skill to be casted on self, rather than on the target of skill
  2600. * &2: random skill level in [1..lv] is chosen
  2601. */
  2602. static void pc_bonus_autospell_onskill(std::vector<s_autospell> &spell, uint16 src_skill, uint16 id, uint16 lv, int16 rate, t_itemid card_id, uint8 flag)
  2603. {
  2604. if (spell.size() == MAX_PC_BONUS) {
  2605. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2606. return;
  2607. }
  2608. if (!rate)
  2609. return;
  2610. struct s_autospell entry = {};
  2611. entry.trigger_skill = src_skill;
  2612. entry.id = id;
  2613. entry.lv = lv;
  2614. entry.rate = cap_value(rate, -1000, 1000);
  2615. entry.card_id = card_id;
  2616. entry.flag = flag;
  2617. spell.push_back(entry);
  2618. }
  2619. /**
  2620. * Add inflict effect bonus for player while attacking/attacked
  2621. * @param effect: Effect array
  2622. * @param sc: SC/Effect type
  2623. * @param rate: Success chance
  2624. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2625. * @param flag: Target flag
  2626. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2627. */
  2628. static void pc_bonus_addeff(std::vector<s_addeffect> &effect, enum sc_type sc, int32 rate, int16 arrow_rate, unsigned char flag, uint32 duration)
  2629. {
  2630. if (effect.size() == MAX_PC_BONUS) {
  2631. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2632. return;
  2633. }
  2634. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2635. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2636. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2637. flag |= ATF_TARGET; //Default target: enemy.
  2638. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2639. flag |= ATF_WEAPON; //Default type: weapon.
  2640. if (!duration)
  2641. duration = (uint32)skill_get_time2(status_db.getSkill(sc), 7);
  2642. for (auto &it : effect) {
  2643. if (it.sc == sc && it.flag == flag) {
  2644. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2645. it.arrow_rate = util::safe_addition_cap(it.arrow_rate, arrow_rate, (int16)SHRT_MAX);
  2646. it.duration = umax(it.duration, duration);
  2647. return;
  2648. }
  2649. }
  2650. struct s_addeffect entry = {};
  2651. entry.sc = sc;
  2652. entry.rate = rate;
  2653. entry.arrow_rate = arrow_rate;
  2654. entry.flag = flag;
  2655. entry.duration = duration;
  2656. effect.push_back(entry);
  2657. }
  2658. /**
  2659. * Add inflict effect bonus for player while attacking using skill
  2660. * @param effect: Effect array
  2661. * @param sc: SC/Effect type
  2662. * @param rate: Success chance
  2663. * @param skill_id: Skill to cast
  2664. * @param target: Target type
  2665. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2666. */
  2667. static void pc_bonus_addeff_onskill(std::vector<s_addeffectonskill> &effect, enum sc_type sc, int32 rate, int16 skill_id, unsigned char target, uint32 duration)
  2668. {
  2669. if (effect.size() == MAX_PC_BONUS) {
  2670. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2671. return;
  2672. }
  2673. if (!duration)
  2674. duration = (uint32)skill_get_time2(status_db.getSkill(sc), 7);
  2675. for (auto &it : effect) {
  2676. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2677. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2678. it.duration = umax(it.duration, duration);
  2679. return;
  2680. }
  2681. }
  2682. struct s_addeffectonskill entry = {};
  2683. entry.sc = sc;
  2684. entry.rate = rate;
  2685. entry.skill_id = skill_id;
  2686. entry.target = target;
  2687. entry.duration = duration;
  2688. effect.push_back(entry);
  2689. }
  2690. /**
  2691. * Adjust/add drop rate modifier for player
  2692. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2693. * @param nameid: item id that will be dropped
  2694. * @param group: group id
  2695. * @param class_: target class
  2696. * @param race: target race. if < 0, means monster_id
  2697. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2698. */
  2699. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int32 class_, int16 race, int32 rate)
  2700. {
  2701. if (!nameid && !group) {
  2702. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2703. return;
  2704. }
  2705. if (nameid && !item_db.exists(nameid)) {
  2706. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2707. return;
  2708. }
  2709. if (group && !itemdb_group.exists(group)) {
  2710. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2711. return;
  2712. }
  2713. if (drop.size() == MAX_PC_BONUS) {
  2714. 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);
  2715. return;
  2716. }
  2717. //Apply config rate adjustment settings.
  2718. if (rate >= 0) { //Absolute drop.
  2719. if (battle_config.item_rate_adddrop != 100)
  2720. rate = rate*battle_config.item_rate_adddrop/100;
  2721. if (rate < battle_config.item_drop_adddrop_min)
  2722. rate = battle_config.item_drop_adddrop_min;
  2723. else if (rate > battle_config.item_drop_adddrop_max)
  2724. rate = battle_config.item_drop_adddrop_max;
  2725. } else { //Relative drop, max/min limits are applied at drop time.
  2726. if (battle_config.item_rate_adddrop != 100)
  2727. rate = rate*battle_config.item_rate_adddrop/100;
  2728. if (rate > -1)
  2729. rate = -1;
  2730. }
  2731. for (auto &it : drop) {
  2732. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2733. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2734. it.rate = util::safe_addition_cap(it.rate, rate, 10000);
  2735. return;
  2736. }
  2737. }
  2738. struct s_add_drop entry = {};
  2739. entry.nameid = nameid;
  2740. entry.group = group;
  2741. entry.race = race;
  2742. entry.class_ = class_;
  2743. entry.rate = cap_value(rate, -10000, 10000);
  2744. drop.push_back(entry);
  2745. }
  2746. s_autobonus::~s_autobonus(){
  2747. if( this->active != INVALID_TIMER ){
  2748. delete_timer( this->active, pc_endautobonus );
  2749. this->active = INVALID_TIMER;
  2750. }
  2751. if( this->bonus_script != nullptr ){
  2752. aFree( this->bonus_script );
  2753. this->bonus_script = nullptr;
  2754. }
  2755. if( this->other_script != nullptr ){
  2756. aFree( this->other_script );
  2757. this->other_script = nullptr;
  2758. }
  2759. }
  2760. /**
  2761. * Add autobonus to player when attacking/attacked
  2762. * @param bonus: Bonus array
  2763. * @param script: Script to execute
  2764. * @param rate: Success chance
  2765. * @param dur: Duration
  2766. * @param flag: Battle flag/skill
  2767. * @param other_script: Secondary script to execute
  2768. * @param pos: Item equip position
  2769. * @param onskill: Skill used to trigger autobonus
  2770. * @return True on success or false otherwise
  2771. */
  2772. bool pc_addautobonus(std::vector<std::shared_ptr<s_autobonus>> &bonus, const char *script, int16 rate, uint32 dur, uint16 flag, const char *other_script, uint32 pos, bool onskill){
  2773. // Check if the same bonus already exists
  2774. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2775. // Compare based on position and bonus script
  2776. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2777. return false;
  2778. }
  2779. }
  2780. if (bonus.size() == MAX_PC_BONUS) {
  2781. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2782. return false;
  2783. }
  2784. if (!onskill) {
  2785. if (!(flag&BF_RANGEMASK))
  2786. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2787. if (!(flag&BF_WEAPONMASK))
  2788. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2789. if (!(flag&BF_SKILLMASK)) {
  2790. if (flag&(BF_MAGIC | BF_MISC))
  2791. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2792. if (flag&BF_WEAPON)
  2793. flag |= BF_NORMAL | BF_SKILL;
  2794. }
  2795. }
  2796. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2797. if (rate < -10000 || rate > 10000)
  2798. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2799. entry->rate = cap_value(rate, -10000, 10000);
  2800. entry->duration = dur;
  2801. entry->active = INVALID_TIMER;
  2802. entry->atk_type = flag;
  2803. entry->pos = pos;
  2804. entry->bonus_script = aStrdup(script);
  2805. entry->other_script = (other_script ? aStrdup(other_script) : nullptr);
  2806. bonus.push_back(entry);
  2807. return true;
  2808. }
  2809. /**
  2810. * Remove an autobonus from player
  2811. * @param sd: Player data
  2812. * @param bonus: Autobonus array
  2813. * @param restore: Run script on clearing or not
  2814. */
  2815. void pc_delautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2816. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2817. while( it != bonus.end() ){
  2818. std::shared_ptr<s_autobonus> b = *it;
  2819. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2820. uint32 equip_pos_idx = 0;
  2821. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2822. for (uint8 j = 0; j < EQI_MAX; j++) {
  2823. if (sd.equip_index[j] >= 0)
  2824. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2825. }
  2826. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2827. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2828. }else{
  2829. // Not all required items equipped anymore
  2830. restore = false;
  2831. }
  2832. }
  2833. if( restore ){
  2834. it++;
  2835. continue;
  2836. }
  2837. it = bonus.erase(it);
  2838. }
  2839. }
  2840. /**
  2841. * Execute autobonus on player
  2842. * @param sd: Player data
  2843. * @param autobonus: Autobonus to run
  2844. */
  2845. void pc_exeautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2846. {
  2847. if (autobonus->active != INVALID_TIMER)
  2848. delete_timer(autobonus->active, pc_endautobonus);
  2849. if( autobonus->other_script )
  2850. {
  2851. int32 j;
  2852. uint32 equip_pos_idx = 0;
  2853. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2854. for(j = 0; j < EQI_MAX; j++) {
  2855. if(sd.equip_index[j] >= 0)
  2856. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2857. }
  2858. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2859. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2860. }
  2861. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2862. status_calc_pc(&sd,SCO_FORCE);
  2863. }
  2864. /**
  2865. * Remove autobonus timer from player
  2866. */
  2867. TIMER_FUNC(pc_endautobonus){
  2868. map_session_data *sd = map_id2sd(id);
  2869. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2870. nullpo_ret(sd);
  2871. nullpo_ret(bonus);
  2872. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2873. if( autobonus->active == tid ){
  2874. autobonus->active = INVALID_TIMER;
  2875. break;
  2876. }
  2877. }
  2878. status_calc_pc(sd,SCO_FORCE);
  2879. return 0;
  2880. }
  2881. /**
  2882. * Add element bonus to player when attacking
  2883. * @param sd: Player data
  2884. * @param ele: Element to adjust
  2885. * @param rate: Success chance
  2886. * @param flag: Battle flag
  2887. */
  2888. static void pc_bonus_addele(map_session_data* sd, unsigned char ele, int16 rate, int16 flag)
  2889. {
  2890. nullpo_retv(sd);
  2891. struct weapon_data *wd = (sd->state.lr_flag != LR_FLAG_NONE ? &sd->left_weapon : &sd->right_weapon);
  2892. if (wd->addele2.size() == MAX_PC_BONUS) {
  2893. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2894. return;
  2895. }
  2896. if (!(flag&BF_RANGEMASK))
  2897. flag |= BF_SHORT | BF_LONG;
  2898. if (!(flag&BF_WEAPONMASK))
  2899. flag |= BF_WEAPON;
  2900. if (!(flag&BF_SKILLMASK)) {
  2901. if (flag&(BF_MAGIC | BF_MISC))
  2902. flag |= BF_SKILL;
  2903. if (flag&BF_WEAPON)
  2904. flag |= BF_NORMAL | BF_SKILL;
  2905. }
  2906. for (auto &it : wd->addele2) {
  2907. if (it.ele == ele && it.flag == flag) {
  2908. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  2909. return;
  2910. }
  2911. }
  2912. struct s_addele2 entry = {};
  2913. entry.ele = ele;
  2914. entry.rate = cap_value(rate, -10000, 10000);
  2915. entry.flag = flag;
  2916. wd->addele2.push_back(entry);
  2917. }
  2918. /**
  2919. * Reduce element bonus to player when attacking
  2920. * @param sd: Player data
  2921. * @param ele: Element to adjust
  2922. * @param rate: Success chance
  2923. * @param flag: Battle flag
  2924. */
  2925. static void pc_bonus_subele(map_session_data* sd, unsigned char ele, int16 rate, int16 flag)
  2926. {
  2927. nullpo_retv(sd);
  2928. if (sd->subele2.size() == MAX_PC_BONUS) {
  2929. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2930. return;
  2931. }
  2932. if (!(flag&BF_RANGEMASK))
  2933. flag |= BF_SHORT | BF_LONG;
  2934. if (!(flag&BF_WEAPONMASK))
  2935. flag |= BF_WEAPON;
  2936. if (!(flag&BF_SKILLMASK)) {
  2937. if (flag&(BF_MAGIC | BF_MISC))
  2938. flag |= BF_SKILL;
  2939. if (flag&BF_WEAPON)
  2940. flag |= BF_NORMAL | BF_SKILL;
  2941. }
  2942. for (auto &it : sd->subele2) {
  2943. if (it.ele == ele && it.flag == flag) {
  2944. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  2945. return;
  2946. }
  2947. }
  2948. struct s_addele2 entry = {};
  2949. entry.ele = ele;
  2950. entry.rate = cap_value(rate, -10000, 10000);
  2951. entry.flag = flag;
  2952. sd->subele2.push_back(entry);
  2953. }
  2954. /**
  2955. * Adjust race damage to target when attacking
  2956. * @param sd: Player data
  2957. * @param race: Race to adjust
  2958. * @param rate: Success chance
  2959. * @param flag: Battle flag
  2960. */
  2961. static void pc_bonus_subrace(map_session_data* sd, unsigned char race, int16 rate, int16 flag)
  2962. {
  2963. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2964. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2965. return;
  2966. }
  2967. if (!(flag&BF_RANGEMASK))
  2968. flag |= BF_SHORT | BF_LONG;
  2969. if (!(flag&BF_WEAPONMASK))
  2970. flag |= BF_WEAPON;
  2971. if (!(flag&BF_SKILLMASK)) {
  2972. if (flag&(BF_MAGIC | BF_MISC))
  2973. flag |= BF_SKILL;
  2974. if (flag&BF_WEAPON)
  2975. flag |= BF_NORMAL | BF_SKILL;
  2976. }
  2977. for (auto &it : sd->subrace3) {
  2978. if (it.race == race && it.flag == flag) {
  2979. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  2980. return;
  2981. }
  2982. }
  2983. struct s_addrace2 entry = {};
  2984. entry.race = race;
  2985. entry.rate = cap_value(rate, -10000, 10000);
  2986. entry.flag = flag;
  2987. sd->subrace3.push_back(entry);
  2988. }
  2989. /**
  2990. * General item bonus for player
  2991. * @param bonus: Bonus array
  2992. * @param id: Key
  2993. * @param val: Value
  2994. * @param cap_rate: If Value is a rate value that needs to be capped
  2995. */
  2996. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int32 val, bool cap_rate)
  2997. {
  2998. for (auto &it : bonus) {
  2999. if (it.id == id) {
  3000. if (cap_rate)
  3001. it.val = util::safe_addition_cap(it.val, val, 10000);
  3002. else
  3003. it.val += val;
  3004. return;
  3005. }
  3006. }
  3007. struct s_item_bonus entry = {};
  3008. entry.id = id;
  3009. entry.val = cap_rate ? cap_value(val, -10000, 10000) : val;
  3010. bonus.push_back(entry);
  3011. }
  3012. /**
  3013. * Remove HP/SP to player when attacking
  3014. * @param bonus: Bonus array
  3015. * @param rate: Success chance
  3016. * @param per: Percentage of HP/SP to vanish
  3017. * @param flag: Battle flag
  3018. */
  3019. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int32 flag) {
  3020. if (bonus.size() == MAX_PC_BONUS) {
  3021. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  3022. return;
  3023. }
  3024. if (!(flag&BF_RANGEMASK))
  3025. flag |= BF_SHORT | BF_LONG;
  3026. if (!(flag&BF_WEAPONMASK))
  3027. flag |= BF_WEAPON;
  3028. if (!(flag&BF_SKILLMASK)) {
  3029. if (flag&(BF_MAGIC | BF_MISC))
  3030. flag |= BF_SKILL;
  3031. if (flag&BF_WEAPON)
  3032. flag |= BF_NORMAL | BF_SKILL;
  3033. }
  3034. for (auto &it : bonus) {
  3035. if (it.flag == flag) {
  3036. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  3037. it.per += per;
  3038. return;
  3039. }
  3040. }
  3041. struct s_vanish_bonus entry = {};
  3042. entry.rate = cap_value(rate, -10000, 10000);
  3043. entry.per = per;
  3044. entry.flag = flag;
  3045. bonus.push_back(entry);
  3046. }
  3047. /*==========================================
  3048. * Add a bonus(type) to player sd
  3049. * format: bonus bBonusName,val;
  3050. * @param sd
  3051. * @param type Bonus type used by bBonusName
  3052. * @param val Value that usually for rate or fixed value
  3053. *------------------------------------------*/
  3054. void pc_bonus(map_session_data *sd,int32 type,int32 val)
  3055. {
  3056. struct status_data *status;
  3057. int32 bonus;
  3058. nullpo_retv(sd);
  3059. status = &sd->base_status;
  3060. switch(type){
  3061. case SP_STR:
  3062. case SP_AGI:
  3063. case SP_VIT:
  3064. case SP_INT:
  3065. case SP_DEX:
  3066. case SP_LUK:
  3067. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3068. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  3069. break;
  3070. case SP_POW:
  3071. case SP_STA:
  3072. case SP_WIS:
  3073. case SP_SPL:
  3074. case SP_CON:
  3075. case SP_CRT:
  3076. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3077. sd->indexed_bonus.param_bonus[type - SP_POW + PARAM_POW] += val;
  3078. break;
  3079. case SP_ATK1:
  3080. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3081. bonus = status->rhw.atk + val;
  3082. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  3083. }
  3084. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3085. bonus = status->lhw.atk + val;
  3086. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  3087. }
  3088. break;
  3089. case SP_ATK2:
  3090. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3091. bonus = status->rhw.atk2 + val;
  3092. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3093. }
  3094. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3095. bonus = status->lhw.atk2 + val;
  3096. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3097. }
  3098. break;
  3099. case SP_BASE_ATK:
  3100. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3101. #ifdef RENEWAL
  3102. bonus = sd->bonus.eatk + val;
  3103. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3104. #else
  3105. bonus = status->batk + val;
  3106. status->batk = cap_value(bonus, 0, USHRT_MAX);
  3107. #endif
  3108. }
  3109. break;
  3110. case SP_DEF1:
  3111. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3112. bonus = status->def + val;
  3113. #ifdef RENEWAL
  3114. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3115. #else
  3116. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3117. #endif
  3118. }
  3119. break;
  3120. case SP_DEF2:
  3121. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3122. bonus = status->def2 + val;
  3123. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3124. }
  3125. break;
  3126. case SP_MDEF1:
  3127. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3128. bonus = status->mdef + val;
  3129. #ifdef RENEWAL
  3130. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3131. #else
  3132. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3133. #endif
  3134. if( sd->state.lr_flag == LR_FLAG_SHIELD ) {//Shield, used for royal guard
  3135. sd->bonus.shieldmdef += bonus;
  3136. }
  3137. }
  3138. break;
  3139. case SP_MDEF2:
  3140. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3141. bonus = status->mdef2 + val;
  3142. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3143. }
  3144. break;
  3145. case SP_HIT:
  3146. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3147. bonus = status->hit + val;
  3148. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3149. } else
  3150. sd->bonus.arrow_hit+=val;
  3151. break;
  3152. case SP_FLEE1:
  3153. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3154. bonus = status->flee + val;
  3155. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3156. }
  3157. break;
  3158. case SP_FLEE2:
  3159. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3160. bonus = status->flee2 + val*10;
  3161. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3162. }
  3163. break;
  3164. case SP_CRITICAL:
  3165. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3166. bonus = status->cri + val*10;
  3167. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3168. } else
  3169. sd->bonus.arrow_cri += val*10;
  3170. break;
  3171. case SP_PATK:
  3172. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3173. bonus = status->patk + val;
  3174. status->patk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3175. }
  3176. break;
  3177. case SP_SMATK:
  3178. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3179. bonus = status->smatk + val;
  3180. status->smatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3181. }
  3182. break;
  3183. case SP_RES:
  3184. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3185. bonus = status->res + val;
  3186. status->res = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3187. }
  3188. break;
  3189. case SP_MRES:
  3190. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3191. bonus = status->mres + val;
  3192. status->mres = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3193. }
  3194. break;
  3195. case SP_HPLUS:
  3196. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3197. bonus = status->hplus + val;
  3198. status->hplus = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3199. }
  3200. break;
  3201. case SP_CRATE:
  3202. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3203. bonus = status->crate + val;
  3204. status->crate = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3205. }
  3206. break;
  3207. case SP_ATKELE:
  3208. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  3209. switch (sd->state.lr_flag)
  3210. {
  3211. case LR_FLAG_ARROW:
  3212. switch (sd->status.weapon) {
  3213. case W_BOW:
  3214. case W_REVOLVER:
  3215. case W_RIFLE:
  3216. case W_GATLING:
  3217. case W_SHOTGUN:
  3218. case W_GRENADE:
  3219. //Become weapon element.
  3220. status->rhw.ele=val;
  3221. break;
  3222. default: //Become arrow element.
  3223. sd->bonus.arrow_ele=val;
  3224. break;
  3225. }
  3226. break;
  3227. case LR_FLAG_WEAPON:
  3228. status->lhw.ele=val;
  3229. break;
  3230. default:
  3231. status->rhw.ele=val;
  3232. break;
  3233. }
  3234. break;
  3235. case SP_DEFELE:
  3236. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  3237. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3238. status->def_ele=val;
  3239. break;
  3240. case SP_MAXHP:
  3241. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3242. break;
  3243. sd->bonus.hp += val;
  3244. break;
  3245. case SP_MAXSP:
  3246. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3247. break;
  3248. sd->bonus.sp += val;
  3249. break;
  3250. case SP_MAXAP:
  3251. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3252. break;
  3253. sd->bonus.ap += val;
  3254. break;
  3255. case SP_MAXHPRATE:
  3256. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3257. sd->hprate+=val;
  3258. break;
  3259. case SP_MAXSPRATE:
  3260. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3261. sd->sprate+=val;
  3262. break;
  3263. case SP_MAXAPRATE:
  3264. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3265. sd->aprate += val;
  3266. break;
  3267. case SP_SPRATE:
  3268. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3269. sd->dsprate+=val;
  3270. break;
  3271. case SP_ATTACKRANGE:
  3272. switch (sd->state.lr_flag) {
  3273. case LR_FLAG_ARROW:
  3274. switch (sd->status.weapon) {
  3275. case W_BOW:
  3276. case W_REVOLVER:
  3277. case W_RIFLE:
  3278. case W_GATLING:
  3279. case W_SHOTGUN:
  3280. case W_GRENADE:
  3281. status->rhw.range += val;
  3282. }
  3283. break;
  3284. case LR_FLAG_WEAPON:
  3285. status->lhw.range += val;
  3286. break;
  3287. default:
  3288. status->rhw.range += val;
  3289. break;
  3290. }
  3291. break;
  3292. case SP_SPEED_RATE: //Non stackable increase
  3293. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3294. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  3295. break;
  3296. case SP_SPEED_ADDRATE: //Stackable increase
  3297. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3298. sd->bonus.speed_add_rate -= val;
  3299. break;
  3300. case SP_ASPD: //Raw increase
  3301. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3302. sd->bonus.aspd_add -= 10*val;
  3303. break;
  3304. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  3305. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3306. #ifndef RENEWAL_ASPD
  3307. status->aspd_rate -= 10*val;
  3308. #else
  3309. status->aspd_rate2 += val;
  3310. #endif
  3311. break;
  3312. case SP_HP_RECOV_RATE:
  3313. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3314. sd->hprecov_rate += val;
  3315. break;
  3316. case SP_SP_RECOV_RATE:
  3317. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3318. sd->sprecov_rate += val;
  3319. break;
  3320. case SP_CRITICAL_DEF:
  3321. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3322. sd->bonus.critical_def += val;
  3323. break;
  3324. case SP_NEAR_ATK_DEF:
  3325. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3326. sd->bonus.near_attack_def_rate += val;
  3327. break;
  3328. case SP_LONG_ATK_DEF:
  3329. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3330. sd->bonus.long_attack_def_rate += val;
  3331. break;
  3332. case SP_DOUBLE_RATE:
  3333. if (sd->state.lr_flag == LR_FLAG_NONE && sd->bonus.double_rate < val)
  3334. sd->bonus.double_rate = val;
  3335. break;
  3336. case SP_DOUBLE_ADD_RATE:
  3337. if (sd->state.lr_flag == LR_FLAG_NONE)
  3338. sd->bonus.double_add_rate += val;
  3339. break;
  3340. case SP_MATK_RATE:
  3341. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3342. sd->matk_rate += val;
  3343. break;
  3344. case SP_IGNORE_DEF_ELE:
  3345. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  3346. if (sd->state.lr_flag == LR_FLAG_NONE)
  3347. sd->right_weapon.ignore_def_ele |= 1<<val;
  3348. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3349. sd->left_weapon.ignore_def_ele |= 1<<val;
  3350. break;
  3351. case SP_IGNORE_DEF_RACE:
  3352. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  3353. if (sd->state.lr_flag == LR_FLAG_NONE)
  3354. sd->right_weapon.ignore_def_race |= 1<<val;
  3355. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3356. sd->left_weapon.ignore_def_race |= 1<<val;
  3357. break;
  3358. case SP_IGNORE_DEF_CLASS:
  3359. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  3360. if (sd->state.lr_flag == LR_FLAG_NONE)
  3361. sd->right_weapon.ignore_def_class |= 1<<val;
  3362. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3363. sd->left_weapon.ignore_def_class |= 1<<val;
  3364. break;
  3365. case SP_ATK_RATE:
  3366. #ifdef RENEWAL
  3367. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3368. sd->bonus.atk_rate += val;
  3369. #else
  3370. ShowError( "pc_bonus: %s is not supported in Pre-Renewal mode.\n", QUOTE( SP_ATK_RATE ) );
  3371. #endif
  3372. break;
  3373. case SP_MAGIC_ATK_DEF:
  3374. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3375. sd->bonus.magic_def_rate += val;
  3376. break;
  3377. case SP_MISC_ATK_DEF:
  3378. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3379. sd->bonus.misc_def_rate += val;
  3380. break;
  3381. case SP_IGNORE_MDEF_ELE:
  3382. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3383. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3384. sd->bonus.ignore_mdef_ele |= 1<<val;
  3385. break;
  3386. case SP_IGNORE_MDEF_RACE:
  3387. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3388. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3389. sd->bonus.ignore_mdef_race |= 1<<val;
  3390. break;
  3391. case SP_PERFECT_HIT_RATE:
  3392. if (sd->state.lr_flag != LR_FLAG_ARROW && sd->bonus.perfect_hit < val)
  3393. sd->bonus.perfect_hit = val;
  3394. break;
  3395. case SP_PERFECT_HIT_ADD_RATE:
  3396. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3397. sd->bonus.perfect_hit_add += val;
  3398. break;
  3399. case SP_CRITICAL_RATE:
  3400. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3401. sd->critical_rate+=val;
  3402. break;
  3403. case SP_DEF_RATIO_ATK_ELE:
  3404. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3405. if (sd->state.lr_flag == LR_FLAG_NONE)
  3406. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3407. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3408. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3409. break;
  3410. case SP_DEF_RATIO_ATK_RACE:
  3411. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3412. if (sd->state.lr_flag == LR_FLAG_NONE)
  3413. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3414. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3415. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3416. break;
  3417. case SP_DEF_RATIO_ATK_CLASS:
  3418. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3419. if (sd->state.lr_flag == LR_FLAG_NONE)
  3420. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3421. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3422. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3423. break;
  3424. case SP_HIT_RATE:
  3425. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3426. sd->hit_rate += val;
  3427. break;
  3428. case SP_FLEE_RATE:
  3429. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3430. sd->flee_rate += val;
  3431. break;
  3432. case SP_FLEE2_RATE:
  3433. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3434. sd->flee2_rate += val;
  3435. break;
  3436. case SP_DEF_RATE:
  3437. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3438. sd->def_rate += val;
  3439. break;
  3440. case SP_DEF2_RATE:
  3441. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3442. sd->def2_rate += val;
  3443. break;
  3444. case SP_MDEF_RATE:
  3445. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3446. sd->mdef_rate += val;
  3447. break;
  3448. case SP_MDEF2_RATE:
  3449. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3450. sd->mdef2_rate += val;
  3451. break;
  3452. case SP_PATK_RATE:
  3453. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3454. sd->patk_rate += val;
  3455. break;
  3456. case SP_SMATK_RATE:
  3457. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3458. sd->smatk_rate += val;
  3459. break;
  3460. case SP_RES_RATE:
  3461. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3462. sd->res_rate += val;
  3463. break;
  3464. case SP_MRES_RATE:
  3465. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3466. sd->mres_rate += val;
  3467. break;
  3468. case SP_HPLUS_RATE:
  3469. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3470. sd->hplus_rate += val;
  3471. break;
  3472. case SP_CRATE_RATE:
  3473. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3474. sd->crate_rate += val;
  3475. break;
  3476. case SP_RESTART_FULL_RECOVER:
  3477. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3478. sd->special_state.restart_full_recover = 1;
  3479. break;
  3480. case SP_NO_CASTCANCEL:
  3481. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3482. sd->special_state.no_castcancel = 1;
  3483. break;
  3484. case SP_NO_CASTCANCEL2:
  3485. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3486. sd->special_state.no_castcancel2 = 1;
  3487. break;
  3488. case SP_NO_SIZEFIX:
  3489. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3490. sd->special_state.no_sizefix = 1;
  3491. break;
  3492. case SP_NO_MAGIC_DAMAGE:
  3493. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3494. break;
  3495. val+= sd->special_state.no_magic_damage;
  3496. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3497. break;
  3498. case SP_NO_WEAPON_DAMAGE:
  3499. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3500. break;
  3501. val+= sd->special_state.no_weapon_damage;
  3502. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3503. break;
  3504. case SP_NO_MISC_DAMAGE:
  3505. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3506. break;
  3507. val+= sd->special_state.no_misc_damage;
  3508. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3509. break;
  3510. case SP_NO_GEMSTONE:
  3511. if (sd->state.lr_flag != LR_FLAG_ARROW && sd->special_state.no_gemstone != 2)
  3512. sd->special_state.no_gemstone = 1;
  3513. break;
  3514. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3515. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3516. sd->special_state.intravision = 1;
  3517. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3518. }
  3519. break;
  3520. case SP_NO_KNOCKBACK:
  3521. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3522. sd->special_state.no_knockback = 1;
  3523. break;
  3524. case SP_SPLASH_RANGE:
  3525. if(sd->bonus.splash_range < val)
  3526. sd->bonus.splash_range = val;
  3527. break;
  3528. case SP_SPLASH_ADD_RANGE:
  3529. sd->bonus.splash_add_range += val;
  3530. break;
  3531. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3532. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3533. sd->bonus.short_weapon_damage_return += val;
  3534. break;
  3535. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3536. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3537. sd->bonus.long_weapon_damage_return += val;
  3538. break;
  3539. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3540. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3541. sd->bonus.magic_damage_return += val;
  3542. break;
  3543. case SP_REDUCE_DAMAGE_RETURN:
  3544. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3545. sd->bonus.reduce_damage_return += val;
  3546. break;
  3547. case SP_ALL_STATS: // [Valaris]
  3548. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3549. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3550. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3551. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3552. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3553. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3554. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3555. }
  3556. break;
  3557. case SP_ALL_TRAIT_STATS:
  3558. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3559. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3560. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3561. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3562. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3563. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3564. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3565. }
  3566. break;
  3567. case SP_AGI_VIT: // [Valaris]
  3568. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3569. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3570. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3571. }
  3572. break;
  3573. case SP_AGI_DEX_STR: // [Valaris]
  3574. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3575. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3576. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3577. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3578. }
  3579. break;
  3580. case SP_PERFECT_HIDE: // [Valaris]
  3581. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3582. sd->special_state.perfect_hiding=1;
  3583. break;
  3584. case SP_UNBREAKABLE:
  3585. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3586. sd->bonus.unbreakable += val;
  3587. break;
  3588. case SP_UNBREAKABLE_WEAPON:
  3589. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3590. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3591. break;
  3592. case SP_UNBREAKABLE_ARMOR:
  3593. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3594. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3595. break;
  3596. case SP_UNBREAKABLE_HELM:
  3597. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3598. sd->bonus.unbreakable_equip |= EQP_HELM;
  3599. break;
  3600. case SP_UNBREAKABLE_SHIELD:
  3601. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3602. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3603. break;
  3604. case SP_UNBREAKABLE_GARMENT:
  3605. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3606. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3607. break;
  3608. case SP_UNBREAKABLE_SHOES:
  3609. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3610. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3611. break;
  3612. case SP_CLASSCHANGE: // [Valaris]
  3613. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3614. sd->bonus.classchange=val;
  3615. break;
  3616. case SP_SHORT_ATK_RATE:
  3617. if (sd->state.lr_flag != LR_FLAG_ARROW) //[Lupus] it should stack, too. As any other cards rate bonuses
  3618. sd->bonus.short_attack_atk_rate+=val;
  3619. break;
  3620. case SP_LONG_ATK_RATE:
  3621. if (sd->state.lr_flag != LR_FLAG_ARROW) //[Lupus] it should stack, too. As any other cards rate bonuses
  3622. sd->bonus.long_attack_atk_rate+=val;
  3623. break;
  3624. case SP_BREAK_WEAPON_RATE:
  3625. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3626. sd->bonus.break_weapon_rate+=val;
  3627. break;
  3628. case SP_BREAK_ARMOR_RATE:
  3629. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3630. sd->bonus.break_armor_rate+=val;
  3631. break;
  3632. case SP_ADD_STEAL_RATE:
  3633. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3634. sd->bonus.add_steal_rate+=val;
  3635. break;
  3636. case SP_DELAYRATE:
  3637. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3638. sd->bonus.delayrate -= val;
  3639. break;
  3640. case SP_CRIT_ATK_RATE:
  3641. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3642. sd->bonus.crit_atk_rate += val;
  3643. break;
  3644. case SP_CRIT_DEF_RATE:
  3645. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3646. sd->bonus.crit_def_rate += val;
  3647. break;
  3648. case SP_NO_REGEN:
  3649. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3650. sd->regen.state.block|=val;
  3651. break;
  3652. case SP_UNSTRIPABLE_WEAPON:
  3653. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3654. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3655. break;
  3656. case SP_UNSTRIPABLE:
  3657. case SP_UNSTRIPABLE_ARMOR:
  3658. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3659. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3660. break;
  3661. case SP_UNSTRIPABLE_HELM:
  3662. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3663. sd->bonus.unstripable_equip |= EQP_HELM;
  3664. break;
  3665. case SP_UNSTRIPABLE_SHIELD:
  3666. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3667. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3668. break;
  3669. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3670. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3671. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3672. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3673. } else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3674. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3675. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3676. }
  3677. break;
  3678. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3679. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3680. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3681. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3682. } else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3683. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3684. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3685. }
  3686. break;
  3687. case SP_SP_GAIN_VALUE:
  3688. if (sd->state.lr_flag == LR_FLAG_NONE)
  3689. sd->bonus.sp_gain_value += val;
  3690. break;
  3691. case SP_HP_GAIN_VALUE:
  3692. if (sd->state.lr_flag == LR_FLAG_NONE)
  3693. sd->bonus.hp_gain_value += val;
  3694. break;
  3695. case SP_LONG_SP_GAIN_VALUE:
  3696. if (sd->state.lr_flag == LR_FLAG_NONE)
  3697. sd->bonus.long_sp_gain_value += val;
  3698. break;
  3699. case SP_LONG_HP_GAIN_VALUE:
  3700. if (sd->state.lr_flag == LR_FLAG_NONE)
  3701. sd->bonus.long_hp_gain_value += val;
  3702. break;
  3703. case SP_MAGIC_SP_GAIN_VALUE:
  3704. if (sd->state.lr_flag == LR_FLAG_NONE)
  3705. sd->bonus.magic_sp_gain_value += val;
  3706. break;
  3707. case SP_MAGIC_HP_GAIN_VALUE:
  3708. if (sd->state.lr_flag == LR_FLAG_NONE)
  3709. sd->bonus.magic_hp_gain_value += val;
  3710. break;
  3711. case SP_ADD_HEAL_RATE:
  3712. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3713. sd->bonus.add_heal_rate += val;
  3714. break;
  3715. case SP_ADD_HEAL2_RATE:
  3716. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3717. sd->bonus.add_heal2_rate += val;
  3718. break;
  3719. case SP_ADD_ITEM_HEAL_RATE:
  3720. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3721. sd->bonus.itemhealrate2 += val;
  3722. break;
  3723. case SP_EMATK:
  3724. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3725. sd->bonus.ematk += val;
  3726. break;
  3727. case SP_EMATK_HIDDEN:
  3728. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3729. sd->bonus.ematk_hidden += val;
  3730. break;
  3731. case SP_ADD_VARIABLECAST:
  3732. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3733. sd->bonus.add_varcast += val;
  3734. break;
  3735. #ifdef RENEWAL_CAST
  3736. case SP_FIXCASTRATE:
  3737. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3738. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3739. break;
  3740. case SP_ADD_FIXEDCAST:
  3741. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3742. sd->bonus.add_fixcast += val;
  3743. break;
  3744. case SP_CASTRATE:
  3745. case SP_VARCASTRATE:
  3746. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3747. sd->bonus.varcastrate -= val;
  3748. break;
  3749. #else
  3750. case SP_ADD_FIXEDCAST:
  3751. case SP_FIXCASTRATE:
  3752. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3753. break;
  3754. case SP_VARCASTRATE:
  3755. case SP_CASTRATE:
  3756. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3757. sd->castrate += val;
  3758. break;
  3759. #endif
  3760. case SP_ADDMAXWEIGHT:
  3761. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3762. sd->add_max_weight += val;
  3763. break;
  3764. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3765. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3766. break;
  3767. case SP_ABSORB_DMG_MAXHP2:
  3768. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3769. break;
  3770. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3771. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3772. sd->bonus.critical_rangeatk += val*10;
  3773. else
  3774. sd->bonus.arrow_cri += val*10;
  3775. break;
  3776. case SP_WEAPON_ATK_RATE:
  3777. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3778. sd->bonus.weapon_atk_rate += val;
  3779. break;
  3780. case SP_WEAPON_MATK_RATE:
  3781. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3782. sd->bonus.weapon_matk_rate += val;
  3783. break;
  3784. case SP_NO_MADO_FUEL:
  3785. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3786. sd->special_state.no_mado_fuel = 1;
  3787. break;
  3788. case SP_NO_WALK_DELAY:
  3789. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3790. sd->special_state.no_walk_delay = 1;
  3791. break;
  3792. case SP_ADD_ITEM_SPHEAL_RATE:
  3793. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3794. sd->bonus.itemsphealrate2 += val;
  3795. break;
  3796. default:
  3797. if (current_equip_combo_pos > 0) {
  3798. 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);
  3799. }
  3800. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3801. 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);
  3802. }
  3803. else {
  3804. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3805. }
  3806. break;
  3807. }
  3808. }
  3809. /*==========================================
  3810. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3811. * format: bonus2 bBonusName,type2,val;
  3812. * @param sd
  3813. * @param type Bonus type used by bBonusName
  3814. * @param type2
  3815. * @param val Value that usually for rate or fixed value
  3816. *------------------------------------------*/
  3817. void pc_bonus2(map_session_data *sd,int32 type,int32 type2,int32 val)
  3818. {
  3819. nullpo_retv(sd);
  3820. switch(type){
  3821. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3822. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3823. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3824. sd->right_weapon.addele[type2]+=val;
  3825. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3826. sd->left_weapon.addele[type2]+=val;
  3827. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3828. sd->indexed_bonus.arrow_addele[type2]+=val;
  3829. break;
  3830. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3831. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3832. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3833. sd->right_weapon.addrace[type2]+=val;
  3834. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3835. sd->left_weapon.addrace[type2]+=val;
  3836. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3837. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3838. break;
  3839. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3840. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3841. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3842. sd->right_weapon.addclass[type2]+=val;
  3843. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3844. sd->left_weapon.addclass[type2]+=val;
  3845. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3846. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3847. break;
  3848. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3849. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3850. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3851. sd->right_weapon.addsize[type2]+=val;
  3852. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3853. sd->left_weapon.addsize[type2]+=val;
  3854. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3855. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3856. break;
  3857. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3858. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3859. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3860. sd->indexed_bonus.subele_script[type2] += val;
  3861. break;
  3862. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3863. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3864. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3865. sd->indexed_bonus.subrace[type2]+=val;
  3866. break;
  3867. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3868. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3869. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3870. sd->indexed_bonus.subclass[type2]+=val;
  3871. break;
  3872. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3873. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3874. 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);
  3875. break;
  3876. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3877. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3878. 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);
  3879. break;
  3880. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3881. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3882. break;
  3883. if (sd->reseff.size() == MAX_PC_BONUS) {
  3884. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3885. break;
  3886. }
  3887. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3888. break;
  3889. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3890. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3891. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3892. sd->indexed_bonus.magic_addele_script[type2] += val;
  3893. break;
  3894. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3895. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3896. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3897. sd->indexed_bonus.magic_addrace[type2]+=val;
  3898. break;
  3899. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3900. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3901. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3902. sd->indexed_bonus.magic_addclass[type2]+=val;
  3903. break;
  3904. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3905. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3906. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3907. sd->indexed_bonus.magic_addsize[type2]+=val;
  3908. break;
  3909. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3910. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3911. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3912. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3913. break;
  3914. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3915. struct weapon_data *wd = (sd->state.lr_flag != LR_FLAG_NONE ? &sd->left_weapon : &sd->right_weapon);
  3916. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3917. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3918. break;
  3919. }
  3920. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3921. }
  3922. break;
  3923. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3924. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3925. break;
  3926. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3927. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3928. break;
  3929. }
  3930. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3931. break;
  3932. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3933. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3934. break;
  3935. if (sd->add_def.size() == MAX_PC_BONUS) {
  3936. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3937. break;
  3938. }
  3939. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3940. break;
  3941. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3942. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3943. break;
  3944. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3945. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3946. break;
  3947. }
  3948. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3949. break;
  3950. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3951. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3952. sd->right_weapon.hp_drain_rate.rate += type2;
  3953. sd->right_weapon.hp_drain_rate.per += val;
  3954. }
  3955. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3956. sd->left_weapon.hp_drain_rate.rate += type2;
  3957. sd->left_weapon.hp_drain_rate.per += val;
  3958. }
  3959. break;
  3960. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3961. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3962. sd->right_weapon.sp_drain_rate.rate += type2;
  3963. sd->right_weapon.sp_drain_rate.per += val;
  3964. }
  3965. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3966. sd->left_weapon.sp_drain_rate.rate += type2;
  3967. sd->left_weapon.sp_drain_rate.per += val;
  3968. }
  3969. break;
  3970. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3971. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3972. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3973. }
  3974. break;
  3975. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3976. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3977. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3978. }
  3979. break;
  3980. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3981. if (sd->state.lr_flag != LR_FLAG_ARROW && sd->bonus.get_zeny_rate < val) {
  3982. sd->bonus.get_zeny_rate = val;
  3983. sd->bonus.get_zeny_num = type2;
  3984. }
  3985. break;
  3986. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3987. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3988. sd->bonus.get_zeny_rate += val;
  3989. sd->bonus.get_zeny_num += type2;
  3990. }
  3991. break;
  3992. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3993. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3994. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3995. break;
  3996. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3997. sd->special_state.bonus_coma = 1;
  3998. break;
  3999. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  4000. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  4001. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4002. break;
  4003. sd->indexed_bonus.weapon_coma_race[type2] += val;
  4004. sd->special_state.bonus_coma = 1;
  4005. break;
  4006. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  4007. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  4008. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4009. break;
  4010. sd->indexed_bonus.weapon_coma_class[type2] += val;
  4011. sd->special_state.bonus_coma = 1;
  4012. break;
  4013. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  4014. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4015. sd->indexed_bonus.weapon_atk[type2]+=val;
  4016. break;
  4017. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  4018. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4019. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  4020. break;
  4021. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  4022. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  4023. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4024. sd->indexed_bonus.critaddrace[type2] += val*10;
  4025. break;
  4026. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  4027. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4028. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4029. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  4030. break;
  4031. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  4032. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4033. break;
  4034. if (sd->skillatk.size() == MAX_PC_BONUS) {
  4035. 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);
  4036. break;
  4037. }
  4038. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  4039. break;
  4040. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  4041. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4042. break;
  4043. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4044. 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);
  4045. break;
  4046. }
  4047. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  4048. break;
  4049. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  4050. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4051. break;
  4052. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4053. 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);
  4054. break;
  4055. }
  4056. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  4057. break;
  4058. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  4059. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4060. break;
  4061. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4062. 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);
  4063. break;
  4064. }
  4065. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  4066. break;
  4067. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  4068. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4069. sd->hp_loss.value = type2;
  4070. sd->hp_loss.rate = val;
  4071. }
  4072. break;
  4073. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  4074. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4075. sd->hp_regen.value = type2;
  4076. sd->hp_regen.rate = val;
  4077. }
  4078. break;
  4079. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  4080. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4081. sd->percent_hp_regen.value = type2;
  4082. sd->percent_hp_regen.rate = val;
  4083. }
  4084. break;
  4085. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  4086. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4087. sd->percent_sp_regen.value = type2;
  4088. sd->percent_sp_regen.rate = val;
  4089. }
  4090. break;
  4091. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  4092. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  4093. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4094. sd->right_weapon.addrace2[type2] += val;
  4095. else
  4096. sd->left_weapon.addrace2[type2] += val;
  4097. break;
  4098. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  4099. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  4100. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4101. sd->indexed_bonus.subsize[type2]+=val;
  4102. break;
  4103. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  4104. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  4105. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4106. sd->indexed_bonus.weapon_subsize[type2] += val;
  4107. break;
  4108. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  4109. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  4110. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4111. sd->indexed_bonus.magic_subsize[type2]+=val;
  4112. break;
  4113. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  4114. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  4115. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4116. sd->indexed_bonus.subrace2[type2]+=val;
  4117. break;
  4118. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  4119. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4120. break;
  4121. if( !item_db.exists( type2 ) ){
  4122. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  4123. break;
  4124. }
  4125. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  4126. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4127. break;
  4128. }
  4129. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  4130. break;
  4131. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  4132. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4133. break;
  4134. if (!type2 || !itemdb_group.exists(type2)) {
  4135. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  4136. break;
  4137. }
  4138. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  4139. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4140. break;
  4141. }
  4142. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  4143. break;
  4144. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  4145. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  4146. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4147. sd->indexed_bonus.expaddrace[type2]+=val;
  4148. break;
  4149. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  4150. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  4151. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4152. sd->indexed_bonus.expaddclass[type2]+=val;
  4153. break;
  4154. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  4155. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  4156. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4157. sd->indexed_bonus.sp_gain_race[type2]+=val;
  4158. break;
  4159. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  4160. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4161. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  4162. break;
  4163. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  4164. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4165. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  4166. break;
  4167. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  4168. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4169. sd->sp_loss.value = type2;
  4170. sd->sp_loss.rate = val;
  4171. }
  4172. break;
  4173. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  4174. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4175. sd->sp_regen.value = type2;
  4176. sd->sp_regen.rate = val;
  4177. }
  4178. break;
  4179. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  4180. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  4181. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4182. sd->right_weapon.hp_drain_race[type2] += val;
  4183. }
  4184. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4185. sd->left_weapon.hp_drain_race[type2] += val;
  4186. }
  4187. break;
  4188. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  4189. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  4190. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4191. sd->right_weapon.sp_drain_race[type2] += val;
  4192. }
  4193. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4194. sd->left_weapon.sp_drain_race[type2] += val;
  4195. }
  4196. break;
  4197. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  4198. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  4199. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4200. sd->right_weapon.hp_drain_class[type2] += val;
  4201. }
  4202. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4203. sd->left_weapon.hp_drain_class[type2] += val;
  4204. }
  4205. break;
  4206. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  4207. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  4208. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4209. sd->right_weapon.sp_drain_class[type2] += val;
  4210. }
  4211. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4212. sd->left_weapon.sp_drain_class[type2] += val;
  4213. }
  4214. break;
  4215. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  4216. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  4217. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4218. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  4219. break;
  4220. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  4221. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  4222. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4223. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  4224. break;
  4225. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  4226. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  4227. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4228. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  4229. break;
  4230. case SP_SP_IGNORE_RES_RACE_RATE: // bonus2 bIgnoreResRaceRate,r,n;
  4231. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_RES_RACE_RATE);
  4232. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4233. sd->indexed_bonus.ignore_res_by_race[type2] += val;
  4234. break;
  4235. case SP_SP_IGNORE_MRES_RACE_RATE: // bonus2 bIgnoreMResRaceRate,r,n;
  4236. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_MRES_RACE_RATE);
  4237. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4238. sd->indexed_bonus.ignore_mres_by_race[type2] += val;
  4239. break;
  4240. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  4241. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  4242. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4243. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  4244. break;
  4245. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  4246. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4247. break;
  4248. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  4249. 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);
  4250. break;
  4251. }
  4252. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  4253. break;
  4254. case SP_SKILL_DELAY:
  4255. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4256. break;
  4257. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  4258. 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);
  4259. break;
  4260. }
  4261. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  4262. break;
  4263. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  4264. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4265. break;
  4266. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  4267. 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);
  4268. break;
  4269. }
  4270. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  4271. break;
  4272. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  4273. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4274. break;
  4275. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  4276. 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);
  4277. break;
  4278. }
  4279. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  4280. break;
  4281. #ifdef RENEWAL_CAST
  4282. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4283. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4284. break;
  4285. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  4286. 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);
  4287. break;
  4288. }
  4289. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  4290. break;
  4291. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4292. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4293. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4294. break;
  4295. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  4296. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4297. break;
  4298. }
  4299. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  4300. break;
  4301. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4302. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4303. break;
  4304. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  4305. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4306. break;
  4307. }
  4308. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  4309. break;
  4310. #else
  4311. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4312. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4313. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  4314. break;
  4315. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4316. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4317. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4318. break;
  4319. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4320. 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);
  4321. break;
  4322. }
  4323. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  4324. break;
  4325. #endif
  4326. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  4327. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4328. break;
  4329. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  4330. 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);
  4331. break;
  4332. }
  4333. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4334. break;
  4335. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4336. if (sd->subskill.size() == MAX_PC_BONUS) {
  4337. 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);
  4338. break;
  4339. }
  4340. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4341. break;
  4342. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4343. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4344. sd->indexed_bonus.subdefele[type2] += val;
  4345. break;
  4346. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4347. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4348. sd->indexed_bonus.coma_class[type2] += val;
  4349. sd->special_state.bonus_coma = 1;
  4350. break;
  4351. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4352. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4353. sd->indexed_bonus.coma_race[type2] += val;
  4354. sd->special_state.bonus_coma = 1;
  4355. break;
  4356. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4357. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4358. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4359. sd->indexed_bonus.magic_addrace2[type2] += val;
  4360. break;
  4361. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4362. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4363. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4364. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4365. break;
  4366. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4367. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4368. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4369. sd->indexed_bonus.dropaddrace[type2] += val;
  4370. break;
  4371. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4372. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4373. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4374. sd->indexed_bonus.dropaddclass[type2] += val;
  4375. break;
  4376. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4377. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4378. sd->indexed_bonus.magic_subdefele[type2] += val;
  4379. break;
  4380. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4381. if( sd->state.lr_flag == LR_FLAG_ARROW ){
  4382. break;
  4383. }
  4384. if( !item_db.exists( type2 ) ){
  4385. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4386. break;
  4387. }
  4388. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4389. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4390. break;
  4391. }
  4392. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4393. break;
  4394. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4395. if( sd->state.lr_flag == LR_FLAG_ARROW ){
  4396. break;
  4397. }
  4398. if( !type2 || !itemdb_group.exists( type2 ) ){
  4399. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4400. break;
  4401. }
  4402. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4403. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4404. break;
  4405. }
  4406. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4407. break;
  4408. default:
  4409. if (current_equip_combo_pos > 0) {
  4410. 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);
  4411. }
  4412. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4413. 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);
  4414. }
  4415. else {
  4416. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4417. }
  4418. break;
  4419. }
  4420. }
  4421. /**
  4422. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4423. * @param sd
  4424. * @param type Bonus type used by bBonusName
  4425. * @param type2
  4426. * @param type3
  4427. * @param val Value that usually for rate or fixed value
  4428. */
  4429. void pc_bonus3(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 val)
  4430. {
  4431. nullpo_retv(sd);
  4432. switch(type){
  4433. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4434. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4435. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4436. break;
  4437. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  4438. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4439. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  4440. break;
  4441. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4442. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4443. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4444. break;
  4445. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4446. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4447. {
  4448. int32 target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4449. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4450. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4451. }
  4452. break;
  4453. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4454. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4455. {
  4456. int32 target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4457. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4458. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4459. }
  4460. break;
  4461. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4462. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4463. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4464. break;
  4465. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4466. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4467. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4468. break;
  4469. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4470. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4471. 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);
  4472. break;
  4473. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4474. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4475. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4476. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4477. break;
  4478. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4479. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4480. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4481. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4482. break;
  4483. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4484. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4485. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4486. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4487. break;
  4488. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4489. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4490. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4491. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4492. break;
  4493. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4494. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4495. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4496. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4497. break;
  4498. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4499. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4500. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4501. sd->sp_vanish_race[type2].rate += type3;
  4502. sd->sp_vanish_race[type2].per += val;
  4503. }
  4504. break;
  4505. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4506. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4507. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4508. sd->hp_vanish_race[type2].rate += type3;
  4509. sd->hp_vanish_race[type2].per += val;
  4510. }
  4511. break;
  4512. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4513. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4514. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4515. }
  4516. break;
  4517. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4518. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4519. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4520. }
  4521. break;
  4522. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4523. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4524. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4525. break;
  4526. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4527. //if (type3 > sd->norecover_state_race[type2].rate) {
  4528. // sd->norecover_state_race[type2].rate = type3;
  4529. // sd->norecover_state_race[type2].tick = val;
  4530. // break;
  4531. //}
  4532. sd->norecover_state_race[type2].rate = type3;
  4533. sd->norecover_state_race[type2].tick = val;
  4534. break;
  4535. default:
  4536. if (current_equip_combo_pos > 0) {
  4537. 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);
  4538. }
  4539. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4540. 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);
  4541. }
  4542. else {
  4543. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4544. }
  4545. break;
  4546. }
  4547. }
  4548. /**
  4549. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4550. * @param sd
  4551. * @param type Bonus type used by bBonusName
  4552. * @param type2
  4553. * @param type3
  4554. * @param type4
  4555. * @param val Value that usually for rate or fixed value
  4556. */
  4557. void pc_bonus4(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 type4,int32 val)
  4558. {
  4559. nullpo_retv(sd);
  4560. switch(type){
  4561. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4562. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4563. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4564. break;
  4565. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4566. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4567. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4568. break;
  4569. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4570. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4571. {
  4572. int32 target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4573. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4574. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4575. }
  4576. break;
  4577. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4578. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4579. 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);
  4580. break;
  4581. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4582. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4583. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4584. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4585. break;
  4586. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4587. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4588. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4589. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4590. break;
  4591. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4592. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4593. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4594. break;
  4595. sd->def_set_race[type2].rate = type3;
  4596. sd->def_set_race[type2].tick = type4;
  4597. sd->def_set_race[type2].value = val;
  4598. break;
  4599. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4600. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4601. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4602. break;
  4603. sd->mdef_set_race[type2].rate = type3;
  4604. sd->mdef_set_race[type2].tick = type4;
  4605. sd->mdef_set_race[type2].value = val;
  4606. break;
  4607. default:
  4608. if (current_equip_combo_pos > 0) {
  4609. 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);
  4610. }
  4611. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4612. 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);
  4613. }
  4614. else {
  4615. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4616. }
  4617. break;
  4618. }
  4619. }
  4620. /**
  4621. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4622. * @param sd
  4623. * @param type Bonus type used by bBonusName
  4624. * @param type2
  4625. * @param type3
  4626. * @param type4
  4627. * @param val Value that usually for rate or fixed value
  4628. */
  4629. void pc_bonus5(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 type4,int32 type5,int32 val)
  4630. {
  4631. nullpo_retv(sd);
  4632. switch(type){
  4633. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4634. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4635. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4636. break;
  4637. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4638. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4639. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4640. break;
  4641. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4642. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4643. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4644. break;
  4645. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4646. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4647. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4648. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4649. break;
  4650. default:
  4651. if (current_equip_combo_pos > 0) {
  4652. 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);
  4653. }
  4654. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4655. 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);
  4656. }
  4657. else {
  4658. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4659. }
  4660. break;
  4661. }
  4662. }
  4663. /*==========================================
  4664. * Grants a player a given skill. Flag values are:
  4665. * 0 - Grant permanent skill to be bound to skill tree
  4666. * 1 - Grant an item skill (temporary)
  4667. * 2 - Like 1, except the level granted can stack with previously learned level.
  4668. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4669. *------------------------------------------*/
  4670. bool pc_skill(map_session_data* sd, uint16 skill_id, int32 level, enum e_addskill_type type) {
  4671. uint16 idx = 0;
  4672. nullpo_ret(sd);
  4673. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4674. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4675. return false;
  4676. }
  4677. if (level > MAX_SKILL_LEVEL) {
  4678. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4679. return false;
  4680. }
  4681. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4682. 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);
  4683. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4684. }
  4685. switch (type) {
  4686. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4687. sd->status.skill[idx].id = skill_id;
  4688. sd->status.skill[idx].lv = level;
  4689. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4690. if (level == 0) { //Remove skill.
  4691. sd->status.skill[idx].id = 0;
  4692. clif_deleteskill(*sd,skill_id);
  4693. } else
  4694. clif_addskill(*sd,skill_id);
  4695. 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.
  4696. status_calc_pc(sd, SCO_NONE);
  4697. break;
  4698. case ADDSKILL_TEMP: //Item bonus skill.
  4699. if (sd->status.skill[idx].id != 0) {
  4700. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4701. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4702. } else {
  4703. sd->status.skill[idx].id = skill_id;
  4704. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4705. }
  4706. sd->status.skill[idx].lv = max(sd->status.skill[idx].lv, level);
  4707. break;
  4708. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4709. if (sd->status.skill[idx].id != 0) {
  4710. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4711. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4712. } else {
  4713. sd->status.skill[idx].id = skill_id;
  4714. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4715. }
  4716. sd->status.skill[idx].lv += level;
  4717. break;
  4718. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4719. sd->status.skill[idx].id = skill_id;
  4720. sd->status.skill[idx].lv = level;
  4721. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4722. if (level == 0) { //Remove skill.
  4723. sd->status.skill[idx].id = 0;
  4724. clif_deleteskill(*sd,skill_id);
  4725. } else
  4726. clif_addskill(*sd,skill_id);
  4727. 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.
  4728. status_calc_pc(sd, SCO_NONE);
  4729. break;
  4730. default:
  4731. return false;
  4732. }
  4733. return true;
  4734. }
  4735. /**
  4736. * Set's a player's plagiarized skill.
  4737. * @param sd: Player
  4738. * @param skill_id: Skill to be plagiarized
  4739. * @param skill_lv: Skill level to be plagiarized
  4740. * @return True on success or false otherwise
  4741. */
  4742. bool pc_skill_plagiarism(map_session_data &sd, uint16 skill_id, uint16 skill_lv)
  4743. {
  4744. skill_id = skill_dummy2skill_id(skill_id);
  4745. uint16 idx = skill_get_index(skill_id);
  4746. // Use skill index, avoiding out-of-bound array [Cydh]
  4747. if (idx == 0) {
  4748. ShowWarning("pc_skill_plagiarism: invalid skill idx 0 for skill %d.\n", skill_id);
  4749. return false;
  4750. }
  4751. skill_lv = cap_value(skill_lv, 1, skill_get_max(skill_id));
  4752. int32 type = skill_isCopyable(&sd, skill_id);
  4753. if (type == 1) {
  4754. pc_skill_plagiarism_reset(sd, type);
  4755. sd.cloneskill_idx = idx;
  4756. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), skill_id);
  4757. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), skill_lv);
  4758. } else if (type == 2) {
  4759. pc_skill_plagiarism_reset(sd, type);
  4760. sd.reproduceskill_idx = idx;
  4761. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), skill_id);
  4762. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), skill_lv);
  4763. } else {
  4764. ShowWarning("pc_skill_plagiarism: skill %d is not copyable.\n", skill_id);
  4765. return false;
  4766. }
  4767. sd.status.skill[idx].id = skill_id;
  4768. sd.status.skill[idx].lv = static_cast<uint8>(skill_lv);
  4769. sd.status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
  4770. clif_addskill(sd, skill_id);
  4771. return true;
  4772. }
  4773. /**
  4774. * Clear plagiarized skills from a player.
  4775. * @param sd: Player
  4776. * @param type: 1 for Plagiarism or 2 for Reproduce
  4777. * @return True on success or false otherwise
  4778. */
  4779. bool pc_skill_plagiarism_reset(map_session_data &sd, uint8 type)
  4780. {
  4781. uint16 idx;
  4782. if (type == 1)
  4783. idx = sd.cloneskill_idx;
  4784. else if (type == 2)
  4785. idx = sd.reproduceskill_idx;
  4786. else {
  4787. ShowError("pc_skill_plagiarism_reset: Unknown type %d.\n", type);
  4788. return false;
  4789. }
  4790. if (sd.status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED) {
  4791. uint16 skill_id = sd.status.skill[idx].id;
  4792. sd.status.skill[idx].id = 0;
  4793. sd.status.skill[idx].lv = 0;
  4794. sd.status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4795. clif_deleteskill(sd, skill_id);
  4796. if (type == 1) {
  4797. sd.cloneskill_idx = 0;
  4798. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  4799. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  4800. }
  4801. else if (type == 2) {
  4802. sd.reproduceskill_idx = 0;
  4803. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), 0);
  4804. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  4805. }
  4806. }
  4807. return true;
  4808. }
  4809. /*==========================================
  4810. * Append a card to an item ?
  4811. *------------------------------------------*/
  4812. int32 pc_insert_card(map_session_data* sd, int32 idx_card, int32 idx_equip)
  4813. {
  4814. nullpo_ret(sd);
  4815. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4816. return 0;
  4817. }
  4818. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4819. return 0;
  4820. }
  4821. int32 i;
  4822. t_itemid nameid;
  4823. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4824. struct item_data* item_card = sd->inventory_data[idx_card];
  4825. if(item_eq == nullptr)
  4826. return 0; //Invalid item index.
  4827. if(item_card == nullptr)
  4828. return 0; //Invalid card index.
  4829. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4830. return 0; // target item missing
  4831. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4832. return 0; // target card missing
  4833. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4834. return 0; // only weapons and armor are allowed
  4835. if( item_card->type != IT_CARD )
  4836. return 0; // must be a card
  4837. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4838. return 0; // target must be identified
  4839. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4840. return 0; // card slots reserved for other purposes
  4841. if( (item_eq->equip & item_card->equip) == 0 )
  4842. return 0; // card cannot be compounded on this item type
  4843. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4844. return 0; // attempted to place shield card on left-hand weapon.
  4845. 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)) )
  4846. return 0; // specific accessory-card can only be inserted to specific accessory.
  4847. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4848. return 0; // item must be unequipped
  4849. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4850. if( i == item_eq->slots )
  4851. return 0; // no free slots
  4852. // remember the card id to insert
  4853. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4854. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4855. {// failed
  4856. clif_insert_card( *sd, idx_equip, idx_card, true );
  4857. }
  4858. else
  4859. {// success
  4860. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4861. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4862. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4863. clif_insert_card( *sd, idx_equip, idx_card, false );
  4864. }
  4865. return 0;
  4866. }
  4867. /**
  4868. * Returns the count of unidentified items with the option to identify too.
  4869. * @param sd: Player data
  4870. * @param identify_item: Whether or not to identify any unidentified items
  4871. * @return Unidentified items count
  4872. */
  4873. int32 pc_identifyall(map_session_data *sd, bool identify_item)
  4874. {
  4875. int32 unidentified_count = 0;
  4876. for (int32 i = 0; i < MAX_INVENTORY; i++) {
  4877. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4878. if (identify_item == true) {
  4879. sd->inventory.u.items_inventory[i].identify = 1;
  4880. clif_item_identified( *sd, i, false );
  4881. }
  4882. unidentified_count++;
  4883. }
  4884. }
  4885. return unidentified_count;
  4886. }
  4887. //
  4888. // Items
  4889. //
  4890. /*==========================================
  4891. * Update buying value by skills
  4892. *------------------------------------------*/
  4893. int32 pc_modifybuyvalue(map_session_data *sd,int32 orig_value)
  4894. {
  4895. int32 skill,val = orig_value,rate1 = 0,rate2 = 0;
  4896. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4897. rate1 = 5+skill*2-((skill==10)? 1:0);
  4898. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4899. rate2 = 5+skill*4;
  4900. if(rate1 < rate2) rate1 = rate2;
  4901. if(rate1)
  4902. val = (int32)((double)orig_value*(double)(100-rate1)/100.);
  4903. if(val < battle_config.min_shop_buy)
  4904. val = battle_config.min_shop_buy;
  4905. return val;
  4906. }
  4907. /*==========================================
  4908. * Update selling value by skills
  4909. *------------------------------------------*/
  4910. int32 pc_modifysellvalue(map_session_data *sd,int32 orig_value)
  4911. {
  4912. int32 skill,val = orig_value,rate = 0;
  4913. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4914. rate = 5+skill*2-((skill==10)? 1:0);
  4915. if(rate)
  4916. val = (int32)((double)orig_value*(double)(100+rate)/100.);
  4917. if (val < battle_config.min_shop_sell)
  4918. val = battle_config.min_shop_sell;
  4919. return val;
  4920. }
  4921. /*==========================================
  4922. * Checking if we have enough place on inventory for new item
  4923. * Make sure to take 30k as limit (for client I guess)
  4924. * @param sd
  4925. * @param nameid
  4926. * @param amount
  4927. * @return e_chkitem_result
  4928. *------------------------------------------*/
  4929. char pc_checkadditem(map_session_data *sd, t_itemid nameid, int32 amount)
  4930. {
  4931. int32 i;
  4932. struct item_data* data;
  4933. nullpo_ret(sd);
  4934. if(amount > MAX_AMOUNT)
  4935. return CHKADDITEM_OVERAMOUNT;
  4936. data = itemdb_search(nameid);
  4937. if(!itemdb_isstackable2(data))
  4938. return CHKADDITEM_NEW;
  4939. if( data->stack.inventory && amount > data->stack.amount )
  4940. return CHKADDITEM_OVERAMOUNT;
  4941. if (data->flag.guid)
  4942. return CHKADDITEM_NEW;
  4943. for(i=0;i<MAX_INVENTORY;i++){
  4944. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4945. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4946. 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 ) )
  4947. return CHKADDITEM_OVERAMOUNT;
  4948. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4949. if( i >= sd->status.inventory_slots ){
  4950. return CHKADDITEM_OVERAMOUNT;
  4951. }
  4952. return CHKADDITEM_EXIST;
  4953. }
  4954. }
  4955. return CHKADDITEM_NEW;
  4956. }
  4957. /*==========================================
  4958. * Return number of available place in inventory
  4959. * Each non stackable item will reduce place by 1
  4960. * @param sd
  4961. * @return Number of empty slots
  4962. *------------------------------------------*/
  4963. uint8 pc_inventoryblank(map_session_data *sd)
  4964. {
  4965. uint16 i;
  4966. uint8 b;
  4967. nullpo_ret(sd);
  4968. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4969. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4970. b++;
  4971. }
  4972. return b;
  4973. }
  4974. /**
  4975. * Attempts to remove zeny from player
  4976. * @param sd: Player
  4977. * @param zeny: Zeny removed
  4978. * @param type: Log type
  4979. * @param log_charid: (optional) From who to log (if not needed, use 0)
  4980. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4981. */
  4982. char pc_payzeny(map_session_data *sd, int32 zeny, enum e_log_pick_type type, uint32 log_charid)
  4983. {
  4984. nullpo_retr(2,sd);
  4985. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4986. if( zeny < 0 )
  4987. {
  4988. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4989. return 1;
  4990. }
  4991. if( sd->status.zeny < zeny )
  4992. return 1; //Not enough.
  4993. sd->status.zeny -= zeny;
  4994. clif_updatestatus(*sd,SP_ZENY);
  4995. log_zeny(*sd, type, log_charid, -zeny);
  4996. if( zeny > 0 && sd->state.showzeny ) {
  4997. char output[255];
  4998. sprintf(output, "Removed %dz.", zeny);
  4999. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5000. }
  5001. return 0;
  5002. }
  5003. /**
  5004. * Attempts to give zeny to player
  5005. * @param sd: Player
  5006. * @param type: Log type
  5007. * @param log_charid: (optional) From who to log (if not needed, use 0)
  5008. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  5009. */
  5010. char pc_getzeny(map_session_data *sd, int32 zeny, enum e_log_pick_type type, uint32 log_charid)
  5011. {
  5012. nullpo_retr(-1,sd);
  5013. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  5014. if( zeny < 0 )
  5015. {
  5016. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  5017. return 1;
  5018. }
  5019. if( zeny > MAX_ZENY - sd->status.zeny )
  5020. zeny = MAX_ZENY - sd->status.zeny;
  5021. sd->status.zeny += zeny;
  5022. clif_updatestatus(*sd,SP_ZENY);
  5023. log_zeny(*sd, type, log_charid, zeny);
  5024. if( zeny > 0 && sd->state.showzeny ) {
  5025. char output[255];
  5026. sprintf(output, "Gained %dz.", zeny);
  5027. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5028. }
  5029. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  5030. return 0;
  5031. }
  5032. /**
  5033. * Attempts to remove Cash Points from player
  5034. * @param sd: Player
  5035. * @param price: Total points (cash + kafra) the player has to pay
  5036. * @param points: Kafra points the player has to pay
  5037. * @param type: Log type
  5038. * @return -1: Not enough points, otherwise success (cash+points)
  5039. */
  5040. int32 pc_paycash(map_session_data *sd, int32 price, int32 points, e_log_pick_type type)
  5041. {
  5042. int32 cash;
  5043. nullpo_retr(-1,sd);
  5044. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5045. cash = price-points;
  5046. if( sd->cashPoints < cash || sd->kafraPoints < points )
  5047. {
  5048. 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);
  5049. return -1;
  5050. }
  5051. if( cash ){
  5052. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  5053. sd->cashPoints -= cash;
  5054. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  5055. }
  5056. if( points ){
  5057. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  5058. sd->kafraPoints -= points;
  5059. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  5060. }
  5061. if( battle_config.cashshop_show_points )
  5062. {
  5063. char output[CHAT_SIZE_MAX];
  5064. 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.
  5065. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5066. }
  5067. return cash+points;
  5068. }
  5069. /**
  5070. * Attempts to give Cash Points to player
  5071. * @param sd: Player
  5072. * @param cash: Cash points the player gets
  5073. * @param points: Kafra points the player gets
  5074. * @param type: Log type
  5075. * @return -1: Error, otherwise success (cash or points)
  5076. */
  5077. int32 pc_getcash(map_session_data *sd, int32 cash, int32 points, e_log_pick_type type)
  5078. {
  5079. char output[CHAT_SIZE_MAX];
  5080. nullpo_retr(-1,sd);
  5081. cash = cap_value(cash, 0, MAX_CASHPOINT); //prevent command UB
  5082. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5083. if( cash > 0 )
  5084. {
  5085. if( cash > MAX_CASHPOINT-sd->cashPoints )
  5086. {
  5087. 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);
  5088. cash = MAX_CASHPOINT-sd->cashPoints;
  5089. }
  5090. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  5091. sd->cashPoints += cash;
  5092. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  5093. if( battle_config.cashshop_show_points )
  5094. {
  5095. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  5096. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5097. }
  5098. return cash;
  5099. }
  5100. if( points > 0 )
  5101. {
  5102. if( points > MAX_KAFRAPOINT-sd->kafraPoints )
  5103. {
  5104. 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);
  5105. points = MAX_KAFRAPOINT-sd->kafraPoints;
  5106. }
  5107. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  5108. sd->kafraPoints += points;
  5109. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  5110. if( battle_config.cashshop_show_points )
  5111. {
  5112. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  5113. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5114. }
  5115. return points;
  5116. }
  5117. return -1; //shouldn't happen but just in case
  5118. }
  5119. /**
  5120. * Searching a specified itemid in inventory and return his stored index
  5121. * @param sd Player
  5122. * @param nameid Find this Item!
  5123. * @return Stored index in inventory, or -1 if not found.
  5124. **/
  5125. int16 pc_search_inventory(map_session_data *sd, t_itemid nameid) {
  5126. int16 i;
  5127. nullpo_retr(-1, sd);
  5128. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  5129. return ( i < MAX_INVENTORY ) ? i : -1;
  5130. }
  5131. /** Attempt to add a new item to player inventory
  5132. * @param sd
  5133. * @param item_data
  5134. * @param amount
  5135. * @param log_type
  5136. * @return
  5137. * 0 = success
  5138. * 1 = invalid itemid not found or negative amount
  5139. * 2 = overweight
  5140. * 3 = ?
  5141. * 4 = no free place found
  5142. * 5 = max amount reached
  5143. * 6 = ?
  5144. * 7 = stack limitation
  5145. */
  5146. enum e_additem_result pc_additem(map_session_data *sd,struct item *item,int32 amount,e_log_pick_type log_type) {
  5147. struct item_data *id;
  5148. int16 i;
  5149. uint32 w;
  5150. nullpo_retr(ADDITEM_INVALID, sd);
  5151. nullpo_retr(ADDITEM_INVALID, item);
  5152. if( item->nameid == 0 || amount <= 0 )
  5153. return ADDITEM_INVALID;
  5154. if( amount > MAX_AMOUNT )
  5155. return ADDITEM_OVERAMOUNT;
  5156. id = itemdb_search(item->nameid);
  5157. if( id->stack.inventory && amount > id->stack.amount )
  5158. {// item stack limitation
  5159. return ADDITEM_STACKLIMIT;
  5160. }
  5161. w = id->weight*amount;
  5162. if(sd->weight + w > sd->max_weight)
  5163. return ADDITEM_OVERWEIGHT;
  5164. if (id->flag.guid && !item->unique_id)
  5165. item->unique_id = pc_generate_unique_id(sd);
  5166. // Stackable | Non Rental
  5167. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  5168. for( i = 0; i < MAX_INVENTORY; i++ ) {
  5169. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  5170. sd->inventory.u.items_inventory[i].bound == item->bound &&
  5171. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  5172. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  5173. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  5174. 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 ) )
  5175. return ADDITEM_OVERAMOUNT;
  5176. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  5177. if( i >= sd->status.inventory_slots ){
  5178. return ADDITEM_OVERAMOUNT;
  5179. }
  5180. sd->inventory.u.items_inventory[i].amount += amount;
  5181. clif_additem(sd,i,amount,0);
  5182. break;
  5183. }
  5184. }
  5185. }else{
  5186. i = MAX_INVENTORY;
  5187. }
  5188. if (i >= MAX_INVENTORY) {
  5189. i = pc_search_inventory(sd,0);
  5190. if( i < 0 )
  5191. return ADDITEM_OVERITEM;
  5192. if( i >= sd->status.inventory_slots ){
  5193. return ADDITEM_OVERITEM;
  5194. }
  5195. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  5196. // clear equip and favorite fields first, just in case
  5197. if( item->equip )
  5198. sd->inventory.u.items_inventory[i].equip = 0;
  5199. if( item->favorite != 0 )
  5200. sd->inventory.u.items_inventory[i].favorite = 0;
  5201. if( item->equipSwitch )
  5202. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  5203. sd->inventory.u.items_inventory[i].amount = amount;
  5204. sd->inventory_data[i] = id;
  5205. sd->last_addeditem_index = i;
  5206. if (!itemdb_isstackable2(id) || id->flag.guid)
  5207. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  5208. clif_additem(sd,i,amount,0);
  5209. }
  5210. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  5211. sd->weight += w;
  5212. clif_updatestatus(*sd,SP_WEIGHT);
  5213. //Auto-equip
  5214. if(id->flag.autoequip)
  5215. pc_equipitem(sd, i, id->equip);
  5216. /* rental item check */
  5217. if( item->expire_time ) {
  5218. if( time(nullptr) > item->expire_time ) {
  5219. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  5220. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  5221. } else {
  5222. uint32 seconds = (uint32)( item->expire_time - time(nullptr) );
  5223. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  5224. pc_inventory_rental_add(sd, seconds);
  5225. }
  5226. }
  5227. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  5228. pc_show_questinfo(sd);
  5229. return ADDITEM_SUCCESS;
  5230. }
  5231. /*==========================================
  5232. * Remove an item at index n from inventory by amount.
  5233. * @param sd
  5234. * @param n Item index in inventory
  5235. * @param amount
  5236. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  5237. * @param reason Delete reason
  5238. * @param log_type e_log_pick_type
  5239. * @return 1 - invalid itemid or negative amount; 0 - Success
  5240. *------------------------------------------*/
  5241. char pc_delitem(map_session_data *sd,int32 n,int32 amount,int32 type, int16 reason, e_log_pick_type log_type)
  5242. {
  5243. nullpo_retr(1, sd);
  5244. 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)
  5245. return 1;
  5246. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  5247. sd->inventory.u.items_inventory[n].amount -= amount;
  5248. sd->weight -= sd->inventory_data[n]->weight*amount ;
  5249. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  5250. if(sd->inventory.u.items_inventory[n].equip)
  5251. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  5252. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  5253. sd->inventory_data[n] = nullptr;
  5254. }
  5255. if(!(type&1))
  5256. clif_delitem( *sd, n, amount, reason );
  5257. if(!(type&2))
  5258. clif_updatestatus(*sd,SP_WEIGHT);
  5259. pc_show_questinfo(sd);
  5260. return 0;
  5261. }
  5262. /*==========================================
  5263. * Attempt to drop an item.
  5264. * @param sd
  5265. * @param n Item index in inventory
  5266. * @param amount Amount of item
  5267. * @return False = fail; True = success
  5268. *------------------------------------------*/
  5269. bool pc_dropitem(map_session_data *sd,int32 n,int32 amount)
  5270. {
  5271. nullpo_retr(1, sd);
  5272. if(n < 0 || n >= MAX_INVENTORY)
  5273. return false;
  5274. if(amount <= 0)
  5275. return false;
  5276. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  5277. sd->inventory.u.items_inventory[n].amount <= 0 ||
  5278. sd->inventory.u.items_inventory[n].amount < amount ||
  5279. sd->state.trading || sd->state.vending ||
  5280. !sd->inventory_data[n] //pc_delitem would fail on this case.
  5281. )
  5282. return false;
  5283. if( sd->inventory.u.items_inventory[n].equipSwitch )
  5284. return false;
  5285. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  5286. {
  5287. clif_displaymessage (sd->fd, msg_txt(sd,271));
  5288. return false; //Can't drop items in nodrop mapflag maps.
  5289. }
  5290. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  5291. {
  5292. clif_displaymessage (sd->fd, msg_txt(sd,263));
  5293. return false;
  5294. }
  5295. // Bypass drop restriction in map_addflooritem because we've already checked it above
  5296. 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,
  5297. false, DIR_MAX, battle_config.item_stacking?BL_NUL:BL_ITEM))
  5298. {
  5299. return false;
  5300. }
  5301. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  5302. clif_dropitem( *sd, n, amount );
  5303. return true;
  5304. }
  5305. /*==========================================
  5306. * Attempt to pick up an item.
  5307. * @param sd
  5308. * @param fitem Item that will be picked
  5309. * @return False = fail; True = success
  5310. *------------------------------------------*/
  5311. bool pc_takeitem(map_session_data *sd,struct flooritem_data *fitem)
  5312. {
  5313. int32 flag = 0;
  5314. t_tick tick = gettick();
  5315. struct party_data *p = nullptr;
  5316. nullpo_ret(sd);
  5317. nullpo_ret(fitem);
  5318. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  5319. return false; // Distance is too far
  5320. if (sd->sc.cant.pickup)
  5321. return false;
  5322. if (sd->status.party_id)
  5323. p = party_search(sd->status.party_id);
  5324. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  5325. map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  5326. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  5327. if (!(p && p->party.item&1 &&
  5328. first_sd && first_sd->status.party_id == sd->status.party_id
  5329. ))
  5330. return false;
  5331. }
  5332. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  5333. map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  5334. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  5335. if (!(p && p->party.item&1 &&
  5336. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5337. (second_sd && second_sd->status.party_id == sd->status.party_id))
  5338. ))
  5339. return false;
  5340. }
  5341. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  5342. map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  5343. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  5344. if(!(p && p->party.item&1 &&
  5345. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5346. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  5347. (third_sd && third_sd->status.party_id == sd->status.party_id))
  5348. ))
  5349. return false;
  5350. }
  5351. }
  5352. }
  5353. }
  5354. //This function takes care of giving the item to whoever should have it, considering party-share options.
  5355. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  5356. clif_additem(sd,0,0,flag);
  5357. return true;
  5358. }
  5359. //Display pickup animation.
  5360. unit_stop_attack( &sd->bl );
  5361. clif_takeitem(sd->bl,fitem->bl);
  5362. if (fitem->mob_id &&
  5363. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  5364. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  5365. )
  5366. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  5367. map_clearflooritem(&fitem->bl);
  5368. return true;
  5369. }
  5370. /*==========================================
  5371. * Check if item is usable.
  5372. * Return:
  5373. * false = no
  5374. * true = yes
  5375. *------------------------------------------*/
  5376. bool pc_isUseitem(map_session_data *sd,int32 n)
  5377. {
  5378. struct item_data *item;
  5379. t_itemid nameid;
  5380. nullpo_ret(sd);
  5381. item = sd->inventory_data[n];
  5382. nameid = sd->inventory.u.items_inventory[n].nameid;
  5383. if( item == nullptr )
  5384. return false;
  5385. //Not consumable item
  5386. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  5387. return false;
  5388. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  5389. return true;
  5390. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5391. if(mapdata->getMapFlag(MF_NOITEMCONSUMPTION)) //consumable but mapflag prevent it
  5392. return false;
  5393. //Prevent mass item usage. [Skotlex]
  5394. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  5395. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  5396. )
  5397. return false;
  5398. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  5399. clif_msg( *sd, MSI_CANT_USE_WHEN_SITDOWN );
  5400. return false; // You cannot use this item while sitting.
  5401. }
  5402. if (sd->state.storage_flag && item->type != IT_CASH) {
  5403. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  5404. return false; // You cannot use this item while storage is open.
  5405. }
  5406. if (item->flag.dead_branch && (mapdata->getMapFlag(MF_NOBRANCH) || mapdata_flag_gvg2(mapdata)))
  5407. return false;
  5408. if( itemdb_group.item_exists( IG_MF_NOTELEPORT, nameid ) ){
  5409. if( ( mapdata->getMapFlag(MF_NOTELEPORT) || mapdata_flag_gvg2( mapdata ) ) ){
  5410. clif_skill_teleportmessage( *sd, NOTIFY_MAPINFO_CANT_TP );
  5411. return false;
  5412. }
  5413. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5414. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5415. return false;
  5416. }
  5417. }
  5418. if( itemdb_group.item_exists( IG_MF_NORETURN, nameid ) ){
  5419. if( mapdata->getMapFlag(MF_NORETURN) ){
  5420. return false;
  5421. }
  5422. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5423. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5424. return false;
  5425. }
  5426. }
  5427. if( itemdb_group.item_exists( IG_GIANT_FLY_WING, nameid ) ){
  5428. struct party_data *pd = party_search( sd->status.party_id );
  5429. if( pd ){
  5430. int32 i;
  5431. ARR_FIND( 0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader );
  5432. // User is not party leader
  5433. if( i == MAX_PARTY ){
  5434. clif_msg( *sd, MSI_CANNOT_PARTYCALL );
  5435. return false;
  5436. }
  5437. 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 ) );
  5438. // No party members found on same map
  5439. if( i == MAX_PARTY ){
  5440. clif_msg( *sd, MSI_NO_PARTYMEM_ON_THISMAP );
  5441. return false;
  5442. }
  5443. }else{
  5444. clif_msg( *sd, MSI_CANNOT_PARTYCALL );
  5445. return false;
  5446. }
  5447. }
  5448. switch( nameid ) {
  5449. case ITEMID_MERCENARY_RED_POTION:
  5450. case ITEMID_MERCENARY_BLUE_POTION:
  5451. case ITEMID_M_CENTER_POTION:
  5452. case ITEMID_M_AWAKENING_POTION:
  5453. case ITEMID_M_BERSERK_POTION:
  5454. if( sd->md == nullptr || sd->md->db == nullptr )
  5455. return false;
  5456. if( sd->md->sc.cant.consume )
  5457. return false;
  5458. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5459. return false;
  5460. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5461. return false;
  5462. break;
  5463. case ITEMID_NEURALIZER:
  5464. if( !mapdata->getMapFlag(MF_RESET) )
  5465. return false;
  5466. break;
  5467. }
  5468. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != nullptr )
  5469. return false; // Mercenary Scrolls
  5470. // Safe check type cash disappear when overweight [Napster]
  5471. if( item->flag.group || item->type == IT_CASH ){
  5472. #ifdef RENEWAL
  5473. // Check if the player is not overweighted
  5474. // In Renewal the limit is 70% weight and gives the same error message
  5475. if (pc_is70overweight(sd)) {
  5476. clif_msg_color( *sd, MSI_PICKUP_FAILED_ITEMCREATE, color_table[COLOR_RED] );
  5477. return 0;
  5478. }
  5479. #else
  5480. // Check if the player is not overweighted
  5481. // In Pre-Renewal the limit is 50% weight and gives a specific error message
  5482. if (pc_is50overweight(sd)) {
  5483. clif_msg_color( *sd, MSI_CANT_GET_ITEM_BECAUSE_WEIGHT, color_table[COLOR_RED] );
  5484. return 0;
  5485. }
  5486. #endif
  5487. // Check if the player has enough free spaces in the inventory
  5488. // Official servers use 10 as the minimum amount of slots required to get the items
  5489. // The <= is intentional, as in official servers you actually need an extra empty slot
  5490. // TODO: Count the items the player will get and check for the actual inventory space required std::max<size_t>( count, 10 )
  5491. if (pc_inventoryblank(sd) <= 10) {
  5492. #ifdef RENEWAL
  5493. clif_msg_color( *sd, MSI_PICKUP_FAILED_ITEMCREATE, color_table[COLOR_RED] );
  5494. #else
  5495. clif_msg_color( *sd, MSI_CANT_GET_ITEM_BECAUSE_COUNT, color_table[COLOR_RED] );
  5496. #endif
  5497. return 0;
  5498. }
  5499. }
  5500. //Gender check
  5501. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5502. return false;
  5503. //Required level check
  5504. if(item->elv && sd->status.base_level < (uint32)item->elv)
  5505. return false;
  5506. if(item->elvmax && sd->status.base_level > (uint32)item->elvmax)
  5507. return false;
  5508. //Not equipable by class. [Skotlex]
  5509. if (!pc_job_can_use_item(sd,item))
  5510. return false;
  5511. if (sd->sc.cant.consume)
  5512. return false;
  5513. if (!pc_isItemClass(sd,item))
  5514. return false;
  5515. //Dead Branch items
  5516. if( item->flag.dead_branch )
  5517. log_branch(sd);
  5518. return true;
  5519. }
  5520. /*==========================================
  5521. * Last checks to use an item.
  5522. * Return:
  5523. * 0 = fail
  5524. * 1 = success
  5525. *------------------------------------------*/
  5526. int32 pc_useitem(map_session_data *sd,int32 n)
  5527. {
  5528. t_tick tick = gettick();
  5529. int32 amount;
  5530. t_itemid nameid;
  5531. struct script_code *script;
  5532. struct item item;
  5533. struct item_data *id;
  5534. nullpo_ret(sd);
  5535. if (sd->state.mail_writing)
  5536. return 0;
  5537. if (sd->npc_id) {
  5538. if (sd->progressbar.npc_id) {
  5539. clif_progressbar_abort(sd);
  5540. return 0; // First item use attempt cancels the progress bar
  5541. }
  5542. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5543. #ifdef RENEWAL
  5544. clif_msg( *sd, MSI_BUSY );
  5545. #endif
  5546. return 0;
  5547. }
  5548. }
  5549. item = sd->inventory.u.items_inventory[n];
  5550. id = sd->inventory_data[n];
  5551. if (item.nameid == 0 || item.amount <= 0)
  5552. return 0;
  5553. if( !pc_isUseitem(sd,n) )
  5554. return 0;
  5555. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5556. nameid = id->nameid;
  5557. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5558. return 0;
  5559. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5560. if( id->flag.delay_consume > 0 ) {
  5561. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.getSCE(SC_ALL_RIDING) )
  5562. return 0;
  5563. else if( pc_issit(sd) )
  5564. return 0;
  5565. }
  5566. //Since most delay-consume items involve using a "skill-type" target cursor,
  5567. //perform a skill-use check before going through. [Skotlex]
  5568. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5569. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5570. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5571. return 0;
  5572. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5573. return 0;
  5574. /* on restricted maps the item is consumed but the effect is not used */
  5575. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5576. clif_msg( *sd, MSI_IMPOSSIBLE_USEITEM_AREA ); // This item cannot be used within this area
  5577. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5578. clif_useitemack(sd,n,item.amount-1,true);
  5579. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5580. }
  5581. return 0;/* regardless, effect is not run */
  5582. }
  5583. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5584. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5585. return 0;
  5586. }
  5587. sd->itemid = item.nameid;
  5588. sd->itemindex = n;
  5589. amount = item.amount;
  5590. script = id->script;
  5591. //Check if the item is to be consumed immediately [Skotlex]
  5592. if (id->flag.delay_consume > 0)
  5593. clif_useitemack(sd, n, amount, true);
  5594. else
  5595. {
  5596. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5597. {
  5598. clif_useitemack(sd, n, amount - 1, true);
  5599. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5600. }
  5601. else
  5602. clif_useitemack(sd, n, 0, false);
  5603. }
  5604. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5605. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5606. //Update item use time.
  5607. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5608. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5609. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5610. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5611. potion_flag = 0;
  5612. return 1;
  5613. }
  5614. /**
  5615. * Add item on cart for given index.
  5616. * @param sd
  5617. * @param item
  5618. * @param amount
  5619. * @param log_type
  5620. * @return See pc.hpp::e_additem_result
  5621. */
  5622. enum e_additem_result pc_cart_additem(map_session_data *sd,struct item *item,int32 amount,e_log_pick_type log_type)
  5623. {
  5624. struct item_data *data;
  5625. int32 i,w;
  5626. nullpo_retr(ADDITEM_INVALID, sd);
  5627. nullpo_retr(ADDITEM_INVALID, item);
  5628. if(item->nameid == 0 || amount <= 0)
  5629. return ADDITEM_INVALID;
  5630. if (itemdb_ishatched_egg(item))
  5631. return ADDITEM_INVALID;
  5632. data = itemdb_search(item->nameid);
  5633. if( data->stack.cart && amount > data->stack.amount )
  5634. {// item stack limitation
  5635. return ADDITEM_STACKLIMIT;
  5636. }
  5637. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5638. { // Check item trade restrictions [Skotlex]
  5639. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5640. return ADDITEM_INVALID;
  5641. }
  5642. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5643. return ADDITEM_OVERWEIGHT;
  5644. i = MAX_CART;
  5645. if( itemdb_isstackable2(data) && !item->expire_time )
  5646. {
  5647. for (i = 0; i < MAX_CART; i++) {
  5648. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5649. && sd->cart.u.items_cart[i].bound == item->bound
  5650. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5651. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5652. )
  5653. break;
  5654. }
  5655. }
  5656. if( i < MAX_CART )
  5657. {// item already in cart, stack it
  5658. 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 ) )
  5659. return ADDITEM_OVERAMOUNT; // no slot
  5660. sd->cart.u.items_cart[i].amount += amount;
  5661. clif_cart_additem(sd,i,amount);
  5662. }
  5663. else
  5664. {// item not stackable or not present, add it
  5665. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5666. if( i == MAX_CART )
  5667. return ADDITEM_OVERAMOUNT; // no slot
  5668. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5669. sd->cart.u.items_cart[i].id = 0;
  5670. sd->cart.u.items_cart[i].amount = amount;
  5671. sd->cart_num++;
  5672. clif_cart_additem(sd,i,amount);
  5673. }
  5674. sd->cart.u.items_cart[i].favorite = 0; // clear
  5675. sd->cart.u.items_cart[i].equipSwitch = 0;
  5676. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5677. sd->cart_weight += w;
  5678. clif_updatestatus(*sd,SP_CARTINFO);
  5679. return ADDITEM_SUCCESS;
  5680. }
  5681. /*==========================================
  5682. * Delete item on cart for given index.
  5683. *------------------------------------------*/
  5684. void pc_cart_delitem(map_session_data *sd,int32 n,int32 amount,int32 type,e_log_pick_type log_type)
  5685. {
  5686. nullpo_retv(sd);
  5687. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5688. sd->cart.u.items_cart[n].amount < amount)
  5689. return;
  5690. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5691. sd->cart.u.items_cart[n].amount -= amount;
  5692. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5693. if(sd->cart.u.items_cart[n].amount <= 0) {
  5694. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5695. sd->cart_num--;
  5696. }
  5697. if(!type) {
  5698. clif_cart_delitem( *sd, n, amount );
  5699. clif_updatestatus(*sd,SP_CARTINFO);
  5700. }
  5701. }
  5702. /*==========================================
  5703. * Transfer item from inventory to cart.
  5704. *------------------------------------------*/
  5705. void pc_putitemtocart(map_session_data *sd,int32 idx,int32 amount)
  5706. {
  5707. nullpo_retv(sd);
  5708. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5709. return;
  5710. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5711. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5712. return;
  5713. if( item_data->equipSwitch ){
  5714. clif_msg( *sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  5715. return;
  5716. }
  5717. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5718. if (flag == ADDITEM_SUCCESS)
  5719. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5720. else {
  5721. if (flag == ADDITEM_OVERAMOUNT)
  5722. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_COUNT );
  5723. else
  5724. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_WEIGHT );
  5725. clif_additem(sd, idx, amount, 0);
  5726. clif_delitem( *sd, idx, amount, 0 );
  5727. }
  5728. }
  5729. /*==========================================
  5730. * Get number of item in cart.
  5731. * Return:
  5732. -1 = itemid not found or no amount found
  5733. x = remaining itemid on cart after get
  5734. *------------------------------------------*/
  5735. int32 pc_cartitem_amount(map_session_data* sd, int32 idx, int32 amount)
  5736. {
  5737. struct item* item_data;
  5738. nullpo_retr(-1, sd);
  5739. item_data = &sd->cart.u.items_cart[idx];
  5740. if( item_data->nameid == 0 || item_data->amount == 0 )
  5741. return -1;
  5742. return item_data->amount - amount;
  5743. }
  5744. /*==========================================
  5745. * Retrieve an item at index idx from cart.
  5746. *------------------------------------------*/
  5747. bool pc_getitemfromcart(map_session_data *sd,int32 idx,int32 amount)
  5748. {
  5749. nullpo_retr(1, sd);
  5750. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5751. return false;
  5752. struct item *item_data=&sd->cart.u.items_cart[idx];
  5753. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5754. return false;
  5755. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5756. if (flag == ADDITEM_SUCCESS)
  5757. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5758. else {
  5759. clif_cart_delitem( *sd, idx, amount );
  5760. clif_additem(sd, idx, amount, flag);
  5761. clif_cart_additem(sd, idx, amount);
  5762. }
  5763. return true;
  5764. }
  5765. /*==========================================
  5766. * Bound Item Check
  5767. * Type:
  5768. * 1 Account Bound
  5769. * 2 Guild Bound
  5770. * 3 Party Bound
  5771. * 4 Character Bound
  5772. *------------------------------------------*/
  5773. int32 pc_bound_chk(TBL_PC *sd,enum bound_type type,int32 *idxlist)
  5774. {
  5775. int32 i = 0, j = 0;
  5776. for(i = 0; i < MAX_INVENTORY; i++) {
  5777. 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) {
  5778. idxlist[j] = i;
  5779. j++;
  5780. }
  5781. }
  5782. return j;
  5783. }
  5784. /*==========================================
  5785. * Display item stolen msg to player sd
  5786. *------------------------------------------*/
  5787. int32 pc_show_steal(struct block_list *bl,va_list ap)
  5788. {
  5789. map_session_data *sd;
  5790. t_itemid itemid;
  5791. char output[100];
  5792. sd=va_arg(ap,map_session_data *);
  5793. itemid=va_arg(ap,int32);
  5794. std::shared_ptr<item_data> id = item_db.find(itemid);
  5795. if(id == nullptr)
  5796. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5797. else
  5798. sprintf(output,"%s stole %s.",sd->status.name,id->ename.c_str());
  5799. clif_displaymessage( ((map_session_data *)bl)->fd, output);
  5800. return 0;
  5801. }
  5802. /**
  5803. * Steal an item from bl (mob).
  5804. * @param sd: Player data
  5805. * @param bl: Object to steal from
  5806. * @param skill_lv: Level of skill used
  5807. * @return True on success or false otherwise
  5808. */
  5809. bool pc_steal_item(map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5810. {
  5811. int32 i;
  5812. t_itemid itemid;
  5813. double rate;
  5814. unsigned char flag = 0;
  5815. struct mob_data *md;
  5816. if(!sd || !bl || bl->type!=BL_MOB)
  5817. return false;
  5818. md = (TBL_MOB *)bl;
  5819. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5820. return false;
  5821. status_data* sd_status = status_get_status_data(sd->bl);
  5822. status_data* md_status = status_get_status_data(*bl);
  5823. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5824. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5825. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5826. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5827. ) { //Can't steal from
  5828. md->state.steal_flag = UCHAR_MAX;
  5829. return false;
  5830. }
  5831. // base skill success chance (percentual)
  5832. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5833. rate += sd->bonus.add_steal_rate;
  5834. if( rate < 1
  5835. #ifdef RENEWAL
  5836. || rnd()%100 >= rate
  5837. #endif
  5838. )
  5839. return false;
  5840. // Try dropping one item, in the order from first to last possible slot.
  5841. // Droprate is affected by the skill success rate.
  5842. for( i = 0; i < MAX_MOB_DROP; i++ )
  5843. if( item_db.exists(md->db->dropitem[i].nameid) && !md->db->dropitem[i].steal_protected && rnd() % 10000 < md->db->dropitem[i].rate
  5844. #ifndef RENEWAL
  5845. * rate/100.
  5846. #endif
  5847. )
  5848. break;
  5849. if( i == MAX_MOB_DROP )
  5850. return false;
  5851. itemid = md->db->dropitem[i].nameid;
  5852. struct item tmp_item = {};
  5853. tmp_item.nameid = itemid;
  5854. tmp_item.amount = 1;
  5855. tmp_item.identify = itemdb_isidentified(itemid);
  5856. if( battle_config.skill_steal_random_options ){
  5857. mob_setdropitem_option( tmp_item, md->db->dropitem[i] );
  5858. }
  5859. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5860. //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?
  5861. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5862. if(flag) { //Failed to steal due to overweight
  5863. clif_additem(sd,0,0,flag);
  5864. return false;
  5865. }
  5866. if(battle_config.show_steal_in_same_party)
  5867. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5868. //Logs items, Stolen from mobs [Lupus]
  5869. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5870. //A Rare Steal Global Announce by Lupus
  5871. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5872. struct item_data *i_data;
  5873. char message[128];
  5874. i_data = itemdb_search(itemid);
  5875. 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);
  5876. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5877. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5878. }
  5879. return true;
  5880. }
  5881. /*==========================================
  5882. * Stole zeny from bl (mob)
  5883. * return
  5884. * 0 = fail
  5885. * 1 = success
  5886. *------------------------------------------*/
  5887. int32 pc_steal_coin(map_session_data *sd,struct block_list *target)
  5888. {
  5889. int32 rate, target_lv;
  5890. struct mob_data *md;
  5891. if(!sd || !target || target->type != BL_MOB)
  5892. return 0;
  5893. md = (TBL_MOB*)target;
  5894. target_lv = status_get_lv(target);
  5895. if (md->state.steal_coin_flag || md->sc.getSCE(SC_STONE) || md->sc.getSCE(SC_FREEZE) || md->sc.getSCE(SC_HANDICAPSTATE_FROSTBITE) ||
  5896. md->sc.getSCE(SC_HANDICAPSTATE_SWOONING) || md->sc.getSCE(SC_HANDICAPSTATE_LIGHTNINGSTRIKE) || md->sc.getSCE(SC_HANDICAPSTATE_CRYSTALLIZATION) ||
  5897. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5898. return 0;
  5899. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5900. if(rnd()%1000 < rate)
  5901. {
  5902. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5903. int32 amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5904. pc_getzeny(sd, amount, LOG_TYPE_STEAL);
  5905. md->state.steal_coin_flag = 1;
  5906. return 1;
  5907. }
  5908. return 0;
  5909. }
  5910. /*==========================================
  5911. * Set's a player position.
  5912. * @param sd
  5913. * @param mapindex
  5914. * @param x
  5915. * @param y
  5916. * @param clrtype
  5917. * @return SETPOS_OK Success
  5918. * SETPOS_MAPINDEX Invalid map index
  5919. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5920. * SETPOS_AUTOTRADE Player is in autotrade state
  5921. *------------------------------------------*/
  5922. enum e_setpos pc_setpos(map_session_data* sd, uint16 mapindex, int32 x, int32 y, clr_type clrtype)
  5923. {
  5924. nullpo_retr(SETPOS_OK,sd);
  5925. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5926. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5927. return SETPOS_MAPINDEX;
  5928. }
  5929. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5930. return SETPOS_AUTOTRADE;
  5931. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5932. pc_setstand(sd, true);
  5933. pc_setrestartvalue(sd,1);
  5934. }
  5935. int16 m = map_mapindex2mapid(mapindex);
  5936. struct map_data *mapdata = map_getmapdata(m);
  5937. status_change *sc = status_get_sc(&sd->bl);
  5938. sd->state.changemap = (sd->mapindex != mapindex);
  5939. sd->state.warping = 1;
  5940. sd->state.workinprogress = WIP_DISABLE_NONE;
  5941. sd->state.mail_writing = false;
  5942. sd->state.refineui_open = false;
  5943. if( sd->state.changemap ) { // Misc map-changing settings
  5944. int32 curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5945. if (curr_map_instance_id != new_map_instance_id) {
  5946. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5947. instance_delusers(curr_map_instance_id);
  5948. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5949. }
  5950. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5951. instance_addusers(new_map_instance_id);
  5952. }
  5953. if (sd->bg_id && mapdata && !mapdata->getMapFlag(MF_BATTLEGROUND)) // Moving to a map that isn't a Battlegrounds
  5954. bg_team_leave(sd, false, true);
  5955. sd->state.pmap = sd->bl.m;
  5956. if (sc != nullptr && !sc->empty()) { // Cancel some map related stuff.
  5957. if (sc->cant.warp)
  5958. return SETPOS_MAPINDEX; // You may not get out!
  5959. for (const auto &it : status_db) {
  5960. if (sc->getSCE(it.first)) {
  5961. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5962. status_change_end(&sd->bl, static_cast<sc_type>(it.first));
  5963. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5964. status_change_entry *sce = sd->sc.getSCE(it.first);
  5965. if (sce->timer != INVALID_TIMER)
  5966. delete_timer(sce->timer, status_change_timer);
  5967. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5968. }
  5969. }
  5970. }
  5971. }
  5972. for(int32 i = 0; i < EQI_MAX; i++ ) {
  5973. if( sd->equip_index[i] >= 0 )
  5974. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  5975. pc_unequipitem(sd,sd->equip_index[i],2);
  5976. }
  5977. }
  5978. if (battle_config.clear_unit_onwarp&BL_PC)
  5979. skill_clear_unitgroup(&sd->bl);
  5980. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5981. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5982. }
  5983. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5984. guild_send_dot_remove(sd);
  5985. bg_send_dot_remove(sd);
  5986. if (sd->regen.state.gc)
  5987. sd->regen.state.gc = 0;
  5988. if (mapdata) {
  5989. // make sure vending is allowed here
  5990. if (sd->state.vending && mapdata->getMapFlag(MF_NOVENDING)) {
  5991. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5992. vending_closevending(sd);
  5993. }
  5994. // make sure buyingstore is allowed here
  5995. if (sd->state.buyingstore && mapdata->getMapFlag(MF_NOBUYINGSTORE)) {
  5996. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5997. buyingstore_close(sd);
  5998. }
  5999. }
  6000. channel_pcquit(sd,4); //quit map chan
  6001. // Remove Cloaked NPCs on map change
  6002. sd->cloaked_npc.clear();
  6003. }
  6004. if( m < 0 )
  6005. {
  6006. uint32 ip;
  6007. uint16 port;
  6008. struct script_state *st;
  6009. //if can't find any map-servers, just abort setting position.
  6010. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  6011. return SETPOS_NO_MAPSERVER;
  6012. if (sd->npc_id){
  6013. npc_event_dequeue(sd,false);
  6014. st = sd->st;
  6015. }else{
  6016. st = nullptr;
  6017. }
  6018. if (sd->bg_id) // Switching map servers, remove from bg
  6019. bg_team_leave(sd, false, true);
  6020. if (sd->state.vending) // Stop vending
  6021. vending_closevending(sd);
  6022. if (sd->state.buyingstore) // Stop buyingstore
  6023. buyingstore_close(sd);
  6024. npc_script_event( *sd, NPCE_LOGOUT );
  6025. //remove from map, THEN change x/y coordinates
  6026. unit_remove_map_pc(sd,clrtype);
  6027. sd->mapindex = mapindex;
  6028. sd->bl.x=x;
  6029. sd->bl.y=y;
  6030. pc_clean_skilltree(sd);
  6031. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  6032. chrif_changemapserver(sd, ip, (int16)port);
  6033. //Free session data from this map server [Kevin]
  6034. unit_free_pc(sd);
  6035. if( st ){
  6036. // Has to be done here, because otherwise unit_free_pc will free the stack already
  6037. st->state = END;
  6038. }
  6039. return SETPOS_OK;
  6040. }
  6041. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  6042. {
  6043. 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);
  6044. x = y = 0; // make it random
  6045. }
  6046. if( x == 0 && y == 0 ) { // pick a random walkable cell
  6047. int32 c=0;
  6048. do {
  6049. x = rnd()%(mapdata->xs-2)+1;
  6050. y = rnd()%(mapdata->ys-2)+1;
  6051. c++;
  6052. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  6053. 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));
  6054. return SETPOS_OK; //preventing warp
  6055. //break; //allow warp anyway
  6056. }
  6057. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  6058. }
  6059. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  6060. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  6061. vending_closevending(sd);
  6062. }
  6063. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  6064. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  6065. buyingstore_close(sd);
  6066. }
  6067. if(sd->bl.prev != nullptr){
  6068. unit_remove_map_pc(sd,clrtype);
  6069. clif_changemap( *sd, m, x, y );
  6070. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  6071. sd->state.rewarp = 1;
  6072. sd->mapindex = mapindex;
  6073. sd->bl.m = m;
  6074. sd->bl.x = sd->ud.to_x = x;
  6075. sd->bl.y = sd->ud.to_y = y;
  6076. if( sd->status.guild_id > 0 && mapdata->getMapFlag(MF_GVG_CASTLE) )
  6077. { // Increased guild castle regen [Valaris]
  6078. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  6079. if(gc && gc->guild_id == sd->status.guild_id)
  6080. sd->regen.state.gc = 1;
  6081. }
  6082. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  6083. {
  6084. sd->pd->bl.m = m;
  6085. sd->pd->bl.x = sd->pd->ud.to_x = x;
  6086. sd->pd->bl.y = sd->pd->ud.to_y = y;
  6087. sd->pd->ud.dir = sd->ud.dir;
  6088. }
  6089. if( hom_is_active(sd->hd) )
  6090. {
  6091. if (sd->state.changemap)
  6092. status_db.removeByStatusFlag(&sd->hd->bl, { SCF_REMOVEFROMHOMONMAPWARP });
  6093. else
  6094. status_db.removeByStatusFlag(&sd->hd->bl, { SCF_REMOVEFROMHOMONWARP });
  6095. if (battle_config.hom_delay_reset_warp) {
  6096. skill_blockhomun_clear(*sd->hd);
  6097. }
  6098. sd->hd->bl.m = m;
  6099. sd->hd->bl.x = sd->hd->ud.to_x = x;
  6100. sd->hd->bl.y = sd->hd->ud.to_y = y;
  6101. sd->hd->ud.dir = sd->ud.dir;
  6102. }
  6103. if( sd->md )
  6104. {
  6105. sd->md->bl.m = m;
  6106. sd->md->bl.x = sd->md->ud.to_x = x;
  6107. sd->md->bl.y = sd->md->ud.to_y = y;
  6108. sd->md->ud.dir = sd->ud.dir;
  6109. }
  6110. if( sd->ed ) {
  6111. sd->ed->bl.m = m;
  6112. sd->ed->bl.x = sd->ed->ud.to_x = x;
  6113. sd->ed->bl.y = sd->ed->ud.to_y = y;
  6114. sd->ed->ud.dir = sd->ud.dir;
  6115. }
  6116. pc_cell_basilica(sd);
  6117. //check if we gonna be rewarped [lighta]
  6118. if(npc_check_areanpc(1,m,x,y,0)){
  6119. sd->count_rewarp++;
  6120. }
  6121. else
  6122. sd->count_rewarp = 0;
  6123. if (sd->state.vending)
  6124. vending_update(*sd);
  6125. if (sd->state.buyingstore)
  6126. buyingstore_update(*sd);
  6127. return SETPOS_OK;
  6128. }
  6129. enum e_setpos pc_setpos_savepoint( map_session_data& sd, clr_type clrtype ){
  6130. struct map_data *mapdata = map_getmapdata( sd.bl.m );
  6131. if( mapdata != nullptr && mapdata->getMapFlag(MF_NOSAVE) && mapdata->save.map ){
  6132. return pc_setpos( &sd, mapdata->save.map, mapdata->save.x, mapdata->save.y, clrtype );
  6133. }else{
  6134. return pc_setpos( &sd, mapindex_name2id( sd.status.save_point.map ), sd.status.save_point.x, sd.status.save_point.y, clrtype );
  6135. }
  6136. }
  6137. /*==========================================
  6138. * Warp player sd to random location on current map.
  6139. * May fail if no walkable cell found (1000 attempts).
  6140. * Return:
  6141. * 0 = Success
  6142. * 1,2,3 = Fail
  6143. *------------------------------------------*/
  6144. char pc_randomwarp(map_session_data *sd, clr_type type, bool ignore_mapflag)
  6145. {
  6146. int16 x,y,i=0;
  6147. nullpo_ret(sd);
  6148. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6149. if (mapdata->getMapFlag(MF_NOTELEPORT) && !ignore_mapflag) //Teleport forbidden
  6150. return 3;
  6151. int32 edge = battle_config.map_edge_size;
  6152. do {
  6153. x = rnd_value<int16>(edge, mapdata->xs - edge - 1);
  6154. y = rnd_value<int16>(edge, mapdata->ys - edge - 1);
  6155. } 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);
  6156. if (i < 1000)
  6157. return pc_setpos(sd,mapdata->index,x,y,type);
  6158. return 3;
  6159. }
  6160. /*==========================================
  6161. * Records a memo point at sd's current position
  6162. * pos - entry to replace, (-1: shift oldest entry out)
  6163. *------------------------------------------*/
  6164. bool pc_memo(map_session_data* sd, int32 pos)
  6165. {
  6166. int32 skill;
  6167. nullpo_ret(sd);
  6168. // check mapflags
  6169. 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) ) {
  6170. clif_skill_teleportmessage( *sd, NOTIFY_MAPINFO_CANT_MEMO ); // "Saved point cannot be memorized."
  6171. return false;
  6172. }
  6173. // check inputs
  6174. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  6175. return false; // invalid input
  6176. // check required skill level
  6177. skill = pc_checkskill(sd, AL_WARP);
  6178. if( skill < 1 ) {
  6179. clif_skill_memomessage( *sd, WARPPOINT_NOT_LEARNED ); // "You haven't learned Warp."
  6180. return false;
  6181. }
  6182. if( skill < 2 || skill - 2 < pos ) {
  6183. clif_skill_memomessage( *sd, WARPPOINT_LOW_LEVEL ); // "Skill Level is not high enough."
  6184. return false;
  6185. }
  6186. if( pos == -1 )
  6187. {
  6188. uint8 i;
  6189. const char* mapname = map_mapid2mapname( sd->bl.m );
  6190. // prevent memo-ing the same map multiple times
  6191. ARR_FIND( 0, MAX_MEMOPOINTS, i, strncmp( sd->status.memo_point[i].map, mapname, sizeof( sd->status.memo_point[i].map ) ) == 0 );
  6192. memmove( &sd->status.memo_point[1], &sd->status.memo_point[0], ( u8min( i, MAX_MEMOPOINTS - 1 ) ) * sizeof( struct s_point_str ) );
  6193. pos = 0;
  6194. }
  6195. if( map_getmapdata(sd->bl.m)->instance_id ) {
  6196. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  6197. return false;
  6198. }
  6199. safestrncpy( sd->status.memo_point[pos].map, map_mapid2mapname( sd->bl.m ), sizeof( sd->status.memo_point[pos].map ) );
  6200. sd->status.memo_point[pos].x = sd->bl.x;
  6201. sd->status.memo_point[pos].y = sd->bl.y;
  6202. clif_skill_memomessage( *sd, WARPPOINT_SUCCESS );
  6203. return true;
  6204. }
  6205. //
  6206. // Skills
  6207. //
  6208. /**
  6209. * Get the skill current cooldown for player.
  6210. * (get the db base cooldown for skill + player specific cooldown)
  6211. * @param sd : player pointer
  6212. * @param id : skill id
  6213. * @param lv : skill lv
  6214. * @return player skill cooldown
  6215. */
  6216. int32 pc_get_skillcooldown(map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  6217. if (skill_id == SJ_NOVAEXPLOSING) {
  6218. status_change *sc = status_get_sc(&sd->bl);
  6219. if (sc && sc->getSCE(SC_DIMENSION))
  6220. return 0;
  6221. }
  6222. int32 cooldown = skill_get_cooldown(skill_id, skill_lv);
  6223. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  6224. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  6225. for (auto &it : sd->skillcooldown) {
  6226. if (it.id == skill_id) {
  6227. cooldown += it.val;
  6228. break;
  6229. }
  6230. }
  6231. return max(0, cooldown);
  6232. }
  6233. /*==========================================
  6234. * Return player sd skill_lv learned for given skill
  6235. *------------------------------------------*/
  6236. uint8 pc_checkskill(map_session_data *sd, uint16 skill_id)
  6237. {
  6238. uint16 idx = 0;
  6239. if (sd == nullptr)
  6240. return 0;
  6241. #ifdef RENEWAL
  6242. if ((idx = skill_get_index(skill_id)) == 0) {
  6243. #else
  6244. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  6245. if( skill_id >= RK_ENCHANTBLADE ){
  6246. // Silently fail for now -> future update planned
  6247. return 0;
  6248. }
  6249. #endif
  6250. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  6251. return 0;
  6252. }
  6253. if (SKILL_CHK_GUILD(skill_id) ) {
  6254. if (sd->status.guild_id>0 && sd->guild)
  6255. return guild_checkskill(sd->guild->guild,skill_id);
  6256. return 0;
  6257. }
  6258. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  6259. }
  6260. /**
  6261. * Returns the flag of the given skill (when learned).
  6262. * @param sd: Player data
  6263. * @param skill_id: Skill to lookup
  6264. * @return Skill flag type
  6265. */
  6266. e_skill_flag pc_checkskill_flag(map_session_data &sd, uint16 skill_id) {
  6267. uint16 idx;
  6268. #ifdef RENEWAL
  6269. if ((idx = skill_get_index(skill_id)) == 0) {
  6270. #else
  6271. if ((idx = skill_db.get_index(skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__)) == 0) {
  6272. if (skill_id >= RK_ENCHANTBLADE) {
  6273. // Silently fail for now -> future update planned
  6274. return SKILL_FLAG_NONE;
  6275. }
  6276. #endif
  6277. ShowError("pc_checkskill_flag: Invalid skill id %d (char_id=%d).\n", skill_id, sd.status.char_id);
  6278. return SKILL_FLAG_NONE;
  6279. }
  6280. 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;
  6281. }
  6282. /**
  6283. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  6284. * @param sd: Player data
  6285. * @param type: Summoner Power Type
  6286. * @return Skill points invested
  6287. */
  6288. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  6289. if (sd == nullptr)
  6290. return 0;
  6291. uint8 count = 0;
  6292. switch (type) {
  6293. case SUMMONER_POWER_SEA:
  6294. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  6295. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  6296. break;
  6297. case SUMMONER_POWER_LAND:
  6298. 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) +
  6299. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  6300. break;
  6301. case SUMMONER_POWER_LIFE:
  6302. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  6303. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  6304. break;
  6305. }
  6306. return count;
  6307. }
  6308. /**
  6309. * Checks for Imperial Guard's passive skills.
  6310. * @param sd: Player data
  6311. * @param flag:
  6312. * Flag&1 = IG_SHIELD_MASTERY
  6313. * Flag&2 = IG_SPEAR_SWORD_M
  6314. */
  6315. uint8 pc_checkskill_imperial_guard(map_session_data *sd, int16 flag)
  6316. {
  6317. nullpo_retr(0, sd);
  6318. uint8 count = 0;
  6319. if (flag&1 && sd->status.shield > 0)
  6320. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  6321. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  6322. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  6323. return count;
  6324. }
  6325. /**
  6326. * Check if we still have the correct weapon to continue the skill (actually status)
  6327. * If not ending it
  6328. * @param sd
  6329. * @return 0:error, 1:check done
  6330. */
  6331. static void pc_checkallowskill(map_session_data *sd)
  6332. {
  6333. nullpo_retv(sd);
  6334. if(sd->sc.empty())
  6335. return;
  6336. for (const auto &it : status_db) {
  6337. sc_type status = it.second->type;
  6338. std::bitset<SCF_MAX> flag = it.second->flag;
  6339. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  6340. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  6341. continue;
  6342. if (sd->sc.getSCE(status) && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  6343. status_change_end(&sd->bl, status);
  6344. }
  6345. if (flag[SCF_REQUIRENOWEAPON]) {
  6346. if (sd->sc.getSCE(status) && sd->status.weapon)
  6347. status_change_end( &sd->bl, status );
  6348. }
  6349. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  6350. if (sd->sc.getSCE(status) && sd->status.shield <= 0)
  6351. status_change_end(&sd->bl, status);
  6352. }
  6353. }
  6354. }
  6355. /*==========================================
  6356. * Return equipped index of item on player sd at pos
  6357. * Return
  6358. * -1 : Nothing equipped
  6359. * idx : (this index could be used in inventory to found item_data)
  6360. *------------------------------------------*/
  6361. int16 pc_checkequip(map_session_data *sd,int32 pos, bool checkall)
  6362. {
  6363. uint8 i;
  6364. nullpo_retr(-1, sd);
  6365. for(i=0;i<EQI_MAX;i++){
  6366. if(pos & equip_bitmask[i]){
  6367. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  6368. // Check all if any match is found
  6369. continue;
  6370. }
  6371. return sd->equip_index[i];
  6372. }
  6373. }
  6374. return -1;
  6375. }
  6376. /*==========================================
  6377. * Check if sd has nameid equipped somewhere
  6378. * @sd : the player session
  6379. * @nameid : id of the item to check
  6380. * @min : : see pc.hpp enum equip_index from ? to @max
  6381. * @max : see pc.hpp enum equip_index for @min to ?
  6382. * -return true,false
  6383. *------------------------------------------*/
  6384. bool pc_checkequip2(map_session_data *sd, t_itemid nameid, int32 min, int32 max)
  6385. {
  6386. int32 i;
  6387. for(i = min; i < max; i++) {
  6388. if(equip_bitmask[i]) {
  6389. int32 idx = sd->equip_index[i];
  6390. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  6391. return true;
  6392. }
  6393. }
  6394. return false;
  6395. }
  6396. /*==========================================
  6397. * Convert's from the client's lame Job ID system
  6398. * to the map server's 'makes sense' system. [Skotlex]
  6399. *------------------------------------------*/
  6400. uint64 pc_jobid2mapid(uint16 b_class)
  6401. {
  6402. switch(b_class)
  6403. {
  6404. //Novice And 1-1 Jobs
  6405. case JOB_NOVICE: return MAPID_NOVICE;
  6406. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  6407. case JOB_MAGE: return MAPID_MAGE;
  6408. case JOB_ARCHER: return MAPID_ARCHER;
  6409. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  6410. case JOB_MERCHANT: return MAPID_MERCHANT;
  6411. case JOB_THIEF: return MAPID_THIEF;
  6412. case JOB_TAEKWON: return MAPID_TAEKWON;
  6413. case JOB_WEDDING: return MAPID_WEDDING;
  6414. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  6415. case JOB_NINJA: return MAPID_NINJA;
  6416. case JOB_XMAS: return MAPID_XMAS;
  6417. case JOB_SUMMER: return MAPID_SUMMER;
  6418. case JOB_HANBOK: return MAPID_HANBOK;
  6419. case JOB_GANGSI: return MAPID_GANGSI;
  6420. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  6421. case JOB_SUMMER2: return MAPID_SUMMER2;
  6422. //2-1 Jobs
  6423. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  6424. case JOB_KNIGHT: return MAPID_KNIGHT;
  6425. case JOB_WIZARD: return MAPID_WIZARD;
  6426. case JOB_HUNTER: return MAPID_HUNTER;
  6427. case JOB_PRIEST: return MAPID_PRIEST;
  6428. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  6429. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  6430. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  6431. case JOB_KAGEROU:
  6432. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  6433. case JOB_REBELLION: return MAPID_REBELLION;
  6434. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  6435. //2-2 Jobs
  6436. case JOB_CRUSADER: return MAPID_CRUSADER;
  6437. case JOB_SAGE: return MAPID_SAGE;
  6438. case JOB_BARD:
  6439. case JOB_DANCER: return MAPID_BARDDANCER;
  6440. case JOB_MONK: return MAPID_MONK;
  6441. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  6442. case JOB_ROGUE: return MAPID_ROGUE;
  6443. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  6444. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  6445. //Trans Novice And Trans 1-1 Jobs
  6446. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  6447. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  6448. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  6449. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  6450. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  6451. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  6452. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  6453. //Trans 2-1 Jobs
  6454. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  6455. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  6456. case JOB_SNIPER: return MAPID_SNIPER;
  6457. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  6458. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6459. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6460. //Trans 2-2 Jobs
  6461. case JOB_PALADIN: return MAPID_PALADIN;
  6462. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6463. case JOB_CLOWN:
  6464. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6465. case JOB_CHAMPION: return MAPID_CHAMPION;
  6466. case JOB_CREATOR: return MAPID_CREATOR;
  6467. case JOB_STALKER: return MAPID_STALKER;
  6468. //Baby Novice And Baby 1-1 Jobs
  6469. case JOB_BABY: return MAPID_BABY;
  6470. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6471. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6472. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6473. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6474. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6475. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6476. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6477. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6478. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6479. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6480. //Baby 2-1 Jobs
  6481. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6482. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6483. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6484. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6485. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6486. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6487. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6488. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6489. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6490. case JOB_BABY_KAGEROU:
  6491. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6492. //Baby 2-2 Jobs
  6493. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6494. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6495. case JOB_BABY_BARD:
  6496. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6497. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6498. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6499. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6500. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6501. //3-1 Jobs
  6502. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6503. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6504. case JOB_WARLOCK: return MAPID_WARLOCK;
  6505. case JOB_RANGER: return MAPID_RANGER;
  6506. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6507. case JOB_MECHANIC: return MAPID_MECHANIC;
  6508. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6509. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6510. //3-2 Jobs
  6511. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6512. case JOB_SORCERER: return MAPID_SORCERER;
  6513. case JOB_MINSTREL:
  6514. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6515. case JOB_SURA: return MAPID_SURA;
  6516. case JOB_GENETIC: return MAPID_GENETIC;
  6517. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6518. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6519. //Trans 3-1 Jobs
  6520. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6521. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6522. case JOB_RANGER_T: return MAPID_RANGER_T;
  6523. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6524. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6525. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6526. //Trans 3-2 Jobs
  6527. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6528. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6529. case JOB_MINSTREL_T:
  6530. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6531. case JOB_SURA_T: return MAPID_SURA_T;
  6532. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6533. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6534. //Baby 3-1 Jobs
  6535. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6536. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6537. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6538. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6539. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6540. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6541. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6542. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6543. //Baby 3-2 Jobs
  6544. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6545. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6546. case JOB_BABY_MINSTREL:
  6547. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6548. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6549. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6550. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6551. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6552. //Doram Jobs
  6553. case JOB_SUMMONER: return MAPID_SUMMONER;
  6554. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6555. //4-1 Jobs
  6556. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6557. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6558. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6559. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6560. case JOB_CARDINAL: return MAPID_CARDINAL;
  6561. case JOB_MEISTER: return MAPID_MEISTER;
  6562. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6563. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6564. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6565. case JOB_SHINKIRO:
  6566. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6567. //4-2 Jobs
  6568. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6569. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6570. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6571. case JOB_TROUBADOUR:
  6572. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6573. case JOB_BIOLO: return MAPID_BIOLO;
  6574. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6575. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6576. //Unknown
  6577. default:
  6578. return -1;
  6579. }
  6580. }
  6581. //Reverts the map-style class id to the client-style one.
  6582. int32 pc_mapid2jobid(uint64 class_, int32 sex)
  6583. {
  6584. switch(class_) {
  6585. //Novice And 1-1 Jobs
  6586. case MAPID_NOVICE: return JOB_NOVICE;
  6587. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6588. case MAPID_MAGE: return JOB_MAGE;
  6589. case MAPID_ARCHER: return JOB_ARCHER;
  6590. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6591. case MAPID_MERCHANT: return JOB_MERCHANT;
  6592. case MAPID_THIEF: return JOB_THIEF;
  6593. case MAPID_TAEKWON: return JOB_TAEKWON;
  6594. case MAPID_WEDDING: return JOB_WEDDING;
  6595. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6596. case MAPID_NINJA: return JOB_NINJA;
  6597. case MAPID_XMAS: return JOB_XMAS;
  6598. case MAPID_SUMMER: return JOB_SUMMER;
  6599. case MAPID_HANBOK: return JOB_HANBOK;
  6600. case MAPID_GANGSI: return JOB_GANGSI;
  6601. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6602. case MAPID_SUMMER2: return JOB_SUMMER2;
  6603. //2-1 Jobs
  6604. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6605. case MAPID_KNIGHT: return JOB_KNIGHT;
  6606. case MAPID_WIZARD: return JOB_WIZARD;
  6607. case MAPID_HUNTER: return JOB_HUNTER;
  6608. case MAPID_PRIEST: return JOB_PRIEST;
  6609. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6610. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6611. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6612. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6613. case MAPID_REBELLION: return JOB_REBELLION;
  6614. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6615. //2-2 Jobs
  6616. case MAPID_CRUSADER: return JOB_CRUSADER;
  6617. case MAPID_SAGE: return JOB_SAGE;
  6618. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6619. case MAPID_MONK: return JOB_MONK;
  6620. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6621. case MAPID_ROGUE: return JOB_ROGUE;
  6622. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6623. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6624. //Trans Novice And Trans 2-1 Jobs
  6625. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6626. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6627. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6628. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6629. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6630. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6631. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6632. //Trans 2-1 Jobs
  6633. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6634. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6635. case MAPID_SNIPER: return JOB_SNIPER;
  6636. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6637. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6638. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6639. //Trans 2-2 Jobs
  6640. case MAPID_PALADIN: return JOB_PALADIN;
  6641. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6642. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6643. case MAPID_CHAMPION: return JOB_CHAMPION;
  6644. case MAPID_CREATOR: return JOB_CREATOR;
  6645. case MAPID_STALKER: return JOB_STALKER;
  6646. //Baby Novice And Baby 1-1 Jobs
  6647. case MAPID_BABY: return JOB_BABY;
  6648. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6649. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6650. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6651. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6652. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6653. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6654. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6655. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6656. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6657. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6658. //Baby 2-1 Jobs
  6659. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6660. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6661. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6662. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6663. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6664. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6665. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6666. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6667. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6668. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6669. //Baby 2-2 Jobs
  6670. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6671. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6672. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6673. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6674. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6675. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6676. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6677. //3-1 Jobs
  6678. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6679. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6680. case MAPID_WARLOCK: return JOB_WARLOCK;
  6681. case MAPID_RANGER: return JOB_RANGER;
  6682. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6683. case MAPID_MECHANIC: return JOB_MECHANIC;
  6684. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6685. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6686. //3-2 Jobs
  6687. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6688. case MAPID_SORCERER: return JOB_SORCERER;
  6689. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6690. case MAPID_SURA: return JOB_SURA;
  6691. case MAPID_GENETIC: return JOB_GENETIC;
  6692. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6693. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6694. //Trans 3-1 Jobs
  6695. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6696. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6697. case MAPID_RANGER_T: return JOB_RANGER_T;
  6698. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6699. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6700. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6701. //Trans 3-2 Jobs
  6702. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6703. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6704. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6705. case MAPID_SURA_T: return JOB_SURA_T;
  6706. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6707. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6708. //Baby 3-1 Jobs
  6709. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6710. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6711. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6712. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6713. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6714. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6715. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6716. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6717. //Baby 3-2 Jobs
  6718. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6719. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6720. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6721. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6722. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6723. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6724. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6725. //Doram Jobs
  6726. case MAPID_SUMMONER: return JOB_SUMMONER;
  6727. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6728. //4-1 Jobs
  6729. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6730. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6731. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6732. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6733. case MAPID_CARDINAL: return JOB_CARDINAL;
  6734. case MAPID_MEISTER: return JOB_MEISTER;
  6735. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6736. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6737. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6738. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6739. //4-2 Jobs
  6740. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6741. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6742. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6743. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6744. case MAPID_BIOLO: return JOB_BIOLO;
  6745. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6746. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6747. //Unknown
  6748. default:
  6749. return -1;
  6750. }
  6751. }
  6752. /*====================================================
  6753. * This function return the name of the job (by [Yor])
  6754. *----------------------------------------------------*/
  6755. const char* job_name(int32 class_)
  6756. {
  6757. switch (class_) {
  6758. case JOB_NOVICE:
  6759. case JOB_SWORDMAN:
  6760. case JOB_MAGE:
  6761. case JOB_ARCHER:
  6762. case JOB_ACOLYTE:
  6763. case JOB_MERCHANT:
  6764. case JOB_THIEF:
  6765. return msg_txt(nullptr,550 - JOB_NOVICE+class_);
  6766. case JOB_KNIGHT:
  6767. case JOB_PRIEST:
  6768. case JOB_WIZARD:
  6769. case JOB_BLACKSMITH:
  6770. case JOB_HUNTER:
  6771. case JOB_ASSASSIN:
  6772. return msg_txt(nullptr,557 - JOB_KNIGHT+class_);
  6773. case JOB_KNIGHT2:
  6774. return msg_txt(nullptr,557);
  6775. case JOB_CRUSADER:
  6776. case JOB_MONK:
  6777. case JOB_SAGE:
  6778. case JOB_ROGUE:
  6779. case JOB_ALCHEMIST:
  6780. case JOB_BARD:
  6781. case JOB_DANCER:
  6782. return msg_txt(nullptr,563 - JOB_CRUSADER+class_);
  6783. case JOB_CRUSADER2:
  6784. return msg_txt(nullptr,563);
  6785. case JOB_WEDDING:
  6786. case JOB_SUPER_NOVICE:
  6787. case JOB_GUNSLINGER:
  6788. case JOB_NINJA:
  6789. case JOB_XMAS:
  6790. return msg_txt(nullptr,570 - JOB_WEDDING+class_);
  6791. case JOB_SUMMER:
  6792. case JOB_SUMMER2:
  6793. return msg_txt(nullptr,621);
  6794. case JOB_HANBOK:
  6795. return msg_txt(nullptr,694);
  6796. case JOB_OKTOBERFEST:
  6797. return msg_txt(nullptr,696);
  6798. case JOB_NOVICE_HIGH:
  6799. case JOB_SWORDMAN_HIGH:
  6800. case JOB_MAGE_HIGH:
  6801. case JOB_ARCHER_HIGH:
  6802. case JOB_ACOLYTE_HIGH:
  6803. case JOB_MERCHANT_HIGH:
  6804. case JOB_THIEF_HIGH:
  6805. return msg_txt(nullptr,575 - JOB_NOVICE_HIGH+class_);
  6806. case JOB_LORD_KNIGHT:
  6807. case JOB_HIGH_PRIEST:
  6808. case JOB_HIGH_WIZARD:
  6809. case JOB_WHITESMITH:
  6810. case JOB_SNIPER:
  6811. case JOB_ASSASSIN_CROSS:
  6812. return msg_txt(nullptr,582 - JOB_LORD_KNIGHT+class_);
  6813. case JOB_LORD_KNIGHT2:
  6814. return msg_txt(nullptr,582);
  6815. case JOB_PALADIN:
  6816. case JOB_CHAMPION:
  6817. case JOB_PROFESSOR:
  6818. case JOB_STALKER:
  6819. case JOB_CREATOR:
  6820. case JOB_CLOWN:
  6821. case JOB_GYPSY:
  6822. return msg_txt(nullptr,588 - JOB_PALADIN + class_);
  6823. case JOB_PALADIN2:
  6824. return msg_txt(nullptr,588);
  6825. case JOB_BABY:
  6826. case JOB_BABY_SWORDMAN:
  6827. case JOB_BABY_MAGE:
  6828. case JOB_BABY_ARCHER:
  6829. case JOB_BABY_ACOLYTE:
  6830. case JOB_BABY_MERCHANT:
  6831. case JOB_BABY_THIEF:
  6832. return msg_txt(nullptr,595 - JOB_BABY + class_);
  6833. case JOB_BABY_KNIGHT:
  6834. case JOB_BABY_PRIEST:
  6835. case JOB_BABY_WIZARD:
  6836. case JOB_BABY_BLACKSMITH:
  6837. case JOB_BABY_HUNTER:
  6838. case JOB_BABY_ASSASSIN:
  6839. return msg_txt(nullptr,602 - JOB_BABY_KNIGHT + class_);
  6840. case JOB_BABY_KNIGHT2:
  6841. return msg_txt(nullptr,602);
  6842. case JOB_BABY_CRUSADER:
  6843. case JOB_BABY_MONK:
  6844. case JOB_BABY_SAGE:
  6845. case JOB_BABY_ROGUE:
  6846. case JOB_BABY_ALCHEMIST:
  6847. case JOB_BABY_BARD:
  6848. case JOB_BABY_DANCER:
  6849. return msg_txt(nullptr,608 - JOB_BABY_CRUSADER + class_);
  6850. case JOB_BABY_CRUSADER2:
  6851. return msg_txt(nullptr,608);
  6852. case JOB_SUPER_BABY:
  6853. return msg_txt(nullptr,615);
  6854. case JOB_TAEKWON:
  6855. return msg_txt(nullptr,616);
  6856. case JOB_STAR_GLADIATOR:
  6857. case JOB_STAR_GLADIATOR2:
  6858. return msg_txt(nullptr,617);
  6859. case JOB_SOUL_LINKER:
  6860. return msg_txt(nullptr,618);
  6861. case JOB_GANGSI:
  6862. case JOB_DEATH_KNIGHT:
  6863. case JOB_DARK_COLLECTOR:
  6864. return msg_txt(nullptr,622 - JOB_GANGSI+class_);
  6865. case JOB_RUNE_KNIGHT:
  6866. case JOB_WARLOCK:
  6867. case JOB_RANGER:
  6868. case JOB_ARCH_BISHOP:
  6869. case JOB_MECHANIC:
  6870. case JOB_GUILLOTINE_CROSS:
  6871. return msg_txt(nullptr,625 - JOB_RUNE_KNIGHT+class_);
  6872. case JOB_RUNE_KNIGHT_T:
  6873. case JOB_WARLOCK_T:
  6874. case JOB_RANGER_T:
  6875. case JOB_ARCH_BISHOP_T:
  6876. case JOB_MECHANIC_T:
  6877. case JOB_GUILLOTINE_CROSS_T:
  6878. return msg_txt(nullptr,681 - JOB_RUNE_KNIGHT_T+class_);
  6879. case JOB_ROYAL_GUARD:
  6880. case JOB_SORCERER:
  6881. case JOB_MINSTREL:
  6882. case JOB_WANDERER:
  6883. case JOB_SURA:
  6884. case JOB_GENETIC:
  6885. case JOB_SHADOW_CHASER:
  6886. return msg_txt(nullptr,631 - JOB_ROYAL_GUARD+class_);
  6887. case JOB_ROYAL_GUARD_T:
  6888. case JOB_SORCERER_T:
  6889. case JOB_MINSTREL_T:
  6890. case JOB_WANDERER_T:
  6891. case JOB_SURA_T:
  6892. case JOB_GENETIC_T:
  6893. case JOB_SHADOW_CHASER_T:
  6894. return msg_txt(nullptr,687 - JOB_ROYAL_GUARD_T+class_);
  6895. case JOB_RUNE_KNIGHT2:
  6896. case JOB_RUNE_KNIGHT_T2:
  6897. return msg_txt(nullptr,625);
  6898. case JOB_ROYAL_GUARD2:
  6899. case JOB_ROYAL_GUARD_T2:
  6900. return msg_txt(nullptr,631);
  6901. case JOB_RANGER2:
  6902. case JOB_RANGER_T2:
  6903. return msg_txt(nullptr,627);
  6904. case JOB_MECHANIC2:
  6905. case JOB_MECHANIC_T2:
  6906. return msg_txt(nullptr,629);
  6907. case JOB_BABY_RUNE_KNIGHT:
  6908. case JOB_BABY_WARLOCK:
  6909. case JOB_BABY_RANGER:
  6910. case JOB_BABY_ARCH_BISHOP:
  6911. case JOB_BABY_MECHANIC:
  6912. case JOB_BABY_GUILLOTINE_CROSS:
  6913. case JOB_BABY_ROYAL_GUARD:
  6914. case JOB_BABY_SORCERER:
  6915. case JOB_BABY_MINSTREL:
  6916. case JOB_BABY_WANDERER:
  6917. case JOB_BABY_SURA:
  6918. case JOB_BABY_GENETIC:
  6919. case JOB_BABY_SHADOW_CHASER:
  6920. return msg_txt(nullptr,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6921. case JOB_BABY_RUNE_KNIGHT2:
  6922. return msg_txt(nullptr,638);
  6923. case JOB_BABY_ROYAL_GUARD2:
  6924. return msg_txt(nullptr,644);
  6925. case JOB_BABY_RANGER2:
  6926. return msg_txt(nullptr,640);
  6927. case JOB_BABY_MECHANIC2:
  6928. return msg_txt(nullptr,642);
  6929. case JOB_SUPER_NOVICE_E:
  6930. case JOB_SUPER_BABY_E:
  6931. return msg_txt(nullptr,651 - JOB_SUPER_NOVICE_E+class_);
  6932. case JOB_KAGEROU:
  6933. case JOB_OBORO:
  6934. return msg_txt(nullptr,653 - JOB_KAGEROU+class_);
  6935. case JOB_REBELLION:
  6936. return msg_txt(nullptr,695);
  6937. case JOB_SUMMONER:
  6938. return msg_txt(nullptr,697);
  6939. case JOB_BABY_SUMMONER:
  6940. return msg_txt(nullptr,698);
  6941. case JOB_BABY_NINJA:
  6942. return msg_txt(nullptr,699);
  6943. case JOB_BABY_KAGEROU:
  6944. case JOB_BABY_OBORO:
  6945. case JOB_BABY_TAEKWON:
  6946. case JOB_BABY_STAR_GLADIATOR:
  6947. case JOB_BABY_SOUL_LINKER:
  6948. case JOB_BABY_GUNSLINGER:
  6949. case JOB_BABY_REBELLION:
  6950. return msg_txt(nullptr,753 - JOB_BABY_KAGEROU+class_);
  6951. case JOB_BABY_STAR_GLADIATOR2:
  6952. return msg_txt(nullptr,756);
  6953. case JOB_STAR_EMPEROR:
  6954. case JOB_SOUL_REAPER:
  6955. case JOB_BABY_STAR_EMPEROR:
  6956. case JOB_BABY_SOUL_REAPER:
  6957. return msg_txt(nullptr,782 - JOB_STAR_EMPEROR + class_);
  6958. case JOB_STAR_EMPEROR2:
  6959. return msg_txt(nullptr,782);
  6960. case JOB_BABY_STAR_EMPEROR2:
  6961. return msg_txt(nullptr,784);
  6962. case JOB_DRAGON_KNIGHT:
  6963. case JOB_MEISTER:
  6964. case JOB_SHADOW_CROSS:
  6965. case JOB_ARCH_MAGE:
  6966. case JOB_CARDINAL:
  6967. case JOB_WINDHAWK:
  6968. case JOB_IMPERIAL_GUARD:
  6969. case JOB_BIOLO:
  6970. case JOB_ABYSS_CHASER:
  6971. case JOB_ELEMENTAL_MASTER:
  6972. case JOB_INQUISITOR:
  6973. case JOB_TROUBADOUR:
  6974. case JOB_TROUVERE:
  6975. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6976. case JOB_WINDHAWK2:
  6977. return msg_txt( nullptr, 805);
  6978. case JOB_MEISTER2:
  6979. return msg_txt( nullptr, 801 );
  6980. case JOB_DRAGON_KNIGHT2:
  6981. return msg_txt( nullptr, 800 );
  6982. case JOB_IMPERIAL_GUARD2:
  6983. return msg_txt( nullptr, 806 );
  6984. case JOB_SKY_EMPEROR:
  6985. case JOB_SOUL_ASCETIC:
  6986. case JOB_SHINKIRO:
  6987. case JOB_SHIRANUI:
  6988. case JOB_NIGHT_WATCH:
  6989. case JOB_HYPER_NOVICE:
  6990. case JOB_SPIRIT_HANDLER:
  6991. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6992. case JOB_SKY_EMPEROR2:
  6993. return msg_txt( nullptr, 813 );
  6994. default:
  6995. return msg_txt(nullptr,655);
  6996. }
  6997. }
  6998. /*====================================================
  6999. * Timered function to make id follow a target.
  7000. * @id = bl.id (player only atm)
  7001. * target is define in sd->followtarget (bl.id)
  7002. * used by pc_follow
  7003. *----------------------------------------------------*/
  7004. TIMER_FUNC(pc_follow_timer){
  7005. map_session_data *sd;
  7006. struct block_list *tbl;
  7007. sd = map_id2sd(id);
  7008. nullpo_ret(sd);
  7009. if (sd->followtimer != tid){
  7010. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  7011. sd->followtimer = INVALID_TIMER;
  7012. return 0;
  7013. }
  7014. sd->followtimer = INVALID_TIMER;
  7015. tbl = map_id2bl(sd->followtarget);
  7016. if (tbl == nullptr || pc_isdead(sd))
  7017. {
  7018. pc_stop_following(sd);
  7019. return 0;
  7020. }
  7021. // either player or target is currently detached from map blocks (could be teleporting),
  7022. // but still connected to this map, so we'll just increment the timer and check back later
  7023. if (sd->bl.prev != nullptr && tbl->prev != nullptr &&
  7024. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  7025. {
  7026. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, nullptr, nullptr)) {
  7027. if (!check_distance_bl(&sd->bl, tbl, 5))
  7028. unit_walktobl(&sd->bl, tbl, 5, 0);
  7029. } else
  7030. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  7031. }
  7032. sd->followtimer = add_timer(
  7033. tick + 1000, // increase time a bit to loosen up map's load
  7034. pc_follow_timer, sd->bl.id, 0);
  7035. return 0;
  7036. }
  7037. int32 pc_stop_following (map_session_data *sd)
  7038. {
  7039. nullpo_ret(sd);
  7040. if (sd->followtimer != INVALID_TIMER) {
  7041. delete_timer(sd->followtimer,pc_follow_timer);
  7042. sd->followtimer = INVALID_TIMER;
  7043. }
  7044. sd->followtarget = -1;
  7045. sd->ud.target_to = 0;
  7046. unit_stop_walking( &sd->bl, USW_FIXPOS );
  7047. return 0;
  7048. }
  7049. int32 pc_follow(map_session_data *sd,int32 target_id)
  7050. {
  7051. struct block_list *bl = map_id2bl(target_id);
  7052. if (bl == nullptr /*|| bl->type != BL_PC*/)
  7053. return 1;
  7054. if (sd->followtimer != INVALID_TIMER)
  7055. pc_stop_following(sd);
  7056. sd->followtarget = target_id;
  7057. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7058. return 0;
  7059. }
  7060. int32 pc_checkbaselevelup(map_session_data *sd) {
  7061. t_exp next = pc_nextbaseexp(sd);
  7062. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  7063. return 0;
  7064. uint32 base_level = sd->status.base_level;
  7065. do {
  7066. sd->status.base_exp -= next;
  7067. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7068. 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 )
  7069. sd->status.base_exp = next-1;
  7070. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  7071. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  7072. sd->status.base_level++;
  7073. if( pc_is_maxbaselv(sd) ){
  7074. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  7075. break;
  7076. }
  7077. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  7078. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  7079. status_calc_pet(sd->pd,SCO_NONE);
  7080. clif_updatestatus(*sd,SP_STATUSPOINT);
  7081. clif_updatestatus(*sd,SP_TRAITPOINT);
  7082. clif_updatestatus(*sd,SP_BASELEVEL);
  7083. clif_updatestatus(*sd,SP_BASEEXP);
  7084. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  7085. status_calc_pc(sd,SCO_FORCE);
  7086. status_percent_heal(&sd->bl,100,100);
  7087. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7088. for (const auto &status : status_db) {
  7089. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  7090. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, 120000); //All buffs with super novice flag for 2 minutes
  7091. }
  7092. if (sd->state.snovice_dead_flag)
  7093. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  7094. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  7095. for (const auto &status : status_db) {
  7096. if (status.second->flag[SCF_TAEKWONANGEL])
  7097. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  7098. }
  7099. }
  7100. clif_misceffect( sd->bl, NOTIFYEFFECT_BASE_LEVEL_UP );
  7101. npc_script_event( *sd, NPCE_BASELVUP );
  7102. if(sd->status.party_id)
  7103. party_send_levelup(sd);
  7104. pc_baselevelchanged(sd);
  7105. for (; base_level <= sd->status.base_level; base_level++) {
  7106. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  7107. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  7108. }
  7109. return 1;
  7110. }
  7111. void pc_baselevelchanged(map_session_data *sd) {
  7112. uint8 i;
  7113. for( i = 0; i < EQI_MAX; i++ ) {
  7114. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  7115. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (uint32)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  7116. pc_unequipitem(sd, sd->equip_index[i], 3);
  7117. }
  7118. }
  7119. pc_show_questinfo(sd);
  7120. }
  7121. int32 pc_checkjoblevelup(map_session_data *sd)
  7122. {
  7123. t_exp next = pc_nextjobexp(sd);
  7124. nullpo_ret(sd);
  7125. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  7126. return 0;
  7127. uint32 job_level = sd->status.job_level;
  7128. do {
  7129. sd->status.job_exp -= next;
  7130. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7131. 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 )
  7132. sd->status.job_exp = next-1;
  7133. sd->status.job_level ++;
  7134. sd->status.skill_point ++;
  7135. if( pc_is_maxjoblv(sd) ){
  7136. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  7137. break;
  7138. }
  7139. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  7140. clif_updatestatus(*sd,SP_JOBLEVEL);
  7141. clif_updatestatus(*sd,SP_JOBEXP);
  7142. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  7143. clif_updatestatus(*sd,SP_SKILLPOINT);
  7144. status_calc_pc(sd,SCO_FORCE);
  7145. clif_misceffect( sd->bl, NOTIFYEFFECT_JOB_LEVEL_UP );
  7146. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7147. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7148. npc_script_event( *sd, NPCE_JOBLVUP );
  7149. for (; job_level <= sd->status.job_level; job_level++)
  7150. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  7151. pc_show_questinfo(sd);
  7152. return 1;
  7153. }
  7154. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  7155. * @param sd Player
  7156. * @param base_exp Base EXP before peronal bonuses
  7157. * @param job_exp Job EXP before peronal bonuses
  7158. * @param src Block list that affecting the exp calculation
  7159. */
  7160. static void pc_calcexp(map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  7161. {
  7162. int32 bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  7163. if (src) {
  7164. status_data* status = status_get_status_data(*src);
  7165. if( sd->indexed_bonus.expaddrace[status->race] )
  7166. bonus += sd->indexed_bonus.expaddrace[status->race];
  7167. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  7168. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  7169. if( sd->indexed_bonus.expaddclass[status->class_] )
  7170. bonus += sd->indexed_bonus.expaddclass[status->class_];
  7171. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  7172. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  7173. if (battle_config.pk_mode &&
  7174. (int32)(status_get_lv(src) - sd->status.base_level) >= 20)
  7175. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  7176. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  7177. vip_bonus_base = battle_config.vip_base_exp_increase;
  7178. vip_bonus_job = battle_config.vip_job_exp_increase;
  7179. }
  7180. }
  7181. // Give EXPBOOST for quests even if src is nullptr.
  7182. if (sd->sc.getSCE(SC_EXPBOOST)) {
  7183. bonus += sd->sc.getSCE(SC_EXPBOOST)->val1;
  7184. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  7185. bonus += (sd->sc.getSCE(SC_EXPBOOST)->val1 / battle_config.vip_bm_increase);
  7186. }
  7187. if (sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND))
  7188. bonus += sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)->val1;
  7189. if (*base_exp) {
  7190. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  7191. *base_exp = cap_value(exp, 1, MAX_EXP);
  7192. }
  7193. // Give JEXPBOOST for quests even if src is nullptr.
  7194. if (sd->sc.getSCE(SC_JEXPBOOST))
  7195. bonus += sd->sc.getSCE(SC_JEXPBOOST)->val1;
  7196. if (sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)) // Increase Jexp as well
  7197. bonus += sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)->val1;
  7198. if (*job_exp) {
  7199. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  7200. *job_exp = cap_value(exp, 1, MAX_EXP);
  7201. }
  7202. return;
  7203. }
  7204. /**
  7205. * Show EXP gained by player in percentage by @showexp
  7206. * @param sd Player
  7207. * @param base_exp Base EXP gained/loss
  7208. * @param next_base_exp Base EXP needed for next base level
  7209. * @param job_exp Job EXP gained/loss
  7210. * @param next_job_exp Job EXP needed for next job level
  7211. * @param lost True:EXP penalty, lose EXP
  7212. **/
  7213. 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) {
  7214. char output[CHAT_SIZE_MAX];
  7215. nullpo_retv(sd);
  7216. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  7217. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  7218. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  7219. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  7220. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  7221. }
  7222. /**
  7223. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  7224. * @param sd Player
  7225. * @param src EXP source
  7226. * @param base_exp Base EXP gained
  7227. * @param base_exp Job EXP gained
  7228. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  7229. * @return
  7230. **/
  7231. void pc_gainexp(map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  7232. {
  7233. t_exp nextb = 0, nextj = 0;
  7234. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  7235. nullpo_retv(sd);
  7236. if(sd->bl.prev == nullptr || pc_isdead(sd))
  7237. return;
  7238. if (!(exp_flag&2)) {
  7239. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  7240. return; // no exp on pvp maps
  7241. if (sd->status.guild_id>0)
  7242. base_exp -= guild_payexp(sd,base_exp);
  7243. }
  7244. flag = ((base_exp) ? 1 : 0) |
  7245. ((job_exp) ? 2 : 0) |
  7246. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  7247. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  7248. if (!(exp_flag&2))
  7249. pc_calcexp(sd, &base_exp, &job_exp, src);
  7250. nextb = pc_nextbaseexp(sd);
  7251. nextj = pc_nextjobexp(sd);
  7252. if (flag&4){
  7253. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  7254. base_exp = 0;
  7255. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  7256. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  7257. }
  7258. if (flag&8){
  7259. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  7260. job_exp = 0;
  7261. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  7262. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  7263. }
  7264. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  7265. //Note that this value should never be greater than the original
  7266. //therefore no overflow checks are needed. [Skotlex]
  7267. if (nextb > 0) {
  7268. float nextbp = (float) base_exp / (float) nextb;
  7269. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  7270. base_exp = (uint32)(battle_config.max_exp_gain_rate/1000.*nextb);
  7271. }
  7272. if (nextj > 0) {
  7273. float nextjp = (float) job_exp / (float) nextj;
  7274. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  7275. job_exp = (uint32)(battle_config.max_exp_gain_rate/1000.*nextj);
  7276. }
  7277. }
  7278. // Give EXP for Base Level
  7279. if (base_exp) {
  7280. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  7281. if (!pc_checkbaselevelup(sd))
  7282. clif_updatestatus(*sd,SP_BASEEXP);
  7283. }
  7284. // Give EXP for Job Level
  7285. if (job_exp) {
  7286. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  7287. if (!pc_checkjoblevelup(sd))
  7288. clif_updatestatus(*sd,SP_JOBEXP);
  7289. }
  7290. if (flag&1)
  7291. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  7292. if (flag&2)
  7293. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  7294. if (sd->state.showexp && (base_exp || job_exp))
  7295. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  7296. }
  7297. /**
  7298. * Lost Base/Job EXP from a player
  7299. * @param sd Player
  7300. * @param base_exp Base EXP lost
  7301. * @param job_exp Job EXP lost
  7302. **/
  7303. void pc_lostexp(map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  7304. nullpo_retv(sd);
  7305. if (base_exp) {
  7306. base_exp = u64min(sd->status.base_exp, base_exp);
  7307. sd->status.base_exp -= base_exp;
  7308. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  7309. clif_updatestatus(*sd, SP_BASEEXP);
  7310. }
  7311. if (job_exp) {
  7312. job_exp = u64min(sd->status.job_exp, job_exp);
  7313. sd->status.job_exp -= job_exp;
  7314. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  7315. clif_updatestatus(*sd, SP_JOBEXP);
  7316. }
  7317. if (sd->state.showexp && (base_exp || job_exp))
  7318. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  7319. }
  7320. /**
  7321. * Returns max base level for this character's class.
  7322. * @param job_id: Player's class
  7323. * @return Max Base Level
  7324. */
  7325. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  7326. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7327. return job ? job->max_base_level : 0;
  7328. }
  7329. /**
  7330. * Returns max base level for this character.
  7331. * @param sd Player
  7332. * @return Max Base Level
  7333. **/
  7334. uint32 pc_maxbaselv(map_session_data *sd){
  7335. return job_db.get_maxBaseLv(sd->status.class_);
  7336. }
  7337. /**
  7338. * Returns max job level for this character's class.
  7339. * @param job_id: Player's class
  7340. * @return Max Job Level
  7341. */
  7342. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  7343. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7344. return job ? job->max_job_level : 0;
  7345. }
  7346. /**
  7347. * Returns max job level for this character.
  7348. * @param sd Player
  7349. * @return Max Job Level
  7350. **/
  7351. uint32 pc_maxjoblv(map_session_data *sd){
  7352. return job_db.get_maxJobLv(sd->status.class_);
  7353. }
  7354. /**
  7355. * Check if player is reached max base level
  7356. * @param sd
  7357. * @return True if reached max level
  7358. **/
  7359. bool pc_is_maxbaselv(map_session_data *sd) {
  7360. nullpo_retr(false, sd);
  7361. return (sd->status.base_level >= pc_maxbaselv(sd));
  7362. }
  7363. /**
  7364. * Check if player is reached max base level
  7365. * @param sd
  7366. * @return True if reached max level
  7367. **/
  7368. bool pc_is_maxjoblv(map_session_data *sd) {
  7369. nullpo_retr(false, sd);
  7370. return (sd->status.job_level >= pc_maxjoblv(sd));
  7371. }
  7372. /**
  7373. * Returns base experience for this character's class.
  7374. * @param job_id: Player's class
  7375. * @param level: Player's level
  7376. * @return Base EXP
  7377. */
  7378. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  7379. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7380. return job ? job->base_exp[level - 1] : 0;
  7381. }
  7382. /**
  7383. * Base exp needed for player to level up.
  7384. * @param sd
  7385. * @return Base EXP needed for next base level
  7386. **/
  7387. t_exp pc_nextbaseexp(map_session_data *sd){
  7388. nullpo_ret(sd);
  7389. if (sd->status.base_level == 0) // Is this something that possible?
  7390. return 0;
  7391. if (pc_is_maxbaselv(sd))
  7392. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  7393. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  7394. }
  7395. /**
  7396. * Returns job experience for this character's class.
  7397. * @param job_id: Player's class
  7398. * @param level: Player's level
  7399. * @return Job EXP
  7400. */
  7401. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  7402. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7403. return job ? job->job_exp[level - 1] : 0;
  7404. }
  7405. /**
  7406. * Job exp needed for player to level up.
  7407. * @param sd
  7408. * @return Job EXP needed for next job level
  7409. **/
  7410. t_exp pc_nextjobexp(map_session_data *sd){
  7411. nullpo_ret(sd);
  7412. if (sd->status.job_level == 0) // Is this something that possible?
  7413. return 0;
  7414. if (pc_is_maxjoblv(sd))
  7415. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  7416. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  7417. }
  7418. /**
  7419. * Returns max weight base for this character's class.
  7420. * @param job_id: Player's class
  7421. * @return Max weight base
  7422. */
  7423. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  7424. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7425. return job ? job->max_weight_base : 0;
  7426. }
  7427. /// Returns the value of the specified stat.
  7428. int32 pc_getstat(map_session_data *sd, int32 type)
  7429. {
  7430. nullpo_retr(-1, sd);
  7431. switch( type ) {
  7432. case SP_STR: return sd->status.str;
  7433. case SP_AGI: return sd->status.agi;
  7434. case SP_VIT: return sd->status.vit;
  7435. case SP_INT: return sd->status.int_;
  7436. case SP_DEX: return sd->status.dex;
  7437. case SP_LUK: return sd->status.luk;
  7438. case SP_POW: return sd->status.pow;
  7439. case SP_STA: return sd->status.sta;
  7440. case SP_WIS: return sd->status.wis;
  7441. case SP_SPL: return sd->status.spl;
  7442. case SP_CON: return sd->status.con;
  7443. case SP_CRT: return sd->status.crt;
  7444. default:
  7445. return -1;
  7446. }
  7447. }
  7448. /// Sets the specified stat to the specified value.
  7449. /// Returns the new value.
  7450. int32 pc_setstat(map_session_data* sd, int32 type, int32 val)
  7451. {
  7452. nullpo_retr(-1, sd);
  7453. switch( type ) {
  7454. case SP_STR: sd->status.str = val; break;
  7455. case SP_AGI: sd->status.agi = val; break;
  7456. case SP_VIT: sd->status.vit = val; break;
  7457. case SP_INT: sd->status.int_ = val; break;
  7458. case SP_DEX: sd->status.dex = val; break;
  7459. case SP_LUK: sd->status.luk = val; break;
  7460. case SP_POW: sd->status.pow = val; break;
  7461. case SP_STA: sd->status.sta = val; break;
  7462. case SP_WIS: sd->status.wis = val; break;
  7463. case SP_SPL: sd->status.spl = val; break;
  7464. case SP_CON: sd->status.con = val; break;
  7465. case SP_CRT: sd->status.crt = val; break;
  7466. default:
  7467. return -1;
  7468. }
  7469. return val;
  7470. }
  7471. /**
  7472. * Gets the total number of status points at the provided level.
  7473. * @param level: Player base level.
  7474. * @return Total number of status points at specific base level.
  7475. */
  7476. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7477. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7478. if( entry != nullptr ){
  7479. return entry->statpoints;
  7480. }else{
  7481. return 0;
  7482. }
  7483. }
  7484. /**
  7485. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7486. * @param level: Player base level.
  7487. * @param table: Use table value or formula.
  7488. * @return Status points at specific base level.
  7489. */
  7490. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7491. uint32 next_level = this->get_table_point( level + 1 );
  7492. uint32 current_level = this->get_table_point( level );
  7493. if( next_level > current_level ){
  7494. return next_level - current_level;
  7495. }else{
  7496. return 0;
  7497. }
  7498. }
  7499. /**
  7500. * Gets the total number of trait points at the provided level.
  7501. * @param level: Player base level.
  7502. * @return Total number of trait points at specific base level.
  7503. */
  7504. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7505. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7506. if( entry != nullptr ){
  7507. return entry->traitpoints;
  7508. }else{
  7509. return 0;
  7510. }
  7511. }
  7512. /**
  7513. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7514. * @param level: Player base level.
  7515. * @param table: Use table value or formula.
  7516. * @return Trait points at specific base level.
  7517. */
  7518. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7519. uint32 next_level = this->get_trait_table_point( level + 1 );
  7520. uint32 current_level = this->get_trait_table_point( level );
  7521. if( next_level > current_level ){
  7522. return next_level - current_level;
  7523. }else{
  7524. return 0;
  7525. }
  7526. }
  7527. #ifdef RENEWAL_STAT
  7528. /// Renewal status point cost formula
  7529. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7530. #else
  7531. /// Pre-Renewal status point cost formula
  7532. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7533. #endif
  7534. /// Returns the number of stat points needed to change the specified stat by val.
  7535. /// If val is negative, returns the number of stat points that would be needed to
  7536. /// raise the specified stat from (current value - val) to current value.
  7537. int32 pc_need_status_point(map_session_data* sd, int32 type, int32 val)
  7538. {
  7539. int32 low, high, sp = 0, max = 0;
  7540. if ( val == 0 )
  7541. return 0;
  7542. low = pc_getstat(sd,type);
  7543. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7544. if ( low >= max && val > 0 )
  7545. return 0; // Official servers show '0' when max is reached
  7546. high = low + val;
  7547. if ( val < 0 )
  7548. std::swap(low, high);
  7549. for ( ; low < high; low++ )
  7550. sp += PC_STATUS_POINT_COST(low);
  7551. return sp;
  7552. }
  7553. /**
  7554. * Returns the value the specified stat can be increased by with the current
  7555. * amount of available status points for the current character's class.
  7556. *
  7557. * @param sd The target character.
  7558. * @param type Stat to verify.
  7559. * @return Maximum value the stat could grow by.
  7560. */
  7561. int32 pc_maxparameterincrease(map_session_data* sd, int32 type)
  7562. {
  7563. int32 base, final_val, status_points, max_param;
  7564. nullpo_ret(sd);
  7565. base = final_val = pc_getstat(sd, type);
  7566. status_points = sd->status.status_point;
  7567. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7568. while (final_val <= max_param && status_points >= 0) {
  7569. status_points -= PC_STATUS_POINT_COST(final_val);
  7570. final_val++;
  7571. }
  7572. final_val--;
  7573. return (final_val > base ? final_val-base : 0);
  7574. }
  7575. /**
  7576. * Raises a stat by the specified amount.
  7577. *
  7578. * Obeys max_parameter limits.
  7579. * Subtracts status points according to the cost of the increased stat points.
  7580. *
  7581. * @param sd The target character.
  7582. * @param type The stat to change (see enum _sp)
  7583. * @param increase The stat increase (strictly positive) amount.
  7584. * @retval true if the stat was increased by any amount.
  7585. * @retval false if there were no changes.
  7586. */
  7587. bool pc_statusup(map_session_data* sd, int32 type, int32 increase)
  7588. {
  7589. int32 max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7590. nullpo_ret(sd);
  7591. // check conditions
  7592. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7593. clif_statusupack( *sd, type, false );
  7594. return false;
  7595. }
  7596. // check limits
  7597. current = pc_getstat(sd, type);
  7598. max_increase = pc_maxparameterincrease(sd, type);
  7599. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7600. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7601. clif_statusupack( *sd, type, false );
  7602. return false;
  7603. }
  7604. // check status points
  7605. needed_points = pc_need_status_point(sd, type, increase);
  7606. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7607. clif_statusupack( *sd, type, false );
  7608. return false;
  7609. }
  7610. // set new values
  7611. final_value = pc_setstat(sd, type, current + increase);
  7612. sd->status.status_point -= needed_points;
  7613. status_calc_pc(sd,SCO_NONE);
  7614. // update increase cost indicator
  7615. clif_updatestatus(*sd, static_cast<_sp>( SP_USTR + type-SP_STR ) );
  7616. // update statpoint count
  7617. clif_updatestatus(*sd, SP_STATUSPOINT);
  7618. // update stat value
  7619. clif_statusupack( *sd, type, true, final_value );
  7620. if( final_value > 255 )
  7621. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7622. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7623. return true;
  7624. }
  7625. /**
  7626. * Raises a stat by the specified amount.
  7627. *
  7628. * Obeys max_parameter limits.
  7629. * Does not subtract status points for the cost of the modified stat points.
  7630. *
  7631. * @param sd The target character.
  7632. * @param type The stat to change (see enum _sp)
  7633. * @param val The stat increase (or decrease) amount.
  7634. * @return the stat increase amount.
  7635. * @retval 0 if no changes were made.
  7636. */
  7637. int32 pc_statusup2(map_session_data* sd, int32 type, int32 val)
  7638. {
  7639. int32 max, need;
  7640. nullpo_ret(sd);
  7641. if( type < SP_STR || type > SP_LUK )
  7642. {
  7643. clif_statusupack( *sd, type, false );
  7644. return 0;
  7645. }
  7646. need = pc_need_status_point(sd,type,1);
  7647. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7648. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7649. status_calc_pc(sd,SCO_NONE);
  7650. // update increase cost indicator
  7651. if( need != pc_need_status_point(sd,type,1) )
  7652. clif_updatestatus(*sd, static_cast<_sp>( SP_USTR + type-SP_STR ) );
  7653. // update stat value
  7654. clif_statusupack( *sd, type, true, val );
  7655. if( val > 255 )
  7656. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7657. return val;
  7658. }
  7659. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7660. /// If val is negative, returns the number of trait stat points that would be needed to
  7661. /// raise the specified trait stat from (current value - val) to current value.
  7662. int32 pc_need_trait_point(map_session_data* sd, int32 type, int32 val)
  7663. {
  7664. nullpo_retr(0, sd);
  7665. if (val == 0 || type < SP_POW || type > SP_CRT)
  7666. return 0;
  7667. int32 low = pc_getstat(sd, type);
  7668. int32 max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7669. if (low >= max && val > 0)
  7670. return 0; // Official servers show '0' when max is reached
  7671. int32 high = low + val, sp = 0;
  7672. if (val < 0)
  7673. std::swap(low, high);
  7674. for (; low < high; low++)
  7675. sp += 1;
  7676. return sp;
  7677. }
  7678. /**
  7679. * Returns the value the specified trait stat can be increased by with the current
  7680. * amount of available trait status points for the current character's class.
  7681. *
  7682. * @param sd The target character.
  7683. * @param type Trait stat to verify.
  7684. * @return Maximum value the stat could grow by.
  7685. */
  7686. int32 pc_maxtraitparameterincrease(map_session_data* sd, int32 type)
  7687. {
  7688. nullpo_ret(sd);
  7689. if( type < SP_POW || type > SP_CRT ){
  7690. return 0;
  7691. }
  7692. int32 base, final_val = pc_getstat(sd, type);
  7693. int32 trait_points = sd->status.trait_point;
  7694. int32 max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7695. base = final_val;
  7696. while (final_val <= max_param && trait_points >= 0) {
  7697. trait_points -= 1;
  7698. final_val++;
  7699. }
  7700. final_val--;
  7701. return (final_val > base ? final_val - base : 0);
  7702. }
  7703. /**
  7704. * Raises a trait stat by the specified amount.
  7705. *
  7706. * Obeys max_traitparameter limits.
  7707. * Subtracts trait status points according to the cost of the increased trait stat points.
  7708. *
  7709. * @param sd The target character.
  7710. * @param type The stat to change (see enum _sp)
  7711. * @param increase The stat increase (strictly positive) amount.
  7712. * @retval true if the trait stat was increased by any amount.
  7713. * @retval false if there were no changes.
  7714. */
  7715. bool pc_traitstatusup(map_session_data* sd, int32 type, int32 increase)
  7716. {
  7717. nullpo_ret(sd);
  7718. // check conditions
  7719. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7720. clif_statusupack( *sd, type, false );
  7721. return false;
  7722. }
  7723. // check limits
  7724. int32 current = pc_getstat(sd, type);
  7725. int32 max_increase = pc_maxtraitparameterincrease(sd, type);
  7726. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7727. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7728. clif_statusupack( *sd, type, false );
  7729. return false;
  7730. }
  7731. // check status points
  7732. int32 needed_points = pc_need_trait_point(sd, type, increase);
  7733. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7734. clif_statusupack( *sd, type, false );
  7735. return false;
  7736. }
  7737. // set new values
  7738. int32 final_value = pc_setstat(sd, type, current + increase);
  7739. sd->status.trait_point -= needed_points;
  7740. status_calc_pc(sd, SCO_NONE);
  7741. // update increase cost indicator
  7742. clif_updatestatus(*sd, static_cast<_sp>( SP_UPOW + type - SP_POW ) );
  7743. // update statpoint count
  7744. clif_updatestatus(*sd, SP_TRAITPOINT);
  7745. // update stat value
  7746. clif_statusupack( *sd, type, true, final_value );
  7747. if (final_value > 255)
  7748. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7749. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7750. return true;
  7751. }
  7752. /**
  7753. * Raises a trait stat by the specified amount.
  7754. *
  7755. * Obeys max_trait_parameter limits.
  7756. * Does not subtract status points for the cost of the modified stat points.
  7757. *
  7758. * @param sd The target character.
  7759. * @param type The stat to change (see enum _sp)
  7760. * @param val The stat increase (or decrease) amount.
  7761. * @return the stat increase amount.
  7762. * @retval 0 if no changes were made.
  7763. */
  7764. int32 pc_traitstatusup2(map_session_data* sd, int32 type, int32 val)
  7765. {
  7766. nullpo_ret(sd);
  7767. if (type < SP_POW || type > SP_CRT) {
  7768. clif_statusupack( *sd, type, false );
  7769. return 0;
  7770. }
  7771. int32 need = pc_need_trait_point(sd, type, 1);
  7772. int32 max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7773. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7774. status_calc_pc(sd, SCO_NONE);
  7775. // update increase cost indicator
  7776. if (need != pc_need_trait_point(sd, type, 1))
  7777. clif_updatestatus(*sd, static_cast<_sp>( SP_UPOW + type - SP_POW ) );
  7778. // update stat value
  7779. clif_statusupack( *sd, type, true, val );
  7780. if (val > 255)
  7781. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7782. return val;
  7783. }
  7784. /*==========================================
  7785. * Update skill_lv for player sd
  7786. * Skill point allocation
  7787. *------------------------------------------*/
  7788. void pc_skillup(map_session_data *sd,uint16 skill_id)
  7789. {
  7790. uint16 idx = skill_get_index(skill_id);
  7791. nullpo_retv(sd);
  7792. if (!idx) {
  7793. if (skill_id)
  7794. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7795. return;
  7796. }
  7797. // Level up guild skill
  7798. if (SKILL_CHK_GUILD(skill_id)) {
  7799. guild_skillup(sd, skill_id);
  7800. return;
  7801. }
  7802. // Level up homunculus skill
  7803. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7804. hom_skillup(sd->hd, skill_id);
  7805. return;
  7806. }
  7807. else {
  7808. if( sd->status.skill_point > 0 &&
  7809. sd->status.skill[idx].id &&
  7810. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7811. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7812. {
  7813. sd->status.skill[idx].lv++;
  7814. sd->status.skill_point--;
  7815. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7816. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7817. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7818. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7819. else
  7820. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7821. uint16 lv = sd->status.skill[idx].lv;
  7822. int32 range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7823. bool upgradable = ( lv < skill_tree_get_max( sd->status.skill[idx].id, sd->status.class_ ) );
  7824. clif_skillup( *sd, skill_id, lv, range, upgradable );
  7825. clif_updatestatus(*sd,SP_SKILLPOINT);
  7826. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7827. clif_updatestatus(*sd,SP_CARTINFO);
  7828. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7829. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7830. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7831. clif_skillinfoblock(sd);
  7832. }
  7833. //else
  7834. // 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);
  7835. }
  7836. }
  7837. /*==========================================
  7838. * /allskill
  7839. *------------------------------------------*/
  7840. int32 pc_allskillup(map_session_data *sd)
  7841. {
  7842. int32 i;
  7843. nullpo_ret(sd);
  7844. for (i = 0; i < MAX_SKILL; i++) {
  7845. 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) {
  7846. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7847. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7848. if (sd->status.skill[i].lv == 0)
  7849. sd->status.skill[i].id = 0;
  7850. }
  7851. }
  7852. if (!pc_grant_allskills(sd, true)) {
  7853. uint16 sk_id;
  7854. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7855. if (tree != nullptr && !tree->skills.empty()) {
  7856. for (const auto &skillsit : tree->skills) {
  7857. sk_id = skillsit.first;
  7858. uint16 sk_idx = skill_get_index(sk_id);
  7859. if (sk_idx == 0)
  7860. continue;
  7861. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7862. if (
  7863. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7864. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7865. sk_id == SG_DEVIL
  7866. )
  7867. continue; //Cannot be learned normally.
  7868. sd->status.skill[sk_idx].id = sk_id;
  7869. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7870. }
  7871. }
  7872. }
  7873. status_calc_pc(sd,SCO_NONE);
  7874. //Required because if you could level up all skills previously,
  7875. //the update will not be sent as only the lv variable changes.
  7876. clif_skillinfoblock(sd);
  7877. return 0;
  7878. }
  7879. /*==========================================
  7880. * /resetlvl
  7881. *------------------------------------------*/
  7882. int32 pc_resetlvl(map_session_data* sd,int32 type)
  7883. {
  7884. int32 i;
  7885. nullpo_ret(sd);
  7886. if (type != 3) //Also reset skills
  7887. pc_resetskill(sd, 0);
  7888. if(type == 1){
  7889. sd->status.skill_point=0;
  7890. sd->status.trait_point = 0;
  7891. sd->status.base_level=1;
  7892. sd->status.job_level=1;
  7893. sd->status.base_exp=0;
  7894. sd->status.job_exp=0;
  7895. if(sd->sc.option !=0)
  7896. sd->sc.option = 0;
  7897. sd->status.str=1;
  7898. sd->status.agi=1;
  7899. sd->status.vit=1;
  7900. sd->status.int_=1;
  7901. sd->status.dex=1;
  7902. sd->status.luk=1;
  7903. sd->status.pow=0;
  7904. sd->status.sta=0;
  7905. sd->status.wis=0;
  7906. sd->status.spl=0;
  7907. sd->status.con=0;
  7908. sd->status.crt=0;
  7909. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7910. sd->status.status_point=100; // not 88 [celest]
  7911. // give platinum skills upon changing
  7912. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7913. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7914. }
  7915. }
  7916. if(type == 2){
  7917. sd->status.skill_point=0;
  7918. sd->status.base_level=1;
  7919. sd->status.job_level=1;
  7920. sd->status.base_exp=0;
  7921. sd->status.job_exp=0;
  7922. }
  7923. if(type == 3){
  7924. sd->status.base_level=1;
  7925. sd->status.base_exp=0;
  7926. }
  7927. if(type == 4){
  7928. sd->status.job_level=1;
  7929. sd->status.job_exp=0;
  7930. }
  7931. clif_updatestatus(*sd,SP_STATUSPOINT);
  7932. clif_updatestatus(*sd,SP_TRAITPOINT);
  7933. clif_updatestatus(*sd,SP_STR);
  7934. clif_updatestatus(*sd,SP_AGI);
  7935. clif_updatestatus(*sd,SP_VIT);
  7936. clif_updatestatus(*sd,SP_INT);
  7937. clif_updatestatus(*sd,SP_DEX);
  7938. clif_updatestatus(*sd,SP_LUK);
  7939. clif_updatestatus(*sd,SP_POW);
  7940. clif_updatestatus(*sd,SP_STA);
  7941. clif_updatestatus(*sd,SP_WIS);
  7942. clif_updatestatus(*sd,SP_SPL);
  7943. clif_updatestatus(*sd,SP_CON);
  7944. clif_updatestatus(*sd,SP_CRT);
  7945. clif_updatestatus(*sd,SP_BASELEVEL);
  7946. clif_updatestatus(*sd,SP_JOBLEVEL);
  7947. clif_updatestatus(*sd,SP_STATUSPOINT);
  7948. clif_updatestatus(*sd,SP_BASEEXP);
  7949. clif_updatestatus(*sd,SP_JOBEXP);
  7950. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  7951. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  7952. clif_updatestatus(*sd,SP_SKILLPOINT);
  7953. clif_updatestatus(*sd,SP_USTR); // Updates needed stat points - Valaris
  7954. clif_updatestatus(*sd,SP_UAGI);
  7955. clif_updatestatus(*sd,SP_UVIT);
  7956. clif_updatestatus(*sd,SP_UINT);
  7957. clif_updatestatus(*sd,SP_UDEX);
  7958. clif_updatestatus(*sd,SP_ULUK); // End Addition
  7959. clif_updatestatus(*sd,SP_UPOW);
  7960. clif_updatestatus(*sd,SP_USTA);
  7961. clif_updatestatus(*sd,SP_UWIS);
  7962. clif_updatestatus(*sd,SP_USPL);
  7963. clif_updatestatus(*sd,SP_UCON);
  7964. clif_updatestatus(*sd,SP_UCRT);
  7965. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7966. if(sd->equip_index[i] >= 0)
  7967. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  7968. pc_unequipitem(sd,sd->equip_index[i],2);
  7969. }
  7970. }
  7971. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7972. party_send_levelup(sd);
  7973. status_calc_pc(sd, SCO_FORCE);
  7974. clif_skillinfoblock(sd);
  7975. return 0;
  7976. }
  7977. /*==========================================
  7978. * /resetstate
  7979. *------------------------------------------*/
  7980. int32 pc_resetstate(map_session_data* sd)
  7981. {
  7982. nullpo_ret(sd);
  7983. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7984. 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",
  7985. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7986. sd->status.base_level = pc_maxbaselv( sd );
  7987. clif_updatestatus( *sd, SP_BASELEVEL );
  7988. }
  7989. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7990. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7991. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7992. sd->status.status_point += battle_config.transcendent_status_points;
  7993. }
  7994. if ((sd->class_&JOBL_FOURTH) != 0) {
  7995. sd->status.trait_point += battle_config.trait_points_job_change;
  7996. }
  7997. pc_setstat(sd, SP_STR, 1);
  7998. pc_setstat(sd, SP_AGI, 1);
  7999. pc_setstat(sd, SP_VIT, 1);
  8000. pc_setstat(sd, SP_INT, 1);
  8001. pc_setstat(sd, SP_DEX, 1);
  8002. pc_setstat(sd, SP_LUK, 1);
  8003. pc_setstat(sd, SP_POW, 0);
  8004. pc_setstat(sd, SP_STA, 0);
  8005. pc_setstat(sd, SP_WIS, 0);
  8006. pc_setstat(sd, SP_SPL, 0);
  8007. pc_setstat(sd, SP_CON, 0);
  8008. pc_setstat(sd, SP_CRT, 0);
  8009. clif_updatestatus(*sd,SP_STR);
  8010. clif_updatestatus(*sd,SP_AGI);
  8011. clif_updatestatus(*sd,SP_VIT);
  8012. clif_updatestatus(*sd,SP_INT);
  8013. clif_updatestatus(*sd,SP_DEX);
  8014. clif_updatestatus(*sd,SP_LUK);
  8015. clif_updatestatus(*sd,SP_POW);
  8016. clif_updatestatus(*sd,SP_STA);
  8017. clif_updatestatus(*sd,SP_WIS);
  8018. clif_updatestatus(*sd,SP_SPL);
  8019. clif_updatestatus(*sd,SP_CON);
  8020. clif_updatestatus(*sd,SP_CRT);
  8021. clif_updatestatus(*sd,SP_USTR); // Updates needed stat points - Valaris
  8022. clif_updatestatus(*sd,SP_UAGI);
  8023. clif_updatestatus(*sd,SP_UVIT);
  8024. clif_updatestatus(*sd,SP_UINT);
  8025. clif_updatestatus(*sd,SP_UDEX);
  8026. clif_updatestatus(*sd,SP_ULUK); // End Addition
  8027. clif_updatestatus(*sd,SP_UPOW);
  8028. clif_updatestatus(*sd,SP_USTA);
  8029. clif_updatestatus(*sd,SP_UWIS);
  8030. clif_updatestatus(*sd,SP_USPL);
  8031. clif_updatestatus(*sd,SP_UCON);
  8032. clif_updatestatus(*sd,SP_UCRT);
  8033. clif_updatestatus(*sd,SP_STATUSPOINT);
  8034. clif_updatestatus(*sd,SP_TRAITPOINT);
  8035. if( sd->mission_mobid ) { //bugreport:2200
  8036. sd->mission_mobid = 0;
  8037. sd->mission_count = 0;
  8038. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  8039. }
  8040. status_calc_pc(sd, SCO_NONE);
  8041. return 1;
  8042. }
  8043. /*==========================================
  8044. * /resetskill
  8045. * if flag&1, perform block resync and status_calc call.
  8046. * if flag&2, just count total amount of skill points used by player, do not really reset.
  8047. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  8048. *------------------------------------------*/
  8049. int32 pc_resetskill(map_session_data* sd, int32 flag)
  8050. {
  8051. int32 i, skill_point=0;
  8052. nullpo_ret(sd);
  8053. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  8054. return 0;
  8055. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  8056. /**
  8057. * 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)
  8058. **/
  8059. if( pc_is_taekwon_ranker(sd) )
  8060. return 0;
  8061. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  8062. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  8063. i = sd->sc.option;
  8064. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  8065. i &= ~OPTION_RIDING;
  8066. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  8067. i &= ~OPTION_FALCON;
  8068. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  8069. i &= ~OPTION_DRAGON;
  8070. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  8071. i &= ~OPTION_WUG;
  8072. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  8073. i &= ~OPTION_WUGRIDER;
  8074. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  8075. i &= ~OPTION_MADOGEAR;
  8076. #ifndef NEW_CARTS
  8077. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  8078. i &= ~OPTION_CART;
  8079. #else
  8080. if( sd->sc.getSCE(SC_PUSH_CART) )
  8081. pc_setcart(sd, 0);
  8082. #endif
  8083. if( i != sd->sc.option )
  8084. pc_setoption(sd, i);
  8085. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  8086. hom_vaporize(sd, HOM_ST_ACTIVE);
  8087. if (sd->sc.getSCE(SC_SPRITEMABLE) && pc_checkskill(sd, SU_SPRITEMABLE))
  8088. status_change_end(&sd->bl, SC_SPRITEMABLE);
  8089. if (sd->sc.getSCE(SC_SOULATTACK) && pc_checkskill(sd, SU_SOULATTACK))
  8090. status_change_end(&sd->bl, SC_SOULATTACK);
  8091. }
  8092. for (const auto &skill : skill_db) {
  8093. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  8094. uint8 lv = sd->status.skill[idx].lv;
  8095. if (lv == 0 || skill_id == 0)
  8096. continue;
  8097. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  8098. continue;
  8099. // Don't reset trick dead if not a novice/baby
  8100. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8101. {
  8102. sd->status.skill[idx].lv = 0;
  8103. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8104. continue;
  8105. }
  8106. // do not reset basic skill
  8107. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8108. continue;
  8109. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  8110. continue;
  8111. if( flag&4 && !skill_ischangesex(skill_id) )
  8112. continue;
  8113. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  8114. { //Only handle quest skills in a special way when you can't learn them manually
  8115. if( battle_config.quest_skill_reset && !(flag&2) )
  8116. { //Wipe them
  8117. sd->status.skill[idx].lv = 0;
  8118. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8119. }
  8120. continue;
  8121. }
  8122. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  8123. skill_point += lv;
  8124. else
  8125. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  8126. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  8127. if( !(flag&2) )
  8128. {// reset
  8129. sd->status.skill[idx].lv = 0;
  8130. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8131. }
  8132. }
  8133. if( flag&2 || !skill_point ) return skill_point;
  8134. sd->status.skill_point += skill_point;
  8135. if (flag&1) {
  8136. clif_updatestatus(*sd,SP_SKILLPOINT);
  8137. clif_skillinfoblock(sd);
  8138. status_calc_pc(sd, SCO_FORCE);
  8139. }
  8140. return skill_point;
  8141. }
  8142. /*==========================================
  8143. * /resetfeel [Komurka]
  8144. *------------------------------------------*/
  8145. int32 pc_resetfeel(map_session_data* sd)
  8146. {
  8147. int32 i;
  8148. nullpo_ret(sd);
  8149. for (i=0; i<MAX_PC_FEELHATE; i++)
  8150. {
  8151. sd->feel_map[i].m = -1;
  8152. sd->feel_map[i].index = 0;
  8153. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  8154. }
  8155. return 0;
  8156. }
  8157. int32 pc_resethate(map_session_data* sd)
  8158. {
  8159. int32 i;
  8160. nullpo_ret(sd);
  8161. for (i=0; i<MAX_PC_FEELHATE; i++)
  8162. {
  8163. sd->hate_mob[i] = -1;
  8164. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  8165. }
  8166. return 0;
  8167. }
  8168. int32 pc_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8169. {
  8170. int32 bonus = 0;
  8171. nullpo_ret(sd);
  8172. skill_id = skill_dummy2skill_id(skill_id);
  8173. for (auto &it : sd->skillatk) {
  8174. if (it.id == skill_id) {
  8175. bonus += it.val;
  8176. break;
  8177. }
  8178. }
  8179. return bonus;
  8180. }
  8181. int32 pc_sub_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8182. {
  8183. int32 bonus = 0;
  8184. nullpo_ret(sd);
  8185. skill_id = skill_dummy2skill_id(skill_id);
  8186. for (auto &it : sd->subskill) {
  8187. if (it.id == skill_id) {
  8188. bonus += it.val;
  8189. break;
  8190. }
  8191. }
  8192. return bonus;
  8193. }
  8194. int32 pc_skillheal_bonus(map_session_data *sd, uint16 skill_id) {
  8195. int32 bonus = sd->bonus.add_heal_rate;
  8196. nullpo_ret(sd);
  8197. skill_id = skill_dummy2skill_id(skill_id);
  8198. if( bonus ) {
  8199. switch( skill_id ) {
  8200. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  8201. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  8202. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  8203. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  8204. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  8205. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  8206. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  8207. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  8208. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  8209. }
  8210. }
  8211. for (auto &it : sd->skillheal) {
  8212. if (it.id == skill_id) {
  8213. bonus += it.val;
  8214. break;
  8215. }
  8216. }
  8217. return bonus;
  8218. }
  8219. int32 pc_skillheal2_bonus(map_session_data *sd, uint16 skill_id) {
  8220. int32 bonus = sd->bonus.add_heal2_rate;
  8221. skill_id = skill_dummy2skill_id(skill_id);
  8222. for (auto &it : sd->skillheal2) {
  8223. if (it.id == skill_id) {
  8224. bonus += it.val;
  8225. break;
  8226. }
  8227. }
  8228. return bonus;
  8229. }
  8230. void pc_respawn(map_session_data* sd, clr_type clrtype)
  8231. {
  8232. if( !pc_isdead(sd) )
  8233. return; // not applicable
  8234. if( sd->bg_id && bg_member_respawn(sd) )
  8235. return; // member revived by battleground
  8236. pc_setstand(sd, true);
  8237. pc_setrestartvalue(sd,3);
  8238. if( pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, clrtype ) != SETPOS_OK ){
  8239. clif_resurrection( sd->bl ); //If warping fails, send a normal stand up packet.
  8240. }
  8241. }
  8242. static TIMER_FUNC(pc_respawn_timer){
  8243. map_session_data *sd = map_id2sd(id);
  8244. if( sd != nullptr )
  8245. {
  8246. sd->pvp_point=0;
  8247. sd->respawn_tid = INVALID_TIMER;
  8248. pc_respawn(sd,CLR_OUTSIGHT);
  8249. }
  8250. return 0;
  8251. }
  8252. /*==========================================
  8253. * Invoked when a player has received damage
  8254. *------------------------------------------*/
  8255. void pc_damage(map_session_data *sd,struct block_list *src,uint32 hp, uint32 sp, uint32 ap)
  8256. {
  8257. if (ap) clif_updatestatus(*sd,SP_AP);
  8258. if (sp) clif_updatestatus(*sd,SP_SP);
  8259. if (hp) clif_updatestatus(*sd,SP_HP);
  8260. else return;
  8261. if (!src)
  8262. return;
  8263. if( pc_issit(sd) ) {
  8264. pc_setstand(sd, true);
  8265. skill_sit(sd,0);
  8266. }
  8267. if (sd->progressbar.npc_id)
  8268. clif_progressbar_abort(sd);
  8269. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  8270. pet_target_check(sd->pd,src,1);
  8271. if( sd->status.ele_id > 0 )
  8272. elemental_set_target(sd,src);
  8273. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  8274. sd->canlog_tick = gettick();
  8275. }
  8276. TIMER_FUNC(pc_close_npc_timer){
  8277. map_session_data* sd = map_id2sd( id );
  8278. if( sd != nullptr ){
  8279. pc_close_npc( sd, static_cast<int32>( data ) );
  8280. }
  8281. return 0;
  8282. }
  8283. /**
  8284. * Method to properly close a NPC for player and clear anything related.
  8285. * @param sd: Player attached
  8286. * @param flag: Method of closure
  8287. * 1: Produce a close button and end the NPC
  8288. * 2: End the NPC (best for no dialog windows)
  8289. */
  8290. void pc_close_npc(map_session_data *sd,int32 flag)
  8291. {
  8292. nullpo_retv(sd);
  8293. if (sd->npc_id || sd->npc_shopid) {
  8294. if (sd->state.using_fake_npc) {
  8295. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, *sd );
  8296. sd->state.using_fake_npc = 0;
  8297. }
  8298. if (sd->st) {
  8299. if(sd->st->state == RUN){ //wait ending code execution
  8300. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  8301. return;
  8302. }
  8303. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  8304. sd->st->mes_active = 0;
  8305. }
  8306. sd->state.menu_or_input = 0;
  8307. sd->npc_menu = 0;
  8308. sd->npc_shopid = 0;
  8309. #ifdef SECURE_NPCTIMEOUT
  8310. if( sd->npc_idle_timer != INVALID_TIMER ){
  8311. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  8312. sd->npc_idle_timer = INVALID_TIMER;
  8313. }
  8314. #endif
  8315. if (sd->st) {
  8316. if (sd->st->state == CLOSE) {
  8317. clif_scriptclose( *sd, sd->npc_id );
  8318. clif_cutin( *sd, "", 255); // Force to end cutin [Haydrich]
  8319. sd->st->state = END; // Force to end now
  8320. }
  8321. if (sd->st->state == END) { // free attached scripts that are waiting
  8322. script_free_state(sd->st);
  8323. sd->st = nullptr;
  8324. sd->npc_id = 0;
  8325. }
  8326. }
  8327. }
  8328. }
  8329. /*==========================================
  8330. * Invoked when a player has negative current hp
  8331. *------------------------------------------*/
  8332. int32 pc_dead(map_session_data *sd,struct block_list *src)
  8333. {
  8334. int32 i=0,k=0;
  8335. t_tick tick = gettick();
  8336. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8337. // Activate Steel body if a super novice dies at 99+% exp [celest]
  8338. // Super Novices have no kill or die functions attached when saved by their angel
  8339. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  8340. t_exp exp = pc_nextbaseexp(sd);
  8341. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  8342. sd->state.snovice_dead_flag = 1;
  8343. pc_setrestartvalue(sd,1);
  8344. status_percent_heal(&sd->bl, 100, 100);
  8345. clif_resurrection( sd->bl );
  8346. if(battle_config.pc_invincible_time)
  8347. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8348. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  8349. if(mapdata_flag_gvg2(mapdata))
  8350. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  8351. return 0;
  8352. }
  8353. }
  8354. for(k = 0; k < MAX_DEVOTION; k++) {
  8355. if (sd->devotion[k]){
  8356. map_session_data *devsd = map_id2sd(sd->devotion[k]);
  8357. if (devsd)
  8358. status_change_end(&devsd->bl, SC_DEVOTION);
  8359. sd->devotion[k] = 0;
  8360. }
  8361. }
  8362. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  8363. if (sd->stellar_mark[k]) {
  8364. map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  8365. if (smarksd)
  8366. status_change_end(&smarksd->bl, SC_FLASHKICK);
  8367. sd->stellar_mark[k] = 0;
  8368. }
  8369. }
  8370. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  8371. if (sd->united_soul[k]) {
  8372. map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  8373. if (usoulsd)
  8374. status_change_end(&usoulsd->bl, SC_SOULUNITY);
  8375. sd->united_soul[k] = 0;
  8376. }
  8377. }
  8378. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  8379. if (sd->servant_sign[k]) {
  8380. map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  8381. if (ssignsd)
  8382. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN);
  8383. sd->servant_sign[k] = 0;
  8384. }
  8385. }
  8386. if(sd->shadowform_id) { //if we were target of shadowform
  8387. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM);
  8388. sd->shadowform_id = 0; //should be remove on status end anyway
  8389. }
  8390. if(sd->status.pet_id > 0 && sd->pd) {
  8391. struct pet_data *pd = sd->pd;
  8392. if( !mapdata->getMapFlag(MF_NOEXPPENALTY) ) {
  8393. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  8394. clif_send_petdata( sd, *sd->pd, CHANGESTATEPET_INTIMACY );
  8395. }
  8396. if( sd->pd->target_id ) // Unlock all targets...
  8397. pet_unlocktarget(sd->pd);
  8398. }
  8399. 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)
  8400. hom_vaporize(sd, HOM_ST_ACTIVE);
  8401. if( sd->md )
  8402. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  8403. if( sd->ed )
  8404. elemental_delete(sd->ed);
  8405. // Leave duel if you die [LuzZza]
  8406. if(battle_config.duel_autoleave_when_die) {
  8407. if(sd->duel_group > 0)
  8408. duel_leave(sd->duel_group, sd);
  8409. if(sd->duel_invite > 0)
  8410. duel_reject(sd->duel_invite, sd);
  8411. }
  8412. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  8413. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  8414. sd->skill_keep_using.tid = INVALID_TIMER;
  8415. }
  8416. pc_close_npc(sd,2); //close npc if we were using one
  8417. /* e.g. not killed thru pc_damage */
  8418. if( pc_issit(sd) ) {
  8419. clif_status_load(&sd->bl,EFST_SIT,0);
  8420. }
  8421. pc_setdead(sd);
  8422. clif_party_dead( *sd );
  8423. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  8424. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  8425. if (battle_config.loose_ap_on_death == 1)
  8426. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  8427. //Reset menu skills/item skills
  8428. if ((sd->skillitem) != 0)
  8429. sd->skillitem = sd->skillitemlv = 0;
  8430. if ((sd->menuskill_id) != 0)
  8431. sd->menuskill_id = sd->menuskill_val = 0;
  8432. //Reset ticks.
  8433. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  8434. if ( sd->spiritball !=0 )
  8435. pc_delspiritball(sd,sd->spiritball,0);
  8436. if (sd->soulball != 0)
  8437. pc_delsoulball( *sd, sd->soulball );
  8438. if (sd->servantball != 0)
  8439. pc_delservantball( *sd, sd->servantball );
  8440. if (sd->abyssball != 0)
  8441. pc_delabyssball( *sd, sd->abyssball );
  8442. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  8443. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  8444. if (src)
  8445. switch (src->type) {
  8446. case BL_MOB:
  8447. {
  8448. struct mob_data *md=(struct mob_data *)src;
  8449. if(md->target_id==sd->bl.id)
  8450. mob_unlocktarget(md,tick);
  8451. if(battle_config.mobs_level_up && md->status.hp &&
  8452. (uint32)md->level < pc_maxbaselv(sd) &&
  8453. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  8454. ) { // monster level up [Valaris]
  8455. clif_misceffect( md->bl, NOTIFYEFFECT_BASE_LEVEL_UP );
  8456. md->level++;
  8457. status_calc_mob(md, SCO_NONE);
  8458. status_percent_heal(src,10,0);
  8459. if( battle_config.show_mob_info&4 )
  8460. {// update name with new level
  8461. clif_name_area(&md->bl);
  8462. }
  8463. }
  8464. src = battle_get_master(src); // Maybe Player Summon
  8465. }
  8466. break;
  8467. case BL_PET: //Pass on to master...
  8468. case BL_HOM:
  8469. case BL_MER:
  8470. src = battle_get_master(src);
  8471. break;
  8472. }
  8473. if (src && src->type == BL_PC) {
  8474. map_session_data *ssd = (map_session_data *)src;
  8475. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8476. npc_script_event( *ssd, NPCE_KILLPC );
  8477. if (battle_config.pk_mode&2) {
  8478. ssd->status.manner -= 5;
  8479. if(ssd->status.manner < 0)
  8480. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8481. #if 0
  8482. // PK/Karma system code (not enabled yet) [celest]
  8483. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8484. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8485. // karma going down = more 'good' / more honourable.
  8486. // The Karma System way...
  8487. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8488. sd->status.karma--;
  8489. ssd->status.karma--;
  8490. }
  8491. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8492. ssd->status.karma++;
  8493. // or the PK System way...
  8494. if (sd->status.karma > 0) // player killed is dishonourable?
  8495. ssd->status.karma--; // honour points earned
  8496. sd->status.karma++; // honour points lost
  8497. // To-do: Receive exp on certain occasions
  8498. #endif
  8499. }
  8500. }
  8501. if(battle_config.bone_drop==2
  8502. || (battle_config.bone_drop==1 && mapdata->getMapFlag(MF_PVP)))
  8503. {
  8504. struct item item_tmp;
  8505. memset(&item_tmp,0,sizeof(item_tmp));
  8506. item_tmp.nameid=ITEMID_SKULL_;
  8507. item_tmp.identify=1;
  8508. item_tmp.card[0]=CARD0_CREATE;
  8509. item_tmp.card[1]=0;
  8510. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8511. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8512. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,4,0);
  8513. }
  8514. //Remove bonus_script when dead
  8515. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8516. // changed penalty options, added death by player if pk_mode [Valaris]
  8517. if(battle_config.death_penalty_type
  8518. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8519. && !sd->sc.cant.deathpenalty
  8520. && !mapdata->getMapFlag(MF_NOEXPPENALTY) && !mapdata_flag_gvg2(mapdata))
  8521. {
  8522. t_exp base_penalty = 0;
  8523. t_exp job_penalty = 0;
  8524. uint32 zeny_penalty = 0;
  8525. if (pc_isvip(sd)) { // EXP penalty for VIP
  8526. base_penalty = battle_config.vip_exp_penalty_base;
  8527. job_penalty = battle_config.vip_exp_penalty_job;
  8528. zeny_penalty = battle_config.vip_zeny_penalty;
  8529. } else {
  8530. base_penalty = battle_config.death_penalty_base;
  8531. job_penalty = battle_config.death_penalty_job;
  8532. zeny_penalty = battle_config.zeny_penalty;
  8533. }
  8534. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8535. switch (battle_config.death_penalty_type) {
  8536. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8537. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8538. }
  8539. if (base_penalty){ //recheck after altering to speedup
  8540. if (battle_config.pk_mode && src && src->type==BL_PC)
  8541. base_penalty *= 2;
  8542. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8543. }
  8544. }
  8545. else
  8546. base_penalty = 0;
  8547. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8548. switch (battle_config.death_penalty_type) {
  8549. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8550. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8551. }
  8552. if (job_penalty) {
  8553. if (battle_config.pk_mode && src && src->type==BL_PC)
  8554. job_penalty *= 2;
  8555. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8556. }
  8557. }
  8558. else
  8559. job_penalty = 0;
  8560. if (base_penalty || job_penalty) {
  8561. int16 insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8562. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8563. pc_lostexp(sd, base_penalty, job_penalty);
  8564. }
  8565. if( zeny_penalty > 0 && !mapdata->getMapFlag(MF_NOZENYPENALTY)) {
  8566. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8567. if(zeny_penalty)
  8568. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER);
  8569. }
  8570. }
  8571. if( mapdata->getMapFlag(MF_PVP_NIGHTMAREDROP) ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8572. for (const auto &it : mapdata->drop_list) {
  8573. int32 id = it.drop_id, per = it.drop_per;
  8574. enum e_nightmare_drop_type type = it.drop_type;
  8575. if(id == 0)
  8576. continue;
  8577. if(id == -1){
  8578. int32 eq_num=0,eq_n[MAX_INVENTORY];
  8579. memset(eq_n,0,sizeof(eq_n));
  8580. for(i=0;i<MAX_INVENTORY;i++) {
  8581. if( sd->inventory.u.items_inventory[i].nameid == 0 ){
  8582. continue;
  8583. }
  8584. if( !pc_candrop( sd, &sd->inventory.u.items_inventory[i] ) ){
  8585. continue;
  8586. }
  8587. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8588. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8589. || type == NMDT_ALL)
  8590. {
  8591. eq_n[eq_num++] = i;
  8592. }
  8593. }
  8594. if(eq_num > 0){
  8595. if(rnd()%10000 < per) {
  8596. int32 n = eq_n[rnd() % eq_num];
  8597. if(sd->inventory.u.items_inventory[n].equip)
  8598. pc_unequipitem(sd,n,3);
  8599. pc_dropitem(sd,n,1);
  8600. }
  8601. }
  8602. }
  8603. else if(id > 0) {
  8604. for(i=0;i<MAX_INVENTORY;i++){
  8605. if(sd->inventory.u.items_inventory[i].nameid == id
  8606. && pc_candrop( sd, &sd->inventory.u.items_inventory[i] )
  8607. && rnd()%10000 < per
  8608. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8609. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8610. || type == NMDT_ALL) ){
  8611. if(sd->inventory.u.items_inventory[i].equip)
  8612. pc_unequipitem(sd,i,3);
  8613. pc_dropitem(sd,i,1);
  8614. break;
  8615. }
  8616. }
  8617. }
  8618. }
  8619. }
  8620. // pvp
  8621. // disable certain pvp functions on pk_mode [Valaris]
  8622. if( !battle_config.pk_mode && mapdata->getMapFlag(MF_PVP) && !mapdata->getMapFlag(MF_PVP_NOCALCRANK) ) {
  8623. sd->pvp_point -= 5;
  8624. sd->pvp_lost++;
  8625. if( src && src->type == BL_PC ) {
  8626. map_session_data *ssd = (map_session_data *)src;
  8627. ssd->pvp_point++;
  8628. ssd->pvp_won++;
  8629. }
  8630. if( sd->pvp_point < 0 ) {
  8631. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8632. return 1|8;
  8633. }
  8634. }
  8635. //GvG
  8636. if( mapdata_flag_gvg2(mapdata) ) {
  8637. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8638. return 1|8;
  8639. }
  8640. else if( sd->bg_id ) {
  8641. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8642. if (bg) {
  8643. if (bg->cemetery.map > 0) { // Respawn by BG
  8644. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8645. return 1|8;
  8646. }
  8647. }
  8648. }
  8649. //Reset "can log out" tick.
  8650. if( battle_config.prevent_logout )
  8651. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8652. return 1;
  8653. }
  8654. void pc_revive(map_session_data *sd,uint32 hp, uint32 sp, uint32 ap) {
  8655. if(hp) clif_updatestatus(*sd,SP_HP);
  8656. if(sp) clif_updatestatus(*sd,SP_SP);
  8657. if(ap) clif_updatestatus(*sd,SP_AP);
  8658. pc_setstand(sd, true);
  8659. if(battle_config.pc_invincible_time > 0)
  8660. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8661. if (sd->state.gmaster_flag && sd->guild) {
  8662. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  8663. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  8664. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  8665. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  8666. }
  8667. }
  8668. bool pc_revive_item(map_session_data *sd) {
  8669. nullpo_retr(false, sd);
  8670. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8671. return false;
  8672. if (sd->sc.getSCE(SC_HELLPOWER)) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8673. return false;
  8674. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8675. uint8 hp = 100, sp = 100;
  8676. if (item_position < 0) {
  8677. if (sd->sc.getSCE(SC_LIGHT_OF_REGENE)) {
  8678. hp = sd->sc.getSCE(SC_LIGHT_OF_REGENE)->val2;
  8679. sp = 0;
  8680. }
  8681. else
  8682. return false;
  8683. }
  8684. if (!status_revive(&sd->bl, hp, sp))
  8685. return false;
  8686. if (item_position < 0)
  8687. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE);
  8688. else
  8689. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8690. clif_skill_nodamage(&sd->bl, sd->bl, ALL_RESURRECTION, 4);
  8691. return true;
  8692. }
  8693. // script
  8694. //
  8695. /*==========================================
  8696. * script reading pc status registry
  8697. *------------------------------------------*/
  8698. int64 pc_readparam(map_session_data* sd,int64 type)
  8699. {
  8700. int64 val = 0;
  8701. nullpo_ret(sd);
  8702. switch(type) {
  8703. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8704. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8705. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8706. case SP_ZENY: val = sd->status.zeny; break;
  8707. case SP_BASELEVEL: val = sd->status.base_level; break;
  8708. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8709. case SP_CLASS: val = sd->status.class_; break;
  8710. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8711. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8712. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8713. case SP_SEX: val = sd->status.sex; break;
  8714. case SP_WEIGHT: val = sd->weight; break;
  8715. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8716. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8717. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8718. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8719. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8720. case SP_HP: val = sd->battle_status.hp; break;
  8721. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8722. case SP_SP: val = sd->battle_status.sp; break;
  8723. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8724. case SP_AP: val = sd->battle_status.ap; break;
  8725. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8726. case SP_STR: val = sd->status.str; break;
  8727. case SP_AGI: val = sd->status.agi; break;
  8728. case SP_VIT: val = sd->status.vit; break;
  8729. case SP_INT: val = sd->status.int_; break;
  8730. case SP_DEX: val = sd->status.dex; break;
  8731. case SP_LUK: val = sd->status.luk; break;
  8732. case SP_POW: val = sd->status.pow; break;
  8733. case SP_STA: val = sd->status.sta; break;
  8734. case SP_WIS: val = sd->status.wis; break;
  8735. case SP_SPL: val = sd->status.spl; break;
  8736. case SP_CON: val = sd->status.con; break;
  8737. case SP_CRT: val = sd->status.crt; break;
  8738. case SP_KARMA: val = sd->status.karma; break;
  8739. case SP_MANNER: val = sd->status.manner; break;
  8740. case SP_FAME: val = sd->status.fame; break;
  8741. case SP_KILLERRID: val = sd->killerrid; break;
  8742. case SP_KILLEDRID: val = sd->killedrid; break;
  8743. case SP_KILLEDGID: val = sd->killedgid; break;
  8744. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8745. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8746. case SP_CHARRENAME: val = sd->status.rename; break;
  8747. case SP_CHARFONT: val = sd->status.font; break;
  8748. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8749. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8750. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8751. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8752. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8753. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8754. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8755. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8756. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8757. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8758. case SP_ASPD: val = (AMOTION_ZERO_ASPD-sd->battle_status.amotion)/AMOTION_INTERVAL; break;
  8759. case SP_BASE_ATK:
  8760. #ifdef RENEWAL
  8761. val = sd->bonus.eatk;
  8762. #else
  8763. val = sd->battle_status.batk;
  8764. #endif
  8765. break;
  8766. case SP_DEF1: val = sd->battle_status.def; break;
  8767. case SP_DEF2: val = sd->battle_status.def2; break;
  8768. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8769. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8770. case SP_HIT: val = sd->battle_status.hit; break;
  8771. case SP_FLEE1: val = sd->battle_status.flee; break;
  8772. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8773. case SP_PATK: val = sd->battle_status.patk; break;
  8774. case SP_SMATK: val = sd->battle_status.smatk; break;
  8775. case SP_RES: val = sd->battle_status.res; break;
  8776. case SP_MRES: val = sd->battle_status.mres; break;
  8777. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8778. case SP_CRATE: val = sd->battle_status.crate; break;
  8779. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8780. case SP_MAXHPRATE: val = sd->hprate; break;
  8781. case SP_MAXSPRATE: val = sd->sprate; break;
  8782. case SP_MAXAPRATE: val = sd->aprate; break;
  8783. case SP_SPRATE: val = sd->dsprate; break;
  8784. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8785. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8786. case SP_ASPD_RATE:
  8787. #ifndef RENEWAL_ASPD
  8788. val = sd->battle_status.aspd_rate;
  8789. #else
  8790. val = sd->battle_status.aspd_rate2;
  8791. #endif
  8792. break;
  8793. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8794. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8795. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8796. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8797. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8798. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8799. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8800. case SP_MATK_RATE: val = sd->matk_rate; break;
  8801. case SP_ATK_RATE:
  8802. #ifdef RENEWAL
  8803. val = sd->bonus.atk_rate;
  8804. #else
  8805. ShowError( "pc_readparam: %s is not supported in Pre-Renewal mode.\n", QUOTE( SP_ATK_RATE ) );
  8806. #endif
  8807. break;
  8808. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8809. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8810. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8811. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8812. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8813. case SP_HIT_RATE: val = sd->hit_rate; break;
  8814. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8815. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8816. case SP_DEF_RATE: val = sd->def_rate; break;
  8817. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8818. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8819. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8820. case SP_PATK_RATE: val = sd->patk_rate; break;
  8821. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8822. case SP_RES_RATE: val = sd->res_rate; break;
  8823. case SP_MRES_RATE: val = sd->mres_rate; break;
  8824. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8825. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8826. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8827. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8828. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8829. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8830. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8831. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8832. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8833. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8834. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8835. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8836. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8837. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8838. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8839. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8840. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8841. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8842. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8843. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8844. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8845. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8846. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8847. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8848. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8849. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8850. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8851. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8852. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8853. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8854. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8855. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8856. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8857. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8858. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8859. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8860. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8861. case SP_UNSTRIPABLE:
  8862. case SP_UNSTRIPABLE_ARMOR:
  8863. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8864. break;
  8865. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8866. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8867. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8868. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8869. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8870. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8871. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8872. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8873. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8874. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8875. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8876. case SP_EMATK: val = sd->bonus.ematk; break;
  8877. case SP_EMATK_HIDDEN: val = sd->bonus.ematk_hidden; break;
  8878. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8879. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8880. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8881. case SP_CASTRATE:
  8882. case SP_VARCASTRATE:
  8883. #ifdef RENEWAL_CAST
  8884. val = sd->bonus.varcastrate; break;
  8885. #else
  8886. val = sd->castrate; break;
  8887. #endif
  8888. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8889. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8890. default:
  8891. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8892. return -1;
  8893. }
  8894. return val;
  8895. }
  8896. /*==========================================
  8897. * script set pc status registry
  8898. *------------------------------------------*/
  8899. bool pc_setparam(map_session_data *sd,int64 type,int64 val_tmp)
  8900. {
  8901. nullpo_retr(false,sd);
  8902. int32 val = static_cast<uint32>(val_tmp);
  8903. switch(type){
  8904. case SP_BASELEVEL:
  8905. if (val > pc_maxbaselv(sd)) //Capping to max
  8906. val = pc_maxbaselv(sd);
  8907. if (val > sd->status.base_level) {
  8908. for( int32 i = 0; i < (int32)( val - sd->status.base_level ); i++ ){
  8909. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8910. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8911. }
  8912. }
  8913. sd->status.base_level = val;
  8914. sd->status.base_exp = 0;
  8915. // clif_updatestatus(*sd, SP_BASELEVEL); // Gets updated at the bottom
  8916. clif_updatestatus(*sd, SP_NEXTBASEEXP);
  8917. clif_updatestatus(*sd, SP_STATUSPOINT);
  8918. clif_updatestatus(*sd, SP_TRAITPOINT);
  8919. clif_updatestatus(*sd, SP_BASEEXP);
  8920. status_calc_pc(sd, SCO_FORCE);
  8921. if(sd->status.party_id)
  8922. party_send_levelup(sd);
  8923. break;
  8924. case SP_JOBLEVEL:
  8925. if (val >= sd->status.job_level) {
  8926. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8927. sd->status.skill_point += val - sd->status.job_level;
  8928. clif_updatestatus(*sd, SP_SKILLPOINT);
  8929. }
  8930. sd->status.job_level = val;
  8931. sd->status.job_exp = 0;
  8932. // clif_updatestatus(*sd, SP_JOBLEVEL); // Gets updated at the bottom
  8933. clif_updatestatus(*sd, SP_NEXTJOBEXP);
  8934. clif_updatestatus(*sd, SP_JOBEXP);
  8935. status_calc_pc(sd, SCO_FORCE);
  8936. break;
  8937. case SP_SKILLPOINT:
  8938. sd->status.skill_point = val;
  8939. break;
  8940. case SP_STATUSPOINT:
  8941. sd->status.status_point = val;
  8942. break;
  8943. case SP_TRAITPOINT:
  8944. sd->status.trait_point = val;
  8945. break;
  8946. case SP_ZENY:
  8947. if( val < 0 )
  8948. return false;// can't set negative zeny
  8949. log_zeny(*sd, LOG_TYPE_SCRIPT, sd->status.char_id, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8950. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8951. break;
  8952. case SP_BASEEXP:
  8953. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8954. if (val_tmp < sd->status.base_exp) // Lost
  8955. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8956. else // Gained
  8957. pc_gainexp(sd, nullptr, val_tmp - sd->status.base_exp, 0, 2);
  8958. return true;
  8959. case SP_JOBEXP:
  8960. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8961. if (val_tmp < sd->status.job_exp) // Lost
  8962. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8963. else // Gained
  8964. pc_gainexp(sd, nullptr, 0, val_tmp - sd->status.job_exp, 2);
  8965. return true;
  8966. case SP_SEX:
  8967. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8968. break;
  8969. case SP_WEIGHT:
  8970. sd->weight = val;
  8971. break;
  8972. case SP_MAXWEIGHT:
  8973. sd->max_weight = val;
  8974. break;
  8975. case SP_HP:
  8976. sd->battle_status.hp = cap_value(val, 1, (int32)sd->battle_status.max_hp);
  8977. break;
  8978. case SP_MAXHP:
  8979. if (sd->status.base_level < 100)
  8980. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8981. else if (sd->status.base_level < 151)
  8982. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8983. else
  8984. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8985. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8986. {
  8987. sd->battle_status.hp = sd->battle_status.max_hp;
  8988. clif_updatestatus(*sd, SP_HP);
  8989. }
  8990. break;
  8991. case SP_SP:
  8992. sd->battle_status.sp = cap_value(val, 0, (int32)sd->battle_status.max_sp);
  8993. break;
  8994. case SP_MAXSP:
  8995. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8996. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8997. {
  8998. sd->battle_status.sp = sd->battle_status.max_sp;
  8999. clif_updatestatus(*sd, SP_SP);
  9000. }
  9001. break;
  9002. case SP_AP:
  9003. sd->battle_status.ap = cap_value(val, 0, (int32)sd->battle_status.max_ap);
  9004. break;
  9005. case SP_MAXAP:
  9006. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  9007. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  9008. sd->battle_status.ap = sd->battle_status.max_ap;
  9009. clif_updatestatus(*sd, SP_AP);
  9010. }
  9011. break;
  9012. case SP_STR:
  9013. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  9014. break;
  9015. case SP_AGI:
  9016. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  9017. break;
  9018. case SP_VIT:
  9019. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  9020. break;
  9021. case SP_INT:
  9022. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  9023. break;
  9024. case SP_DEX:
  9025. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  9026. break;
  9027. case SP_LUK:
  9028. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  9029. break;
  9030. case SP_POW:
  9031. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  9032. break;
  9033. case SP_STA:
  9034. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  9035. break;
  9036. case SP_WIS:
  9037. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  9038. break;
  9039. case SP_SPL:
  9040. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  9041. break;
  9042. case SP_CON:
  9043. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  9044. break;
  9045. case SP_CRT:
  9046. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  9047. break;
  9048. case SP_KARMA:
  9049. sd->status.karma = val;
  9050. break;
  9051. case SP_MANNER:
  9052. sd->status.manner = val;
  9053. if( val < 0 )
  9054. sc_start(nullptr, &sd->bl, SC_NOCHAT, 100, 0, 0);
  9055. else {
  9056. status_change_end(&sd->bl, SC_NOCHAT);
  9057. clif_manner_message(sd, 5);
  9058. }
  9059. return true; // status_change_start/status_change_end already sends packets warning the client
  9060. case SP_FAME:
  9061. sd->status.fame = val;
  9062. break;
  9063. case SP_KILLERRID:
  9064. sd->killerrid = val;
  9065. return true;
  9066. case SP_KILLEDRID:
  9067. sd->killedrid = val;
  9068. return true;
  9069. case SP_KILLEDGID:
  9070. sd->killedgid = val;
  9071. return true;
  9072. case SP_CHARMOVE:
  9073. sd->status.character_moves = val;
  9074. return true;
  9075. case SP_CHARRENAME:
  9076. sd->status.rename = val;
  9077. return true;
  9078. case SP_CHARFONT:
  9079. sd->status.font = val;
  9080. clif_font(sd);
  9081. return true;
  9082. case SP_BANK_VAULT:
  9083. if (val < 0)
  9084. return false;
  9085. log_zeny(*sd, LOG_TYPE_BANK, sd->status.char_id, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  9086. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  9087. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  9088. return true;
  9089. case SP_ROULETTE_BRONZE:
  9090. sd->roulette_point.bronze = val;
  9091. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  9092. return true;
  9093. case SP_ROULETTE_SILVER:
  9094. sd->roulette_point.silver = val;
  9095. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  9096. return true;
  9097. case SP_ROULETTE_GOLD:
  9098. sd->roulette_point.gold = val;
  9099. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  9100. return true;
  9101. case SP_CASHPOINTS:
  9102. if (val < 0)
  9103. return false;
  9104. if (!sd->state.connect_new)
  9105. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_CASHPOINT)));
  9106. sd->cashPoints = cap_value(val, 0, MAX_CASHPOINT);
  9107. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  9108. return true;
  9109. case SP_KAFRAPOINTS:
  9110. if (val < 0)
  9111. return false;
  9112. if (!sd->state.connect_new)
  9113. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_KAFRAPOINT)));
  9114. sd->kafraPoints = cap_value(val, 0, MAX_KAFRAPOINT);
  9115. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  9116. return true;
  9117. case SP_PCDIECOUNTER:
  9118. if (val < 0)
  9119. return false;
  9120. if (sd->die_counter == val)
  9121. return true;
  9122. sd->die_counter = val;
  9123. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  9124. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  9125. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  9126. return true;
  9127. case SP_COOKMASTERY:
  9128. if (sd->cook_mastery == val)
  9129. return true;
  9130. val = cap_value(val, 0, 1999);
  9131. sd->cook_mastery = val;
  9132. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  9133. return true;
  9134. default:
  9135. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  9136. return false;
  9137. }
  9138. clif_updatestatus(*sd,static_cast<_sp>(type));
  9139. return true;
  9140. }
  9141. /*==========================================
  9142. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  9143. *------------------------------------------*/
  9144. void pc_heal(map_session_data *sd,uint32 hp,uint32 sp, uint32 ap, int32 type)
  9145. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  9146. nullpo_retv(sd);
  9147. if (type&2) {
  9148. if (hp || type&4) {
  9149. clif_heal( *sd, SP_HP, hp );
  9150. clif_update_hp(*sd);
  9151. }
  9152. if (sp)
  9153. clif_heal( *sd, SP_SP, sp );
  9154. if (ap)
  9155. clif_heal( *sd, SP_AP, ap );
  9156. } else {
  9157. if(hp)
  9158. clif_updatestatus(*sd,SP_HP);
  9159. if(sp)
  9160. clif_updatestatus(*sd,SP_SP);
  9161. if (ap)
  9162. clif_updatestatus(*sd,SP_AP);
  9163. }
  9164. return;
  9165. }
  9166. /**
  9167. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  9168. * @param sd: Player data
  9169. * @param itemid: Item ID
  9170. * @param hp: HP to heal
  9171. * @param sp: SP to heal
  9172. * @return Amount healed to an object
  9173. */
  9174. int32 pc_itemheal(map_session_data *sd, t_itemid itemid, int32 hp, int32 sp)
  9175. {
  9176. int32 bonus, tmp, penalty = 0;
  9177. if (hp) {
  9178. bonus = 100 + (sd->battle_status.vit * 2) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9179. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9180. if (potion_flag == 2) {
  9181. bonus += bonus * 50 / 100;
  9182. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_ROGUE)
  9183. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  9184. }
  9185. //All item bonuses.
  9186. bonus += sd->bonus.itemhealrate2;
  9187. //Item Group bonuses
  9188. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  9189. //Individual item bonuses.
  9190. for(const auto &it : sd->itemhealrate) {
  9191. if (it.id == itemid) {
  9192. bonus += bonus * it.val / 100;
  9193. break;
  9194. }
  9195. }
  9196. // Recovery Potion
  9197. if (sd->sc.getSCE(SC_INCHEALRATE))
  9198. bonus += bonus * sd->sc.getSCE(SC_INCHEALRATE)->val1 / 100;
  9199. tmp = hp * bonus / 100; // Overflow check
  9200. if (bonus != 100 && tmp > hp)
  9201. hp = tmp;
  9202. }
  9203. if (sp) {
  9204. bonus = 100 + (sd->battle_status.int_ * 2) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9205. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9206. if (potion_flag == 2)
  9207. bonus += bonus * 50 / 100;
  9208. // All item bonuses.
  9209. bonus += sd->bonus.itemsphealrate2;
  9210. // Item Group bonuses
  9211. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  9212. // Individual item bonuses.
  9213. for( const auto &it : sd->itemsphealrate ){
  9214. if( it.id == itemid ){
  9215. bonus += bonus * it.val / 100;
  9216. break;
  9217. }
  9218. }
  9219. tmp = sp * bonus / 100; // Overflow check
  9220. if (bonus != 100 && tmp > sp)
  9221. sp = tmp;
  9222. }
  9223. if (!sd->sc.empty()) {
  9224. // Critical Wound and Death Hurt stack
  9225. if (sd->sc.getSCE(SC_CRITICALWOUND))
  9226. penalty += sd->sc.getSCE(SC_CRITICALWOUND)->val2;
  9227. if (sd->sc.getSCE(SC_DEATHHURT) && sd->sc.getSCE(SC_DEATHHURT)->val3 == 1)
  9228. penalty += 20;
  9229. if (sd->sc.getSCE(SC_NORECOVER_STATE))
  9230. penalty = 100;
  9231. if (sd->sc.getSCE(SC_VITALITYACTIVATION))
  9232. hp += hp / 2; // 1.5 times
  9233. if (sd->sc.getSCE(SC_WATER_INSIGNIA) && sd->sc.getSCE(SC_WATER_INSIGNIA)->val1 == 2) {
  9234. hp += hp / 10;
  9235. sp += sp / 10;
  9236. }
  9237. #ifdef RENEWAL
  9238. if (sd->sc.getSCE(SC_APPLEIDUN))
  9239. hp += sd->sc.getSCE(SC_APPLEIDUN)->val3 / 100;
  9240. #endif
  9241. if (penalty > 0) {
  9242. hp -= hp * penalty / 100;
  9243. sp -= sp * penalty / 100;
  9244. }
  9245. #ifdef RENEWAL
  9246. if (sd->sc.getSCE(SC_EXTREMITYFIST))
  9247. sp = 0;
  9248. #endif
  9249. if (sd->sc.getSCE(SC_BITESCAR))
  9250. hp = 0;
  9251. }
  9252. return status_heal(&sd->bl, hp, sp, 1);
  9253. }
  9254. /*==========================================
  9255. * HP/SP Recovery
  9256. * Heal player hp nad/or sp by rate
  9257. *------------------------------------------*/
  9258. int32 pc_percentheal(map_session_data *sd,int32 hp,int32 sp)
  9259. {
  9260. nullpo_ret(sd);
  9261. if (hp > 100) hp = 100;
  9262. else if (hp <-100) hp = -100;
  9263. if (sp > 100) sp = 100;
  9264. else if (sp <-100) sp = -100;
  9265. if(hp >= 0 && sp >= 0) //Heal
  9266. return status_percent_heal(&sd->bl, hp, sp);
  9267. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  9268. return status_percent_damage(nullptr, &sd->bl, hp, sp, hp==-100);
  9269. //Crossed signs
  9270. if(hp) {
  9271. if(hp > 0)
  9272. status_percent_heal(&sd->bl, hp, 0);
  9273. else
  9274. status_percent_damage(nullptr, &sd->bl, hp, 0, hp==-100);
  9275. }
  9276. if(sp) {
  9277. if(sp > 0)
  9278. status_percent_heal(&sd->bl, 0, sp);
  9279. else
  9280. status_percent_damage(nullptr, &sd->bl, 0, sp, false);
  9281. }
  9282. return 0;
  9283. }
  9284. static int32 jobchange_killclone(struct block_list *bl, va_list ap)
  9285. {
  9286. struct mob_data *md;
  9287. int32 flag;
  9288. md = (struct mob_data *)bl;
  9289. nullpo_ret(md);
  9290. flag = va_arg(ap, int32);
  9291. if (md->master_id && md->special_state.clone && md->master_id == flag)
  9292. status_kill(&md->bl);
  9293. return 1;
  9294. }
  9295. /**
  9296. * Called when player changes job
  9297. * Rewrote to make it tidider [Celest]
  9298. * @param sd
  9299. * @param job JOB ID. See enum e_job
  9300. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  9301. * @return True if success, false if failed
  9302. **/
  9303. bool pc_jobchange(map_session_data *sd,int32 job, char upper)
  9304. {
  9305. int32 i, fame_flag = 0;
  9306. nullpo_retr(false,sd);
  9307. if (job < 0)
  9308. return false;
  9309. //Normalize job.
  9310. uint64 b_class = pc_jobid2mapid(job);
  9311. if (b_class == -1)
  9312. return false;
  9313. switch (upper) {
  9314. case 1:
  9315. b_class|= JOBL_UPPER;
  9316. break;
  9317. case 2:
  9318. b_class|= JOBL_BABY;
  9319. break;
  9320. }
  9321. //This will automatically adjust bard/dancer classes to the correct gender
  9322. //That is, if you try to jobchange into dancer, it will turn you to bard.
  9323. job = pc_mapid2jobid(b_class, sd->status.sex);
  9324. if (job == -1)
  9325. return false;
  9326. if ((uint16)b_class == sd->class_)
  9327. return false; //Nothing to change.
  9328. // If the job does not exist in the job db, dont allow changing to it
  9329. if( !job_db.exists( job ) ){
  9330. return false;
  9331. }
  9332. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  9333. // Changing to 4th job
  9334. sd->change_level_4th = sd->status.job_level;
  9335. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  9336. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  9337. // changing from 2nd to 3rd job
  9338. sd->change_level_3rd = sd->status.job_level;
  9339. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  9340. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  9341. // changing from 1st to 2nd job
  9342. sd->change_level_2nd = sd->status.job_level;
  9343. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  9344. }
  9345. if(sd->cloneskill_idx > 0) {
  9346. pc_skill_plagiarism_reset(*sd, 1);
  9347. }
  9348. if(sd->reproduceskill_idx > 0) {
  9349. pc_skill_plagiarism_reset(*sd, 2);
  9350. }
  9351. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  9352. status_db.changeSkillTree(sd);
  9353. }
  9354. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  9355. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  9356. pc_resetfeel(sd);
  9357. pc_resethate(sd);
  9358. }
  9359. // Reset body style to 0 before changing job to avoid
  9360. // errors since not every job has a alternate outfit.
  9361. sd->status.body = 0;
  9362. clif_changelook(&sd->bl,LOOK_BODY2,0);
  9363. sd->status.class_ = job;
  9364. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  9365. uint64 previous_class = sd->class_;
  9366. sd->class_ = (uint16)b_class;
  9367. sd->status.job_level=1;
  9368. sd->status.job_exp=0;
  9369. if (sd->status.base_level > pc_maxbaselv(sd)) {
  9370. sd->status.base_level = pc_maxbaselv(sd);
  9371. sd->status.base_exp=0;
  9372. pc_resetstate(sd);
  9373. clif_updatestatus(*sd,SP_STATUSPOINT);
  9374. clif_updatestatus(*sd,SP_TRAITPOINT);
  9375. clif_updatestatus(*sd,SP_BASELEVEL);
  9376. clif_updatestatus(*sd,SP_BASEEXP);
  9377. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  9378. }
  9379. // Give or reduce transcendent status points
  9380. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  9381. sd->status.status_point += battle_config.transcendent_status_points;
  9382. clif_updatestatus(*sd,SP_STATUSPOINT);
  9383. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  9384. if( sd->status.status_point < battle_config.transcendent_status_points ){
  9385. // The player already used his bonus points, so we have to reset his status points
  9386. pc_resetstate(sd);
  9387. }else{
  9388. sd->status.status_point -= battle_config.transcendent_status_points;
  9389. clif_updatestatus(*sd,SP_STATUSPOINT);
  9390. }
  9391. }
  9392. // Give or reduce trait status points
  9393. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  9394. sd->status.trait_point += battle_config.trait_points_job_change;
  9395. clif_updatestatus(*sd, SP_TRAITPOINT);
  9396. clif_updatestatus(*sd, SP_UPOW);
  9397. clif_updatestatus(*sd, SP_USTA);
  9398. clif_updatestatus(*sd, SP_UWIS);
  9399. clif_updatestatus(*sd, SP_USPL);
  9400. clif_updatestatus(*sd, SP_UCON);
  9401. clif_updatestatus(*sd, SP_UCRT);
  9402. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  9403. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  9404. // Player may have already used the trait status points. Force a reset.
  9405. pc_resetstate(sd);
  9406. } else {
  9407. sd->status.trait_point = 0;
  9408. clif_updatestatus(*sd, SP_TRAITPOINT);
  9409. clif_updatestatus(*sd, SP_UPOW);
  9410. clif_updatestatus(*sd, SP_USTA);
  9411. clif_updatestatus(*sd, SP_UWIS);
  9412. clif_updatestatus(*sd, SP_USPL);
  9413. clif_updatestatus(*sd, SP_UCON);
  9414. clif_updatestatus(*sd, SP_UCRT);
  9415. }
  9416. }
  9417. clif_updatestatus(*sd,SP_JOBLEVEL);
  9418. clif_updatestatus(*sd,SP_JOBEXP);
  9419. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  9420. for(i=0;i<EQI_MAX;i++) {
  9421. if(sd->equip_index[i] >= 0)
  9422. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  9423. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  9424. }
  9425. }
  9426. //Change look, if disguised, you need to undisguise
  9427. //to correctly calculate new job sprite without
  9428. if (sd->disguise)
  9429. pc_disguise(sd, 0);
  9430. status_set_viewdata(&sd->bl, job);
  9431. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  9432. #if PACKETVER >= 20151001
  9433. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  9434. #endif
  9435. if(sd->vd.cloth_color)
  9436. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9437. /*
  9438. if(sd->vd.body_style)
  9439. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9440. */
  9441. //Update skill tree.
  9442. pc_calc_skilltree(sd);
  9443. clif_skillinfoblock(sd);
  9444. if (sd->ed)
  9445. elemental_delete(sd->ed);
  9446. if (sd->state.vending)
  9447. vending_closevending(sd);
  9448. if (sd->state.buyingstore)
  9449. buyingstore_close(sd);
  9450. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  9451. //Remove peco/cart/falcon
  9452. i = sd->sc.option;
  9453. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  9454. i&=~OPTION_RIDING;
  9455. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9456. i&=~OPTION_FALCON;
  9457. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9458. i&=~OPTION_DRAGON;
  9459. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9460. i&=~OPTION_WUGRIDER;
  9461. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9462. i&=~OPTION_WUG;
  9463. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9464. i&=~OPTION_MADOGEAR;
  9465. #ifndef NEW_CARTS
  9466. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9467. i&=~OPTION_CART;
  9468. #else
  9469. if( sd->sc.getSCE(SC_PUSH_CART) && !pc_checkskill(sd, MC_PUSHCART) )
  9470. pc_setcart(sd, 0);
  9471. #endif
  9472. if(i != sd->sc.option)
  9473. pc_setoption(sd, i);
  9474. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9475. hom_vaporize(sd, HOM_ST_ACTIVE);
  9476. if (sd->sc.getSCE(SC_SPRITEMABLE) && !pc_checkskill(sd, SU_SPRITEMABLE))
  9477. status_change_end(&sd->bl, SC_SPRITEMABLE);
  9478. if (sd->sc.getSCE(SC_SOULATTACK) && !pc_checkskill(sd, SU_SOULATTACK))
  9479. status_change_end(&sd->bl, SC_SOULATTACK);
  9480. if( sd->sc.getSCE(SC_SPIRIT) ){
  9481. status_change_end( &sd->bl, SC_SPIRIT );
  9482. }
  9483. if(sd->status.manner < 0){
  9484. clif_changemanner( *sd );
  9485. clif_updatestatus(*sd,SP_MANNER);
  9486. }
  9487. status_calc_pc(sd,SCO_FORCE);
  9488. pc_checkallowskill(sd);
  9489. pc_equiplookall(sd);
  9490. pc_show_questinfo(sd);
  9491. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9492. if( sd->status.party_id ){
  9493. struct party_data* p;
  9494. if( ( p = party_search( sd->status.party_id ) ) != nullptr ){
  9495. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9496. if( i < MAX_PARTY ){
  9497. p->party.member[i].class_ = sd->status.class_;
  9498. clif_party_job_and_level( *sd );
  9499. }
  9500. }
  9501. }
  9502. chrif_save(sd, CSAVE_NORMAL);
  9503. //if you were previously famous, not anymore.
  9504. if (fame_flag)
  9505. chrif_buildfamelist();
  9506. else if (sd->status.fame > 0) {
  9507. //It may be that now they are famous?
  9508. switch (sd->class_&MAPID_UPPERMASK) {
  9509. case MAPID_BLACKSMITH:
  9510. case MAPID_ALCHEMIST:
  9511. case MAPID_TAEKWON:
  9512. chrif_buildfamelist();
  9513. break;
  9514. }
  9515. }
  9516. return true;
  9517. }
  9518. /*==========================================
  9519. * Tell client player sd has change equipement
  9520. *------------------------------------------*/
  9521. void pc_equiplookall(map_session_data *sd)
  9522. {
  9523. nullpo_retv(sd);
  9524. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9525. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9526. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9527. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9528. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9529. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9530. }
  9531. /*==========================================
  9532. * Tell client player sd has change look (hair,equip...)
  9533. *------------------------------------------*/
  9534. void pc_changelook(map_session_data *sd,int32 type,int32 val) {
  9535. nullpo_retv(sd);
  9536. switch(type) {
  9537. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9538. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9539. if (sd->status.hair != val) {
  9540. sd->status.hair = val;
  9541. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9542. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9543. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9544. }
  9545. break;
  9546. case LOOK_WEAPON:
  9547. sd->status.weapon = val;
  9548. break;
  9549. case LOOK_HEAD_BOTTOM:
  9550. sd->status.head_bottom = val;
  9551. sd->setlook_head_bottom = val;
  9552. break;
  9553. case LOOK_HEAD_TOP:
  9554. sd->status.head_top = val;
  9555. sd->setlook_head_top = val;
  9556. break;
  9557. case LOOK_HEAD_MID:
  9558. sd->status.head_mid = val;
  9559. sd->setlook_head_mid = val;
  9560. break;
  9561. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9562. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9563. if (sd->status.hair_color != val) {
  9564. sd->status.hair_color = val;
  9565. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9566. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9567. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9568. }
  9569. break;
  9570. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9571. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9572. sd->status.clothes_color = val;
  9573. break;
  9574. case LOOK_SHIELD:
  9575. sd->status.shield = val;
  9576. break;
  9577. case LOOK_SHOES:
  9578. break;
  9579. case LOOK_ROBE:
  9580. sd->status.robe = val;
  9581. sd->setlook_robe = val;
  9582. break;
  9583. case LOOK_BODY2:
  9584. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9585. sd->status.body = val;
  9586. break;
  9587. }
  9588. clif_changelook(&sd->bl, type, val);
  9589. }
  9590. /*==========================================
  9591. * Give an option (type) to player (sd) and display it to client
  9592. *------------------------------------------*/
  9593. void pc_setoption(map_session_data *sd,int32 type, int32 subtype)
  9594. {
  9595. int32 p_type, new_look=0;
  9596. nullpo_retv(sd);
  9597. p_type = sd->sc.option;
  9598. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9599. sd->sc.option=type;
  9600. clif_changeoption(&sd->bl);
  9601. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9602. { // Mounting
  9603. clif_status_load(&sd->bl,EFST_RIDING,1);
  9604. status_calc_pc(sd,SCO_NONE);
  9605. }
  9606. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9607. { // Dismount
  9608. clif_status_load(&sd->bl,EFST_RIDING,0);
  9609. status_calc_pc(sd,SCO_NONE);
  9610. }
  9611. #ifndef NEW_CARTS
  9612. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9613. clif_cartlist(sd);
  9614. clif_updatestatus(*sd, SP_CARTINFO);
  9615. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9616. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9617. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9618. clif_clearcart(sd->fd);
  9619. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9620. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9621. }
  9622. #endif
  9623. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9624. clif_status_load(&sd->bl,EFST_FALCON,1);
  9625. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9626. clif_status_load(&sd->bl,EFST_FALCON,0);
  9627. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9628. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9629. status_calc_pc(sd,SCO_NONE);
  9630. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9631. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9632. status_calc_pc(sd,SCO_NONE);
  9633. }
  9634. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9635. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9636. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9637. status_change_end(&sd->bl, SC_MADOGEAR);
  9638. }
  9639. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9640. new_look = JOB_STAR_GLADIATOR2;
  9641. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9642. new_look = -1;
  9643. if (sd->disguise || !new_look)
  9644. return; //Disguises break sprite changes
  9645. if (new_look < 0) { //Restore normal look.
  9646. status_set_viewdata(&sd->bl, sd->status.class_);
  9647. new_look = sd->vd.class_;
  9648. }
  9649. // Stop attacking on new view change (to prevent wedding/santa attacks).
  9650. unit_stop_attack( &sd->bl );
  9651. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9652. if (sd->vd.cloth_color)
  9653. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9654. if( sd->vd.body_style )
  9655. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9656. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9657. }
  9658. /**
  9659. * Give player a cart
  9660. * @param sd Player
  9661. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9662. **/
  9663. bool pc_setcart(map_session_data *sd,int32 type) {
  9664. #ifndef NEW_CARTS
  9665. int32 cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9666. int32 option;
  9667. #endif
  9668. nullpo_retr(false,sd);
  9669. if( type < 0 || type > MAX_CARTS )
  9670. return false;// Never trust the values sent by the client! [Skotlex]
  9671. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9672. return false;// Push cart is required
  9673. #ifdef NEW_CARTS
  9674. switch( type ) {
  9675. case 0:
  9676. if( !sd->sc.getSCE(SC_PUSH_CART) )
  9677. return 0;
  9678. status_change_end(&sd->bl,SC_PUSH_CART);
  9679. clif_clearcart(sd->fd);
  9680. break;
  9681. default:/* everything else is an allowed ID so we can move on */
  9682. if( !sd->sc.getSCE(SC_PUSH_CART) ) { /* first time, so fill cart data */
  9683. clif_cartlist(sd);
  9684. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9685. }
  9686. clif_updatestatus(*sd, SP_CARTINFO);
  9687. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9688. break;
  9689. }
  9690. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9691. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9692. #else
  9693. // Update option
  9694. option = sd->sc.option;
  9695. option &= ~OPTION_CART;// clear cart bits
  9696. option |= cart[type]; // set cart
  9697. pc_setoption(sd, option);
  9698. #endif
  9699. return true;
  9700. }
  9701. /*==========================================
  9702. * Give player a falcon
  9703. *------------------------------------------*/
  9704. void pc_setfalcon(map_session_data* sd, int32 flag)
  9705. {
  9706. if( flag ){
  9707. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9708. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9709. } else if( pc_isfalcon(sd) ){
  9710. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9711. }
  9712. }
  9713. /*==========================================
  9714. * Set player riding
  9715. *------------------------------------------*/
  9716. void pc_setriding(map_session_data* sd, int32 flag)
  9717. {
  9718. if( sd->sc.getSCE(SC_ALL_RIDING) )
  9719. return;
  9720. if( flag ){
  9721. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9722. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9723. } else if( pc_isriding(sd) ){
  9724. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9725. }
  9726. }
  9727. /**
  9728. * Give player a mado
  9729. * @param sd: Player
  9730. * @param flag: Enable or disable mado
  9731. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9732. */
  9733. void pc_setmadogear(map_session_data *sd, bool flag, e_mado_type type)
  9734. {
  9735. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9736. return;
  9737. if (flag) {
  9738. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9739. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9740. } else if (pc_ismadogear(sd)) {
  9741. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9742. }
  9743. }
  9744. /*==========================================
  9745. * Check if player can drop an item
  9746. *------------------------------------------*/
  9747. bool pc_candrop(map_session_data *sd, struct item *item)
  9748. {
  9749. if (sd->sc.cant.drop)
  9750. return false;
  9751. if( item && itemdb_ishatched_egg(item) )
  9752. return false;
  9753. if (pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL)) // no restriction
  9754. return true;
  9755. if( !pc_can_give_items(sd) )
  9756. return false;
  9757. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  9758. return false;
  9759. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9760. }
  9761. /*==========================================
  9762. * Read '@type' variables (temporary numeric char reg)
  9763. *------------------------------------------*/
  9764. int64 pc_readreg(map_session_data* sd, int64 reg)
  9765. {
  9766. return i64db_i64get(sd->regs.vars, reg);
  9767. }
  9768. /*==========================================
  9769. * Set '@type' variables (temporary numeric char reg)
  9770. *------------------------------------------*/
  9771. bool pc_setreg(map_session_data* sd, int64 reg, int64 val)
  9772. {
  9773. uint32 index = script_getvaridx(reg);
  9774. nullpo_retr(false, sd);
  9775. if( val ) {
  9776. i64db_i64put(sd->regs.vars, reg, val);
  9777. if( index )
  9778. script_array_update(&sd->regs, reg, false);
  9779. } else {
  9780. i64db_remove(sd->regs.vars, reg);
  9781. if( index )
  9782. script_array_update(&sd->regs, reg, true);
  9783. }
  9784. return true;
  9785. }
  9786. /*==========================================
  9787. * Read '@type$' variables (temporary string char reg)
  9788. *------------------------------------------*/
  9789. char* pc_readregstr(map_session_data* sd, int64 reg)
  9790. {
  9791. struct script_reg_str *p = nullptr;
  9792. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9793. return p ? p->value : nullptr;
  9794. }
  9795. /*==========================================
  9796. * Set '@type$' variables (temporary string char reg)
  9797. *------------------------------------------*/
  9798. bool pc_setregstr(map_session_data* sd, int64 reg, const char* str)
  9799. {
  9800. struct script_reg_str *p = nullptr;
  9801. uint32 index = script_getvaridx(reg);
  9802. DBData prev;
  9803. nullpo_retr(false, sd);
  9804. if( str[0] ) {
  9805. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9806. p->value = aStrdup(str);
  9807. p->flag.type = 1;
  9808. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9809. p = (struct script_reg_str *)db_data2ptr(&prev);
  9810. if( p->value )
  9811. aFree(p->value);
  9812. ers_free(str_reg_ers, p);
  9813. } else {
  9814. if( index )
  9815. script_array_update(&sd->regs, reg, false);
  9816. }
  9817. } else {
  9818. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9819. p = (struct script_reg_str *)db_data2ptr(&prev);
  9820. if( p->value )
  9821. aFree(p->value);
  9822. ers_free(str_reg_ers, p);
  9823. if( index )
  9824. script_array_update(&sd->regs, reg, true);
  9825. }
  9826. }
  9827. return true;
  9828. }
  9829. /**
  9830. * Serves the following variable types:
  9831. * - 'type' (permanent numeric char reg)
  9832. * - '#type' (permanent numeric account reg)
  9833. * - '##type' (permanent numeric account reg2)
  9834. **/
  9835. int64 pc_readregistry(map_session_data *sd, int64 reg)
  9836. {
  9837. struct script_reg_num *p = nullptr;
  9838. if (!sd->vars_ok) {
  9839. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9840. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9841. //intif->request_registry(sd,type==3?4:type);
  9842. set_eof(sd->fd);
  9843. return 0;
  9844. }
  9845. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9846. return p ? p->value : 0;
  9847. }
  9848. /**
  9849. * Serves the following variable types:
  9850. * - 'type$' (permanent str char reg)
  9851. * - '#type$' (permanent str account reg)
  9852. * - '##type$' (permanent str account reg2)
  9853. **/
  9854. char* pc_readregistry_str(map_session_data *sd, int64 reg)
  9855. {
  9856. struct script_reg_str *p = nullptr;
  9857. if (!sd->vars_ok) {
  9858. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9859. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9860. //intif->request_registry(sd,type==3?4:type);
  9861. set_eof(sd->fd);
  9862. return nullptr;
  9863. }
  9864. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9865. return p ? p->value : nullptr;
  9866. }
  9867. /**
  9868. * Serves the following variable types:
  9869. * - 'type' (permanent numeric char reg)
  9870. * - '#type' (permanent numeric account reg)
  9871. * - '##type' (permanent numeric account reg2)
  9872. **/
  9873. bool pc_setregistry(map_session_data *sd, int64 reg, int64 val)
  9874. {
  9875. struct script_reg_num *p = nullptr;
  9876. const char *regname = get_str(script_getvarid(reg));
  9877. uint32 index = script_getvaridx(reg);
  9878. if ( !reg_load && !sd->vars_ok ) {
  9879. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9880. return false;
  9881. }
  9882. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9883. if( val ) {
  9884. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9885. script_array_update(&sd->regs, reg, false);
  9886. p->value = val;
  9887. } else {
  9888. p->value = 0;
  9889. if( index )
  9890. script_array_update(&sd->regs, reg, true);
  9891. }
  9892. if (!reg_load)
  9893. p->flag.update = 1;/* either way, it will require either delete or replace */
  9894. } else if( val ) {
  9895. DBData prev;
  9896. if( index )
  9897. script_array_update(&sd->regs, reg, false);
  9898. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9899. p->value = val;
  9900. if (!reg_load)
  9901. p->flag.update = 1;
  9902. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9903. p = (struct script_reg_num *)db_data2ptr(&prev);
  9904. ers_free(num_reg_ers, p);
  9905. }
  9906. }
  9907. if (!reg_load && p)
  9908. sd->vars_dirty = true;
  9909. return true;
  9910. }
  9911. /**
  9912. * Serves the following variable types:
  9913. * - 'type$' (permanent str char reg)
  9914. * - '#type$' (permanent str account reg)
  9915. * - '##type$' (permanent str account reg2)
  9916. **/
  9917. bool pc_setregistry_str(map_session_data *sd, int64 reg, const char *val)
  9918. {
  9919. struct script_reg_str *p = nullptr;
  9920. const char *regname = get_str(script_getvarid(reg));
  9921. uint32 index = script_getvaridx(reg);
  9922. size_t vlen = 0;
  9923. if (!reg_load && !sd->vars_ok) {
  9924. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9925. return false;
  9926. }
  9927. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9928. {
  9929. 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);
  9930. return false;
  9931. }
  9932. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9933. if( val[0] ) {
  9934. if( p->value )
  9935. aFree(p->value);
  9936. else if ( index ) // an entry that was deleted, so we reset
  9937. script_array_update(&sd->regs, reg, false);
  9938. p->value = aStrdup(val);
  9939. } else {
  9940. if (p->value)
  9941. aFree(p->value);
  9942. p->value = nullptr;
  9943. if( index )
  9944. script_array_update(&sd->regs, reg, true);
  9945. }
  9946. if( !reg_load )
  9947. p->flag.update = 1; // either way, it will require either delete or replace
  9948. } else if( val[0] ) {
  9949. DBData prev;
  9950. if( index )
  9951. script_array_update(&sd->regs, reg, false);
  9952. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9953. p->value = aStrdup(val);
  9954. if( !reg_load )
  9955. p->flag.update = 1;
  9956. p->flag.type = 1;
  9957. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9958. p = (struct script_reg_str *)db_data2ptr(&prev);
  9959. if( p->value )
  9960. aFree(p->value);
  9961. ers_free(str_reg_ers, p);
  9962. }
  9963. }
  9964. if( !reg_load && p )
  9965. sd->vars_dirty = true;
  9966. return true;
  9967. }
  9968. /**
  9969. * Set value of player variable
  9970. * @param sd Player
  9971. * @param reg Variable name
  9972. * @param value
  9973. * @return True if success, false if failed.
  9974. **/
  9975. bool pc_setreg2(map_session_data *sd, const char *reg, int64 val) {
  9976. char prefix = reg[0];
  9977. nullpo_retr(false, sd);
  9978. if( is_string_variable( reg ) ){
  9979. ShowError( "pc_setreg2: Invalid variable '%s'. String type variables are not supported.\n", reg );
  9980. return false;
  9981. }
  9982. if( !not_server_variable( prefix ) ){
  9983. ShowError( "pc_setreg2: Invalid variable scope '%s'.\n", reg );
  9984. return false;
  9985. }
  9986. val = cap_value(val, INT_MIN, INT_MAX);
  9987. switch (prefix) {
  9988. case '@':
  9989. return pc_setreg(sd, add_str(reg), val);
  9990. case '#':
  9991. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9992. default:
  9993. return pc_setglobalreg(sd, add_str(reg), val);
  9994. }
  9995. }
  9996. /**
  9997. * Get value of player variable
  9998. * @param sd Player
  9999. * @param reg Variable name
  10000. * @return Variable value or 0 if failed.
  10001. **/
  10002. int64 pc_readreg2(map_session_data *sd, const char *reg) {
  10003. char prefix = reg[0];
  10004. nullpo_ret(sd);
  10005. if( is_string_variable( reg ) ){
  10006. ShowError( "pc_readreg2: Invalid variable '%s'. String type variables are not supported.\n", reg );
  10007. return 0;
  10008. }
  10009. if( !not_server_variable( prefix ) ){
  10010. ShowError( "pc_readreg2: Invalid variable scope '%s'.\n", reg );
  10011. return 0;
  10012. }
  10013. switch (prefix) {
  10014. case '@':
  10015. return pc_readreg(sd, add_str(reg));
  10016. case '#':
  10017. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  10018. default:
  10019. return pc_readglobalreg(sd, add_str(reg));
  10020. }
  10021. }
  10022. /*==========================================
  10023. * Exec eventtimer for player sd (retrieved from map_session (id))
  10024. *------------------------------------------*/
  10025. static TIMER_FUNC(pc_eventtimer){
  10026. map_session_data *sd=map_id2sd(id);
  10027. char *p = (char *)data;
  10028. int32 i;
  10029. if(sd==nullptr)
  10030. return 0;
  10031. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  10032. if( i < MAX_EVENTTIMER )
  10033. {
  10034. sd->eventtimer[i] = INVALID_TIMER;
  10035. sd->eventcount--;
  10036. npc_event(sd,p,0);
  10037. }
  10038. else
  10039. ShowError("pc_eventtimer: no such event timer\n");
  10040. if (p) aFree(p);
  10041. return 0;
  10042. }
  10043. /*==========================================
  10044. * Add eventtimer for player sd ?
  10045. *------------------------------------------*/
  10046. bool pc_addeventtimer(map_session_data *sd,int32 tick,const char *name)
  10047. {
  10048. int32 i;
  10049. nullpo_retr(false,sd);
  10050. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  10051. if( i == MAX_EVENTTIMER )
  10052. return false;
  10053. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  10054. sd->eventcount++;
  10055. return true;
  10056. }
  10057. /*==========================================
  10058. * Del eventtimer for player sd ?
  10059. *------------------------------------------*/
  10060. bool pc_deleventtimer(map_session_data *sd,const char *name)
  10061. {
  10062. char* p = nullptr;
  10063. int32 i;
  10064. nullpo_retr(false,sd);
  10065. if (sd->eventcount == 0)
  10066. return false;
  10067. // find the named event timer
  10068. ARR_FIND( 0, MAX_EVENTTIMER, i,
  10069. sd->eventtimer[i] != INVALID_TIMER &&
  10070. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != nullptr &&
  10071. strcmp(p, name) == 0
  10072. );
  10073. if( i == MAX_EVENTTIMER )
  10074. return false; // not found
  10075. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10076. sd->eventtimer[i] = INVALID_TIMER;
  10077. if(sd->eventcount > 0)
  10078. sd->eventcount--;
  10079. aFree(p);
  10080. return true;
  10081. }
  10082. /*==========================================
  10083. * Update eventtimer count for player sd
  10084. *------------------------------------------*/
  10085. void pc_addeventtimercount(map_session_data *sd,const char *name,int32 tick)
  10086. {
  10087. int32 i;
  10088. nullpo_retv(sd);
  10089. for(i=0;i<MAX_EVENTTIMER;i++)
  10090. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  10091. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  10092. addtick_timer(sd->eventtimer[i],tick);
  10093. break;
  10094. }
  10095. }
  10096. /*==========================================
  10097. * Remove all eventtimer for player sd
  10098. *------------------------------------------*/
  10099. void pc_cleareventtimer(map_session_data *sd)
  10100. {
  10101. int32 i;
  10102. nullpo_retv(sd);
  10103. if (sd->eventcount == 0)
  10104. return;
  10105. for(i=0;i<MAX_EVENTTIMER;i++){
  10106. if( sd->eventtimer[i] != INVALID_TIMER ){
  10107. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  10108. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10109. sd->eventtimer[i] = INVALID_TIMER;
  10110. if(sd->eventcount > 0) //avoid looping to max val
  10111. sd->eventcount--;
  10112. if (p) aFree(p);
  10113. }
  10114. }
  10115. }
  10116. /**
  10117. * Called when an item with combo is worn
  10118. * @param sd: Player data
  10119. * @param data: Item data
  10120. * @return Number of succeeded combo(s)
  10121. */
  10122. static int32 pc_checkcombo(map_session_data *sd, item_data *data) {
  10123. int32 success = 0;
  10124. for (const auto &item_combo : data->combos) {
  10125. bool do_continue = false;
  10126. // Ensure this isn't a duplicate combo
  10127. for (const auto player_combo : sd->combos) {
  10128. if (player_combo->id == item_combo->id) {
  10129. do_continue = true;
  10130. break;
  10131. }
  10132. }
  10133. // Combo already equipped
  10134. if (do_continue)
  10135. continue;
  10136. size_t nb_itemCombo = item_combo->nameid.size();
  10137. if (nb_itemCombo < 2) // A combo with less then 2 item?
  10138. continue;
  10139. struct s_itemchk {
  10140. int32 idx;
  10141. t_itemid nameid, card[MAX_SLOTS];
  10142. s_itemchk() : idx(0), nameid(0), card() {};
  10143. };
  10144. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  10145. size_t j;
  10146. uint32 pos = 0;
  10147. for (j = 0; j < nb_itemCombo; j++) {
  10148. t_itemid id = item_combo->nameid[j];
  10149. bool found = false;
  10150. for (int16 k = 0; k < EQI_MAX; k++) {
  10151. int16 index = sd->equip_index[k];
  10152. if (index < 0)
  10153. continue;
  10154. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  10155. continue;
  10156. if (!sd->inventory_data[index])
  10157. continue;
  10158. if (itemdb_type(id) != IT_CARD) {
  10159. if (sd->inventory_data[index]->nameid != id)
  10160. continue;
  10161. if (j > 0) { // Check if this item not already used
  10162. do_continue = false;
  10163. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  10164. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  10165. do_continue = true;
  10166. break;
  10167. }
  10168. }
  10169. if (do_continue)
  10170. continue;
  10171. }
  10172. combo_idx[j].nameid = id;
  10173. combo_idx[j].idx = index;
  10174. pos |= sd->inventory.u.items_inventory[index].equip;
  10175. found = true;
  10176. break;
  10177. } else { // Cards and enchants
  10178. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  10179. continue;
  10180. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  10181. do_continue = false;
  10182. if (sd->inventory.u.items_inventory[index].card[z] != id)
  10183. continue;
  10184. if (j > 0) {
  10185. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  10186. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  10187. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  10188. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  10189. do_continue = true;
  10190. break;
  10191. }
  10192. }
  10193. }
  10194. }
  10195. }
  10196. if (do_continue)
  10197. continue;
  10198. combo_idx[j].nameid = id;
  10199. combo_idx[j].idx = index;
  10200. combo_idx[j].card[z] = id;
  10201. pos |= sd->inventory.u.items_inventory[index].equip;
  10202. found = true;
  10203. break;
  10204. }
  10205. }
  10206. }
  10207. if (!found)
  10208. break; // Unable to found all the IDs for this combo, return
  10209. }
  10210. // Broke out of the count loop without finding all IDs, move to the next combo
  10211. if (j < nb_itemCombo)
  10212. continue;
  10213. // All items in the combo are matching
  10214. auto entry = std::make_shared<s_combos>();
  10215. entry->bonus = item_combo->script;
  10216. entry->id = item_combo->id;
  10217. entry->pos = pos;
  10218. sd->combos.push_back(entry);
  10219. combo_idx.clear();
  10220. success++;
  10221. }
  10222. return success;
  10223. }
  10224. /**
  10225. * Called when an item with combo is removed
  10226. * @param sd: Player data
  10227. * @param data: Item data
  10228. * @return Number of removed combo(s)
  10229. */
  10230. static int32 pc_removecombo(map_session_data *sd, item_data *data ) {
  10231. if (sd->combos.empty())
  10232. return 0; // Nothing to do here, player has no combos
  10233. int32 retval = 0;
  10234. for (const auto &item_combo : data->combos) {
  10235. std::shared_ptr<s_combos> del_combo = nullptr;
  10236. // Check if this combo exists on this player
  10237. for (const auto &player_combo : sd->combos) {
  10238. if (player_combo->id == item_combo->id) {
  10239. del_combo = player_combo;
  10240. break;
  10241. }
  10242. }
  10243. // No match, skip this combo
  10244. if (del_combo == nullptr)
  10245. continue;
  10246. util::vector_erase_if_exists(sd->combos, del_combo);
  10247. retval++;
  10248. // Check if combo requirements still fit
  10249. if (pc_checkcombo(sd, data))
  10250. continue;
  10251. // It's empty, clear all the memory
  10252. if (sd->combos.empty()) {
  10253. sd->combos.clear();
  10254. return retval; // Return at this point as there are no more combos to check
  10255. }
  10256. }
  10257. return retval;
  10258. }
  10259. /**
  10260. * Load combo data(s) of player
  10261. * @param sd: Player data
  10262. * @return ret numbers of succeed combo
  10263. */
  10264. int32 pc_load_combo(map_session_data *sd) {
  10265. int32 ret = 0;
  10266. for (int16 i = 0; i < EQI_MAX; i++) {
  10267. item_data *id;
  10268. int16 idx = sd->equip_index[i];
  10269. if (idx < 0 || !(id = sd->inventory_data[idx]))
  10270. continue;
  10271. if (!id->combos.empty())
  10272. ret += pc_checkcombo(sd, id);
  10273. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  10274. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  10275. if (!sd->inventory.u.items_inventory[idx].card[j])
  10276. continue;
  10277. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[idx].card[j]);
  10278. if (data != nullptr) {
  10279. if (!data->combos.empty())
  10280. ret += pc_checkcombo(sd, data.get());
  10281. }
  10282. }
  10283. }
  10284. }
  10285. return ret;
  10286. }
  10287. /*==========================================
  10288. * Equip item on player sd at req_pos from inventory index n
  10289. * return: false - fail; true - success
  10290. *------------------------------------------*/
  10291. bool pc_equipitem(map_session_data *sd,int16 n,int32 req_pos,bool equipswitch)
  10292. {
  10293. int32 i, pos, flag = 0, iflag;
  10294. struct item_data *id;
  10295. int16* equip_index;
  10296. nullpo_retr(false,sd);
  10297. if( n < 0 || n >= MAX_INVENTORY ) {
  10298. if( equipswitch ){
  10299. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10300. }else{
  10301. // Does not deserve an answer... [Lemongrass]
  10302. //clif_equipitemack( sd, ITEM_EQUIP_ACK_FAIL, n );
  10303. }
  10304. return false;
  10305. }
  10306. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  10307. if( equipswitch ){
  10308. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10309. }else{
  10310. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n );
  10311. }
  10312. return false;
  10313. }
  10314. if (!(id = sd->inventory_data[n]))
  10315. return false;
  10316. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  10317. if(battle_config.battle_log && !equipswitch)
  10318. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  10319. uint8 res = pc_isequip( sd, n );
  10320. if( res != ITEM_EQUIP_ACK_OK ){
  10321. if( equipswitch ){
  10322. clif_equipswitch_add( sd, n, req_pos, res );
  10323. }else{
  10324. clif_equipitemack( *sd, res, n ); // fail
  10325. }
  10326. return false;
  10327. }
  10328. if( equipswitch && id->type == IT_AMMO ){
  10329. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10330. return false;
  10331. }
  10332. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0) {
  10333. if( equipswitch ){
  10334. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10335. }else{
  10336. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); // fail
  10337. }
  10338. return false;
  10339. }
  10340. if( !sd->sc.empty() && (sd->sc.cant.equip || (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) {
  10341. if( equipswitch ){
  10342. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10343. }else{
  10344. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); //Fail
  10345. }
  10346. return false;
  10347. }
  10348. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  10349. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  10350. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  10351. clif_notify_bindOnEquip( *sd, n );
  10352. }
  10353. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  10354. pos = req_pos&EQP_ACC;
  10355. if (pos == EQP_ACC) //User specified both slots.
  10356. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  10357. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  10358. if (!sd->inventory.u.items_inventory[n].card[i])
  10359. continue;
  10360. std::shared_ptr<item_data> card_data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10361. if (card_data) {
  10362. int32 card_pos = card_data->equip;
  10363. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  10364. pos = card_pos; // Use the card's equip position
  10365. break;
  10366. }
  10367. }
  10368. }
  10369. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  10370. pos = (req_pos&EQP_ARMS);
  10371. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  10372. #ifdef RENEWAL
  10373. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  10374. #else
  10375. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  10376. #endif
  10377. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  10378. pos = req_pos&EQP_SHADOW_ACC;
  10379. if (pos == EQP_SHADOW_ACC)
  10380. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  10381. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  10382. pos = (req_pos&EQP_SHADOW_ARMS);
  10383. if( pos == EQP_SHADOW_ARMS )
  10384. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  10385. }
  10386. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  10387. //Update skill-block range database when weapon range changes. [Skotlex]
  10388. i = equip_index[EQI_HAND_R];
  10389. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  10390. flag = 1;
  10391. else
  10392. flag = id->range != sd->inventory_data[i]->range;
  10393. }
  10394. if( equipswitch ){
  10395. for( i = 0; i < EQI_MAX; i++ ){
  10396. if( pos&equip_bitmask[i] ){
  10397. // If there was already an item assigned to this slot
  10398. if( sd->equip_switch_index[i] >= 0 ){
  10399. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  10400. }
  10401. // Assign the new index to it
  10402. sd->equip_switch_index[i] = n;
  10403. }
  10404. }
  10405. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  10406. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  10407. return true;
  10408. }else{
  10409. for(i=0;i<EQI_MAX;i++) {
  10410. if(pos & equip_bitmask[i]) {
  10411. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  10412. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  10413. sd->equip_index[i] = n;
  10414. }
  10415. }
  10416. pc_equipswitch_remove(sd, n);
  10417. if(pos==EQP_AMMO) {
  10418. clif_arrowequip( *sd );
  10419. clif_arrow_fail( *sd, ARROWFAIL_SUCCESS );
  10420. }
  10421. else
  10422. clif_equipitemack( *sd, ITEM_EQUIP_ACK_OK, n, pos );
  10423. sd->inventory.u.items_inventory[n].equip = pos;
  10424. }
  10425. if(pos & EQP_HAND_R) {
  10426. if(id)
  10427. sd->weapontype1 = id->subtype;
  10428. else
  10429. sd->weapontype1 = W_FIST;
  10430. pc_calcweapontype(sd);
  10431. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10432. }
  10433. if(pos & EQP_HAND_L) {
  10434. if(id) {
  10435. if(id->type == IT_WEAPON) {
  10436. sd->status.shield = W_FIST;
  10437. sd->weapontype2 = id->subtype;
  10438. }
  10439. else
  10440. if(id->type == IT_ARMOR) {
  10441. sd->status.shield = id->look;
  10442. sd->weapontype2 = W_FIST;
  10443. }
  10444. }
  10445. else
  10446. sd->status.shield = sd->weapontype2 = W_FIST;
  10447. pc_calcweapontype(sd);
  10448. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10449. }
  10450. if(pos & EQP_SHOES)
  10451. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10452. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  10453. int16 idx = sd->equip_index[EQI_AMMO];
  10454. if (idx >= 0) {
  10455. switch (sd->inventory_data[idx]->subtype) {
  10456. case AMMO_ARROW:
  10457. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  10458. pc_unequipitem(sd, idx, 2 | 4);
  10459. break;
  10460. case AMMO_BULLET:
  10461. case AMMO_SHELL:
  10462. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10463. #ifdef RENEWAL
  10464. && id->subtype != W_GRENADE
  10465. #endif
  10466. )
  10467. pc_unequipitem(sd, idx, 2 | 4);
  10468. break;
  10469. #ifndef RENEWAL
  10470. case AMMO_GRENADE:
  10471. if (id->subtype != W_GRENADE)
  10472. pc_unequipitem(sd, idx, 2 | 4);
  10473. break;
  10474. #endif
  10475. }
  10476. }
  10477. }
  10478. pc_set_costume_view(sd);
  10479. pc_checkallowskill(sd); //Check if status changes should be halted.
  10480. iflag = sd->npc_item_flag;
  10481. // Check for combos (MUST be before status_calc_pc)
  10482. if (!id->combos.empty())
  10483. pc_checkcombo(sd, id);
  10484. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10485. ; // No cards
  10486. else {
  10487. for (i = 0; i < MAX_SLOTS; i++) {
  10488. if (!sd->inventory.u.items_inventory[n].card[i])
  10489. continue;
  10490. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10491. if (data != nullptr) {
  10492. if (!data->combos.empty())
  10493. pc_checkcombo(sd, data.get());
  10494. }
  10495. }
  10496. }
  10497. status_calc_pc(sd,SCO_NONE);
  10498. if (flag) //Update skill data
  10499. clif_skillinfoblock(sd);
  10500. //OnEquip script [Skotlex]
  10501. if (id) {
  10502. current_equip_item_index = n;
  10503. current_equip_card_id = 0;
  10504. //only run the script if item isn't restricted
  10505. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  10506. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10507. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10508. ; //No cards
  10509. else {
  10510. for( i = 0; i < MAX_SLOTS; i++ ) {
  10511. if (!sd->inventory.u.items_inventory[n].card[i])
  10512. continue;
  10513. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10514. if ( data != nullptr ) {
  10515. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data.get(), sd->bl.m))) {
  10516. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10517. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10518. }
  10519. }
  10520. current_equip_card_id = 0;
  10521. }
  10522. }
  10523. current_equip_item_index = -1;
  10524. }
  10525. sd->npc_item_flag = iflag;
  10526. return true;
  10527. }
  10528. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int32 position ){
  10529. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10530. while( it != bonus.end() ){
  10531. std::shared_ptr<s_autobonus> b = *it;
  10532. if( ( b->pos & position ) != b->pos ){
  10533. it++;
  10534. continue;
  10535. }
  10536. it = bonus.erase( it );
  10537. }
  10538. }
  10539. /**
  10540. * Recalculate player status on unequip
  10541. * @param sd: Player data
  10542. * @param n: Item inventory index
  10543. * @param flag: Whether to recalculate a player's status or not
  10544. * @return True on success or false on failure
  10545. */
  10546. static void pc_unequipitem_sub(map_session_data *sd, int32 n, int32 flag) {
  10547. int32 i, iflag;
  10548. bool status_calc = false;
  10549. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10550. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10551. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10552. sd->inventory.u.items_inventory[n].equip = 0;
  10553. if (!(flag & 4))
  10554. pc_checkallowskill(sd);
  10555. iflag = sd->npc_item_flag;
  10556. // Check for combos (MUST be before status_calc_pc)
  10557. if (sd->inventory_data[n]) {
  10558. if (!sd->inventory_data[n]->combos.empty()) {
  10559. if (pc_removecombo(sd, sd->inventory_data[n]))
  10560. status_calc = true;
  10561. }
  10562. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10563. ; // No cards
  10564. else {
  10565. for (i = 0; i < MAX_SLOTS; i++) {
  10566. if (!sd->inventory.u.items_inventory[n].card[i])
  10567. continue;
  10568. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10569. if (data != nullptr) {
  10570. if (!data->combos.empty()) {
  10571. if (pc_removecombo(sd, data.get()))
  10572. status_calc = true;
  10573. }
  10574. }
  10575. }
  10576. }
  10577. }
  10578. if (flag & 1 || status_calc) {
  10579. pc_checkallowskill(sd);
  10580. status_calc_pc(sd, SCO_FORCE);
  10581. }
  10582. if (sd->sc.getSCE(SC_SIGNUMCRUCIS) && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10583. status_change_end(&sd->bl, SC_SIGNUMCRUCIS);
  10584. //OnUnEquip script [Skotlex]
  10585. if (sd->inventory_data[n]) {
  10586. current_equip_item_index = n;
  10587. current_equip_card_id = 0;
  10588. if (sd->inventory_data[n]->unequip_script)
  10589. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10590. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10591. ; //No cards
  10592. else {
  10593. for (i = 0; i < MAX_SLOTS; i++) {
  10594. if (!sd->inventory.u.items_inventory[n].card[i])
  10595. continue;
  10596. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10597. if (data != nullptr) {
  10598. if (data->unequip_script) {
  10599. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10600. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10601. }
  10602. }
  10603. current_equip_card_id = 0;
  10604. }
  10605. }
  10606. current_equip_item_index = -1;
  10607. }
  10608. sd->npc_item_flag = iflag;
  10609. }
  10610. /**
  10611. * Called when attempting to unequip an item from a player
  10612. * @param sd: Player data
  10613. * @param n: Item inventory index
  10614. * @param flag: Type of unequip
  10615. * 0 - only unequip
  10616. * 1 - calculate status after unequipping
  10617. * 2 - force unequip
  10618. * 4 - unequip by switching equipment
  10619. * @return True on success or false on failure
  10620. */
  10621. bool pc_unequipitem(map_session_data *sd, int32 n, int32 flag) {
  10622. int32 i, pos;
  10623. nullpo_retr(false,sd);
  10624. if (n < 0 || n >= MAX_INVENTORY) {
  10625. clif_unequipitemack(*sd,0,0,false);
  10626. return false;
  10627. }
  10628. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10629. clif_unequipitemack(*sd,n,0,false);
  10630. return false; //Nothing to unequip
  10631. }
  10632. // status change that makes player cannot unequip equipment
  10633. if (!(flag&2) && !sd->sc.empty() &&( sd->sc.cant.unequip ||
  10634. (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10635. {
  10636. clif_unequipitemack(*sd,n,0,false);
  10637. return false;
  10638. }
  10639. if (battle_config.battle_log)
  10640. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10641. for(i = 0; i < EQI_MAX; i++) {
  10642. if (pos & equip_bitmask[i])
  10643. sd->equip_index[i] = -1;
  10644. }
  10645. if(pos & EQP_HAND_R) {
  10646. sd->weapontype1 = W_FIST;
  10647. sd->status.weapon = sd->weapontype2;
  10648. pc_calcweapontype(sd);
  10649. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10650. if( !battle_config.dancing_weaponswitch_fix )
  10651. status_change_end(&sd->bl, SC_DANCING); // Unequipping => stop dancing.
  10652. #ifdef RENEWAL
  10653. if (battle_config.switch_remove_edp&2) {
  10654. #else
  10655. if (battle_config.switch_remove_edp&1) {
  10656. #endif
  10657. status_change_end(&sd->bl, SC_EDP);
  10658. }
  10659. }
  10660. if(pos & EQP_HAND_L) {
  10661. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10662. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10663. sd->status.shield = sd->weapontype2 = W_FIST;
  10664. pc_calcweapontype(sd);
  10665. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10666. }
  10667. if(pos & EQP_SHOES)
  10668. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10669. clif_unequipitemack(*sd,n,pos,true);
  10670. pc_set_costume_view(sd);
  10671. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10672. // On weapon change (right and left hand)
  10673. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10674. if (battle_config.ammo_unequip && !(flag & 4)) {
  10675. switch (sd->inventory_data[n]->subtype) {
  10676. case W_BOW:
  10677. case W_MUSICAL:
  10678. case W_WHIP:
  10679. case W_REVOLVER:
  10680. case W_RIFLE:
  10681. case W_GATLING:
  10682. case W_SHOTGUN:
  10683. case W_GRENADE: {
  10684. int16 idx = sd->equip_index[EQI_AMMO];
  10685. if (idx >= 0) {
  10686. sd->equip_index[EQI_AMMO] = -1;
  10687. clif_unequipitemack(*sd, idx, sd->inventory.u.items_inventory[idx].equip, true);
  10688. pc_unequipitem_sub(sd, idx, 0);
  10689. }
  10690. }
  10691. break;
  10692. }
  10693. }
  10694. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10695. }
  10696. // On armor change
  10697. if (pos & EQP_ARMOR) {
  10698. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10699. }
  10700. // On equipment change
  10701. #ifndef RENEWAL
  10702. if (!(flag&4))
  10703. status_change_end(&sd->bl, SC_CONCENTRATION);
  10704. #endif
  10705. // On ammo change
  10706. 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_))
  10707. status_change_end(&sd->bl, SC_P_ALTER);
  10708. pc_unequipitem_sub(sd, n, flag);
  10709. return true;
  10710. }
  10711. int32 pc_equipswitch( map_session_data* sd, int32 index ){
  10712. // Get the target equip mask
  10713. int32 position = sd->inventory.u.items_inventory[index].equipSwitch;
  10714. // Get the currently equipped item
  10715. int16 equippedItem = pc_checkequip( sd, position, true );
  10716. // No item equipped at the target
  10717. if( equippedItem == -1 ){
  10718. // Remove it from the equip switch
  10719. pc_equipswitch_remove( sd, index );
  10720. pc_equipitem( sd, index, position );
  10721. return position;
  10722. }else{
  10723. std::map<int32, int32> unequipped;
  10724. int32 unequipped_position = 0;
  10725. // Unequip all items that interfere
  10726. for( int32 i = 0; i < EQI_MAX; i++ ){
  10727. int32 unequip_index = sd->equip_index[i];
  10728. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10729. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10730. // Store the unequipped index and position mask for later
  10731. unequipped[unequip_index] = unequip_item->equip;
  10732. // Keep the position for later
  10733. unequipped_position |= unequip_item->equip;
  10734. // Unequip the item
  10735. pc_unequipitem( sd, unequip_index, 0 );
  10736. }
  10737. }
  10738. int32 all_position = position | unequipped_position;
  10739. // Equip everything that is hit by the mask
  10740. for( int32 i = 0; i < EQI_MAX; i++ ){
  10741. int32 exchange_index = sd->equip_switch_index[i];
  10742. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10743. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10744. // Store the target position
  10745. int32 exchange_position = exchange_item->equipSwitch;
  10746. // Remove the item from equip switch
  10747. pc_equipswitch_remove( sd, exchange_index );
  10748. // Equip the item at the destinated position
  10749. pc_equipitem( sd, exchange_index, exchange_position );
  10750. }
  10751. }
  10752. // Place all unequipped items into the equip switch window
  10753. for( std::pair<int32, int32> pair : unequipped ){
  10754. int32 unequipped_index = pair.first;
  10755. int32 unequipped_position = pair.second;
  10756. // Rebuild the index cache
  10757. for( int32 i = 0; i < EQI_MAX; i++ ){
  10758. if( unequipped_position & equip_bitmask[i] ){
  10759. sd->equip_switch_index[i] = unequipped_index;
  10760. }
  10761. }
  10762. // Set the correct position mask
  10763. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10764. // Notify the client
  10765. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10766. }
  10767. return all_position;
  10768. }
  10769. }
  10770. void pc_equipswitch_remove( map_session_data* sd, int32 index ){
  10771. struct item* item = &sd->inventory.u.items_inventory[index];
  10772. if( !item->equipSwitch ){
  10773. return;
  10774. }
  10775. for( int32 i = 0; i < EQI_MAX; i++ ){
  10776. // If a match is found
  10777. if( sd->equip_switch_index[i] == index ){
  10778. // Remove it from the slot
  10779. sd->equip_switch_index[i] = -1;
  10780. }
  10781. }
  10782. // Send out one packet for all slots using the current item's mask
  10783. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10784. item->equipSwitch = 0;
  10785. }
  10786. /*==========================================
  10787. * Checking if player (sd) has an invalid item
  10788. * and is unequiped on map load (item_noequip)
  10789. *------------------------------------------*/
  10790. void pc_checkitem(map_session_data *sd) {
  10791. int32 i, calc_flag = 0;
  10792. struct item* it;
  10793. nullpo_retv(sd);
  10794. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10795. return;
  10796. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10797. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10798. it = &sd->inventory.u.items_inventory[i];
  10799. if( it->nameid == 0 )
  10800. continue;
  10801. if( !it->equip )
  10802. continue;
  10803. if( it->equip&~pc_equippoint(sd,i) ) {
  10804. pc_unequipitem(sd, i, 2);
  10805. calc_flag = 1;
  10806. continue;
  10807. }
  10808. 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) ) {
  10809. pc_unequipitem(sd, i, 2);
  10810. calc_flag = 1;
  10811. continue;
  10812. }
  10813. }
  10814. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10815. it = &sd->inventory.u.items_inventory[i];
  10816. if( it->nameid == 0 )
  10817. continue;
  10818. if( !it->equipSwitch )
  10819. continue;
  10820. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10821. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10822. for( int32 j = 0; j < EQI_MAX; j++ ){
  10823. if( sd->equip_switch_index[j] == i ){
  10824. sd->equip_switch_index[j] = -1;
  10825. }
  10826. }
  10827. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10828. continue;
  10829. }
  10830. }
  10831. if( calc_flag && sd->state.active ) {
  10832. pc_checkallowskill(sd);
  10833. status_calc_pc(sd,SCO_NONE);
  10834. }
  10835. }
  10836. /*==========================================
  10837. * Checks for unavailable items and removes them.
  10838. * @param sd: Player data
  10839. * @param type Forced check:
  10840. * 1 - Inventory
  10841. * 2 - Cart
  10842. * 4 - Storage
  10843. *------------------------------------------*/
  10844. void pc_check_available_item(map_session_data *sd, uint8 type)
  10845. {
  10846. int32 i;
  10847. t_itemid nameid;
  10848. char output[256];
  10849. nullpo_retv(sd);
  10850. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10851. for(i = 0; i < MAX_INVENTORY; i++) {
  10852. nameid = sd->inventory.u.items_inventory[i].nameid;
  10853. if (!nameid)
  10854. continue;
  10855. if (!itemdb_available(nameid)) {
  10856. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10857. clif_displaymessage(sd->fd, output);
  10858. 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);
  10859. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10860. continue;
  10861. }
  10862. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10863. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10864. }
  10865. }
  10866. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10867. for(i = 0; i < MAX_CART; i++) {
  10868. nameid = sd->cart.u.items_cart[i].nameid;
  10869. if (!nameid)
  10870. continue;
  10871. if (!itemdb_available(nameid)) {
  10872. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10873. clif_displaymessage(sd->fd, output);
  10874. 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);
  10875. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10876. continue;
  10877. }
  10878. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10879. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10880. }
  10881. }
  10882. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10883. for(i = 0; i < sd->storage.max_amount; i++) {
  10884. nameid = sd->storage.u.items_storage[i].nameid;
  10885. if (!nameid)
  10886. continue;
  10887. if (!itemdb_available(nameid)) {
  10888. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10889. clif_displaymessage(sd->fd, output);
  10890. 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);
  10891. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10892. continue;
  10893. }
  10894. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10895. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10896. }
  10897. }
  10898. }
  10899. /*==========================================
  10900. * Update PVP rank for sd1 in cmp to sd2
  10901. *------------------------------------------*/
  10902. static int32 pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10903. {
  10904. map_session_data *sd1,*sd2;
  10905. sd1=(map_session_data *)bl;
  10906. sd2=va_arg(ap,map_session_data *);
  10907. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10908. {// cannot register pvp rank for hidden GMs
  10909. return 0;
  10910. }
  10911. if( sd1->pvp_point > sd2->pvp_point )
  10912. sd2->pvp_rank++;
  10913. return 0;
  10914. }
  10915. /*==========================================
  10916. * Calculate new rank beetween all present players (map_foreachinallarea)
  10917. * and display result
  10918. *------------------------------------------*/
  10919. int32 pc_calc_pvprank(map_session_data *sd)
  10920. {
  10921. int32 old = sd->pvp_rank;
  10922. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10923. sd->pvp_rank=1;
  10924. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10925. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10926. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10927. return sd->pvp_rank;
  10928. }
  10929. /*==========================================
  10930. * Calculate next sd ranking calculation from config
  10931. *------------------------------------------*/
  10932. TIMER_FUNC(pc_calc_pvprank_timer){
  10933. map_session_data *sd;
  10934. sd=map_id2sd(id);
  10935. if(sd==nullptr)
  10936. return 0;
  10937. sd->pvp_timer = INVALID_TIMER;
  10938. if( pc_isinvisible(sd) )
  10939. {// do not calculate the pvp rank for a hidden GM
  10940. return 0;
  10941. }
  10942. if( pc_calc_pvprank(sd) > 0 )
  10943. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10944. return 0;
  10945. }
  10946. /*==========================================
  10947. * Checking if sd is married
  10948. * Return:
  10949. * partner_id = yes
  10950. * 0 = no
  10951. *------------------------------------------*/
  10952. int32 pc_ismarried(map_session_data *sd)
  10953. {
  10954. if(sd == nullptr)
  10955. return -1;
  10956. if(sd->status.partner_id > 0)
  10957. return sd->status.partner_id;
  10958. else
  10959. return 0;
  10960. }
  10961. /*==========================================
  10962. * Marry player sd to player dstsd
  10963. * Return:
  10964. * false = fail
  10965. * true = success
  10966. *------------------------------------------*/
  10967. bool pc_marriage(map_session_data *sd,map_session_data *dstsd)
  10968. {
  10969. if(sd == nullptr || dstsd == nullptr ||
  10970. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10971. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10972. return false;
  10973. sd->status.partner_id = dstsd->status.char_id;
  10974. dstsd->status.partner_id = sd->status.char_id;
  10975. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10976. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10977. return true;
  10978. }
  10979. /*==========================================
  10980. * Divorce sd from its partner
  10981. * Return:
  10982. * false = fail
  10983. * true = success
  10984. *------------------------------------------*/
  10985. bool pc_divorce(map_session_data *sd)
  10986. {
  10987. map_session_data *p_sd;
  10988. int32 i;
  10989. if( sd == nullptr || !pc_ismarried(sd) )
  10990. return false;
  10991. if( !sd->status.partner_id )
  10992. return false; // Char is not married
  10993. if( (p_sd = map_charid2sd(sd->status.partner_id)) == nullptr )
  10994. { // Lets char server do the divorce
  10995. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10996. return false; // No char server connected
  10997. return true;
  10998. }
  10999. // Both players online, lets do the divorce manually
  11000. sd->status.partner_id = 0;
  11001. p_sd->status.partner_id = 0;
  11002. for( i = 0; i < MAX_INVENTORY; i++ )
  11003. {
  11004. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  11005. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  11006. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  11007. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  11008. }
  11009. clif_divorced( *sd, p_sd->status.name );
  11010. clif_divorced( *p_sd, sd->status.name );
  11011. return true;
  11012. }
  11013. /**
  11014. * Get the partner map_session_data of a player
  11015. * @param sd : the husband|wife session
  11016. * @return partner session or nullptr
  11017. */
  11018. map_session_data *pc_get_partner(map_session_data *sd){
  11019. if (!sd || !pc_ismarried(sd))
  11020. return nullptr;
  11021. return map_charid2sd(sd->status.partner_id);
  11022. }
  11023. /**
  11024. * Get the father map_session_data of a player
  11025. * @param sd : the baby session
  11026. * @return father session or nullptr
  11027. */
  11028. map_session_data *pc_get_father (map_session_data *sd){
  11029. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  11030. return nullptr;
  11031. return map_charid2sd(sd->status.father);
  11032. }
  11033. /**
  11034. * Get the mother map_session_data of a player
  11035. * @param sd : the baby session
  11036. * @return mother session or nullptr
  11037. */
  11038. map_session_data *pc_get_mother (map_session_data *sd){
  11039. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  11040. return nullptr;
  11041. return map_charid2sd(sd->status.mother);
  11042. }
  11043. /*==========================================
  11044. * Get sd children charid. (Need to be married)
  11045. *------------------------------------------*/
  11046. map_session_data *pc_get_child (map_session_data *sd)
  11047. {
  11048. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  11049. // charid2sd returns nullptr if not found
  11050. return nullptr;
  11051. return map_charid2sd(sd->status.child);
  11052. }
  11053. /*==========================================
  11054. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  11055. *------------------------------------------*/
  11056. void pc_bleeding (map_session_data *sd, t_tick diff_tick)
  11057. {
  11058. int32 hp = 0, sp = 0;
  11059. if( pc_isdead(sd) )
  11060. return;
  11061. if (sd->hp_loss.value) {
  11062. sd->hp_loss.tick += diff_tick;
  11063. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  11064. hp += sd->hp_loss.value;
  11065. sd->hp_loss.tick -= sd->hp_loss.rate;
  11066. }
  11067. if(hp >= sd->battle_status.hp)
  11068. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  11069. }
  11070. if (sd->sp_loss.value) {
  11071. sd->sp_loss.tick += diff_tick;
  11072. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  11073. sp += sd->sp_loss.value;
  11074. sd->sp_loss.tick -= sd->sp_loss.rate;
  11075. }
  11076. }
  11077. if (hp > 0 || sp > 0)
  11078. status_zap(&sd->bl, hp, sp);
  11079. }
  11080. //Character regen. Flag is used to know which types of regen can take place.
  11081. //&1: HP regen
  11082. //&2: SP regen
  11083. void pc_regen (map_session_data *sd, t_tick diff_tick)
  11084. {
  11085. int32 hp = 0, sp = 0;
  11086. if (sd->hp_regen.value) {
  11087. sd->hp_regen.tick += diff_tick;
  11088. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  11089. hp += sd->hp_regen.value;
  11090. sd->hp_regen.tick -= sd->hp_regen.rate;
  11091. }
  11092. }
  11093. if (sd->sp_regen.value) {
  11094. sd->sp_regen.tick += diff_tick;
  11095. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  11096. sp += sd->sp_regen.value;
  11097. sd->sp_regen.tick -= sd->sp_regen.rate;
  11098. }
  11099. }
  11100. if (sd->percent_hp_regen.value) {
  11101. sd->percent_hp_regen.tick += diff_tick;
  11102. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  11103. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  11104. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  11105. }
  11106. }
  11107. if (sd->percent_sp_regen.value) {
  11108. sd->percent_sp_regen.tick += diff_tick;
  11109. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  11110. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  11111. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  11112. }
  11113. }
  11114. if (hp > 0 || sp > 0)
  11115. status_heal(&sd->bl, hp, sp, 0);
  11116. }
  11117. /*==========================================
  11118. * Memo player sd savepoint. (map,x,y)
  11119. *------------------------------------------*/
  11120. void pc_setsavepoint(map_session_data *sd, int16 mapindex,int32 x,int32 y)
  11121. {
  11122. nullpo_retv(sd);
  11123. safestrncpy( sd->status.save_point.map, mapindex_id2name( mapindex ), sizeof( sd->status.save_point.map ) );
  11124. sd->status.save_point.x = x;
  11125. sd->status.save_point.y = y;
  11126. }
  11127. /*==========================================
  11128. * Save 1 player data at autosave interval
  11129. *------------------------------------------*/
  11130. static TIMER_FUNC(pc_autosave){
  11131. int32 interval;
  11132. struct s_mapiterator* iter;
  11133. map_session_data* sd;
  11134. static int32 last_save_id = 0, save_flag = 0;
  11135. if(save_flag == 2) //Someone was saved on last call, normal cycle
  11136. save_flag = 0;
  11137. else
  11138. save_flag = 1; //Noone was saved, so save first found char.
  11139. iter = mapit_getallusers();
  11140. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11141. {
  11142. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  11143. continue;
  11144. if(sd->bl.id == last_save_id && save_flag != 1) {
  11145. save_flag = 1;
  11146. continue;
  11147. }
  11148. if(save_flag != 1) //Not our turn to save yet.
  11149. continue;
  11150. //Save char.
  11151. last_save_id = sd->bl.id;
  11152. save_flag = 2;
  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.
  11689. * @param level: Base level of player
  11690. * @param job:
  11691. * @return base_hp
  11692. */
  11693. uint32 JobDatabase::calc_basehp( const uint16 level, const std::shared_ptr<s_job_info>& job ){
  11694. uint64 mapid = pc_jobid2mapid( job->job_id );
  11695. double base_hp = 35.;
  11696. base_hp += floor( level * ( job->hp_increase / 100. ) );
  11697. for( uint16 i = 2; i <= level; i++ ){
  11698. // Don't have round()
  11699. base_hp += floor( ( ( job->hp_factor / 100. ) * i ) + 0.5 );
  11700. }
  11701. #ifndef RENEWAL
  11702. if( (mapid & MAPID_BASEMASK) == MAPID_NINJA ){
  11703. if( level >= 10 ){
  11704. base_hp += 90.;
  11705. }
  11706. }else if( (mapid & MAPID_BASEMASK) == MAPID_GUNSLINGER ){
  11707. if( level >= 10 ){
  11708. base_hp += 90.;
  11709. }
  11710. }
  11711. #endif
  11712. if( (mapid&MAPID_BASEMASK) == MAPID_SUMMONER ){
  11713. base_hp += floor( ( base_hp / 2 ) + 0.5 );
  11714. }else if( (mapid&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE ){
  11715. // Supernovice lvl99 HP bonus.
  11716. if( level >= 99 ){
  11717. base_hp += 2000.;
  11718. }
  11719. // Supernovice lvl150 HP bonus.
  11720. if( level >= 150 ){
  11721. base_hp += 2000.;
  11722. }
  11723. }
  11724. return static_cast<uint32>( base_hp );
  11725. }
  11726. /**
  11727. * Calculates base SP of player.
  11728. * @param level: Base level of player
  11729. * @param job:
  11730. * @return base_sp
  11731. */
  11732. uint32 JobDatabase::calc_basesp( const uint16 level, const std::shared_ptr<s_job_info>& job ){
  11733. uint64 mapid = pc_jobid2mapid( job->job_id );
  11734. double base_sp = 10;
  11735. base_sp += floor( level * ( job->sp_increase / 100. ) );
  11736. for( uint16 i = 2; i <= level; i++ ){
  11737. // Don't have round()
  11738. base_sp += floor( ( ( job->sp_factor / 100. ) * i ) + 0.5 );
  11739. }
  11740. if( (mapid & MAPID_BASEMASK) == MAPID_NINJA ){
  11741. if( level >= 10 ){
  11742. base_sp -= 22.;
  11743. }else{
  11744. base_sp = 11. + 3. * level;
  11745. }
  11746. }else if( (mapid & MAPID_BASEMASK) == MAPID_GUNSLINGER ){
  11747. if( level >= 10 ){
  11748. base_sp -= 18.;
  11749. }else{
  11750. base_sp = 9. + 3. * level;
  11751. }
  11752. }else if( (mapid&MAPID_BASEMASK) == MAPID_SUMMONER ){
  11753. base_sp += floor( ( base_sp / 2 ) + 0.5 );
  11754. }
  11755. return static_cast<uint32>( base_sp );
  11756. }
  11757. /**
  11758. * Calculates base AP of player.
  11759. * @param level: Base level of player
  11760. * @param job_id:
  11761. * @return base_sp
  11762. */
  11763. uint32 JobDatabase::calc_baseap( const uint16 level, const std::shared_ptr<s_job_info>& job ){
  11764. uint64 mapid = pc_jobid2mapid( job->job_id );
  11765. double base_ap = 0;
  11766. if( level >= 200 ){
  11767. base_ap = 200.0;
  11768. }
  11769. base_ap += floor( level * ( job->ap_increase / 100. ) );
  11770. for( uint16 i = 2; i <= level; i++ ){
  11771. // Don't have round()
  11772. base_ap += floor( ( ( job->ap_factor / 100. ) * i ) + 0.5 );
  11773. }
  11774. return static_cast<uint32>( base_ap );
  11775. }
  11776. const std::string JobDatabase::getDefaultLocation() {
  11777. return std::string(db_path) + "/job_stats.yml";
  11778. }
  11779. /**
  11780. * Reads and parses an entry from the job_db.
  11781. * @param node: YAML node containing the entry.
  11782. * @return count of successfully parsed rows
  11783. */
  11784. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11785. if (this->nodeExists(node, "Jobs")) {
  11786. const ryml::NodeRef& jobsNode = node["Jobs"];
  11787. for (const auto &jobit : jobsNode) {
  11788. std::string job_name;
  11789. c4::from_chars(jobit.key(), &job_name);
  11790. std::string job_name_constant = "JOB_" + job_name;
  11791. int64 job_id;
  11792. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11793. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11794. return 0;
  11795. }
  11796. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11797. bool exists = job != nullptr;
  11798. if (!exists) {
  11799. job = std::make_shared<s_job_info>();
  11800. job->job_id = static_cast<uint16>( job_id );
  11801. job->job_bonus.resize(MAX_LEVEL);
  11802. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11803. job->base_hp.resize(MAX_LEVEL);
  11804. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11805. job->base_sp.resize(MAX_LEVEL);
  11806. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11807. job->base_ap.resize(MAX_LEVEL);
  11808. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11809. }
  11810. if (this->nodeExists(node, "MaxWeight")) {
  11811. uint32 weight;
  11812. if (!this->asUInt32(node, "MaxWeight", weight))
  11813. return 0;
  11814. job->max_weight_base = weight;
  11815. } else {
  11816. if (!exists)
  11817. job->max_weight_base = 20000;
  11818. }
  11819. if (this->nodeExists(node, "HpFactor")) {
  11820. uint32 hp;
  11821. if (!this->asUInt32(node, "HpFactor", hp))
  11822. return 0;
  11823. job->hp_factor = hp;
  11824. } else {
  11825. if (!exists)
  11826. job->hp_factor = 0;
  11827. }
  11828. if (this->nodeExists(node, "HpIncrease")) {
  11829. uint32 hp;
  11830. if (!this->asUInt32(node, "HpIncrease", hp))
  11831. return 0;
  11832. job->hp_increase = hp;
  11833. } else {
  11834. if (!exists)
  11835. job->hp_increase = 500;
  11836. }
  11837. if( this->nodeExists( node, "SpFactor" ) ){
  11838. uint32 sp;
  11839. if( !this->asUInt32( node, "SpFactor", sp ) ){
  11840. return 0;
  11841. }
  11842. job->sp_factor = sp;
  11843. }else{
  11844. if( !exists ){
  11845. job->sp_factor = 0;
  11846. }
  11847. }
  11848. if (this->nodeExists(node, "SpIncrease")) {
  11849. uint32 sp;
  11850. if (!this->asUInt32(node, "SpIncrease", sp))
  11851. return 0;
  11852. job->sp_increase = sp;
  11853. } else {
  11854. if (!exists)
  11855. job->sp_increase = 100;
  11856. }
  11857. if( this->nodeExists( node, "ApFactor" ) ){
  11858. uint32 ap;
  11859. if( !this->asUInt32( node, "ApFactor", ap ) ){
  11860. return 0;
  11861. }
  11862. job->ap_factor = ap;
  11863. }else{
  11864. if( !exists ){
  11865. job->ap_factor = 0;
  11866. }
  11867. }
  11868. if( this->nodeExists( node, "ApIncrease" ) ){
  11869. uint32 ap;
  11870. if( !this->asUInt32( node, "ApIncrease", ap ) ){
  11871. return 0;
  11872. }
  11873. job->ap_increase = ap;
  11874. }else{
  11875. if( !exists ){
  11876. job->ap_increase = 0;
  11877. }
  11878. }
  11879. if (this->nodeExists(node, "BaseASPD")) {
  11880. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11881. uint8 max = MAX_WEAPON_TYPE;
  11882. int32 def_aspd = AMOTION_ZERO_ASPD;
  11883. #ifdef RENEWAL
  11884. // Renewal adds an extra column for shields
  11885. max += 1;
  11886. // Renewal uses ASPD penalty which is amotion divided by the amotion interval
  11887. def_aspd /= AMOTION_INTERVAL;
  11888. #endif
  11889. if (!exists) {
  11890. job->aspd_base.resize(max);
  11891. std::fill(job->aspd_base.begin(), job->aspd_base.end(), def_aspd);
  11892. }
  11893. for (const auto &aspdit : aspdNode) {
  11894. std::string weapon;
  11895. c4::from_chars(aspdit.key(), &weapon);
  11896. std::string weapon_constant = "W_" + weapon;
  11897. int64 constant;
  11898. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11899. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11900. continue;
  11901. }
  11902. if (constant < W_FIST || constant >= max) {
  11903. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11904. continue;
  11905. }
  11906. int16 aspd;
  11907. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11908. return 0;
  11909. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11910. }
  11911. } else {
  11912. if (!exists) {
  11913. uint8 max = MAX_WEAPON_TYPE;
  11914. int32 def_aspd = AMOTION_ZERO_ASPD;
  11915. #ifdef RENEWAL
  11916. // Renewal adds an extra column for shields
  11917. max += 1;
  11918. // Renewal uses ASPD penalty which is amotion divided by the amotion interval
  11919. def_aspd /= AMOTION_INTERVAL;
  11920. #endif
  11921. job->aspd_base.resize(max);
  11922. std::fill(job->aspd_base.begin(), job->aspd_base.end(), def_aspd);
  11923. }
  11924. }
  11925. if (this->nodeExists(node, "MaxStats")) {
  11926. const ryml::NodeRef& statNode = node["MaxStats"];
  11927. for (const auto &statit : statNode) {
  11928. std::string stat;
  11929. c4::from_chars(statit.key(), &stat);
  11930. std::string stat_constant = "PARAM_" + stat;
  11931. int64 constant;
  11932. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11933. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11934. continue;
  11935. }
  11936. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11937. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11938. continue;
  11939. }
  11940. uint16 max;
  11941. if (!this->asUInt16(statNode, stat.c_str(), max))
  11942. return 0;
  11943. job->max_param[constant] = max;
  11944. }
  11945. }
  11946. if (this->nodeExists(node, "MaxBaseLevel")) {
  11947. uint16 level;
  11948. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11949. return 0;
  11950. if (level == 0 || level > MAX_LEVEL) {
  11951. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11952. level = MAX_LEVEL;
  11953. }
  11954. job->max_base_level = level;
  11955. } else {
  11956. if (!exists)
  11957. job->max_base_level = MAX_LEVEL;
  11958. }
  11959. if (this->nodeExists(node, "BaseExp")) {
  11960. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11961. uint16 level;
  11962. if (!this->asUInt16(bexpNode, "Level", level))
  11963. return 0;
  11964. if (level > job->max_base_level)
  11965. continue;
  11966. if (level == 0 || level > MAX_LEVEL) {
  11967. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11968. return 0;
  11969. }
  11970. if (this->nodeExists(bexpNode, "Exp")) {
  11971. t_exp exp;
  11972. if (!this->asUInt64(bexpNode, "Exp", exp))
  11973. return 0;
  11974. job->base_exp[level - 1] = exp;
  11975. }
  11976. }
  11977. }
  11978. if (this->nodeExists(node, "MaxJobLevel")) {
  11979. uint16 level;
  11980. if (!this->asUInt16(node, "MaxJobLevel", level))
  11981. return 0;
  11982. if (level == 0 || level > MAX_LEVEL) {
  11983. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11984. level = MAX_LEVEL;
  11985. }
  11986. job->max_job_level = level;
  11987. } else {
  11988. if (!exists)
  11989. job->max_job_level = MAX_LEVEL;
  11990. }
  11991. if (this->nodeExists(node, "JobExp")) {
  11992. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11993. uint16 level;
  11994. if (!this->asUInt16(jexpNode, "Level", level))
  11995. return 0;
  11996. if (level > job->max_job_level)
  11997. continue;
  11998. if (level == 0 || level > MAX_LEVEL) {
  11999. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  12000. return 0;
  12001. }
  12002. if (this->nodeExists(jexpNode, "Exp")) {
  12003. t_exp exp;
  12004. if (!this->asUInt64(jexpNode, "Exp", exp))
  12005. return 0;
  12006. job->job_exp[level - 1] = exp;
  12007. }
  12008. }
  12009. }
  12010. if (this->nodeExists(node, "BonusStats")) {
  12011. const ryml::NodeRef& bonusNode = node["BonusStats"];
  12012. for (const ryml::NodeRef& levelNode : bonusNode) {
  12013. uint16 level;
  12014. if (!this->asUInt16(levelNode, "Level", level))
  12015. return 0;
  12016. if (level == 0 || level > MAX_LEVEL) {
  12017. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  12018. return 0;
  12019. }
  12020. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  12021. if (this->nodeExists(levelNode, parameter_names[idx])) {
  12022. int16 change;
  12023. if (!this->asInt16(levelNode, parameter_names[idx], change))
  12024. return 0;
  12025. job->job_bonus[level - 1][idx] = change;
  12026. }
  12027. }
  12028. }
  12029. }
  12030. #ifdef HP_SP_TABLES
  12031. if (this->nodeExists(node, "BaseHp")) {
  12032. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  12033. uint16 level;
  12034. if (!this->asUInt16(bhpNode, "Level", level))
  12035. return 0;
  12036. if (level > job->max_base_level)
  12037. continue;
  12038. if (level == 0 || level > MAX_LEVEL) {
  12039. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  12040. return 0;
  12041. }
  12042. if (this->nodeExists(bhpNode, "Hp")) {
  12043. uint32 points;
  12044. if (!this->asUInt32(bhpNode, "Hp", points))
  12045. return 0;
  12046. job->base_hp[level - 1] = points;
  12047. }
  12048. }
  12049. }
  12050. if (this->nodeExists(node, "BaseSp")) {
  12051. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  12052. uint16 level;
  12053. if (!this->asUInt16(bspNode, "Level", level))
  12054. return 0;
  12055. if (level > job->max_base_level)
  12056. continue;
  12057. if (level == 0 || level > MAX_LEVEL) {
  12058. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  12059. return 0;
  12060. }
  12061. if (this->nodeExists(bspNode, "Sp")) {
  12062. uint32 points;
  12063. if (!this->asUInt32(bspNode, "Sp", points))
  12064. return 0;
  12065. job->base_sp[level - 1] = points;
  12066. }
  12067. }
  12068. }
  12069. if (this->nodeExists(node, "BaseAp")) {
  12070. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  12071. uint16 level;
  12072. if (!this->asUInt16(bapNode, "Level", level))
  12073. return 0;
  12074. if (level > job->max_base_level)
  12075. continue;
  12076. if (level == 0 || level > MAX_LEVEL) {
  12077. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  12078. return 0;
  12079. }
  12080. if (this->nodeExists(bapNode, "Ap")) {
  12081. uint32 points;
  12082. if (!this->asUInt32(bapNode, "Ap", points))
  12083. return 0;
  12084. job->base_ap[level - 1] = points;
  12085. }
  12086. }
  12087. }
  12088. #endif
  12089. if (!exists)
  12090. this->put(static_cast<uint16>(job_id), job);
  12091. }
  12092. }
  12093. return 1;
  12094. }
  12095. void JobDatabase::loadingFinished() {
  12096. // Checking if all class have their data
  12097. for (auto &jobIt : *this) {
  12098. uint16 job_id = jobIt.first;
  12099. if (!pcdb_checkid(job_id))
  12100. continue;
  12101. 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)
  12102. continue; // Classes that do not need exp tables.
  12103. std::shared_ptr<s_job_info> job = jobIt.second;
  12104. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  12105. if (!maxBaseLv)
  12106. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  12107. if (!maxJobLv)
  12108. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  12109. // Check and init the empty values
  12110. if (job->base_hp.empty())
  12111. job->base_hp.resize(maxBaseLv);
  12112. for (uint16 j = 0; j < maxBaseLv; j++) {
  12113. if (job->base_hp[j] == 0)
  12114. job->base_hp[j] = this->calc_basehp( j + 1, job );
  12115. }
  12116. if (job->base_sp.empty())
  12117. job->base_sp.resize(maxBaseLv);
  12118. for (uint16 j = 0; j < maxBaseLv; j++) {
  12119. if (job->base_sp[j] == 0)
  12120. job->base_sp[j] = this->calc_basesp( j + 1, job );
  12121. }
  12122. if( job->base_ap.empty() ){
  12123. job->base_ap.resize( maxBaseLv );
  12124. }
  12125. for( uint16 j = 0; j < maxBaseLv; j++ ){
  12126. if( job->base_ap[j] == 0 ){
  12127. job->base_ap[j] = this->calc_baseap( j + 1, job );
  12128. }
  12129. }
  12130. // Resize to the maximum base level
  12131. if (job->base_hp.capacity() > maxBaseLv)
  12132. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  12133. if (job->base_sp.capacity() > maxBaseLv)
  12134. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  12135. if (job->base_ap.capacity() > maxBaseLv)
  12136. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  12137. // Resize to the maximum job level
  12138. if (job->job_bonus.capacity() > maxJobLv)
  12139. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  12140. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  12141. // Store total
  12142. int16 current = 0;
  12143. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  12144. // Add the bonus from this job level
  12145. current += job->job_bonus[job_level][parameter];
  12146. // Set the new total on this job level
  12147. job->job_bonus[job_level][parameter] = current;
  12148. }
  12149. }
  12150. uint64 class_ = pc_jobid2mapid( job_id );
  12151. // Set normal status limits
  12152. uint16 max = battle_config.max_parameter;
  12153. do{
  12154. // Always check babies first
  12155. if( class_ & JOBL_BABY ){
  12156. if( class_ & JOBL_THIRD ){
  12157. max = battle_config.max_baby_third_parameter;
  12158. break;
  12159. }else{
  12160. max = battle_config.max_baby_parameter;
  12161. break;
  12162. }
  12163. }
  12164. // Summoner
  12165. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  12166. max = battle_config.max_summoner_parameter;
  12167. break;
  12168. }
  12169. // Extended classes
  12170. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  12171. max = battle_config.max_extended_parameter;
  12172. break;
  12173. }
  12174. if( class_ & JOBL_FOURTH ){
  12175. max = battle_config.max_fourth_parameter;
  12176. break;
  12177. }
  12178. // 3rd class
  12179. if( class_ & JOBL_THIRD ){
  12180. // Transcendent
  12181. if( class_ & JOBL_UPPER ){
  12182. max = battle_config.max_third_trans_parameter;
  12183. break;
  12184. }else{
  12185. max = battle_config.max_third_parameter;
  12186. break;
  12187. }
  12188. }
  12189. // Transcendent
  12190. if( class_ & JOBL_UPPER ){
  12191. max = battle_config.max_trans_parameter;
  12192. break;
  12193. }
  12194. }while( false );
  12195. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  12196. // If it is not explicitly set in the database file
  12197. if( job->max_param[parameter] == 0 ){
  12198. job->max_param[parameter] = max;
  12199. }
  12200. }
  12201. // Set trait status limit
  12202. if( class_ & JOBL_FOURTH ){
  12203. max = battle_config.max_trait_parameter;
  12204. }else{
  12205. max = 0;
  12206. }
  12207. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  12208. // If it is not explicitly set in the database file
  12209. if( job->max_param[parameter] == 0 ){
  12210. job->max_param[parameter] = max;
  12211. }
  12212. }
  12213. }
  12214. TypesafeCachedYamlDatabase::loadingFinished();
  12215. }
  12216. /**
  12217. * Read job_noenter_map.txt
  12218. **/
  12219. static bool pc_readdb_job_noenter_map( char *str[], size_t columns, size_t current ){
  12220. int32 class_ = -1;
  12221. int64 class_tmp;
  12222. if (ISDIGIT(str[0][0])) {
  12223. class_ = atoi(str[0]);
  12224. } else {
  12225. if (!script_get_constant(str[0], &class_tmp)) {
  12226. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  12227. return false;
  12228. }
  12229. class_ = static_cast<int32>(class_tmp);
  12230. }
  12231. if (!pcdb_checkid(class_)) {
  12232. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  12233. return false;
  12234. }
  12235. std::shared_ptr<s_job_info> job = job_db.find(class_);
  12236. if (job == nullptr) {
  12237. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  12238. return false;
  12239. }
  12240. job->noenter_map.zone = atoi(str[1]);
  12241. job->noenter_map.group_lv = atoi(str[2]);
  12242. return true;
  12243. }
  12244. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  12245. return std::string(db_path) + "/statpoint.yml";
  12246. }
  12247. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  12248. uint16 level;
  12249. if (!this->asUInt16(node, "Level", level))
  12250. return 0;
  12251. if (level == 0) {
  12252. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  12253. return 0;
  12254. }
  12255. if (level > MAX_LEVEL) {
  12256. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  12257. return 0;
  12258. }
  12259. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12260. bool exists = entry != nullptr;
  12261. if( !exists ){
  12262. if( !this->nodesExist( node, { "Points" } ) ){
  12263. return 0;
  12264. }
  12265. entry = std::make_shared<s_statpoint_entry>();
  12266. entry->level = level;
  12267. }
  12268. if( this->nodeExists( node, "Points" ) ){
  12269. uint32 points;
  12270. if( !this->asUInt32( node, "Points", points ) ){
  12271. return 0;
  12272. }
  12273. entry->statpoints = points;
  12274. }
  12275. if( this->nodeExists( node, "TraitPoints" ) ){
  12276. uint32 traitpoints;
  12277. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  12278. return 0;
  12279. }
  12280. entry->traitpoints = traitpoints;
  12281. }else{
  12282. if( !exists ){
  12283. entry->traitpoints = 0;
  12284. }
  12285. }
  12286. if( !exists ){
  12287. this->put( level, entry );
  12288. }
  12289. return 1;
  12290. }
  12291. /**
  12292. * Generate the remaining parts of the db if necessary.
  12293. */
  12294. void PlayerStatPointDatabase::loadingFinished(){
  12295. const uint16 trait_start_level = 200;
  12296. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  12297. if( level_one == nullptr ){
  12298. if( battle_config.use_statpoint_table ){
  12299. ShowError( "Missing status points for Level 1\n" );
  12300. }
  12301. level_one = std::make_shared<s_statpoint_entry>();
  12302. level_one->level = 1;
  12303. level_one->statpoints = inter_config.start_status_points;
  12304. level_one->traitpoints = 0;
  12305. this->put( 1, level_one );
  12306. }else if( battle_config.use_statpoint_table ){
  12307. if( level_one->statpoints != inter_config.start_status_points ){
  12308. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, inter_config.start_status_points );
  12309. level_one->statpoints = inter_config.start_status_points;
  12310. }
  12311. }else{
  12312. level_one->statpoints = inter_config.start_status_points;
  12313. level_one->traitpoints = 0;
  12314. }
  12315. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  12316. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  12317. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12318. bool exists = entry != nullptr;
  12319. if( !exists ){
  12320. entry = std::make_shared<s_statpoint_entry>();
  12321. entry->level = level;
  12322. this->put( level, entry );
  12323. }
  12324. if( !battle_config.use_statpoint_table || !exists ){
  12325. if( battle_config.use_statpoint_table ){
  12326. ShowError("Missing status points for Level %hu\n", level);
  12327. }
  12328. if( level <= trait_start_level ){
  12329. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  12330. }else{
  12331. entry->statpoints = last_level->statpoints;
  12332. }
  12333. }
  12334. if( !battle_config.use_traitpoint_table || !exists ){
  12335. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  12336. ShowError( "Missing trait points for Level %hu\n", level );
  12337. }
  12338. if( level > trait_start_level ){
  12339. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  12340. }else{
  12341. entry->traitpoints = 0;
  12342. }
  12343. }
  12344. // Store it for next iteration
  12345. last_level = entry;
  12346. }
  12347. TypesafeCachedYamlDatabase::loadingFinished();
  12348. }
  12349. /*==========================================
  12350. * pc DB reading.
  12351. * job_stats.yml - Job values
  12352. * skill_tree.txt - skill tree for every class
  12353. * attr_fix.yml - elemental adjustment table
  12354. *------------------------------------------*/
  12355. void pc_readdb(void) {
  12356. int32 i, s = 1;
  12357. const char* dbsubpath[] = {
  12358. "",
  12359. "/" DBIMPORT,
  12360. //add other path here
  12361. };
  12362. //reset
  12363. job_db.clear(); // job_info table
  12364. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  12365. penalty_db.load();
  12366. #endif
  12367. statpoint_db.clear();
  12368. job_db.load();
  12369. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  12370. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  12371. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  12372. char* dbsubpath1 = (char*)aMalloc(n1+1);
  12373. char* dbsubpath2 = (char*)aMalloc(n2+1);
  12374. if(i==0) {
  12375. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12376. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  12377. }
  12378. else {
  12379. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12380. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  12381. }
  12382. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  12383. aFree(dbsubpath1);
  12384. aFree(dbsubpath2);
  12385. }
  12386. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  12387. skill_tree_db.reload();
  12388. statpoint_db.load();
  12389. }
  12390. // Read MOTD on startup. [Valaris]
  12391. int32 pc_read_motd(void)
  12392. {
  12393. FILE* fp;
  12394. // clear old MOTD
  12395. memset(motd_text, 0, sizeof(motd_text));
  12396. // read current MOTD
  12397. if( ( fp = fopen(motd_txt, "r") ) != nullptr )
  12398. {
  12399. uint32 entries = 0;
  12400. char buf[CHAT_SIZE_MAX];
  12401. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  12402. {
  12403. uint32 lines = 0;
  12404. size_t len;
  12405. lines++;
  12406. if( buf[0] == '/' && buf[1] == '/' )
  12407. continue;
  12408. len = strlen(buf);
  12409. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  12410. len--;
  12411. if( len ) {
  12412. char * ptr;
  12413. buf[len] = 0;
  12414. if( ( ptr = strstr(buf, " :") ) != nullptr && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  12415. 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);
  12416. }
  12417. else {// empty line
  12418. buf[0] = ' ';
  12419. buf[1] = 0;
  12420. }
  12421. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  12422. entries++;
  12423. }
  12424. fclose(fp);
  12425. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  12426. }
  12427. else
  12428. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  12429. return 0;
  12430. }
  12431. void pc_itemcd_do(map_session_data *sd, bool load) {
  12432. int32 i,cursor = 0;
  12433. struct item_cd* cd = nullptr;
  12434. if( load ) {
  12435. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  12436. // no item cooldown is associated with this character
  12437. return;
  12438. }
  12439. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12440. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  12441. sd->item_delay[cursor].tick = cd->tick[i];
  12442. sd->item_delay[cursor].nameid = cd->nameid[i];
  12443. cursor++;
  12444. }
  12445. }
  12446. idb_remove(itemcd_db,sd->status.char_id);
  12447. } else {
  12448. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  12449. // create a new skill cooldown object for map storage
  12450. CREATE( cd, struct item_cd, 1 );
  12451. idb_put( itemcd_db, sd->status.char_id, cd );
  12452. }
  12453. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12454. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  12455. cd->tick[cursor] = sd->item_delay[i].tick;
  12456. cd->nameid[cursor] = sd->item_delay[i].nameid;
  12457. cursor++;
  12458. }
  12459. }
  12460. }
  12461. return;
  12462. }
  12463. /**
  12464. * Add item delay to player's item delay data
  12465. * @param sd Player
  12466. * @param id Item data
  12467. * @param tick Current tick
  12468. * @param n Item index in inventory
  12469. * @return 0: No delay, can consume item.
  12470. * 1: Has delay, cancel consumption.
  12471. **/
  12472. uint8 pc_itemcd_add(map_session_data *sd, struct item_data *id, t_tick tick, uint16 n) {
  12473. int32 i;
  12474. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  12475. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  12476. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  12477. if( i < MAX_ITEMDELAYS ) {
  12478. if( sd->item_delay[i].nameid ) {// found
  12479. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  12480. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  12481. char e_msg[CHAT_SIZE_MAX];
  12482. if( e_tick > 99 )
  12483. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  12484. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  12485. else
  12486. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  12487. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  12488. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  12489. return 1; // Delay has not expired yet
  12490. }
  12491. } else {// not yet used item (all slots are initially empty)
  12492. sd->item_delay[i].nameid = id->nameid;
  12493. }
  12494. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  12495. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  12496. } else {// should not happen
  12497. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  12498. }
  12499. //clean up used delays so we can give room for more
  12500. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12501. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  12502. sd->item_delay[i].tick = 0;
  12503. sd->item_delay[i].nameid = 0;
  12504. }
  12505. }
  12506. return 0;
  12507. }
  12508. /**
  12509. * Check if player has delay to reuse item
  12510. * @param sd Player
  12511. * @param id Item data
  12512. * @param tick Current tick
  12513. * @param n Item index in inventory
  12514. * @return 0: No delay, can consume item.
  12515. * 1: Has delay, cancel consumption.
  12516. **/
  12517. uint8 pc_itemcd_check(map_session_data *sd, struct item_data *id, t_tick tick, uint16 n) {
  12518. status_change *sc = nullptr;
  12519. nullpo_retr(0, sd);
  12520. nullpo_retr(0, id);
  12521. // Do normal delay assignment
  12522. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  12523. return pc_itemcd_add(sd, id, tick, n);
  12524. // Send reply of delay remains
  12525. if (sc->getSCE(id->delay.sc)) {
  12526. const struct TimerData *timer = get_timer(sc->getSCE(id->delay.sc)->timer);
  12527. clif_msg_value( *sd, MSI_ITEM_REUSE_LIMIT_SECOND, (int32)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99) );
  12528. return 1;
  12529. }
  12530. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  12531. return 0;
  12532. }
  12533. /**
  12534. * Status change data arrived from char-server
  12535. * @param sd: Player data
  12536. */
  12537. void pc_scdata_received(map_session_data *sd) {
  12538. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12539. clif_weight_limit( sd );
  12540. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) && pc_attendance_counter(sd) < 200 ){
  12541. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12542. }
  12543. sd->state.pc_loaded = true;
  12544. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12545. sd->state.connect_new = 1;
  12546. clif_parse_LoadEndAck(sd->fd, sd);
  12547. }
  12548. if (pc_iscarton(sd)) {
  12549. sd->cart_weight_max = 0; // Force a client refesh
  12550. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12551. }
  12552. if (sd->sc.getSCE(SC_SOULENERGY))
  12553. sd->soulball = sd->sc.getSCE(SC_SOULENERGY)->val1;
  12554. }
  12555. /**
  12556. * Check player account expiration time and rental item expirations
  12557. * @param sd: Player data
  12558. */
  12559. void pc_check_expiration(map_session_data *sd) {
  12560. #ifndef ENABLE_SC_SAVING
  12561. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12562. #endif
  12563. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12564. time_t exp_time = sd->expiration_time;
  12565. char tmpstr[1024];
  12566. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12567. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12568. pc_expire_check(sd);
  12569. }
  12570. }
  12571. TIMER_FUNC(pc_expiration_timer){
  12572. map_session_data *sd = map_id2sd(id);
  12573. if( !sd ) return 0;
  12574. sd->expiration_tid = INVALID_TIMER;
  12575. if( sd->fd )
  12576. clif_authfail_fd(sd->fd,10);
  12577. map_quit(sd);
  12578. return 0;
  12579. }
  12580. TIMER_FUNC(pc_autotrade_timer){
  12581. map_session_data *sd = map_id2sd(id);
  12582. if (!sd)
  12583. return 0;
  12584. sd->autotrade_tid = INVALID_TIMER;
  12585. if (sd->state.autotrade&2)
  12586. vending_reopen(*sd);
  12587. if (sd->state.autotrade&4)
  12588. buyingstore_reopen(sd);
  12589. if (!sd->vender_id && !sd->buyer_id) {
  12590. sd->state.autotrade = 0;
  12591. map_quit(sd);
  12592. }
  12593. return 0;
  12594. }
  12595. /* this timer exists only when a character with a expire timer > 24h is online */
  12596. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12597. TIMER_FUNC(pc_global_expiration_timer){
  12598. struct s_mapiterator* iter;
  12599. map_session_data* sd;
  12600. iter = mapit_getallusers();
  12601. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12602. if( sd->expiration_time )
  12603. pc_expire_check(sd);
  12604. mapit_free(iter);
  12605. return 0;
  12606. }
  12607. void pc_expire_check(map_session_data *sd) {
  12608. /* ongoing timer */
  12609. if( sd->expiration_tid != INVALID_TIMER )
  12610. return;
  12611. /* not within the next 24h, enable the global check */
  12612. if( sd->expiration_time > (time(nullptr) + ((60 * 60) * 24)) ) {
  12613. /* global check not running, enable */
  12614. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12615. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12616. return;
  12617. }
  12618. sd->expiration_tid = add_timer(gettick() + (uint32)(sd->expiration_time - time(nullptr)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12619. }
  12620. /**
  12621. * Deposit some money to bank
  12622. * @param sd
  12623. * @param money Amount of money to deposit
  12624. **/
  12625. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(map_session_data *sd, int32 money) {
  12626. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12627. return BDA_ERROR;
  12628. }
  12629. uint32 limit_check = money + sd->bank_vault;
  12630. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12631. return BDA_OVERFLOW;
  12632. } else if ( money > sd->status.zeny ) {
  12633. return BDA_NO_MONEY;
  12634. }
  12635. if( pc_payzeny(sd, money, LOG_TYPE_BANK) )
  12636. return BDA_NO_MONEY;
  12637. sd->bank_vault += money;
  12638. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12639. if( save_settings&CHARSAVE_BANK )
  12640. chrif_save(sd, CSAVE_NORMAL);
  12641. return BDA_SUCCESS;
  12642. }
  12643. /**
  12644. * Withdraw money from bank
  12645. * @param sd
  12646. * @param money Amount of money that will be withdrawn
  12647. **/
  12648. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(map_session_data *sd, int32 money) {
  12649. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12650. return BWA_UNKNOWN_ERROR;
  12651. }
  12652. uint32 limit_check = money + sd->status.zeny;
  12653. if( money <= 0 ) {
  12654. return BWA_UNKNOWN_ERROR;
  12655. } else if ( money > sd->bank_vault ) {
  12656. return BWA_NO_MONEY;
  12657. } else if ( limit_check > MAX_ZENY ) {
  12658. /* no official response for this scenario exists. */
  12659. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12660. return BWA_UNKNOWN_ERROR;
  12661. }
  12662. if( pc_getzeny(sd,money, LOG_TYPE_BANK) )
  12663. return BWA_NO_MONEY;
  12664. sd->bank_vault -= money;
  12665. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12666. if( save_settings&CHARSAVE_BANK )
  12667. chrif_save(sd, CSAVE_NORMAL);
  12668. return BWA_SUCCESS;
  12669. }
  12670. /**
  12671. * Clear Crimson Marker data from caster
  12672. * @param sd: Player
  12673. **/
  12674. void pc_crimson_marker_clear(map_session_data *sd) {
  12675. uint8 i;
  12676. if (!sd)
  12677. return;
  12678. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12679. struct block_list *bl = nullptr;
  12680. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12681. status_change_end(bl,SC_C_MARKER);
  12682. sd->c_marker[i] = 0;
  12683. }
  12684. }
  12685. /**
  12686. * Show version to player
  12687. * @param sd: Player
  12688. **/
  12689. void pc_show_version(map_session_data *sd) {
  12690. const char* svn = get_svn_revision();
  12691. char buf[CHAT_SIZE_MAX];
  12692. if( svn[0] != UNKNOWN_VERSION )
  12693. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12694. else {
  12695. const char* git = get_git_hash();
  12696. if( git[0] != UNKNOWN_VERSION )
  12697. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12698. else
  12699. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12700. }
  12701. clif_displaymessage(sd->fd,buf);
  12702. }
  12703. /**
  12704. * Run bonus_script on player
  12705. * @param sd
  12706. * @author [Cydh]
  12707. **/
  12708. void pc_bonus_script(map_session_data *sd) {
  12709. t_tick now = gettick();
  12710. struct linkdb_node *node = nullptr, *next = nullptr;
  12711. if (!sd || !(node = sd->bonus_script.head))
  12712. return;
  12713. while (node) {
  12714. struct s_bonus_script_entry *entry = nullptr;
  12715. next = node->next;
  12716. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12717. // Only start timer for new bonus_script
  12718. if (entry->tid == INVALID_TIMER) {
  12719. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12720. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12721. entry->tick += now;
  12722. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12723. }
  12724. if (entry->script)
  12725. run_script(entry->script, 0, sd->bl.id, 0);
  12726. else
  12727. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12728. }
  12729. node = next;
  12730. }
  12731. }
  12732. /**
  12733. * Add bonus_script to player
  12734. * @param sd Player
  12735. * @param script_str Script string
  12736. * @param dur Duration in ms
  12737. * @param icon EFST
  12738. * @param flag Flags @see enum e_bonus_script_flags
  12739. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12740. * @return New created entry pointer or nullptr if failed or nullptr if duplicate fail
  12741. * @author [Cydh]
  12742. **/
  12743. 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) {
  12744. struct script_code *script = nullptr;
  12745. struct linkdb_node *node = nullptr;
  12746. struct s_bonus_script_entry *entry = nullptr;
  12747. if (!sd)
  12748. return nullptr;
  12749. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12750. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12751. return nullptr;
  12752. }
  12753. // Duplication checks
  12754. if ((node = sd->bonus_script.head)) {
  12755. while (node) {
  12756. entry = (struct s_bonus_script_entry *)node->data;
  12757. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12758. t_tick newdur = gettick() + dur;
  12759. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12760. settick_timer(entry->tid, newdur);
  12761. script_free_code(script);
  12762. return nullptr;
  12763. }
  12764. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12765. break;
  12766. else { // No duplicate bonus
  12767. script_free_code(script);
  12768. return nullptr;
  12769. }
  12770. }
  12771. node = node->next;
  12772. }
  12773. }
  12774. CREATE(entry, struct s_bonus_script_entry, 1);
  12775. entry->script_buf = StringBuf_Malloc();
  12776. StringBuf_AppendStr(entry->script_buf, script_str);
  12777. entry->tid = INVALID_TIMER;
  12778. entry->flag = flag;
  12779. entry->icon = icon;
  12780. entry->tick = dur; // Use duration first, on run change to expire time
  12781. entry->type = type;
  12782. entry->script = script;
  12783. sd->bonus_script.count++;
  12784. return entry;
  12785. }
  12786. /**
  12787. * Remove bonus_script data from player
  12788. * @param sd: Target player
  12789. * @param list: Bonus script entry from player
  12790. * @author [Cydh]
  12791. **/
  12792. void pc_bonus_script_free_entry(map_session_data *sd, struct s_bonus_script_entry *entry) {
  12793. if (entry->tid != INVALID_TIMER)
  12794. delete_timer(entry->tid, pc_bonus_script_timer);
  12795. if (entry->script)
  12796. script_free_code(entry->script);
  12797. if (entry->script_buf)
  12798. StringBuf_Free(entry->script_buf);
  12799. if (sd) {
  12800. if (entry->icon != EFST_BLANK)
  12801. clif_status_load(&sd->bl, entry->icon, 0);
  12802. if (sd->bonus_script.count > 0)
  12803. sd->bonus_script.count--;
  12804. }
  12805. aFree(entry);
  12806. }
  12807. /**
  12808. * Do final process if no entry left
  12809. * @param sd
  12810. **/
  12811. static void inline pc_bonus_script_check_final(map_session_data *sd) {
  12812. if (sd->bonus_script.count == 0) {
  12813. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12814. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12815. linkdb_final(&sd->bonus_script.head);
  12816. }
  12817. }
  12818. /**
  12819. * Timer for bonus_script
  12820. * @param tid
  12821. * @param tick
  12822. * @param id
  12823. * @param data
  12824. * @author [Cydh]
  12825. **/
  12826. TIMER_FUNC(pc_bonus_script_timer){
  12827. map_session_data *sd;
  12828. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12829. sd = map_id2sd(id);
  12830. if (!sd) {
  12831. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12832. return 0;
  12833. }
  12834. if (tid == INVALID_TIMER)
  12835. return 0;
  12836. if (!sd->bonus_script.head || entry == nullptr) {
  12837. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12838. return 0;
  12839. }
  12840. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12841. pc_bonus_script_free_entry(sd, entry);
  12842. pc_bonus_script_check_final(sd);
  12843. status_calc_pc(sd,SCO_NONE);
  12844. return 0;
  12845. }
  12846. /**
  12847. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12848. * @param sd: Target player
  12849. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12850. * @author [Cydh]
  12851. **/
  12852. void pc_bonus_script_clear(map_session_data *sd, uint32 flag) {
  12853. struct linkdb_node *node = nullptr;
  12854. uint16 count = 0;
  12855. if (!sd || !(node = sd->bonus_script.head))
  12856. return;
  12857. while (node) {
  12858. struct linkdb_node *next = node->next;
  12859. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12860. if (entry && (
  12861. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12862. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12863. (flag&entry->flag) || // Matched flag
  12864. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12865. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12866. )
  12867. )
  12868. {
  12869. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12870. pc_bonus_script_free_entry(sd, entry);
  12871. count++;
  12872. }
  12873. node = next;
  12874. }
  12875. pc_bonus_script_check_final(sd);
  12876. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12877. status_calc_pc(sd,SCO_NONE);
  12878. }
  12879. /** [Cydh]
  12880. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12881. * @param sd: Target player
  12882. */
  12883. void pc_cell_basilica(map_session_data *sd) {
  12884. nullpo_retv(sd);
  12885. #ifdef RENEWAL
  12886. enum sc_type type = SC_BASILICA_CELL;
  12887. #else
  12888. enum sc_type type = SC_BASILICA;
  12889. #endif
  12890. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12891. if (sd->sc.getSCE(type))
  12892. status_change_end(&sd->bl, type);
  12893. }
  12894. else if (!sd->sc.getSCE(type))
  12895. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12896. }
  12897. /** [Cydh]
  12898. * Get maximum specified parameter for specified class
  12899. * @param sd: Player data
  12900. * @param param: Max parameter to check
  12901. * @return max_param
  12902. */
  12903. uint16 pc_maxparameter(map_session_data *sd, e_params param) {
  12904. nullpo_retr(0, sd);
  12905. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12906. if( job == nullptr || param == PARAM_MAX ){
  12907. return 0;
  12908. }
  12909. return job->max_param[param];
  12910. }
  12911. /**
  12912. * Get max ASPD for player based on Class
  12913. * @param sd Player
  12914. * @return ASPD
  12915. */
  12916. int16 pc_maxaspd(map_session_data *sd) {
  12917. nullpo_ret(sd);
  12918. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12919. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12920. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12921. battle_config.max_aspd ));
  12922. }
  12923. /**
  12924. * Calculates total item-group related bonuses for the given item
  12925. * @param sd Player
  12926. * @param nameid Item ID
  12927. * @return Heal rate
  12928. **/
  12929. int16 pc_get_itemgroup_bonus(map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12930. if (bonuses.empty())
  12931. return 0;
  12932. int16 bonus = 0;
  12933. for (const auto &it : bonuses) {
  12934. uint16 group_id = it.id;
  12935. if (group_id == 0)
  12936. continue;
  12937. if (itemdb_group.item_exists(group_id, nameid))
  12938. bonus += it.val;
  12939. }
  12940. return bonus;
  12941. }
  12942. /**
  12943. * Calculates total item-group related bonuses for the given item group
  12944. * @param sd Player
  12945. * @param group_id Item Group ID
  12946. * @return Heal rate
  12947. **/
  12948. int16 pc_get_itemgroup_bonus_group(map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12949. if (bonuses.empty())
  12950. return 0;
  12951. for (const auto &it : bonuses) {
  12952. if (it.id == group_id)
  12953. return it.val;
  12954. }
  12955. return 0;
  12956. }
  12957. /**
  12958. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12959. * @param eqi Item EQI of enum equip_index
  12960. * @param *equip_index Player's equip_index[]
  12961. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12962. * @return True if item in same inventory index, False if doesn't
  12963. */
  12964. bool pc_is_same_equip_index(enum equip_index eqi, int16 *equip_index, int16 index) {
  12965. if (index < 0 || index >= MAX_INVENTORY)
  12966. return true;
  12967. // Dual weapon checks
  12968. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12969. return true;
  12970. // Headgear with Mid & Low location
  12971. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12972. return true;
  12973. // Headgear with Top & Mid or Low location
  12974. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12975. return true;
  12976. // Headgear with Mid & Low location
  12977. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12978. return true;
  12979. // Headgear with Top & Mid or Low location
  12980. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12981. return true;
  12982. return false;
  12983. }
  12984. /**
  12985. * Generate Unique item ID for player
  12986. * @param sd : Player
  12987. * @return A generated Unique item ID
  12988. */
  12989. uint64 pc_generate_unique_id(map_session_data *sd) {
  12990. nullpo_ret(sd);
  12991. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12992. }
  12993. /**
  12994. * Validating skill from player after logged on
  12995. * @param sd
  12996. **/
  12997. void pc_validate_skill(map_session_data *sd) {
  12998. if (sd) {
  12999. uint16 i = 0, count = 0;
  13000. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  13001. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  13002. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  13003. for (i = 0; i < MAX_SKILL; i++) {
  13004. uint16 idx = 0;
  13005. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  13006. continue;
  13007. if ((idx = skill_get_index(tmp_skills[i].id))) {
  13008. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  13009. count++;
  13010. }
  13011. else
  13012. 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);
  13013. }
  13014. }
  13015. }
  13016. /**
  13017. * Show available NPC Quest / Event Icon Check [Kisuka]
  13018. * @param sd Player
  13019. **/
  13020. void pc_show_questinfo(map_session_data *sd) {
  13021. #if PACKETVER >= 20090218
  13022. nullpo_retv(sd);
  13023. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  13024. return;
  13025. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  13026. nullpo_retv(mapdata);
  13027. if (mapdata->qi_npc.empty())
  13028. return;
  13029. if (mapdata->qi_npc.size() != sd->qi_display.size())
  13030. return; // init was not called yet
  13031. for (int32 i = 0; i < mapdata->qi_npc.size(); i++) {
  13032. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  13033. if (!nd || nd->qi_data.empty())
  13034. continue;
  13035. bool show = false;
  13036. for (auto &qi : nd->qi_data) {
  13037. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  13038. show = true;
  13039. // Check if need to be displayed
  13040. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  13041. sd->qi_display[i].is_active = true;
  13042. sd->qi_display[i].icon = qi->icon;
  13043. sd->qi_display[i].color = qi->color;
  13044. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  13045. }
  13046. break;
  13047. }
  13048. }
  13049. if (show == false) {
  13050. // Check if need to be hide
  13051. if (sd->qi_display[i].is_active) {
  13052. sd->qi_display[i].is_active = false;
  13053. sd->qi_display[i].icon = QTYPE_NONE;
  13054. sd->qi_display[i].color = QMARK_NONE;
  13055. #if PACKETVER >= 20120410
  13056. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  13057. #else
  13058. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  13059. #endif
  13060. }
  13061. }
  13062. }
  13063. #endif
  13064. }
  13065. /**
  13066. * Reinit the questinfo for player when changing map
  13067. * @param sd Player
  13068. **/
  13069. void pc_show_questinfo_reinit(map_session_data *sd) {
  13070. #if PACKETVER >= 20090218
  13071. nullpo_retv(sd);
  13072. sd->qi_display.clear();
  13073. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  13074. return;
  13075. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  13076. nullpo_retv(mapdata);
  13077. if (mapdata->qi_npc.empty())
  13078. return;
  13079. sd->qi_display.reserve( mapdata->qi_npc.size() );
  13080. for( int32 i = 0; i < mapdata->qi_npc.size(); i++ ){
  13081. sd->qi_display.push_back( s_qi_display() );
  13082. }
  13083. #endif
  13084. }
  13085. /**
  13086. * Check if a job is allowed to enter the map
  13087. * @param jobid Job ID see enum e_job or sd->status.class_
  13088. * @param m ID -an index- for direct indexing map[] array
  13089. * @return 1 if job is allowed, 0 otherwise
  13090. **/
  13091. bool pc_job_can_entermap(enum e_job jobid, int32 m, int32 group_lv) {
  13092. // Map is other map server.
  13093. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  13094. if (m < 0)
  13095. return true;
  13096. struct map_data *mapdata = map_getmapdata(m);
  13097. if (!mapdata->cell)
  13098. return false;
  13099. if (!pcdb_checkid(jobid))
  13100. return false;
  13101. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  13102. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  13103. return true;
  13104. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  13105. (job->noenter_map.zone&2 && mapdata->getMapFlag(MF_PVP)) || // PVP
  13106. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  13107. (job->noenter_map.zone&8 && mapdata->getMapFlag(MF_BATTLEGROUND)) || // Battleground
  13108. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  13109. (job->noenter_map.zone&(mapdata->zone) && mapdata->getMapFlag(MF_RESTRICTED)) // Zone restriction
  13110. )
  13111. return false;
  13112. return true;
  13113. }
  13114. /**
  13115. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  13116. * @param sd
  13117. **/
  13118. void pc_set_costume_view(map_session_data *sd) {
  13119. int32 i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  13120. struct item_data *id = nullptr;
  13121. nullpo_retv(sd);
  13122. head_low = sd->status.head_bottom;
  13123. head_mid = sd->status.head_mid;
  13124. head_top = sd->status.head_top;
  13125. robe = sd->status.robe;
  13126. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  13127. //Added check to prevent sending the same look on multiple slots ->
  13128. //causes client to redraw item on top of itself. (suggested by Lupus)
  13129. // Normal headgear checks
  13130. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13131. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  13132. sd->status.head_bottom = id->look;
  13133. else
  13134. sd->status.head_bottom = 0;
  13135. }
  13136. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13137. if (!(id->equip&(EQP_HEAD_TOP)))
  13138. sd->status.head_mid = id->look;
  13139. else
  13140. sd->status.head_mid = 0;
  13141. }
  13142. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13143. sd->status.head_top = id->look;
  13144. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13145. sd->status.robe = id->look;
  13146. // Costumes check
  13147. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  13148. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13149. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  13150. sd->status.head_bottom = id->look;
  13151. else
  13152. sd->status.head_bottom = 0;
  13153. }
  13154. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13155. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  13156. sd->status.head_mid = id->look;
  13157. else
  13158. sd->status.head_mid = 0;
  13159. }
  13160. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13161. sd->status.head_top = id->look;
  13162. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13163. sd->status.robe = id->look;
  13164. }
  13165. if (sd->setlook_head_bottom)
  13166. sd->status.head_bottom = sd->setlook_head_bottom;
  13167. if (sd->setlook_head_mid)
  13168. sd->status.head_mid = sd->setlook_head_mid;
  13169. if (sd->setlook_head_top)
  13170. sd->status.head_top = sd->setlook_head_top;
  13171. if (sd->setlook_robe)
  13172. sd->status.robe = sd->setlook_robe;
  13173. if (head_low != sd->status.head_bottom)
  13174. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  13175. if (head_mid != sd->status.head_mid)
  13176. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  13177. if (head_top != sd->status.head_top)
  13178. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  13179. if (robe != sd->status.robe)
  13180. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  13181. }
  13182. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  13183. uint32 date = date_get(DT_YYYYMMDD);
  13184. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  13185. std::shared_ptr<s_attendance_period> period = pair.second;
  13186. if( period->start <= date && period->end >= date ){
  13187. return period;
  13188. }
  13189. }
  13190. return nullptr;
  13191. }
  13192. bool pc_attendance_enabled(){
  13193. // Check if the attendance feature is disabled
  13194. if( !battle_config.feature_attendance ){
  13195. return false;
  13196. }
  13197. // Check if there is a running attendance period
  13198. return pc_attendance_period() != nullptr;
  13199. }
  13200. static inline bool pc_attendance_rewarded_today( map_session_data* sd ){
  13201. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  13202. }
  13203. int32 pc_attendance_counter( map_session_data* sd ){
  13204. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13205. // No running attendance period
  13206. if( period == nullptr ){
  13207. return 0;
  13208. }
  13209. // Get the counter for the current period
  13210. int32 counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13211. // Check if we have a remaining counter from a previous period
  13212. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  13213. // Reset the counter to zero
  13214. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  13215. return 0;
  13216. }
  13217. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  13218. }
  13219. void pc_attendance_claim_reward( map_session_data* sd ){
  13220. // If the user's group does not have the permission
  13221. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  13222. return;
  13223. }
  13224. // Check if the attendance feature is disabled
  13225. if( !pc_attendance_enabled() ){
  13226. return;
  13227. }
  13228. // Check if the user already got his reward today
  13229. if( pc_attendance_rewarded_today( sd ) ){
  13230. return;
  13231. }
  13232. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13233. attendance_counter += 1;
  13234. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13235. if( period == nullptr ){
  13236. return;
  13237. }
  13238. if( period->rewards.size() < attendance_counter ){
  13239. return;
  13240. }
  13241. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  13242. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  13243. if( save_settings&CHARSAVE_ATTENDANCE )
  13244. chrif_save(sd, CSAVE_NORMAL);
  13245. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  13246. struct mail_message msg;
  13247. memset( &msg, 0, sizeof( struct mail_message ) );
  13248. msg.dest_id = sd->status.char_id;
  13249. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  13250. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  13251. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  13252. msg.item[0].nameid = reward->item_id;
  13253. msg.item[0].amount = reward->amount;
  13254. msg.item[0].identify = 1;
  13255. msg.status = MAIL_NEW;
  13256. msg.type = MAIL_INBOX_NORMAL;
  13257. msg.timestamp = time(nullptr);
  13258. intif_Mail_send(0, &msg);
  13259. clif_attendence_response( sd, attendance_counter );
  13260. }
  13261. /**
  13262. * Send a player to jail and determine the location to send in jail.
  13263. * @param sd: Player data
  13264. * @param duration: Duration in minutes (default INT_MAX = infinite)
  13265. */
  13266. void pc_jail(map_session_data &sd, int32 duration) {
  13267. uint16 m_index = mapindex_name2id(MAP_JAIL);
  13268. int16 x, y;
  13269. switch (rnd() % 2) { // Jail Locations
  13270. case 0: // Jail #1
  13271. x = 49;
  13272. y = 75;
  13273. break;
  13274. default: // Default Jail
  13275. x = 24;
  13276. y = 75;
  13277. break;
  13278. }
  13279. duration = i32max(0, duration); // Can't be less than 0 seconds.
  13280. // If duration > 0 then triggered via jailfor which checks every minute.
  13281. // If duration == INT_MAX then triggered via jail for infinite duration.
  13282. // If duration == 0 then triggered via unjail and end status.
  13283. if (duration > 0)
  13284. sc_start4(nullptr, &sd.bl, SC_JAILED, 100, duration, m_index, x, y, 60000);
  13285. else
  13286. status_change_end(&sd.bl, SC_JAILED);
  13287. }
  13288. /**
  13289. * Determine the punishment type when failing macro checks.
  13290. * @param sd: Player data
  13291. * @param stype: Macro detection status type (for banning)
  13292. */
  13293. static void pc_macro_punishment(map_session_data &sd, e_macro_detect_status stype) {
  13294. int32 duration = 0;
  13295. // Determine if there's a unique duration
  13296. if (battle_config.macro_detection_punishment_time > 0) {
  13297. char time[13];
  13298. safesnprintf(time, 13, "+%dnm", battle_config.macro_detection_punishment_time);
  13299. duration = static_cast<int32>(solve_time(time));
  13300. }
  13301. // Delete the timer
  13302. if (sd.macro_detect.timer != INVALID_TIMER)
  13303. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13304. int32 reporter_aid = sd.macro_detect.reporter_aid;
  13305. // Clear the macro detect data
  13306. sd.macro_detect = {};
  13307. sd.macro_detect.timer = INVALID_TIMER;
  13308. // Unblock all actions for the player
  13309. sd.state.block_action &= ~PCBLOCK_ALL;
  13310. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13311. if (battle_config.macro_detection_punishment == 0) { // Ban
  13312. clif_macro_detector_status(sd, stype);
  13313. chrif_req_login_operation(reporter_aid, sd.status.name, (duration == 0 ? CHRIF_OP_LOGIN_BLOCK : CHRIF_OP_LOGIN_BAN), duration, 0, 0);
  13314. } else { // Jail
  13315. // Send success to close the window without closing the client
  13316. clif_macro_detector_status(sd, MCD_GOOD);
  13317. pc_jail(sd, (duration == 0 ? INT_MAX : duration / 60));
  13318. }
  13319. }
  13320. /**
  13321. * Save a captcha image to memory via /macro_register.
  13322. * @param sd: Player data
  13323. * @param image_size: Captcha image size
  13324. * @param captcha_answer: Answer to captcha
  13325. */
  13326. void pc_macro_captcha_register(map_session_data &sd, uint16 image_size, const char captcha_answer[CAPTCHA_ANSWER_SIZE_MAX]) {
  13327. nullpo_retv(captcha_answer);
  13328. sd.captcha_upload.cd = nullptr;
  13329. sd.captcha_upload.upload_size = 0;
  13330. if (strlen(captcha_answer) < CAPTCHA_ANSWER_SIZE_MIN || image_size == 0 || image_size > CAPTCHA_BMP_SIZE) {
  13331. clif_captcha_upload_request(sd); // Notify client of failure.
  13332. return;
  13333. }
  13334. std::shared_ptr<s_captcha_data> cd = std::make_shared<s_captcha_data>();
  13335. sd.captcha_upload.cd = cd;
  13336. cd->image_size = image_size;
  13337. safestrncpy(cd->captcha_answer, captcha_answer, sizeof(cd->captcha_answer));
  13338. memset(cd->image_data, 0, sizeof(cd->image_data));
  13339. // Request the image data from the client.
  13340. clif_captcha_upload_request(sd);
  13341. }
  13342. /**
  13343. * Save captcha image to server.
  13344. * @param sd: Player data
  13345. * @param captcha_key: Captcha ID
  13346. * @param upload_size: Captcha size
  13347. * @param upload_data: Image data
  13348. */
  13349. void pc_macro_captcha_register_upload(map_session_data &sd, uint16 upload_size, const char *upload_data) {
  13350. nullpo_retv(upload_data);
  13351. memcpy(&sd.captcha_upload.cd->image_data[sd.captcha_upload.upload_size], upload_data, upload_size);
  13352. sd.captcha_upload.upload_size += upload_size;
  13353. // Notify that the image finished uploading.
  13354. if (sd.captcha_upload.upload_size == sd.captcha_upload.cd->image_size) {
  13355. // Tell the client that the upload was finished
  13356. clif_captcha_upload_end(sd);
  13357. // Look for a free key
  13358. uint16 index;
  13359. for (index = 0; index < UINT16_MAX; index++) {
  13360. if (!captcha_db.exists(index)) {
  13361. break;
  13362. }
  13363. }
  13364. if (index == UINT16_MAX) {
  13365. // no free key found...
  13366. sd.captcha_upload.cd = nullptr;
  13367. sd.captcha_upload.upload_size = 0;
  13368. return;
  13369. }
  13370. captcha_db.put(index, sd.captcha_upload.cd);
  13371. sd.captcha_upload.cd = nullptr;
  13372. sd.captcha_upload.upload_size = 0;
  13373. // TODO: write YAML and BMP file?
  13374. }
  13375. }
  13376. /**
  13377. * Timer attached to target player with attempts to confirm captcha.
  13378. */
  13379. TIMER_FUNC(pc_macro_detector_timeout) {
  13380. map_session_data *sd = map_id2sd(id);
  13381. nullpo_ret(sd);
  13382. // Remove the current timer
  13383. sd->macro_detect.timer = INVALID_TIMER;
  13384. // Deduct an answering attempt
  13385. sd->macro_detect.retry -= 1;
  13386. if (sd->macro_detect.retry == 0) {
  13387. // All attempts have been exhausted, punish the user
  13388. pc_macro_punishment(*sd, MCD_TIMEOUT);
  13389. } else {
  13390. // Update the client
  13391. clif_macro_detector_request_show(*sd);
  13392. // Start a new timer
  13393. sd->macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd->bl.id, 0);
  13394. }
  13395. return 0;
  13396. }
  13397. /**
  13398. * Check player's captcha answer.
  13399. * @param sd: Player data
  13400. * @param captcha_answer: Captcha answer entered by player
  13401. */
  13402. void pc_macro_detector_process_answer(map_session_data &sd, const char captcha_answer[CAPTCHA_ANSWER_SIZE_MAX]) {
  13403. nullpo_retv(captcha_answer);
  13404. const std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  13405. // Has no captcha request
  13406. if (cd == nullptr) {
  13407. return;
  13408. }
  13409. // Correct answer
  13410. if (strcmp(captcha_answer, cd->captcha_answer) == 0) {
  13411. // Delete the timer
  13412. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13413. // Clear the macro detect data
  13414. sd.macro_detect = {};
  13415. sd.macro_detect.timer = INVALID_TIMER;
  13416. // Unblock all actions for the player
  13417. sd.state.block_action &= ~PCBLOCK_ALL;
  13418. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13419. // Assign temporary macro variable to check failures
  13420. pc_setreg(&sd, add_str("@captcha_retries"), battle_config.macro_detection_retry - sd.macro_detect.retry);
  13421. // Grant bonuses via script
  13422. if( cd->bonus_script != nullptr ){
  13423. run_script(cd->bonus_script, 0, sd.bl.id, fake_nd->bl.id);
  13424. }
  13425. // Notify the client
  13426. clif_macro_detector_status(sd, MCD_GOOD);
  13427. } else {
  13428. // Deduct an answering attempt
  13429. sd.macro_detect.retry -= 1;
  13430. // All attempts have been exhausted, punish the user
  13431. if (sd.macro_detect.retry <= 0) {
  13432. pc_macro_punishment(sd, MCD_INCORRECT);
  13433. return;
  13434. }
  13435. // Incorrect response, update the client
  13436. clif_macro_detector_request_show(sd);
  13437. // Reset the timer
  13438. addtick_timer(sd.macro_detect.timer, gettick() + battle_config.macro_detection_timeout);
  13439. }
  13440. }
  13441. /**
  13442. * Determine if a player tries to log out during a captcha check.
  13443. * @param sd: Player data
  13444. */
  13445. void pc_macro_detector_disconnect(map_session_data &sd) {
  13446. // Delete the timeout timer
  13447. if (sd.macro_detect.timer != INVALID_TIMER) {
  13448. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13449. sd.macro_detect.timer = INVALID_TIMER;
  13450. }
  13451. // If the player disconnects before clearing the challenge the player is punished.
  13452. if (sd.macro_detect.retry != 0)
  13453. pc_macro_punishment(sd, MCD_TIMEOUT);
  13454. }
  13455. /**
  13456. * Save a list of players from an area select via /macro_detector.
  13457. */
  13458. int32 pc_macro_reporter_area_select_sub(block_list *bl, va_list ap) {
  13459. nullpo_retr(0, bl);
  13460. if (bl->type != BL_PC)
  13461. return 0;
  13462. std::vector<uint32> *aid_list = va_arg(ap, std::vector<uint32> *);
  13463. nullpo_ret(aid_list);
  13464. aid_list->push_back(bl->id);
  13465. return 0;
  13466. }
  13467. /**
  13468. * Area select via /macro_detector.
  13469. * @param sd: Player data
  13470. * @param x: X location
  13471. * @param y: Y location
  13472. * @param radius: Area
  13473. */
  13474. void pc_macro_reporter_area_select(map_session_data &sd, const int16 x, const int16 y, const int8 radius) {
  13475. std::vector<uint32> aid_list;
  13476. map_foreachinarea(pc_macro_reporter_area_select_sub, sd.bl.m, x - radius, y - radius, x + radius, y + radius, BL_PC, &aid_list);
  13477. clif_macro_reporter_select(sd, aid_list);
  13478. }
  13479. /**
  13480. * Send out captcha check to player.
  13481. * @param sd: Target player data
  13482. * @param reporter_account_id: Account ID of reporter
  13483. */
  13484. void pc_macro_reporter_process(map_session_data &sd, int32 reporter_account_id) {
  13485. if (captcha_db.empty())
  13486. return;
  13487. // Pick a random image from the database.
  13488. const std::shared_ptr<s_captcha_data> cd = captcha_db.random();
  13489. // Set macro detection data.
  13490. sd.macro_detect.cd = cd;
  13491. sd.macro_detect.reporter_aid = reporter_account_id;
  13492. sd.macro_detect.retry = battle_config.macro_detection_retry;
  13493. // Block all actions for the target player.
  13494. sd.state.block_action |= (PCBLOCK_ALL | PCBLOCK_IMMUNE);
  13495. // Open macro detect client side.
  13496. clif_macro_detector_request(sd);
  13497. // Start the timeout timer.
  13498. sd.macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd.bl.id, 0);
  13499. }
  13500. /**
  13501. * Parse a BMP image to memory.
  13502. * @param filepath: Image file location
  13503. * @param cd: Captcha data
  13504. */
  13505. bool pc_macro_read_captcha_db_loadbmp(const std::string &filepath, std::shared_ptr<s_captcha_data> cd) {
  13506. if (cd == nullptr)
  13507. return false;
  13508. FILE *fp = fopen(filepath.c_str(), "rb");
  13509. if (fp == nullptr) {
  13510. ShowError("%s: Failed to open file \"%s\"\n", __func__, filepath.c_str());
  13511. return false;
  13512. }
  13513. // Load the file data and verify magic
  13514. char bmp_data[CAPTCHA_BMP_SIZE];
  13515. if (fread(bmp_data, CAPTCHA_BMP_SIZE, 1, fp) != 1) {
  13516. ShowError("%s: Failed to read data from \"%s\"\n", __func__, filepath.c_str());
  13517. fclose(fp);
  13518. return false;
  13519. }
  13520. fclose(fp);
  13521. if (bmp_data[0] != 'B' || bmp_data[1] != 'M') {
  13522. ShowError("%s: Invalid BMP file header given at \"%s\"\n", __func__, filepath.c_str());
  13523. return false;
  13524. }
  13525. // Compress the data into the destination
  13526. unsigned long com_size = sizeof(cd->image_data);
  13527. encode_zip(cd->image_data, &com_size, bmp_data, CAPTCHA_BMP_SIZE);
  13528. cd->image_size = static_cast<int16>(com_size);
  13529. return true;
  13530. }
  13531. const std::string CaptchaDatabase::getDefaultLocation() {
  13532. return std::string(db_path) + "/captcha_db.yml";
  13533. }
  13534. /**
  13535. * Reads and parses an entry from the captcha_db.
  13536. * @param node: YAML node containing the entry.
  13537. * @return count of successfully parsed rows
  13538. */
  13539. uint64 CaptchaDatabase::parseBodyNode(const ryml::NodeRef &node) {
  13540. uint16 index;
  13541. if (!this->asUInt16(node, "Id", index))
  13542. return 0;
  13543. std::shared_ptr<s_captcha_data> cd = captcha_db.find(index);
  13544. bool exists = cd != nullptr;
  13545. if (!exists) {
  13546. if (!this->nodesExist(node, { "Filename", "Answer" }))
  13547. return 0;
  13548. cd = std::make_shared<s_captcha_data>();
  13549. cd->index = index;
  13550. }
  13551. if (this->nodeExists(node, "Filename")) {
  13552. std::string filename;
  13553. if (!this->asString(node, "Filename", filename))
  13554. return 0;
  13555. if (!pc_macro_read_captcha_db_loadbmp(filename, cd)) {
  13556. this->invalidWarning(node["Filename"], "Failed to parse BMP image, skipping...\n");
  13557. return 0;
  13558. }
  13559. }
  13560. if (this->nodeExists(node, "Answer")) {
  13561. std::string answer;
  13562. if (!this->asString(node, "Answer", answer))
  13563. return 0;
  13564. if (answer.length() < CAPTCHA_ANSWER_SIZE_MIN || answer.length() > CAPTCHA_ANSWER_SIZE_MAX) {
  13565. this->invalidWarning(node["Answer"], "The captcha answer must be between 4~%d characters, skipping...\n", CAPTCHA_ANSWER_SIZE_MAX);
  13566. return 0;
  13567. }
  13568. safestrncpy(cd->captcha_answer, answer.c_str(), sizeof(cd->captcha_answer));
  13569. }
  13570. if (this->nodeExists(node, "Bonus")) {
  13571. if( node["Bonus"].val_is_null() ){
  13572. if( cd->bonus_script ){
  13573. script_free_code( cd->bonus_script );
  13574. }
  13575. cd->bonus_script = nullptr;
  13576. }else{
  13577. std::string script;
  13578. if (!this->asString(node, "Bonus", script)) {
  13579. return 0;
  13580. }
  13581. if (cd->bonus_script) {
  13582. script_free_code(cd->bonus_script);
  13583. cd->bonus_script = nullptr;
  13584. }
  13585. cd->bonus_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Bonus"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13586. }
  13587. } else {
  13588. if (!exists)
  13589. 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);
  13590. }
  13591. if (!exists)
  13592. captcha_db.put(index, cd);
  13593. return 1;
  13594. }
  13595. /*==========================================
  13596. * pc Init/Terminate
  13597. *------------------------------------------*/
  13598. void do_final_pc(void) {
  13599. db_destroy(itemcd_db);
  13600. do_final_pc_groups();
  13601. ers_destroy(pc_sc_display_ers);
  13602. ers_destroy(num_reg_ers);
  13603. ers_destroy(str_reg_ers);
  13604. attendance_db.clear();
  13605. reputation_db.clear();
  13606. #ifdef MAP_GENERATOR
  13607. reputationgroup_db.clear();
  13608. #endif
  13609. penalty_db.clear();
  13610. captcha_db.clear();
  13611. }
  13612. void do_init_pc(void) {
  13613. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  13614. pc_readdb();
  13615. pc_read_motd(); // Read MOTD [Valaris]
  13616. attendance_db.load();
  13617. reputation_db.load();
  13618. #ifdef MAP_GENERATOR
  13619. reputationgroup_db.load();
  13620. #endif
  13621. captcha_db.load();
  13622. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  13623. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  13624. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  13625. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  13626. add_timer_func_list(pc_autosave, "pc_autosave");
  13627. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  13628. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  13629. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  13630. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  13631. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  13632. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  13633. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  13634. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  13635. add_timer_func_list(pc_macro_detector_timeout, "pc_macro_detector_timeout");
  13636. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  13637. // 0=day, 1=night [Yor]
  13638. night_flag = battle_config.night_at_start ? 1 : 0;
  13639. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  13640. int32 day_duration = battle_config.day_duration;
  13641. int32 night_duration = battle_config.night_duration;
  13642. // add night/day timer [Yor]
  13643. add_timer_func_list(map_day_timer, "map_day_timer");
  13644. add_timer_func_list(map_night_timer, "map_night_timer");
  13645. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  13646. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  13647. }
  13648. do_init_pc_groups();
  13649. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  13650. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13651. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13652. ers_chunk_size(pc_sc_display_ers, 150);
  13653. ers_chunk_size(num_reg_ers, 300);
  13654. ers_chunk_size(str_reg_ers, 50);
  13655. }