pc.cpp 481 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934
  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(int));
  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(int));
  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(int));
  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. sd.status.fame += count;
  821. if (sd.status.fame > MAX_FAME)
  822. sd.status.fame = MAX_FAME;
  823. switch(sd.class_&MAPID_UPPERMASK){
  824. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  825. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  826. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  827. default:
  828. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd.status.class_), sd.status.class_ );
  829. return false;
  830. }
  831. clif_update_rankingpoint(sd, ranktype, count);
  832. chrif_updatefamelist(sd, ranktype);
  833. return true;
  834. }
  835. /**
  836. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  837. * @param sd
  838. * @param job Job use enum e_mapid
  839. * @return Rank
  840. */
  841. unsigned char pc_famerank(uint32 char_id, int32 job)
  842. {
  843. uint8 i;
  844. switch(job){
  845. case MAPID_BLACKSMITH: // Blacksmith
  846. for(i = 0; i < MAX_FAME_LIST; i++){
  847. if(smith_fame_list[i].id == char_id)
  848. return i + 1;
  849. }
  850. break;
  851. case MAPID_ALCHEMIST: // Alchemist
  852. for(i = 0; i < MAX_FAME_LIST; i++){
  853. if(chemist_fame_list[i].id == char_id)
  854. return i + 1;
  855. }
  856. break;
  857. case MAPID_TAEKWON: // Taekwon
  858. for(i = 0; i < MAX_FAME_LIST; i++){
  859. if(taekwon_fame_list[i].id == char_id)
  860. return i + 1;
  861. }
  862. break;
  863. }
  864. return 0;
  865. }
  866. /**
  867. * Restart player's HP & SP value
  868. * @param sd
  869. * @param type Restart type: 1 - Normal Resurection
  870. */
  871. void pc_setrestartvalue(map_session_data *sd, char type) {
  872. struct status_data *status, *b_status;
  873. nullpo_retv(sd);
  874. b_status = &sd->base_status;
  875. status = &sd->battle_status;
  876. if (type&1) { //Normal resurrection
  877. status->hp = 1; //Otherwise status_heal may fail if dead.
  878. status_heal(&sd->bl, b_status->hp, 0, 1);
  879. if( status->sp < b_status->sp )
  880. status_set_sp(&sd->bl, b_status->sp, 1);
  881. } else { //Just for saving on the char-server (with values as if respawned)
  882. sd->status.hp = b_status->hp;
  883. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  884. sd->status.ap = (status->ap < b_status->ap)?b_status->ap:status->ap;
  885. }
  886. }
  887. /*==========================================
  888. Rental System
  889. *------------------------------------------*/
  890. /**
  891. * Ends a rental and removes the item/effect
  892. * @param tid: Tick ID
  893. * @param tick: Timer
  894. * @param id: Timer ID
  895. * @param data: Data
  896. * @return false - failure, true - success
  897. */
  898. TIMER_FUNC(pc_inventory_rental_end){
  899. map_session_data *sd = map_id2sd(id);
  900. if( sd == nullptr )
  901. return 0;
  902. if( tid != sd->rental_timer ) {
  903. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  904. return 0;
  905. }
  906. pc_inventory_rentals(sd);
  907. return 1;
  908. }
  909. /**
  910. * Removes the rental timer from the player
  911. * @param sd: Player data
  912. */
  913. void pc_inventory_rental_clear(map_session_data *sd)
  914. {
  915. if( sd->rental_timer != INVALID_TIMER ) {
  916. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  917. sd->rental_timer = INVALID_TIMER;
  918. }
  919. }
  920. /**
  921. * Check for items in the player's inventory that are rental type
  922. * @param sd: Player data
  923. */
  924. void pc_inventory_rentals(map_session_data *sd)
  925. {
  926. int32 i, c = 0;
  927. uint32 next_tick = UINT_MAX;
  928. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  929. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  930. continue; // Nothing here
  931. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  932. continue;
  933. if( sd->inventory.u.items_inventory[i].expire_time <= time(nullptr) ) {
  934. if (sd->inventory_data[i]->unequip_script)
  935. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  936. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  937. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  938. } else {
  939. uint32 expire_tick = (uint32)(sd->inventory.u.items_inventory[i].expire_time - time(nullptr));
  940. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  941. next_tick = umin(expire_tick * 1000U, next_tick);
  942. c++;
  943. }
  944. }
  945. 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
  946. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  947. else
  948. sd->rental_timer = INVALID_TIMER;
  949. }
  950. /**
  951. * Add a rental item to the player and adjusts the rental timer appropriately
  952. * @param sd: Player data
  953. * @param seconds: Rental time
  954. */
  955. void pc_inventory_rental_add(map_session_data *sd, uint32 seconds)
  956. {
  957. t_tick tick = seconds * 1000;
  958. if( sd == nullptr )
  959. return;
  960. if( sd->rental_timer != INVALID_TIMER ) {
  961. const struct TimerData * td;
  962. td = get_timer(sd->rental_timer);
  963. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  964. pc_inventory_rental_clear(sd);
  965. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  966. }
  967. } else
  968. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  969. }
  970. /**
  971. * Check if the player can sell the current item
  972. * @param sd: map_session_data of the player
  973. * @param item: struct of the checking item
  974. * @param shoptype: NPC's sub type see enum npc_subtype
  975. * @return bool 'true' is sellable, 'false' otherwise
  976. */
  977. bool pc_can_sell_item(map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  978. if (sd == nullptr || item == nullptr)
  979. return false;
  980. if (!pc_can_give_items(sd))
  981. return false;
  982. if (item->equip > 0 || item->amount < 0)
  983. return false;
  984. if (battle_config.hide_fav_sell && item->favorite != 0)
  985. return false; //Cannot sell favs (optional config)
  986. if (!battle_config.rental_transaction && item->expire_time)
  987. return false; // Cannot Sell Rental Items
  988. if( item->equipSwitch ){
  989. return false;
  990. }
  991. if (itemdb_ishatched_egg(item))
  992. return false;
  993. switch (shoptype) {
  994. case NPCTYPE_SHOP:
  995. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  996. item->bound != BOUND_GUILD ||
  997. (sd->guild && sd->status.char_id == sd->guild->guild.member[0].char_id) ||
  998. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  999. ))
  1000. return true;
  1001. break;
  1002. case NPCTYPE_ITEMSHOP:
  1003. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  1004. item->bound != BOUND_GUILD ||
  1005. (sd->guild && sd->status.char_id == sd->guild->guild.member[0].char_id) ||
  1006. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1007. ))
  1008. return true;
  1009. else if (!item->bound) {
  1010. struct item_data *itd = itemdb_search(item->nameid);
  1011. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  1012. return true;
  1013. }
  1014. break;
  1015. }
  1016. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  1017. return false;
  1018. if (item->bound && !pc_can_give_bounded_items(sd))
  1019. return false; // Don't allow sale of bound items
  1020. return true;
  1021. }
  1022. /**
  1023. * Determines if player can give / drop / trade / vend items
  1024. */
  1025. bool pc_can_give_items(map_session_data *sd)
  1026. {
  1027. return (pc_has_permission(sd, PC_PERM_TRADE) || pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL));
  1028. }
  1029. /**
  1030. * Determines if player can give / drop / trade / vend bounded items
  1031. */
  1032. bool pc_can_give_bounded_items(map_session_data *sd)
  1033. {
  1034. return (pc_has_permission(sd, PC_PERM_TRADE_BOUNDED) || pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL));
  1035. }
  1036. /**
  1037. * Determine if an item in a player's inventory is tradeable based on several merits.
  1038. * Checks for item_trade, bound, and rental restrictions.
  1039. * @param sd: Player data
  1040. * @param index: Item inventory index
  1041. * @return True if the item can be traded or false otherwise
  1042. */
  1043. bool pc_can_trade_item(map_session_data *sd, int32 index) {
  1044. if (sd && index >= 0) {
  1045. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  1046. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  1047. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  1048. );
  1049. }
  1050. return false;
  1051. }
  1052. /*==========================================
  1053. * Prepares character for saving.
  1054. * @param sd
  1055. *------------------------------------------*/
  1056. void pc_makesavestatus(map_session_data *sd) {
  1057. nullpo_retv(sd);
  1058. if(!battle_config.save_clothcolor)
  1059. sd->status.clothes_color = 0;
  1060. if(!battle_config.save_body_style)
  1061. sd->status.body = 0;
  1062. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  1063. //status change load/saving. [Skotlex]
  1064. #ifdef NEW_CARTS
  1065. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1066. #else
  1067. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1068. #endif
  1069. // Mark last point as not instance related by default
  1070. sd->status.last_point_instanceid = 0;
  1071. if (sd->sc.getSCE(SC_JAILED)) { //When Jailed, do not move last point.
  1072. if(pc_isdead(sd)){
  1073. pc_setrestartvalue(sd, 0);
  1074. } else {
  1075. sd->status.hp = sd->battle_status.hp;
  1076. sd->status.sp = sd->battle_status.sp;
  1077. sd->status.ap = sd->battle_status.ap;
  1078. }
  1079. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1080. sd->status.last_point.x = sd->bl.x;
  1081. sd->status.last_point.y = sd->bl.y;
  1082. return;
  1083. }
  1084. if( pc_isdead( sd ) ){
  1085. pc_setrestartvalue( sd, 0 );
  1086. // Return to save point
  1087. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1088. sd->status.last_point.x = sd->status.save_point.x;
  1089. sd->status.last_point.y = sd->status.save_point.y;
  1090. }else{
  1091. sd->status.hp = sd->battle_status.hp;
  1092. sd->status.sp = sd->battle_status.sp;
  1093. sd->status.ap = sd->battle_status.ap;
  1094. struct map_data* mapdata = map_getmapdata( sd->bl.m );
  1095. // If saving is not allowed on the map, we return the player to the designated point
  1096. if( mapdata->getMapFlag(MF_NOSAVE) ){
  1097. // The map has a specific return point
  1098. if( mapdata->save.map ){
  1099. safestrncpy( sd->status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd->status.last_point.map ) );
  1100. sd->status.last_point.x = mapdata->save.x;
  1101. sd->status.last_point.y = mapdata->save.y;
  1102. // Return the user to his save point
  1103. }else{
  1104. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1105. sd->status.last_point.x = sd->status.save_point.x;
  1106. sd->status.last_point.y = sd->status.save_point.y;
  1107. }
  1108. // If the user is on a instance map, special handling is needed
  1109. }else if( mapdata->instance_id ){
  1110. if( battle_config.instance_allow_reconnect ){
  1111. // Store the original mapname
  1112. struct map_data* mapdata_source = map_getmapdata( mapdata->instance_src_map );
  1113. mapindex_getmapname( mapindex_id2name( mapdata_source->index ), sd->status.last_point.map );
  1114. sd->status.last_point.x = sd->bl.x;
  1115. sd->status.last_point.y = sd->bl.y;
  1116. sd->status.last_point_instanceid = mapdata->instance_id;
  1117. }else{
  1118. // Return the user to his save point
  1119. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1120. sd->status.last_point.x = sd->status.save_point.x;
  1121. sd->status.last_point.y = sd->status.save_point.y;
  1122. }
  1123. }else{
  1124. // Save normally
  1125. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1126. sd->status.last_point.x = sd->bl.x;
  1127. sd->status.last_point.y = sd->bl.y;
  1128. }
  1129. }
  1130. }
  1131. /*==========================================
  1132. * Off init ? Connection?
  1133. *------------------------------------------*/
  1134. void pc_setnewpc(map_session_data *sd, uint32 account_id, uint32 char_id, int32 login_id1, t_tick client_tick, int32 sex, int32 fd) {
  1135. nullpo_retv(sd);
  1136. sd->bl.id = account_id;
  1137. sd->status.account_id = account_id;
  1138. sd->status.char_id = char_id;
  1139. sd->status.sex = sex;
  1140. sd->login_id1 = login_id1;
  1141. sd->login_id2 = 0; // at this point, we can not know the value :(
  1142. sd->client_tick = client_tick;
  1143. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  1144. sd->bl.type = BL_PC;
  1145. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  1146. sd->canlog_tick = gettick();
  1147. //Required to prevent homunculus copuing a base speed of 0.
  1148. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  1149. }
  1150. /**
  1151. * Get equip point for an equip
  1152. * @param sd
  1153. * @param id
  1154. */
  1155. int32 pc_equippoint_sub(map_session_data *sd,struct item_data* id){
  1156. int32 ep = 0;
  1157. nullpo_ret(sd);
  1158. nullpo_ret(id);
  1159. if (!itemdb_isequip2(id))
  1160. return 0; //Not equippable by players.
  1161. ep = id->equip;
  1162. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  1163. 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]
  1164. if (ep == EQP_WEAPON)
  1165. return EQP_ARMS;
  1166. if (ep == EQP_SHADOW_WEAPON)
  1167. return EQP_SHADOW_ARMS;
  1168. }
  1169. }
  1170. return ep;
  1171. }
  1172. /**
  1173. * Get equip point for an equip
  1174. * @param sd
  1175. * @param n Equip index in inventory
  1176. */
  1177. int32 pc_equippoint(map_session_data *sd,int32 n){
  1178. nullpo_ret(sd);
  1179. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  1180. }
  1181. /**
  1182. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  1183. * @param sd : player session
  1184. * @return 0 sucess, 1:invalid sd
  1185. */
  1186. void pc_setinventorydata(map_session_data *sd)
  1187. {
  1188. uint8 i;
  1189. nullpo_retv(sd);
  1190. for(i = 0; i < MAX_INVENTORY; i++) {
  1191. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  1192. sd->inventory_data[i] = id?itemdb_search(id):nullptr;
  1193. }
  1194. }
  1195. /**
  1196. * 'Calculates' weapon type
  1197. * @param sd : Player
  1198. */
  1199. void pc_calcweapontype(map_session_data *sd)
  1200. {
  1201. nullpo_retv(sd);
  1202. // single-hand
  1203. if(sd->weapontype2 == W_FIST) {
  1204. sd->status.weapon = sd->weapontype1;
  1205. return;
  1206. }
  1207. if(sd->weapontype1 == W_FIST) {
  1208. sd->status.weapon = sd->weapontype2;
  1209. return;
  1210. }
  1211. // dual-wield
  1212. sd->status.weapon = W_FIST;
  1213. switch (sd->weapontype1){
  1214. case W_DAGGER:
  1215. switch (sd->weapontype2) {
  1216. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  1217. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  1218. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  1219. }
  1220. break;
  1221. case W_1HSWORD:
  1222. switch (sd->weapontype2) {
  1223. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  1224. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  1225. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  1226. }
  1227. break;
  1228. case W_1HAXE:
  1229. switch (sd->weapontype2) {
  1230. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  1231. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  1232. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  1233. }
  1234. }
  1235. // unknown, default to right hand type
  1236. if (!sd->status.weapon)
  1237. sd->status.weapon = sd->weapontype1;
  1238. }
  1239. /**
  1240. * Set equip index
  1241. * @param sd : Player
  1242. */
  1243. void pc_setequipindex(map_session_data *sd)
  1244. {
  1245. uint16 i;
  1246. nullpo_retv(sd);
  1247. for (i = 0; i < EQI_MAX; i++){
  1248. sd->equip_index[i] = -1;
  1249. sd->equip_switch_index[i] = -1;
  1250. }
  1251. for (i = 0; i < MAX_INVENTORY; i++) {
  1252. if (sd->inventory.u.items_inventory[i].nameid == 0)
  1253. continue;
  1254. if (sd->inventory.u.items_inventory[i].equip) {
  1255. uint8 j;
  1256. for (j = 0; j < EQI_MAX; j++)
  1257. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  1258. sd->equip_index[j] = i;
  1259. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  1260. if (sd->inventory_data[i])
  1261. sd->weapontype1 = sd->inventory_data[i]->subtype;
  1262. else
  1263. sd->weapontype1 = W_FIST;
  1264. }
  1265. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  1266. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  1267. sd->weapontype2 = sd->inventory_data[i]->subtype;
  1268. else
  1269. sd->weapontype2 = W_FIST;
  1270. }
  1271. }
  1272. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  1273. for (uint8 j = 0; j < EQI_MAX; j++) {
  1274. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1275. sd->equip_switch_index[j] = i;
  1276. }
  1277. }
  1278. }
  1279. }
  1280. pc_calcweapontype(sd);
  1281. }
  1282. //static int32 pc_isAllowedCardOn(map_session_data *sd,int32 s,int32 eqindex,int32 flag)
  1283. //{
  1284. // int32 i;
  1285. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1286. // std::shared_ptr<item_data> data;
  1287. //
  1288. // //Crafted/made/hatched items.
  1289. // if (itemdb_isspecial(item->card[0]))
  1290. // return 1;
  1291. //
  1292. // /* scan for enchant armor gems */
  1293. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1294. // s = MAX_SLOTS - 1;
  1295. //
  1296. // ARR_FIND( 0, s, i, item->card[i] && (data = item_db.find(item->card[i])) != nullptr && data->flag.no_equip&flag );
  1297. // return( i < s ) ? 0 : 1;
  1298. //}
  1299. /**
  1300. * Check if an item is equiped by player
  1301. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1302. * @param sd : player session
  1303. * @param nameid : itemid
  1304. * @return 1:yes, 0:no
  1305. */
  1306. bool pc_isequipped(map_session_data *sd, t_itemid nameid)
  1307. {
  1308. uint8 i;
  1309. for( i = 0; i < EQI_MAX; i++ )
  1310. {
  1311. short index = sd->equip_index[i], j;
  1312. if( index < 0 )
  1313. continue;
  1314. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1315. continue;
  1316. if( !sd->inventory_data[index] )
  1317. continue;
  1318. if( sd->inventory_data[index]->nameid == nameid )
  1319. return true;
  1320. for( j = 0; j < MAX_SLOTS; j++ ){
  1321. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1322. return true;
  1323. }
  1324. }
  1325. return false;
  1326. }
  1327. /**
  1328. * Check adoption rules
  1329. * @param p1_sd: Player 1
  1330. * @param p2_sd: Player 2
  1331. * @param b_sd: Player that will be adopted
  1332. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1333. * ADOPT_ALREADY_ADOPTED - Already adopted
  1334. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1335. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1336. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1337. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1338. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1339. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1340. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1341. */
  1342. enum adopt_responses pc_try_adopt(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1343. {
  1344. if( !p1_sd || !p2_sd || !b_sd )
  1345. return ADOPT_CHARACTER_NOT_FOUND;
  1346. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1347. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1348. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1349. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1350. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1351. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1352. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1353. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1354. // Parents need to have their ring equipped
  1355. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1356. return ADOPT_EQUIP_RINGS;
  1357. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1358. return ADOPT_EQUIP_RINGS;
  1359. // Already adopted a baby
  1360. if( p1_sd->status.child || p2_sd->status.child ) {
  1361. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1362. return ADOPT_MORE_CHILDREN;
  1363. }
  1364. // Parents need at least lvl 70 to adopt
  1365. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1366. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1367. return ADOPT_LEVEL_70;
  1368. }
  1369. if( b_sd->status.partner_id ) {
  1370. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1371. return ADOPT_MARRIED;
  1372. }
  1373. 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 ) )
  1374. return ADOPT_NOT_NOVICE;
  1375. return ADOPT_ALLOWED;
  1376. }
  1377. /*==========================================
  1378. * Adoption Process
  1379. *------------------------------------------*/
  1380. bool pc_adoption(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1381. {
  1382. int32 job, joblevel;
  1383. t_exp jobexp;
  1384. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1385. return false;
  1386. // Preserve current job levels and progress
  1387. joblevel = b_sd->status.job_level;
  1388. jobexp = b_sd->status.job_exp;
  1389. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1390. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1391. { // Success, proceed to configure parents and baby skills
  1392. p1_sd->status.child = b_sd->status.char_id;
  1393. p2_sd->status.child = b_sd->status.char_id;
  1394. b_sd->status.father = p1_sd->status.char_id;
  1395. b_sd->status.mother = p2_sd->status.char_id;
  1396. // Restore progress
  1397. b_sd->status.job_level = joblevel;
  1398. clif_updatestatus(*b_sd, SP_JOBLEVEL);
  1399. b_sd->status.job_exp = jobexp;
  1400. clif_updatestatus(*b_sd, SP_JOBEXP);
  1401. // Baby Skills
  1402. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1403. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1404. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1405. // Parents Skills
  1406. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1407. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1408. chrif_save(p1_sd, CSAVE_NORMAL);
  1409. chrif_save(p2_sd, CSAVE_NORMAL);
  1410. chrif_save(b_sd, CSAVE_NORMAL);
  1411. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1412. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1413. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1414. return true;
  1415. }
  1416. return false; // Job Change Fail
  1417. }
  1418. static bool pc_job_can_use_item( map_session_data* sd, struct item_data* item ){
  1419. nullpo_retr( false, sd );
  1420. nullpo_retr( false, item );
  1421. // Calculate the required bit to check
  1422. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1423. size_t index;
  1424. // 2-1
  1425. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1426. index = 1;
  1427. // 2-2
  1428. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1429. index = 2;
  1430. // Basejob
  1431. }else{
  1432. index = 0;
  1433. }
  1434. return ( item->class_base[index] & job ) != 0;
  1435. }
  1436. /*==========================================
  1437. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1438. Returns:
  1439. false : Cannot use/equip
  1440. true : Can use/equip
  1441. * Credits:
  1442. [Inkfish] for first idea
  1443. [Haru] for third-classes extension
  1444. [Cydh] finishing :D
  1445. *------------------------------------------*/
  1446. static bool pc_isItemClass (map_session_data *sd, struct item_data* item) {
  1447. while (1) {
  1448. 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)
  1449. break;
  1450. #ifndef RENEWAL
  1451. //allow third classes to use trans. class items
  1452. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1453. break;
  1454. //third-baby classes can use same item too
  1455. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1456. break;
  1457. //don't need to decide specific rules for third-classes?
  1458. //items for third classes can be used for all third classes
  1459. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1460. break;
  1461. #else
  1462. //trans. classes (exl. third-trans.)
  1463. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1464. break;
  1465. //baby classes (exl. third-baby)
  1466. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1467. break;
  1468. //third classes (exl. third-trans. and baby-third and fourth)
  1469. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) && !(sd->class_&JOBL_FOURTH))
  1470. break;
  1471. //trans-third classes (exl. fourth)
  1472. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_FOURTH))
  1473. break;
  1474. //third-baby classes (exl. fourth)
  1475. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY && !(sd->class_&JOBL_FOURTH))
  1476. break;
  1477. //fourth classes
  1478. if (item->class_upper&ITEMJ_FOURTH && sd->class_&JOBL_FOURTH)
  1479. break;
  1480. #endif
  1481. return false;
  1482. }
  1483. return true;
  1484. }
  1485. /*=================================================
  1486. * Checks if the player can equip the item at index n in inventory.
  1487. * @param sd
  1488. * @param n Item index in inventory
  1489. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1490. *------------------------------------------------*/
  1491. uint8 pc_isequip(map_session_data *sd,int32 n)
  1492. {
  1493. struct item_data *item;
  1494. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1495. item = sd->inventory_data[n];
  1496. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1497. return ITEM_EQUIP_ACK_OK;
  1498. if(item == nullptr)
  1499. return ITEM_EQUIP_ACK_FAIL;
  1500. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_SUPERNOVICE) {
  1501. //Spirit of Super Novice equip bonuses. [Skotlex]
  1502. if (sd->status.base_level >= 90 && item->equip & EQP_HELM)
  1503. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1504. if (sd->status.base_level >= 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1505. switch (item->subtype) { //In weapons, the look determines type of weapon.
  1506. case W_DAGGER: //All level 4 - Daggers
  1507. case W_1HSWORD: //All level 4 - 1H Swords
  1508. case W_1HAXE: //All level 4 - 1H Axes
  1509. case W_MACE: //All level 4 - 1H Maces
  1510. case W_STAFF: //All level 4 - 1H Staves
  1511. case W_2HSTAFF: //All level 4 - 2H Staves
  1512. return ITEM_EQUIP_ACK_OK;
  1513. }
  1514. }
  1515. if(item->elv && sd->status.base_level < static_cast<uint32>(item->elv))
  1516. return ITEM_EQUIP_ACK_FAILLEVEL;
  1517. if(item->elvmax && sd->status.base_level > static_cast<uint32>(item->elvmax))
  1518. return ITEM_EQUIP_ACK_FAILLEVEL;
  1519. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1520. return ITEM_EQUIP_ACK_FAIL;
  1521. // Broken equip
  1522. if (sd->inventory.u.items_inventory[n].attribute == 1)
  1523. return ITEM_EQUIP_ACK_FAIL;
  1524. //fail to equip if item is restricted
  1525. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1526. return ITEM_EQUIP_ACK_FAIL;
  1527. if (item->equip&EQP_AMMO) {
  1528. switch (item->subtype) {
  1529. case AMMO_ARROW:
  1530. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1531. clif_msg(sd, MSI_FAIL_NEED_EQUIPPED_BOW);
  1532. return ITEM_EQUIP_ACK_FAIL;
  1533. }
  1534. break;
  1535. case AMMO_DAGGER:
  1536. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1537. return ITEM_EQUIP_ACK_FAIL;
  1538. break;
  1539. case AMMO_BULLET:
  1540. case AMMO_SHELL:
  1541. 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
  1542. #ifdef RENEWAL
  1543. && sd->status.weapon != W_GRENADE
  1544. #endif
  1545. ) {
  1546. clif_msg(sd, MSI_WRONG_BULLET);
  1547. return ITEM_EQUIP_ACK_FAIL;
  1548. }
  1549. break;
  1550. #ifndef RENEWAL
  1551. case AMMO_GRENADE:
  1552. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1553. clif_msg(sd, MSI_WRONG_BULLET);
  1554. return ITEM_EQUIP_ACK_FAIL;
  1555. }
  1556. break;
  1557. #endif
  1558. case AMMO_CANNONBALL:
  1559. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1560. clif_msg(sd, MSI_USESKILL_FAIL_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1561. return ITEM_EQUIP_ACK_FAIL;
  1562. }
  1563. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1564. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1565. clif_msg(sd, MSI_USESKILL_FAIL_CART); // Only available when cart is mounted.
  1566. return ITEM_EQUIP_ACK_FAIL;
  1567. }
  1568. break;
  1569. }
  1570. }
  1571. if (!sd->sc.empty()) {
  1572. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.getSCE(SC_STRIPWEAPON)) // Also works with left-hand weapons [DracoRPG]
  1573. return ITEM_EQUIP_ACK_FAIL;
  1574. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.getSCE(SC_STRIPSHIELD))
  1575. return ITEM_EQUIP_ACK_FAIL;
  1576. if(item->equip & EQP_ARMOR && sd->sc.getSCE(SC_STRIPARMOR))
  1577. return ITEM_EQUIP_ACK_FAIL;
  1578. if(item->equip & EQP_HEAD_TOP && sd->sc.getSCE(SC_STRIPHELM))
  1579. return ITEM_EQUIP_ACK_FAIL;
  1580. if(item->equip & EQP_ACC && sd->sc.getSCE(SC__STRIPACCESSORY))
  1581. return ITEM_EQUIP_ACK_FAIL;
  1582. if (item->equip & EQP_ARMS && sd->sc.getSCE(SC__WEAKNESS))
  1583. return ITEM_EQUIP_ACK_FAIL;
  1584. if (item->equip & EQP_SHADOW_GEAR && sd->sc.getSCE(SC_SHADOW_STRIP))
  1585. return ITEM_EQUIP_ACK_FAIL;
  1586. if(item->equip && (sd->sc.getSCE(SC_KYOUGAKU) || sd->sc.getSCE(SC_SUHIDE)))
  1587. return ITEM_EQUIP_ACK_FAIL;
  1588. }
  1589. //Not equipable by class. [Skotlex]
  1590. if (!pc_job_can_use_item(sd,item))
  1591. return ITEM_EQUIP_ACK_FAIL;
  1592. if (!pc_isItemClass(sd, item))
  1593. return ITEM_EQUIP_ACK_FAIL;
  1594. return ITEM_EQUIP_ACK_OK;
  1595. }
  1596. /**
  1597. * Performs some special modifications to a player's last point location,
  1598. * if the map has a nosave mapflag or if the map was an instance.
  1599. * @param sd: Player data
  1600. * @return True if the player should be returned to his savepoint or false if not
  1601. */
  1602. bool pc_lastpoint_special( map_session_data& sd ){
  1603. int16 mapid = map_mapname2mapid( sd.status.last_point.map );
  1604. // Should not happen because otherwise the char-server would have sent the player to another map-server
  1605. if( mapid < 0 ){
  1606. // Return the player to his savepoint
  1607. return true;
  1608. }
  1609. struct map_data* mapdata = map_getmapdata( mapid );
  1610. if( mapdata == nullptr ){
  1611. // Return the player to his savepoint
  1612. return true;
  1613. }
  1614. if (strcmpi(sd.status.last_point.map, MAP_JAIL) == 0) {
  1615. // Don't return jailed player to save point.
  1616. return false;
  1617. }
  1618. // Maybe since the player's logout the nosave mapflag was added to the map
  1619. if( mapdata->getMapFlag(MF_NOSAVE) ){
  1620. // The map has a specific return point
  1621. if( mapdata->save.map ){
  1622. safestrncpy( sd.status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd.status.last_point.map ) );
  1623. sd.status.last_point.x = mapdata->save.x;
  1624. sd.status.last_point.y = mapdata->save.y;
  1625. return false;
  1626. }else{
  1627. // Return the user to his save point
  1628. return true;
  1629. }
  1630. }
  1631. // Check if the last point was an instance
  1632. if( sd.status.last_point_instanceid == 0 ){
  1633. // Nothing to do
  1634. return false;
  1635. }
  1636. // Check if returning to the instance is allowed in general
  1637. if( !battle_config.instance_allow_reconnect ){
  1638. // Return the player to his savepoint
  1639. return true;
  1640. }
  1641. std::shared_ptr<s_instance_data> instance_data = util::umap_find( instances, sd.status.last_point_instanceid );
  1642. if( instance_data == nullptr ){
  1643. // Instance does not exist anymore, return the player to his savepoint
  1644. return true;
  1645. }
  1646. switch( instance_data->mode ){
  1647. case IM_NONE:
  1648. // Cannot validate
  1649. break;
  1650. case IM_CHAR:
  1651. if( sd.status.char_id != instance_data->owner_id ){
  1652. // It is a character bound instance and the player is not the owner, return the player to his savepoint
  1653. return true;
  1654. }
  1655. break;
  1656. case IM_PARTY:
  1657. if( sd.status.party_id != instance_data->owner_id ){
  1658. // It is a party bound instance and the player is not in the party, return the player to his savepoint
  1659. return true;
  1660. }
  1661. break;
  1662. case IM_GUILD:
  1663. if( sd.status.guild_id != instance_data->owner_id ){
  1664. // It is a guild bound instance and the player is not in the guild, return the player to his savepoint
  1665. return true;
  1666. }
  1667. break;
  1668. case IM_CLAN:
  1669. if( sd.status.clan_id != instance_data->owner_id ){
  1670. // It is a clan bound instance and the player is not in the clan, return the player to his savepoint
  1671. return true;
  1672. }
  1673. break;
  1674. }
  1675. int16 imapid = instance_mapid( mapid, sd.status.last_point_instanceid );
  1676. if( imapid < 0 ){
  1677. // Instance does not contain this map anymore, return the player to his savepoint
  1678. return true;
  1679. }
  1680. // Overwrite the stored "source mapname" with the real mapname
  1681. instance_generate_mapname( mapid, sd.status.last_point_instanceid, sd.status.last_point.map );
  1682. // X/Y coordinates are already correct and do not need to be modified
  1683. // Dont warp the player back to his savepoint
  1684. return false;
  1685. }
  1686. /*==========================================
  1687. * No problem with the session id
  1688. * set the status that has been sent from char server
  1689. *------------------------------------------*/
  1690. bool pc_authok(map_session_data *sd, uint32 login_id2, time_t expiration_time, int32 group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1691. {
  1692. t_tick tick = gettick();
  1693. uint32 ip = session[sd->fd]->client_addr;
  1694. sd->login_id2 = login_id2;
  1695. sd->group_id = group_id;
  1696. /* load user permissions */
  1697. pc_group_pc_load(sd);
  1698. memcpy(&sd->status, st, sizeof(*st));
  1699. if (st->sex != sd->status.sex) {
  1700. clif_authfail_fd(sd->fd, 0);
  1701. return false;
  1702. }
  1703. //Set the map-server used job id. [Skotlex]
  1704. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1705. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1706. 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);
  1707. sd->status.class_ = JOB_NOVICE;
  1708. sd->class_ = MAPID_NOVICE;
  1709. } else
  1710. sd->class_ = class_;
  1711. // Checks and fixes to character status data, that are required
  1712. // in case of configuration change or stuff, which cannot be
  1713. // checked on char-server.
  1714. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1715. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1716. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1717. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1718. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1719. sd->state.connect_new = 1;
  1720. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1721. sd->invincible_timer = INVALID_TIMER;
  1722. sd->npc_timer_id = INVALID_TIMER;
  1723. sd->pvp_timer = INVALID_TIMER;
  1724. sd->expiration_tid = INVALID_TIMER;
  1725. sd->autotrade_tid = INVALID_TIMER;
  1726. sd->respawn_tid = INVALID_TIMER;
  1727. sd->tid_queue_active = INVALID_TIMER;
  1728. sd->macro_detect.timer = INVALID_TIMER;
  1729. sd->skill_keep_using.tid = INVALID_TIMER;
  1730. sd->skill_keep_using.skill_id = 0;
  1731. sd->skill_keep_using.level = 0;
  1732. sd->skill_keep_using.target = 0;
  1733. #ifdef SECURE_NPCTIMEOUT
  1734. // Initialize to defaults/expected
  1735. sd->npc_idle_timer = INVALID_TIMER;
  1736. sd->npc_idle_tick = tick;
  1737. sd->npc_idle_type = NPCT_INPUT;
  1738. sd->state.ignoretimeout = false;
  1739. #endif
  1740. sd->canuseitem_tick = tick;
  1741. sd->canusecashfood_tick = tick;
  1742. sd->canequip_tick = tick;
  1743. sd->cantalk_tick = tick;
  1744. sd->canskill_tick = tick;
  1745. sd->cansendmail_tick = tick;
  1746. sd->idletime = last_tick;
  1747. sd->regen.tick.hp = tick;
  1748. sd->regen.tick.sp = tick;
  1749. for(int32 i = 0; i < MAX_SPIRITBALL; i++)
  1750. sd->spirit_timer[i] = INVALID_TIMER;
  1751. if (battle_config.item_auto_get)
  1752. sd->state.autoloot = 10000;
  1753. if (battle_config.disp_experience)
  1754. sd->state.showexp = 1;
  1755. if (battle_config.disp_zeny)
  1756. sd->state.showzeny = 1;
  1757. #ifdef VIP_ENABLE
  1758. if (!battle_config.vip_disp_rate)
  1759. sd->vip.disableshowrate = 1;
  1760. #endif
  1761. if (!(battle_config.display_skill_fail&2))
  1762. sd->state.showdelay = 1;
  1763. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1764. memset(&sd->cart, 0, sizeof(struct s_storage));
  1765. memset(&sd->storage, 0, sizeof(struct s_storage));
  1766. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1767. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1768. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1769. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1770. sd->status.option &= ~OPTION_INVISIBLE;
  1771. }
  1772. status_change_init(&sd->bl);
  1773. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1774. unit_dataset(&sd->bl);
  1775. sd->guild_x = -1;
  1776. sd->guild_y = -1;
  1777. sd->delayed_damage = 0;
  1778. // Event Timers
  1779. for( int32 i = 0; i < MAX_EVENTTIMER; i++ )
  1780. sd->eventtimer[i] = INVALID_TIMER;
  1781. // Rental Timer
  1782. sd->rental_timer = INVALID_TIMER;
  1783. for( int32 i = 0; i < 3; i++ )
  1784. sd->hate_mob[i] = -1;
  1785. sd->quest_log = nullptr;
  1786. sd->num_quests = 0;
  1787. sd->avail_quests = 0;
  1788. sd->save_quest = false;
  1789. sd->count_rewarp = 0;
  1790. sd->mail.pending_weight = 0;
  1791. sd->mail.pending_zeny = 0;
  1792. sd->mail.pending_slots = 0;
  1793. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1794. sd->regs.arrays = nullptr;
  1795. sd->vars_dirty = false;
  1796. sd->vars_ok = false;
  1797. sd->vars_received = 0x0;
  1798. // Check if the player's last point requires special handling and if conditions apply to return the player to his savepoint
  1799. if( pc_lastpoint_special( *sd ) ){
  1800. // The player should be warped back to his savepoint
  1801. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1802. sd->status.last_point.x = sd->status.save_point.x;
  1803. sd->status.last_point.y = sd->status.save_point.y;
  1804. }
  1805. //warp player
  1806. 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 );
  1807. if( setpos_result != SETPOS_OK ){
  1808. ShowError( "Last_point_map %s not found (error code %d)\n", sd->status.last_point.map, setpos_result );
  1809. // try warping to a default map instead (church graveyard)
  1810. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1811. // if we fail again
  1812. clif_authfail_fd(sd->fd, 0);
  1813. return false;
  1814. }
  1815. }
  1816. clif_inventory_expansion_info( sd );
  1817. clif_authok(sd);
  1818. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1819. sd->die_counter=-1;
  1820. //display login notice
  1821. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1822. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1823. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1824. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1825. sd->status.name, sd->status.account_id, sd->status.char_id,
  1826. CONVIP(ip), sd->group_id);
  1827. // Send friends list
  1828. clif_friendslist_send( *sd );
  1829. if( !changing_mapservers ) {
  1830. if (battle_config.display_version == 1)
  1831. pc_show_version(sd);
  1832. // Message of the Day [Valaris]
  1833. for(int32 i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1834. if (battle_config.motd_type)
  1835. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1836. else
  1837. clif_displaymessage(sd->fd, motd_text[i]);
  1838. }
  1839. if (expiration_time != 0)
  1840. sd->expiration_time = expiration_time;
  1841. /**
  1842. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1843. **/
  1844. clif_changemap( *sd, sd->bl.m, sd->bl.x, sd->bl.y );
  1845. }
  1846. pc_validate_skill(sd);
  1847. /* [Ind] */
  1848. sd->sc_display = nullptr;
  1849. sd->sc_display_count = 0;
  1850. // Player has not yet received the CashShop list
  1851. sd->status.cashshop_sent = false;
  1852. sd->last_addeditem_index = -1;
  1853. sd->bonus_script.head = nullptr;
  1854. sd->bonus_script.count = 0;
  1855. // Initialize BG queue
  1856. sd->bg_queue_id = 0;
  1857. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1858. sd->hatEffects = {};
  1859. #endif
  1860. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1861. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1862. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1863. clif_updatestatus(*sd, SP_BASEEXP);
  1864. }
  1865. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1866. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1867. clif_updatestatus(*sd, SP_JOBEXP);
  1868. }
  1869. // Request all registries (auth is considered completed whence they arrive)
  1870. intif_request_registry(sd,7);
  1871. return true;
  1872. }
  1873. /*==========================================
  1874. * Closes a connection because it failed to be authenticated from the char server.
  1875. *------------------------------------------*/
  1876. void pc_authfail(map_session_data *sd)
  1877. {
  1878. clif_authfail_fd(sd->fd, 0);
  1879. return;
  1880. }
  1881. /**
  1882. * Player register a bl as hatred
  1883. * @param sd : player session
  1884. * @param pos : hate position [0;2]
  1885. * @param bl : target bl
  1886. * @return false:failed, true:success
  1887. */
  1888. bool pc_set_hate_mob(map_session_data *sd, int32 pos, struct block_list *bl)
  1889. {
  1890. int32 class_;
  1891. if (!sd || !bl || pos < 0 || pos > 2)
  1892. return false;
  1893. if (sd->hate_mob[pos] != -1)
  1894. { //Can't change hate targets.
  1895. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1896. return false;
  1897. }
  1898. class_ = status_get_class(bl);
  1899. if (!pcdb_checkid(class_)) {
  1900. uint32 max_hp = status_get_max_hp(bl);
  1901. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1902. return false;
  1903. if (pos != status_get_size(bl))
  1904. return false; //Wrong size
  1905. }
  1906. sd->hate_mob[pos] = class_;
  1907. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1908. clif_hate_info(sd, pos, class_, 1);
  1909. return true;
  1910. }
  1911. /*==========================================
  1912. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1913. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1914. * See intif_parse_StorageReceived() for item operations [lighta]
  1915. *------------------------------------------*/
  1916. void pc_reg_received(map_session_data *sd)
  1917. {
  1918. uint8 i;
  1919. sd->vars_ok = true;
  1920. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1921. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1922. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1923. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1924. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1925. if (msg_checklangtype(sd->langtype,true) < 0)
  1926. sd->langtype = 0; //invalid langtype reset to default
  1927. // Cash shop
  1928. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1929. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1930. // Cooking Exp
  1931. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1932. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1933. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1934. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1935. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1936. }
  1937. if (battle_config.feature_banking)
  1938. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1939. if (battle_config.feature_roulette) {
  1940. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1941. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1942. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1943. }
  1944. sd->roulette.prizeIdx = -1;
  1945. //SG map and mob read [Komurka]
  1946. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1947. uint16 j;
  1948. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1949. sd->feel_map[i].index = j;
  1950. sd->feel_map[i].m = map_mapindex2mapid(j);
  1951. } else {
  1952. sd->feel_map[i].index = 0;
  1953. sd->feel_map[i].m = -1;
  1954. }
  1955. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1956. }
  1957. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1958. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1959. sd->cloneskill_idx = skill_get_index(skid);
  1960. if (sd->cloneskill_idx > 0) {
  1961. sd->status.skill[sd->cloneskill_idx].id = skid;
  1962. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1963. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1964. sd->status.skill[sd->cloneskill_idx].lv = i;
  1965. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1966. }
  1967. }
  1968. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1969. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1970. sd->reproduceskill_idx = skill_get_index(skid);
  1971. if (sd->reproduceskill_idx > 0) {
  1972. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1973. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1974. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1975. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1976. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1977. }
  1978. }
  1979. //Weird... maybe registries were reloaded?
  1980. if (sd->state.active)
  1981. return;
  1982. sd->state.active = 1;
  1983. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1984. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1985. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1986. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1987. // Restore IM_CHAR instance to the player
  1988. for (const auto &instance : instances) {
  1989. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1990. sd->instance_id = instance.first;
  1991. break;
  1992. }
  1993. }
  1994. if (sd->status.party_id > 0)
  1995. party_member_joined( *sd );
  1996. if (sd->status.guild_id > 0)
  1997. guild_member_joined(sd);
  1998. if (sd->status.clan_id > 0)
  1999. clan_member_joined( *sd );
  2000. #if !( PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410 )
  2001. // 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
  2002. clif_instance_info( *sd );
  2003. #endif
  2004. // pet
  2005. if (sd->status.pet_id > 0)
  2006. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  2007. // Homunculus [albator]
  2008. if( sd->status.hom_id > 0 )
  2009. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  2010. if( sd->status.mer_id > 0 )
  2011. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  2012. if( sd->status.ele_id > 0 )
  2013. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  2014. map_addiddb(&sd->bl);
  2015. map_delnickdb(sd->status.char_id, sd->status.name);
  2016. if (!chrif_auth_finished(sd))
  2017. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  2018. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  2019. chrif_bsdata_request(sd->status.char_id);
  2020. #ifdef VIP_ENABLE
  2021. sd->vip.time = 0;
  2022. sd->vip.enabled = 0;
  2023. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  2024. #endif
  2025. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  2026. intif_request_questlog(sd);
  2027. if (battle_config.feature_achievement) {
  2028. sd->achievement_data.total_score = 0;
  2029. sd->achievement_data.level = 0;
  2030. sd->achievement_data.save = false;
  2031. sd->achievement_data.count = 0;
  2032. sd->achievement_data.incompleteCount = 0;
  2033. sd->achievement_data.achievements = nullptr;
  2034. intif_request_achievements(sd->status.char_id);
  2035. }
  2036. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  2037. sd->state.connect_new = 1;
  2038. clif_parse_LoadEndAck(sd->fd, sd);
  2039. if( pc_isinvisible( sd ) ){
  2040. sd->vd.class_ = JT_INVISIBLE;
  2041. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  2042. // decrement the number of pvp players on the map
  2043. map_getmapdata( sd->bl.m )->users_pvp--;
  2044. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  2045. // unregister the player for ranking
  2046. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  2047. sd->pvp_timer = INVALID_TIMER;
  2048. }
  2049. clif_changeoption( &sd->bl );
  2050. }
  2051. }
  2052. channel_autojoin(sd);
  2053. }
  2054. static int32 pc_calc_skillpoint(map_session_data* sd)
  2055. {
  2056. uint16 i, skill_point = 0;
  2057. nullpo_ret(sd);
  2058. for(i = 1; i < MAX_SKILL; i++) {
  2059. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  2060. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  2061. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  2062. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  2063. )
  2064. {
  2065. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  2066. skill_point += sd->status.skill[i].lv;
  2067. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  2068. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  2069. }
  2070. }
  2071. }
  2072. return skill_point;
  2073. }
  2074. static bool pc_grant_allskills(map_session_data *sd, bool addlv) {
  2075. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  2076. return false;
  2077. /**
  2078. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  2079. * skill tree and since they have no icons they'll give resource errors
  2080. * Get ALL skills except npc/guild ones. [Skotlex]
  2081. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  2082. **/
  2083. for (const auto &skill : skill_db) {
  2084. uint16 skill_id = skill.second->nameid;
  2085. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  2086. continue;
  2087. switch (skill_id) {
  2088. case SM_SELFPROVOKE:
  2089. case AB_DUPLELIGHT_MELEE:
  2090. case AB_DUPLELIGHT_MAGIC:
  2091. case WL_CHAINLIGHTNING_ATK:
  2092. case WL_TETRAVORTEX_FIRE:
  2093. case WL_TETRAVORTEX_WATER:
  2094. case WL_TETRAVORTEX_WIND:
  2095. case WL_TETRAVORTEX_GROUND:
  2096. case WL_SUMMON_ATK_FIRE:
  2097. case WL_SUMMON_ATK_WIND:
  2098. case WL_SUMMON_ATK_WATER:
  2099. case WL_SUMMON_ATK_GROUND:
  2100. case LG_OVERBRAND_BRANDISH:
  2101. case LG_OVERBRAND_PLUSATK:
  2102. case WM_SEVERE_RAINSTORM_MELEE:
  2103. case RL_R_TRIP_PLUSATK:
  2104. case SG_DEVIL:
  2105. case MO_TRIPLEATTACK:
  2106. case RG_SNATCHER:
  2107. continue;
  2108. default:
  2109. {
  2110. uint8 lv = (uint8)skill_get_max(skill_id);
  2111. if (lv > 0) {
  2112. uint16 idx = skill_get_index(skill_id);
  2113. sd->status.skill[idx].id = skill_id;
  2114. if (addlv)
  2115. sd->status.skill[idx].lv = lv;
  2116. }
  2117. }
  2118. break;
  2119. }
  2120. }
  2121. return true;
  2122. }
  2123. /*==========================================
  2124. * Calculation of skill level.
  2125. * @param sd
  2126. *------------------------------------------*/
  2127. void pc_calc_skilltree(map_session_data *sd)
  2128. {
  2129. nullpo_retv(sd);
  2130. uint64 job = pc_calc_skilltree_normalize_job(sd);
  2131. int32 class_ = pc_mapid2jobid(job, sd->status.sex);
  2132. if( class_ == -1 )
  2133. { //Unable to normalize job??
  2134. 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 );
  2135. return;
  2136. }
  2137. uint16 job_id = class_;
  2138. class_ = pc_class2idx(class_);
  2139. for (const auto &skill : skill_db) {
  2140. uint16 skill_id = skill.second->nameid;
  2141. uint16 idx = skill_get_index(skill_id);
  2142. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  2143. sd->status.skill[idx].id = 0; //First clear skills.
  2144. /* permanent skills that must be re-checked */
  2145. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  2146. if (skill_id == 0) {
  2147. sd->status.skill[idx].id = 0;
  2148. sd->status.skill[idx].lv = 0;
  2149. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2150. continue;
  2151. }
  2152. switch (skill_id) {
  2153. case NV_TRICKDEAD:
  2154. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  2155. sd->status.skill[idx].id = 0;
  2156. sd->status.skill[idx].lv = 0;
  2157. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2158. }
  2159. break;
  2160. }
  2161. }
  2162. }
  2163. for (const auto &skill : skill_db) {
  2164. uint16 idx = skill_get_index(skill.second->nameid);
  2165. // Restore original level of skills after deleting earned skills.
  2166. 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 ) {
  2167. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2168. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2169. }
  2170. }
  2171. // Removes Taekwon Ranker skill bonus
  2172. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  2173. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  2174. if (tree != nullptr && !tree->skills.empty()) {
  2175. for (const auto &it : tree->skills) {
  2176. uint16 sk_idx = skill_get_index(it.first);
  2177. if (sk_idx == 0)
  2178. continue;
  2179. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  2180. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  2181. continue;
  2182. sd->status.skill[sk_idx].id = 0;
  2183. }
  2184. }
  2185. }
  2186. }
  2187. // Grant all skills
  2188. pc_grant_allskills(sd, false);
  2189. int32 flag;
  2190. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2191. do {
  2192. flag = 0;
  2193. if (tree == nullptr || tree->skills.empty())
  2194. break;
  2195. for (const auto &skillsit : tree->skills) {
  2196. bool fail = false;
  2197. uint16 skid = skillsit.first;
  2198. uint16 sk_idx = skill_get_index(skid);
  2199. if (sd->status.skill[sk_idx].id)
  2200. continue; //Skill already known.
  2201. if (!battle_config.skillfree) {
  2202. // Checking required skills
  2203. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2204. if (entry != nullptr && !entry->need.empty()) {
  2205. for (const auto &it : entry->need) {
  2206. uint16 sk_need_id = it.first;
  2207. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2208. if (sk_need_idx > 0) {
  2209. uint16 sk_need = sk_need_id;
  2210. 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)
  2211. sk_need = 0; //Not learned.
  2212. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  2213. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2214. else
  2215. sk_need = pc_checkskill(sd,sk_need_id);
  2216. if (sk_need < it.second) {
  2217. fail = true;
  2218. break;
  2219. }
  2220. }
  2221. }
  2222. }
  2223. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  2224. int32 c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2225. c_ = pc_class2idx(c_);
  2226. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  2227. fail = true; // base level requirement wasn't satisfied
  2228. }
  2229. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  2230. int32 c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2231. c_ = pc_class2idx(c_);
  2232. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  2233. fail = true; // job level requirement wasn't satisfied
  2234. }
  2235. }
  2236. if (!fail) {
  2237. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2238. if (!sd->status.skill[sk_idx].lv && (
  2239. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2240. skill->inf2[INF2_ISWEDDING] ||
  2241. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2242. ))
  2243. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  2244. sd->status.skill[sk_idx].id = skid;
  2245. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  2246. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  2247. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  2248. }
  2249. flag = 1; // skill list has changed, perform another pass
  2250. }
  2251. }
  2252. } while(flag);
  2253. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  2254. uint16 skid = 0;
  2255. /* Taekwon Ranker Bonus Skill Tree
  2256. ============================================
  2257. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  2258. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  2259. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  2260. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2261. if (tree != nullptr && !tree->skills.empty()) {
  2262. for (const auto &it : tree->skills) {
  2263. skid = it.first;
  2264. uint16 sk_idx = skill_get_index(skid);
  2265. if (sk_idx == 0)
  2266. continue;
  2267. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  2268. continue; //Do not include Quest/Wedding skills.
  2269. if( sd->status.skill[sk_idx].id == 0 ) {
  2270. sd->status.skill[sk_idx].id = skid;
  2271. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  2272. } else if( skid != NV_BASIC )
  2273. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  2274. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  2275. }
  2276. }
  2277. }
  2278. // Enable Bard/Dancer spirit linked skills.
  2279. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_BARDDANCER) {
  2280. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  2281. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  2282. { BA_POEMBRAGI, DC_FORTUNEKISS },
  2283. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  2284. for (const auto &skill : linked_skills) {
  2285. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  2286. continue;
  2287. // Tag it as a non-savable, non-uppable, bonus skill
  2288. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  2289. }
  2290. }
  2291. }
  2292. //Checks if you can learn a new skill after having leveled up a skill.
  2293. static void pc_check_skilltree(map_session_data *sd)
  2294. {
  2295. int32 flag = 0;
  2296. if (battle_config.skillfree)
  2297. return; //Function serves no purpose if this is set
  2298. int32 c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  2299. if (c == -1) { //Unable to normalize job??
  2300. 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);
  2301. return;
  2302. }
  2303. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  2304. if (tree == nullptr || tree->skills.empty())
  2305. return;
  2306. do {
  2307. flag = 0;
  2308. for (const auto &skillsit : tree->skills) {
  2309. uint16 skid = skillsit.first;
  2310. uint16 sk_idx = skill_get_index(skid);
  2311. bool fail = false;
  2312. if (sd->status.skill[sk_idx].id) //Already learned
  2313. continue;
  2314. // Checking required skills
  2315. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2316. if (entry != nullptr && !entry->need.empty()) {
  2317. for (const auto &it : entry->need) {
  2318. uint16 sk_need_id = it.first;
  2319. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2320. if (sk_need_id > 0) {
  2321. short sk_need = sk_need_id;
  2322. 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)
  2323. sk_need = 0; //Not learned.
  2324. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  2325. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2326. else
  2327. sk_need = pc_checkskill(sd,sk_need_id);
  2328. if (sk_need < it.second) {
  2329. fail = true;
  2330. break;
  2331. }
  2332. }
  2333. }
  2334. }
  2335. if( fail )
  2336. continue;
  2337. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2338. continue;
  2339. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2340. if( !sd->status.skill[sk_idx].lv && (
  2341. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2342. skill->inf2[INF2_ISWEDDING] ||
  2343. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2344. ) )
  2345. continue; //Cannot be learned via normal means.
  2346. sd->status.skill[sk_idx].id = skid;
  2347. flag = 1;
  2348. }
  2349. } while(flag);
  2350. }
  2351. // Make sure all the skills are in the correct condition
  2352. // before persisting to the backend.. [MouseJstr]
  2353. void pc_clean_skilltree(map_session_data *sd)
  2354. {
  2355. uint16 i;
  2356. for (i = 0; i < MAX_SKILL; i++){
  2357. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2358. sd->status.skill[i].id = 0;
  2359. sd->status.skill[i].lv = 0;
  2360. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2361. }
  2362. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2363. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2364. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2365. }
  2366. }
  2367. }
  2368. uint64 pc_calc_skilltree_normalize_job_sub( map_session_data *sd ){
  2369. int32 skill_point = pc_calc_skillpoint( sd );
  2370. if( sd->class_ & MAPID_SUMMONER ){
  2371. // Novice's skill points for basic skill.
  2372. std::shared_ptr<s_job_info> summoner_job = job_db.find( JOB_SUMMONER );
  2373. int32 summoner_skills = summoner_job->max_job_level - 1;
  2374. if( skill_point < summoner_skills ){
  2375. return MAPID_SUMMONER;
  2376. }
  2377. skill_point -= summoner_skills;
  2378. }else{
  2379. // Novice's skill points for basic skill.
  2380. std::shared_ptr<s_job_info> novice_job = job_db.find( JOB_NOVICE );
  2381. int32 novice_skills = novice_job->max_job_level - 1;
  2382. if( skill_point < novice_skills ){
  2383. return MAPID_NOVICE;
  2384. }
  2385. skill_point -= novice_skills;
  2386. }
  2387. // 1st Class Job LV Check
  2388. if( sd->class_ & JOBL_2 && ( sd->class_ & MAPID_UPPERMASK ) != MAPID_SUPER_NOVICE ){
  2389. uint64 mapid_1st = sd->class_ & MAPID_BASEMASK;
  2390. int32 class_1st = pc_mapid2jobid( mapid_1st, sd->status.sex );
  2391. if( !sd->change_level_2nd ){
  2392. if( class_1st >= JOB_NOVICE ){
  2393. sd->change_level_2nd = job_db.find( class_1st )->max_job_level;
  2394. }else{
  2395. sd->change_level_2nd = 0;
  2396. }
  2397. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  2398. }
  2399. if( class_1st > 0 && skill_point < ( sd->change_level_2nd - 1 ) ){
  2400. return mapid_1st;
  2401. }
  2402. skill_point -= ( sd->change_level_2nd - 1 );
  2403. }
  2404. // 2nd Class Job LV Check
  2405. if( sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E ){
  2406. uint64 mapid_2nd = sd->class_ & MAPID_UPPERMASK;
  2407. int32 class_2nd = pc_mapid2jobid( mapid_2nd, sd->status.sex );
  2408. if( !sd->change_level_3rd ){
  2409. if( class_2nd >= JOB_NOVICE ){
  2410. sd->change_level_3rd = job_db.find( class_2nd )->max_job_level;
  2411. }else{
  2412. sd->change_level_3rd = 0;
  2413. }
  2414. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  2415. }
  2416. if( class_2nd > 0 && skill_point < ( sd->change_level_3rd - 1 ) ){
  2417. return mapid_2nd;
  2418. }
  2419. skill_point -= ( sd->change_level_3rd - 1 );
  2420. }
  2421. // 3rd Class Job LV Check
  2422. if( sd->class_ & JOBL_FOURTH ){
  2423. uint64 mapid_3rd = sd->class_ & MAPID_THIRDMASK | JOBL_THIRD;
  2424. int32 class_3rd = pc_mapid2jobid( mapid_3rd, sd->status.sex );
  2425. if( !sd->change_level_4th ){
  2426. if( class_3rd >= JOB_NOVICE ){
  2427. sd->change_level_4th = job_db.find( class_3rd )->max_job_level;
  2428. }else{
  2429. sd->change_level_4th = 0;
  2430. }
  2431. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ), sd->change_level_4th );
  2432. }
  2433. if( class_3rd > 0 && skill_point < ( sd->change_level_4th - 1 ) ){
  2434. return mapid_3rd;
  2435. }
  2436. skill_point -= ( sd->change_level_4th - 1 );
  2437. }
  2438. return sd->class_;
  2439. }
  2440. uint64 pc_calc_skilltree_normalize_job( map_session_data *sd ){
  2441. if( !battle_config.skillup_limit || pc_has_permission( sd, PC_PERM_ALL_SKILL ) ){
  2442. return sd->class_;
  2443. }
  2444. uint64 c = pc_calc_skilltree_normalize_job_sub( sd );
  2445. // Special Masks
  2446. if( sd->class_&JOBL_UPPER ){
  2447. if( pc_mapid2jobid( c | JOBL_UPPER, sd->status.sex ) >= JOB_NOVICE ){
  2448. c |= JOBL_UPPER;// Rebirth Job
  2449. }
  2450. }
  2451. if( sd->class_&JOBL_BABY ){
  2452. if( pc_mapid2jobid( c | JOBL_BABY, sd->status.sex ) >= JOB_NOVICE ){
  2453. c |= JOBL_BABY;// Baby Job
  2454. }
  2455. }
  2456. return c;
  2457. }
  2458. /*==========================================
  2459. * Updates the weight status
  2460. *------------------------------------------
  2461. * 1: overweight 50% for pre-renewal and 70% for renewal
  2462. * 2: overweight 90%
  2463. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2464. */
  2465. void pc_updateweightstatus(map_session_data *sd)
  2466. {
  2467. int32 old_overweight;
  2468. int32 new_overweight;
  2469. nullpo_retv(sd);
  2470. old_overweight = (sd->sc.getSCE(SC_WEIGHT90)) ? 2 : (sd->sc.getSCE(SC_WEIGHT50)) ? 1 : 0;
  2471. #ifdef RENEWAL
  2472. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2473. #else
  2474. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2475. #endif
  2476. if( old_overweight == new_overweight )
  2477. return; // no change
  2478. // stop old status change
  2479. if( old_overweight == 1 )
  2480. status_change_end(&sd->bl, SC_WEIGHT50);
  2481. else if( old_overweight == 2 )
  2482. status_change_end(&sd->bl, SC_WEIGHT90);
  2483. // start new status change
  2484. if( new_overweight == 1 )
  2485. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2486. else if( new_overweight == 2 )
  2487. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2488. // update overweight status
  2489. sd->regen.state.overweight = new_overweight;
  2490. }
  2491. int32 pc_disguise(map_session_data *sd, int32 class_)
  2492. {
  2493. if (!class_ && !sd->disguise)
  2494. return 0;
  2495. if (class_ && sd->disguise == class_)
  2496. return 0;
  2497. if(pc_isinvisible(sd))
  2498. { //Character is invisible. Stealth class-change. [Skotlex]
  2499. sd->disguise = class_; //viewdata is set on uncloaking.
  2500. return 2;
  2501. }
  2502. if (sd->bl.prev != nullptr) {
  2503. pc_stop_walking(sd, 0);
  2504. clif_clearunit_area( sd->bl, CLR_OUTSIGHT );
  2505. }
  2506. if (!class_) {
  2507. sd->disguise = 0;
  2508. class_ = sd->status.class_;
  2509. } else
  2510. sd->disguise=class_;
  2511. status_set_viewdata(&sd->bl, class_);
  2512. clif_changeoption(&sd->bl);
  2513. if (sd->bl.prev != nullptr) {
  2514. clif_spawn(&sd->bl);
  2515. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2516. { //It seems the cart info is lost on undisguise.
  2517. clif_cartlist(sd);
  2518. clif_updatestatus(*sd,SP_CARTINFO);
  2519. }
  2520. if (sd->chatID) {
  2521. struct chat_data* cd;
  2522. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != nullptr)
  2523. clif_dispchat(cd,0);
  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, short rate, short 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, (short)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, short 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, short 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, (short)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, short 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_, short 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, short 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, short rate, short 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, (short)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, short rate, short 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, (short)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, short rate, short 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, (short)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. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3367. sd->bonus.atk_rate += val;
  3368. break;
  3369. case SP_MAGIC_ATK_DEF:
  3370. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3371. sd->bonus.magic_def_rate += val;
  3372. break;
  3373. case SP_MISC_ATK_DEF:
  3374. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3375. sd->bonus.misc_def_rate += val;
  3376. break;
  3377. case SP_IGNORE_MDEF_ELE:
  3378. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3379. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3380. sd->bonus.ignore_mdef_ele |= 1<<val;
  3381. break;
  3382. case SP_IGNORE_MDEF_RACE:
  3383. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3384. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3385. sd->bonus.ignore_mdef_race |= 1<<val;
  3386. break;
  3387. case SP_PERFECT_HIT_RATE:
  3388. if (sd->state.lr_flag != LR_FLAG_ARROW && sd->bonus.perfect_hit < val)
  3389. sd->bonus.perfect_hit = val;
  3390. break;
  3391. case SP_PERFECT_HIT_ADD_RATE:
  3392. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3393. sd->bonus.perfect_hit_add += val;
  3394. break;
  3395. case SP_CRITICAL_RATE:
  3396. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3397. sd->critical_rate+=val;
  3398. break;
  3399. case SP_DEF_RATIO_ATK_ELE:
  3400. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3401. if (sd->state.lr_flag == LR_FLAG_NONE)
  3402. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3403. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3404. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3405. break;
  3406. case SP_DEF_RATIO_ATK_RACE:
  3407. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3408. if (sd->state.lr_flag == LR_FLAG_NONE)
  3409. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3410. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3411. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3412. break;
  3413. case SP_DEF_RATIO_ATK_CLASS:
  3414. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3415. if (sd->state.lr_flag == LR_FLAG_NONE)
  3416. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3417. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3418. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3419. break;
  3420. case SP_HIT_RATE:
  3421. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3422. sd->hit_rate += val;
  3423. break;
  3424. case SP_FLEE_RATE:
  3425. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3426. sd->flee_rate += val;
  3427. break;
  3428. case SP_FLEE2_RATE:
  3429. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3430. sd->flee2_rate += val;
  3431. break;
  3432. case SP_DEF_RATE:
  3433. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3434. sd->def_rate += val;
  3435. break;
  3436. case SP_DEF2_RATE:
  3437. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3438. sd->def2_rate += val;
  3439. break;
  3440. case SP_MDEF_RATE:
  3441. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3442. sd->mdef_rate += val;
  3443. break;
  3444. case SP_MDEF2_RATE:
  3445. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3446. sd->mdef2_rate += val;
  3447. break;
  3448. case SP_PATK_RATE:
  3449. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3450. sd->patk_rate += val;
  3451. break;
  3452. case SP_SMATK_RATE:
  3453. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3454. sd->smatk_rate += val;
  3455. break;
  3456. case SP_RES_RATE:
  3457. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3458. sd->res_rate += val;
  3459. break;
  3460. case SP_MRES_RATE:
  3461. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3462. sd->mres_rate += val;
  3463. break;
  3464. case SP_HPLUS_RATE:
  3465. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3466. sd->hplus_rate += val;
  3467. break;
  3468. case SP_CRATE_RATE:
  3469. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3470. sd->crate_rate += val;
  3471. break;
  3472. case SP_RESTART_FULL_RECOVER:
  3473. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3474. sd->special_state.restart_full_recover = 1;
  3475. break;
  3476. case SP_NO_CASTCANCEL:
  3477. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3478. sd->special_state.no_castcancel = 1;
  3479. break;
  3480. case SP_NO_CASTCANCEL2:
  3481. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3482. sd->special_state.no_castcancel2 = 1;
  3483. break;
  3484. case SP_NO_SIZEFIX:
  3485. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3486. sd->special_state.no_sizefix = 1;
  3487. break;
  3488. case SP_NO_MAGIC_DAMAGE:
  3489. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3490. break;
  3491. val+= sd->special_state.no_magic_damage;
  3492. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3493. break;
  3494. case SP_NO_WEAPON_DAMAGE:
  3495. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3496. break;
  3497. val+= sd->special_state.no_weapon_damage;
  3498. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3499. break;
  3500. case SP_NO_MISC_DAMAGE:
  3501. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3502. break;
  3503. val+= sd->special_state.no_misc_damage;
  3504. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3505. break;
  3506. case SP_NO_GEMSTONE:
  3507. if (sd->state.lr_flag != LR_FLAG_ARROW && sd->special_state.no_gemstone != 2)
  3508. sd->special_state.no_gemstone = 1;
  3509. break;
  3510. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3511. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3512. sd->special_state.intravision = 1;
  3513. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3514. }
  3515. break;
  3516. case SP_NO_KNOCKBACK:
  3517. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3518. sd->special_state.no_knockback = 1;
  3519. break;
  3520. case SP_SPLASH_RANGE:
  3521. if(sd->bonus.splash_range < val)
  3522. sd->bonus.splash_range = val;
  3523. break;
  3524. case SP_SPLASH_ADD_RANGE:
  3525. sd->bonus.splash_add_range += val;
  3526. break;
  3527. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3528. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3529. sd->bonus.short_weapon_damage_return += val;
  3530. break;
  3531. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3532. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3533. sd->bonus.long_weapon_damage_return += val;
  3534. break;
  3535. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3536. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3537. sd->bonus.magic_damage_return += val;
  3538. break;
  3539. case SP_REDUCE_DAMAGE_RETURN:
  3540. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3541. sd->bonus.reduce_damage_return += val;
  3542. break;
  3543. case SP_ALL_STATS: // [Valaris]
  3544. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3545. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3546. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3547. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3548. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3549. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3550. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3551. }
  3552. break;
  3553. case SP_ALL_TRAIT_STATS:
  3554. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3555. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3556. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3557. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3558. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3559. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3560. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3561. }
  3562. break;
  3563. case SP_AGI_VIT: // [Valaris]
  3564. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3565. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3566. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3567. }
  3568. break;
  3569. case SP_AGI_DEX_STR: // [Valaris]
  3570. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3571. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3572. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3573. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3574. }
  3575. break;
  3576. case SP_PERFECT_HIDE: // [Valaris]
  3577. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3578. sd->special_state.perfect_hiding=1;
  3579. break;
  3580. case SP_UNBREAKABLE:
  3581. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3582. sd->bonus.unbreakable += val;
  3583. break;
  3584. case SP_UNBREAKABLE_WEAPON:
  3585. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3586. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3587. break;
  3588. case SP_UNBREAKABLE_ARMOR:
  3589. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3590. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3591. break;
  3592. case SP_UNBREAKABLE_HELM:
  3593. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3594. sd->bonus.unbreakable_equip |= EQP_HELM;
  3595. break;
  3596. case SP_UNBREAKABLE_SHIELD:
  3597. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3598. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3599. break;
  3600. case SP_UNBREAKABLE_GARMENT:
  3601. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3602. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3603. break;
  3604. case SP_UNBREAKABLE_SHOES:
  3605. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3606. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3607. break;
  3608. case SP_CLASSCHANGE: // [Valaris]
  3609. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3610. sd->bonus.classchange=val;
  3611. break;
  3612. case SP_SHORT_ATK_RATE:
  3613. if (sd->state.lr_flag != LR_FLAG_ARROW) //[Lupus] it should stack, too. As any other cards rate bonuses
  3614. sd->bonus.short_attack_atk_rate+=val;
  3615. break;
  3616. case SP_LONG_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.long_attack_atk_rate+=val;
  3619. break;
  3620. case SP_BREAK_WEAPON_RATE:
  3621. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3622. sd->bonus.break_weapon_rate+=val;
  3623. break;
  3624. case SP_BREAK_ARMOR_RATE:
  3625. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3626. sd->bonus.break_armor_rate+=val;
  3627. break;
  3628. case SP_ADD_STEAL_RATE:
  3629. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3630. sd->bonus.add_steal_rate+=val;
  3631. break;
  3632. case SP_DELAYRATE:
  3633. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3634. sd->bonus.delayrate -= val;
  3635. break;
  3636. case SP_CRIT_ATK_RATE:
  3637. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3638. sd->bonus.crit_atk_rate += val;
  3639. break;
  3640. case SP_CRIT_DEF_RATE:
  3641. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3642. sd->bonus.crit_def_rate += val;
  3643. break;
  3644. case SP_NO_REGEN:
  3645. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3646. sd->regen.state.block|=val;
  3647. break;
  3648. case SP_UNSTRIPABLE_WEAPON:
  3649. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3650. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3651. break;
  3652. case SP_UNSTRIPABLE:
  3653. case SP_UNSTRIPABLE_ARMOR:
  3654. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3655. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3656. break;
  3657. case SP_UNSTRIPABLE_HELM:
  3658. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3659. sd->bonus.unstripable_equip |= EQP_HELM;
  3660. break;
  3661. case SP_UNSTRIPABLE_SHIELD:
  3662. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3663. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3664. break;
  3665. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3666. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3667. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3668. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3669. } else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3670. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3671. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3672. }
  3673. break;
  3674. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3675. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3676. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3677. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3678. } else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3679. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3680. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3681. }
  3682. break;
  3683. case SP_SP_GAIN_VALUE:
  3684. if (sd->state.lr_flag == LR_FLAG_NONE)
  3685. sd->bonus.sp_gain_value += val;
  3686. break;
  3687. case SP_HP_GAIN_VALUE:
  3688. if (sd->state.lr_flag == LR_FLAG_NONE)
  3689. sd->bonus.hp_gain_value += val;
  3690. break;
  3691. case SP_LONG_SP_GAIN_VALUE:
  3692. if (sd->state.lr_flag == LR_FLAG_NONE)
  3693. sd->bonus.long_sp_gain_value += val;
  3694. break;
  3695. case SP_LONG_HP_GAIN_VALUE:
  3696. if (sd->state.lr_flag == LR_FLAG_NONE)
  3697. sd->bonus.long_hp_gain_value += val;
  3698. break;
  3699. case SP_MAGIC_SP_GAIN_VALUE:
  3700. if (sd->state.lr_flag == LR_FLAG_NONE)
  3701. sd->bonus.magic_sp_gain_value += val;
  3702. break;
  3703. case SP_MAGIC_HP_GAIN_VALUE:
  3704. if (sd->state.lr_flag == LR_FLAG_NONE)
  3705. sd->bonus.magic_hp_gain_value += val;
  3706. break;
  3707. case SP_ADD_HEAL_RATE:
  3708. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3709. sd->bonus.add_heal_rate += val;
  3710. break;
  3711. case SP_ADD_HEAL2_RATE:
  3712. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3713. sd->bonus.add_heal2_rate += val;
  3714. break;
  3715. case SP_ADD_ITEM_HEAL_RATE:
  3716. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3717. sd->bonus.itemhealrate2 += val;
  3718. break;
  3719. case SP_EMATK:
  3720. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3721. sd->bonus.ematk += val;
  3722. break;
  3723. case SP_ADD_VARIABLECAST:
  3724. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3725. sd->bonus.add_varcast += val;
  3726. break;
  3727. #ifdef RENEWAL_CAST
  3728. case SP_FIXCASTRATE:
  3729. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3730. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3731. break;
  3732. case SP_ADD_FIXEDCAST:
  3733. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3734. sd->bonus.add_fixcast += val;
  3735. break;
  3736. case SP_CASTRATE:
  3737. case SP_VARCASTRATE:
  3738. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3739. sd->bonus.varcastrate -= val;
  3740. break;
  3741. #else
  3742. case SP_ADD_FIXEDCAST:
  3743. case SP_FIXCASTRATE:
  3744. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3745. break;
  3746. case SP_VARCASTRATE:
  3747. case SP_CASTRATE:
  3748. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3749. sd->castrate += val;
  3750. break;
  3751. #endif
  3752. case SP_ADDMAXWEIGHT:
  3753. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3754. sd->add_max_weight += val;
  3755. break;
  3756. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3757. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3758. break;
  3759. case SP_ABSORB_DMG_MAXHP2:
  3760. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3761. break;
  3762. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3763. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3764. sd->bonus.critical_rangeatk += val*10;
  3765. else
  3766. sd->bonus.arrow_cri += val*10;
  3767. break;
  3768. case SP_WEAPON_ATK_RATE:
  3769. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3770. sd->bonus.weapon_atk_rate += val;
  3771. break;
  3772. case SP_WEAPON_MATK_RATE:
  3773. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3774. sd->bonus.weapon_matk_rate += val;
  3775. break;
  3776. case SP_NO_MADO_FUEL:
  3777. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3778. sd->special_state.no_mado_fuel = 1;
  3779. break;
  3780. case SP_NO_WALK_DELAY:
  3781. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3782. sd->special_state.no_walk_delay = 1;
  3783. break;
  3784. case SP_ADD_ITEM_SPHEAL_RATE:
  3785. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3786. sd->bonus.itemsphealrate2 += val;
  3787. break;
  3788. default:
  3789. if (current_equip_combo_pos > 0) {
  3790. 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);
  3791. }
  3792. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3793. 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);
  3794. }
  3795. else {
  3796. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3797. }
  3798. break;
  3799. }
  3800. }
  3801. /*==========================================
  3802. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3803. * format: bonus2 bBonusName,type2,val;
  3804. * @param sd
  3805. * @param type Bonus type used by bBonusName
  3806. * @param type2
  3807. * @param val Value that usually for rate or fixed value
  3808. *------------------------------------------*/
  3809. void pc_bonus2(map_session_data *sd,int32 type,int32 type2,int32 val)
  3810. {
  3811. nullpo_retv(sd);
  3812. switch(type){
  3813. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3814. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3815. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3816. sd->right_weapon.addele[type2]+=val;
  3817. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3818. sd->left_weapon.addele[type2]+=val;
  3819. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3820. sd->indexed_bonus.arrow_addele[type2]+=val;
  3821. break;
  3822. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3823. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3824. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3825. sd->right_weapon.addrace[type2]+=val;
  3826. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3827. sd->left_weapon.addrace[type2]+=val;
  3828. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3829. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3830. break;
  3831. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3832. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3833. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3834. sd->right_weapon.addclass[type2]+=val;
  3835. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3836. sd->left_weapon.addclass[type2]+=val;
  3837. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3838. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3839. break;
  3840. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3841. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3842. if (sd->state.lr_flag == LR_FLAG_NONE || sd->state.lr_flag == LR_FLAG_SHIELD)
  3843. sd->right_weapon.addsize[type2]+=val;
  3844. else if (sd->state.lr_flag == LR_FLAG_WEAPON)
  3845. sd->left_weapon.addsize[type2]+=val;
  3846. else if (sd->state.lr_flag == LR_FLAG_ARROW)
  3847. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3848. break;
  3849. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3850. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3851. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3852. sd->indexed_bonus.subele_script[type2] += val;
  3853. break;
  3854. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3855. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3856. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3857. sd->indexed_bonus.subrace[type2]+=val;
  3858. break;
  3859. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3860. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3861. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3862. sd->indexed_bonus.subclass[type2]+=val;
  3863. break;
  3864. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3865. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3866. 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);
  3867. break;
  3868. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3869. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  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, ATF_SELF, 0);
  3871. break;
  3872. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3873. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3874. break;
  3875. if (sd->reseff.size() == MAX_PC_BONUS) {
  3876. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3877. break;
  3878. }
  3879. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3880. break;
  3881. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3882. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3883. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3884. sd->indexed_bonus.magic_addele_script[type2] += val;
  3885. break;
  3886. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3887. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3888. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3889. sd->indexed_bonus.magic_addrace[type2]+=val;
  3890. break;
  3891. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3892. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3893. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3894. sd->indexed_bonus.magic_addclass[type2]+=val;
  3895. break;
  3896. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3897. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3898. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3899. sd->indexed_bonus.magic_addsize[type2]+=val;
  3900. break;
  3901. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3902. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3903. if (sd->state.lr_flag != LR_FLAG_ARROW)
  3904. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3905. break;
  3906. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3907. struct weapon_data *wd = (sd->state.lr_flag != LR_FLAG_NONE ? &sd->left_weapon : &sd->right_weapon);
  3908. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3909. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3910. break;
  3911. }
  3912. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3913. }
  3914. break;
  3915. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3916. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3917. break;
  3918. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3919. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3920. break;
  3921. }
  3922. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3923. break;
  3924. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3925. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3926. break;
  3927. if (sd->add_def.size() == MAX_PC_BONUS) {
  3928. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3929. break;
  3930. }
  3931. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3932. break;
  3933. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3934. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3935. break;
  3936. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3937. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3938. break;
  3939. }
  3940. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3941. break;
  3942. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3943. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3944. sd->right_weapon.hp_drain_rate.rate += type2;
  3945. sd->right_weapon.hp_drain_rate.per += val;
  3946. }
  3947. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3948. sd->left_weapon.hp_drain_rate.rate += type2;
  3949. sd->left_weapon.hp_drain_rate.per += val;
  3950. }
  3951. break;
  3952. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3953. if (sd->state.lr_flag == LR_FLAG_NONE) {
  3954. sd->right_weapon.sp_drain_rate.rate += type2;
  3955. sd->right_weapon.sp_drain_rate.per += val;
  3956. }
  3957. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  3958. sd->left_weapon.sp_drain_rate.rate += type2;
  3959. sd->left_weapon.sp_drain_rate.per += val;
  3960. }
  3961. break;
  3962. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3963. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3964. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3965. }
  3966. break;
  3967. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3968. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3969. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3970. }
  3971. break;
  3972. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3973. if (sd->state.lr_flag != LR_FLAG_ARROW && sd->bonus.get_zeny_rate < val) {
  3974. sd->bonus.get_zeny_rate = val;
  3975. sd->bonus.get_zeny_num = type2;
  3976. }
  3977. break;
  3978. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3979. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  3980. sd->bonus.get_zeny_rate += val;
  3981. sd->bonus.get_zeny_num += type2;
  3982. }
  3983. break;
  3984. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3985. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3986. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3987. break;
  3988. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3989. sd->special_state.bonus_coma = 1;
  3990. break;
  3991. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3992. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3993. if (sd->state.lr_flag == LR_FLAG_ARROW)
  3994. break;
  3995. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3996. sd->special_state.bonus_coma = 1;
  3997. break;
  3998. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3999. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  4000. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4001. break;
  4002. sd->indexed_bonus.weapon_coma_class[type2] += val;
  4003. sd->special_state.bonus_coma = 1;
  4004. break;
  4005. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  4006. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4007. sd->indexed_bonus.weapon_atk[type2]+=val;
  4008. break;
  4009. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  4010. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4011. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  4012. break;
  4013. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  4014. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  4015. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4016. sd->indexed_bonus.critaddrace[type2] += val*10;
  4017. break;
  4018. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  4019. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4020. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4021. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  4022. break;
  4023. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  4024. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4025. break;
  4026. if (sd->skillatk.size() == MAX_PC_BONUS) {
  4027. 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);
  4028. break;
  4029. }
  4030. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  4031. break;
  4032. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  4033. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4034. break;
  4035. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4036. 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);
  4037. break;
  4038. }
  4039. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  4040. break;
  4041. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  4042. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4043. break;
  4044. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  4045. 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);
  4046. break;
  4047. }
  4048. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  4049. break;
  4050. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  4051. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4052. break;
  4053. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4054. 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);
  4055. break;
  4056. }
  4057. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  4058. break;
  4059. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  4060. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4061. sd->hp_loss.value = type2;
  4062. sd->hp_loss.rate = val;
  4063. }
  4064. break;
  4065. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  4066. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4067. sd->hp_regen.value = type2;
  4068. sd->hp_regen.rate = val;
  4069. }
  4070. break;
  4071. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  4072. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4073. sd->percent_hp_regen.value = type2;
  4074. sd->percent_hp_regen.rate = val;
  4075. }
  4076. break;
  4077. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  4078. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4079. sd->percent_sp_regen.value = type2;
  4080. sd->percent_sp_regen.rate = val;
  4081. }
  4082. break;
  4083. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  4084. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  4085. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4086. sd->right_weapon.addrace2[type2] += val;
  4087. else
  4088. sd->left_weapon.addrace2[type2] += val;
  4089. break;
  4090. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  4091. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  4092. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4093. sd->indexed_bonus.subsize[type2]+=val;
  4094. break;
  4095. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  4096. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  4097. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4098. sd->indexed_bonus.weapon_subsize[type2] += val;
  4099. break;
  4100. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  4101. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  4102. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4103. sd->indexed_bonus.magic_subsize[type2]+=val;
  4104. break;
  4105. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  4106. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  4107. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4108. sd->indexed_bonus.subrace2[type2]+=val;
  4109. break;
  4110. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  4111. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4112. break;
  4113. if( !item_db.exists( type2 ) ){
  4114. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  4115. break;
  4116. }
  4117. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  4118. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4119. break;
  4120. }
  4121. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  4122. break;
  4123. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  4124. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4125. break;
  4126. if (!type2 || !itemdb_group.exists(type2)) {
  4127. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  4128. break;
  4129. }
  4130. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  4131. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4132. break;
  4133. }
  4134. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  4135. break;
  4136. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  4137. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  4138. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4139. sd->indexed_bonus.expaddrace[type2]+=val;
  4140. break;
  4141. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  4142. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  4143. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4144. sd->indexed_bonus.expaddclass[type2]+=val;
  4145. break;
  4146. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  4147. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  4148. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4149. sd->indexed_bonus.sp_gain_race[type2]+=val;
  4150. break;
  4151. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  4152. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4153. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  4154. break;
  4155. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  4156. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4157. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  4158. break;
  4159. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  4160. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4161. sd->sp_loss.value = type2;
  4162. sd->sp_loss.rate = val;
  4163. }
  4164. break;
  4165. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  4166. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4167. sd->sp_regen.value = type2;
  4168. sd->sp_regen.rate = val;
  4169. }
  4170. break;
  4171. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  4172. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  4173. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4174. sd->right_weapon.hp_drain_race[type2] += val;
  4175. }
  4176. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4177. sd->left_weapon.hp_drain_race[type2] += val;
  4178. }
  4179. break;
  4180. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  4181. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  4182. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4183. sd->right_weapon.sp_drain_race[type2] += val;
  4184. }
  4185. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4186. sd->left_weapon.sp_drain_race[type2] += val;
  4187. }
  4188. break;
  4189. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  4190. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  4191. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4192. sd->right_weapon.hp_drain_class[type2] += val;
  4193. }
  4194. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4195. sd->left_weapon.hp_drain_class[type2] += val;
  4196. }
  4197. break;
  4198. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  4199. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  4200. if (sd->state.lr_flag == LR_FLAG_NONE) {
  4201. sd->right_weapon.sp_drain_class[type2] += val;
  4202. }
  4203. else if (sd->state.lr_flag == LR_FLAG_WEAPON) {
  4204. sd->left_weapon.sp_drain_class[type2] += val;
  4205. }
  4206. break;
  4207. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  4208. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  4209. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4210. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  4211. break;
  4212. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  4213. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  4214. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4215. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  4216. break;
  4217. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  4218. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  4219. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4220. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  4221. break;
  4222. case SP_SP_IGNORE_RES_RACE_RATE: // bonus2 bIgnoreResRaceRate,r,n;
  4223. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_RES_RACE_RATE);
  4224. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4225. sd->indexed_bonus.ignore_res_by_race[type2] += val;
  4226. break;
  4227. case SP_SP_IGNORE_MRES_RACE_RATE: // bonus2 bIgnoreMResRaceRate,r,n;
  4228. PC_BONUS_CHK_RACE(type2,SP_SP_IGNORE_MRES_RACE_RATE);
  4229. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4230. sd->indexed_bonus.ignore_mres_by_race[type2] += val;
  4231. break;
  4232. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  4233. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  4234. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4235. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  4236. break;
  4237. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  4238. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4239. break;
  4240. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  4241. 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);
  4242. break;
  4243. }
  4244. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  4245. break;
  4246. case SP_SKILL_DELAY:
  4247. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4248. break;
  4249. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  4250. 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);
  4251. break;
  4252. }
  4253. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  4254. break;
  4255. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  4256. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4257. break;
  4258. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  4259. 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);
  4260. break;
  4261. }
  4262. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  4263. break;
  4264. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  4265. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4266. break;
  4267. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  4268. 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);
  4269. break;
  4270. }
  4271. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  4272. break;
  4273. #ifdef RENEWAL_CAST
  4274. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4275. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4276. break;
  4277. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  4278. 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);
  4279. break;
  4280. }
  4281. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  4282. break;
  4283. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4284. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4285. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4286. break;
  4287. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  4288. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4289. break;
  4290. }
  4291. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  4292. break;
  4293. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4294. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4295. break;
  4296. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  4297. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4298. break;
  4299. }
  4300. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  4301. break;
  4302. #else
  4303. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4304. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4305. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  4306. break;
  4307. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4308. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4309. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4310. break;
  4311. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4312. 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);
  4313. break;
  4314. }
  4315. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  4316. break;
  4317. #endif
  4318. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  4319. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4320. break;
  4321. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  4322. 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);
  4323. break;
  4324. }
  4325. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4326. break;
  4327. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4328. if (sd->subskill.size() == MAX_PC_BONUS) {
  4329. 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);
  4330. break;
  4331. }
  4332. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4333. break;
  4334. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4335. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4336. sd->indexed_bonus.subdefele[type2] += val;
  4337. break;
  4338. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4339. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4340. sd->indexed_bonus.coma_class[type2] += val;
  4341. sd->special_state.bonus_coma = 1;
  4342. break;
  4343. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4344. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4345. sd->indexed_bonus.coma_race[type2] += val;
  4346. sd->special_state.bonus_coma = 1;
  4347. break;
  4348. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4349. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4350. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4351. sd->indexed_bonus.magic_addrace2[type2] += val;
  4352. break;
  4353. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4354. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4355. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4356. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4357. break;
  4358. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4359. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4360. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4361. sd->indexed_bonus.dropaddrace[type2] += val;
  4362. break;
  4363. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4364. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4365. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4366. sd->indexed_bonus.dropaddclass[type2] += val;
  4367. break;
  4368. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4369. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4370. sd->indexed_bonus.magic_subdefele[type2] += val;
  4371. break;
  4372. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4373. if( sd->state.lr_flag == LR_FLAG_ARROW ){
  4374. break;
  4375. }
  4376. if( !item_db.exists( type2 ) ){
  4377. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4378. break;
  4379. }
  4380. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4381. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4382. break;
  4383. }
  4384. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4385. break;
  4386. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4387. if( sd->state.lr_flag == LR_FLAG_ARROW ){
  4388. break;
  4389. }
  4390. if( !type2 || !itemdb_group.exists( type2 ) ){
  4391. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4392. break;
  4393. }
  4394. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4395. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4396. break;
  4397. }
  4398. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4399. break;
  4400. default:
  4401. if (current_equip_combo_pos > 0) {
  4402. 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);
  4403. }
  4404. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4405. 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);
  4406. }
  4407. else {
  4408. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4409. }
  4410. break;
  4411. }
  4412. }
  4413. /**
  4414. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4415. * @param sd
  4416. * @param type Bonus type used by bBonusName
  4417. * @param type2
  4418. * @param type3
  4419. * @param val Value that usually for rate or fixed value
  4420. */
  4421. void pc_bonus3(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 val)
  4422. {
  4423. nullpo_retv(sd);
  4424. switch(type){
  4425. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4426. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4427. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4428. break;
  4429. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,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_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4434. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4435. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4436. break;
  4437. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4438. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4439. {
  4440. int32 target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4441. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4442. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4443. }
  4444. break;
  4445. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4446. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4447. {
  4448. int32 target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4449. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4450. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4451. }
  4452. break;
  4453. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4454. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4455. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4456. break;
  4457. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4458. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4459. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4460. break;
  4461. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4462. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4463. 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);
  4464. break;
  4465. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4466. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4467. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4468. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4469. break;
  4470. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4471. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4472. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4473. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4474. break;
  4475. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4476. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4477. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4478. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4479. break;
  4480. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4481. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4482. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4483. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4484. break;
  4485. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4486. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4487. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4488. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4489. break;
  4490. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4491. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4492. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4493. sd->sp_vanish_race[type2].rate += type3;
  4494. sd->sp_vanish_race[type2].per += val;
  4495. }
  4496. break;
  4497. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4498. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4499. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4500. sd->hp_vanish_race[type2].rate += type3;
  4501. sd->hp_vanish_race[type2].per += val;
  4502. }
  4503. break;
  4504. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4505. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4506. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4507. }
  4508. break;
  4509. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4510. if (sd->state.lr_flag != LR_FLAG_ARROW) {
  4511. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4512. }
  4513. break;
  4514. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4515. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4516. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4517. break;
  4518. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4519. //if (type3 > sd->norecover_state_race[type2].rate) {
  4520. // sd->norecover_state_race[type2].rate = type3;
  4521. // sd->norecover_state_race[type2].tick = val;
  4522. // break;
  4523. //}
  4524. sd->norecover_state_race[type2].rate = type3;
  4525. sd->norecover_state_race[type2].tick = val;
  4526. break;
  4527. default:
  4528. if (current_equip_combo_pos > 0) {
  4529. 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);
  4530. }
  4531. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4532. 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);
  4533. }
  4534. else {
  4535. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4536. }
  4537. break;
  4538. }
  4539. }
  4540. /**
  4541. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4542. * @param sd
  4543. * @param type Bonus type used by bBonusName
  4544. * @param type2
  4545. * @param type3
  4546. * @param type4
  4547. * @param val Value that usually for rate or fixed value
  4548. */
  4549. void pc_bonus4(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 type4,int32 val)
  4550. {
  4551. nullpo_retv(sd);
  4552. switch(type){
  4553. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4554. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4555. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4556. break;
  4557. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4558. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4559. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4560. break;
  4561. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4562. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4563. {
  4564. int32 target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4565. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4566. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4567. }
  4568. break;
  4569. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4570. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4571. 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);
  4572. break;
  4573. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4574. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4575. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4576. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4577. break;
  4578. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4579. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4580. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4581. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4582. break;
  4583. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4584. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4585. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4586. break;
  4587. sd->def_set_race[type2].rate = type3;
  4588. sd->def_set_race[type2].tick = type4;
  4589. sd->def_set_race[type2].value = val;
  4590. break;
  4591. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4592. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4593. if (sd->state.lr_flag == LR_FLAG_ARROW)
  4594. break;
  4595. sd->mdef_set_race[type2].rate = type3;
  4596. sd->mdef_set_race[type2].tick = type4;
  4597. sd->mdef_set_race[type2].value = val;
  4598. break;
  4599. default:
  4600. if (current_equip_combo_pos > 0) {
  4601. 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);
  4602. }
  4603. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4604. 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);
  4605. }
  4606. else {
  4607. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4608. }
  4609. break;
  4610. }
  4611. }
  4612. /**
  4613. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4614. * @param sd
  4615. * @param type Bonus type used by bBonusName
  4616. * @param type2
  4617. * @param type3
  4618. * @param type4
  4619. * @param val Value that usually for rate or fixed value
  4620. */
  4621. void pc_bonus5(map_session_data *sd,int32 type,int32 type2,int32 type3,int32 type4,int32 type5,int32 val)
  4622. {
  4623. nullpo_retv(sd);
  4624. switch(type){
  4625. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4626. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4627. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4628. break;
  4629. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4630. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4631. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4632. break;
  4633. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4634. if (sd->state.lr_flag != LR_FLAG_ARROW)
  4635. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4636. break;
  4637. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4638. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4639. if( sd->state.lr_flag != LR_FLAG_ARROW )
  4640. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4641. break;
  4642. default:
  4643. if (current_equip_combo_pos > 0) {
  4644. 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);
  4645. }
  4646. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4647. 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);
  4648. }
  4649. else {
  4650. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4651. }
  4652. break;
  4653. }
  4654. }
  4655. /*==========================================
  4656. * Grants a player a given skill. Flag values are:
  4657. * 0 - Grant permanent skill to be bound to skill tree
  4658. * 1 - Grant an item skill (temporary)
  4659. * 2 - Like 1, except the level granted can stack with previously learned level.
  4660. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4661. *------------------------------------------*/
  4662. bool pc_skill(map_session_data* sd, uint16 skill_id, int32 level, enum e_addskill_type type) {
  4663. uint16 idx = 0;
  4664. nullpo_ret(sd);
  4665. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4666. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4667. return false;
  4668. }
  4669. if (level > MAX_SKILL_LEVEL) {
  4670. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4671. return false;
  4672. }
  4673. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4674. 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);
  4675. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4676. }
  4677. switch (type) {
  4678. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4679. sd->status.skill[idx].id = skill_id;
  4680. sd->status.skill[idx].lv = level;
  4681. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4682. if (level == 0) { //Remove skill.
  4683. sd->status.skill[idx].id = 0;
  4684. clif_deleteskill(*sd,skill_id);
  4685. } else
  4686. clif_addskill(*sd,skill_id);
  4687. 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.
  4688. status_calc_pc(sd, SCO_NONE);
  4689. break;
  4690. case ADDSKILL_TEMP: //Item bonus skill.
  4691. if (sd->status.skill[idx].id != 0) {
  4692. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4693. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4694. } else {
  4695. sd->status.skill[idx].id = skill_id;
  4696. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4697. }
  4698. sd->status.skill[idx].lv = max(sd->status.skill[idx].lv, level);
  4699. break;
  4700. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4701. if (sd->status.skill[idx].id != 0) {
  4702. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4703. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4704. } else {
  4705. sd->status.skill[idx].id = skill_id;
  4706. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4707. }
  4708. sd->status.skill[idx].lv += level;
  4709. break;
  4710. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4711. sd->status.skill[idx].id = skill_id;
  4712. sd->status.skill[idx].lv = level;
  4713. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4714. if (level == 0) { //Remove skill.
  4715. sd->status.skill[idx].id = 0;
  4716. clif_deleteskill(*sd,skill_id);
  4717. } else
  4718. clif_addskill(*sd,skill_id);
  4719. 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.
  4720. status_calc_pc(sd, SCO_NONE);
  4721. break;
  4722. default:
  4723. return false;
  4724. }
  4725. return true;
  4726. }
  4727. /**
  4728. * Set's a player's plagiarized skill.
  4729. * @param sd: Player
  4730. * @param skill_id: Skill to be plagiarized
  4731. * @param skill_lv: Skill level to be plagiarized
  4732. * @return True on success or false otherwise
  4733. */
  4734. bool pc_skill_plagiarism(map_session_data &sd, uint16 skill_id, uint16 skill_lv)
  4735. {
  4736. skill_id = skill_dummy2skill_id(skill_id);
  4737. uint16 idx = skill_get_index(skill_id);
  4738. // Use skill index, avoiding out-of-bound array [Cydh]
  4739. if (idx == 0) {
  4740. ShowWarning("pc_skill_plagiarism: invalid skill idx 0 for skill %d.\n", skill_id);
  4741. return false;
  4742. }
  4743. skill_lv = cap_value(skill_lv, 1, skill_get_max(skill_id));
  4744. int32 type = skill_isCopyable(&sd, skill_id);
  4745. if (type == 1) {
  4746. pc_skill_plagiarism_reset(sd, type);
  4747. sd.cloneskill_idx = idx;
  4748. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), skill_id);
  4749. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), skill_lv);
  4750. } else if (type == 2) {
  4751. pc_skill_plagiarism_reset(sd, type);
  4752. sd.reproduceskill_idx = idx;
  4753. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), skill_id);
  4754. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), skill_lv);
  4755. } else {
  4756. ShowWarning("pc_skill_plagiarism: skill %d is not copyable.\n", skill_id);
  4757. return false;
  4758. }
  4759. sd.status.skill[idx].id = skill_id;
  4760. sd.status.skill[idx].lv = static_cast<uint8>(skill_lv);
  4761. sd.status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
  4762. clif_addskill(sd, skill_id);
  4763. return true;
  4764. }
  4765. /**
  4766. * Clear plagiarized skills from a player.
  4767. * @param sd: Player
  4768. * @param type: 1 for Plagiarism or 2 for Reproduce
  4769. * @return True on success or false otherwise
  4770. */
  4771. bool pc_skill_plagiarism_reset(map_session_data &sd, uint8 type)
  4772. {
  4773. uint16 idx;
  4774. if (type == 1)
  4775. idx = sd.cloneskill_idx;
  4776. else if (type == 2)
  4777. idx = sd.reproduceskill_idx;
  4778. else {
  4779. ShowError("pc_skill_plagiarism_reset: Unknown type %d.\n", type);
  4780. return false;
  4781. }
  4782. if (sd.status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED) {
  4783. uint16 skill_id = sd.status.skill[idx].id;
  4784. sd.status.skill[idx].id = 0;
  4785. sd.status.skill[idx].lv = 0;
  4786. sd.status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4787. clif_deleteskill(sd, skill_id);
  4788. if (type == 1) {
  4789. sd.cloneskill_idx = 0;
  4790. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  4791. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  4792. }
  4793. else if (type == 2) {
  4794. sd.reproduceskill_idx = 0;
  4795. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), 0);
  4796. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  4797. }
  4798. }
  4799. return true;
  4800. }
  4801. /*==========================================
  4802. * Append a card to an item ?
  4803. *------------------------------------------*/
  4804. int32 pc_insert_card(map_session_data* sd, int32 idx_card, int32 idx_equip)
  4805. {
  4806. nullpo_ret(sd);
  4807. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4808. return 0;
  4809. }
  4810. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4811. return 0;
  4812. }
  4813. int32 i;
  4814. t_itemid nameid;
  4815. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4816. struct item_data* item_card = sd->inventory_data[idx_card];
  4817. if(item_eq == nullptr)
  4818. return 0; //Invalid item index.
  4819. if(item_card == nullptr)
  4820. return 0; //Invalid card index.
  4821. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4822. return 0; // target item missing
  4823. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4824. return 0; // target card missing
  4825. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4826. return 0; // only weapons and armor are allowed
  4827. if( item_card->type != IT_CARD )
  4828. return 0; // must be a card
  4829. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4830. return 0; // target must be identified
  4831. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4832. return 0; // card slots reserved for other purposes
  4833. if( (item_eq->equip & item_card->equip) == 0 )
  4834. return 0; // card cannot be compounded on this item type
  4835. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4836. return 0; // attempted to place shield card on left-hand weapon.
  4837. 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)) )
  4838. return 0; // specific accessory-card can only be inserted to specific accessory.
  4839. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4840. return 0; // item must be unequipped
  4841. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4842. if( i == item_eq->slots )
  4843. return 0; // no free slots
  4844. // remember the card id to insert
  4845. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4846. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4847. {// failed
  4848. clif_insert_card( *sd, idx_equip, idx_card, true );
  4849. }
  4850. else
  4851. {// success
  4852. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4853. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4854. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4855. clif_insert_card( *sd, idx_equip, idx_card, false );
  4856. }
  4857. return 0;
  4858. }
  4859. /**
  4860. * Returns the count of unidentified items with the option to identify too.
  4861. * @param sd: Player data
  4862. * @param identify_item: Whether or not to identify any unidentified items
  4863. * @return Unidentified items count
  4864. */
  4865. int32 pc_identifyall(map_session_data *sd, bool identify_item)
  4866. {
  4867. int32 unidentified_count = 0;
  4868. for (int32 i = 0; i < MAX_INVENTORY; i++) {
  4869. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4870. if (identify_item == true) {
  4871. sd->inventory.u.items_inventory[i].identify = 1;
  4872. clif_item_identified( *sd, i, false );
  4873. }
  4874. unidentified_count++;
  4875. }
  4876. }
  4877. return unidentified_count;
  4878. }
  4879. //
  4880. // Items
  4881. //
  4882. /*==========================================
  4883. * Update buying value by skills
  4884. *------------------------------------------*/
  4885. int32 pc_modifybuyvalue(map_session_data *sd,int32 orig_value)
  4886. {
  4887. int32 skill,val = orig_value,rate1 = 0,rate2 = 0;
  4888. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4889. rate1 = 5+skill*2-((skill==10)? 1:0);
  4890. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4891. rate2 = 5+skill*4;
  4892. if(rate1 < rate2) rate1 = rate2;
  4893. if(rate1)
  4894. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4895. if(val < battle_config.min_shop_buy)
  4896. val = battle_config.min_shop_buy;
  4897. return val;
  4898. }
  4899. /*==========================================
  4900. * Update selling value by skills
  4901. *------------------------------------------*/
  4902. int32 pc_modifysellvalue(map_session_data *sd,int32 orig_value)
  4903. {
  4904. int32 skill,val = orig_value,rate = 0;
  4905. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4906. rate = 5+skill*2-((skill==10)? 1:0);
  4907. if(rate)
  4908. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4909. if (val < battle_config.min_shop_sell)
  4910. val = battle_config.min_shop_sell;
  4911. return val;
  4912. }
  4913. /*==========================================
  4914. * Checking if we have enough place on inventory for new item
  4915. * Make sure to take 30k as limit (for client I guess)
  4916. * @param sd
  4917. * @param nameid
  4918. * @param amount
  4919. * @return e_chkitem_result
  4920. *------------------------------------------*/
  4921. char pc_checkadditem(map_session_data *sd, t_itemid nameid, int32 amount)
  4922. {
  4923. int32 i;
  4924. struct item_data* data;
  4925. nullpo_ret(sd);
  4926. if(amount > MAX_AMOUNT)
  4927. return CHKADDITEM_OVERAMOUNT;
  4928. data = itemdb_search(nameid);
  4929. if(!itemdb_isstackable2(data))
  4930. return CHKADDITEM_NEW;
  4931. if( data->stack.inventory && amount > data->stack.amount )
  4932. return CHKADDITEM_OVERAMOUNT;
  4933. if (data->flag.guid)
  4934. return CHKADDITEM_NEW;
  4935. for(i=0;i<MAX_INVENTORY;i++){
  4936. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4937. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4938. 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 ) )
  4939. return CHKADDITEM_OVERAMOUNT;
  4940. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4941. if( i >= sd->status.inventory_slots ){
  4942. return CHKADDITEM_OVERAMOUNT;
  4943. }
  4944. return CHKADDITEM_EXIST;
  4945. }
  4946. }
  4947. return CHKADDITEM_NEW;
  4948. }
  4949. /*==========================================
  4950. * Return number of available place in inventory
  4951. * Each non stackable item will reduce place by 1
  4952. * @param sd
  4953. * @return Number of empty slots
  4954. *------------------------------------------*/
  4955. uint8 pc_inventoryblank(map_session_data *sd)
  4956. {
  4957. uint16 i;
  4958. uint8 b;
  4959. nullpo_ret(sd);
  4960. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4961. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4962. b++;
  4963. }
  4964. return b;
  4965. }
  4966. /**
  4967. * Attempts to remove zeny from player
  4968. * @param sd: Player
  4969. * @param zeny: Zeny removed
  4970. * @param type: Log type
  4971. * @param log_charid: (optional) From who to log (if not needed, use 0)
  4972. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4973. */
  4974. char pc_payzeny(map_session_data *sd, int32 zeny, enum e_log_pick_type type, uint32 log_charid)
  4975. {
  4976. nullpo_retr(2,sd);
  4977. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4978. if( zeny < 0 )
  4979. {
  4980. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4981. return 1;
  4982. }
  4983. if( sd->status.zeny < zeny )
  4984. return 1; //Not enough.
  4985. sd->status.zeny -= zeny;
  4986. clif_updatestatus(*sd,SP_ZENY);
  4987. log_zeny(*sd, type, log_charid, -zeny);
  4988. if( zeny > 0 && sd->state.showzeny ) {
  4989. char output[255];
  4990. sprintf(output, "Removed %dz.", zeny);
  4991. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4992. }
  4993. return 0;
  4994. }
  4995. /**
  4996. * Attempts to give zeny to player
  4997. * @param sd: Player
  4998. * @param type: Log type
  4999. * @param log_charid: (optional) From who to log (if not needed, use 0)
  5000. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  5001. */
  5002. char pc_getzeny(map_session_data *sd, int32 zeny, enum e_log_pick_type type, uint32 log_charid)
  5003. {
  5004. nullpo_retr(-1,sd);
  5005. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  5006. if( zeny < 0 )
  5007. {
  5008. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  5009. return 1;
  5010. }
  5011. if( zeny > MAX_ZENY - sd->status.zeny )
  5012. zeny = MAX_ZENY - sd->status.zeny;
  5013. sd->status.zeny += zeny;
  5014. clif_updatestatus(*sd,SP_ZENY);
  5015. log_zeny(*sd, type, log_charid, zeny);
  5016. if( zeny > 0 && sd->state.showzeny ) {
  5017. char output[255];
  5018. sprintf(output, "Gained %dz.", zeny);
  5019. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5020. }
  5021. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  5022. return 0;
  5023. }
  5024. /**
  5025. * Attempts to remove Cash Points from player
  5026. * @param sd: Player
  5027. * @param price: Total points (cash + kafra) the player has to pay
  5028. * @param points: Kafra points the player has to pay
  5029. * @param type: Log type
  5030. * @return -1: Not enough points, otherwise success (cash+points)
  5031. */
  5032. int32 pc_paycash(map_session_data *sd, int32 price, int32 points, e_log_pick_type type)
  5033. {
  5034. int32 cash;
  5035. nullpo_retr(-1,sd);
  5036. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5037. cash = price-points;
  5038. if( sd->cashPoints < cash || sd->kafraPoints < points )
  5039. {
  5040. 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);
  5041. return -1;
  5042. }
  5043. if( cash ){
  5044. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  5045. sd->cashPoints -= cash;
  5046. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  5047. }
  5048. if( points ){
  5049. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  5050. sd->kafraPoints -= points;
  5051. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  5052. }
  5053. if( battle_config.cashshop_show_points )
  5054. {
  5055. char output[CHAT_SIZE_MAX];
  5056. 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.
  5057. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5058. }
  5059. return cash+points;
  5060. }
  5061. /**
  5062. * Attempts to give Cash Points to player
  5063. * @param sd: Player
  5064. * @param cash: Cash points the player gets
  5065. * @param points: Kafra points the player gets
  5066. * @param type: Log type
  5067. * @return -1: Error, otherwise success (cash or points)
  5068. */
  5069. int32 pc_getcash(map_session_data *sd, int32 cash, int32 points, e_log_pick_type type)
  5070. {
  5071. char output[CHAT_SIZE_MAX];
  5072. nullpo_retr(-1,sd);
  5073. cash = cap_value(cash, 0, MAX_CASHPOINT); //prevent command UB
  5074. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  5075. if( cash > 0 )
  5076. {
  5077. if( cash > MAX_CASHPOINT-sd->cashPoints )
  5078. {
  5079. 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);
  5080. cash = MAX_CASHPOINT-sd->cashPoints;
  5081. }
  5082. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  5083. sd->cashPoints += cash;
  5084. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  5085. if( battle_config.cashshop_show_points )
  5086. {
  5087. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  5088. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5089. }
  5090. return cash;
  5091. }
  5092. if( points > 0 )
  5093. {
  5094. if( points > MAX_KAFRAPOINT-sd->kafraPoints )
  5095. {
  5096. 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);
  5097. points = MAX_KAFRAPOINT-sd->kafraPoints;
  5098. }
  5099. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  5100. sd->kafraPoints += points;
  5101. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  5102. if( battle_config.cashshop_show_points )
  5103. {
  5104. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  5105. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5106. }
  5107. return points;
  5108. }
  5109. return -1; //shouldn't happen but just in case
  5110. }
  5111. /**
  5112. * Searching a specified itemid in inventory and return his stored index
  5113. * @param sd Player
  5114. * @param nameid Find this Item!
  5115. * @return Stored index in inventory, or -1 if not found.
  5116. **/
  5117. short pc_search_inventory(map_session_data *sd, t_itemid nameid) {
  5118. short i;
  5119. nullpo_retr(-1, sd);
  5120. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  5121. return ( i < MAX_INVENTORY ) ? i : -1;
  5122. }
  5123. /** Attempt to add a new item to player inventory
  5124. * @param sd
  5125. * @param item_data
  5126. * @param amount
  5127. * @param log_type
  5128. * @return
  5129. * 0 = success
  5130. * 1 = invalid itemid not found or negative amount
  5131. * 2 = overweight
  5132. * 3 = ?
  5133. * 4 = no free place found
  5134. * 5 = max amount reached
  5135. * 6 = ?
  5136. * 7 = stack limitation
  5137. */
  5138. enum e_additem_result pc_additem(map_session_data *sd,struct item *item,int32 amount,e_log_pick_type log_type) {
  5139. struct item_data *id;
  5140. int16 i;
  5141. uint32 w;
  5142. nullpo_retr(ADDITEM_INVALID, sd);
  5143. nullpo_retr(ADDITEM_INVALID, item);
  5144. if( item->nameid == 0 || amount <= 0 )
  5145. return ADDITEM_INVALID;
  5146. if( amount > MAX_AMOUNT )
  5147. return ADDITEM_OVERAMOUNT;
  5148. id = itemdb_search(item->nameid);
  5149. if( id->stack.inventory && amount > id->stack.amount )
  5150. {// item stack limitation
  5151. return ADDITEM_STACKLIMIT;
  5152. }
  5153. w = id->weight*amount;
  5154. if(sd->weight + w > sd->max_weight)
  5155. return ADDITEM_OVERWEIGHT;
  5156. if (id->flag.guid && !item->unique_id)
  5157. item->unique_id = pc_generate_unique_id(sd);
  5158. // Stackable | Non Rental
  5159. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  5160. for( i = 0; i < MAX_INVENTORY; i++ ) {
  5161. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  5162. sd->inventory.u.items_inventory[i].bound == item->bound &&
  5163. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  5164. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  5165. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  5166. 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 ) )
  5167. return ADDITEM_OVERAMOUNT;
  5168. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  5169. if( i >= sd->status.inventory_slots ){
  5170. return ADDITEM_OVERAMOUNT;
  5171. }
  5172. sd->inventory.u.items_inventory[i].amount += amount;
  5173. clif_additem(sd,i,amount,0);
  5174. break;
  5175. }
  5176. }
  5177. }else{
  5178. i = MAX_INVENTORY;
  5179. }
  5180. if (i >= MAX_INVENTORY) {
  5181. i = pc_search_inventory(sd,0);
  5182. if( i < 0 )
  5183. return ADDITEM_OVERITEM;
  5184. if( i >= sd->status.inventory_slots ){
  5185. return ADDITEM_OVERITEM;
  5186. }
  5187. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  5188. // clear equip and favorite fields first, just in case
  5189. if( item->equip )
  5190. sd->inventory.u.items_inventory[i].equip = 0;
  5191. if( item->favorite != 0 )
  5192. sd->inventory.u.items_inventory[i].favorite = 0;
  5193. if( item->equipSwitch )
  5194. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  5195. sd->inventory.u.items_inventory[i].amount = amount;
  5196. sd->inventory_data[i] = id;
  5197. sd->last_addeditem_index = i;
  5198. if (!itemdb_isstackable2(id) || id->flag.guid)
  5199. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  5200. clif_additem(sd,i,amount,0);
  5201. }
  5202. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  5203. sd->weight += w;
  5204. clif_updatestatus(*sd,SP_WEIGHT);
  5205. //Auto-equip
  5206. if(id->flag.autoequip)
  5207. pc_equipitem(sd, i, id->equip);
  5208. /* rental item check */
  5209. if( item->expire_time ) {
  5210. if( time(nullptr) > item->expire_time ) {
  5211. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  5212. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  5213. } else {
  5214. uint32 seconds = (uint32)( item->expire_time - time(nullptr) );
  5215. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  5216. pc_inventory_rental_add(sd, seconds);
  5217. }
  5218. }
  5219. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  5220. pc_show_questinfo(sd);
  5221. return ADDITEM_SUCCESS;
  5222. }
  5223. /*==========================================
  5224. * Remove an item at index n from inventory by amount.
  5225. * @param sd
  5226. * @param n Item index in inventory
  5227. * @param amount
  5228. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  5229. * @param reason Delete reason
  5230. * @param log_type e_log_pick_type
  5231. * @return 1 - invalid itemid or negative amount; 0 - Success
  5232. *------------------------------------------*/
  5233. char pc_delitem(map_session_data *sd,int32 n,int32 amount,int32 type, short reason, e_log_pick_type log_type)
  5234. {
  5235. nullpo_retr(1, sd);
  5236. 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)
  5237. return 1;
  5238. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  5239. sd->inventory.u.items_inventory[n].amount -= amount;
  5240. sd->weight -= sd->inventory_data[n]->weight*amount ;
  5241. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  5242. if(sd->inventory.u.items_inventory[n].equip)
  5243. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  5244. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  5245. sd->inventory_data[n] = nullptr;
  5246. }
  5247. if(!(type&1))
  5248. clif_delitem( *sd, n, amount, reason );
  5249. if(!(type&2))
  5250. clif_updatestatus(*sd,SP_WEIGHT);
  5251. pc_show_questinfo(sd);
  5252. return 0;
  5253. }
  5254. /*==========================================
  5255. * Attempt to drop an item.
  5256. * @param sd
  5257. * @param n Item index in inventory
  5258. * @param amount Amount of item
  5259. * @return False = fail; True = success
  5260. *------------------------------------------*/
  5261. bool pc_dropitem(map_session_data *sd,int32 n,int32 amount)
  5262. {
  5263. nullpo_retr(1, sd);
  5264. if(n < 0 || n >= MAX_INVENTORY)
  5265. return false;
  5266. if(amount <= 0)
  5267. return false;
  5268. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  5269. sd->inventory.u.items_inventory[n].amount <= 0 ||
  5270. sd->inventory.u.items_inventory[n].amount < amount ||
  5271. sd->state.trading || sd->state.vending ||
  5272. !sd->inventory_data[n] //pc_delitem would fail on this case.
  5273. )
  5274. return false;
  5275. if( sd->inventory.u.items_inventory[n].equipSwitch )
  5276. return false;
  5277. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  5278. {
  5279. clif_displaymessage (sd->fd, msg_txt(sd,271));
  5280. return false; //Can't drop items in nodrop mapflag maps.
  5281. }
  5282. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  5283. {
  5284. clif_displaymessage (sd->fd, msg_txt(sd,263));
  5285. return false;
  5286. }
  5287. // Bypass drop restriction in map_addflooritem because we've already checked it above
  5288. 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,
  5289. false, DIR_MAX, battle_config.item_stacking?BL_NUL:BL_ITEM))
  5290. {
  5291. return false;
  5292. }
  5293. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  5294. clif_dropitem( *sd, n, amount );
  5295. return true;
  5296. }
  5297. /*==========================================
  5298. * Attempt to pick up an item.
  5299. * @param sd
  5300. * @param fitem Item that will be picked
  5301. * @return False = fail; True = success
  5302. *------------------------------------------*/
  5303. bool pc_takeitem(map_session_data *sd,struct flooritem_data *fitem)
  5304. {
  5305. int32 flag = 0;
  5306. t_tick tick = gettick();
  5307. struct party_data *p = nullptr;
  5308. nullpo_ret(sd);
  5309. nullpo_ret(fitem);
  5310. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  5311. return false; // Distance is too far
  5312. if (sd->sc.cant.pickup)
  5313. return false;
  5314. if (sd->status.party_id)
  5315. p = party_search(sd->status.party_id);
  5316. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  5317. map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  5318. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  5319. if (!(p && p->party.item&1 &&
  5320. first_sd && first_sd->status.party_id == sd->status.party_id
  5321. ))
  5322. return false;
  5323. }
  5324. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  5325. map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  5326. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  5327. if (!(p && p->party.item&1 &&
  5328. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5329. (second_sd && second_sd->status.party_id == sd->status.party_id))
  5330. ))
  5331. return false;
  5332. }
  5333. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  5334. map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  5335. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  5336. if(!(p && p->party.item&1 &&
  5337. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5338. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  5339. (third_sd && third_sd->status.party_id == sd->status.party_id))
  5340. ))
  5341. return false;
  5342. }
  5343. }
  5344. }
  5345. }
  5346. //This function takes care of giving the item to whoever should have it, considering party-share options.
  5347. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  5348. clif_additem(sd,0,0,flag);
  5349. return true;
  5350. }
  5351. //Display pickup animation.
  5352. pc_stop_attack(sd);
  5353. clif_takeitem(sd->bl,fitem->bl);
  5354. if (fitem->mob_id &&
  5355. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  5356. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  5357. )
  5358. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  5359. map_clearflooritem(&fitem->bl);
  5360. return true;
  5361. }
  5362. /*==========================================
  5363. * Check if item is usable.
  5364. * Return:
  5365. * false = no
  5366. * true = yes
  5367. *------------------------------------------*/
  5368. bool pc_isUseitem(map_session_data *sd,int32 n)
  5369. {
  5370. struct item_data *item;
  5371. t_itemid nameid;
  5372. nullpo_ret(sd);
  5373. item = sd->inventory_data[n];
  5374. nameid = sd->inventory.u.items_inventory[n].nameid;
  5375. if( item == nullptr )
  5376. return false;
  5377. //Not consumable item
  5378. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  5379. return false;
  5380. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  5381. return true;
  5382. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5383. if(mapdata->getMapFlag(MF_NOITEMCONSUMPTION)) //consumable but mapflag prevent it
  5384. return false;
  5385. //Prevent mass item usage. [Skotlex]
  5386. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  5387. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  5388. )
  5389. return false;
  5390. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  5391. clif_msg(sd, MSI_CANT_USE_WHEN_SITDOWN);
  5392. return false; // You cannot use this item while sitting.
  5393. }
  5394. if (sd->state.storage_flag && item->type != IT_CASH) {
  5395. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  5396. return false; // You cannot use this item while storage is open.
  5397. }
  5398. if (item->flag.dead_branch && (mapdata->getMapFlag(MF_NOBRANCH) || mapdata_flag_gvg2(mapdata)))
  5399. return false;
  5400. if( itemdb_group.item_exists( IG_MF_NOTELEPORT, nameid ) ){
  5401. if( ( mapdata->getMapFlag(MF_NOTELEPORT) || mapdata_flag_gvg2( mapdata ) ) ){
  5402. clif_skill_teleportmessage( *sd, NOTIFY_MAPINFO_CANT_TP );
  5403. return false;
  5404. }
  5405. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5406. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5407. return false;
  5408. }
  5409. }
  5410. if( itemdb_group.item_exists( IG_MF_NORETURN, nameid ) ){
  5411. if( mapdata->getMapFlag(MF_NORETURN) ){
  5412. return false;
  5413. }
  5414. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5415. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5416. return false;
  5417. }
  5418. }
  5419. if( itemdb_group.item_exists( IG_GIANT_FLY_WING, nameid ) ){
  5420. struct party_data *pd = party_search( sd->status.party_id );
  5421. if( pd ){
  5422. int32 i;
  5423. ARR_FIND( 0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader );
  5424. // User is not party leader
  5425. if( i == MAX_PARTY ){
  5426. clif_msg( sd, MSI_CANNOT_PARTYCALL);
  5427. return false;
  5428. }
  5429. 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 ) );
  5430. // No party members found on same map
  5431. if( i == MAX_PARTY ){
  5432. clif_msg( sd, MSI_NO_PARTYMEM_ON_THISMAP );
  5433. return false;
  5434. }
  5435. }else{
  5436. clif_msg( sd, MSI_CANNOT_PARTYCALL);
  5437. return false;
  5438. }
  5439. }
  5440. switch( nameid ) {
  5441. case ITEMID_MERCENARY_RED_POTION:
  5442. case ITEMID_MERCENARY_BLUE_POTION:
  5443. case ITEMID_M_CENTER_POTION:
  5444. case ITEMID_M_AWAKENING_POTION:
  5445. case ITEMID_M_BERSERK_POTION:
  5446. if( sd->md == nullptr || sd->md->db == nullptr )
  5447. return false;
  5448. if( sd->md->sc.cant.consume )
  5449. return false;
  5450. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5451. return false;
  5452. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5453. return false;
  5454. break;
  5455. case ITEMID_NEURALIZER:
  5456. if( !mapdata->getMapFlag(MF_RESET) )
  5457. return false;
  5458. break;
  5459. }
  5460. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != nullptr )
  5461. return false; // Mercenary Scrolls
  5462. // Safe check type cash disappear when overweight [Napster]
  5463. if( item->flag.group || item->type == IT_CASH ){
  5464. #ifdef RENEWAL
  5465. // Check if the player is not overweighted
  5466. // In Renewal the limit is 70% weight and gives the same error message
  5467. if (pc_is70overweight(sd)) {
  5468. clif_msg_color(sd, MSI_PICKUP_FAILED_ITEMCREATE, color_table[COLOR_RED]);
  5469. return 0;
  5470. }
  5471. #else
  5472. // Check if the player is not overweighted
  5473. // In Pre-Renewal the limit is 50% weight and gives a specific error message
  5474. if (pc_is50overweight(sd)) {
  5475. clif_msg_color(sd, MSI_CANT_GET_ITEM_BECAUSE_WEIGHT, color_table[COLOR_RED]);
  5476. return 0;
  5477. }
  5478. #endif
  5479. // Check if the player has enough free spaces in the inventory
  5480. // Official servers use 10 as the minimum amount of slots required to get the items
  5481. // The <= is intentional, as in official servers you actually need an extra empty slot
  5482. // TODO: Count the items the player will get and check for the actual inventory space required std::max<size_t>( count, 10 )
  5483. if (pc_inventoryblank(sd) <= 10) {
  5484. #ifdef RENEWAL
  5485. clif_msg_color(sd, MSI_PICKUP_FAILED_ITEMCREATE, color_table[COLOR_RED]);
  5486. #else
  5487. clif_msg_color(sd, MSI_CANT_GET_ITEM_BECAUSE_COUNT, color_table[COLOR_RED]);
  5488. #endif
  5489. return 0;
  5490. }
  5491. }
  5492. //Gender check
  5493. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5494. return false;
  5495. //Required level check
  5496. if(item->elv && sd->status.base_level < (uint32)item->elv)
  5497. return false;
  5498. if(item->elvmax && sd->status.base_level > (uint32)item->elvmax)
  5499. return false;
  5500. //Not equipable by class. [Skotlex]
  5501. if (!pc_job_can_use_item(sd,item))
  5502. return false;
  5503. if (sd->sc.cant.consume)
  5504. return false;
  5505. if (!pc_isItemClass(sd,item))
  5506. return false;
  5507. //Dead Branch items
  5508. if( item->flag.dead_branch )
  5509. log_branch(sd);
  5510. return true;
  5511. }
  5512. /*==========================================
  5513. * Last checks to use an item.
  5514. * Return:
  5515. * 0 = fail
  5516. * 1 = success
  5517. *------------------------------------------*/
  5518. int32 pc_useitem(map_session_data *sd,int32 n)
  5519. {
  5520. t_tick tick = gettick();
  5521. int32 amount;
  5522. t_itemid nameid;
  5523. struct script_code *script;
  5524. struct item item;
  5525. struct item_data *id;
  5526. nullpo_ret(sd);
  5527. if (sd->state.mail_writing)
  5528. return 0;
  5529. if (sd->npc_id) {
  5530. if (sd->progressbar.npc_id) {
  5531. clif_progressbar_abort(sd);
  5532. return 0; // First item use attempt cancels the progress bar
  5533. }
  5534. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5535. #ifdef RENEWAL
  5536. clif_msg( sd, MSI_BUSY);
  5537. #endif
  5538. return 0;
  5539. }
  5540. }
  5541. item = sd->inventory.u.items_inventory[n];
  5542. id = sd->inventory_data[n];
  5543. if (item.nameid == 0 || item.amount <= 0)
  5544. return 0;
  5545. if( !pc_isUseitem(sd,n) )
  5546. return 0;
  5547. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5548. nameid = id->nameid;
  5549. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5550. return 0;
  5551. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5552. if( id->flag.delay_consume > 0 ) {
  5553. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.getSCE(SC_ALL_RIDING) )
  5554. return 0;
  5555. else if( pc_issit(sd) )
  5556. return 0;
  5557. }
  5558. //Since most delay-consume items involve using a "skill-type" target cursor,
  5559. //perform a skill-use check before going through. [Skotlex]
  5560. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5561. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5562. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5563. return 0;
  5564. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5565. return 0;
  5566. /* on restricted maps the item is consumed but the effect is not used */
  5567. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5568. clif_msg(sd, MSI_IMPOSSIBLE_USEITEM_AREA); // This item cannot be used within this area
  5569. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5570. clif_useitemack(sd,n,item.amount-1,true);
  5571. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5572. }
  5573. return 0;/* regardless, effect is not run */
  5574. }
  5575. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5576. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5577. return 0;
  5578. }
  5579. sd->itemid = item.nameid;
  5580. sd->itemindex = n;
  5581. amount = item.amount;
  5582. script = id->script;
  5583. //Check if the item is to be consumed immediately [Skotlex]
  5584. if (id->flag.delay_consume > 0)
  5585. clif_useitemack(sd, n, amount, true);
  5586. else
  5587. {
  5588. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5589. {
  5590. clif_useitemack(sd, n, amount - 1, true);
  5591. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5592. }
  5593. else
  5594. clif_useitemack(sd, n, 0, false);
  5595. }
  5596. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5597. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5598. //Update item use time.
  5599. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5600. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5601. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5602. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5603. potion_flag = 0;
  5604. return 1;
  5605. }
  5606. /**
  5607. * Add item on cart for given index.
  5608. * @param sd
  5609. * @param item
  5610. * @param amount
  5611. * @param log_type
  5612. * @return See pc.hpp::e_additem_result
  5613. */
  5614. enum e_additem_result pc_cart_additem(map_session_data *sd,struct item *item,int32 amount,e_log_pick_type log_type)
  5615. {
  5616. struct item_data *data;
  5617. int32 i,w;
  5618. nullpo_retr(ADDITEM_INVALID, sd);
  5619. nullpo_retr(ADDITEM_INVALID, item);
  5620. if(item->nameid == 0 || amount <= 0)
  5621. return ADDITEM_INVALID;
  5622. if (itemdb_ishatched_egg(item))
  5623. return ADDITEM_INVALID;
  5624. data = itemdb_search(item->nameid);
  5625. if( data->stack.cart && amount > data->stack.amount )
  5626. {// item stack limitation
  5627. return ADDITEM_STACKLIMIT;
  5628. }
  5629. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5630. { // Check item trade restrictions [Skotlex]
  5631. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5632. return ADDITEM_INVALID;
  5633. }
  5634. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5635. return ADDITEM_OVERWEIGHT;
  5636. i = MAX_CART;
  5637. if( itemdb_isstackable2(data) && !item->expire_time )
  5638. {
  5639. for (i = 0; i < MAX_CART; i++) {
  5640. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5641. && sd->cart.u.items_cart[i].bound == item->bound
  5642. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5643. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5644. )
  5645. break;
  5646. }
  5647. }
  5648. if( i < MAX_CART )
  5649. {// item already in cart, stack it
  5650. 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 ) )
  5651. return ADDITEM_OVERAMOUNT; // no slot
  5652. sd->cart.u.items_cart[i].amount += amount;
  5653. clif_cart_additem(sd,i,amount);
  5654. }
  5655. else
  5656. {// item not stackable or not present, add it
  5657. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5658. if( i == MAX_CART )
  5659. return ADDITEM_OVERAMOUNT; // no slot
  5660. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5661. sd->cart.u.items_cart[i].id = 0;
  5662. sd->cart.u.items_cart[i].amount = amount;
  5663. sd->cart_num++;
  5664. clif_cart_additem(sd,i,amount);
  5665. }
  5666. sd->cart.u.items_cart[i].favorite = 0; // clear
  5667. sd->cart.u.items_cart[i].equipSwitch = 0;
  5668. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5669. sd->cart_weight += w;
  5670. clif_updatestatus(*sd,SP_CARTINFO);
  5671. return ADDITEM_SUCCESS;
  5672. }
  5673. /*==========================================
  5674. * Delete item on cart for given index.
  5675. *------------------------------------------*/
  5676. void pc_cart_delitem(map_session_data *sd,int32 n,int32 amount,int32 type,e_log_pick_type log_type)
  5677. {
  5678. nullpo_retv(sd);
  5679. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5680. sd->cart.u.items_cart[n].amount < amount)
  5681. return;
  5682. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5683. sd->cart.u.items_cart[n].amount -= amount;
  5684. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5685. if(sd->cart.u.items_cart[n].amount <= 0) {
  5686. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5687. sd->cart_num--;
  5688. }
  5689. if(!type) {
  5690. clif_cart_delitem( *sd, n, amount );
  5691. clif_updatestatus(*sd,SP_CARTINFO);
  5692. }
  5693. }
  5694. /*==========================================
  5695. * Transfer item from inventory to cart.
  5696. *------------------------------------------*/
  5697. void pc_putitemtocart(map_session_data *sd,int32 idx,int32 amount)
  5698. {
  5699. nullpo_retv(sd);
  5700. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5701. return;
  5702. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5703. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5704. return;
  5705. if( item_data->equipSwitch ){
  5706. clif_msg( sd, MSI_SWAP_EQUIPITEM_UNREGISTER_FIRST );
  5707. return;
  5708. }
  5709. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5710. if (flag == ADDITEM_SUCCESS)
  5711. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5712. else {
  5713. if (flag == ADDITEM_OVERAMOUNT)
  5714. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_COUNT );
  5715. else
  5716. clif_cart_additem_ack( *sd, ADDITEM_TO_CART_FAIL_WEIGHT );
  5717. clif_additem(sd, idx, amount, 0);
  5718. clif_delitem( *sd, idx, amount, 0 );
  5719. }
  5720. }
  5721. /*==========================================
  5722. * Get number of item in cart.
  5723. * Return:
  5724. -1 = itemid not found or no amount found
  5725. x = remaining itemid on cart after get
  5726. *------------------------------------------*/
  5727. int32 pc_cartitem_amount(map_session_data* sd, int32 idx, int32 amount)
  5728. {
  5729. struct item* item_data;
  5730. nullpo_retr(-1, sd);
  5731. item_data = &sd->cart.u.items_cart[idx];
  5732. if( item_data->nameid == 0 || item_data->amount == 0 )
  5733. return -1;
  5734. return item_data->amount - amount;
  5735. }
  5736. /*==========================================
  5737. * Retrieve an item at index idx from cart.
  5738. *------------------------------------------*/
  5739. bool pc_getitemfromcart(map_session_data *sd,int32 idx,int32 amount)
  5740. {
  5741. nullpo_retr(1, sd);
  5742. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5743. return false;
  5744. struct item *item_data=&sd->cart.u.items_cart[idx];
  5745. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5746. return false;
  5747. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5748. if (flag == ADDITEM_SUCCESS)
  5749. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5750. else {
  5751. clif_cart_delitem( *sd, idx, amount );
  5752. clif_additem(sd, idx, amount, flag);
  5753. clif_cart_additem(sd, idx, amount);
  5754. }
  5755. return true;
  5756. }
  5757. /*==========================================
  5758. * Bound Item Check
  5759. * Type:
  5760. * 1 Account Bound
  5761. * 2 Guild Bound
  5762. * 3 Party Bound
  5763. * 4 Character Bound
  5764. *------------------------------------------*/
  5765. int32 pc_bound_chk(TBL_PC *sd,enum bound_type type,int32 *idxlist)
  5766. {
  5767. int32 i = 0, j = 0;
  5768. for(i = 0; i < MAX_INVENTORY; i++) {
  5769. 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) {
  5770. idxlist[j] = i;
  5771. j++;
  5772. }
  5773. }
  5774. return j;
  5775. }
  5776. /*==========================================
  5777. * Display item stolen msg to player sd
  5778. *------------------------------------------*/
  5779. int32 pc_show_steal(struct block_list *bl,va_list ap)
  5780. {
  5781. map_session_data *sd;
  5782. t_itemid itemid;
  5783. char output[100];
  5784. sd=va_arg(ap,map_session_data *);
  5785. itemid=va_arg(ap,int);
  5786. std::shared_ptr<item_data> id = item_db.find(itemid);
  5787. if(id == nullptr)
  5788. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5789. else
  5790. sprintf(output,"%s stole %s.",sd->status.name,id->ename.c_str());
  5791. clif_displaymessage( ((map_session_data *)bl)->fd, output);
  5792. return 0;
  5793. }
  5794. /**
  5795. * Steal an item from bl (mob).
  5796. * @param sd: Player data
  5797. * @param bl: Object to steal from
  5798. * @param skill_lv: Level of skill used
  5799. * @return True on success or false otherwise
  5800. */
  5801. bool pc_steal_item(map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5802. {
  5803. int32 i;
  5804. t_itemid itemid;
  5805. double rate;
  5806. unsigned char flag = 0;
  5807. struct mob_data *md;
  5808. if(!sd || !bl || bl->type!=BL_MOB)
  5809. return false;
  5810. md = (TBL_MOB *)bl;
  5811. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5812. return false;
  5813. status_data* sd_status = status_get_status_data(sd->bl);
  5814. status_data* md_status = status_get_status_data(*bl);
  5815. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5816. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5817. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5818. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5819. ) { //Can't steal from
  5820. md->state.steal_flag = UCHAR_MAX;
  5821. return false;
  5822. }
  5823. // base skill success chance (percentual)
  5824. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5825. rate += sd->bonus.add_steal_rate;
  5826. if( rate < 1
  5827. #ifdef RENEWAL
  5828. || rnd()%100 >= rate
  5829. #endif
  5830. )
  5831. return false;
  5832. // Try dropping one item, in the order from first to last possible slot.
  5833. // Droprate is affected by the skill success rate.
  5834. for( i = 0; i < MAX_MOB_DROP; i++ )
  5835. if( item_db.exists(md->db->dropitem[i].nameid) && !md->db->dropitem[i].steal_protected && rnd() % 10000 < md->db->dropitem[i].rate
  5836. #ifndef RENEWAL
  5837. * rate/100.
  5838. #endif
  5839. )
  5840. break;
  5841. if( i == MAX_MOB_DROP )
  5842. return false;
  5843. itemid = md->db->dropitem[i].nameid;
  5844. struct item tmp_item = {};
  5845. tmp_item.nameid = itemid;
  5846. tmp_item.amount = 1;
  5847. tmp_item.identify = itemdb_isidentified(itemid);
  5848. if( battle_config.skill_steal_random_options ){
  5849. mob_setdropitem_option( tmp_item, md->db->dropitem[i] );
  5850. }
  5851. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5852. //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?
  5853. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5854. if(flag) { //Failed to steal due to overweight
  5855. clif_additem(sd,0,0,flag);
  5856. return false;
  5857. }
  5858. if(battle_config.show_steal_in_same_party)
  5859. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5860. //Logs items, Stolen from mobs [Lupus]
  5861. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5862. //A Rare Steal Global Announce by Lupus
  5863. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5864. struct item_data *i_data;
  5865. char message[128];
  5866. i_data = itemdb_search(itemid);
  5867. 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);
  5868. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5869. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5870. }
  5871. return true;
  5872. }
  5873. /*==========================================
  5874. * Stole zeny from bl (mob)
  5875. * return
  5876. * 0 = fail
  5877. * 1 = success
  5878. *------------------------------------------*/
  5879. int32 pc_steal_coin(map_session_data *sd,struct block_list *target)
  5880. {
  5881. int32 rate, target_lv;
  5882. struct mob_data *md;
  5883. if(!sd || !target || target->type != BL_MOB)
  5884. return 0;
  5885. md = (TBL_MOB*)target;
  5886. target_lv = status_get_lv(target);
  5887. if (md->state.steal_coin_flag || md->sc.getSCE(SC_STONE) || md->sc.getSCE(SC_FREEZE) || md->sc.getSCE(SC_HANDICAPSTATE_FROSTBITE) ||
  5888. md->sc.getSCE(SC_HANDICAPSTATE_SWOONING) || md->sc.getSCE(SC_HANDICAPSTATE_LIGHTNINGSTRIKE) || md->sc.getSCE(SC_HANDICAPSTATE_CRYSTALLIZATION) ||
  5889. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5890. return 0;
  5891. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5892. if(rnd()%1000 < rate)
  5893. {
  5894. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5895. int32 amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5896. pc_getzeny(sd, amount, LOG_TYPE_STEAL);
  5897. md->state.steal_coin_flag = 1;
  5898. return 1;
  5899. }
  5900. return 0;
  5901. }
  5902. /*==========================================
  5903. * Set's a player position.
  5904. * @param sd
  5905. * @param mapindex
  5906. * @param x
  5907. * @param y
  5908. * @param clrtype
  5909. * @return SETPOS_OK Success
  5910. * SETPOS_MAPINDEX Invalid map index
  5911. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5912. * SETPOS_AUTOTRADE Player is in autotrade state
  5913. *------------------------------------------*/
  5914. enum e_setpos pc_setpos(map_session_data* sd, unsigned short mapindex, int32 x, int32 y, clr_type clrtype)
  5915. {
  5916. nullpo_retr(SETPOS_OK,sd);
  5917. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5918. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5919. return SETPOS_MAPINDEX;
  5920. }
  5921. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5922. return SETPOS_AUTOTRADE;
  5923. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5924. pc_setstand(sd, true);
  5925. pc_setrestartvalue(sd,1);
  5926. }
  5927. int16 m = map_mapindex2mapid(mapindex);
  5928. struct map_data *mapdata = map_getmapdata(m);
  5929. status_change *sc = status_get_sc(&sd->bl);
  5930. sd->state.changemap = (sd->mapindex != mapindex);
  5931. sd->state.warping = 1;
  5932. sd->state.workinprogress = WIP_DISABLE_NONE;
  5933. sd->state.mail_writing = false;
  5934. sd->state.refineui_open = false;
  5935. if( sd->state.changemap ) { // Misc map-changing settings
  5936. int32 curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5937. if (curr_map_instance_id != new_map_instance_id) {
  5938. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5939. instance_delusers(curr_map_instance_id);
  5940. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5941. }
  5942. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5943. instance_addusers(new_map_instance_id);
  5944. }
  5945. if (sd->bg_id && mapdata && !mapdata->getMapFlag(MF_BATTLEGROUND)) // Moving to a map that isn't a Battlegrounds
  5946. bg_team_leave(sd, false, true);
  5947. sd->state.pmap = sd->bl.m;
  5948. if (sc != nullptr && !sc->empty()) { // Cancel some map related stuff.
  5949. if (sc->cant.warp)
  5950. return SETPOS_MAPINDEX; // You may not get out!
  5951. for (const auto &it : status_db) {
  5952. if (sc->getSCE(it.first)) {
  5953. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5954. status_change_end(&sd->bl, static_cast<sc_type>(it.first));
  5955. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5956. status_change_entry *sce = sd->sc.getSCE(it.first);
  5957. if (sce->timer != INVALID_TIMER)
  5958. delete_timer(sce->timer, status_change_timer);
  5959. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5960. }
  5961. }
  5962. }
  5963. }
  5964. for(int32 i = 0; i < EQI_MAX; i++ ) {
  5965. if( sd->equip_index[i] >= 0 )
  5966. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  5967. pc_unequipitem(sd,sd->equip_index[i],2);
  5968. }
  5969. }
  5970. if (battle_config.clear_unit_onwarp&BL_PC)
  5971. skill_clear_unitgroup(&sd->bl);
  5972. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5973. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5974. }
  5975. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5976. guild_send_dot_remove(sd);
  5977. bg_send_dot_remove(sd);
  5978. if (sd->regen.state.gc)
  5979. sd->regen.state.gc = 0;
  5980. if (mapdata) {
  5981. // make sure vending is allowed here
  5982. if (sd->state.vending && mapdata->getMapFlag(MF_NOVENDING)) {
  5983. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5984. vending_closevending(sd);
  5985. }
  5986. // make sure buyingstore is allowed here
  5987. if (sd->state.buyingstore && mapdata->getMapFlag(MF_NOBUYINGSTORE)) {
  5988. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5989. buyingstore_close(sd);
  5990. }
  5991. }
  5992. channel_pcquit(sd,4); //quit map chan
  5993. // Remove Cloaked NPCs on map change
  5994. sd->cloaked_npc.clear();
  5995. }
  5996. if( m < 0 )
  5997. {
  5998. uint32 ip;
  5999. uint16 port;
  6000. struct script_state *st;
  6001. //if can't find any map-servers, just abort setting position.
  6002. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  6003. return SETPOS_NO_MAPSERVER;
  6004. if (sd->npc_id){
  6005. npc_event_dequeue(sd,false);
  6006. st = sd->st;
  6007. }else{
  6008. st = nullptr;
  6009. }
  6010. if (sd->bg_id) // Switching map servers, remove from bg
  6011. bg_team_leave(sd, false, true);
  6012. if (sd->state.vending) // Stop vending
  6013. vending_closevending(sd);
  6014. if (sd->state.buyingstore) // Stop buyingstore
  6015. buyingstore_close(sd);
  6016. npc_script_event( *sd, NPCE_LOGOUT );
  6017. //remove from map, THEN change x/y coordinates
  6018. unit_remove_map_pc(sd,clrtype);
  6019. sd->mapindex = mapindex;
  6020. sd->bl.x=x;
  6021. sd->bl.y=y;
  6022. pc_clean_skilltree(sd);
  6023. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  6024. chrif_changemapserver(sd, ip, (short)port);
  6025. //Free session data from this map server [Kevin]
  6026. unit_free_pc(sd);
  6027. if( st ){
  6028. // Has to be done here, because otherwise unit_free_pc will free the stack already
  6029. st->state = END;
  6030. }
  6031. return SETPOS_OK;
  6032. }
  6033. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  6034. {
  6035. 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);
  6036. x = y = 0; // make it random
  6037. }
  6038. if( x == 0 && y == 0 ) { // pick a random walkable cell
  6039. int32 c=0;
  6040. do {
  6041. x = rnd()%(mapdata->xs-2)+1;
  6042. y = rnd()%(mapdata->ys-2)+1;
  6043. c++;
  6044. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  6045. 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));
  6046. return SETPOS_OK; //preventing warp
  6047. //break; //allow warp anyway
  6048. }
  6049. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  6050. }
  6051. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  6052. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  6053. vending_closevending(sd);
  6054. }
  6055. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  6056. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  6057. buyingstore_close(sd);
  6058. }
  6059. if(sd->bl.prev != nullptr){
  6060. unit_remove_map_pc(sd,clrtype);
  6061. clif_changemap( *sd, m, x, y );
  6062. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  6063. sd->state.rewarp = 1;
  6064. sd->mapindex = mapindex;
  6065. sd->bl.m = m;
  6066. sd->bl.x = sd->ud.to_x = x;
  6067. sd->bl.y = sd->ud.to_y = y;
  6068. if( sd->status.guild_id > 0 && mapdata->getMapFlag(MF_GVG_CASTLE) )
  6069. { // Increased guild castle regen [Valaris]
  6070. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  6071. if(gc && gc->guild_id == sd->status.guild_id)
  6072. sd->regen.state.gc = 1;
  6073. }
  6074. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  6075. {
  6076. sd->pd->bl.m = m;
  6077. sd->pd->bl.x = sd->pd->ud.to_x = x;
  6078. sd->pd->bl.y = sd->pd->ud.to_y = y;
  6079. sd->pd->ud.dir = sd->ud.dir;
  6080. }
  6081. if( hom_is_active(sd->hd) )
  6082. {
  6083. if (sd->state.changemap)
  6084. status_db.removeByStatusFlag(&sd->hd->bl, { SCF_REMOVEFROMHOMONMAPWARP });
  6085. else
  6086. status_db.removeByStatusFlag(&sd->hd->bl, { SCF_REMOVEFROMHOMONWARP });
  6087. if (battle_config.hom_delay_reset_warp) {
  6088. sd->hd->blockskill.clear();
  6089. sd->hd->blockskill.shrink_to_fit();
  6090. }
  6091. sd->hd->bl.m = m;
  6092. sd->hd->bl.x = sd->hd->ud.to_x = x;
  6093. sd->hd->bl.y = sd->hd->ud.to_y = y;
  6094. sd->hd->ud.dir = sd->ud.dir;
  6095. }
  6096. if( sd->md )
  6097. {
  6098. sd->md->bl.m = m;
  6099. sd->md->bl.x = sd->md->ud.to_x = x;
  6100. sd->md->bl.y = sd->md->ud.to_y = y;
  6101. sd->md->ud.dir = sd->ud.dir;
  6102. }
  6103. if( sd->ed ) {
  6104. sd->ed->bl.m = m;
  6105. sd->ed->bl.x = sd->ed->ud.to_x = x;
  6106. sd->ed->bl.y = sd->ed->ud.to_y = y;
  6107. sd->ed->ud.dir = sd->ud.dir;
  6108. }
  6109. pc_cell_basilica(sd);
  6110. //check if we gonna be rewarped [lighta]
  6111. if(npc_check_areanpc(1,m,x,y,0)){
  6112. sd->count_rewarp++;
  6113. }
  6114. else
  6115. sd->count_rewarp = 0;
  6116. if (sd->state.vending)
  6117. vending_update(*sd);
  6118. if (sd->state.buyingstore)
  6119. buyingstore_update(*sd);
  6120. return SETPOS_OK;
  6121. }
  6122. enum e_setpos pc_setpos_savepoint( map_session_data& sd, clr_type clrtype ){
  6123. struct map_data *mapdata = map_getmapdata( sd.bl.m );
  6124. if( mapdata != nullptr && mapdata->getMapFlag(MF_NOSAVE) && mapdata->save.map ){
  6125. return pc_setpos( &sd, mapdata->save.map, mapdata->save.x, mapdata->save.y, clrtype );
  6126. }else{
  6127. return pc_setpos( &sd, mapindex_name2id( sd.status.save_point.map ), sd.status.save_point.x, sd.status.save_point.y, clrtype );
  6128. }
  6129. }
  6130. /*==========================================
  6131. * Warp player sd to random location on current map.
  6132. * May fail if no walkable cell found (1000 attempts).
  6133. * Return:
  6134. * 0 = Success
  6135. * 1,2,3 = Fail
  6136. *------------------------------------------*/
  6137. char pc_randomwarp(map_session_data *sd, clr_type type, bool ignore_mapflag)
  6138. {
  6139. int16 x,y,i=0;
  6140. nullpo_ret(sd);
  6141. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6142. if (mapdata->getMapFlag(MF_NOTELEPORT) && !ignore_mapflag) //Teleport forbidden
  6143. return 3;
  6144. int32 edge = battle_config.map_edge_size;
  6145. do {
  6146. x = rnd_value<int16>(edge, mapdata->xs - edge - 1);
  6147. y = rnd_value<int16>(edge, mapdata->ys - edge - 1);
  6148. } 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);
  6149. if (i < 1000)
  6150. return pc_setpos(sd,mapdata->index,x,y,type);
  6151. return 3;
  6152. }
  6153. /*==========================================
  6154. * Records a memo point at sd's current position
  6155. * pos - entry to replace, (-1: shift oldest entry out)
  6156. *------------------------------------------*/
  6157. bool pc_memo(map_session_data* sd, int32 pos)
  6158. {
  6159. int32 skill;
  6160. nullpo_ret(sd);
  6161. // check mapflags
  6162. 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) ) {
  6163. clif_skill_teleportmessage( *sd, NOTIFY_MAPINFO_CANT_MEMO ); // "Saved point cannot be memorized."
  6164. return false;
  6165. }
  6166. // check inputs
  6167. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  6168. return false; // invalid input
  6169. // check required skill level
  6170. skill = pc_checkskill(sd, AL_WARP);
  6171. if( skill < 1 ) {
  6172. clif_skill_memomessage( *sd, WARPPOINT_NOT_LEARNED ); // "You haven't learned Warp."
  6173. return false;
  6174. }
  6175. if( skill < 2 || skill - 2 < pos ) {
  6176. clif_skill_memomessage( *sd, WARPPOINT_LOW_LEVEL ); // "Skill Level is not high enough."
  6177. return false;
  6178. }
  6179. if( pos == -1 )
  6180. {
  6181. uint8 i;
  6182. const char* mapname = map_mapid2mapname( sd->bl.m );
  6183. // prevent memo-ing the same map multiple times
  6184. ARR_FIND( 0, MAX_MEMOPOINTS, i, strncmp( sd->status.memo_point[i].map, mapname, sizeof( sd->status.memo_point[i].map ) ) == 0 );
  6185. memmove( &sd->status.memo_point[1], &sd->status.memo_point[0], ( u8min( i, MAX_MEMOPOINTS - 1 ) ) * sizeof( struct s_point_str ) );
  6186. pos = 0;
  6187. }
  6188. if( map_getmapdata(sd->bl.m)->instance_id ) {
  6189. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  6190. return false;
  6191. }
  6192. safestrncpy( sd->status.memo_point[pos].map, map_mapid2mapname( sd->bl.m ), sizeof( sd->status.memo_point[pos].map ) );
  6193. sd->status.memo_point[pos].x = sd->bl.x;
  6194. sd->status.memo_point[pos].y = sd->bl.y;
  6195. clif_skill_memomessage( *sd, WARPPOINT_SUCCESS );
  6196. return true;
  6197. }
  6198. //
  6199. // Skills
  6200. //
  6201. /**
  6202. * Get the skill current cooldown for player.
  6203. * (get the db base cooldown for skill + player specific cooldown)
  6204. * @param sd : player pointer
  6205. * @param id : skill id
  6206. * @param lv : skill lv
  6207. * @return player skill cooldown
  6208. */
  6209. int32 pc_get_skillcooldown(map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  6210. if (skill_id == SJ_NOVAEXPLOSING) {
  6211. status_change *sc = status_get_sc(&sd->bl);
  6212. if (sc && sc->getSCE(SC_DIMENSION))
  6213. return 0;
  6214. }
  6215. int32 cooldown = skill_get_cooldown(skill_id, skill_lv);
  6216. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  6217. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  6218. for (auto &it : sd->skillcooldown) {
  6219. if (it.id == skill_id) {
  6220. cooldown += it.val;
  6221. break;
  6222. }
  6223. }
  6224. return max(0, cooldown);
  6225. }
  6226. /*==========================================
  6227. * Return player sd skill_lv learned for given skill
  6228. *------------------------------------------*/
  6229. uint8 pc_checkskill(map_session_data *sd, uint16 skill_id)
  6230. {
  6231. uint16 idx = 0;
  6232. if (sd == nullptr)
  6233. return 0;
  6234. #ifdef RENEWAL
  6235. if ((idx = skill_get_index(skill_id)) == 0) {
  6236. #else
  6237. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  6238. if( skill_id >= RK_ENCHANTBLADE ){
  6239. // Silently fail for now -> future update planned
  6240. return 0;
  6241. }
  6242. #endif
  6243. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  6244. return 0;
  6245. }
  6246. if (SKILL_CHK_GUILD(skill_id) ) {
  6247. if (sd->status.guild_id>0 && sd->guild)
  6248. return guild_checkskill(sd->guild->guild,skill_id);
  6249. return 0;
  6250. }
  6251. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  6252. }
  6253. /**
  6254. * Returns the flag of the given skill (when learned).
  6255. * @param sd: Player data
  6256. * @param skill_id: Skill to lookup
  6257. * @return Skill flag type
  6258. */
  6259. e_skill_flag pc_checkskill_flag(map_session_data &sd, uint16 skill_id) {
  6260. uint16 idx;
  6261. #ifdef RENEWAL
  6262. if ((idx = skill_get_index(skill_id)) == 0) {
  6263. #else
  6264. if ((idx = skill_db.get_index(skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__)) == 0) {
  6265. if (skill_id >= RK_ENCHANTBLADE) {
  6266. // Silently fail for now -> future update planned
  6267. return SKILL_FLAG_NONE;
  6268. }
  6269. #endif
  6270. ShowError("pc_checkskill_flag: Invalid skill id %d (char_id=%d).\n", skill_id, sd.status.char_id);
  6271. return SKILL_FLAG_NONE;
  6272. }
  6273. 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;
  6274. }
  6275. /**
  6276. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  6277. * @param sd: Player data
  6278. * @param type: Summoner Power Type
  6279. * @return Skill points invested
  6280. */
  6281. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  6282. if (sd == nullptr)
  6283. return 0;
  6284. uint8 count = 0;
  6285. switch (type) {
  6286. case SUMMONER_POWER_SEA:
  6287. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  6288. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  6289. break;
  6290. case SUMMONER_POWER_LAND:
  6291. 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) +
  6292. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  6293. break;
  6294. case SUMMONER_POWER_LIFE:
  6295. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  6296. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  6297. break;
  6298. }
  6299. return count;
  6300. }
  6301. /**
  6302. * Checks for Imperial Guard's passive skills.
  6303. * @param sd: Player data
  6304. * @param flag:
  6305. * Flag&1 = IG_SHIELD_MASTERY
  6306. * Flag&2 = IG_SPEAR_SWORD_M
  6307. */
  6308. uint8 pc_checkskill_imperial_guard(map_session_data *sd, short flag)
  6309. {
  6310. nullpo_retr(0, sd);
  6311. uint8 count = 0;
  6312. if (flag&1 && sd->status.shield > 0)
  6313. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  6314. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  6315. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  6316. return count;
  6317. }
  6318. /**
  6319. * Check if we still have the correct weapon to continue the skill (actually status)
  6320. * If not ending it
  6321. * @param sd
  6322. * @return 0:error, 1:check done
  6323. */
  6324. static void pc_checkallowskill(map_session_data *sd)
  6325. {
  6326. nullpo_retv(sd);
  6327. if(sd->sc.empty())
  6328. return;
  6329. for (const auto &it : status_db) {
  6330. sc_type status = it.second->type;
  6331. std::bitset<SCF_MAX> flag = it.second->flag;
  6332. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  6333. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  6334. continue;
  6335. if (sd->sc.getSCE(status) && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  6336. status_change_end(&sd->bl, status);
  6337. }
  6338. if (flag[SCF_REQUIRENOWEAPON]) {
  6339. if (sd->sc.getSCE(status) && sd->status.weapon)
  6340. status_change_end(&sd->bl, status, INVALID_TIMER);
  6341. }
  6342. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  6343. if (sd->sc.getSCE(status) && sd->status.shield <= 0)
  6344. status_change_end(&sd->bl, status);
  6345. }
  6346. }
  6347. }
  6348. /*==========================================
  6349. * Return equipped index of item on player sd at pos
  6350. * Return
  6351. * -1 : Nothing equipped
  6352. * idx : (this index could be used in inventory to found item_data)
  6353. *------------------------------------------*/
  6354. short pc_checkequip(map_session_data *sd,int32 pos, bool checkall)
  6355. {
  6356. uint8 i;
  6357. nullpo_retr(-1, sd);
  6358. for(i=0;i<EQI_MAX;i++){
  6359. if(pos & equip_bitmask[i]){
  6360. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  6361. // Check all if any match is found
  6362. continue;
  6363. }
  6364. return sd->equip_index[i];
  6365. }
  6366. }
  6367. return -1;
  6368. }
  6369. /*==========================================
  6370. * Check if sd has nameid equipped somewhere
  6371. * @sd : the player session
  6372. * @nameid : id of the item to check
  6373. * @min : : see pc.hpp enum equip_index from ? to @max
  6374. * @max : see pc.hpp enum equip_index for @min to ?
  6375. * -return true,false
  6376. *------------------------------------------*/
  6377. bool pc_checkequip2(map_session_data *sd, t_itemid nameid, int32 min, int32 max)
  6378. {
  6379. int32 i;
  6380. for(i = min; i < max; i++) {
  6381. if(equip_bitmask[i]) {
  6382. int32 idx = sd->equip_index[i];
  6383. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  6384. return true;
  6385. }
  6386. }
  6387. return false;
  6388. }
  6389. /*==========================================
  6390. * Convert's from the client's lame Job ID system
  6391. * to the map server's 'makes sense' system. [Skotlex]
  6392. *------------------------------------------*/
  6393. uint64 pc_jobid2mapid(unsigned short b_class)
  6394. {
  6395. switch(b_class)
  6396. {
  6397. //Novice And 1-1 Jobs
  6398. case JOB_NOVICE: return MAPID_NOVICE;
  6399. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  6400. case JOB_MAGE: return MAPID_MAGE;
  6401. case JOB_ARCHER: return MAPID_ARCHER;
  6402. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  6403. case JOB_MERCHANT: return MAPID_MERCHANT;
  6404. case JOB_THIEF: return MAPID_THIEF;
  6405. case JOB_TAEKWON: return MAPID_TAEKWON;
  6406. case JOB_WEDDING: return MAPID_WEDDING;
  6407. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  6408. case JOB_NINJA: return MAPID_NINJA;
  6409. case JOB_XMAS: return MAPID_XMAS;
  6410. case JOB_SUMMER: return MAPID_SUMMER;
  6411. case JOB_HANBOK: return MAPID_HANBOK;
  6412. case JOB_GANGSI: return MAPID_GANGSI;
  6413. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  6414. case JOB_SUMMER2: return MAPID_SUMMER2;
  6415. //2-1 Jobs
  6416. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  6417. case JOB_KNIGHT: return MAPID_KNIGHT;
  6418. case JOB_WIZARD: return MAPID_WIZARD;
  6419. case JOB_HUNTER: return MAPID_HUNTER;
  6420. case JOB_PRIEST: return MAPID_PRIEST;
  6421. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  6422. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  6423. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  6424. case JOB_KAGEROU:
  6425. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  6426. case JOB_REBELLION: return MAPID_REBELLION;
  6427. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  6428. //2-2 Jobs
  6429. case JOB_CRUSADER: return MAPID_CRUSADER;
  6430. case JOB_SAGE: return MAPID_SAGE;
  6431. case JOB_BARD:
  6432. case JOB_DANCER: return MAPID_BARDDANCER;
  6433. case JOB_MONK: return MAPID_MONK;
  6434. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  6435. case JOB_ROGUE: return MAPID_ROGUE;
  6436. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  6437. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  6438. //Trans Novice And Trans 1-1 Jobs
  6439. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  6440. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  6441. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  6442. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  6443. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  6444. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  6445. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  6446. //Trans 2-1 Jobs
  6447. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  6448. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  6449. case JOB_SNIPER: return MAPID_SNIPER;
  6450. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  6451. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6452. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6453. //Trans 2-2 Jobs
  6454. case JOB_PALADIN: return MAPID_PALADIN;
  6455. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6456. case JOB_CLOWN:
  6457. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6458. case JOB_CHAMPION: return MAPID_CHAMPION;
  6459. case JOB_CREATOR: return MAPID_CREATOR;
  6460. case JOB_STALKER: return MAPID_STALKER;
  6461. //Baby Novice And Baby 1-1 Jobs
  6462. case JOB_BABY: return MAPID_BABY;
  6463. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6464. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6465. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6466. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6467. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6468. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6469. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6470. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6471. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6472. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6473. //Baby 2-1 Jobs
  6474. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6475. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6476. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6477. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6478. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6479. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6480. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6481. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6482. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6483. case JOB_BABY_KAGEROU:
  6484. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6485. //Baby 2-2 Jobs
  6486. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6487. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6488. case JOB_BABY_BARD:
  6489. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6490. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6491. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6492. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6493. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6494. //3-1 Jobs
  6495. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6496. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6497. case JOB_WARLOCK: return MAPID_WARLOCK;
  6498. case JOB_RANGER: return MAPID_RANGER;
  6499. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6500. case JOB_MECHANIC: return MAPID_MECHANIC;
  6501. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6502. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6503. //3-2 Jobs
  6504. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6505. case JOB_SORCERER: return MAPID_SORCERER;
  6506. case JOB_MINSTREL:
  6507. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6508. case JOB_SURA: return MAPID_SURA;
  6509. case JOB_GENETIC: return MAPID_GENETIC;
  6510. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6511. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6512. //Trans 3-1 Jobs
  6513. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6514. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6515. case JOB_RANGER_T: return MAPID_RANGER_T;
  6516. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6517. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6518. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6519. //Trans 3-2 Jobs
  6520. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6521. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6522. case JOB_MINSTREL_T:
  6523. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6524. case JOB_SURA_T: return MAPID_SURA_T;
  6525. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6526. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6527. //Baby 3-1 Jobs
  6528. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6529. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6530. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6531. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6532. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6533. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6534. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6535. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6536. //Baby 3-2 Jobs
  6537. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6538. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6539. case JOB_BABY_MINSTREL:
  6540. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6541. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6542. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6543. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6544. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6545. //Doram Jobs
  6546. case JOB_SUMMONER: return MAPID_SUMMONER;
  6547. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6548. //4-1 Jobs
  6549. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6550. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6551. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6552. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6553. case JOB_CARDINAL: return MAPID_CARDINAL;
  6554. case JOB_MEISTER: return MAPID_MEISTER;
  6555. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6556. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6557. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6558. case JOB_SHINKIRO:
  6559. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6560. //4-2 Jobs
  6561. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6562. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6563. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6564. case JOB_TROUBADOUR:
  6565. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6566. case JOB_BIOLO: return MAPID_BIOLO;
  6567. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6568. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6569. //Unknown
  6570. default:
  6571. return -1;
  6572. }
  6573. }
  6574. //Reverts the map-style class id to the client-style one.
  6575. int32 pc_mapid2jobid(uint64 class_, int32 sex)
  6576. {
  6577. switch(class_) {
  6578. //Novice And 1-1 Jobs
  6579. case MAPID_NOVICE: return JOB_NOVICE;
  6580. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6581. case MAPID_MAGE: return JOB_MAGE;
  6582. case MAPID_ARCHER: return JOB_ARCHER;
  6583. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6584. case MAPID_MERCHANT: return JOB_MERCHANT;
  6585. case MAPID_THIEF: return JOB_THIEF;
  6586. case MAPID_TAEKWON: return JOB_TAEKWON;
  6587. case MAPID_WEDDING: return JOB_WEDDING;
  6588. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6589. case MAPID_NINJA: return JOB_NINJA;
  6590. case MAPID_XMAS: return JOB_XMAS;
  6591. case MAPID_SUMMER: return JOB_SUMMER;
  6592. case MAPID_HANBOK: return JOB_HANBOK;
  6593. case MAPID_GANGSI: return JOB_GANGSI;
  6594. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6595. case MAPID_SUMMER2: return JOB_SUMMER2;
  6596. //2-1 Jobs
  6597. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6598. case MAPID_KNIGHT: return JOB_KNIGHT;
  6599. case MAPID_WIZARD: return JOB_WIZARD;
  6600. case MAPID_HUNTER: return JOB_HUNTER;
  6601. case MAPID_PRIEST: return JOB_PRIEST;
  6602. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6603. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6604. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6605. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6606. case MAPID_REBELLION: return JOB_REBELLION;
  6607. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6608. //2-2 Jobs
  6609. case MAPID_CRUSADER: return JOB_CRUSADER;
  6610. case MAPID_SAGE: return JOB_SAGE;
  6611. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6612. case MAPID_MONK: return JOB_MONK;
  6613. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6614. case MAPID_ROGUE: return JOB_ROGUE;
  6615. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6616. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6617. //Trans Novice And Trans 2-1 Jobs
  6618. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6619. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6620. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6621. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6622. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6623. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6624. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6625. //Trans 2-1 Jobs
  6626. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6627. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6628. case MAPID_SNIPER: return JOB_SNIPER;
  6629. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6630. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6631. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6632. //Trans 2-2 Jobs
  6633. case MAPID_PALADIN: return JOB_PALADIN;
  6634. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6635. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6636. case MAPID_CHAMPION: return JOB_CHAMPION;
  6637. case MAPID_CREATOR: return JOB_CREATOR;
  6638. case MAPID_STALKER: return JOB_STALKER;
  6639. //Baby Novice And Baby 1-1 Jobs
  6640. case MAPID_BABY: return JOB_BABY;
  6641. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6642. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6643. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6644. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6645. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6646. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6647. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6648. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6649. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6650. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6651. //Baby 2-1 Jobs
  6652. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6653. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6654. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6655. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6656. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6657. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6658. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6659. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6660. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6661. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6662. //Baby 2-2 Jobs
  6663. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6664. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6665. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6666. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6667. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6668. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6669. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6670. //3-1 Jobs
  6671. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6672. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6673. case MAPID_WARLOCK: return JOB_WARLOCK;
  6674. case MAPID_RANGER: return JOB_RANGER;
  6675. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6676. case MAPID_MECHANIC: return JOB_MECHANIC;
  6677. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6678. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6679. //3-2 Jobs
  6680. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6681. case MAPID_SORCERER: return JOB_SORCERER;
  6682. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6683. case MAPID_SURA: return JOB_SURA;
  6684. case MAPID_GENETIC: return JOB_GENETIC;
  6685. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6686. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6687. //Trans 3-1 Jobs
  6688. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6689. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6690. case MAPID_RANGER_T: return JOB_RANGER_T;
  6691. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6692. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6693. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6694. //Trans 3-2 Jobs
  6695. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6696. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6697. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6698. case MAPID_SURA_T: return JOB_SURA_T;
  6699. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6700. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6701. //Baby 3-1 Jobs
  6702. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6703. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6704. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6705. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6706. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6707. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6708. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6709. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6710. //Baby 3-2 Jobs
  6711. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6712. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6713. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6714. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6715. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6716. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6717. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6718. //Doram Jobs
  6719. case MAPID_SUMMONER: return JOB_SUMMONER;
  6720. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6721. //4-1 Jobs
  6722. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6723. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6724. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6725. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6726. case MAPID_CARDINAL: return JOB_CARDINAL;
  6727. case MAPID_MEISTER: return JOB_MEISTER;
  6728. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6729. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6730. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6731. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6732. //4-2 Jobs
  6733. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6734. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6735. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6736. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6737. case MAPID_BIOLO: return JOB_BIOLO;
  6738. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6739. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6740. //Unknown
  6741. default:
  6742. return -1;
  6743. }
  6744. }
  6745. /*====================================================
  6746. * This function return the name of the job (by [Yor])
  6747. *----------------------------------------------------*/
  6748. const char* job_name(int32 class_)
  6749. {
  6750. switch (class_) {
  6751. case JOB_NOVICE:
  6752. case JOB_SWORDMAN:
  6753. case JOB_MAGE:
  6754. case JOB_ARCHER:
  6755. case JOB_ACOLYTE:
  6756. case JOB_MERCHANT:
  6757. case JOB_THIEF:
  6758. return msg_txt(nullptr,550 - JOB_NOVICE+class_);
  6759. case JOB_KNIGHT:
  6760. case JOB_PRIEST:
  6761. case JOB_WIZARD:
  6762. case JOB_BLACKSMITH:
  6763. case JOB_HUNTER:
  6764. case JOB_ASSASSIN:
  6765. return msg_txt(nullptr,557 - JOB_KNIGHT+class_);
  6766. case JOB_KNIGHT2:
  6767. return msg_txt(nullptr,557);
  6768. case JOB_CRUSADER:
  6769. case JOB_MONK:
  6770. case JOB_SAGE:
  6771. case JOB_ROGUE:
  6772. case JOB_ALCHEMIST:
  6773. case JOB_BARD:
  6774. case JOB_DANCER:
  6775. return msg_txt(nullptr,563 - JOB_CRUSADER+class_);
  6776. case JOB_CRUSADER2:
  6777. return msg_txt(nullptr,563);
  6778. case JOB_WEDDING:
  6779. case JOB_SUPER_NOVICE:
  6780. case JOB_GUNSLINGER:
  6781. case JOB_NINJA:
  6782. case JOB_XMAS:
  6783. return msg_txt(nullptr,570 - JOB_WEDDING+class_);
  6784. case JOB_SUMMER:
  6785. case JOB_SUMMER2:
  6786. return msg_txt(nullptr,621);
  6787. case JOB_HANBOK:
  6788. return msg_txt(nullptr,694);
  6789. case JOB_OKTOBERFEST:
  6790. return msg_txt(nullptr,696);
  6791. case JOB_NOVICE_HIGH:
  6792. case JOB_SWORDMAN_HIGH:
  6793. case JOB_MAGE_HIGH:
  6794. case JOB_ARCHER_HIGH:
  6795. case JOB_ACOLYTE_HIGH:
  6796. case JOB_MERCHANT_HIGH:
  6797. case JOB_THIEF_HIGH:
  6798. return msg_txt(nullptr,575 - JOB_NOVICE_HIGH+class_);
  6799. case JOB_LORD_KNIGHT:
  6800. case JOB_HIGH_PRIEST:
  6801. case JOB_HIGH_WIZARD:
  6802. case JOB_WHITESMITH:
  6803. case JOB_SNIPER:
  6804. case JOB_ASSASSIN_CROSS:
  6805. return msg_txt(nullptr,582 - JOB_LORD_KNIGHT+class_);
  6806. case JOB_LORD_KNIGHT2:
  6807. return msg_txt(nullptr,582);
  6808. case JOB_PALADIN:
  6809. case JOB_CHAMPION:
  6810. case JOB_PROFESSOR:
  6811. case JOB_STALKER:
  6812. case JOB_CREATOR:
  6813. case JOB_CLOWN:
  6814. case JOB_GYPSY:
  6815. return msg_txt(nullptr,588 - JOB_PALADIN + class_);
  6816. case JOB_PALADIN2:
  6817. return msg_txt(nullptr,588);
  6818. case JOB_BABY:
  6819. case JOB_BABY_SWORDMAN:
  6820. case JOB_BABY_MAGE:
  6821. case JOB_BABY_ARCHER:
  6822. case JOB_BABY_ACOLYTE:
  6823. case JOB_BABY_MERCHANT:
  6824. case JOB_BABY_THIEF:
  6825. return msg_txt(nullptr,595 - JOB_BABY + class_);
  6826. case JOB_BABY_KNIGHT:
  6827. case JOB_BABY_PRIEST:
  6828. case JOB_BABY_WIZARD:
  6829. case JOB_BABY_BLACKSMITH:
  6830. case JOB_BABY_HUNTER:
  6831. case JOB_BABY_ASSASSIN:
  6832. return msg_txt(nullptr,602 - JOB_BABY_KNIGHT + class_);
  6833. case JOB_BABY_KNIGHT2:
  6834. return msg_txt(nullptr,602);
  6835. case JOB_BABY_CRUSADER:
  6836. case JOB_BABY_MONK:
  6837. case JOB_BABY_SAGE:
  6838. case JOB_BABY_ROGUE:
  6839. case JOB_BABY_ALCHEMIST:
  6840. case JOB_BABY_BARD:
  6841. case JOB_BABY_DANCER:
  6842. return msg_txt(nullptr,608 - JOB_BABY_CRUSADER + class_);
  6843. case JOB_BABY_CRUSADER2:
  6844. return msg_txt(nullptr,608);
  6845. case JOB_SUPER_BABY:
  6846. return msg_txt(nullptr,615);
  6847. case JOB_TAEKWON:
  6848. return msg_txt(nullptr,616);
  6849. case JOB_STAR_GLADIATOR:
  6850. case JOB_STAR_GLADIATOR2:
  6851. return msg_txt(nullptr,617);
  6852. case JOB_SOUL_LINKER:
  6853. return msg_txt(nullptr,618);
  6854. case JOB_GANGSI:
  6855. case JOB_DEATH_KNIGHT:
  6856. case JOB_DARK_COLLECTOR:
  6857. return msg_txt(nullptr,622 - JOB_GANGSI+class_);
  6858. case JOB_RUNE_KNIGHT:
  6859. case JOB_WARLOCK:
  6860. case JOB_RANGER:
  6861. case JOB_ARCH_BISHOP:
  6862. case JOB_MECHANIC:
  6863. case JOB_GUILLOTINE_CROSS:
  6864. return msg_txt(nullptr,625 - JOB_RUNE_KNIGHT+class_);
  6865. case JOB_RUNE_KNIGHT_T:
  6866. case JOB_WARLOCK_T:
  6867. case JOB_RANGER_T:
  6868. case JOB_ARCH_BISHOP_T:
  6869. case JOB_MECHANIC_T:
  6870. case JOB_GUILLOTINE_CROSS_T:
  6871. return msg_txt(nullptr,681 - JOB_RUNE_KNIGHT_T+class_);
  6872. case JOB_ROYAL_GUARD:
  6873. case JOB_SORCERER:
  6874. case JOB_MINSTREL:
  6875. case JOB_WANDERER:
  6876. case JOB_SURA:
  6877. case JOB_GENETIC:
  6878. case JOB_SHADOW_CHASER:
  6879. return msg_txt(nullptr,631 - JOB_ROYAL_GUARD+class_);
  6880. case JOB_ROYAL_GUARD_T:
  6881. case JOB_SORCERER_T:
  6882. case JOB_MINSTREL_T:
  6883. case JOB_WANDERER_T:
  6884. case JOB_SURA_T:
  6885. case JOB_GENETIC_T:
  6886. case JOB_SHADOW_CHASER_T:
  6887. return msg_txt(nullptr,687 - JOB_ROYAL_GUARD_T+class_);
  6888. case JOB_RUNE_KNIGHT2:
  6889. case JOB_RUNE_KNIGHT_T2:
  6890. return msg_txt(nullptr,625);
  6891. case JOB_ROYAL_GUARD2:
  6892. case JOB_ROYAL_GUARD_T2:
  6893. return msg_txt(nullptr,631);
  6894. case JOB_RANGER2:
  6895. case JOB_RANGER_T2:
  6896. return msg_txt(nullptr,627);
  6897. case JOB_MECHANIC2:
  6898. case JOB_MECHANIC_T2:
  6899. return msg_txt(nullptr,629);
  6900. case JOB_BABY_RUNE_KNIGHT:
  6901. case JOB_BABY_WARLOCK:
  6902. case JOB_BABY_RANGER:
  6903. case JOB_BABY_ARCH_BISHOP:
  6904. case JOB_BABY_MECHANIC:
  6905. case JOB_BABY_GUILLOTINE_CROSS:
  6906. case JOB_BABY_ROYAL_GUARD:
  6907. case JOB_BABY_SORCERER:
  6908. case JOB_BABY_MINSTREL:
  6909. case JOB_BABY_WANDERER:
  6910. case JOB_BABY_SURA:
  6911. case JOB_BABY_GENETIC:
  6912. case JOB_BABY_SHADOW_CHASER:
  6913. return msg_txt(nullptr,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6914. case JOB_BABY_RUNE_KNIGHT2:
  6915. return msg_txt(nullptr,638);
  6916. case JOB_BABY_ROYAL_GUARD2:
  6917. return msg_txt(nullptr,644);
  6918. case JOB_BABY_RANGER2:
  6919. return msg_txt(nullptr,640);
  6920. case JOB_BABY_MECHANIC2:
  6921. return msg_txt(nullptr,642);
  6922. case JOB_SUPER_NOVICE_E:
  6923. case JOB_SUPER_BABY_E:
  6924. return msg_txt(nullptr,651 - JOB_SUPER_NOVICE_E+class_);
  6925. case JOB_KAGEROU:
  6926. case JOB_OBORO:
  6927. return msg_txt(nullptr,653 - JOB_KAGEROU+class_);
  6928. case JOB_REBELLION:
  6929. return msg_txt(nullptr,695);
  6930. case JOB_SUMMONER:
  6931. return msg_txt(nullptr,697);
  6932. case JOB_BABY_SUMMONER:
  6933. return msg_txt(nullptr,698);
  6934. case JOB_BABY_NINJA:
  6935. return msg_txt(nullptr,699);
  6936. case JOB_BABY_KAGEROU:
  6937. case JOB_BABY_OBORO:
  6938. case JOB_BABY_TAEKWON:
  6939. case JOB_BABY_STAR_GLADIATOR:
  6940. case JOB_BABY_SOUL_LINKER:
  6941. case JOB_BABY_GUNSLINGER:
  6942. case JOB_BABY_REBELLION:
  6943. return msg_txt(nullptr,753 - JOB_BABY_KAGEROU+class_);
  6944. case JOB_BABY_STAR_GLADIATOR2:
  6945. return msg_txt(nullptr,756);
  6946. case JOB_STAR_EMPEROR:
  6947. case JOB_SOUL_REAPER:
  6948. case JOB_BABY_STAR_EMPEROR:
  6949. case JOB_BABY_SOUL_REAPER:
  6950. return msg_txt(nullptr,782 - JOB_STAR_EMPEROR + class_);
  6951. case JOB_STAR_EMPEROR2:
  6952. return msg_txt(nullptr,782);
  6953. case JOB_BABY_STAR_EMPEROR2:
  6954. return msg_txt(nullptr,784);
  6955. case JOB_DRAGON_KNIGHT:
  6956. case JOB_MEISTER:
  6957. case JOB_SHADOW_CROSS:
  6958. case JOB_ARCH_MAGE:
  6959. case JOB_CARDINAL:
  6960. case JOB_WINDHAWK:
  6961. case JOB_IMPERIAL_GUARD:
  6962. case JOB_BIOLO:
  6963. case JOB_ABYSS_CHASER:
  6964. case JOB_ELEMENTAL_MASTER:
  6965. case JOB_INQUISITOR:
  6966. case JOB_TROUBADOUR:
  6967. case JOB_TROUVERE:
  6968. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6969. case JOB_WINDHAWK2:
  6970. return msg_txt( nullptr, 805);
  6971. case JOB_MEISTER2:
  6972. return msg_txt( nullptr, 801 );
  6973. case JOB_DRAGON_KNIGHT2:
  6974. return msg_txt( nullptr, 800 );
  6975. case JOB_IMPERIAL_GUARD2:
  6976. return msg_txt( nullptr, 806 );
  6977. case JOB_SKY_EMPEROR:
  6978. case JOB_SOUL_ASCETIC:
  6979. case JOB_SHINKIRO:
  6980. case JOB_SHIRANUI:
  6981. case JOB_NIGHT_WATCH:
  6982. case JOB_HYPER_NOVICE:
  6983. case JOB_SPIRIT_HANDLER:
  6984. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6985. case JOB_SKY_EMPEROR2:
  6986. return msg_txt( nullptr, 813 );
  6987. default:
  6988. return msg_txt(nullptr,655);
  6989. }
  6990. }
  6991. /*====================================================
  6992. * Timered function to make id follow a target.
  6993. * @id = bl.id (player only atm)
  6994. * target is define in sd->followtarget (bl.id)
  6995. * used by pc_follow
  6996. *----------------------------------------------------*/
  6997. TIMER_FUNC(pc_follow_timer){
  6998. map_session_data *sd;
  6999. struct block_list *tbl;
  7000. sd = map_id2sd(id);
  7001. nullpo_ret(sd);
  7002. if (sd->followtimer != tid){
  7003. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  7004. sd->followtimer = INVALID_TIMER;
  7005. return 0;
  7006. }
  7007. sd->followtimer = INVALID_TIMER;
  7008. tbl = map_id2bl(sd->followtarget);
  7009. if (tbl == nullptr || pc_isdead(sd))
  7010. {
  7011. pc_stop_following(sd);
  7012. return 0;
  7013. }
  7014. // either player or target is currently detached from map blocks (could be teleporting),
  7015. // but still connected to this map, so we'll just increment the timer and check back later
  7016. if (sd->bl.prev != nullptr && tbl->prev != nullptr &&
  7017. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  7018. {
  7019. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, nullptr, nullptr)) {
  7020. if (!check_distance_bl(&sd->bl, tbl, 5))
  7021. unit_walktobl(&sd->bl, tbl, 5, 0);
  7022. } else
  7023. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  7024. }
  7025. sd->followtimer = add_timer(
  7026. tick + 1000, // increase time a bit to loosen up map's load
  7027. pc_follow_timer, sd->bl.id, 0);
  7028. return 0;
  7029. }
  7030. int32 pc_stop_following (map_session_data *sd)
  7031. {
  7032. nullpo_ret(sd);
  7033. if (sd->followtimer != INVALID_TIMER) {
  7034. delete_timer(sd->followtimer,pc_follow_timer);
  7035. sd->followtimer = INVALID_TIMER;
  7036. }
  7037. sd->followtarget = -1;
  7038. sd->ud.target_to = 0;
  7039. unit_stop_walking(&sd->bl, 1);
  7040. return 0;
  7041. }
  7042. int32 pc_follow(map_session_data *sd,int32 target_id)
  7043. {
  7044. struct block_list *bl = map_id2bl(target_id);
  7045. if (bl == nullptr /*|| bl->type != BL_PC*/)
  7046. return 1;
  7047. if (sd->followtimer != INVALID_TIMER)
  7048. pc_stop_following(sd);
  7049. sd->followtarget = target_id;
  7050. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  7051. return 0;
  7052. }
  7053. int32 pc_checkbaselevelup(map_session_data *sd) {
  7054. t_exp next = pc_nextbaseexp(sd);
  7055. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  7056. return 0;
  7057. uint32 base_level = sd->status.base_level;
  7058. do {
  7059. sd->status.base_exp -= next;
  7060. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7061. 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 )
  7062. sd->status.base_exp = next-1;
  7063. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  7064. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  7065. sd->status.base_level++;
  7066. if( pc_is_maxbaselv(sd) ){
  7067. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  7068. break;
  7069. }
  7070. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  7071. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  7072. status_calc_pet(sd->pd,SCO_NONE);
  7073. clif_updatestatus(*sd,SP_STATUSPOINT);
  7074. clif_updatestatus(*sd,SP_TRAITPOINT);
  7075. clif_updatestatus(*sd,SP_BASELEVEL);
  7076. clif_updatestatus(*sd,SP_BASEEXP);
  7077. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  7078. status_calc_pc(sd,SCO_FORCE);
  7079. status_percent_heal(&sd->bl,100,100);
  7080. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  7081. for (const auto &status : status_db) {
  7082. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  7083. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, 120000); //All buffs with super novice flag for 2 minutes
  7084. }
  7085. if (sd->state.snovice_dead_flag)
  7086. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  7087. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  7088. for (const auto &status : status_db) {
  7089. if (status.second->flag[SCF_TAEKWONANGEL])
  7090. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  7091. }
  7092. }
  7093. clif_misceffect( sd->bl, NOTIFYEFFECT_BASE_LEVEL_UP );
  7094. npc_script_event( *sd, NPCE_BASELVUP );
  7095. if(sd->status.party_id)
  7096. party_send_levelup(sd);
  7097. pc_baselevelchanged(sd);
  7098. for (; base_level <= sd->status.base_level; base_level++) {
  7099. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  7100. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  7101. }
  7102. return 1;
  7103. }
  7104. void pc_baselevelchanged(map_session_data *sd) {
  7105. uint8 i;
  7106. for( i = 0; i < EQI_MAX; i++ ) {
  7107. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  7108. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (uint32)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  7109. pc_unequipitem(sd, sd->equip_index[i], 3);
  7110. }
  7111. }
  7112. pc_show_questinfo(sd);
  7113. }
  7114. int32 pc_checkjoblevelup(map_session_data *sd)
  7115. {
  7116. t_exp next = pc_nextjobexp(sd);
  7117. nullpo_ret(sd);
  7118. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  7119. return 0;
  7120. uint32 job_level = sd->status.job_level;
  7121. do {
  7122. sd->status.job_exp -= next;
  7123. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  7124. 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 )
  7125. sd->status.job_exp = next-1;
  7126. sd->status.job_level ++;
  7127. sd->status.skill_point ++;
  7128. if( pc_is_maxjoblv(sd) ){
  7129. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  7130. break;
  7131. }
  7132. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  7133. clif_updatestatus(*sd,SP_JOBLEVEL);
  7134. clif_updatestatus(*sd,SP_JOBEXP);
  7135. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  7136. clif_updatestatus(*sd,SP_SKILLPOINT);
  7137. status_calc_pc(sd,SCO_FORCE);
  7138. clif_misceffect( sd->bl, NOTIFYEFFECT_JOB_LEVEL_UP );
  7139. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7140. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7141. npc_script_event( *sd, NPCE_JOBLVUP );
  7142. for (; job_level <= sd->status.job_level; job_level++)
  7143. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  7144. pc_show_questinfo(sd);
  7145. return 1;
  7146. }
  7147. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  7148. * @param sd Player
  7149. * @param base_exp Base EXP before peronal bonuses
  7150. * @param job_exp Job EXP before peronal bonuses
  7151. * @param src Block list that affecting the exp calculation
  7152. */
  7153. static void pc_calcexp(map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  7154. {
  7155. int32 bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  7156. if (src) {
  7157. status_data* status = status_get_status_data(*src);
  7158. if( sd->indexed_bonus.expaddrace[status->race] )
  7159. bonus += sd->indexed_bonus.expaddrace[status->race];
  7160. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  7161. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  7162. if( sd->indexed_bonus.expaddclass[status->class_] )
  7163. bonus += sd->indexed_bonus.expaddclass[status->class_];
  7164. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  7165. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  7166. if (battle_config.pk_mode &&
  7167. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  7168. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  7169. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  7170. vip_bonus_base = battle_config.vip_base_exp_increase;
  7171. vip_bonus_job = battle_config.vip_job_exp_increase;
  7172. }
  7173. }
  7174. // Give EXPBOOST for quests even if src is nullptr.
  7175. if (sd->sc.getSCE(SC_EXPBOOST)) {
  7176. bonus += sd->sc.getSCE(SC_EXPBOOST)->val1;
  7177. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  7178. bonus += (sd->sc.getSCE(SC_EXPBOOST)->val1 / battle_config.vip_bm_increase);
  7179. }
  7180. if (sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND))
  7181. bonus += sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)->val1;
  7182. if (*base_exp) {
  7183. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  7184. *base_exp = cap_value(exp, 1, MAX_EXP);
  7185. }
  7186. // Give JEXPBOOST for quests even if src is nullptr.
  7187. if (sd->sc.getSCE(SC_JEXPBOOST))
  7188. bonus += sd->sc.getSCE(SC_JEXPBOOST)->val1;
  7189. if (sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)) // Increase Jexp as well
  7190. bonus += sd->sc.getSCE(SC_PERIOD_PLUSEXP_2ND)->val1;
  7191. if (*job_exp) {
  7192. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  7193. *job_exp = cap_value(exp, 1, MAX_EXP);
  7194. }
  7195. return;
  7196. }
  7197. /**
  7198. * Show EXP gained by player in percentage by @showexp
  7199. * @param sd Player
  7200. * @param base_exp Base EXP gained/loss
  7201. * @param next_base_exp Base EXP needed for next base level
  7202. * @param job_exp Job EXP gained/loss
  7203. * @param next_job_exp Job EXP needed for next job level
  7204. * @param lost True:EXP penalty, lose EXP
  7205. **/
  7206. 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) {
  7207. char output[CHAT_SIZE_MAX];
  7208. nullpo_retv(sd);
  7209. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  7210. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  7211. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  7212. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  7213. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  7214. }
  7215. /**
  7216. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  7217. * @param sd Player
  7218. * @param src EXP source
  7219. * @param base_exp Base EXP gained
  7220. * @param base_exp Job EXP gained
  7221. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  7222. * @return
  7223. **/
  7224. void pc_gainexp(map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  7225. {
  7226. t_exp nextb = 0, nextj = 0;
  7227. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  7228. nullpo_retv(sd);
  7229. if(sd->bl.prev == nullptr || pc_isdead(sd))
  7230. return;
  7231. if (!(exp_flag&2)) {
  7232. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  7233. return; // no exp on pvp maps
  7234. if (sd->status.guild_id>0)
  7235. base_exp -= guild_payexp(sd,base_exp);
  7236. }
  7237. flag = ((base_exp) ? 1 : 0) |
  7238. ((job_exp) ? 2 : 0) |
  7239. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  7240. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  7241. if (!(exp_flag&2))
  7242. pc_calcexp(sd, &base_exp, &job_exp, src);
  7243. nextb = pc_nextbaseexp(sd);
  7244. nextj = pc_nextjobexp(sd);
  7245. if (flag&4){
  7246. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  7247. base_exp = 0;
  7248. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  7249. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  7250. }
  7251. if (flag&8){
  7252. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  7253. job_exp = 0;
  7254. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  7255. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  7256. }
  7257. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  7258. //Note that this value should never be greater than the original
  7259. //therefore no overflow checks are needed. [Skotlex]
  7260. if (nextb > 0) {
  7261. float nextbp = (float) base_exp / (float) nextb;
  7262. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  7263. base_exp = (uint32)(battle_config.max_exp_gain_rate/1000.*nextb);
  7264. }
  7265. if (nextj > 0) {
  7266. float nextjp = (float) job_exp / (float) nextj;
  7267. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  7268. job_exp = (uint32)(battle_config.max_exp_gain_rate/1000.*nextj);
  7269. }
  7270. }
  7271. // Give EXP for Base Level
  7272. if (base_exp) {
  7273. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  7274. if (!pc_checkbaselevelup(sd))
  7275. clif_updatestatus(*sd,SP_BASEEXP);
  7276. }
  7277. // Give EXP for Job Level
  7278. if (job_exp) {
  7279. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  7280. if (!pc_checkjoblevelup(sd))
  7281. clif_updatestatus(*sd,SP_JOBEXP);
  7282. }
  7283. if (flag&1)
  7284. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  7285. if (flag&2)
  7286. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  7287. if (sd->state.showexp && (base_exp || job_exp))
  7288. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  7289. }
  7290. /**
  7291. * Lost Base/Job EXP from a player
  7292. * @param sd Player
  7293. * @param base_exp Base EXP lost
  7294. * @param job_exp Job EXP lost
  7295. **/
  7296. void pc_lostexp(map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  7297. nullpo_retv(sd);
  7298. if (base_exp) {
  7299. base_exp = u64min(sd->status.base_exp, base_exp);
  7300. sd->status.base_exp -= base_exp;
  7301. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  7302. clif_updatestatus(*sd, SP_BASEEXP);
  7303. }
  7304. if (job_exp) {
  7305. job_exp = u64min(sd->status.job_exp, job_exp);
  7306. sd->status.job_exp -= job_exp;
  7307. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  7308. clif_updatestatus(*sd, SP_JOBEXP);
  7309. }
  7310. if (sd->state.showexp && (base_exp || job_exp))
  7311. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  7312. }
  7313. /**
  7314. * Returns max base level for this character's class.
  7315. * @param job_id: Player's class
  7316. * @return Max Base Level
  7317. */
  7318. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  7319. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7320. return job ? job->max_base_level : 0;
  7321. }
  7322. /**
  7323. * Returns max base level for this character.
  7324. * @param sd Player
  7325. * @return Max Base Level
  7326. **/
  7327. uint32 pc_maxbaselv(map_session_data *sd){
  7328. return job_db.get_maxBaseLv(sd->status.class_);
  7329. }
  7330. /**
  7331. * Returns max job level for this character's class.
  7332. * @param job_id: Player's class
  7333. * @return Max Job Level
  7334. */
  7335. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  7336. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7337. return job ? job->max_job_level : 0;
  7338. }
  7339. /**
  7340. * Returns max job level for this character.
  7341. * @param sd Player
  7342. * @return Max Job Level
  7343. **/
  7344. uint32 pc_maxjoblv(map_session_data *sd){
  7345. return job_db.get_maxJobLv(sd->status.class_);
  7346. }
  7347. /**
  7348. * Check if player is reached max base level
  7349. * @param sd
  7350. * @return True if reached max level
  7351. **/
  7352. bool pc_is_maxbaselv(map_session_data *sd) {
  7353. nullpo_retr(false, sd);
  7354. return (sd->status.base_level >= pc_maxbaselv(sd));
  7355. }
  7356. /**
  7357. * Check if player is reached max base level
  7358. * @param sd
  7359. * @return True if reached max level
  7360. **/
  7361. bool pc_is_maxjoblv(map_session_data *sd) {
  7362. nullpo_retr(false, sd);
  7363. return (sd->status.job_level >= pc_maxjoblv(sd));
  7364. }
  7365. /**
  7366. * Returns base experience for this character's class.
  7367. * @param job_id: Player's class
  7368. * @param level: Player's level
  7369. * @return Base EXP
  7370. */
  7371. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  7372. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7373. return job ? job->base_exp[level - 1] : 0;
  7374. }
  7375. /**
  7376. * Base exp needed for player to level up.
  7377. * @param sd
  7378. * @return Base EXP needed for next base level
  7379. **/
  7380. t_exp pc_nextbaseexp(map_session_data *sd){
  7381. nullpo_ret(sd);
  7382. if (sd->status.base_level == 0) // Is this something that possible?
  7383. return 0;
  7384. if (pc_is_maxbaselv(sd))
  7385. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  7386. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  7387. }
  7388. /**
  7389. * Returns job experience for this character's class.
  7390. * @param job_id: Player's class
  7391. * @param level: Player's level
  7392. * @return Job EXP
  7393. */
  7394. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  7395. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7396. return job ? job->job_exp[level - 1] : 0;
  7397. }
  7398. /**
  7399. * Job exp needed for player to level up.
  7400. * @param sd
  7401. * @return Job EXP needed for next job level
  7402. **/
  7403. t_exp pc_nextjobexp(map_session_data *sd){
  7404. nullpo_ret(sd);
  7405. if (sd->status.job_level == 0) // Is this something that possible?
  7406. return 0;
  7407. if (pc_is_maxjoblv(sd))
  7408. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  7409. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  7410. }
  7411. /**
  7412. * Returns max weight base for this character's class.
  7413. * @param job_id: Player's class
  7414. * @return Max weight base
  7415. */
  7416. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  7417. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7418. return job ? job->max_weight_base : 0;
  7419. }
  7420. /// Returns the value of the specified stat.
  7421. int32 pc_getstat(map_session_data *sd, int32 type)
  7422. {
  7423. nullpo_retr(-1, sd);
  7424. switch( type ) {
  7425. case SP_STR: return sd->status.str;
  7426. case SP_AGI: return sd->status.agi;
  7427. case SP_VIT: return sd->status.vit;
  7428. case SP_INT: return sd->status.int_;
  7429. case SP_DEX: return sd->status.dex;
  7430. case SP_LUK: return sd->status.luk;
  7431. case SP_POW: return sd->status.pow;
  7432. case SP_STA: return sd->status.sta;
  7433. case SP_WIS: return sd->status.wis;
  7434. case SP_SPL: return sd->status.spl;
  7435. case SP_CON: return sd->status.con;
  7436. case SP_CRT: return sd->status.crt;
  7437. default:
  7438. return -1;
  7439. }
  7440. }
  7441. /// Sets the specified stat to the specified value.
  7442. /// Returns the new value.
  7443. int32 pc_setstat(map_session_data* sd, int32 type, int32 val)
  7444. {
  7445. nullpo_retr(-1, sd);
  7446. switch( type ) {
  7447. case SP_STR: sd->status.str = val; break;
  7448. case SP_AGI: sd->status.agi = val; break;
  7449. case SP_VIT: sd->status.vit = val; break;
  7450. case SP_INT: sd->status.int_ = val; break;
  7451. case SP_DEX: sd->status.dex = val; break;
  7452. case SP_LUK: sd->status.luk = val; break;
  7453. case SP_POW: sd->status.pow = val; break;
  7454. case SP_STA: sd->status.sta = val; break;
  7455. case SP_WIS: sd->status.wis = val; break;
  7456. case SP_SPL: sd->status.spl = val; break;
  7457. case SP_CON: sd->status.con = val; break;
  7458. case SP_CRT: sd->status.crt = val; break;
  7459. default:
  7460. return -1;
  7461. }
  7462. return val;
  7463. }
  7464. /**
  7465. * Gets the total number of status points at the provided level.
  7466. * @param level: Player base level.
  7467. * @return Total number of status points at specific base level.
  7468. */
  7469. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7470. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7471. if( entry != nullptr ){
  7472. return entry->statpoints;
  7473. }else{
  7474. return 0;
  7475. }
  7476. }
  7477. /**
  7478. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7479. * @param level: Player base level.
  7480. * @param table: Use table value or formula.
  7481. * @return Status points at specific base level.
  7482. */
  7483. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7484. uint32 next_level = this->get_table_point( level + 1 );
  7485. uint32 current_level = this->get_table_point( level );
  7486. if( next_level > current_level ){
  7487. return next_level - current_level;
  7488. }else{
  7489. return 0;
  7490. }
  7491. }
  7492. /**
  7493. * Gets the total number of trait points at the provided level.
  7494. * @param level: Player base level.
  7495. * @return Total number of trait points at specific base level.
  7496. */
  7497. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7498. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7499. if( entry != nullptr ){
  7500. return entry->traitpoints;
  7501. }else{
  7502. return 0;
  7503. }
  7504. }
  7505. /**
  7506. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7507. * @param level: Player base level.
  7508. * @param table: Use table value or formula.
  7509. * @return Trait points at specific base level.
  7510. */
  7511. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7512. uint32 next_level = this->get_trait_table_point( level + 1 );
  7513. uint32 current_level = this->get_trait_table_point( level );
  7514. if( next_level > current_level ){
  7515. return next_level - current_level;
  7516. }else{
  7517. return 0;
  7518. }
  7519. }
  7520. #ifdef RENEWAL_STAT
  7521. /// Renewal status point cost formula
  7522. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7523. #else
  7524. /// Pre-Renewal status point cost formula
  7525. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7526. #endif
  7527. /// Returns the number of stat points needed to change the specified stat by val.
  7528. /// If val is negative, returns the number of stat points that would be needed to
  7529. /// raise the specified stat from (current value - val) to current value.
  7530. int32 pc_need_status_point(map_session_data* sd, int32 type, int32 val)
  7531. {
  7532. int32 low, high, sp = 0, max = 0;
  7533. if ( val == 0 )
  7534. return 0;
  7535. low = pc_getstat(sd,type);
  7536. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7537. if ( low >= max && val > 0 )
  7538. return 0; // Official servers show '0' when max is reached
  7539. high = low + val;
  7540. if ( val < 0 )
  7541. std::swap(low, high);
  7542. for ( ; low < high; low++ )
  7543. sp += PC_STATUS_POINT_COST(low);
  7544. return sp;
  7545. }
  7546. /**
  7547. * Returns the value the specified stat can be increased by with the current
  7548. * amount of available status points for the current character's class.
  7549. *
  7550. * @param sd The target character.
  7551. * @param type Stat to verify.
  7552. * @return Maximum value the stat could grow by.
  7553. */
  7554. int32 pc_maxparameterincrease(map_session_data* sd, int32 type)
  7555. {
  7556. int32 base, final_val, status_points, max_param;
  7557. nullpo_ret(sd);
  7558. base = final_val = pc_getstat(sd, type);
  7559. status_points = sd->status.status_point;
  7560. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7561. while (final_val <= max_param && status_points >= 0) {
  7562. status_points -= PC_STATUS_POINT_COST(final_val);
  7563. final_val++;
  7564. }
  7565. final_val--;
  7566. return (final_val > base ? final_val-base : 0);
  7567. }
  7568. /**
  7569. * Raises a stat by the specified amount.
  7570. *
  7571. * Obeys max_parameter limits.
  7572. * Subtracts status points according to the cost of the increased stat points.
  7573. *
  7574. * @param sd The target character.
  7575. * @param type The stat to change (see enum _sp)
  7576. * @param increase The stat increase (strictly positive) amount.
  7577. * @retval true if the stat was increased by any amount.
  7578. * @retval false if there were no changes.
  7579. */
  7580. bool pc_statusup(map_session_data* sd, int32 type, int32 increase)
  7581. {
  7582. int32 max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7583. nullpo_ret(sd);
  7584. // check conditions
  7585. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7586. clif_statusupack( *sd, type, false );
  7587. return false;
  7588. }
  7589. // check limits
  7590. current = pc_getstat(sd, type);
  7591. max_increase = pc_maxparameterincrease(sd, type);
  7592. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7593. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7594. clif_statusupack( *sd, type, false );
  7595. return false;
  7596. }
  7597. // check status points
  7598. needed_points = pc_need_status_point(sd, type, increase);
  7599. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7600. clif_statusupack( *sd, type, false );
  7601. return false;
  7602. }
  7603. // set new values
  7604. final_value = pc_setstat(sd, type, current + increase);
  7605. sd->status.status_point -= needed_points;
  7606. status_calc_pc(sd,SCO_NONE);
  7607. // update increase cost indicator
  7608. clif_updatestatus(*sd, static_cast<_sp>( SP_USTR + type-SP_STR ) );
  7609. // update statpoint count
  7610. clif_updatestatus(*sd, SP_STATUSPOINT);
  7611. // update stat value
  7612. clif_statusupack( *sd, type, true, final_value );
  7613. if( final_value > 255 )
  7614. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7615. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7616. return true;
  7617. }
  7618. /**
  7619. * Raises a stat by the specified amount.
  7620. *
  7621. * Obeys max_parameter limits.
  7622. * Does not subtract status points for the cost of the modified stat points.
  7623. *
  7624. * @param sd The target character.
  7625. * @param type The stat to change (see enum _sp)
  7626. * @param val The stat increase (or decrease) amount.
  7627. * @return the stat increase amount.
  7628. * @retval 0 if no changes were made.
  7629. */
  7630. int32 pc_statusup2(map_session_data* sd, int32 type, int32 val)
  7631. {
  7632. int32 max, need;
  7633. nullpo_ret(sd);
  7634. if( type < SP_STR || type > SP_LUK )
  7635. {
  7636. clif_statusupack( *sd, type, false );
  7637. return 0;
  7638. }
  7639. need = pc_need_status_point(sd,type,1);
  7640. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7641. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7642. status_calc_pc(sd,SCO_NONE);
  7643. // update increase cost indicator
  7644. if( need != pc_need_status_point(sd,type,1) )
  7645. clif_updatestatus(*sd, static_cast<_sp>( SP_USTR + type-SP_STR ) );
  7646. // update stat value
  7647. clif_statusupack( *sd, type, true, val );
  7648. if( val > 255 )
  7649. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7650. return val;
  7651. }
  7652. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7653. /// If val is negative, returns the number of trait stat points that would be needed to
  7654. /// raise the specified trait stat from (current value - val) to current value.
  7655. int32 pc_need_trait_point(map_session_data* sd, int32 type, int32 val)
  7656. {
  7657. nullpo_retr(0, sd);
  7658. if (val == 0 || type < SP_POW || type > SP_CRT)
  7659. return 0;
  7660. int32 low = pc_getstat(sd, type);
  7661. int32 max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7662. if (low >= max && val > 0)
  7663. return 0; // Official servers show '0' when max is reached
  7664. int32 high = low + val, sp = 0;
  7665. if (val < 0)
  7666. std::swap(low, high);
  7667. for (; low < high; low++)
  7668. sp += 1;
  7669. return sp;
  7670. }
  7671. /**
  7672. * Returns the value the specified trait stat can be increased by with the current
  7673. * amount of available trait status points for the current character's class.
  7674. *
  7675. * @param sd The target character.
  7676. * @param type Trait stat to verify.
  7677. * @return Maximum value the stat could grow by.
  7678. */
  7679. int32 pc_maxtraitparameterincrease(map_session_data* sd, int32 type)
  7680. {
  7681. nullpo_ret(sd);
  7682. if( type < SP_POW || type > SP_CRT ){
  7683. return 0;
  7684. }
  7685. int32 base, final_val = pc_getstat(sd, type);
  7686. int32 trait_points = sd->status.trait_point;
  7687. int32 max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7688. base = final_val;
  7689. while (final_val <= max_param && trait_points >= 0) {
  7690. trait_points -= 1;
  7691. final_val++;
  7692. }
  7693. final_val--;
  7694. return (final_val > base ? final_val - base : 0);
  7695. }
  7696. /**
  7697. * Raises a trait stat by the specified amount.
  7698. *
  7699. * Obeys max_traitparameter limits.
  7700. * Subtracts trait status points according to the cost of the increased trait stat points.
  7701. *
  7702. * @param sd The target character.
  7703. * @param type The stat to change (see enum _sp)
  7704. * @param increase The stat increase (strictly positive) amount.
  7705. * @retval true if the trait stat was increased by any amount.
  7706. * @retval false if there were no changes.
  7707. */
  7708. bool pc_traitstatusup(map_session_data* sd, int32 type, int32 increase)
  7709. {
  7710. nullpo_ret(sd);
  7711. // check conditions
  7712. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7713. clif_statusupack( *sd, type, false );
  7714. return false;
  7715. }
  7716. // check limits
  7717. int32 current = pc_getstat(sd, type);
  7718. int32 max_increase = pc_maxtraitparameterincrease(sd, type);
  7719. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7720. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7721. clif_statusupack( *sd, type, false );
  7722. return false;
  7723. }
  7724. // check status points
  7725. int32 needed_points = pc_need_trait_point(sd, type, increase);
  7726. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7727. clif_statusupack( *sd, type, false );
  7728. return false;
  7729. }
  7730. // set new values
  7731. int32 final_value = pc_setstat(sd, type, current + increase);
  7732. sd->status.trait_point -= needed_points;
  7733. status_calc_pc(sd, SCO_NONE);
  7734. // update increase cost indicator
  7735. clif_updatestatus(*sd, static_cast<_sp>( SP_UPOW + type - SP_POW ) );
  7736. // update statpoint count
  7737. clif_updatestatus(*sd, SP_TRAITPOINT);
  7738. // update stat value
  7739. clif_statusupack( *sd, type, true, final_value );
  7740. if (final_value > 255)
  7741. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7742. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7743. return true;
  7744. }
  7745. /**
  7746. * Raises a trait stat by the specified amount.
  7747. *
  7748. * Obeys max_trait_parameter limits.
  7749. * Does not subtract status points for the cost of the modified stat points.
  7750. *
  7751. * @param sd The target character.
  7752. * @param type The stat to change (see enum _sp)
  7753. * @param val The stat increase (or decrease) amount.
  7754. * @return the stat increase amount.
  7755. * @retval 0 if no changes were made.
  7756. */
  7757. int32 pc_traitstatusup2(map_session_data* sd, int32 type, int32 val)
  7758. {
  7759. nullpo_ret(sd);
  7760. if (type < SP_POW || type > SP_CRT) {
  7761. clif_statusupack( *sd, type, false );
  7762. return 0;
  7763. }
  7764. int32 need = pc_need_trait_point(sd, type, 1);
  7765. int32 max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7766. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7767. status_calc_pc(sd, SCO_NONE);
  7768. // update increase cost indicator
  7769. if (need != pc_need_trait_point(sd, type, 1))
  7770. clif_updatestatus(*sd, static_cast<_sp>( SP_UPOW + type - SP_POW ) );
  7771. // update stat value
  7772. clif_statusupack( *sd, type, true, val );
  7773. if (val > 255)
  7774. clif_updatestatus(*sd, static_cast<_sp>( type ) ); // send after the 'ack' to override the truncated value
  7775. return val;
  7776. }
  7777. /*==========================================
  7778. * Update skill_lv for player sd
  7779. * Skill point allocation
  7780. *------------------------------------------*/
  7781. void pc_skillup(map_session_data *sd,uint16 skill_id)
  7782. {
  7783. uint16 idx = skill_get_index(skill_id);
  7784. nullpo_retv(sd);
  7785. if (!idx) {
  7786. if (skill_id)
  7787. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7788. return;
  7789. }
  7790. // Level up guild skill
  7791. if (SKILL_CHK_GUILD(skill_id)) {
  7792. guild_skillup(sd, skill_id);
  7793. return;
  7794. }
  7795. // Level up homunculus skill
  7796. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7797. hom_skillup(sd->hd, skill_id);
  7798. return;
  7799. }
  7800. else {
  7801. if( sd->status.skill_point > 0 &&
  7802. sd->status.skill[idx].id &&
  7803. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7804. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7805. {
  7806. int32 lv, range, upgradable;
  7807. sd->status.skill[idx].lv++;
  7808. sd->status.skill_point--;
  7809. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7810. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7811. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7812. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7813. else
  7814. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7815. lv = sd->status.skill[idx].lv;
  7816. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7817. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  7818. clif_skillup(sd,skill_id,lv,range,upgradable);
  7819. clif_updatestatus(*sd,SP_SKILLPOINT);
  7820. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7821. clif_updatestatus(*sd,SP_CARTINFO);
  7822. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7823. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7824. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7825. clif_skillinfoblock(sd);
  7826. }
  7827. //else
  7828. // 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);
  7829. }
  7830. }
  7831. /*==========================================
  7832. * /allskill
  7833. *------------------------------------------*/
  7834. int32 pc_allskillup(map_session_data *sd)
  7835. {
  7836. int32 i;
  7837. nullpo_ret(sd);
  7838. for (i = 0; i < MAX_SKILL; i++) {
  7839. 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) {
  7840. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7841. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7842. if (sd->status.skill[i].lv == 0)
  7843. sd->status.skill[i].id = 0;
  7844. }
  7845. }
  7846. if (!pc_grant_allskills(sd, true)) {
  7847. uint16 sk_id;
  7848. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7849. if (tree != nullptr && !tree->skills.empty()) {
  7850. for (const auto &skillsit : tree->skills) {
  7851. sk_id = skillsit.first;
  7852. uint16 sk_idx = skill_get_index(sk_id);
  7853. if (sk_idx == 0)
  7854. continue;
  7855. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7856. if (
  7857. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7858. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7859. sk_id == SG_DEVIL
  7860. )
  7861. continue; //Cannot be learned normally.
  7862. sd->status.skill[sk_idx].id = sk_id;
  7863. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7864. }
  7865. }
  7866. }
  7867. status_calc_pc(sd,SCO_NONE);
  7868. //Required because if you could level up all skills previously,
  7869. //the update will not be sent as only the lv variable changes.
  7870. clif_skillinfoblock(sd);
  7871. return 0;
  7872. }
  7873. /*==========================================
  7874. * /resetlvl
  7875. *------------------------------------------*/
  7876. int32 pc_resetlvl(map_session_data* sd,int32 type)
  7877. {
  7878. int32 i;
  7879. nullpo_ret(sd);
  7880. if (type != 3) //Also reset skills
  7881. pc_resetskill(sd, 0);
  7882. if(type == 1){
  7883. sd->status.skill_point=0;
  7884. sd->status.trait_point = 0;
  7885. sd->status.base_level=1;
  7886. sd->status.job_level=1;
  7887. sd->status.base_exp=0;
  7888. sd->status.job_exp=0;
  7889. if(sd->sc.option !=0)
  7890. sd->sc.option = 0;
  7891. sd->status.str=1;
  7892. sd->status.agi=1;
  7893. sd->status.vit=1;
  7894. sd->status.int_=1;
  7895. sd->status.dex=1;
  7896. sd->status.luk=1;
  7897. sd->status.pow=0;
  7898. sd->status.sta=0;
  7899. sd->status.wis=0;
  7900. sd->status.spl=0;
  7901. sd->status.con=0;
  7902. sd->status.crt=0;
  7903. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7904. sd->status.status_point=100; // not 88 [celest]
  7905. // give platinum skills upon changing
  7906. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7907. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7908. }
  7909. }
  7910. if(type == 2){
  7911. sd->status.skill_point=0;
  7912. sd->status.base_level=1;
  7913. sd->status.job_level=1;
  7914. sd->status.base_exp=0;
  7915. sd->status.job_exp=0;
  7916. }
  7917. if(type == 3){
  7918. sd->status.base_level=1;
  7919. sd->status.base_exp=0;
  7920. }
  7921. if(type == 4){
  7922. sd->status.job_level=1;
  7923. sd->status.job_exp=0;
  7924. }
  7925. clif_updatestatus(*sd,SP_STATUSPOINT);
  7926. clif_updatestatus(*sd,SP_TRAITPOINT);
  7927. clif_updatestatus(*sd,SP_STR);
  7928. clif_updatestatus(*sd,SP_AGI);
  7929. clif_updatestatus(*sd,SP_VIT);
  7930. clif_updatestatus(*sd,SP_INT);
  7931. clif_updatestatus(*sd,SP_DEX);
  7932. clif_updatestatus(*sd,SP_LUK);
  7933. clif_updatestatus(*sd,SP_POW);
  7934. clif_updatestatus(*sd,SP_STA);
  7935. clif_updatestatus(*sd,SP_WIS);
  7936. clif_updatestatus(*sd,SP_SPL);
  7937. clif_updatestatus(*sd,SP_CON);
  7938. clif_updatestatus(*sd,SP_CRT);
  7939. clif_updatestatus(*sd,SP_BASELEVEL);
  7940. clif_updatestatus(*sd,SP_JOBLEVEL);
  7941. clif_updatestatus(*sd,SP_STATUSPOINT);
  7942. clif_updatestatus(*sd,SP_BASEEXP);
  7943. clif_updatestatus(*sd,SP_JOBEXP);
  7944. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  7945. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  7946. clif_updatestatus(*sd,SP_SKILLPOINT);
  7947. clif_updatestatus(*sd,SP_USTR); // Updates needed stat points - Valaris
  7948. clif_updatestatus(*sd,SP_UAGI);
  7949. clif_updatestatus(*sd,SP_UVIT);
  7950. clif_updatestatus(*sd,SP_UINT);
  7951. clif_updatestatus(*sd,SP_UDEX);
  7952. clif_updatestatus(*sd,SP_ULUK); // End Addition
  7953. clif_updatestatus(*sd,SP_UPOW);
  7954. clif_updatestatus(*sd,SP_USTA);
  7955. clif_updatestatus(*sd,SP_UWIS);
  7956. clif_updatestatus(*sd,SP_USPL);
  7957. clif_updatestatus(*sd,SP_UCON);
  7958. clif_updatestatus(*sd,SP_UCRT);
  7959. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7960. if(sd->equip_index[i] >= 0)
  7961. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  7962. pc_unequipitem(sd,sd->equip_index[i],2);
  7963. }
  7964. }
  7965. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7966. party_send_levelup(sd);
  7967. status_calc_pc(sd, SCO_FORCE);
  7968. clif_skillinfoblock(sd);
  7969. return 0;
  7970. }
  7971. /*==========================================
  7972. * /resetstate
  7973. *------------------------------------------*/
  7974. int32 pc_resetstate(map_session_data* sd)
  7975. {
  7976. nullpo_ret(sd);
  7977. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7978. 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",
  7979. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7980. sd->status.base_level = pc_maxbaselv( sd );
  7981. clif_updatestatus( *sd, SP_BASELEVEL );
  7982. }
  7983. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7984. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7985. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7986. sd->status.status_point += battle_config.transcendent_status_points;
  7987. }
  7988. if ((sd->class_&JOBL_FOURTH) != 0) {
  7989. sd->status.trait_point += battle_config.trait_points_job_change;
  7990. }
  7991. pc_setstat(sd, SP_STR, 1);
  7992. pc_setstat(sd, SP_AGI, 1);
  7993. pc_setstat(sd, SP_VIT, 1);
  7994. pc_setstat(sd, SP_INT, 1);
  7995. pc_setstat(sd, SP_DEX, 1);
  7996. pc_setstat(sd, SP_LUK, 1);
  7997. pc_setstat(sd, SP_POW, 0);
  7998. pc_setstat(sd, SP_STA, 0);
  7999. pc_setstat(sd, SP_WIS, 0);
  8000. pc_setstat(sd, SP_SPL, 0);
  8001. pc_setstat(sd, SP_CON, 0);
  8002. pc_setstat(sd, SP_CRT, 0);
  8003. clif_updatestatus(*sd,SP_STR);
  8004. clif_updatestatus(*sd,SP_AGI);
  8005. clif_updatestatus(*sd,SP_VIT);
  8006. clif_updatestatus(*sd,SP_INT);
  8007. clif_updatestatus(*sd,SP_DEX);
  8008. clif_updatestatus(*sd,SP_LUK);
  8009. clif_updatestatus(*sd,SP_POW);
  8010. clif_updatestatus(*sd,SP_STA);
  8011. clif_updatestatus(*sd,SP_WIS);
  8012. clif_updatestatus(*sd,SP_SPL);
  8013. clif_updatestatus(*sd,SP_CON);
  8014. clif_updatestatus(*sd,SP_CRT);
  8015. clif_updatestatus(*sd,SP_USTR); // Updates needed stat points - Valaris
  8016. clif_updatestatus(*sd,SP_UAGI);
  8017. clif_updatestatus(*sd,SP_UVIT);
  8018. clif_updatestatus(*sd,SP_UINT);
  8019. clif_updatestatus(*sd,SP_UDEX);
  8020. clif_updatestatus(*sd,SP_ULUK); // End Addition
  8021. clif_updatestatus(*sd,SP_UPOW);
  8022. clif_updatestatus(*sd,SP_USTA);
  8023. clif_updatestatus(*sd,SP_UWIS);
  8024. clif_updatestatus(*sd,SP_USPL);
  8025. clif_updatestatus(*sd,SP_UCON);
  8026. clif_updatestatus(*sd,SP_UCRT);
  8027. clif_updatestatus(*sd,SP_STATUSPOINT);
  8028. clif_updatestatus(*sd,SP_TRAITPOINT);
  8029. if( sd->mission_mobid ) { //bugreport:2200
  8030. sd->mission_mobid = 0;
  8031. sd->mission_count = 0;
  8032. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  8033. }
  8034. status_calc_pc(sd, SCO_NONE);
  8035. return 1;
  8036. }
  8037. /*==========================================
  8038. * /resetskill
  8039. * if flag&1, perform block resync and status_calc call.
  8040. * if flag&2, just count total amount of skill points used by player, do not really reset.
  8041. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  8042. *------------------------------------------*/
  8043. int32 pc_resetskill(map_session_data* sd, int32 flag)
  8044. {
  8045. int32 i, skill_point=0;
  8046. nullpo_ret(sd);
  8047. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  8048. return 0;
  8049. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  8050. /**
  8051. * 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)
  8052. **/
  8053. if( pc_is_taekwon_ranker(sd) )
  8054. return 0;
  8055. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  8056. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  8057. i = sd->sc.option;
  8058. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  8059. i &= ~OPTION_RIDING;
  8060. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  8061. i &= ~OPTION_FALCON;
  8062. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  8063. i &= ~OPTION_DRAGON;
  8064. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  8065. i &= ~OPTION_WUG;
  8066. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  8067. i &= ~OPTION_WUGRIDER;
  8068. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  8069. i &= ~OPTION_MADOGEAR;
  8070. #ifndef NEW_CARTS
  8071. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  8072. i &= ~OPTION_CART;
  8073. #else
  8074. if( sd->sc.getSCE(SC_PUSH_CART) )
  8075. pc_setcart(sd, 0);
  8076. #endif
  8077. if( i != sd->sc.option )
  8078. pc_setoption(sd, i);
  8079. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  8080. hom_vaporize(sd, HOM_ST_ACTIVE);
  8081. if (sd->sc.getSCE(SC_SPRITEMABLE) && pc_checkskill(sd, SU_SPRITEMABLE))
  8082. status_change_end(&sd->bl, SC_SPRITEMABLE);
  8083. if (sd->sc.getSCE(SC_SOULATTACK) && pc_checkskill(sd, SU_SOULATTACK))
  8084. status_change_end(&sd->bl, SC_SOULATTACK);
  8085. }
  8086. for (const auto &skill : skill_db) {
  8087. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  8088. uint8 lv = sd->status.skill[idx].lv;
  8089. if (lv == 0 || skill_id == 0)
  8090. continue;
  8091. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  8092. continue;
  8093. // Don't reset trick dead if not a novice/baby
  8094. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8095. {
  8096. sd->status.skill[idx].lv = 0;
  8097. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8098. continue;
  8099. }
  8100. // do not reset basic skill
  8101. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  8102. continue;
  8103. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  8104. continue;
  8105. if( flag&4 && !skill_ischangesex(skill_id) )
  8106. continue;
  8107. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  8108. { //Only handle quest skills in a special way when you can't learn them manually
  8109. if( battle_config.quest_skill_reset && !(flag&2) )
  8110. { //Wipe them
  8111. sd->status.skill[idx].lv = 0;
  8112. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8113. }
  8114. continue;
  8115. }
  8116. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  8117. skill_point += lv;
  8118. else
  8119. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  8120. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  8121. if( !(flag&2) )
  8122. {// reset
  8123. sd->status.skill[idx].lv = 0;
  8124. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  8125. }
  8126. }
  8127. if( flag&2 || !skill_point ) return skill_point;
  8128. sd->status.skill_point += skill_point;
  8129. if (flag&1) {
  8130. clif_updatestatus(*sd,SP_SKILLPOINT);
  8131. clif_skillinfoblock(sd);
  8132. status_calc_pc(sd, SCO_FORCE);
  8133. }
  8134. return skill_point;
  8135. }
  8136. /*==========================================
  8137. * /resetfeel [Komurka]
  8138. *------------------------------------------*/
  8139. int32 pc_resetfeel(map_session_data* sd)
  8140. {
  8141. int32 i;
  8142. nullpo_ret(sd);
  8143. for (i=0; i<MAX_PC_FEELHATE; i++)
  8144. {
  8145. sd->feel_map[i].m = -1;
  8146. sd->feel_map[i].index = 0;
  8147. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  8148. }
  8149. return 0;
  8150. }
  8151. int32 pc_resethate(map_session_data* sd)
  8152. {
  8153. int32 i;
  8154. nullpo_ret(sd);
  8155. for (i=0; i<MAX_PC_FEELHATE; i++)
  8156. {
  8157. sd->hate_mob[i] = -1;
  8158. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  8159. }
  8160. return 0;
  8161. }
  8162. int32 pc_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8163. {
  8164. int32 bonus = 0;
  8165. nullpo_ret(sd);
  8166. skill_id = skill_dummy2skill_id(skill_id);
  8167. for (auto &it : sd->skillatk) {
  8168. if (it.id == skill_id) {
  8169. bonus += it.val;
  8170. break;
  8171. }
  8172. }
  8173. return bonus;
  8174. }
  8175. int32 pc_sub_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8176. {
  8177. int32 bonus = 0;
  8178. nullpo_ret(sd);
  8179. skill_id = skill_dummy2skill_id(skill_id);
  8180. for (auto &it : sd->subskill) {
  8181. if (it.id == skill_id) {
  8182. bonus += it.val;
  8183. break;
  8184. }
  8185. }
  8186. return bonus;
  8187. }
  8188. int32 pc_skillheal_bonus(map_session_data *sd, uint16 skill_id) {
  8189. int32 bonus = sd->bonus.add_heal_rate;
  8190. nullpo_ret(sd);
  8191. skill_id = skill_dummy2skill_id(skill_id);
  8192. if( bonus ) {
  8193. switch( skill_id ) {
  8194. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  8195. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  8196. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  8197. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  8198. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  8199. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  8200. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  8201. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  8202. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  8203. }
  8204. }
  8205. for (auto &it : sd->skillheal) {
  8206. if (it.id == skill_id) {
  8207. bonus += it.val;
  8208. break;
  8209. }
  8210. }
  8211. return bonus;
  8212. }
  8213. int32 pc_skillheal2_bonus(map_session_data *sd, uint16 skill_id) {
  8214. int32 bonus = sd->bonus.add_heal2_rate;
  8215. skill_id = skill_dummy2skill_id(skill_id);
  8216. for (auto &it : sd->skillheal2) {
  8217. if (it.id == skill_id) {
  8218. bonus += it.val;
  8219. break;
  8220. }
  8221. }
  8222. return bonus;
  8223. }
  8224. void pc_respawn(map_session_data* sd, clr_type clrtype)
  8225. {
  8226. if( !pc_isdead(sd) )
  8227. return; // not applicable
  8228. if( sd->bg_id && bg_member_respawn(sd) )
  8229. return; // member revived by battleground
  8230. pc_setstand(sd, true);
  8231. pc_setrestartvalue(sd,3);
  8232. if( pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, clrtype ) != SETPOS_OK ){
  8233. clif_resurrection( sd->bl ); //If warping fails, send a normal stand up packet.
  8234. }
  8235. }
  8236. static TIMER_FUNC(pc_respawn_timer){
  8237. map_session_data *sd = map_id2sd(id);
  8238. if( sd != nullptr )
  8239. {
  8240. sd->pvp_point=0;
  8241. sd->respawn_tid = INVALID_TIMER;
  8242. pc_respawn(sd,CLR_OUTSIGHT);
  8243. }
  8244. return 0;
  8245. }
  8246. /*==========================================
  8247. * Invoked when a player has received damage
  8248. *------------------------------------------*/
  8249. void pc_damage(map_session_data *sd,struct block_list *src,uint32 hp, uint32 sp, uint32 ap)
  8250. {
  8251. if (ap) clif_updatestatus(*sd,SP_AP);
  8252. if (sp) clif_updatestatus(*sd,SP_SP);
  8253. if (hp) clif_updatestatus(*sd,SP_HP);
  8254. else return;
  8255. if (!src)
  8256. return;
  8257. if( pc_issit(sd) ) {
  8258. pc_setstand(sd, true);
  8259. skill_sit(sd,0);
  8260. }
  8261. if (sd->progressbar.npc_id)
  8262. clif_progressbar_abort(sd);
  8263. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  8264. pet_target_check(sd->pd,src,1);
  8265. if( sd->status.ele_id > 0 )
  8266. elemental_set_target(sd,src);
  8267. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  8268. sd->canlog_tick = gettick();
  8269. }
  8270. TIMER_FUNC(pc_close_npc_timer){
  8271. map_session_data* sd = map_id2sd( id );
  8272. if( sd != nullptr ){
  8273. pc_close_npc( sd, static_cast<int>( data ) );
  8274. }
  8275. return 0;
  8276. }
  8277. /**
  8278. * Method to properly close a NPC for player and clear anything related.
  8279. * @param sd: Player attached
  8280. * @param flag: Method of closure
  8281. * 1: Produce a close button and end the NPC
  8282. * 2: End the NPC (best for no dialog windows)
  8283. */
  8284. void pc_close_npc(map_session_data *sd,int32 flag)
  8285. {
  8286. nullpo_retv(sd);
  8287. if (sd->npc_id || sd->npc_shopid) {
  8288. if (sd->state.using_fake_npc) {
  8289. clif_clearunit_single( sd->npc_id, CLR_OUTSIGHT, *sd );
  8290. sd->state.using_fake_npc = 0;
  8291. }
  8292. if (sd->st) {
  8293. if(sd->st->state == RUN){ //wait ending code execution
  8294. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  8295. return;
  8296. }
  8297. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  8298. sd->st->mes_active = 0;
  8299. }
  8300. sd->state.menu_or_input = 0;
  8301. sd->npc_menu = 0;
  8302. sd->npc_shopid = 0;
  8303. #ifdef SECURE_NPCTIMEOUT
  8304. if( sd->npc_idle_timer != INVALID_TIMER ){
  8305. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  8306. sd->npc_idle_timer = INVALID_TIMER;
  8307. }
  8308. #endif
  8309. if (sd->st) {
  8310. if (sd->st->state == CLOSE) {
  8311. clif_scriptclose( *sd, sd->npc_id );
  8312. clif_cutin( *sd, "", 255); // Force to end cutin [Haydrich]
  8313. sd->st->state = END; // Force to end now
  8314. }
  8315. if (sd->st->state == END) { // free attached scripts that are waiting
  8316. script_free_state(sd->st);
  8317. sd->st = nullptr;
  8318. sd->npc_id = 0;
  8319. }
  8320. }
  8321. }
  8322. }
  8323. /*==========================================
  8324. * Invoked when a player has negative current hp
  8325. *------------------------------------------*/
  8326. int32 pc_dead(map_session_data *sd,struct block_list *src)
  8327. {
  8328. int32 i=0,k=0;
  8329. t_tick tick = gettick();
  8330. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8331. // Activate Steel body if a super novice dies at 99+% exp [celest]
  8332. // Super Novices have no kill or die functions attached when saved by their angel
  8333. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  8334. t_exp exp = pc_nextbaseexp(sd);
  8335. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  8336. sd->state.snovice_dead_flag = 1;
  8337. pc_setrestartvalue(sd,1);
  8338. status_percent_heal(&sd->bl, 100, 100);
  8339. clif_resurrection( sd->bl );
  8340. if(battle_config.pc_invincible_time)
  8341. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8342. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  8343. if(mapdata_flag_gvg2(mapdata))
  8344. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  8345. return 0;
  8346. }
  8347. }
  8348. for(k = 0; k < MAX_DEVOTION; k++) {
  8349. if (sd->devotion[k]){
  8350. map_session_data *devsd = map_id2sd(sd->devotion[k]);
  8351. if (devsd)
  8352. status_change_end(&devsd->bl, SC_DEVOTION);
  8353. sd->devotion[k] = 0;
  8354. }
  8355. }
  8356. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  8357. if (sd->stellar_mark[k]) {
  8358. map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  8359. if (smarksd)
  8360. status_change_end(&smarksd->bl, SC_FLASHKICK);
  8361. sd->stellar_mark[k] = 0;
  8362. }
  8363. }
  8364. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  8365. if (sd->united_soul[k]) {
  8366. map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  8367. if (usoulsd)
  8368. status_change_end(&usoulsd->bl, SC_SOULUNITY);
  8369. sd->united_soul[k] = 0;
  8370. }
  8371. }
  8372. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  8373. if (sd->servant_sign[k]) {
  8374. map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  8375. if (ssignsd)
  8376. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN);
  8377. sd->servant_sign[k] = 0;
  8378. }
  8379. }
  8380. if(sd->shadowform_id) { //if we were target of shadowform
  8381. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM);
  8382. sd->shadowform_id = 0; //should be remove on status end anyway
  8383. }
  8384. if(sd->status.pet_id > 0 && sd->pd) {
  8385. struct pet_data *pd = sd->pd;
  8386. if( !mapdata->getMapFlag(MF_NOEXPPENALTY) ) {
  8387. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  8388. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  8389. }
  8390. if( sd->pd->target_id ) // Unlock all targets...
  8391. pet_unlocktarget(sd->pd);
  8392. }
  8393. 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)
  8394. hom_vaporize(sd, HOM_ST_ACTIVE);
  8395. if( sd->md )
  8396. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  8397. if( sd->ed )
  8398. elemental_delete(sd->ed);
  8399. // Leave duel if you die [LuzZza]
  8400. if(battle_config.duel_autoleave_when_die) {
  8401. if(sd->duel_group > 0)
  8402. duel_leave(sd->duel_group, sd);
  8403. if(sd->duel_invite > 0)
  8404. duel_reject(sd->duel_invite, sd);
  8405. }
  8406. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  8407. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  8408. sd->skill_keep_using.tid = INVALID_TIMER;
  8409. }
  8410. pc_close_npc(sd,2); //close npc if we were using one
  8411. /* e.g. not killed thru pc_damage */
  8412. if( pc_issit(sd) ) {
  8413. clif_status_load(&sd->bl,EFST_SIT,0);
  8414. }
  8415. pc_setdead(sd);
  8416. clif_party_dead( *sd );
  8417. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  8418. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  8419. if (battle_config.loose_ap_on_death == 1)
  8420. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  8421. //Reset menu skills/item skills
  8422. if ((sd->skillitem) != 0)
  8423. sd->skillitem = sd->skillitemlv = 0;
  8424. if ((sd->menuskill_id) != 0)
  8425. sd->menuskill_id = sd->menuskill_val = 0;
  8426. //Reset ticks.
  8427. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  8428. if ( sd->spiritball !=0 )
  8429. pc_delspiritball(sd,sd->spiritball,0);
  8430. if (sd->soulball != 0)
  8431. pc_delsoulball( *sd, sd->soulball );
  8432. if (sd->servantball != 0)
  8433. pc_delservantball( *sd, sd->servantball );
  8434. if (sd->abyssball != 0)
  8435. pc_delabyssball( *sd, sd->abyssball );
  8436. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  8437. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  8438. if (src)
  8439. switch (src->type) {
  8440. case BL_MOB:
  8441. {
  8442. struct mob_data *md=(struct mob_data *)src;
  8443. if(md->target_id==sd->bl.id)
  8444. mob_unlocktarget(md,tick);
  8445. if(battle_config.mobs_level_up && md->status.hp &&
  8446. (uint32)md->level < pc_maxbaselv(sd) &&
  8447. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  8448. ) { // monster level up [Valaris]
  8449. clif_misceffect( md->bl, NOTIFYEFFECT_BASE_LEVEL_UP );
  8450. md->level++;
  8451. status_calc_mob(md, SCO_NONE);
  8452. status_percent_heal(src,10,0);
  8453. if( battle_config.show_mob_info&4 )
  8454. {// update name with new level
  8455. clif_name_area(&md->bl);
  8456. }
  8457. }
  8458. src = battle_get_master(src); // Maybe Player Summon
  8459. }
  8460. break;
  8461. case BL_PET: //Pass on to master...
  8462. case BL_HOM:
  8463. case BL_MER:
  8464. src = battle_get_master(src);
  8465. break;
  8466. }
  8467. if (src && src->type == BL_PC) {
  8468. map_session_data *ssd = (map_session_data *)src;
  8469. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8470. npc_script_event( *ssd, NPCE_KILLPC );
  8471. if (battle_config.pk_mode&2) {
  8472. ssd->status.manner -= 5;
  8473. if(ssd->status.manner < 0)
  8474. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8475. #if 0
  8476. // PK/Karma system code (not enabled yet) [celest]
  8477. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8478. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8479. // karma going down = more 'good' / more honourable.
  8480. // The Karma System way...
  8481. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8482. sd->status.karma--;
  8483. ssd->status.karma--;
  8484. }
  8485. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8486. ssd->status.karma++;
  8487. // or the PK System way...
  8488. if (sd->status.karma > 0) // player killed is dishonourable?
  8489. ssd->status.karma--; // honour points earned
  8490. sd->status.karma++; // honour points lost
  8491. // To-do: Receive exp on certain occasions
  8492. #endif
  8493. }
  8494. }
  8495. if(battle_config.bone_drop==2
  8496. || (battle_config.bone_drop==1 && mapdata->getMapFlag(MF_PVP)))
  8497. {
  8498. struct item item_tmp;
  8499. memset(&item_tmp,0,sizeof(item_tmp));
  8500. item_tmp.nameid=ITEMID_SKULL_;
  8501. item_tmp.identify=1;
  8502. item_tmp.card[0]=CARD0_CREATE;
  8503. item_tmp.card[1]=0;
  8504. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8505. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8506. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,4,0);
  8507. }
  8508. //Remove bonus_script when dead
  8509. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8510. // changed penalty options, added death by player if pk_mode [Valaris]
  8511. if(battle_config.death_penalty_type
  8512. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8513. && !sd->sc.cant.deathpenalty
  8514. && !mapdata->getMapFlag(MF_NOEXPPENALTY) && !mapdata_flag_gvg2(mapdata))
  8515. {
  8516. t_exp base_penalty = 0;
  8517. t_exp job_penalty = 0;
  8518. uint32 zeny_penalty = 0;
  8519. if (pc_isvip(sd)) { // EXP penalty for VIP
  8520. base_penalty = battle_config.vip_exp_penalty_base;
  8521. job_penalty = battle_config.vip_exp_penalty_job;
  8522. zeny_penalty = battle_config.vip_zeny_penalty;
  8523. } else {
  8524. base_penalty = battle_config.death_penalty_base;
  8525. job_penalty = battle_config.death_penalty_job;
  8526. zeny_penalty = battle_config.zeny_penalty;
  8527. }
  8528. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8529. switch (battle_config.death_penalty_type) {
  8530. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8531. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8532. }
  8533. if (base_penalty){ //recheck after altering to speedup
  8534. if (battle_config.pk_mode && src && src->type==BL_PC)
  8535. base_penalty *= 2;
  8536. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8537. }
  8538. }
  8539. else
  8540. base_penalty = 0;
  8541. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8542. switch (battle_config.death_penalty_type) {
  8543. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8544. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8545. }
  8546. if (job_penalty) {
  8547. if (battle_config.pk_mode && src && src->type==BL_PC)
  8548. job_penalty *= 2;
  8549. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8550. }
  8551. }
  8552. else
  8553. job_penalty = 0;
  8554. if (base_penalty || job_penalty) {
  8555. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8556. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8557. pc_lostexp(sd, base_penalty, job_penalty);
  8558. }
  8559. if( zeny_penalty > 0 && !mapdata->getMapFlag(MF_NOZENYPENALTY)) {
  8560. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8561. if(zeny_penalty)
  8562. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER);
  8563. }
  8564. }
  8565. if( mapdata->getMapFlag(MF_PVP_NIGHTMAREDROP) ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8566. for (const auto &it : mapdata->drop_list) {
  8567. int32 id = it.drop_id, per = it.drop_per;
  8568. enum e_nightmare_drop_type type = it.drop_type;
  8569. if(id == 0)
  8570. continue;
  8571. if(id == -1){
  8572. int32 eq_num=0,eq_n[MAX_INVENTORY];
  8573. memset(eq_n,0,sizeof(eq_n));
  8574. for(i=0;i<MAX_INVENTORY;i++) {
  8575. if( sd->inventory.u.items_inventory[i].nameid == 0 ){
  8576. continue;
  8577. }
  8578. if( !pc_candrop( sd, &sd->inventory.u.items_inventory[i] ) ){
  8579. continue;
  8580. }
  8581. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8582. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8583. || type == NMDT_ALL)
  8584. {
  8585. eq_n[eq_num++] = i;
  8586. }
  8587. }
  8588. if(eq_num > 0){
  8589. if(rnd()%10000 < per) {
  8590. int32 n = eq_n[rnd() % eq_num];
  8591. if(sd->inventory.u.items_inventory[n].equip)
  8592. pc_unequipitem(sd,n,3);
  8593. pc_dropitem(sd,n,1);
  8594. }
  8595. }
  8596. }
  8597. else if(id > 0) {
  8598. for(i=0;i<MAX_INVENTORY;i++){
  8599. if(sd->inventory.u.items_inventory[i].nameid == id
  8600. && pc_candrop( sd, &sd->inventory.u.items_inventory[i] )
  8601. && rnd()%10000 < per
  8602. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8603. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8604. || type == NMDT_ALL) ){
  8605. if(sd->inventory.u.items_inventory[i].equip)
  8606. pc_unequipitem(sd,i,3);
  8607. pc_dropitem(sd,i,1);
  8608. break;
  8609. }
  8610. }
  8611. }
  8612. }
  8613. }
  8614. // pvp
  8615. // disable certain pvp functions on pk_mode [Valaris]
  8616. if( !battle_config.pk_mode && mapdata->getMapFlag(MF_PVP) && !mapdata->getMapFlag(MF_PVP_NOCALCRANK) ) {
  8617. sd->pvp_point -= 5;
  8618. sd->pvp_lost++;
  8619. if( src && src->type == BL_PC ) {
  8620. map_session_data *ssd = (map_session_data *)src;
  8621. ssd->pvp_point++;
  8622. ssd->pvp_won++;
  8623. }
  8624. if( sd->pvp_point < 0 ) {
  8625. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8626. return 1|8;
  8627. }
  8628. }
  8629. //GvG
  8630. if( mapdata_flag_gvg2(mapdata) ) {
  8631. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8632. return 1|8;
  8633. }
  8634. else if( sd->bg_id ) {
  8635. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8636. if (bg) {
  8637. if (bg->cemetery.map > 0) { // Respawn by BG
  8638. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8639. return 1|8;
  8640. }
  8641. }
  8642. }
  8643. //Reset "can log out" tick.
  8644. if( battle_config.prevent_logout )
  8645. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8646. return 1;
  8647. }
  8648. void pc_revive(map_session_data *sd,uint32 hp, uint32 sp, uint32 ap) {
  8649. if(hp) clif_updatestatus(*sd,SP_HP);
  8650. if(sp) clif_updatestatus(*sd,SP_SP);
  8651. if(ap) clif_updatestatus(*sd,SP_AP);
  8652. pc_setstand(sd, true);
  8653. if(battle_config.pc_invincible_time > 0)
  8654. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8655. if (sd->state.gmaster_flag && sd->guild) {
  8656. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild->guild,GD_LEADERSHIP));
  8657. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild->guild,GD_GLORYWOUNDS));
  8658. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild->guild,GD_SOULCOLD));
  8659. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild->guild,GD_HAWKEYES));
  8660. }
  8661. }
  8662. bool pc_revive_item(map_session_data *sd) {
  8663. nullpo_retr(false, sd);
  8664. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8665. return false;
  8666. if (sd->sc.getSCE(SC_HELLPOWER)) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8667. return false;
  8668. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8669. uint8 hp = 100, sp = 100;
  8670. if (item_position < 0) {
  8671. if (sd->sc.getSCE(SC_LIGHT_OF_REGENE)) {
  8672. hp = sd->sc.getSCE(SC_LIGHT_OF_REGENE)->val2;
  8673. sp = 0;
  8674. }
  8675. else
  8676. return false;
  8677. }
  8678. if (!status_revive(&sd->bl, hp, sp))
  8679. return false;
  8680. if (item_position < 0)
  8681. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE);
  8682. else
  8683. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8684. clif_skill_nodamage(&sd->bl, sd->bl, ALL_RESURRECTION, 4);
  8685. return true;
  8686. }
  8687. // script
  8688. //
  8689. /*==========================================
  8690. * script reading pc status registry
  8691. *------------------------------------------*/
  8692. int64 pc_readparam(map_session_data* sd,int64 type)
  8693. {
  8694. int64 val = 0;
  8695. nullpo_ret(sd);
  8696. switch(type) {
  8697. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8698. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8699. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8700. case SP_ZENY: val = sd->status.zeny; break;
  8701. case SP_BASELEVEL: val = sd->status.base_level; break;
  8702. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8703. case SP_CLASS: val = sd->status.class_; break;
  8704. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8705. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8706. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8707. case SP_SEX: val = sd->status.sex; break;
  8708. case SP_WEIGHT: val = sd->weight; break;
  8709. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8710. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8711. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8712. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8713. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8714. case SP_HP: val = sd->battle_status.hp; break;
  8715. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8716. case SP_SP: val = sd->battle_status.sp; break;
  8717. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8718. case SP_AP: val = sd->battle_status.ap; break;
  8719. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8720. case SP_STR: val = sd->status.str; break;
  8721. case SP_AGI: val = sd->status.agi; break;
  8722. case SP_VIT: val = sd->status.vit; break;
  8723. case SP_INT: val = sd->status.int_; break;
  8724. case SP_DEX: val = sd->status.dex; break;
  8725. case SP_LUK: val = sd->status.luk; break;
  8726. case SP_POW: val = sd->status.pow; break;
  8727. case SP_STA: val = sd->status.sta; break;
  8728. case SP_WIS: val = sd->status.wis; break;
  8729. case SP_SPL: val = sd->status.spl; break;
  8730. case SP_CON: val = sd->status.con; break;
  8731. case SP_CRT: val = sd->status.crt; break;
  8732. case SP_KARMA: val = sd->status.karma; break;
  8733. case SP_MANNER: val = sd->status.manner; break;
  8734. case SP_FAME: val = sd->status.fame; break;
  8735. case SP_KILLERRID: val = sd->killerrid; break;
  8736. case SP_KILLEDRID: val = sd->killedrid; break;
  8737. case SP_KILLEDGID: val = sd->killedgid; break;
  8738. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8739. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8740. case SP_CHARRENAME: val = sd->status.rename; break;
  8741. case SP_CHARFONT: val = sd->status.font; break;
  8742. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8743. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8744. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8745. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8746. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8747. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8748. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8749. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8750. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8751. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8752. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  8753. case SP_BASE_ATK:
  8754. #ifdef RENEWAL
  8755. val = sd->bonus.eatk;
  8756. #else
  8757. val = sd->battle_status.batk;
  8758. #endif
  8759. break;
  8760. case SP_DEF1: val = sd->battle_status.def; break;
  8761. case SP_DEF2: val = sd->battle_status.def2; break;
  8762. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8763. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8764. case SP_HIT: val = sd->battle_status.hit; break;
  8765. case SP_FLEE1: val = sd->battle_status.flee; break;
  8766. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8767. case SP_PATK: val = sd->battle_status.patk; break;
  8768. case SP_SMATK: val = sd->battle_status.smatk; break;
  8769. case SP_RES: val = sd->battle_status.res; break;
  8770. case SP_MRES: val = sd->battle_status.mres; break;
  8771. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8772. case SP_CRATE: val = sd->battle_status.crate; break;
  8773. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8774. case SP_MAXHPRATE: val = sd->hprate; break;
  8775. case SP_MAXSPRATE: val = sd->sprate; break;
  8776. case SP_MAXAPRATE: val = sd->aprate; break;
  8777. case SP_SPRATE: val = sd->dsprate; break;
  8778. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8779. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8780. case SP_ASPD_RATE:
  8781. #ifndef RENEWAL_ASPD
  8782. val = sd->battle_status.aspd_rate;
  8783. #else
  8784. val = sd->battle_status.aspd_rate2;
  8785. #endif
  8786. break;
  8787. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8788. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8789. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8790. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8791. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8792. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8793. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8794. case SP_MATK_RATE: val = sd->matk_rate; break;
  8795. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  8796. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8797. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8798. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8799. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8800. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8801. case SP_HIT_RATE: val = sd->hit_rate; break;
  8802. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8803. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8804. case SP_DEF_RATE: val = sd->def_rate; break;
  8805. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8806. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8807. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8808. case SP_PATK_RATE: val = sd->patk_rate; break;
  8809. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8810. case SP_RES_RATE: val = sd->res_rate; break;
  8811. case SP_MRES_RATE: val = sd->mres_rate; break;
  8812. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8813. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8814. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8815. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8816. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8817. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8818. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8819. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8820. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8821. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8822. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8823. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8824. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8825. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8826. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8827. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8828. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8829. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8830. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8831. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8832. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8833. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8834. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8835. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8836. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8837. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8838. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8839. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8840. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8841. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8842. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8843. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8844. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8845. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8846. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8847. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8848. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8849. case SP_UNSTRIPABLE:
  8850. case SP_UNSTRIPABLE_ARMOR:
  8851. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8852. break;
  8853. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8854. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8855. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8856. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8857. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8858. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8859. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8860. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8861. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8862. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8863. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8864. case SP_EMATK: val = sd->bonus.ematk; break;
  8865. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8866. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8867. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8868. case SP_CASTRATE:
  8869. case SP_VARCASTRATE:
  8870. #ifdef RENEWAL_CAST
  8871. val = sd->bonus.varcastrate; break;
  8872. #else
  8873. val = sd->castrate; break;
  8874. #endif
  8875. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8876. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8877. default:
  8878. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8879. return -1;
  8880. }
  8881. return val;
  8882. }
  8883. /*==========================================
  8884. * script set pc status registry
  8885. *------------------------------------------*/
  8886. bool pc_setparam(map_session_data *sd,int64 type,int64 val_tmp)
  8887. {
  8888. nullpo_retr(false,sd);
  8889. int32 val = static_cast<uint32>(val_tmp);
  8890. switch(type){
  8891. case SP_BASELEVEL:
  8892. if (val > pc_maxbaselv(sd)) //Capping to max
  8893. val = pc_maxbaselv(sd);
  8894. if (val > sd->status.base_level) {
  8895. for( int32 i = 0; i < (int)( val - sd->status.base_level ); i++ ){
  8896. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8897. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8898. }
  8899. }
  8900. sd->status.base_level = val;
  8901. sd->status.base_exp = 0;
  8902. // clif_updatestatus(*sd, SP_BASELEVEL); // Gets updated at the bottom
  8903. clif_updatestatus(*sd, SP_NEXTBASEEXP);
  8904. clif_updatestatus(*sd, SP_STATUSPOINT);
  8905. clif_updatestatus(*sd, SP_TRAITPOINT);
  8906. clif_updatestatus(*sd, SP_BASEEXP);
  8907. status_calc_pc(sd, SCO_FORCE);
  8908. if(sd->status.party_id)
  8909. party_send_levelup(sd);
  8910. break;
  8911. case SP_JOBLEVEL:
  8912. if (val >= sd->status.job_level) {
  8913. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8914. sd->status.skill_point += val - sd->status.job_level;
  8915. clif_updatestatus(*sd, SP_SKILLPOINT);
  8916. }
  8917. sd->status.job_level = val;
  8918. sd->status.job_exp = 0;
  8919. // clif_updatestatus(*sd, SP_JOBLEVEL); // Gets updated at the bottom
  8920. clif_updatestatus(*sd, SP_NEXTJOBEXP);
  8921. clif_updatestatus(*sd, SP_JOBEXP);
  8922. status_calc_pc(sd, SCO_FORCE);
  8923. break;
  8924. case SP_SKILLPOINT:
  8925. sd->status.skill_point = val;
  8926. break;
  8927. case SP_STATUSPOINT:
  8928. sd->status.status_point = val;
  8929. break;
  8930. case SP_TRAITPOINT:
  8931. sd->status.trait_point = val;
  8932. break;
  8933. case SP_ZENY:
  8934. if( val < 0 )
  8935. return false;// can't set negative zeny
  8936. log_zeny(*sd, LOG_TYPE_SCRIPT, sd->status.char_id, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8937. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8938. break;
  8939. case SP_BASEEXP:
  8940. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8941. if (val_tmp < sd->status.base_exp) // Lost
  8942. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8943. else // Gained
  8944. pc_gainexp(sd, nullptr, val_tmp - sd->status.base_exp, 0, 2);
  8945. return true;
  8946. case SP_JOBEXP:
  8947. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8948. if (val_tmp < sd->status.job_exp) // Lost
  8949. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8950. else // Gained
  8951. pc_gainexp(sd, nullptr, 0, val_tmp - sd->status.job_exp, 2);
  8952. return true;
  8953. case SP_SEX:
  8954. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8955. break;
  8956. case SP_WEIGHT:
  8957. sd->weight = val;
  8958. break;
  8959. case SP_MAXWEIGHT:
  8960. sd->max_weight = val;
  8961. break;
  8962. case SP_HP:
  8963. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  8964. break;
  8965. case SP_MAXHP:
  8966. if (sd->status.base_level < 100)
  8967. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8968. else if (sd->status.base_level < 151)
  8969. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8970. else
  8971. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8972. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8973. {
  8974. sd->battle_status.hp = sd->battle_status.max_hp;
  8975. clif_updatestatus(*sd, SP_HP);
  8976. }
  8977. break;
  8978. case SP_SP:
  8979. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  8980. break;
  8981. case SP_MAXSP:
  8982. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8983. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8984. {
  8985. sd->battle_status.sp = sd->battle_status.max_sp;
  8986. clif_updatestatus(*sd, SP_SP);
  8987. }
  8988. break;
  8989. case SP_AP:
  8990. sd->battle_status.ap = cap_value(val, 0, (int)sd->battle_status.max_ap);
  8991. break;
  8992. case SP_MAXAP:
  8993. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  8994. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  8995. sd->battle_status.ap = sd->battle_status.max_ap;
  8996. clif_updatestatus(*sd, SP_AP);
  8997. }
  8998. break;
  8999. case SP_STR:
  9000. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  9001. break;
  9002. case SP_AGI:
  9003. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  9004. break;
  9005. case SP_VIT:
  9006. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  9007. break;
  9008. case SP_INT:
  9009. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  9010. break;
  9011. case SP_DEX:
  9012. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  9013. break;
  9014. case SP_LUK:
  9015. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  9016. break;
  9017. case SP_POW:
  9018. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  9019. break;
  9020. case SP_STA:
  9021. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  9022. break;
  9023. case SP_WIS:
  9024. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  9025. break;
  9026. case SP_SPL:
  9027. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  9028. break;
  9029. case SP_CON:
  9030. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  9031. break;
  9032. case SP_CRT:
  9033. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  9034. break;
  9035. case SP_KARMA:
  9036. sd->status.karma = val;
  9037. break;
  9038. case SP_MANNER:
  9039. sd->status.manner = val;
  9040. if( val < 0 )
  9041. sc_start(nullptr, &sd->bl, SC_NOCHAT, 100, 0, 0);
  9042. else {
  9043. status_change_end(&sd->bl, SC_NOCHAT);
  9044. clif_manner_message(sd, 5);
  9045. }
  9046. return true; // status_change_start/status_change_end already sends packets warning the client
  9047. case SP_FAME:
  9048. sd->status.fame = val;
  9049. break;
  9050. case SP_KILLERRID:
  9051. sd->killerrid = val;
  9052. return true;
  9053. case SP_KILLEDRID:
  9054. sd->killedrid = val;
  9055. return true;
  9056. case SP_KILLEDGID:
  9057. sd->killedgid = val;
  9058. return true;
  9059. case SP_CHARMOVE:
  9060. sd->status.character_moves = val;
  9061. return true;
  9062. case SP_CHARRENAME:
  9063. sd->status.rename = val;
  9064. return true;
  9065. case SP_CHARFONT:
  9066. sd->status.font = val;
  9067. clif_font(sd);
  9068. return true;
  9069. case SP_BANK_VAULT:
  9070. if (val < 0)
  9071. return false;
  9072. log_zeny(*sd, LOG_TYPE_BANK, sd->status.char_id, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  9073. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  9074. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  9075. return true;
  9076. case SP_ROULETTE_BRONZE:
  9077. sd->roulette_point.bronze = val;
  9078. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  9079. return true;
  9080. case SP_ROULETTE_SILVER:
  9081. sd->roulette_point.silver = val;
  9082. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  9083. return true;
  9084. case SP_ROULETTE_GOLD:
  9085. sd->roulette_point.gold = val;
  9086. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  9087. return true;
  9088. case SP_CASHPOINTS:
  9089. if (val < 0)
  9090. return false;
  9091. if (!sd->state.connect_new)
  9092. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_CASHPOINT)));
  9093. sd->cashPoints = cap_value(val, 0, MAX_CASHPOINT);
  9094. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  9095. return true;
  9096. case SP_KAFRAPOINTS:
  9097. if (val < 0)
  9098. return false;
  9099. if (!sd->state.connect_new)
  9100. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_KAFRAPOINT)));
  9101. sd->kafraPoints = cap_value(val, 0, MAX_KAFRAPOINT);
  9102. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  9103. return true;
  9104. case SP_PCDIECOUNTER:
  9105. if (val < 0)
  9106. return false;
  9107. if (sd->die_counter == val)
  9108. return true;
  9109. sd->die_counter = val;
  9110. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  9111. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  9112. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  9113. return true;
  9114. case SP_COOKMASTERY:
  9115. if (sd->cook_mastery == val)
  9116. return true;
  9117. val = cap_value(val, 0, 1999);
  9118. sd->cook_mastery = val;
  9119. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  9120. return true;
  9121. default:
  9122. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  9123. return false;
  9124. }
  9125. clif_updatestatus(*sd,static_cast<_sp>(type));
  9126. return true;
  9127. }
  9128. /*==========================================
  9129. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  9130. *------------------------------------------*/
  9131. void pc_heal(map_session_data *sd,uint32 hp,uint32 sp, uint32 ap, int32 type)
  9132. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  9133. nullpo_retv(sd);
  9134. if (type&2) {
  9135. if (hp || type&4) {
  9136. clif_heal( *sd, SP_HP, hp );
  9137. clif_update_hp(*sd);
  9138. }
  9139. if (sp)
  9140. clif_heal( *sd, SP_SP, sp );
  9141. if (ap)
  9142. clif_heal( *sd, SP_AP, ap );
  9143. } else {
  9144. if(hp)
  9145. clif_updatestatus(*sd,SP_HP);
  9146. if(sp)
  9147. clif_updatestatus(*sd,SP_SP);
  9148. if (ap)
  9149. clif_updatestatus(*sd,SP_AP);
  9150. }
  9151. return;
  9152. }
  9153. /**
  9154. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  9155. * @param sd: Player data
  9156. * @param itemid: Item ID
  9157. * @param hp: HP to heal
  9158. * @param sp: SP to heal
  9159. * @return Amount healed to an object
  9160. */
  9161. int32 pc_itemheal(map_session_data *sd, t_itemid itemid, int32 hp, int32 sp)
  9162. {
  9163. int32 bonus, tmp, penalty = 0;
  9164. if (hp) {
  9165. bonus = 100 + (sd->battle_status.vit * 2) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9166. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9167. if (potion_flag == 2) {
  9168. bonus += bonus * 50 / 100;
  9169. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_ROGUE)
  9170. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  9171. }
  9172. //All item bonuses.
  9173. bonus += sd->bonus.itemhealrate2;
  9174. //Item Group bonuses
  9175. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  9176. //Individual item bonuses.
  9177. for(const auto &it : sd->itemhealrate) {
  9178. if (it.id == itemid) {
  9179. bonus += bonus * it.val / 100;
  9180. break;
  9181. }
  9182. }
  9183. // Recovery Potion
  9184. if (sd->sc.getSCE(SC_INCHEALRATE))
  9185. bonus += bonus * sd->sc.getSCE(SC_INCHEALRATE)->val1 / 100;
  9186. tmp = hp * bonus / 100; // Overflow check
  9187. if (bonus != 100 && tmp > hp)
  9188. hp = tmp;
  9189. }
  9190. if (sp) {
  9191. bonus = 100 + (sd->battle_status.int_ * 2) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9192. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9193. if (potion_flag == 2)
  9194. bonus += bonus * 50 / 100;
  9195. // All item bonuses.
  9196. bonus += sd->bonus.itemsphealrate2;
  9197. // Item Group bonuses
  9198. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  9199. // Individual item bonuses.
  9200. for( const auto &it : sd->itemsphealrate ){
  9201. if( it.id == itemid ){
  9202. bonus += bonus * it.val / 100;
  9203. break;
  9204. }
  9205. }
  9206. tmp = sp * bonus / 100; // Overflow check
  9207. if (bonus != 100 && tmp > sp)
  9208. sp = tmp;
  9209. }
  9210. if (!sd->sc.empty()) {
  9211. // Critical Wound and Death Hurt stack
  9212. if (sd->sc.getSCE(SC_CRITICALWOUND))
  9213. penalty += sd->sc.getSCE(SC_CRITICALWOUND)->val2;
  9214. if (sd->sc.getSCE(SC_DEATHHURT) && sd->sc.getSCE(SC_DEATHHURT)->val3 == 1)
  9215. penalty += 20;
  9216. if (sd->sc.getSCE(SC_NORECOVER_STATE))
  9217. penalty = 100;
  9218. if (sd->sc.getSCE(SC_VITALITYACTIVATION))
  9219. hp += hp / 2; // 1.5 times
  9220. if (sd->sc.getSCE(SC_WATER_INSIGNIA) && sd->sc.getSCE(SC_WATER_INSIGNIA)->val1 == 2) {
  9221. hp += hp / 10;
  9222. sp += sp / 10;
  9223. }
  9224. #ifdef RENEWAL
  9225. if (sd->sc.getSCE(SC_APPLEIDUN))
  9226. hp += sd->sc.getSCE(SC_APPLEIDUN)->val3 / 100;
  9227. #endif
  9228. if (penalty > 0) {
  9229. hp -= hp * penalty / 100;
  9230. sp -= sp * penalty / 100;
  9231. }
  9232. #ifdef RENEWAL
  9233. if (sd->sc.getSCE(SC_EXTREMITYFIST))
  9234. sp = 0;
  9235. #endif
  9236. if (sd->sc.getSCE(SC_BITESCAR))
  9237. hp = 0;
  9238. }
  9239. return status_heal(&sd->bl, hp, sp, 1);
  9240. }
  9241. /*==========================================
  9242. * HP/SP Recovery
  9243. * Heal player hp nad/or sp by rate
  9244. *------------------------------------------*/
  9245. int32 pc_percentheal(map_session_data *sd,int32 hp,int32 sp)
  9246. {
  9247. nullpo_ret(sd);
  9248. if (hp > 100) hp = 100;
  9249. else if (hp <-100) hp = -100;
  9250. if (sp > 100) sp = 100;
  9251. else if (sp <-100) sp = -100;
  9252. if(hp >= 0 && sp >= 0) //Heal
  9253. return status_percent_heal(&sd->bl, hp, sp);
  9254. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  9255. return status_percent_damage(nullptr, &sd->bl, hp, sp, hp==-100);
  9256. //Crossed signs
  9257. if(hp) {
  9258. if(hp > 0)
  9259. status_percent_heal(&sd->bl, hp, 0);
  9260. else
  9261. status_percent_damage(nullptr, &sd->bl, hp, 0, hp==-100);
  9262. }
  9263. if(sp) {
  9264. if(sp > 0)
  9265. status_percent_heal(&sd->bl, 0, sp);
  9266. else
  9267. status_percent_damage(nullptr, &sd->bl, 0, sp, false);
  9268. }
  9269. return 0;
  9270. }
  9271. static int32 jobchange_killclone(struct block_list *bl, va_list ap)
  9272. {
  9273. struct mob_data *md;
  9274. int32 flag;
  9275. md = (struct mob_data *)bl;
  9276. nullpo_ret(md);
  9277. flag = va_arg(ap, int);
  9278. if (md->master_id && md->special_state.clone && md->master_id == flag)
  9279. status_kill(&md->bl);
  9280. return 1;
  9281. }
  9282. /**
  9283. * Called when player changes job
  9284. * Rewrote to make it tidider [Celest]
  9285. * @param sd
  9286. * @param job JOB ID. See enum e_job
  9287. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  9288. * @return True if success, false if failed
  9289. **/
  9290. bool pc_jobchange(map_session_data *sd,int32 job, char upper)
  9291. {
  9292. int32 i, fame_flag = 0;
  9293. nullpo_retr(false,sd);
  9294. if (job < 0)
  9295. return false;
  9296. //Normalize job.
  9297. uint64 b_class = pc_jobid2mapid(job);
  9298. if (b_class == -1)
  9299. return false;
  9300. switch (upper) {
  9301. case 1:
  9302. b_class|= JOBL_UPPER;
  9303. break;
  9304. case 2:
  9305. b_class|= JOBL_BABY;
  9306. break;
  9307. }
  9308. //This will automatically adjust bard/dancer classes to the correct gender
  9309. //That is, if you try to jobchange into dancer, it will turn you to bard.
  9310. job = pc_mapid2jobid(b_class, sd->status.sex);
  9311. if (job == -1)
  9312. return false;
  9313. if ((unsigned short)b_class == sd->class_)
  9314. return false; //Nothing to change.
  9315. // If the job does not exist in the job db, dont allow changing to it
  9316. if( !job_db.exists( job ) ){
  9317. return false;
  9318. }
  9319. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  9320. // Changing to 4th job
  9321. sd->change_level_4th = sd->status.job_level;
  9322. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  9323. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  9324. // changing from 2nd to 3rd job
  9325. sd->change_level_3rd = sd->status.job_level;
  9326. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  9327. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  9328. // changing from 1st to 2nd job
  9329. sd->change_level_2nd = sd->status.job_level;
  9330. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  9331. }
  9332. if(sd->cloneskill_idx > 0) {
  9333. pc_skill_plagiarism_reset(*sd, 1);
  9334. }
  9335. if(sd->reproduceskill_idx > 0) {
  9336. pc_skill_plagiarism_reset(*sd, 2);
  9337. }
  9338. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  9339. status_db.changeSkillTree(sd);
  9340. }
  9341. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  9342. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  9343. pc_resetfeel(sd);
  9344. pc_resethate(sd);
  9345. }
  9346. // Reset body style to 0 before changing job to avoid
  9347. // errors since not every job has a alternate outfit.
  9348. sd->status.body = 0;
  9349. clif_changelook(&sd->bl,LOOK_BODY2,0);
  9350. sd->status.class_ = job;
  9351. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  9352. uint64 previous_class = sd->class_;
  9353. sd->class_ = (unsigned short)b_class;
  9354. sd->status.job_level=1;
  9355. sd->status.job_exp=0;
  9356. if (sd->status.base_level > pc_maxbaselv(sd)) {
  9357. sd->status.base_level = pc_maxbaselv(sd);
  9358. sd->status.base_exp=0;
  9359. pc_resetstate(sd);
  9360. clif_updatestatus(*sd,SP_STATUSPOINT);
  9361. clif_updatestatus(*sd,SP_TRAITPOINT);
  9362. clif_updatestatus(*sd,SP_BASELEVEL);
  9363. clif_updatestatus(*sd,SP_BASEEXP);
  9364. clif_updatestatus(*sd,SP_NEXTBASEEXP);
  9365. }
  9366. // Give or reduce transcendent status points
  9367. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  9368. sd->status.status_point += battle_config.transcendent_status_points;
  9369. clif_updatestatus(*sd,SP_STATUSPOINT);
  9370. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  9371. if( sd->status.status_point < battle_config.transcendent_status_points ){
  9372. // The player already used his bonus points, so we have to reset his status points
  9373. pc_resetstate(sd);
  9374. }else{
  9375. sd->status.status_point -= battle_config.transcendent_status_points;
  9376. clif_updatestatus(*sd,SP_STATUSPOINT);
  9377. }
  9378. }
  9379. // Give or reduce trait status points
  9380. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  9381. sd->status.trait_point += battle_config.trait_points_job_change;
  9382. clif_updatestatus(*sd, SP_TRAITPOINT);
  9383. clif_updatestatus(*sd, SP_UPOW);
  9384. clif_updatestatus(*sd, SP_USTA);
  9385. clif_updatestatus(*sd, SP_UWIS);
  9386. clif_updatestatus(*sd, SP_USPL);
  9387. clif_updatestatus(*sd, SP_UCON);
  9388. clif_updatestatus(*sd, SP_UCRT);
  9389. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  9390. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  9391. // Player may have already used the trait status points. Force a reset.
  9392. pc_resetstate(sd);
  9393. } else {
  9394. sd->status.trait_point = 0;
  9395. clif_updatestatus(*sd, SP_TRAITPOINT);
  9396. clif_updatestatus(*sd, SP_UPOW);
  9397. clif_updatestatus(*sd, SP_USTA);
  9398. clif_updatestatus(*sd, SP_UWIS);
  9399. clif_updatestatus(*sd, SP_USPL);
  9400. clif_updatestatus(*sd, SP_UCON);
  9401. clif_updatestatus(*sd, SP_UCRT);
  9402. }
  9403. }
  9404. clif_updatestatus(*sd,SP_JOBLEVEL);
  9405. clif_updatestatus(*sd,SP_JOBEXP);
  9406. clif_updatestatus(*sd,SP_NEXTJOBEXP);
  9407. for(i=0;i<EQI_MAX;i++) {
  9408. if(sd->equip_index[i] >= 0)
  9409. if( pc_isequip( sd, sd->equip_index[i] ) != ITEM_EQUIP_ACK_OK ){
  9410. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  9411. }
  9412. }
  9413. //Change look, if disguised, you need to undisguise
  9414. //to correctly calculate new job sprite without
  9415. if (sd->disguise)
  9416. pc_disguise(sd, 0);
  9417. status_set_viewdata(&sd->bl, job);
  9418. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  9419. #if PACKETVER >= 20151001
  9420. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  9421. #endif
  9422. if(sd->vd.cloth_color)
  9423. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9424. /*
  9425. if(sd->vd.body_style)
  9426. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9427. */
  9428. //Update skill tree.
  9429. pc_calc_skilltree(sd);
  9430. clif_skillinfoblock(sd);
  9431. if (sd->ed)
  9432. elemental_delete(sd->ed);
  9433. if (sd->state.vending)
  9434. vending_closevending(sd);
  9435. if (sd->state.buyingstore)
  9436. buyingstore_close(sd);
  9437. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  9438. //Remove peco/cart/falcon
  9439. i = sd->sc.option;
  9440. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  9441. i&=~OPTION_RIDING;
  9442. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9443. i&=~OPTION_FALCON;
  9444. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9445. i&=~OPTION_DRAGON;
  9446. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9447. i&=~OPTION_WUGRIDER;
  9448. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9449. i&=~OPTION_WUG;
  9450. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9451. i&=~OPTION_MADOGEAR;
  9452. #ifndef NEW_CARTS
  9453. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9454. i&=~OPTION_CART;
  9455. #else
  9456. if( sd->sc.getSCE(SC_PUSH_CART) && !pc_checkskill(sd, MC_PUSHCART) )
  9457. pc_setcart(sd, 0);
  9458. #endif
  9459. if(i != sd->sc.option)
  9460. pc_setoption(sd, i);
  9461. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9462. hom_vaporize(sd, HOM_ST_ACTIVE);
  9463. if (sd->sc.getSCE(SC_SPRITEMABLE) && !pc_checkskill(sd, SU_SPRITEMABLE))
  9464. status_change_end(&sd->bl, SC_SPRITEMABLE);
  9465. if (sd->sc.getSCE(SC_SOULATTACK) && !pc_checkskill(sd, SU_SOULATTACK))
  9466. status_change_end(&sd->bl, SC_SOULATTACK);
  9467. if( sd->sc.getSCE(SC_SPIRIT) ){
  9468. status_change_end( &sd->bl, SC_SPIRIT );
  9469. }
  9470. if(sd->status.manner < 0){
  9471. clif_changemanner( *sd );
  9472. clif_updatestatus(*sd,SP_MANNER);
  9473. }
  9474. status_calc_pc(sd,SCO_FORCE);
  9475. pc_checkallowskill(sd);
  9476. pc_equiplookall(sd);
  9477. pc_show_questinfo(sd);
  9478. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9479. if( sd->status.party_id ){
  9480. struct party_data* p;
  9481. if( ( p = party_search( sd->status.party_id ) ) != nullptr ){
  9482. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9483. if( i < MAX_PARTY ){
  9484. p->party.member[i].class_ = sd->status.class_;
  9485. clif_party_job_and_level( *sd );
  9486. }
  9487. }
  9488. }
  9489. chrif_save(sd, CSAVE_NORMAL);
  9490. //if you were previously famous, not anymore.
  9491. if (fame_flag)
  9492. chrif_buildfamelist();
  9493. else if (sd->status.fame > 0) {
  9494. //It may be that now they are famous?
  9495. switch (sd->class_&MAPID_UPPERMASK) {
  9496. case MAPID_BLACKSMITH:
  9497. case MAPID_ALCHEMIST:
  9498. case MAPID_TAEKWON:
  9499. chrif_buildfamelist();
  9500. break;
  9501. }
  9502. }
  9503. return true;
  9504. }
  9505. /*==========================================
  9506. * Tell client player sd has change equipement
  9507. *------------------------------------------*/
  9508. void pc_equiplookall(map_session_data *sd)
  9509. {
  9510. nullpo_retv(sd);
  9511. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9512. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9513. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9514. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9515. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9516. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9517. }
  9518. /*==========================================
  9519. * Tell client player sd has change look (hair,equip...)
  9520. *------------------------------------------*/
  9521. void pc_changelook(map_session_data *sd,int32 type,int32 val) {
  9522. nullpo_retv(sd);
  9523. switch(type) {
  9524. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9525. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9526. if (sd->status.hair != val) {
  9527. sd->status.hair = val;
  9528. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9529. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9530. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9531. }
  9532. break;
  9533. case LOOK_WEAPON:
  9534. sd->status.weapon = val;
  9535. break;
  9536. case LOOK_HEAD_BOTTOM:
  9537. sd->status.head_bottom = val;
  9538. sd->setlook_head_bottom = val;
  9539. break;
  9540. case LOOK_HEAD_TOP:
  9541. sd->status.head_top = val;
  9542. sd->setlook_head_top = val;
  9543. break;
  9544. case LOOK_HEAD_MID:
  9545. sd->status.head_mid = val;
  9546. sd->setlook_head_mid = val;
  9547. break;
  9548. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9549. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9550. if (sd->status.hair_color != val) {
  9551. sd->status.hair_color = val;
  9552. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9553. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9554. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9555. }
  9556. break;
  9557. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9558. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9559. sd->status.clothes_color = val;
  9560. break;
  9561. case LOOK_SHIELD:
  9562. sd->status.shield = val;
  9563. break;
  9564. case LOOK_SHOES:
  9565. break;
  9566. case LOOK_ROBE:
  9567. sd->status.robe = val;
  9568. sd->setlook_robe = val;
  9569. break;
  9570. case LOOK_BODY2:
  9571. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9572. sd->status.body = val;
  9573. break;
  9574. }
  9575. clif_changelook(&sd->bl, type, val);
  9576. }
  9577. /*==========================================
  9578. * Give an option (type) to player (sd) and display it to client
  9579. *------------------------------------------*/
  9580. void pc_setoption(map_session_data *sd,int32 type, int32 subtype)
  9581. {
  9582. int32 p_type, new_look=0;
  9583. nullpo_retv(sd);
  9584. p_type = sd->sc.option;
  9585. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9586. sd->sc.option=type;
  9587. clif_changeoption(&sd->bl);
  9588. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9589. { // Mounting
  9590. clif_status_load(&sd->bl,EFST_RIDING,1);
  9591. status_calc_pc(sd,SCO_NONE);
  9592. }
  9593. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9594. { // Dismount
  9595. clif_status_load(&sd->bl,EFST_RIDING,0);
  9596. status_calc_pc(sd,SCO_NONE);
  9597. }
  9598. #ifndef NEW_CARTS
  9599. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9600. clif_cartlist(sd);
  9601. clif_updatestatus(*sd, SP_CARTINFO);
  9602. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9603. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9604. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9605. clif_clearcart(sd->fd);
  9606. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9607. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9608. }
  9609. #endif
  9610. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9611. clif_status_load(&sd->bl,EFST_FALCON,1);
  9612. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9613. clif_status_load(&sd->bl,EFST_FALCON,0);
  9614. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9615. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9616. status_calc_pc(sd,SCO_NONE);
  9617. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9618. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9619. status_calc_pc(sd,SCO_NONE);
  9620. }
  9621. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9622. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9623. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9624. status_change_end(&sd->bl, SC_MADOGEAR);
  9625. }
  9626. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9627. new_look = JOB_STAR_GLADIATOR2;
  9628. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9629. new_look = -1;
  9630. if (sd->disguise || !new_look)
  9631. return; //Disguises break sprite changes
  9632. if (new_look < 0) { //Restore normal look.
  9633. status_set_viewdata(&sd->bl, sd->status.class_);
  9634. new_look = sd->vd.class_;
  9635. }
  9636. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  9637. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9638. if (sd->vd.cloth_color)
  9639. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9640. if( sd->vd.body_style )
  9641. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9642. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9643. }
  9644. /**
  9645. * Give player a cart
  9646. * @param sd Player
  9647. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9648. **/
  9649. bool pc_setcart(map_session_data *sd,int32 type) {
  9650. #ifndef NEW_CARTS
  9651. int32 cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9652. int32 option;
  9653. #endif
  9654. nullpo_retr(false,sd);
  9655. if( type < 0 || type > MAX_CARTS )
  9656. return false;// Never trust the values sent by the client! [Skotlex]
  9657. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9658. return false;// Push cart is required
  9659. #ifdef NEW_CARTS
  9660. switch( type ) {
  9661. case 0:
  9662. if( !sd->sc.getSCE(SC_PUSH_CART) )
  9663. return 0;
  9664. status_change_end(&sd->bl,SC_PUSH_CART);
  9665. clif_clearcart(sd->fd);
  9666. break;
  9667. default:/* everything else is an allowed ID so we can move on */
  9668. if( !sd->sc.getSCE(SC_PUSH_CART) ) { /* first time, so fill cart data */
  9669. clif_cartlist(sd);
  9670. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9671. }
  9672. clif_updatestatus(*sd, SP_CARTINFO);
  9673. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9674. break;
  9675. }
  9676. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9677. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9678. #else
  9679. // Update option
  9680. option = sd->sc.option;
  9681. option &= ~OPTION_CART;// clear cart bits
  9682. option |= cart[type]; // set cart
  9683. pc_setoption(sd, option);
  9684. #endif
  9685. return true;
  9686. }
  9687. /*==========================================
  9688. * Give player a falcon
  9689. *------------------------------------------*/
  9690. void pc_setfalcon(map_session_data* sd, int32 flag)
  9691. {
  9692. if( flag ){
  9693. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9694. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9695. } else if( pc_isfalcon(sd) ){
  9696. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9697. }
  9698. }
  9699. /*==========================================
  9700. * Set player riding
  9701. *------------------------------------------*/
  9702. void pc_setriding(map_session_data* sd, int32 flag)
  9703. {
  9704. if( sd->sc.getSCE(SC_ALL_RIDING) )
  9705. return;
  9706. if( flag ){
  9707. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9708. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9709. } else if( pc_isriding(sd) ){
  9710. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9711. }
  9712. }
  9713. /**
  9714. * Give player a mado
  9715. * @param sd: Player
  9716. * @param flag: Enable or disable mado
  9717. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9718. */
  9719. void pc_setmadogear(map_session_data *sd, bool flag, e_mado_type type)
  9720. {
  9721. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9722. return;
  9723. if (flag) {
  9724. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9725. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9726. } else if (pc_ismadogear(sd)) {
  9727. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9728. }
  9729. }
  9730. /*==========================================
  9731. * Check if player can drop an item
  9732. *------------------------------------------*/
  9733. bool pc_candrop(map_session_data *sd, struct item *item)
  9734. {
  9735. if (sd->sc.cant.drop)
  9736. return false;
  9737. if( item && itemdb_ishatched_egg(item) )
  9738. return false;
  9739. if (pc_has_permission(sd, PC_PERM_TRADE_UNCONDITIONAL)) // no restriction
  9740. return true;
  9741. if( !pc_can_give_items(sd) )
  9742. return false;
  9743. if( item && (item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) )
  9744. return false;
  9745. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9746. }
  9747. /*==========================================
  9748. * Read '@type' variables (temporary numeric char reg)
  9749. *------------------------------------------*/
  9750. int64 pc_readreg(map_session_data* sd, int64 reg)
  9751. {
  9752. return i64db_i64get(sd->regs.vars, reg);
  9753. }
  9754. /*==========================================
  9755. * Set '@type' variables (temporary numeric char reg)
  9756. *------------------------------------------*/
  9757. bool pc_setreg(map_session_data* sd, int64 reg, int64 val)
  9758. {
  9759. uint32 index = script_getvaridx(reg);
  9760. nullpo_retr(false, sd);
  9761. if( val ) {
  9762. i64db_i64put(sd->regs.vars, reg, val);
  9763. if( index )
  9764. script_array_update(&sd->regs, reg, false);
  9765. } else {
  9766. i64db_remove(sd->regs.vars, reg);
  9767. if( index )
  9768. script_array_update(&sd->regs, reg, true);
  9769. }
  9770. return true;
  9771. }
  9772. /*==========================================
  9773. * Read '@type$' variables (temporary string char reg)
  9774. *------------------------------------------*/
  9775. char* pc_readregstr(map_session_data* sd, int64 reg)
  9776. {
  9777. struct script_reg_str *p = nullptr;
  9778. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9779. return p ? p->value : nullptr;
  9780. }
  9781. /*==========================================
  9782. * Set '@type$' variables (temporary string char reg)
  9783. *------------------------------------------*/
  9784. bool pc_setregstr(map_session_data* sd, int64 reg, const char* str)
  9785. {
  9786. struct script_reg_str *p = nullptr;
  9787. uint32 index = script_getvaridx(reg);
  9788. DBData prev;
  9789. nullpo_retr(false, sd);
  9790. if( str[0] ) {
  9791. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9792. p->value = aStrdup(str);
  9793. p->flag.type = 1;
  9794. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9795. p = (struct script_reg_str *)db_data2ptr(&prev);
  9796. if( p->value )
  9797. aFree(p->value);
  9798. ers_free(str_reg_ers, p);
  9799. } else {
  9800. if( index )
  9801. script_array_update(&sd->regs, reg, false);
  9802. }
  9803. } else {
  9804. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9805. p = (struct script_reg_str *)db_data2ptr(&prev);
  9806. if( p->value )
  9807. aFree(p->value);
  9808. ers_free(str_reg_ers, p);
  9809. if( index )
  9810. script_array_update(&sd->regs, reg, true);
  9811. }
  9812. }
  9813. return true;
  9814. }
  9815. /**
  9816. * Serves the following variable types:
  9817. * - 'type' (permanent numeric char reg)
  9818. * - '#type' (permanent numeric account reg)
  9819. * - '##type' (permanent numeric account reg2)
  9820. **/
  9821. int64 pc_readregistry(map_session_data *sd, int64 reg)
  9822. {
  9823. struct script_reg_num *p = nullptr;
  9824. if (!sd->vars_ok) {
  9825. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9826. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9827. //intif->request_registry(sd,type==3?4:type);
  9828. set_eof(sd->fd);
  9829. return 0;
  9830. }
  9831. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9832. return p ? p->value : 0;
  9833. }
  9834. /**
  9835. * Serves the following variable types:
  9836. * - 'type$' (permanent str char reg)
  9837. * - '#type$' (permanent str account reg)
  9838. * - '##type$' (permanent str account reg2)
  9839. **/
  9840. char* pc_readregistry_str(map_session_data *sd, int64 reg)
  9841. {
  9842. struct script_reg_str *p = nullptr;
  9843. if (!sd->vars_ok) {
  9844. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9845. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9846. //intif->request_registry(sd,type==3?4:type);
  9847. set_eof(sd->fd);
  9848. return nullptr;
  9849. }
  9850. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9851. return p ? p->value : nullptr;
  9852. }
  9853. /**
  9854. * Serves the following variable types:
  9855. * - 'type' (permanent numeric char reg)
  9856. * - '#type' (permanent numeric account reg)
  9857. * - '##type' (permanent numeric account reg2)
  9858. **/
  9859. bool pc_setregistry(map_session_data *sd, int64 reg, int64 val)
  9860. {
  9861. struct script_reg_num *p = nullptr;
  9862. const char *regname = get_str(script_getvarid(reg));
  9863. uint32 index = script_getvaridx(reg);
  9864. if ( !reg_load && !sd->vars_ok ) {
  9865. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9866. return false;
  9867. }
  9868. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9869. if( val ) {
  9870. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9871. script_array_update(&sd->regs, reg, false);
  9872. p->value = val;
  9873. } else {
  9874. p->value = 0;
  9875. if( index )
  9876. script_array_update(&sd->regs, reg, true);
  9877. }
  9878. if (!reg_load)
  9879. p->flag.update = 1;/* either way, it will require either delete or replace */
  9880. } else if( val ) {
  9881. DBData prev;
  9882. if( index )
  9883. script_array_update(&sd->regs, reg, false);
  9884. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9885. p->value = val;
  9886. if (!reg_load)
  9887. p->flag.update = 1;
  9888. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9889. p = (struct script_reg_num *)db_data2ptr(&prev);
  9890. ers_free(num_reg_ers, p);
  9891. }
  9892. }
  9893. if (!reg_load && p)
  9894. sd->vars_dirty = true;
  9895. return true;
  9896. }
  9897. /**
  9898. * Serves the following variable types:
  9899. * - 'type$' (permanent str char reg)
  9900. * - '#type$' (permanent str account reg)
  9901. * - '##type$' (permanent str account reg2)
  9902. **/
  9903. bool pc_setregistry_str(map_session_data *sd, int64 reg, const char *val)
  9904. {
  9905. struct script_reg_str *p = nullptr;
  9906. const char *regname = get_str(script_getvarid(reg));
  9907. uint32 index = script_getvaridx(reg);
  9908. size_t vlen = 0;
  9909. if (!reg_load && !sd->vars_ok) {
  9910. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9911. return false;
  9912. }
  9913. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9914. {
  9915. 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);
  9916. return false;
  9917. }
  9918. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9919. if( val[0] ) {
  9920. if( p->value )
  9921. aFree(p->value);
  9922. else if ( index ) // an entry that was deleted, so we reset
  9923. script_array_update(&sd->regs, reg, false);
  9924. p->value = aStrdup(val);
  9925. } else {
  9926. if (p->value)
  9927. aFree(p->value);
  9928. p->value = nullptr;
  9929. if( index )
  9930. script_array_update(&sd->regs, reg, true);
  9931. }
  9932. if( !reg_load )
  9933. p->flag.update = 1; // either way, it will require either delete or replace
  9934. } else if( val[0] ) {
  9935. DBData prev;
  9936. if( index )
  9937. script_array_update(&sd->regs, reg, false);
  9938. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9939. p->value = aStrdup(val);
  9940. if( !reg_load )
  9941. p->flag.update = 1;
  9942. p->flag.type = 1;
  9943. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9944. p = (struct script_reg_str *)db_data2ptr(&prev);
  9945. if( p->value )
  9946. aFree(p->value);
  9947. ers_free(str_reg_ers, p);
  9948. }
  9949. }
  9950. if( !reg_load && p )
  9951. sd->vars_dirty = true;
  9952. return true;
  9953. }
  9954. /**
  9955. * Set value of player variable
  9956. * @param sd Player
  9957. * @param reg Variable name
  9958. * @param value
  9959. * @return True if success, false if failed.
  9960. **/
  9961. bool pc_setreg2(map_session_data *sd, const char *reg, int64 val) {
  9962. char prefix = reg[0];
  9963. nullpo_retr(false, sd);
  9964. if (reg[strlen(reg)-1] == '$') {
  9965. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9966. return false;
  9967. }
  9968. val = cap_value(val, INT_MIN, INT_MAX);
  9969. switch (prefix) {
  9970. case '.':
  9971. case '\'':
  9972. case '$':
  9973. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9974. return false;
  9975. case '@':
  9976. return pc_setreg(sd, add_str(reg), val);
  9977. case '#':
  9978. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9979. default:
  9980. return pc_setglobalreg(sd, add_str(reg), val);
  9981. }
  9982. return false;
  9983. }
  9984. /**
  9985. * Get value of player variable
  9986. * @param sd Player
  9987. * @param reg Variable name
  9988. * @return Variable value or 0 if failed.
  9989. **/
  9990. int64 pc_readreg2(map_session_data *sd, const char *reg) {
  9991. char prefix = reg[0];
  9992. nullpo_ret(sd);
  9993. if (reg[strlen(reg)-1] == '$') {
  9994. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9995. return 0;
  9996. }
  9997. switch (prefix) {
  9998. case '.':
  9999. case '\'':
  10000. case '$':
  10001. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  10002. return 0;
  10003. case '@':
  10004. return pc_readreg(sd, add_str(reg));
  10005. case '#':
  10006. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  10007. default:
  10008. return pc_readglobalreg(sd, add_str(reg));
  10009. }
  10010. return 0;
  10011. }
  10012. /*==========================================
  10013. * Exec eventtimer for player sd (retrieved from map_session (id))
  10014. *------------------------------------------*/
  10015. static TIMER_FUNC(pc_eventtimer){
  10016. map_session_data *sd=map_id2sd(id);
  10017. char *p = (char *)data;
  10018. int32 i;
  10019. if(sd==nullptr)
  10020. return 0;
  10021. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  10022. if( i < MAX_EVENTTIMER )
  10023. {
  10024. sd->eventtimer[i] = INVALID_TIMER;
  10025. sd->eventcount--;
  10026. npc_event(sd,p,0);
  10027. }
  10028. else
  10029. ShowError("pc_eventtimer: no such event timer\n");
  10030. if (p) aFree(p);
  10031. return 0;
  10032. }
  10033. /*==========================================
  10034. * Add eventtimer for player sd ?
  10035. *------------------------------------------*/
  10036. bool pc_addeventtimer(map_session_data *sd,int32 tick,const char *name)
  10037. {
  10038. int32 i;
  10039. nullpo_retr(false,sd);
  10040. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  10041. if( i == MAX_EVENTTIMER )
  10042. return false;
  10043. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  10044. sd->eventcount++;
  10045. return true;
  10046. }
  10047. /*==========================================
  10048. * Del eventtimer for player sd ?
  10049. *------------------------------------------*/
  10050. bool pc_deleventtimer(map_session_data *sd,const char *name)
  10051. {
  10052. char* p = nullptr;
  10053. int32 i;
  10054. nullpo_retr(false,sd);
  10055. if (sd->eventcount == 0)
  10056. return false;
  10057. // find the named event timer
  10058. ARR_FIND( 0, MAX_EVENTTIMER, i,
  10059. sd->eventtimer[i] != INVALID_TIMER &&
  10060. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != nullptr &&
  10061. strcmp(p, name) == 0
  10062. );
  10063. if( i == MAX_EVENTTIMER )
  10064. return false; // not found
  10065. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10066. sd->eventtimer[i] = INVALID_TIMER;
  10067. if(sd->eventcount > 0)
  10068. sd->eventcount--;
  10069. aFree(p);
  10070. return true;
  10071. }
  10072. /*==========================================
  10073. * Update eventtimer count for player sd
  10074. *------------------------------------------*/
  10075. void pc_addeventtimercount(map_session_data *sd,const char *name,int32 tick)
  10076. {
  10077. int32 i;
  10078. nullpo_retv(sd);
  10079. for(i=0;i<MAX_EVENTTIMER;i++)
  10080. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  10081. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  10082. addtick_timer(sd->eventtimer[i],tick);
  10083. break;
  10084. }
  10085. }
  10086. /*==========================================
  10087. * Remove all eventtimer for player sd
  10088. *------------------------------------------*/
  10089. void pc_cleareventtimer(map_session_data *sd)
  10090. {
  10091. int32 i;
  10092. nullpo_retv(sd);
  10093. if (sd->eventcount == 0)
  10094. return;
  10095. for(i=0;i<MAX_EVENTTIMER;i++){
  10096. if( sd->eventtimer[i] != INVALID_TIMER ){
  10097. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  10098. delete_timer(sd->eventtimer[i],pc_eventtimer);
  10099. sd->eventtimer[i] = INVALID_TIMER;
  10100. if(sd->eventcount > 0) //avoid looping to max val
  10101. sd->eventcount--;
  10102. if (p) aFree(p);
  10103. }
  10104. }
  10105. }
  10106. /**
  10107. * Called when an item with combo is worn
  10108. * @param sd: Player data
  10109. * @param data: Item data
  10110. * @return Number of succeeded combo(s)
  10111. */
  10112. static int32 pc_checkcombo(map_session_data *sd, item_data *data) {
  10113. int32 success = 0;
  10114. for (const auto &item_combo : data->combos) {
  10115. bool do_continue = false;
  10116. // Ensure this isn't a duplicate combo
  10117. for (const auto player_combo : sd->combos) {
  10118. if (player_combo->id == item_combo->id) {
  10119. do_continue = true;
  10120. break;
  10121. }
  10122. }
  10123. // Combo already equipped
  10124. if (do_continue)
  10125. continue;
  10126. size_t nb_itemCombo = item_combo->nameid.size();
  10127. if (nb_itemCombo < 2) // A combo with less then 2 item?
  10128. continue;
  10129. struct s_itemchk {
  10130. int32 idx;
  10131. t_itemid nameid, card[MAX_SLOTS];
  10132. s_itemchk() : idx(0), nameid(0), card() {};
  10133. };
  10134. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  10135. size_t j;
  10136. uint32 pos = 0;
  10137. for (j = 0; j < nb_itemCombo; j++) {
  10138. t_itemid id = item_combo->nameid[j];
  10139. bool found = false;
  10140. for (int16 k = 0; k < EQI_MAX; k++) {
  10141. short index = sd->equip_index[k];
  10142. if (index < 0)
  10143. continue;
  10144. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  10145. continue;
  10146. if (!sd->inventory_data[index])
  10147. continue;
  10148. if (itemdb_type(id) != IT_CARD) {
  10149. if (sd->inventory_data[index]->nameid != id)
  10150. continue;
  10151. if (j > 0) { // Check if this item not already used
  10152. do_continue = false;
  10153. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  10154. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  10155. do_continue = true;
  10156. break;
  10157. }
  10158. }
  10159. if (do_continue)
  10160. continue;
  10161. }
  10162. combo_idx[j].nameid = id;
  10163. combo_idx[j].idx = index;
  10164. pos |= sd->inventory.u.items_inventory[index].equip;
  10165. found = true;
  10166. break;
  10167. } else { // Cards and enchants
  10168. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  10169. continue;
  10170. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  10171. do_continue = false;
  10172. if (sd->inventory.u.items_inventory[index].card[z] != id)
  10173. continue;
  10174. if (j > 0) {
  10175. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  10176. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  10177. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  10178. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  10179. do_continue = true;
  10180. break;
  10181. }
  10182. }
  10183. }
  10184. }
  10185. }
  10186. if (do_continue)
  10187. continue;
  10188. combo_idx[j].nameid = id;
  10189. combo_idx[j].idx = index;
  10190. combo_idx[j].card[z] = id;
  10191. pos |= sd->inventory.u.items_inventory[index].equip;
  10192. found = true;
  10193. break;
  10194. }
  10195. }
  10196. }
  10197. if (!found)
  10198. break; // Unable to found all the IDs for this combo, return
  10199. }
  10200. // Broke out of the count loop without finding all IDs, move to the next combo
  10201. if (j < nb_itemCombo)
  10202. continue;
  10203. // All items in the combo are matching
  10204. auto entry = std::make_shared<s_combos>();
  10205. entry->bonus = item_combo->script;
  10206. entry->id = item_combo->id;
  10207. entry->pos = pos;
  10208. sd->combos.push_back(entry);
  10209. combo_idx.clear();
  10210. success++;
  10211. }
  10212. return success;
  10213. }
  10214. /**
  10215. * Called when an item with combo is removed
  10216. * @param sd: Player data
  10217. * @param data: Item data
  10218. * @return Number of removed combo(s)
  10219. */
  10220. static int32 pc_removecombo(map_session_data *sd, item_data *data ) {
  10221. if (sd->combos.empty())
  10222. return 0; // Nothing to do here, player has no combos
  10223. int32 retval = 0;
  10224. for (const auto &item_combo : data->combos) {
  10225. std::shared_ptr<s_combos> del_combo = nullptr;
  10226. // Check if this combo exists on this player
  10227. for (const auto &player_combo : sd->combos) {
  10228. if (player_combo->id == item_combo->id) {
  10229. del_combo = player_combo;
  10230. break;
  10231. }
  10232. }
  10233. // No match, skip this combo
  10234. if (del_combo == nullptr)
  10235. continue;
  10236. util::vector_erase_if_exists(sd->combos, del_combo);
  10237. retval++;
  10238. // Check if combo requirements still fit
  10239. if (pc_checkcombo(sd, data))
  10240. continue;
  10241. // It's empty, clear all the memory
  10242. if (sd->combos.empty()) {
  10243. sd->combos.clear();
  10244. return retval; // Return at this point as there are no more combos to check
  10245. }
  10246. }
  10247. return retval;
  10248. }
  10249. /**
  10250. * Load combo data(s) of player
  10251. * @param sd: Player data
  10252. * @return ret numbers of succeed combo
  10253. */
  10254. int32 pc_load_combo(map_session_data *sd) {
  10255. int32 ret = 0;
  10256. for (int16 i = 0; i < EQI_MAX; i++) {
  10257. item_data *id;
  10258. short idx = sd->equip_index[i];
  10259. if (idx < 0 || !(id = sd->inventory_data[idx]))
  10260. continue;
  10261. if (!id->combos.empty())
  10262. ret += pc_checkcombo(sd, id);
  10263. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  10264. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  10265. if (!sd->inventory.u.items_inventory[idx].card[j])
  10266. continue;
  10267. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[idx].card[j]);
  10268. if (data != nullptr) {
  10269. if (!data->combos.empty())
  10270. ret += pc_checkcombo(sd, data.get());
  10271. }
  10272. }
  10273. }
  10274. }
  10275. return ret;
  10276. }
  10277. /*==========================================
  10278. * Equip item on player sd at req_pos from inventory index n
  10279. * return: false - fail; true - success
  10280. *------------------------------------------*/
  10281. bool pc_equipitem(map_session_data *sd,short n,int32 req_pos,bool equipswitch)
  10282. {
  10283. int32 i, pos, flag = 0, iflag;
  10284. struct item_data *id;
  10285. short* equip_index;
  10286. nullpo_retr(false,sd);
  10287. if( n < 0 || n >= MAX_INVENTORY ) {
  10288. if( equipswitch ){
  10289. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10290. }else{
  10291. // Does not deserve an answer... [Lemongrass]
  10292. //clif_equipitemack( sd, ITEM_EQUIP_ACK_FAIL, n );
  10293. }
  10294. return false;
  10295. }
  10296. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  10297. if( equipswitch ){
  10298. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10299. }else{
  10300. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n );
  10301. }
  10302. return false;
  10303. }
  10304. if (!(id = sd->inventory_data[n]))
  10305. return false;
  10306. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  10307. if(battle_config.battle_log && !equipswitch)
  10308. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  10309. uint8 res = pc_isequip( sd, n );
  10310. if( res != ITEM_EQUIP_ACK_OK ){
  10311. if( equipswitch ){
  10312. clif_equipswitch_add( sd, n, req_pos, res );
  10313. }else{
  10314. clif_equipitemack( *sd, res, n ); // fail
  10315. }
  10316. return false;
  10317. }
  10318. if( equipswitch && id->type == IT_AMMO ){
  10319. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10320. return false;
  10321. }
  10322. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0) {
  10323. if( equipswitch ){
  10324. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10325. }else{
  10326. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); // fail
  10327. }
  10328. return false;
  10329. }
  10330. if( !sd->sc.empty() && (sd->sc.cant.equip || (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) {
  10331. if( equipswitch ){
  10332. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10333. }else{
  10334. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); //Fail
  10335. }
  10336. return false;
  10337. }
  10338. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  10339. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  10340. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  10341. clif_notify_bindOnEquip( *sd, n );
  10342. }
  10343. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  10344. pos = req_pos&EQP_ACC;
  10345. if (pos == EQP_ACC) //User specified both slots.
  10346. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  10347. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  10348. if (!sd->inventory.u.items_inventory[n].card[i])
  10349. continue;
  10350. std::shared_ptr<item_data> card_data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10351. if (card_data) {
  10352. int32 card_pos = card_data->equip;
  10353. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  10354. pos = card_pos; // Use the card's equip position
  10355. break;
  10356. }
  10357. }
  10358. }
  10359. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  10360. pos = (req_pos&EQP_ARMS);
  10361. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  10362. #ifdef RENEWAL
  10363. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  10364. #else
  10365. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  10366. #endif
  10367. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  10368. pos = req_pos&EQP_SHADOW_ACC;
  10369. if (pos == EQP_SHADOW_ACC)
  10370. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  10371. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  10372. pos = (req_pos&EQP_SHADOW_ARMS);
  10373. if( pos == EQP_SHADOW_ARMS )
  10374. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  10375. }
  10376. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  10377. //Update skill-block range database when weapon range changes. [Skotlex]
  10378. i = equip_index[EQI_HAND_R];
  10379. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  10380. flag = 1;
  10381. else
  10382. flag = id->range != sd->inventory_data[i]->range;
  10383. }
  10384. if( equipswitch ){
  10385. for( i = 0; i < EQI_MAX; i++ ){
  10386. if( pos&equip_bitmask[i] ){
  10387. // If there was already an item assigned to this slot
  10388. if( sd->equip_switch_index[i] >= 0 ){
  10389. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  10390. }
  10391. // Assign the new index to it
  10392. sd->equip_switch_index[i] = n;
  10393. }
  10394. }
  10395. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  10396. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  10397. return true;
  10398. }else{
  10399. for(i=0;i<EQI_MAX;i++) {
  10400. if(pos & equip_bitmask[i]) {
  10401. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  10402. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  10403. sd->equip_index[i] = n;
  10404. }
  10405. }
  10406. pc_equipswitch_remove(sd, n);
  10407. if(pos==EQP_AMMO) {
  10408. clif_arrowequip( *sd );
  10409. clif_arrow_fail( *sd, ARROWFAIL_SUCCESS );
  10410. }
  10411. else
  10412. clif_equipitemack( *sd, ITEM_EQUIP_ACK_OK, n, pos );
  10413. sd->inventory.u.items_inventory[n].equip = pos;
  10414. }
  10415. if(pos & EQP_HAND_R) {
  10416. if(id)
  10417. sd->weapontype1 = id->subtype;
  10418. else
  10419. sd->weapontype1 = W_FIST;
  10420. pc_calcweapontype(sd);
  10421. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10422. }
  10423. if(pos & EQP_HAND_L) {
  10424. if(id) {
  10425. if(id->type == IT_WEAPON) {
  10426. sd->status.shield = W_FIST;
  10427. sd->weapontype2 = id->subtype;
  10428. }
  10429. else
  10430. if(id->type == IT_ARMOR) {
  10431. sd->status.shield = id->look;
  10432. sd->weapontype2 = W_FIST;
  10433. }
  10434. }
  10435. else
  10436. sd->status.shield = sd->weapontype2 = W_FIST;
  10437. pc_calcweapontype(sd);
  10438. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10439. }
  10440. if(pos & EQP_SHOES)
  10441. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10442. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  10443. short idx = sd->equip_index[EQI_AMMO];
  10444. if (idx >= 0) {
  10445. switch (sd->inventory_data[idx]->subtype) {
  10446. case AMMO_ARROW:
  10447. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  10448. pc_unequipitem(sd, idx, 2 | 4);
  10449. break;
  10450. case AMMO_BULLET:
  10451. case AMMO_SHELL:
  10452. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10453. #ifdef RENEWAL
  10454. && id->subtype != W_GRENADE
  10455. #endif
  10456. )
  10457. pc_unequipitem(sd, idx, 2 | 4);
  10458. break;
  10459. #ifndef RENEWAL
  10460. case AMMO_GRENADE:
  10461. if (id->subtype != W_GRENADE)
  10462. pc_unequipitem(sd, idx, 2 | 4);
  10463. break;
  10464. #endif
  10465. }
  10466. }
  10467. }
  10468. pc_set_costume_view(sd);
  10469. pc_checkallowskill(sd); //Check if status changes should be halted.
  10470. iflag = sd->npc_item_flag;
  10471. // Check for combos (MUST be before status_calc_pc)
  10472. if (!id->combos.empty())
  10473. pc_checkcombo(sd, id);
  10474. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10475. ; // No cards
  10476. else {
  10477. for (i = 0; i < MAX_SLOTS; i++) {
  10478. if (!sd->inventory.u.items_inventory[n].card[i])
  10479. continue;
  10480. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10481. if (data != nullptr) {
  10482. if (!data->combos.empty())
  10483. pc_checkcombo(sd, data.get());
  10484. }
  10485. }
  10486. }
  10487. status_calc_pc(sd,SCO_NONE);
  10488. if (flag) //Update skill data
  10489. clif_skillinfoblock(sd);
  10490. //OnEquip script [Skotlex]
  10491. if (id) {
  10492. current_equip_item_index = n;
  10493. current_equip_card_id = 0;
  10494. //only run the script if item isn't restricted
  10495. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  10496. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10497. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10498. ; //No cards
  10499. else {
  10500. for( i = 0; i < MAX_SLOTS; i++ ) {
  10501. if (!sd->inventory.u.items_inventory[n].card[i])
  10502. continue;
  10503. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10504. if ( data != nullptr ) {
  10505. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data.get(), sd->bl.m))) {
  10506. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10507. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10508. }
  10509. }
  10510. current_equip_card_id = 0;
  10511. }
  10512. }
  10513. current_equip_item_index = -1;
  10514. }
  10515. sd->npc_item_flag = iflag;
  10516. return true;
  10517. }
  10518. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int32 position ){
  10519. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10520. while( it != bonus.end() ){
  10521. std::shared_ptr<s_autobonus> b = *it;
  10522. if( ( b->pos & position ) != b->pos ){
  10523. it++;
  10524. continue;
  10525. }
  10526. it = bonus.erase( it );
  10527. }
  10528. }
  10529. /**
  10530. * Recalculate player status on unequip
  10531. * @param sd: Player data
  10532. * @param n: Item inventory index
  10533. * @param flag: Whether to recalculate a player's status or not
  10534. * @return True on success or false on failure
  10535. */
  10536. static void pc_unequipitem_sub(map_session_data *sd, int32 n, int32 flag) {
  10537. int32 i, iflag;
  10538. bool status_calc = false;
  10539. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10540. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10541. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10542. sd->inventory.u.items_inventory[n].equip = 0;
  10543. if (!(flag & 4))
  10544. pc_checkallowskill(sd);
  10545. iflag = sd->npc_item_flag;
  10546. // Check for combos (MUST be before status_calc_pc)
  10547. if (sd->inventory_data[n]) {
  10548. if (!sd->inventory_data[n]->combos.empty()) {
  10549. if (pc_removecombo(sd, sd->inventory_data[n]))
  10550. status_calc = true;
  10551. }
  10552. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10553. ; // No cards
  10554. else {
  10555. for (i = 0; i < MAX_SLOTS; i++) {
  10556. if (!sd->inventory.u.items_inventory[n].card[i])
  10557. continue;
  10558. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10559. if (data != nullptr) {
  10560. if (!data->combos.empty()) {
  10561. if (pc_removecombo(sd, data.get()))
  10562. status_calc = true;
  10563. }
  10564. }
  10565. }
  10566. }
  10567. }
  10568. if (flag & 1 || status_calc) {
  10569. pc_checkallowskill(sd);
  10570. status_calc_pc(sd, SCO_FORCE);
  10571. }
  10572. if (sd->sc.getSCE(SC_SIGNUMCRUCIS) && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10573. status_change_end(&sd->bl, SC_SIGNUMCRUCIS);
  10574. //OnUnEquip script [Skotlex]
  10575. if (sd->inventory_data[n]) {
  10576. current_equip_item_index = n;
  10577. current_equip_card_id = 0;
  10578. if (sd->inventory_data[n]->unequip_script)
  10579. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10580. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10581. ; //No cards
  10582. else {
  10583. for (i = 0; i < MAX_SLOTS; i++) {
  10584. if (!sd->inventory.u.items_inventory[n].card[i])
  10585. continue;
  10586. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10587. if (data != nullptr) {
  10588. if (data->unequip_script) {
  10589. current_equip_card_id = sd->inventory.u.items_inventory[n].card[i];
  10590. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10591. }
  10592. }
  10593. current_equip_card_id = 0;
  10594. }
  10595. }
  10596. current_equip_item_index = -1;
  10597. }
  10598. sd->npc_item_flag = iflag;
  10599. }
  10600. /**
  10601. * Called when attempting to unequip an item from a player
  10602. * @param sd: Player data
  10603. * @param n: Item inventory index
  10604. * @param flag: Type of unequip
  10605. * 0 - only unequip
  10606. * 1 - calculate status after unequipping
  10607. * 2 - force unequip
  10608. * 4 - unequip by switching equipment
  10609. * @return True on success or false on failure
  10610. */
  10611. bool pc_unequipitem(map_session_data *sd, int32 n, int32 flag) {
  10612. int32 i, pos;
  10613. nullpo_retr(false,sd);
  10614. if (n < 0 || n >= MAX_INVENTORY) {
  10615. clif_unequipitemack(*sd,0,0,false);
  10616. return false;
  10617. }
  10618. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10619. clif_unequipitemack(*sd,n,0,false);
  10620. return false; //Nothing to unequip
  10621. }
  10622. // status change that makes player cannot unequip equipment
  10623. if (!(flag&2) && !sd->sc.empty() &&( sd->sc.cant.unequip ||
  10624. (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10625. {
  10626. clif_unequipitemack(*sd,n,0,false);
  10627. return false;
  10628. }
  10629. if (battle_config.battle_log)
  10630. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10631. for(i = 0; i < EQI_MAX; i++) {
  10632. if (pos & equip_bitmask[i])
  10633. sd->equip_index[i] = -1;
  10634. }
  10635. if(pos & EQP_HAND_R) {
  10636. sd->weapontype1 = W_FIST;
  10637. sd->status.weapon = sd->weapontype2;
  10638. pc_calcweapontype(sd);
  10639. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10640. if( !battle_config.dancing_weaponswitch_fix )
  10641. status_change_end(&sd->bl, SC_DANCING); // Unequipping => stop dancing.
  10642. #ifdef RENEWAL
  10643. if (battle_config.switch_remove_edp&2) {
  10644. #else
  10645. if (battle_config.switch_remove_edp&1) {
  10646. #endif
  10647. status_change_end(&sd->bl, SC_EDP);
  10648. }
  10649. }
  10650. if(pos & EQP_HAND_L) {
  10651. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10652. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10653. sd->status.shield = sd->weapontype2 = W_FIST;
  10654. pc_calcweapontype(sd);
  10655. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10656. }
  10657. if(pos & EQP_SHOES)
  10658. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10659. clif_unequipitemack(*sd,n,pos,true);
  10660. pc_set_costume_view(sd);
  10661. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10662. // On weapon change (right and left hand)
  10663. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10664. if (battle_config.ammo_unequip && !(flag & 4)) {
  10665. switch (sd->inventory_data[n]->subtype) {
  10666. case W_BOW:
  10667. case W_MUSICAL:
  10668. case W_WHIP:
  10669. case W_REVOLVER:
  10670. case W_RIFLE:
  10671. case W_GATLING:
  10672. case W_SHOTGUN:
  10673. case W_GRENADE: {
  10674. short idx = sd->equip_index[EQI_AMMO];
  10675. if (idx >= 0) {
  10676. sd->equip_index[EQI_AMMO] = -1;
  10677. clif_unequipitemack(*sd, idx, sd->inventory.u.items_inventory[idx].equip, true);
  10678. pc_unequipitem_sub(sd, idx, 0);
  10679. }
  10680. }
  10681. break;
  10682. }
  10683. }
  10684. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10685. }
  10686. // On armor change
  10687. if (pos & EQP_ARMOR) {
  10688. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10689. }
  10690. // On equipment change
  10691. #ifndef RENEWAL
  10692. if (!(flag&4))
  10693. status_change_end(&sd->bl, SC_CONCENTRATION);
  10694. #endif
  10695. // On ammo change
  10696. 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_))
  10697. status_change_end(&sd->bl, SC_P_ALTER);
  10698. pc_unequipitem_sub(sd, n, flag);
  10699. return true;
  10700. }
  10701. int32 pc_equipswitch( map_session_data* sd, int32 index ){
  10702. // Get the target equip mask
  10703. int32 position = sd->inventory.u.items_inventory[index].equipSwitch;
  10704. // Get the currently equipped item
  10705. short equippedItem = pc_checkequip( sd, position, true );
  10706. // No item equipped at the target
  10707. if( equippedItem == -1 ){
  10708. // Remove it from the equip switch
  10709. pc_equipswitch_remove( sd, index );
  10710. pc_equipitem( sd, index, position );
  10711. return position;
  10712. }else{
  10713. std::map<int, int> unequipped;
  10714. int32 unequipped_position = 0;
  10715. // Unequip all items that interfere
  10716. for( int32 i = 0; i < EQI_MAX; i++ ){
  10717. int32 unequip_index = sd->equip_index[i];
  10718. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10719. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10720. // Store the unequipped index and position mask for later
  10721. unequipped[unequip_index] = unequip_item->equip;
  10722. // Keep the position for later
  10723. unequipped_position |= unequip_item->equip;
  10724. // Unequip the item
  10725. pc_unequipitem( sd, unequip_index, 0 );
  10726. }
  10727. }
  10728. int32 all_position = position | unequipped_position;
  10729. // Equip everything that is hit by the mask
  10730. for( int32 i = 0; i < EQI_MAX; i++ ){
  10731. int32 exchange_index = sd->equip_switch_index[i];
  10732. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10733. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10734. // Store the target position
  10735. int32 exchange_position = exchange_item->equipSwitch;
  10736. // Remove the item from equip switch
  10737. pc_equipswitch_remove( sd, exchange_index );
  10738. // Equip the item at the destinated position
  10739. pc_equipitem( sd, exchange_index, exchange_position );
  10740. }
  10741. }
  10742. // Place all unequipped items into the equip switch window
  10743. for( std::pair<int, int> pair : unequipped ){
  10744. int32 unequipped_index = pair.first;
  10745. int32 unequipped_position = pair.second;
  10746. // Rebuild the index cache
  10747. for( int32 i = 0; i < EQI_MAX; i++ ){
  10748. if( unequipped_position & equip_bitmask[i] ){
  10749. sd->equip_switch_index[i] = unequipped_index;
  10750. }
  10751. }
  10752. // Set the correct position mask
  10753. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10754. // Notify the client
  10755. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10756. }
  10757. return all_position;
  10758. }
  10759. }
  10760. void pc_equipswitch_remove( map_session_data* sd, int32 index ){
  10761. struct item* item = &sd->inventory.u.items_inventory[index];
  10762. if( !item->equipSwitch ){
  10763. return;
  10764. }
  10765. for( int32 i = 0; i < EQI_MAX; i++ ){
  10766. // If a match is found
  10767. if( sd->equip_switch_index[i] == index ){
  10768. // Remove it from the slot
  10769. sd->equip_switch_index[i] = -1;
  10770. }
  10771. }
  10772. // Send out one packet for all slots using the current item's mask
  10773. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10774. item->equipSwitch = 0;
  10775. }
  10776. /*==========================================
  10777. * Checking if player (sd) has an invalid item
  10778. * and is unequiped on map load (item_noequip)
  10779. *------------------------------------------*/
  10780. void pc_checkitem(map_session_data *sd) {
  10781. int32 i, calc_flag = 0;
  10782. struct item* it;
  10783. nullpo_retv(sd);
  10784. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10785. return;
  10786. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10787. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10788. it = &sd->inventory.u.items_inventory[i];
  10789. if( it->nameid == 0 )
  10790. continue;
  10791. if( !it->equip )
  10792. continue;
  10793. if( it->equip&~pc_equippoint(sd,i) ) {
  10794. pc_unequipitem(sd, i, 2);
  10795. calc_flag = 1;
  10796. continue;
  10797. }
  10798. 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) ) {
  10799. pc_unequipitem(sd, i, 2);
  10800. calc_flag = 1;
  10801. continue;
  10802. }
  10803. }
  10804. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10805. it = &sd->inventory.u.items_inventory[i];
  10806. if( it->nameid == 0 )
  10807. continue;
  10808. if( !it->equipSwitch )
  10809. continue;
  10810. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10811. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10812. for( int32 j = 0; j < EQI_MAX; j++ ){
  10813. if( sd->equip_switch_index[j] == i ){
  10814. sd->equip_switch_index[j] = -1;
  10815. }
  10816. }
  10817. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10818. continue;
  10819. }
  10820. }
  10821. if( calc_flag && sd->state.active ) {
  10822. pc_checkallowskill(sd);
  10823. status_calc_pc(sd,SCO_NONE);
  10824. }
  10825. }
  10826. /*==========================================
  10827. * Checks for unavailable items and removes them.
  10828. * @param sd: Player data
  10829. * @param type Forced check:
  10830. * 1 - Inventory
  10831. * 2 - Cart
  10832. * 4 - Storage
  10833. *------------------------------------------*/
  10834. void pc_check_available_item(map_session_data *sd, uint8 type)
  10835. {
  10836. int32 i;
  10837. t_itemid nameid;
  10838. char output[256];
  10839. nullpo_retv(sd);
  10840. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10841. for(i = 0; i < MAX_INVENTORY; i++) {
  10842. nameid = sd->inventory.u.items_inventory[i].nameid;
  10843. if (!nameid)
  10844. continue;
  10845. if (!itemdb_available(nameid)) {
  10846. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10847. clif_displaymessage(sd->fd, output);
  10848. 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);
  10849. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10850. continue;
  10851. }
  10852. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10853. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10854. }
  10855. }
  10856. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10857. for(i = 0; i < MAX_CART; i++) {
  10858. nameid = sd->cart.u.items_cart[i].nameid;
  10859. if (!nameid)
  10860. continue;
  10861. if (!itemdb_available(nameid)) {
  10862. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10863. clif_displaymessage(sd->fd, output);
  10864. 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);
  10865. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10866. continue;
  10867. }
  10868. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10869. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10870. }
  10871. }
  10872. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10873. for(i = 0; i < sd->storage.max_amount; i++) {
  10874. nameid = sd->storage.u.items_storage[i].nameid;
  10875. if (!nameid)
  10876. continue;
  10877. if (!itemdb_available(nameid)) {
  10878. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10879. clif_displaymessage(sd->fd, output);
  10880. 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);
  10881. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10882. continue;
  10883. }
  10884. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10885. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10886. }
  10887. }
  10888. }
  10889. /*==========================================
  10890. * Update PVP rank for sd1 in cmp to sd2
  10891. *------------------------------------------*/
  10892. static int32 pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10893. {
  10894. map_session_data *sd1,*sd2;
  10895. sd1=(map_session_data *)bl;
  10896. sd2=va_arg(ap,map_session_data *);
  10897. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10898. {// cannot register pvp rank for hidden GMs
  10899. return 0;
  10900. }
  10901. if( sd1->pvp_point > sd2->pvp_point )
  10902. sd2->pvp_rank++;
  10903. return 0;
  10904. }
  10905. /*==========================================
  10906. * Calculate new rank beetween all present players (map_foreachinallarea)
  10907. * and display result
  10908. *------------------------------------------*/
  10909. int32 pc_calc_pvprank(map_session_data *sd)
  10910. {
  10911. int32 old = sd->pvp_rank;
  10912. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10913. sd->pvp_rank=1;
  10914. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10915. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10916. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10917. return sd->pvp_rank;
  10918. }
  10919. /*==========================================
  10920. * Calculate next sd ranking calculation from config
  10921. *------------------------------------------*/
  10922. TIMER_FUNC(pc_calc_pvprank_timer){
  10923. map_session_data *sd;
  10924. sd=map_id2sd(id);
  10925. if(sd==nullptr)
  10926. return 0;
  10927. sd->pvp_timer = INVALID_TIMER;
  10928. if( pc_isinvisible(sd) )
  10929. {// do not calculate the pvp rank for a hidden GM
  10930. return 0;
  10931. }
  10932. if( pc_calc_pvprank(sd) > 0 )
  10933. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10934. return 0;
  10935. }
  10936. /*==========================================
  10937. * Checking if sd is married
  10938. * Return:
  10939. * partner_id = yes
  10940. * 0 = no
  10941. *------------------------------------------*/
  10942. int32 pc_ismarried(map_session_data *sd)
  10943. {
  10944. if(sd == nullptr)
  10945. return -1;
  10946. if(sd->status.partner_id > 0)
  10947. return sd->status.partner_id;
  10948. else
  10949. return 0;
  10950. }
  10951. /*==========================================
  10952. * Marry player sd to player dstsd
  10953. * Return:
  10954. * false = fail
  10955. * true = success
  10956. *------------------------------------------*/
  10957. bool pc_marriage(map_session_data *sd,map_session_data *dstsd)
  10958. {
  10959. if(sd == nullptr || dstsd == nullptr ||
  10960. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10961. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10962. return false;
  10963. sd->status.partner_id = dstsd->status.char_id;
  10964. dstsd->status.partner_id = sd->status.char_id;
  10965. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10966. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10967. return true;
  10968. }
  10969. /*==========================================
  10970. * Divorce sd from its partner
  10971. * Return:
  10972. * false = fail
  10973. * true = success
  10974. *------------------------------------------*/
  10975. bool pc_divorce(map_session_data *sd)
  10976. {
  10977. map_session_data *p_sd;
  10978. int32 i;
  10979. if( sd == nullptr || !pc_ismarried(sd) )
  10980. return false;
  10981. if( !sd->status.partner_id )
  10982. return false; // Char is not married
  10983. if( (p_sd = map_charid2sd(sd->status.partner_id)) == nullptr )
  10984. { // Lets char server do the divorce
  10985. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10986. return false; // No char server connected
  10987. return true;
  10988. }
  10989. // Both players online, lets do the divorce manually
  10990. sd->status.partner_id = 0;
  10991. p_sd->status.partner_id = 0;
  10992. for( i = 0; i < MAX_INVENTORY; i++ )
  10993. {
  10994. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10995. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10996. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10997. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10998. }
  10999. clif_divorced(sd, p_sd->status.name);
  11000. clif_divorced(p_sd, sd->status.name);
  11001. return true;
  11002. }
  11003. /**
  11004. * Get the partner map_session_data of a player
  11005. * @param sd : the husband|wife session
  11006. * @return partner session or nullptr
  11007. */
  11008. map_session_data *pc_get_partner(map_session_data *sd){
  11009. if (!sd || !pc_ismarried(sd))
  11010. return nullptr;
  11011. return map_charid2sd(sd->status.partner_id);
  11012. }
  11013. /**
  11014. * Get the father map_session_data of a player
  11015. * @param sd : the baby session
  11016. * @return father session or nullptr
  11017. */
  11018. map_session_data *pc_get_father (map_session_data *sd){
  11019. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  11020. return nullptr;
  11021. return map_charid2sd(sd->status.father);
  11022. }
  11023. /**
  11024. * Get the mother map_session_data of a player
  11025. * @param sd : the baby session
  11026. * @return mother session or nullptr
  11027. */
  11028. map_session_data *pc_get_mother (map_session_data *sd){
  11029. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  11030. return nullptr;
  11031. return map_charid2sd(sd->status.mother);
  11032. }
  11033. /*==========================================
  11034. * Get sd children charid. (Need to be married)
  11035. *------------------------------------------*/
  11036. map_session_data *pc_get_child (map_session_data *sd)
  11037. {
  11038. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  11039. // charid2sd returns nullptr if not found
  11040. return nullptr;
  11041. return map_charid2sd(sd->status.child);
  11042. }
  11043. /*==========================================
  11044. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  11045. *------------------------------------------*/
  11046. void pc_bleeding (map_session_data *sd, t_tick diff_tick)
  11047. {
  11048. int32 hp = 0, sp = 0;
  11049. if( pc_isdead(sd) )
  11050. return;
  11051. if (sd->hp_loss.value) {
  11052. sd->hp_loss.tick += diff_tick;
  11053. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  11054. hp += sd->hp_loss.value;
  11055. sd->hp_loss.tick -= sd->hp_loss.rate;
  11056. }
  11057. if(hp >= sd->battle_status.hp)
  11058. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  11059. }
  11060. if (sd->sp_loss.value) {
  11061. sd->sp_loss.tick += diff_tick;
  11062. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  11063. sp += sd->sp_loss.value;
  11064. sd->sp_loss.tick -= sd->sp_loss.rate;
  11065. }
  11066. }
  11067. if (hp > 0 || sp > 0)
  11068. status_zap(&sd->bl, hp, sp);
  11069. }
  11070. //Character regen. Flag is used to know which types of regen can take place.
  11071. //&1: HP regen
  11072. //&2: SP regen
  11073. void pc_regen (map_session_data *sd, t_tick diff_tick)
  11074. {
  11075. int32 hp = 0, sp = 0;
  11076. if (sd->hp_regen.value) {
  11077. sd->hp_regen.tick += diff_tick;
  11078. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  11079. hp += sd->hp_regen.value;
  11080. sd->hp_regen.tick -= sd->hp_regen.rate;
  11081. }
  11082. }
  11083. if (sd->sp_regen.value) {
  11084. sd->sp_regen.tick += diff_tick;
  11085. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  11086. sp += sd->sp_regen.value;
  11087. sd->sp_regen.tick -= sd->sp_regen.rate;
  11088. }
  11089. }
  11090. if (sd->percent_hp_regen.value) {
  11091. sd->percent_hp_regen.tick += diff_tick;
  11092. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  11093. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  11094. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  11095. }
  11096. }
  11097. if (sd->percent_sp_regen.value) {
  11098. sd->percent_sp_regen.tick += diff_tick;
  11099. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  11100. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  11101. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  11102. }
  11103. }
  11104. if (hp > 0 || sp > 0)
  11105. status_heal(&sd->bl, hp, sp, 0);
  11106. }
  11107. /*==========================================
  11108. * Memo player sd savepoint. (map,x,y)
  11109. *------------------------------------------*/
  11110. void pc_setsavepoint(map_session_data *sd, short mapindex,int32 x,int32 y)
  11111. {
  11112. nullpo_retv(sd);
  11113. safestrncpy( sd->status.save_point.map, mapindex_id2name( mapindex ), sizeof( sd->status.save_point.map ) );
  11114. sd->status.save_point.x = x;
  11115. sd->status.save_point.y = y;
  11116. }
  11117. /*==========================================
  11118. * Save 1 player data at autosave interval
  11119. *------------------------------------------*/
  11120. static TIMER_FUNC(pc_autosave){
  11121. int32 interval;
  11122. struct s_mapiterator* iter;
  11123. map_session_data* sd;
  11124. static int32 last_save_id = 0, save_flag = 0;
  11125. if(save_flag == 2) //Someone was saved on last call, normal cycle
  11126. save_flag = 0;
  11127. else
  11128. save_flag = 1; //Noone was saved, so save first found char.
  11129. iter = mapit_getallusers();
  11130. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  11131. {
  11132. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  11133. continue;
  11134. if(sd->bl.id == last_save_id && save_flag != 1) {
  11135. save_flag = 1;
  11136. continue;
  11137. }
  11138. if(save_flag != 1) //Not our turn to save yet.
  11139. continue;
  11140. //Save char.
  11141. last_save_id = sd->bl.id;
  11142. save_flag = 2;
  11143. if (pc_isvip(sd)) // Check if we're still VIP
  11144. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  11145. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  11146. break;
  11147. }
  11148. mapit_free(iter);
  11149. interval = autosave_interval/(map_usercount()+1);
  11150. if(interval < minsave_interval)
  11151. interval = minsave_interval;
  11152. add_timer(gettick()+interval,pc_autosave,0,0);
  11153. return 0;
  11154. }
  11155. static int32 pc_daynight_timer_sub(map_session_data *sd,va_list ap)
  11156. {
  11157. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  11158. { //Night/day state does not match.
  11159. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  11160. sd->state.night = night_flag;
  11161. return 1;
  11162. }
  11163. return 0;
  11164. }
  11165. /*================================================
  11166. * timer to do the day [Yor]
  11167. * data: 0 = called by timer, 1 = gmcommand/script
  11168. *------------------------------------------------*/
  11169. TIMER_FUNC(map_day_timer){
  11170. char tmp_soutput[1024];
  11171. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  11172. return 0;
  11173. if (!night_flag)
  11174. return 0; //Already day.
  11175. night_flag = 0; // 0=day, 1=night [Yor]
  11176. map_foreachpc(pc_daynight_timer_sub);
  11177. strcpy(tmp_soutput, (data == 0) ? msg_txt(nullptr,502) : msg_txt(nullptr,60)); // The day has arrived!
  11178. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11179. return 0;
  11180. }
  11181. /*================================================
  11182. * timer to do the night [Yor]
  11183. * data: 0 = called by timer, 1 = gmcommand/script
  11184. *------------------------------------------------*/
  11185. TIMER_FUNC(map_night_timer){
  11186. char tmp_soutput[1024];
  11187. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  11188. return 0;
  11189. if (night_flag)
  11190. return 0; //Already nigth.
  11191. night_flag = 1; // 0=day, 1=night [Yor]
  11192. map_foreachpc(pc_daynight_timer_sub);
  11193. strcpy(tmp_soutput, (data == 0) ? msg_txt(nullptr,503) : msg_txt(nullptr,59)); // The night has fallen...
  11194. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11195. return 0;
  11196. }
  11197. /**
  11198. * Attempt to stand up a player
  11199. * @param sd
  11200. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  11201. * @return True if success, Fals if failed
  11202. */
  11203. bool pc_setstand(map_session_data *sd, bool force){
  11204. nullpo_ret(sd);
  11205. // Cannot stand yet
  11206. // TODO: Move to SCS_NOSTAND [Cydh]
  11207. if (!force && (sd->sc.getSCE(SC_SITDOWN_FORCE) || sd->sc.getSCE(SC_BANANA_BOMB_SITDOWN)))
  11208. return false;
  11209. status_change_end(&sd->bl, SC_TENSIONRELAX);
  11210. clif_status_load(&sd->bl,EFST_SIT,0);
  11211. clif_standing(sd->bl); //Inform area PC is standing
  11212. //Reset sitting tick.
  11213. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  11214. if( pc_isdead( sd ) ){
  11215. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11216. clif_party_dead( *sd );
  11217. }else{
  11218. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11219. }
  11220. return true;
  11221. }
  11222. /**
  11223. * Calculate Overheat value
  11224. * @param sd: Player data
  11225. * @param heat: Amount of Heat to adjust
  11226. **/
  11227. void pc_overheat(map_session_data &sd, int16 heat) {
  11228. status_change_entry *sce = sd.sc.getSCE(SC_OVERHEAT_LIMITPOINT);
  11229. if (sce) {
  11230. sce->val1 += heat;
  11231. sce->val1 = cap_value(sce->val1, 0, 1000);
  11232. if (heat < 0 && sce->val1 == 0) { // Cooling device used.
  11233. status_change_end(&sd.bl, SC_OVERHEAT_LIMITPOINT);
  11234. status_change_end(&sd.bl, SC_OVERHEAT);
  11235. }
  11236. } else if (heat > 0)
  11237. sc_start(&sd.bl, &sd.bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  11238. }
  11239. /**
  11240. * Check if player is autolooting given itemID.
  11241. */
  11242. bool pc_isautolooting(map_session_data *sd, t_itemid nameid)
  11243. {
  11244. uint8 i = 0;
  11245. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  11246. return true;
  11247. if (!sd->state.autolooting)
  11248. return false;
  11249. if (sd->state.autolooting)
  11250. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  11251. return (i != AUTOLOOTITEM_SIZE);
  11252. }
  11253. /**
  11254. * Checks if player can use @/#command
  11255. * @param sd Player map session data
  11256. * @param command Command name without @/# and params
  11257. * @param type is it atcommand or charcommand
  11258. */
  11259. bool pc_can_use_command( map_session_data *sd, const char *command, AtCommandType type ){
  11260. return sd->group->can_use_command( command, type );
  11261. }
  11262. bool pc_has_permission( map_session_data* sd, e_pc_permission permission ){
  11263. return sd->permissions.test( permission );
  11264. }
  11265. /**
  11266. * Checks if commands used by a player should be logged
  11267. * according to their group setting.
  11268. * @param sd Player map session data
  11269. */
  11270. bool pc_should_log_commands( map_session_data *sd ){
  11271. return sd->group->log_commands;
  11272. }
  11273. /**
  11274. * Spirit Charm expiration timer.
  11275. * @see TimerFunc
  11276. */
  11277. static TIMER_FUNC(pc_spiritcharm_timer){
  11278. map_session_data *sd;
  11279. int32 i;
  11280. if ((sd = (map_session_data *)map_id2sd(id)) == nullptr || sd->bl.type != BL_PC)
  11281. return 1;
  11282. if (sd->spiritcharm <= 0) {
  11283. 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);
  11284. sd->spiritcharm = 0;
  11285. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11286. return 0;
  11287. }
  11288. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  11289. if (i == sd->spiritcharm) {
  11290. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  11291. return 0;
  11292. }
  11293. sd->spiritcharm--;
  11294. if (i != sd->spiritcharm)
  11295. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  11296. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11297. if (sd->spiritcharm <= 0)
  11298. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11299. clif_spiritcharm( *sd );
  11300. return 0;
  11301. }
  11302. /**
  11303. * Adds a spirit charm.
  11304. * @param sd: Target character
  11305. * @param interval: Duration
  11306. * @param max: Maximum amount of charms to add
  11307. * @param type: Charm type (@see spirit_charm_types)
  11308. */
  11309. void pc_addspiritcharm(map_session_data *sd, int32 interval, int32 max, int32 type)
  11310. {
  11311. int32 tid, i;
  11312. nullpo_retv(sd);
  11313. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  11314. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  11315. if (max > MAX_SPIRITCHARM)
  11316. max = MAX_SPIRITCHARM;
  11317. if (sd->spiritcharm < 0)
  11318. sd->spiritcharm = 0;
  11319. if (sd->spiritcharm && sd->spiritcharm >= max) {
  11320. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  11321. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  11322. sd->spiritcharm--;
  11323. if (sd->spiritcharm != 0)
  11324. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  11325. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11326. }
  11327. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  11328. 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);
  11329. if (i != sd->spiritcharm)
  11330. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  11331. sd->spiritcharm_timer[i] = tid;
  11332. sd->spiritcharm++;
  11333. sd->spiritcharm_type = type;
  11334. clif_spiritcharm( *sd );
  11335. }
  11336. /**
  11337. * Removes one or more spirit charms.
  11338. * @param sd: The target character
  11339. * @param count: Amount of charms to remove
  11340. * @param type: Type of charm to remove
  11341. */
  11342. void pc_delspiritcharm(map_session_data *sd, int32 count, int32 type)
  11343. {
  11344. int32 i;
  11345. nullpo_retv(sd);
  11346. if (sd->spiritcharm_type != type)
  11347. return;
  11348. if (sd->spiritcharm <= 0) {
  11349. sd->spiritcharm = 0;
  11350. return;
  11351. }
  11352. if (count <= 0)
  11353. return;
  11354. if (count > sd->spiritcharm)
  11355. count = sd->spiritcharm;
  11356. sd->spiritcharm -= count;
  11357. if (count > MAX_SPIRITCHARM)
  11358. count = MAX_SPIRITCHARM;
  11359. for (i = 0; i < count; i++) {
  11360. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  11361. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  11362. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11363. }
  11364. }
  11365. for (i = count; i < MAX_SPIRITCHARM; i++) {
  11366. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  11367. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11368. }
  11369. if (sd->spiritcharm <= 0)
  11370. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11371. clif_spiritcharm( *sd );
  11372. }
  11373. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11374. /**
  11375. * Renewal EXP/Item Drop rate modifier based on level penalty
  11376. * @param level_diff: Monster and Player level difference
  11377. * @param mob_class: Monster class
  11378. * @param mode: Monster mode
  11379. * @param type: 1 - EXP, 2 - Item Drop
  11380. * @return Penalty rate
  11381. */
  11382. uint16 pc_level_penalty_mod( map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  11383. // No player was attached, we don't use any modifier (100 = rates are not touched)
  11384. if( sd == nullptr ){
  11385. return 100;
  11386. }
  11387. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  11388. return 100;
  11389. }
  11390. int32 monster_level;
  11391. if( md != nullptr ){
  11392. monster_level = md->level;
  11393. mob = md->db;
  11394. }else if( mob != nullptr ){
  11395. monster_level = mob->lv;
  11396. }else{
  11397. return 100;
  11398. }
  11399. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  11400. return 100;
  11401. }
  11402. int32 level_difference = monster_level - sd->status.base_level;
  11403. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  11404. if( penalty != nullptr ){
  11405. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  11406. }else{
  11407. return 100;
  11408. }
  11409. }
  11410. #endif
  11411. int32 pc_split_str(char *str,char **val,int32 num)
  11412. {
  11413. int32 i;
  11414. for (i=0; i<num && str; i++){
  11415. val[i] = str;
  11416. str = strchr(str,',');
  11417. if (str && i<num-1) //Do not remove a trailing comma.
  11418. *str++=0;
  11419. }
  11420. return i;
  11421. }
  11422. int32 pc_split_atoui(char* str, uint32* val, char sep, int32 max)
  11423. {
  11424. static int32 warning=0;
  11425. int32 i,j;
  11426. for (i=0; i<max; i++) {
  11427. double f;
  11428. if (!str) break;
  11429. f = atof(str);
  11430. if (f < 0)
  11431. val[i] = 0;
  11432. else if (f > UINT_MAX) {
  11433. val[i] = UINT_MAX;
  11434. if (!warning) {
  11435. warning = 1;
  11436. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  11437. }
  11438. } else
  11439. val[i] = (uint32)f;
  11440. str = strchr(str,sep);
  11441. if (str)
  11442. *str++=0;
  11443. }
  11444. //Zero up the remaining.
  11445. for(j=i; j < max; j++)
  11446. val[j] = 0;
  11447. return i;
  11448. }
  11449. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int32 class_, uint16 skill_id) {
  11450. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  11451. if (tree != nullptr)
  11452. return util::umap_find(tree->skills, skill_id);
  11453. return nullptr;
  11454. }
  11455. const std::string SkillTreeDatabase::getDefaultLocation() {
  11456. return std::string(db_path) + "/skill_tree.yml";
  11457. }
  11458. /**
  11459. * Reads and parses an entry from the skill_tree.
  11460. * @param node: YAML node containing the entry.
  11461. * @return count of successfully parsed rows
  11462. */
  11463. uint64 SkillTreeDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11464. std::string job_name;
  11465. if (!this->asString(node, "Job", job_name))
  11466. return 0;
  11467. int64 constant;
  11468. std::string job_name_constant = "JOB_" + job_name;
  11469. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11470. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  11471. return 0;
  11472. }
  11473. uint16 job_id = static_cast<uint16>(constant);
  11474. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  11475. bool exists = tree != nullptr;
  11476. if (!exists)
  11477. tree = std::make_shared<s_skill_tree>();
  11478. if (this->nodeExists(node, "Inherit")) {
  11479. const ryml::NodeRef& InheritNode = node["Inherit"];
  11480. for (const auto &Inheritit : InheritNode) {
  11481. std::string inheritname;
  11482. c4::from_chars(Inheritit.key(), &inheritname);
  11483. std::string inheritname_constant = "JOB_" + inheritname;
  11484. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11485. this->invalidWarning(InheritNode[Inheritit.key()], "Invalid job %s.\n", inheritname.c_str());
  11486. return 0;
  11487. }
  11488. bool active;
  11489. if (!this->asBool(InheritNode, inheritname, active))
  11490. return 0;
  11491. uint16 inherit_job = static_cast<uint16>(constant);
  11492. if (!active) {
  11493. if (exists)
  11494. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  11495. }
  11496. else {
  11497. if (!util::vector_exists(tree->inherit_job, inherit_job))
  11498. tree->inherit_job.push_back(inherit_job);
  11499. }
  11500. }
  11501. }
  11502. if (this->nodeExists(node, "Tree")) {
  11503. for (const auto &it : node["Tree"]) {
  11504. std::string skill_name;
  11505. if (!this->asString(it, "Name", skill_name))
  11506. return 0;
  11507. uint16 skill_id = skill_name2id(skill_name.c_str());
  11508. if (skill_id == 0) {
  11509. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  11510. return 0;
  11511. }
  11512. if (!skill_get_index(skill_id)) {
  11513. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  11514. return 0;
  11515. }
  11516. std::shared_ptr<s_skill_tree_entry> entry;
  11517. bool skill_exists = tree->skills.count(skill_id) > 0;
  11518. if (skill_exists)
  11519. entry = tree->skills[skill_id];
  11520. else
  11521. entry = std::make_shared<s_skill_tree_entry>();
  11522. entry->skill_id = skill_id;
  11523. uint16 max_lv;
  11524. if (!this->asUInt16(it, "MaxLevel", max_lv))
  11525. return 0;
  11526. if (max_lv > MAX_SKILL_LEVEL) {
  11527. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11528. return 0;
  11529. }
  11530. uint16 skill_lv_max = skill_get_max(skill_id);
  11531. if (max_lv > skill_lv_max) {
  11532. 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);
  11533. max_lv = skill_lv_max;
  11534. }
  11535. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11536. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11537. // 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());
  11538. // continue;
  11539. // }
  11540. entry->max_lv = max_lv;
  11541. if (this->nodeExists(it, "BaseLevel")) {
  11542. uint32 baselv;
  11543. if (!this->asUInt32(it, "BaseLevel", baselv))
  11544. return 0;
  11545. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11546. if (baselv > baselv_max) {
  11547. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11548. skill_id, baselv, job_name.c_str(), baselv_max);
  11549. baselv = baselv_max;
  11550. }
  11551. entry->baselv = baselv;
  11552. } else {
  11553. if (!skill_exists)
  11554. entry->baselv = 0;
  11555. }
  11556. if (this->nodeExists(it, "JobLevel")) {
  11557. uint32 joblv;
  11558. if (!this->asUInt32(it, "JobLevel", joblv))
  11559. return 0;
  11560. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11561. if (joblv > joblv_max) {
  11562. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11563. skill_id, joblv, job_name.c_str(), joblv_max);
  11564. joblv = joblv_max;
  11565. }
  11566. entry->joblv = joblv;
  11567. } else {
  11568. if (!skill_exists)
  11569. entry->joblv = 0;
  11570. }
  11571. if (this->nodeExists(it, "Requires")) {
  11572. for (const auto &Requiresit : it["Requires"]) {
  11573. if (!this->nodesExist(Requiresit, { "Name" }))
  11574. return 0;
  11575. std::string skill_name_req;
  11576. if (!this->asString(Requiresit, "Name", skill_name_req))
  11577. return 0;
  11578. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11579. if (skill_id_req == 0) {
  11580. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11581. return 0;
  11582. }
  11583. uint16 lv_req;
  11584. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11585. return 0;
  11586. if (lv_req > MAX_SKILL_LEVEL) {
  11587. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11588. return 0;
  11589. }
  11590. uint16 lv_req_max = skill_get_max(skill_id_req);
  11591. if (lv_req > lv_req_max) {
  11592. 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);
  11593. lv_req = lv_req_max;
  11594. }
  11595. if (lv_req == 0) {
  11596. if (entry->need.erase(skill_id_req) == 0)
  11597. 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());
  11598. continue;
  11599. }
  11600. entry->need[skill_id_req] = lv_req;
  11601. }
  11602. }
  11603. if (this->nodeExists(it, "Exclude")) {
  11604. bool exclude;
  11605. if (!this->asBool(it, "Exclude", exclude))
  11606. return 0;
  11607. entry->exclude_inherit = exclude;
  11608. } else {
  11609. if (!skill_exists)
  11610. entry->exclude_inherit = false;
  11611. }
  11612. if (!skill_exists)
  11613. tree->skills.insert({ skill_id, entry });
  11614. }
  11615. }
  11616. if (!exists)
  11617. this->put(job_id, tree);
  11618. return true;
  11619. }
  11620. void SkillTreeDatabase::loadingFinished() {
  11621. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11622. for (auto &data : *this) {
  11623. if (data.second->inherit_job.empty())
  11624. continue;
  11625. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11626. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11627. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11628. for (const auto &inherit_job : data.second->inherit_job) {
  11629. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11630. if (tree == nullptr || tree->skills.empty())
  11631. continue;
  11632. for (const auto &it : tree->skills) {
  11633. if (it.second->exclude_inherit)
  11634. continue;
  11635. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11636. continue;
  11637. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11638. skill_tree->skills[it.first] = it.second;
  11639. else
  11640. skill_tree->skills.insert({ it.first, it.second });
  11641. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11642. if (skill->baselv > baselv_max) {
  11643. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11644. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11645. skill->baselv = baselv_max;
  11646. }
  11647. if (skill->joblv > joblv_max) {
  11648. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11649. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11650. skill->joblv = joblv_max;
  11651. }
  11652. }
  11653. }
  11654. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11655. job_tree.insert({ data.first, skill_tree });
  11656. }
  11657. if (!job_tree.empty()) {
  11658. for (auto &data : *this) {
  11659. if (job_tree.count(data.first) == 0)
  11660. continue;
  11661. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11662. }
  11663. }
  11664. // remove skills with max_lv = 0
  11665. for (const auto &job : *this) {
  11666. if (job.second->skills.empty())
  11667. continue;
  11668. auto it = job.second->skills.begin();
  11669. while( it != job.second->skills.end() ){
  11670. if( it->second->max_lv == 0 ){
  11671. it = job.second->skills.erase( it );
  11672. }else{
  11673. it++;
  11674. }
  11675. }
  11676. }
  11677. TypesafeYamlDatabase::loadingFinished();
  11678. }
  11679. /**
  11680. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  11681. * @param level: Base level of player
  11682. * @param job_id: Job ID @see enum e_job
  11683. * @return base_hp
  11684. * @author [Cydh]
  11685. */
  11686. static uint32 pc_calc_basehp(uint16 level, uint16 job_id) {
  11687. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11688. double base_hp = 35 + level * (job->hp_increase / 100.);
  11689. #ifndef RENEWAL
  11690. if (level >= 10 && (job_id == JOB_NINJA || job_id == JOB_GUNSLINGER))
  11691. base_hp += 90;
  11692. #endif
  11693. for (uint16 i = 2; i <= level; i++)
  11694. base_hp += floor(((job->hp_factor / 100.) * i) + 0.5); //Don't have round()
  11695. if (job_id == JOB_SUMMONER || job_id == JOB_SPIRIT_HANDLER)
  11696. base_hp += floor((base_hp / 2) + 0.5);
  11697. return (uint32)base_hp;
  11698. }
  11699. /**
  11700. * Calculates base sp of player.
  11701. * @param level: Base level of player
  11702. * @param job_id: Job ID @see enum e_job
  11703. * @return base_sp
  11704. * @author [Playtester]
  11705. */
  11706. static uint32 pc_calc_basesp(uint16 level, uint16 job_id) {
  11707. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11708. double base_sp = 10 + floor(level * (job->sp_increase / 100.));
  11709. switch (job_id) {
  11710. case JOB_NINJA:
  11711. if (level >= 10)
  11712. base_sp -= 22;
  11713. else
  11714. base_sp = 11 + 3*level;
  11715. break;
  11716. case JOB_GUNSLINGER:
  11717. if (level > 10)
  11718. base_sp -= 18;
  11719. else
  11720. base_sp = 9 + 3*level;
  11721. break;
  11722. case JOB_SUMMONER:
  11723. case JOB_SPIRIT_HANDLER:
  11724. base_sp -= floor(base_sp / 2);
  11725. break;
  11726. }
  11727. return (uint32)base_sp;
  11728. }
  11729. const std::string JobDatabase::getDefaultLocation() {
  11730. return std::string(db_path) + "/job_stats.yml";
  11731. }
  11732. /**
  11733. * Reads and parses an entry from the job_db.
  11734. * @param node: YAML node containing the entry.
  11735. * @return count of successfully parsed rows
  11736. */
  11737. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11738. if (this->nodeExists(node, "Jobs")) {
  11739. const ryml::NodeRef& jobsNode = node["Jobs"];
  11740. for (const auto &jobit : jobsNode) {
  11741. std::string job_name;
  11742. c4::from_chars(jobit.key(), &job_name);
  11743. std::string job_name_constant = "JOB_" + job_name;
  11744. int64 job_id;
  11745. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11746. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11747. return 0;
  11748. }
  11749. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11750. bool exists = job != nullptr;
  11751. if (!exists) {
  11752. job = std::make_shared<s_job_info>();
  11753. job->job_bonus.resize(MAX_LEVEL);
  11754. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11755. job->base_hp.resize(MAX_LEVEL);
  11756. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11757. job->base_sp.resize(MAX_LEVEL);
  11758. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11759. job->base_ap.resize(MAX_LEVEL);
  11760. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11761. }
  11762. if (this->nodeExists(node, "MaxWeight")) {
  11763. uint32 weight;
  11764. if (!this->asUInt32(node, "MaxWeight", weight))
  11765. return 0;
  11766. job->max_weight_base = weight;
  11767. } else {
  11768. if (!exists)
  11769. job->max_weight_base = 20000;
  11770. }
  11771. if (this->nodeExists(node, "HpFactor")) {
  11772. uint32 hp;
  11773. if (!this->asUInt32(node, "HpFactor", hp))
  11774. return 0;
  11775. job->hp_factor = hp;
  11776. } else {
  11777. if (!exists)
  11778. job->hp_factor = 0;
  11779. }
  11780. if (this->nodeExists(node, "HpIncrease")) {
  11781. uint32 hp;
  11782. if (!this->asUInt32(node, "HpIncrease", hp))
  11783. return 0;
  11784. job->hp_increase = hp;
  11785. } else {
  11786. if (!exists)
  11787. job->hp_increase = 500;
  11788. }
  11789. if (this->nodeExists(node, "SpIncrease")) {
  11790. uint32 sp;
  11791. if (!this->asUInt32(node, "SpIncrease", sp))
  11792. return 0;
  11793. job->sp_increase = sp;
  11794. } else {
  11795. if (!exists)
  11796. job->sp_increase = 100;
  11797. }
  11798. if (this->nodeExists(node, "BaseASPD")) {
  11799. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11800. uint8 max = MAX_WEAPON_TYPE;
  11801. #ifdef RENEWAL // Renewal adds an extra column for shields
  11802. max += 1;
  11803. #endif
  11804. if (!exists) {
  11805. job->aspd_base.resize(max);
  11806. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11807. }
  11808. for (const auto &aspdit : aspdNode) {
  11809. std::string weapon;
  11810. c4::from_chars(aspdit.key(), &weapon);
  11811. std::string weapon_constant = "W_" + weapon;
  11812. int64 constant;
  11813. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11814. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11815. continue;
  11816. }
  11817. if (constant < W_FIST || constant >= max) {
  11818. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11819. continue;
  11820. }
  11821. int16 aspd;
  11822. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11823. return 0;
  11824. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11825. }
  11826. } else {
  11827. if (!exists) {
  11828. uint8 max = MAX_WEAPON_TYPE;
  11829. #ifdef RENEWAL // Renewal adds an extra column for shields
  11830. max += 1;
  11831. #endif
  11832. job->aspd_base.resize(max);
  11833. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11834. }
  11835. }
  11836. if (this->nodeExists(node, "MaxStats")) {
  11837. const ryml::NodeRef& statNode = node["MaxStats"];
  11838. for (const auto &statit : statNode) {
  11839. std::string stat;
  11840. c4::from_chars(statit.key(), &stat);
  11841. std::string stat_constant = "PARAM_" + stat;
  11842. int64 constant;
  11843. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11844. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11845. continue;
  11846. }
  11847. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11848. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11849. continue;
  11850. }
  11851. uint16 max;
  11852. if (!this->asUInt16(statNode, stat.c_str(), max))
  11853. return 0;
  11854. job->max_param[constant] = max;
  11855. }
  11856. }
  11857. if (this->nodeExists(node, "MaxBaseLevel")) {
  11858. uint16 level;
  11859. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11860. return 0;
  11861. if (level == 0 || level > MAX_LEVEL) {
  11862. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11863. level = MAX_LEVEL;
  11864. }
  11865. job->max_base_level = level;
  11866. } else {
  11867. if (!exists)
  11868. job->max_base_level = MAX_LEVEL;
  11869. }
  11870. if (this->nodeExists(node, "BaseExp")) {
  11871. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11872. uint16 level;
  11873. if (!this->asUInt16(bexpNode, "Level", level))
  11874. return 0;
  11875. if (level > job->max_base_level)
  11876. continue;
  11877. if (level == 0 || level > MAX_LEVEL) {
  11878. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11879. return 0;
  11880. }
  11881. if (this->nodeExists(bexpNode, "Exp")) {
  11882. t_exp exp;
  11883. if (!this->asUInt64(bexpNode, "Exp", exp))
  11884. return 0;
  11885. job->base_exp[level - 1] = exp;
  11886. }
  11887. }
  11888. }
  11889. if (this->nodeExists(node, "MaxJobLevel")) {
  11890. uint16 level;
  11891. if (!this->asUInt16(node, "MaxJobLevel", level))
  11892. return 0;
  11893. if (level == 0 || level > MAX_LEVEL) {
  11894. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11895. level = MAX_LEVEL;
  11896. }
  11897. job->max_job_level = level;
  11898. } else {
  11899. if (!exists)
  11900. job->max_job_level = MAX_LEVEL;
  11901. }
  11902. if (this->nodeExists(node, "JobExp")) {
  11903. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11904. uint16 level;
  11905. if (!this->asUInt16(jexpNode, "Level", level))
  11906. return 0;
  11907. if (level > job->max_job_level)
  11908. continue;
  11909. if (level == 0 || level > MAX_LEVEL) {
  11910. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11911. return 0;
  11912. }
  11913. if (this->nodeExists(jexpNode, "Exp")) {
  11914. t_exp exp;
  11915. if (!this->asUInt64(jexpNode, "Exp", exp))
  11916. return 0;
  11917. job->job_exp[level - 1] = exp;
  11918. }
  11919. }
  11920. }
  11921. if (this->nodeExists(node, "BonusStats")) {
  11922. const ryml::NodeRef& bonusNode = node["BonusStats"];
  11923. for (const ryml::NodeRef& levelNode : bonusNode) {
  11924. uint16 level;
  11925. if (!this->asUInt16(levelNode, "Level", level))
  11926. return 0;
  11927. if (level == 0 || level > MAX_LEVEL) {
  11928. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11929. return 0;
  11930. }
  11931. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  11932. if (this->nodeExists(levelNode, parameter_names[idx])) {
  11933. int16 change;
  11934. if (!this->asInt16(levelNode, parameter_names[idx], change))
  11935. return 0;
  11936. job->job_bonus[level - 1][idx] = change;
  11937. }
  11938. }
  11939. }
  11940. }
  11941. #ifdef HP_SP_TABLES
  11942. if (this->nodeExists(node, "BaseHp")) {
  11943. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  11944. uint16 level;
  11945. if (!this->asUInt16(bhpNode, "Level", level))
  11946. return 0;
  11947. if (level > job->max_base_level)
  11948. continue;
  11949. if (level == 0 || level > MAX_LEVEL) {
  11950. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11951. return 0;
  11952. }
  11953. if (this->nodeExists(bhpNode, "Hp")) {
  11954. uint32 points;
  11955. if (!this->asUInt32(bhpNode, "Hp", points))
  11956. return 0;
  11957. job->base_hp[level - 1] = points;
  11958. }
  11959. }
  11960. }
  11961. if (this->nodeExists(node, "BaseSp")) {
  11962. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  11963. uint16 level;
  11964. if (!this->asUInt16(bspNode, "Level", level))
  11965. return 0;
  11966. if (level > job->max_base_level)
  11967. continue;
  11968. if (level == 0 || level > MAX_LEVEL) {
  11969. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11970. return 0;
  11971. }
  11972. if (this->nodeExists(bspNode, "Sp")) {
  11973. uint32 points;
  11974. if (!this->asUInt32(bspNode, "Sp", points))
  11975. return 0;
  11976. job->base_sp[level - 1] = points;
  11977. }
  11978. }
  11979. }
  11980. if (this->nodeExists(node, "BaseAp")) {
  11981. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  11982. uint16 level;
  11983. if (!this->asUInt16(bapNode, "Level", level))
  11984. return 0;
  11985. if (level > job->max_base_level)
  11986. continue;
  11987. if (level == 0 || level > MAX_LEVEL) {
  11988. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11989. return 0;
  11990. }
  11991. if (this->nodeExists(bapNode, "Ap")) {
  11992. uint32 points;
  11993. if (!this->asUInt32(bapNode, "Ap", points))
  11994. return 0;
  11995. job->base_ap[level - 1] = points;
  11996. }
  11997. }
  11998. }
  11999. #endif
  12000. if (!exists)
  12001. this->put(static_cast<uint16>(job_id), job);
  12002. }
  12003. }
  12004. return 1;
  12005. }
  12006. void JobDatabase::loadingFinished() {
  12007. // Checking if all class have their data
  12008. for (auto &jobIt : *this) {
  12009. uint16 job_id = jobIt.first;
  12010. if (!pcdb_checkid(job_id))
  12011. continue;
  12012. 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)
  12013. continue; // Classes that do not need exp tables.
  12014. std::shared_ptr<s_job_info> job = jobIt.second;
  12015. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  12016. if (!maxBaseLv)
  12017. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  12018. if (!maxJobLv)
  12019. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  12020. // Init and checking the empty value of Base HP/SP [Cydh]
  12021. if (job->base_hp.empty())
  12022. job->base_hp.resize(maxBaseLv);
  12023. for (uint16 j = 0; j < maxBaseLv; j++) {
  12024. if (job->base_hp[j] == 0)
  12025. job->base_hp[j] = pc_calc_basehp(j + 1, job_id);
  12026. }
  12027. if (job->base_sp.empty())
  12028. job->base_sp.resize(maxBaseLv);
  12029. for (uint16 j = 0; j < maxBaseLv; j++) {
  12030. if (job->base_sp[j] == 0)
  12031. job->base_sp[j] = pc_calc_basesp(j + 1, job_id);
  12032. }
  12033. // Resize to the maximum base level
  12034. if (job->base_hp.capacity() > maxBaseLv)
  12035. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  12036. if (job->base_sp.capacity() > maxBaseLv)
  12037. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  12038. if (job->base_ap.capacity() > maxBaseLv)
  12039. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  12040. // Resize to the maximum job level
  12041. if (job->job_bonus.capacity() > maxJobLv)
  12042. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  12043. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  12044. // Store total
  12045. int16 current = 0;
  12046. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  12047. // Add the bonus from this job level
  12048. current += job->job_bonus[job_level][parameter];
  12049. // Set the new total on this job level
  12050. job->job_bonus[job_level][parameter] = current;
  12051. }
  12052. }
  12053. uint64 class_ = pc_jobid2mapid( job_id );
  12054. // Set normal status limits
  12055. uint16 max = battle_config.max_parameter;
  12056. do{
  12057. // Always check babies first
  12058. if( class_ & JOBL_BABY ){
  12059. if( class_ & JOBL_THIRD ){
  12060. max = battle_config.max_baby_third_parameter;
  12061. break;
  12062. }else{
  12063. max = battle_config.max_baby_parameter;
  12064. break;
  12065. }
  12066. }
  12067. // Summoner
  12068. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  12069. max = battle_config.max_summoner_parameter;
  12070. break;
  12071. }
  12072. // Extended classes
  12073. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  12074. max = battle_config.max_extended_parameter;
  12075. break;
  12076. }
  12077. if( class_ & JOBL_FOURTH ){
  12078. max = battle_config.max_fourth_parameter;
  12079. break;
  12080. }
  12081. // 3rd class
  12082. if( class_ & JOBL_THIRD ){
  12083. // Transcendent
  12084. if( class_ & JOBL_UPPER ){
  12085. max = battle_config.max_third_trans_parameter;
  12086. break;
  12087. }else{
  12088. max = battle_config.max_third_parameter;
  12089. break;
  12090. }
  12091. }
  12092. // Transcendent
  12093. if( class_ & JOBL_UPPER ){
  12094. max = battle_config.max_trans_parameter;
  12095. break;
  12096. }
  12097. }while( false );
  12098. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  12099. // If it is not explicitly set in the database file
  12100. if( job->max_param[parameter] == 0 ){
  12101. job->max_param[parameter] = max;
  12102. }
  12103. }
  12104. // Set trait status limit
  12105. if( class_ & JOBL_FOURTH ){
  12106. max = battle_config.max_trait_parameter;
  12107. }else{
  12108. max = 0;
  12109. }
  12110. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  12111. // If it is not explicitly set in the database file
  12112. if( job->max_param[parameter] == 0 ){
  12113. job->max_param[parameter] = max;
  12114. }
  12115. }
  12116. }
  12117. TypesafeCachedYamlDatabase::loadingFinished();
  12118. }
  12119. /**
  12120. * Read job_noenter_map.txt
  12121. **/
  12122. static bool pc_readdb_job_noenter_map( char *str[], size_t columns, size_t current ){
  12123. int32 class_ = -1;
  12124. int64 class_tmp;
  12125. if (ISDIGIT(str[0][0])) {
  12126. class_ = atoi(str[0]);
  12127. } else {
  12128. if (!script_get_constant(str[0], &class_tmp)) {
  12129. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  12130. return false;
  12131. }
  12132. class_ = static_cast<int>(class_tmp);
  12133. }
  12134. if (!pcdb_checkid(class_)) {
  12135. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  12136. return false;
  12137. }
  12138. std::shared_ptr<s_job_info> job = job_db.find(class_);
  12139. if (job == nullptr) {
  12140. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  12141. return false;
  12142. }
  12143. job->noenter_map.zone = atoi(str[1]);
  12144. job->noenter_map.group_lv = atoi(str[2]);
  12145. return true;
  12146. }
  12147. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  12148. return std::string(db_path) + "/statpoint.yml";
  12149. }
  12150. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  12151. uint16 level;
  12152. if (!this->asUInt16(node, "Level", level))
  12153. return 0;
  12154. if (level == 0) {
  12155. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  12156. return 0;
  12157. }
  12158. if (level > MAX_LEVEL) {
  12159. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  12160. return 0;
  12161. }
  12162. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12163. bool exists = entry != nullptr;
  12164. if( !exists ){
  12165. if( !this->nodesExist( node, { "Points" } ) ){
  12166. return 0;
  12167. }
  12168. entry = std::make_shared<s_statpoint_entry>();
  12169. entry->level = level;
  12170. }
  12171. if( this->nodeExists( node, "Points" ) ){
  12172. uint32 points;
  12173. if( !this->asUInt32( node, "Points", points ) ){
  12174. return 0;
  12175. }
  12176. entry->statpoints = points;
  12177. }
  12178. if( this->nodeExists( node, "TraitPoints" ) ){
  12179. uint32 traitpoints;
  12180. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  12181. return 0;
  12182. }
  12183. entry->traitpoints = traitpoints;
  12184. }else{
  12185. if( !exists ){
  12186. entry->traitpoints = 0;
  12187. }
  12188. }
  12189. if( !exists ){
  12190. this->put( level, entry );
  12191. }
  12192. return 1;
  12193. }
  12194. /**
  12195. * Generate the remaining parts of the db if necessary.
  12196. */
  12197. void PlayerStatPointDatabase::loadingFinished(){
  12198. const uint16 trait_start_level = 200;
  12199. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  12200. if( level_one == nullptr ){
  12201. if( battle_config.use_statpoint_table ){
  12202. ShowError( "Missing status points for Level 1\n" );
  12203. }
  12204. level_one = std::make_shared<s_statpoint_entry>();
  12205. level_one->level = 1;
  12206. level_one->statpoints = inter_config.start_status_points;
  12207. level_one->traitpoints = 0;
  12208. this->put( 1, level_one );
  12209. }else if( battle_config.use_statpoint_table ){
  12210. if( level_one->statpoints != inter_config.start_status_points ){
  12211. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, inter_config.start_status_points );
  12212. level_one->statpoints = inter_config.start_status_points;
  12213. }
  12214. }else{
  12215. level_one->statpoints = inter_config.start_status_points;
  12216. level_one->traitpoints = 0;
  12217. }
  12218. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  12219. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  12220. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12221. bool exists = entry != nullptr;
  12222. if( !exists ){
  12223. entry = std::make_shared<s_statpoint_entry>();
  12224. entry->level = level;
  12225. this->put( level, entry );
  12226. }
  12227. if( !battle_config.use_statpoint_table || !exists ){
  12228. if( battle_config.use_statpoint_table ){
  12229. ShowError("Missing status points for Level %hu\n", level);
  12230. }
  12231. if( level <= trait_start_level ){
  12232. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  12233. }else{
  12234. entry->statpoints = last_level->statpoints;
  12235. }
  12236. }
  12237. if( !battle_config.use_traitpoint_table || !exists ){
  12238. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  12239. ShowError( "Missing trait points for Level %hu\n", level );
  12240. }
  12241. if( level > trait_start_level ){
  12242. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  12243. }else{
  12244. entry->traitpoints = 0;
  12245. }
  12246. }
  12247. // Store it for next iteration
  12248. last_level = entry;
  12249. }
  12250. TypesafeCachedYamlDatabase::loadingFinished();
  12251. }
  12252. /*==========================================
  12253. * pc DB reading.
  12254. * job_stats.yml - Job values
  12255. * skill_tree.txt - skill tree for every class
  12256. * attr_fix.yml - elemental adjustment table
  12257. *------------------------------------------*/
  12258. void pc_readdb(void) {
  12259. int32 i, s = 1;
  12260. const char* dbsubpath[] = {
  12261. "",
  12262. "/" DBIMPORT,
  12263. //add other path here
  12264. };
  12265. //reset
  12266. job_db.clear(); // job_info table
  12267. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  12268. penalty_db.load();
  12269. #endif
  12270. statpoint_db.clear();
  12271. job_db.load();
  12272. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  12273. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  12274. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  12275. char* dbsubpath1 = (char*)aMalloc(n1+1);
  12276. char* dbsubpath2 = (char*)aMalloc(n2+1);
  12277. if(i==0) {
  12278. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12279. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  12280. }
  12281. else {
  12282. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12283. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  12284. }
  12285. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  12286. aFree(dbsubpath1);
  12287. aFree(dbsubpath2);
  12288. }
  12289. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  12290. skill_tree_db.reload();
  12291. statpoint_db.load();
  12292. }
  12293. // Read MOTD on startup. [Valaris]
  12294. int32 pc_read_motd(void)
  12295. {
  12296. FILE* fp;
  12297. // clear old MOTD
  12298. memset(motd_text, 0, sizeof(motd_text));
  12299. // read current MOTD
  12300. if( ( fp = fopen(motd_txt, "r") ) != nullptr )
  12301. {
  12302. uint32 entries = 0;
  12303. char buf[CHAT_SIZE_MAX];
  12304. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  12305. {
  12306. uint32 lines = 0;
  12307. size_t len;
  12308. lines++;
  12309. if( buf[0] == '/' && buf[1] == '/' )
  12310. continue;
  12311. len = strlen(buf);
  12312. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  12313. len--;
  12314. if( len ) {
  12315. char * ptr;
  12316. buf[len] = 0;
  12317. if( ( ptr = strstr(buf, " :") ) != nullptr && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  12318. 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);
  12319. }
  12320. else {// empty line
  12321. buf[0] = ' ';
  12322. buf[1] = 0;
  12323. }
  12324. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  12325. entries++;
  12326. }
  12327. fclose(fp);
  12328. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  12329. }
  12330. else
  12331. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  12332. return 0;
  12333. }
  12334. void pc_itemcd_do(map_session_data *sd, bool load) {
  12335. int32 i,cursor = 0;
  12336. struct item_cd* cd = nullptr;
  12337. if( load ) {
  12338. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  12339. // no item cooldown is associated with this character
  12340. return;
  12341. }
  12342. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12343. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  12344. sd->item_delay[cursor].tick = cd->tick[i];
  12345. sd->item_delay[cursor].nameid = cd->nameid[i];
  12346. cursor++;
  12347. }
  12348. }
  12349. idb_remove(itemcd_db,sd->status.char_id);
  12350. } else {
  12351. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  12352. // create a new skill cooldown object for map storage
  12353. CREATE( cd, struct item_cd, 1 );
  12354. idb_put( itemcd_db, sd->status.char_id, cd );
  12355. }
  12356. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12357. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  12358. cd->tick[cursor] = sd->item_delay[i].tick;
  12359. cd->nameid[cursor] = sd->item_delay[i].nameid;
  12360. cursor++;
  12361. }
  12362. }
  12363. }
  12364. return;
  12365. }
  12366. /**
  12367. * Add item delay to player's item delay data
  12368. * @param sd Player
  12369. * @param id Item data
  12370. * @param tick Current tick
  12371. * @param n Item index in inventory
  12372. * @return 0: No delay, can consume item.
  12373. * 1: Has delay, cancel consumption.
  12374. **/
  12375. uint8 pc_itemcd_add(map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12376. int32 i;
  12377. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  12378. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  12379. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  12380. if( i < MAX_ITEMDELAYS ) {
  12381. if( sd->item_delay[i].nameid ) {// found
  12382. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  12383. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  12384. char e_msg[CHAT_SIZE_MAX];
  12385. if( e_tick > 99 )
  12386. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  12387. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  12388. else
  12389. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  12390. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  12391. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  12392. return 1; // Delay has not expired yet
  12393. }
  12394. } else {// not yet used item (all slots are initially empty)
  12395. sd->item_delay[i].nameid = id->nameid;
  12396. }
  12397. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  12398. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  12399. } else {// should not happen
  12400. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  12401. }
  12402. //clean up used delays so we can give room for more
  12403. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12404. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  12405. sd->item_delay[i].tick = 0;
  12406. sd->item_delay[i].nameid = 0;
  12407. }
  12408. }
  12409. return 0;
  12410. }
  12411. /**
  12412. * Check if player has delay to reuse item
  12413. * @param sd Player
  12414. * @param id Item data
  12415. * @param tick Current tick
  12416. * @param n Item index in inventory
  12417. * @return 0: No delay, can consume item.
  12418. * 1: Has delay, cancel consumption.
  12419. **/
  12420. uint8 pc_itemcd_check(map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12421. status_change *sc = nullptr;
  12422. nullpo_retr(0, sd);
  12423. nullpo_retr(0, id);
  12424. // Do normal delay assignment
  12425. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  12426. return pc_itemcd_add(sd, id, tick, n);
  12427. // Send reply of delay remains
  12428. if (sc->getSCE(id->delay.sc)) {
  12429. const struct TimerData *timer = get_timer(sc->getSCE(id->delay.sc)->timer);
  12430. clif_msg_value(sd, MSI_ITEM_REUSE_LIMIT_SECOND, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  12431. return 1;
  12432. }
  12433. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  12434. return 0;
  12435. }
  12436. /**
  12437. * Clear the dmglog data from player
  12438. * @param sd
  12439. * @param md
  12440. **/
  12441. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  12442. {
  12443. uint8 i;
  12444. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  12445. if (i < DAMAGELOG_SIZE) {
  12446. md->dmglog[i].id = 0;
  12447. md->dmglog[i].dmg = 0;
  12448. md->dmglog[i].flag = 0;
  12449. }
  12450. }
  12451. /**
  12452. * Add log to player's dmglog
  12453. * @param sd
  12454. * @param id Monster's GID
  12455. **/
  12456. void pc_damage_log_add(map_session_data *sd, int32 id)
  12457. {
  12458. uint8 i = 0;
  12459. if (!sd || !id)
  12460. return;
  12461. //Only store new data, don't need to renew the old one with same id
  12462. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  12463. if (i < DAMAGELOG_SIZE_PC)
  12464. return;
  12465. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12466. if (sd->dmglog[i] == 0) {
  12467. sd->dmglog[i] = id;
  12468. return;
  12469. }
  12470. }
  12471. }
  12472. /**
  12473. * Clear dmglog data from player
  12474. * @param sd
  12475. * @param id Monster's id
  12476. **/
  12477. void pc_damage_log_clear(map_session_data *sd, int32 id)
  12478. {
  12479. uint8 i;
  12480. struct mob_data *md = nullptr;
  12481. if (!sd)
  12482. return;
  12483. if (!id) {
  12484. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12485. if( !sd->dmglog[i] ) //skip the empty value
  12486. continue;
  12487. if ((md = map_id2md(sd->dmglog[i])))
  12488. pc_clear_log_damage_sub(sd->status.char_id,md);
  12489. sd->dmglog[i] = 0;
  12490. }
  12491. }
  12492. else {
  12493. if ((md = map_id2md(id)))
  12494. pc_clear_log_damage_sub(sd->status.char_id,md);
  12495. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  12496. if (i < DAMAGELOG_SIZE_PC)
  12497. sd->dmglog[i] = 0;
  12498. }
  12499. }
  12500. /**
  12501. * Status change data arrived from char-server
  12502. * @param sd: Player data
  12503. */
  12504. void pc_scdata_received(map_session_data *sd) {
  12505. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12506. clif_weight_limit( sd );
  12507. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) && pc_attendance_counter(sd) < 200 ){
  12508. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12509. }
  12510. sd->state.pc_loaded = true;
  12511. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12512. sd->state.connect_new = 1;
  12513. clif_parse_LoadEndAck(sd->fd, sd);
  12514. }
  12515. if (pc_iscarton(sd)) {
  12516. sd->cart_weight_max = 0; // Force a client refesh
  12517. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12518. }
  12519. if (sd->sc.getSCE(SC_SOULENERGY))
  12520. sd->soulball = sd->sc.getSCE(SC_SOULENERGY)->val1;
  12521. }
  12522. /**
  12523. * Check player account expiration time and rental item expirations
  12524. * @param sd: Player data
  12525. */
  12526. void pc_check_expiration(map_session_data *sd) {
  12527. #ifndef ENABLE_SC_SAVING
  12528. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12529. #endif
  12530. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12531. time_t exp_time = sd->expiration_time;
  12532. char tmpstr[1024];
  12533. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12534. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12535. pc_expire_check(sd);
  12536. }
  12537. }
  12538. TIMER_FUNC(pc_expiration_timer){
  12539. map_session_data *sd = map_id2sd(id);
  12540. if( !sd ) return 0;
  12541. sd->expiration_tid = INVALID_TIMER;
  12542. if( sd->fd )
  12543. clif_authfail_fd(sd->fd,10);
  12544. map_quit(sd);
  12545. return 0;
  12546. }
  12547. TIMER_FUNC(pc_autotrade_timer){
  12548. map_session_data *sd = map_id2sd(id);
  12549. if (!sd)
  12550. return 0;
  12551. sd->autotrade_tid = INVALID_TIMER;
  12552. if (sd->state.autotrade&2)
  12553. vending_reopen(*sd);
  12554. if (sd->state.autotrade&4)
  12555. buyingstore_reopen(sd);
  12556. if (!sd->vender_id && !sd->buyer_id) {
  12557. sd->state.autotrade = 0;
  12558. map_quit(sd);
  12559. }
  12560. return 0;
  12561. }
  12562. /* this timer exists only when a character with a expire timer > 24h is online */
  12563. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12564. TIMER_FUNC(pc_global_expiration_timer){
  12565. struct s_mapiterator* iter;
  12566. map_session_data* sd;
  12567. iter = mapit_getallusers();
  12568. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12569. if( sd->expiration_time )
  12570. pc_expire_check(sd);
  12571. mapit_free(iter);
  12572. return 0;
  12573. }
  12574. void pc_expire_check(map_session_data *sd) {
  12575. /* ongoing timer */
  12576. if( sd->expiration_tid != INVALID_TIMER )
  12577. return;
  12578. /* not within the next 24h, enable the global check */
  12579. if( sd->expiration_time > (time(nullptr) + ((60 * 60) * 24)) ) {
  12580. /* global check not running, enable */
  12581. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12582. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12583. return;
  12584. }
  12585. sd->expiration_tid = add_timer(gettick() + (uint32)(sd->expiration_time - time(nullptr)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12586. }
  12587. /**
  12588. * Deposit some money to bank
  12589. * @param sd
  12590. * @param money Amount of money to deposit
  12591. **/
  12592. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(map_session_data *sd, int32 money) {
  12593. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12594. return BDA_ERROR;
  12595. }
  12596. uint32 limit_check = money + sd->bank_vault;
  12597. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12598. return BDA_OVERFLOW;
  12599. } else if ( money > sd->status.zeny ) {
  12600. return BDA_NO_MONEY;
  12601. }
  12602. if( pc_payzeny(sd, money, LOG_TYPE_BANK) )
  12603. return BDA_NO_MONEY;
  12604. sd->bank_vault += money;
  12605. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12606. if( save_settings&CHARSAVE_BANK )
  12607. chrif_save(sd, CSAVE_NORMAL);
  12608. return BDA_SUCCESS;
  12609. }
  12610. /**
  12611. * Withdraw money from bank
  12612. * @param sd
  12613. * @param money Amount of money that will be withdrawn
  12614. **/
  12615. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(map_session_data *sd, int32 money) {
  12616. if (battle_config.feature_banking_state_enforce && !sd->state.banking) {
  12617. return BWA_UNKNOWN_ERROR;
  12618. }
  12619. uint32 limit_check = money + sd->status.zeny;
  12620. if( money <= 0 ) {
  12621. return BWA_UNKNOWN_ERROR;
  12622. } else if ( money > sd->bank_vault ) {
  12623. return BWA_NO_MONEY;
  12624. } else if ( limit_check > MAX_ZENY ) {
  12625. /* no official response for this scenario exists. */
  12626. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12627. return BWA_UNKNOWN_ERROR;
  12628. }
  12629. if( pc_getzeny(sd,money, LOG_TYPE_BANK) )
  12630. return BWA_NO_MONEY;
  12631. sd->bank_vault -= money;
  12632. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12633. if( save_settings&CHARSAVE_BANK )
  12634. chrif_save(sd, CSAVE_NORMAL);
  12635. return BWA_SUCCESS;
  12636. }
  12637. /**
  12638. * Clear Crimson Marker data from caster
  12639. * @param sd: Player
  12640. **/
  12641. void pc_crimson_marker_clear(map_session_data *sd) {
  12642. uint8 i;
  12643. if (!sd)
  12644. return;
  12645. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12646. struct block_list *bl = nullptr;
  12647. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12648. status_change_end(bl,SC_C_MARKER);
  12649. sd->c_marker[i] = 0;
  12650. }
  12651. }
  12652. /**
  12653. * Show version to player
  12654. * @param sd: Player
  12655. **/
  12656. void pc_show_version(map_session_data *sd) {
  12657. const char* svn = get_svn_revision();
  12658. char buf[CHAT_SIZE_MAX];
  12659. if( svn[0] != UNKNOWN_VERSION )
  12660. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12661. else {
  12662. const char* git = get_git_hash();
  12663. if( git[0] != UNKNOWN_VERSION )
  12664. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12665. else
  12666. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12667. }
  12668. clif_displaymessage(sd->fd,buf);
  12669. }
  12670. /**
  12671. * Run bonus_script on player
  12672. * @param sd
  12673. * @author [Cydh]
  12674. **/
  12675. void pc_bonus_script(map_session_data *sd) {
  12676. t_tick now = gettick();
  12677. struct linkdb_node *node = nullptr, *next = nullptr;
  12678. if (!sd || !(node = sd->bonus_script.head))
  12679. return;
  12680. while (node) {
  12681. struct s_bonus_script_entry *entry = nullptr;
  12682. next = node->next;
  12683. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12684. // Only start timer for new bonus_script
  12685. if (entry->tid == INVALID_TIMER) {
  12686. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12687. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12688. entry->tick += now;
  12689. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12690. }
  12691. if (entry->script)
  12692. run_script(entry->script, 0, sd->bl.id, 0);
  12693. else
  12694. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12695. }
  12696. node = next;
  12697. }
  12698. }
  12699. /**
  12700. * Add bonus_script to player
  12701. * @param sd Player
  12702. * @param script_str Script string
  12703. * @param dur Duration in ms
  12704. * @param icon EFST
  12705. * @param flag Flags @see enum e_bonus_script_flags
  12706. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12707. * @return New created entry pointer or nullptr if failed or nullptr if duplicate fail
  12708. * @author [Cydh]
  12709. **/
  12710. 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) {
  12711. struct script_code *script = nullptr;
  12712. struct linkdb_node *node = nullptr;
  12713. struct s_bonus_script_entry *entry = nullptr;
  12714. if (!sd)
  12715. return nullptr;
  12716. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12717. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12718. return nullptr;
  12719. }
  12720. // Duplication checks
  12721. if ((node = sd->bonus_script.head)) {
  12722. while (node) {
  12723. entry = (struct s_bonus_script_entry *)node->data;
  12724. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12725. t_tick newdur = gettick() + dur;
  12726. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12727. settick_timer(entry->tid, newdur);
  12728. script_free_code(script);
  12729. return nullptr;
  12730. }
  12731. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12732. break;
  12733. else { // No duplicate bonus
  12734. script_free_code(script);
  12735. return nullptr;
  12736. }
  12737. }
  12738. node = node->next;
  12739. }
  12740. }
  12741. CREATE(entry, struct s_bonus_script_entry, 1);
  12742. entry->script_buf = StringBuf_Malloc();
  12743. StringBuf_AppendStr(entry->script_buf, script_str);
  12744. entry->tid = INVALID_TIMER;
  12745. entry->flag = flag;
  12746. entry->icon = icon;
  12747. entry->tick = dur; // Use duration first, on run change to expire time
  12748. entry->type = type;
  12749. entry->script = script;
  12750. sd->bonus_script.count++;
  12751. return entry;
  12752. }
  12753. /**
  12754. * Remove bonus_script data from player
  12755. * @param sd: Target player
  12756. * @param list: Bonus script entry from player
  12757. * @author [Cydh]
  12758. **/
  12759. void pc_bonus_script_free_entry(map_session_data *sd, struct s_bonus_script_entry *entry) {
  12760. if (entry->tid != INVALID_TIMER)
  12761. delete_timer(entry->tid, pc_bonus_script_timer);
  12762. if (entry->script)
  12763. script_free_code(entry->script);
  12764. if (entry->script_buf)
  12765. StringBuf_Free(entry->script_buf);
  12766. if (sd) {
  12767. if (entry->icon != EFST_BLANK)
  12768. clif_status_load(&sd->bl, entry->icon, 0);
  12769. if (sd->bonus_script.count > 0)
  12770. sd->bonus_script.count--;
  12771. }
  12772. aFree(entry);
  12773. }
  12774. /**
  12775. * Do final process if no entry left
  12776. * @param sd
  12777. **/
  12778. static void inline pc_bonus_script_check_final(map_session_data *sd) {
  12779. if (sd->bonus_script.count == 0) {
  12780. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12781. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12782. linkdb_final(&sd->bonus_script.head);
  12783. }
  12784. }
  12785. /**
  12786. * Timer for bonus_script
  12787. * @param tid
  12788. * @param tick
  12789. * @param id
  12790. * @param data
  12791. * @author [Cydh]
  12792. **/
  12793. TIMER_FUNC(pc_bonus_script_timer){
  12794. map_session_data *sd;
  12795. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12796. sd = map_id2sd(id);
  12797. if (!sd) {
  12798. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12799. return 0;
  12800. }
  12801. if (tid == INVALID_TIMER)
  12802. return 0;
  12803. if (!sd->bonus_script.head || entry == nullptr) {
  12804. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12805. return 0;
  12806. }
  12807. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12808. pc_bonus_script_free_entry(sd, entry);
  12809. pc_bonus_script_check_final(sd);
  12810. status_calc_pc(sd,SCO_NONE);
  12811. return 0;
  12812. }
  12813. /**
  12814. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12815. * @param sd: Target player
  12816. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12817. * @author [Cydh]
  12818. **/
  12819. void pc_bonus_script_clear(map_session_data *sd, uint32 flag) {
  12820. struct linkdb_node *node = nullptr;
  12821. uint16 count = 0;
  12822. if (!sd || !(node = sd->bonus_script.head))
  12823. return;
  12824. while (node) {
  12825. struct linkdb_node *next = node->next;
  12826. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12827. if (entry && (
  12828. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12829. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12830. (flag&entry->flag) || // Matched flag
  12831. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12832. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12833. )
  12834. )
  12835. {
  12836. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12837. pc_bonus_script_free_entry(sd, entry);
  12838. count++;
  12839. }
  12840. node = next;
  12841. }
  12842. pc_bonus_script_check_final(sd);
  12843. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12844. status_calc_pc(sd,SCO_NONE);
  12845. }
  12846. /** [Cydh]
  12847. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12848. * @param sd: Target player
  12849. */
  12850. void pc_cell_basilica(map_session_data *sd) {
  12851. nullpo_retv(sd);
  12852. #ifdef RENEWAL
  12853. enum sc_type type = SC_BASILICA_CELL;
  12854. #else
  12855. enum sc_type type = SC_BASILICA;
  12856. #endif
  12857. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12858. if (sd->sc.getSCE(type))
  12859. status_change_end(&sd->bl, type);
  12860. }
  12861. else if (!sd->sc.getSCE(type))
  12862. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12863. }
  12864. /** [Cydh]
  12865. * Get maximum specified parameter for specified class
  12866. * @param sd: Player data
  12867. * @param param: Max parameter to check
  12868. * @return max_param
  12869. */
  12870. uint16 pc_maxparameter(map_session_data *sd, e_params param) {
  12871. nullpo_retr(0, sd);
  12872. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12873. if( job == nullptr || param == PARAM_MAX ){
  12874. return 0;
  12875. }
  12876. return job->max_param[param];
  12877. }
  12878. /**
  12879. * Get max ASPD for player based on Class
  12880. * @param sd Player
  12881. * @return ASPD
  12882. */
  12883. short pc_maxaspd(map_session_data *sd) {
  12884. nullpo_ret(sd);
  12885. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12886. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12887. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12888. battle_config.max_aspd ));
  12889. }
  12890. /**
  12891. * Calculates total item-group related bonuses for the given item
  12892. * @param sd Player
  12893. * @param nameid Item ID
  12894. * @return Heal rate
  12895. **/
  12896. short pc_get_itemgroup_bonus(map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12897. if (bonuses.empty())
  12898. return 0;
  12899. short bonus = 0;
  12900. for (const auto &it : bonuses) {
  12901. uint16 group_id = it.id;
  12902. if (group_id == 0)
  12903. continue;
  12904. if (itemdb_group.item_exists(group_id, nameid))
  12905. bonus += it.val;
  12906. }
  12907. return bonus;
  12908. }
  12909. /**
  12910. * Calculates total item-group related bonuses for the given item group
  12911. * @param sd Player
  12912. * @param group_id Item Group ID
  12913. * @return Heal rate
  12914. **/
  12915. short pc_get_itemgroup_bonus_group(map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12916. if (bonuses.empty())
  12917. return 0;
  12918. for (const auto &it : bonuses) {
  12919. if (it.id == group_id)
  12920. return it.val;
  12921. }
  12922. return 0;
  12923. }
  12924. /**
  12925. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12926. * @param eqi Item EQI of enum equip_index
  12927. * @param *equip_index Player's equip_index[]
  12928. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12929. * @return True if item in same inventory index, False if doesn't
  12930. */
  12931. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  12932. if (index < 0 || index >= MAX_INVENTORY)
  12933. return true;
  12934. // Dual weapon checks
  12935. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12936. return true;
  12937. // Headgear with Mid & Low location
  12938. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12939. return true;
  12940. // Headgear with Top & Mid or Low location
  12941. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12942. return true;
  12943. // Headgear with Mid & Low location
  12944. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12945. return true;
  12946. // Headgear with Top & Mid or Low location
  12947. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12948. return true;
  12949. return false;
  12950. }
  12951. /**
  12952. * Generate Unique item ID for player
  12953. * @param sd : Player
  12954. * @return A generated Unique item ID
  12955. */
  12956. uint64 pc_generate_unique_id(map_session_data *sd) {
  12957. nullpo_ret(sd);
  12958. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12959. }
  12960. /**
  12961. * Validating skill from player after logged on
  12962. * @param sd
  12963. **/
  12964. void pc_validate_skill(map_session_data *sd) {
  12965. if (sd) {
  12966. uint16 i = 0, count = 0;
  12967. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  12968. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  12969. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  12970. for (i = 0; i < MAX_SKILL; i++) {
  12971. uint16 idx = 0;
  12972. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  12973. continue;
  12974. if ((idx = skill_get_index(tmp_skills[i].id))) {
  12975. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  12976. count++;
  12977. }
  12978. else
  12979. 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);
  12980. }
  12981. }
  12982. }
  12983. /**
  12984. * Show available NPC Quest / Event Icon Check [Kisuka]
  12985. * @param sd Player
  12986. **/
  12987. void pc_show_questinfo(map_session_data *sd) {
  12988. #if PACKETVER >= 20090218
  12989. nullpo_retv(sd);
  12990. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12991. return;
  12992. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12993. nullpo_retv(mapdata);
  12994. if (mapdata->qi_npc.empty())
  12995. return;
  12996. if (mapdata->qi_npc.size() != sd->qi_display.size())
  12997. return; // init was not called yet
  12998. for (int32 i = 0; i < mapdata->qi_npc.size(); i++) {
  12999. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  13000. if (!nd || nd->qi_data.empty())
  13001. continue;
  13002. bool show = false;
  13003. for (auto &qi : nd->qi_data) {
  13004. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  13005. show = true;
  13006. // Check if need to be displayed
  13007. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  13008. sd->qi_display[i].is_active = true;
  13009. sd->qi_display[i].icon = qi->icon;
  13010. sd->qi_display[i].color = qi->color;
  13011. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  13012. }
  13013. break;
  13014. }
  13015. }
  13016. if (show == false) {
  13017. // Check if need to be hide
  13018. if (sd->qi_display[i].is_active) {
  13019. sd->qi_display[i].is_active = false;
  13020. sd->qi_display[i].icon = QTYPE_NONE;
  13021. sd->qi_display[i].color = QMARK_NONE;
  13022. #if PACKETVER >= 20120410
  13023. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  13024. #else
  13025. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  13026. #endif
  13027. }
  13028. }
  13029. }
  13030. #endif
  13031. }
  13032. /**
  13033. * Reinit the questinfo for player when changing map
  13034. * @param sd Player
  13035. **/
  13036. void pc_show_questinfo_reinit(map_session_data *sd) {
  13037. #if PACKETVER >= 20090218
  13038. nullpo_retv(sd);
  13039. sd->qi_display.clear();
  13040. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  13041. return;
  13042. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  13043. nullpo_retv(mapdata);
  13044. if (mapdata->qi_npc.empty())
  13045. return;
  13046. sd->qi_display.reserve( mapdata->qi_npc.size() );
  13047. for( int32 i = 0; i < mapdata->qi_npc.size(); i++ ){
  13048. sd->qi_display.push_back( s_qi_display() );
  13049. }
  13050. #endif
  13051. }
  13052. /**
  13053. * Check if a job is allowed to enter the map
  13054. * @param jobid Job ID see enum e_job or sd->status.class_
  13055. * @param m ID -an index- for direct indexing map[] array
  13056. * @return 1 if job is allowed, 0 otherwise
  13057. **/
  13058. bool pc_job_can_entermap(enum e_job jobid, int32 m, int32 group_lv) {
  13059. // Map is other map server.
  13060. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  13061. if (m < 0)
  13062. return true;
  13063. struct map_data *mapdata = map_getmapdata(m);
  13064. if (!mapdata->cell)
  13065. return false;
  13066. if (!pcdb_checkid(jobid))
  13067. return false;
  13068. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  13069. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  13070. return true;
  13071. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  13072. (job->noenter_map.zone&2 && mapdata->getMapFlag(MF_PVP)) || // PVP
  13073. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  13074. (job->noenter_map.zone&8 && mapdata->getMapFlag(MF_BATTLEGROUND)) || // Battleground
  13075. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  13076. (job->noenter_map.zone&(mapdata->zone) && mapdata->getMapFlag(MF_RESTRICTED)) // Zone restriction
  13077. )
  13078. return false;
  13079. return true;
  13080. }
  13081. /**
  13082. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  13083. * @param sd
  13084. **/
  13085. void pc_set_costume_view(map_session_data *sd) {
  13086. int32 i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  13087. struct item_data *id = nullptr;
  13088. nullpo_retv(sd);
  13089. head_low = sd->status.head_bottom;
  13090. head_mid = sd->status.head_mid;
  13091. head_top = sd->status.head_top;
  13092. robe = sd->status.robe;
  13093. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  13094. //Added check to prevent sending the same look on multiple slots ->
  13095. //causes client to redraw item on top of itself. (suggested by Lupus)
  13096. // Normal headgear checks
  13097. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13098. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  13099. sd->status.head_bottom = id->look;
  13100. else
  13101. sd->status.head_bottom = 0;
  13102. }
  13103. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13104. if (!(id->equip&(EQP_HEAD_TOP)))
  13105. sd->status.head_mid = id->look;
  13106. else
  13107. sd->status.head_mid = 0;
  13108. }
  13109. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13110. sd->status.head_top = id->look;
  13111. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13112. sd->status.robe = id->look;
  13113. // Costumes check
  13114. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  13115. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  13116. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  13117. sd->status.head_bottom = id->look;
  13118. else
  13119. sd->status.head_bottom = 0;
  13120. }
  13121. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  13122. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  13123. sd->status.head_mid = id->look;
  13124. else
  13125. sd->status.head_mid = 0;
  13126. }
  13127. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  13128. sd->status.head_top = id->look;
  13129. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  13130. sd->status.robe = id->look;
  13131. }
  13132. if (sd->setlook_head_bottom)
  13133. sd->status.head_bottom = sd->setlook_head_bottom;
  13134. if (sd->setlook_head_mid)
  13135. sd->status.head_mid = sd->setlook_head_mid;
  13136. if (sd->setlook_head_top)
  13137. sd->status.head_top = sd->setlook_head_top;
  13138. if (sd->setlook_robe)
  13139. sd->status.robe = sd->setlook_robe;
  13140. if (head_low != sd->status.head_bottom)
  13141. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  13142. if (head_mid != sd->status.head_mid)
  13143. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  13144. if (head_top != sd->status.head_top)
  13145. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  13146. if (robe != sd->status.robe)
  13147. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  13148. }
  13149. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  13150. uint32 date = date_get(DT_YYYYMMDD);
  13151. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  13152. std::shared_ptr<s_attendance_period> period = pair.second;
  13153. if( period->start <= date && period->end >= date ){
  13154. return period;
  13155. }
  13156. }
  13157. return nullptr;
  13158. }
  13159. bool pc_attendance_enabled(){
  13160. // Check if the attendance feature is disabled
  13161. if( !battle_config.feature_attendance ){
  13162. return false;
  13163. }
  13164. // Check if there is a running attendance period
  13165. return pc_attendance_period() != nullptr;
  13166. }
  13167. static inline bool pc_attendance_rewarded_today( map_session_data* sd ){
  13168. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  13169. }
  13170. int32 pc_attendance_counter( map_session_data* sd ){
  13171. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13172. // No running attendance period
  13173. if( period == nullptr ){
  13174. return 0;
  13175. }
  13176. // Get the counter for the current period
  13177. int32 counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13178. // Check if we have a remaining counter from a previous period
  13179. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  13180. // Reset the counter to zero
  13181. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  13182. return 0;
  13183. }
  13184. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  13185. }
  13186. void pc_attendance_claim_reward( map_session_data* sd ){
  13187. // If the user's group does not have the permission
  13188. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  13189. return;
  13190. }
  13191. // Check if the attendance feature is disabled
  13192. if( !pc_attendance_enabled() ){
  13193. return;
  13194. }
  13195. // Check if the user already got his reward today
  13196. if( pc_attendance_rewarded_today( sd ) ){
  13197. return;
  13198. }
  13199. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13200. attendance_counter += 1;
  13201. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13202. if( period == nullptr ){
  13203. return;
  13204. }
  13205. if( period->rewards.size() < attendance_counter ){
  13206. return;
  13207. }
  13208. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  13209. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  13210. if( save_settings&CHARSAVE_ATTENDANCE )
  13211. chrif_save(sd, CSAVE_NORMAL);
  13212. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  13213. struct mail_message msg;
  13214. memset( &msg, 0, sizeof( struct mail_message ) );
  13215. msg.dest_id = sd->status.char_id;
  13216. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  13217. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  13218. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  13219. msg.item[0].nameid = reward->item_id;
  13220. msg.item[0].amount = reward->amount;
  13221. msg.item[0].identify = 1;
  13222. msg.status = MAIL_NEW;
  13223. msg.type = MAIL_INBOX_NORMAL;
  13224. msg.timestamp = time(nullptr);
  13225. intif_Mail_send(0, &msg);
  13226. clif_attendence_response( sd, attendance_counter );
  13227. }
  13228. /**
  13229. * Send a player to jail and determine the location to send in jail.
  13230. * @param sd: Player data
  13231. * @param duration: Duration in minutes (default INT_MAX = infinite)
  13232. */
  13233. void pc_jail(map_session_data &sd, int32 duration) {
  13234. uint16 m_index = mapindex_name2id(MAP_JAIL);
  13235. int16 x, y;
  13236. switch (rnd() % 2) { // Jail Locations
  13237. case 0: // Jail #1
  13238. x = 49;
  13239. y = 75;
  13240. break;
  13241. default: // Default Jail
  13242. x = 24;
  13243. y = 75;
  13244. break;
  13245. }
  13246. duration = i32max(0, duration); // Can't be less than 0 seconds.
  13247. // If duration > 0 then triggered via jailfor which checks every minute.
  13248. // If duration == INT_MAX then triggered via jail for infinite duration.
  13249. // If duration == 0 then triggered via unjail and end status.
  13250. if (duration > 0)
  13251. sc_start4(nullptr, &sd.bl, SC_JAILED, 100, duration, m_index, x, y, 60000);
  13252. else
  13253. status_change_end(&sd.bl, SC_JAILED);
  13254. }
  13255. /**
  13256. * Determine the punishment type when failing macro checks.
  13257. * @param sd: Player data
  13258. * @param stype: Macro detection status type (for banning)
  13259. */
  13260. static void pc_macro_punishment(map_session_data &sd, e_macro_detect_status stype) {
  13261. int32 duration = 0;
  13262. // Determine if there's a unique duration
  13263. if (battle_config.macro_detection_punishment_time > 0) {
  13264. char time[13];
  13265. safesnprintf(time, 13, "+%dnm", battle_config.macro_detection_punishment_time);
  13266. duration = static_cast<int32>(solve_time(time));
  13267. }
  13268. // Delete the timer
  13269. if (sd.macro_detect.timer != INVALID_TIMER)
  13270. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13271. // Clear the macro detect data
  13272. sd.macro_detect = {};
  13273. sd.macro_detect.timer = INVALID_TIMER;
  13274. // Unblock all actions for the player
  13275. sd.state.block_action &= ~PCBLOCK_ALL;
  13276. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13277. if (battle_config.macro_detection_punishment == 0) { // Ban
  13278. clif_macro_detector_status(sd, stype);
  13279. chrif_req_login_operation(sd.macro_detect.reporter_aid, sd.status.name, (duration == 0 ? CHRIF_OP_LOGIN_BLOCK : CHRIF_OP_LOGIN_BAN), duration, 0, 0);
  13280. } else { // Jail
  13281. // Send success to close the window without closing the client
  13282. clif_macro_detector_status(sd, MCD_GOOD);
  13283. pc_jail(sd, (duration == 0 ? INT_MAX : duration / 60));
  13284. }
  13285. }
  13286. /**
  13287. * Save a captcha image to memory via /macro_register.
  13288. * @param sd: Player data
  13289. * @param image_size: Captcha image size
  13290. * @param captcha_answer: Answer to captcha
  13291. */
  13292. void pc_macro_captcha_register(map_session_data &sd, uint16 image_size, const char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13293. nullpo_retv(captcha_answer);
  13294. sd.captcha_upload.cd = nullptr;
  13295. sd.captcha_upload.upload_size = 0;
  13296. if (strlen(captcha_answer) < 4 || image_size == 0 || image_size > CAPTCHA_BMP_SIZE) {
  13297. clif_captcha_upload_request(sd); // Notify client of failure.
  13298. return;
  13299. }
  13300. std::shared_ptr<s_captcha_data> cd = std::make_shared<s_captcha_data>();
  13301. sd.captcha_upload.cd = cd;
  13302. cd->image_size = image_size;
  13303. safestrncpy(cd->captcha_answer, captcha_answer, sizeof(cd->captcha_answer));
  13304. memset(cd->image_data, 0, sizeof(cd->image_data));
  13305. // Request the image data from the client.
  13306. clif_captcha_upload_request(sd);
  13307. }
  13308. /**
  13309. * Save captcha image to server.
  13310. * @param sd: Player data
  13311. * @param captcha_key: Captcha ID
  13312. * @param upload_size: Captcha size
  13313. * @param upload_data: Image data
  13314. */
  13315. void pc_macro_captcha_register_upload(map_session_data &sd, uint16 upload_size, const char *upload_data) {
  13316. nullpo_retv(upload_data);
  13317. memcpy(&sd.captcha_upload.cd->image_data[sd.captcha_upload.upload_size], upload_data, upload_size);
  13318. sd.captcha_upload.upload_size += upload_size;
  13319. // Notify that the image finished uploading.
  13320. if (sd.captcha_upload.upload_size == sd.captcha_upload.cd->image_size) {
  13321. // Tell the client that the upload was finished
  13322. clif_captcha_upload_end(sd);
  13323. // Look for a free key
  13324. uint16 index;
  13325. for (index = 0; index < UINT16_MAX; index++) {
  13326. if (!captcha_db.exists(index)) {
  13327. break;
  13328. }
  13329. }
  13330. if (index == UINT16_MAX) {
  13331. // no free key found...
  13332. sd.captcha_upload.cd = nullptr;
  13333. sd.captcha_upload.upload_size = 0;
  13334. return;
  13335. }
  13336. captcha_db.put(index, sd.captcha_upload.cd);
  13337. sd.captcha_upload.cd = nullptr;
  13338. sd.captcha_upload.upload_size = 0;
  13339. // TODO: write YAML and BMP file?
  13340. }
  13341. }
  13342. /**
  13343. * Timer attached to target player with attempts to confirm captcha.
  13344. */
  13345. TIMER_FUNC(pc_macro_detector_timeout) {
  13346. map_session_data *sd = map_id2sd(id);
  13347. nullpo_ret(sd);
  13348. // Remove the current timer
  13349. sd->macro_detect.timer = INVALID_TIMER;
  13350. // Deduct an answering attempt
  13351. sd->macro_detect.retry -= 1;
  13352. if (sd->macro_detect.retry == 0) {
  13353. // All attempts have been exhausted, punish the user
  13354. pc_macro_punishment(*sd, MCD_TIMEOUT);
  13355. } else {
  13356. // Update the client
  13357. clif_macro_detector_request_show(*sd);
  13358. // Start a new timer
  13359. sd->macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd->bl.id, 0);
  13360. }
  13361. return 0;
  13362. }
  13363. /**
  13364. * Check player's captcha answer.
  13365. * @param sd: Player data
  13366. * @param captcha_answer: Captcha answer entered by player
  13367. */
  13368. void pc_macro_detector_process_answer(map_session_data &sd, const char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13369. nullpo_retv(captcha_answer);
  13370. const std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  13371. // Has no captcha request
  13372. if (cd == nullptr) {
  13373. return;
  13374. }
  13375. // Correct answer
  13376. if (strcmp(captcha_answer, cd->captcha_answer) == 0) {
  13377. // Delete the timer
  13378. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13379. // Clear the macro detect data
  13380. sd.macro_detect = {};
  13381. sd.macro_detect.timer = INVALID_TIMER;
  13382. // Unblock all actions for the player
  13383. sd.state.block_action &= ~PCBLOCK_ALL;
  13384. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13385. // Assign temporary macro variable to check failures
  13386. pc_setreg(&sd, add_str("@captcha_retries"), battle_config.macro_detection_retry - sd.macro_detect.retry);
  13387. // Grant bonuses via script
  13388. run_script(cd->bonus_script, 0, sd.bl.id, fake_nd->bl.id);
  13389. // Notify the client
  13390. clif_macro_detector_status(sd, MCD_GOOD);
  13391. } else {
  13392. // Deduct an answering attempt
  13393. sd.macro_detect.retry -= 1;
  13394. // All attempts have been exhausted, punish the user
  13395. if (sd.macro_detect.retry <= 0) {
  13396. pc_macro_punishment(sd, MCD_INCORRECT);
  13397. return;
  13398. }
  13399. // Incorrect response, update the client
  13400. clif_macro_detector_request_show(sd);
  13401. // Reset the timer
  13402. addtick_timer(sd.macro_detect.timer, gettick() + battle_config.macro_detection_timeout);
  13403. }
  13404. }
  13405. /**
  13406. * Determine if a player tries to log out during a captcha check.
  13407. * @param sd: Player data
  13408. */
  13409. void pc_macro_detector_disconnect(map_session_data &sd) {
  13410. // Delete the timeout timer
  13411. if (sd.macro_detect.timer != INVALID_TIMER) {
  13412. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13413. sd.macro_detect.timer = INVALID_TIMER;
  13414. }
  13415. // If the player disconnects before clearing the challenge the player is punished.
  13416. if (sd.macro_detect.retry != 0)
  13417. pc_macro_punishment(sd, MCD_TIMEOUT);
  13418. }
  13419. /**
  13420. * Save a list of players from an area select via /macro_detector.
  13421. */
  13422. int32 pc_macro_reporter_area_select_sub(block_list *bl, va_list ap) {
  13423. nullpo_retr(0, bl);
  13424. if (bl->type != BL_PC)
  13425. return 0;
  13426. std::vector<uint32> *aid_list = va_arg(ap, std::vector<uint32> *);
  13427. nullpo_ret(aid_list);
  13428. aid_list->push_back(bl->id);
  13429. return 0;
  13430. }
  13431. /**
  13432. * Area select via /macro_detector.
  13433. * @param sd: Player data
  13434. * @param x: X location
  13435. * @param y: Y location
  13436. * @param radius: Area
  13437. */
  13438. void pc_macro_reporter_area_select(map_session_data &sd, const int16 x, const int16 y, const int8 radius) {
  13439. std::vector<uint32> aid_list;
  13440. map_foreachinarea(pc_macro_reporter_area_select_sub, sd.bl.m, x - radius, y - radius, x + radius, y + radius, BL_PC, &aid_list);
  13441. clif_macro_reporter_select(sd, aid_list);
  13442. }
  13443. /**
  13444. * Send out captcha check to player.
  13445. * @param sd: Target player data
  13446. * @param reporter_account_id: Account ID of reporter
  13447. */
  13448. void pc_macro_reporter_process(map_session_data &sd, int32 reporter_account_id) {
  13449. if (captcha_db.empty())
  13450. return;
  13451. // Pick a random image from the database.
  13452. const std::shared_ptr<s_captcha_data> cd = captcha_db.random();
  13453. // Set macro detection data.
  13454. sd.macro_detect.cd = cd;
  13455. sd.macro_detect.reporter_aid = reporter_account_id;
  13456. sd.macro_detect.retry = battle_config.macro_detection_retry;
  13457. // Block all actions for the target player.
  13458. sd.state.block_action |= (PCBLOCK_ALL | PCBLOCK_IMMUNE);
  13459. // Open macro detect client side.
  13460. clif_macro_detector_request(sd);
  13461. // Start the timeout timer.
  13462. sd.macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd.bl.id, 0);
  13463. }
  13464. /**
  13465. * Parse a BMP image to memory.
  13466. * @param filepath: Image file location
  13467. * @param cd: Captcha data
  13468. */
  13469. bool pc_macro_read_captcha_db_loadbmp(const std::string &filepath, std::shared_ptr<s_captcha_data> cd) {
  13470. if (cd == nullptr)
  13471. return false;
  13472. FILE *fp = fopen(filepath.c_str(), "rb");
  13473. if (fp == nullptr) {
  13474. ShowError("%s: Failed to open file \"%s\"\n", __func__, filepath.c_str());
  13475. return false;
  13476. }
  13477. // Load the file data and verify magic
  13478. char bmp_data[CAPTCHA_BMP_SIZE];
  13479. if (fread(bmp_data, CAPTCHA_BMP_SIZE, 1, fp) != 1) {
  13480. ShowError("%s: Failed to read data from \"%s\"\n", __func__, filepath.c_str());
  13481. fclose(fp);
  13482. return false;
  13483. }
  13484. fclose(fp);
  13485. if (bmp_data[0] != 'B' || bmp_data[1] != 'M') {
  13486. ShowError("%s: Invalid BMP file header given at \"%s\"\n", __func__, filepath.c_str());
  13487. return false;
  13488. }
  13489. // Compress the data into the destination
  13490. unsigned long com_size = sizeof(cd->image_data);
  13491. encode_zip(cd->image_data, &com_size, bmp_data, CAPTCHA_BMP_SIZE);
  13492. cd->image_size = static_cast<int16>(com_size);
  13493. return true;
  13494. }
  13495. const std::string CaptchaDatabase::getDefaultLocation() {
  13496. return std::string(db_path) + "/captcha_db.yml";
  13497. }
  13498. /**
  13499. * Reads and parses an entry from the captcha_db.
  13500. * @param node: YAML node containing the entry.
  13501. * @return count of successfully parsed rows
  13502. */
  13503. uint64 CaptchaDatabase::parseBodyNode(const ryml::NodeRef &node) {
  13504. uint16 index;
  13505. if (!this->asUInt16(node, "Id", index))
  13506. return 0;
  13507. std::shared_ptr<s_captcha_data> cd = captcha_db.find(index);
  13508. bool exists = cd != nullptr;
  13509. if (!exists) {
  13510. if (!this->nodesExist(node, { "Filename", "Answer" }))
  13511. return 0;
  13512. cd = std::make_shared<s_captcha_data>();
  13513. cd->index = index;
  13514. }
  13515. if (this->nodeExists(node, "Filename")) {
  13516. std::string filename;
  13517. if (!this->asString(node, "Filename", filename))
  13518. return 0;
  13519. if (!pc_macro_read_captcha_db_loadbmp(filename, cd)) {
  13520. this->invalidWarning(node["Filename"], "Failed to parse BMP image, skipping...\n");
  13521. return 0;
  13522. }
  13523. }
  13524. if (this->nodeExists(node, "Answer")) {
  13525. std::string answer;
  13526. if (!this->asString(node, "Answer", answer))
  13527. return 0;
  13528. if (answer.length() < 4 || answer.length() > CAPTCHA_ANSWER_SIZE) {
  13529. this->invalidWarning(node["Answer"], "The captcha answer must be between 4~%d characters, skipping...", CAPTCHA_ANSWER_SIZE);
  13530. return 0;
  13531. }
  13532. safestrncpy(cd->captcha_answer, answer.c_str(), sizeof(cd->captcha_answer));
  13533. }
  13534. if (this->nodeExists(node, "Bonus")) {
  13535. std::string script;
  13536. if (!this->asString(node, "Bonus", script)) {
  13537. return 0;
  13538. }
  13539. if (cd->bonus_script) {
  13540. script_free_code(cd->bonus_script);
  13541. cd->bonus_script = nullptr;
  13542. }
  13543. cd->bonus_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Bonus"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13544. } else {
  13545. if (!exists)
  13546. 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);
  13547. }
  13548. if (!exists)
  13549. captcha_db.put(index, cd);
  13550. return 1;
  13551. }
  13552. /*==========================================
  13553. * pc Init/Terminate
  13554. *------------------------------------------*/
  13555. void do_final_pc(void) {
  13556. db_destroy(itemcd_db);
  13557. do_final_pc_groups();
  13558. ers_destroy(pc_sc_display_ers);
  13559. ers_destroy(num_reg_ers);
  13560. ers_destroy(str_reg_ers);
  13561. attendance_db.clear();
  13562. reputation_db.clear();
  13563. #ifdef MAP_GENERATOR
  13564. reputationgroup_db.clear();
  13565. #endif
  13566. penalty_db.clear();
  13567. captcha_db.clear();
  13568. }
  13569. void do_init_pc(void) {
  13570. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  13571. pc_readdb();
  13572. pc_read_motd(); // Read MOTD [Valaris]
  13573. attendance_db.load();
  13574. reputation_db.load();
  13575. #ifdef MAP_GENERATOR
  13576. reputationgroup_db.load();
  13577. #endif
  13578. captcha_db.load();
  13579. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  13580. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  13581. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  13582. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  13583. add_timer_func_list(pc_autosave, "pc_autosave");
  13584. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  13585. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  13586. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  13587. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  13588. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  13589. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  13590. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  13591. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  13592. add_timer_func_list(pc_macro_detector_timeout, "pc_macro_detector_timeout");
  13593. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  13594. // 0=day, 1=night [Yor]
  13595. night_flag = battle_config.night_at_start ? 1 : 0;
  13596. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  13597. int32 day_duration = battle_config.day_duration;
  13598. int32 night_duration = battle_config.night_duration;
  13599. // add night/day timer [Yor]
  13600. add_timer_func_list(map_day_timer, "map_day_timer");
  13601. add_timer_func_list(map_night_timer, "map_night_timer");
  13602. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  13603. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  13604. }
  13605. do_init_pc_groups();
  13606. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  13607. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13608. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13609. ers_chunk_size(pc_sc_display_ers, 150);
  13610. ers_chunk_size(num_reg_ers, 300);
  13611. ers_chunk_size(str_reg_ers, 50);
  13612. }