pc.cpp 482 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993
  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_ADD_VARIABLECAST:
  3728. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3729. sd->bonus.add_varcast += val;
  3730. break;
  3731. #ifdef RENEWAL_CAST
  3732. case SP_FIXCASTRATE:
  3733. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3734. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3735. break;
  3736. case SP_ADD_FIXEDCAST:
  3737. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3738. sd->bonus.add_fixcast += val;
  3739. break;
  3740. case SP_CASTRATE:
  3741. case SP_VARCASTRATE:
  3742. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3743. sd->bonus.varcastrate -= val;
  3744. break;
  3745. #else
  3746. case SP_ADD_FIXEDCAST:
  3747. case SP_FIXCASTRATE:
  3748. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3749. break;
  3750. case SP_VARCASTRATE:
  3751. case SP_CASTRATE:
  3752. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3753. sd->castrate += val;
  3754. break;
  3755. #endif
  3756. case SP_ADDMAXWEIGHT:
  3757. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3758. sd->add_max_weight += val;
  3759. break;
  3760. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3761. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3762. break;
  3763. case SP_ABSORB_DMG_MAXHP2:
  3764. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3765. break;
  3766. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3767. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3768. sd->bonus.critical_rangeatk += val*10;
  3769. else
  3770. sd->bonus.arrow_cri += val*10;
  3771. break;
  3772. case SP_WEAPON_ATK_RATE:
  3773. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3774. sd->bonus.weapon_atk_rate += val;
  3775. break;
  3776. case SP_WEAPON_MATK_RATE:
  3777. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3778. sd->bonus.weapon_matk_rate += val;
  3779. break;
  3780. case SP_NO_MADO_FUEL:
  3781. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3782. sd->special_state.no_mado_fuel = 1;
  3783. break;
  3784. case SP_NO_WALK_DELAY:
  3785. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3786. sd->special_state.no_walk_delay = 1;
  3787. break;
  3788. case SP_ADD_ITEM_SPHEAL_RATE:
  3789. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3790. sd->bonus.itemsphealrate2 += val;
  3791. break;
  3792. default:
  3793. if (current_equip_combo_pos > 0) {
  3794. 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);
  3795. }
  3796. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3797. 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);
  3798. }
  3799. else {
  3800. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3801. }
  3802. break;
  3803. }
  3804. }
  3805. /*==========================================
  3806. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3807. * format: bonus2 bBonusName,type2,val;
  3808. * @param sd
  3809. * @param type Bonus type used by bBonusName
  3810. * @param type2
  3811. * @param val Value that usually for rate or fixed value
  3812. *------------------------------------------*/
  3813. void pc_bonus2(map_session_data *sd,int32 type,int32 type2,int32 val)
  3814. {
  3815. nullpo_retv(sd);
  3816. switch(type){
  3817. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3818. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3819. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3820. sd->right_weapon.addele[type2]+=val;
  3821. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3822. sd->left_weapon.addele[type2]+=val;
  3823. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3824. sd->indexed_bonus.arrow_addele[type2]+=val;
  3825. break;
  3826. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3827. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3828. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3829. sd->right_weapon.addrace[type2]+=val;
  3830. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3831. sd->left_weapon.addrace[type2]+=val;
  3832. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3833. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3834. break;
  3835. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3836. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3837. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3838. sd->right_weapon.addclass[type2]+=val;
  3839. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3840. sd->left_weapon.addclass[type2]+=val;
  3841. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3842. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3843. break;
  3844. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3845. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3846. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3847. sd->right_weapon.addsize[type2]+=val;
  3848. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3849. sd->left_weapon.addsize[type2]+=val;
  3850. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3851. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3852. break;
  3853. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3854. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3855. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3856. sd->indexed_bonus.subele_script[type2] += val;
  3857. break;
  3858. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3859. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3860. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3861. sd->indexed_bonus.subrace[type2]+=val;
  3862. break;
  3863. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3864. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3865. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3866. sd->indexed_bonus.subclass[type2]+=val;
  3867. break;
  3868. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3869. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3870. 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);
  3871. break;
  3872. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3873. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  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, ATF_SELF, 0);
  3875. break;
  3876. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3877. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3878. break;
  3879. if (sd->reseff.size() == MAX_PC_BONUS) {
  3880. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3881. break;
  3882. }
  3883. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3884. break;
  3885. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3886. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3887. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3888. sd->indexed_bonus.magic_addele_script[type2] += val;
  3889. break;
  3890. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3891. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3892. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3893. sd->indexed_bonus.magic_addrace[type2]+=val;
  3894. break;
  3895. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3896. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3897. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3898. sd->indexed_bonus.magic_addclass[type2]+=val;
  3899. break;
  3900. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3901. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3902. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3903. sd->indexed_bonus.magic_addsize[type2]+=val;
  3904. break;
  3905. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3906. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3907. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3908. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3909. break;
  3910. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3911. struct weapon_data *wd = (sd->state.lr_flag != LR_FLAG_NONE ? &sd->left_weapon : &sd->right_weapon);
  3912. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3913. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3914. break;
  3915. }
  3916. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3917. }
  3918. break;
  3919. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3920. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3921. break;
  3922. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3923. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3924. break;
  3925. }
  3926. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3927. break;
  3928. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3929. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3930. break;
  3931. if (sd->add_def.size() == MAX_PC_BONUS) {
  3932. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3933. break;
  3934. }
  3935. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3936. break;
  3937. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3938. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3939. break;
  3940. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3941. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3942. break;
  3943. }
  3944. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3945. break;
  3946. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3947. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3948. sd->right_weapon.hp_drain_rate.rate += type2;
  3949. sd->right_weapon.hp_drain_rate.per += val;
  3950. }
  3951. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3952. sd->left_weapon.hp_drain_rate.rate += type2;
  3953. sd->left_weapon.hp_drain_rate.per += val;
  3954. }
  3955. break;
  3956. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3957. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3958. sd->right_weapon.sp_drain_rate.rate += type2;
  3959. sd->right_weapon.sp_drain_rate.per += val;
  3960. }
  3961. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3962. sd->left_weapon.sp_drain_rate.rate += type2;
  3963. sd->left_weapon.sp_drain_rate.per += val;
  3964. }
  3965. break;
  3966. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3967. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3968. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3969. }
  3970. break;
  3971. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3972. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3973. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3974. }
  3975. break;
  3976. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3977. if (sd->state.lr_flag != LR_FLAG_ARROW && sd->bonus.get_zeny_rate < val) {
  3978. sd->bonus.get_zeny_rate = val;
  3979. sd->bonus.get_zeny_num = type2;
  3980. }
  3981. break;
  3982. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3983. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3984. sd->bonus.get_zeny_rate += val;
  3985. sd->bonus.get_zeny_num += type2;
  3986. }
  3987. break;
  3988. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3989. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3990. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3991. break;
  3992. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3993. sd->special_state.bonus_coma = 1;
  3994. break;
  3995. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3996. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3997. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3998. break;
  3999. sd->indexed_bonus.weapon_coma_race[type2] += val;
  4000. sd->special_state.bonus_coma = 1;
  4001. break;
  4002. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  4003. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  4004. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4005. break;
  4006. sd->indexed_bonus.weapon_coma_class[type2] += val;
  4007. sd->special_state.bonus_coma = 1;
  4008. break;
  4009. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  4010. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4011. sd->indexed_bonus.weapon_atk[type2]+=val;
  4012. break;
  4013. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  4014. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4015. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  4016. break;
  4017. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  4018. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  4019. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4020. sd->indexed_bonus.critaddrace[type2] += val*10;
  4021. break;
  4022. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  4023. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4024. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4025. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  4026. break;
  4027. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  4028. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4029. break;
  4030. if (sd->skillatk.size() == MAX_PC_BONUS) {
  4031. 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);
  4032. break;
  4033. }
  4034. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  4035. break;
  4036. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  4037. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4038. break;
  4039. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4040. 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);
  4041. break;
  4042. }
  4043. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  4044. break;
  4045. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  4046. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4047. break;
  4048. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4049. 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);
  4050. break;
  4051. }
  4052. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  4053. break;
  4054. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  4055. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4056. break;
  4057. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4058. 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);
  4059. break;
  4060. }
  4061. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  4062. break;
  4063. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  4064. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4065. sd->hp_loss.value = type2;
  4066. sd->hp_loss.rate = val;
  4067. }
  4068. break;
  4069. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  4070. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4071. sd->hp_regen.value = type2;
  4072. sd->hp_regen.rate = val;
  4073. }
  4074. break;
  4075. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  4076. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4077. sd->percent_hp_regen.value = type2;
  4078. sd->percent_hp_regen.rate = val;
  4079. }
  4080. break;
  4081. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  4082. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4083. sd->percent_sp_regen.value = type2;
  4084. sd->percent_sp_regen.rate = val;
  4085. }
  4086. break;
  4087. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  4088. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  4089. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4090. sd->right_weapon.addrace2[type2] += val;
  4091. else
  4092. sd->left_weapon.addrace2[type2] += val;
  4093. break;
  4094. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  4095. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  4096. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4097. sd->indexed_bonus.subsize[type2]+=val;
  4098. break;
  4099. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  4100. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  4101. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4102. sd->indexed_bonus.weapon_subsize[type2] += val;
  4103. break;
  4104. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  4105. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  4106. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4107. sd->indexed_bonus.magic_subsize[type2]+=val;
  4108. break;
  4109. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  4110. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  4111. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4112. sd->indexed_bonus.subrace2[type2]+=val;
  4113. break;
  4114. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  4115. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4116. break;
  4117. if( !item_db.exists( type2 ) ){
  4118. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  4119. break;
  4120. }
  4121. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  4122. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4123. break;
  4124. }
  4125. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  4126. break;
  4127. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  4128. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4129. break;
  4130. if (!type2 || !itemdb_group.exists(type2)) {
  4131. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  4132. break;
  4133. }
  4134. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  4135. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4136. break;
  4137. }
  4138. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  4139. break;
  4140. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  4141. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  4142. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4143. sd->indexed_bonus.expaddrace[type2]+=val;
  4144. break;
  4145. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  4146. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  4147. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4148. sd->indexed_bonus.expaddclass[type2]+=val;
  4149. break;
  4150. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  4151. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  4152. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4153. sd->indexed_bonus.sp_gain_race[type2]+=val;
  4154. break;
  4155. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  4156. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4157. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  4158. break;
  4159. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  4160. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4161. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  4162. break;
  4163. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  4164. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4165. sd->sp_loss.value = type2;
  4166. sd->sp_loss.rate = val;
  4167. }
  4168. break;
  4169. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  4170. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4171. sd->sp_regen.value = type2;
  4172. sd->sp_regen.rate = val;
  4173. }
  4174. break;
  4175. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  4176. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  4177. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4178. sd->right_weapon.hp_drain_race[type2] += val;
  4179. }
  4180. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4181. sd->left_weapon.hp_drain_race[type2] += val;
  4182. }
  4183. break;
  4184. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  4185. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  4186. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4187. sd->right_weapon.sp_drain_race[type2] += val;
  4188. }
  4189. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4190. sd->left_weapon.sp_drain_race[type2] += val;
  4191. }
  4192. break;
  4193. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  4194. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  4195. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4196. sd->right_weapon.hp_drain_class[type2] += val;
  4197. }
  4198. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4199. sd->left_weapon.hp_drain_class[type2] += val;
  4200. }
  4201. break;
  4202. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  4203. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  4204. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4205. sd->right_weapon.sp_drain_class[type2] += val;
  4206. }
  4207. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4208. sd->left_weapon.sp_drain_class[type2] += val;
  4209. }
  4210. break;
  4211. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  4212. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  4213. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4214. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  4215. break;
  4216. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  4217. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  4218. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4219. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  4220. break;
  4221. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  4222. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  4223. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4224. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  4225. break;
  4226. case SP_SP_IGNORE_RES_RACE_RATE: // bonus2 bIgnoreResRaceRate,r,n;
  4227. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_RES_RACE_RATE);
  4228. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4229. sd->indexed_bonus.ignore_res_by_race[type2] += val;
  4230. break;
  4231. case SP_SP_IGNORE_MRES_RACE_RATE: // bonus2 bIgnoreMResRaceRate,r,n;
  4232. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_MRES_RACE_RATE);
  4233. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4234. sd->indexed_bonus.ignore_mres_by_race[type2] += val;
  4235. break;
  4236. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  4237. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  4238. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4239. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  4240. break;
  4241. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  4242. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4243. break;
  4244. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  4245. 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);
  4246. break;
  4247. }
  4248. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  4249. break;
  4250. case SP_SKILL_DELAY:
  4251. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4252. break;
  4253. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  4254. 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);
  4255. break;
  4256. }
  4257. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  4258. break;
  4259. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  4260. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4261. break;
  4262. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  4263. 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);
  4264. break;
  4265. }
  4266. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  4267. break;
  4268. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  4269. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4270. break;
  4271. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  4272. 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);
  4273. break;
  4274. }
  4275. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  4276. break;
  4277. #ifdef RENEWAL_CAST
  4278. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4279. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4280. break;
  4281. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  4282. 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);
  4283. break;
  4284. }
  4285. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  4286. break;
  4287. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4288. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4289. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4290. break;
  4291. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  4292. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4293. break;
  4294. }
  4295. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  4296. break;
  4297. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4298. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4299. break;
  4300. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  4301. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4302. break;
  4303. }
  4304. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  4305. break;
  4306. #else
  4307. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4308. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4309. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  4310. break;
  4311. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4312. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4313. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4314. break;
  4315. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4316. 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);
  4317. break;
  4318. }
  4319. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  4320. break;
  4321. #endif
  4322. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  4323. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4324. break;
  4325. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  4326. 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);
  4327. break;
  4328. }
  4329. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4330. break;
  4331. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4332. if (sd->subskill.size() == MAX_PC_BONUS) {
  4333. 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);
  4334. break;
  4335. }
  4336. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4337. break;
  4338. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4339. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4340. sd->indexed_bonus.subdefele[type2] += val;
  4341. break;
  4342. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4343. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4344. sd->indexed_bonus.coma_class[type2] += val;
  4345. sd->special_state.bonus_coma = 1;
  4346. break;
  4347. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4348. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4349. sd->indexed_bonus.coma_race[type2] += val;
  4350. sd->special_state.bonus_coma = 1;
  4351. break;
  4352. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4353. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4354. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4355. sd->indexed_bonus.magic_addrace2[type2] += val;
  4356. break;
  4357. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4358. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4359. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4360. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4361. break;
  4362. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4363. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4364. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4365. sd->indexed_bonus.dropaddrace[type2] += val;
  4366. break;
  4367. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4368. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4369. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4370. sd->indexed_bonus.dropaddclass[type2] += val;
  4371. break;
  4372. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4373. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4374. sd->indexed_bonus.magic_subdefele[type2] += val;
  4375. break;
  4376. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4377. if( sd->state.lr_flag == LR_FLAG_ARROW ){
  4378. break;
  4379. }
  4380. if( !item_db.exists( type2 ) ){
  4381. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4382. break;
  4383. }
  4384. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4385. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4386. break;
  4387. }
  4388. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4389. break;
  4390. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4391. if( sd->state.lr_flag == LR_FLAG_ARROW ){
  4392. break;
  4393. }
  4394. if( !type2 || !itemdb_group.exists( type2 ) ){
  4395. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4396. break;
  4397. }
  4398. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4399. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4400. break;
  4401. }
  4402. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4403. break;
  4404. default:
  4405. if (current_equip_combo_pos > 0) {
  4406. 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);
  4407. }
  4408. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4409. 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);
  4410. }
  4411. else {
  4412. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4413. }
  4414. break;
  4415. }
  4416. }
  4417. /**
  4418. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4419. * @param sd
  4420. * @param type Bonus type used by bBonusName
  4421. * @param type2
  4422. * @param type3
  4423. * @param val Value that usually for rate or fixed value
  4424. */
  4425. void pc_bonus3(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 val)
  4426. {
  4427. nullpo_retv(sd);
  4428. switch(type){
  4429. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4430. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4431. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4432. break;
  4433. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,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_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4438. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4439. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4440. break;
  4441. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4442. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4443. {
  4444. int32 target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4445. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4446. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4447. }
  4448. break;
  4449. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4450. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4451. {
  4452. int32 target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4453. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4454. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4455. }
  4456. break;
  4457. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4458. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4459. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4460. break;
  4461. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4462. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4463. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4464. break;
  4465. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4466. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4467. 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);
  4468. break;
  4469. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4470. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4471. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4472. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4473. break;
  4474. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4475. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4476. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4477. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4478. break;
  4479. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4480. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4481. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4482. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4483. break;
  4484. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4485. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4486. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4487. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4488. break;
  4489. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4490. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4491. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4492. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4493. break;
  4494. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4495. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4496. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4497. sd->sp_vanish_race[type2].rate += type3;
  4498. sd->sp_vanish_race[type2].per += val;
  4499. }
  4500. break;
  4501. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4502. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4503. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4504. sd->hp_vanish_race[type2].rate += type3;
  4505. sd->hp_vanish_race[type2].per += val;
  4506. }
  4507. break;
  4508. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4509. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4510. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4511. }
  4512. break;
  4513. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4514. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4515. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4516. }
  4517. break;
  4518. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4519. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4520. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4521. break;
  4522. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4523. //if (type3 > sd->norecover_state_race[type2].rate) {
  4524. // sd->norecover_state_race[type2].rate = type3;
  4525. // sd->norecover_state_race[type2].tick = val;
  4526. // break;
  4527. //}
  4528. sd->norecover_state_race[type2].rate = type3;
  4529. sd->norecover_state_race[type2].tick = val;
  4530. break;
  4531. default:
  4532. if (current_equip_combo_pos > 0) {
  4533. 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);
  4534. }
  4535. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4536. 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);
  4537. }
  4538. else {
  4539. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4540. }
  4541. break;
  4542. }
  4543. }
  4544. /**
  4545. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4546. * @param sd
  4547. * @param type Bonus type used by bBonusName
  4548. * @param type2
  4549. * @param type3
  4550. * @param type4
  4551. * @param val Value that usually for rate or fixed value
  4552. */
  4553. void pc_bonus4(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 type4,int32 val)
  4554. {
  4555. nullpo_retv(sd);
  4556. switch(type){
  4557. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4558. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4559. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4560. break;
  4561. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4562. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4563. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4564. break;
  4565. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4566. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4567. {
  4568. int32 target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4569. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4570. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4571. }
  4572. break;
  4573. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4574. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4575. 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);
  4576. break;
  4577. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4578. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4579. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4580. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4581. break;
  4582. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4583. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4584. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4585. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4586. break;
  4587. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4588. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4589. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4590. break;
  4591. sd->def_set_race[type2].rate = type3;
  4592. sd->def_set_race[type2].tick = type4;
  4593. sd->def_set_race[type2].value = val;
  4594. break;
  4595. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4596. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4597. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4598. break;
  4599. sd->mdef_set_race[type2].rate = type3;
  4600. sd->mdef_set_race[type2].tick = type4;
  4601. sd->mdef_set_race[type2].value = val;
  4602. break;
  4603. default:
  4604. if (current_equip_combo_pos > 0) {
  4605. 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);
  4606. }
  4607. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4608. 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);
  4609. }
  4610. else {
  4611. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4612. }
  4613. break;
  4614. }
  4615. }
  4616. /**
  4617. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4618. * @param sd
  4619. * @param type Bonus type used by bBonusName
  4620. * @param type2
  4621. * @param type3
  4622. * @param type4
  4623. * @param val Value that usually for rate or fixed value
  4624. */
  4625. void pc_bonus5(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 type4,int32 type5,int32 val)
  4626. {
  4627. nullpo_retv(sd);
  4628. switch(type){
  4629. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4630. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4631. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4632. break;
  4633. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4634. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4635. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4636. break;
  4637. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4638. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4639. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4640. break;
  4641. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4642. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4643. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4644. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4645. break;
  4646. default:
  4647. if (current_equip_combo_pos > 0) {
  4648. 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);
  4649. }
  4650. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4651. 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);
  4652. }
  4653. else {
  4654. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4655. }
  4656. break;
  4657. }
  4658. }
  4659. /*==========================================
  4660. * Grants a player a given skill. Flag values are:
  4661. * 0 - Grant permanent skill to be bound to skill tree
  4662. * 1 - Grant an item skill (temporary)
  4663. * 2 - Like 1, except the level granted can stack with previously learned level.
  4664. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4665. *------------------------------------------*/
  4666. bool pc_skill(map_session_data* sd, uint16 skill_id, int32 level, enum e_addskill_type type) {
  4667. uint16 idx = 0;
  4668. nullpo_ret(sd);
  4669. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4670. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4671. return false;
  4672. }
  4673. if (level > MAX_SKILL_LEVEL) {
  4674. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4675. return false;
  4676. }
  4677. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4678. 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);
  4679. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4680. }
  4681. switch (type) {
  4682. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4683. sd->status.skill[idx].id = skill_id;
  4684. sd->status.skill[idx].lv = level;
  4685. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4686. if (level == 0) { //Remove skill.
  4687. sd->status.skill[idx].id = 0;
  4688. clif_deleteskill(*sd,skill_id);
  4689. } else
  4690. clif_addskill(*sd,skill_id);
  4691. 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.
  4692. status_calc_pc(sd, SCO_NONE);
  4693. break;
  4694. case ADDSKILL_TEMP: //Item bonus skill.
  4695. if (sd->status.skill[idx].id != 0) {
  4696. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4697. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4698. } else {
  4699. sd->status.skill[idx].id = skill_id;
  4700. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4701. }
  4702. sd->status.skill[idx].lv = max(sd->status.skill[idx].lv, level);
  4703. break;
  4704. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4705. if (sd->status.skill[idx].id != 0) {
  4706. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4707. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4708. } else {
  4709. sd->status.skill[idx].id = skill_id;
  4710. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4711. }
  4712. sd->status.skill[idx].lv += level;
  4713. break;
  4714. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4715. sd->status.skill[idx].id = skill_id;
  4716. sd->status.skill[idx].lv = level;
  4717. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4718. if (level == 0) { //Remove skill.
  4719. sd->status.skill[idx].id = 0;
  4720. clif_deleteskill(*sd,skill_id);
  4721. } else
  4722. clif_addskill(*sd,skill_id);
  4723. 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.
  4724. status_calc_pc(sd, SCO_NONE);
  4725. break;
  4726. default:
  4727. return false;
  4728. }
  4729. return true;
  4730. }
  4731. /**
  4732. * Set's a player's plagiarized skill.
  4733. * @param sd: Player
  4734. * @param skill_id: Skill to be plagiarized
  4735. * @param skill_lv: Skill level to be plagiarized
  4736. * @return True on success or false otherwise
  4737. */
  4738. bool pc_skill_plagiarism(map_session_data &sd, uint16 skill_id, uint16 skill_lv)
  4739. {
  4740. skill_id = skill_dummy2skill_id(skill_id);
  4741. uint16 idx = skill_get_index(skill_id);
  4742. // Use skill index, avoiding out-of-bound array [Cydh]
  4743. if (idx == 0) {
  4744. ShowWarning("pc_skill_plagiarism: invalid skill idx 0 for skill %d.\n", skill_id);
  4745. return false;
  4746. }
  4747. skill_lv = cap_value(skill_lv, 1, skill_get_max(skill_id));
  4748. int32 type = skill_isCopyable(&sd, skill_id);
  4749. if (type == 1) {
  4750. pc_skill_plagiarism_reset(sd, type);
  4751. sd.cloneskill_idx = idx;
  4752. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), skill_id);
  4753. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), skill_lv);
  4754. } else if (type == 2) {
  4755. pc_skill_plagiarism_reset(sd, type);
  4756. sd.reproduceskill_idx = idx;
  4757. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), skill_id);
  4758. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), skill_lv);
  4759. } else {
  4760. ShowWarning("pc_skill_plagiarism: skill %d is not copyable.\n", skill_id);
  4761. return false;
  4762. }
  4763. sd.status.skill[idx].id = skill_id;
  4764. sd.status.skill[idx].lv = static_cast<uint8>(skill_lv);
  4765. sd.status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
  4766. clif_addskill(sd, skill_id);
  4767. return true;
  4768. }
  4769. /**
  4770. * Clear plagiarized skills from a player.
  4771. * @param sd: Player
  4772. * @param type: 1 for Plagiarism or 2 for Reproduce
  4773. * @return True on success or false otherwise
  4774. */
  4775. bool pc_skill_plagiarism_reset(map_session_data &sd, uint8 type)
  4776. {
  4777. uint16 idx;
  4778. if (type == 1)
  4779. idx = sd.cloneskill_idx;
  4780. else if (type == 2)
  4781. idx = sd.reproduceskill_idx;
  4782. else {
  4783. ShowError("pc_skill_plagiarism_reset: Unknown type %d.\n", type);
  4784. return false;
  4785. }
  4786. if (sd.status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED) {
  4787. uint16 skill_id = sd.status.skill[idx].id;
  4788. sd.status.skill[idx].id = 0;
  4789. sd.status.skill[idx].lv = 0;
  4790. sd.status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4791. clif_deleteskill(sd, skill_id);
  4792. if (type == 1) {
  4793. sd.cloneskill_idx = 0;
  4794. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  4795. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  4796. }
  4797. else if (type == 2) {
  4798. sd.reproduceskill_idx = 0;
  4799. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), 0);
  4800. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  4801. }
  4802. }
  4803. return true;
  4804. }
  4805. /*==========================================
  4806. * Append a card to an item ?
  4807. *------------------------------------------*/
  4808. int32 pc_insert_card(map_session_data* sd, int32 idx_card, int32 idx_equip)
  4809. {
  4810. nullpo_ret(sd);
  4811. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4812. return 0;
  4813. }
  4814. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4815. return 0;
  4816. }
  4817. int32 i;
  4818. t_itemid nameid;
  4819. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4820. struct item_data* item_card = sd->inventory_data[idx_card];
  4821. if(item_eq == nullptr)
  4822. return 0; //Invalid item index.
  4823. if(item_card == nullptr)
  4824. return 0; //Invalid card index.
  4825. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4826. return 0; // target item missing
  4827. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4828. return 0; // target card missing
  4829. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4830. return 0; // only weapons and armor are allowed
  4831. if( item_card->type != IT_CARD )
  4832. return 0; // must be a card
  4833. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4834. return 0; // target must be identified
  4835. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4836. return 0; // card slots reserved for other purposes
  4837. if( (item_eq->equip & item_card->equip) == 0 )
  4838. return 0; // card cannot be compounded on this item type
  4839. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4840. return 0; // attempted to place shield card on left-hand weapon.
  4841. 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)) )
  4842. return 0; // specific accessory-card can only be inserted to specific accessory.
  4843. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4844. return 0; // item must be unequipped
  4845. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4846. if( i == item_eq->slots )
  4847. return 0; // no free slots
  4848. // remember the card id to insert
  4849. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4850. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4851. {// failed
  4852. clif_insert_card( *sd, idx_equip, idx_card, true );
  4853. }
  4854. else
  4855. {// success
  4856. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4857. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4858. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4859. clif_insert_card( *sd, idx_equip, idx_card, false );
  4860. }
  4861. return 0;
  4862. }
  4863. /**
  4864. * Returns the count of unidentified items with the option to identify too.
  4865. * @param sd: Player data
  4866. * @param identify_item: Whether or not to identify any unidentified items
  4867. * @return Unidentified items count
  4868. */
  4869. int32 pc_identifyall(map_session_data *sd, bool identify_item)
  4870. {
  4871. int32 unidentified_count = 0;
  4872. for (int32 i = 0; i < MAX_INVENTORY; i++) {
  4873. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4874. if (identify_item == true) {
  4875. sd->inventory.u.items_inventory[i].identify = 1;
  4876. clif_item_identified( *sd, i, false );
  4877. }
  4878. unidentified_count++;
  4879. }
  4880. }
  4881. return unidentified_count;
  4882. }
  4883. //
  4884. // Items
  4885. //
  4886. /*==========================================
  4887. * Update buying value by skills
  4888. *------------------------------------------*/
  4889. int32 pc_modifybuyvalue(map_session_data *sd,int32 orig_value)
  4890. {
  4891. int32 skill,val = orig_value,rate1 = 0,rate2 = 0;
  4892. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4893. rate1 = 5+skill*2-((skill==10)? 1:0);
  4894. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4895. rate2 = 5+skill*4;
  4896. if(rate1 < rate2) rate1 = rate2;
  4897. if(rate1)
  4898. val = (int32)((double)orig_value*(double)(100-rate1)/100.);
  4899. if(val < battle_config.min_shop_buy)
  4900. val = battle_config.min_shop_buy;
  4901. return val;
  4902. }
  4903. /*==========================================
  4904. * Update selling value by skills
  4905. *------------------------------------------*/
  4906. int32 pc_modifysellvalue(map_session_data *sd,int32 orig_value)
  4907. {
  4908. int32 skill,val = orig_value,rate = 0;
  4909. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4910. rate = 5+skill*2-((skill==10)? 1:0);
  4911. if(rate)
  4912. val = (int32)((double)orig_value*(double)(100+rate)/100.);
  4913. if (val < battle_config.min_shop_sell)
  4914. val = battle_config.min_shop_sell;
  4915. return val;
  4916. }
  4917. /*==========================================
  4918. * Checking if we have enough place on inventory for new item
  4919. * Make sure to take 30k as limit (for client I guess)
  4920. * @param sd
  4921. * @param nameid
  4922. * @param amount
  4923. * @return e_chkitem_result
  4924. *------------------------------------------*/
  4925. char pc_checkadditem(map_session_data *sd, t_itemid nameid, int32 amount)
  4926. {
  4927. int32 i;
  4928. struct item_data* data;
  4929. nullpo_ret(sd);
  4930. if(amount > MAX_AMOUNT)
  4931. return CHKADDITEM_OVERAMOUNT;
  4932. data = itemdb_search(nameid);
  4933. if(!itemdb_isstackable2(data))
  4934. return CHKADDITEM_NEW;
  4935. if( data->stack.inventory && amount > data->stack.amount )
  4936. return CHKADDITEM_OVERAMOUNT;
  4937. if (data->flag.guid)
  4938. return CHKADDITEM_NEW;
  4939. for(i=0;i<MAX_INVENTORY;i++){
  4940. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4941. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4942. 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 ) )
  4943. return CHKADDITEM_OVERAMOUNT;
  4944. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4945. if( i >= sd->status.inventory_slots ){
  4946. return CHKADDITEM_OVERAMOUNT;
  4947. }
  4948. return CHKADDITEM_EXIST;
  4949. }
  4950. }
  4951. return CHKADDITEM_NEW;
  4952. }
  4953. /*==========================================
  4954. * Return number of available place in inventory
  4955. * Each non stackable item will reduce place by 1
  4956. * @param sd
  4957. * @return Number of empty slots
  4958. *------------------------------------------*/
  4959. uint8 pc_inventoryblank(map_session_data *sd)
  4960. {
  4961. uint16 i;
  4962. uint8 b;
  4963. nullpo_ret(sd);
  4964. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4965. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4966. b++;
  4967. }
  4968. return b;
  4969. }
  4970. /**
  4971. * Attempts to remove zeny from player
  4972. * @param sd: Player
  4973. * @param zeny: Zeny removed
  4974. * @param type: Log type
  4975. * @param log_charid: (optional) From who to log (if not needed, use 0)
  4976. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4977. */
  4978. char pc_payzeny(map_session_data *sd, int32 zeny, enum e_log_pick_type type, uint32 log_charid)
  4979. {
  4980. nullpo_retr(2,sd);
  4981. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4982. if( zeny < 0 )
  4983. {
  4984. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4985. return 1;
  4986. }
  4987. if( sd->status.zeny < zeny )
  4988. return 1; //Not enough.
  4989. sd->status.zeny -= zeny;
  4990. clif_updatestatus(*sd,SP_ZENY);
  4991. log_zeny(*sd, type, log_charid, -zeny);
  4992. if( zeny > 0 && sd->state.showzeny ) {
  4993. char output[255];
  4994. sprintf(output, "Removed %dz.", zeny);
  4995. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4996. }
  4997. return 0;
  4998. }
  4999. /**
  5000. * Attempts to give zeny to player
  5001. * @param sd: Player
  5002. * @param type: Log type
  5003. * @param log_charid: (optional) From who to log (if not needed, use 0)
  5004. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  5005. */
  5006. char pc_getzeny(map_session_data *sd, int32 zeny, enum e_log_pick_type type, uint32 log_charid)
  5007. {
  5008. nullpo_retr(-1,sd);
  5009. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  5010. if( zeny < 0 )
  5011. {
  5012. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  5013. return 1;
  5014. }
  5015. if( zeny > MAX_ZENY - sd->status.zeny )
  5016. zeny = MAX_ZENY - sd->status.zeny;
  5017. sd->status.zeny += zeny;
  5018. clif_updatestatus(*sd,SP_ZENY);
  5019. log_zeny(*sd, type, log_charid, zeny);
  5020. if( zeny > 0 && sd->state.showzeny ) {
  5021. char output[255];
  5022. sprintf(output, "Gained %dz.", zeny);
  5023. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5024. }
  5025. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  5026. return 0;
  5027. }
  5028. /**
  5029. * Attempts to remove Cash Points from player
  5030. * @param sd: Player
  5031. * @param price: Total points (cash + kafra) the player has to pay
  5032. * @param points: Kafra points the player has to pay
  5033. * @param type: Log type
  5034. * @return -1: Not enough points, otherwise success (cash+points)
  5035. */
  5036. int32 pc_paycash(map_session_data *sd, int32 price, int32 points, e_log_pick_type type)
  5037. {
  5038. int32 cash;
  5039. nullpo_retr(-1,sd);
  5040. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5041. cash = price-points;
  5042. if( sd->cashPoints < cash || sd->kafraPoints < points )
  5043. {
  5044. 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);
  5045. return -1;
  5046. }
  5047. if( cash ){
  5048. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  5049. sd->cashPoints -= cash;
  5050. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  5051. }
  5052. if( points ){
  5053. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  5054. sd->kafraPoints -= points;
  5055. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  5056. }
  5057. if( battle_config.cashshop_show_points )
  5058. {
  5059. char output[CHAT_SIZE_MAX];
  5060. 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.
  5061. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5062. }
  5063. return cash+points;
  5064. }
  5065. /**
  5066. * Attempts to give Cash Points to player
  5067. * @param sd: Player
  5068. * @param cash: Cash points the player gets
  5069. * @param points: Kafra points the player gets
  5070. * @param type: Log type
  5071. * @return -1: Error, otherwise success (cash or points)
  5072. */
  5073. int32 pc_getcash(map_session_data *sd, int32 cash, int32 points, e_log_pick_type type)
  5074. {
  5075. char output[CHAT_SIZE_MAX];
  5076. nullpo_retr(-1,sd);
  5077. cash = cap_value(cash, 0, MAX_CASHPOINT); //prevent command UB
  5078. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5079. if( cash > 0 )
  5080. {
  5081. if( cash > MAX_CASHPOINT-sd->cashPoints )
  5082. {
  5083. 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);
  5084. cash = MAX_CASHPOINT-sd->cashPoints;
  5085. }
  5086. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  5087. sd->cashPoints += cash;
  5088. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  5089. if( battle_config.cashshop_show_points )
  5090. {
  5091. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  5092. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5093. }
  5094. return cash;
  5095. }
  5096. if( points > 0 )
  5097. {
  5098. if( points > MAX_KAFRAPOINT-sd->kafraPoints )
  5099. {
  5100. 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);
  5101. points = MAX_KAFRAPOINT-sd->kafraPoints;
  5102. }
  5103. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  5104. sd->kafraPoints += points;
  5105. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  5106. if( battle_config.cashshop_show_points )
  5107. {
  5108. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  5109. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5110. }
  5111. return points;
  5112. }
  5113. return -1; //shouldn't happen but just in case
  5114. }
  5115. /**
  5116. * Searching a specified itemid in inventory and return his stored index
  5117. * @param sd Player
  5118. * @param nameid Find this Item!
  5119. * @return Stored index in inventory, or -1 if not found.
  5120. **/
  5121. int16 pc_search_inventory(map_session_data *sd, t_itemid nameid) {
  5122. int16 i;
  5123. nullpo_retr(-1, sd);
  5124. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  5125. return ( i < MAX_INVENTORY ) ? i : -1;
  5126. }
  5127. /** Attempt to add a new item to player inventory
  5128. * @param sd
  5129. * @param item_data
  5130. * @param amount
  5131. * @param log_type
  5132. * @return
  5133. * 0 = success
  5134. * 1 = invalid itemid not found or negative amount
  5135. * 2 = overweight
  5136. * 3 = ?
  5137. * 4 = no free place found
  5138. * 5 = max amount reached
  5139. * 6 = ?
  5140. * 7 = stack limitation
  5141. */
  5142. enum e_additem_result pc_additem(map_session_data *sd,struct item *item,int32 amount,e_log_pick_type log_type) {
  5143. struct item_data *id;
  5144. int16 i;
  5145. uint32 w;
  5146. nullpo_retr(ADDITEM_INVALID, sd);
  5147. nullpo_retr(ADDITEM_INVALID, item);
  5148. if( item->nameid == 0 || amount <= 0 )
  5149. return ADDITEM_INVALID;
  5150. if( amount > MAX_AMOUNT )
  5151. return ADDITEM_OVERAMOUNT;
  5152. id = itemdb_search(item->nameid);
  5153. if( id->stack.inventory && amount > id->stack.amount )
  5154. {// item stack limitation
  5155. return ADDITEM_STACKLIMIT;
  5156. }
  5157. w = id->weight*amount;
  5158. if(sd->weight + w > sd->max_weight)
  5159. return ADDITEM_OVERWEIGHT;
  5160. if (id->flag.guid && !item->unique_id)
  5161. item->unique_id = pc_generate_unique_id(sd);
  5162. // Stackable | Non Rental
  5163. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  5164. for( i = 0; i < MAX_INVENTORY; i++ ) {
  5165. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  5166. sd->inventory.u.items_inventory[i].bound == item->bound &&
  5167. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  5168. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  5169. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  5170. 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 ) )
  5171. return ADDITEM_OVERAMOUNT;
  5172. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  5173. if( i >= sd->status.inventory_slots ){
  5174. return ADDITEM_OVERAMOUNT;
  5175. }
  5176. sd->inventory.u.items_inventory[i].amount += amount;
  5177. clif_additem(sd,i,amount,0);
  5178. break;
  5179. }
  5180. }
  5181. }else{
  5182. i = MAX_INVENTORY;
  5183. }
  5184. if (i >= MAX_INVENTORY) {
  5185. i = pc_search_inventory(sd,0);
  5186. if( i < 0 )
  5187. return ADDITEM_OVERITEM;
  5188. if( i >= sd->status.inventory_slots ){
  5189. return ADDITEM_OVERITEM;
  5190. }
  5191. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  5192. // clear equip and favorite fields first, just in case
  5193. if( item->equip )
  5194. sd->inventory.u.items_inventory[i].equip = 0;
  5195. if( item->favorite != 0 )
  5196. sd->inventory.u.items_inventory[i].favorite = 0;
  5197. if( item->equipSwitch )
  5198. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  5199. sd->inventory.u.items_inventory[i].amount = amount;
  5200. sd->inventory_data[i] = id;
  5201. sd->last_addeditem_index = i;
  5202. if (!itemdb_isstackable2(id) || id->flag.guid)
  5203. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  5204. clif_additem(sd,i,amount,0);
  5205. }
  5206. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  5207. sd->weight += w;
  5208. clif_updatestatus(*sd,SP_WEIGHT);
  5209. //Auto-equip
  5210. if(id->flag.autoequip)
  5211. pc_equipitem(sd, i, id->equip);
  5212. /* rental item check */
  5213. if( item->expire_time ) {
  5214. if( time(nullptr) > item->expire_time ) {
  5215. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  5216. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  5217. } else {
  5218. uint32 seconds = (uint32)( item->expire_time - time(nullptr) );
  5219. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  5220. pc_inventory_rental_add(sd, seconds);
  5221. }
  5222. }
  5223. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  5224. pc_show_questinfo(sd);
  5225. return ADDITEM_SUCCESS;
  5226. }
  5227. /*==========================================
  5228. * Remove an item at index n from inventory by amount.
  5229. * @param sd
  5230. * @param n Item index in inventory
  5231. * @param amount
  5232. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  5233. * @param reason Delete reason
  5234. * @param log_type e_log_pick_type
  5235. * @return 1 - invalid itemid or negative amount; 0 - Success
  5236. *------------------------------------------*/
  5237. char pc_delitem(map_session_data *sd,int32 n,int32 amount,int32 type, int16 reason, e_log_pick_type log_type)
  5238. {
  5239. nullpo_retr(1, sd);
  5240. 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)
  5241. return 1;
  5242. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  5243. sd->inventory.u.items_inventory[n].amount -= amount;
  5244. sd->weight -= sd->inventory_data[n]->weight*amount ;
  5245. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  5246. if(sd->inventory.u.items_inventory[n].equip)
  5247. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  5248. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  5249. sd->inventory_data[n] = nullptr;
  5250. }
  5251. if(!(type&1))
  5252. clif_delitem( *sd, n, amount, reason );
  5253. if(!(type&2))
  5254. clif_updatestatus(*sd,SP_WEIGHT);
  5255. pc_show_questinfo(sd);
  5256. return 0;
  5257. }
  5258. /*==========================================
  5259. * Attempt to drop an item.
  5260. * @param sd
  5261. * @param n Item index in inventory
  5262. * @param amount Amount of item
  5263. * @return False = fail; True = success
  5264. *------------------------------------------*/
  5265. bool pc_dropitem(map_session_data *sd,int32 n,int32 amount)
  5266. {
  5267. nullpo_retr(1, sd);
  5268. if(n < 0 || n >= MAX_INVENTORY)
  5269. return false;
  5270. if(amount <= 0)
  5271. return false;
  5272. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  5273. sd->inventory.u.items_inventory[n].amount <= 0 ||
  5274. sd->inventory.u.items_inventory[n].amount < amount ||
  5275. sd->state.trading || sd->state.vending ||
  5276. !sd->inventory_data[n] //pc_delitem would fail on this case.
  5277. )
  5278. return false;
  5279. if( sd->inventory.u.items_inventory[n].equipSwitch )
  5280. return false;
  5281. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  5282. {
  5283. clif_displaymessage (sd->fd, msg_txt(sd,271));
  5284. return false; //Can't drop items in nodrop mapflag maps.
  5285. }
  5286. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  5287. {
  5288. clif_displaymessage (sd->fd, msg_txt(sd,263));
  5289. return false;
  5290. }
  5291. // Bypass drop restriction in map_addflooritem because we've already checked it above
  5292. 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,
  5293. false, DIR_MAX, battle_config.item_stacking?BL_NUL:BL_ITEM))
  5294. {
  5295. return false;
  5296. }
  5297. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  5298. clif_dropitem( *sd, n, amount );
  5299. return true;
  5300. }
  5301. /*==========================================
  5302. * Attempt to pick up an item.
  5303. * @param sd
  5304. * @param fitem Item that will be picked
  5305. * @return False = fail; True = success
  5306. *------------------------------------------*/
  5307. bool pc_takeitem(map_session_data *sd,struct flooritem_data *fitem)
  5308. {
  5309. int32 flag = 0;
  5310. t_tick tick = gettick();
  5311. struct party_data *p = nullptr;
  5312. nullpo_ret(sd);
  5313. nullpo_ret(fitem);
  5314. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  5315. return false; // Distance is too far
  5316. if (sd->sc.cant.pickup)
  5317. return false;
  5318. if (sd->status.party_id)
  5319. p = party_search(sd->status.party_id);
  5320. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  5321. map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  5322. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  5323. if (!(p && p->party.item&1 &&
  5324. first_sd && first_sd->status.party_id == sd->status.party_id
  5325. ))
  5326. return false;
  5327. }
  5328. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  5329. map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  5330. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  5331. if (!(p && p->party.item&1 &&
  5332. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5333. (second_sd && second_sd->status.party_id == sd->status.party_id))
  5334. ))
  5335. return false;
  5336. }
  5337. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  5338. map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  5339. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  5340. if(!(p && p->party.item&1 &&
  5341. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5342. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  5343. (third_sd && third_sd->status.party_id == sd->status.party_id))
  5344. ))
  5345. return false;
  5346. }
  5347. }
  5348. }
  5349. }
  5350. //This function takes care of giving the item to whoever should have it, considering party-share options.
  5351. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  5352. clif_additem(sd,0,0,flag);
  5353. return true;
  5354. }
  5355. //Display pickup animation.
  5356. unit_stop_attack( &sd->bl );
  5357. clif_takeitem(sd->bl,fitem->bl);
  5358. if (fitem->mob_id &&
  5359. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  5360. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  5361. )
  5362. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  5363. map_clearflooritem(&fitem->bl);
  5364. return true;
  5365. }
  5366. /*==========================================
  5367. * Check if item is usable.
  5368. * Return:
  5369. * false = no
  5370. * true = yes
  5371. *------------------------------------------*/
  5372. bool pc_isUseitem(map_session_data *sd,int32 n)
  5373. {
  5374. struct item_data *item;
  5375. t_itemid nameid;
  5376. nullpo_ret(sd);
  5377. item = sd->inventory_data[n];
  5378. nameid = sd->inventory.u.items_inventory[n].nameid;
  5379. if( item == nullptr )
  5380. return false;
  5381. //Not consumable item
  5382. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  5383. return false;
  5384. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  5385. return true;
  5386. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5387. if(mapdata->getMapFlag(MF_NOITEMCONSUMPTION)) //consumable but mapflag prevent it
  5388. return false;
  5389. //Prevent mass item usage. [Skotlex]
  5390. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  5391. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  5392. )
  5393. return false;
  5394. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  5395. clif_msg( *sd, MSI_CANT_USE_WHEN_SITDOWN );
  5396. return false; // You cannot use this item while sitting.
  5397. }
  5398. if (sd->state.storage_flag && item->type != IT_CASH) {
  5399. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  5400. return false; // You cannot use this item while storage is open.
  5401. }
  5402. if (item->flag.dead_branch && (mapdata->getMapFlag(MF_NOBRANCH) || mapdata_flag_gvg2(mapdata)))
  5403. return false;
  5404. if( itemdb_group.item_exists( IG_MF_NOTELEPORT, nameid ) ){
  5405. if( ( mapdata->getMapFlag(MF_NOTELEPORT) || mapdata_flag_gvg2( mapdata ) ) ){
  5406. clif_skill_teleportmessage( *sd, NOTIFY_MAPINFO_CANT_TP );
  5407. return false;
  5408. }
  5409. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5410. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5411. return false;
  5412. }
  5413. }
  5414. if( itemdb_group.item_exists( IG_MF_NORETURN, nameid ) ){
  5415. if( mapdata->getMapFlag(MF_NORETURN) ){
  5416. return false;
  5417. }
  5418. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5419. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5420. return false;
  5421. }
  5422. }
  5423. if( itemdb_group.item_exists( IG_GIANT_FLY_WING, nameid ) ){
  5424. struct party_data *pd = party_search( sd->status.party_id );
  5425. if( pd ){
  5426. int32 i;
  5427. ARR_FIND( 0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader );
  5428. // User is not party leader
  5429. if( i == MAX_PARTY ){
  5430. clif_msg( *sd, MSI_CANNOT_PARTYCALL );
  5431. return false;
  5432. }
  5433. 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 ) );
  5434. // No party members found on same map
  5435. if( i == MAX_PARTY ){
  5436. clif_msg( *sd, MSI_NO_PARTYMEM_ON_THISMAP );
  5437. return false;
  5438. }
  5439. }else{
  5440. clif_msg( *sd, MSI_CANNOT_PARTYCALL );
  5441. return false;
  5442. }
  5443. }
  5444. switch( nameid ) {
  5445. case ITEMID_MERCENARY_RED_POTION:
  5446. case ITEMID_MERCENARY_BLUE_POTION:
  5447. case ITEMID_M_CENTER_POTION:
  5448. case ITEMID_M_AWAKENING_POTION:
  5449. case ITEMID_M_BERSERK_POTION:
  5450. if( sd->md == nullptr || sd->md->db == nullptr )
  5451. return false;
  5452. if( sd->md->sc.cant.consume )
  5453. return false;
  5454. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5455. return false;
  5456. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5457. return false;
  5458. break;
  5459. case ITEMID_NEURALIZER:
  5460. if( !mapdata->getMapFlag(MF_RESET) )
  5461. return false;
  5462. break;
  5463. }
  5464. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != nullptr )
  5465. return false; // Mercenary Scrolls
  5466. // Safe check type cash disappear when overweight [Napster]
  5467. if( item->flag.group || item->type == IT_CASH ){
  5468. #ifdef RENEWAL
  5469. // Check if the player is not overweighted
  5470. // In Renewal the limit is 70% weight and gives the same error message
  5471. if (pc_is70overweight(sd)) {
  5472. clif_msg_color( *sd, MSI_PICKUP_FAILED_ITEMCREATE, color_table[COLOR_RED] );
  5473. return 0;
  5474. }
  5475. #else
  5476. // Check if the player is not overweighted
  5477. // In Pre-Renewal the limit is 50% weight and gives a specific error message
  5478. if (pc_is50overweight(sd)) {
  5479. clif_msg_color( *sd, MSI_CANT_GET_ITEM_BECAUSE_WEIGHT, color_table[COLOR_RED] );
  5480. return 0;
  5481. }
  5482. #endif
  5483. // Check if the player has enough free spaces in the inventory
  5484. // Official servers use 10 as the minimum amount of slots required to get the items
  5485. // The <= is intentional, as in official servers you actually need an extra empty slot
  5486. // TODO: Count the items the player will get and check for the actual inventory space required std::max<size_t>( count, 10 )
  5487. if (pc_inventoryblank(sd) <= 10) {
  5488. #ifdef RENEWAL
  5489. clif_msg_color( *sd, MSI_PICKUP_FAILED_ITEMCREATE, color_table[COLOR_RED] );
  5490. #else
  5491. clif_msg_color( *sd, MSI_CANT_GET_ITEM_BECAUSE_COUNT, color_table[COLOR_RED] );
  5492. #endif
  5493. return 0;
  5494. }
  5495. }
  5496. //Gender check
  5497. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5498. return false;
  5499. //Required level check
  5500. if(item->elv && sd->status.base_level < (uint32)item->elv)
  5501. return false;
  5502. if(item->elvmax && sd->status.base_level > (uint32)item->elvmax)
  5503. return false;
  5504. //Not equipable by class. [Skotlex]
  5505. if (!pc_job_can_use_item(sd,item))
  5506. return false;
  5507. if (sd->sc.cant.consume)
  5508. return false;
  5509. if (!pc_isItemClass(sd,item))
  5510. return false;
  5511. //Dead Branch items
  5512. if( item->flag.dead_branch )
  5513. log_branch(sd);
  5514. return true;
  5515. }
  5516. /*==========================================
  5517. * Last checks to use an item.
  5518. * Return:
  5519. * 0 = fail
  5520. * 1 = success
  5521. *------------------------------------------*/
  5522. int32 pc_useitem(map_session_data *sd,int32 n)
  5523. {
  5524. t_tick tick = gettick();
  5525. int32 amount;
  5526. t_itemid nameid;
  5527. struct script_code *script;
  5528. struct item item;
  5529. struct item_data *id;
  5530. nullpo_ret(sd);
  5531. if (sd->state.mail_writing)
  5532. return 0;
  5533. if (sd->npc_id) {
  5534. if (sd->progressbar.npc_id) {
  5535. clif_progressbar_abort(sd);
  5536. return 0; // First item use attempt cancels the progress bar
  5537. }
  5538. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5539. #ifdef RENEWAL
  5540. clif_msg( *sd, MSI_BUSY );
  5541. #endif
  5542. return 0;
  5543. }
  5544. }
  5545. item = sd->inventory.u.items_inventory[n];
  5546. id = sd->inventory_data[n];
  5547. if (item.nameid == 0 || item.amount <= 0)
  5548. return 0;
  5549. if( !pc_isUseitem(sd,n) )
  5550. return 0;
  5551. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5552. nameid = id->nameid;
  5553. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5554. return 0;
  5555. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5556. if( id->flag.delay_consume > 0 ) {
  5557. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.getSCE(SC_ALL_RIDING) )
  5558. return 0;
  5559. else if( pc_issit(sd) )
  5560. return 0;
  5561. }
  5562. //Since most delay-consume items involve using a "skill-type" target cursor,
  5563. //perform a skill-use check before going through. [Skotlex]
  5564. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5565. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5566. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5567. return 0;
  5568. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5569. return 0;
  5570. /* on restricted maps the item is consumed but the effect is not used */
  5571. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5572. clif_msg( *sd, MSI_IMPOSSIBLE_USEITEM_AREA ); // This item cannot be used within this area
  5573. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5574. clif_useitemack(sd,n,item.amount-1,true);
  5575. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5576. }
  5577. return 0;/* regardless, effect is not run */
  5578. }
  5579. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5580. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5581. return 0;
  5582. }
  5583. sd->itemid = item.nameid;
  5584. sd->itemindex = n;
  5585. amount = item.amount;
  5586. script = id->script;
  5587. //Check if the item is to be consumed immediately [Skotlex]
  5588. if (id->flag.delay_consume > 0)
  5589. clif_useitemack(sd, n, amount, true);
  5590. else
  5591. {
  5592. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5593. {
  5594. clif_useitemack(sd, n, amount - 1, true);
  5595. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5596. }
  5597. else
  5598. clif_useitemack(sd, n, 0, false);
  5599. }
  5600. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5601. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5602. //Update item use time.
  5603. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5604. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5605. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5606. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5607. potion_flag = 0;
  5608. return 1;
  5609. }
  5610. /**
  5611. * Add item on cart for given index.
  5612. * @param sd
  5613. * @param item
  5614. * @param amount
  5615. * @param log_type
  5616. * @return See pc.hpp::e_additem_result
  5617. */
  5618. enum e_additem_result pc_cart_additem(map_session_data *sd,struct item *item,int32 amount,e_log_pick_type log_type)
  5619. {
  5620. struct item_data *data;
  5621. int32 i,w;
  5622. nullpo_retr(ADDITEM_INVALID, sd);
  5623. nullpo_retr(ADDITEM_INVALID, item);
  5624. if(item->nameid == 0 || amount <= 0)
  5625. return ADDITEM_INVALID;
  5626. if (itemdb_ishatched_egg(item))
  5627. return ADDITEM_INVALID;
  5628. data = itemdb_search(item->nameid);
  5629. if( data->stack.cart && amount > data->stack.amount )
  5630. {// item stack limitation
  5631. return ADDITEM_STACKLIMIT;
  5632. }
  5633. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5634. { // Check item trade restrictions [Skotlex]
  5635. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5636. return ADDITEM_INVALID;
  5637. }
  5638. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5639. return ADDITEM_OVERWEIGHT;
  5640. i = MAX_CART;
  5641. if( itemdb_isstackable2(data) && !item->expire_time )
  5642. {
  5643. for (i = 0; i < MAX_CART; i++) {
  5644. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5645. && sd->cart.u.items_cart[i].bound == item->bound
  5646. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5647. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5648. )
  5649. break;
  5650. }
  5651. }
  5652. if( i < MAX_CART )
  5653. {// item already in cart, stack it
  5654. 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 ) )
  5655. return ADDITEM_OVERAMOUNT; // no slot
  5656. sd->cart.u.items_cart[i].amount += amount;
  5657. clif_cart_additem(sd,i,amount);
  5658. }
  5659. else
  5660. {// item not stackable or not present, add it
  5661. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5662. if( i == MAX_CART )
  5663. return ADDITEM_OVERAMOUNT; // no slot
  5664. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5665. sd->cart.u.items_cart[i].id = 0;
  5666. sd->cart.u.items_cart[i].amount = amount;
  5667. sd->cart_num++;
  5668. clif_cart_additem(sd,i,amount);
  5669. }
  5670. sd->cart.u.items_cart[i].favorite = 0; // clear
  5671. sd->cart.u.items_cart[i].equipSwitch = 0;
  5672. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5673. sd->cart_weight += w;
  5674. clif_updatestatus(*sd,SP_CARTINFO);
  5675. return ADDITEM_SUCCESS;
  5676. }
  5677. /*==========================================
  5678. * Delete item on cart for given index.
  5679. *------------------------------------------*/
  5680. void pc_cart_delitem(map_session_data *sd,int32 n,int32 amount,int32 type,e_log_pick_type log_type)
  5681. {
  5682. nullpo_retv(sd);
  5683. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5684. sd->cart.u.items_cart[n].amount < amount)
  5685. return;
  5686. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5687. sd->cart.u.items_cart[n].amount -= amount;
  5688. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5689. if(sd->cart.u.items_cart[n].amount <= 0) {
  5690. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5691. sd->cart_num--;
  5692. }
  5693. if(!type) {
  5694. clif_cart_delitem( *sd, n, amount );
  5695. clif_updatestatus(*sd,SP_CARTINFO);
  5696. }
  5697. }
  5698. /*==========================================
  5699. * Transfer item from inventory to cart.
  5700. *------------------------------------------*/
  5701. void pc_putitemtocart(map_session_data *sd,int32 idx,int32 amount)
  5702. {
  5703. nullpo_retv(sd);
  5704. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5705. return;
  5706. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5707. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5708. return;
  5709. if( item_data->equipSwitch ){
  5710. clif_msg( *sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  5711. return;
  5712. }
  5713. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5714. if (flag == ADDITEM_SUCCESS)
  5715. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5716. else {
  5717. if (flag == ADDITEM_OVERAMOUNT)
  5718. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_COUNT );
  5719. else
  5720. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_WEIGHT );
  5721. clif_additem(sd, idx, amount, 0);
  5722. clif_delitem( *sd, idx, amount, 0 );
  5723. }
  5724. }
  5725. /*==========================================
  5726. * Get number of item in cart.
  5727. * Return:
  5728. -1 = itemid not found or no amount found
  5729. x = remaining itemid on cart after get
  5730. *------------------------------------------*/
  5731. int32 pc_cartitem_amount(map_session_data* sd, int32 idx, int32 amount)
  5732. {
  5733. struct item* item_data;
  5734. nullpo_retr(-1, sd);
  5735. item_data = &sd->cart.u.items_cart[idx];
  5736. if( item_data->nameid == 0 || item_data->amount == 0 )
  5737. return -1;
  5738. return item_data->amount - amount;
  5739. }
  5740. /*==========================================
  5741. * Retrieve an item at index idx from cart.
  5742. *------------------------------------------*/
  5743. bool pc_getitemfromcart(map_session_data *sd,int32 idx,int32 amount)
  5744. {
  5745. nullpo_retr(1, sd);
  5746. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5747. return false;
  5748. struct item *item_data=&sd->cart.u.items_cart[idx];
  5749. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5750. return false;
  5751. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5752. if (flag == ADDITEM_SUCCESS)
  5753. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5754. else {
  5755. clif_cart_delitem( *sd, idx, amount );
  5756. clif_additem(sd, idx, amount, flag);
  5757. clif_cart_additem(sd, idx, amount);
  5758. }
  5759. return true;
  5760. }
  5761. /*==========================================
  5762. * Bound Item Check
  5763. * Type:
  5764. * 1 Account Bound
  5765. * 2 Guild Bound
  5766. * 3 Party Bound
  5767. * 4 Character Bound
  5768. *------------------------------------------*/
  5769. int32 pc_bound_chk(TBL_PC *sd,enum bound_type type,int32 *idxlist)
  5770. {
  5771. int32 i = 0, j = 0;
  5772. for(i = 0; i < MAX_INVENTORY; i++) {
  5773. 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) {
  5774. idxlist[j] = i;
  5775. j++;
  5776. }
  5777. }
  5778. return j;
  5779. }
  5780. /*==========================================
  5781. * Display item stolen msg to player sd
  5782. *------------------------------------------*/
  5783. int32 pc_show_steal(struct block_list *bl,va_list ap)
  5784. {
  5785. map_session_data *sd;
  5786. t_itemid itemid;
  5787. char output[100];
  5788. sd=va_arg(ap,map_session_data *);
  5789. itemid=va_arg(ap,int32);
  5790. std::shared_ptr<item_data> id = item_db.find(itemid);
  5791. if(id == nullptr)
  5792. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5793. else
  5794. sprintf(output,"%s stole %s.",sd->status.name,id->ename.c_str());
  5795. clif_displaymessage( ((map_session_data *)bl)->fd, output);
  5796. return 0;
  5797. }
  5798. /**
  5799. * Steal an item from bl (mob).
  5800. * @param sd: Player data
  5801. * @param bl: Object to steal from
  5802. * @param skill_lv: Level of skill used
  5803. * @return True on success or false otherwise
  5804. */
  5805. bool pc_steal_item(map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5806. {
  5807. int32 i;
  5808. t_itemid itemid;
  5809. double rate;
  5810. unsigned char flag = 0;
  5811. struct mob_data *md;
  5812. if(!sd || !bl || bl->type!=BL_MOB)
  5813. return false;
  5814. md = (TBL_MOB *)bl;
  5815. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5816. return false;
  5817. status_data* sd_status = status_get_status_data(sd->bl);
  5818. status_data* md_status = status_get_status_data(*bl);
  5819. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5820. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5821. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5822. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5823. ) { //Can't steal from
  5824. md->state.steal_flag = UCHAR_MAX;
  5825. return false;
  5826. }
  5827. // base skill success chance (percentual)
  5828. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5829. rate += sd->bonus.add_steal_rate;
  5830. if( rate < 1
  5831. #ifdef RENEWAL
  5832. || rnd()%100 >= rate
  5833. #endif
  5834. )
  5835. return false;
  5836. // Try dropping one item, in the order from first to last possible slot.
  5837. // Droprate is affected by the skill success rate.
  5838. for( i = 0; i < MAX_MOB_DROP; i++ )
  5839. if( item_db.exists(md->db->dropitem[i].nameid) && !md->db->dropitem[i].steal_protected && rnd() % 10000 < md->db->dropitem[i].rate
  5840. #ifndef RENEWAL
  5841. * rate/100.
  5842. #endif
  5843. )
  5844. break;
  5845. if( i == MAX_MOB_DROP )
  5846. return false;
  5847. itemid = md->db->dropitem[i].nameid;
  5848. struct item tmp_item = {};
  5849. tmp_item.nameid = itemid;
  5850. tmp_item.amount = 1;
  5851. tmp_item.identify = itemdb_isidentified(itemid);
  5852. if( battle_config.skill_steal_random_options ){
  5853. mob_setdropitem_option( tmp_item, md->db->dropitem[i] );
  5854. }
  5855. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5856. //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?
  5857. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5858. if(flag) { //Failed to steal due to overweight
  5859. clif_additem(sd,0,0,flag);
  5860. return false;
  5861. }
  5862. if(battle_config.show_steal_in_same_party)
  5863. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5864. //Logs items, Stolen from mobs [Lupus]
  5865. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5866. //A Rare Steal Global Announce by Lupus
  5867. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5868. struct item_data *i_data;
  5869. char message[128];
  5870. i_data = itemdb_search(itemid);
  5871. 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);
  5872. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5873. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5874. }
  5875. return true;
  5876. }
  5877. /*==========================================
  5878. * Stole zeny from bl (mob)
  5879. * return
  5880. * 0 = fail
  5881. * 1 = success
  5882. *------------------------------------------*/
  5883. int32 pc_steal_coin(map_session_data *sd,struct block_list *target)
  5884. {
  5885. int32 rate, target_lv;
  5886. struct mob_data *md;
  5887. if(!sd || !target || target->type != BL_MOB)
  5888. return 0;
  5889. md = (TBL_MOB*)target;
  5890. target_lv = status_get_lv(target);
  5891. if (md->state.steal_coin_flag || md->sc.getSCE(SC_STONE) || md->sc.getSCE(SC_FREEZE) || md->sc.getSCE(SC_HANDICAPSTATE_FROSTBITE) ||
  5892. md->sc.getSCE(SC_HANDICAPSTATE_SWOONING) || md->sc.getSCE(SC_HANDICAPSTATE_LIGHTNINGSTRIKE) || md->sc.getSCE(SC_HANDICAPSTATE_CRYSTALLIZATION) ||
  5893. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5894. return 0;
  5895. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5896. if(rnd()%1000 < rate)
  5897. {
  5898. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5899. int32 amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5900. pc_getzeny(sd, amount, LOG_TYPE_STEAL);
  5901. md->state.steal_coin_flag = 1;
  5902. return 1;
  5903. }
  5904. return 0;
  5905. }
  5906. /*==========================================
  5907. * Set's a player position.
  5908. * @param sd
  5909. * @param mapindex
  5910. * @param x
  5911. * @param y
  5912. * @param clrtype
  5913. * @return SETPOS_OK Success
  5914. * SETPOS_MAPINDEX Invalid map index
  5915. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5916. * SETPOS_AUTOTRADE Player is in autotrade state
  5917. *------------------------------------------*/
  5918. enum e_setpos pc_setpos(map_session_data* sd, uint16 mapindex, int32 x, int32 y, clr_type clrtype)
  5919. {
  5920. nullpo_retr(SETPOS_OK,sd);
  5921. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5922. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5923. return SETPOS_MAPINDEX;
  5924. }
  5925. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5926. return SETPOS_AUTOTRADE;
  5927. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5928. pc_setstand(sd, true);
  5929. pc_setrestartvalue(sd,1);
  5930. }
  5931. int16 m = map_mapindex2mapid(mapindex);
  5932. struct map_data *mapdata = map_getmapdata(m);
  5933. status_change *sc = status_get_sc(&sd->bl);
  5934. sd->state.changemap = (sd->mapindex != mapindex);
  5935. sd->state.warping = 1;
  5936. sd->state.workinprogress = WIP_DISABLE_NONE;
  5937. sd->state.mail_writing = false;
  5938. sd->state.refineui_open = false;
  5939. if( sd->state.changemap ) { // Misc map-changing settings
  5940. int32 curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5941. if (curr_map_instance_id != new_map_instance_id) {
  5942. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5943. instance_delusers(curr_map_instance_id);
  5944. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5945. }
  5946. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5947. instance_addusers(new_map_instance_id);
  5948. }
  5949. if (sd->bg_id && mapdata && !mapdata->getMapFlag(MF_BATTLEGROUND)) // Moving to a map that isn't a Battlegrounds
  5950. bg_team_leave(sd, false, true);
  5951. sd->state.pmap = sd->bl.m;
  5952. if (sc != nullptr && !sc->empty()) { // Cancel some map related stuff.
  5953. if (sc->cant.warp)
  5954. return SETPOS_MAPINDEX; // You may not get out!
  5955. for (const auto &it : status_db) {
  5956. if (sc->getSCE(it.first)) {
  5957. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5958. status_change_end(&sd->bl, static_cast<sc_type>(it.first));
  5959. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5960. status_change_entry *sce = sd->sc.getSCE(it.first);
  5961. if (sce->timer != INVALID_TIMER)
  5962. delete_timer(sce->timer, status_change_timer);
  5963. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5964. }
  5965. }
  5966. }
  5967. }
  5968. for(int32 i = 0; i < EQI_MAX; i++ ) {
  5969. if( sd->equip_index[i] >= 0 )
  5970. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  5971. pc_unequipitem(sd,sd->equip_index[i],2);
  5972. }
  5973. }
  5974. if (battle_config.clear_unit_onwarp&BL_PC)
  5975. skill_clear_unitgroup(&sd->bl);
  5976. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5977. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5978. }
  5979. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5980. guild_send_dot_remove(sd);
  5981. bg_send_dot_remove(sd);
  5982. if (sd->regen.state.gc)
  5983. sd->regen.state.gc = 0;
  5984. if (mapdata) {
  5985. // make sure vending is allowed here
  5986. if (sd->state.vending && mapdata->getMapFlag(MF_NOVENDING)) {
  5987. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5988. vending_closevending(sd);
  5989. }
  5990. // make sure buyingstore is allowed here
  5991. if (sd->state.buyingstore && mapdata->getMapFlag(MF_NOBUYINGSTORE)) {
  5992. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5993. buyingstore_close(sd);
  5994. }
  5995. }
  5996. channel_pcquit(sd,4); //quit map chan
  5997. // Remove Cloaked NPCs on map change
  5998. sd->cloaked_npc.clear();
  5999. }
  6000. if( m < 0 )
  6001. {
  6002. uint32 ip;
  6003. uint16 port;
  6004. struct script_state *st;
  6005. //if can't find any map-servers, just abort setting position.
  6006. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  6007. return SETPOS_NO_MAPSERVER;
  6008. if (sd->npc_id){
  6009. npc_event_dequeue(sd,false);
  6010. st = sd->st;
  6011. }else{
  6012. st = nullptr;
  6013. }
  6014. if (sd->bg_id) // Switching map servers, remove from bg
  6015. bg_team_leave(sd, false, true);
  6016. if (sd->state.vending) // Stop vending
  6017. vending_closevending(sd);
  6018. if (sd->state.buyingstore) // Stop buyingstore
  6019. buyingstore_close(sd);
  6020. npc_script_event( *sd, NPCE_LOGOUT );
  6021. //remove from map, THEN change x/y coordinates
  6022. unit_remove_map_pc(sd,clrtype);
  6023. sd->mapindex = mapindex;
  6024. sd->bl.x=x;
  6025. sd->bl.y=y;
  6026. pc_clean_skilltree(sd);
  6027. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  6028. chrif_changemapserver(sd, ip, (int16)port);
  6029. //Free session data from this map server [Kevin]
  6030. unit_free_pc(sd);
  6031. if( st ){
  6032. // Has to be done here, because otherwise unit_free_pc will free the stack already
  6033. st->state = END;
  6034. }
  6035. return SETPOS_OK;
  6036. }
  6037. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  6038. {
  6039. 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);
  6040. x = y = 0; // make it random
  6041. }
  6042. if( x == 0 && y == 0 ) { // pick a random walkable cell
  6043. int32 c=0;
  6044. do {
  6045. x = rnd()%(mapdata->xs-2)+1;
  6046. y = rnd()%(mapdata->ys-2)+1;
  6047. c++;
  6048. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  6049. 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));
  6050. return SETPOS_OK; //preventing warp
  6051. //break; //allow warp anyway
  6052. }
  6053. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  6054. }
  6055. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  6056. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  6057. vending_closevending(sd);
  6058. }
  6059. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  6060. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  6061. buyingstore_close(sd);
  6062. }
  6063. if(sd->bl.prev != nullptr){
  6064. unit_remove_map_pc(sd,clrtype);
  6065. clif_changemap( *sd, m, x, y );
  6066. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  6067. sd->state.rewarp = 1;
  6068. sd->mapindex = mapindex;
  6069. sd->bl.m = m;
  6070. sd->bl.x = sd->ud.to_x = x;
  6071. sd->bl.y = sd->ud.to_y = y;
  6072. if( sd->status.guild_id > 0 && mapdata->getMapFlag(MF_GVG_CASTLE) )
  6073. { // Increased guild castle regen [Valaris]
  6074. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  6075. if(gc && gc->guild_id == sd->status.guild_id)
  6076. sd->regen.state.gc = 1;
  6077. }
  6078. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  6079. {
  6080. sd->pd->bl.m = m;
  6081. sd->pd->bl.x = sd->pd->ud.to_x = x;
  6082. sd->pd->bl.y = sd->pd->ud.to_y = y;
  6083. sd->pd->ud.dir = sd->ud.dir;
  6084. }
  6085. if( hom_is_active(sd->hd) )
  6086. {
  6087. if (sd->state.changemap)
  6088. status_db.removeByStatusFlag(&sd->hd->bl, { SCF_REMOVEFROMHOMONMAPWARP });
  6089. else
  6090. status_db.removeByStatusFlag(&sd->hd->bl, { SCF_REMOVEFROMHOMONWARP });
  6091. if (battle_config.hom_delay_reset_warp) {
  6092. skill_blockhomun_clear(*sd->hd);
  6093. }
  6094. sd->hd->bl.m = m;
  6095. sd->hd->bl.x = sd->hd->ud.to_x = x;
  6096. sd->hd->bl.y = sd->hd->ud.to_y = y;
  6097. sd->hd->ud.dir = sd->ud.dir;
  6098. }
  6099. if( sd->md )
  6100. {
  6101. sd->md->bl.m = m;
  6102. sd->md->bl.x = sd->md->ud.to_x = x;
  6103. sd->md->bl.y = sd->md->ud.to_y = y;
  6104. sd->md->ud.dir = sd->ud.dir;
  6105. }
  6106. if( sd->ed ) {
  6107. sd->ed->bl.m = m;
  6108. sd->ed->bl.x = sd->ed->ud.to_x = x;
  6109. sd->ed->bl.y = sd->ed->ud.to_y = y;
  6110. sd->ed->ud.dir = sd->ud.dir;
  6111. }
  6112. pc_cell_basilica(sd);
  6113. //check if we gonna be rewarped [lighta]
  6114. if(npc_check_areanpc(1,m,x,y,0)){
  6115. sd->count_rewarp++;
  6116. }
  6117. else
  6118. sd->count_rewarp = 0;
  6119. if (sd->state.vending)
  6120. vending_update(*sd);
  6121. if (sd->state.buyingstore)
  6122. buyingstore_update(*sd);
  6123. return SETPOS_OK;
  6124. }
  6125. enum e_setpos pc_setpos_savepoint( map_session_data& sd, clr_type clrtype ){
  6126. struct map_data *mapdata = map_getmapdata( sd.bl.m );
  6127. if( mapdata != nullptr && mapdata->getMapFlag(MF_NOSAVE) && mapdata->save.map ){
  6128. return pc_setpos( &sd, mapdata->save.map, mapdata->save.x, mapdata->save.y, clrtype );
  6129. }else{
  6130. return pc_setpos( &sd, mapindex_name2id( sd.status.save_point.map ), sd.status.save_point.x, sd.status.save_point.y, clrtype );
  6131. }
  6132. }
  6133. /*==========================================
  6134. * Warp player sd to random location on current map.
  6135. * May fail if no walkable cell found (1000 attempts).
  6136. * Return:
  6137. * 0 = Success
  6138. * 1,2,3 = Fail
  6139. *------------------------------------------*/
  6140. char pc_randomwarp(map_session_data *sd, clr_type type, bool ignore_mapflag)
  6141. {
  6142. int16 x,y,i=0;
  6143. nullpo_ret(sd);
  6144. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6145. if (mapdata->getMapFlag(MF_NOTELEPORT) && !ignore_mapflag) //Teleport forbidden
  6146. return 3;
  6147. int32 edge = battle_config.map_edge_size;
  6148. do {
  6149. x = rnd_value<int16>(edge, mapdata->xs - edge - 1);
  6150. y = rnd_value<int16>(edge, mapdata->ys - edge - 1);
  6151. } 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);
  6152. if (i < 1000)
  6153. return pc_setpos(sd,mapdata->index,x,y,type);
  6154. return 3;
  6155. }
  6156. /*==========================================
  6157. * Records a memo point at sd's current position
  6158. * pos - entry to replace, (-1: shift oldest entry out)
  6159. *------------------------------------------*/
  6160. bool pc_memo(map_session_data* sd, int32 pos)
  6161. {
  6162. int32 skill;
  6163. nullpo_ret(sd);
  6164. // check mapflags
  6165. 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) ) {
  6166. clif_skill_teleportmessage( *sd, NOTIFY_MAPINFO_CANT_MEMO ); // "Saved point cannot be memorized."
  6167. return false;
  6168. }
  6169. // check inputs
  6170. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  6171. return false; // invalid input
  6172. // check required skill level
  6173. skill = pc_checkskill(sd, AL_WARP);
  6174. if( skill < 1 ) {
  6175. clif_skill_memomessage( *sd, WARPPOINT_NOT_LEARNED ); // "You haven't learned Warp."
  6176. return false;
  6177. }
  6178. if( skill < 2 || skill - 2 < pos ) {
  6179. clif_skill_memomessage( *sd, WARPPOINT_LOW_LEVEL ); // "Skill Level is not high enough."
  6180. return false;
  6181. }
  6182. if( pos == -1 )
  6183. {
  6184. uint8 i;
  6185. const char* mapname = map_mapid2mapname( sd->bl.m );
  6186. // prevent memo-ing the same map multiple times
  6187. ARR_FIND( 0, MAX_MEMOPOINTS, i, strncmp( sd->status.memo_point[i].map, mapname, sizeof( sd->status.memo_point[i].map ) ) == 0 );
  6188. memmove( &sd->status.memo_point[1], &sd->status.memo_point[0], ( u8min( i, MAX_MEMOPOINTS - 1 ) ) * sizeof( struct s_point_str ) );
  6189. pos = 0;
  6190. }
  6191. if( map_getmapdata(sd->bl.m)->instance_id ) {
  6192. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  6193. return false;
  6194. }
  6195. safestrncpy( sd->status.memo_point[pos].map, map_mapid2mapname( sd->bl.m ), sizeof( sd->status.memo_point[pos].map ) );
  6196. sd->status.memo_point[pos].x = sd->bl.x;
  6197. sd->status.memo_point[pos].y = sd->bl.y;
  6198. clif_skill_memomessage( *sd, WARPPOINT_SUCCESS );
  6199. return true;
  6200. }
  6201. //
  6202. // Skills
  6203. //
  6204. /**
  6205. * Get the skill current cooldown for player.
  6206. * (get the db base cooldown for skill + player specific cooldown)
  6207. * @param sd : player pointer
  6208. * @param id : skill id
  6209. * @param lv : skill lv
  6210. * @return player skill cooldown
  6211. */
  6212. int32 pc_get_skillcooldown(map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  6213. if (skill_id == SJ_NOVAEXPLOSING) {
  6214. status_change *sc = status_get_sc(&sd->bl);
  6215. if (sc && sc->getSCE(SC_DIMENSION))
  6216. return 0;
  6217. }
  6218. int32 cooldown = skill_get_cooldown(skill_id, skill_lv);
  6219. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  6220. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  6221. for (auto &it : sd->skillcooldown) {
  6222. if (it.id == skill_id) {
  6223. cooldown += it.val;
  6224. break;
  6225. }
  6226. }
  6227. return max(0, cooldown);
  6228. }
  6229. /*==========================================
  6230. * Return player sd skill_lv learned for given skill
  6231. *------------------------------------------*/
  6232. uint8 pc_checkskill(map_session_data *sd, uint16 skill_id)
  6233. {
  6234. uint16 idx = 0;
  6235. if (sd == nullptr)
  6236. return 0;
  6237. #ifdef RENEWAL
  6238. if ((idx = skill_get_index(skill_id)) == 0) {
  6239. #else
  6240. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  6241. if( skill_id >= RK_ENCHANTBLADE ){
  6242. // Silently fail for now -> future update planned
  6243. return 0;
  6244. }
  6245. #endif
  6246. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  6247. return 0;
  6248. }
  6249. if (SKILL_CHK_GUILD(skill_id) ) {
  6250. if (sd->status.guild_id>0 && sd->guild)
  6251. return guild_checkskill(sd->guild->guild,skill_id);
  6252. return 0;
  6253. }
  6254. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  6255. }
  6256. /**
  6257. * Returns the flag of the given skill (when learned).
  6258. * @param sd: Player data
  6259. * @param skill_id: Skill to lookup
  6260. * @return Skill flag type
  6261. */
  6262. e_skill_flag pc_checkskill_flag(map_session_data &sd, uint16 skill_id) {
  6263. uint16 idx;
  6264. #ifdef RENEWAL
  6265. if ((idx = skill_get_index(skill_id)) == 0) {
  6266. #else
  6267. if ((idx = skill_db.get_index(skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__)) == 0) {
  6268. if (skill_id >= RK_ENCHANTBLADE) {
  6269. // Silently fail for now -> future update planned
  6270. return SKILL_FLAG_NONE;
  6271. }
  6272. #endif
  6273. ShowError("pc_checkskill_flag: Invalid skill id %d (char_id=%d).\n", skill_id, sd.status.char_id);
  6274. return SKILL_FLAG_NONE;
  6275. }
  6276. 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;
  6277. }
  6278. /**
  6279. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  6280. * @param sd: Player data
  6281. * @param type: Summoner Power Type
  6282. * @return Skill points invested
  6283. */
  6284. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  6285. if (sd == nullptr)
  6286. return 0;
  6287. uint8 count = 0;
  6288. switch (type) {
  6289. case SUMMONER_POWER_SEA:
  6290. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  6291. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  6292. break;
  6293. case SUMMONER_POWER_LAND:
  6294. 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) +
  6295. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  6296. break;
  6297. case SUMMONER_POWER_LIFE:
  6298. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  6299. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  6300. break;
  6301. }
  6302. return count;
  6303. }
  6304. /**
  6305. * Checks for Imperial Guard's passive skills.
  6306. * @param sd: Player data
  6307. * @param flag:
  6308. * Flag&1 = IG_SHIELD_MASTERY
  6309. * Flag&2 = IG_SPEAR_SWORD_M
  6310. */
  6311. uint8 pc_checkskill_imperial_guard(map_session_data *sd, int16 flag)
  6312. {
  6313. nullpo_retr(0, sd);
  6314. uint8 count = 0;
  6315. if (flag&1 && sd->status.shield > 0)
  6316. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  6317. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  6318. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  6319. return count;
  6320. }
  6321. /**
  6322. * Check if we still have the correct weapon to continue the skill (actually status)
  6323. * If not ending it
  6324. * @param sd
  6325. * @return 0:error, 1:check done
  6326. */
  6327. static void pc_checkallowskill(map_session_data *sd)
  6328. {
  6329. nullpo_retv(sd);
  6330. if(sd->sc.empty())
  6331. return;
  6332. for (const auto &it : status_db) {
  6333. sc_type status = it.second->type;
  6334. std::bitset<SCF_MAX> flag = it.second->flag;
  6335. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  6336. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  6337. continue;
  6338. if (sd->sc.getSCE(status) && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  6339. status_change_end(&sd->bl, status);
  6340. }
  6341. if (flag[SCF_REQUIRENOWEAPON]) {
  6342. if (sd->sc.getSCE(status) && sd->status.weapon)
  6343. status_change_end( &sd->bl, status );
  6344. }
  6345. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  6346. if (sd->sc.getSCE(status) && sd->status.shield <= 0)
  6347. status_change_end(&sd->bl, status);
  6348. }
  6349. }
  6350. }
  6351. /*==========================================
  6352. * Return equipped index of item on player sd at pos
  6353. * Return
  6354. * -1 : Nothing equipped
  6355. * idx : (this index could be used in inventory to found item_data)
  6356. *------------------------------------------*/
  6357. int16 pc_checkequip(map_session_data *sd,int32 pos, bool checkall)
  6358. {
  6359. uint8 i;
  6360. nullpo_retr(-1, sd);
  6361. for(i=0;i<EQI_MAX;i++){
  6362. if(pos & equip_bitmask[i]){
  6363. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  6364. // Check all if any match is found
  6365. continue;
  6366. }
  6367. return sd->equip_index[i];
  6368. }
  6369. }
  6370. return -1;
  6371. }
  6372. /*==========================================
  6373. * Check if sd has nameid equipped somewhere
  6374. * @sd : the player session
  6375. * @nameid : id of the item to check
  6376. * @min : : see pc.hpp enum equip_index from ? to @max
  6377. * @max : see pc.hpp enum equip_index for @min to ?
  6378. * -return true,false
  6379. *------------------------------------------*/
  6380. bool pc_checkequip2(map_session_data *sd, t_itemid nameid, int32 min, int32 max)
  6381. {
  6382. int32 i;
  6383. for(i = min; i < max; i++) {
  6384. if(equip_bitmask[i]) {
  6385. int32 idx = sd->equip_index[i];
  6386. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  6387. return true;
  6388. }
  6389. }
  6390. return false;
  6391. }
  6392. /*==========================================
  6393. * Convert's from the client's lame Job ID system
  6394. * to the map server's 'makes sense' system. [Skotlex]
  6395. *------------------------------------------*/
  6396. uint64 pc_jobid2mapid(uint16 b_class)
  6397. {
  6398. switch(b_class)
  6399. {
  6400. //Novice And 1-1 Jobs
  6401. case JOB_NOVICE: return MAPID_NOVICE;
  6402. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  6403. case JOB_MAGE: return MAPID_MAGE;
  6404. case JOB_ARCHER: return MAPID_ARCHER;
  6405. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  6406. case JOB_MERCHANT: return MAPID_MERCHANT;
  6407. case JOB_THIEF: return MAPID_THIEF;
  6408. case JOB_TAEKWON: return MAPID_TAEKWON;
  6409. case JOB_WEDDING: return MAPID_WEDDING;
  6410. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  6411. case JOB_NINJA: return MAPID_NINJA;
  6412. case JOB_XMAS: return MAPID_XMAS;
  6413. case JOB_SUMMER: return MAPID_SUMMER;
  6414. case JOB_HANBOK: return MAPID_HANBOK;
  6415. case JOB_GANGSI: return MAPID_GANGSI;
  6416. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  6417. case JOB_SUMMER2: return MAPID_SUMMER2;
  6418. //2-1 Jobs
  6419. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  6420. case JOB_KNIGHT: return MAPID_KNIGHT;
  6421. case JOB_WIZARD: return MAPID_WIZARD;
  6422. case JOB_HUNTER: return MAPID_HUNTER;
  6423. case JOB_PRIEST: return MAPID_PRIEST;
  6424. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  6425. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  6426. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  6427. case JOB_KAGEROU:
  6428. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  6429. case JOB_REBELLION: return MAPID_REBELLION;
  6430. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  6431. //2-2 Jobs
  6432. case JOB_CRUSADER: return MAPID_CRUSADER;
  6433. case JOB_SAGE: return MAPID_SAGE;
  6434. case JOB_BARD:
  6435. case JOB_DANCER: return MAPID_BARDDANCER;
  6436. case JOB_MONK: return MAPID_MONK;
  6437. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  6438. case JOB_ROGUE: return MAPID_ROGUE;
  6439. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  6440. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  6441. //Trans Novice And Trans 1-1 Jobs
  6442. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  6443. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  6444. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  6445. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  6446. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  6447. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  6448. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  6449. //Trans 2-1 Jobs
  6450. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  6451. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  6452. case JOB_SNIPER: return MAPID_SNIPER;
  6453. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  6454. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6455. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6456. //Trans 2-2 Jobs
  6457. case JOB_PALADIN: return MAPID_PALADIN;
  6458. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6459. case JOB_CLOWN:
  6460. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6461. case JOB_CHAMPION: return MAPID_CHAMPION;
  6462. case JOB_CREATOR: return MAPID_CREATOR;
  6463. case JOB_STALKER: return MAPID_STALKER;
  6464. //Baby Novice And Baby 1-1 Jobs
  6465. case JOB_BABY: return MAPID_BABY;
  6466. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6467. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6468. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6469. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6470. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6471. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6472. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6473. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6474. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6475. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6476. //Baby 2-1 Jobs
  6477. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6478. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6479. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6480. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6481. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6482. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6483. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6484. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6485. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6486. case JOB_BABY_KAGEROU:
  6487. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6488. //Baby 2-2 Jobs
  6489. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6490. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6491. case JOB_BABY_BARD:
  6492. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6493. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6494. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6495. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6496. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6497. //3-1 Jobs
  6498. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6499. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6500. case JOB_WARLOCK: return MAPID_WARLOCK;
  6501. case JOB_RANGER: return MAPID_RANGER;
  6502. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6503. case JOB_MECHANIC: return MAPID_MECHANIC;
  6504. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6505. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6506. //3-2 Jobs
  6507. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6508. case JOB_SORCERER: return MAPID_SORCERER;
  6509. case JOB_MINSTREL:
  6510. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6511. case JOB_SURA: return MAPID_SURA;
  6512. case JOB_GENETIC: return MAPID_GENETIC;
  6513. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6514. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6515. //Trans 3-1 Jobs
  6516. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6517. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6518. case JOB_RANGER_T: return MAPID_RANGER_T;
  6519. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6520. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6521. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6522. //Trans 3-2 Jobs
  6523. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6524. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6525. case JOB_MINSTREL_T:
  6526. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6527. case JOB_SURA_T: return MAPID_SURA_T;
  6528. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6529. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6530. //Baby 3-1 Jobs
  6531. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6532. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6533. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6534. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6535. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6536. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6537. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6538. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6539. //Baby 3-2 Jobs
  6540. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6541. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6542. case JOB_BABY_MINSTREL:
  6543. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6544. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6545. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6546. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6547. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6548. //Doram Jobs
  6549. case JOB_SUMMONER: return MAPID_SUMMONER;
  6550. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6551. //4-1 Jobs
  6552. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6553. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6554. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6555. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6556. case JOB_CARDINAL: return MAPID_CARDINAL;
  6557. case JOB_MEISTER: return MAPID_MEISTER;
  6558. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6559. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6560. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6561. case JOB_SHINKIRO:
  6562. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6563. //4-2 Jobs
  6564. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6565. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6566. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6567. case JOB_TROUBADOUR:
  6568. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6569. case JOB_BIOLO: return MAPID_BIOLO;
  6570. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6571. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6572. //Unknown
  6573. default:
  6574. return -1;
  6575. }
  6576. }
  6577. //Reverts the map-style class id to the client-style one.
  6578. int32 pc_mapid2jobid(uint64 class_, int32 sex)
  6579. {
  6580. switch(class_) {
  6581. //Novice And 1-1 Jobs
  6582. case MAPID_NOVICE: return JOB_NOVICE;
  6583. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6584. case MAPID_MAGE: return JOB_MAGE;
  6585. case MAPID_ARCHER: return JOB_ARCHER;
  6586. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6587. case MAPID_MERCHANT: return JOB_MERCHANT;
  6588. case MAPID_THIEF: return JOB_THIEF;
  6589. case MAPID_TAEKWON: return JOB_TAEKWON;
  6590. case MAPID_WEDDING: return JOB_WEDDING;
  6591. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6592. case MAPID_NINJA: return JOB_NINJA;
  6593. case MAPID_XMAS: return JOB_XMAS;
  6594. case MAPID_SUMMER: return JOB_SUMMER;
  6595. case MAPID_HANBOK: return JOB_HANBOK;
  6596. case MAPID_GANGSI: return JOB_GANGSI;
  6597. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6598. case MAPID_SUMMER2: return JOB_SUMMER2;
  6599. //2-1 Jobs
  6600. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6601. case MAPID_KNIGHT: return JOB_KNIGHT;
  6602. case MAPID_WIZARD: return JOB_WIZARD;
  6603. case MAPID_HUNTER: return JOB_HUNTER;
  6604. case MAPID_PRIEST: return JOB_PRIEST;
  6605. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6606. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6607. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6608. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6609. case MAPID_REBELLION: return JOB_REBELLION;
  6610. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6611. //2-2 Jobs
  6612. case MAPID_CRUSADER: return JOB_CRUSADER;
  6613. case MAPID_SAGE: return JOB_SAGE;
  6614. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6615. case MAPID_MONK: return JOB_MONK;
  6616. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6617. case MAPID_ROGUE: return JOB_ROGUE;
  6618. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6619. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6620. //Trans Novice And Trans 2-1 Jobs
  6621. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6622. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6623. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6624. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6625. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6626. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6627. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6628. //Trans 2-1 Jobs
  6629. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6630. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6631. case MAPID_SNIPER: return JOB_SNIPER;
  6632. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6633. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6634. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6635. //Trans 2-2 Jobs
  6636. case MAPID_PALADIN: return JOB_PALADIN;
  6637. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6638. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6639. case MAPID_CHAMPION: return JOB_CHAMPION;
  6640. case MAPID_CREATOR: return JOB_CREATOR;
  6641. case MAPID_STALKER: return JOB_STALKER;
  6642. //Baby Novice And Baby 1-1 Jobs
  6643. case MAPID_BABY: return JOB_BABY;
  6644. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6645. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6646. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6647. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6648. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6649. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6650. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6651. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6652. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6653. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6654. //Baby 2-1 Jobs
  6655. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6656. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6657. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6658. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6659. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6660. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6661. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6662. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6663. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6664. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6665. //Baby 2-2 Jobs
  6666. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6667. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6668. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6669. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6670. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6671. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6672. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6673. //3-1 Jobs
  6674. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6675. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6676. case MAPID_WARLOCK: return JOB_WARLOCK;
  6677. case MAPID_RANGER: return JOB_RANGER;
  6678. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6679. case MAPID_MECHANIC: return JOB_MECHANIC;
  6680. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6681. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6682. //3-2 Jobs
  6683. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6684. case MAPID_SORCERER: return JOB_SORCERER;
  6685. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6686. case MAPID_SURA: return JOB_SURA;
  6687. case MAPID_GENETIC: return JOB_GENETIC;
  6688. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6689. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6690. //Trans 3-1 Jobs
  6691. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6692. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6693. case MAPID_RANGER_T: return JOB_RANGER_T;
  6694. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6695. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6696. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6697. //Trans 3-2 Jobs
  6698. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6699. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6700. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6701. case MAPID_SURA_T: return JOB_SURA_T;
  6702. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6703. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6704. //Baby 3-1 Jobs
  6705. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6706. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6707. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6708. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6709. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6710. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6711. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6712. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6713. //Baby 3-2 Jobs
  6714. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6715. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6716. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6717. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6718. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6719. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6720. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6721. //Doram Jobs
  6722. case MAPID_SUMMONER: return JOB_SUMMONER;
  6723. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6724. //4-1 Jobs
  6725. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6726. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6727. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6728. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6729. case MAPID_CARDINAL: return JOB_CARDINAL;
  6730. case MAPID_MEISTER: return JOB_MEISTER;
  6731. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6732. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6733. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6734. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6735. //4-2 Jobs
  6736. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6737. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6738. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6739. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6740. case MAPID_BIOLO: return JOB_BIOLO;
  6741. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6742. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6743. //Unknown
  6744. default:
  6745. return -1;
  6746. }
  6747. }
  6748. /*====================================================
  6749. * This function return the name of the job (by [Yor])
  6750. *----------------------------------------------------*/
  6751. const char* job_name(int32 class_)
  6752. {
  6753. switch (class_) {
  6754. case JOB_NOVICE:
  6755. case JOB_SWORDMAN:
  6756. case JOB_MAGE:
  6757. case JOB_ARCHER:
  6758. case JOB_ACOLYTE:
  6759. case JOB_MERCHANT:
  6760. case JOB_THIEF:
  6761. return msg_txt(nullptr,550 - JOB_NOVICE+class_);
  6762. case JOB_KNIGHT:
  6763. case JOB_PRIEST:
  6764. case JOB_WIZARD:
  6765. case JOB_BLACKSMITH:
  6766. case JOB_HUNTER:
  6767. case JOB_ASSASSIN:
  6768. return msg_txt(nullptr,557 - JOB_KNIGHT+class_);
  6769. case JOB_KNIGHT2:
  6770. return msg_txt(nullptr,557);
  6771. case JOB_CRUSADER:
  6772. case JOB_MONK:
  6773. case JOB_SAGE:
  6774. case JOB_ROGUE:
  6775. case JOB_ALCHEMIST:
  6776. case JOB_BARD:
  6777. case JOB_DANCER:
  6778. return msg_txt(nullptr,563 - JOB_CRUSADER+class_);
  6779. case JOB_CRUSADER2:
  6780. return msg_txt(nullptr,563);
  6781. case JOB_WEDDING:
  6782. case JOB_SUPER_NOVICE:
  6783. case JOB_GUNSLINGER:
  6784. case JOB_NINJA:
  6785. case JOB_XMAS:
  6786. return msg_txt(nullptr,570 - JOB_WEDDING+class_);
  6787. case JOB_SUMMER:
  6788. case JOB_SUMMER2:
  6789. return msg_txt(nullptr,621);
  6790. case JOB_HANBOK:
  6791. return msg_txt(nullptr,694);
  6792. case JOB_OKTOBERFEST:
  6793. return msg_txt(nullptr,696);
  6794. case JOB_NOVICE_HIGH:
  6795. case JOB_SWORDMAN_HIGH:
  6796. case JOB_MAGE_HIGH:
  6797. case JOB_ARCHER_HIGH:
  6798. case JOB_ACOLYTE_HIGH:
  6799. case JOB_MERCHANT_HIGH:
  6800. case JOB_THIEF_HIGH:
  6801. return msg_txt(nullptr,575 - JOB_NOVICE_HIGH+class_);
  6802. case JOB_LORD_KNIGHT:
  6803. case JOB_HIGH_PRIEST:
  6804. case JOB_HIGH_WIZARD:
  6805. case JOB_WHITESMITH:
  6806. case JOB_SNIPER:
  6807. case JOB_ASSASSIN_CROSS:
  6808. return msg_txt(nullptr,582 - JOB_LORD_KNIGHT+class_);
  6809. case JOB_LORD_KNIGHT2:
  6810. return msg_txt(nullptr,582);
  6811. case JOB_PALADIN:
  6812. case JOB_CHAMPION:
  6813. case JOB_PROFESSOR:
  6814. case JOB_STALKER:
  6815. case JOB_CREATOR:
  6816. case JOB_CLOWN:
  6817. case JOB_GYPSY:
  6818. return msg_txt(nullptr,588 - JOB_PALADIN + class_);
  6819. case JOB_PALADIN2:
  6820. return msg_txt(nullptr,588);
  6821. case JOB_BABY:
  6822. case JOB_BABY_SWORDMAN:
  6823. case JOB_BABY_MAGE:
  6824. case JOB_BABY_ARCHER:
  6825. case JOB_BABY_ACOLYTE:
  6826. case JOB_BABY_MERCHANT:
  6827. case JOB_BABY_THIEF:
  6828. return msg_txt(nullptr,595 - JOB_BABY + class_);
  6829. case JOB_BABY_KNIGHT:
  6830. case JOB_BABY_PRIEST:
  6831. case JOB_BABY_WIZARD:
  6832. case JOB_BABY_BLACKSMITH:
  6833. case JOB_BABY_HUNTER:
  6834. case JOB_BABY_ASSASSIN:
  6835. return msg_txt(nullptr,602 - JOB_BABY_KNIGHT + class_);
  6836. case JOB_BABY_KNIGHT2:
  6837. return msg_txt(nullptr,602);
  6838. case JOB_BABY_CRUSADER:
  6839. case JOB_BABY_MONK:
  6840. case JOB_BABY_SAGE:
  6841. case JOB_BABY_ROGUE:
  6842. case JOB_BABY_ALCHEMIST:
  6843. case JOB_BABY_BARD:
  6844. case JOB_BABY_DANCER:
  6845. return msg_txt(nullptr,608 - JOB_BABY_CRUSADER + class_);
  6846. case JOB_BABY_CRUSADER2:
  6847. return msg_txt(nullptr,608);
  6848. case JOB_SUPER_BABY:
  6849. return msg_txt(nullptr,615);
  6850. case JOB_TAEKWON:
  6851. return msg_txt(nullptr,616);
  6852. case JOB_STAR_GLADIATOR:
  6853. case JOB_STAR_GLADIATOR2:
  6854. return msg_txt(nullptr,617);
  6855. case JOB_SOUL_LINKER:
  6856. return msg_txt(nullptr,618);
  6857. case JOB_GANGSI:
  6858. case JOB_DEATH_KNIGHT:
  6859. case JOB_DARK_COLLECTOR:
  6860. return msg_txt(nullptr,622 - JOB_GANGSI+class_);
  6861. case JOB_RUNE_KNIGHT:
  6862. case JOB_WARLOCK:
  6863. case JOB_RANGER:
  6864. case JOB_ARCH_BISHOP:
  6865. case JOB_MECHANIC:
  6866. case JOB_GUILLOTINE_CROSS:
  6867. return msg_txt(nullptr,625 - JOB_RUNE_KNIGHT+class_);
  6868. case JOB_RUNE_KNIGHT_T:
  6869. case JOB_WARLOCK_T:
  6870. case JOB_RANGER_T:
  6871. case JOB_ARCH_BISHOP_T:
  6872. case JOB_MECHANIC_T:
  6873. case JOB_GUILLOTINE_CROSS_T:
  6874. return msg_txt(nullptr,681 - JOB_RUNE_KNIGHT_T+class_);
  6875. case JOB_ROYAL_GUARD:
  6876. case JOB_SORCERER:
  6877. case JOB_MINSTREL:
  6878. case JOB_WANDERER:
  6879. case JOB_SURA:
  6880. case JOB_GENETIC:
  6881. case JOB_SHADOW_CHASER:
  6882. return msg_txt(nullptr,631 - JOB_ROYAL_GUARD+class_);
  6883. case JOB_ROYAL_GUARD_T:
  6884. case JOB_SORCERER_T:
  6885. case JOB_MINSTREL_T:
  6886. case JOB_WANDERER_T:
  6887. case JOB_SURA_T:
  6888. case JOB_GENETIC_T:
  6889. case JOB_SHADOW_CHASER_T:
  6890. return msg_txt(nullptr,687 - JOB_ROYAL_GUARD_T+class_);
  6891. case JOB_RUNE_KNIGHT2:
  6892. case JOB_RUNE_KNIGHT_T2:
  6893. return msg_txt(nullptr,625);
  6894. case JOB_ROYAL_GUARD2:
  6895. case JOB_ROYAL_GUARD_T2:
  6896. return msg_txt(nullptr,631);
  6897. case JOB_RANGER2:
  6898. case JOB_RANGER_T2:
  6899. return msg_txt(nullptr,627);
  6900. case JOB_MECHANIC2:
  6901. case JOB_MECHANIC_T2:
  6902. return msg_txt(nullptr,629);
  6903. case JOB_BABY_RUNE_KNIGHT:
  6904. case JOB_BABY_WARLOCK:
  6905. case JOB_BABY_RANGER:
  6906. case JOB_BABY_ARCH_BISHOP:
  6907. case JOB_BABY_MECHANIC:
  6908. case JOB_BABY_GUILLOTINE_CROSS:
  6909. case JOB_BABY_ROYAL_GUARD:
  6910. case JOB_BABY_SORCERER:
  6911. case JOB_BABY_MINSTREL:
  6912. case JOB_BABY_WANDERER:
  6913. case JOB_BABY_SURA:
  6914. case JOB_BABY_GENETIC:
  6915. case JOB_BABY_SHADOW_CHASER:
  6916. return msg_txt(nullptr,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6917. case JOB_BABY_RUNE_KNIGHT2:
  6918. return msg_txt(nullptr,638);
  6919. case JOB_BABY_ROYAL_GUARD2:
  6920. return msg_txt(nullptr,644);
  6921. case JOB_BABY_RANGER2:
  6922. return msg_txt(nullptr,640);
  6923. case JOB_BABY_MECHANIC2:
  6924. return msg_txt(nullptr,642);
  6925. case JOB_SUPER_NOVICE_E:
  6926. case JOB_SUPER_BABY_E:
  6927. return msg_txt(nullptr,651 - JOB_SUPER_NOVICE_E+class_);
  6928. case JOB_KAGEROU:
  6929. case JOB_OBORO:
  6930. return msg_txt(nullptr,653 - JOB_KAGEROU+class_);
  6931. case JOB_REBELLION:
  6932. return msg_txt(nullptr,695);
  6933. case JOB_SUMMONER:
  6934. return msg_txt(nullptr,697);
  6935. case JOB_BABY_SUMMONER:
  6936. return msg_txt(nullptr,698);
  6937. case JOB_BABY_NINJA:
  6938. return msg_txt(nullptr,699);
  6939. case JOB_BABY_KAGEROU:
  6940. case JOB_BABY_OBORO:
  6941. case JOB_BABY_TAEKWON:
  6942. case JOB_BABY_STAR_GLADIATOR:
  6943. case JOB_BABY_SOUL_LINKER:
  6944. case JOB_BABY_GUNSLINGER:
  6945. case JOB_BABY_REBELLION:
  6946. return msg_txt(nullptr,753 - JOB_BABY_KAGEROU+class_);
  6947. case JOB_BABY_STAR_GLADIATOR2:
  6948. return msg_txt(nullptr,756);
  6949. case JOB_STAR_EMPEROR:
  6950. case JOB_SOUL_REAPER:
  6951. case JOB_BABY_STAR_EMPEROR:
  6952. case JOB_BABY_SOUL_REAPER:
  6953. return msg_txt(nullptr,782 - JOB_STAR_EMPEROR + class_);
  6954. case JOB_STAR_EMPEROR2:
  6955. return msg_txt(nullptr,782);
  6956. case JOB_BABY_STAR_EMPEROR2:
  6957. return msg_txt(nullptr,784);
  6958. case JOB_DRAGON_KNIGHT:
  6959. case JOB_MEISTER:
  6960. case JOB_SHADOW_CROSS:
  6961. case JOB_ARCH_MAGE:
  6962. case JOB_CARDINAL:
  6963. case JOB_WINDHAWK:
  6964. case JOB_IMPERIAL_GUARD:
  6965. case JOB_BIOLO:
  6966. case JOB_ABYSS_CHASER:
  6967. case JOB_ELEMENTAL_MASTER:
  6968. case JOB_INQUISITOR:
  6969. case JOB_TROUBADOUR:
  6970. case JOB_TROUVERE:
  6971. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6972. case JOB_WINDHAWK2:
  6973. return msg_txt( nullptr, 805);
  6974. case JOB_MEISTER2:
  6975. return msg_txt( nullptr, 801 );
  6976. case JOB_DRAGON_KNIGHT2:
  6977. return msg_txt( nullptr, 800 );
  6978. case JOB_IMPERIAL_GUARD2:
  6979. return msg_txt( nullptr, 806 );
  6980. case JOB_SKY_EMPEROR:
  6981. case JOB_SOUL_ASCETIC:
  6982. case JOB_SHINKIRO:
  6983. case JOB_SHIRANUI:
  6984. case JOB_NIGHT_WATCH:
  6985. case JOB_HYPER_NOVICE:
  6986. case JOB_SPIRIT_HANDLER:
  6987. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6988. case JOB_SKY_EMPEROR2:
  6989. return msg_txt( nullptr, 813 );
  6990. default:
  6991. return msg_txt(nullptr,655);
  6992. }
  6993. }
  6994. /*====================================================
  6995. * Timered function to make id follow a target.
  6996. * @id = bl.id (player only atm)
  6997. * target is define in sd->followtarget (bl.id)
  6998. * used by pc_follow
  6999. *----------------------------------------------------*/
  7000. TIMER_FUNC(pc_follow_timer){
  7001. map_session_data *sd;
  7002. struct block_list *tbl;
  7003. sd = map_id2sd(id);
  7004. nullpo_ret(sd);
  7005. if (sd->followtimer != tid){
  7006. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  7007. sd->followtimer = INVALID_TIMER;
  7008. return 0;
  7009. }
  7010. sd->followtimer = INVALID_TIMER;
  7011. tbl = map_id2bl(sd->followtarget);
  7012. if (tbl == nullptr || pc_isdead(sd))
  7013. {
  7014. pc_stop_following(sd);
  7015. return 0;
  7016. }
  7017. // either player or target is currently detached from map blocks (could be teleporting),
  7018. // but still connected to this map, so we'll just increment the timer and check back later
  7019. if (sd->bl.prev != nullptr && tbl->prev != nullptr &&
  7020. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  7021. {
  7022. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, nullptr, nullptr)) {
  7023. if (!check_distance_bl(&sd->bl, tbl, 5))
  7024. unit_walktobl(&sd->bl, tbl, 5, 0);
  7025. } else
  7026. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  7027. }
  7028. sd->followtimer = add_timer(
  7029. tick + 1000, // increase time a bit to loosen up map's load
  7030. pc_follow_timer, sd->bl.id, 0);
  7031. return 0;
  7032. }
  7033. int32 pc_stop_following (map_session_data *sd)
  7034. {
  7035. nullpo_ret(sd);
  7036. if (sd->followtimer != INVALID_TIMER) {
  7037. delete_timer(sd->followtimer,pc_follow_timer);
  7038. sd->followtimer = INVALID_TIMER;
  7039. }
  7040. sd->followtarget = -1;
  7041. sd->ud.target_to = 0;
  7042. unit_stop_walking( &sd->bl, USW_FIXPOS );
  7043. return 0;
  7044. }
  7045. int32 pc_follow(map_session_data *sd,int32 target_id)
  7046. {
  7047. struct block_list *bl = map_id2bl(target_id);
  7048. if (bl == nullptr /*|| bl->type != BL_PC*/)
  7049. return 1;
  7050. if (sd->followtimer != INVALID_TIMER)
  7051. pc_stop_following(sd);
  7052. sd->followtarget = target_id;
  7053. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7054. return 0;
  7055. }
  7056. int32 pc_checkbaselevelup(map_session_data *sd) {
  7057. t_exp next = pc_nextbaseexp(sd);
  7058. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  7059. return 0;
  7060. uint32 base_level = sd->status.base_level;
  7061. do {
  7062. sd->status.base_exp -= next;
  7063. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7064. 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 )
  7065. sd->status.base_exp = next-1;
  7066. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  7067. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  7068. sd->status.base_level++;
  7069. if( pc_is_maxbaselv(sd) ){
  7070. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  7071. break;
  7072. }
  7073. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  7074. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  7075. status_calc_pet(sd->pd,SCO_NONE);
  7076. clif_updatestatus(*sd,SP_STATUSPOINT);
  7077. clif_updatestatus(*sd,SP_TRAITPOINT);
  7078. clif_updatestatus(*sd,SP_BASELEVEL);
  7079. clif_updatestatus(*sd,SP_BASEEXP);
  7080. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  7081. status_calc_pc(sd,SCO_FORCE);
  7082. status_percent_heal(&sd->bl,100,100);
  7083. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7084. for (const auto &status : status_db) {
  7085. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  7086. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, 120000); //All buffs with super novice flag for 2 minutes
  7087. }
  7088. if (sd->state.snovice_dead_flag)
  7089. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  7090. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  7091. for (const auto &status : status_db) {
  7092. if (status.second->flag[SCF_TAEKWONANGEL])
  7093. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  7094. }
  7095. }
  7096. clif_misceffect( sd->bl, NOTIFYEFFECT_BASE_LEVEL_UP );
  7097. npc_script_event( *sd, NPCE_BASELVUP );
  7098. if(sd->status.party_id)
  7099. party_send_levelup(sd);
  7100. pc_baselevelchanged(sd);
  7101. for (; base_level <= sd->status.base_level; base_level++) {
  7102. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  7103. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  7104. }
  7105. return 1;
  7106. }
  7107. void pc_baselevelchanged(map_session_data *sd) {
  7108. uint8 i;
  7109. for( i = 0; i < EQI_MAX; i++ ) {
  7110. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  7111. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (uint32)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  7112. pc_unequipitem(sd, sd->equip_index[i], 3);
  7113. }
  7114. }
  7115. pc_show_questinfo(sd);
  7116. }
  7117. int32 pc_checkjoblevelup(map_session_data *sd)
  7118. {
  7119. t_exp next = pc_nextjobexp(sd);
  7120. nullpo_ret(sd);
  7121. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  7122. return 0;
  7123. uint32 job_level = sd->status.job_level;
  7124. do {
  7125. sd->status.job_exp -= next;
  7126. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7127. 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 )
  7128. sd->status.job_exp = next-1;
  7129. sd->status.job_level ++;
  7130. sd->status.skill_point ++;
  7131. if( pc_is_maxjoblv(sd) ){
  7132. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  7133. break;
  7134. }
  7135. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  7136. clif_updatestatus(*sd,SP_JOBLEVEL);
  7137. clif_updatestatus(*sd,SP_JOBEXP);
  7138. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  7139. clif_updatestatus(*sd,SP_SKILLPOINT);
  7140. status_calc_pc(sd,SCO_FORCE);
  7141. clif_misceffect( sd->bl, NOTIFYEFFECT_JOB_LEVEL_UP );
  7142. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7143. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7144. npc_script_event( *sd, NPCE_JOBLVUP );
  7145. for (; job_level <= sd->status.job_level; job_level++)
  7146. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  7147. pc_show_questinfo(sd);
  7148. return 1;
  7149. }
  7150. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  7151. * @param sd Player
  7152. * @param base_exp Base EXP before peronal bonuses
  7153. * @param job_exp Job EXP before peronal bonuses
  7154. * @param src Block list that affecting the exp calculation
  7155. */
  7156. static void pc_calcexp(map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  7157. {
  7158. int32 bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  7159. if (src) {
  7160. status_data* status = status_get_status_data(*src);
  7161. if( sd->indexed_bonus.expaddrace[status->race] )
  7162. bonus += sd->indexed_bonus.expaddrace[status->race];
  7163. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  7164. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  7165. if( sd->indexed_bonus.expaddclass[status->class_] )
  7166. bonus += sd->indexed_bonus.expaddclass[status->class_];
  7167. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  7168. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  7169. if (battle_config.pk_mode &&
  7170. (int32)(status_get_lv(src) - sd->status.base_level) >= 20)
  7171. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  7172. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  7173. vip_bonus_base = battle_config.vip_base_exp_increase;
  7174. vip_bonus_job = battle_config.vip_job_exp_increase;
  7175. }
  7176. }
  7177. // Give EXPBOOST for quests even if src is nullptr.
  7178. if (sd->sc.getSCE(SC_EXPBOOST)) {
  7179. bonus += sd->sc.getSCE(SC_EXPBOOST)->val1;
  7180. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  7181. bonus += (sd->sc.getSCE(SC_EXPBOOST)->val1 / battle_config.vip_bm_increase);
  7182. }
  7183. if (sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND))
  7184. bonus += sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)->val1;
  7185. if (*base_exp) {
  7186. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  7187. *base_exp = cap_value(exp, 1, MAX_EXP);
  7188. }
  7189. // Give JEXPBOOST for quests even if src is nullptr.
  7190. if (sd->sc.getSCE(SC_JEXPBOOST))
  7191. bonus += sd->sc.getSCE(SC_JEXPBOOST)->val1;
  7192. if (sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)) // Increase Jexp as well
  7193. bonus += sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)->val1;
  7194. if (*job_exp) {
  7195. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  7196. *job_exp = cap_value(exp, 1, MAX_EXP);
  7197. }
  7198. return;
  7199. }
  7200. /**
  7201. * Show EXP gained by player in percentage by @showexp
  7202. * @param sd Player
  7203. * @param base_exp Base EXP gained/loss
  7204. * @param next_base_exp Base EXP needed for next base level
  7205. * @param job_exp Job EXP gained/loss
  7206. * @param next_job_exp Job EXP needed for next job level
  7207. * @param lost True:EXP penalty, lose EXP
  7208. **/
  7209. 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) {
  7210. char output[CHAT_SIZE_MAX];
  7211. nullpo_retv(sd);
  7212. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  7213. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  7214. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  7215. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  7216. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  7217. }
  7218. /**
  7219. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  7220. * @param sd Player
  7221. * @param src EXP source
  7222. * @param base_exp Base EXP gained
  7223. * @param base_exp Job EXP gained
  7224. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  7225. * @return
  7226. **/
  7227. void pc_gainexp(map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  7228. {
  7229. t_exp nextb = 0, nextj = 0;
  7230. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  7231. nullpo_retv(sd);
  7232. if(sd->bl.prev == nullptr || pc_isdead(sd))
  7233. return;
  7234. if (!(exp_flag&2)) {
  7235. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  7236. return; // no exp on pvp maps
  7237. if (sd->status.guild_id>0)
  7238. base_exp -= guild_payexp(sd,base_exp);
  7239. }
  7240. flag = ((base_exp) ? 1 : 0) |
  7241. ((job_exp) ? 2 : 0) |
  7242. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  7243. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  7244. if (!(exp_flag&2))
  7245. pc_calcexp(sd, &base_exp, &job_exp, src);
  7246. nextb = pc_nextbaseexp(sd);
  7247. nextj = pc_nextjobexp(sd);
  7248. if (flag&4){
  7249. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  7250. base_exp = 0;
  7251. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  7252. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  7253. }
  7254. if (flag&8){
  7255. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  7256. job_exp = 0;
  7257. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  7258. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  7259. }
  7260. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  7261. //Note that this value should never be greater than the original
  7262. //therefore no overflow checks are needed. [Skotlex]
  7263. if (nextb > 0) {
  7264. float nextbp = (float) base_exp / (float) nextb;
  7265. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  7266. base_exp = (uint32)(battle_config.max_exp_gain_rate/1000.*nextb);
  7267. }
  7268. if (nextj > 0) {
  7269. float nextjp = (float) job_exp / (float) nextj;
  7270. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  7271. job_exp = (uint32)(battle_config.max_exp_gain_rate/1000.*nextj);
  7272. }
  7273. }
  7274. // Give EXP for Base Level
  7275. if (base_exp) {
  7276. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  7277. if (!pc_checkbaselevelup(sd))
  7278. clif_updatestatus(*sd,SP_BASEEXP);
  7279. }
  7280. // Give EXP for Job Level
  7281. if (job_exp) {
  7282. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  7283. if (!pc_checkjoblevelup(sd))
  7284. clif_updatestatus(*sd,SP_JOBEXP);
  7285. }
  7286. if (flag&1)
  7287. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  7288. if (flag&2)
  7289. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  7290. if (sd->state.showexp && (base_exp || job_exp))
  7291. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  7292. }
  7293. /**
  7294. * Lost Base/Job EXP from a player
  7295. * @param sd Player
  7296. * @param base_exp Base EXP lost
  7297. * @param job_exp Job EXP lost
  7298. **/
  7299. void pc_lostexp(map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  7300. nullpo_retv(sd);
  7301. if (base_exp) {
  7302. base_exp = u64min(sd->status.base_exp, base_exp);
  7303. sd->status.base_exp -= base_exp;
  7304. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  7305. clif_updatestatus(*sd, SP_BASEEXP);
  7306. }
  7307. if (job_exp) {
  7308. job_exp = u64min(sd->status.job_exp, job_exp);
  7309. sd->status.job_exp -= job_exp;
  7310. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  7311. clif_updatestatus(*sd, SP_JOBEXP);
  7312. }
  7313. if (sd->state.showexp && (base_exp || job_exp))
  7314. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  7315. }
  7316. /**
  7317. * Returns max base level for this character's class.
  7318. * @param job_id: Player's class
  7319. * @return Max Base Level
  7320. */
  7321. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  7322. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7323. return job ? job->max_base_level : 0;
  7324. }
  7325. /**
  7326. * Returns max base level for this character.
  7327. * @param sd Player
  7328. * @return Max Base Level
  7329. **/
  7330. uint32 pc_maxbaselv(map_session_data *sd){
  7331. return job_db.get_maxBaseLv(sd->status.class_);
  7332. }
  7333. /**
  7334. * Returns max job level for this character's class.
  7335. * @param job_id: Player's class
  7336. * @return Max Job Level
  7337. */
  7338. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  7339. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7340. return job ? job->max_job_level : 0;
  7341. }
  7342. /**
  7343. * Returns max job level for this character.
  7344. * @param sd Player
  7345. * @return Max Job Level
  7346. **/
  7347. uint32 pc_maxjoblv(map_session_data *sd){
  7348. return job_db.get_maxJobLv(sd->status.class_);
  7349. }
  7350. /**
  7351. * Check if player is reached max base level
  7352. * @param sd
  7353. * @return True if reached max level
  7354. **/
  7355. bool pc_is_maxbaselv(map_session_data *sd) {
  7356. nullpo_retr(false, sd);
  7357. return (sd->status.base_level >= pc_maxbaselv(sd));
  7358. }
  7359. /**
  7360. * Check if player is reached max base level
  7361. * @param sd
  7362. * @return True if reached max level
  7363. **/
  7364. bool pc_is_maxjoblv(map_session_data *sd) {
  7365. nullpo_retr(false, sd);
  7366. return (sd->status.job_level >= pc_maxjoblv(sd));
  7367. }
  7368. /**
  7369. * Returns base experience for this character's class.
  7370. * @param job_id: Player's class
  7371. * @param level: Player's level
  7372. * @return Base EXP
  7373. */
  7374. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  7375. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7376. return job ? job->base_exp[level - 1] : 0;
  7377. }
  7378. /**
  7379. * Base exp needed for player to level up.
  7380. * @param sd
  7381. * @return Base EXP needed for next base level
  7382. **/
  7383. t_exp pc_nextbaseexp(map_session_data *sd){
  7384. nullpo_ret(sd);
  7385. if (sd->status.base_level == 0) // Is this something that possible?
  7386. return 0;
  7387. if (pc_is_maxbaselv(sd))
  7388. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  7389. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  7390. }
  7391. /**
  7392. * Returns job experience for this character's class.
  7393. * @param job_id: Player's class
  7394. * @param level: Player's level
  7395. * @return Job EXP
  7396. */
  7397. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  7398. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7399. return job ? job->job_exp[level - 1] : 0;
  7400. }
  7401. /**
  7402. * Job exp needed for player to level up.
  7403. * @param sd
  7404. * @return Job EXP needed for next job level
  7405. **/
  7406. t_exp pc_nextjobexp(map_session_data *sd){
  7407. nullpo_ret(sd);
  7408. if (sd->status.job_level == 0) // Is this something that possible?
  7409. return 0;
  7410. if (pc_is_maxjoblv(sd))
  7411. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  7412. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  7413. }
  7414. /**
  7415. * Returns max weight base for this character's class.
  7416. * @param job_id: Player's class
  7417. * @return Max weight base
  7418. */
  7419. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  7420. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7421. return job ? job->max_weight_base : 0;
  7422. }
  7423. /// Returns the value of the specified stat.
  7424. int32 pc_getstat(map_session_data *sd, int32 type)
  7425. {
  7426. nullpo_retr(-1, sd);
  7427. switch( type ) {
  7428. case SP_STR: return sd->status.str;
  7429. case SP_AGI: return sd->status.agi;
  7430. case SP_VIT: return sd->status.vit;
  7431. case SP_INT: return sd->status.int_;
  7432. case SP_DEX: return sd->status.dex;
  7433. case SP_LUK: return sd->status.luk;
  7434. case SP_POW: return sd->status.pow;
  7435. case SP_STA: return sd->status.sta;
  7436. case SP_WIS: return sd->status.wis;
  7437. case SP_SPL: return sd->status.spl;
  7438. case SP_CON: return sd->status.con;
  7439. case SP_CRT: return sd->status.crt;
  7440. default:
  7441. return -1;
  7442. }
  7443. }
  7444. /// Sets the specified stat to the specified value.
  7445. /// Returns the new value.
  7446. int32 pc_setstat(map_session_data* sd, int32 type, int32 val)
  7447. {
  7448. nullpo_retr(-1, sd);
  7449. switch( type ) {
  7450. case SP_STR: sd->status.str = val; break;
  7451. case SP_AGI: sd->status.agi = val; break;
  7452. case SP_VIT: sd->status.vit = val; break;
  7453. case SP_INT: sd->status.int_ = val; break;
  7454. case SP_DEX: sd->status.dex = val; break;
  7455. case SP_LUK: sd->status.luk = val; break;
  7456. case SP_POW: sd->status.pow = val; break;
  7457. case SP_STA: sd->status.sta = val; break;
  7458. case SP_WIS: sd->status.wis = val; break;
  7459. case SP_SPL: sd->status.spl = val; break;
  7460. case SP_CON: sd->status.con = val; break;
  7461. case SP_CRT: sd->status.crt = val; break;
  7462. default:
  7463. return -1;
  7464. }
  7465. return val;
  7466. }
  7467. /**
  7468. * Gets the total number of status points at the provided level.
  7469. * @param level: Player base level.
  7470. * @return Total number of status points at specific base level.
  7471. */
  7472. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7473. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7474. if( entry != nullptr ){
  7475. return entry->statpoints;
  7476. }else{
  7477. return 0;
  7478. }
  7479. }
  7480. /**
  7481. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7482. * @param level: Player base level.
  7483. * @param table: Use table value or formula.
  7484. * @return Status points at specific base level.
  7485. */
  7486. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7487. uint32 next_level = this->get_table_point( level + 1 );
  7488. uint32 current_level = this->get_table_point( level );
  7489. if( next_level > current_level ){
  7490. return next_level - current_level;
  7491. }else{
  7492. return 0;
  7493. }
  7494. }
  7495. /**
  7496. * Gets the total number of trait points at the provided level.
  7497. * @param level: Player base level.
  7498. * @return Total number of trait points at specific base level.
  7499. */
  7500. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7501. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7502. if( entry != nullptr ){
  7503. return entry->traitpoints;
  7504. }else{
  7505. return 0;
  7506. }
  7507. }
  7508. /**
  7509. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7510. * @param level: Player base level.
  7511. * @param table: Use table value or formula.
  7512. * @return Trait points at specific base level.
  7513. */
  7514. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7515. uint32 next_level = this->get_trait_table_point( level + 1 );
  7516. uint32 current_level = this->get_trait_table_point( level );
  7517. if( next_level > current_level ){
  7518. return next_level - current_level;
  7519. }else{
  7520. return 0;
  7521. }
  7522. }
  7523. #ifdef RENEWAL_STAT
  7524. /// Renewal status point cost formula
  7525. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7526. #else
  7527. /// Pre-Renewal status point cost formula
  7528. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7529. #endif
  7530. /// Returns the number of stat points needed to change the specified stat by val.
  7531. /// If val is negative, returns the number of stat points that would be needed to
  7532. /// raise the specified stat from (current value - val) to current value.
  7533. int32 pc_need_status_point(map_session_data* sd, int32 type, int32 val)
  7534. {
  7535. int32 low, high, sp = 0, max = 0;
  7536. if ( val == 0 )
  7537. return 0;
  7538. low = pc_getstat(sd,type);
  7539. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7540. if ( low >= max && val > 0 )
  7541. return 0; // Official servers show '0' when max is reached
  7542. high = low + val;
  7543. if ( val < 0 )
  7544. std::swap(low, high);
  7545. for ( ; low < high; low++ )
  7546. sp += PC_STATUS_POINT_COST(low);
  7547. return sp;
  7548. }
  7549. /**
  7550. * Returns the value the specified stat can be increased by with the current
  7551. * amount of available status points for the current character's class.
  7552. *
  7553. * @param sd The target character.
  7554. * @param type Stat to verify.
  7555. * @return Maximum value the stat could grow by.
  7556. */
  7557. int32 pc_maxparameterincrease(map_session_data* sd, int32 type)
  7558. {
  7559. int32 base, final_val, status_points, max_param;
  7560. nullpo_ret(sd);
  7561. base = final_val = pc_getstat(sd, type);
  7562. status_points = sd->status.status_point;
  7563. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7564. while (final_val <= max_param && status_points >= 0) {
  7565. status_points -= PC_STATUS_POINT_COST(final_val);
  7566. final_val++;
  7567. }
  7568. final_val--;
  7569. return (final_val > base ? final_val-base : 0);
  7570. }
  7571. /**
  7572. * Raises a stat by the specified amount.
  7573. *
  7574. * Obeys max_parameter limits.
  7575. * Subtracts status points according to the cost of the increased stat points.
  7576. *
  7577. * @param sd The target character.
  7578. * @param type The stat to change (see enum _sp)
  7579. * @param increase The stat increase (strictly positive) amount.
  7580. * @retval true if the stat was increased by any amount.
  7581. * @retval false if there were no changes.
  7582. */
  7583. bool pc_statusup(map_session_data* sd, int32 type, int32 increase)
  7584. {
  7585. int32 max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7586. nullpo_ret(sd);
  7587. // check conditions
  7588. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7589. clif_statusupack( *sd, type, false );
  7590. return false;
  7591. }
  7592. // check limits
  7593. current = pc_getstat(sd, type);
  7594. max_increase = pc_maxparameterincrease(sd, type);
  7595. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7596. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7597. clif_statusupack( *sd, type, false );
  7598. return false;
  7599. }
  7600. // check status points
  7601. needed_points = pc_need_status_point(sd, type, increase);
  7602. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7603. clif_statusupack( *sd, type, false );
  7604. return false;
  7605. }
  7606. // set new values
  7607. final_value = pc_setstat(sd, type, current + increase);
  7608. sd->status.status_point -= needed_points;
  7609. status_calc_pc(sd,SCO_NONE);
  7610. // update increase cost indicator
  7611. clif_updatestatus(*sd, static_cast<_sp>( SP_USTR + type-SP_STR ) );
  7612. // update statpoint count
  7613. clif_updatestatus(*sd, SP_STATUSPOINT);
  7614. // update stat value
  7615. clif_statusupack( *sd, type, true, final_value );
  7616. if( final_value > 255 )
  7617. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7618. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7619. return true;
  7620. }
  7621. /**
  7622. * Raises a stat by the specified amount.
  7623. *
  7624. * Obeys max_parameter limits.
  7625. * Does not subtract status points for the cost of the modified stat points.
  7626. *
  7627. * @param sd The target character.
  7628. * @param type The stat to change (see enum _sp)
  7629. * @param val The stat increase (or decrease) amount.
  7630. * @return the stat increase amount.
  7631. * @retval 0 if no changes were made.
  7632. */
  7633. int32 pc_statusup2(map_session_data* sd, int32 type, int32 val)
  7634. {
  7635. int32 max, need;
  7636. nullpo_ret(sd);
  7637. if( type < SP_STR || type > SP_LUK )
  7638. {
  7639. clif_statusupack( *sd, type, false );
  7640. return 0;
  7641. }
  7642. need = pc_need_status_point(sd,type,1);
  7643. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7644. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7645. status_calc_pc(sd,SCO_NONE);
  7646. // update increase cost indicator
  7647. if( need != pc_need_status_point(sd,type,1) )
  7648. clif_updatestatus(*sd, static_cast<_sp>( SP_USTR + type-SP_STR ) );
  7649. // update stat value
  7650. clif_statusupack( *sd, type, true, val );
  7651. if( val > 255 )
  7652. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7653. return val;
  7654. }
  7655. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7656. /// If val is negative, returns the number of trait stat points that would be needed to
  7657. /// raise the specified trait stat from (current value - val) to current value.
  7658. int32 pc_need_trait_point(map_session_data* sd, int32 type, int32 val)
  7659. {
  7660. nullpo_retr(0, sd);
  7661. if (val == 0 || type < SP_POW || type > SP_CRT)
  7662. return 0;
  7663. int32 low = pc_getstat(sd, type);
  7664. int32 max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7665. if (low >= max && val > 0)
  7666. return 0; // Official servers show '0' when max is reached
  7667. int32 high = low + val, sp = 0;
  7668. if (val < 0)
  7669. std::swap(low, high);
  7670. for (; low < high; low++)
  7671. sp += 1;
  7672. return sp;
  7673. }
  7674. /**
  7675. * Returns the value the specified trait stat can be increased by with the current
  7676. * amount of available trait status points for the current character's class.
  7677. *
  7678. * @param sd The target character.
  7679. * @param type Trait stat to verify.
  7680. * @return Maximum value the stat could grow by.
  7681. */
  7682. int32 pc_maxtraitparameterincrease(map_session_data* sd, int32 type)
  7683. {
  7684. nullpo_ret(sd);
  7685. if( type < SP_POW || type > SP_CRT ){
  7686. return 0;
  7687. }
  7688. int32 base, final_val = pc_getstat(sd, type);
  7689. int32 trait_points = sd->status.trait_point;
  7690. int32 max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7691. base = final_val;
  7692. while (final_val <= max_param && trait_points >= 0) {
  7693. trait_points -= 1;
  7694. final_val++;
  7695. }
  7696. final_val--;
  7697. return (final_val > base ? final_val - base : 0);
  7698. }
  7699. /**
  7700. * Raises a trait stat by the specified amount.
  7701. *
  7702. * Obeys max_traitparameter limits.
  7703. * Subtracts trait status points according to the cost of the increased trait stat points.
  7704. *
  7705. * @param sd The target character.
  7706. * @param type The stat to change (see enum _sp)
  7707. * @param increase The stat increase (strictly positive) amount.
  7708. * @retval true if the trait stat was increased by any amount.
  7709. * @retval false if there were no changes.
  7710. */
  7711. bool pc_traitstatusup(map_session_data* sd, int32 type, int32 increase)
  7712. {
  7713. nullpo_ret(sd);
  7714. // check conditions
  7715. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7716. clif_statusupack( *sd, type, false );
  7717. return false;
  7718. }
  7719. // check limits
  7720. int32 current = pc_getstat(sd, type);
  7721. int32 max_increase = pc_maxtraitparameterincrease(sd, type);
  7722. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7723. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7724. clif_statusupack( *sd, type, false );
  7725. return false;
  7726. }
  7727. // check status points
  7728. int32 needed_points = pc_need_trait_point(sd, type, increase);
  7729. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7730. clif_statusupack( *sd, type, false );
  7731. return false;
  7732. }
  7733. // set new values
  7734. int32 final_value = pc_setstat(sd, type, current + increase);
  7735. sd->status.trait_point -= needed_points;
  7736. status_calc_pc(sd, SCO_NONE);
  7737. // update increase cost indicator
  7738. clif_updatestatus(*sd, static_cast<_sp>( SP_UPOW + type - SP_POW ) );
  7739. // update statpoint count
  7740. clif_updatestatus(*sd, SP_TRAITPOINT);
  7741. // update stat value
  7742. clif_statusupack( *sd, type, true, final_value );
  7743. if (final_value > 255)
  7744. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7745. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7746. return true;
  7747. }
  7748. /**
  7749. * Raises a trait stat by the specified amount.
  7750. *
  7751. * Obeys max_trait_parameter limits.
  7752. * Does not subtract status points for the cost of the modified stat points.
  7753. *
  7754. * @param sd The target character.
  7755. * @param type The stat to change (see enum _sp)
  7756. * @param val The stat increase (or decrease) amount.
  7757. * @return the stat increase amount.
  7758. * @retval 0 if no changes were made.
  7759. */
  7760. int32 pc_traitstatusup2(map_session_data* sd, int32 type, int32 val)
  7761. {
  7762. nullpo_ret(sd);
  7763. if (type < SP_POW || type > SP_CRT) {
  7764. clif_statusupack( *sd, type, false );
  7765. return 0;
  7766. }
  7767. int32 need = pc_need_trait_point(sd, type, 1);
  7768. int32 max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7769. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7770. status_calc_pc(sd, SCO_NONE);
  7771. // update increase cost indicator
  7772. if (need != pc_need_trait_point(sd, type, 1))
  7773. clif_updatestatus(*sd, static_cast<_sp>( SP_UPOW + type - SP_POW ) );
  7774. // update stat value
  7775. clif_statusupack( *sd, type, true, val );
  7776. if (val > 255)
  7777. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7778. return val;
  7779. }
  7780. /*==========================================
  7781. * Update skill_lv for player sd
  7782. * Skill point allocation
  7783. *------------------------------------------*/
  7784. void pc_skillup(map_session_data *sd,uint16 skill_id)
  7785. {
  7786. uint16 idx = skill_get_index(skill_id);
  7787. nullpo_retv(sd);
  7788. if (!idx) {
  7789. if (skill_id)
  7790. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7791. return;
  7792. }
  7793. // Level up guild skill
  7794. if (SKILL_CHK_GUILD(skill_id)) {
  7795. guild_skillup(sd, skill_id);
  7796. return;
  7797. }
  7798. // Level up homunculus skill
  7799. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7800. hom_skillup(sd->hd, skill_id);
  7801. return;
  7802. }
  7803. else {
  7804. if( sd->status.skill_point > 0 &&
  7805. sd->status.skill[idx].id &&
  7806. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7807. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7808. {
  7809. sd->status.skill[idx].lv++;
  7810. sd->status.skill_point--;
  7811. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7812. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7813. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7814. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7815. else
  7816. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7817. uint16 lv = sd->status.skill[idx].lv;
  7818. int32 range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7819. bool upgradable = ( lv < skill_tree_get_max( sd->status.skill[idx].id, sd->status.class_ ) );
  7820. clif_skillup( *sd, skill_id, lv, range, upgradable );
  7821. clif_updatestatus(*sd,SP_SKILLPOINT);
  7822. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7823. clif_updatestatus(*sd,SP_CARTINFO);
  7824. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7825. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7826. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7827. clif_skillinfoblock(sd);
  7828. }
  7829. //else
  7830. // 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);
  7831. }
  7832. }
  7833. /*==========================================
  7834. * /allskill
  7835. *------------------------------------------*/
  7836. int32 pc_allskillup(map_session_data *sd)
  7837. {
  7838. int32 i;
  7839. nullpo_ret(sd);
  7840. for (i = 0; i < MAX_SKILL; i++) {
  7841. 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) {
  7842. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7843. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7844. if (sd->status.skill[i].lv == 0)
  7845. sd->status.skill[i].id = 0;
  7846. }
  7847. }
  7848. if (!pc_grant_allskills(sd, true)) {
  7849. uint16 sk_id;
  7850. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7851. if (tree != nullptr && !tree->skills.empty()) {
  7852. for (const auto &skillsit : tree->skills) {
  7853. sk_id = skillsit.first;
  7854. uint16 sk_idx = skill_get_index(sk_id);
  7855. if (sk_idx == 0)
  7856. continue;
  7857. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7858. if (
  7859. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7860. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7861. sk_id == SG_DEVIL
  7862. )
  7863. continue; //Cannot be learned normally.
  7864. sd->status.skill[sk_idx].id = sk_id;
  7865. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7866. }
  7867. }
  7868. }
  7869. status_calc_pc(sd,SCO_NONE);
  7870. //Required because if you could level up all skills previously,
  7871. //the update will not be sent as only the lv variable changes.
  7872. clif_skillinfoblock(sd);
  7873. return 0;
  7874. }
  7875. /*==========================================
  7876. * /resetlvl
  7877. *------------------------------------------*/
  7878. int32 pc_resetlvl(map_session_data* sd,int32 type)
  7879. {
  7880. int32 i;
  7881. nullpo_ret(sd);
  7882. if (type != 3) //Also reset skills
  7883. pc_resetskill(sd, 0);
  7884. if(type == 1){
  7885. sd->status.skill_point=0;
  7886. sd->status.trait_point = 0;
  7887. sd->status.base_level=1;
  7888. sd->status.job_level=1;
  7889. sd->status.base_exp=0;
  7890. sd->status.job_exp=0;
  7891. if(sd->sc.option !=0)
  7892. sd->sc.option = 0;
  7893. sd->status.str=1;
  7894. sd->status.agi=1;
  7895. sd->status.vit=1;
  7896. sd->status.int_=1;
  7897. sd->status.dex=1;
  7898. sd->status.luk=1;
  7899. sd->status.pow=0;
  7900. sd->status.sta=0;
  7901. sd->status.wis=0;
  7902. sd->status.spl=0;
  7903. sd->status.con=0;
  7904. sd->status.crt=0;
  7905. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7906. sd->status.status_point=100; // not 88 [celest]
  7907. // give platinum skills upon changing
  7908. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7909. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7910. }
  7911. }
  7912. if(type == 2){
  7913. sd->status.skill_point=0;
  7914. sd->status.base_level=1;
  7915. sd->status.job_level=1;
  7916. sd->status.base_exp=0;
  7917. sd->status.job_exp=0;
  7918. }
  7919. if(type == 3){
  7920. sd->status.base_level=1;
  7921. sd->status.base_exp=0;
  7922. }
  7923. if(type == 4){
  7924. sd->status.job_level=1;
  7925. sd->status.job_exp=0;
  7926. }
  7927. clif_updatestatus(*sd,SP_STATUSPOINT);
  7928. clif_updatestatus(*sd,SP_TRAITPOINT);
  7929. clif_updatestatus(*sd,SP_STR);
  7930. clif_updatestatus(*sd,SP_AGI);
  7931. clif_updatestatus(*sd,SP_VIT);
  7932. clif_updatestatus(*sd,SP_INT);
  7933. clif_updatestatus(*sd,SP_DEX);
  7934. clif_updatestatus(*sd,SP_LUK);
  7935. clif_updatestatus(*sd,SP_POW);
  7936. clif_updatestatus(*sd,SP_STA);
  7937. clif_updatestatus(*sd,SP_WIS);
  7938. clif_updatestatus(*sd,SP_SPL);
  7939. clif_updatestatus(*sd,SP_CON);
  7940. clif_updatestatus(*sd,SP_CRT);
  7941. clif_updatestatus(*sd,SP_BASELEVEL);
  7942. clif_updatestatus(*sd,SP_JOBLEVEL);
  7943. clif_updatestatus(*sd,SP_STATUSPOINT);
  7944. clif_updatestatus(*sd,SP_BASEEXP);
  7945. clif_updatestatus(*sd,SP_JOBEXP);
  7946. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  7947. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  7948. clif_updatestatus(*sd,SP_SKILLPOINT);
  7949. clif_updatestatus(*sd,SP_USTR); // Updates needed stat points - Valaris
  7950. clif_updatestatus(*sd,SP_UAGI);
  7951. clif_updatestatus(*sd,SP_UVIT);
  7952. clif_updatestatus(*sd,SP_UINT);
  7953. clif_updatestatus(*sd,SP_UDEX);
  7954. clif_updatestatus(*sd,SP_ULUK); // End Addition
  7955. clif_updatestatus(*sd,SP_UPOW);
  7956. clif_updatestatus(*sd,SP_USTA);
  7957. clif_updatestatus(*sd,SP_UWIS);
  7958. clif_updatestatus(*sd,SP_USPL);
  7959. clif_updatestatus(*sd,SP_UCON);
  7960. clif_updatestatus(*sd,SP_UCRT);
  7961. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7962. if(sd->equip_index[i] >= 0)
  7963. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  7964. pc_unequipitem(sd,sd->equip_index[i],2);
  7965. }
  7966. }
  7967. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7968. party_send_levelup(sd);
  7969. status_calc_pc(sd, SCO_FORCE);
  7970. clif_skillinfoblock(sd);
  7971. return 0;
  7972. }
  7973. /*==========================================
  7974. * /resetstate
  7975. *------------------------------------------*/
  7976. int32 pc_resetstate(map_session_data* sd)
  7977. {
  7978. nullpo_ret(sd);
  7979. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7980. 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",
  7981. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7982. sd->status.base_level = pc_maxbaselv( sd );
  7983. clif_updatestatus( *sd, SP_BASELEVEL );
  7984. }
  7985. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7986. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7987. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7988. sd->status.status_point += battle_config.transcendent_status_points;
  7989. }
  7990. if ((sd->class_&JOBL_FOURTH) != 0) {
  7991. sd->status.trait_point += battle_config.trait_points_job_change;
  7992. }
  7993. pc_setstat(sd, SP_STR, 1);
  7994. pc_setstat(sd, SP_AGI, 1);
  7995. pc_setstat(sd, SP_VIT, 1);
  7996. pc_setstat(sd, SP_INT, 1);
  7997. pc_setstat(sd, SP_DEX, 1);
  7998. pc_setstat(sd, SP_LUK, 1);
  7999. pc_setstat(sd, SP_POW, 0);
  8000. pc_setstat(sd, SP_STA, 0);
  8001. pc_setstat(sd, SP_WIS, 0);
  8002. pc_setstat(sd, SP_SPL, 0);
  8003. pc_setstat(sd, SP_CON, 0);
  8004. pc_setstat(sd, SP_CRT, 0);
  8005. clif_updatestatus(*sd,SP_STR);
  8006. clif_updatestatus(*sd,SP_AGI);
  8007. clif_updatestatus(*sd,SP_VIT);
  8008. clif_updatestatus(*sd,SP_INT);
  8009. clif_updatestatus(*sd,SP_DEX);
  8010. clif_updatestatus(*sd,SP_LUK);
  8011. clif_updatestatus(*sd,SP_POW);
  8012. clif_updatestatus(*sd,SP_STA);
  8013. clif_updatestatus(*sd,SP_WIS);
  8014. clif_updatestatus(*sd,SP_SPL);
  8015. clif_updatestatus(*sd,SP_CON);
  8016. clif_updatestatus(*sd,SP_CRT);
  8017. clif_updatestatus(*sd,SP_USTR); // Updates needed stat points - Valaris
  8018. clif_updatestatus(*sd,SP_UAGI);
  8019. clif_updatestatus(*sd,SP_UVIT);
  8020. clif_updatestatus(*sd,SP_UINT);
  8021. clif_updatestatus(*sd,SP_UDEX);
  8022. clif_updatestatus(*sd,SP_ULUK); // End Addition
  8023. clif_updatestatus(*sd,SP_UPOW);
  8024. clif_updatestatus(*sd,SP_USTA);
  8025. clif_updatestatus(*sd,SP_UWIS);
  8026. clif_updatestatus(*sd,SP_USPL);
  8027. clif_updatestatus(*sd,SP_UCON);
  8028. clif_updatestatus(*sd,SP_UCRT);
  8029. clif_updatestatus(*sd,SP_STATUSPOINT);
  8030. clif_updatestatus(*sd,SP_TRAITPOINT);
  8031. if( sd->mission_mobid ) { //bugreport:2200
  8032. sd->mission_mobid = 0;
  8033. sd->mission_count = 0;
  8034. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  8035. }
  8036. status_calc_pc(sd, SCO_NONE);
  8037. return 1;
  8038. }
  8039. /*==========================================
  8040. * /resetskill
  8041. * if flag&1, perform block resync and status_calc call.
  8042. * if flag&2, just count total amount of skill points used by player, do not really reset.
  8043. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  8044. *------------------------------------------*/
  8045. int32 pc_resetskill(map_session_data* sd, int32 flag)
  8046. {
  8047. int32 i, skill_point=0;
  8048. nullpo_ret(sd);
  8049. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  8050. return 0;
  8051. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  8052. /**
  8053. * 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)
  8054. **/
  8055. if( pc_is_taekwon_ranker(sd) )
  8056. return 0;
  8057. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  8058. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  8059. i = sd->sc.option;
  8060. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  8061. i &= ~OPTION_RIDING;
  8062. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  8063. i &= ~OPTION_FALCON;
  8064. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  8065. i &= ~OPTION_DRAGON;
  8066. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  8067. i &= ~OPTION_WUG;
  8068. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  8069. i &= ~OPTION_WUGRIDER;
  8070. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  8071. i &= ~OPTION_MADOGEAR;
  8072. #ifndef NEW_CARTS
  8073. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  8074. i &= ~OPTION_CART;
  8075. #else
  8076. if( sd->sc.getSCE(SC_PUSH_CART) )
  8077. pc_setcart(sd, 0);
  8078. #endif
  8079. if( i != sd->sc.option )
  8080. pc_setoption(sd, i);
  8081. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  8082. hom_vaporize(sd, HOM_ST_ACTIVE);
  8083. if (sd->sc.getSCE(SC_SPRITEMABLE) && pc_checkskill(sd, SU_SPRITEMABLE))
  8084. status_change_end(&sd->bl, SC_SPRITEMABLE);
  8085. if (sd->sc.getSCE(SC_SOULATTACK) && pc_checkskill(sd, SU_SOULATTACK))
  8086. status_change_end(&sd->bl, SC_SOULATTACK);
  8087. }
  8088. for (const auto &skill : skill_db) {
  8089. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  8090. uint8 lv = sd->status.skill[idx].lv;
  8091. if (lv == 0 || skill_id == 0)
  8092. continue;
  8093. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  8094. continue;
  8095. // Don't reset trick dead if not a novice/baby
  8096. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8097. {
  8098. sd->status.skill[idx].lv = 0;
  8099. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8100. continue;
  8101. }
  8102. // do not reset basic skill
  8103. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8104. continue;
  8105. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  8106. continue;
  8107. if( flag&4 && !skill_ischangesex(skill_id) )
  8108. continue;
  8109. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  8110. { //Only handle quest skills in a special way when you can't learn them manually
  8111. if( battle_config.quest_skill_reset && !(flag&2) )
  8112. { //Wipe them
  8113. sd->status.skill[idx].lv = 0;
  8114. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8115. }
  8116. continue;
  8117. }
  8118. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  8119. skill_point += lv;
  8120. else
  8121. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  8122. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  8123. if( !(flag&2) )
  8124. {// reset
  8125. sd->status.skill[idx].lv = 0;
  8126. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8127. }
  8128. }
  8129. if( flag&2 || !skill_point ) return skill_point;
  8130. sd->status.skill_point += skill_point;
  8131. if (flag&1) {
  8132. clif_updatestatus(*sd,SP_SKILLPOINT);
  8133. clif_skillinfoblock(sd);
  8134. status_calc_pc(sd, SCO_FORCE);
  8135. }
  8136. return skill_point;
  8137. }
  8138. /*==========================================
  8139. * /resetfeel [Komurka]
  8140. *------------------------------------------*/
  8141. int32 pc_resetfeel(map_session_data* sd)
  8142. {
  8143. int32 i;
  8144. nullpo_ret(sd);
  8145. for (i=0; i<MAX_PC_FEELHATE; i++)
  8146. {
  8147. sd->feel_map[i].m = -1;
  8148. sd->feel_map[i].index = 0;
  8149. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  8150. }
  8151. return 0;
  8152. }
  8153. int32 pc_resethate(map_session_data* sd)
  8154. {
  8155. int32 i;
  8156. nullpo_ret(sd);
  8157. for (i=0; i<MAX_PC_FEELHATE; i++)
  8158. {
  8159. sd->hate_mob[i] = -1;
  8160. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  8161. }
  8162. return 0;
  8163. }
  8164. int32 pc_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8165. {
  8166. int32 bonus = 0;
  8167. nullpo_ret(sd);
  8168. skill_id = skill_dummy2skill_id(skill_id);
  8169. for (auto &it : sd->skillatk) {
  8170. if (it.id == skill_id) {
  8171. bonus += it.val;
  8172. break;
  8173. }
  8174. }
  8175. return bonus;
  8176. }
  8177. int32 pc_sub_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8178. {
  8179. int32 bonus = 0;
  8180. nullpo_ret(sd);
  8181. skill_id = skill_dummy2skill_id(skill_id);
  8182. for (auto &it : sd->subskill) {
  8183. if (it.id == skill_id) {
  8184. bonus += it.val;
  8185. break;
  8186. }
  8187. }
  8188. return bonus;
  8189. }
  8190. int32 pc_skillheal_bonus(map_session_data *sd, uint16 skill_id) {
  8191. int32 bonus = sd->bonus.add_heal_rate;
  8192. nullpo_ret(sd);
  8193. skill_id = skill_dummy2skill_id(skill_id);
  8194. if( bonus ) {
  8195. switch( skill_id ) {
  8196. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  8197. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  8198. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  8199. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  8200. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  8201. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  8202. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  8203. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  8204. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  8205. }
  8206. }
  8207. for (auto &it : sd->skillheal) {
  8208. if (it.id == skill_id) {
  8209. bonus += it.val;
  8210. break;
  8211. }
  8212. }
  8213. return bonus;
  8214. }
  8215. int32 pc_skillheal2_bonus(map_session_data *sd, uint16 skill_id) {
  8216. int32 bonus = sd->bonus.add_heal2_rate;
  8217. skill_id = skill_dummy2skill_id(skill_id);
  8218. for (auto &it : sd->skillheal2) {
  8219. if (it.id == skill_id) {
  8220. bonus += it.val;
  8221. break;
  8222. }
  8223. }
  8224. return bonus;
  8225. }
  8226. void pc_respawn(map_session_data* sd, clr_type clrtype)
  8227. {
  8228. if( !pc_isdead(sd) )
  8229. return; // not applicable
  8230. if( sd->bg_id && bg_member_respawn(sd) )
  8231. return; // member revived by battleground
  8232. pc_setstand(sd, true);
  8233. pc_setrestartvalue(sd,3);
  8234. if( pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, clrtype ) != SETPOS_OK ){
  8235. clif_resurrection( sd->bl ); //If warping fails, send a normal stand up packet.
  8236. }
  8237. }
  8238. static TIMER_FUNC(pc_respawn_timer){
  8239. map_session_data *sd = map_id2sd(id);
  8240. if( sd != nullptr )
  8241. {
  8242. sd->pvp_point=0;
  8243. sd->respawn_tid = INVALID_TIMER;
  8244. pc_respawn(sd,CLR_OUTSIGHT);
  8245. }
  8246. return 0;
  8247. }
  8248. /*==========================================
  8249. * Invoked when a player has received damage
  8250. *------------------------------------------*/
  8251. void pc_damage(map_session_data *sd,struct block_list *src,uint32 hp, uint32 sp, uint32 ap)
  8252. {
  8253. if (ap) clif_updatestatus(*sd,SP_AP);
  8254. if (sp) clif_updatestatus(*sd,SP_SP);
  8255. if (hp) clif_updatestatus(*sd,SP_HP);
  8256. else return;
  8257. if (!src)
  8258. return;
  8259. if( pc_issit(sd) ) {
  8260. pc_setstand(sd, true);
  8261. skill_sit(sd,0);
  8262. }
  8263. if (sd->progressbar.npc_id)
  8264. clif_progressbar_abort(sd);
  8265. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  8266. pet_target_check(sd->pd,src,1);
  8267. if( sd->status.ele_id > 0 )
  8268. elemental_set_target(sd,src);
  8269. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  8270. sd->canlog_tick = gettick();
  8271. }
  8272. TIMER_FUNC(pc_close_npc_timer){
  8273. map_session_data* sd = map_id2sd( id );
  8274. if( sd != nullptr ){
  8275. pc_close_npc( sd, static_cast<int32>( data ) );
  8276. }
  8277. return 0;
  8278. }
  8279. /**
  8280. * Method to properly close a NPC for player and clear anything related.
  8281. * @param sd: Player attached
  8282. * @param flag: Method of closure
  8283. * 1: Produce a close button and end the NPC
  8284. * 2: End the NPC (best for no dialog windows)
  8285. */
  8286. void pc_close_npc(map_session_data *sd,int32 flag)
  8287. {
  8288. nullpo_retv(sd);
  8289. if (sd->npc_id || sd->npc_shopid) {
  8290. if (sd->state.using_fake_npc) {
  8291. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, *sd );
  8292. sd->state.using_fake_npc = 0;
  8293. }
  8294. if (sd->st) {
  8295. if(sd->st->state == RUN){ //wait ending code execution
  8296. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  8297. return;
  8298. }
  8299. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  8300. sd->st->mes_active = 0;
  8301. }
  8302. sd->state.menu_or_input = 0;
  8303. sd->npc_menu = 0;
  8304. sd->npc_shopid = 0;
  8305. #ifdef SECURE_NPCTIMEOUT
  8306. if( sd->npc_idle_timer != INVALID_TIMER ){
  8307. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  8308. sd->npc_idle_timer = INVALID_TIMER;
  8309. }
  8310. #endif
  8311. if (sd->st) {
  8312. if (sd->st->state == CLOSE) {
  8313. clif_scriptclose( *sd, sd->npc_id );
  8314. clif_cutin( *sd, "", 255); // Force to end cutin [Haydrich]
  8315. sd->st->state = END; // Force to end now
  8316. }
  8317. if (sd->st->state == END) { // free attached scripts that are waiting
  8318. script_free_state(sd->st);
  8319. sd->st = nullptr;
  8320. sd->npc_id = 0;
  8321. }
  8322. }
  8323. }
  8324. }
  8325. /*==========================================
  8326. * Invoked when a player has negative current hp
  8327. *------------------------------------------*/
  8328. int32 pc_dead(map_session_data *sd,struct block_list *src)
  8329. {
  8330. int32 i=0,k=0;
  8331. t_tick tick = gettick();
  8332. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8333. // Activate Steel body if a super novice dies at 99+% exp [celest]
  8334. // Super Novices have no kill or die functions attached when saved by their angel
  8335. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  8336. t_exp exp = pc_nextbaseexp(sd);
  8337. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  8338. sd->state.snovice_dead_flag = 1;
  8339. pc_setrestartvalue(sd,1);
  8340. status_percent_heal(&sd->bl, 100, 100);
  8341. clif_resurrection( sd->bl );
  8342. if(battle_config.pc_invincible_time)
  8343. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8344. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  8345. if(mapdata_flag_gvg2(mapdata))
  8346. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  8347. return 0;
  8348. }
  8349. }
  8350. for(k = 0; k < MAX_DEVOTION; k++) {
  8351. if (sd->devotion[k]){
  8352. map_session_data *devsd = map_id2sd(sd->devotion[k]);
  8353. if (devsd)
  8354. status_change_end(&devsd->bl, SC_DEVOTION);
  8355. sd->devotion[k] = 0;
  8356. }
  8357. }
  8358. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  8359. if (sd->stellar_mark[k]) {
  8360. map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  8361. if (smarksd)
  8362. status_change_end(&smarksd->bl, SC_FLASHKICK);
  8363. sd->stellar_mark[k] = 0;
  8364. }
  8365. }
  8366. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  8367. if (sd->united_soul[k]) {
  8368. map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  8369. if (usoulsd)
  8370. status_change_end(&usoulsd->bl, SC_SOULUNITY);
  8371. sd->united_soul[k] = 0;
  8372. }
  8373. }
  8374. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  8375. if (sd->servant_sign[k]) {
  8376. map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  8377. if (ssignsd)
  8378. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN);
  8379. sd->servant_sign[k] = 0;
  8380. }
  8381. }
  8382. if(sd->shadowform_id) { //if we were target of shadowform
  8383. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM);
  8384. sd->shadowform_id = 0; //should be remove on status end anyway
  8385. }
  8386. if(sd->status.pet_id > 0 && sd->pd) {
  8387. struct pet_data *pd = sd->pd;
  8388. if( !mapdata->getMapFlag(MF_NOEXPPENALTY) ) {
  8389. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  8390. clif_send_petdata( sd, *sd->pd, CHANGESTATEPET_INTIMACY );
  8391. }
  8392. if( sd->pd->target_id ) // Unlock all targets...
  8393. pet_unlocktarget(sd->pd);
  8394. }
  8395. 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)
  8396. hom_vaporize(sd, HOM_ST_ACTIVE);
  8397. if( sd->md )
  8398. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  8399. if( sd->ed )
  8400. elemental_delete(sd->ed);
  8401. // Leave duel if you die [LuzZza]
  8402. if(battle_config.duel_autoleave_when_die) {
  8403. if(sd->duel_group > 0)
  8404. duel_leave(sd->duel_group, sd);
  8405. if(sd->duel_invite > 0)
  8406. duel_reject(sd->duel_invite, sd);
  8407. }
  8408. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  8409. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  8410. sd->skill_keep_using.tid = INVALID_TIMER;
  8411. }
  8412. pc_close_npc(sd,2); //close npc if we were using one
  8413. /* e.g. not killed thru pc_damage */
  8414. if( pc_issit(sd) ) {
  8415. clif_status_load(&sd->bl,EFST_SIT,0);
  8416. }
  8417. pc_setdead(sd);
  8418. clif_party_dead( *sd );
  8419. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  8420. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  8421. if (battle_config.loose_ap_on_death == 1)
  8422. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  8423. //Reset menu skills/item skills
  8424. if ((sd->skillitem) != 0)
  8425. sd->skillitem = sd->skillitemlv = 0;
  8426. if ((sd->menuskill_id) != 0)
  8427. sd->menuskill_id = sd->menuskill_val = 0;
  8428. //Reset ticks.
  8429. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  8430. if ( sd->spiritball !=0 )
  8431. pc_delspiritball(sd,sd->spiritball,0);
  8432. if (sd->soulball != 0)
  8433. pc_delsoulball( *sd, sd->soulball );
  8434. if (sd->servantball != 0)
  8435. pc_delservantball( *sd, sd->servantball );
  8436. if (sd->abyssball != 0)
  8437. pc_delabyssball( *sd, sd->abyssball );
  8438. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  8439. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  8440. if (src)
  8441. switch (src->type) {
  8442. case BL_MOB:
  8443. {
  8444. struct mob_data *md=(struct mob_data *)src;
  8445. if(md->target_id==sd->bl.id)
  8446. mob_unlocktarget(md,tick);
  8447. if(battle_config.mobs_level_up && md->status.hp &&
  8448. (uint32)md->level < pc_maxbaselv(sd) &&
  8449. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  8450. ) { // monster level up [Valaris]
  8451. clif_misceffect( md->bl, NOTIFYEFFECT_BASE_LEVEL_UP );
  8452. md->level++;
  8453. status_calc_mob(md, SCO_NONE);
  8454. status_percent_heal(src,10,0);
  8455. if( battle_config.show_mob_info&4 )
  8456. {// update name with new level
  8457. clif_name_area(&md->bl);
  8458. }
  8459. }
  8460. src = battle_get_master(src); // Maybe Player Summon
  8461. }
  8462. break;
  8463. case BL_PET: //Pass on to master...
  8464. case BL_HOM:
  8465. case BL_MER:
  8466. src = battle_get_master(src);
  8467. break;
  8468. }
  8469. if (src && src->type == BL_PC) {
  8470. map_session_data *ssd = (map_session_data *)src;
  8471. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8472. npc_script_event( *ssd, NPCE_KILLPC );
  8473. if (battle_config.pk_mode&2) {
  8474. ssd->status.manner -= 5;
  8475. if(ssd->status.manner < 0)
  8476. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8477. #if 0
  8478. // PK/Karma system code (not enabled yet) [celest]
  8479. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8480. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8481. // karma going down = more 'good' / more honourable.
  8482. // The Karma System way...
  8483. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8484. sd->status.karma--;
  8485. ssd->status.karma--;
  8486. }
  8487. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8488. ssd->status.karma++;
  8489. // or the PK System way...
  8490. if (sd->status.karma > 0) // player killed is dishonourable?
  8491. ssd->status.karma--; // honour points earned
  8492. sd->status.karma++; // honour points lost
  8493. // To-do: Receive exp on certain occasions
  8494. #endif
  8495. }
  8496. }
  8497. if(battle_config.bone_drop==2
  8498. || (battle_config.bone_drop==1 && mapdata->getMapFlag(MF_PVP)))
  8499. {
  8500. struct item item_tmp;
  8501. memset(&item_tmp,0,sizeof(item_tmp));
  8502. item_tmp.nameid=ITEMID_SKULL_;
  8503. item_tmp.identify=1;
  8504. item_tmp.card[0]=CARD0_CREATE;
  8505. item_tmp.card[1]=0;
  8506. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8507. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8508. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,4,0);
  8509. }
  8510. //Remove bonus_script when dead
  8511. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8512. // changed penalty options, added death by player if pk_mode [Valaris]
  8513. if(battle_config.death_penalty_type
  8514. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8515. && !sd->sc.cant.deathpenalty
  8516. && !mapdata->getMapFlag(MF_NOEXPPENALTY) && !mapdata_flag_gvg2(mapdata))
  8517. {
  8518. t_exp base_penalty = 0;
  8519. t_exp job_penalty = 0;
  8520. uint32 zeny_penalty = 0;
  8521. if (pc_isvip(sd)) { // EXP penalty for VIP
  8522. base_penalty = battle_config.vip_exp_penalty_base;
  8523. job_penalty = battle_config.vip_exp_penalty_job;
  8524. zeny_penalty = battle_config.vip_zeny_penalty;
  8525. } else {
  8526. base_penalty = battle_config.death_penalty_base;
  8527. job_penalty = battle_config.death_penalty_job;
  8528. zeny_penalty = battle_config.zeny_penalty;
  8529. }
  8530. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8531. switch (battle_config.death_penalty_type) {
  8532. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8533. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8534. }
  8535. if (base_penalty){ //recheck after altering to speedup
  8536. if (battle_config.pk_mode && src && src->type==BL_PC)
  8537. base_penalty *= 2;
  8538. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8539. }
  8540. }
  8541. else
  8542. base_penalty = 0;
  8543. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8544. switch (battle_config.death_penalty_type) {
  8545. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8546. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8547. }
  8548. if (job_penalty) {
  8549. if (battle_config.pk_mode && src && src->type==BL_PC)
  8550. job_penalty *= 2;
  8551. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8552. }
  8553. }
  8554. else
  8555. job_penalty = 0;
  8556. if (base_penalty || job_penalty) {
  8557. int16 insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8558. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8559. pc_lostexp(sd, base_penalty, job_penalty);
  8560. }
  8561. if( zeny_penalty > 0 && !mapdata->getMapFlag(MF_NOZENYPENALTY)) {
  8562. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8563. if(zeny_penalty)
  8564. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER);
  8565. }
  8566. }
  8567. if( mapdata->getMapFlag(MF_PVP_NIGHTMAREDROP) ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8568. for (const auto &it : mapdata->drop_list) {
  8569. int32 id = it.drop_id, per = it.drop_per;
  8570. enum e_nightmare_drop_type type = it.drop_type;
  8571. if(id == 0)
  8572. continue;
  8573. if(id == -1){
  8574. int32 eq_num=0,eq_n[MAX_INVENTORY];
  8575. memset(eq_n,0,sizeof(eq_n));
  8576. for(i=0;i<MAX_INVENTORY;i++) {
  8577. if( sd->inventory.u.items_inventory[i].nameid == 0 ){
  8578. continue;
  8579. }
  8580. if( !pc_candrop( sd, &sd->inventory.u.items_inventory[i] ) ){
  8581. continue;
  8582. }
  8583. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8584. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8585. || type == NMDT_ALL)
  8586. {
  8587. eq_n[eq_num++] = i;
  8588. }
  8589. }
  8590. if(eq_num > 0){
  8591. if(rnd()%10000 < per) {
  8592. int32 n = eq_n[rnd() % eq_num];
  8593. if(sd->inventory.u.items_inventory[n].equip)
  8594. pc_unequipitem(sd,n,3);
  8595. pc_dropitem(sd,n,1);
  8596. }
  8597. }
  8598. }
  8599. else if(id > 0) {
  8600. for(i=0;i<MAX_INVENTORY;i++){
  8601. if(sd->inventory.u.items_inventory[i].nameid == id
  8602. && pc_candrop( sd, &sd->inventory.u.items_inventory[i] )
  8603. && rnd()%10000 < per
  8604. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8605. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8606. || type == NMDT_ALL) ){
  8607. if(sd->inventory.u.items_inventory[i].equip)
  8608. pc_unequipitem(sd,i,3);
  8609. pc_dropitem(sd,i,1);
  8610. break;
  8611. }
  8612. }
  8613. }
  8614. }
  8615. }
  8616. // pvp
  8617. // disable certain pvp functions on pk_mode [Valaris]
  8618. if( !battle_config.pk_mode && mapdata->getMapFlag(MF_PVP) && !mapdata->getMapFlag(MF_PVP_NOCALCRANK) ) {
  8619. sd->pvp_point -= 5;
  8620. sd->pvp_lost++;
  8621. if( src && src->type == BL_PC ) {
  8622. map_session_data *ssd = (map_session_data *)src;
  8623. ssd->pvp_point++;
  8624. ssd->pvp_won++;
  8625. }
  8626. if( sd->pvp_point < 0 ) {
  8627. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8628. return 1|8;
  8629. }
  8630. }
  8631. //GvG
  8632. if( mapdata_flag_gvg2(mapdata) ) {
  8633. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8634. return 1|8;
  8635. }
  8636. else if( sd->bg_id ) {
  8637. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8638. if (bg) {
  8639. if (bg->cemetery.map > 0) { // Respawn by BG
  8640. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8641. return 1|8;
  8642. }
  8643. }
  8644. }
  8645. //Reset "can log out" tick.
  8646. if( battle_config.prevent_logout )
  8647. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8648. return 1;
  8649. }
  8650. void pc_revive(map_session_data *sd,uint32 hp, uint32 sp, uint32 ap) {
  8651. if(hp) clif_updatestatus(*sd,SP_HP);
  8652. if(sp) clif_updatestatus(*sd,SP_SP);
  8653. if(ap) clif_updatestatus(*sd,SP_AP);
  8654. pc_setstand(sd, true);
  8655. if(battle_config.pc_invincible_time > 0)
  8656. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8657. if (sd->state.gmaster_flag && sd->guild) {
  8658. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  8659. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  8660. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  8661. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  8662. }
  8663. }
  8664. bool pc_revive_item(map_session_data *sd) {
  8665. nullpo_retr(false, sd);
  8666. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8667. return false;
  8668. if (sd->sc.getSCE(SC_HELLPOWER)) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8669. return false;
  8670. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8671. uint8 hp = 100, sp = 100;
  8672. if (item_position < 0) {
  8673. if (sd->sc.getSCE(SC_LIGHT_OF_REGENE)) {
  8674. hp = sd->sc.getSCE(SC_LIGHT_OF_REGENE)->val2;
  8675. sp = 0;
  8676. }
  8677. else
  8678. return false;
  8679. }
  8680. if (!status_revive(&sd->bl, hp, sp))
  8681. return false;
  8682. if (item_position < 0)
  8683. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE);
  8684. else
  8685. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8686. clif_skill_nodamage(&sd->bl, sd->bl, ALL_RESURRECTION, 4);
  8687. return true;
  8688. }
  8689. // script
  8690. //
  8691. /*==========================================
  8692. * script reading pc status registry
  8693. *------------------------------------------*/
  8694. int64 pc_readparam(map_session_data* sd,int64 type)
  8695. {
  8696. int64 val = 0;
  8697. nullpo_ret(sd);
  8698. switch(type) {
  8699. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8700. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8701. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8702. case SP_ZENY: val = sd->status.zeny; break;
  8703. case SP_BASELEVEL: val = sd->status.base_level; break;
  8704. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8705. case SP_CLASS: val = sd->status.class_; break;
  8706. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8707. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8708. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8709. case SP_SEX: val = sd->status.sex; break;
  8710. case SP_WEIGHT: val = sd->weight; break;
  8711. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8712. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8713. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8714. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8715. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8716. case SP_HP: val = sd->battle_status.hp; break;
  8717. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8718. case SP_SP: val = sd->battle_status.sp; break;
  8719. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8720. case SP_AP: val = sd->battle_status.ap; break;
  8721. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8722. case SP_STR: val = sd->status.str; break;
  8723. case SP_AGI: val = sd->status.agi; break;
  8724. case SP_VIT: val = sd->status.vit; break;
  8725. case SP_INT: val = sd->status.int_; break;
  8726. case SP_DEX: val = sd->status.dex; break;
  8727. case SP_LUK: val = sd->status.luk; break;
  8728. case SP_POW: val = sd->status.pow; break;
  8729. case SP_STA: val = sd->status.sta; break;
  8730. case SP_WIS: val = sd->status.wis; break;
  8731. case SP_SPL: val = sd->status.spl; break;
  8732. case SP_CON: val = sd->status.con; break;
  8733. case SP_CRT: val = sd->status.crt; break;
  8734. case SP_KARMA: val = sd->status.karma; break;
  8735. case SP_MANNER: val = sd->status.manner; break;
  8736. case SP_FAME: val = sd->status.fame; break;
  8737. case SP_KILLERRID: val = sd->killerrid; break;
  8738. case SP_KILLEDRID: val = sd->killedrid; break;
  8739. case SP_KILLEDGID: val = sd->killedgid; break;
  8740. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8741. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8742. case SP_CHARRENAME: val = sd->status.rename; break;
  8743. case SP_CHARFONT: val = sd->status.font; break;
  8744. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8745. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8746. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8747. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8748. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8749. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8750. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8751. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8752. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8753. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8754. case SP_ASPD: val = (AMOTION_ZERO_ASPD-sd->battle_status.amotion)/AMOTION_INTERVAL; break;
  8755. case SP_BASE_ATK:
  8756. #ifdef RENEWAL
  8757. val = sd->bonus.eatk;
  8758. #else
  8759. val = sd->battle_status.batk;
  8760. #endif
  8761. break;
  8762. case SP_DEF1: val = sd->battle_status.def; break;
  8763. case SP_DEF2: val = sd->battle_status.def2; break;
  8764. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8765. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8766. case SP_HIT: val = sd->battle_status.hit; break;
  8767. case SP_FLEE1: val = sd->battle_status.flee; break;
  8768. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8769. case SP_PATK: val = sd->battle_status.patk; break;
  8770. case SP_SMATK: val = sd->battle_status.smatk; break;
  8771. case SP_RES: val = sd->battle_status.res; break;
  8772. case SP_MRES: val = sd->battle_status.mres; break;
  8773. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8774. case SP_CRATE: val = sd->battle_status.crate; break;
  8775. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8776. case SP_MAXHPRATE: val = sd->hprate; break;
  8777. case SP_MAXSPRATE: val = sd->sprate; break;
  8778. case SP_MAXAPRATE: val = sd->aprate; break;
  8779. case SP_SPRATE: val = sd->dsprate; break;
  8780. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8781. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8782. case SP_ASPD_RATE:
  8783. #ifndef RENEWAL_ASPD
  8784. val = sd->battle_status.aspd_rate;
  8785. #else
  8786. val = sd->battle_status.aspd_rate2;
  8787. #endif
  8788. break;
  8789. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8790. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8791. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8792. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8793. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8794. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8795. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8796. case SP_MATK_RATE: val = sd->matk_rate; break;
  8797. case SP_ATK_RATE:
  8798. #ifdef RENEWAL
  8799. val = sd->bonus.atk_rate;
  8800. #else
  8801. ShowError( "pc_readparam: %s is not supported in Pre-Renewal mode.\n", QUOTE( SP_ATK_RATE ) );
  8802. #endif
  8803. break;
  8804. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8805. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8806. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8807. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8808. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8809. case SP_HIT_RATE: val = sd->hit_rate; break;
  8810. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8811. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8812. case SP_DEF_RATE: val = sd->def_rate; break;
  8813. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8814. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8815. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8816. case SP_PATK_RATE: val = sd->patk_rate; break;
  8817. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8818. case SP_RES_RATE: val = sd->res_rate; break;
  8819. case SP_MRES_RATE: val = sd->mres_rate; break;
  8820. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8821. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8822. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8823. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8824. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8825. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8826. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8827. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8828. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8829. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8830. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8831. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8832. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8833. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8834. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8835. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8836. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8837. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8838. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8839. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8840. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8841. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8842. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8843. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8844. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8845. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8846. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8847. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8848. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8849. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8850. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8851. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8852. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8853. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8854. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8855. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8856. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8857. case SP_UNSTRIPABLE:
  8858. case SP_UNSTRIPABLE_ARMOR:
  8859. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8860. break;
  8861. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8862. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8863. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8864. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8865. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8866. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8867. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8868. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8869. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8870. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8871. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8872. case SP_EMATK: val = sd->bonus.ematk; break;
  8873. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8874. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8875. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8876. case SP_CASTRATE:
  8877. case SP_VARCASTRATE:
  8878. #ifdef RENEWAL_CAST
  8879. val = sd->bonus.varcastrate; break;
  8880. #else
  8881. val = sd->castrate; break;
  8882. #endif
  8883. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8884. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8885. default:
  8886. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8887. return -1;
  8888. }
  8889. return val;
  8890. }
  8891. /*==========================================
  8892. * script set pc status registry
  8893. *------------------------------------------*/
  8894. bool pc_setparam(map_session_data *sd,int64 type,int64 val_tmp)
  8895. {
  8896. nullpo_retr(false,sd);
  8897. int32 val = static_cast<uint32>(val_tmp);
  8898. switch(type){
  8899. case SP_BASELEVEL:
  8900. if (val > pc_maxbaselv(sd)) //Capping to max
  8901. val = pc_maxbaselv(sd);
  8902. if (val > sd->status.base_level) {
  8903. for( int32 i = 0; i < (int32)( val - sd->status.base_level ); i++ ){
  8904. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8905. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8906. }
  8907. }
  8908. sd->status.base_level = val;
  8909. sd->status.base_exp = 0;
  8910. // clif_updatestatus(*sd, SP_BASELEVEL); // Gets updated at the bottom
  8911. clif_updatestatus(*sd, SP_NEXTBASEEXP);
  8912. clif_updatestatus(*sd, SP_STATUSPOINT);
  8913. clif_updatestatus(*sd, SP_TRAITPOINT);
  8914. clif_updatestatus(*sd, SP_BASEEXP);
  8915. status_calc_pc(sd, SCO_FORCE);
  8916. if(sd->status.party_id)
  8917. party_send_levelup(sd);
  8918. break;
  8919. case SP_JOBLEVEL:
  8920. if (val >= sd->status.job_level) {
  8921. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8922. sd->status.skill_point += val - sd->status.job_level;
  8923. clif_updatestatus(*sd, SP_SKILLPOINT);
  8924. }
  8925. sd->status.job_level = val;
  8926. sd->status.job_exp = 0;
  8927. // clif_updatestatus(*sd, SP_JOBLEVEL); // Gets updated at the bottom
  8928. clif_updatestatus(*sd, SP_NEXTJOBEXP);
  8929. clif_updatestatus(*sd, SP_JOBEXP);
  8930. status_calc_pc(sd, SCO_FORCE);
  8931. break;
  8932. case SP_SKILLPOINT:
  8933. sd->status.skill_point = val;
  8934. break;
  8935. case SP_STATUSPOINT:
  8936. sd->status.status_point = val;
  8937. break;
  8938. case SP_TRAITPOINT:
  8939. sd->status.trait_point = val;
  8940. break;
  8941. case SP_ZENY:
  8942. if( val < 0 )
  8943. return false;// can't set negative zeny
  8944. log_zeny(*sd, LOG_TYPE_SCRIPT, sd->status.char_id, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8945. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8946. break;
  8947. case SP_BASEEXP:
  8948. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8949. if (val_tmp < sd->status.base_exp) // Lost
  8950. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8951. else // Gained
  8952. pc_gainexp(sd, nullptr, val_tmp - sd->status.base_exp, 0, 2);
  8953. return true;
  8954. case SP_JOBEXP:
  8955. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8956. if (val_tmp < sd->status.job_exp) // Lost
  8957. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8958. else // Gained
  8959. pc_gainexp(sd, nullptr, 0, val_tmp - sd->status.job_exp, 2);
  8960. return true;
  8961. case SP_SEX:
  8962. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8963. break;
  8964. case SP_WEIGHT:
  8965. sd->weight = val;
  8966. break;
  8967. case SP_MAXWEIGHT:
  8968. sd->max_weight = val;
  8969. break;
  8970. case SP_HP:
  8971. sd->battle_status.hp = cap_value(val, 1, (int32)sd->battle_status.max_hp);
  8972. break;
  8973. case SP_MAXHP:
  8974. if (sd->status.base_level < 100)
  8975. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8976. else if (sd->status.base_level < 151)
  8977. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8978. else
  8979. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8980. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8981. {
  8982. sd->battle_status.hp = sd->battle_status.max_hp;
  8983. clif_updatestatus(*sd, SP_HP);
  8984. }
  8985. break;
  8986. case SP_SP:
  8987. sd->battle_status.sp = cap_value(val, 0, (int32)sd->battle_status.max_sp);
  8988. break;
  8989. case SP_MAXSP:
  8990. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8991. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8992. {
  8993. sd->battle_status.sp = sd->battle_status.max_sp;
  8994. clif_updatestatus(*sd, SP_SP);
  8995. }
  8996. break;
  8997. case SP_AP:
  8998. sd->battle_status.ap = cap_value(val, 0, (int32)sd->battle_status.max_ap);
  8999. break;
  9000. case SP_MAXAP:
  9001. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  9002. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  9003. sd->battle_status.ap = sd->battle_status.max_ap;
  9004. clif_updatestatus(*sd, SP_AP);
  9005. }
  9006. break;
  9007. case SP_STR:
  9008. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  9009. break;
  9010. case SP_AGI:
  9011. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  9012. break;
  9013. case SP_VIT:
  9014. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  9015. break;
  9016. case SP_INT:
  9017. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  9018. break;
  9019. case SP_DEX:
  9020. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  9021. break;
  9022. case SP_LUK:
  9023. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  9024. break;
  9025. case SP_POW:
  9026. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  9027. break;
  9028. case SP_STA:
  9029. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  9030. break;
  9031. case SP_WIS:
  9032. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  9033. break;
  9034. case SP_SPL:
  9035. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  9036. break;
  9037. case SP_CON:
  9038. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  9039. break;
  9040. case SP_CRT:
  9041. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  9042. break;
  9043. case SP_KARMA:
  9044. sd->status.karma = val;
  9045. break;
  9046. case SP_MANNER:
  9047. sd->status.manner = val;
  9048. if( val < 0 )
  9049. sc_start(nullptr, &sd->bl, SC_NOCHAT, 100, 0, 0);
  9050. else {
  9051. status_change_end(&sd->bl, SC_NOCHAT);
  9052. clif_manner_message(sd, 5);
  9053. }
  9054. return true; // status_change_start/status_change_end already sends packets warning the client
  9055. case SP_FAME:
  9056. sd->status.fame = val;
  9057. break;
  9058. case SP_KILLERRID:
  9059. sd->killerrid = val;
  9060. return true;
  9061. case SP_KILLEDRID:
  9062. sd->killedrid = val;
  9063. return true;
  9064. case SP_KILLEDGID:
  9065. sd->killedgid = val;
  9066. return true;
  9067. case SP_CHARMOVE:
  9068. sd->status.character_moves = val;
  9069. return true;
  9070. case SP_CHARRENAME:
  9071. sd->status.rename = val;
  9072. return true;
  9073. case SP_CHARFONT:
  9074. sd->status.font = val;
  9075. clif_font(sd);
  9076. return true;
  9077. case SP_BANK_VAULT:
  9078. if (val < 0)
  9079. return false;
  9080. log_zeny(*sd, LOG_TYPE_BANK, sd->status.char_id, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  9081. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  9082. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  9083. return true;
  9084. case SP_ROULETTE_BRONZE:
  9085. sd->roulette_point.bronze = val;
  9086. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  9087. return true;
  9088. case SP_ROULETTE_SILVER:
  9089. sd->roulette_point.silver = val;
  9090. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  9091. return true;
  9092. case SP_ROULETTE_GOLD:
  9093. sd->roulette_point.gold = val;
  9094. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  9095. return true;
  9096. case SP_CASHPOINTS:
  9097. if (val < 0)
  9098. return false;
  9099. if (!sd->state.connect_new)
  9100. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_CASHPOINT)));
  9101. sd->cashPoints = cap_value(val, 0, MAX_CASHPOINT);
  9102. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  9103. return true;
  9104. case SP_KAFRAPOINTS:
  9105. if (val < 0)
  9106. return false;
  9107. if (!sd->state.connect_new)
  9108. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_KAFRAPOINT)));
  9109. sd->kafraPoints = cap_value(val, 0, MAX_KAFRAPOINT);
  9110. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  9111. return true;
  9112. case SP_PCDIECOUNTER:
  9113. if (val < 0)
  9114. return false;
  9115. if (sd->die_counter == val)
  9116. return true;
  9117. sd->die_counter = val;
  9118. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  9119. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  9120. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  9121. return true;
  9122. case SP_COOKMASTERY:
  9123. if (sd->cook_mastery == val)
  9124. return true;
  9125. val = cap_value(val, 0, 1999);
  9126. sd->cook_mastery = val;
  9127. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  9128. return true;
  9129. default:
  9130. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  9131. return false;
  9132. }
  9133. clif_updatestatus(*sd,static_cast<_sp>(type));
  9134. return true;
  9135. }
  9136. /*==========================================
  9137. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  9138. *------------------------------------------*/
  9139. void pc_heal(map_session_data *sd,uint32 hp,uint32 sp, uint32 ap, int32 type)
  9140. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  9141. nullpo_retv(sd);
  9142. if (type&2) {
  9143. if (hp || type&4) {
  9144. clif_heal( *sd, SP_HP, hp );
  9145. clif_update_hp(*sd);
  9146. }
  9147. if (sp)
  9148. clif_heal( *sd, SP_SP, sp );
  9149. if (ap)
  9150. clif_heal( *sd, SP_AP, ap );
  9151. } else {
  9152. if(hp)
  9153. clif_updatestatus(*sd,SP_HP);
  9154. if(sp)
  9155. clif_updatestatus(*sd,SP_SP);
  9156. if (ap)
  9157. clif_updatestatus(*sd,SP_AP);
  9158. }
  9159. return;
  9160. }
  9161. /**
  9162. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  9163. * @param sd: Player data
  9164. * @param itemid: Item ID
  9165. * @param hp: HP to heal
  9166. * @param sp: SP to heal
  9167. * @return Amount healed to an object
  9168. */
  9169. int32 pc_itemheal(map_session_data *sd, t_itemid itemid, int32 hp, int32 sp)
  9170. {
  9171. int32 bonus, tmp, penalty = 0;
  9172. if (hp) {
  9173. bonus = 100 + (sd->battle_status.vit * 2) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9174. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9175. if (potion_flag == 2) {
  9176. bonus += bonus * 50 / 100;
  9177. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_ROGUE)
  9178. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  9179. }
  9180. //All item bonuses.
  9181. bonus += sd->bonus.itemhealrate2;
  9182. //Item Group bonuses
  9183. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  9184. //Individual item bonuses.
  9185. for(const auto &it : sd->itemhealrate) {
  9186. if (it.id == itemid) {
  9187. bonus += bonus * it.val / 100;
  9188. break;
  9189. }
  9190. }
  9191. // Recovery Potion
  9192. if (sd->sc.getSCE(SC_INCHEALRATE))
  9193. bonus += bonus * sd->sc.getSCE(SC_INCHEALRATE)->val1 / 100;
  9194. tmp = hp * bonus / 100; // Overflow check
  9195. if (bonus != 100 && tmp > hp)
  9196. hp = tmp;
  9197. }
  9198. if (sp) {
  9199. bonus = 100 + (sd->battle_status.int_ * 2) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9200. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9201. if (potion_flag == 2)
  9202. bonus += bonus * 50 / 100;
  9203. // All item bonuses.
  9204. bonus += sd->bonus.itemsphealrate2;
  9205. // Item Group bonuses
  9206. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  9207. // Individual item bonuses.
  9208. for( const auto &it : sd->itemsphealrate ){
  9209. if( it.id == itemid ){
  9210. bonus += bonus * it.val / 100;
  9211. break;
  9212. }
  9213. }
  9214. tmp = sp * bonus / 100; // Overflow check
  9215. if (bonus != 100 && tmp > sp)
  9216. sp = tmp;
  9217. }
  9218. if (!sd->sc.empty()) {
  9219. // Critical Wound and Death Hurt stack
  9220. if (sd->sc.getSCE(SC_CRITICALWOUND))
  9221. penalty += sd->sc.getSCE(SC_CRITICALWOUND)->val2;
  9222. if (sd->sc.getSCE(SC_DEATHHURT) && sd->sc.getSCE(SC_DEATHHURT)->val3 == 1)
  9223. penalty += 20;
  9224. if (sd->sc.getSCE(SC_NORECOVER_STATE))
  9225. penalty = 100;
  9226. if (sd->sc.getSCE(SC_VITALITYACTIVATION))
  9227. hp += hp / 2; // 1.5 times
  9228. if (sd->sc.getSCE(SC_WATER_INSIGNIA) && sd->sc.getSCE(SC_WATER_INSIGNIA)->val1 == 2) {
  9229. hp += hp / 10;
  9230. sp += sp / 10;
  9231. }
  9232. #ifdef RENEWAL
  9233. if (sd->sc.getSCE(SC_APPLEIDUN))
  9234. hp += sd->sc.getSCE(SC_APPLEIDUN)->val3 / 100;
  9235. #endif
  9236. if (penalty > 0) {
  9237. hp -= hp * penalty / 100;
  9238. sp -= sp * penalty / 100;
  9239. }
  9240. #ifdef RENEWAL
  9241. if (sd->sc.getSCE(SC_EXTREMITYFIST))
  9242. sp = 0;
  9243. #endif
  9244. if (sd->sc.getSCE(SC_BITESCAR))
  9245. hp = 0;
  9246. }
  9247. return status_heal(&sd->bl, hp, sp, 1);
  9248. }
  9249. /*==========================================
  9250. * HP/SP Recovery
  9251. * Heal player hp nad/or sp by rate
  9252. *------------------------------------------*/
  9253. int32 pc_percentheal(map_session_data *sd,int32 hp,int32 sp)
  9254. {
  9255. nullpo_ret(sd);
  9256. if (hp > 100) hp = 100;
  9257. else if (hp <-100) hp = -100;
  9258. if (sp > 100) sp = 100;
  9259. else if (sp <-100) sp = -100;
  9260. if(hp >= 0 && sp >= 0) //Heal
  9261. return status_percent_heal(&sd->bl, hp, sp);
  9262. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  9263. return status_percent_damage(nullptr, &sd->bl, hp, sp, hp==-100);
  9264. //Crossed signs
  9265. if(hp) {
  9266. if(hp > 0)
  9267. status_percent_heal(&sd->bl, hp, 0);
  9268. else
  9269. status_percent_damage(nullptr, &sd->bl, hp, 0, hp==-100);
  9270. }
  9271. if(sp) {
  9272. if(sp > 0)
  9273. status_percent_heal(&sd->bl, 0, sp);
  9274. else
  9275. status_percent_damage(nullptr, &sd->bl, 0, sp, false);
  9276. }
  9277. return 0;
  9278. }
  9279. static int32 jobchange_killclone(struct block_list *bl, va_list ap)
  9280. {
  9281. struct mob_data *md;
  9282. int32 flag;
  9283. md = (struct mob_data *)bl;
  9284. nullpo_ret(md);
  9285. flag = va_arg(ap, int32);
  9286. if (md->master_id && md->special_state.clone && md->master_id == flag)
  9287. status_kill(&md->bl);
  9288. return 1;
  9289. }
  9290. /**
  9291. * Called when player changes job
  9292. * Rewrote to make it tidider [Celest]
  9293. * @param sd
  9294. * @param job JOB ID. See enum e_job
  9295. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  9296. * @return True if success, false if failed
  9297. **/
  9298. bool pc_jobchange(map_session_data *sd,int32 job, char upper)
  9299. {
  9300. int32 i, fame_flag = 0;
  9301. nullpo_retr(false,sd);
  9302. if (job < 0)
  9303. return false;
  9304. //Normalize job.
  9305. uint64 b_class = pc_jobid2mapid(job);
  9306. if (b_class == -1)
  9307. return false;
  9308. switch (upper) {
  9309. case 1:
  9310. b_class|= JOBL_UPPER;
  9311. break;
  9312. case 2:
  9313. b_class|= JOBL_BABY;
  9314. break;
  9315. }
  9316. //This will automatically adjust bard/dancer classes to the correct gender
  9317. //That is, if you try to jobchange into dancer, it will turn you to bard.
  9318. job = pc_mapid2jobid(b_class, sd->status.sex);
  9319. if (job == -1)
  9320. return false;
  9321. if ((uint16)b_class == sd->class_)
  9322. return false; //Nothing to change.
  9323. // If the job does not exist in the job db, dont allow changing to it
  9324. if( !job_db.exists( job ) ){
  9325. return false;
  9326. }
  9327. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  9328. // Changing to 4th job
  9329. sd->change_level_4th = sd->status.job_level;
  9330. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  9331. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  9332. // changing from 2nd to 3rd job
  9333. sd->change_level_3rd = sd->status.job_level;
  9334. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  9335. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  9336. // changing from 1st to 2nd job
  9337. sd->change_level_2nd = sd->status.job_level;
  9338. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  9339. }
  9340. if(sd->cloneskill_idx > 0) {
  9341. pc_skill_plagiarism_reset(*sd, 1);
  9342. }
  9343. if(sd->reproduceskill_idx > 0) {
  9344. pc_skill_plagiarism_reset(*sd, 2);
  9345. }
  9346. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  9347. status_db.changeSkillTree(sd);
  9348. }
  9349. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  9350. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  9351. pc_resetfeel(sd);
  9352. pc_resethate(sd);
  9353. }
  9354. // Reset body style to 0 before changing job to avoid
  9355. // errors since not every job has a alternate outfit.
  9356. sd->status.body = 0;
  9357. clif_changelook(&sd->bl,LOOK_BODY2,0);
  9358. sd->status.class_ = job;
  9359. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  9360. uint64 previous_class = sd->class_;
  9361. sd->class_ = (uint16)b_class;
  9362. sd->status.job_level=1;
  9363. sd->status.job_exp=0;
  9364. if (sd->status.base_level > pc_maxbaselv(sd)) {
  9365. sd->status.base_level = pc_maxbaselv(sd);
  9366. sd->status.base_exp=0;
  9367. pc_resetstate(sd);
  9368. clif_updatestatus(*sd,SP_STATUSPOINT);
  9369. clif_updatestatus(*sd,SP_TRAITPOINT);
  9370. clif_updatestatus(*sd,SP_BASELEVEL);
  9371. clif_updatestatus(*sd,SP_BASEEXP);
  9372. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  9373. }
  9374. // Give or reduce transcendent status points
  9375. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  9376. sd->status.status_point += battle_config.transcendent_status_points;
  9377. clif_updatestatus(*sd,SP_STATUSPOINT);
  9378. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  9379. if( sd->status.status_point < battle_config.transcendent_status_points ){
  9380. // The player already used his bonus points, so we have to reset his status points
  9381. pc_resetstate(sd);
  9382. }else{
  9383. sd->status.status_point -= battle_config.transcendent_status_points;
  9384. clif_updatestatus(*sd,SP_STATUSPOINT);
  9385. }
  9386. }
  9387. // Give or reduce trait status points
  9388. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  9389. sd->status.trait_point += battle_config.trait_points_job_change;
  9390. clif_updatestatus(*sd, SP_TRAITPOINT);
  9391. clif_updatestatus(*sd, SP_UPOW);
  9392. clif_updatestatus(*sd, SP_USTA);
  9393. clif_updatestatus(*sd, SP_UWIS);
  9394. clif_updatestatus(*sd, SP_USPL);
  9395. clif_updatestatus(*sd, SP_UCON);
  9396. clif_updatestatus(*sd, SP_UCRT);
  9397. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  9398. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  9399. // Player may have already used the trait status points. Force a reset.
  9400. pc_resetstate(sd);
  9401. } else {
  9402. sd->status.trait_point = 0;
  9403. clif_updatestatus(*sd, SP_TRAITPOINT);
  9404. clif_updatestatus(*sd, SP_UPOW);
  9405. clif_updatestatus(*sd, SP_USTA);
  9406. clif_updatestatus(*sd, SP_UWIS);
  9407. clif_updatestatus(*sd, SP_USPL);
  9408. clif_updatestatus(*sd, SP_UCON);
  9409. clif_updatestatus(*sd, SP_UCRT);
  9410. }
  9411. }
  9412. clif_updatestatus(*sd,SP_JOBLEVEL);
  9413. clif_updatestatus(*sd,SP_JOBEXP);
  9414. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  9415. for(i=0;i<EQI_MAX;i++) {
  9416. if(sd->equip_index[i] >= 0)
  9417. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  9418. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  9419. }
  9420. }
  9421. //Change look, if disguised, you need to undisguise
  9422. //to correctly calculate new job sprite without
  9423. if (sd->disguise)
  9424. pc_disguise(sd, 0);
  9425. status_set_viewdata(&sd->bl, job);
  9426. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  9427. #if PACKETVER >= 20151001
  9428. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  9429. #endif
  9430. if(sd->vd.cloth_color)
  9431. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9432. /*
  9433. if(sd->vd.body_style)
  9434. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9435. */
  9436. //Update skill tree.
  9437. pc_calc_skilltree(sd);
  9438. clif_skillinfoblock(sd);
  9439. if (sd->ed)
  9440. elemental_delete(sd->ed);
  9441. if (sd->state.vending)
  9442. vending_closevending(sd);
  9443. if (sd->state.buyingstore)
  9444. buyingstore_close(sd);
  9445. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  9446. //Remove peco/cart/falcon
  9447. i = sd->sc.option;
  9448. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  9449. i&=~OPTION_RIDING;
  9450. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9451. i&=~OPTION_FALCON;
  9452. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9453. i&=~OPTION_DRAGON;
  9454. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9455. i&=~OPTION_WUGRIDER;
  9456. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9457. i&=~OPTION_WUG;
  9458. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9459. i&=~OPTION_MADOGEAR;
  9460. #ifndef NEW_CARTS
  9461. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9462. i&=~OPTION_CART;
  9463. #else
  9464. if( sd->sc.getSCE(SC_PUSH_CART) && !pc_checkskill(sd, MC_PUSHCART) )
  9465. pc_setcart(sd, 0);
  9466. #endif
  9467. if(i != sd->sc.option)
  9468. pc_setoption(sd, i);
  9469. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9470. hom_vaporize(sd, HOM_ST_ACTIVE);
  9471. if (sd->sc.getSCE(SC_SPRITEMABLE) && !pc_checkskill(sd, SU_SPRITEMABLE))
  9472. status_change_end(&sd->bl, SC_SPRITEMABLE);
  9473. if (sd->sc.getSCE(SC_SOULATTACK) && !pc_checkskill(sd, SU_SOULATTACK))
  9474. status_change_end(&sd->bl, SC_SOULATTACK);
  9475. if( sd->sc.getSCE(SC_SPIRIT) ){
  9476. status_change_end( &sd->bl, SC_SPIRIT );
  9477. }
  9478. if(sd->status.manner < 0){
  9479. clif_changemanner( *sd );
  9480. clif_updatestatus(*sd,SP_MANNER);
  9481. }
  9482. status_calc_pc(sd,SCO_FORCE);
  9483. pc_checkallowskill(sd);
  9484. pc_equiplookall(sd);
  9485. pc_show_questinfo(sd);
  9486. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9487. if( sd->status.party_id ){
  9488. struct party_data* p;
  9489. if( ( p = party_search( sd->status.party_id ) ) != nullptr ){
  9490. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9491. if( i < MAX_PARTY ){
  9492. p->party.member[i].class_ = sd->status.class_;
  9493. clif_party_job_and_level( *sd );
  9494. }
  9495. }
  9496. }
  9497. chrif_save(sd, CSAVE_NORMAL);
  9498. //if you were previously famous, not anymore.
  9499. if (fame_flag)
  9500. chrif_buildfamelist();
  9501. else if (sd->status.fame > 0) {
  9502. //It may be that now they are famous?
  9503. switch (sd->class_&MAPID_UPPERMASK) {
  9504. case MAPID_BLACKSMITH:
  9505. case MAPID_ALCHEMIST:
  9506. case MAPID_TAEKWON:
  9507. chrif_buildfamelist();
  9508. break;
  9509. }
  9510. }
  9511. return true;
  9512. }
  9513. /*==========================================
  9514. * Tell client player sd has change equipement
  9515. *------------------------------------------*/
  9516. void pc_equiplookall(map_session_data *sd)
  9517. {
  9518. nullpo_retv(sd);
  9519. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9520. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9521. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9522. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9523. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9524. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9525. }
  9526. /*==========================================
  9527. * Tell client player sd has change look (hair,equip...)
  9528. *------------------------------------------*/
  9529. void pc_changelook(map_session_data *sd,int32 type,int32 val) {
  9530. nullpo_retv(sd);
  9531. switch(type) {
  9532. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9533. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9534. if (sd->status.hair != val) {
  9535. sd->status.hair = val;
  9536. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9537. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9538. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9539. }
  9540. break;
  9541. case LOOK_WEAPON:
  9542. sd->status.weapon = val;
  9543. break;
  9544. case LOOK_HEAD_BOTTOM:
  9545. sd->status.head_bottom = val;
  9546. sd->setlook_head_bottom = val;
  9547. break;
  9548. case LOOK_HEAD_TOP:
  9549. sd->status.head_top = val;
  9550. sd->setlook_head_top = val;
  9551. break;
  9552. case LOOK_HEAD_MID:
  9553. sd->status.head_mid = val;
  9554. sd->setlook_head_mid = val;
  9555. break;
  9556. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9557. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9558. if (sd->status.hair_color != val) {
  9559. sd->status.hair_color = val;
  9560. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9561. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9562. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9563. }
  9564. break;
  9565. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9566. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9567. sd->status.clothes_color = val;
  9568. break;
  9569. case LOOK_SHIELD:
  9570. sd->status.shield = val;
  9571. break;
  9572. case LOOK_SHOES:
  9573. break;
  9574. case LOOK_ROBE:
  9575. sd->status.robe = val;
  9576. sd->setlook_robe = val;
  9577. break;
  9578. case LOOK_BODY2:
  9579. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9580. sd->status.body = val;
  9581. break;
  9582. }
  9583. clif_changelook(&sd->bl, type, val);
  9584. }
  9585. /*==========================================
  9586. * Give an option (type) to player (sd) and display it to client
  9587. *------------------------------------------*/
  9588. void pc_setoption(map_session_data *sd,int32 type, int32 subtype)
  9589. {
  9590. int32 p_type, new_look=0;
  9591. nullpo_retv(sd);
  9592. p_type = sd->sc.option;
  9593. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9594. sd->sc.option=type;
  9595. clif_changeoption(&sd->bl);
  9596. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9597. { // Mounting
  9598. clif_status_load(&sd->bl,EFST_RIDING,1);
  9599. status_calc_pc(sd,SCO_NONE);
  9600. }
  9601. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9602. { // Dismount
  9603. clif_status_load(&sd->bl,EFST_RIDING,0);
  9604. status_calc_pc(sd,SCO_NONE);
  9605. }
  9606. #ifndef NEW_CARTS
  9607. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9608. clif_cartlist(sd);
  9609. clif_updatestatus(*sd, SP_CARTINFO);
  9610. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9611. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9612. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9613. clif_clearcart(sd->fd);
  9614. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9615. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9616. }
  9617. #endif
  9618. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9619. clif_status_load(&sd->bl,EFST_FALCON,1);
  9620. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9621. clif_status_load(&sd->bl,EFST_FALCON,0);
  9622. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9623. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9624. status_calc_pc(sd,SCO_NONE);
  9625. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9626. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9627. status_calc_pc(sd,SCO_NONE);
  9628. }
  9629. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9630. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9631. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9632. status_change_end(&sd->bl, SC_MADOGEAR);
  9633. }
  9634. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9635. new_look = JOB_STAR_GLADIATOR2;
  9636. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9637. new_look = -1;
  9638. if (sd->disguise || !new_look)
  9639. return; //Disguises break sprite changes
  9640. if (new_look < 0) { //Restore normal look.
  9641. status_set_viewdata(&sd->bl, sd->status.class_);
  9642. new_look = sd->vd.class_;
  9643. }
  9644. // Stop attacking on new view change (to prevent wedding/santa attacks).
  9645. unit_stop_attack( &sd->bl );
  9646. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9647. if (sd->vd.cloth_color)
  9648. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9649. if( sd->vd.body_style )
  9650. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9651. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9652. }
  9653. /**
  9654. * Give player a cart
  9655. * @param sd Player
  9656. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9657. **/
  9658. bool pc_setcart(map_session_data *sd,int32 type) {
  9659. #ifndef NEW_CARTS
  9660. int32 cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9661. int32 option;
  9662. #endif
  9663. nullpo_retr(false,sd);
  9664. if( type < 0 || type > MAX_CARTS )
  9665. return false;// Never trust the values sent by the client! [Skotlex]
  9666. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9667. return false;// Push cart is required
  9668. #ifdef NEW_CARTS
  9669. switch( type ) {
  9670. case 0:
  9671. if( !sd->sc.getSCE(SC_PUSH_CART) )
  9672. return 0;
  9673. status_change_end(&sd->bl,SC_PUSH_CART);
  9674. clif_clearcart(sd->fd);
  9675. break;
  9676. default:/* everything else is an allowed ID so we can move on */
  9677. if( !sd->sc.getSCE(SC_PUSH_CART) ) { /* first time, so fill cart data */
  9678. clif_cartlist(sd);
  9679. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9680. }
  9681. clif_updatestatus(*sd, SP_CARTINFO);
  9682. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9683. break;
  9684. }
  9685. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9686. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9687. #else
  9688. // Update option
  9689. option = sd->sc.option;
  9690. option &= ~OPTION_CART;// clear cart bits
  9691. option |= cart[type]; // set cart
  9692. pc_setoption(sd, option);
  9693. #endif
  9694. return true;
  9695. }
  9696. /*==========================================
  9697. * Give player a falcon
  9698. *------------------------------------------*/
  9699. void pc_setfalcon(map_session_data* sd, int32 flag)
  9700. {
  9701. if( flag ){
  9702. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9703. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9704. } else if( pc_isfalcon(sd) ){
  9705. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9706. }
  9707. }
  9708. /*==========================================
  9709. * Set player riding
  9710. *------------------------------------------*/
  9711. void pc_setriding(map_session_data* sd, int32 flag)
  9712. {
  9713. if( sd->sc.getSCE(SC_ALL_RIDING) )
  9714. return;
  9715. if( flag ){
  9716. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9717. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9718. } else if( pc_isriding(sd) ){
  9719. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9720. }
  9721. }
  9722. /**
  9723. * Give player a mado
  9724. * @param sd: Player
  9725. * @param flag: Enable or disable mado
  9726. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9727. */
  9728. void pc_setmadogear(map_session_data *sd, bool flag, e_mado_type type)
  9729. {
  9730. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9731. return;
  9732. if (flag) {
  9733. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9734. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9735. } else if (pc_ismadogear(sd)) {
  9736. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9737. }
  9738. }
  9739. /*==========================================
  9740. * Check if player can drop an item
  9741. *------------------------------------------*/
  9742. bool pc_candrop(map_session_data *sd, struct item *item)
  9743. {
  9744. if (sd->sc.cant.drop)
  9745. return false;
  9746. if( item && itemdb_ishatched_egg(item) )
  9747. return false;
  9748. if (pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL)) // no restriction
  9749. return true;
  9750. if( !pc_can_give_items(sd) )
  9751. return false;
  9752. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  9753. return false;
  9754. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9755. }
  9756. /*==========================================
  9757. * Read '@type' variables (temporary numeric char reg)
  9758. *------------------------------------------*/
  9759. int64 pc_readreg(map_session_data* sd, int64 reg)
  9760. {
  9761. return i64db_i64get(sd->regs.vars, reg);
  9762. }
  9763. /*==========================================
  9764. * Set '@type' variables (temporary numeric char reg)
  9765. *------------------------------------------*/
  9766. bool pc_setreg(map_session_data* sd, int64 reg, int64 val)
  9767. {
  9768. uint32 index = script_getvaridx(reg);
  9769. nullpo_retr(false, sd);
  9770. if( val ) {
  9771. i64db_i64put(sd->regs.vars, reg, val);
  9772. if( index )
  9773. script_array_update(&sd->regs, reg, false);
  9774. } else {
  9775. i64db_remove(sd->regs.vars, reg);
  9776. if( index )
  9777. script_array_update(&sd->regs, reg, true);
  9778. }
  9779. return true;
  9780. }
  9781. /*==========================================
  9782. * Read '@type$' variables (temporary string char reg)
  9783. *------------------------------------------*/
  9784. char* pc_readregstr(map_session_data* sd, int64 reg)
  9785. {
  9786. struct script_reg_str *p = nullptr;
  9787. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9788. return p ? p->value : nullptr;
  9789. }
  9790. /*==========================================
  9791. * Set '@type$' variables (temporary string char reg)
  9792. *------------------------------------------*/
  9793. bool pc_setregstr(map_session_data* sd, int64 reg, const char* str)
  9794. {
  9795. struct script_reg_str *p = nullptr;
  9796. uint32 index = script_getvaridx(reg);
  9797. DBData prev;
  9798. nullpo_retr(false, sd);
  9799. if( str[0] ) {
  9800. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9801. p->value = aStrdup(str);
  9802. p->flag.type = 1;
  9803. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9804. p = (struct script_reg_str *)db_data2ptr(&prev);
  9805. if( p->value )
  9806. aFree(p->value);
  9807. ers_free(str_reg_ers, p);
  9808. } else {
  9809. if( index )
  9810. script_array_update(&sd->regs, reg, false);
  9811. }
  9812. } else {
  9813. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9814. p = (struct script_reg_str *)db_data2ptr(&prev);
  9815. if( p->value )
  9816. aFree(p->value);
  9817. ers_free(str_reg_ers, p);
  9818. if( index )
  9819. script_array_update(&sd->regs, reg, true);
  9820. }
  9821. }
  9822. return true;
  9823. }
  9824. /**
  9825. * Serves the following variable types:
  9826. * - 'type' (permanent numeric char reg)
  9827. * - '#type' (permanent numeric account reg)
  9828. * - '##type' (permanent numeric account reg2)
  9829. **/
  9830. int64 pc_readregistry(map_session_data *sd, int64 reg)
  9831. {
  9832. struct script_reg_num *p = nullptr;
  9833. if (!sd->vars_ok) {
  9834. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9835. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9836. //intif->request_registry(sd,type==3?4:type);
  9837. set_eof(sd->fd);
  9838. return 0;
  9839. }
  9840. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9841. return p ? p->value : 0;
  9842. }
  9843. /**
  9844. * Serves the following variable types:
  9845. * - 'type$' (permanent str char reg)
  9846. * - '#type$' (permanent str account reg)
  9847. * - '##type$' (permanent str account reg2)
  9848. **/
  9849. char* pc_readregistry_str(map_session_data *sd, int64 reg)
  9850. {
  9851. struct script_reg_str *p = nullptr;
  9852. if (!sd->vars_ok) {
  9853. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9854. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9855. //intif->request_registry(sd,type==3?4:type);
  9856. set_eof(sd->fd);
  9857. return nullptr;
  9858. }
  9859. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9860. return p ? p->value : nullptr;
  9861. }
  9862. /**
  9863. * Serves the following variable types:
  9864. * - 'type' (permanent numeric char reg)
  9865. * - '#type' (permanent numeric account reg)
  9866. * - '##type' (permanent numeric account reg2)
  9867. **/
  9868. bool pc_setregistry(map_session_data *sd, int64 reg, int64 val)
  9869. {
  9870. struct script_reg_num *p = nullptr;
  9871. const char *regname = get_str(script_getvarid(reg));
  9872. uint32 index = script_getvaridx(reg);
  9873. if ( !reg_load && !sd->vars_ok ) {
  9874. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9875. return false;
  9876. }
  9877. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9878. if( val ) {
  9879. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9880. script_array_update(&sd->regs, reg, false);
  9881. p->value = val;
  9882. } else {
  9883. p->value = 0;
  9884. if( index )
  9885. script_array_update(&sd->regs, reg, true);
  9886. }
  9887. if (!reg_load)
  9888. p->flag.update = 1;/* either way, it will require either delete or replace */
  9889. } else if( val ) {
  9890. DBData prev;
  9891. if( index )
  9892. script_array_update(&sd->regs, reg, false);
  9893. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9894. p->value = val;
  9895. if (!reg_load)
  9896. p->flag.update = 1;
  9897. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9898. p = (struct script_reg_num *)db_data2ptr(&prev);
  9899. ers_free(num_reg_ers, p);
  9900. }
  9901. }
  9902. if (!reg_load && p)
  9903. sd->vars_dirty = true;
  9904. return true;
  9905. }
  9906. /**
  9907. * Serves the following variable types:
  9908. * - 'type$' (permanent str char reg)
  9909. * - '#type$' (permanent str account reg)
  9910. * - '##type$' (permanent str account reg2)
  9911. **/
  9912. bool pc_setregistry_str(map_session_data *sd, int64 reg, const char *val)
  9913. {
  9914. struct script_reg_str *p = nullptr;
  9915. const char *regname = get_str(script_getvarid(reg));
  9916. uint32 index = script_getvaridx(reg);
  9917. size_t vlen = 0;
  9918. if (!reg_load && !sd->vars_ok) {
  9919. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9920. return false;
  9921. }
  9922. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9923. {
  9924. 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);
  9925. return false;
  9926. }
  9927. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9928. if( val[0] ) {
  9929. if( p->value )
  9930. aFree(p->value);
  9931. else if ( index ) // an entry that was deleted, so we reset
  9932. script_array_update(&sd->regs, reg, false);
  9933. p->value = aStrdup(val);
  9934. } else {
  9935. if (p->value)
  9936. aFree(p->value);
  9937. p->value = nullptr;
  9938. if( index )
  9939. script_array_update(&sd->regs, reg, true);
  9940. }
  9941. if( !reg_load )
  9942. p->flag.update = 1; // either way, it will require either delete or replace
  9943. } else if( val[0] ) {
  9944. DBData prev;
  9945. if( index )
  9946. script_array_update(&sd->regs, reg, false);
  9947. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9948. p->value = aStrdup(val);
  9949. if( !reg_load )
  9950. p->flag.update = 1;
  9951. p->flag.type = 1;
  9952. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9953. p = (struct script_reg_str *)db_data2ptr(&prev);
  9954. if( p->value )
  9955. aFree(p->value);
  9956. ers_free(str_reg_ers, p);
  9957. }
  9958. }
  9959. if( !reg_load && p )
  9960. sd->vars_dirty = true;
  9961. return true;
  9962. }
  9963. /**
  9964. * Set value of player variable
  9965. * @param sd Player
  9966. * @param reg Variable name
  9967. * @param value
  9968. * @return True if success, false if failed.
  9969. **/
  9970. bool pc_setreg2(map_session_data *sd, const char *reg, int64 val) {
  9971. char prefix = reg[0];
  9972. nullpo_retr(false, sd);
  9973. if (reg[strlen(reg)-1] == '$') {
  9974. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9975. return false;
  9976. }
  9977. val = cap_value(val, INT_MIN, INT_MAX);
  9978. switch (prefix) {
  9979. case '.':
  9980. case '\'':
  9981. case '$':
  9982. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9983. return false;
  9984. case '@':
  9985. return pc_setreg(sd, add_str(reg), val);
  9986. case '#':
  9987. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9988. default:
  9989. return pc_setglobalreg(sd, add_str(reg), val);
  9990. }
  9991. return false;
  9992. }
  9993. /**
  9994. * Get value of player variable
  9995. * @param sd Player
  9996. * @param reg Variable name
  9997. * @return Variable value or 0 if failed.
  9998. **/
  9999. int64 pc_readreg2(map_session_data *sd, const char *reg) {
  10000. char prefix = reg[0];
  10001. nullpo_ret(sd);
  10002. if (reg[strlen(reg)-1] == '$') {
  10003. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  10004. return 0;
  10005. }
  10006. switch (prefix) {
  10007. case '.':
  10008. case '\'':
  10009. case '$':
  10010. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  10011. return 0;
  10012. case '@':
  10013. return pc_readreg(sd, add_str(reg));
  10014. case '#':
  10015. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  10016. default:
  10017. return pc_readglobalreg(sd, add_str(reg));
  10018. }
  10019. return 0;
  10020. }
  10021. /*==========================================
  10022. * Exec eventtimer for player sd (retrieved from map_session (id))
  10023. *------------------------------------------*/
  10024. static TIMER_FUNC(pc_eventtimer){
  10025. map_session_data *sd=map_id2sd(id);
  10026. char *p = (char *)data;
  10027. int32 i;
  10028. if(sd==nullptr)
  10029. return 0;
  10030. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  10031. if( i < MAX_EVENTTIMER )
  10032. {
  10033. sd->eventtimer[i] = INVALID_TIMER;
  10034. sd->eventcount--;
  10035. npc_event(sd,p,0);
  10036. }
  10037. else
  10038. ShowError("pc_eventtimer: no such event timer\n");
  10039. if (p) aFree(p);
  10040. return 0;
  10041. }
  10042. /*==========================================
  10043. * Add eventtimer for player sd ?
  10044. *------------------------------------------*/
  10045. bool pc_addeventtimer(map_session_data *sd,int32 tick,const char *name)
  10046. {
  10047. int32 i;
  10048. nullpo_retr(false,sd);
  10049. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  10050. if( i == MAX_EVENTTIMER )
  10051. return false;
  10052. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  10053. sd->eventcount++;
  10054. return true;
  10055. }
  10056. /*==========================================
  10057. * Del eventtimer for player sd ?
  10058. *------------------------------------------*/
  10059. bool pc_deleventtimer(map_session_data *sd,const char *name)
  10060. {
  10061. char* p = nullptr;
  10062. int32 i;
  10063. nullpo_retr(false,sd);
  10064. if (sd->eventcount == 0)
  10065. return false;
  10066. // find the named event timer
  10067. ARR_FIND( 0, MAX_EVENTTIMER, i,
  10068. sd->eventtimer[i] != INVALID_TIMER &&
  10069. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != nullptr &&
  10070. strcmp(p, name) == 0
  10071. );
  10072. if( i == MAX_EVENTTIMER )
  10073. return false; // not found
  10074. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10075. sd->eventtimer[i] = INVALID_TIMER;
  10076. if(sd->eventcount > 0)
  10077. sd->eventcount--;
  10078. aFree(p);
  10079. return true;
  10080. }
  10081. /*==========================================
  10082. * Update eventtimer count for player sd
  10083. *------------------------------------------*/
  10084. void pc_addeventtimercount(map_session_data *sd,const char *name,int32 tick)
  10085. {
  10086. int32 i;
  10087. nullpo_retv(sd);
  10088. for(i=0;i<MAX_EVENTTIMER;i++)
  10089. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  10090. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  10091. addtick_timer(sd->eventtimer[i],tick);
  10092. break;
  10093. }
  10094. }
  10095. /*==========================================
  10096. * Remove all eventtimer for player sd
  10097. *------------------------------------------*/
  10098. void pc_cleareventtimer(map_session_data *sd)
  10099. {
  10100. int32 i;
  10101. nullpo_retv(sd);
  10102. if (sd->eventcount == 0)
  10103. return;
  10104. for(i=0;i<MAX_EVENTTIMER;i++){
  10105. if( sd->eventtimer[i] != INVALID_TIMER ){
  10106. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  10107. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10108. sd->eventtimer[i] = INVALID_TIMER;
  10109. if(sd->eventcount > 0) //avoid looping to max val
  10110. sd->eventcount--;
  10111. if (p) aFree(p);
  10112. }
  10113. }
  10114. }
  10115. /**
  10116. * Called when an item with combo is worn
  10117. * @param sd: Player data
  10118. * @param data: Item data
  10119. * @return Number of succeeded combo(s)
  10120. */
  10121. static int32 pc_checkcombo(map_session_data *sd, item_data *data) {
  10122. int32 success = 0;
  10123. for (const auto &item_combo : data->combos) {
  10124. bool do_continue = false;
  10125. // Ensure this isn't a duplicate combo
  10126. for (const auto player_combo : sd->combos) {
  10127. if (player_combo->id == item_combo->id) {
  10128. do_continue = true;
  10129. break;
  10130. }
  10131. }
  10132. // Combo already equipped
  10133. if (do_continue)
  10134. continue;
  10135. size_t nb_itemCombo = item_combo->nameid.size();
  10136. if (nb_itemCombo < 2) // A combo with less then 2 item?
  10137. continue;
  10138. struct s_itemchk {
  10139. int32 idx;
  10140. t_itemid nameid, card[MAX_SLOTS];
  10141. s_itemchk() : idx(0), nameid(0), card() {};
  10142. };
  10143. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  10144. size_t j;
  10145. uint32 pos = 0;
  10146. for (j = 0; j < nb_itemCombo; j++) {
  10147. t_itemid id = item_combo->nameid[j];
  10148. bool found = false;
  10149. for (int16 k = 0; k < EQI_MAX; k++) {
  10150. int16 index = sd->equip_index[k];
  10151. if (index < 0)
  10152. continue;
  10153. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  10154. continue;
  10155. if (!sd->inventory_data[index])
  10156. continue;
  10157. if (itemdb_type(id) != IT_CARD) {
  10158. if (sd->inventory_data[index]->nameid != id)
  10159. continue;
  10160. if (j > 0) { // Check if this item not already used
  10161. do_continue = false;
  10162. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  10163. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  10164. do_continue = true;
  10165. break;
  10166. }
  10167. }
  10168. if (do_continue)
  10169. continue;
  10170. }
  10171. combo_idx[j].nameid = id;
  10172. combo_idx[j].idx = index;
  10173. pos |= sd->inventory.u.items_inventory[index].equip;
  10174. found = true;
  10175. break;
  10176. } else { // Cards and enchants
  10177. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  10178. continue;
  10179. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  10180. do_continue = false;
  10181. if (sd->inventory.u.items_inventory[index].card[z] != id)
  10182. continue;
  10183. if (j > 0) {
  10184. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  10185. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  10186. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  10187. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  10188. do_continue = true;
  10189. break;
  10190. }
  10191. }
  10192. }
  10193. }
  10194. }
  10195. if (do_continue)
  10196. continue;
  10197. combo_idx[j].nameid = id;
  10198. combo_idx[j].idx = index;
  10199. combo_idx[j].card[z] = id;
  10200. pos |= sd->inventory.u.items_inventory[index].equip;
  10201. found = true;
  10202. break;
  10203. }
  10204. }
  10205. }
  10206. if (!found)
  10207. break; // Unable to found all the IDs for this combo, return
  10208. }
  10209. // Broke out of the count loop without finding all IDs, move to the next combo
  10210. if (j < nb_itemCombo)
  10211. continue;
  10212. // All items in the combo are matching
  10213. auto entry = std::make_shared<s_combos>();
  10214. entry->bonus = item_combo->script;
  10215. entry->id = item_combo->id;
  10216. entry->pos = pos;
  10217. sd->combos.push_back(entry);
  10218. combo_idx.clear();
  10219. success++;
  10220. }
  10221. return success;
  10222. }
  10223. /**
  10224. * Called when an item with combo is removed
  10225. * @param sd: Player data
  10226. * @param data: Item data
  10227. * @return Number of removed combo(s)
  10228. */
  10229. static int32 pc_removecombo(map_session_data *sd, item_data *data ) {
  10230. if (sd->combos.empty())
  10231. return 0; // Nothing to do here, player has no combos
  10232. int32 retval = 0;
  10233. for (const auto &item_combo : data->combos) {
  10234. std::shared_ptr<s_combos> del_combo = nullptr;
  10235. // Check if this combo exists on this player
  10236. for (const auto &player_combo : sd->combos) {
  10237. if (player_combo->id == item_combo->id) {
  10238. del_combo = player_combo;
  10239. break;
  10240. }
  10241. }
  10242. // No match, skip this combo
  10243. if (del_combo == nullptr)
  10244. continue;
  10245. util::vector_erase_if_exists(sd->combos, del_combo);
  10246. retval++;
  10247. // Check if combo requirements still fit
  10248. if (pc_checkcombo(sd, data))
  10249. continue;
  10250. // It's empty, clear all the memory
  10251. if (sd->combos.empty()) {
  10252. sd->combos.clear();
  10253. return retval; // Return at this point as there are no more combos to check
  10254. }
  10255. }
  10256. return retval;
  10257. }
  10258. /**
  10259. * Load combo data(s) of player
  10260. * @param sd: Player data
  10261. * @return ret numbers of succeed combo
  10262. */
  10263. int32 pc_load_combo(map_session_data *sd) {
  10264. int32 ret = 0;
  10265. for (int16 i = 0; i < EQI_MAX; i++) {
  10266. item_data *id;
  10267. int16 idx = sd->equip_index[i];
  10268. if (idx < 0 || !(id = sd->inventory_data[idx]))
  10269. continue;
  10270. if (!id->combos.empty())
  10271. ret += pc_checkcombo(sd, id);
  10272. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  10273. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  10274. if (!sd->inventory.u.items_inventory[idx].card[j])
  10275. continue;
  10276. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[idx].card[j]);
  10277. if (data != nullptr) {
  10278. if (!data->combos.empty())
  10279. ret += pc_checkcombo(sd, data.get());
  10280. }
  10281. }
  10282. }
  10283. }
  10284. return ret;
  10285. }
  10286. /*==========================================
  10287. * Equip item on player sd at req_pos from inventory index n
  10288. * return: false - fail; true - success
  10289. *------------------------------------------*/
  10290. bool pc_equipitem(map_session_data *sd,int16 n,int32 req_pos,bool equipswitch)
  10291. {
  10292. int32 i, pos, flag = 0, iflag;
  10293. struct item_data *id;
  10294. int16* equip_index;
  10295. nullpo_retr(false,sd);
  10296. if( n < 0 || n >= MAX_INVENTORY ) {
  10297. if( equipswitch ){
  10298. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10299. }else{
  10300. // Does not deserve an answer... [Lemongrass]
  10301. //clif_equipitemack( sd, ITEM_EQUIP_ACK_FAIL, n );
  10302. }
  10303. return false;
  10304. }
  10305. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  10306. if( equipswitch ){
  10307. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10308. }else{
  10309. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n );
  10310. }
  10311. return false;
  10312. }
  10313. if (!(id = sd->inventory_data[n]))
  10314. return false;
  10315. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  10316. if(battle_config.battle_log && !equipswitch)
  10317. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  10318. uint8 res = pc_isequip( sd, n );
  10319. if( res != ITEM_EQUIP_ACK_OK ){
  10320. if( equipswitch ){
  10321. clif_equipswitch_add( sd, n, req_pos, res );
  10322. }else{
  10323. clif_equipitemack( *sd, res, n ); // fail
  10324. }
  10325. return false;
  10326. }
  10327. if( equipswitch && id->type == IT_AMMO ){
  10328. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10329. return false;
  10330. }
  10331. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0) {
  10332. if( equipswitch ){
  10333. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10334. }else{
  10335. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); // fail
  10336. }
  10337. return false;
  10338. }
  10339. if( !sd->sc.empty() && (sd->sc.cant.equip || (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) {
  10340. if( equipswitch ){
  10341. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10342. }else{
  10343. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); //Fail
  10344. }
  10345. return false;
  10346. }
  10347. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  10348. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  10349. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  10350. clif_notify_bindOnEquip( *sd, n );
  10351. }
  10352. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  10353. pos = req_pos&EQP_ACC;
  10354. if (pos == EQP_ACC) //User specified both slots.
  10355. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  10356. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  10357. if (!sd->inventory.u.items_inventory[n].card[i])
  10358. continue;
  10359. std::shared_ptr<item_data> card_data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10360. if (card_data) {
  10361. int32 card_pos = card_data->equip;
  10362. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  10363. pos = card_pos; // Use the card's equip position
  10364. break;
  10365. }
  10366. }
  10367. }
  10368. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  10369. pos = (req_pos&EQP_ARMS);
  10370. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  10371. #ifdef RENEWAL
  10372. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  10373. #else
  10374. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  10375. #endif
  10376. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  10377. pos = req_pos&EQP_SHADOW_ACC;
  10378. if (pos == EQP_SHADOW_ACC)
  10379. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  10380. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  10381. pos = (req_pos&EQP_SHADOW_ARMS);
  10382. if( pos == EQP_SHADOW_ARMS )
  10383. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  10384. }
  10385. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  10386. //Update skill-block range database when weapon range changes. [Skotlex]
  10387. i = equip_index[EQI_HAND_R];
  10388. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  10389. flag = 1;
  10390. else
  10391. flag = id->range != sd->inventory_data[i]->range;
  10392. }
  10393. if( equipswitch ){
  10394. for( i = 0; i < EQI_MAX; i++ ){
  10395. if( pos&equip_bitmask[i] ){
  10396. // If there was already an item assigned to this slot
  10397. if( sd->equip_switch_index[i] >= 0 ){
  10398. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  10399. }
  10400. // Assign the new index to it
  10401. sd->equip_switch_index[i] = n;
  10402. }
  10403. }
  10404. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  10405. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  10406. return true;
  10407. }else{
  10408. for(i=0;i<EQI_MAX;i++) {
  10409. if(pos & equip_bitmask[i]) {
  10410. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  10411. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  10412. sd->equip_index[i] = n;
  10413. }
  10414. }
  10415. pc_equipswitch_remove(sd, n);
  10416. if(pos==EQP_AMMO) {
  10417. clif_arrowequip( *sd );
  10418. clif_arrow_fail( *sd, ARROWFAIL_SUCCESS );
  10419. }
  10420. else
  10421. clif_equipitemack( *sd, ITEM_EQUIP_ACK_OK, n, pos );
  10422. sd->inventory.u.items_inventory[n].equip = pos;
  10423. }
  10424. if(pos & EQP_HAND_R) {
  10425. if(id)
  10426. sd->weapontype1 = id->subtype;
  10427. else
  10428. sd->weapontype1 = W_FIST;
  10429. pc_calcweapontype(sd);
  10430. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10431. }
  10432. if(pos & EQP_HAND_L) {
  10433. if(id) {
  10434. if(id->type == IT_WEAPON) {
  10435. sd->status.shield = W_FIST;
  10436. sd->weapontype2 = id->subtype;
  10437. }
  10438. else
  10439. if(id->type == IT_ARMOR) {
  10440. sd->status.shield = id->look;
  10441. sd->weapontype2 = W_FIST;
  10442. }
  10443. }
  10444. else
  10445. sd->status.shield = sd->weapontype2 = W_FIST;
  10446. pc_calcweapontype(sd);
  10447. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10448. }
  10449. if(pos & EQP_SHOES)
  10450. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10451. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  10452. int16 idx = sd->equip_index[EQI_AMMO];
  10453. if (idx >= 0) {
  10454. switch (sd->inventory_data[idx]->subtype) {
  10455. case AMMO_ARROW:
  10456. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  10457. pc_unequipitem(sd, idx, 2 | 4);
  10458. break;
  10459. case AMMO_BULLET:
  10460. case AMMO_SHELL:
  10461. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10462. #ifdef RENEWAL
  10463. && id->subtype != W_GRENADE
  10464. #endif
  10465. )
  10466. pc_unequipitem(sd, idx, 2 | 4);
  10467. break;
  10468. #ifndef RENEWAL
  10469. case AMMO_GRENADE:
  10470. if (id->subtype != W_GRENADE)
  10471. pc_unequipitem(sd, idx, 2 | 4);
  10472. break;
  10473. #endif
  10474. }
  10475. }
  10476. }
  10477. pc_set_costume_view(sd);
  10478. pc_checkallowskill(sd); //Check if status changes should be halted.
  10479. iflag = sd->npc_item_flag;
  10480. // Check for combos (MUST be before status_calc_pc)
  10481. if (!id->combos.empty())
  10482. pc_checkcombo(sd, id);
  10483. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10484. ; // No cards
  10485. else {
  10486. for (i = 0; i < MAX_SLOTS; i++) {
  10487. if (!sd->inventory.u.items_inventory[n].card[i])
  10488. continue;
  10489. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10490. if (data != nullptr) {
  10491. if (!data->combos.empty())
  10492. pc_checkcombo(sd, data.get());
  10493. }
  10494. }
  10495. }
  10496. status_calc_pc(sd,SCO_NONE);
  10497. if (flag) //Update skill data
  10498. clif_skillinfoblock(sd);
  10499. //OnEquip script [Skotlex]
  10500. if (id) {
  10501. current_equip_item_index = n;
  10502. current_equip_card_id = 0;
  10503. //only run the script if item isn't restricted
  10504. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  10505. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10506. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10507. ; //No cards
  10508. else {
  10509. for( i = 0; i < MAX_SLOTS; i++ ) {
  10510. if (!sd->inventory.u.items_inventory[n].card[i])
  10511. continue;
  10512. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10513. if ( data != nullptr ) {
  10514. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data.get(), sd->bl.m))) {
  10515. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10516. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10517. }
  10518. }
  10519. current_equip_card_id = 0;
  10520. }
  10521. }
  10522. current_equip_item_index = -1;
  10523. }
  10524. sd->npc_item_flag = iflag;
  10525. return true;
  10526. }
  10527. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int32 position ){
  10528. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10529. while( it != bonus.end() ){
  10530. std::shared_ptr<s_autobonus> b = *it;
  10531. if( ( b->pos & position ) != b->pos ){
  10532. it++;
  10533. continue;
  10534. }
  10535. it = bonus.erase( it );
  10536. }
  10537. }
  10538. /**
  10539. * Recalculate player status on unequip
  10540. * @param sd: Player data
  10541. * @param n: Item inventory index
  10542. * @param flag: Whether to recalculate a player's status or not
  10543. * @return True on success or false on failure
  10544. */
  10545. static void pc_unequipitem_sub(map_session_data *sd, int32 n, int32 flag) {
  10546. int32 i, iflag;
  10547. bool status_calc = false;
  10548. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10549. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10550. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10551. sd->inventory.u.items_inventory[n].equip = 0;
  10552. if (!(flag & 4))
  10553. pc_checkallowskill(sd);
  10554. iflag = sd->npc_item_flag;
  10555. // Check for combos (MUST be before status_calc_pc)
  10556. if (sd->inventory_data[n]) {
  10557. if (!sd->inventory_data[n]->combos.empty()) {
  10558. if (pc_removecombo(sd, sd->inventory_data[n]))
  10559. status_calc = true;
  10560. }
  10561. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10562. ; // No cards
  10563. else {
  10564. for (i = 0; i < MAX_SLOTS; i++) {
  10565. if (!sd->inventory.u.items_inventory[n].card[i])
  10566. continue;
  10567. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10568. if (data != nullptr) {
  10569. if (!data->combos.empty()) {
  10570. if (pc_removecombo(sd, data.get()))
  10571. status_calc = true;
  10572. }
  10573. }
  10574. }
  10575. }
  10576. }
  10577. if (flag & 1 || status_calc) {
  10578. pc_checkallowskill(sd);
  10579. status_calc_pc(sd, SCO_FORCE);
  10580. }
  10581. if (sd->sc.getSCE(SC_SIGNUMCRUCIS) && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10582. status_change_end(&sd->bl, SC_SIGNUMCRUCIS);
  10583. //OnUnEquip script [Skotlex]
  10584. if (sd->inventory_data[n]) {
  10585. current_equip_item_index = n;
  10586. current_equip_card_id = 0;
  10587. if (sd->inventory_data[n]->unequip_script)
  10588. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10589. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10590. ; //No cards
  10591. else {
  10592. for (i = 0; i < MAX_SLOTS; i++) {
  10593. if (!sd->inventory.u.items_inventory[n].card[i])
  10594. continue;
  10595. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10596. if (data != nullptr) {
  10597. if (data->unequip_script) {
  10598. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10599. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10600. }
  10601. }
  10602. current_equip_card_id = 0;
  10603. }
  10604. }
  10605. current_equip_item_index = -1;
  10606. }
  10607. sd->npc_item_flag = iflag;
  10608. }
  10609. /**
  10610. * Called when attempting to unequip an item from a player
  10611. * @param sd: Player data
  10612. * @param n: Item inventory index
  10613. * @param flag: Type of unequip
  10614. * 0 - only unequip
  10615. * 1 - calculate status after unequipping
  10616. * 2 - force unequip
  10617. * 4 - unequip by switching equipment
  10618. * @return True on success or false on failure
  10619. */
  10620. bool pc_unequipitem(map_session_data *sd, int32 n, int32 flag) {
  10621. int32 i, pos;
  10622. nullpo_retr(false,sd);
  10623. if (n < 0 || n >= MAX_INVENTORY) {
  10624. clif_unequipitemack(*sd,0,0,false);
  10625. return false;
  10626. }
  10627. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10628. clif_unequipitemack(*sd,n,0,false);
  10629. return false; //Nothing to unequip
  10630. }
  10631. // status change that makes player cannot unequip equipment
  10632. if (!(flag&2) && !sd->sc.empty() &&( sd->sc.cant.unequip ||
  10633. (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10634. {
  10635. clif_unequipitemack(*sd,n,0,false);
  10636. return false;
  10637. }
  10638. if (battle_config.battle_log)
  10639. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10640. for(i = 0; i < EQI_MAX; i++) {
  10641. if (pos & equip_bitmask[i])
  10642. sd->equip_index[i] = -1;
  10643. }
  10644. if(pos & EQP_HAND_R) {
  10645. sd->weapontype1 = W_FIST;
  10646. sd->status.weapon = sd->weapontype2;
  10647. pc_calcweapontype(sd);
  10648. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10649. if( !battle_config.dancing_weaponswitch_fix )
  10650. status_change_end(&sd->bl, SC_DANCING); // Unequipping => stop dancing.
  10651. #ifdef RENEWAL
  10652. if (battle_config.switch_remove_edp&2) {
  10653. #else
  10654. if (battle_config.switch_remove_edp&1) {
  10655. #endif
  10656. status_change_end(&sd->bl, SC_EDP);
  10657. }
  10658. }
  10659. if(pos & EQP_HAND_L) {
  10660. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10661. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10662. sd->status.shield = sd->weapontype2 = W_FIST;
  10663. pc_calcweapontype(sd);
  10664. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10665. }
  10666. if(pos & EQP_SHOES)
  10667. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10668. clif_unequipitemack(*sd,n,pos,true);
  10669. pc_set_costume_view(sd);
  10670. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10671. // On weapon change (right and left hand)
  10672. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10673. if (battle_config.ammo_unequip && !(flag & 4)) {
  10674. switch (sd->inventory_data[n]->subtype) {
  10675. case W_BOW:
  10676. case W_MUSICAL:
  10677. case W_WHIP:
  10678. case W_REVOLVER:
  10679. case W_RIFLE:
  10680. case W_GATLING:
  10681. case W_SHOTGUN:
  10682. case W_GRENADE: {
  10683. int16 idx = sd->equip_index[EQI_AMMO];
  10684. if (idx >= 0) {
  10685. sd->equip_index[EQI_AMMO] = -1;
  10686. clif_unequipitemack(*sd, idx, sd->inventory.u.items_inventory[idx].equip, true);
  10687. pc_unequipitem_sub(sd, idx, 0);
  10688. }
  10689. }
  10690. break;
  10691. }
  10692. }
  10693. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10694. }
  10695. // On armor change
  10696. if (pos & EQP_ARMOR) {
  10697. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10698. }
  10699. // On equipment change
  10700. #ifndef RENEWAL
  10701. if (!(flag&4))
  10702. status_change_end(&sd->bl, SC_CONCENTRATION);
  10703. #endif
  10704. // On ammo change
  10705. 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_))
  10706. status_change_end(&sd->bl, SC_P_ALTER);
  10707. pc_unequipitem_sub(sd, n, flag);
  10708. return true;
  10709. }
  10710. int32 pc_equipswitch( map_session_data* sd, int32 index ){
  10711. // Get the target equip mask
  10712. int32 position = sd->inventory.u.items_inventory[index].equipSwitch;
  10713. // Get the currently equipped item
  10714. int16 equippedItem = pc_checkequip( sd, position, true );
  10715. // No item equipped at the target
  10716. if( equippedItem == -1 ){
  10717. // Remove it from the equip switch
  10718. pc_equipswitch_remove( sd, index );
  10719. pc_equipitem( sd, index, position );
  10720. return position;
  10721. }else{
  10722. std::map<int32, int32> unequipped;
  10723. int32 unequipped_position = 0;
  10724. // Unequip all items that interfere
  10725. for( int32 i = 0; i < EQI_MAX; i++ ){
  10726. int32 unequip_index = sd->equip_index[i];
  10727. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10728. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10729. // Store the unequipped index and position mask for later
  10730. unequipped[unequip_index] = unequip_item->equip;
  10731. // Keep the position for later
  10732. unequipped_position |= unequip_item->equip;
  10733. // Unequip the item
  10734. pc_unequipitem( sd, unequip_index, 0 );
  10735. }
  10736. }
  10737. int32 all_position = position | unequipped_position;
  10738. // Equip everything that is hit by the mask
  10739. for( int32 i = 0; i < EQI_MAX; i++ ){
  10740. int32 exchange_index = sd->equip_switch_index[i];
  10741. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10742. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10743. // Store the target position
  10744. int32 exchange_position = exchange_item->equipSwitch;
  10745. // Remove the item from equip switch
  10746. pc_equipswitch_remove( sd, exchange_index );
  10747. // Equip the item at the destinated position
  10748. pc_equipitem( sd, exchange_index, exchange_position );
  10749. }
  10750. }
  10751. // Place all unequipped items into the equip switch window
  10752. for( std::pair<int32, int32> pair : unequipped ){
  10753. int32 unequipped_index = pair.first;
  10754. int32 unequipped_position = pair.second;
  10755. // Rebuild the index cache
  10756. for( int32 i = 0; i < EQI_MAX; i++ ){
  10757. if( unequipped_position & equip_bitmask[i] ){
  10758. sd->equip_switch_index[i] = unequipped_index;
  10759. }
  10760. }
  10761. // Set the correct position mask
  10762. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10763. // Notify the client
  10764. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10765. }
  10766. return all_position;
  10767. }
  10768. }
  10769. void pc_equipswitch_remove( map_session_data* sd, int32 index ){
  10770. struct item* item = &sd->inventory.u.items_inventory[index];
  10771. if( !item->equipSwitch ){
  10772. return;
  10773. }
  10774. for( int32 i = 0; i < EQI_MAX; i++ ){
  10775. // If a match is found
  10776. if( sd->equip_switch_index[i] == index ){
  10777. // Remove it from the slot
  10778. sd->equip_switch_index[i] = -1;
  10779. }
  10780. }
  10781. // Send out one packet for all slots using the current item's mask
  10782. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10783. item->equipSwitch = 0;
  10784. }
  10785. /*==========================================
  10786. * Checking if player (sd) has an invalid item
  10787. * and is unequiped on map load (item_noequip)
  10788. *------------------------------------------*/
  10789. void pc_checkitem(map_session_data *sd) {
  10790. int32 i, calc_flag = 0;
  10791. struct item* it;
  10792. nullpo_retv(sd);
  10793. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10794. return;
  10795. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10796. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10797. it = &sd->inventory.u.items_inventory[i];
  10798. if( it->nameid == 0 )
  10799. continue;
  10800. if( !it->equip )
  10801. continue;
  10802. if( it->equip&~pc_equippoint(sd,i) ) {
  10803. pc_unequipitem(sd, i, 2);
  10804. calc_flag = 1;
  10805. continue;
  10806. }
  10807. 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) ) {
  10808. pc_unequipitem(sd, i, 2);
  10809. calc_flag = 1;
  10810. continue;
  10811. }
  10812. }
  10813. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10814. it = &sd->inventory.u.items_inventory[i];
  10815. if( it->nameid == 0 )
  10816. continue;
  10817. if( !it->equipSwitch )
  10818. continue;
  10819. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10820. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10821. for( int32 j = 0; j < EQI_MAX; j++ ){
  10822. if( sd->equip_switch_index[j] == i ){
  10823. sd->equip_switch_index[j] = -1;
  10824. }
  10825. }
  10826. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10827. continue;
  10828. }
  10829. }
  10830. if( calc_flag && sd->state.active ) {
  10831. pc_checkallowskill(sd);
  10832. status_calc_pc(sd,SCO_NONE);
  10833. }
  10834. }
  10835. /*==========================================
  10836. * Checks for unavailable items and removes them.
  10837. * @param sd: Player data
  10838. * @param type Forced check:
  10839. * 1 - Inventory
  10840. * 2 - Cart
  10841. * 4 - Storage
  10842. *------------------------------------------*/
  10843. void pc_check_available_item(map_session_data *sd, uint8 type)
  10844. {
  10845. int32 i;
  10846. t_itemid nameid;
  10847. char output[256];
  10848. nullpo_retv(sd);
  10849. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10850. for(i = 0; i < MAX_INVENTORY; i++) {
  10851. nameid = sd->inventory.u.items_inventory[i].nameid;
  10852. if (!nameid)
  10853. continue;
  10854. if (!itemdb_available(nameid)) {
  10855. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10856. clif_displaymessage(sd->fd, output);
  10857. 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);
  10858. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10859. continue;
  10860. }
  10861. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10862. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10863. }
  10864. }
  10865. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10866. for(i = 0; i < MAX_CART; i++) {
  10867. nameid = sd->cart.u.items_cart[i].nameid;
  10868. if (!nameid)
  10869. continue;
  10870. if (!itemdb_available(nameid)) {
  10871. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10872. clif_displaymessage(sd->fd, output);
  10873. 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);
  10874. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10875. continue;
  10876. }
  10877. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10878. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10879. }
  10880. }
  10881. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10882. for(i = 0; i < sd->storage.max_amount; i++) {
  10883. nameid = sd->storage.u.items_storage[i].nameid;
  10884. if (!nameid)
  10885. continue;
  10886. if (!itemdb_available(nameid)) {
  10887. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10888. clif_displaymessage(sd->fd, output);
  10889. 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);
  10890. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10891. continue;
  10892. }
  10893. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10894. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10895. }
  10896. }
  10897. }
  10898. /*==========================================
  10899. * Update PVP rank for sd1 in cmp to sd2
  10900. *------------------------------------------*/
  10901. static int32 pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10902. {
  10903. map_session_data *sd1,*sd2;
  10904. sd1=(map_session_data *)bl;
  10905. sd2=va_arg(ap,map_session_data *);
  10906. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10907. {// cannot register pvp rank for hidden GMs
  10908. return 0;
  10909. }
  10910. if( sd1->pvp_point > sd2->pvp_point )
  10911. sd2->pvp_rank++;
  10912. return 0;
  10913. }
  10914. /*==========================================
  10915. * Calculate new rank beetween all present players (map_foreachinallarea)
  10916. * and display result
  10917. *------------------------------------------*/
  10918. int32 pc_calc_pvprank(map_session_data *sd)
  10919. {
  10920. int32 old = sd->pvp_rank;
  10921. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10922. sd->pvp_rank=1;
  10923. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10924. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10925. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10926. return sd->pvp_rank;
  10927. }
  10928. /*==========================================
  10929. * Calculate next sd ranking calculation from config
  10930. *------------------------------------------*/
  10931. TIMER_FUNC(pc_calc_pvprank_timer){
  10932. map_session_data *sd;
  10933. sd=map_id2sd(id);
  10934. if(sd==nullptr)
  10935. return 0;
  10936. sd->pvp_timer = INVALID_TIMER;
  10937. if( pc_isinvisible(sd) )
  10938. {// do not calculate the pvp rank for a hidden GM
  10939. return 0;
  10940. }
  10941. if( pc_calc_pvprank(sd) > 0 )
  10942. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10943. return 0;
  10944. }
  10945. /*==========================================
  10946. * Checking if sd is married
  10947. * Return:
  10948. * partner_id = yes
  10949. * 0 = no
  10950. *------------------------------------------*/
  10951. int32 pc_ismarried(map_session_data *sd)
  10952. {
  10953. if(sd == nullptr)
  10954. return -1;
  10955. if(sd->status.partner_id > 0)
  10956. return sd->status.partner_id;
  10957. else
  10958. return 0;
  10959. }
  10960. /*==========================================
  10961. * Marry player sd to player dstsd
  10962. * Return:
  10963. * false = fail
  10964. * true = success
  10965. *------------------------------------------*/
  10966. bool pc_marriage(map_session_data *sd,map_session_data *dstsd)
  10967. {
  10968. if(sd == nullptr || dstsd == nullptr ||
  10969. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10970. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10971. return false;
  10972. sd->status.partner_id = dstsd->status.char_id;
  10973. dstsd->status.partner_id = sd->status.char_id;
  10974. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10975. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10976. return true;
  10977. }
  10978. /*==========================================
  10979. * Divorce sd from its partner
  10980. * Return:
  10981. * false = fail
  10982. * true = success
  10983. *------------------------------------------*/
  10984. bool pc_divorce(map_session_data *sd)
  10985. {
  10986. map_session_data *p_sd;
  10987. int32 i;
  10988. if( sd == nullptr || !pc_ismarried(sd) )
  10989. return false;
  10990. if( !sd->status.partner_id )
  10991. return false; // Char is not married
  10992. if( (p_sd = map_charid2sd(sd->status.partner_id)) == nullptr )
  10993. { // Lets char server do the divorce
  10994. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10995. return false; // No char server connected
  10996. return true;
  10997. }
  10998. // Both players online, lets do the divorce manually
  10999. sd->status.partner_id = 0;
  11000. p_sd->status.partner_id = 0;
  11001. for( i = 0; i < MAX_INVENTORY; i++ )
  11002. {
  11003. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  11004. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  11005. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  11006. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  11007. }
  11008. clif_divorced( *sd, p_sd->status.name );
  11009. clif_divorced( *p_sd, sd->status.name );
  11010. return true;
  11011. }
  11012. /**
  11013. * Get the partner map_session_data of a player
  11014. * @param sd : the husband|wife session
  11015. * @return partner session or nullptr
  11016. */
  11017. map_session_data *pc_get_partner(map_session_data *sd){
  11018. if (!sd || !pc_ismarried(sd))
  11019. return nullptr;
  11020. return map_charid2sd(sd->status.partner_id);
  11021. }
  11022. /**
  11023. * Get the father map_session_data of a player
  11024. * @param sd : the baby session
  11025. * @return father session or nullptr
  11026. */
  11027. map_session_data *pc_get_father (map_session_data *sd){
  11028. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  11029. return nullptr;
  11030. return map_charid2sd(sd->status.father);
  11031. }
  11032. /**
  11033. * Get the mother map_session_data of a player
  11034. * @param sd : the baby session
  11035. * @return mother session or nullptr
  11036. */
  11037. map_session_data *pc_get_mother (map_session_data *sd){
  11038. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  11039. return nullptr;
  11040. return map_charid2sd(sd->status.mother);
  11041. }
  11042. /*==========================================
  11043. * Get sd children charid. (Need to be married)
  11044. *------------------------------------------*/
  11045. map_session_data *pc_get_child (map_session_data *sd)
  11046. {
  11047. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  11048. // charid2sd returns nullptr if not found
  11049. return nullptr;
  11050. return map_charid2sd(sd->status.child);
  11051. }
  11052. /*==========================================
  11053. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  11054. *------------------------------------------*/
  11055. void pc_bleeding (map_session_data *sd, t_tick diff_tick)
  11056. {
  11057. int32 hp = 0, sp = 0;
  11058. if( pc_isdead(sd) )
  11059. return;
  11060. if (sd->hp_loss.value) {
  11061. sd->hp_loss.tick += diff_tick;
  11062. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  11063. hp += sd->hp_loss.value;
  11064. sd->hp_loss.tick -= sd->hp_loss.rate;
  11065. }
  11066. if(hp >= sd->battle_status.hp)
  11067. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  11068. }
  11069. if (sd->sp_loss.value) {
  11070. sd->sp_loss.tick += diff_tick;
  11071. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  11072. sp += sd->sp_loss.value;
  11073. sd->sp_loss.tick -= sd->sp_loss.rate;
  11074. }
  11075. }
  11076. if (hp > 0 || sp > 0)
  11077. status_zap(&sd->bl, hp, sp);
  11078. }
  11079. //Character regen. Flag is used to know which types of regen can take place.
  11080. //&1: HP regen
  11081. //&2: SP regen
  11082. void pc_regen (map_session_data *sd, t_tick diff_tick)
  11083. {
  11084. int32 hp = 0, sp = 0;
  11085. if (sd->hp_regen.value) {
  11086. sd->hp_regen.tick += diff_tick;
  11087. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  11088. hp += sd->hp_regen.value;
  11089. sd->hp_regen.tick -= sd->hp_regen.rate;
  11090. }
  11091. }
  11092. if (sd->sp_regen.value) {
  11093. sd->sp_regen.tick += diff_tick;
  11094. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  11095. sp += sd->sp_regen.value;
  11096. sd->sp_regen.tick -= sd->sp_regen.rate;
  11097. }
  11098. }
  11099. if (sd->percent_hp_regen.value) {
  11100. sd->percent_hp_regen.tick += diff_tick;
  11101. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  11102. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  11103. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  11104. }
  11105. }
  11106. if (sd->percent_sp_regen.value) {
  11107. sd->percent_sp_regen.tick += diff_tick;
  11108. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  11109. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  11110. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  11111. }
  11112. }
  11113. if (hp > 0 || sp > 0)
  11114. status_heal(&sd->bl, hp, sp, 0);
  11115. }
  11116. /*==========================================
  11117. * Memo player sd savepoint. (map,x,y)
  11118. *------------------------------------------*/
  11119. void pc_setsavepoint(map_session_data *sd, int16 mapindex,int32 x,int32 y)
  11120. {
  11121. nullpo_retv(sd);
  11122. safestrncpy( sd->status.save_point.map, mapindex_id2name( mapindex ), sizeof( sd->status.save_point.map ) );
  11123. sd->status.save_point.x = x;
  11124. sd->status.save_point.y = y;
  11125. }
  11126. /*==========================================
  11127. * Save 1 player data at autosave interval
  11128. *------------------------------------------*/
  11129. static TIMER_FUNC(pc_autosave){
  11130. int32 interval;
  11131. struct s_mapiterator* iter;
  11132. map_session_data* sd;
  11133. static int32 last_save_id = 0, save_flag = 0;
  11134. if(save_flag == 2) //Someone was saved on last call, normal cycle
  11135. save_flag = 0;
  11136. else
  11137. save_flag = 1; //Noone was saved, so save first found char.
  11138. iter = mapit_getallusers();
  11139. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11140. {
  11141. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  11142. continue;
  11143. if(sd->bl.id == last_save_id && save_flag != 1) {
  11144. save_flag = 1;
  11145. continue;
  11146. }
  11147. if(save_flag != 1) //Not our turn to save yet.
  11148. continue;
  11149. //Save char.
  11150. last_save_id = sd->bl.id;
  11151. save_flag = 2;
  11152. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  11153. break;
  11154. }
  11155. mapit_free(iter);
  11156. interval = autosave_interval/(map_usercount()+1);
  11157. if(interval < minsave_interval)
  11158. interval = minsave_interval;
  11159. add_timer(gettick()+interval,pc_autosave,0,0);
  11160. return 0;
  11161. }
  11162. static int32 pc_daynight_timer_sub(map_session_data *sd,va_list ap)
  11163. {
  11164. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  11165. { //Night/day state does not match.
  11166. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  11167. sd->state.night = night_flag;
  11168. return 1;
  11169. }
  11170. return 0;
  11171. }
  11172. /*================================================
  11173. * timer to do the day [Yor]
  11174. * data: 0 = called by timer, 1 = gmcommand/script
  11175. *------------------------------------------------*/
  11176. TIMER_FUNC(map_day_timer){
  11177. char tmp_soutput[1024];
  11178. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  11179. return 0;
  11180. if (!night_flag)
  11181. return 0; //Already day.
  11182. night_flag = 0; // 0=day, 1=night [Yor]
  11183. map_foreachpc(pc_daynight_timer_sub);
  11184. strcpy(tmp_soutput, (data == 0) ? msg_txt(nullptr,502) : msg_txt(nullptr,60)); // The day has arrived!
  11185. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11186. return 0;
  11187. }
  11188. /*================================================
  11189. * timer to do the night [Yor]
  11190. * data: 0 = called by timer, 1 = gmcommand/script
  11191. *------------------------------------------------*/
  11192. TIMER_FUNC(map_night_timer){
  11193. char tmp_soutput[1024];
  11194. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  11195. return 0;
  11196. if (night_flag)
  11197. return 0; //Already nigth.
  11198. night_flag = 1; // 0=day, 1=night [Yor]
  11199. map_foreachpc(pc_daynight_timer_sub);
  11200. strcpy(tmp_soutput, (data == 0) ? msg_txt(nullptr,503) : msg_txt(nullptr,59)); // The night has fallen...
  11201. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11202. return 0;
  11203. }
  11204. /**
  11205. * Attempt to stand up a player
  11206. * @param sd
  11207. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  11208. * @return True if success, Fals if failed
  11209. */
  11210. bool pc_setstand(map_session_data *sd, bool force){
  11211. nullpo_ret(sd);
  11212. // Cannot stand yet
  11213. // TODO: Move to SCS_NOSTAND [Cydh]
  11214. if (!force && (sd->sc.getSCE(SC_SITDOWN_FORCE) || sd->sc.getSCE(SC_BANANA_BOMB_SITDOWN)))
  11215. return false;
  11216. status_change_end(&sd->bl, SC_TENSIONRELAX);
  11217. clif_status_load(&sd->bl,EFST_SIT,0);
  11218. clif_standing(sd->bl); //Inform area PC is standing
  11219. //Reset sitting tick.
  11220. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  11221. if( pc_isdead( sd ) ){
  11222. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11223. clif_party_dead( *sd );
  11224. }else{
  11225. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11226. }
  11227. return true;
  11228. }
  11229. /**
  11230. * Calculate Overheat value
  11231. * @param sd: Player data
  11232. * @param heat: Amount of Heat to adjust
  11233. **/
  11234. void pc_overheat(map_session_data &sd, int16 heat) {
  11235. status_change_entry *sce = sd.sc.getSCE(SC_OVERHEAT_LIMITPOINT);
  11236. if (sce) {
  11237. sce->val1 += heat;
  11238. sce->val1 = cap_value(sce->val1, 0, 1000);
  11239. if (heat < 0 && sce->val1 == 0) { // Cooling device used.
  11240. status_change_end(&sd.bl, SC_OVERHEAT_LIMITPOINT);
  11241. status_change_end(&sd.bl, SC_OVERHEAT);
  11242. }
  11243. } else if (heat > 0)
  11244. sc_start(&sd.bl, &sd.bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  11245. }
  11246. /**
  11247. * Check if player is autolooting given itemID.
  11248. */
  11249. bool pc_isautolooting(map_session_data *sd, t_itemid nameid)
  11250. {
  11251. uint8 i = 0;
  11252. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  11253. return true;
  11254. if (!sd->state.autolooting)
  11255. return false;
  11256. if (sd->state.autolooting)
  11257. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  11258. return (i != AUTOLOOTITEM_SIZE);
  11259. }
  11260. /**
  11261. * Checks if player can use @/#command
  11262. * @param sd Player map session data
  11263. * @param command Command name without @/# and params
  11264. * @param type is it atcommand or charcommand
  11265. */
  11266. bool pc_can_use_command( map_session_data *sd, const char *command, AtCommandType type ){
  11267. return sd->group->can_use_command( command, type );
  11268. }
  11269. bool pc_has_permission( map_session_data* sd, e_pc_permission permission ){
  11270. return sd->permissions.test( permission );
  11271. }
  11272. /**
  11273. * Checks if commands used by a player should be logged
  11274. * according to their group setting.
  11275. * @param sd Player map session data
  11276. */
  11277. bool pc_should_log_commands( map_session_data *sd ){
  11278. return sd->group->log_commands;
  11279. }
  11280. /**
  11281. * Spirit Charm expiration timer.
  11282. * @see TimerFunc
  11283. */
  11284. static TIMER_FUNC(pc_spiritcharm_timer){
  11285. map_session_data *sd;
  11286. int32 i;
  11287. if ((sd = (map_session_data *)map_id2sd(id)) == nullptr || sd->bl.type != BL_PC)
  11288. return 1;
  11289. if (sd->spiritcharm <= 0) {
  11290. 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);
  11291. sd->spiritcharm = 0;
  11292. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11293. return 0;
  11294. }
  11295. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  11296. if (i == sd->spiritcharm) {
  11297. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  11298. return 0;
  11299. }
  11300. sd->spiritcharm--;
  11301. if (i != sd->spiritcharm)
  11302. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int32));
  11303. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11304. if (sd->spiritcharm <= 0)
  11305. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11306. clif_spiritcharm( *sd );
  11307. return 0;
  11308. }
  11309. /**
  11310. * Adds a spirit charm.
  11311. * @param sd: Target character
  11312. * @param interval: Duration
  11313. * @param max: Maximum amount of charms to add
  11314. * @param type: Charm type (@see spirit_charm_types)
  11315. */
  11316. void pc_addspiritcharm(map_session_data *sd, int32 interval, int32 max, int32 type)
  11317. {
  11318. int32 tid, i;
  11319. nullpo_retv(sd);
  11320. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  11321. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  11322. if (max > MAX_SPIRITCHARM)
  11323. max = MAX_SPIRITCHARM;
  11324. if (sd->spiritcharm < 0)
  11325. sd->spiritcharm = 0;
  11326. if (sd->spiritcharm && sd->spiritcharm >= max) {
  11327. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  11328. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  11329. sd->spiritcharm--;
  11330. if (sd->spiritcharm != 0)
  11331. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int32));
  11332. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11333. }
  11334. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  11335. 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);
  11336. if (i != sd->spiritcharm)
  11337. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int32));
  11338. sd->spiritcharm_timer[i] = tid;
  11339. sd->spiritcharm++;
  11340. sd->spiritcharm_type = type;
  11341. clif_spiritcharm( *sd );
  11342. }
  11343. /**
  11344. * Removes one or more spirit charms.
  11345. * @param sd: The target character
  11346. * @param count: Amount of charms to remove
  11347. * @param type: Type of charm to remove
  11348. */
  11349. void pc_delspiritcharm(map_session_data *sd, int32 count, int32 type)
  11350. {
  11351. int32 i;
  11352. nullpo_retv(sd);
  11353. if (sd->spiritcharm_type != type)
  11354. return;
  11355. if (sd->spiritcharm <= 0) {
  11356. sd->spiritcharm = 0;
  11357. return;
  11358. }
  11359. if (count <= 0)
  11360. return;
  11361. if (count > sd->spiritcharm)
  11362. count = sd->spiritcharm;
  11363. sd->spiritcharm -= count;
  11364. if (count > MAX_SPIRITCHARM)
  11365. count = MAX_SPIRITCHARM;
  11366. for (i = 0; i < count; i++) {
  11367. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  11368. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  11369. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11370. }
  11371. }
  11372. for (i = count; i < MAX_SPIRITCHARM; i++) {
  11373. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  11374. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11375. }
  11376. if (sd->spiritcharm <= 0)
  11377. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11378. clif_spiritcharm( *sd );
  11379. }
  11380. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11381. /**
  11382. * Renewal EXP/Item Drop rate modifier based on level penalty
  11383. * @param level_diff: Monster and Player level difference
  11384. * @param mob_class: Monster class
  11385. * @param mode: Monster mode
  11386. * @param type: 1 - EXP, 2 - Item Drop
  11387. * @return Penalty rate
  11388. */
  11389. uint16 pc_level_penalty_mod( map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  11390. // No player was attached, we don't use any modifier (100 = rates are not touched)
  11391. if( sd == nullptr ){
  11392. return 100;
  11393. }
  11394. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  11395. return 100;
  11396. }
  11397. int32 monster_level;
  11398. if( md != nullptr ){
  11399. monster_level = md->level;
  11400. mob = md->db;
  11401. }else if( mob != nullptr ){
  11402. monster_level = mob->lv;
  11403. }else{
  11404. return 100;
  11405. }
  11406. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  11407. return 100;
  11408. }
  11409. int32 level_difference = monster_level - sd->status.base_level;
  11410. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  11411. if( penalty != nullptr ){
  11412. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  11413. }else{
  11414. return 100;
  11415. }
  11416. }
  11417. #endif
  11418. int32 pc_split_str(char *str,char **val,int32 num)
  11419. {
  11420. int32 i;
  11421. for (i=0; i<num && str; i++){
  11422. val[i] = str;
  11423. str = strchr(str,',');
  11424. if (str && i<num-1) //Do not remove a trailing comma.
  11425. *str++=0;
  11426. }
  11427. return i;
  11428. }
  11429. int32 pc_split_atoui(char* str, uint32* val, char sep, int32 max)
  11430. {
  11431. static int32 warning=0;
  11432. int32 i,j;
  11433. for (i=0; i<max; i++) {
  11434. double f;
  11435. if (!str) break;
  11436. f = atof(str);
  11437. if (f < 0)
  11438. val[i] = 0;
  11439. else if (f > UINT_MAX) {
  11440. val[i] = UINT_MAX;
  11441. if (!warning) {
  11442. warning = 1;
  11443. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  11444. }
  11445. } else
  11446. val[i] = (uint32)f;
  11447. str = strchr(str,sep);
  11448. if (str)
  11449. *str++=0;
  11450. }
  11451. //Zero up the remaining.
  11452. for(j=i; j < max; j++)
  11453. val[j] = 0;
  11454. return i;
  11455. }
  11456. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int32 class_, uint16 skill_id) {
  11457. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  11458. if (tree != nullptr)
  11459. return util::umap_find(tree->skills, skill_id);
  11460. return nullptr;
  11461. }
  11462. const std::string SkillTreeDatabase::getDefaultLocation() {
  11463. return std::string(db_path) + "/skill_tree.yml";
  11464. }
  11465. /**
  11466. * Reads and parses an entry from the skill_tree.
  11467. * @param node: YAML node containing the entry.
  11468. * @return count of successfully parsed rows
  11469. */
  11470. uint64 SkillTreeDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11471. std::string job_name;
  11472. if (!this->asString(node, "Job", job_name))
  11473. return 0;
  11474. int64 constant;
  11475. std::string job_name_constant = "JOB_" + job_name;
  11476. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11477. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  11478. return 0;
  11479. }
  11480. uint16 job_id = static_cast<uint16>(constant);
  11481. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  11482. bool exists = tree != nullptr;
  11483. if (!exists)
  11484. tree = std::make_shared<s_skill_tree>();
  11485. if (this->nodeExists(node, "Inherit")) {
  11486. const ryml::NodeRef& InheritNode = node["Inherit"];
  11487. for (const auto &Inheritit : InheritNode) {
  11488. std::string inheritname;
  11489. c4::from_chars(Inheritit.key(), &inheritname);
  11490. std::string inheritname_constant = "JOB_" + inheritname;
  11491. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11492. this->invalidWarning(InheritNode[Inheritit.key()], "Invalid job %s.\n", inheritname.c_str());
  11493. return 0;
  11494. }
  11495. bool active;
  11496. if (!this->asBool(InheritNode, inheritname, active))
  11497. return 0;
  11498. uint16 inherit_job = static_cast<uint16>(constant);
  11499. if (!active) {
  11500. if (exists)
  11501. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  11502. }
  11503. else {
  11504. if (!util::vector_exists(tree->inherit_job, inherit_job))
  11505. tree->inherit_job.push_back(inherit_job);
  11506. }
  11507. }
  11508. }
  11509. if (this->nodeExists(node, "Tree")) {
  11510. for (const auto &it : node["Tree"]) {
  11511. std::string skill_name;
  11512. if (!this->asString(it, "Name", skill_name))
  11513. return 0;
  11514. uint16 skill_id = skill_name2id(skill_name.c_str());
  11515. if (skill_id == 0) {
  11516. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  11517. return 0;
  11518. }
  11519. if (!skill_get_index(skill_id)) {
  11520. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  11521. return 0;
  11522. }
  11523. std::shared_ptr<s_skill_tree_entry> entry;
  11524. bool skill_exists = tree->skills.count(skill_id) > 0;
  11525. if (skill_exists)
  11526. entry = tree->skills[skill_id];
  11527. else
  11528. entry = std::make_shared<s_skill_tree_entry>();
  11529. entry->skill_id = skill_id;
  11530. uint16 max_lv;
  11531. if (!this->asUInt16(it, "MaxLevel", max_lv))
  11532. return 0;
  11533. if (max_lv > MAX_SKILL_LEVEL) {
  11534. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11535. return 0;
  11536. }
  11537. uint16 skill_lv_max = skill_get_max(skill_id);
  11538. if (max_lv > skill_lv_max) {
  11539. 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);
  11540. max_lv = skill_lv_max;
  11541. }
  11542. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11543. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11544. // 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());
  11545. // continue;
  11546. // }
  11547. entry->max_lv = max_lv;
  11548. if (this->nodeExists(it, "BaseLevel")) {
  11549. uint32 baselv;
  11550. if (!this->asUInt32(it, "BaseLevel", baselv))
  11551. return 0;
  11552. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11553. if (baselv > baselv_max) {
  11554. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11555. skill_id, baselv, job_name.c_str(), baselv_max);
  11556. baselv = baselv_max;
  11557. }
  11558. entry->baselv = baselv;
  11559. } else {
  11560. if (!skill_exists)
  11561. entry->baselv = 0;
  11562. }
  11563. if (this->nodeExists(it, "JobLevel")) {
  11564. uint32 joblv;
  11565. if (!this->asUInt32(it, "JobLevel", joblv))
  11566. return 0;
  11567. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11568. if (joblv > joblv_max) {
  11569. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11570. skill_id, joblv, job_name.c_str(), joblv_max);
  11571. joblv = joblv_max;
  11572. }
  11573. entry->joblv = joblv;
  11574. } else {
  11575. if (!skill_exists)
  11576. entry->joblv = 0;
  11577. }
  11578. if (this->nodeExists(it, "Requires")) {
  11579. for (const auto &Requiresit : it["Requires"]) {
  11580. if (!this->nodesExist(Requiresit, { "Name" }))
  11581. return 0;
  11582. std::string skill_name_req;
  11583. if (!this->asString(Requiresit, "Name", skill_name_req))
  11584. return 0;
  11585. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11586. if (skill_id_req == 0) {
  11587. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11588. return 0;
  11589. }
  11590. uint16 lv_req;
  11591. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11592. return 0;
  11593. if (lv_req > MAX_SKILL_LEVEL) {
  11594. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11595. return 0;
  11596. }
  11597. uint16 lv_req_max = skill_get_max(skill_id_req);
  11598. if (lv_req > lv_req_max) {
  11599. 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);
  11600. lv_req = lv_req_max;
  11601. }
  11602. if (lv_req == 0) {
  11603. if (entry->need.erase(skill_id_req) == 0)
  11604. 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());
  11605. continue;
  11606. }
  11607. entry->need[skill_id_req] = lv_req;
  11608. }
  11609. }
  11610. if (this->nodeExists(it, "Exclude")) {
  11611. bool exclude;
  11612. if (!this->asBool(it, "Exclude", exclude))
  11613. return 0;
  11614. entry->exclude_inherit = exclude;
  11615. } else {
  11616. if (!skill_exists)
  11617. entry->exclude_inherit = false;
  11618. }
  11619. if (!skill_exists)
  11620. tree->skills.insert({ skill_id, entry });
  11621. }
  11622. }
  11623. if (!exists)
  11624. this->put(job_id, tree);
  11625. return true;
  11626. }
  11627. void SkillTreeDatabase::loadingFinished() {
  11628. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11629. for (auto &data : *this) {
  11630. if (data.second->inherit_job.empty())
  11631. continue;
  11632. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11633. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11634. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11635. for (const auto &inherit_job : data.second->inherit_job) {
  11636. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11637. if (tree == nullptr || tree->skills.empty())
  11638. continue;
  11639. for (const auto &it : tree->skills) {
  11640. if (it.second->exclude_inherit)
  11641. continue;
  11642. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11643. continue;
  11644. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11645. skill_tree->skills[it.first] = it.second;
  11646. else
  11647. skill_tree->skills.insert({ it.first, it.second });
  11648. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11649. if (skill->baselv > baselv_max) {
  11650. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11651. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11652. skill->baselv = baselv_max;
  11653. }
  11654. if (skill->joblv > joblv_max) {
  11655. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11656. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11657. skill->joblv = joblv_max;
  11658. }
  11659. }
  11660. }
  11661. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11662. job_tree.insert({ data.first, skill_tree });
  11663. }
  11664. if (!job_tree.empty()) {
  11665. for (auto &data : *this) {
  11666. if (job_tree.count(data.first) == 0)
  11667. continue;
  11668. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11669. }
  11670. }
  11671. // remove skills with max_lv = 0
  11672. for (const auto &job : *this) {
  11673. if (job.second->skills.empty())
  11674. continue;
  11675. auto it = job.second->skills.begin();
  11676. while( it != job.second->skills.end() ){
  11677. if( it->second->max_lv == 0 ){
  11678. it = job.second->skills.erase( it );
  11679. }else{
  11680. it++;
  11681. }
  11682. }
  11683. }
  11684. TypesafeYamlDatabase::loadingFinished();
  11685. }
  11686. /**
  11687. * Calculates base HP of player.
  11688. * @param level: Base level of player
  11689. * @param job:
  11690. * @return base_hp
  11691. */
  11692. uint32 JobDatabase::calc_basehp( const uint16 level, const std::shared_ptr<s_job_info>& job ){
  11693. uint64 mapid = pc_jobid2mapid( job->job_id );
  11694. double base_hp = 35.;
  11695. base_hp += floor( level * ( job->hp_increase / 100. ) );
  11696. for( uint16 i = 2; i <= level; i++ ){
  11697. // Don't have round()
  11698. base_hp += floor( ( ( job->hp_factor / 100. ) * i ) + 0.5 );
  11699. }
  11700. #ifndef RENEWAL
  11701. if( (mapid & MAPID_BASEMASK) == MAPID_NINJA ){
  11702. if( level >= 10 ){
  11703. base_hp += 90.;
  11704. }
  11705. }else if( (mapid & MAPID_BASEMASK) == MAPID_GUNSLINGER ){
  11706. if( level >= 10 ){
  11707. base_hp += 90.;
  11708. }
  11709. }
  11710. #endif
  11711. if( (mapid&MAPID_BASEMASK) == MAPID_SUMMONER ){
  11712. base_hp += floor( ( base_hp / 2 ) + 0.5 );
  11713. }else if( (mapid&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE ){
  11714. // Supernovice lvl99 HP bonus.
  11715. if( level >= 99 ){
  11716. base_hp += 2000.;
  11717. }
  11718. // Supernovice lvl150 HP bonus.
  11719. if( level >= 150 ){
  11720. base_hp += 2000.;
  11721. }
  11722. }
  11723. return static_cast<uint32>( base_hp );
  11724. }
  11725. /**
  11726. * Calculates base SP of player.
  11727. * @param level: Base level of player
  11728. * @param job:
  11729. * @return base_sp
  11730. */
  11731. uint32 JobDatabase::calc_basesp( const uint16 level, const std::shared_ptr<s_job_info>& job ){
  11732. uint64 mapid = pc_jobid2mapid( job->job_id );
  11733. double base_sp = 10;
  11734. base_sp += floor( level * ( job->sp_increase / 100. ) );
  11735. for( uint16 i = 2; i <= level; i++ ){
  11736. // Don't have round()
  11737. base_sp += floor( ( ( job->sp_factor / 100. ) * i ) + 0.5 );
  11738. }
  11739. if( (mapid & MAPID_BASEMASK) == MAPID_NINJA ){
  11740. if( level >= 10 ){
  11741. base_sp -= 22.;
  11742. }else{
  11743. base_sp = 11. + 3. * level;
  11744. }
  11745. }else if( (mapid & MAPID_BASEMASK) == MAPID_GUNSLINGER ){
  11746. if( level >= 10 ){
  11747. base_sp -= 18.;
  11748. }else{
  11749. base_sp = 9. + 3. * level;
  11750. }
  11751. }else if( (mapid&MAPID_BASEMASK) == MAPID_SUMMONER ){
  11752. base_sp += floor( ( base_sp / 2 ) + 0.5 );
  11753. }
  11754. return static_cast<uint32>( base_sp );
  11755. }
  11756. /**
  11757. * Calculates base AP of player.
  11758. * @param level: Base level of player
  11759. * @param job_id:
  11760. * @return base_sp
  11761. */
  11762. uint32 JobDatabase::calc_baseap( const uint16 level, const std::shared_ptr<s_job_info>& job ){
  11763. uint64 mapid = pc_jobid2mapid( job->job_id );
  11764. double base_ap = 0;
  11765. if( level >= 200 ){
  11766. base_ap = 200.0;
  11767. }
  11768. base_ap += floor( level * ( job->ap_increase / 100. ) );
  11769. for( uint16 i = 2; i <= level; i++ ){
  11770. // Don't have round()
  11771. base_ap += floor( ( ( job->ap_factor / 100. ) * i ) + 0.5 );
  11772. }
  11773. return static_cast<uint32>( base_ap );
  11774. }
  11775. const std::string JobDatabase::getDefaultLocation() {
  11776. return std::string(db_path) + "/job_stats.yml";
  11777. }
  11778. /**
  11779. * Reads and parses an entry from the job_db.
  11780. * @param node: YAML node containing the entry.
  11781. * @return count of successfully parsed rows
  11782. */
  11783. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11784. if (this->nodeExists(node, "Jobs")) {
  11785. const ryml::NodeRef& jobsNode = node["Jobs"];
  11786. for (const auto &jobit : jobsNode) {
  11787. std::string job_name;
  11788. c4::from_chars(jobit.key(), &job_name);
  11789. std::string job_name_constant = "JOB_" + job_name;
  11790. int64 job_id;
  11791. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11792. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11793. return 0;
  11794. }
  11795. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11796. bool exists = job != nullptr;
  11797. if (!exists) {
  11798. job = std::make_shared<s_job_info>();
  11799. job->job_id = static_cast<uint16>( job_id );
  11800. job->job_bonus.resize(MAX_LEVEL);
  11801. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11802. job->base_hp.resize(MAX_LEVEL);
  11803. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11804. job->base_sp.resize(MAX_LEVEL);
  11805. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11806. job->base_ap.resize(MAX_LEVEL);
  11807. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11808. }
  11809. if (this->nodeExists(node, "MaxWeight")) {
  11810. uint32 weight;
  11811. if (!this->asUInt32(node, "MaxWeight", weight))
  11812. return 0;
  11813. job->max_weight_base = weight;
  11814. } else {
  11815. if (!exists)
  11816. job->max_weight_base = 20000;
  11817. }
  11818. if (this->nodeExists(node, "HpFactor")) {
  11819. uint32 hp;
  11820. if (!this->asUInt32(node, "HpFactor", hp))
  11821. return 0;
  11822. job->hp_factor = hp;
  11823. } else {
  11824. if (!exists)
  11825. job->hp_factor = 0;
  11826. }
  11827. if (this->nodeExists(node, "HpIncrease")) {
  11828. uint32 hp;
  11829. if (!this->asUInt32(node, "HpIncrease", hp))
  11830. return 0;
  11831. job->hp_increase = hp;
  11832. } else {
  11833. if (!exists)
  11834. job->hp_increase = 500;
  11835. }
  11836. if( this->nodeExists( node, "SpFactor" ) ){
  11837. uint32 sp;
  11838. if( !this->asUInt32( node, "SpFactor", sp ) ){
  11839. return 0;
  11840. }
  11841. job->sp_factor = sp;
  11842. }else{
  11843. if( !exists ){
  11844. job->sp_factor = 0;
  11845. }
  11846. }
  11847. if (this->nodeExists(node, "SpIncrease")) {
  11848. uint32 sp;
  11849. if (!this->asUInt32(node, "SpIncrease", sp))
  11850. return 0;
  11851. job->sp_increase = sp;
  11852. } else {
  11853. if (!exists)
  11854. job->sp_increase = 100;
  11855. }
  11856. if( this->nodeExists( node, "ApFactor" ) ){
  11857. uint32 ap;
  11858. if( !this->asUInt32( node, "ApFactor", ap ) ){
  11859. return 0;
  11860. }
  11861. job->ap_factor = ap;
  11862. }else{
  11863. if( !exists ){
  11864. job->ap_factor = 0;
  11865. }
  11866. }
  11867. if( this->nodeExists( node, "ApIncrease" ) ){
  11868. uint32 ap;
  11869. if( !this->asUInt32( node, "ApIncrease", ap ) ){
  11870. return 0;
  11871. }
  11872. job->ap_increase = ap;
  11873. }else{
  11874. if( !exists ){
  11875. job->ap_increase = 0;
  11876. }
  11877. }
  11878. if (this->nodeExists(node, "BaseASPD")) {
  11879. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11880. uint8 max = MAX_WEAPON_TYPE;
  11881. int32 def_aspd = AMOTION_ZERO_ASPD;
  11882. #ifdef RENEWAL
  11883. // Renewal adds an extra column for shields
  11884. max += 1;
  11885. // Renewal uses ASPD penalty which is amotion divided by the amotion interval
  11886. def_aspd /= AMOTION_INTERVAL;
  11887. #endif
  11888. if (!exists) {
  11889. job->aspd_base.resize(max);
  11890. std::fill(job->aspd_base.begin(), job->aspd_base.end(), def_aspd);
  11891. }
  11892. for (const auto &aspdit : aspdNode) {
  11893. std::string weapon;
  11894. c4::from_chars(aspdit.key(), &weapon);
  11895. std::string weapon_constant = "W_" + weapon;
  11896. int64 constant;
  11897. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11898. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11899. continue;
  11900. }
  11901. if (constant < W_FIST || constant >= max) {
  11902. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11903. continue;
  11904. }
  11905. int16 aspd;
  11906. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11907. return 0;
  11908. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11909. }
  11910. } else {
  11911. if (!exists) {
  11912. uint8 max = MAX_WEAPON_TYPE;
  11913. int32 def_aspd = AMOTION_ZERO_ASPD;
  11914. #ifdef RENEWAL
  11915. // Renewal adds an extra column for shields
  11916. max += 1;
  11917. // Renewal uses ASPD penalty which is amotion divided by the amotion interval
  11918. def_aspd /= AMOTION_INTERVAL;
  11919. #endif
  11920. job->aspd_base.resize(max);
  11921. std::fill(job->aspd_base.begin(), job->aspd_base.end(), def_aspd);
  11922. }
  11923. }
  11924. if (this->nodeExists(node, "MaxStats")) {
  11925. const ryml::NodeRef& statNode = node["MaxStats"];
  11926. for (const auto &statit : statNode) {
  11927. std::string stat;
  11928. c4::from_chars(statit.key(), &stat);
  11929. std::string stat_constant = "PARAM_" + stat;
  11930. int64 constant;
  11931. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11932. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11933. continue;
  11934. }
  11935. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11936. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11937. continue;
  11938. }
  11939. uint16 max;
  11940. if (!this->asUInt16(statNode, stat.c_str(), max))
  11941. return 0;
  11942. job->max_param[constant] = max;
  11943. }
  11944. }
  11945. if (this->nodeExists(node, "MaxBaseLevel")) {
  11946. uint16 level;
  11947. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11948. return 0;
  11949. if (level == 0 || level > MAX_LEVEL) {
  11950. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11951. level = MAX_LEVEL;
  11952. }
  11953. job->max_base_level = level;
  11954. } else {
  11955. if (!exists)
  11956. job->max_base_level = MAX_LEVEL;
  11957. }
  11958. if (this->nodeExists(node, "BaseExp")) {
  11959. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11960. uint16 level;
  11961. if (!this->asUInt16(bexpNode, "Level", level))
  11962. return 0;
  11963. if (level > job->max_base_level)
  11964. continue;
  11965. if (level == 0 || level > MAX_LEVEL) {
  11966. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11967. return 0;
  11968. }
  11969. if (this->nodeExists(bexpNode, "Exp")) {
  11970. t_exp exp;
  11971. if (!this->asUInt64(bexpNode, "Exp", exp))
  11972. return 0;
  11973. job->base_exp[level - 1] = exp;
  11974. }
  11975. }
  11976. }
  11977. if (this->nodeExists(node, "MaxJobLevel")) {
  11978. uint16 level;
  11979. if (!this->asUInt16(node, "MaxJobLevel", level))
  11980. return 0;
  11981. if (level == 0 || level > MAX_LEVEL) {
  11982. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11983. level = MAX_LEVEL;
  11984. }
  11985. job->max_job_level = level;
  11986. } else {
  11987. if (!exists)
  11988. job->max_job_level = MAX_LEVEL;
  11989. }
  11990. if (this->nodeExists(node, "JobExp")) {
  11991. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11992. uint16 level;
  11993. if (!this->asUInt16(jexpNode, "Level", level))
  11994. return 0;
  11995. if (level > job->max_job_level)
  11996. continue;
  11997. if (level == 0 || level > MAX_LEVEL) {
  11998. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11999. return 0;
  12000. }
  12001. if (this->nodeExists(jexpNode, "Exp")) {
  12002. t_exp exp;
  12003. if (!this->asUInt64(jexpNode, "Exp", exp))
  12004. return 0;
  12005. job->job_exp[level - 1] = exp;
  12006. }
  12007. }
  12008. }
  12009. if (this->nodeExists(node, "BonusStats")) {
  12010. const ryml::NodeRef& bonusNode = node["BonusStats"];
  12011. for (const ryml::NodeRef& levelNode : bonusNode) {
  12012. uint16 level;
  12013. if (!this->asUInt16(levelNode, "Level", level))
  12014. return 0;
  12015. if (level == 0 || level > MAX_LEVEL) {
  12016. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  12017. return 0;
  12018. }
  12019. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  12020. if (this->nodeExists(levelNode, parameter_names[idx])) {
  12021. int16 change;
  12022. if (!this->asInt16(levelNode, parameter_names[idx], change))
  12023. return 0;
  12024. job->job_bonus[level - 1][idx] = change;
  12025. }
  12026. }
  12027. }
  12028. }
  12029. #ifdef HP_SP_TABLES
  12030. if (this->nodeExists(node, "BaseHp")) {
  12031. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  12032. uint16 level;
  12033. if (!this->asUInt16(bhpNode, "Level", level))
  12034. return 0;
  12035. if (level > job->max_base_level)
  12036. continue;
  12037. if (level == 0 || level > MAX_LEVEL) {
  12038. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  12039. return 0;
  12040. }
  12041. if (this->nodeExists(bhpNode, "Hp")) {
  12042. uint32 points;
  12043. if (!this->asUInt32(bhpNode, "Hp", points))
  12044. return 0;
  12045. job->base_hp[level - 1] = points;
  12046. }
  12047. }
  12048. }
  12049. if (this->nodeExists(node, "BaseSp")) {
  12050. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  12051. uint16 level;
  12052. if (!this->asUInt16(bspNode, "Level", level))
  12053. return 0;
  12054. if (level > job->max_base_level)
  12055. continue;
  12056. if (level == 0 || level > MAX_LEVEL) {
  12057. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  12058. return 0;
  12059. }
  12060. if (this->nodeExists(bspNode, "Sp")) {
  12061. uint32 points;
  12062. if (!this->asUInt32(bspNode, "Sp", points))
  12063. return 0;
  12064. job->base_sp[level - 1] = points;
  12065. }
  12066. }
  12067. }
  12068. if (this->nodeExists(node, "BaseAp")) {
  12069. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  12070. uint16 level;
  12071. if (!this->asUInt16(bapNode, "Level", level))
  12072. return 0;
  12073. if (level > job->max_base_level)
  12074. continue;
  12075. if (level == 0 || level > MAX_LEVEL) {
  12076. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  12077. return 0;
  12078. }
  12079. if (this->nodeExists(bapNode, "Ap")) {
  12080. uint32 points;
  12081. if (!this->asUInt32(bapNode, "Ap", points))
  12082. return 0;
  12083. job->base_ap[level - 1] = points;
  12084. }
  12085. }
  12086. }
  12087. #endif
  12088. if (!exists)
  12089. this->put(static_cast<uint16>(job_id), job);
  12090. }
  12091. }
  12092. return 1;
  12093. }
  12094. void JobDatabase::loadingFinished() {
  12095. // Checking if all class have their data
  12096. for (auto &jobIt : *this) {
  12097. uint16 job_id = jobIt.first;
  12098. if (!pcdb_checkid(job_id))
  12099. continue;
  12100. 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)
  12101. continue; // Classes that do not need exp tables.
  12102. std::shared_ptr<s_job_info> job = jobIt.second;
  12103. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  12104. if (!maxBaseLv)
  12105. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  12106. if (!maxJobLv)
  12107. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  12108. // Check and init the empty values
  12109. if (job->base_hp.empty())
  12110. job->base_hp.resize(maxBaseLv);
  12111. for (uint16 j = 0; j < maxBaseLv; j++) {
  12112. if (job->base_hp[j] == 0)
  12113. job->base_hp[j] = this->calc_basehp( j + 1, job );
  12114. }
  12115. if (job->base_sp.empty())
  12116. job->base_sp.resize(maxBaseLv);
  12117. for (uint16 j = 0; j < maxBaseLv; j++) {
  12118. if (job->base_sp[j] == 0)
  12119. job->base_sp[j] = this->calc_basesp( j + 1, job );
  12120. }
  12121. if( job->base_ap.empty() ){
  12122. job->base_ap.resize( maxBaseLv );
  12123. }
  12124. for( uint16 j = 0; j < maxBaseLv; j++ ){
  12125. if( job->base_ap[j] == 0 ){
  12126. job->base_ap[j] = this->calc_baseap( j + 1, job );
  12127. }
  12128. }
  12129. // Resize to the maximum base level
  12130. if (job->base_hp.capacity() > maxBaseLv)
  12131. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  12132. if (job->base_sp.capacity() > maxBaseLv)
  12133. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  12134. if (job->base_ap.capacity() > maxBaseLv)
  12135. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  12136. // Resize to the maximum job level
  12137. if (job->job_bonus.capacity() > maxJobLv)
  12138. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  12139. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  12140. // Store total
  12141. int16 current = 0;
  12142. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  12143. // Add the bonus from this job level
  12144. current += job->job_bonus[job_level][parameter];
  12145. // Set the new total on this job level
  12146. job->job_bonus[job_level][parameter] = current;
  12147. }
  12148. }
  12149. uint64 class_ = pc_jobid2mapid( job_id );
  12150. // Set normal status limits
  12151. uint16 max = battle_config.max_parameter;
  12152. do{
  12153. // Always check babies first
  12154. if( class_ & JOBL_BABY ){
  12155. if( class_ & JOBL_THIRD ){
  12156. max = battle_config.max_baby_third_parameter;
  12157. break;
  12158. }else{
  12159. max = battle_config.max_baby_parameter;
  12160. break;
  12161. }
  12162. }
  12163. // Summoner
  12164. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  12165. max = battle_config.max_summoner_parameter;
  12166. break;
  12167. }
  12168. // Extended classes
  12169. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  12170. max = battle_config.max_extended_parameter;
  12171. break;
  12172. }
  12173. if( class_ & JOBL_FOURTH ){
  12174. max = battle_config.max_fourth_parameter;
  12175. break;
  12176. }
  12177. // 3rd class
  12178. if( class_ & JOBL_THIRD ){
  12179. // Transcendent
  12180. if( class_ & JOBL_UPPER ){
  12181. max = battle_config.max_third_trans_parameter;
  12182. break;
  12183. }else{
  12184. max = battle_config.max_third_parameter;
  12185. break;
  12186. }
  12187. }
  12188. // Transcendent
  12189. if( class_ & JOBL_UPPER ){
  12190. max = battle_config.max_trans_parameter;
  12191. break;
  12192. }
  12193. }while( false );
  12194. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  12195. // If it is not explicitly set in the database file
  12196. if( job->max_param[parameter] == 0 ){
  12197. job->max_param[parameter] = max;
  12198. }
  12199. }
  12200. // Set trait status limit
  12201. if( class_ & JOBL_FOURTH ){
  12202. max = battle_config.max_trait_parameter;
  12203. }else{
  12204. max = 0;
  12205. }
  12206. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  12207. // If it is not explicitly set in the database file
  12208. if( job->max_param[parameter] == 0 ){
  12209. job->max_param[parameter] = max;
  12210. }
  12211. }
  12212. }
  12213. TypesafeCachedYamlDatabase::loadingFinished();
  12214. }
  12215. /**
  12216. * Read job_noenter_map.txt
  12217. **/
  12218. static bool pc_readdb_job_noenter_map( char *str[], size_t columns, size_t current ){
  12219. int32 class_ = -1;
  12220. int64 class_tmp;
  12221. if (ISDIGIT(str[0][0])) {
  12222. class_ = atoi(str[0]);
  12223. } else {
  12224. if (!script_get_constant(str[0], &class_tmp)) {
  12225. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  12226. return false;
  12227. }
  12228. class_ = static_cast<int32>(class_tmp);
  12229. }
  12230. if (!pcdb_checkid(class_)) {
  12231. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  12232. return false;
  12233. }
  12234. std::shared_ptr<s_job_info> job = job_db.find(class_);
  12235. if (job == nullptr) {
  12236. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  12237. return false;
  12238. }
  12239. job->noenter_map.zone = atoi(str[1]);
  12240. job->noenter_map.group_lv = atoi(str[2]);
  12241. return true;
  12242. }
  12243. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  12244. return std::string(db_path) + "/statpoint.yml";
  12245. }
  12246. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  12247. uint16 level;
  12248. if (!this->asUInt16(node, "Level", level))
  12249. return 0;
  12250. if (level == 0) {
  12251. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  12252. return 0;
  12253. }
  12254. if (level > MAX_LEVEL) {
  12255. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  12256. return 0;
  12257. }
  12258. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12259. bool exists = entry != nullptr;
  12260. if( !exists ){
  12261. if( !this->nodesExist( node, { "Points" } ) ){
  12262. return 0;
  12263. }
  12264. entry = std::make_shared<s_statpoint_entry>();
  12265. entry->level = level;
  12266. }
  12267. if( this->nodeExists( node, "Points" ) ){
  12268. uint32 points;
  12269. if( !this->asUInt32( node, "Points", points ) ){
  12270. return 0;
  12271. }
  12272. entry->statpoints = points;
  12273. }
  12274. if( this->nodeExists( node, "TraitPoints" ) ){
  12275. uint32 traitpoints;
  12276. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  12277. return 0;
  12278. }
  12279. entry->traitpoints = traitpoints;
  12280. }else{
  12281. if( !exists ){
  12282. entry->traitpoints = 0;
  12283. }
  12284. }
  12285. if( !exists ){
  12286. this->put( level, entry );
  12287. }
  12288. return 1;
  12289. }
  12290. /**
  12291. * Generate the remaining parts of the db if necessary.
  12292. */
  12293. void PlayerStatPointDatabase::loadingFinished(){
  12294. const uint16 trait_start_level = 200;
  12295. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  12296. if( level_one == nullptr ){
  12297. if( battle_config.use_statpoint_table ){
  12298. ShowError( "Missing status points for Level 1\n" );
  12299. }
  12300. level_one = std::make_shared<s_statpoint_entry>();
  12301. level_one->level = 1;
  12302. level_one->statpoints = inter_config.start_status_points;
  12303. level_one->traitpoints = 0;
  12304. this->put( 1, level_one );
  12305. }else if( battle_config.use_statpoint_table ){
  12306. if( level_one->statpoints != inter_config.start_status_points ){
  12307. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, inter_config.start_status_points );
  12308. level_one->statpoints = inter_config.start_status_points;
  12309. }
  12310. }else{
  12311. level_one->statpoints = inter_config.start_status_points;
  12312. level_one->traitpoints = 0;
  12313. }
  12314. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  12315. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  12316. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12317. bool exists = entry != nullptr;
  12318. if( !exists ){
  12319. entry = std::make_shared<s_statpoint_entry>();
  12320. entry->level = level;
  12321. this->put( level, entry );
  12322. }
  12323. if( !battle_config.use_statpoint_table || !exists ){
  12324. if( battle_config.use_statpoint_table ){
  12325. ShowError("Missing status points for Level %hu\n", level);
  12326. }
  12327. if( level <= trait_start_level ){
  12328. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  12329. }else{
  12330. entry->statpoints = last_level->statpoints;
  12331. }
  12332. }
  12333. if( !battle_config.use_traitpoint_table || !exists ){
  12334. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  12335. ShowError( "Missing trait points for Level %hu\n", level );
  12336. }
  12337. if( level > trait_start_level ){
  12338. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  12339. }else{
  12340. entry->traitpoints = 0;
  12341. }
  12342. }
  12343. // Store it for next iteration
  12344. last_level = entry;
  12345. }
  12346. TypesafeCachedYamlDatabase::loadingFinished();
  12347. }
  12348. /*==========================================
  12349. * pc DB reading.
  12350. * job_stats.yml - Job values
  12351. * skill_tree.txt - skill tree for every class
  12352. * attr_fix.yml - elemental adjustment table
  12353. *------------------------------------------*/
  12354. void pc_readdb(void) {
  12355. int32 i, s = 1;
  12356. const char* dbsubpath[] = {
  12357. "",
  12358. "/" DBIMPORT,
  12359. //add other path here
  12360. };
  12361. //reset
  12362. job_db.clear(); // job_info table
  12363. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  12364. penalty_db.load();
  12365. #endif
  12366. statpoint_db.clear();
  12367. job_db.load();
  12368. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  12369. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  12370. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  12371. char* dbsubpath1 = (char*)aMalloc(n1+1);
  12372. char* dbsubpath2 = (char*)aMalloc(n2+1);
  12373. if(i==0) {
  12374. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12375. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  12376. }
  12377. else {
  12378. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12379. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  12380. }
  12381. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  12382. aFree(dbsubpath1);
  12383. aFree(dbsubpath2);
  12384. }
  12385. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  12386. skill_tree_db.reload();
  12387. statpoint_db.load();
  12388. }
  12389. // Read MOTD on startup. [Valaris]
  12390. int32 pc_read_motd(void)
  12391. {
  12392. FILE* fp;
  12393. // clear old MOTD
  12394. memset(motd_text, 0, sizeof(motd_text));
  12395. // read current MOTD
  12396. if( ( fp = fopen(motd_txt, "r") ) != nullptr )
  12397. {
  12398. uint32 entries = 0;
  12399. char buf[CHAT_SIZE_MAX];
  12400. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  12401. {
  12402. uint32 lines = 0;
  12403. size_t len;
  12404. lines++;
  12405. if( buf[0] == '/' && buf[1] == '/' )
  12406. continue;
  12407. len = strlen(buf);
  12408. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  12409. len--;
  12410. if( len ) {
  12411. char * ptr;
  12412. buf[len] = 0;
  12413. if( ( ptr = strstr(buf, " :") ) != nullptr && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  12414. 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);
  12415. }
  12416. else {// empty line
  12417. buf[0] = ' ';
  12418. buf[1] = 0;
  12419. }
  12420. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  12421. entries++;
  12422. }
  12423. fclose(fp);
  12424. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  12425. }
  12426. else
  12427. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  12428. return 0;
  12429. }
  12430. void pc_itemcd_do(map_session_data *sd, bool load) {
  12431. int32 i,cursor = 0;
  12432. struct item_cd* cd = nullptr;
  12433. if( load ) {
  12434. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  12435. // no item cooldown is associated with this character
  12436. return;
  12437. }
  12438. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12439. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  12440. sd->item_delay[cursor].tick = cd->tick[i];
  12441. sd->item_delay[cursor].nameid = cd->nameid[i];
  12442. cursor++;
  12443. }
  12444. }
  12445. idb_remove(itemcd_db,sd->status.char_id);
  12446. } else {
  12447. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  12448. // create a new skill cooldown object for map storage
  12449. CREATE( cd, struct item_cd, 1 );
  12450. idb_put( itemcd_db, sd->status.char_id, cd );
  12451. }
  12452. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12453. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  12454. cd->tick[cursor] = sd->item_delay[i].tick;
  12455. cd->nameid[cursor] = sd->item_delay[i].nameid;
  12456. cursor++;
  12457. }
  12458. }
  12459. }
  12460. return;
  12461. }
  12462. /**
  12463. * Add item delay to player's item delay data
  12464. * @param sd Player
  12465. * @param id Item data
  12466. * @param tick Current tick
  12467. * @param n Item index in inventory
  12468. * @return 0: No delay, can consume item.
  12469. * 1: Has delay, cancel consumption.
  12470. **/
  12471. uint8 pc_itemcd_add(map_session_data *sd, struct item_data *id, t_tick tick, uint16 n) {
  12472. int32 i;
  12473. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  12474. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  12475. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  12476. if( i < MAX_ITEMDELAYS ) {
  12477. if( sd->item_delay[i].nameid ) {// found
  12478. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  12479. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  12480. char e_msg[CHAT_SIZE_MAX];
  12481. if( e_tick > 99 )
  12482. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  12483. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  12484. else
  12485. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  12486. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  12487. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  12488. return 1; // Delay has not expired yet
  12489. }
  12490. } else {// not yet used item (all slots are initially empty)
  12491. sd->item_delay[i].nameid = id->nameid;
  12492. }
  12493. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  12494. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  12495. } else {// should not happen
  12496. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  12497. }
  12498. //clean up used delays so we can give room for more
  12499. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12500. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  12501. sd->item_delay[i].tick = 0;
  12502. sd->item_delay[i].nameid = 0;
  12503. }
  12504. }
  12505. return 0;
  12506. }
  12507. /**
  12508. * Check if player has delay to reuse item
  12509. * @param sd Player
  12510. * @param id Item data
  12511. * @param tick Current tick
  12512. * @param n Item index in inventory
  12513. * @return 0: No delay, can consume item.
  12514. * 1: Has delay, cancel consumption.
  12515. **/
  12516. uint8 pc_itemcd_check(map_session_data *sd, struct item_data *id, t_tick tick, uint16 n) {
  12517. status_change *sc = nullptr;
  12518. nullpo_retr(0, sd);
  12519. nullpo_retr(0, id);
  12520. // Do normal delay assignment
  12521. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  12522. return pc_itemcd_add(sd, id, tick, n);
  12523. // Send reply of delay remains
  12524. if (sc->getSCE(id->delay.sc)) {
  12525. const struct TimerData *timer = get_timer(sc->getSCE(id->delay.sc)->timer);
  12526. clif_msg_value(sd, MSI_ITEM_REUSE_LIMIT_SECOND, (int32)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  12527. return 1;
  12528. }
  12529. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  12530. return 0;
  12531. }
  12532. /**
  12533. * Status change data arrived from char-server
  12534. * @param sd: Player data
  12535. */
  12536. void pc_scdata_received(map_session_data *sd) {
  12537. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12538. clif_weight_limit( sd );
  12539. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) && pc_attendance_counter(sd) < 200 ){
  12540. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12541. }
  12542. sd->state.pc_loaded = true;
  12543. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12544. sd->state.connect_new = 1;
  12545. clif_parse_LoadEndAck(sd->fd, sd);
  12546. }
  12547. if (pc_iscarton(sd)) {
  12548. sd->cart_weight_max = 0; // Force a client refesh
  12549. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12550. }
  12551. if (sd->sc.getSCE(SC_SOULENERGY))
  12552. sd->soulball = sd->sc.getSCE(SC_SOULENERGY)->val1;
  12553. }
  12554. /**
  12555. * Check player account expiration time and rental item expirations
  12556. * @param sd: Player data
  12557. */
  12558. void pc_check_expiration(map_session_data *sd) {
  12559. #ifndef ENABLE_SC_SAVING
  12560. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12561. #endif
  12562. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12563. time_t exp_time = sd->expiration_time;
  12564. char tmpstr[1024];
  12565. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12566. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12567. pc_expire_check(sd);
  12568. }
  12569. }
  12570. TIMER_FUNC(pc_expiration_timer){
  12571. map_session_data *sd = map_id2sd(id);
  12572. if( !sd ) return 0;
  12573. sd->expiration_tid = INVALID_TIMER;
  12574. if( sd->fd )
  12575. clif_authfail_fd(sd->fd,10);
  12576. map_quit(sd);
  12577. return 0;
  12578. }
  12579. TIMER_FUNC(pc_autotrade_timer){
  12580. map_session_data *sd = map_id2sd(id);
  12581. if (!sd)
  12582. return 0;
  12583. sd->autotrade_tid = INVALID_TIMER;
  12584. if (sd->state.autotrade&2)
  12585. vending_reopen(*sd);
  12586. if (sd->state.autotrade&4)
  12587. buyingstore_reopen(sd);
  12588. if (!sd->vender_id && !sd->buyer_id) {
  12589. sd->state.autotrade = 0;
  12590. map_quit(sd);
  12591. }
  12592. return 0;
  12593. }
  12594. /* this timer exists only when a character with a expire timer > 24h is online */
  12595. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12596. TIMER_FUNC(pc_global_expiration_timer){
  12597. struct s_mapiterator* iter;
  12598. map_session_data* sd;
  12599. iter = mapit_getallusers();
  12600. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12601. if( sd->expiration_time )
  12602. pc_expire_check(sd);
  12603. mapit_free(iter);
  12604. return 0;
  12605. }
  12606. void pc_expire_check(map_session_data *sd) {
  12607. /* ongoing timer */
  12608. if( sd->expiration_tid != INVALID_TIMER )
  12609. return;
  12610. /* not within the next 24h, enable the global check */
  12611. if( sd->expiration_time > (time(nullptr) + ((60 * 60) * 24)) ) {
  12612. /* global check not running, enable */
  12613. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12614. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12615. return;
  12616. }
  12617. sd->expiration_tid = add_timer(gettick() + (uint32)(sd->expiration_time - time(nullptr)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12618. }
  12619. /**
  12620. * Deposit some money to bank
  12621. * @param sd
  12622. * @param money Amount of money to deposit
  12623. **/
  12624. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(map_session_data *sd, int32 money) {
  12625. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12626. return BDA_ERROR;
  12627. }
  12628. uint32 limit_check = money + sd->bank_vault;
  12629. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12630. return BDA_OVERFLOW;
  12631. } else if ( money > sd->status.zeny ) {
  12632. return BDA_NO_MONEY;
  12633. }
  12634. if( pc_payzeny(sd, money, LOG_TYPE_BANK) )
  12635. return BDA_NO_MONEY;
  12636. sd->bank_vault += money;
  12637. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12638. if( save_settings&CHARSAVE_BANK )
  12639. chrif_save(sd, CSAVE_NORMAL);
  12640. return BDA_SUCCESS;
  12641. }
  12642. /**
  12643. * Withdraw money from bank
  12644. * @param sd
  12645. * @param money Amount of money that will be withdrawn
  12646. **/
  12647. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(map_session_data *sd, int32 money) {
  12648. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12649. return BWA_UNKNOWN_ERROR;
  12650. }
  12651. uint32 limit_check = money + sd->status.zeny;
  12652. if( money <= 0 ) {
  12653. return BWA_UNKNOWN_ERROR;
  12654. } else if ( money > sd->bank_vault ) {
  12655. return BWA_NO_MONEY;
  12656. } else if ( limit_check > MAX_ZENY ) {
  12657. /* no official response for this scenario exists. */
  12658. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12659. return BWA_UNKNOWN_ERROR;
  12660. }
  12661. if( pc_getzeny(sd,money, LOG_TYPE_BANK) )
  12662. return BWA_NO_MONEY;
  12663. sd->bank_vault -= money;
  12664. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12665. if( save_settings&CHARSAVE_BANK )
  12666. chrif_save(sd, CSAVE_NORMAL);
  12667. return BWA_SUCCESS;
  12668. }
  12669. /**
  12670. * Clear Crimson Marker data from caster
  12671. * @param sd: Player
  12672. **/
  12673. void pc_crimson_marker_clear(map_session_data *sd) {
  12674. uint8 i;
  12675. if (!sd)
  12676. return;
  12677. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12678. struct block_list *bl = nullptr;
  12679. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12680. status_change_end(bl,SC_C_MARKER);
  12681. sd->c_marker[i] = 0;
  12682. }
  12683. }
  12684. /**
  12685. * Show version to player
  12686. * @param sd: Player
  12687. **/
  12688. void pc_show_version(map_session_data *sd) {
  12689. const char* svn = get_svn_revision();
  12690. char buf[CHAT_SIZE_MAX];
  12691. if( svn[0] != UNKNOWN_VERSION )
  12692. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12693. else {
  12694. const char* git = get_git_hash();
  12695. if( git[0] != UNKNOWN_VERSION )
  12696. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12697. else
  12698. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12699. }
  12700. clif_displaymessage(sd->fd,buf);
  12701. }
  12702. /**
  12703. * Run bonus_script on player
  12704. * @param sd
  12705. * @author [Cydh]
  12706. **/
  12707. void pc_bonus_script(map_session_data *sd) {
  12708. t_tick now = gettick();
  12709. struct linkdb_node *node = nullptr, *next = nullptr;
  12710. if (!sd || !(node = sd->bonus_script.head))
  12711. return;
  12712. while (node) {
  12713. struct s_bonus_script_entry *entry = nullptr;
  12714. next = node->next;
  12715. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12716. // Only start timer for new bonus_script
  12717. if (entry->tid == INVALID_TIMER) {
  12718. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12719. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12720. entry->tick += now;
  12721. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12722. }
  12723. if (entry->script)
  12724. run_script(entry->script, 0, sd->bl.id, 0);
  12725. else
  12726. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12727. }
  12728. node = next;
  12729. }
  12730. }
  12731. /**
  12732. * Add bonus_script to player
  12733. * @param sd Player
  12734. * @param script_str Script string
  12735. * @param dur Duration in ms
  12736. * @param icon EFST
  12737. * @param flag Flags @see enum e_bonus_script_flags
  12738. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12739. * @return New created entry pointer or nullptr if failed or nullptr if duplicate fail
  12740. * @author [Cydh]
  12741. **/
  12742. 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) {
  12743. struct script_code *script = nullptr;
  12744. struct linkdb_node *node = nullptr;
  12745. struct s_bonus_script_entry *entry = nullptr;
  12746. if (!sd)
  12747. return nullptr;
  12748. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12749. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12750. return nullptr;
  12751. }
  12752. // Duplication checks
  12753. if ((node = sd->bonus_script.head)) {
  12754. while (node) {
  12755. entry = (struct s_bonus_script_entry *)node->data;
  12756. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12757. t_tick newdur = gettick() + dur;
  12758. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12759. settick_timer(entry->tid, newdur);
  12760. script_free_code(script);
  12761. return nullptr;
  12762. }
  12763. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12764. break;
  12765. else { // No duplicate bonus
  12766. script_free_code(script);
  12767. return nullptr;
  12768. }
  12769. }
  12770. node = node->next;
  12771. }
  12772. }
  12773. CREATE(entry, struct s_bonus_script_entry, 1);
  12774. entry->script_buf = StringBuf_Malloc();
  12775. StringBuf_AppendStr(entry->script_buf, script_str);
  12776. entry->tid = INVALID_TIMER;
  12777. entry->flag = flag;
  12778. entry->icon = icon;
  12779. entry->tick = dur; // Use duration first, on run change to expire time
  12780. entry->type = type;
  12781. entry->script = script;
  12782. sd->bonus_script.count++;
  12783. return entry;
  12784. }
  12785. /**
  12786. * Remove bonus_script data from player
  12787. * @param sd: Target player
  12788. * @param list: Bonus script entry from player
  12789. * @author [Cydh]
  12790. **/
  12791. void pc_bonus_script_free_entry(map_session_data *sd, struct s_bonus_script_entry *entry) {
  12792. if (entry->tid != INVALID_TIMER)
  12793. delete_timer(entry->tid, pc_bonus_script_timer);
  12794. if (entry->script)
  12795. script_free_code(entry->script);
  12796. if (entry->script_buf)
  12797. StringBuf_Free(entry->script_buf);
  12798. if (sd) {
  12799. if (entry->icon != EFST_BLANK)
  12800. clif_status_load(&sd->bl, entry->icon, 0);
  12801. if (sd->bonus_script.count > 0)
  12802. sd->bonus_script.count--;
  12803. }
  12804. aFree(entry);
  12805. }
  12806. /**
  12807. * Do final process if no entry left
  12808. * @param sd
  12809. **/
  12810. static void inline pc_bonus_script_check_final(map_session_data *sd) {
  12811. if (sd->bonus_script.count == 0) {
  12812. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12813. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12814. linkdb_final(&sd->bonus_script.head);
  12815. }
  12816. }
  12817. /**
  12818. * Timer for bonus_script
  12819. * @param tid
  12820. * @param tick
  12821. * @param id
  12822. * @param data
  12823. * @author [Cydh]
  12824. **/
  12825. TIMER_FUNC(pc_bonus_script_timer){
  12826. map_session_data *sd;
  12827. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12828. sd = map_id2sd(id);
  12829. if (!sd) {
  12830. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12831. return 0;
  12832. }
  12833. if (tid == INVALID_TIMER)
  12834. return 0;
  12835. if (!sd->bonus_script.head || entry == nullptr) {
  12836. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12837. return 0;
  12838. }
  12839. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12840. pc_bonus_script_free_entry(sd, entry);
  12841. pc_bonus_script_check_final(sd);
  12842. status_calc_pc(sd,SCO_NONE);
  12843. return 0;
  12844. }
  12845. /**
  12846. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12847. * @param sd: Target player
  12848. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12849. * @author [Cydh]
  12850. **/
  12851. void pc_bonus_script_clear(map_session_data *sd, uint32 flag) {
  12852. struct linkdb_node *node = nullptr;
  12853. uint16 count = 0;
  12854. if (!sd || !(node = sd->bonus_script.head))
  12855. return;
  12856. while (node) {
  12857. struct linkdb_node *next = node->next;
  12858. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12859. if (entry && (
  12860. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12861. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12862. (flag&entry->flag) || // Matched flag
  12863. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12864. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12865. )
  12866. )
  12867. {
  12868. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12869. pc_bonus_script_free_entry(sd, entry);
  12870. count++;
  12871. }
  12872. node = next;
  12873. }
  12874. pc_bonus_script_check_final(sd);
  12875. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12876. status_calc_pc(sd,SCO_NONE);
  12877. }
  12878. /** [Cydh]
  12879. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12880. * @param sd: Target player
  12881. */
  12882. void pc_cell_basilica(map_session_data *sd) {
  12883. nullpo_retv(sd);
  12884. #ifdef RENEWAL
  12885. enum sc_type type = SC_BASILICA_CELL;
  12886. #else
  12887. enum sc_type type = SC_BASILICA;
  12888. #endif
  12889. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12890. if (sd->sc.getSCE(type))
  12891. status_change_end(&sd->bl, type);
  12892. }
  12893. else if (!sd->sc.getSCE(type))
  12894. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12895. }
  12896. /** [Cydh]
  12897. * Get maximum specified parameter for specified class
  12898. * @param sd: Player data
  12899. * @param param: Max parameter to check
  12900. * @return max_param
  12901. */
  12902. uint16 pc_maxparameter(map_session_data *sd, e_params param) {
  12903. nullpo_retr(0, sd);
  12904. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12905. if( job == nullptr || param == PARAM_MAX ){
  12906. return 0;
  12907. }
  12908. return job->max_param[param];
  12909. }
  12910. /**
  12911. * Get max ASPD for player based on Class
  12912. * @param sd Player
  12913. * @return ASPD
  12914. */
  12915. int16 pc_maxaspd(map_session_data *sd) {
  12916. nullpo_ret(sd);
  12917. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12918. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12919. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12920. battle_config.max_aspd ));
  12921. }
  12922. /**
  12923. * Calculates total item-group related bonuses for the given item
  12924. * @param sd Player
  12925. * @param nameid Item ID
  12926. * @return Heal rate
  12927. **/
  12928. int16 pc_get_itemgroup_bonus(map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12929. if (bonuses.empty())
  12930. return 0;
  12931. int16 bonus = 0;
  12932. for (const auto &it : bonuses) {
  12933. uint16 group_id = it.id;
  12934. if (group_id == 0)
  12935. continue;
  12936. if (itemdb_group.item_exists(group_id, nameid))
  12937. bonus += it.val;
  12938. }
  12939. return bonus;
  12940. }
  12941. /**
  12942. * Calculates total item-group related bonuses for the given item group
  12943. * @param sd Player
  12944. * @param group_id Item Group ID
  12945. * @return Heal rate
  12946. **/
  12947. int16 pc_get_itemgroup_bonus_group(map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12948. if (bonuses.empty())
  12949. return 0;
  12950. for (const auto &it : bonuses) {
  12951. if (it.id == group_id)
  12952. return it.val;
  12953. }
  12954. return 0;
  12955. }
  12956. /**
  12957. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12958. * @param eqi Item EQI of enum equip_index
  12959. * @param *equip_index Player's equip_index[]
  12960. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12961. * @return True if item in same inventory index, False if doesn't
  12962. */
  12963. bool pc_is_same_equip_index(enum equip_index eqi, int16 *equip_index, int16 index) {
  12964. if (index < 0 || index >= MAX_INVENTORY)
  12965. return true;
  12966. // Dual weapon checks
  12967. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12968. return true;
  12969. // Headgear with Mid & Low location
  12970. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12971. return true;
  12972. // Headgear with Top & Mid or Low location
  12973. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12974. return true;
  12975. // Headgear with Mid & Low location
  12976. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12977. return true;
  12978. // Headgear with Top & Mid or Low location
  12979. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12980. return true;
  12981. return false;
  12982. }
  12983. /**
  12984. * Generate Unique item ID for player
  12985. * @param sd : Player
  12986. * @return A generated Unique item ID
  12987. */
  12988. uint64 pc_generate_unique_id(map_session_data *sd) {
  12989. nullpo_ret(sd);
  12990. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12991. }
  12992. /**
  12993. * Validating skill from player after logged on
  12994. * @param sd
  12995. **/
  12996. void pc_validate_skill(map_session_data *sd) {
  12997. if (sd) {
  12998. uint16 i = 0, count = 0;
  12999. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  13000. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  13001. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  13002. for (i = 0; i < MAX_SKILL; i++) {
  13003. uint16 idx = 0;
  13004. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  13005. continue;
  13006. if ((idx = skill_get_index(tmp_skills[i].id))) {
  13007. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  13008. count++;
  13009. }
  13010. else
  13011. 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);
  13012. }
  13013. }
  13014. }
  13015. /**
  13016. * Show available NPC Quest / Event Icon Check [Kisuka]
  13017. * @param sd Player
  13018. **/
  13019. void pc_show_questinfo(map_session_data *sd) {
  13020. #if PACKETVER >= 20090218
  13021. nullpo_retv(sd);
  13022. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  13023. return;
  13024. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  13025. nullpo_retv(mapdata);
  13026. if (mapdata->qi_npc.empty())
  13027. return;
  13028. if (mapdata->qi_npc.size() != sd->qi_display.size())
  13029. return; // init was not called yet
  13030. for (int32 i = 0; i < mapdata->qi_npc.size(); i++) {
  13031. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  13032. if (!nd || nd->qi_data.empty())
  13033. continue;
  13034. bool show = false;
  13035. for (auto &qi : nd->qi_data) {
  13036. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  13037. show = true;
  13038. // Check if need to be displayed
  13039. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  13040. sd->qi_display[i].is_active = true;
  13041. sd->qi_display[i].icon = qi->icon;
  13042. sd->qi_display[i].color = qi->color;
  13043. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  13044. }
  13045. break;
  13046. }
  13047. }
  13048. if (show == false) {
  13049. // Check if need to be hide
  13050. if (sd->qi_display[i].is_active) {
  13051. sd->qi_display[i].is_active = false;
  13052. sd->qi_display[i].icon = QTYPE_NONE;
  13053. sd->qi_display[i].color = QMARK_NONE;
  13054. #if PACKETVER >= 20120410
  13055. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  13056. #else
  13057. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  13058. #endif
  13059. }
  13060. }
  13061. }
  13062. #endif
  13063. }
  13064. /**
  13065. * Reinit the questinfo for player when changing map
  13066. * @param sd Player
  13067. **/
  13068. void pc_show_questinfo_reinit(map_session_data *sd) {
  13069. #if PACKETVER >= 20090218
  13070. nullpo_retv(sd);
  13071. sd->qi_display.clear();
  13072. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  13073. return;
  13074. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  13075. nullpo_retv(mapdata);
  13076. if (mapdata->qi_npc.empty())
  13077. return;
  13078. sd->qi_display.reserve( mapdata->qi_npc.size() );
  13079. for( int32 i = 0; i < mapdata->qi_npc.size(); i++ ){
  13080. sd->qi_display.push_back( s_qi_display() );
  13081. }
  13082. #endif
  13083. }
  13084. /**
  13085. * Check if a job is allowed to enter the map
  13086. * @param jobid Job ID see enum e_job or sd->status.class_
  13087. * @param m ID -an index- for direct indexing map[] array
  13088. * @return 1 if job is allowed, 0 otherwise
  13089. **/
  13090. bool pc_job_can_entermap(enum e_job jobid, int32 m, int32 group_lv) {
  13091. // Map is other map server.
  13092. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  13093. if (m < 0)
  13094. return true;
  13095. struct map_data *mapdata = map_getmapdata(m);
  13096. if (!mapdata->cell)
  13097. return false;
  13098. if (!pcdb_checkid(jobid))
  13099. return false;
  13100. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  13101. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  13102. return true;
  13103. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  13104. (job->noenter_map.zone&2 && mapdata->getMapFlag(MF_PVP)) || // PVP
  13105. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  13106. (job->noenter_map.zone&8 && mapdata->getMapFlag(MF_BATTLEGROUND)) || // Battleground
  13107. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  13108. (job->noenter_map.zone&(mapdata->zone) && mapdata->getMapFlag(MF_RESTRICTED)) // Zone restriction
  13109. )
  13110. return false;
  13111. return true;
  13112. }
  13113. /**
  13114. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  13115. * @param sd
  13116. **/
  13117. void pc_set_costume_view(map_session_data *sd) {
  13118. int32 i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  13119. struct item_data *id = nullptr;
  13120. nullpo_retv(sd);
  13121. head_low = sd->status.head_bottom;
  13122. head_mid = sd->status.head_mid;
  13123. head_top = sd->status.head_top;
  13124. robe = sd->status.robe;
  13125. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  13126. //Added check to prevent sending the same look on multiple slots ->
  13127. //causes client to redraw item on top of itself. (suggested by Lupus)
  13128. // Normal headgear checks
  13129. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13130. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  13131. sd->status.head_bottom = id->look;
  13132. else
  13133. sd->status.head_bottom = 0;
  13134. }
  13135. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13136. if (!(id->equip&(EQP_HEAD_TOP)))
  13137. sd->status.head_mid = id->look;
  13138. else
  13139. sd->status.head_mid = 0;
  13140. }
  13141. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13142. sd->status.head_top = id->look;
  13143. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13144. sd->status.robe = id->look;
  13145. // Costumes check
  13146. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  13147. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13148. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  13149. sd->status.head_bottom = id->look;
  13150. else
  13151. sd->status.head_bottom = 0;
  13152. }
  13153. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13154. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  13155. sd->status.head_mid = id->look;
  13156. else
  13157. sd->status.head_mid = 0;
  13158. }
  13159. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13160. sd->status.head_top = id->look;
  13161. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13162. sd->status.robe = id->look;
  13163. }
  13164. if (sd->setlook_head_bottom)
  13165. sd->status.head_bottom = sd->setlook_head_bottom;
  13166. if (sd->setlook_head_mid)
  13167. sd->status.head_mid = sd->setlook_head_mid;
  13168. if (sd->setlook_head_top)
  13169. sd->status.head_top = sd->setlook_head_top;
  13170. if (sd->setlook_robe)
  13171. sd->status.robe = sd->setlook_robe;
  13172. if (head_low != sd->status.head_bottom)
  13173. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  13174. if (head_mid != sd->status.head_mid)
  13175. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  13176. if (head_top != sd->status.head_top)
  13177. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  13178. if (robe != sd->status.robe)
  13179. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  13180. }
  13181. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  13182. uint32 date = date_get(DT_YYYYMMDD);
  13183. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  13184. std::shared_ptr<s_attendance_period> period = pair.second;
  13185. if( period->start <= date && period->end >= date ){
  13186. return period;
  13187. }
  13188. }
  13189. return nullptr;
  13190. }
  13191. bool pc_attendance_enabled(){
  13192. // Check if the attendance feature is disabled
  13193. if( !battle_config.feature_attendance ){
  13194. return false;
  13195. }
  13196. // Check if there is a running attendance period
  13197. return pc_attendance_period() != nullptr;
  13198. }
  13199. static inline bool pc_attendance_rewarded_today( map_session_data* sd ){
  13200. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  13201. }
  13202. int32 pc_attendance_counter( map_session_data* sd ){
  13203. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13204. // No running attendance period
  13205. if( period == nullptr ){
  13206. return 0;
  13207. }
  13208. // Get the counter for the current period
  13209. int32 counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13210. // Check if we have a remaining counter from a previous period
  13211. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  13212. // Reset the counter to zero
  13213. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  13214. return 0;
  13215. }
  13216. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  13217. }
  13218. void pc_attendance_claim_reward( map_session_data* sd ){
  13219. // If the user's group does not have the permission
  13220. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  13221. return;
  13222. }
  13223. // Check if the attendance feature is disabled
  13224. if( !pc_attendance_enabled() ){
  13225. return;
  13226. }
  13227. // Check if the user already got his reward today
  13228. if( pc_attendance_rewarded_today( sd ) ){
  13229. return;
  13230. }
  13231. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13232. attendance_counter += 1;
  13233. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13234. if( period == nullptr ){
  13235. return;
  13236. }
  13237. if( period->rewards.size() < attendance_counter ){
  13238. return;
  13239. }
  13240. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  13241. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  13242. if( save_settings&CHARSAVE_ATTENDANCE )
  13243. chrif_save(sd, CSAVE_NORMAL);
  13244. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  13245. struct mail_message msg;
  13246. memset( &msg, 0, sizeof( struct mail_message ) );
  13247. msg.dest_id = sd->status.char_id;
  13248. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  13249. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  13250. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  13251. msg.item[0].nameid = reward->item_id;
  13252. msg.item[0].amount = reward->amount;
  13253. msg.item[0].identify = 1;
  13254. msg.status = MAIL_NEW;
  13255. msg.type = MAIL_INBOX_NORMAL;
  13256. msg.timestamp = time(nullptr);
  13257. intif_Mail_send(0, &msg);
  13258. clif_attendence_response( sd, attendance_counter );
  13259. }
  13260. /**
  13261. * Send a player to jail and determine the location to send in jail.
  13262. * @param sd: Player data
  13263. * @param duration: Duration in minutes (default INT_MAX = infinite)
  13264. */
  13265. void pc_jail(map_session_data &sd, int32 duration) {
  13266. uint16 m_index = mapindex_name2id(MAP_JAIL);
  13267. int16 x, y;
  13268. switch (rnd() % 2) { // Jail Locations
  13269. case 0: // Jail #1
  13270. x = 49;
  13271. y = 75;
  13272. break;
  13273. default: // Default Jail
  13274. x = 24;
  13275. y = 75;
  13276. break;
  13277. }
  13278. duration = i32max(0, duration); // Can't be less than 0 seconds.
  13279. // If duration > 0 then triggered via jailfor which checks every minute.
  13280. // If duration == INT_MAX then triggered via jail for infinite duration.
  13281. // If duration == 0 then triggered via unjail and end status.
  13282. if (duration > 0)
  13283. sc_start4(nullptr, &sd.bl, SC_JAILED, 100, duration, m_index, x, y, 60000);
  13284. else
  13285. status_change_end(&sd.bl, SC_JAILED);
  13286. }
  13287. /**
  13288. * Determine the punishment type when failing macro checks.
  13289. * @param sd: Player data
  13290. * @param stype: Macro detection status type (for banning)
  13291. */
  13292. static void pc_macro_punishment(map_session_data &sd, e_macro_detect_status stype) {
  13293. int32 duration = 0;
  13294. // Determine if there's a unique duration
  13295. if (battle_config.macro_detection_punishment_time > 0) {
  13296. char time[13];
  13297. safesnprintf(time, 13, "+%dnm", battle_config.macro_detection_punishment_time);
  13298. duration = static_cast<int32>(solve_time(time));
  13299. }
  13300. // Delete the timer
  13301. if (sd.macro_detect.timer != INVALID_TIMER)
  13302. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13303. int32 reporter_aid = sd.macro_detect.reporter_aid;
  13304. // Clear the macro detect data
  13305. sd.macro_detect = {};
  13306. sd.macro_detect.timer = INVALID_TIMER;
  13307. // Unblock all actions for the player
  13308. sd.state.block_action &= ~PCBLOCK_ALL;
  13309. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13310. if (battle_config.macro_detection_punishment == 0) { // Ban
  13311. clif_macro_detector_status(sd, stype);
  13312. chrif_req_login_operation(reporter_aid, sd.status.name, (duration == 0 ? CHRIF_OP_LOGIN_BLOCK : CHRIF_OP_LOGIN_BAN), duration, 0, 0);
  13313. } else { // Jail
  13314. // Send success to close the window without closing the client
  13315. clif_macro_detector_status(sd, MCD_GOOD);
  13316. pc_jail(sd, (duration == 0 ? INT_MAX : duration / 60));
  13317. }
  13318. }
  13319. /**
  13320. * Save a captcha image to memory via /macro_register.
  13321. * @param sd: Player data
  13322. * @param image_size: Captcha image size
  13323. * @param captcha_answer: Answer to captcha
  13324. */
  13325. void pc_macro_captcha_register(map_session_data &sd, uint16 image_size, const char captcha_answer[CAPTCHA_ANSWER_SIZE_MAX]) {
  13326. nullpo_retv(captcha_answer);
  13327. sd.captcha_upload.cd = nullptr;
  13328. sd.captcha_upload.upload_size = 0;
  13329. if (strlen(captcha_answer) < CAPTCHA_ANSWER_SIZE_MIN || image_size == 0 || image_size > CAPTCHA_BMP_SIZE) {
  13330. clif_captcha_upload_request(sd); // Notify client of failure.
  13331. return;
  13332. }
  13333. std::shared_ptr<s_captcha_data> cd = std::make_shared<s_captcha_data>();
  13334. sd.captcha_upload.cd = cd;
  13335. cd->image_size = image_size;
  13336. safestrncpy(cd->captcha_answer, captcha_answer, sizeof(cd->captcha_answer));
  13337. memset(cd->image_data, 0, sizeof(cd->image_data));
  13338. // Request the image data from the client.
  13339. clif_captcha_upload_request(sd);
  13340. }
  13341. /**
  13342. * Save captcha image to server.
  13343. * @param sd: Player data
  13344. * @param captcha_key: Captcha ID
  13345. * @param upload_size: Captcha size
  13346. * @param upload_data: Image data
  13347. */
  13348. void pc_macro_captcha_register_upload(map_session_data &sd, uint16 upload_size, const char *upload_data) {
  13349. nullpo_retv(upload_data);
  13350. memcpy(&sd.captcha_upload.cd->image_data[sd.captcha_upload.upload_size], upload_data, upload_size);
  13351. sd.captcha_upload.upload_size += upload_size;
  13352. // Notify that the image finished uploading.
  13353. if (sd.captcha_upload.upload_size == sd.captcha_upload.cd->image_size) {
  13354. // Tell the client that the upload was finished
  13355. clif_captcha_upload_end(sd);
  13356. // Look for a free key
  13357. uint16 index;
  13358. for (index = 0; index < UINT16_MAX; index++) {
  13359. if (!captcha_db.exists(index)) {
  13360. break;
  13361. }
  13362. }
  13363. if (index == UINT16_MAX) {
  13364. // no free key found...
  13365. sd.captcha_upload.cd = nullptr;
  13366. sd.captcha_upload.upload_size = 0;
  13367. return;
  13368. }
  13369. captcha_db.put(index, sd.captcha_upload.cd);
  13370. sd.captcha_upload.cd = nullptr;
  13371. sd.captcha_upload.upload_size = 0;
  13372. // TODO: write YAML and BMP file?
  13373. }
  13374. }
  13375. /**
  13376. * Timer attached to target player with attempts to confirm captcha.
  13377. */
  13378. TIMER_FUNC(pc_macro_detector_timeout) {
  13379. map_session_data *sd = map_id2sd(id);
  13380. nullpo_ret(sd);
  13381. // Remove the current timer
  13382. sd->macro_detect.timer = INVALID_TIMER;
  13383. // Deduct an answering attempt
  13384. sd->macro_detect.retry -= 1;
  13385. if (sd->macro_detect.retry == 0) {
  13386. // All attempts have been exhausted, punish the user
  13387. pc_macro_punishment(*sd, MCD_TIMEOUT);
  13388. } else {
  13389. // Update the client
  13390. clif_macro_detector_request_show(*sd);
  13391. // Start a new timer
  13392. sd->macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd->bl.id, 0);
  13393. }
  13394. return 0;
  13395. }
  13396. /**
  13397. * Check player's captcha answer.
  13398. * @param sd: Player data
  13399. * @param captcha_answer: Captcha answer entered by player
  13400. */
  13401. void pc_macro_detector_process_answer(map_session_data &sd, const char captcha_answer[CAPTCHA_ANSWER_SIZE_MAX]) {
  13402. nullpo_retv(captcha_answer);
  13403. const std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  13404. // Has no captcha request
  13405. if (cd == nullptr) {
  13406. return;
  13407. }
  13408. // Correct answer
  13409. if (strcmp(captcha_answer, cd->captcha_answer) == 0) {
  13410. // Delete the timer
  13411. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13412. // Clear the macro detect data
  13413. sd.macro_detect = {};
  13414. sd.macro_detect.timer = INVALID_TIMER;
  13415. // Unblock all actions for the player
  13416. sd.state.block_action &= ~PCBLOCK_ALL;
  13417. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13418. // Assign temporary macro variable to check failures
  13419. pc_setreg(&sd, add_str("@captcha_retries"), battle_config.macro_detection_retry - sd.macro_detect.retry);
  13420. // Grant bonuses via script
  13421. if( cd->bonus_script != nullptr ){
  13422. run_script(cd->bonus_script, 0, sd.bl.id, fake_nd->bl.id);
  13423. }
  13424. // Notify the client
  13425. clif_macro_detector_status(sd, MCD_GOOD);
  13426. } else {
  13427. // Deduct an answering attempt
  13428. sd.macro_detect.retry -= 1;
  13429. // All attempts have been exhausted, punish the user
  13430. if (sd.macro_detect.retry <= 0) {
  13431. pc_macro_punishment(sd, MCD_INCORRECT);
  13432. return;
  13433. }
  13434. // Incorrect response, update the client
  13435. clif_macro_detector_request_show(sd);
  13436. // Reset the timer
  13437. addtick_timer(sd.macro_detect.timer, gettick() + battle_config.macro_detection_timeout);
  13438. }
  13439. }
  13440. /**
  13441. * Determine if a player tries to log out during a captcha check.
  13442. * @param sd: Player data
  13443. */
  13444. void pc_macro_detector_disconnect(map_session_data &sd) {
  13445. // Delete the timeout timer
  13446. if (sd.macro_detect.timer != INVALID_TIMER) {
  13447. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13448. sd.macro_detect.timer = INVALID_TIMER;
  13449. }
  13450. // If the player disconnects before clearing the challenge the player is punished.
  13451. if (sd.macro_detect.retry != 0)
  13452. pc_macro_punishment(sd, MCD_TIMEOUT);
  13453. }
  13454. /**
  13455. * Save a list of players from an area select via /macro_detector.
  13456. */
  13457. int32 pc_macro_reporter_area_select_sub(block_list *bl, va_list ap) {
  13458. nullpo_retr(0, bl);
  13459. if (bl->type != BL_PC)
  13460. return 0;
  13461. std::vector<uint32> *aid_list = va_arg(ap, std::vector<uint32> *);
  13462. nullpo_ret(aid_list);
  13463. aid_list->push_back(bl->id);
  13464. return 0;
  13465. }
  13466. /**
  13467. * Area select via /macro_detector.
  13468. * @param sd: Player data
  13469. * @param x: X location
  13470. * @param y: Y location
  13471. * @param radius: Area
  13472. */
  13473. void pc_macro_reporter_area_select(map_session_data &sd, const int16 x, const int16 y, const int8 radius) {
  13474. std::vector<uint32> aid_list;
  13475. map_foreachinarea(pc_macro_reporter_area_select_sub, sd.bl.m, x - radius, y - radius, x + radius, y + radius, BL_PC, &aid_list);
  13476. clif_macro_reporter_select(sd, aid_list);
  13477. }
  13478. /**
  13479. * Send out captcha check to player.
  13480. * @param sd: Target player data
  13481. * @param reporter_account_id: Account ID of reporter
  13482. */
  13483. void pc_macro_reporter_process(map_session_data &sd, int32 reporter_account_id) {
  13484. if (captcha_db.empty())
  13485. return;
  13486. // Pick a random image from the database.
  13487. const std::shared_ptr<s_captcha_data> cd = captcha_db.random();
  13488. // Set macro detection data.
  13489. sd.macro_detect.cd = cd;
  13490. sd.macro_detect.reporter_aid = reporter_account_id;
  13491. sd.macro_detect.retry = battle_config.macro_detection_retry;
  13492. // Block all actions for the target player.
  13493. sd.state.block_action |= (PCBLOCK_ALL | PCBLOCK_IMMUNE);
  13494. // Open macro detect client side.
  13495. clif_macro_detector_request(sd);
  13496. // Start the timeout timer.
  13497. sd.macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd.bl.id, 0);
  13498. }
  13499. /**
  13500. * Parse a BMP image to memory.
  13501. * @param filepath: Image file location
  13502. * @param cd: Captcha data
  13503. */
  13504. bool pc_macro_read_captcha_db_loadbmp(const std::string &filepath, std::shared_ptr<s_captcha_data> cd) {
  13505. if (cd == nullptr)
  13506. return false;
  13507. FILE *fp = fopen(filepath.c_str(), "rb");
  13508. if (fp == nullptr) {
  13509. ShowError("%s: Failed to open file \"%s\"\n", __func__, filepath.c_str());
  13510. return false;
  13511. }
  13512. // Load the file data and verify magic
  13513. char bmp_data[CAPTCHA_BMP_SIZE];
  13514. if (fread(bmp_data, CAPTCHA_BMP_SIZE, 1, fp) != 1) {
  13515. ShowError("%s: Failed to read data from \"%s\"\n", __func__, filepath.c_str());
  13516. fclose(fp);
  13517. return false;
  13518. }
  13519. fclose(fp);
  13520. if (bmp_data[0] != 'B' || bmp_data[1] != 'M') {
  13521. ShowError("%s: Invalid BMP file header given at \"%s\"\n", __func__, filepath.c_str());
  13522. return false;
  13523. }
  13524. // Compress the data into the destination
  13525. unsigned long com_size = sizeof(cd->image_data);
  13526. encode_zip(cd->image_data, &com_size, bmp_data, CAPTCHA_BMP_SIZE);
  13527. cd->image_size = static_cast<int16>(com_size);
  13528. return true;
  13529. }
  13530. const std::string CaptchaDatabase::getDefaultLocation() {
  13531. return std::string(db_path) + "/captcha_db.yml";
  13532. }
  13533. /**
  13534. * Reads and parses an entry from the captcha_db.
  13535. * @param node: YAML node containing the entry.
  13536. * @return count of successfully parsed rows
  13537. */
  13538. uint64 CaptchaDatabase::parseBodyNode(const ryml::NodeRef &node) {
  13539. uint16 index;
  13540. if (!this->asUInt16(node, "Id", index))
  13541. return 0;
  13542. std::shared_ptr<s_captcha_data> cd = captcha_db.find(index);
  13543. bool exists = cd != nullptr;
  13544. if (!exists) {
  13545. if (!this->nodesExist(node, { "Filename", "Answer" }))
  13546. return 0;
  13547. cd = std::make_shared<s_captcha_data>();
  13548. cd->index = index;
  13549. }
  13550. if (this->nodeExists(node, "Filename")) {
  13551. std::string filename;
  13552. if (!this->asString(node, "Filename", filename))
  13553. return 0;
  13554. if (!pc_macro_read_captcha_db_loadbmp(filename, cd)) {
  13555. this->invalidWarning(node["Filename"], "Failed to parse BMP image, skipping...\n");
  13556. return 0;
  13557. }
  13558. }
  13559. if (this->nodeExists(node, "Answer")) {
  13560. std::string answer;
  13561. if (!this->asString(node, "Answer", answer))
  13562. return 0;
  13563. if (answer.length() < CAPTCHA_ANSWER_SIZE_MIN || answer.length() > CAPTCHA_ANSWER_SIZE_MAX) {
  13564. this->invalidWarning(node["Answer"], "The captcha answer must be between 4~%d characters, skipping...\n", CAPTCHA_ANSWER_SIZE_MAX);
  13565. return 0;
  13566. }
  13567. safestrncpy(cd->captcha_answer, answer.c_str(), sizeof(cd->captcha_answer));
  13568. }
  13569. if (this->nodeExists(node, "Bonus")) {
  13570. if( node["Bonus"].val_is_null() ){
  13571. if( cd->bonus_script ){
  13572. script_free_code( cd->bonus_script );
  13573. }
  13574. cd->bonus_script = nullptr;
  13575. }else{
  13576. std::string script;
  13577. if (!this->asString(node, "Bonus", script)) {
  13578. return 0;
  13579. }
  13580. if (cd->bonus_script) {
  13581. script_free_code(cd->bonus_script);
  13582. cd->bonus_script = nullptr;
  13583. }
  13584. cd->bonus_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Bonus"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13585. }
  13586. } else {
  13587. if (!exists)
  13588. 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);
  13589. }
  13590. if (!exists)
  13591. captcha_db.put(index, cd);
  13592. return 1;
  13593. }
  13594. /*==========================================
  13595. * pc Init/Terminate
  13596. *------------------------------------------*/
  13597. void do_final_pc(void) {
  13598. db_destroy(itemcd_db);
  13599. do_final_pc_groups();
  13600. ers_destroy(pc_sc_display_ers);
  13601. ers_destroy(num_reg_ers);
  13602. ers_destroy(str_reg_ers);
  13603. attendance_db.clear();
  13604. reputation_db.clear();
  13605. #ifdef MAP_GENERATOR
  13606. reputationgroup_db.clear();
  13607. #endif
  13608. penalty_db.clear();
  13609. captcha_db.clear();
  13610. }
  13611. void do_init_pc(void) {
  13612. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  13613. pc_readdb();
  13614. pc_read_motd(); // Read MOTD [Valaris]
  13615. attendance_db.load();
  13616. reputation_db.load();
  13617. #ifdef MAP_GENERATOR
  13618. reputationgroup_db.load();
  13619. #endif
  13620. captcha_db.load();
  13621. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  13622. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  13623. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  13624. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  13625. add_timer_func_list(pc_autosave, "pc_autosave");
  13626. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  13627. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  13628. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  13629. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  13630. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  13631. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  13632. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  13633. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  13634. add_timer_func_list(pc_macro_detector_timeout, "pc_macro_detector_timeout");
  13635. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  13636. // 0=day, 1=night [Yor]
  13637. night_flag = battle_config.night_at_start ? 1 : 0;
  13638. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  13639. int32 day_duration = battle_config.day_duration;
  13640. int32 night_duration = battle_config.night_duration;
  13641. // add night/day timer [Yor]
  13642. add_timer_func_list(map_day_timer, "map_day_timer");
  13643. add_timer_func_list(map_night_timer, "map_night_timer");
  13644. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  13645. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  13646. }
  13647. do_init_pc_groups();
  13648. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  13649. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13650. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13651. ers_chunk_size(pc_sc_display_ers, 150);
  13652. ers_chunk_size(num_reg_ers, 300);
  13653. ers_chunk_size(str_reg_ers, 50);
  13654. }