pc.cpp 470 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760
  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 <map>
  5. #include <math.h>
  6. #include <stdlib.h>
  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. int pc_split_atoui(char* str, unsigned int* val, char sep, int 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. int day_timer_tid = INVALID_TIMER;
  72. int night_timer_tid = INVALID_TIMER;
  73. struct eri *pc_sc_display_ers = NULL;
  74. struct eri *num_reg_ers;
  75. struct eri *str_reg_ers;
  76. int 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( int 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( int 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( int 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( int 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. unsigned int 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 = NULL; // 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. int pc_class2idx(int 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. int 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. int 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)) == NULL || 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, int 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. int i;
  639. if( (sd=(map_session_data *)map_id2sd(id)) == NULL || 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,int interval,int max)
  667. {
  668. int 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,int count,int 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 max: Max amount of soulballs
  737. */
  738. int pc_addsoulball(map_session_data *sd, int max)
  739. {
  740. nullpo_ret(sd);
  741. status_change *sc = status_get_sc(&sd->bl);
  742. if (sc == nullptr || sc->getSCE(SC_SOULENERGY) == nullptr) {
  743. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, 0, skill_get_time2(SP_SOULCOLLECT, 1));
  744. sd->soulball = 0;
  745. }
  746. max = min(max, MAX_SOUL_BALL);
  747. sd->soulball = cap_value(sd->soulball, 0, MAX_SOUL_BALL);
  748. if (sd->soulball && sd->soulball >= max)
  749. sd->soulball--;
  750. sd->soulball++;
  751. sc_start(&sd->bl, &sd->bl, SC_SOULENERGY, 100, sd->soulball, skill_get_time2(SP_SOULCOLLECT, 1));
  752. clif_soulball(sd);
  753. return 0;
  754. }
  755. /**
  756. * Removes number of soulball from player
  757. * @param sd: Player data
  758. * @param count: Amount to remove
  759. * @param type: true = doesn't give client effect
  760. */
  761. int pc_delsoulball(map_session_data *sd, int count, bool type)
  762. {
  763. nullpo_ret(sd);
  764. if (count <= 0)
  765. return 0;
  766. status_change *sc = status_get_sc(&sd->bl);
  767. if (sd->soulball <= 0 || sc == nullptr || sc->getSCE(SC_SOULENERGY) == nullptr) {
  768. sd->soulball = 0;
  769. }else{
  770. sd->soulball -= cap_value(count, 0, sd->soulball);
  771. if (sd->soulball == 0)
  772. status_change_end(&sd->bl, SC_SOULENERGY);
  773. else
  774. sc->getSCE(SC_SOULENERGY)->val1 = sd->soulball;
  775. }
  776. if (!type)
  777. clif_soulball(sd);
  778. return 0;
  779. }
  780. /**
  781. * Adds servantballs to a player
  782. * @param sd: Player data
  783. * @param amount: Amount to add
  784. */
  785. void pc_addservantball( map_session_data& sd, int count ){
  786. sd.servantball = cap_value( sd.servantball + count, 0, MAX_SERVANTBALL );
  787. clif_servantball( sd );
  788. }
  789. /**
  790. * Removes number of servantballs from player
  791. * @param sd: Player data
  792. * @param count: Amount to remove
  793. */
  794. void pc_delservantball( map_session_data& sd, int count ){
  795. sd.servantball = cap_value( sd.servantball - count, 0, MAX_SERVANTBALL );
  796. clif_servantball( sd );
  797. }
  798. /**
  799. * Adds abyssballs to a player
  800. * @param sd: Player data
  801. * @param amount: Amount to add
  802. */
  803. void pc_addabyssball( map_session_data& sd, int count ){
  804. sd.abyssball = cap_value( sd.abyssball + count, 0, MAX_ABYSSBALL );
  805. clif_abyssball( sd );
  806. }
  807. /**
  808. * Removes number of abyssballs from player
  809. * @param sd: Player data
  810. * @param count: Amount to remove
  811. */
  812. void pc_delabyssball( map_session_data& sd, int count ){
  813. sd.abyssball = cap_value( sd.abyssball - count, 0, MAX_ABYSSBALL );
  814. clif_abyssball( sd );
  815. }
  816. /**
  817. * Increases a player's fame points and displays a notice to him
  818. * @param sd Player
  819. * @param count Fame point
  820. * @return true: on success, false: on error
  821. */
  822. bool pc_addfame(map_session_data &sd, int count)
  823. {
  824. enum e_rank ranktype;
  825. sd.status.fame += count;
  826. if (sd.status.fame > MAX_FAME)
  827. sd.status.fame = MAX_FAME;
  828. switch(sd.class_&MAPID_UPPERMASK){
  829. case MAPID_BLACKSMITH: ranktype = RANK_BLACKSMITH; break;
  830. case MAPID_ALCHEMIST: ranktype = RANK_ALCHEMIST; break;
  831. case MAPID_TAEKWON: ranktype = RANK_TAEKWON; break;
  832. default:
  833. ShowWarning( "pc_addfame: Trying to add fame to class '%s'(%d).\n", job_name(sd.status.class_), sd.status.class_ );
  834. return false;
  835. }
  836. clif_update_rankingpoint(sd, ranktype, count);
  837. chrif_updatefamelist(sd, ranktype);
  838. return true;
  839. }
  840. /**
  841. * Check whether a player ID is in the fame rankers list of its job, returns his/her position if so, 0 else
  842. * @param sd
  843. * @param job Job use enum e_mapid
  844. * @return Rank
  845. */
  846. unsigned char pc_famerank(uint32 char_id, int job)
  847. {
  848. uint8 i;
  849. switch(job){
  850. case MAPID_BLACKSMITH: // Blacksmith
  851. for(i = 0; i < MAX_FAME_LIST; i++){
  852. if(smith_fame_list[i].id == char_id)
  853. return i + 1;
  854. }
  855. break;
  856. case MAPID_ALCHEMIST: // Alchemist
  857. for(i = 0; i < MAX_FAME_LIST; i++){
  858. if(chemist_fame_list[i].id == char_id)
  859. return i + 1;
  860. }
  861. break;
  862. case MAPID_TAEKWON: // Taekwon
  863. for(i = 0; i < MAX_FAME_LIST; i++){
  864. if(taekwon_fame_list[i].id == char_id)
  865. return i + 1;
  866. }
  867. break;
  868. }
  869. return 0;
  870. }
  871. /**
  872. * Restart player's HP & SP value
  873. * @param sd
  874. * @param type Restart type: 1 - Normal Resurection
  875. */
  876. void pc_setrestartvalue(map_session_data *sd, char type) {
  877. struct status_data *status, *b_status;
  878. nullpo_retv(sd);
  879. b_status = &sd->base_status;
  880. status = &sd->battle_status;
  881. if (type&1) { //Normal resurrection
  882. status->hp = 1; //Otherwise status_heal may fail if dead.
  883. status_heal(&sd->bl, b_status->hp, 0, 1);
  884. if( status->sp < b_status->sp )
  885. status_set_sp(&sd->bl, b_status->sp, 1);
  886. } else { //Just for saving on the char-server (with values as if respawned)
  887. sd->status.hp = b_status->hp;
  888. sd->status.sp = (status->sp < b_status->sp)?b_status->sp:status->sp;
  889. sd->status.ap = (status->ap < b_status->ap)?b_status->ap:status->ap;
  890. }
  891. }
  892. /*==========================================
  893. Rental System
  894. *------------------------------------------*/
  895. /**
  896. * Ends a rental and removes the item/effect
  897. * @param tid: Tick ID
  898. * @param tick: Timer
  899. * @param id: Timer ID
  900. * @param data: Data
  901. * @return false - failure, true - success
  902. */
  903. TIMER_FUNC(pc_inventory_rental_end){
  904. map_session_data *sd = map_id2sd(id);
  905. if( sd == NULL )
  906. return 0;
  907. if( tid != sd->rental_timer ) {
  908. ShowError("pc_inventory_rental_end: invalid timer id.\n");
  909. return 0;
  910. }
  911. pc_inventory_rentals(sd);
  912. return 1;
  913. }
  914. /**
  915. * Removes the rental timer from the player
  916. * @param sd: Player data
  917. */
  918. void pc_inventory_rental_clear(map_session_data *sd)
  919. {
  920. if( sd->rental_timer != INVALID_TIMER ) {
  921. delete_timer(sd->rental_timer, pc_inventory_rental_end);
  922. sd->rental_timer = INVALID_TIMER;
  923. }
  924. }
  925. /**
  926. * Check for items in the player's inventory that are rental type
  927. * @param sd: Player data
  928. */
  929. void pc_inventory_rentals(map_session_data *sd)
  930. {
  931. int i, c = 0;
  932. unsigned int next_tick = UINT_MAX;
  933. for( i = 0; i < MAX_INVENTORY; i++ ) { // Check for Rentals on Inventory
  934. if( sd->inventory.u.items_inventory[i].nameid == 0 )
  935. continue; // Nothing here
  936. if( sd->inventory.u.items_inventory[i].expire_time == 0 )
  937. continue;
  938. if( sd->inventory.u.items_inventory[i].expire_time <= time(NULL) ) {
  939. if (sd->inventory_data[i]->unequip_script)
  940. run_script(sd->inventory_data[i]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  941. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  942. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 0, 0, LOG_TYPE_OTHER);
  943. } else {
  944. unsigned int expire_tick = (unsigned int)(sd->inventory.u.items_inventory[i].expire_time - time(NULL));
  945. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, (int)expire_tick);
  946. next_tick = umin(expire_tick * 1000U, next_tick);
  947. c++;
  948. }
  949. }
  950. 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
  951. sd->rental_timer = add_timer(gettick() + umin(next_tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  952. else
  953. sd->rental_timer = INVALID_TIMER;
  954. }
  955. /**
  956. * Add a rental item to the player and adjusts the rental timer appropriately
  957. * @param sd: Player data
  958. * @param seconds: Rental time
  959. */
  960. void pc_inventory_rental_add(map_session_data *sd, unsigned int seconds)
  961. {
  962. t_tick tick = seconds * 1000;
  963. if( sd == NULL )
  964. return;
  965. if( sd->rental_timer != INVALID_TIMER ) {
  966. const struct TimerData * td;
  967. td = get_timer(sd->rental_timer);
  968. if( DIFF_TICK(td->tick, gettick()) > tick ) { // Update Timer as this one ends first than the current one
  969. pc_inventory_rental_clear(sd);
  970. sd->rental_timer = add_timer(gettick() + tick, pc_inventory_rental_end, sd->bl.id, 0);
  971. }
  972. } else
  973. sd->rental_timer = add_timer(gettick() + i64min(tick,3600000), pc_inventory_rental_end, sd->bl.id, 0);
  974. }
  975. /**
  976. * Check if the player can sell the current item
  977. * @param sd: map_session_data of the player
  978. * @param item: struct of the checking item
  979. * @param shoptype: NPC's sub type see enum npc_subtype
  980. * @return bool 'true' is sellable, 'false' otherwise
  981. */
  982. bool pc_can_sell_item(map_session_data *sd, struct item *item, enum npc_subtype shoptype) {
  983. if (sd == NULL || item == NULL)
  984. return false;
  985. if (!pc_can_give_items(sd))
  986. return false;
  987. if (item->equip > 0 || item->amount < 0)
  988. return false;
  989. if (battle_config.hide_fav_sell && item->favorite)
  990. return false; //Cannot sell favs (optional config)
  991. if (!battle_config.rental_transaction && item->expire_time)
  992. return false; // Cannot Sell Rental Items
  993. if( item->equipSwitch ){
  994. return false;
  995. }
  996. if (itemdb_ishatched_egg(item))
  997. return false;
  998. switch (shoptype) {
  999. case NPCTYPE_SHOP:
  1000. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND_SELLABLE && (
  1001. item->bound != BOUND_GUILD ||
  1002. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  1003. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1004. ))
  1005. return true;
  1006. break;
  1007. case NPCTYPE_ITEMSHOP:
  1008. if (item->bound && battle_config.allow_bound_sell&ISR_BOUND && (
  1009. item->bound != BOUND_GUILD ||
  1010. (sd->guild && sd->status.char_id == sd->guild->member[0].char_id) ||
  1011. (item->bound == BOUND_GUILD && !(battle_config.allow_bound_sell&ISR_BOUND_GUILDLEADER_ONLY))
  1012. ))
  1013. return true;
  1014. else if (!item->bound) {
  1015. struct item_data *itd = itemdb_search(item->nameid);
  1016. if (itd && itd->flag.trade_restriction.sell && battle_config.allow_bound_sell&ISR_SELLABLE)
  1017. return true;
  1018. }
  1019. break;
  1020. }
  1021. if (!itemdb_cansell(item, pc_get_group_level(sd)))
  1022. return false;
  1023. if (item->bound && !pc_can_give_bounded_items(sd))
  1024. return false; // Don't allow sale of bound items
  1025. return true;
  1026. }
  1027. /**
  1028. * Determines if player can give / drop / trade / vend items
  1029. */
  1030. bool pc_can_give_items(map_session_data *sd)
  1031. {
  1032. return pc_has_permission(sd, PC_PERM_TRADE);
  1033. }
  1034. /**
  1035. * Determines if player can give / drop / trade / vend bounded items
  1036. */
  1037. bool pc_can_give_bounded_items(map_session_data *sd)
  1038. {
  1039. return pc_has_permission(sd, PC_PERM_TRADE_BOUNDED);
  1040. }
  1041. /**
  1042. * Determine if an item in a player's inventory is tradeable based on several merits.
  1043. * Checks for item_trade, bound, and rental restrictions.
  1044. * @param sd: Player data
  1045. * @param index: Item inventory index
  1046. * @return True if the item can be traded or false otherwise
  1047. */
  1048. bool pc_can_trade_item(map_session_data *sd, int index) {
  1049. if (sd && index >= 0) {
  1050. return (sd->inventory.u.items_inventory[index].expire_time == 0 &&
  1051. (sd->inventory.u.items_inventory[index].bound == 0 || pc_can_give_bounded_items(sd)) &&
  1052. itemdb_cantrade(&sd->inventory.u.items_inventory[index], pc_get_group_level(sd), pc_get_group_level(sd))
  1053. );
  1054. }
  1055. return false;
  1056. }
  1057. /*==========================================
  1058. * Prepares character for saving.
  1059. * @param sd
  1060. *------------------------------------------*/
  1061. void pc_makesavestatus(map_session_data *sd) {
  1062. nullpo_retv(sd);
  1063. if(!battle_config.save_clothcolor)
  1064. sd->status.clothes_color = 0;
  1065. if(!battle_config.save_body_style)
  1066. sd->status.body = 0;
  1067. //Only copy the Cart/Peco/Falcon options, the rest are handled via
  1068. //status change load/saving. [Skotlex]
  1069. #ifdef NEW_CARTS
  1070. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1071. #else
  1072. sd->status.option = sd->sc.option&(OPTION_INVISIBLE|OPTION_CART|OPTION_FALCON|OPTION_RIDING|OPTION_DRAGON|OPTION_WUG|OPTION_WUGRIDER|OPTION_MADOGEAR);
  1073. #endif
  1074. if (sd->sc.getSCE(SC_JAILED)) { //When Jailed, do not move last point.
  1075. if(pc_isdead(sd)){
  1076. pc_setrestartvalue(sd, 0);
  1077. } else {
  1078. sd->status.hp = sd->battle_status.hp;
  1079. sd->status.sp = sd->battle_status.sp;
  1080. sd->status.ap = sd->battle_status.ap;
  1081. }
  1082. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1083. sd->status.last_point.x = sd->bl.x;
  1084. sd->status.last_point.y = sd->bl.y;
  1085. return;
  1086. }
  1087. if( pc_isdead( sd ) ){
  1088. pc_setrestartvalue( sd, 0 );
  1089. // Return to save point
  1090. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1091. sd->status.last_point.x = sd->status.save_point.x;
  1092. sd->status.last_point.y = sd->status.save_point.y;
  1093. }else{
  1094. sd->status.hp = sd->battle_status.hp;
  1095. sd->status.sp = sd->battle_status.sp;
  1096. sd->status.ap = sd->battle_status.ap;
  1097. struct map_data* mapdata = map_getmapdata( sd->bl.m );
  1098. // If saving is not allowed on the map, we return the player to the designated point
  1099. if( mapdata->flag[MF_NOSAVE] ){
  1100. // The map has a specific return point
  1101. if( mapdata->save.map ){
  1102. safestrncpy( sd->status.last_point.map, mapindex_id2name( mapdata->save.map ), sizeof( sd->status.last_point.map ) );
  1103. sd->status.last_point.x = mapdata->save.x;
  1104. sd->status.last_point.y = mapdata->save.y;
  1105. // Return the user to his save point
  1106. }else{
  1107. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1108. sd->status.last_point.x = sd->status.save_point.x;
  1109. sd->status.last_point.y = sd->status.save_point.y;
  1110. }
  1111. // If the user is on a instance map, we return him to his save point
  1112. }else if( mapdata->instance_id ){
  1113. // Return the user to his save point
  1114. safestrncpy( sd->status.last_point.map, sd->status.save_point.map, sizeof( sd->status.last_point.map ) );
  1115. sd->status.last_point.x = sd->status.save_point.x;
  1116. sd->status.last_point.y = sd->status.save_point.y;
  1117. }else{
  1118. // Save normally
  1119. mapindex_getmapname( mapindex_id2name( sd->mapindex ), sd->status.last_point.map );
  1120. sd->status.last_point.x = sd->bl.x;
  1121. sd->status.last_point.y = sd->bl.y;
  1122. }
  1123. }
  1124. }
  1125. /*==========================================
  1126. * Off init ? Connection?
  1127. *------------------------------------------*/
  1128. void pc_setnewpc(map_session_data *sd, uint32 account_id, uint32 char_id, int login_id1, t_tick client_tick, int sex, int fd) {
  1129. nullpo_retv(sd);
  1130. sd->bl.id = account_id;
  1131. sd->status.account_id = account_id;
  1132. sd->status.char_id = char_id;
  1133. sd->status.sex = sex;
  1134. sd->login_id1 = login_id1;
  1135. sd->login_id2 = 0; // at this point, we can not know the value :(
  1136. sd->client_tick = client_tick;
  1137. sd->state.active = 0; //to be set to 1 after player is fully authed and loaded.
  1138. sd->bl.type = BL_PC;
  1139. if(battle_config.prevent_logout_trigger&PLT_LOGIN)
  1140. sd->canlog_tick = gettick();
  1141. //Required to prevent homunculus copuing a base speed of 0.
  1142. sd->battle_status.speed = sd->base_status.speed = DEFAULT_WALK_SPEED;
  1143. }
  1144. /**
  1145. * Get equip point for an equip
  1146. * @param sd
  1147. * @param id
  1148. */
  1149. int pc_equippoint_sub(map_session_data *sd,struct item_data* id){
  1150. int ep = 0;
  1151. nullpo_ret(sd);
  1152. nullpo_ret(id);
  1153. if (!itemdb_isequip2(id))
  1154. return 0; //Not equippable by players.
  1155. ep = id->equip;
  1156. if(id->subtype == W_DAGGER || id->subtype == W_1HSWORD || id->subtype == W_1HAXE) {
  1157. 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]
  1158. if (ep == EQP_WEAPON)
  1159. return EQP_ARMS;
  1160. if (ep == EQP_SHADOW_WEAPON)
  1161. return EQP_SHADOW_ARMS;
  1162. }
  1163. }
  1164. return ep;
  1165. }
  1166. /**
  1167. * Get equip point for an equip
  1168. * @param sd
  1169. * @param n Equip index in inventory
  1170. */
  1171. int pc_equippoint(map_session_data *sd,int n){
  1172. nullpo_ret(sd);
  1173. return pc_equippoint_sub(sd,sd->inventory_data[n]);
  1174. }
  1175. /**
  1176. * Fill inventory_data with struct *item_data through inventory (fill with struct *item)
  1177. * @param sd : player session
  1178. * @return 0 sucess, 1:invalid sd
  1179. */
  1180. void pc_setinventorydata(map_session_data *sd)
  1181. {
  1182. uint8 i;
  1183. nullpo_retv(sd);
  1184. for(i = 0; i < MAX_INVENTORY; i++) {
  1185. t_itemid id = sd->inventory.u.items_inventory[i].nameid;
  1186. sd->inventory_data[i] = id?itemdb_search(id):NULL;
  1187. }
  1188. }
  1189. /**
  1190. * 'Calculates' weapon type
  1191. * @param sd : Player
  1192. */
  1193. void pc_calcweapontype(map_session_data *sd)
  1194. {
  1195. nullpo_retv(sd);
  1196. // single-hand
  1197. if(sd->weapontype2 == W_FIST) {
  1198. sd->status.weapon = sd->weapontype1;
  1199. return;
  1200. }
  1201. if(sd->weapontype1 == W_FIST) {
  1202. sd->status.weapon = sd->weapontype2;
  1203. return;
  1204. }
  1205. // dual-wield
  1206. sd->status.weapon = W_FIST;
  1207. switch (sd->weapontype1){
  1208. case W_DAGGER:
  1209. switch (sd->weapontype2) {
  1210. case W_DAGGER: sd->status.weapon = W_DOUBLE_DD; break;
  1211. case W_1HSWORD: sd->status.weapon = W_DOUBLE_DS; break;
  1212. case W_1HAXE: sd->status.weapon = W_DOUBLE_DA; break;
  1213. }
  1214. break;
  1215. case W_1HSWORD:
  1216. switch (sd->weapontype2) {
  1217. case W_DAGGER: sd->status.weapon = W_DOUBLE_DS; break;
  1218. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SS; break;
  1219. case W_1HAXE: sd->status.weapon = W_DOUBLE_SA; break;
  1220. }
  1221. break;
  1222. case W_1HAXE:
  1223. switch (sd->weapontype2) {
  1224. case W_DAGGER: sd->status.weapon = W_DOUBLE_DA; break;
  1225. case W_1HSWORD: sd->status.weapon = W_DOUBLE_SA; break;
  1226. case W_1HAXE: sd->status.weapon = W_DOUBLE_AA; break;
  1227. }
  1228. }
  1229. // unknown, default to right hand type
  1230. if (!sd->status.weapon)
  1231. sd->status.weapon = sd->weapontype1;
  1232. }
  1233. /**
  1234. * Set equip index
  1235. * @param sd : Player
  1236. */
  1237. void pc_setequipindex(map_session_data *sd)
  1238. {
  1239. uint16 i;
  1240. nullpo_retv(sd);
  1241. for (i = 0; i < EQI_MAX; i++){
  1242. sd->equip_index[i] = -1;
  1243. sd->equip_switch_index[i] = -1;
  1244. }
  1245. for (i = 0; i < MAX_INVENTORY; i++) {
  1246. if (sd->inventory.u.items_inventory[i].nameid == 0)
  1247. continue;
  1248. if (sd->inventory.u.items_inventory[i].equip) {
  1249. uint8 j;
  1250. for (j = 0; j < EQI_MAX; j++)
  1251. if (sd->inventory.u.items_inventory[i].equip & equip_bitmask[j])
  1252. sd->equip_index[j] = i;
  1253. if (sd->inventory.u.items_inventory[i].equip & EQP_HAND_R) {
  1254. if (sd->inventory_data[i])
  1255. sd->weapontype1 = sd->inventory_data[i]->subtype;
  1256. else
  1257. sd->weapontype1 = W_FIST;
  1258. }
  1259. if( sd->inventory.u.items_inventory[i].equip & EQP_HAND_L ) {
  1260. if( sd->inventory_data[i] && sd->inventory_data[i]->type == IT_WEAPON )
  1261. sd->weapontype2 = sd->inventory_data[i]->subtype;
  1262. else
  1263. sd->weapontype2 = W_FIST;
  1264. }
  1265. }
  1266. if (sd->inventory.u.items_inventory[i].equipSwitch) {
  1267. for (uint8 j = 0; j < EQI_MAX; j++) {
  1268. if (sd->inventory.u.items_inventory[i].equipSwitch & equip_bitmask[j]) {
  1269. sd->equip_switch_index[j] = i;
  1270. }
  1271. }
  1272. }
  1273. }
  1274. pc_calcweapontype(sd);
  1275. }
  1276. //static int pc_isAllowedCardOn(map_session_data *sd,int s,int eqindex,int flag)
  1277. //{
  1278. // int i;
  1279. // struct item *item = &sd->inventory.u.items_inventory[eqindex];
  1280. // std::shared_ptr<item_data> data;
  1281. //
  1282. // //Crafted/made/hatched items.
  1283. // if (itemdb_isspecial(item->card[0]))
  1284. // return 1;
  1285. //
  1286. // /* scan for enchant armor gems */
  1287. // if( item->card[MAX_SLOTS - 1] && s < MAX_SLOTS - 1 )
  1288. // s = MAX_SLOTS - 1;
  1289. //
  1290. // ARR_FIND( 0, s, i, item->card[i] && (data = item_db.find(item->card[i])) != NULL && data->flag.no_equip&flag );
  1291. // return( i < s ) ? 0 : 1;
  1292. //}
  1293. /**
  1294. * Check if an item is equiped by player
  1295. * (Check if the itemid is equiped then search if that match the index in inventory (should be))
  1296. * @param sd : player session
  1297. * @param nameid : itemid
  1298. * @return 1:yes, 0:no
  1299. */
  1300. bool pc_isequipped(map_session_data *sd, t_itemid nameid)
  1301. {
  1302. uint8 i;
  1303. for( i = 0; i < EQI_MAX; i++ )
  1304. {
  1305. short index = sd->equip_index[i], j;
  1306. if( index < 0 )
  1307. continue;
  1308. if( pc_is_same_equip_index((enum equip_index)i, sd->equip_index, index) )
  1309. continue;
  1310. if( !sd->inventory_data[index] )
  1311. continue;
  1312. if( sd->inventory_data[index]->nameid == nameid )
  1313. return true;
  1314. for( j = 0; j < MAX_SLOTS; j++ ){
  1315. if( sd->inventory.u.items_inventory[index].card[j] == nameid )
  1316. return true;
  1317. }
  1318. }
  1319. return false;
  1320. }
  1321. /**
  1322. * Check adoption rules
  1323. * @param p1_sd: Player 1
  1324. * @param p2_sd: Player 2
  1325. * @param b_sd: Player that will be adopted
  1326. * @return ADOPT_ALLOWED - Sent message to Baby to accept or deny
  1327. * ADOPT_ALREADY_ADOPTED - Already adopted
  1328. * ADOPT_MARRIED_AND_PARTY - Need to be married and in the same party
  1329. * ADOPT_EQUIP_RINGS - Need wedding rings equipped
  1330. * ADOPT_NOT_NOVICE - Adoptee is not a Novice
  1331. * ADOPT_CHARACTER_NOT_FOUND - Parent or Baby not found
  1332. * ADOPT_MORE_CHILDREN - Cannot adopt more than 1 Baby (client message)
  1333. * ADOPT_LEVEL_70 - Parents need to be level 70+ (client message)
  1334. * ADOPT_MARRIED - Cannot adopt a married person (client message)
  1335. */
  1336. enum adopt_responses pc_try_adopt(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1337. {
  1338. if( !p1_sd || !p2_sd || !b_sd )
  1339. return ADOPT_CHARACTER_NOT_FOUND;
  1340. if( b_sd->status.father || b_sd->status.mother || b_sd->adopt_invite )
  1341. return ADOPT_ALREADY_ADOPTED; // already adopted baby / in adopt request
  1342. if( !p1_sd->status.partner_id || !p1_sd->status.party_id || p1_sd->status.party_id != b_sd->status.party_id )
  1343. return ADOPT_MARRIED_AND_PARTY; // You need to be married and in party with baby to adopt
  1344. if( p1_sd->status.partner_id != p2_sd->status.char_id || p2_sd->status.partner_id != p1_sd->status.char_id )
  1345. return ADOPT_MARRIED_AND_PARTY; // Not married, wrong married
  1346. if( p2_sd->status.party_id != p1_sd->status.party_id )
  1347. return ADOPT_MARRIED_AND_PARTY; // Both parents need to be in the same party
  1348. // Parents need to have their ring equipped
  1349. if( !pc_isequipped(p1_sd, WEDDING_RING_M) && !pc_isequipped(p1_sd, WEDDING_RING_F) )
  1350. return ADOPT_EQUIP_RINGS;
  1351. if( !pc_isequipped(p2_sd, WEDDING_RING_M) && !pc_isequipped(p2_sd, WEDDING_RING_F) )
  1352. return ADOPT_EQUIP_RINGS;
  1353. // Already adopted a baby
  1354. if( p1_sd->status.child || p2_sd->status.child ) {
  1355. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MORE_CHILDREN);
  1356. return ADOPT_MORE_CHILDREN;
  1357. }
  1358. // Parents need at least lvl 70 to adopt
  1359. if( p1_sd->status.base_level < 70 || p2_sd->status.base_level < 70 ) {
  1360. clif_Adopt_reply(p1_sd, ADOPT_REPLY_LEVEL_70);
  1361. return ADOPT_LEVEL_70;
  1362. }
  1363. if( b_sd->status.partner_id ) {
  1364. clif_Adopt_reply(p1_sd, ADOPT_REPLY_MARRIED);
  1365. return ADOPT_MARRIED;
  1366. }
  1367. 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 ) )
  1368. return ADOPT_NOT_NOVICE;
  1369. return ADOPT_ALLOWED;
  1370. }
  1371. /*==========================================
  1372. * Adoption Process
  1373. *------------------------------------------*/
  1374. bool pc_adoption(map_session_data *p1_sd, map_session_data *p2_sd, map_session_data *b_sd)
  1375. {
  1376. int job, joblevel;
  1377. t_exp jobexp;
  1378. if( pc_try_adopt(p1_sd, p2_sd, b_sd) != ADOPT_ALLOWED )
  1379. return false;
  1380. // Preserve current job levels and progress
  1381. joblevel = b_sd->status.job_level;
  1382. jobexp = b_sd->status.job_exp;
  1383. job = pc_mapid2jobid(b_sd->class_|JOBL_BABY, b_sd->status.sex);
  1384. if( job != -1 && pc_jobchange(b_sd, job, 0) )
  1385. { // Success, proceed to configure parents and baby skills
  1386. p1_sd->status.child = b_sd->status.char_id;
  1387. p2_sd->status.child = b_sd->status.char_id;
  1388. b_sd->status.father = p1_sd->status.char_id;
  1389. b_sd->status.mother = p2_sd->status.char_id;
  1390. // Restore progress
  1391. b_sd->status.job_level = joblevel;
  1392. clif_updatestatus(b_sd, SP_JOBLEVEL);
  1393. b_sd->status.job_exp = jobexp;
  1394. clif_updatestatus(b_sd, SP_JOBEXP);
  1395. // Baby Skills
  1396. pc_skill(b_sd, WE_BABY, 1, ADDSKILL_PERMANENT);
  1397. pc_skill(b_sd, WE_CALLPARENT, 1, ADDSKILL_PERMANENT);
  1398. pc_skill(b_sd, WE_CHEERUP, 1, ADDSKILL_PERMANENT);
  1399. // Parents Skills
  1400. pc_skill(p1_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1401. pc_skill(p2_sd, WE_CALLBABY, 1, ADDSKILL_PERMANENT);
  1402. chrif_save(p1_sd, CSAVE_NORMAL);
  1403. chrif_save(p2_sd, CSAVE_NORMAL);
  1404. chrif_save(b_sd, CSAVE_NORMAL);
  1405. achievement_update_objective(b_sd, AG_BABY, 1, 1);
  1406. achievement_update_objective(p1_sd, AG_BABY, 1, 2);
  1407. achievement_update_objective(p2_sd, AG_BABY, 1, 2);
  1408. return true;
  1409. }
  1410. return false; // Job Change Fail
  1411. }
  1412. static bool pc_job_can_use_item( map_session_data* sd, struct item_data* item ){
  1413. nullpo_retr( false, sd );
  1414. nullpo_retr( false, item );
  1415. // Calculate the required bit to check
  1416. uint64 job = 1ULL << ( sd->class_ & MAPID_BASEMASK );
  1417. size_t index;
  1418. // 2-1
  1419. if( ( sd->class_ & JOBL_2_1 ) != 0 ){
  1420. index = 1;
  1421. // 2-2
  1422. }else if( ( sd->class_ & JOBL_2_2 ) != 0 ){
  1423. index = 2;
  1424. // Basejob
  1425. }else{
  1426. index = 0;
  1427. }
  1428. return ( item->class_base[index] & job ) != 0;
  1429. }
  1430. /*==========================================
  1431. * Check if player can use/equip selected item. Used by pc_isUseitem and pc_isequip
  1432. Returns:
  1433. false : Cannot use/equip
  1434. true : Can use/equip
  1435. * Credits:
  1436. [Inkfish] for first idea
  1437. [Haru] for third-classes extension
  1438. [Cydh] finishing :D
  1439. *------------------------------------------*/
  1440. static bool pc_isItemClass (map_session_data *sd, struct item_data* item) {
  1441. while (1) {
  1442. 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)
  1443. break;
  1444. #ifndef RENEWAL
  1445. //allow third classes to use trans. class items
  1446. if (item->class_upper&ITEMJ_UPPER && sd->class_&(JOBL_UPPER|JOBL_THIRD)) //trans. classes
  1447. break;
  1448. //third-baby classes can use same item too
  1449. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY) //baby classes
  1450. break;
  1451. //don't need to decide specific rules for third-classes?
  1452. //items for third classes can be used for all third classes
  1453. if (item->class_upper&(ITEMJ_THIRD|ITEMJ_THIRD_UPPER|ITEMJ_THIRD_BABY) && sd->class_&JOBL_THIRD)
  1454. break;
  1455. #else
  1456. //trans. classes (exl. third-trans.)
  1457. if (item->class_upper&ITEMJ_UPPER && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_THIRD))
  1458. break;
  1459. //baby classes (exl. third-baby)
  1460. if (item->class_upper&ITEMJ_BABY && sd->class_&JOBL_BABY && !(sd->class_&JOBL_THIRD))
  1461. break;
  1462. //third classes (exl. third-trans. and baby-third and fourth)
  1463. if (item->class_upper&ITEMJ_THIRD && sd->class_&JOBL_THIRD && !(sd->class_&(JOBL_UPPER|JOBL_BABY)) && !(sd->class_&JOBL_FOURTH))
  1464. break;
  1465. //trans-third classes (exl. fourth)
  1466. if (item->class_upper&ITEMJ_THIRD_UPPER && sd->class_&JOBL_THIRD && sd->class_&JOBL_UPPER && !(sd->class_&JOBL_FOURTH))
  1467. break;
  1468. //third-baby classes (exl. fourth)
  1469. if (item->class_upper&ITEMJ_THIRD_BABY && sd->class_&JOBL_THIRD && sd->class_&JOBL_BABY && !(sd->class_&JOBL_FOURTH))
  1470. break;
  1471. //fourth classes
  1472. if (item->class_upper&ITEMJ_FOURTH && sd->class_&JOBL_FOURTH)
  1473. break;
  1474. #endif
  1475. return false;
  1476. }
  1477. return true;
  1478. }
  1479. /*=================================================
  1480. * Checks if the player can equip the item at index n in inventory.
  1481. * @param sd
  1482. * @param n Item index in inventory
  1483. * @return ITEM_EQUIP_ACK_OK(0) if can be equipped, or ITEM_EQUIP_ACK_FAIL(1)/ITEM_EQUIP_ACK_FAILLEVEL(2) if can't
  1484. *------------------------------------------------*/
  1485. uint8 pc_isequip(map_session_data *sd,int n)
  1486. {
  1487. struct item_data *item;
  1488. nullpo_retr(ITEM_EQUIP_ACK_FAIL, sd);
  1489. item = sd->inventory_data[n];
  1490. if(pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT))
  1491. return ITEM_EQUIP_ACK_OK;
  1492. if(item == NULL)
  1493. return ITEM_EQUIP_ACK_FAIL;
  1494. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  1495. return ITEM_EQUIP_ACK_FAILLEVEL;
  1496. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  1497. return ITEM_EQUIP_ACK_FAILLEVEL;
  1498. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  1499. return ITEM_EQUIP_ACK_FAIL;
  1500. //fail to equip if item is restricted
  1501. if (!battle_config.allow_equip_restricted_item && itemdb_isNoEquip(item, sd->bl.m))
  1502. return ITEM_EQUIP_ACK_FAIL;
  1503. if (item->equip&EQP_AMMO) {
  1504. switch (item->subtype) {
  1505. case AMMO_ARROW:
  1506. if (battle_config.ammo_check_weapon && sd->status.weapon != W_BOW && sd->status.weapon != W_MUSICAL && sd->status.weapon != W_WHIP) {
  1507. clif_msg(sd, ITEM_NEED_BOW);
  1508. return ITEM_EQUIP_ACK_FAIL;
  1509. }
  1510. break;
  1511. case AMMO_DAGGER:
  1512. if (!pc_checkskill(sd, AS_VENOMKNIFE))
  1513. return ITEM_EQUIP_ACK_FAIL;
  1514. break;
  1515. case AMMO_BULLET:
  1516. case AMMO_SHELL:
  1517. 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
  1518. #ifdef RENEWAL
  1519. && sd->status.weapon != W_GRENADE
  1520. #endif
  1521. ) {
  1522. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1523. return ITEM_EQUIP_ACK_FAIL;
  1524. }
  1525. break;
  1526. #ifndef RENEWAL
  1527. case AMMO_GRENADE:
  1528. if (battle_config.ammo_check_weapon && sd->status.weapon != W_GRENADE) {
  1529. clif_msg(sd, ITEM_BULLET_EQUIP_FAIL);
  1530. return ITEM_EQUIP_ACK_FAIL;
  1531. }
  1532. break;
  1533. #endif
  1534. case AMMO_CANNONBALL:
  1535. if (!pc_ismadogear(sd) && (sd->status.class_ == JOB_MECHANIC_T || sd->status.class_ == JOB_MECHANIC)) {
  1536. clif_msg(sd, ITEM_NEED_MADOGEAR); // Item can only be used when Mado Gear is mounted.
  1537. return ITEM_EQUIP_ACK_FAIL;
  1538. }
  1539. if (sd->state.active && !pc_iscarton(sd) && //Check if sc data is already loaded
  1540. (sd->status.class_ == JOB_GENETIC_T || sd->status.class_ == JOB_GENETIC)) {
  1541. clif_msg(sd, ITEM_NEED_CART); // Only available when cart is mounted.
  1542. return ITEM_EQUIP_ACK_FAIL;
  1543. }
  1544. break;
  1545. }
  1546. }
  1547. if (sd->sc.count) {
  1548. if(item->equip & EQP_ARMS && item->type == IT_WEAPON && sd->sc.getSCE(SC_STRIPWEAPON)) // Also works with left-hand weapons [DracoRPG]
  1549. return ITEM_EQUIP_ACK_FAIL;
  1550. if(item->equip & EQP_SHIELD && item->type == IT_ARMOR && sd->sc.getSCE(SC_STRIPSHIELD))
  1551. return ITEM_EQUIP_ACK_FAIL;
  1552. if(item->equip & EQP_ARMOR && sd->sc.getSCE(SC_STRIPARMOR))
  1553. return ITEM_EQUIP_ACK_FAIL;
  1554. if(item->equip & EQP_HEAD_TOP && sd->sc.getSCE(SC_STRIPHELM))
  1555. return ITEM_EQUIP_ACK_FAIL;
  1556. if(item->equip & EQP_ACC && sd->sc.getSCE(SC__STRIPACCESSORY))
  1557. return ITEM_EQUIP_ACK_FAIL;
  1558. if (item->equip & EQP_ARMS && sd->sc.getSCE(SC__WEAKNESS))
  1559. return ITEM_EQUIP_ACK_FAIL;
  1560. if (item->equip & EQP_SHADOW_GEAR && sd->sc.getSCE(SC_SHADOW_STRIP))
  1561. return ITEM_EQUIP_ACK_FAIL;
  1562. if(item->equip && (sd->sc.getSCE(SC_KYOUGAKU) || sd->sc.getSCE(SC_SUHIDE)))
  1563. return ITEM_EQUIP_ACK_FAIL;
  1564. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_SUPERNOVICE) {
  1565. //Spirit of Super Novice equip bonuses. [Skotlex]
  1566. if (sd->status.base_level > 90 && item->equip & EQP_HELM)
  1567. return ITEM_EQUIP_ACK_OK; //Can equip all helms
  1568. if (sd->status.base_level > 96 && item->equip & EQP_ARMS && item->type == IT_WEAPON && item->weapon_level == 4)
  1569. switch(item->subtype) { //In weapons, the look determines type of weapon.
  1570. case W_DAGGER: //All level 4 - Daggers
  1571. case W_1HSWORD: //All level 4 - 1H Swords
  1572. case W_1HAXE: //All level 4 - 1H Axes
  1573. case W_MACE: //All level 4 - 1H Maces
  1574. case W_STAFF: //All level 4 - 1H Staves
  1575. case W_2HSTAFF: //All level 4 - 2H Staves
  1576. return ITEM_EQUIP_ACK_OK;
  1577. }
  1578. }
  1579. }
  1580. //Not equipable by class. [Skotlex]
  1581. if (!pc_job_can_use_item(sd,item))
  1582. return ITEM_EQUIP_ACK_FAIL;
  1583. if (!pc_isItemClass(sd, item))
  1584. return ITEM_EQUIP_ACK_FAIL;
  1585. return ITEM_EQUIP_ACK_OK;
  1586. }
  1587. /*==========================================
  1588. * No problem with the session id
  1589. * set the status that has been sent from char server
  1590. *------------------------------------------*/
  1591. bool pc_authok(map_session_data *sd, uint32 login_id2, time_t expiration_time, int group_id, struct mmo_charstatus *st, bool changing_mapservers)
  1592. {
  1593. t_tick tick = gettick();
  1594. uint32 ip = session[sd->fd]->client_addr;
  1595. sd->login_id2 = login_id2;
  1596. sd->group_id = group_id;
  1597. /* load user permissions */
  1598. pc_group_pc_load(sd);
  1599. memcpy(&sd->status, st, sizeof(*st));
  1600. if (st->sex != sd->status.sex) {
  1601. clif_authfail_fd(sd->fd, 0);
  1602. return false;
  1603. }
  1604. //Set the map-server used job id. [Skotlex]
  1605. uint64 class_ = pc_jobid2mapid(sd->status.class_);
  1606. if (class_ == -1 || !job_db.exists(sd->status.class_)) { //Invalid class?
  1607. 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);
  1608. sd->status.class_ = JOB_NOVICE;
  1609. sd->class_ = MAPID_NOVICE;
  1610. } else
  1611. sd->class_ = class_;
  1612. // Checks and fixes to character status data, that are required
  1613. // in case of configuration change or stuff, which cannot be
  1614. // checked on char-server.
  1615. sd->status.hair = cap_value(sd->status.hair,MIN_HAIR_STYLE,MAX_HAIR_STYLE);
  1616. sd->status.hair_color = cap_value(sd->status.hair_color,MIN_HAIR_COLOR,MAX_HAIR_COLOR);
  1617. sd->status.clothes_color = cap_value(sd->status.clothes_color,MIN_CLOTH_COLOR,MAX_CLOTH_COLOR);
  1618. sd->status.body = cap_value(sd->status.body,MIN_BODY_STYLE,MAX_BODY_STYLE);
  1619. //Initializations to null/0 unneeded since map_session_data was filled with 0 upon allocation.
  1620. sd->state.connect_new = 1;
  1621. sd->followtimer = INVALID_TIMER; // [MouseJstr]
  1622. sd->invincible_timer = INVALID_TIMER;
  1623. sd->npc_timer_id = INVALID_TIMER;
  1624. sd->pvp_timer = INVALID_TIMER;
  1625. sd->expiration_tid = INVALID_TIMER;
  1626. sd->autotrade_tid = INVALID_TIMER;
  1627. sd->respawn_tid = INVALID_TIMER;
  1628. sd->tid_queue_active = INVALID_TIMER;
  1629. sd->macro_detect.timer = INVALID_TIMER;
  1630. sd->skill_keep_using.tid = INVALID_TIMER;
  1631. sd->skill_keep_using.skill_id = 0;
  1632. sd->skill_keep_using.level = 0;
  1633. sd->skill_keep_using.target = 0;
  1634. #ifdef SECURE_NPCTIMEOUT
  1635. // Initialize to defaults/expected
  1636. sd->npc_idle_timer = INVALID_TIMER;
  1637. sd->npc_idle_tick = tick;
  1638. sd->npc_idle_type = NPCT_INPUT;
  1639. sd->state.ignoretimeout = false;
  1640. #endif
  1641. sd->canuseitem_tick = tick;
  1642. sd->canusecashfood_tick = tick;
  1643. sd->canequip_tick = tick;
  1644. sd->cantalk_tick = tick;
  1645. sd->canskill_tick = tick;
  1646. sd->cansendmail_tick = tick;
  1647. sd->idletime = last_tick;
  1648. sd->regen.tick.hp = tick;
  1649. sd->regen.tick.sp = tick;
  1650. for(int i = 0; i < MAX_SPIRITBALL; i++)
  1651. sd->spirit_timer[i] = INVALID_TIMER;
  1652. if (battle_config.item_auto_get)
  1653. sd->state.autoloot = 10000;
  1654. if (battle_config.disp_experience)
  1655. sd->state.showexp = 1;
  1656. if (battle_config.disp_zeny)
  1657. sd->state.showzeny = 1;
  1658. #ifdef VIP_ENABLE
  1659. if (!battle_config.vip_disp_rate)
  1660. sd->vip.disableshowrate = 1;
  1661. #endif
  1662. if (!(battle_config.display_skill_fail&2))
  1663. sd->state.showdelay = 1;
  1664. memset(&sd->inventory, 0, sizeof(struct s_storage));
  1665. memset(&sd->cart, 0, sizeof(struct s_storage));
  1666. memset(&sd->storage, 0, sizeof(struct s_storage));
  1667. memset(&sd->premiumStorage, 0, sizeof(struct s_storage));
  1668. memset(&sd->equip_index, -1, sizeof(sd->equip_index));
  1669. memset(&sd->equip_switch_index, -1, sizeof(sd->equip_switch_index));
  1670. if( pc_isinvisible(sd) && !pc_can_use_command( sd, "hide", COMMAND_ATCOMMAND ) ){
  1671. sd->status.option &= ~OPTION_INVISIBLE;
  1672. }
  1673. status_change_init(&sd->bl);
  1674. sd->sc.option = sd->status.option; //This is the actual option used in battle.
  1675. unit_dataset(&sd->bl);
  1676. sd->guild_x = -1;
  1677. sd->guild_y = -1;
  1678. sd->delayed_damage = 0;
  1679. // Event Timers
  1680. for( int i = 0; i < MAX_EVENTTIMER; i++ )
  1681. sd->eventtimer[i] = INVALID_TIMER;
  1682. // Rental Timer
  1683. sd->rental_timer = INVALID_TIMER;
  1684. for( int i = 0; i < 3; i++ )
  1685. sd->hate_mob[i] = -1;
  1686. sd->quest_log = NULL;
  1687. sd->num_quests = 0;
  1688. sd->avail_quests = 0;
  1689. sd->save_quest = false;
  1690. sd->count_rewarp = 0;
  1691. sd->mail.pending_weight = 0;
  1692. sd->mail.pending_zeny = 0;
  1693. sd->mail.pending_slots = 0;
  1694. sd->regs.vars = i64db_alloc(DB_OPT_BASE);
  1695. sd->regs.arrays = NULL;
  1696. sd->vars_dirty = false;
  1697. sd->vars_ok = false;
  1698. sd->vars_received = 0x0;
  1699. //warp player
  1700. 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 );
  1701. if( setpos_result != SETPOS_OK ){
  1702. ShowError( "Last_point_map %s not found (error code %d)\n", sd->status.last_point.map, setpos_result );
  1703. // try warping to a default map instead (church graveyard)
  1704. if (pc_setpos(sd, mapindex_name2id(MAP_PRONTERA), 273, 354, CLR_OUTSIGHT) != SETPOS_OK) {
  1705. // if we fail again
  1706. clif_authfail_fd(sd->fd, 0);
  1707. return false;
  1708. }
  1709. }
  1710. clif_inventory_expansion_info( sd );
  1711. clif_authok(sd);
  1712. //Prevent S. Novices from getting the no-death bonus just yet. [Skotlex]
  1713. sd->die_counter=-1;
  1714. //display login notice
  1715. ShowInfo("'" CL_WHITE "%s" CL_RESET "' logged in."
  1716. " (AID/CID: '" CL_WHITE "%d/%d" CL_RESET "',"
  1717. " IP: '" CL_WHITE "%d.%d.%d.%d" CL_RESET "',"
  1718. " Group '" CL_WHITE "%d" CL_RESET "').\n",
  1719. sd->status.name, sd->status.account_id, sd->status.char_id,
  1720. CONVIP(ip), sd->group_id);
  1721. // Send friends list
  1722. clif_friendslist_send( *sd );
  1723. if( !changing_mapservers ) {
  1724. if (battle_config.display_version == 1)
  1725. pc_show_version(sd);
  1726. // Message of the Day [Valaris]
  1727. for(int i=0; i < MOTD_LINE_SIZE && motd_text[i][0]; i++) {
  1728. if (battle_config.motd_type)
  1729. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], motd_text[i], false, SELF);
  1730. else
  1731. clif_displaymessage(sd->fd, motd_text[i]);
  1732. }
  1733. if (expiration_time != 0)
  1734. sd->expiration_time = expiration_time;
  1735. /**
  1736. * Fixes login-without-aura glitch (the screen won't blink at this point, don't worry :P)
  1737. **/
  1738. clif_changemap(sd,sd->bl.m,sd->bl.x,sd->bl.y);
  1739. }
  1740. pc_validate_skill(sd);
  1741. /* [Ind] */
  1742. sd->sc_display = NULL;
  1743. sd->sc_display_count = 0;
  1744. // Player has not yet received the CashShop list
  1745. sd->status.cashshop_sent = false;
  1746. sd->last_addeditem_index = -1;
  1747. sd->bonus_script.head = NULL;
  1748. sd->bonus_script.count = 0;
  1749. // Initialize BG queue
  1750. sd->bg_queue_id = 0;
  1751. #if PACKETVER_MAIN_NUM >= 20150507 || PACKETVER_RE_NUM >= 20150429 || defined(PACKETVER_ZERO)
  1752. sd->hatEffects = {};
  1753. #endif
  1754. sd->catch_target_class = PET_CATCH_FAIL;
  1755. // Check EXP overflow, since in previous revision EXP on Max Level can be more than 'official' Max EXP
  1756. if (pc_is_maxbaselv(sd) && sd->status.base_exp > MAX_LEVEL_BASE_EXP) {
  1757. sd->status.base_exp = MAX_LEVEL_BASE_EXP;
  1758. clif_updatestatus(sd, SP_BASEEXP);
  1759. }
  1760. if (pc_is_maxjoblv(sd) && sd->status.job_exp > MAX_LEVEL_JOB_EXP) {
  1761. sd->status.job_exp = MAX_LEVEL_JOB_EXP;
  1762. clif_updatestatus(sd, SP_JOBEXP);
  1763. }
  1764. // Request all registries (auth is considered completed whence they arrive)
  1765. intif_request_registry(sd,7);
  1766. return true;
  1767. }
  1768. /*==========================================
  1769. * Closes a connection because it failed to be authenticated from the char server.
  1770. *------------------------------------------*/
  1771. void pc_authfail(map_session_data *sd)
  1772. {
  1773. clif_authfail_fd(sd->fd, 0);
  1774. return;
  1775. }
  1776. /**
  1777. * Player register a bl as hatred
  1778. * @param sd : player session
  1779. * @param pos : hate position [0;2]
  1780. * @param bl : target bl
  1781. * @return false:failed, true:success
  1782. */
  1783. bool pc_set_hate_mob(map_session_data *sd, int pos, struct block_list *bl)
  1784. {
  1785. int class_;
  1786. if (!sd || !bl || pos < 0 || pos > 2)
  1787. return false;
  1788. if (sd->hate_mob[pos] != -1)
  1789. { //Can't change hate targets.
  1790. clif_hate_info(sd, pos, sd->hate_mob[pos], 0); //Display current
  1791. return false;
  1792. }
  1793. class_ = status_get_class(bl);
  1794. if (!pcdb_checkid(class_)) {
  1795. unsigned int max_hp = status_get_max_hp(bl);
  1796. if ((pos == 1 && max_hp < 6000) || (pos == 2 && max_hp < 20000))
  1797. return false;
  1798. if (pos != status_get_size(bl))
  1799. return false; //Wrong size
  1800. }
  1801. sd->hate_mob[pos] = class_;
  1802. pc_setglobalreg(sd, add_str(sg_info[pos].hate_var), class_+1);
  1803. clif_hate_info(sd, pos, class_, 1);
  1804. return true;
  1805. }
  1806. /*==========================================
  1807. * Invoked once after the char/account/account2 registry variables are received. [Skotlex]
  1808. * We didn't receive item information at this point so DO NOT attempt to do item operations here.
  1809. * See intif_parse_StorageReceived() for item operations [lighta]
  1810. *------------------------------------------*/
  1811. void pc_reg_received(map_session_data *sd)
  1812. {
  1813. uint8 i;
  1814. sd->vars_ok = true;
  1815. sd->change_level_2nd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE2ND_VAR)));
  1816. sd->change_level_3rd = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE3RD_VAR)));
  1817. sd->change_level_4th = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(JOBCHANGE4TH_VAR)));
  1818. sd->die_counter = static_cast<int>(pc_readglobalreg(sd, add_str(PCDIECOUNTER_VAR)));
  1819. sd->langtype = static_cast<int>(pc_readaccountreg(sd, add_str(LANGTYPE_VAR)));
  1820. if (msg_checklangtype(sd->langtype,true) < 0)
  1821. sd->langtype = 0; //invalid langtype reset to default
  1822. // Cash shop
  1823. sd->cashPoints = static_cast<int>(pc_readaccountreg(sd, add_str(CASHPOINT_VAR)));
  1824. sd->kafraPoints = static_cast<int>(pc_readaccountreg(sd, add_str(KAFRAPOINT_VAR)));
  1825. // Cooking Exp
  1826. sd->cook_mastery = static_cast<short>(pc_readglobalreg(sd, add_str(COOKMASTERY_VAR)));
  1827. if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON )
  1828. { // Better check for class rather than skill to prevent "skill resets" from unsetting this
  1829. sd->mission_mobid = static_cast<short>(pc_readglobalreg(sd, add_str(TKMISSIONID_VAR)));
  1830. sd->mission_count = static_cast<unsigned char>(pc_readglobalreg(sd, add_str(TKMISSIONCOUNT_VAR)));
  1831. }
  1832. if (battle_config.feature_banking)
  1833. sd->bank_vault = static_cast<int>(pc_readreg2(sd, BANK_VAULT_VAR));
  1834. if (battle_config.feature_roulette) {
  1835. sd->roulette_point.bronze = static_cast<int>(pc_readreg2(sd, ROULETTE_BRONZE_VAR));
  1836. sd->roulette_point.silver = static_cast<int>(pc_readreg2(sd, ROULETTE_SILVER_VAR));
  1837. sd->roulette_point.gold = static_cast<int>(pc_readreg2(sd, ROULETTE_GOLD_VAR));
  1838. }
  1839. sd->roulette.prizeIdx = -1;
  1840. //SG map and mob read [Komurka]
  1841. for(i=0;i<MAX_PC_FEELHATE;i++) { //for now - someone need to make reading from txt/sql
  1842. uint16 j;
  1843. if ((j = static_cast<uint16>(pc_readglobalreg(sd, add_str(sg_info[i].feel_var)))) != 0) {
  1844. sd->feel_map[i].index = j;
  1845. sd->feel_map[i].m = map_mapindex2mapid(j);
  1846. } else {
  1847. sd->feel_map[i].index = 0;
  1848. sd->feel_map[i].m = -1;
  1849. }
  1850. sd->hate_mob[i] = static_cast<short>(pc_readglobalreg(sd, add_str(sg_info[i].hate_var)))-1;
  1851. }
  1852. if ((i = pc_checkskill(sd,RG_PLAGIARISM)) > 0) {
  1853. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM)));
  1854. sd->cloneskill_idx = skill_get_index(skid);
  1855. if (sd->cloneskill_idx > 0) {
  1856. sd->status.skill[sd->cloneskill_idx].id = skid;
  1857. sd->status.skill[sd->cloneskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_PLAGIARISM_LV)));
  1858. if (sd->status.skill[sd->cloneskill_idx].lv > i)
  1859. sd->status.skill[sd->cloneskill_idx].lv = i;
  1860. sd->status.skill[sd->cloneskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1861. }
  1862. }
  1863. if ((i = pc_checkskill(sd,SC_REPRODUCE)) > 0) {
  1864. unsigned short skid = static_cast<unsigned short>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE)));
  1865. sd->reproduceskill_idx = skill_get_index(skid);
  1866. if (sd->reproduceskill_idx > 0) {
  1867. sd->status.skill[sd->reproduceskill_idx].id = skid;
  1868. sd->status.skill[sd->reproduceskill_idx].lv = static_cast<uint8>(pc_readglobalreg(sd, add_str(SKILL_VAR_REPRODUCE_LV)));
  1869. if (i < sd->status.skill[sd->reproduceskill_idx].lv)
  1870. sd->status.skill[sd->reproduceskill_idx].lv = i;
  1871. sd->status.skill[sd->reproduceskill_idx].flag = SKILL_FLAG_PLAGIARIZED;
  1872. }
  1873. }
  1874. //Weird... maybe registries were reloaded?
  1875. if (sd->state.active)
  1876. return;
  1877. sd->state.active = 1;
  1878. sd->state.pc_loaded = false; // Ensure inventory data and status data is loaded before we calculate player stats
  1879. intif_storage_request(sd,TABLE_STORAGE, 0, STOR_MODE_ALL); // Request storage data
  1880. intif_storage_request(sd,TABLE_CART, 0, STOR_MODE_ALL); // Request cart data
  1881. intif_storage_request(sd,TABLE_INVENTORY, 0, STOR_MODE_ALL); // Request inventory data
  1882. // Restore IM_CHAR instance to the player
  1883. for (const auto &instance : instances) {
  1884. if (instance.second->mode == IM_CHAR && instance.second->owner_id == sd->status.char_id) {
  1885. sd->instance_id = instance.first;
  1886. break;
  1887. }
  1888. }
  1889. if (sd->status.party_id > 0)
  1890. party_member_joined(sd);
  1891. if (sd->status.guild_id > 0)
  1892. guild_member_joined(sd);
  1893. if (sd->status.clan_id > 0)
  1894. clan_member_joined(sd);
  1895. #if !( PACKETVER_MAIN_NUM >= 20190403 || PACKETVER_RE_NUM >= 20190320 || PACKETVER_ZERO_NUM >= 20190410 )
  1896. // 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
  1897. clif_instance_info( *sd );
  1898. #endif
  1899. // pet
  1900. if (sd->status.pet_id > 0)
  1901. intif_request_petdata(sd->status.account_id, sd->status.char_id, sd->status.pet_id);
  1902. // Homunculus [albator]
  1903. if( sd->status.hom_id > 0 )
  1904. intif_homunculus_requestload(sd->status.account_id, sd->status.hom_id);
  1905. if( sd->status.mer_id > 0 )
  1906. intif_mercenary_request(sd->status.mer_id, sd->status.char_id);
  1907. if( sd->status.ele_id > 0 )
  1908. intif_elemental_request(sd->status.ele_id, sd->status.char_id);
  1909. map_addiddb(&sd->bl);
  1910. map_delnickdb(sd->status.char_id, sd->status.name);
  1911. if (!chrif_auth_finished(sd))
  1912. ShowError("pc_reg_received: Failed to properly remove player %d:%d from logging db!\n", sd->status.account_id, sd->status.char_id);
  1913. chrif_skillcooldown_request(sd->status.account_id, sd->status.char_id);
  1914. chrif_bsdata_request(sd->status.char_id);
  1915. #ifdef VIP_ENABLE
  1916. sd->vip.time = 0;
  1917. sd->vip.enabled = 0;
  1918. chrif_req_login_operation(sd->status.account_id, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1|8, 0); // request VIP information
  1919. #endif
  1920. intif_Mail_requestinbox(sd->status.char_id, 0, MAIL_INBOX_NORMAL); // MAIL SYSTEM - Request Mail Inbox
  1921. intif_request_questlog(sd);
  1922. if (battle_config.feature_achievement) {
  1923. sd->achievement_data.total_score = 0;
  1924. sd->achievement_data.level = 0;
  1925. sd->achievement_data.save = false;
  1926. sd->achievement_data.count = 0;
  1927. sd->achievement_data.incompleteCount = 0;
  1928. sd->achievement_data.achievements = NULL;
  1929. intif_request_achievements(sd->status.char_id);
  1930. }
  1931. if (sd->state.connect_new == 0 && sd->fd) { //Character already loaded map! Gotta trigger LoadEndAck manually.
  1932. sd->state.connect_new = 1;
  1933. clif_parse_LoadEndAck(sd->fd, sd);
  1934. if( pc_isinvisible( sd ) ){
  1935. sd->vd.class_ = JT_INVISIBLE;
  1936. clif_displaymessage( sd->fd, msg_txt( sd, 11 ) ); // Invisible: On
  1937. // decrement the number of pvp players on the map
  1938. map_getmapdata( sd->bl.m )->users_pvp--;
  1939. if( map_getmapflag( sd->bl.m, MF_PVP ) && !map_getmapflag( sd->bl.m, MF_PVP_NOCALCRANK ) && sd->pvp_timer != INVALID_TIMER ){
  1940. // unregister the player for ranking
  1941. delete_timer( sd->pvp_timer, pc_calc_pvprank_timer );
  1942. sd->pvp_timer = INVALID_TIMER;
  1943. }
  1944. clif_changeoption( &sd->bl );
  1945. }
  1946. }
  1947. channel_autojoin(sd);
  1948. }
  1949. static int pc_calc_skillpoint(map_session_data* sd)
  1950. {
  1951. uint16 i, skill_point = 0;
  1952. nullpo_ret(sd);
  1953. for(i = 1; i < MAX_SKILL; i++) {
  1954. if( sd->status.skill[i].id && sd->status.skill[i].lv > 0) {
  1955. std::shared_ptr<s_skill_db> skill = skill_db.find(sd->status.skill[i].id);
  1956. if ((!skill->inf2[INF2_ISQUEST] || battle_config.quest_skill_learn) &&
  1957. (!skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT]) //Do not count wedding/link skills. [Skotlex]
  1958. )
  1959. {
  1960. if(sd->status.skill[i].flag == SKILL_FLAG_PERMANENT)
  1961. skill_point += sd->status.skill[i].lv;
  1962. else if(sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0)
  1963. skill_point += (sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0);
  1964. }
  1965. }
  1966. }
  1967. return skill_point;
  1968. }
  1969. static bool pc_grant_allskills(map_session_data *sd, bool addlv) {
  1970. if (!sd || !pc_has_permission(sd, PC_PERM_ALL_SKILL))
  1971. return false;
  1972. /**
  1973. * Dummy skills must NOT be added here otherwise they'll be displayed in the,
  1974. * skill tree and since they have no icons they'll give resource errors
  1975. * Get ALL skills except npc/guild ones. [Skotlex]
  1976. * Don't add SG_DEVIL [Komurka] and MO_TRIPLEATTACK and RG_SNATCHER [ultramage]
  1977. **/
  1978. for (const auto &skill : skill_db) {
  1979. uint16 skill_id = skill.second->nameid;
  1980. if (skill_id == 0 || skill.second->inf2[INF2_ISNPC]|| skill.second->inf2[INF2_ISGUILD])
  1981. continue;
  1982. switch (skill_id) {
  1983. case SM_SELFPROVOKE:
  1984. case AB_DUPLELIGHT_MELEE:
  1985. case AB_DUPLELIGHT_MAGIC:
  1986. case WL_CHAINLIGHTNING_ATK:
  1987. case WL_TETRAVORTEX_FIRE:
  1988. case WL_TETRAVORTEX_WATER:
  1989. case WL_TETRAVORTEX_WIND:
  1990. case WL_TETRAVORTEX_GROUND:
  1991. case WL_SUMMON_ATK_FIRE:
  1992. case WL_SUMMON_ATK_WIND:
  1993. case WL_SUMMON_ATK_WATER:
  1994. case WL_SUMMON_ATK_GROUND:
  1995. case LG_OVERBRAND_BRANDISH:
  1996. case LG_OVERBRAND_PLUSATK:
  1997. case WM_SEVERE_RAINSTORM_MELEE:
  1998. case RL_R_TRIP_PLUSATK:
  1999. case SG_DEVIL:
  2000. case MO_TRIPLEATTACK:
  2001. case RG_SNATCHER:
  2002. continue;
  2003. default:
  2004. {
  2005. uint8 lv = (uint8)skill_get_max(skill_id);
  2006. if (lv > 0) {
  2007. uint16 idx = skill_get_index(skill_id);
  2008. sd->status.skill[idx].id = skill_id;
  2009. if (addlv)
  2010. sd->status.skill[idx].lv = lv;
  2011. }
  2012. }
  2013. break;
  2014. }
  2015. }
  2016. return true;
  2017. }
  2018. /*==========================================
  2019. * Calculation of skill level.
  2020. * @param sd
  2021. *------------------------------------------*/
  2022. void pc_calc_skilltree(map_session_data *sd)
  2023. {
  2024. nullpo_retv(sd);
  2025. uint64 job = pc_calc_skilltree_normalize_job(sd);
  2026. int class_ = pc_mapid2jobid(job, sd->status.sex);
  2027. if( class_ == -1 )
  2028. { //Unable to normalize job??
  2029. 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 );
  2030. return;
  2031. }
  2032. uint16 job_id = class_;
  2033. class_ = pc_class2idx(class_);
  2034. for (const auto &skill : skill_db) {
  2035. uint16 skill_id = skill.second->nameid;
  2036. uint16 idx = skill_get_index(skill_id);
  2037. if( sd->status.skill[idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[idx].flag != SKILL_FLAG_PERM_GRANTED ) //Don't touch these
  2038. sd->status.skill[idx].id = 0; //First clear skills.
  2039. /* permanent skills that must be re-checked */
  2040. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED ) {
  2041. if (skill_id == 0) {
  2042. sd->status.skill[idx].id = 0;
  2043. sd->status.skill[idx].lv = 0;
  2044. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2045. continue;
  2046. }
  2047. switch (skill_id) {
  2048. case NV_TRICKDEAD:
  2049. if( (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE ) {
  2050. sd->status.skill[idx].id = 0;
  2051. sd->status.skill[idx].lv = 0;
  2052. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2053. }
  2054. break;
  2055. }
  2056. }
  2057. }
  2058. for (const auto &skill : skill_db) {
  2059. uint16 idx = skill_get_index(skill.second->nameid);
  2060. // Restore original level of skills after deleting earned skills.
  2061. 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 ) {
  2062. sd->status.skill[idx].lv = (sd->status.skill[idx].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2063. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  2064. }
  2065. }
  2066. // Removes Taekwon Ranker skill bonus
  2067. if ((sd->class_&MAPID_UPPERMASK) != MAPID_TAEKWON) {
  2068. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(JOB_TAEKWON);
  2069. if (tree != nullptr && !tree->skills.empty()) {
  2070. for (const auto &it : tree->skills) {
  2071. uint16 sk_idx = skill_get_index(it.first);
  2072. if (sk_idx == 0)
  2073. continue;
  2074. if (sd->status.skill[sk_idx].flag != SKILL_FLAG_PLAGIARIZED && sd->status.skill[sk_idx].flag != SKILL_FLAG_PERM_GRANTED) {
  2075. if (it.first == NV_BASIC || it.first == NV_FIRSTAID || it.first == WE_CALLBABY)
  2076. continue;
  2077. sd->status.skill[sk_idx].id = 0;
  2078. }
  2079. }
  2080. }
  2081. }
  2082. // Grant all skills
  2083. pc_grant_allskills(sd, false);
  2084. int flag;
  2085. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2086. do {
  2087. flag = 0;
  2088. if (tree == nullptr || tree->skills.empty())
  2089. break;
  2090. for (const auto &skillsit : tree->skills) {
  2091. bool fail = false;
  2092. uint16 skid = skillsit.first;
  2093. uint16 sk_idx = skill_get_index(skid);
  2094. if (sd->status.skill[sk_idx].id)
  2095. continue; //Skill already known.
  2096. if (!battle_config.skillfree) {
  2097. // Checking required skills
  2098. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2099. if (entry != nullptr && !entry->need.empty()) {
  2100. for (const auto &it : entry->need) {
  2101. uint16 sk_need_id = it.first;
  2102. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2103. if (sk_need_idx > 0) {
  2104. uint16 sk_need = sk_need_id;
  2105. 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)
  2106. sk_need = 0; //Not learned.
  2107. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real learned level
  2108. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2109. else
  2110. sk_need = pc_checkskill(sd,sk_need_id);
  2111. if (sk_need < it.second) {
  2112. fail = true;
  2113. break;
  2114. }
  2115. }
  2116. }
  2117. }
  2118. if (sd->status.base_level < entry->baselv) { //We need to get the actual class in this case
  2119. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2120. c_ = pc_class2idx(c_);
  2121. if (class_ == c_ || (class_ != c_ && sd->status.base_level < entry->baselv))
  2122. fail = true; // base level requirement wasn't satisfied
  2123. }
  2124. if (sd->status.job_level < entry->joblv) { //We need to get the actual class in this case
  2125. int c_ = pc_mapid2jobid(sd->class_, sd->status.sex);
  2126. c_ = pc_class2idx(c_);
  2127. if (class_ == c_ || (class_ != c_ && sd->status.job_level < entry->joblv))
  2128. fail = true; // job level requirement wasn't satisfied
  2129. }
  2130. }
  2131. if (!fail) {
  2132. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2133. if (!sd->status.skill[sk_idx].lv && (
  2134. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2135. skill->inf2[INF2_ISWEDDING] ||
  2136. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2137. ))
  2138. continue; //Cannot be learned via normal means. Note this check DOES allows raising already known skills.
  2139. sd->status.skill[sk_idx].id = skid;
  2140. if(skill->inf2[INF2_ISSPIRIT]) { //Spirit skills cannot be learned, they will only show up on your tree when you get buffed.
  2141. sd->status.skill[sk_idx].lv = 1; // need to manually specify a skill level
  2142. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; //So it is not saved, and tagged as a "bonus" skill.
  2143. }
  2144. flag = 1; // skill list has changed, perform another pass
  2145. }
  2146. }
  2147. } while(flag);
  2148. if( class_ > 0 && sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) ) {
  2149. uint16 skid = 0;
  2150. /* Taekwon Ranker Bonus Skill Tree
  2151. ============================================
  2152. - Grant All Taekwon Tree, but only as Bonus Skills in case they drop from ranking.
  2153. - (c > 0) to avoid grant Novice Skill Tree in case of Skill Reset (need more logic)
  2154. - (sd->status.skill_point == 0) to wait until all skill points are assigned to avoid problems with Job Change quest. */
  2155. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(job_id);
  2156. if (tree != nullptr && !tree->skills.empty()) {
  2157. for (const auto &it : tree->skills) {
  2158. skid = it.first;
  2159. uint16 sk_idx = skill_get_index(skid);
  2160. if (sk_idx == 0)
  2161. continue;
  2162. if( skill_get_inf2_(skid, { INF2_ISQUEST, INF2_ISWEDDING }) )
  2163. continue; //Do not include Quest/Wedding skills.
  2164. if( sd->status.skill[sk_idx].id == 0 ) {
  2165. sd->status.skill[sk_idx].id = skid;
  2166. sd->status.skill[sk_idx].flag = SKILL_FLAG_TEMPORARY; // So it is not saved, and tagged as a "bonus" skill.
  2167. } else if( skid != NV_BASIC )
  2168. sd->status.skill[sk_idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[sk_idx].lv; // Remember original level
  2169. sd->status.skill[sk_idx].lv = skill_tree_get_max(skid, sd->status.class_);
  2170. }
  2171. }
  2172. }
  2173. // Enable Bard/Dancer spirit linked skills.
  2174. if (sd->sc.count && sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_BARDDANCER) {
  2175. std::vector<std::vector<uint16>> linked_skills = { { BA_WHISTLE, DC_HUMMING },
  2176. { BA_ASSASSINCROSS, DC_DONTFORGETME },
  2177. { BA_POEMBRAGI, DC_FORTUNEKISS },
  2178. { BA_APPLEIDUN, DC_SERVICEFORYOU } };
  2179. for (const auto &skill : linked_skills) {
  2180. if (pc_checkskill(sd, skill[!sd->status.sex]) < 10)
  2181. continue;
  2182. // Tag it as a non-savable, non-uppable, bonus skill
  2183. pc_skill(sd, skill[sd->status.sex], 10, ADDSKILL_TEMP);
  2184. }
  2185. }
  2186. }
  2187. //Checks if you can learn a new skill after having leveled up a skill.
  2188. static void pc_check_skilltree(map_session_data *sd)
  2189. {
  2190. int flag = 0;
  2191. if (battle_config.skillfree)
  2192. return; //Function serves no purpose if this is set
  2193. int c = pc_mapid2jobid( pc_calc_skilltree_normalize_job( sd ), sd->status.sex );
  2194. if (c == -1) { //Unable to normalize job??
  2195. 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);
  2196. return;
  2197. }
  2198. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(c);
  2199. if (tree == nullptr || tree->skills.empty())
  2200. return;
  2201. do {
  2202. flag = 0;
  2203. for (const auto &skillsit : tree->skills) {
  2204. uint16 skid = skillsit.first;
  2205. uint16 sk_idx = skill_get_index(skid);
  2206. bool fail = false;
  2207. if (sd->status.skill[sk_idx].id) //Already learned
  2208. continue;
  2209. // Checking required skills
  2210. std::shared_ptr<s_skill_tree_entry> entry = skillsit.second;
  2211. if (entry != nullptr && !entry->need.empty()) {
  2212. for (const auto &it : entry->need) {
  2213. uint16 sk_need_id = it.first;
  2214. uint16 sk_need_idx = skill_get_index(sk_need_id);
  2215. if (sk_need_id > 0) {
  2216. short sk_need = sk_need_id;
  2217. 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)
  2218. sk_need = 0; //Not learned.
  2219. else if (sd->status.skill[sk_need_idx].flag >= SKILL_FLAG_REPLACED_LV_0) //Real lerned level
  2220. sk_need = sd->status.skill[sk_need_idx].flag - SKILL_FLAG_REPLACED_LV_0;
  2221. else
  2222. sk_need = pc_checkskill(sd,sk_need_id);
  2223. if (sk_need < it.second) {
  2224. fail = true;
  2225. break;
  2226. }
  2227. }
  2228. }
  2229. }
  2230. if( fail )
  2231. continue;
  2232. if (sd->status.base_level < entry->baselv || sd->status.job_level < entry->joblv)
  2233. continue;
  2234. std::shared_ptr<s_skill_db> skill = skill_db.find(skid);
  2235. if( !sd->status.skill[sk_idx].lv && (
  2236. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  2237. skill->inf2[INF2_ISWEDDING] ||
  2238. (skill->inf2[INF2_ISSPIRIT] && !sd->sc.getSCE(SC_SPIRIT))
  2239. ) )
  2240. continue; //Cannot be learned via normal means.
  2241. sd->status.skill[sk_idx].id = skid;
  2242. flag = 1;
  2243. }
  2244. } while(flag);
  2245. }
  2246. // Make sure all the skills are in the correct condition
  2247. // before persisting to the backend.. [MouseJstr]
  2248. void pc_clean_skilltree(map_session_data *sd)
  2249. {
  2250. uint16 i;
  2251. for (i = 0; i < MAX_SKILL; i++){
  2252. if (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY || sd->status.skill[i].flag == SKILL_FLAG_PLAGIARIZED) {
  2253. sd->status.skill[i].id = 0;
  2254. sd->status.skill[i].lv = 0;
  2255. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2256. }
  2257. else if (sd->status.skill[i].flag >= SKILL_FLAG_REPLACED_LV_0){
  2258. sd->status.skill[i].lv = sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  2259. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  2260. }
  2261. }
  2262. }
  2263. uint64 pc_calc_skilltree_normalize_job_sub( map_session_data *sd ){
  2264. int skill_point = pc_calc_skillpoint( sd );
  2265. if( sd->class_ & MAPID_SUMMONER ){
  2266. // Novice's skill points for basic skill.
  2267. std::shared_ptr<s_job_info> summoner_job = job_db.find( JOB_SUMMONER );
  2268. int summoner_skills = summoner_job->max_job_level - 1;
  2269. if( skill_point < summoner_skills ){
  2270. return MAPID_SUMMONER;
  2271. }
  2272. skill_point -= summoner_skills;
  2273. }else{
  2274. // Novice's skill points for basic skill.
  2275. std::shared_ptr<s_job_info> novice_job = job_db.find( JOB_NOVICE );
  2276. int novice_skills = novice_job->max_job_level - 1;
  2277. if( skill_point < novice_skills ){
  2278. return MAPID_NOVICE;
  2279. }
  2280. skill_point -= novice_skills;
  2281. }
  2282. // 1st Class Job LV Check
  2283. if( sd->class_ & JOBL_2 && ( sd->class_ & MAPID_UPPERMASK ) != MAPID_SUPER_NOVICE ){
  2284. uint64 mapid_1st = sd->class_ & MAPID_BASEMASK;
  2285. int class_1st = pc_mapid2jobid( mapid_1st, sd->status.sex );
  2286. if( !sd->change_level_2nd ){
  2287. if( class_1st >= JOB_NOVICE ){
  2288. sd->change_level_2nd = job_db.find( class_1st )->max_job_level;
  2289. }else{
  2290. sd->change_level_2nd = 0;
  2291. }
  2292. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  2293. }
  2294. if( class_1st > 0 && skill_point < ( sd->change_level_2nd - 1 ) ){
  2295. return mapid_1st;
  2296. }
  2297. skill_point -= ( sd->change_level_2nd - 1 );
  2298. }
  2299. // 2nd Class Job LV Check
  2300. if( sd->class_ & JOBL_THIRD && (sd->class_ & MAPID_THIRDMASK) != MAPID_SUPER_NOVICE_E ){
  2301. uint64 mapid_2nd = sd->class_ & MAPID_UPPERMASK;
  2302. int class_2nd = pc_mapid2jobid( mapid_2nd, sd->status.sex );
  2303. if( !sd->change_level_3rd ){
  2304. if( class_2nd >= JOB_NOVICE ){
  2305. sd->change_level_3rd = job_db.find( class_2nd )->max_job_level;
  2306. }else{
  2307. sd->change_level_3rd = 0;
  2308. }
  2309. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  2310. }
  2311. if( class_2nd > 0 && skill_point < ( sd->change_level_3rd - 1 ) ){
  2312. return mapid_2nd;
  2313. }
  2314. skill_point -= ( sd->change_level_3rd - 1 );
  2315. }
  2316. // 3rd Class Job LV Check
  2317. if( sd->class_ & JOBL_FOURTH ){
  2318. uint64 mapid_3rd = sd->class_ & MAPID_THIRDMASK | JOBL_THIRD;
  2319. int class_3rd = pc_mapid2jobid( mapid_3rd, sd->status.sex );
  2320. if( !sd->change_level_4th ){
  2321. if( class_3rd >= JOB_NOVICE ){
  2322. sd->change_level_4th = job_db.find( class_3rd )->max_job_level;
  2323. }else{
  2324. sd->change_level_4th = 0;
  2325. }
  2326. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ), sd->change_level_4th );
  2327. }
  2328. if( class_3rd > 0 && skill_point < ( sd->change_level_4th - 1 ) ){
  2329. return mapid_3rd;
  2330. }
  2331. skill_point -= ( sd->change_level_4th - 1 );
  2332. }
  2333. return sd->class_;
  2334. }
  2335. uint64 pc_calc_skilltree_normalize_job( map_session_data *sd ){
  2336. if( !battle_config.skillup_limit || pc_has_permission( sd, PC_PERM_ALL_SKILL ) ){
  2337. return sd->class_;
  2338. }
  2339. uint64 c = pc_calc_skilltree_normalize_job_sub( sd );
  2340. // Special Masks
  2341. if( sd->class_&JOBL_UPPER ){
  2342. if( pc_mapid2jobid( c | JOBL_UPPER, sd->status.sex ) >= JOB_NOVICE ){
  2343. c |= JOBL_UPPER;// Rebirth Job
  2344. }
  2345. }
  2346. if( sd->class_&JOBL_BABY ){
  2347. if( pc_mapid2jobid( c | JOBL_BABY, sd->status.sex ) >= JOB_NOVICE ){
  2348. c |= JOBL_BABY;// Baby Job
  2349. }
  2350. }
  2351. return c;
  2352. }
  2353. /*==========================================
  2354. * Updates the weight status
  2355. *------------------------------------------
  2356. * 1: overweight 50% for pre-renewal and 70% for renewal
  2357. * 2: overweight 90%
  2358. * It's assumed that SC_WEIGHT50 and SC_WEIGHT90 are only started/stopped here.
  2359. */
  2360. void pc_updateweightstatus(map_session_data *sd)
  2361. {
  2362. int old_overweight;
  2363. int new_overweight;
  2364. nullpo_retv(sd);
  2365. old_overweight = (sd->sc.getSCE(SC_WEIGHT90)) ? 2 : (sd->sc.getSCE(SC_WEIGHT50)) ? 1 : 0;
  2366. #ifdef RENEWAL
  2367. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is70overweight(sd)) ? 1 : 0;
  2368. #else
  2369. new_overweight = (pc_is90overweight(sd)) ? 2 : (pc_is50overweight(sd)) ? 1 : 0;
  2370. #endif
  2371. if( old_overweight == new_overweight )
  2372. return; // no change
  2373. // stop old status change
  2374. if( old_overweight == 1 )
  2375. status_change_end(&sd->bl, SC_WEIGHT50);
  2376. else if( old_overweight == 2 )
  2377. status_change_end(&sd->bl, SC_WEIGHT90);
  2378. // start new status change
  2379. if( new_overweight == 1 )
  2380. sc_start(&sd->bl,&sd->bl, SC_WEIGHT50, 100, 0, 0);
  2381. else if( new_overweight == 2 )
  2382. sc_start(&sd->bl,&sd->bl, SC_WEIGHT90, 100, 0, 0);
  2383. // update overweight status
  2384. sd->regen.state.overweight = new_overweight;
  2385. }
  2386. int pc_disguise(map_session_data *sd, int class_)
  2387. {
  2388. if (!class_ && !sd->disguise)
  2389. return 0;
  2390. if (class_ && sd->disguise == class_)
  2391. return 0;
  2392. if(pc_isinvisible(sd))
  2393. { //Character is invisible. Stealth class-change. [Skotlex]
  2394. sd->disguise = class_; //viewdata is set on uncloaking.
  2395. return 2;
  2396. }
  2397. if (sd->bl.prev != NULL) {
  2398. pc_stop_walking(sd, 0);
  2399. clif_clearunit_area(&sd->bl, CLR_OUTSIGHT);
  2400. }
  2401. if (!class_) {
  2402. sd->disguise = 0;
  2403. class_ = sd->status.class_;
  2404. } else
  2405. sd->disguise=class_;
  2406. status_set_viewdata(&sd->bl, class_);
  2407. clif_changeoption(&sd->bl);
  2408. if (sd->bl.prev != NULL) {
  2409. clif_spawn(&sd->bl);
  2410. if (class_ == sd->status.class_ && pc_iscarton(sd))
  2411. { //It seems the cart info is lost on undisguise.
  2412. clif_cartlist(sd);
  2413. clif_updatestatus(sd,SP_CARTINFO);
  2414. }
  2415. if (sd->chatID) {
  2416. struct chat_data* cd;
  2417. if ((cd = (struct chat_data*)map_id2bl(sd->chatID)) != NULL)
  2418. clif_dispchat(cd,0);
  2419. }
  2420. }
  2421. return 1;
  2422. }
  2423. /// Show error message
  2424. #define PC_BONUS_SHOW_ERROR(type,type2,val) { ShowError("%s: %s: Invalid %s %d.\n",__FUNCTION__,#type,#type2,(val)); break; }
  2425. /// Check for valid Element, break & show error message if invalid Element
  2426. #define PC_BONUS_CHK_ELEMENT(ele,bonus) { if (!CHK_ELEMENT((ele))) { PC_BONUS_SHOW_ERROR((bonus),Element,(ele)); }}
  2427. /// Check for valid Race, break & show error message if invalid Race
  2428. #define PC_BONUS_CHK_RACE(rc,bonus) { if (!CHK_RACE((rc))) { PC_BONUS_SHOW_ERROR((bonus),Race,(rc)); }}
  2429. /// Check for valid Race2, break & show error message if invalid Race2
  2430. #define PC_BONUS_CHK_RACE2(rc2,bonus) { if (!CHK_RACE2((rc2))) { PC_BONUS_SHOW_ERROR((bonus),Race2,(rc2)); }}
  2431. /// Check for valid Class, break & show error message if invalid Class
  2432. #define PC_BONUS_CHK_CLASS(cl,bonus) { if (!CHK_CLASS((cl))) { PC_BONUS_SHOW_ERROR((bonus),Class,(cl)); }}
  2433. /// Check for valid Size, break & show error message if invalid Size
  2434. #define PC_BONUS_CHK_SIZE(sz,bonus) { if (!CHK_MOBSIZE((sz))) { PC_BONUS_SHOW_ERROR((bonus),Size,(sz)); }}
  2435. /// Check for valid SC, break & show error message if invalid SC
  2436. #define PC_BONUS_CHK_SC(sc,bonus) { if ((sc) <= SC_NONE || (sc) >= SC_MAX) { PC_BONUS_SHOW_ERROR((bonus),Effect,(sc)); }}
  2437. /**
  2438. * Add auto spell bonus for player while attacking/attacked
  2439. * @param spell: Spell array
  2440. * @param id: Skill to cast
  2441. * @param lv: Skill level
  2442. * @param rate: Success chance
  2443. * @param battle_flag: Battle flag
  2444. * @param card_id: Used to prevent card stacking
  2445. * @param flag: Flags used for extra arguments
  2446. * &0: forces the skill to be casted on self, rather than on the target
  2447. * &1: forces the skill to be casted on target, rather than self
  2448. * &2: random skill level in [1..lv] is chosen
  2449. */
  2450. 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)
  2451. {
  2452. if (spell.size() == MAX_PC_BONUS) {
  2453. ShowWarning("pc_bonus_autospell: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2454. return;
  2455. }
  2456. if (!rate)
  2457. return;
  2458. if (!(battle_flag&BF_RANGEMASK))
  2459. battle_flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2460. if (!(battle_flag&BF_WEAPONMASK))
  2461. battle_flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2462. if (!(battle_flag&BF_SKILLMASK)) {
  2463. if (battle_flag&(BF_MAGIC | BF_MISC))
  2464. battle_flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2465. if (battle_flag&BF_WEAPON)
  2466. battle_flag |= BF_NORMAL; //By default autospells should only trigger on normal weapon attacks.
  2467. }
  2468. for (auto &it : spell) {
  2469. 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) {
  2470. if (!battle_config.autospell_stacking && it.rate > 0 && rate > 0) // Stacking disabled
  2471. return;
  2472. it.rate = util::safe_addition_cap(it.rate, rate, (short)1000);
  2473. return;
  2474. }
  2475. }
  2476. struct s_autospell entry = {};
  2477. if (rate < -1000 || rate > 1000)
  2478. ShowWarning("pc_bonus_autospell: Item bonus rate %d exceeds -1000~1000 range, capping.\n", rate);
  2479. entry.id = id;
  2480. entry.lv = lv;
  2481. entry.rate = cap_value(rate, -1000, 1000);
  2482. entry.battle_flag = battle_flag;
  2483. entry.card_id = card_id;
  2484. entry.flag = flag;
  2485. spell.push_back(entry);
  2486. }
  2487. /**
  2488. * Add auto spell bonus for player while using skills
  2489. * @param spell: Spell array
  2490. * @param src_skill: Trigger skill
  2491. * @param id: Support or target type
  2492. * @param lv: Skill level
  2493. * @param rate: Success chance
  2494. * @param card_id: Used to prevent card stacking
  2495. * @param flag: Flags used for extra arguments
  2496. * &1: forces the skill to be casted on self, rather than on the target of skill
  2497. * &2: random skill level in [1..lv] is chosen
  2498. */
  2499. 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)
  2500. {
  2501. if (spell.size() == MAX_PC_BONUS) {
  2502. ShowWarning("pc_bonus_autospell_onskill: Reached max (%d) number of autospells per character!\n", MAX_PC_BONUS);
  2503. return;
  2504. }
  2505. if (!rate)
  2506. return;
  2507. struct s_autospell entry = {};
  2508. if (rate < -1000 || rate > 1000)
  2509. ShowWarning("pc_bonus_onskill: Item bonus rate %d exceeds -1000~1000 range, capping.\n", rate);
  2510. entry.trigger_skill = src_skill;
  2511. entry.id = id;
  2512. entry.lv = lv;
  2513. entry.rate = cap_value(rate, -1000, 1000);
  2514. entry.card_id = card_id;
  2515. entry.flag = flag;
  2516. spell.push_back(entry);
  2517. }
  2518. /**
  2519. * Add inflict effect bonus for player while attacking/attacked
  2520. * @param effect: Effect array
  2521. * @param sc: SC/Effect type
  2522. * @param rate: Success chance
  2523. * @param arrow_rate: success chance if bonus comes from arrow-type item
  2524. * @param flag: Target flag
  2525. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2526. */
  2527. static void pc_bonus_addeff(std::vector<s_addeffect> &effect, enum sc_type sc, int rate, short arrow_rate, unsigned char flag, unsigned int duration)
  2528. {
  2529. if (effect.size() == MAX_PC_BONUS) {
  2530. ShowWarning("pc_bonus_addeff: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2531. return;
  2532. }
  2533. if (!(flag&(ATF_SHORT | ATF_LONG)))
  2534. flag |= ATF_SHORT | ATF_LONG; //Default range: both
  2535. if (!(flag&(ATF_TARGET | ATF_SELF)))
  2536. flag |= ATF_TARGET; //Default target: enemy.
  2537. if (!(flag&(ATF_WEAPON | ATF_MAGIC | ATF_MISC)))
  2538. flag |= ATF_WEAPON; //Default type: weapon.
  2539. if (!duration)
  2540. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2541. for (auto &it : effect) {
  2542. if (it.sc == sc && it.flag == flag) {
  2543. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2544. it.arrow_rate = util::safe_addition_cap(it.arrow_rate, arrow_rate, (short)SHRT_MAX);
  2545. it.duration = umax(it.duration, duration);
  2546. return;
  2547. }
  2548. }
  2549. struct s_addeffect entry = {};
  2550. entry.sc = sc;
  2551. entry.rate = rate;
  2552. entry.arrow_rate = arrow_rate;
  2553. entry.flag = flag;
  2554. entry.duration = duration;
  2555. effect.push_back(entry);
  2556. }
  2557. /**
  2558. * Add inflict effect bonus for player while attacking using skill
  2559. * @param effect: Effect array
  2560. * @param sc: SC/Effect type
  2561. * @param rate: Success chance
  2562. * @param skill_id: Skill to cast
  2563. * @param target: Target type
  2564. * @param duration: Duration. If 0 use default duration lookup for associated skill with level 7
  2565. */
  2566. static void pc_bonus_addeff_onskill(std::vector<s_addeffectonskill> &effect, enum sc_type sc, int rate, short skill_id, unsigned char target, unsigned int duration)
  2567. {
  2568. if (effect.size() == MAX_PC_BONUS) {
  2569. ShowWarning("pc_bonus_addeff_onskill: Reached max (%d) number of add effects per character!\n", MAX_PC_BONUS);
  2570. return;
  2571. }
  2572. if (!duration)
  2573. duration = (unsigned int)skill_get_time2(status_db.getSkill(sc), 7);
  2574. for (auto &it : effect) {
  2575. if (it.sc == sc && it.skill_id == skill_id && it.target == target) {
  2576. it.rate = util::safe_addition_cap(it.rate, rate, INT_MAX);
  2577. it.duration = umax(it.duration, duration);
  2578. return;
  2579. }
  2580. }
  2581. struct s_addeffectonskill entry = {};
  2582. entry.sc = sc;
  2583. entry.rate = rate;
  2584. entry.skill_id = skill_id;
  2585. entry.target = target;
  2586. entry.duration = duration;
  2587. effect.push_back(entry);
  2588. }
  2589. /**
  2590. * Adjust/add drop rate modifier for player
  2591. * @param drop: Player's sd->add_drop (struct s_add_drop)
  2592. * @param nameid: item id that will be dropped
  2593. * @param group: group id
  2594. * @param class_: target class
  2595. * @param race: target race. if < 0, means monster_id
  2596. * @param rate: rate value: 1 ~ 10000. If < 0, it will be multiplied with mob level/10
  2597. */
  2598. static void pc_bonus_item_drop(std::vector<s_add_drop> &drop, t_itemid nameid, uint16 group, int class_, short race, int rate)
  2599. {
  2600. if (!nameid && !group) {
  2601. ShowWarning("pc_bonus_item_drop: No Item ID nor Item Group ID specified.\n");
  2602. return;
  2603. }
  2604. if (nameid && !item_db.exists(nameid)) {
  2605. ShowWarning("pc_bonus_item_drop: Invalid item id %u\n",nameid);
  2606. return;
  2607. }
  2608. if (group && !itemdb_group.exists(group)) {
  2609. ShowWarning("pc_bonus_item_drop: Invalid Item Group %hu\n",group);
  2610. return;
  2611. }
  2612. if (drop.size() == MAX_PC_BONUS) {
  2613. 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);
  2614. return;
  2615. }
  2616. //Apply config rate adjustment settings.
  2617. if (rate >= 0) { //Absolute drop.
  2618. if (battle_config.item_rate_adddrop != 100)
  2619. rate = rate*battle_config.item_rate_adddrop/100;
  2620. if (rate < battle_config.item_drop_adddrop_min)
  2621. rate = battle_config.item_drop_adddrop_min;
  2622. else if (rate > battle_config.item_drop_adddrop_max)
  2623. rate = battle_config.item_drop_adddrop_max;
  2624. } else { //Relative drop, max/min limits are applied at drop time.
  2625. if (battle_config.item_rate_adddrop != 100)
  2626. rate = rate*battle_config.item_rate_adddrop/100;
  2627. if (rate > -1)
  2628. rate = -1;
  2629. }
  2630. for (auto &it : drop) {
  2631. if (it.nameid == nameid && it.group == group && it.race == race && it.class_ == class_) {
  2632. if ((rate < 0 && it.rate < 0) || (rate > 0 && it.rate > 0)) //Adjust the rate if it has same classification
  2633. it.rate = util::safe_addition_cap(it.rate, rate, 10000);
  2634. return;
  2635. }
  2636. }
  2637. struct s_add_drop entry = {};
  2638. if (rate < -10000 || rate > 10000)
  2639. ShowWarning("pc_bonus_item_drop: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2640. entry.nameid = nameid;
  2641. entry.group = group;
  2642. entry.race = race;
  2643. entry.class_ = class_;
  2644. entry.rate = cap_value(rate, -10000, 10000);
  2645. drop.push_back(entry);
  2646. }
  2647. s_autobonus::~s_autobonus(){
  2648. if( this->active != INVALID_TIMER ){
  2649. delete_timer( this->active, pc_endautobonus );
  2650. this->active = INVALID_TIMER;
  2651. }
  2652. if( this->bonus_script != nullptr ){
  2653. aFree( this->bonus_script );
  2654. this->bonus_script = nullptr;
  2655. }
  2656. if( this->other_script != nullptr ){
  2657. aFree( this->other_script );
  2658. this->other_script = nullptr;
  2659. }
  2660. }
  2661. /**
  2662. * Add autobonus to player when attacking/attacked
  2663. * @param bonus: Bonus array
  2664. * @param script: Script to execute
  2665. * @param rate: Success chance
  2666. * @param dur: Duration
  2667. * @param flag: Battle flag/skill
  2668. * @param other_script: Secondary script to execute
  2669. * @param pos: Item equip position
  2670. * @param onskill: Skill used to trigger autobonus
  2671. * @return True on success or false otherwise
  2672. */
  2673. bool pc_addautobonus(std::vector<std::shared_ptr<s_autobonus>> &bonus, const char *script, short rate, unsigned int dur, uint16 flag, const char *other_script, unsigned int pos, bool onskill){
  2674. // Check if the same bonus already exists
  2675. for( std::shared_ptr<s_autobonus> autobonus : bonus ){
  2676. // Compare based on position and bonus script
  2677. if( autobonus->pos == pos && strcmp( script, autobonus->bonus_script ) == 0 ){
  2678. return false;
  2679. }
  2680. }
  2681. if (bonus.size() == MAX_PC_BONUS) {
  2682. ShowWarning("pc_addautobonus: Reached max (%d) number of autobonus per character!\n", MAX_PC_BONUS);
  2683. return false;
  2684. }
  2685. if (!onskill) {
  2686. if (!(flag&BF_RANGEMASK))
  2687. flag |= BF_SHORT | BF_LONG; //No range defined? Use both.
  2688. if (!(flag&BF_WEAPONMASK))
  2689. flag |= BF_WEAPON; //No attack type defined? Use weapon.
  2690. if (!(flag&BF_SKILLMASK)) {
  2691. if (flag&(BF_MAGIC | BF_MISC))
  2692. flag |= BF_SKILL; //These two would never trigger without BF_SKILL
  2693. if (flag&BF_WEAPON)
  2694. flag |= BF_NORMAL | BF_SKILL;
  2695. }
  2696. }
  2697. std::shared_ptr<s_autobonus> entry = std::make_shared<s_autobonus>();
  2698. if (rate < -10000 || rate > 10000)
  2699. ShowWarning("pc_addautobonus: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2700. entry->rate = cap_value(rate, -10000, 10000);
  2701. entry->duration = dur;
  2702. entry->active = INVALID_TIMER;
  2703. entry->atk_type = flag;
  2704. entry->pos = pos;
  2705. entry->bonus_script = aStrdup(script);
  2706. entry->other_script = (other_script ? aStrdup(other_script) : NULL);
  2707. bonus.push_back(entry);
  2708. return true;
  2709. }
  2710. /**
  2711. * Remove an autobonus from player
  2712. * @param sd: Player data
  2713. * @param bonus: Autobonus array
  2714. * @param restore: Run script on clearing or not
  2715. */
  2716. void pc_delautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> &bonus, bool restore){
  2717. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  2718. while( it != bonus.end() ){
  2719. std::shared_ptr<s_autobonus> b = *it;
  2720. if( b->active != INVALID_TIMER && restore && b->bonus_script != nullptr ){
  2721. unsigned int equip_pos_idx = 0;
  2722. // Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2723. for (uint8 j = 0; j < EQI_MAX; j++) {
  2724. if (sd.equip_index[j] >= 0)
  2725. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2726. }
  2727. if( ( equip_pos_idx&b->pos ) == b->pos ){
  2728. script_run_autobonus(b->bonus_script, &sd, b->pos);
  2729. }else{
  2730. // Not all required items equipped anymore
  2731. restore = false;
  2732. }
  2733. }
  2734. if( restore ){
  2735. it++;
  2736. continue;
  2737. }
  2738. it = bonus.erase(it);
  2739. }
  2740. }
  2741. /**
  2742. * Execute autobonus on player
  2743. * @param sd: Player data
  2744. * @param autobonus: Autobonus to run
  2745. */
  2746. void pc_exeautobonus(map_session_data &sd, std::vector<std::shared_ptr<s_autobonus>> *bonus, std::shared_ptr<s_autobonus> autobonus)
  2747. {
  2748. if (autobonus->active != INVALID_TIMER)
  2749. delete_timer(autobonus->active, pc_endautobonus);
  2750. if( autobonus->other_script )
  2751. {
  2752. int j;
  2753. unsigned int equip_pos_idx = 0;
  2754. //Create a list of all equipped positions to see if all items needed for the autobonus are still present [Playtester]
  2755. for(j = 0; j < EQI_MAX; j++) {
  2756. if(sd.equip_index[j] >= 0)
  2757. equip_pos_idx |= sd.inventory.u.items_inventory[sd.equip_index[j]].equip;
  2758. }
  2759. if((equip_pos_idx&autobonus->pos) == autobonus->pos)
  2760. script_run_autobonus(autobonus->other_script,&sd,autobonus->pos);
  2761. }
  2762. autobonus->active = add_timer(gettick()+autobonus->duration, pc_endautobonus, sd.bl.id, (intptr_t)bonus);
  2763. status_calc_pc(&sd,SCO_FORCE);
  2764. }
  2765. /**
  2766. * Remove autobonus timer from player
  2767. */
  2768. TIMER_FUNC(pc_endautobonus){
  2769. map_session_data *sd = map_id2sd(id);
  2770. std::vector<std::shared_ptr<s_autobonus>> *bonus = (std::vector<std::shared_ptr<s_autobonus>> *)data;
  2771. nullpo_ret(sd);
  2772. nullpo_ret(bonus);
  2773. for( std::shared_ptr<s_autobonus> autobonus : *bonus ){
  2774. if( autobonus->active == tid ){
  2775. autobonus->active = INVALID_TIMER;
  2776. break;
  2777. }
  2778. }
  2779. status_calc_pc(sd,SCO_FORCE);
  2780. return 0;
  2781. }
  2782. /**
  2783. * Add element bonus to player when attacking
  2784. * @param sd: Player data
  2785. * @param ele: Element to adjust
  2786. * @param rate: Success chance
  2787. * @param flag: Battle flag
  2788. */
  2789. static void pc_bonus_addele(map_session_data* sd, unsigned char ele, short rate, short flag)
  2790. {
  2791. nullpo_retv(sd);
  2792. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  2793. if (wd->addele2.size() == MAX_PC_BONUS) {
  2794. ShowWarning("pc_bonus_addele: Reached max (%d) number of add element damage bonuses per character!\n", MAX_PC_BONUS);
  2795. return;
  2796. }
  2797. if (!(flag&BF_RANGEMASK))
  2798. flag |= BF_SHORT | BF_LONG;
  2799. if (!(flag&BF_WEAPONMASK))
  2800. flag |= BF_WEAPON;
  2801. if (!(flag&BF_SKILLMASK)) {
  2802. if (flag&(BF_MAGIC | BF_MISC))
  2803. flag |= BF_SKILL;
  2804. if (flag&BF_WEAPON)
  2805. flag |= BF_NORMAL | BF_SKILL;
  2806. }
  2807. for (auto &it : wd->addele2) {
  2808. if (it.ele == ele && it.flag == flag) {
  2809. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2810. return;
  2811. }
  2812. }
  2813. struct s_addele2 entry = {};
  2814. if (rate < -10000 || rate > 10000)
  2815. ShowWarning("pc_bonus_addele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2816. entry.ele = ele;
  2817. entry.rate = cap_value(rate, -10000, 10000);
  2818. entry.flag = flag;
  2819. wd->addele2.push_back(entry);
  2820. }
  2821. /**
  2822. * Reduce element bonus to player when attacking
  2823. * @param sd: Player data
  2824. * @param ele: Element to adjust
  2825. * @param rate: Success chance
  2826. * @param flag: Battle flag
  2827. */
  2828. static void pc_bonus_subele(map_session_data* sd, unsigned char ele, short rate, short flag)
  2829. {
  2830. nullpo_retv(sd);
  2831. if (sd->subele2.size() == MAX_PC_BONUS) {
  2832. ShowWarning("pc_bonus_subele: Reached max (%d) number of resist element damage bonuses per character!\n", MAX_PC_BONUS);
  2833. return;
  2834. }
  2835. if (!(flag&BF_RANGEMASK))
  2836. flag |= BF_SHORT | BF_LONG;
  2837. if (!(flag&BF_WEAPONMASK))
  2838. flag |= BF_WEAPON;
  2839. if (!(flag&BF_SKILLMASK)) {
  2840. if (flag&(BF_MAGIC | BF_MISC))
  2841. flag |= BF_SKILL;
  2842. if (flag&BF_WEAPON)
  2843. flag |= BF_NORMAL | BF_SKILL;
  2844. }
  2845. for (auto &it : sd->subele2) {
  2846. if (it.ele == ele && it.flag == flag) {
  2847. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2848. return;
  2849. }
  2850. }
  2851. struct s_addele2 entry = {};
  2852. if (rate < -10000 || rate > 10000)
  2853. ShowWarning("pc_bonus_subele: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2854. entry.ele = ele;
  2855. entry.rate = cap_value(rate, -10000, 10000);
  2856. entry.flag = flag;
  2857. sd->subele2.push_back(entry);
  2858. }
  2859. /**
  2860. * Adjust race damage to target when attacking
  2861. * @param sd: Player data
  2862. * @param race: Race to adjust
  2863. * @param rate: Success chance
  2864. * @param flag: Battle flag
  2865. */
  2866. static void pc_bonus_subrace(map_session_data* sd, unsigned char race, short rate, short flag)
  2867. {
  2868. if (sd->subrace3.size() == MAX_PC_BONUS) {
  2869. ShowWarning("pc_bonus_subrace: Reached max (%d) number of resist race damage bonuses per character!\n", MAX_PC_BONUS);
  2870. return;
  2871. }
  2872. if (!(flag&BF_RANGEMASK))
  2873. flag |= BF_SHORT | BF_LONG;
  2874. if (!(flag&BF_WEAPONMASK))
  2875. flag |= BF_WEAPON;
  2876. if (!(flag&BF_SKILLMASK)) {
  2877. if (flag&(BF_MAGIC | BF_MISC))
  2878. flag |= BF_SKILL;
  2879. if (flag&BF_WEAPON)
  2880. flag |= BF_NORMAL | BF_SKILL;
  2881. }
  2882. for (auto &it : sd->subrace3) {
  2883. if (it.race == race && it.flag == flag) {
  2884. it.rate = util::safe_addition_cap(it.rate, rate, (short)10000);
  2885. return;
  2886. }
  2887. }
  2888. struct s_addrace2 entry = {};
  2889. if (rate < -10000 || rate > 10000)
  2890. ShowWarning("pc_bonus_subrace: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2891. entry.race = race;
  2892. entry.rate = cap_value(rate, -10000, 10000);
  2893. entry.flag = flag;
  2894. sd->subrace3.push_back(entry);
  2895. }
  2896. /**
  2897. * General item bonus for player
  2898. * @param bonus: Bonus array
  2899. * @param id: Key
  2900. * @param val: Value
  2901. * @param cap_rate: If Value is a rate value that needs to be capped
  2902. */
  2903. static void pc_bonus_itembonus(std::vector<s_item_bonus> &bonus, uint16 id, int val, bool cap_rate)
  2904. {
  2905. for (auto &it : bonus) {
  2906. if (it.id == id) {
  2907. if (cap_rate)
  2908. it.val = util::safe_addition_cap(it.val, val, 10000);
  2909. else
  2910. it.val += val;
  2911. return;
  2912. }
  2913. }
  2914. struct s_item_bonus entry = {};
  2915. if (cap_rate && (val < -10000 || val > 10000)) {
  2916. ShowWarning("pc_bonus_itembonus: Item bonus val %d exceeds -10000~10000 range, capping.\n", val);
  2917. val = cap_value(val, -10000, 10000);
  2918. }
  2919. entry.id = id;
  2920. entry.val = val;
  2921. bonus.push_back(entry);
  2922. }
  2923. /**
  2924. * Remove HP/SP to player when attacking
  2925. * @param bonus: Bonus array
  2926. * @param rate: Success chance
  2927. * @param per: Percentage of HP/SP to vanish
  2928. * @param flag: Battle flag
  2929. */
  2930. static void pc_bonus_addvanish(std::vector<s_vanish_bonus> &bonus, int16 rate, int16 per, int flag) {
  2931. if (bonus.size() == MAX_PC_BONUS) {
  2932. ShowWarning("pc_bonus_addvanish: Reached max (%d) number of vanish damage bonuses per character!\n", MAX_PC_BONUS);
  2933. return;
  2934. }
  2935. if (!(flag&BF_RANGEMASK))
  2936. flag |= BF_SHORT | BF_LONG;
  2937. if (!(flag&BF_WEAPONMASK))
  2938. flag |= BF_WEAPON;
  2939. if (!(flag&BF_SKILLMASK)) {
  2940. if (flag&(BF_MAGIC | BF_MISC))
  2941. flag |= BF_SKILL;
  2942. if (flag&BF_WEAPON)
  2943. flag |= BF_NORMAL | BF_SKILL;
  2944. }
  2945. for (auto &it : bonus) {
  2946. if (it.flag == flag) {
  2947. it.rate = util::safe_addition_cap(it.rate, rate, (int16)10000);
  2948. it.per += per;
  2949. return;
  2950. }
  2951. }
  2952. struct s_vanish_bonus entry = {};
  2953. if (rate < -10000 || rate > 10000)
  2954. ShowWarning("pc_bonus_addvanish: Item bonus rate %d exceeds -10000~10000 range, capping.\n", rate);
  2955. entry.rate = cap_value(rate, -10000, 10000);
  2956. entry.per = per;
  2957. entry.flag = flag;
  2958. bonus.push_back(entry);
  2959. }
  2960. /*==========================================
  2961. * Add a bonus(type) to player sd
  2962. * format: bonus bBonusName,val;
  2963. * @param sd
  2964. * @param type Bonus type used by bBonusName
  2965. * @param val Value that usually for rate or fixed value
  2966. *------------------------------------------*/
  2967. void pc_bonus(map_session_data *sd,int type,int val)
  2968. {
  2969. struct status_data *status;
  2970. int bonus;
  2971. nullpo_retv(sd);
  2972. status = &sd->base_status;
  2973. switch(type){
  2974. case SP_STR:
  2975. case SP_AGI:
  2976. case SP_VIT:
  2977. case SP_INT:
  2978. case SP_DEX:
  2979. case SP_LUK:
  2980. if(sd->state.lr_flag != 2)
  2981. sd->indexed_bonus.param_bonus[type-SP_STR]+=val;
  2982. break;
  2983. case SP_POW:
  2984. case SP_STA:
  2985. case SP_WIS:
  2986. case SP_SPL:
  2987. case SP_CON:
  2988. case SP_CRT:
  2989. if (sd->state.lr_flag != 2)
  2990. sd->indexed_bonus.param_bonus[type - SP_POW + PARAM_POW] += val;
  2991. break;
  2992. case SP_ATK1:
  2993. if(!sd->state.lr_flag) {
  2994. bonus = status->rhw.atk + val;
  2995. status->rhw.atk = cap_value(bonus, 0, USHRT_MAX);
  2996. }
  2997. else if(sd->state.lr_flag == 1) {
  2998. bonus = status->lhw.atk + val;
  2999. status->lhw.atk = cap_value(bonus, 0, USHRT_MAX);
  3000. }
  3001. break;
  3002. case SP_ATK2:
  3003. if(!sd->state.lr_flag) {
  3004. bonus = status->rhw.atk2 + val;
  3005. status->rhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3006. }
  3007. else if(sd->state.lr_flag == 1) {
  3008. bonus = status->lhw.atk2 + val;
  3009. status->lhw.atk2 = cap_value(bonus, 0, USHRT_MAX);
  3010. }
  3011. break;
  3012. case SP_BASE_ATK:
  3013. if(sd->state.lr_flag != 2) {
  3014. #ifdef RENEWAL
  3015. bonus = sd->bonus.eatk + val;
  3016. sd->bonus.eatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3017. #else
  3018. bonus = status->batk + val;
  3019. status->batk = cap_value(bonus, 0, USHRT_MAX);
  3020. #endif
  3021. }
  3022. break;
  3023. case SP_DEF1:
  3024. if(sd->state.lr_flag != 2) {
  3025. bonus = status->def + val;
  3026. #ifdef RENEWAL
  3027. status->def = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3028. #else
  3029. status->def = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3030. #endif
  3031. }
  3032. break;
  3033. case SP_DEF2:
  3034. if(sd->state.lr_flag != 2) {
  3035. bonus = status->def2 + val;
  3036. status->def2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3037. }
  3038. break;
  3039. case SP_MDEF1:
  3040. if(sd->state.lr_flag != 2) {
  3041. bonus = status->mdef + val;
  3042. #ifdef RENEWAL
  3043. status->mdef = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3044. #else
  3045. status->mdef = cap_value(bonus, CHAR_MIN, CHAR_MAX);
  3046. #endif
  3047. if( sd->state.lr_flag == 3 ) {//Shield, used for royal guard
  3048. sd->bonus.shieldmdef += bonus;
  3049. }
  3050. }
  3051. break;
  3052. case SP_MDEF2:
  3053. if(sd->state.lr_flag != 2) {
  3054. bonus = status->mdef2 + val;
  3055. status->mdef2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3056. }
  3057. break;
  3058. case SP_HIT:
  3059. if(sd->state.lr_flag != 2) {
  3060. bonus = status->hit + val;
  3061. status->hit = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3062. } else
  3063. sd->bonus.arrow_hit+=val;
  3064. break;
  3065. case SP_FLEE1:
  3066. if(sd->state.lr_flag != 2) {
  3067. bonus = status->flee + val;
  3068. status->flee = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3069. }
  3070. break;
  3071. case SP_FLEE2:
  3072. if(sd->state.lr_flag != 2) {
  3073. bonus = status->flee2 + val*10;
  3074. status->flee2 = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3075. }
  3076. break;
  3077. case SP_CRITICAL:
  3078. if(sd->state.lr_flag != 2) {
  3079. bonus = status->cri + val*10;
  3080. status->cri = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3081. } else
  3082. sd->bonus.arrow_cri += val*10;
  3083. break;
  3084. case SP_PATK:
  3085. if (sd->state.lr_flag != 2) {
  3086. bonus = status->patk + val;
  3087. status->patk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3088. }
  3089. break;
  3090. case SP_SMATK:
  3091. if (sd->state.lr_flag != 2) {
  3092. bonus = status->smatk + val;
  3093. status->smatk = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3094. }
  3095. break;
  3096. case SP_RES:
  3097. if (sd->state.lr_flag != 2) {
  3098. bonus = status->res + val;
  3099. status->res = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3100. }
  3101. break;
  3102. case SP_MRES:
  3103. if (sd->state.lr_flag != 2) {
  3104. bonus = status->mres + val;
  3105. status->mres = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3106. }
  3107. break;
  3108. case SP_HPLUS:
  3109. if (sd->state.lr_flag != 2) {
  3110. bonus = status->hplus + val;
  3111. status->hplus = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3112. }
  3113. break;
  3114. case SP_CRATE:
  3115. if (sd->state.lr_flag != 2) {
  3116. bonus = status->crate + val;
  3117. status->crate = cap_value(bonus, SHRT_MIN, SHRT_MAX);
  3118. }
  3119. break;
  3120. case SP_ATKELE:
  3121. PC_BONUS_CHK_ELEMENT(val,SP_ATKELE);
  3122. switch (sd->state.lr_flag)
  3123. {
  3124. case 2:
  3125. switch (sd->status.weapon) {
  3126. case W_BOW:
  3127. case W_REVOLVER:
  3128. case W_RIFLE:
  3129. case W_GATLING:
  3130. case W_SHOTGUN:
  3131. case W_GRENADE:
  3132. //Become weapon element.
  3133. status->rhw.ele=val;
  3134. break;
  3135. default: //Become arrow element.
  3136. sd->bonus.arrow_ele=val;
  3137. break;
  3138. }
  3139. break;
  3140. case 1:
  3141. status->lhw.ele=val;
  3142. break;
  3143. default:
  3144. status->rhw.ele=val;
  3145. break;
  3146. }
  3147. break;
  3148. case SP_DEFELE:
  3149. PC_BONUS_CHK_ELEMENT(val,SP_DEFELE);
  3150. if(sd->state.lr_flag != 2)
  3151. status->def_ele=val;
  3152. break;
  3153. case SP_MAXHP:
  3154. if(sd->state.lr_flag == 2)
  3155. break;
  3156. sd->bonus.hp += val;
  3157. break;
  3158. case SP_MAXSP:
  3159. if(sd->state.lr_flag == 2)
  3160. break;
  3161. sd->bonus.sp += val;
  3162. break;
  3163. case SP_MAXAP:
  3164. if (sd->state.lr_flag == 2)
  3165. break;
  3166. sd->bonus.ap += val;
  3167. break;
  3168. case SP_MAXHPRATE:
  3169. if(sd->state.lr_flag != 2)
  3170. sd->hprate+=val;
  3171. break;
  3172. case SP_MAXSPRATE:
  3173. if(sd->state.lr_flag != 2)
  3174. sd->sprate+=val;
  3175. break;
  3176. case SP_MAXAPRATE:
  3177. if (sd->state.lr_flag != 2)
  3178. sd->aprate += val;
  3179. break;
  3180. case SP_SPRATE:
  3181. if(sd->state.lr_flag != 2)
  3182. sd->dsprate+=val;
  3183. break;
  3184. case SP_ATTACKRANGE:
  3185. switch (sd->state.lr_flag) {
  3186. case 2:
  3187. switch (sd->status.weapon) {
  3188. case W_BOW:
  3189. case W_REVOLVER:
  3190. case W_RIFLE:
  3191. case W_GATLING:
  3192. case W_SHOTGUN:
  3193. case W_GRENADE:
  3194. status->rhw.range += val;
  3195. }
  3196. break;
  3197. case 1:
  3198. status->lhw.range += val;
  3199. break;
  3200. default:
  3201. status->rhw.range += val;
  3202. break;
  3203. }
  3204. break;
  3205. case SP_SPEED_RATE: //Non stackable increase
  3206. if(sd->state.lr_flag != 2)
  3207. sd->bonus.speed_rate = min(sd->bonus.speed_rate, -val);
  3208. break;
  3209. case SP_SPEED_ADDRATE: //Stackable increase
  3210. if(sd->state.lr_flag != 2)
  3211. sd->bonus.speed_add_rate -= val;
  3212. break;
  3213. case SP_ASPD: //Raw increase
  3214. if(sd->state.lr_flag != 2)
  3215. sd->bonus.aspd_add -= 10*val;
  3216. break;
  3217. case SP_ASPD_RATE: //Stackable increase - Made it linear as per rodatazone
  3218. if(sd->state.lr_flag != 2)
  3219. #ifndef RENEWAL_ASPD
  3220. status->aspd_rate -= 10*val;
  3221. #else
  3222. status->aspd_rate2 += val;
  3223. #endif
  3224. break;
  3225. case SP_HP_RECOV_RATE:
  3226. if(sd->state.lr_flag != 2)
  3227. sd->hprecov_rate += val;
  3228. break;
  3229. case SP_SP_RECOV_RATE:
  3230. if(sd->state.lr_flag != 2)
  3231. sd->sprecov_rate += val;
  3232. break;
  3233. case SP_CRITICAL_DEF:
  3234. if(sd->state.lr_flag != 2)
  3235. sd->bonus.critical_def += val;
  3236. break;
  3237. case SP_NEAR_ATK_DEF:
  3238. if(sd->state.lr_flag != 2)
  3239. sd->bonus.near_attack_def_rate += val;
  3240. break;
  3241. case SP_LONG_ATK_DEF:
  3242. if(sd->state.lr_flag != 2)
  3243. sd->bonus.long_attack_def_rate += val;
  3244. break;
  3245. case SP_DOUBLE_RATE:
  3246. if(sd->state.lr_flag == 0 && sd->bonus.double_rate < val)
  3247. sd->bonus.double_rate = val;
  3248. break;
  3249. case SP_DOUBLE_ADD_RATE:
  3250. if(sd->state.lr_flag == 0)
  3251. sd->bonus.double_add_rate += val;
  3252. break;
  3253. case SP_MATK_RATE:
  3254. if(sd->state.lr_flag != 2)
  3255. sd->matk_rate += val;
  3256. break;
  3257. case SP_IGNORE_DEF_ELE:
  3258. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_DEF_ELE);
  3259. if(!sd->state.lr_flag)
  3260. sd->right_weapon.ignore_def_ele |= 1<<val;
  3261. else if(sd->state.lr_flag == 1)
  3262. sd->left_weapon.ignore_def_ele |= 1<<val;
  3263. break;
  3264. case SP_IGNORE_DEF_RACE:
  3265. PC_BONUS_CHK_RACE(val,SP_IGNORE_DEF_RACE);
  3266. if(!sd->state.lr_flag)
  3267. sd->right_weapon.ignore_def_race |= 1<<val;
  3268. else if(sd->state.lr_flag == 1)
  3269. sd->left_weapon.ignore_def_race |= 1<<val;
  3270. break;
  3271. case SP_IGNORE_DEF_CLASS:
  3272. PC_BONUS_CHK_CLASS(val,SP_IGNORE_DEF_CLASS);
  3273. if(!sd->state.lr_flag)
  3274. sd->right_weapon.ignore_def_class |= 1<<val;
  3275. else if(sd->state.lr_flag == 1)
  3276. sd->left_weapon.ignore_def_class |= 1<<val;
  3277. break;
  3278. case SP_ATK_RATE:
  3279. if(sd->state.lr_flag != 2)
  3280. sd->bonus.atk_rate += val;
  3281. break;
  3282. case SP_MAGIC_ATK_DEF:
  3283. if(sd->state.lr_flag != 2)
  3284. sd->bonus.magic_def_rate += val;
  3285. break;
  3286. case SP_MISC_ATK_DEF:
  3287. if(sd->state.lr_flag != 2)
  3288. sd->bonus.misc_def_rate += val;
  3289. break;
  3290. case SP_IGNORE_MDEF_ELE:
  3291. PC_BONUS_CHK_ELEMENT(val,SP_IGNORE_MDEF_ELE);
  3292. if(sd->state.lr_flag != 2)
  3293. sd->bonus.ignore_mdef_ele |= 1<<val;
  3294. break;
  3295. case SP_IGNORE_MDEF_RACE:
  3296. PC_BONUS_CHK_RACE(val,SP_IGNORE_MDEF_RACE);
  3297. if(sd->state.lr_flag != 2)
  3298. sd->bonus.ignore_mdef_race |= 1<<val;
  3299. break;
  3300. case SP_PERFECT_HIT_RATE:
  3301. if(sd->state.lr_flag != 2 && sd->bonus.perfect_hit < val)
  3302. sd->bonus.perfect_hit = val;
  3303. break;
  3304. case SP_PERFECT_HIT_ADD_RATE:
  3305. if(sd->state.lr_flag != 2)
  3306. sd->bonus.perfect_hit_add += val;
  3307. break;
  3308. case SP_CRITICAL_RATE:
  3309. if(sd->state.lr_flag != 2)
  3310. sd->critical_rate+=val;
  3311. break;
  3312. case SP_DEF_RATIO_ATK_ELE:
  3313. PC_BONUS_CHK_ELEMENT(val,SP_DEF_RATIO_ATK_ELE);
  3314. if(!sd->state.lr_flag)
  3315. sd->right_weapon.def_ratio_atk_ele |= 1<<val;
  3316. else if(sd->state.lr_flag == 1)
  3317. sd->left_weapon.def_ratio_atk_ele |= 1<<val;
  3318. break;
  3319. case SP_DEF_RATIO_ATK_RACE:
  3320. PC_BONUS_CHK_RACE(val,SP_DEF_RATIO_ATK_RACE);
  3321. if(!sd->state.lr_flag)
  3322. sd->right_weapon.def_ratio_atk_race |= 1<<val;
  3323. else if(sd->state.lr_flag == 1)
  3324. sd->left_weapon.def_ratio_atk_race |= 1<<val;
  3325. break;
  3326. case SP_DEF_RATIO_ATK_CLASS:
  3327. PC_BONUS_CHK_CLASS(val,SP_DEF_RATIO_ATK_CLASS);
  3328. if(!sd->state.lr_flag)
  3329. sd->right_weapon.def_ratio_atk_class |= 1<<val;
  3330. else if(sd->state.lr_flag == 1)
  3331. sd->left_weapon.def_ratio_atk_class |= 1<<val;
  3332. break;
  3333. case SP_HIT_RATE:
  3334. if(sd->state.lr_flag != 2)
  3335. sd->hit_rate += val;
  3336. break;
  3337. case SP_FLEE_RATE:
  3338. if(sd->state.lr_flag != 2)
  3339. sd->flee_rate += val;
  3340. break;
  3341. case SP_FLEE2_RATE:
  3342. if(sd->state.lr_flag != 2)
  3343. sd->flee2_rate += val;
  3344. break;
  3345. case SP_DEF_RATE:
  3346. if(sd->state.lr_flag != 2)
  3347. sd->def_rate += val;
  3348. break;
  3349. case SP_DEF2_RATE:
  3350. if(sd->state.lr_flag != 2)
  3351. sd->def2_rate += val;
  3352. break;
  3353. case SP_MDEF_RATE:
  3354. if(sd->state.lr_flag != 2)
  3355. sd->mdef_rate += val;
  3356. break;
  3357. case SP_MDEF2_RATE:
  3358. if(sd->state.lr_flag != 2)
  3359. sd->mdef2_rate += val;
  3360. break;
  3361. case SP_PATK_RATE:
  3362. if (sd->state.lr_flag != 2)
  3363. sd->patk_rate += val;
  3364. break;
  3365. case SP_SMATK_RATE:
  3366. if (sd->state.lr_flag != 2)
  3367. sd->smatk_rate += val;
  3368. break;
  3369. case SP_RES_RATE:
  3370. if (sd->state.lr_flag != 2)
  3371. sd->res_rate += val;
  3372. break;
  3373. case SP_MRES_RATE:
  3374. if (sd->state.lr_flag != 2)
  3375. sd->mres_rate += val;
  3376. break;
  3377. case SP_HPLUS_RATE:
  3378. if (sd->state.lr_flag != 2)
  3379. sd->hplus_rate += val;
  3380. break;
  3381. case SP_CRATE_RATE:
  3382. if (sd->state.lr_flag != 2)
  3383. sd->crate_rate += val;
  3384. break;
  3385. case SP_RESTART_FULL_RECOVER:
  3386. if(sd->state.lr_flag != 2)
  3387. sd->special_state.restart_full_recover = 1;
  3388. break;
  3389. case SP_NO_CASTCANCEL:
  3390. if(sd->state.lr_flag != 2)
  3391. sd->special_state.no_castcancel = 1;
  3392. break;
  3393. case SP_NO_CASTCANCEL2:
  3394. if(sd->state.lr_flag != 2)
  3395. sd->special_state.no_castcancel2 = 1;
  3396. break;
  3397. case SP_NO_SIZEFIX:
  3398. if(sd->state.lr_flag != 2)
  3399. sd->special_state.no_sizefix = 1;
  3400. break;
  3401. case SP_NO_MAGIC_DAMAGE:
  3402. if(sd->state.lr_flag == 2)
  3403. break;
  3404. val+= sd->special_state.no_magic_damage;
  3405. sd->special_state.no_magic_damage = cap_value(val,0,100);
  3406. break;
  3407. case SP_NO_WEAPON_DAMAGE:
  3408. if(sd->state.lr_flag == 2)
  3409. break;
  3410. val+= sd->special_state.no_weapon_damage;
  3411. sd->special_state.no_weapon_damage = cap_value(val,0,100);
  3412. break;
  3413. case SP_NO_MISC_DAMAGE:
  3414. if(sd->state.lr_flag == 2)
  3415. break;
  3416. val+= sd->special_state.no_misc_damage;
  3417. sd->special_state.no_misc_damage = cap_value(val,0,100);
  3418. break;
  3419. case SP_NO_GEMSTONE:
  3420. if(sd->state.lr_flag != 2 && sd->special_state.no_gemstone != 2)
  3421. sd->special_state.no_gemstone = 1;
  3422. break;
  3423. case SP_INTRAVISION: // Maya Purple Card effect allowing to see Hiding/Cloaking people [DracoRPG]
  3424. if(sd->state.lr_flag != 2) {
  3425. sd->special_state.intravision = 1;
  3426. clif_status_load(&sd->bl, EFST_CLAIRVOYANCE, 1);
  3427. }
  3428. break;
  3429. case SP_NO_KNOCKBACK:
  3430. if(sd->state.lr_flag != 2)
  3431. sd->special_state.no_knockback = 1;
  3432. break;
  3433. case SP_SPLASH_RANGE:
  3434. if(sd->bonus.splash_range < val)
  3435. sd->bonus.splash_range = val;
  3436. break;
  3437. case SP_SPLASH_ADD_RANGE:
  3438. sd->bonus.splash_add_range += val;
  3439. break;
  3440. case SP_SHORT_WEAPON_DAMAGE_RETURN:
  3441. if(sd->state.lr_flag != 2)
  3442. sd->bonus.short_weapon_damage_return += val;
  3443. break;
  3444. case SP_LONG_WEAPON_DAMAGE_RETURN:
  3445. if(sd->state.lr_flag != 2)
  3446. sd->bonus.long_weapon_damage_return += val;
  3447. break;
  3448. case SP_MAGIC_DAMAGE_RETURN: //AppleGirl Was Here
  3449. if(sd->state.lr_flag != 2)
  3450. sd->bonus.magic_damage_return += val;
  3451. break;
  3452. case SP_REDUCE_DAMAGE_RETURN:
  3453. if (sd->state.lr_flag != 2)
  3454. sd->bonus.reduce_damage_return += val;
  3455. break;
  3456. case SP_ALL_STATS: // [Valaris]
  3457. if(sd->state.lr_flag!=2) {
  3458. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3459. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3460. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3461. sd->indexed_bonus.param_bonus[SP_INT-SP_STR]+=val;
  3462. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3463. sd->indexed_bonus.param_bonus[SP_LUK-SP_STR]+=val;
  3464. }
  3465. break;
  3466. case SP_ALL_TRAIT_STATS:
  3467. if (sd->state.lr_flag != 2) {
  3468. sd->indexed_bonus.param_bonus[PARAM_POW] += val;
  3469. sd->indexed_bonus.param_bonus[PARAM_STA] += val;
  3470. sd->indexed_bonus.param_bonus[PARAM_WIS] += val;
  3471. sd->indexed_bonus.param_bonus[PARAM_SPL] += val;
  3472. sd->indexed_bonus.param_bonus[PARAM_CON] += val;
  3473. sd->indexed_bonus.param_bonus[PARAM_CRT] += val;
  3474. }
  3475. break;
  3476. case SP_AGI_VIT: // [Valaris]
  3477. if(sd->state.lr_flag!=2) {
  3478. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3479. sd->indexed_bonus.param_bonus[SP_VIT-SP_STR]+=val;
  3480. }
  3481. break;
  3482. case SP_AGI_DEX_STR: // [Valaris]
  3483. if(sd->state.lr_flag!=2) {
  3484. sd->indexed_bonus.param_bonus[SP_AGI-SP_STR]+=val;
  3485. sd->indexed_bonus.param_bonus[SP_DEX-SP_STR]+=val;
  3486. sd->indexed_bonus.param_bonus[SP_STR-SP_STR]+=val;
  3487. }
  3488. break;
  3489. case SP_PERFECT_HIDE: // [Valaris]
  3490. if(sd->state.lr_flag!=2)
  3491. sd->special_state.perfect_hiding=1;
  3492. break;
  3493. case SP_UNBREAKABLE:
  3494. if(sd->state.lr_flag!=2)
  3495. sd->bonus.unbreakable += val;
  3496. break;
  3497. case SP_UNBREAKABLE_WEAPON:
  3498. if(sd->state.lr_flag != 2)
  3499. sd->bonus.unbreakable_equip |= EQP_WEAPON;
  3500. break;
  3501. case SP_UNBREAKABLE_ARMOR:
  3502. if(sd->state.lr_flag != 2)
  3503. sd->bonus.unbreakable_equip |= EQP_ARMOR;
  3504. break;
  3505. case SP_UNBREAKABLE_HELM:
  3506. if(sd->state.lr_flag != 2)
  3507. sd->bonus.unbreakable_equip |= EQP_HELM;
  3508. break;
  3509. case SP_UNBREAKABLE_SHIELD:
  3510. if(sd->state.lr_flag != 2)
  3511. sd->bonus.unbreakable_equip |= EQP_SHIELD;
  3512. break;
  3513. case SP_UNBREAKABLE_GARMENT:
  3514. if(sd->state.lr_flag != 2)
  3515. sd->bonus.unbreakable_equip |= EQP_GARMENT;
  3516. break;
  3517. case SP_UNBREAKABLE_SHOES:
  3518. if(sd->state.lr_flag != 2)
  3519. sd->bonus.unbreakable_equip |= EQP_SHOES;
  3520. break;
  3521. case SP_CLASSCHANGE: // [Valaris]
  3522. if(sd->state.lr_flag !=2)
  3523. sd->bonus.classchange=val;
  3524. break;
  3525. case SP_SHORT_ATK_RATE:
  3526. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3527. sd->bonus.short_attack_atk_rate+=val;
  3528. break;
  3529. case SP_LONG_ATK_RATE:
  3530. if(sd->state.lr_flag != 2) //[Lupus] it should stack, too. As any other cards rate bonuses
  3531. sd->bonus.long_attack_atk_rate+=val;
  3532. break;
  3533. case SP_BREAK_WEAPON_RATE:
  3534. if(sd->state.lr_flag != 2)
  3535. sd->bonus.break_weapon_rate+=val;
  3536. break;
  3537. case SP_BREAK_ARMOR_RATE:
  3538. if(sd->state.lr_flag != 2)
  3539. sd->bonus.break_armor_rate+=val;
  3540. break;
  3541. case SP_ADD_STEAL_RATE:
  3542. if(sd->state.lr_flag != 2)
  3543. sd->bonus.add_steal_rate+=val;
  3544. break;
  3545. case SP_DELAYRATE:
  3546. if(sd->state.lr_flag != 2)
  3547. sd->bonus.delayrate -= val;
  3548. break;
  3549. case SP_CRIT_ATK_RATE:
  3550. if(sd->state.lr_flag != 2)
  3551. sd->bonus.crit_atk_rate += val;
  3552. break;
  3553. case SP_CRIT_DEF_RATE:
  3554. if(sd->state.lr_flag != 2)
  3555. sd->bonus.crit_def_rate += val;
  3556. break;
  3557. case SP_NO_REGEN:
  3558. if(sd->state.lr_flag != 2)
  3559. sd->regen.state.block|=val;
  3560. break;
  3561. case SP_UNSTRIPABLE_WEAPON:
  3562. if(sd->state.lr_flag != 2)
  3563. sd->bonus.unstripable_equip |= EQP_WEAPON;
  3564. break;
  3565. case SP_UNSTRIPABLE:
  3566. case SP_UNSTRIPABLE_ARMOR:
  3567. if(sd->state.lr_flag != 2)
  3568. sd->bonus.unstripable_equip |= EQP_ARMOR;
  3569. break;
  3570. case SP_UNSTRIPABLE_HELM:
  3571. if(sd->state.lr_flag != 2)
  3572. sd->bonus.unstripable_equip |= EQP_HELM;
  3573. break;
  3574. case SP_UNSTRIPABLE_SHIELD:
  3575. if(sd->state.lr_flag != 2)
  3576. sd->bonus.unstripable_equip |= EQP_SHIELD;
  3577. break;
  3578. case SP_HP_DRAIN_VALUE: // bonus bHPDrainValue,n;
  3579. if(!sd->state.lr_flag) {
  3580. sd->right_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3581. sd->right_weapon.hp_drain_class[CLASS_BOSS] += val;
  3582. } else if(sd->state.lr_flag == 1) {
  3583. sd->left_weapon.hp_drain_class[CLASS_NORMAL] += val;
  3584. sd->left_weapon.hp_drain_class[CLASS_BOSS] += val;
  3585. }
  3586. break;
  3587. case SP_SP_DRAIN_VALUE: // bonus bSPDrainValue,n;
  3588. if(!sd->state.lr_flag) {
  3589. sd->right_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3590. sd->right_weapon.sp_drain_class[CLASS_BOSS] += val;
  3591. } else if(sd->state.lr_flag == 1) {
  3592. sd->left_weapon.sp_drain_class[CLASS_NORMAL] += val;
  3593. sd->left_weapon.sp_drain_class[CLASS_BOSS] += val;
  3594. }
  3595. break;
  3596. case SP_SP_GAIN_VALUE:
  3597. if(!sd->state.lr_flag)
  3598. sd->bonus.sp_gain_value += val;
  3599. break;
  3600. case SP_HP_GAIN_VALUE:
  3601. if(!sd->state.lr_flag)
  3602. sd->bonus.hp_gain_value += val;
  3603. break;
  3604. case SP_LONG_SP_GAIN_VALUE:
  3605. if(!sd->state.lr_flag)
  3606. sd->bonus.long_sp_gain_value += val;
  3607. case SP_LONG_HP_GAIN_VALUE:
  3608. if(!sd->state.lr_flag)
  3609. sd->bonus.long_hp_gain_value += val;
  3610. break;
  3611. case SP_MAGIC_SP_GAIN_VALUE:
  3612. if(!sd->state.lr_flag)
  3613. sd->bonus.magic_sp_gain_value += val;
  3614. break;
  3615. case SP_MAGIC_HP_GAIN_VALUE:
  3616. if(!sd->state.lr_flag)
  3617. sd->bonus.magic_hp_gain_value += val;
  3618. break;
  3619. case SP_ADD_HEAL_RATE:
  3620. if(sd->state.lr_flag != 2)
  3621. sd->bonus.add_heal_rate += val;
  3622. break;
  3623. case SP_ADD_HEAL2_RATE:
  3624. if(sd->state.lr_flag != 2)
  3625. sd->bonus.add_heal2_rate += val;
  3626. break;
  3627. case SP_ADD_ITEM_HEAL_RATE:
  3628. if(sd->state.lr_flag != 2)
  3629. sd->bonus.itemhealrate2 += val;
  3630. break;
  3631. case SP_EMATK:
  3632. if(sd->state.lr_flag != 2)
  3633. sd->bonus.ematk += val;
  3634. break;
  3635. case SP_ADD_VARIABLECAST:
  3636. if (sd->state.lr_flag != 2)
  3637. sd->bonus.add_varcast += val;
  3638. break;
  3639. #ifdef RENEWAL_CAST
  3640. case SP_FIXCASTRATE:
  3641. if(sd->state.lr_flag != 2)
  3642. sd->bonus.fixcastrate = min(sd->bonus.fixcastrate,val);
  3643. break;
  3644. case SP_ADD_FIXEDCAST:
  3645. if(sd->state.lr_flag != 2)
  3646. sd->bonus.add_fixcast += val;
  3647. break;
  3648. case SP_CASTRATE:
  3649. case SP_VARCASTRATE:
  3650. if(sd->state.lr_flag != 2)
  3651. sd->bonus.varcastrate -= val;
  3652. break;
  3653. #else
  3654. case SP_ADD_FIXEDCAST:
  3655. case SP_FIXCASTRATE:
  3656. //ShowWarning("pc_bonus: non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  3657. break;
  3658. case SP_VARCASTRATE:
  3659. case SP_CASTRATE:
  3660. if(sd->state.lr_flag != 2)
  3661. sd->castrate += val;
  3662. break;
  3663. #endif
  3664. case SP_ADDMAXWEIGHT:
  3665. if (sd->state.lr_flag != 2)
  3666. sd->add_max_weight += val;
  3667. break;
  3668. case SP_ABSORB_DMG_MAXHP: // bonus bAbsorbDmgMaxHP,n;
  3669. sd->bonus.absorb_dmg_maxhp = max(sd->bonus.absorb_dmg_maxhp, val);
  3670. break;
  3671. case SP_ABSORB_DMG_MAXHP2:
  3672. sd->bonus.absorb_dmg_maxhp2 = max(sd->bonus.absorb_dmg_maxhp2, val);
  3673. break;
  3674. case SP_CRITICAL_RANGEATK: // bonus bCriticalLong,n;
  3675. if (sd->state.lr_flag != 2)
  3676. sd->bonus.critical_rangeatk += val*10;
  3677. else
  3678. sd->bonus.arrow_cri += val*10;
  3679. break;
  3680. case SP_WEAPON_ATK_RATE:
  3681. if (sd->state.lr_flag != 2)
  3682. sd->bonus.weapon_atk_rate += val;
  3683. break;
  3684. case SP_WEAPON_MATK_RATE:
  3685. if (sd->state.lr_flag != 2)
  3686. sd->bonus.weapon_matk_rate += val;
  3687. break;
  3688. case SP_NO_MADO_FUEL:
  3689. if (sd->state.lr_flag != 2)
  3690. sd->special_state.no_mado_fuel = 1;
  3691. break;
  3692. case SP_NO_WALK_DELAY:
  3693. if (sd->state.lr_flag != 2)
  3694. sd->special_state.no_walk_delay = 1;
  3695. break;
  3696. case SP_ADD_ITEM_SPHEAL_RATE:
  3697. if(sd->state.lr_flag != 2)
  3698. sd->bonus.itemsphealrate2 += val;
  3699. break;
  3700. default:
  3701. if (current_equip_combo_pos > 0) {
  3702. 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);
  3703. }
  3704. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  3705. 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);
  3706. }
  3707. else {
  3708. ShowWarning("pc_bonus: unknown bonus type %d %d in unknown usage. Report this!\n", type, val);
  3709. }
  3710. break;
  3711. }
  3712. }
  3713. /*==========================================
  3714. * Player bonus (type) with args type2 and val, called trough bonus2 (npc)
  3715. * format: bonus2 bBonusName,type2,val;
  3716. * @param sd
  3717. * @param type Bonus type used by bBonusName
  3718. * @param type2
  3719. * @param val Value that usually for rate or fixed value
  3720. *------------------------------------------*/
  3721. void pc_bonus2(map_session_data *sd,int type,int type2,int val)
  3722. {
  3723. nullpo_retv(sd);
  3724. switch(type){
  3725. case SP_ADDELE: // bonus2 bAddEle,e,x;
  3726. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  3727. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3728. sd->right_weapon.addele[type2]+=val;
  3729. else if(sd->state.lr_flag == 1)
  3730. sd->left_weapon.addele[type2]+=val;
  3731. else if(sd->state.lr_flag == 2)
  3732. sd->indexed_bonus.arrow_addele[type2]+=val;
  3733. break;
  3734. case SP_ADDRACE: // bonus2 bAddRace,r,x;
  3735. PC_BONUS_CHK_RACE(type2,SP_ADDRACE);
  3736. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3737. sd->right_weapon.addrace[type2]+=val;
  3738. else if(sd->state.lr_flag == 1)
  3739. sd->left_weapon.addrace[type2]+=val;
  3740. else if(sd->state.lr_flag == 2)
  3741. sd->indexed_bonus.arrow_addrace[type2]+=val;
  3742. break;
  3743. case SP_ADDCLASS: // bonus2 bAddClass,c,x;
  3744. PC_BONUS_CHK_CLASS(type2,SP_ADDCLASS);
  3745. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3746. sd->right_weapon.addclass[type2]+=val;
  3747. else if(sd->state.lr_flag == 1)
  3748. sd->left_weapon.addclass[type2]+=val;
  3749. else if(sd->state.lr_flag == 2)
  3750. sd->indexed_bonus.arrow_addclass[type2]+=val;
  3751. break;
  3752. case SP_ADDSIZE: // bonus2 bAddSize,s,x;
  3753. PC_BONUS_CHK_SIZE(type2,SP_ADDSIZE);
  3754. if(!sd->state.lr_flag || sd->state.lr_flag == 3)
  3755. sd->right_weapon.addsize[type2]+=val;
  3756. else if(sd->state.lr_flag == 1)
  3757. sd->left_weapon.addsize[type2]+=val;
  3758. else if(sd->state.lr_flag == 2)
  3759. sd->indexed_bonus.arrow_addsize[type2]+=val;
  3760. break;
  3761. case SP_SUBELE: // bonus2 bSubEle,e,x;
  3762. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  3763. if(sd->state.lr_flag != 2)
  3764. sd->indexed_bonus.subele_script[type2] += val;
  3765. break;
  3766. case SP_SUBRACE: // bonus2 bSubRace,r,x;
  3767. PC_BONUS_CHK_RACE(type2,SP_SUBRACE);
  3768. if(sd->state.lr_flag != 2)
  3769. sd->indexed_bonus.subrace[type2]+=val;
  3770. break;
  3771. case SP_SUBCLASS: // bonus2 bSubClass,c,x;
  3772. PC_BONUS_CHK_CLASS(type2,SP_SUBCLASS);
  3773. if(sd->state.lr_flag != 2)
  3774. sd->indexed_bonus.subclass[type2]+=val;
  3775. break;
  3776. case SP_ADDEFF: // bonus2 bAddEff,eff,n;
  3777. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  3778. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, 0, 0);
  3779. break;
  3780. case SP_ADDEFF2: // bonus2 bAddEff2,eff,n;
  3781. PC_BONUS_CHK_SC(type2,SP_ADDEFF2);
  3782. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? val : 0, sd->state.lr_flag == 2 ? val : 0, ATF_SELF, 0);
  3783. break;
  3784. case SP_RESEFF: // bonus2 bResEff,eff,n;
  3785. if(sd->state.lr_flag == 2)
  3786. break;
  3787. if (sd->reseff.size() == MAX_PC_BONUS) {
  3788. ShowWarning("pc_bonus2: SP_RESEFF: Reached max (%d) number of resistance bonuses per character!\n", MAX_PC_BONUS);
  3789. break;
  3790. }
  3791. pc_bonus_itembonus(sd->reseff, type2, val, true);
  3792. break;
  3793. case SP_MAGIC_ADDELE: // bonus2 bMagicAddEle,e,x;
  3794. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ADDELE);
  3795. if(sd->state.lr_flag != 2)
  3796. sd->indexed_bonus.magic_addele_script[type2] += val;
  3797. break;
  3798. case SP_MAGIC_ADDRACE: // bonus2 bMagicAddRace,r,x;
  3799. PC_BONUS_CHK_RACE(type2,SP_MAGIC_ADDRACE);
  3800. if(sd->state.lr_flag != 2)
  3801. sd->indexed_bonus.magic_addrace[type2]+=val;
  3802. break;
  3803. case SP_MAGIC_ADDCLASS: // bonus2 bMagicAddClass,c,x;
  3804. PC_BONUS_CHK_CLASS(type2,SP_MAGIC_ADDCLASS);
  3805. if(sd->state.lr_flag != 2)
  3806. sd->indexed_bonus.magic_addclass[type2]+=val;
  3807. break;
  3808. case SP_MAGIC_ADDSIZE: // bonus2 bMagicAddSize,s,x;
  3809. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_ADDSIZE);
  3810. if(sd->state.lr_flag != 2)
  3811. sd->indexed_bonus.magic_addsize[type2]+=val;
  3812. break;
  3813. case SP_MAGIC_ATK_ELE: // bonus2 bMagicAtkEle,e,x;
  3814. PC_BONUS_CHK_ELEMENT(type2,SP_MAGIC_ATK_ELE);
  3815. if(sd->state.lr_flag != 2)
  3816. sd->indexed_bonus.magic_atk_ele[type2]+=val;
  3817. break;
  3818. case SP_ADD_DAMAGE_CLASS: { // bonus2 bAddDamageClass,mid,x;
  3819. struct weapon_data *wd = (sd->state.lr_flag ? &sd->left_weapon : &sd->right_weapon);
  3820. if (wd->add_dmg.size() == MAX_PC_BONUS) {
  3821. ShowWarning("pc_bonus2: SP_ADD_DAMAGE_CLASS: Reached max (%d) number of add class damage bonuses per character!\n", MAX_PC_BONUS);
  3822. break;
  3823. }
  3824. pc_bonus_itembonus(wd->add_dmg, type2, val, false);
  3825. }
  3826. break;
  3827. case SP_ADD_MAGIC_DAMAGE_CLASS: // bonus2 bAddMagicDamageClass,mid,x;
  3828. if(sd->state.lr_flag == 2)
  3829. break;
  3830. if (sd->add_mdmg.size() == MAX_PC_BONUS) {
  3831. ShowWarning("pc_bonus2: SP_ADD_MAGIC_DAMAGE_CLASS: Reached max (%d) number of add class magic dmg bonuses per character!\n", MAX_PC_BONUS);
  3832. break;
  3833. }
  3834. pc_bonus_itembonus(sd->add_mdmg, type2, val, false);
  3835. break;
  3836. case SP_ADD_DEF_MONSTER: // bonus2 bAddDefMonster,mid,x;
  3837. if(sd->state.lr_flag == 2)
  3838. break;
  3839. if (sd->add_def.size() == MAX_PC_BONUS) {
  3840. ShowWarning("pc_bonus2: SP_ADD_DEF_MONSTER: Reached max (%d) number of add class def bonuses per character!\n", MAX_PC_BONUS);
  3841. break;
  3842. }
  3843. pc_bonus_itembonus(sd->add_def, type2, val, false);
  3844. break;
  3845. case SP_ADD_MDEF_MONSTER: // bonus2 bAddMDefMonster,mid,x;
  3846. if(sd->state.lr_flag == 2)
  3847. break;
  3848. if (sd->add_mdef.size() == MAX_PC_BONUS) {
  3849. ShowWarning("pc_bonus2: SP_ADD_MDEF_MONSTER: Reached max (%d) number of add class mdef bonuses per character!\n", MAX_PC_BONUS);
  3850. break;
  3851. }
  3852. pc_bonus_itembonus(sd->add_mdef, type2, val, false);
  3853. break;
  3854. case SP_HP_DRAIN_RATE: // bonus2 bHPDrainRate,x,n;
  3855. if(!sd->state.lr_flag) {
  3856. sd->right_weapon.hp_drain_rate.rate += type2;
  3857. sd->right_weapon.hp_drain_rate.per += val;
  3858. }
  3859. else if(sd->state.lr_flag == 1) {
  3860. sd->left_weapon.hp_drain_rate.rate += type2;
  3861. sd->left_weapon.hp_drain_rate.per += val;
  3862. }
  3863. break;
  3864. case SP_SP_DRAIN_RATE: // bonus2 bSPDrainRate,x,n;
  3865. if(!sd->state.lr_flag) {
  3866. sd->right_weapon.sp_drain_rate.rate += type2;
  3867. sd->right_weapon.sp_drain_rate.per += val;
  3868. }
  3869. else if(sd->state.lr_flag == 1) {
  3870. sd->left_weapon.sp_drain_rate.rate += type2;
  3871. sd->left_weapon.sp_drain_rate.per += val;
  3872. }
  3873. break;
  3874. case SP_SP_VANISH_RATE: // bonus2 bSPVanishRate,x,n;
  3875. if(sd->state.lr_flag != 2) {
  3876. pc_bonus_addvanish(sd->sp_vanish, type2, val, BF_NORMAL);
  3877. }
  3878. break;
  3879. case SP_HP_VANISH_RATE: // bonus2 bHPVanishRate,x,n;
  3880. if(sd->state.lr_flag != 2) {
  3881. pc_bonus_addvanish(sd->hp_vanish, type2, val, BF_NORMAL);
  3882. }
  3883. break;
  3884. case SP_GET_ZENY_NUM: // bonus2 bGetZenyNum,x,n;
  3885. if(sd->state.lr_flag != 2 && sd->bonus.get_zeny_rate < val) {
  3886. sd->bonus.get_zeny_rate = val;
  3887. sd->bonus.get_zeny_num = type2;
  3888. }
  3889. break;
  3890. case SP_ADD_GET_ZENY_NUM: // bonus2 bAddGetZenyNum,x,n;
  3891. if(sd->state.lr_flag != 2) {
  3892. sd->bonus.get_zeny_rate += val;
  3893. sd->bonus.get_zeny_num += type2;
  3894. }
  3895. break;
  3896. case SP_WEAPON_COMA_ELE: // bonus2 bWeaponComaEle,e,n;
  3897. PC_BONUS_CHK_ELEMENT(type2,SP_WEAPON_COMA_ELE);
  3898. if(sd->state.lr_flag == 2)
  3899. break;
  3900. sd->indexed_bonus.weapon_coma_ele[type2] += val;
  3901. sd->special_state.bonus_coma = 1;
  3902. break;
  3903. case SP_WEAPON_COMA_RACE: // bonus2 bWeaponComaRace,r,n;
  3904. PC_BONUS_CHK_RACE(type2,SP_WEAPON_COMA_RACE);
  3905. if(sd->state.lr_flag == 2)
  3906. break;
  3907. sd->indexed_bonus.weapon_coma_race[type2] += val;
  3908. sd->special_state.bonus_coma = 1;
  3909. break;
  3910. case SP_WEAPON_COMA_CLASS: // bonus2 bWeaponComaClass,c,n;
  3911. PC_BONUS_CHK_CLASS(type2,SP_WEAPON_COMA_CLASS);
  3912. if(sd->state.lr_flag == 2)
  3913. break;
  3914. sd->indexed_bonus.weapon_coma_class[type2] += val;
  3915. sd->special_state.bonus_coma = 1;
  3916. break;
  3917. case SP_WEAPON_ATK: // bonus2 bWeaponAtk,w,n;
  3918. if(sd->state.lr_flag != 2)
  3919. sd->indexed_bonus.weapon_atk[type2]+=val;
  3920. break;
  3921. case SP_WEAPON_DAMAGE_RATE: // bonus2 bWeaponDamageRate,w,n;
  3922. if(sd->state.lr_flag != 2)
  3923. sd->indexed_bonus.weapon_damage_rate[type2]+=val;
  3924. break;
  3925. case SP_CRITICAL_ADDRACE: // bonus2 bCriticalAddRace,r,n;
  3926. PC_BONUS_CHK_RACE(type2,SP_CRITICAL_ADDRACE);
  3927. if(sd->state.lr_flag != 2)
  3928. sd->indexed_bonus.critaddrace[type2] += val*10;
  3929. break;
  3930. case SP_ADDEFF_WHENHIT: // bonus2 bAddEffWhenHit,eff,n;
  3931. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  3932. if(sd->state.lr_flag != 2)
  3933. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, val, 0, 0, 0);
  3934. break;
  3935. case SP_SKILL_ATK: // bonus2 bSkillAtk,sk,n;
  3936. if(sd->state.lr_flag == 2)
  3937. break;
  3938. if (sd->skillatk.size() == MAX_PC_BONUS) {
  3939. 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);
  3940. break;
  3941. }
  3942. pc_bonus_itembonus(sd->skillatk, type2, val, false);
  3943. break;
  3944. case SP_SKILL_HEAL: // bonus2 bSkillHeal,sk,n;
  3945. if(sd->state.lr_flag == 2)
  3946. break;
  3947. if (sd->skillheal.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3948. 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);
  3949. break;
  3950. }
  3951. pc_bonus_itembonus(sd->skillheal, type2, val, false);
  3952. break;
  3953. case SP_SKILL_HEAL2: // bonus2 bSkillHeal2,sk,n;
  3954. if(sd->state.lr_flag == 2)
  3955. break;
  3956. if (sd->skillheal2.size() == MAX_PC_BONUS) { // Better mention this so the array length can be updated. [Skotlex]
  3957. 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);
  3958. break;
  3959. }
  3960. pc_bonus_itembonus(sd->skillheal2, type2, val, false);
  3961. break;
  3962. case SP_ADD_SKILL_BLOW: // bonus2 bAddSkillBlow,sk,n;
  3963. if(sd->state.lr_flag == 2)
  3964. break;
  3965. if (sd->skillblown.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  3966. 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);
  3967. break;
  3968. }
  3969. pc_bonus_itembonus(sd->skillblown, type2, val, false);
  3970. break;
  3971. case SP_HP_LOSS_RATE: // bonus2 bHPLossRate,n,t;
  3972. if(sd->state.lr_flag != 2) {
  3973. sd->hp_loss.value = type2;
  3974. sd->hp_loss.rate = val;
  3975. }
  3976. break;
  3977. case SP_HP_REGEN_RATE: // bonus2 bHPRegenRate,n,t;
  3978. if(sd->state.lr_flag != 2) {
  3979. sd->hp_regen.value = type2;
  3980. sd->hp_regen.rate = val;
  3981. }
  3982. break;
  3983. case SP_REGEN_PERCENT_HP: // bonus2 bRegenPercentHP,n,t;
  3984. if (sd->state.lr_flag != 2) {
  3985. sd->percent_hp_regen.value = type2;
  3986. sd->percent_hp_regen.rate = val;
  3987. }
  3988. break;
  3989. case SP_REGEN_PERCENT_SP: // bonus2 bRegenPercentSP,n,t;
  3990. if (sd->state.lr_flag != 2) {
  3991. sd->percent_sp_regen.value = type2;
  3992. sd->percent_sp_regen.rate = val;
  3993. }
  3994. break;
  3995. case SP_ADDRACE2: // bonus2 bAddRace2,mr,x;
  3996. PC_BONUS_CHK_RACE2(type2,SP_ADDRACE2);
  3997. if(sd->state.lr_flag != 2)
  3998. sd->right_weapon.addrace2[type2] += val;
  3999. else
  4000. sd->left_weapon.addrace2[type2] += val;
  4001. break;
  4002. case SP_SUBSIZE: // bonus2 bSubSize,s,x;
  4003. PC_BONUS_CHK_SIZE(type2,SP_SUBSIZE);
  4004. if(sd->state.lr_flag != 2)
  4005. sd->indexed_bonus.subsize[type2]+=val;
  4006. break;
  4007. case SP_WEAPON_SUBSIZE: // bonus2 bWeaponSubSize,s,x;
  4008. PC_BONUS_CHK_SIZE(type2, SP_WEAPON_SUBSIZE);
  4009. if (sd->state.lr_flag != 2)
  4010. sd->indexed_bonus.weapon_subsize[type2] += val;
  4011. break;
  4012. case SP_MAGIC_SUBSIZE: // bonus2 bMagicSubSize,s,x;
  4013. PC_BONUS_CHK_SIZE(type2,SP_MAGIC_SUBSIZE);
  4014. if(sd->state.lr_flag != 2)
  4015. sd->indexed_bonus.magic_subsize[type2]+=val;
  4016. break;
  4017. case SP_SUBRACE2: // bonus2 bSubRace2,mr,x;
  4018. PC_BONUS_CHK_RACE2(type2,SP_SUBRACE2);
  4019. if(sd->state.lr_flag != 2)
  4020. sd->indexed_bonus.subrace2[type2]+=val;
  4021. break;
  4022. case SP_ADD_ITEM_HEAL_RATE: // bonus2 bAddItemHealRate,iid,n;
  4023. if(sd->state.lr_flag == 2)
  4024. break;
  4025. if( !item_db.exists( type2 ) ){
  4026. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE Invalid item with id %d\n", type2);
  4027. break;
  4028. }
  4029. if (sd->itemhealrate.size() == MAX_PC_BONUS) {
  4030. ShowWarning("pc_bonus2: SP_ADD_ITEM_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4031. break;
  4032. }
  4033. pc_bonus_itembonus(sd->itemhealrate, type2, val, false);
  4034. break;
  4035. case SP_ADD_ITEMGROUP_HEAL_RATE: // bonus2 bAddItemGroupHealRate,ig,n;
  4036. if (sd->state.lr_flag == 2)
  4037. break;
  4038. if (!type2 || !itemdb_group.exists(type2)) {
  4039. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Invalid item group with id %d\n", type2);
  4040. break;
  4041. }
  4042. if (sd->itemgrouphealrate.size() == MAX_PC_BONUS) {
  4043. ShowWarning("pc_bonus2: SP_ADD_ITEMGROUP_HEAL_RATE: Reached max (%d) number of item heal bonuses per character!\n", MAX_PC_BONUS);
  4044. break;
  4045. }
  4046. pc_bonus_itembonus(sd->itemgrouphealrate, type2, val, false);
  4047. break;
  4048. case SP_EXP_ADDRACE: // bonus2 bExpAddRace,r,x;
  4049. PC_BONUS_CHK_RACE(type2,SP_EXP_ADDRACE);
  4050. if(sd->state.lr_flag != 2)
  4051. sd->indexed_bonus.expaddrace[type2]+=val;
  4052. break;
  4053. case SP_EXP_ADDCLASS: // bonus2 bExpAddClass,c,x;
  4054. PC_BONUS_CHK_CLASS(type2,SP_EXP_ADDCLASS);
  4055. if(sd->state.lr_flag != 2)
  4056. sd->indexed_bonus.expaddclass[type2]+=val;
  4057. break;
  4058. case SP_SP_GAIN_RACE: // bonus2 bSPGainRace,r,n;
  4059. PC_BONUS_CHK_RACE(type2,SP_SP_GAIN_RACE);
  4060. if(sd->state.lr_flag != 2)
  4061. sd->indexed_bonus.sp_gain_race[type2]+=val;
  4062. break;
  4063. case SP_ADD_MONSTER_DROP_ITEM: // bonus2 bAddMonsterDropItem,iid,n;
  4064. if (sd->state.lr_flag != 2)
  4065. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_ALL, RC_NONE_, val);
  4066. break;
  4067. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus2 bAddMonsterDropItemGroup,ig,n;
  4068. if (sd->state.lr_flag != 2)
  4069. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_ALL, RC_NONE_, val);
  4070. break;
  4071. case SP_SP_LOSS_RATE: // bonus2 bSPLossRate,n,t;
  4072. if(sd->state.lr_flag != 2) {
  4073. sd->sp_loss.value = type2;
  4074. sd->sp_loss.rate = val;
  4075. }
  4076. break;
  4077. case SP_SP_REGEN_RATE: // bonus2 bSPRegenRate,n,t;
  4078. if(sd->state.lr_flag != 2) {
  4079. sd->sp_regen.value = type2;
  4080. sd->sp_regen.rate = val;
  4081. }
  4082. break;
  4083. case SP_HP_DRAIN_VALUE_RACE: // bonus2 bHPDrainValueRace,r,n;
  4084. PC_BONUS_CHK_RACE(type2,SP_HP_DRAIN_VALUE_RACE);
  4085. if(!sd->state.lr_flag) {
  4086. sd->right_weapon.hp_drain_race[type2] += val;
  4087. }
  4088. else if(sd->state.lr_flag == 1) {
  4089. sd->left_weapon.hp_drain_race[type2] += val;
  4090. }
  4091. break;
  4092. case SP_SP_DRAIN_VALUE_RACE: // bonus2 bSPDrainValueRace,r,n;
  4093. PC_BONUS_CHK_RACE(type2,SP_SP_DRAIN_VALUE_RACE);
  4094. if(!sd->state.lr_flag) {
  4095. sd->right_weapon.sp_drain_race[type2] += val;
  4096. }
  4097. else if(sd->state.lr_flag == 1) {
  4098. sd->left_weapon.sp_drain_race[type2] += val;
  4099. }
  4100. break;
  4101. case SP_HP_DRAIN_VALUE_CLASS: // bonus2 bHPDrainValueClass,c,n;
  4102. PC_BONUS_CHK_CLASS(type2,SP_HP_DRAIN_VALUE_CLASS);
  4103. if(!sd->state.lr_flag) {
  4104. sd->right_weapon.hp_drain_class[type2] += val;
  4105. }
  4106. else if(sd->state.lr_flag == 1) {
  4107. sd->left_weapon.hp_drain_class[type2] += val;
  4108. }
  4109. break;
  4110. case SP_SP_DRAIN_VALUE_CLASS: // bonus2 bSPDrainValueClass,c,n;
  4111. PC_BONUS_CHK_CLASS(type2,SP_SP_DRAIN_VALUE_CLASS);
  4112. if(!sd->state.lr_flag) {
  4113. sd->right_weapon.sp_drain_class[type2] += val;
  4114. }
  4115. else if(sd->state.lr_flag == 1) {
  4116. sd->left_weapon.sp_drain_class[type2] += val;
  4117. }
  4118. break;
  4119. case SP_IGNORE_MDEF_RACE_RATE: // bonus2 bIgnoreMdefRaceRate,r,n;
  4120. PC_BONUS_CHK_RACE(type2,SP_IGNORE_MDEF_RACE_RATE);
  4121. if(sd->state.lr_flag != 2)
  4122. sd->indexed_bonus.ignore_mdef_by_race[type2] += val;
  4123. break;
  4124. case SP_IGNORE_MDEF_CLASS_RATE: // bonus2 bIgnoreMdefClassRate,c,n;
  4125. PC_BONUS_CHK_CLASS(type2,SP_IGNORE_MDEF_CLASS_RATE);
  4126. if(sd->state.lr_flag != 2)
  4127. sd->indexed_bonus.ignore_mdef_by_class[type2] += val;
  4128. break;
  4129. case SP_IGNORE_DEF_RACE_RATE: // bonus2 bIgnoreDefRaceRate,r,n;
  4130. PC_BONUS_CHK_RACE(type2,SP_IGNORE_DEF_RACE_RATE);
  4131. if(sd->state.lr_flag != 2)
  4132. sd->indexed_bonus.ignore_def_by_race[type2] += val;
  4133. break;
  4134. case SP_IGNORE_DEF_CLASS_RATE: // bonus2 bIgnoreDefClassRate,r,n;
  4135. PC_BONUS_CHK_CLASS(type2, SP_IGNORE_DEF_CLASS_RATE);
  4136. if (sd->state.lr_flag != 2)
  4137. sd->indexed_bonus.ignore_def_by_class[type2] += val;
  4138. break;
  4139. case SP_SKILL_USE_SP_RATE: // bonus2 bSkillUseSPrate,sk,n;
  4140. if(sd->state.lr_flag == 2)
  4141. break;
  4142. if (sd->skillusesprate.size() == MAX_PC_BONUS) {
  4143. 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);
  4144. break;
  4145. }
  4146. pc_bonus_itembonus(sd->skillusesprate, type2, val, true);
  4147. break;
  4148. case SP_SKILL_DELAY:
  4149. if(sd->state.lr_flag == 2)
  4150. break;
  4151. if (sd->skilldelay.size() == MAX_PC_BONUS) {
  4152. 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);
  4153. break;
  4154. }
  4155. pc_bonus_itembonus(sd->skilldelay, type2, val, false);
  4156. break;
  4157. case SP_SKILL_COOLDOWN: // bonus2 bSkillCooldown,sk,t;
  4158. if(sd->state.lr_flag == 2)
  4159. break;
  4160. if (sd->skillcooldown.size() == MAX_PC_BONUS) {
  4161. 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);
  4162. break;
  4163. }
  4164. pc_bonus_itembonus(sd->skillcooldown, type2, val, false);
  4165. break;
  4166. case SP_SKILL_VARIABLECAST: // bonus2 bSkillVariableCast,sk,t;
  4167. if (sd->state.lr_flag == 2)
  4168. break;
  4169. if (sd->skillvarcast.size() == MAX_PC_BONUS) {
  4170. 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);
  4171. break;
  4172. }
  4173. pc_bonus_itembonus(sd->skillvarcast, type2, val, false);
  4174. break;
  4175. #ifdef RENEWAL_CAST
  4176. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4177. if(sd->state.lr_flag == 2)
  4178. break;
  4179. if (sd->skillfixcast.size() == MAX_PC_BONUS) {
  4180. 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);
  4181. break;
  4182. }
  4183. pc_bonus_itembonus(sd->skillfixcast, type2, val, false);
  4184. break;
  4185. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4186. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4187. if(sd->state.lr_flag == 2)
  4188. break;
  4189. if (sd->skillcastrate.size() == MAX_PC_BONUS) {
  4190. ShowWarning("pc_bonus2: SP_VARCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4191. break;
  4192. }
  4193. pc_bonus_itembonus(sd->skillcastrate, type2, -val, true); // Send inversed value here
  4194. break;
  4195. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4196. if(sd->state.lr_flag == 2)
  4197. break;
  4198. if (sd->skillfixcastrate.size() == MAX_PC_BONUS) {
  4199. ShowWarning("pc_bonus2: SP_FIXCASTRATE: Reached max (%d) number of skills per character, bonus skill %d (%d%%) lost.\n", MAX_PC_BONUS, type2, val);
  4200. break;
  4201. }
  4202. pc_bonus_itembonus(sd->skillfixcastrate, type2, -val, true); // Send inversed value here
  4203. break;
  4204. #else
  4205. case SP_SKILL_FIXEDCAST: // bonus2 bSkillFixedCast,sk,t;
  4206. case SP_FIXCASTRATE: // bonus2 bFixedCastrate,sk,n;
  4207. //ShowWarning("pc_bonus2: Non-RENEWAL_CAST doesn't support this bonus %d.\n", type);
  4208. break;
  4209. case SP_VARCASTRATE: // bonus2 bVariableCastrate,sk,n;
  4210. case SP_CASTRATE: // bonus2 bCastrate,sk,n;
  4211. if(sd->state.lr_flag == 2)
  4212. break;
  4213. if (sd->skillcastrate.size() == MAX_PC_BONUS) { //Better mention this so the array length can be updated. [Skotlex]
  4214. 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);
  4215. break;
  4216. }
  4217. pc_bonus_itembonus(sd->skillcastrate, type2, val, true);
  4218. break;
  4219. #endif
  4220. case SP_SKILL_USE_SP: // bonus2 bSkillUseSP,sk,n;
  4221. if(sd->state.lr_flag == 2)
  4222. break;
  4223. if (sd->skillusesp.size() == MAX_PC_BONUS) {
  4224. 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);
  4225. break;
  4226. }
  4227. pc_bonus_itembonus(sd->skillusesp, type2, val, false);
  4228. break;
  4229. case SP_SUB_SKILL: // bonus2 bSubSkill,sk,n;
  4230. if (sd->subskill.size() == MAX_PC_BONUS) {
  4231. 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);
  4232. break;
  4233. }
  4234. pc_bonus_itembonus(sd->subskill, type2, val, false);
  4235. break;
  4236. case SP_SUBDEF_ELE: // bonus2 bSubDefEle,e,x;
  4237. PC_BONUS_CHK_ELEMENT(type2,SP_SUBDEF_ELE);
  4238. sd->indexed_bonus.subdefele[type2] += val;
  4239. break;
  4240. case SP_COMA_CLASS: // bonus2 bComaClass,c,n;
  4241. PC_BONUS_CHK_CLASS(type2,SP_COMA_CLASS);
  4242. sd->indexed_bonus.coma_class[type2] += val;
  4243. sd->special_state.bonus_coma = 1;
  4244. break;
  4245. case SP_COMA_RACE: // bonus2 bComaRace,r,n;
  4246. PC_BONUS_CHK_RACE(type2,SP_COMA_RACE);
  4247. sd->indexed_bonus.coma_race[type2] += val;
  4248. sd->special_state.bonus_coma = 1;
  4249. break;
  4250. case SP_MAGIC_ADDRACE2: // bonus2 bMagicAddRace2,mr,n;
  4251. PC_BONUS_CHK_RACE2(type2, SP_MAGIC_ADDRACE2);
  4252. if(sd->state.lr_flag != 2)
  4253. sd->indexed_bonus.magic_addrace2[type2] += val;
  4254. break;
  4255. case SP_IGNORE_MDEF_RACE2_RATE: //bonus2 bIgnoreMdefRace2Rate,mr,n;
  4256. PC_BONUS_CHK_RACE2(type2, SP_IGNORE_MDEF_RACE2);
  4257. if (sd->state.lr_flag != 2)
  4258. sd->indexed_bonus.ignore_mdef_by_race2[type2] += val;
  4259. break;
  4260. case SP_DROP_ADDRACE: // bonus2 bDropAddRace,r,x;
  4261. PC_BONUS_CHK_RACE(type2, SP_DROP_ADDRACE);
  4262. if (sd->state.lr_flag != 2)
  4263. sd->indexed_bonus.dropaddrace[type2] += val;
  4264. break;
  4265. case SP_DROP_ADDCLASS: // bonus2 bDropAddClass,c,x;
  4266. PC_BONUS_CHK_CLASS(type2, SP_DROP_ADDCLASS);
  4267. if (sd->state.lr_flag != 2)
  4268. sd->indexed_bonus.dropaddclass[type2] += val;
  4269. break;
  4270. case SP_MAGIC_SUBDEF_ELE: // bonus2 bMagicSubDefEle,e,x;
  4271. PC_BONUS_CHK_ELEMENT(type2, SP_MAGIC_SUBDEF_ELE);
  4272. sd->indexed_bonus.magic_subdefele[type2] += val;
  4273. break;
  4274. case SP_ADD_ITEM_SPHEAL_RATE: // bonus2 bAddItemSPHealRate,iid,n;
  4275. if( sd->state.lr_flag == 2 ){
  4276. break;
  4277. }
  4278. if( !item_db.exists( type2 ) ){
  4279. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE Invalid item with id %d\n", type2 );
  4280. break;
  4281. }
  4282. if( sd->itemsphealrate.size() == MAX_PC_BONUS ){
  4283. ShowWarning( "pc_bonus2: SP_ADD_ITEM_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4284. break;
  4285. }
  4286. pc_bonus_itembonus( sd->itemsphealrate, type2, val, false );
  4287. break;
  4288. case SP_ADD_ITEMGROUP_SPHEAL_RATE: // bonus2 bAddItemGroupSPHealRate,ig,n;
  4289. if( sd->state.lr_flag == 2 ){
  4290. break;
  4291. }
  4292. if( !type2 || !itemdb_group.exists( type2 ) ){
  4293. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Invalid item group with id %d\n", type2 );
  4294. break;
  4295. }
  4296. if( sd->itemgroupsphealrate.size() == MAX_PC_BONUS ){
  4297. ShowWarning( "pc_bonus2: SP_ADD_ITEMGROUP_SPHEAL_RATE: Reached max (%d) number of item SP heal bonuses per character!\n", MAX_PC_BONUS );
  4298. break;
  4299. }
  4300. pc_bonus_itembonus( sd->itemgroupsphealrate, type2, val, false );
  4301. break;
  4302. default:
  4303. if (current_equip_combo_pos > 0) {
  4304. 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);
  4305. }
  4306. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4307. 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);
  4308. }
  4309. else {
  4310. ShowWarning("pc_bonus2: unknown bonus type %d %d %d in unknown usage. Report this!\n", type, type2, val);
  4311. }
  4312. break;
  4313. }
  4314. }
  4315. /**
  4316. * Gives item bonus to player for format: bonus3 bBonusName,type2,type3,val;
  4317. * @param sd
  4318. * @param type Bonus type used by bBonusName
  4319. * @param type2
  4320. * @param type3
  4321. * @param val Value that usually for rate or fixed value
  4322. */
  4323. void pc_bonus3(map_session_data *sd,int type,int type2,int type3,int val)
  4324. {
  4325. nullpo_retv(sd);
  4326. switch(type){
  4327. case SP_ADD_MONSTER_DROP_ITEM: // bonus3 bAddMonsterDropItem,iid,r,n;
  4328. if(sd->state.lr_flag != 2)
  4329. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, type3, val);
  4330. break;
  4331. case SP_ADD_MONSTER_ID_DROP_ITEM: // bonus3 bAddMonsterIdDropItem,iid,mid,n;
  4332. if(sd->state.lr_flag != 2)
  4333. pc_bonus_item_drop(sd->add_drop, type2, 0, CLASS_NONE, -type3, val);
  4334. break;
  4335. case SP_ADD_CLASS_DROP_ITEM: // bonus3 bAddClassDropItem,iid,c,n;
  4336. if(sd->state.lr_flag != 2)
  4337. pc_bonus_item_drop(sd->add_drop, type2, 0, type3, RC_NONE_, val);
  4338. break;
  4339. case SP_AUTOSPELL: // bonus3 bAutoSpell,sk,y,n;
  4340. if(sd->state.lr_flag != 2)
  4341. {
  4342. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4343. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4344. pc_bonus_autospell(sd->autospell, type2, type3, val, 0, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4345. }
  4346. break;
  4347. case SP_AUTOSPELL_WHENHIT: // bonus3 bAutoSpellWhenHit,sk,y,n;
  4348. if(sd->state.lr_flag != 2)
  4349. {
  4350. int target = skill_get_inf(type2); //Support or Self (non-auto-target) skills should pick self.
  4351. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type2, INF2_NOTARGETSELF));
  4352. pc_bonus_autospell(sd->autospell2, type2, type3, val, BF_NORMAL|BF_SKILL, current_equip_card_id, target ? AUTOSPELL_FORCE_SELF : AUTOSPELL_FORCE_TARGET);
  4353. }
  4354. break;
  4355. case SP_ADD_MONSTER_DROP_ITEMGROUP: // bonus3 bAddMonsterDropItemGroup,ig,r,n;
  4356. if (sd->state.lr_flag != 2)
  4357. pc_bonus_item_drop(sd->add_drop, 0, type2, CLASS_NONE, type3, val);
  4358. break;
  4359. case SP_ADD_CLASS_DROP_ITEMGROUP: // bonus3 bAddClassDropItemGroup,ig,c,n;
  4360. if (sd->state.lr_flag != 2)
  4361. pc_bonus_item_drop(sd->add_drop, 0, type2, type3, RC_NONE_, val);
  4362. break;
  4363. case SP_ADDEFF: // bonus3 bAddEff,eff,n,y;
  4364. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4365. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, val, 0);
  4366. break;
  4367. case SP_ADDEFF_WHENHIT: // bonus3 bAddEffWhenHit,eff,n,y;
  4368. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4369. if(sd->state.lr_flag != 2)
  4370. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, val, 0);
  4371. break;
  4372. case SP_ADDEFF_ONSKILL: // bonus3 bAddEffOnSkill,sk,eff,n;
  4373. PC_BONUS_CHK_SC(type2,SP_ADDEFF_ONSKILL);
  4374. if( sd->state.lr_flag != 2 )
  4375. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, val, type2, ATF_TARGET, 0);
  4376. break;
  4377. case SP_ADDELE: // bonus3 bAddEle,e,x,bf;
  4378. PC_BONUS_CHK_ELEMENT(type2,SP_ADDELE);
  4379. if (sd->state.lr_flag != 2)
  4380. pc_bonus_addele(sd, (unsigned char)type2, type3, val);
  4381. break;
  4382. case SP_SUBELE: // bonus3 bSubEle,e,x,bf;
  4383. PC_BONUS_CHK_ELEMENT(type2,SP_SUBELE);
  4384. if (sd->state.lr_flag != 2)
  4385. pc_bonus_subele(sd, (unsigned char)type2, type3, val);
  4386. break;
  4387. case SP_SUBRACE: // bonus3 bSubRace,r,x,bf;
  4388. PC_BONUS_CHK_RACE(type2, SP_SUBRACE);
  4389. if (sd->state.lr_flag != 2)
  4390. pc_bonus_subrace(sd, (unsigned char)type2, type3, val);
  4391. break;
  4392. case SP_SP_VANISH_RACE_RATE: // bonus3 bSPVanishRaceRate,r,x,n;
  4393. PC_BONUS_CHK_RACE(type2,SP_SP_VANISH_RACE_RATE);
  4394. if(sd->state.lr_flag != 2) {
  4395. sd->sp_vanish_race[type2].rate += type3;
  4396. sd->sp_vanish_race[type2].per += val;
  4397. }
  4398. break;
  4399. case SP_HP_VANISH_RACE_RATE: // bonus3 bHPVanishRaceRate,r,x,n;
  4400. PC_BONUS_CHK_RACE(type2,SP_HP_VANISH_RACE_RATE);
  4401. if(sd->state.lr_flag != 2) {
  4402. sd->hp_vanish_race[type2].rate += type3;
  4403. sd->hp_vanish_race[type2].per += val;
  4404. }
  4405. break;
  4406. case SP_SP_VANISH_RATE: // bonus3 bSPVanishRate,x,n,bf;
  4407. if(sd->state.lr_flag != 2) {
  4408. pc_bonus_addvanish(sd->sp_vanish, type2, type3, val);
  4409. }
  4410. break;
  4411. case SP_HP_VANISH_RATE: // bonus3 bHPVanishRate,x,n,bf;
  4412. if(sd->state.lr_flag != 2) {
  4413. pc_bonus_addvanish(sd->hp_vanish, type2, type3, val);
  4414. }
  4415. break;
  4416. case SP_STATE_NORECOVER_RACE: // bonus3 bStateNoRecoverRace,r,x,t;
  4417. PC_BONUS_CHK_RACE(type2, SP_STATE_NORECOVER_RACE);
  4418. if (sd->state.lr_flag == 2)
  4419. break;
  4420. //! CONFIRM: Is it not stackable? Does not check max or min value?
  4421. //if (type3 > sd->norecover_state_race[type2].rate) {
  4422. // sd->norecover_state_race[type2].rate = type3;
  4423. // sd->norecover_state_race[type2].tick = val;
  4424. // break;
  4425. //}
  4426. sd->norecover_state_race[type2].rate = type3;
  4427. sd->norecover_state_race[type2].tick = val;
  4428. break;
  4429. default:
  4430. if (current_equip_combo_pos > 0) {
  4431. 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);
  4432. }
  4433. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4434. 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);
  4435. }
  4436. else {
  4437. ShowWarning("pc_bonus3: unknown bonus type %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, val);
  4438. }
  4439. break;
  4440. }
  4441. }
  4442. /**
  4443. * Gives item bonus to player for format: bonus4 bBonusName,type2,type3,type4,val;
  4444. * @param sd
  4445. * @param type Bonus type used by bBonusName
  4446. * @param type2
  4447. * @param type3
  4448. * @param type4
  4449. * @param val Value that usually for rate or fixed value
  4450. */
  4451. void pc_bonus4(map_session_data *sd,int type,int type2,int type3,int type4,int val)
  4452. {
  4453. nullpo_retv(sd);
  4454. switch(type){
  4455. case SP_AUTOSPELL: // bonus4 bAutoSpell,sk,y,n,i;
  4456. if(sd->state.lr_flag != 2)
  4457. pc_bonus_autospell(sd->autospell, type2, type3, type4, 0, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4458. break;
  4459. case SP_AUTOSPELL_WHENHIT: // bonus4 bAutoSpellWhenHit,sk,y,n,i;
  4460. if(sd->state.lr_flag != 2)
  4461. pc_bonus_autospell(sd->autospell2, type2, type3, type4, BF_NORMAL|BF_SKILL, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4462. break;
  4463. case SP_AUTOSPELL_ONSKILL: // bonus4 bAutoSpellOnSkill,sk,x,y,n;
  4464. if(sd->state.lr_flag != 2)
  4465. {
  4466. int target = skill_get_inf(type3); //Support or Self (non-auto-target) skills should pick self.
  4467. target = target&INF_SUPPORT_SKILL || (target&INF_SELF_SKILL && !skill_get_inf2(type3, INF2_NOTARGETSELF));
  4468. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, val, current_equip_card_id, target ? AUTOSPELL_FORCE_TARGET : AUTOSPELL_FORCE_SELF);
  4469. }
  4470. break;
  4471. case SP_ADDEFF: // bonus4 bAddEff,eff,n,y,t;
  4472. PC_BONUS_CHK_SC(type2,SP_ADDEFF);
  4473. pc_bonus_addeff(sd->addeff, (sc_type)type2, sd->state.lr_flag != 2 ? type3 : 0, sd->state.lr_flag == 2 ? type3 : 0, type4, val);
  4474. break;
  4475. case SP_ADDEFF_WHENHIT: // bonus4 bAddEffWhenHit,eff,n,y,t;
  4476. PC_BONUS_CHK_SC(type2,SP_ADDEFF_WHENHIT);
  4477. if (sd->state.lr_flag != 2)
  4478. pc_bonus_addeff(sd->addeff_atked, (sc_type)type2, type3, 0, type4, val);
  4479. break;
  4480. case SP_ADDEFF_ONSKILL: // bonus4 bAddEffOnSkill,sk,eff,n,y;
  4481. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4482. if( sd->state.lr_flag != 2 )
  4483. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, val, 0);
  4484. break;
  4485. case SP_SET_DEF_RACE: // bonus4 bSetDefRace,r,n,t,y;
  4486. PC_BONUS_CHK_RACE(type2,SP_SET_DEF_RACE);
  4487. if(sd->state.lr_flag == 2)
  4488. break;
  4489. sd->def_set_race[type2].rate = type3;
  4490. sd->def_set_race[type2].tick = type4;
  4491. sd->def_set_race[type2].value = val;
  4492. break;
  4493. case SP_SET_MDEF_RACE: // bonus4 bSetMDefRace,r,n,t,y;
  4494. PC_BONUS_CHK_RACE(type2,SP_SET_MDEF_RACE);
  4495. if(sd->state.lr_flag == 2)
  4496. break;
  4497. sd->mdef_set_race[type2].rate = type3;
  4498. sd->mdef_set_race[type2].tick = type4;
  4499. sd->mdef_set_race[type2].value = val;
  4500. break;
  4501. default:
  4502. if (current_equip_combo_pos > 0) {
  4503. 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);
  4504. }
  4505. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4506. 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);
  4507. }
  4508. else {
  4509. ShowWarning("pc_bonus4: unknown bonus type %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, val);
  4510. }
  4511. break;
  4512. }
  4513. }
  4514. /**
  4515. * Gives item bonus to player for format: bonus5 bBonusName,type2,type3,type4,val;
  4516. * @param sd
  4517. * @param type Bonus type used by bBonusName
  4518. * @param type2
  4519. * @param type3
  4520. * @param type4
  4521. * @param val Value that usually for rate or fixed value
  4522. */
  4523. void pc_bonus5(map_session_data *sd,int type,int type2,int type3,int type4,int type5,int val)
  4524. {
  4525. nullpo_retv(sd);
  4526. switch(type){
  4527. case SP_AUTOSPELL: // bonus5 bAutoSpell,sk,y,n,bf,i;
  4528. if(sd->state.lr_flag != 2)
  4529. pc_bonus_autospell(sd->autospell, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4530. break;
  4531. case SP_AUTOSPELL_WHENHIT: // bonus5 bAutoSpellWhenHit,sk,y,n,bf,i;
  4532. if(sd->state.lr_flag != 2)
  4533. pc_bonus_autospell(sd->autospell2, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4534. break;
  4535. case SP_AUTOSPELL_ONSKILL: // bonus5 bAutoSpellOnSkill,sk,x,y,n,i;
  4536. if(sd->state.lr_flag != 2)
  4537. pc_bonus_autospell_onskill(sd->autospell3, type2, type3, type4, type5, current_equip_card_id, val & AUTOSPELL_FORCE_ALL);
  4538. break;
  4539. case SP_ADDEFF_ONSKILL: // bonus5 bAddEffOnSkill,sk,eff,n,y,t;
  4540. PC_BONUS_CHK_SC(type3,SP_ADDEFF_ONSKILL);
  4541. if( sd->state.lr_flag != 2 )
  4542. pc_bonus_addeff_onskill(sd->addeff_onskill, (sc_type)type3, type4, type2, type5, val);
  4543. break;
  4544. default:
  4545. if (current_equip_combo_pos > 0) {
  4546. 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);
  4547. }
  4548. else if (current_equip_card_id > 0 || current_equip_item_index > 0) {
  4549. 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);
  4550. }
  4551. else {
  4552. ShowWarning("pc_bonus5: unknown bonus type %d %d %d %d %d %d in unknown usage. Report this!\n", type, type2, type3, type4, type5, val);
  4553. }
  4554. break;
  4555. }
  4556. }
  4557. /*==========================================
  4558. * Grants a player a given skill. Flag values are:
  4559. * 0 - Grant permanent skill to be bound to skill tree
  4560. * 1 - Grant an item skill (temporary)
  4561. * 2 - Like 1, except the level granted can stack with previously learned level.
  4562. * 4 - Like 0, except the skill will ignore skill tree (saves through job changes and resets).
  4563. *------------------------------------------*/
  4564. bool pc_skill(map_session_data* sd, uint16 skill_id, int level, enum e_addskill_type type) {
  4565. uint16 idx = 0;
  4566. nullpo_ret(sd);
  4567. if (!skill_id || !(idx = skill_get_index(skill_id))) {
  4568. ShowError("pc_skill: Skill with id %d does not exist in the skill database\n", skill_id);
  4569. return false;
  4570. }
  4571. if (level > MAX_SKILL_LEVEL) {
  4572. ShowError("pc_skill: Skill level %d too high. Max lv supported is %d\n", level, MAX_SKILL_LEVEL);
  4573. return false;
  4574. }
  4575. if (type == ADDSKILL_TEMP_ADDLEVEL && sd->status.skill[idx].lv + level > MAX_SKILL_LEVEL) {
  4576. 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);
  4577. level = MAX_SKILL_LEVEL - sd->status.skill[idx].lv;
  4578. }
  4579. switch (type) {
  4580. case ADDSKILL_PERMANENT: //Set skill data overwriting whatever was there before.
  4581. sd->status.skill[idx].id = skill_id;
  4582. sd->status.skill[idx].lv = level;
  4583. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4584. if (level == 0) { //Remove skill.
  4585. sd->status.skill[idx].id = 0;
  4586. clif_deleteskill(sd,skill_id);
  4587. } else
  4588. clif_addskill(sd,skill_id);
  4589. 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.
  4590. status_calc_pc(sd, SCO_NONE);
  4591. break;
  4592. case ADDSKILL_TEMP: //Item bonus skill.
  4593. if (sd->status.skill[idx].id != 0) {
  4594. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT) //Non-granted skill, store it's level.
  4595. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv;
  4596. } else {
  4597. sd->status.skill[idx].id = skill_id;
  4598. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY;
  4599. }
  4600. sd->status.skill[idx].lv = max(sd->status.skill[idx].lv, level);
  4601. break;
  4602. case ADDSKILL_TEMP_ADDLEVEL: //Add skill bonus on top of what you had.
  4603. if (sd->status.skill[idx].id != 0) {
  4604. if (sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT)
  4605. sd->status.skill[idx].flag = SKILL_FLAG_REPLACED_LV_0 + sd->status.skill[idx].lv; // Store previous level.
  4606. } else {
  4607. sd->status.skill[idx].id = skill_id;
  4608. sd->status.skill[idx].flag = SKILL_FLAG_TEMPORARY; //Set that this is a bonus skill.
  4609. }
  4610. sd->status.skill[idx].lv += level;
  4611. break;
  4612. case ADDSKILL_PERMANENT_GRANTED: //Permanent granted skills ignore the skill tree
  4613. sd->status.skill[idx].id = skill_id;
  4614. sd->status.skill[idx].lv = level;
  4615. sd->status.skill[idx].flag = SKILL_FLAG_PERM_GRANTED;
  4616. if (level == 0) { //Remove skill.
  4617. sd->status.skill[idx].id = 0;
  4618. clif_deleteskill(sd,skill_id);
  4619. } else
  4620. clif_addskill(sd,skill_id);
  4621. 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.
  4622. status_calc_pc(sd, SCO_NONE);
  4623. break;
  4624. default:
  4625. return false;
  4626. }
  4627. return true;
  4628. }
  4629. /**
  4630. * Set's a player's plagiarized skill.
  4631. * @param sd: Player
  4632. * @param skill_id: Skill to be plagiarized
  4633. * @param skill_lv: Skill level to be plagiarized
  4634. * @return True on success or false otherwise
  4635. */
  4636. bool pc_skill_plagiarism(map_session_data &sd, uint16 skill_id, uint16 skill_lv)
  4637. {
  4638. skill_id = skill_dummy2skill_id(skill_id);
  4639. uint16 idx = skill_get_index(skill_id);
  4640. // Use skill index, avoiding out-of-bound array [Cydh]
  4641. if (idx == 0) {
  4642. ShowWarning("pc_skill_plagiarism: invalid skill idx 0 for skill %d.\n", skill_id);
  4643. return false;
  4644. }
  4645. skill_lv = cap_value(skill_lv, 1, skill_get_max(skill_id));
  4646. int type = skill_isCopyable(&sd, skill_id);
  4647. if (type == 1) {
  4648. pc_skill_plagiarism_reset(sd, type);
  4649. sd.cloneskill_idx = idx;
  4650. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), skill_id);
  4651. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), skill_lv);
  4652. } else if (type == 2) {
  4653. pc_skill_plagiarism_reset(sd, type);
  4654. sd.reproduceskill_idx = idx;
  4655. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), skill_id);
  4656. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), skill_lv);
  4657. } else {
  4658. ShowWarning("pc_skill_plagiarism: skill %d is not copyable.\n", skill_id);
  4659. return false;
  4660. }
  4661. sd.status.skill[idx].id = skill_id;
  4662. sd.status.skill[idx].lv = static_cast<uint8>(skill_lv);
  4663. sd.status.skill[idx].flag = SKILL_FLAG_PLAGIARIZED;
  4664. clif_addskill(&sd, skill_id);
  4665. return true;
  4666. }
  4667. /**
  4668. * Clear plagiarized skills from a player.
  4669. * @param sd: Player
  4670. * @param type: 1 for Plagiarism or 2 for Reproduce
  4671. * @return True on success or false otherwise
  4672. */
  4673. bool pc_skill_plagiarism_reset(map_session_data &sd, uint8 type)
  4674. {
  4675. uint16 idx;
  4676. if (type == 1)
  4677. idx = sd.cloneskill_idx;
  4678. else if (type == 2)
  4679. idx = sd.reproduceskill_idx;
  4680. else {
  4681. ShowError("pc_skill_plagiarism_reset: Unknown type %d.\n", type);
  4682. return false;
  4683. }
  4684. if (sd.status.skill[idx].flag == SKILL_FLAG_PLAGIARIZED) {
  4685. uint16 skill_id = sd.status.skill[idx].id;
  4686. sd.status.skill[idx].id = 0;
  4687. sd.status.skill[idx].lv = 0;
  4688. sd.status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  4689. clif_deleteskill(&sd, skill_id);
  4690. if (type == 1) {
  4691. sd.cloneskill_idx = 0;
  4692. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM), 0);
  4693. pc_setglobalreg(&sd, add_str(SKILL_VAR_PLAGIARISM_LV), 0);
  4694. }
  4695. else if (type == 2) {
  4696. sd.reproduceskill_idx = 0;
  4697. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE), 0);
  4698. pc_setglobalreg(&sd, add_str(SKILL_VAR_REPRODUCE_LV), 0);
  4699. }
  4700. }
  4701. return true;
  4702. }
  4703. /*==========================================
  4704. * Append a card to an item ?
  4705. *------------------------------------------*/
  4706. int pc_insert_card(map_session_data* sd, int idx_card, int idx_equip)
  4707. {
  4708. nullpo_ret(sd);
  4709. if (idx_equip < 0 || idx_equip >= MAX_INVENTORY) {
  4710. return 0;
  4711. }
  4712. if (idx_card < 0 || idx_card >= MAX_INVENTORY) {
  4713. return 0;
  4714. }
  4715. int i;
  4716. t_itemid nameid;
  4717. struct item_data* item_eq = sd->inventory_data[idx_equip];
  4718. struct item_data* item_card = sd->inventory_data[idx_card];
  4719. if(item_eq == nullptr)
  4720. return 0; //Invalid item index.
  4721. if(item_card == nullptr)
  4722. return 0; //Invalid card index.
  4723. if( sd->inventory.u.items_inventory[idx_equip].nameid == 0 || sd->inventory.u.items_inventory[idx_equip].amount < 1 )
  4724. return 0; // target item missing
  4725. if( sd->inventory.u.items_inventory[idx_card].nameid == 0 || sd->inventory.u.items_inventory[idx_card].amount < 1 )
  4726. return 0; // target card missing
  4727. if( item_eq->type != IT_WEAPON && item_eq->type != IT_ARMOR )
  4728. return 0; // only weapons and armor are allowed
  4729. if( item_card->type != IT_CARD )
  4730. return 0; // must be a card
  4731. if( sd->inventory.u.items_inventory[idx_equip].identify == 0 )
  4732. return 0; // target must be identified
  4733. if( itemdb_isspecial(sd->inventory.u.items_inventory[idx_equip].card[0]) )
  4734. return 0; // card slots reserved for other purposes
  4735. if( (item_eq->equip & item_card->equip) == 0 )
  4736. return 0; // card cannot be compounded on this item type
  4737. if( item_eq->type == IT_WEAPON && item_card->equip == EQP_SHIELD )
  4738. return 0; // attempted to place shield card on left-hand weapon.
  4739. 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)) )
  4740. return 0; // specific accessory-card can only be inserted to specific accessory.
  4741. if( sd->inventory.u.items_inventory[idx_equip].equip != 0 )
  4742. return 0; // item must be unequipped
  4743. ARR_FIND( 0, item_eq->slots, i, sd->inventory.u.items_inventory[idx_equip].card[i] == 0 );
  4744. if( i == item_eq->slots )
  4745. return 0; // no free slots
  4746. // remember the card id to insert
  4747. nameid = sd->inventory.u.items_inventory[idx_card].nameid;
  4748. if( pc_delitem(sd,idx_card,1,1,0,LOG_TYPE_OTHER) == 1 )
  4749. {// failed
  4750. clif_insert_card(sd,idx_equip,idx_card,1);
  4751. }
  4752. else
  4753. {// success
  4754. log_pick_pc(sd, LOG_TYPE_OTHER, -1, &sd->inventory.u.items_inventory[idx_equip]);
  4755. sd->inventory.u.items_inventory[idx_equip].card[i] = nameid;
  4756. log_pick_pc(sd, LOG_TYPE_OTHER, 1, &sd->inventory.u.items_inventory[idx_equip]);
  4757. clif_insert_card(sd,idx_equip,idx_card,0);
  4758. }
  4759. return 0;
  4760. }
  4761. /**
  4762. * Returns the count of unidentified items with the option to identify too.
  4763. * @param sd: Player data
  4764. * @param identify_item: Whether or not to identify any unidentified items
  4765. * @return Unidentified items count
  4766. */
  4767. int pc_identifyall(map_session_data *sd, bool identify_item)
  4768. {
  4769. int unidentified_count = 0;
  4770. for (int i = 0; i < MAX_INVENTORY; i++) {
  4771. if (sd->inventory.u.items_inventory[i].nameid > 0 && sd->inventory.u.items_inventory[i].identify != 1) {
  4772. if (identify_item == true) {
  4773. sd->inventory.u.items_inventory[i].identify = 1;
  4774. clif_item_identified(sd,i,0);
  4775. }
  4776. unidentified_count++;
  4777. }
  4778. }
  4779. return unidentified_count;
  4780. }
  4781. //
  4782. // Items
  4783. //
  4784. /*==========================================
  4785. * Update buying value by skills
  4786. *------------------------------------------*/
  4787. int pc_modifybuyvalue(map_session_data *sd,int orig_value)
  4788. {
  4789. int skill,val = orig_value,rate1 = 0,rate2 = 0;
  4790. if((skill=pc_checkskill(sd,MC_DISCOUNT))>0) // merchant discount
  4791. rate1 = 5+skill*2-((skill==10)? 1:0);
  4792. if((skill=pc_checkskill(sd,RG_COMPULSION))>0) // rogue discount
  4793. rate2 = 5+skill*4;
  4794. if(rate1 < rate2) rate1 = rate2;
  4795. if(rate1)
  4796. val = (int)((double)orig_value*(double)(100-rate1)/100.);
  4797. if(val < battle_config.min_shop_buy)
  4798. val = battle_config.min_shop_buy;
  4799. return val;
  4800. }
  4801. /*==========================================
  4802. * Update selling value by skills
  4803. *------------------------------------------*/
  4804. int pc_modifysellvalue(map_session_data *sd,int orig_value)
  4805. {
  4806. int skill,val = orig_value,rate = 0;
  4807. if((skill=pc_checkskill(sd,MC_OVERCHARGE))>0) //OverCharge
  4808. rate = 5+skill*2-((skill==10)? 1:0);
  4809. if(rate)
  4810. val = (int)((double)orig_value*(double)(100+rate)/100.);
  4811. if (val < battle_config.min_shop_sell)
  4812. val = battle_config.min_shop_sell;
  4813. return val;
  4814. }
  4815. /*==========================================
  4816. * Checking if we have enough place on inventory for new item
  4817. * Make sure to take 30k as limit (for client I guess)
  4818. * @param sd
  4819. * @param nameid
  4820. * @param amount
  4821. * @return e_chkitem_result
  4822. *------------------------------------------*/
  4823. char pc_checkadditem(map_session_data *sd, t_itemid nameid, int amount)
  4824. {
  4825. int i;
  4826. struct item_data* data;
  4827. nullpo_ret(sd);
  4828. if(amount > MAX_AMOUNT)
  4829. return CHKADDITEM_OVERAMOUNT;
  4830. data = itemdb_search(nameid);
  4831. if(!itemdb_isstackable2(data))
  4832. return CHKADDITEM_NEW;
  4833. if( data->stack.inventory && amount > data->stack.amount )
  4834. return CHKADDITEM_OVERAMOUNT;
  4835. if (data->flag.guid)
  4836. return CHKADDITEM_NEW;
  4837. for(i=0;i<MAX_INVENTORY;i++){
  4838. // FIXME: This does not consider the checked item's cards, thus could check a wrong slot for stackability.
  4839. if(sd->inventory.u.items_inventory[i].nameid == nameid){
  4840. 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 ) )
  4841. return CHKADDITEM_OVERAMOUNT;
  4842. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  4843. if( i >= sd->status.inventory_slots ){
  4844. return CHKADDITEM_OVERAMOUNT;
  4845. }
  4846. return CHKADDITEM_EXIST;
  4847. }
  4848. }
  4849. return CHKADDITEM_NEW;
  4850. }
  4851. /*==========================================
  4852. * Return number of available place in inventory
  4853. * Each non stackable item will reduce place by 1
  4854. * @param sd
  4855. * @return Number of empty slots
  4856. *------------------------------------------*/
  4857. uint8 pc_inventoryblank(map_session_data *sd)
  4858. {
  4859. uint16 i;
  4860. uint8 b;
  4861. nullpo_ret(sd);
  4862. for(i = 0, b = 0; i < sd->status.inventory_slots; i++){
  4863. if(sd->inventory.u.items_inventory[i].nameid == 0)
  4864. b++;
  4865. }
  4866. return b;
  4867. }
  4868. /**
  4869. * Attempts to remove zeny from player
  4870. * @param sd: Player
  4871. * @param zeny: Zeny removed
  4872. * @param type: Log type
  4873. * @param tsd: (optional) From who to log (if null take sd)
  4874. * @return 0: Success, 1: Failed (Removing negative Zeny or not enough Zeny), 2: Player not found
  4875. */
  4876. char pc_payzeny(map_session_data *sd, int zeny, enum e_log_pick_type type, map_session_data *tsd)
  4877. {
  4878. nullpo_retr(2,sd);
  4879. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4880. if( zeny < 0 )
  4881. {
  4882. ShowError("pc_payzeny: Paying negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4883. return 1;
  4884. }
  4885. if( sd->status.zeny < zeny )
  4886. return 1; //Not enough.
  4887. sd->status.zeny -= zeny;
  4888. clif_updatestatus(sd,SP_ZENY);
  4889. if(!tsd) tsd = sd;
  4890. log_zeny(sd, type, tsd, -zeny);
  4891. if( zeny > 0 && sd->state.showzeny ) {
  4892. char output[255];
  4893. sprintf(output, "Removed %dz.", zeny);
  4894. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4895. }
  4896. return 0;
  4897. }
  4898. /**
  4899. * Attempts to give zeny to player
  4900. * @param sd: Player
  4901. * @param type: Log type
  4902. * @param tsd: (optional) From who to log (if null take sd)
  4903. * @return -1: Player not found, 0: Success, 1: Giving negative Zeny
  4904. */
  4905. char pc_getzeny(map_session_data *sd, int zeny, enum e_log_pick_type type, map_session_data *tsd)
  4906. {
  4907. nullpo_retr(-1,sd);
  4908. zeny = cap_value(zeny,-MAX_ZENY,MAX_ZENY); //prevent command UB
  4909. if( zeny < 0 )
  4910. {
  4911. ShowError("pc_getzeny: Obtaining negative Zeny (zeny=%d, account_id=%d, char_id=%d).\n", zeny, sd->status.account_id, sd->status.char_id);
  4912. return 1;
  4913. }
  4914. if( zeny > MAX_ZENY - sd->status.zeny )
  4915. zeny = MAX_ZENY - sd->status.zeny;
  4916. sd->status.zeny += zeny;
  4917. clif_updatestatus(sd,SP_ZENY);
  4918. if(!tsd) tsd = sd;
  4919. log_zeny(sd, type, tsd, zeny);
  4920. if( zeny > 0 && sd->state.showzeny ) {
  4921. char output[255];
  4922. sprintf(output, "Gained %dz.", zeny);
  4923. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4924. }
  4925. achievement_update_objective(sd, AG_GET_ZENY, 1, sd->status.zeny);
  4926. return 0;
  4927. }
  4928. /**
  4929. * Attempts to remove Cash Points from player
  4930. * @param sd: Player
  4931. * @param price: Total points (cash + kafra) the player has to pay
  4932. * @param points: Kafra points the player has to pay
  4933. * @param type: Log type
  4934. * @return -1: Not enough points, otherwise success (cash+points)
  4935. */
  4936. int pc_paycash(map_session_data *sd, int price, int points, e_log_pick_type type)
  4937. {
  4938. int cash;
  4939. nullpo_retr(-1,sd);
  4940. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  4941. cash = price-points;
  4942. if( sd->cashPoints < cash || sd->kafraPoints < points )
  4943. {
  4944. 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);
  4945. return -1;
  4946. }
  4947. if( cash ){
  4948. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints - cash);
  4949. sd->cashPoints -= cash;
  4950. log_cash( sd, type, LOG_CASH_TYPE_CASH, -cash );
  4951. }
  4952. if( points ){
  4953. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints - points);
  4954. sd->kafraPoints -= points;
  4955. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, -points );
  4956. }
  4957. if( battle_config.cashshop_show_points )
  4958. {
  4959. char output[CHAT_SIZE_MAX];
  4960. 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.
  4961. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4962. }
  4963. return cash+points;
  4964. }
  4965. /**
  4966. * Attempts to give Cash Points to player
  4967. * @param sd: Player
  4968. * @param cash: Cash points the player gets
  4969. * @param points: Kafra points the player gets
  4970. * @param type: Log type
  4971. * @return -1: Error, otherwise success (cash or points)
  4972. */
  4973. int pc_getcash(map_session_data *sd, int cash, int points, e_log_pick_type type)
  4974. {
  4975. char output[CHAT_SIZE_MAX];
  4976. nullpo_retr(-1,sd);
  4977. cash = cap_value(cash, 0, MAX_CASHPOINT); //prevent command UB
  4978. points = cap_value(points, 0, MAX_KAFRAPOINT); //prevent command UB
  4979. if( cash > 0 )
  4980. {
  4981. if( cash > MAX_CASHPOINT-sd->cashPoints )
  4982. {
  4983. 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);
  4984. cash = MAX_CASHPOINT-sd->cashPoints;
  4985. }
  4986. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints+cash);
  4987. sd->cashPoints += cash;
  4988. log_cash( sd, type, LOG_CASH_TYPE_CASH, cash );
  4989. if( battle_config.cashshop_show_points )
  4990. {
  4991. sprintf(output, msg_txt(sd,505), cash, sd->cashPoints);
  4992. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  4993. }
  4994. return cash;
  4995. }
  4996. if( points > 0 )
  4997. {
  4998. if( points > MAX_KAFRAPOINT-sd->kafraPoints )
  4999. {
  5000. 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);
  5001. points = MAX_KAFRAPOINT-sd->kafraPoints;
  5002. }
  5003. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints+points);
  5004. sd->kafraPoints += points;
  5005. log_cash( sd, type, LOG_CASH_TYPE_KAFRA, points );
  5006. if( battle_config.cashshop_show_points )
  5007. {
  5008. sprintf(output, msg_txt(sd,506), points, sd->kafraPoints);
  5009. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  5010. }
  5011. return points;
  5012. }
  5013. return -1; //shouldn't happen but just in case
  5014. }
  5015. /**
  5016. * Searching a specified itemid in inventory and return his stored index
  5017. * @param sd Player
  5018. * @param nameid Find this Item!
  5019. * @return Stored index in inventory, or -1 if not found.
  5020. **/
  5021. short pc_search_inventory(map_session_data *sd, t_itemid nameid) {
  5022. short i;
  5023. nullpo_retr(-1, sd);
  5024. ARR_FIND( 0, MAX_INVENTORY, i, sd->inventory.u.items_inventory[i].nameid == nameid && (sd->inventory.u.items_inventory[i].amount > 0 || nameid == 0) );
  5025. return ( i < MAX_INVENTORY ) ? i : -1;
  5026. }
  5027. /** Attempt to add a new item to player inventory
  5028. * @param sd
  5029. * @param item_data
  5030. * @param amount
  5031. * @param log_type
  5032. * @return
  5033. * 0 = success
  5034. * 1 = invalid itemid not found or negative amount
  5035. * 2 = overweight
  5036. * 3 = ?
  5037. * 4 = no free place found
  5038. * 5 = max amount reached
  5039. * 6 = ?
  5040. * 7 = stack limitation
  5041. */
  5042. enum e_additem_result pc_additem(map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type) {
  5043. struct item_data *id;
  5044. int16 i;
  5045. unsigned int w;
  5046. nullpo_retr(ADDITEM_INVALID, sd);
  5047. nullpo_retr(ADDITEM_INVALID, item);
  5048. if( item->nameid == 0 || amount <= 0 )
  5049. return ADDITEM_INVALID;
  5050. if( amount > MAX_AMOUNT )
  5051. return ADDITEM_OVERAMOUNT;
  5052. id = itemdb_search(item->nameid);
  5053. if( id->stack.inventory && amount > id->stack.amount )
  5054. {// item stack limitation
  5055. return ADDITEM_STACKLIMIT;
  5056. }
  5057. w = id->weight*amount;
  5058. if(sd->weight + w > sd->max_weight)
  5059. return ADDITEM_OVERWEIGHT;
  5060. if (id->flag.guid && !item->unique_id)
  5061. item->unique_id = pc_generate_unique_id(sd);
  5062. // Stackable | Non Rental
  5063. if( itemdb_isstackable2(id) && item->expire_time == 0 ) {
  5064. for( i = 0; i < MAX_INVENTORY; i++ ) {
  5065. if( sd->inventory.u.items_inventory[i].nameid == item->nameid &&
  5066. sd->inventory.u.items_inventory[i].bound == item->bound &&
  5067. sd->inventory.u.items_inventory[i].expire_time == 0 &&
  5068. sd->inventory.u.items_inventory[i].unique_id == item->unique_id &&
  5069. memcmp(&sd->inventory.u.items_inventory[i].card, &item->card, sizeof(item->card)) == 0 ) {
  5070. 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 ) )
  5071. return ADDITEM_OVERAMOUNT;
  5072. // If the item is in the inventory already, but the player is not allowed to use that many slots anymore
  5073. if( i >= sd->status.inventory_slots ){
  5074. return ADDITEM_OVERAMOUNT;
  5075. }
  5076. sd->inventory.u.items_inventory[i].amount += amount;
  5077. clif_additem(sd,i,amount,0);
  5078. break;
  5079. }
  5080. }
  5081. }else{
  5082. i = MAX_INVENTORY;
  5083. }
  5084. if (i >= MAX_INVENTORY) {
  5085. i = pc_search_inventory(sd,0);
  5086. if( i < 0 )
  5087. return ADDITEM_OVERITEM;
  5088. if( i >= sd->status.inventory_slots ){
  5089. return ADDITEM_OVERITEM;
  5090. }
  5091. memcpy(&sd->inventory.u.items_inventory[i], item, sizeof(sd->inventory.u.items_inventory[0]));
  5092. // clear equip and favorite fields first, just in case
  5093. if( item->equip )
  5094. sd->inventory.u.items_inventory[i].equip = 0;
  5095. if( item->favorite )
  5096. sd->inventory.u.items_inventory[i].favorite = 0;
  5097. if( item->equipSwitch )
  5098. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  5099. sd->inventory.u.items_inventory[i].amount = amount;
  5100. sd->inventory_data[i] = id;
  5101. sd->last_addeditem_index = i;
  5102. if (!itemdb_isstackable2(id) || id->flag.guid)
  5103. sd->inventory.u.items_inventory[i].unique_id = item->unique_id ? item->unique_id : pc_generate_unique_id(sd);
  5104. clif_additem(sd,i,amount,0);
  5105. }
  5106. log_pick_pc(sd, log_type, amount, &sd->inventory.u.items_inventory[i]);
  5107. sd->weight += w;
  5108. clif_updatestatus(sd,SP_WEIGHT);
  5109. //Auto-equip
  5110. if(id->flag.autoequip)
  5111. pc_equipitem(sd, i, id->equip);
  5112. /* rental item check */
  5113. if( item->expire_time ) {
  5114. if( time(NULL) > item->expire_time ) {
  5115. clif_rental_expired(sd, i, sd->inventory.u.items_inventory[i].nameid);
  5116. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 1, 0, LOG_TYPE_OTHER);
  5117. } else {
  5118. unsigned int seconds = (unsigned int)( item->expire_time - time(NULL) );
  5119. clif_rental_time(sd, sd->inventory.u.items_inventory[i].nameid, seconds);
  5120. pc_inventory_rental_add(sd, seconds);
  5121. }
  5122. }
  5123. achievement_update_objective(sd, AG_GET_ITEM, 1, id->value_sell);
  5124. pc_show_questinfo(sd);
  5125. return ADDITEM_SUCCESS;
  5126. }
  5127. /*==========================================
  5128. * Remove an item at index n from inventory by amount.
  5129. * @param sd
  5130. * @param n Item index in inventory
  5131. * @param amount
  5132. * @param type &1: Don't notify deletion; &2 Don't notify weight change; &4 Don't calculate status
  5133. * @param reason Delete reason
  5134. * @param log_type e_log_pick_type
  5135. * @return 1 - invalid itemid or negative amount; 0 - Success
  5136. *------------------------------------------*/
  5137. char pc_delitem(map_session_data *sd,int n,int amount,int type, short reason, e_log_pick_type log_type)
  5138. {
  5139. nullpo_retr(1, sd);
  5140. 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] == NULL)
  5141. return 1;
  5142. log_pick_pc(sd, log_type, -amount, &sd->inventory.u.items_inventory[n]);
  5143. sd->inventory.u.items_inventory[n].amount -= amount;
  5144. sd->weight -= sd->inventory_data[n]->weight*amount ;
  5145. if( sd->inventory.u.items_inventory[n].amount <= 0 ){
  5146. if(sd->inventory.u.items_inventory[n].equip)
  5147. pc_unequipitem(sd,n,2|(!(type&4) ? 1 : 0));
  5148. memset(&sd->inventory.u.items_inventory[n],0,sizeof(sd->inventory.u.items_inventory[0]));
  5149. sd->inventory_data[n] = NULL;
  5150. }
  5151. if(!(type&1))
  5152. clif_delitem(sd,n,amount,reason);
  5153. if(!(type&2))
  5154. clif_updatestatus(sd,SP_WEIGHT);
  5155. pc_show_questinfo(sd);
  5156. return 0;
  5157. }
  5158. /*==========================================
  5159. * Attempt to drop an item.
  5160. * @param sd
  5161. * @param n Item index in inventory
  5162. * @param amount Amount of item
  5163. * @return False = fail; True = success
  5164. *------------------------------------------*/
  5165. bool pc_dropitem(map_session_data *sd,int n,int amount)
  5166. {
  5167. nullpo_retr(1, sd);
  5168. if(n < 0 || n >= MAX_INVENTORY)
  5169. return false;
  5170. if(amount <= 0)
  5171. return false;
  5172. if(sd->inventory.u.items_inventory[n].nameid == 0 ||
  5173. sd->inventory.u.items_inventory[n].amount <= 0 ||
  5174. sd->inventory.u.items_inventory[n].amount < amount ||
  5175. sd->state.trading || sd->state.vending ||
  5176. !sd->inventory_data[n] //pc_delitem would fail on this case.
  5177. )
  5178. return false;
  5179. if( sd->inventory.u.items_inventory[n].equipSwitch )
  5180. return false;
  5181. if( map_getmapflag(sd->bl.m, MF_NODROP) )
  5182. {
  5183. clif_displaymessage (sd->fd, msg_txt(sd,271));
  5184. return false; //Can't drop items in nodrop mapflag maps.
  5185. }
  5186. if( !pc_candrop(sd,&sd->inventory.u.items_inventory[n]) )
  5187. {
  5188. clif_displaymessage (sd->fd, msg_txt(sd,263));
  5189. return false;
  5190. }
  5191. if (!map_addflooritem(&sd->inventory.u.items_inventory[n], amount, sd->bl.m, sd->bl.x, sd->bl.y, 0, 0, 0, 2, 0))
  5192. return false;
  5193. pc_delitem(sd, n, amount, 1, 0, LOG_TYPE_PICKDROP_PLAYER);
  5194. clif_dropitem(sd, n, amount);
  5195. return true;
  5196. }
  5197. /*==========================================
  5198. * Attempt to pick up an item.
  5199. * @param sd
  5200. * @param fitem Item that will be picked
  5201. * @return False = fail; True = success
  5202. *------------------------------------------*/
  5203. bool pc_takeitem(map_session_data *sd,struct flooritem_data *fitem)
  5204. {
  5205. int flag = 0;
  5206. t_tick tick = gettick();
  5207. struct party_data *p = NULL;
  5208. nullpo_ret(sd);
  5209. nullpo_ret(fitem);
  5210. if (!check_distance_bl(&fitem->bl, &sd->bl, 2) && sd->ud.skill_id!=BS_GREED)
  5211. return false; // Distance is too far
  5212. if (sd->sc.cant.pickup)
  5213. return false;
  5214. if (sd->status.party_id)
  5215. p = party_search(sd->status.party_id);
  5216. if (fitem->first_get_charid > 0 && fitem->first_get_charid != sd->status.char_id) {
  5217. map_session_data *first_sd = map_charid2sd(fitem->first_get_charid);
  5218. if (DIFF_TICK(tick,fitem->first_get_tick) < 0) {
  5219. if (!(p && p->party.item&1 &&
  5220. first_sd && first_sd->status.party_id == sd->status.party_id
  5221. ))
  5222. return false;
  5223. }
  5224. else if (fitem->second_get_charid > 0 && fitem->second_get_charid != sd->status.char_id) {
  5225. map_session_data *second_sd = map_charid2sd(fitem->second_get_charid);
  5226. if (DIFF_TICK(tick, fitem->second_get_tick) < 0) {
  5227. if (!(p && p->party.item&1 &&
  5228. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5229. (second_sd && second_sd->status.party_id == sd->status.party_id))
  5230. ))
  5231. return false;
  5232. }
  5233. else if (fitem->third_get_charid > 0 && fitem->third_get_charid != sd->status.char_id){
  5234. map_session_data *third_sd = map_charid2sd(fitem->third_get_charid);
  5235. if (DIFF_TICK(tick,fitem->third_get_tick) < 0) {
  5236. if(!(p && p->party.item&1 &&
  5237. ((first_sd && first_sd->status.party_id == sd->status.party_id) ||
  5238. (second_sd && second_sd->status.party_id == sd->status.party_id) ||
  5239. (third_sd && third_sd->status.party_id == sd->status.party_id))
  5240. ))
  5241. return false;
  5242. }
  5243. }
  5244. }
  5245. }
  5246. //This function takes care of giving the item to whoever should have it, considering party-share options.
  5247. if ((flag = party_share_loot(p,sd,&fitem->item, fitem->first_get_charid))) {
  5248. clif_additem(sd,0,0,flag);
  5249. return true;
  5250. }
  5251. //Display pickup animation.
  5252. pc_stop_attack(sd);
  5253. clif_takeitem(&sd->bl,&fitem->bl);
  5254. if (fitem->mob_id &&
  5255. (itemdb_search(fitem->item.nameid))->flag.broadcast &&
  5256. (!p || !(p->party.item&2)) // Somehow, if party's pickup distribution is 'Even Share', no announcemet
  5257. )
  5258. intif_broadcast_obtain_special_item(sd, fitem->item.nameid, fitem->mob_id, ITEMOBTAIN_TYPE_MONSTER_ITEM);
  5259. map_clearflooritem(&fitem->bl);
  5260. return true;
  5261. }
  5262. /*==========================================
  5263. * Check if item is usable.
  5264. * Return:
  5265. * false = no
  5266. * true = yes
  5267. *------------------------------------------*/
  5268. bool pc_isUseitem(map_session_data *sd,int n)
  5269. {
  5270. struct item_data *item;
  5271. t_itemid nameid;
  5272. nullpo_ret(sd);
  5273. item = sd->inventory_data[n];
  5274. nameid = sd->inventory.u.items_inventory[n].nameid;
  5275. if( item == NULL )
  5276. return false;
  5277. //Not consumable item
  5278. if( item->type != IT_HEALING && item->type != IT_USABLE && item->type != IT_CASH )
  5279. return false;
  5280. if (pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL))
  5281. return true;
  5282. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  5283. if(mapdata->flag[MF_NOITEMCONSUMPTION]) //consumable but mapflag prevent it
  5284. return false;
  5285. //Prevent mass item usage. [Skotlex]
  5286. if( DIFF_TICK(sd->canuseitem_tick,gettick()) > 0 ||
  5287. (itemdb_group.item_exists(IG_CASH_FOOD, nameid) && DIFF_TICK(sd->canusecashfood_tick,gettick()) > 0)
  5288. )
  5289. return false;
  5290. if( (item->item_usage.sitting) && (pc_issit(sd) == 1) && (pc_get_group_level(sd) < item->item_usage.override) ) {
  5291. clif_msg(sd,ITEM_NOUSE_SITTING);
  5292. return false; // You cannot use this item while sitting.
  5293. }
  5294. if (sd->state.storage_flag && item->type != IT_CASH) {
  5295. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd,388), false, SELF);
  5296. return false; // You cannot use this item while storage is open.
  5297. }
  5298. if (item->flag.dead_branch && (mapdata->flag[MF_NOBRANCH] || mapdata_flag_gvg2(mapdata)))
  5299. return false;
  5300. if( itemdb_group.item_exists( IG_MF_NOTELEPORT, nameid ) ){
  5301. if( ( mapdata->flag[MF_NOTELEPORT] || mapdata_flag_gvg2( mapdata ) ) ){
  5302. clif_skill_teleportmessage( sd, 0 );
  5303. return false;
  5304. }
  5305. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5306. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5307. return false;
  5308. }
  5309. }
  5310. if( itemdb_group.item_exists( IG_MF_NORETURN, nameid ) ){
  5311. if( mapdata->flag[MF_NORETURN] ){
  5312. return false;
  5313. }
  5314. if( sd->duel_group && !battle_config.duel_allow_teleport ){
  5315. clif_displaymessage( sd->fd, msg_txt( sd, 663 ) ); // Duel: Can't use this item in duel.
  5316. return false;
  5317. }
  5318. }
  5319. if( itemdb_group.item_exists( IG_GIANT_FLY_WING, nameid ) ){
  5320. struct party_data *pd = party_search( sd->status.party_id );
  5321. if( pd ){
  5322. int i;
  5323. ARR_FIND( 0, MAX_PARTY, i, pd->data[i].sd == sd && pd->party.member[i].leader );
  5324. // User is not party leader
  5325. if( i == MAX_PARTY ){
  5326. clif_msg( sd, ITEM_PARTY_MEMBER_NOT_SUMMONED );
  5327. return false;
  5328. }
  5329. 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 ) );
  5330. // No party members found on same map
  5331. if( i == MAX_PARTY ){
  5332. clif_msg( sd, ITEM_PARTY_NO_MEMBER_IN_MAP );
  5333. return false;
  5334. }
  5335. }else{
  5336. clif_msg( sd, ITEM_PARTY_MEMBER_NOT_SUMMONED );
  5337. return false;
  5338. }
  5339. }
  5340. switch( nameid ) {
  5341. case ITEMID_MERCENARY_RED_POTION:
  5342. case ITEMID_MERCENARY_BLUE_POTION:
  5343. case ITEMID_M_CENTER_POTION:
  5344. case ITEMID_M_AWAKENING_POTION:
  5345. case ITEMID_M_BERSERK_POTION:
  5346. if( sd->md == NULL || sd->md->db == NULL )
  5347. return false;
  5348. if( sd->md->sc.cant.consume )
  5349. return false;
  5350. if( nameid == ITEMID_M_AWAKENING_POTION && sd->md->db->lv < 40 )
  5351. return false;
  5352. if( nameid == ITEMID_M_BERSERK_POTION && sd->md->db->lv < 80 )
  5353. return false;
  5354. break;
  5355. case ITEMID_NEURALIZER:
  5356. if( !mapdata->flag[MF_RESET] )
  5357. return false;
  5358. break;
  5359. }
  5360. if( itemdb_group.item_exists(IG_MERCENARY, nameid) && sd->md != NULL )
  5361. return false; // Mercenary Scrolls
  5362. if( item->flag.group || item->type == IT_CASH) { //safe check type cash disappear when overweight [Napster]
  5363. if( pc_is90overweight(sd) ) {
  5364. clif_msg(sd, ITEM_CANT_OBTAIN_WEIGHT);
  5365. return false;
  5366. }
  5367. if( !pc_inventoryblank(sd) ) {
  5368. clif_messagecolor(&sd->bl, color_table[COLOR_RED], msg_txt(sd, 732), false, SELF); //Item cannot be open when inventory is full
  5369. return false;
  5370. }
  5371. }
  5372. //Gender check
  5373. if(item->sex != SEX_BOTH && sd->status.sex != item->sex)
  5374. return false;
  5375. //Required level check
  5376. if(item->elv && sd->status.base_level < (unsigned int)item->elv)
  5377. return false;
  5378. if(item->elvmax && sd->status.base_level > (unsigned int)item->elvmax)
  5379. return false;
  5380. //Not equipable by class. [Skotlex]
  5381. if (!pc_job_can_use_item(sd,item))
  5382. return false;
  5383. if (sd->sc.cant.consume)
  5384. return false;
  5385. if (!pc_isItemClass(sd,item))
  5386. return false;
  5387. //Dead Branch items
  5388. if( item->flag.dead_branch )
  5389. log_branch(sd);
  5390. return true;
  5391. }
  5392. /*==========================================
  5393. * Last checks to use an item.
  5394. * Return:
  5395. * 0 = fail
  5396. * 1 = success
  5397. *------------------------------------------*/
  5398. int pc_useitem(map_session_data *sd,int n)
  5399. {
  5400. t_tick tick = gettick();
  5401. int amount;
  5402. t_itemid nameid;
  5403. struct script_code *script;
  5404. struct item item;
  5405. struct item_data *id;
  5406. nullpo_ret(sd);
  5407. if (sd->state.mail_writing)
  5408. return 0;
  5409. if (sd->npc_id) {
  5410. if (sd->progressbar.npc_id) {
  5411. clif_progressbar_abort(sd);
  5412. return 0; // First item use attempt cancels the progress bar
  5413. }
  5414. if( pc_hasprogress( sd, WIP_DISABLE_SKILLITEM ) || !sd->npc_item_flag ){
  5415. #ifdef RENEWAL
  5416. clif_msg( sd, WORK_IN_PROGRESS );
  5417. #endif
  5418. return 0;
  5419. }
  5420. }
  5421. item = sd->inventory.u.items_inventory[n];
  5422. id = sd->inventory_data[n];
  5423. if (item.nameid == 0 || item.amount <= 0)
  5424. return 0;
  5425. if( !pc_isUseitem(sd,n) )
  5426. return 0;
  5427. // Store information for later use before it is lost (via pc_delitem) [Paradox924X]
  5428. nameid = id->nameid;
  5429. if (nameid != ITEMID_NAUTHIZ && sd->sc.opt1 > 0 && sd->sc.opt1 != OPT1_STONEWAIT && sd->sc.opt1 != OPT1_BURNING)
  5430. return 0;
  5431. /* Items with delayed consume are not meant to work while in mounts except reins of mount(12622) */
  5432. if( id->flag.delay_consume > 0 ) {
  5433. if( nameid != ITEMID_REINS_OF_MOUNT && sd->sc.getSCE(SC_ALL_RIDING) )
  5434. return 0;
  5435. else if( pc_issit(sd) )
  5436. return 0;
  5437. }
  5438. //Since most delay-consume items involve using a "skill-type" target cursor,
  5439. //perform a skill-use check before going through. [Skotlex]
  5440. //resurrection was picked as testing skill, as a non-offensive, generic skill, it will do.
  5441. //FIXME: Is this really needed here? It'll be checked in unit.cpp after all and this prevents skill items using when silenced [Inkfish]
  5442. if( id->flag.delay_consume > 0 && ( sd->ud.skilltimer != INVALID_TIMER /*|| !status_check_skilluse(&sd->bl, &sd->bl, ALL_RESURRECTION, 0)*/ ) )
  5443. return 0;
  5444. if( id->delay.duration > 0 && !pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && pc_itemcd_check(sd, id, tick, n))
  5445. return 0;
  5446. /* on restricted maps the item is consumed but the effect is not used */
  5447. if (!pc_has_permission(sd,PC_PERM_ITEM_UNCONDITIONAL) && itemdb_isNoEquip(id,sd->bl.m)) {
  5448. clif_msg(sd,ITEM_CANT_USE_AREA); // This item cannot be used within this area
  5449. if( battle_config.allow_consume_restricted_item && id->flag.delay_consume > 0 ) { //need confirmation for delayed consumption items
  5450. clif_useitemack(sd,n,item.amount-1,true);
  5451. pc_delitem(sd,n,1,1,0,LOG_TYPE_CONSUME);
  5452. }
  5453. return 0;/* regardless, effect is not run */
  5454. }
  5455. if (pet_db_search(id->nameid, PET_CATCH) != nullptr && map_getmapflag(sd->bl.m, MF_NOPETCAPTURE)) {
  5456. clif_displaymessage(sd->fd, msg_txt(sd, 669)); // You can't catch any pet on this map.
  5457. return 0;
  5458. }
  5459. sd->itemid = item.nameid;
  5460. sd->itemindex = n;
  5461. if(sd->catch_target_class != PET_CATCH_FAIL) //Abort pet catching.
  5462. sd->catch_target_class = PET_CATCH_FAIL;
  5463. amount = item.amount;
  5464. script = id->script;
  5465. //Check if the item is to be consumed immediately [Skotlex]
  5466. if (id->flag.delay_consume > 0)
  5467. clif_useitemack(sd, n, amount, true);
  5468. else
  5469. {
  5470. if( item.expire_time == 0 && nameid != ITEMID_REINS_OF_MOUNT )
  5471. {
  5472. clif_useitemack(sd, n, amount - 1, true);
  5473. pc_delitem(sd, n, 1, 1, 0, LOG_TYPE_CONSUME); // Rental Usable Items are not deleted until expiration
  5474. }
  5475. else
  5476. clif_useitemack(sd, n, 0, false);
  5477. }
  5478. if (item.card[0]==CARD0_CREATE && pc_famerank(MakeDWord(item.card[2],item.card[3]), MAPID_ALCHEMIST))
  5479. potion_flag = 2; // Famous player's potions have 50% more efficiency
  5480. //Update item use time.
  5481. sd->canuseitem_tick = tick + battle_config.item_use_interval;
  5482. if( itemdb_group.item_exists(IG_CASH_FOOD, nameid) )
  5483. sd->canusecashfood_tick = tick + battle_config.cashfood_use_interval;
  5484. run_script(script,0,sd->bl.id,fake_nd->bl.id);
  5485. potion_flag = 0;
  5486. return 1;
  5487. }
  5488. /**
  5489. * Add item on cart for given index.
  5490. * @param sd
  5491. * @param item
  5492. * @param amount
  5493. * @param log_type
  5494. * @return See pc.hpp::e_additem_result
  5495. */
  5496. enum e_additem_result pc_cart_additem(map_session_data *sd,struct item *item,int amount,e_log_pick_type log_type)
  5497. {
  5498. struct item_data *data;
  5499. int i,w;
  5500. nullpo_retr(ADDITEM_INVALID, sd);
  5501. nullpo_retr(ADDITEM_INVALID, item);
  5502. if(item->nameid == 0 || amount <= 0)
  5503. return ADDITEM_INVALID;
  5504. if (itemdb_ishatched_egg(item))
  5505. return ADDITEM_INVALID;
  5506. data = itemdb_search(item->nameid);
  5507. if( data->stack.cart && amount > data->stack.amount )
  5508. {// item stack limitation
  5509. return ADDITEM_STACKLIMIT;
  5510. }
  5511. if( !itemdb_cancartstore(item, pc_get_group_level(sd)) || (item->bound > BOUND_ACCOUNT && !pc_can_give_bounded_items(sd)))
  5512. { // Check item trade restrictions [Skotlex]
  5513. clif_displaymessage (sd->fd, msg_txt(sd,264));
  5514. return ADDITEM_INVALID;
  5515. }
  5516. if( (w = data->weight*amount) + sd->cart_weight > sd->cart_weight_max )
  5517. return ADDITEM_OVERWEIGHT;
  5518. i = MAX_CART;
  5519. if( itemdb_isstackable2(data) && !item->expire_time )
  5520. {
  5521. for (i = 0; i < MAX_CART; i++) {
  5522. if (sd->cart.u.items_cart[i].nameid == item->nameid
  5523. && sd->cart.u.items_cart[i].bound == item->bound
  5524. && sd->cart.u.items_cart[i].unique_id == item->unique_id
  5525. && memcmp(sd->cart.u.items_cart[i].card, item->card, sizeof(item->card)) == 0
  5526. )
  5527. break;
  5528. }
  5529. }
  5530. if( i < MAX_CART )
  5531. {// item already in cart, stack it
  5532. 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 ) )
  5533. return ADDITEM_OVERAMOUNT; // no slot
  5534. sd->cart.u.items_cart[i].amount += amount;
  5535. clif_cart_additem(sd,i,amount);
  5536. }
  5537. else
  5538. {// item not stackable or not present, add it
  5539. ARR_FIND( 0, MAX_CART, i, sd->cart.u.items_cart[i].nameid == 0 );
  5540. if( i == MAX_CART )
  5541. return ADDITEM_OVERAMOUNT; // no slot
  5542. memcpy(&sd->cart.u.items_cart[i],item,sizeof(sd->cart.u.items_cart[0]));
  5543. sd->cart.u.items_cart[i].id = 0;
  5544. sd->cart.u.items_cart[i].amount = amount;
  5545. sd->cart_num++;
  5546. clif_cart_additem(sd,i,amount);
  5547. }
  5548. sd->cart.u.items_cart[i].favorite = 0; // clear
  5549. sd->cart.u.items_cart[i].equipSwitch = 0;
  5550. log_pick_pc(sd, log_type, amount, &sd->cart.u.items_cart[i]);
  5551. sd->cart_weight += w;
  5552. clif_updatestatus(sd,SP_CARTINFO);
  5553. return ADDITEM_SUCCESS;
  5554. }
  5555. /*==========================================
  5556. * Delete item on cart for given index.
  5557. *------------------------------------------*/
  5558. void pc_cart_delitem(map_session_data *sd,int n,int amount,int type,e_log_pick_type log_type)
  5559. {
  5560. nullpo_retv(sd);
  5561. if(sd->cart.u.items_cart[n].nameid == 0 ||
  5562. sd->cart.u.items_cart[n].amount < amount)
  5563. return;
  5564. log_pick_pc(sd, log_type, -amount, &sd->cart.u.items_cart[n]);
  5565. sd->cart.u.items_cart[n].amount -= amount;
  5566. sd->cart_weight -= itemdb_weight(sd->cart.u.items_cart[n].nameid) * amount;
  5567. if(sd->cart.u.items_cart[n].amount <= 0) {
  5568. memset(&sd->cart.u.items_cart[n],0,sizeof(sd->cart.u.items_cart[0]));
  5569. sd->cart_num--;
  5570. }
  5571. if(!type) {
  5572. clif_cart_delitem(sd,n,amount);
  5573. clif_updatestatus(sd,SP_CARTINFO);
  5574. }
  5575. }
  5576. /*==========================================
  5577. * Transfer item from inventory to cart.
  5578. *------------------------------------------*/
  5579. void pc_putitemtocart(map_session_data *sd,int idx,int amount)
  5580. {
  5581. nullpo_retv(sd);
  5582. if (idx < 0 || idx >= MAX_INVENTORY) //Invalid index check [Skotlex]
  5583. return;
  5584. struct item *item_data = &sd->inventory.u.items_inventory[idx];
  5585. if( item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend )
  5586. return;
  5587. if( item_data->equipSwitch ){
  5588. clif_msg( sd, C_ITEM_EQUIP_SWITCH );
  5589. return;
  5590. }
  5591. enum e_additem_result flag = pc_cart_additem(sd,item_data,amount,LOG_TYPE_NONE);
  5592. if (flag == ADDITEM_SUCCESS)
  5593. pc_delitem(sd,idx,amount,0,5,LOG_TYPE_NONE);
  5594. else {
  5595. clif_cart_additem_ack(sd, (flag == ADDITEM_OVERAMOUNT) ? ADDITEM_TO_CART_FAIL_COUNT : ADDITEM_TO_CART_FAIL_WEIGHT);
  5596. clif_additem(sd, idx, amount, 0);
  5597. clif_delitem(sd, idx, amount, 0);
  5598. }
  5599. }
  5600. /*==========================================
  5601. * Get number of item in cart.
  5602. * Return:
  5603. -1 = itemid not found or no amount found
  5604. x = remaining itemid on cart after get
  5605. *------------------------------------------*/
  5606. int pc_cartitem_amount(map_session_data* sd, int idx, int amount)
  5607. {
  5608. struct item* item_data;
  5609. nullpo_retr(-1, sd);
  5610. item_data = &sd->cart.u.items_cart[idx];
  5611. if( item_data->nameid == 0 || item_data->amount == 0 )
  5612. return -1;
  5613. return item_data->amount - amount;
  5614. }
  5615. /*==========================================
  5616. * Retrieve an item at index idx from cart.
  5617. *------------------------------------------*/
  5618. void pc_getitemfromcart(map_session_data *sd,int idx,int amount)
  5619. {
  5620. nullpo_retv(sd);
  5621. if (idx < 0 || idx >= MAX_CART) //Invalid index check [Skotlex]
  5622. return;
  5623. struct item *item_data=&sd->cart.u.items_cart[idx];
  5624. if (item_data->nameid == 0 || amount < 1 || item_data->amount < amount || sd->state.vending || sd->state.prevend)
  5625. return;
  5626. enum e_additem_result flag = pc_additem(sd, item_data, amount, LOG_TYPE_NONE);
  5627. if (flag == ADDITEM_SUCCESS)
  5628. pc_cart_delitem(sd, idx, amount, 0, LOG_TYPE_NONE);
  5629. else {
  5630. clif_cart_delitem(sd, idx, amount);
  5631. clif_additem(sd, idx, amount, flag);
  5632. clif_cart_additem(sd, idx, amount);
  5633. }
  5634. }
  5635. /*==========================================
  5636. * Bound Item Check
  5637. * Type:
  5638. * 1 Account Bound
  5639. * 2 Guild Bound
  5640. * 3 Party Bound
  5641. * 4 Character Bound
  5642. *------------------------------------------*/
  5643. int pc_bound_chk(TBL_PC *sd,enum bound_type type,int *idxlist)
  5644. {
  5645. int i = 0, j = 0;
  5646. for(i = 0; i < MAX_INVENTORY; i++) {
  5647. 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) {
  5648. idxlist[j] = i;
  5649. j++;
  5650. }
  5651. }
  5652. return j;
  5653. }
  5654. /*==========================================
  5655. * Display item stolen msg to player sd
  5656. *------------------------------------------*/
  5657. int pc_show_steal(struct block_list *bl,va_list ap)
  5658. {
  5659. map_session_data *sd;
  5660. t_itemid itemid;
  5661. char output[100];
  5662. sd=va_arg(ap,map_session_data *);
  5663. itemid=va_arg(ap,int);
  5664. std::shared_ptr<item_data> id = item_db.find(itemid);
  5665. if(id == nullptr)
  5666. sprintf(output,"%s stole an Unknown Item (id: %u).",sd->status.name, itemid);
  5667. else
  5668. sprintf(output,"%s stole %s.",sd->status.name,id->ename.c_str());
  5669. clif_displaymessage( ((map_session_data *)bl)->fd, output);
  5670. return 0;
  5671. }
  5672. /**
  5673. * Steal an item from bl (mob).
  5674. * @param sd: Player data
  5675. * @param bl: Object to steal from
  5676. * @param skill_lv: Level of skill used
  5677. * @return True on success or false otherwise
  5678. */
  5679. bool pc_steal_item(map_session_data *sd,struct block_list *bl, uint16 skill_lv)
  5680. {
  5681. int i;
  5682. t_itemid itemid;
  5683. double rate;
  5684. unsigned char flag = 0;
  5685. struct status_data *sd_status, *md_status;
  5686. struct mob_data *md;
  5687. if(!sd || !bl || bl->type!=BL_MOB)
  5688. return false;
  5689. md = (TBL_MOB *)bl;
  5690. if(md->state.steal_flag == UCHAR_MAX || ( md->sc.opt1 && md->sc.opt1 != OPT1_BURNING ) ) //already stolen from / status change check
  5691. return false;
  5692. sd_status= status_get_status_data(&sd->bl);
  5693. md_status= status_get_status_data(bl);
  5694. if (md->master_id || status_has_mode(md_status, MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE) ||
  5695. map_getmapflag(bl->m, MF_NOMOBLOOT) || // check noloot map flag [Lorky]
  5696. (battle_config.skill_steal_max_tries && //Reached limit of steal attempts. [Lupus]
  5697. md->state.steal_flag++ >= battle_config.skill_steal_max_tries)
  5698. ) { //Can't steal from
  5699. md->state.steal_flag = UCHAR_MAX;
  5700. return false;
  5701. }
  5702. // base skill success chance (percentual)
  5703. rate = (sd_status->dex - md_status->dex)/2 + skill_lv*6 + 4;
  5704. rate += sd->bonus.add_steal_rate;
  5705. if( rate < 1
  5706. #ifdef RENEWAL
  5707. || rnd()%100 >= rate
  5708. #endif
  5709. )
  5710. return false;
  5711. // Try dropping one item, in the order from first to last possible slot.
  5712. // Droprate is affected by the skill success rate.
  5713. for( i = 0; i < MAX_MOB_DROP; i++ )
  5714. if( item_db.exists(md->db->dropitem[i].nameid) && !md->db->dropitem[i].steal_protected && rnd() % 10000 < md->db->dropitem[i].rate
  5715. #ifndef RENEWAL
  5716. * rate/100.
  5717. #endif
  5718. )
  5719. break;
  5720. if( i == MAX_MOB_DROP )
  5721. return false;
  5722. itemid = md->db->dropitem[i].nameid;
  5723. struct item tmp_item = {};
  5724. tmp_item.nameid = itemid;
  5725. tmp_item.amount = 1;
  5726. tmp_item.identify = itemdb_isidentified(itemid);
  5727. if( battle_config.skill_steal_random_options ){
  5728. mob_setdropitem_option( &tmp_item, &md->db->dropitem[i] );
  5729. }
  5730. flag = pc_additem(sd,&tmp_item,1,LOG_TYPE_PICKDROP_PLAYER);
  5731. //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?
  5732. md->state.steal_flag = UCHAR_MAX; //you can't steal from this mob any more
  5733. if(flag) { //Failed to steal due to overweight
  5734. clif_additem(sd,0,0,flag);
  5735. return false;
  5736. }
  5737. if(battle_config.show_steal_in_same_party)
  5738. party_foreachsamemap(pc_show_steal,sd,AREA_SIZE,sd,tmp_item.nameid);
  5739. //Logs items, Stolen from mobs [Lupus]
  5740. log_pick_mob(md, LOG_TYPE_STEAL, -1, &tmp_item);
  5741. //A Rare Steal Global Announce by Lupus
  5742. if(md->db->dropitem[i].rate <= battle_config.rare_drop_announce) {
  5743. struct item_data *i_data;
  5744. char message[128];
  5745. i_data = itemdb_search(itemid);
  5746. 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);
  5747. //MSG: "'%s' stole %s's %s (chance: %0.02f%%)"
  5748. intif_broadcast(message, strlen(message) + 1, BC_DEFAULT);
  5749. }
  5750. return true;
  5751. }
  5752. /*==========================================
  5753. * Stole zeny from bl (mob)
  5754. * return
  5755. * 0 = fail
  5756. * 1 = success
  5757. *------------------------------------------*/
  5758. int pc_steal_coin(map_session_data *sd,struct block_list *target)
  5759. {
  5760. int rate, target_lv;
  5761. struct mob_data *md;
  5762. if(!sd || !target || target->type != BL_MOB)
  5763. return 0;
  5764. md = (TBL_MOB*)target;
  5765. target_lv = status_get_lv(target);
  5766. if (md->state.steal_coin_flag || md->sc.getSCE(SC_STONE) || md->sc.getSCE(SC_FREEZE) || md->sc.getSCE(SC_HANDICAPSTATE_FROSTBITE) ||
  5767. md->sc.getSCE(SC_HANDICAPSTATE_SWOONING) || md->sc.getSCE(SC_HANDICAPSTATE_LIGHTNINGSTRIKE) || md->sc.getSCE(SC_HANDICAPSTATE_CRYSTALLIZATION) ||
  5768. status_bl_has_mode(target,MD_STATUSIMMUNE) || util::vector_exists(status_get_race2(&md->bl), RC2_TREASURE))
  5769. return 0;
  5770. rate = sd->battle_status.dex / 2 + 2 * (sd->status.base_level - target_lv) + (10 * pc_checkskill(sd, RG_STEALCOIN)) + sd->battle_status.luk / 2;
  5771. if(rnd()%1000 < rate)
  5772. {
  5773. // Zeny Steal Amount: (rnd() % (10 * target_lv + 1 - 8 * target_lv)) + 8 * target_lv
  5774. int amount = (rnd() % (2 * target_lv + 1)) + 8 * target_lv; // Reduced formula
  5775. pc_getzeny(sd, amount, LOG_TYPE_STEAL, NULL);
  5776. md->state.steal_coin_flag = 1;
  5777. return 1;
  5778. }
  5779. return 0;
  5780. }
  5781. /*==========================================
  5782. * Set's a player position.
  5783. * @param sd
  5784. * @param mapindex
  5785. * @param x
  5786. * @param y
  5787. * @param clrtype
  5788. * @return SETPOS_OK Success
  5789. * SETPOS_MAPINDEX Invalid map index
  5790. * SETPOS_NO_MAPSERVER Map not in this map-server, and failed to locate alternate map-server.
  5791. * SETPOS_AUTOTRADE Player is in autotrade state
  5792. *------------------------------------------*/
  5793. enum e_setpos pc_setpos(map_session_data* sd, unsigned short mapindex, int x, int y, clr_type clrtype)
  5794. {
  5795. nullpo_retr(SETPOS_OK,sd);
  5796. if( !mapindex || !mapindex_id2name(mapindex) ) {
  5797. ShowDebug("pc_setpos: Passed mapindex(%d) is invalid!\n", mapindex);
  5798. return SETPOS_MAPINDEX;
  5799. }
  5800. if ( sd->state.autotrade && (sd->vender_id || sd->buyer_id) ) // Player with autotrade just causes clif glitch! @ FIXME
  5801. return SETPOS_AUTOTRADE;
  5802. if( battle_config.revive_onwarp && pc_isdead(sd) ) { //Revive dead people before warping them
  5803. pc_setstand(sd, true);
  5804. pc_setrestartvalue(sd,1);
  5805. }
  5806. int16 m = map_mapindex2mapid(mapindex);
  5807. struct map_data *mapdata = map_getmapdata(m);
  5808. status_change *sc = status_get_sc(&sd->bl);
  5809. sd->state.changemap = (sd->mapindex != mapindex);
  5810. sd->state.warping = 1;
  5811. sd->state.workinprogress = WIP_DISABLE_NONE;
  5812. sd->state.mail_writing = false;
  5813. sd->state.refineui_open = false;
  5814. if( sd->state.changemap ) { // Misc map-changing settings
  5815. int curr_map_instance_id = map_getmapdata(sd->bl.m)->instance_id, new_map_instance_id = (mapdata ? mapdata->instance_id : 0);
  5816. if (curr_map_instance_id != new_map_instance_id) {
  5817. if (curr_map_instance_id > 0) { // Update instance timer for the map on leave
  5818. instance_delusers(curr_map_instance_id);
  5819. sd->instance_mode = util::umap_find(instances, curr_map_instance_id)->mode; // Store mode for instance destruction button checks
  5820. }
  5821. if (new_map_instance_id > 0) // Update instance timer for the map on enter
  5822. instance_addusers(new_map_instance_id);
  5823. }
  5824. if (sd->bg_id && mapdata && !mapdata->flag[MF_BATTLEGROUND]) // Moving to a map that isn't a Battlegrounds
  5825. bg_team_leave(sd, false, true);
  5826. sd->state.pmap = sd->bl.m;
  5827. if (sc && sc->count) { // Cancel some map related stuff.
  5828. if (sc->cant.warp)
  5829. return SETPOS_MAPINDEX; // You may not get out!
  5830. for (const auto &it : status_db) {
  5831. if (sc->getSCE(it.first)) {
  5832. if (it.second->flag[SCF_REMOVEONMAPWARP])
  5833. status_change_end(&sd->bl, static_cast<sc_type>(it.first));
  5834. if (it.second->flag[SCF_RESTARTONMAPWARP] && it.second->skill_id > 0) {
  5835. status_change_entry *sce = sd->sc.getSCE(it.first);
  5836. if (sce->timer != INVALID_TIMER)
  5837. delete_timer(sce->timer, status_change_timer);
  5838. sce->timer = add_timer(gettick() + skill_get_time(it.second->skill_id, sce->val1), status_change_timer, sd->bl.id, it.first);
  5839. }
  5840. }
  5841. }
  5842. }
  5843. for(int i = 0; i < EQI_MAX; i++ ) {
  5844. if( sd->equip_index[i] >= 0 )
  5845. if( pc_isequip(sd,sd->equip_index[i]) )
  5846. pc_unequipitem(sd,sd->equip_index[i],2);
  5847. }
  5848. if (battle_config.clear_unit_onwarp&BL_PC)
  5849. skill_clear_unitgroup(&sd->bl);
  5850. if( battle_config.loose_ap_on_map && mapdata_flag_vs( mapdata ) ){
  5851. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  5852. }
  5853. party_send_dot_remove(sd); //minimap dot fix [Kevin]
  5854. guild_send_dot_remove(sd);
  5855. bg_send_dot_remove(sd);
  5856. if (sd->regen.state.gc)
  5857. sd->regen.state.gc = 0;
  5858. if (mapdata) {
  5859. // make sure vending is allowed here
  5860. if (sd->state.vending && mapdata->flag[MF_NOVENDING]) {
  5861. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5862. vending_closevending(sd);
  5863. }
  5864. // make sure buyingstore is allowed here
  5865. if (sd->state.buyingstore && mapdata->flag[MF_NOBUYINGSTORE]) {
  5866. clif_displaymessage(sd->fd, msg_txt(sd, 276)); // "You can't open a shop on this map"
  5867. buyingstore_close(sd);
  5868. }
  5869. }
  5870. channel_pcquit(sd,4); //quit map chan
  5871. // Remove Cloaked NPC if changing to another map
  5872. for (auto it = sd->cloaked_npc.begin(); it != sd->cloaked_npc.end(); ++it) {
  5873. block_list *npc_bl = map_id2bl(*it);
  5874. if (npc_bl && npc_bl->m != m) {
  5875. sd->cloaked_npc.erase(it);
  5876. break;
  5877. }
  5878. }
  5879. }
  5880. if( m < 0 )
  5881. {
  5882. uint32 ip;
  5883. uint16 port;
  5884. struct script_state *st;
  5885. //if can't find any map-servers, just abort setting position.
  5886. if(!sd->mapindex || map_mapname2ipport(mapindex,&ip,&port))
  5887. return SETPOS_NO_MAPSERVER;
  5888. if (sd->npc_id){
  5889. npc_event_dequeue(sd,false);
  5890. st = sd->st;
  5891. }else{
  5892. st = nullptr;
  5893. }
  5894. if (sd->bg_id) // Switching map servers, remove from bg
  5895. bg_team_leave(sd, false, true);
  5896. if (sd->state.vending) // Stop vending
  5897. vending_closevending(sd);
  5898. if (sd->state.buyingstore) // Stop buyingstore
  5899. buyingstore_close(sd);
  5900. npc_script_event(sd, NPCE_LOGOUT);
  5901. //remove from map, THEN change x/y coordinates
  5902. unit_remove_map_pc(sd,clrtype);
  5903. sd->mapindex = mapindex;
  5904. sd->bl.x=x;
  5905. sd->bl.y=y;
  5906. pc_clean_skilltree(sd);
  5907. chrif_save(sd, CSAVE_CHANGE_MAPSERV|CSAVE_INVENTORY|CSAVE_CART);
  5908. chrif_changemapserver(sd, ip, (short)port);
  5909. //Free session data from this map server [Kevin]
  5910. unit_free_pc(sd);
  5911. if( st ){
  5912. // Has to be done here, because otherwise unit_free_pc will free the stack already
  5913. st->state = END;
  5914. }
  5915. return SETPOS_OK;
  5916. }
  5917. if( x < 0 || x >= mapdata->xs || y < 0 || y >= mapdata->ys )
  5918. {
  5919. 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);
  5920. x = y = 0; // make it random
  5921. }
  5922. if( x == 0 && y == 0 ) { // pick a random walkable cell
  5923. int c=0;
  5924. do {
  5925. x = rnd()%(mapdata->xs-2)+1;
  5926. y = rnd()%(mapdata->ys-2)+1;
  5927. c++;
  5928. if(c > (mapdata->xs * mapdata->ys)*3){ //force out
  5929. 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));
  5930. return SETPOS_OK; //preventing warp
  5931. //break; //allow warp anyway
  5932. }
  5933. } while(map_getcell(m,x,y,CELL_CHKNOPASS) || (!battle_config.teleport_on_portal && npc_check_areanpc(1,m,x,y,1)));
  5934. }
  5935. if (sd->state.vending && map_getcell(m,x,y,CELL_CHKNOVENDING)) {
  5936. clif_displaymessage (sd->fd, msg_txt(sd,204)); // "You can't open a shop on this cell."
  5937. vending_closevending(sd);
  5938. }
  5939. if (sd->state.buyingstore && map_getcell(m, x, y, CELL_CHKNOBUYINGSTORE)) {
  5940. clif_displaymessage(sd->fd, msg_txt(sd, 204)); // "You can't open a shop on this cell."
  5941. buyingstore_close(sd);
  5942. }
  5943. if(sd->bl.prev != NULL){
  5944. unit_remove_map_pc(sd,clrtype);
  5945. clif_changemap(sd,m,x,y); // [MouseJstr]
  5946. } else if(sd->state.active) //Tag player for rewarping after map-loading is done. [Skotlex]
  5947. sd->state.rewarp = 1;
  5948. sd->mapindex = mapindex;
  5949. sd->bl.m = m;
  5950. sd->bl.x = sd->ud.to_x = x;
  5951. sd->bl.y = sd->ud.to_y = y;
  5952. if( sd->status.guild_id > 0 && mapdata->flag[MF_GVG_CASTLE] )
  5953. { // Increased guild castle regen [Valaris]
  5954. std::shared_ptr<guild_castle> gc = castle_db.mapindex2gc(sd->mapindex);
  5955. if(gc && gc->guild_id == sd->status.guild_id)
  5956. sd->regen.state.gc = 1;
  5957. }
  5958. if( sd->status.pet_id > 0 && sd->pd && sd->pd->pet.intimate > 0 )
  5959. {
  5960. sd->pd->bl.m = m;
  5961. sd->pd->bl.x = sd->pd->ud.to_x = x;
  5962. sd->pd->bl.y = sd->pd->ud.to_y = y;
  5963. sd->pd->ud.dir = sd->ud.dir;
  5964. }
  5965. if( hom_is_active(sd->hd) )
  5966. {
  5967. sd->hd->bl.m = m;
  5968. sd->hd->bl.x = sd->hd->ud.to_x = x;
  5969. sd->hd->bl.y = sd->hd->ud.to_y = y;
  5970. sd->hd->ud.dir = sd->ud.dir;
  5971. }
  5972. if( sd->md )
  5973. {
  5974. sd->md->bl.m = m;
  5975. sd->md->bl.x = sd->md->ud.to_x = x;
  5976. sd->md->bl.y = sd->md->ud.to_y = y;
  5977. sd->md->ud.dir = sd->ud.dir;
  5978. }
  5979. if( sd->ed ) {
  5980. sd->ed->bl.m = m;
  5981. sd->ed->bl.x = sd->ed->ud.to_x = x;
  5982. sd->ed->bl.y = sd->ed->ud.to_y = y;
  5983. sd->ed->ud.dir = sd->ud.dir;
  5984. }
  5985. pc_cell_basilica(sd);
  5986. //check if we gonna be rewarped [lighta]
  5987. if(npc_check_areanpc(1,m,x,y,0)){
  5988. sd->count_rewarp++;
  5989. }
  5990. else
  5991. sd->count_rewarp = 0;
  5992. if (sd->state.vending)
  5993. vending_update(*sd);
  5994. if (sd->state.buyingstore)
  5995. buyingstore_update(*sd);
  5996. return SETPOS_OK;
  5997. }
  5998. enum e_setpos pc_setpos_savepoint( map_session_data& sd, clr_type clrtype ){
  5999. struct map_data *mapdata = map_getmapdata( sd.bl.m );
  6000. if( mapdata != nullptr && mapdata->flag[MF_NOSAVE] && mapdata->save.map ){
  6001. return pc_setpos( &sd, mapdata->save.map, mapdata->save.x, mapdata->save.y, clrtype );
  6002. }else{
  6003. return pc_setpos( &sd, mapindex_name2id( sd.status.save_point.map ), sd.status.save_point.x, sd.status.save_point.y, clrtype );
  6004. }
  6005. }
  6006. /*==========================================
  6007. * Warp player sd to random location on current map.
  6008. * May fail if no walkable cell found (1000 attempts).
  6009. * Return:
  6010. * 0 = Success
  6011. * 1,2,3 = Fail
  6012. *------------------------------------------*/
  6013. char pc_randomwarp(map_session_data *sd, clr_type type, bool ignore_mapflag)
  6014. {
  6015. int x,y,i=0;
  6016. nullpo_ret(sd);
  6017. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  6018. if (mapdata->flag[MF_NOTELEPORT] && !ignore_mapflag) //Teleport forbidden
  6019. return 3;
  6020. do {
  6021. x = rnd()%(mapdata->xs-2)+1;
  6022. y = rnd()%(mapdata->ys-2)+1;
  6023. } 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);
  6024. if (i < 1000)
  6025. return pc_setpos(sd,mapdata->index,x,y,type);
  6026. return 3;
  6027. }
  6028. /*==========================================
  6029. * Records a memo point at sd's current position
  6030. * pos - entry to replace, (-1: shift oldest entry out)
  6031. *------------------------------------------*/
  6032. bool pc_memo(map_session_data* sd, int pos)
  6033. {
  6034. int skill;
  6035. nullpo_ret(sd);
  6036. // check mapflags
  6037. 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) ) {
  6038. clif_skill_teleportmessage(sd, 1); // "Saved point cannot be memorized."
  6039. return false;
  6040. }
  6041. // check inputs
  6042. if( pos < -1 || pos >= MAX_MEMOPOINTS )
  6043. return false; // invalid input
  6044. // check required skill level
  6045. skill = pc_checkskill(sd, AL_WARP);
  6046. if( skill < 1 ) {
  6047. clif_skill_memomessage(sd,2); // "You haven't learned Warp."
  6048. return false;
  6049. }
  6050. if( skill < 2 || skill - 2 < pos ) {
  6051. clif_skill_memomessage(sd,1); // "Skill Level is not high enough."
  6052. return false;
  6053. }
  6054. if( pos == -1 )
  6055. {
  6056. uint8 i;
  6057. const char* mapname = map_mapid2mapname( sd->bl.m );
  6058. // prevent memo-ing the same map multiple times
  6059. ARR_FIND( 0, MAX_MEMOPOINTS, i, strncmp( sd->status.memo_point[i].map, mapname, sizeof( sd->status.memo_point[i].map ) ) == 0 );
  6060. memmove( &sd->status.memo_point[1], &sd->status.memo_point[0], ( u8min( i, MAX_MEMOPOINTS - 1 ) ) * sizeof( struct s_point_str ) );
  6061. pos = 0;
  6062. }
  6063. if( map_getmapdata(sd->bl.m)->instance_id ) {
  6064. clif_displaymessage( sd->fd, msg_txt(sd,384) ); // You cannot create a memo in an instance.
  6065. return false;
  6066. }
  6067. safestrncpy( sd->status.memo_point[pos].map, map_mapid2mapname( sd->bl.m ), sizeof( sd->status.memo_point[pos].map ) );
  6068. sd->status.memo_point[pos].x = sd->bl.x;
  6069. sd->status.memo_point[pos].y = sd->bl.y;
  6070. clif_skill_memomessage(sd, 0);
  6071. return true;
  6072. }
  6073. //
  6074. // Skills
  6075. //
  6076. /**
  6077. * Get the skill current cooldown for player.
  6078. * (get the db base cooldown for skill + player specific cooldown)
  6079. * @param sd : player pointer
  6080. * @param id : skill id
  6081. * @param lv : skill lv
  6082. * @return player skill cooldown
  6083. */
  6084. int pc_get_skillcooldown(map_session_data *sd, uint16 skill_id, uint16 skill_lv) {
  6085. if (skill_id == SJ_NOVAEXPLOSING) {
  6086. status_change *sc = status_get_sc(&sd->bl);
  6087. if (sc && sc->getSCE(SC_DIMENSION))
  6088. return 0;
  6089. }
  6090. int cooldown = skill_get_cooldown(skill_id, skill_lv);
  6091. if (skill_id == SU_TUNABELLY && pc_checkskill(sd, SU_SPIRITOFSEA) > 0)
  6092. cooldown -= skill_get_time(SU_TUNABELLY, skill_lv);
  6093. for (auto &it : sd->skillcooldown) {
  6094. if (it.id == skill_id) {
  6095. cooldown += it.val;
  6096. break;
  6097. }
  6098. }
  6099. return max(0, cooldown);
  6100. }
  6101. /*==========================================
  6102. * Return player sd skill_lv learned for given skill
  6103. *------------------------------------------*/
  6104. uint8 pc_checkskill(map_session_data *sd, uint16 skill_id)
  6105. {
  6106. uint16 idx = 0;
  6107. if (sd == NULL)
  6108. return 0;
  6109. #ifdef RENEWAL
  6110. if ((idx = skill_get_index(skill_id)) == 0) {
  6111. #else
  6112. if( ( idx = skill_db.get_index( skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__ ) ) == 0 ){
  6113. if( skill_id >= RK_ENCHANTBLADE ){
  6114. // Silently fail for now -> future update planned
  6115. return 0;
  6116. }
  6117. #endif
  6118. ShowError("pc_checkskill: Invalid skill id %d (char_id=%d).\n", skill_id, sd->status.char_id);
  6119. return 0;
  6120. }
  6121. if (SKILL_CHK_GUILD(skill_id) ) {
  6122. struct guild *g;
  6123. if( sd->status.guild_id>0 && (g=sd->guild)!=NULL)
  6124. return guild_checkskill(g,skill_id);
  6125. return 0;
  6126. }
  6127. return (sd->status.skill[idx].id == skill_id) ? sd->status.skill[idx].lv : 0;
  6128. }
  6129. /**
  6130. * Returns the flag of the given skill (when learned).
  6131. * @param sd: Player data
  6132. * @param skill_id: Skill to lookup
  6133. * @return Skill flag type
  6134. */
  6135. e_skill_flag pc_checkskill_flag(map_session_data &sd, uint16 skill_id) {
  6136. uint16 idx;
  6137. #ifdef RENEWAL
  6138. if ((idx = skill_get_index(skill_id)) == 0) {
  6139. #else
  6140. if ((idx = skill_db.get_index(skill_id, skill_id >= RK_ENCHANTBLADE, __FUNCTION__, __FILE__, __LINE__)) == 0) {
  6141. if (skill_id >= RK_ENCHANTBLADE) {
  6142. // Silently fail for now -> future update planned
  6143. return SKILL_FLAG_NONE;
  6144. }
  6145. #endif
  6146. ShowError("pc_checkskill_flag: Invalid skill id %d (char_id=%d).\n", skill_id, sd.status.char_id);
  6147. return SKILL_FLAG_NONE;
  6148. }
  6149. 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;
  6150. }
  6151. /**
  6152. * Returns the amount of skill points invested in a Summoner's Power of Sea/Land/Life
  6153. * @param sd: Player data
  6154. * @param type: Summoner Power Type
  6155. * @return Skill points invested
  6156. */
  6157. uint8 pc_checkskill_summoner(map_session_data *sd, e_summoner_power_type type) {
  6158. if (sd == nullptr)
  6159. return 0;
  6160. uint8 count = 0;
  6161. switch (type) {
  6162. case SUMMONER_POWER_SEA:
  6163. count = pc_checkskill(sd, SU_TUNABELLY) + pc_checkskill(sd, SU_TUNAPARTY) + pc_checkskill(sd, SU_BUNCHOFSHRIMP) + pc_checkskill(sd, SU_FRESHSHRIMP) +
  6164. pc_checkskill(sd, SU_GROOMING) + pc_checkskill(sd, SU_PURRING) + pc_checkskill(sd, SU_SHRIMPARTY);
  6165. break;
  6166. case SUMMONER_POWER_LAND:
  6167. 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) +
  6168. pc_checkskill(sd, SU_CHATTERING) + pc_checkskill(sd, SU_MEOWMEOW) + pc_checkskill(sd, SU_NYANGGRASS);
  6169. break;
  6170. case SUMMONER_POWER_LIFE:
  6171. count = pc_checkskill(sd, SU_SCAROFTAROU) + pc_checkskill(sd, SU_PICKYPECK) + pc_checkskill(sd, SU_ARCLOUSEDASH) + pc_checkskill(sd, SU_LUNATICCARROTBEAT) +
  6172. pc_checkskill(sd, SU_HISS) + pc_checkskill(sd, SU_POWEROFFLOCK) + pc_checkskill(sd, SU_SVG_SPIRIT);
  6173. break;
  6174. }
  6175. return count;
  6176. }
  6177. /**
  6178. * Checks for Imperial Guard's passive skills.
  6179. * @param sd: Player data
  6180. * @param flag:
  6181. * Flag&1 = IG_SHIELD_MASTERY
  6182. * Flag&2 = IG_SPEAR_SWORD_M
  6183. */
  6184. uint8 pc_checkskill_imperial_guard(map_session_data *sd, short flag)
  6185. {
  6186. nullpo_retr(0, sd);
  6187. uint8 count = 0;
  6188. if (flag&1 && sd->status.shield > 0)
  6189. count += pc_checkskill(sd, IG_SHIELD_MASTERY);
  6190. if (flag&2 && (sd->status.weapon == W_1HSWORD || sd->status.weapon == W_1HSPEAR || sd->status.weapon == W_2HSPEAR))
  6191. count += pc_checkskill(sd, IG_SPEAR_SWORD_M);
  6192. return count;
  6193. }
  6194. /**
  6195. * Check if we still have the correct weapon to continue the skill (actually status)
  6196. * If not ending it
  6197. * @param sd
  6198. * @return 0:error, 1:check done
  6199. */
  6200. static void pc_checkallowskill(map_session_data *sd)
  6201. {
  6202. nullpo_retv(sd);
  6203. if(!sd->sc.count)
  6204. return;
  6205. for (const auto &it : status_db) {
  6206. sc_type status = it.second->type;
  6207. std::bitset<SCF_MAX> flag = it.second->flag;
  6208. if (flag[SCF_REQUIREWEAPON]) { // Skills requiring specific weapon types
  6209. if (status == SC_DANCING && !battle_config.dancing_weaponswitch_fix)
  6210. continue;
  6211. if (sd->sc.getSCE(status) && !pc_check_weapontype(sd, skill_get_weapontype(it.second->skill_id)))
  6212. status_change_end(&sd->bl, status);
  6213. }
  6214. if (flag[SCF_REQUIRESHIELD]) { // Skills requiring a shield
  6215. if (sd->sc.getSCE(status) && sd->status.shield <= 0)
  6216. status_change_end(&sd->bl, status);
  6217. }
  6218. }
  6219. }
  6220. /*==========================================
  6221. * Return equipped index of item on player sd at pos
  6222. * Return
  6223. * -1 : Nothing equipped
  6224. * idx : (this index could be used in inventory to found item_data)
  6225. *------------------------------------------*/
  6226. short pc_checkequip(map_session_data *sd,int pos, bool checkall)
  6227. {
  6228. uint8 i;
  6229. nullpo_retr(-1, sd);
  6230. for(i=0;i<EQI_MAX;i++){
  6231. if(pos & equip_bitmask[i]){
  6232. if( checkall && ( pos&~equip_bitmask[i] ) != 0 && sd->equip_index[i] == -1 ){
  6233. // Check all if any match is found
  6234. continue;
  6235. }
  6236. return sd->equip_index[i];
  6237. }
  6238. }
  6239. return -1;
  6240. }
  6241. /*==========================================
  6242. * Check if sd has nameid equipped somewhere
  6243. * @sd : the player session
  6244. * @nameid : id of the item to check
  6245. * @min : : see pc.hpp enum equip_index from ? to @max
  6246. * @max : see pc.hpp enum equip_index for @min to ?
  6247. * -return true,false
  6248. *------------------------------------------*/
  6249. bool pc_checkequip2(map_session_data *sd, t_itemid nameid, int min, int max)
  6250. {
  6251. int i;
  6252. for(i = min; i < max; i++) {
  6253. if(equip_bitmask[i]) {
  6254. int idx = sd->equip_index[i];
  6255. if (sd->inventory.u.items_inventory[idx].nameid == nameid)
  6256. return true;
  6257. }
  6258. }
  6259. return false;
  6260. }
  6261. /*==========================================
  6262. * Convert's from the client's lame Job ID system
  6263. * to the map server's 'makes sense' system. [Skotlex]
  6264. *------------------------------------------*/
  6265. uint64 pc_jobid2mapid(unsigned short b_class)
  6266. {
  6267. switch(b_class)
  6268. {
  6269. //Novice And 1-1 Jobs
  6270. case JOB_NOVICE: return MAPID_NOVICE;
  6271. case JOB_SWORDMAN: return MAPID_SWORDMAN;
  6272. case JOB_MAGE: return MAPID_MAGE;
  6273. case JOB_ARCHER: return MAPID_ARCHER;
  6274. case JOB_ACOLYTE: return MAPID_ACOLYTE;
  6275. case JOB_MERCHANT: return MAPID_MERCHANT;
  6276. case JOB_THIEF: return MAPID_THIEF;
  6277. case JOB_TAEKWON: return MAPID_TAEKWON;
  6278. case JOB_WEDDING: return MAPID_WEDDING;
  6279. case JOB_GUNSLINGER: return MAPID_GUNSLINGER;
  6280. case JOB_NINJA: return MAPID_NINJA;
  6281. case JOB_XMAS: return MAPID_XMAS;
  6282. case JOB_SUMMER: return MAPID_SUMMER;
  6283. case JOB_HANBOK: return MAPID_HANBOK;
  6284. case JOB_GANGSI: return MAPID_GANGSI;
  6285. case JOB_OKTOBERFEST: return MAPID_OKTOBERFEST;
  6286. case JOB_SUMMER2: return MAPID_SUMMER2;
  6287. //2-1 Jobs
  6288. case JOB_SUPER_NOVICE: return MAPID_SUPER_NOVICE;
  6289. case JOB_KNIGHT: return MAPID_KNIGHT;
  6290. case JOB_WIZARD: return MAPID_WIZARD;
  6291. case JOB_HUNTER: return MAPID_HUNTER;
  6292. case JOB_PRIEST: return MAPID_PRIEST;
  6293. case JOB_BLACKSMITH: return MAPID_BLACKSMITH;
  6294. case JOB_ASSASSIN: return MAPID_ASSASSIN;
  6295. case JOB_STAR_GLADIATOR: return MAPID_STAR_GLADIATOR;
  6296. case JOB_KAGEROU:
  6297. case JOB_OBORO: return MAPID_KAGEROUOBORO;
  6298. case JOB_REBELLION: return MAPID_REBELLION;
  6299. case JOB_DEATH_KNIGHT: return MAPID_DEATH_KNIGHT;
  6300. //2-2 Jobs
  6301. case JOB_CRUSADER: return MAPID_CRUSADER;
  6302. case JOB_SAGE: return MAPID_SAGE;
  6303. case JOB_BARD:
  6304. case JOB_DANCER: return MAPID_BARDDANCER;
  6305. case JOB_MONK: return MAPID_MONK;
  6306. case JOB_ALCHEMIST: return MAPID_ALCHEMIST;
  6307. case JOB_ROGUE: return MAPID_ROGUE;
  6308. case JOB_SOUL_LINKER: return MAPID_SOUL_LINKER;
  6309. case JOB_DARK_COLLECTOR: return MAPID_DARK_COLLECTOR;
  6310. //Trans Novice And Trans 1-1 Jobs
  6311. case JOB_NOVICE_HIGH: return MAPID_NOVICE_HIGH;
  6312. case JOB_SWORDMAN_HIGH: return MAPID_SWORDMAN_HIGH;
  6313. case JOB_MAGE_HIGH: return MAPID_MAGE_HIGH;
  6314. case JOB_ARCHER_HIGH: return MAPID_ARCHER_HIGH;
  6315. case JOB_ACOLYTE_HIGH: return MAPID_ACOLYTE_HIGH;
  6316. case JOB_MERCHANT_HIGH: return MAPID_MERCHANT_HIGH;
  6317. case JOB_THIEF_HIGH: return MAPID_THIEF_HIGH;
  6318. //Trans 2-1 Jobs
  6319. case JOB_LORD_KNIGHT: return MAPID_LORD_KNIGHT;
  6320. case JOB_HIGH_WIZARD: return MAPID_HIGH_WIZARD;
  6321. case JOB_SNIPER: return MAPID_SNIPER;
  6322. case JOB_HIGH_PRIEST: return MAPID_HIGH_PRIEST;
  6323. case JOB_WHITESMITH: return MAPID_WHITESMITH;
  6324. case JOB_ASSASSIN_CROSS: return MAPID_ASSASSIN_CROSS;
  6325. //Trans 2-2 Jobs
  6326. case JOB_PALADIN: return MAPID_PALADIN;
  6327. case JOB_PROFESSOR: return MAPID_PROFESSOR;
  6328. case JOB_CLOWN:
  6329. case JOB_GYPSY: return MAPID_CLOWNGYPSY;
  6330. case JOB_CHAMPION: return MAPID_CHAMPION;
  6331. case JOB_CREATOR: return MAPID_CREATOR;
  6332. case JOB_STALKER: return MAPID_STALKER;
  6333. //Baby Novice And Baby 1-1 Jobs
  6334. case JOB_BABY: return MAPID_BABY;
  6335. case JOB_BABY_SWORDMAN: return MAPID_BABY_SWORDMAN;
  6336. case JOB_BABY_MAGE: return MAPID_BABY_MAGE;
  6337. case JOB_BABY_ARCHER: return MAPID_BABY_ARCHER;
  6338. case JOB_BABY_ACOLYTE: return MAPID_BABY_ACOLYTE;
  6339. case JOB_BABY_MERCHANT: return MAPID_BABY_MERCHANT;
  6340. case JOB_BABY_THIEF: return MAPID_BABY_THIEF;
  6341. case JOB_BABY_TAEKWON: return MAPID_BABY_TAEKWON;
  6342. case JOB_BABY_GUNSLINGER: return MAPID_BABY_GUNSLINGER;
  6343. case JOB_BABY_NINJA: return MAPID_BABY_NINJA;
  6344. case JOB_BABY_SUMMONER: return MAPID_BABY_SUMMONER;
  6345. //Baby 2-1 Jobs
  6346. case JOB_SUPER_BABY: return MAPID_SUPER_BABY;
  6347. case JOB_BABY_KNIGHT: return MAPID_BABY_KNIGHT;
  6348. case JOB_BABY_WIZARD: return MAPID_BABY_WIZARD;
  6349. case JOB_BABY_HUNTER: return MAPID_BABY_HUNTER;
  6350. case JOB_BABY_PRIEST: return MAPID_BABY_PRIEST;
  6351. case JOB_BABY_BLACKSMITH: return MAPID_BABY_BLACKSMITH;
  6352. case JOB_BABY_ASSASSIN: return MAPID_BABY_ASSASSIN;
  6353. case JOB_BABY_STAR_GLADIATOR: return MAPID_BABY_STAR_GLADIATOR;
  6354. case JOB_BABY_REBELLION: return MAPID_BABY_REBELLION;
  6355. case JOB_BABY_KAGEROU:
  6356. case JOB_BABY_OBORO: return MAPID_BABY_KAGEROUOBORO;
  6357. //Baby 2-2 Jobs
  6358. case JOB_BABY_CRUSADER: return MAPID_BABY_CRUSADER;
  6359. case JOB_BABY_SAGE: return MAPID_BABY_SAGE;
  6360. case JOB_BABY_BARD:
  6361. case JOB_BABY_DANCER: return MAPID_BABY_BARDDANCER;
  6362. case JOB_BABY_MONK: return MAPID_BABY_MONK;
  6363. case JOB_BABY_ALCHEMIST: return MAPID_BABY_ALCHEMIST;
  6364. case JOB_BABY_ROGUE: return MAPID_BABY_ROGUE;
  6365. case JOB_BABY_SOUL_LINKER: return MAPID_BABY_SOUL_LINKER;
  6366. //3-1 Jobs
  6367. case JOB_SUPER_NOVICE_E: return MAPID_SUPER_NOVICE_E;
  6368. case JOB_RUNE_KNIGHT: return MAPID_RUNE_KNIGHT;
  6369. case JOB_WARLOCK: return MAPID_WARLOCK;
  6370. case JOB_RANGER: return MAPID_RANGER;
  6371. case JOB_ARCH_BISHOP: return MAPID_ARCH_BISHOP;
  6372. case JOB_MECHANIC: return MAPID_MECHANIC;
  6373. case JOB_GUILLOTINE_CROSS: return MAPID_GUILLOTINE_CROSS;
  6374. case JOB_STAR_EMPEROR: return MAPID_STAR_EMPEROR;
  6375. //3-2 Jobs
  6376. case JOB_ROYAL_GUARD: return MAPID_ROYAL_GUARD;
  6377. case JOB_SORCERER: return MAPID_SORCERER;
  6378. case JOB_MINSTREL:
  6379. case JOB_WANDERER: return MAPID_MINSTRELWANDERER;
  6380. case JOB_SURA: return MAPID_SURA;
  6381. case JOB_GENETIC: return MAPID_GENETIC;
  6382. case JOB_SHADOW_CHASER: return MAPID_SHADOW_CHASER;
  6383. case JOB_SOUL_REAPER: return MAPID_SOUL_REAPER;
  6384. //Trans 3-1 Jobs
  6385. case JOB_RUNE_KNIGHT_T: return MAPID_RUNE_KNIGHT_T;
  6386. case JOB_WARLOCK_T: return MAPID_WARLOCK_T;
  6387. case JOB_RANGER_T: return MAPID_RANGER_T;
  6388. case JOB_ARCH_BISHOP_T: return MAPID_ARCH_BISHOP_T;
  6389. case JOB_MECHANIC_T: return MAPID_MECHANIC_T;
  6390. case JOB_GUILLOTINE_CROSS_T: return MAPID_GUILLOTINE_CROSS_T;
  6391. //Trans 3-2 Jobs
  6392. case JOB_ROYAL_GUARD_T: return MAPID_ROYAL_GUARD_T;
  6393. case JOB_SORCERER_T: return MAPID_SORCERER_T;
  6394. case JOB_MINSTREL_T:
  6395. case JOB_WANDERER_T: return MAPID_MINSTRELWANDERER_T;
  6396. case JOB_SURA_T: return MAPID_SURA_T;
  6397. case JOB_GENETIC_T: return MAPID_GENETIC_T;
  6398. case JOB_SHADOW_CHASER_T: return MAPID_SHADOW_CHASER_T;
  6399. //Baby 3-1 Jobs
  6400. case JOB_SUPER_BABY_E: return MAPID_SUPER_BABY_E;
  6401. case JOB_BABY_RUNE_KNIGHT: return MAPID_BABY_RUNE_KNIGHT;
  6402. case JOB_BABY_WARLOCK: return MAPID_BABY_WARLOCK;
  6403. case JOB_BABY_RANGER: return MAPID_BABY_RANGER;
  6404. case JOB_BABY_ARCH_BISHOP: return MAPID_BABY_ARCH_BISHOP;
  6405. case JOB_BABY_MECHANIC: return MAPID_BABY_MECHANIC;
  6406. case JOB_BABY_GUILLOTINE_CROSS: return MAPID_BABY_GUILLOTINE_CROSS;
  6407. case JOB_BABY_STAR_EMPEROR: return MAPID_BABY_STAR_EMPEROR;
  6408. //Baby 3-2 Jobs
  6409. case JOB_BABY_ROYAL_GUARD: return MAPID_BABY_ROYAL_GUARD;
  6410. case JOB_BABY_SORCERER: return MAPID_BABY_SORCERER;
  6411. case JOB_BABY_MINSTREL:
  6412. case JOB_BABY_WANDERER: return MAPID_BABY_MINSTRELWANDERER;
  6413. case JOB_BABY_SURA: return MAPID_BABY_SURA;
  6414. case JOB_BABY_GENETIC: return MAPID_BABY_GENETIC;
  6415. case JOB_BABY_SHADOW_CHASER: return MAPID_BABY_SHADOW_CHASER;
  6416. case JOB_BABY_SOUL_REAPER: return MAPID_BABY_SOUL_REAPER;
  6417. //Doram Jobs
  6418. case JOB_SUMMONER: return MAPID_SUMMONER;
  6419. case JOB_SPIRIT_HANDLER: return MAPID_SPIRIT_HANDLER;
  6420. //4-1 Jobs
  6421. case JOB_HYPER_NOVICE: return MAPID_HYPER_NOVICE;
  6422. case JOB_DRAGON_KNIGHT: return MAPID_DRAGON_KNIGHT;
  6423. case JOB_ARCH_MAGE: return MAPID_ARCH_MAGE;
  6424. case JOB_WINDHAWK: return MAPID_WINDHAWK;
  6425. case JOB_CARDINAL: return MAPID_CARDINAL;
  6426. case JOB_MEISTER: return MAPID_MEISTER;
  6427. case JOB_SHADOW_CROSS: return MAPID_SHADOW_CROSS;
  6428. case JOB_SKY_EMPEROR: return MAPID_SKY_EMPEROR;
  6429. case JOB_NIGHT_WATCH: return MAPID_NIGHT_WATCH;
  6430. case JOB_SHINKIRO:
  6431. case JOB_SHIRANUI: return MAPID_SHINKIRO_SHIRANUI;
  6432. //4-2 Jobs
  6433. case JOB_IMPERIAL_GUARD: return MAPID_IMPERIAL_GUARD;
  6434. case JOB_ELEMENTAL_MASTER: return MAPID_ELEMENTAL_MASTER;
  6435. case JOB_INQUISITOR: return MAPID_INQUISITOR;
  6436. case JOB_TROUBADOUR:
  6437. case JOB_TROUVERE: return MAPID_TROUBADOURTROUVERE;
  6438. case JOB_BIOLO: return MAPID_BIOLO;
  6439. case JOB_ABYSS_CHASER: return MAPID_ABYSS_CHASER;
  6440. case JOB_SOUL_ASCETIC: return MAPID_SOUL_ASCETIC;
  6441. //Unknown
  6442. default:
  6443. return -1;
  6444. }
  6445. }
  6446. //Reverts the map-style class id to the client-style one.
  6447. int pc_mapid2jobid(uint64 class_, int sex)
  6448. {
  6449. switch(class_) {
  6450. //Novice And 1-1 Jobs
  6451. case MAPID_NOVICE: return JOB_NOVICE;
  6452. case MAPID_SWORDMAN: return JOB_SWORDMAN;
  6453. case MAPID_MAGE: return JOB_MAGE;
  6454. case MAPID_ARCHER: return JOB_ARCHER;
  6455. case MAPID_ACOLYTE: return JOB_ACOLYTE;
  6456. case MAPID_MERCHANT: return JOB_MERCHANT;
  6457. case MAPID_THIEF: return JOB_THIEF;
  6458. case MAPID_TAEKWON: return JOB_TAEKWON;
  6459. case MAPID_WEDDING: return JOB_WEDDING;
  6460. case MAPID_GUNSLINGER: return JOB_GUNSLINGER;
  6461. case MAPID_NINJA: return JOB_NINJA;
  6462. case MAPID_XMAS: return JOB_XMAS;
  6463. case MAPID_SUMMER: return JOB_SUMMER;
  6464. case MAPID_HANBOK: return JOB_HANBOK;
  6465. case MAPID_GANGSI: return JOB_GANGSI;
  6466. case MAPID_OKTOBERFEST: return JOB_OKTOBERFEST;
  6467. case MAPID_SUMMER2: return JOB_SUMMER2;
  6468. //2-1 Jobs
  6469. case MAPID_SUPER_NOVICE: return JOB_SUPER_NOVICE;
  6470. case MAPID_KNIGHT: return JOB_KNIGHT;
  6471. case MAPID_WIZARD: return JOB_WIZARD;
  6472. case MAPID_HUNTER: return JOB_HUNTER;
  6473. case MAPID_PRIEST: return JOB_PRIEST;
  6474. case MAPID_BLACKSMITH: return JOB_BLACKSMITH;
  6475. case MAPID_ASSASSIN: return JOB_ASSASSIN;
  6476. case MAPID_STAR_GLADIATOR: return JOB_STAR_GLADIATOR;
  6477. case MAPID_KAGEROUOBORO: return sex?JOB_KAGEROU:JOB_OBORO;
  6478. case MAPID_REBELLION: return JOB_REBELLION;
  6479. case MAPID_DEATH_KNIGHT: return JOB_DEATH_KNIGHT;
  6480. //2-2 Jobs
  6481. case MAPID_CRUSADER: return JOB_CRUSADER;
  6482. case MAPID_SAGE: return JOB_SAGE;
  6483. case MAPID_BARDDANCER: return sex?JOB_BARD:JOB_DANCER;
  6484. case MAPID_MONK: return JOB_MONK;
  6485. case MAPID_ALCHEMIST: return JOB_ALCHEMIST;
  6486. case MAPID_ROGUE: return JOB_ROGUE;
  6487. case MAPID_SOUL_LINKER: return JOB_SOUL_LINKER;
  6488. case MAPID_DARK_COLLECTOR: return JOB_DARK_COLLECTOR;
  6489. //Trans Novice And Trans 2-1 Jobs
  6490. case MAPID_NOVICE_HIGH: return JOB_NOVICE_HIGH;
  6491. case MAPID_SWORDMAN_HIGH: return JOB_SWORDMAN_HIGH;
  6492. case MAPID_MAGE_HIGH: return JOB_MAGE_HIGH;
  6493. case MAPID_ARCHER_HIGH: return JOB_ARCHER_HIGH;
  6494. case MAPID_ACOLYTE_HIGH: return JOB_ACOLYTE_HIGH;
  6495. case MAPID_MERCHANT_HIGH: return JOB_MERCHANT_HIGH;
  6496. case MAPID_THIEF_HIGH: return JOB_THIEF_HIGH;
  6497. //Trans 2-1 Jobs
  6498. case MAPID_LORD_KNIGHT: return JOB_LORD_KNIGHT;
  6499. case MAPID_HIGH_WIZARD: return JOB_HIGH_WIZARD;
  6500. case MAPID_SNIPER: return JOB_SNIPER;
  6501. case MAPID_HIGH_PRIEST: return JOB_HIGH_PRIEST;
  6502. case MAPID_WHITESMITH: return JOB_WHITESMITH;
  6503. case MAPID_ASSASSIN_CROSS: return JOB_ASSASSIN_CROSS;
  6504. //Trans 2-2 Jobs
  6505. case MAPID_PALADIN: return JOB_PALADIN;
  6506. case MAPID_PROFESSOR: return JOB_PROFESSOR;
  6507. case MAPID_CLOWNGYPSY: return sex?JOB_CLOWN:JOB_GYPSY;
  6508. case MAPID_CHAMPION: return JOB_CHAMPION;
  6509. case MAPID_CREATOR: return JOB_CREATOR;
  6510. case MAPID_STALKER: return JOB_STALKER;
  6511. //Baby Novice And Baby 1-1 Jobs
  6512. case MAPID_BABY: return JOB_BABY;
  6513. case MAPID_BABY_SWORDMAN: return JOB_BABY_SWORDMAN;
  6514. case MAPID_BABY_MAGE: return JOB_BABY_MAGE;
  6515. case MAPID_BABY_ARCHER: return JOB_BABY_ARCHER;
  6516. case MAPID_BABY_ACOLYTE: return JOB_BABY_ACOLYTE;
  6517. case MAPID_BABY_MERCHANT: return JOB_BABY_MERCHANT;
  6518. case MAPID_BABY_THIEF: return JOB_BABY_THIEF;
  6519. case MAPID_BABY_TAEKWON: return JOB_BABY_TAEKWON;
  6520. case MAPID_BABY_GUNSLINGER: return JOB_BABY_GUNSLINGER;
  6521. case MAPID_BABY_NINJA: return JOB_BABY_NINJA;
  6522. case MAPID_BABY_SUMMONER: return JOB_BABY_SUMMONER;
  6523. //Baby 2-1 Jobs
  6524. case MAPID_SUPER_BABY: return JOB_SUPER_BABY;
  6525. case MAPID_BABY_KNIGHT: return JOB_BABY_KNIGHT;
  6526. case MAPID_BABY_WIZARD: return JOB_BABY_WIZARD;
  6527. case MAPID_BABY_HUNTER: return JOB_BABY_HUNTER;
  6528. case MAPID_BABY_PRIEST: return JOB_BABY_PRIEST;
  6529. case MAPID_BABY_BLACKSMITH: return JOB_BABY_BLACKSMITH;
  6530. case MAPID_BABY_ASSASSIN: return JOB_BABY_ASSASSIN;
  6531. case MAPID_BABY_STAR_GLADIATOR: return JOB_BABY_STAR_GLADIATOR;
  6532. case MAPID_BABY_REBELLION: return JOB_BABY_REBELLION;
  6533. case MAPID_BABY_KAGEROUOBORO: return sex?JOB_BABY_KAGEROU:JOB_BABY_OBORO;
  6534. //Baby 2-2 Jobs
  6535. case MAPID_BABY_CRUSADER: return JOB_BABY_CRUSADER;
  6536. case MAPID_BABY_SAGE: return JOB_BABY_SAGE;
  6537. case MAPID_BABY_BARDDANCER: return sex?JOB_BABY_BARD:JOB_BABY_DANCER;
  6538. case MAPID_BABY_MONK: return JOB_BABY_MONK;
  6539. case MAPID_BABY_ALCHEMIST: return JOB_BABY_ALCHEMIST;
  6540. case MAPID_BABY_ROGUE: return JOB_BABY_ROGUE;
  6541. case MAPID_BABY_SOUL_LINKER: return JOB_BABY_SOUL_LINKER;
  6542. //3-1 Jobs
  6543. case MAPID_SUPER_NOVICE_E: return JOB_SUPER_NOVICE_E;
  6544. case MAPID_RUNE_KNIGHT: return JOB_RUNE_KNIGHT;
  6545. case MAPID_WARLOCK: return JOB_WARLOCK;
  6546. case MAPID_RANGER: return JOB_RANGER;
  6547. case MAPID_ARCH_BISHOP: return JOB_ARCH_BISHOP;
  6548. case MAPID_MECHANIC: return JOB_MECHANIC;
  6549. case MAPID_GUILLOTINE_CROSS: return JOB_GUILLOTINE_CROSS;
  6550. case MAPID_STAR_EMPEROR: return JOB_STAR_EMPEROR;
  6551. //3-2 Jobs
  6552. case MAPID_ROYAL_GUARD: return JOB_ROYAL_GUARD;
  6553. case MAPID_SORCERER: return JOB_SORCERER;
  6554. case MAPID_MINSTRELWANDERER: return sex?JOB_MINSTREL:JOB_WANDERER;
  6555. case MAPID_SURA: return JOB_SURA;
  6556. case MAPID_GENETIC: return JOB_GENETIC;
  6557. case MAPID_SHADOW_CHASER: return JOB_SHADOW_CHASER;
  6558. case MAPID_SOUL_REAPER: return JOB_SOUL_REAPER;
  6559. //Trans 3-1 Jobs
  6560. case MAPID_RUNE_KNIGHT_T: return JOB_RUNE_KNIGHT_T;
  6561. case MAPID_WARLOCK_T: return JOB_WARLOCK_T;
  6562. case MAPID_RANGER_T: return JOB_RANGER_T;
  6563. case MAPID_ARCH_BISHOP_T: return JOB_ARCH_BISHOP_T;
  6564. case MAPID_MECHANIC_T: return JOB_MECHANIC_T;
  6565. case MAPID_GUILLOTINE_CROSS_T: return JOB_GUILLOTINE_CROSS_T;
  6566. //Trans 3-2 Jobs
  6567. case MAPID_ROYAL_GUARD_T: return JOB_ROYAL_GUARD_T;
  6568. case MAPID_SORCERER_T: return JOB_SORCERER_T;
  6569. case MAPID_MINSTRELWANDERER_T: return sex?JOB_MINSTREL_T:JOB_WANDERER_T;
  6570. case MAPID_SURA_T: return JOB_SURA_T;
  6571. case MAPID_GENETIC_T: return JOB_GENETIC_T;
  6572. case MAPID_SHADOW_CHASER_T: return JOB_SHADOW_CHASER_T;
  6573. //Baby 3-1 Jobs
  6574. case MAPID_SUPER_BABY_E: return JOB_SUPER_BABY_E;
  6575. case MAPID_BABY_RUNE_KNIGHT: return JOB_BABY_RUNE_KNIGHT;
  6576. case MAPID_BABY_WARLOCK: return JOB_BABY_WARLOCK;
  6577. case MAPID_BABY_RANGER: return JOB_BABY_RANGER;
  6578. case MAPID_BABY_ARCH_BISHOP: return JOB_BABY_ARCH_BISHOP;
  6579. case MAPID_BABY_MECHANIC: return JOB_BABY_MECHANIC;
  6580. case MAPID_BABY_GUILLOTINE_CROSS: return JOB_BABY_GUILLOTINE_CROSS;
  6581. case MAPID_BABY_STAR_EMPEROR: return JOB_BABY_STAR_EMPEROR;
  6582. //Baby 3-2 Jobs
  6583. case MAPID_BABY_ROYAL_GUARD: return JOB_BABY_ROYAL_GUARD;
  6584. case MAPID_BABY_SORCERER: return JOB_BABY_SORCERER;
  6585. case MAPID_BABY_MINSTRELWANDERER: return sex?JOB_BABY_MINSTREL:JOB_BABY_WANDERER;
  6586. case MAPID_BABY_SURA: return JOB_BABY_SURA;
  6587. case MAPID_BABY_GENETIC: return JOB_BABY_GENETIC;
  6588. case MAPID_BABY_SHADOW_CHASER: return JOB_BABY_SHADOW_CHASER;
  6589. case MAPID_BABY_SOUL_REAPER: return JOB_BABY_SOUL_REAPER;
  6590. //Doram Jobs
  6591. case MAPID_SUMMONER: return JOB_SUMMONER;
  6592. case MAPID_SPIRIT_HANDLER: return JOB_SPIRIT_HANDLER;
  6593. //4-1 Jobs
  6594. case MAPID_HYPER_NOVICE: return JOB_HYPER_NOVICE;
  6595. case MAPID_DRAGON_KNIGHT: return JOB_DRAGON_KNIGHT;
  6596. case MAPID_ARCH_MAGE: return JOB_ARCH_MAGE;
  6597. case MAPID_WINDHAWK: return JOB_WINDHAWK;
  6598. case MAPID_CARDINAL: return JOB_CARDINAL;
  6599. case MAPID_MEISTER: return JOB_MEISTER;
  6600. case MAPID_SHADOW_CROSS: return JOB_SHADOW_CROSS;
  6601. case MAPID_SKY_EMPEROR: return JOB_SKY_EMPEROR;
  6602. case MAPID_NIGHT_WATCH: return JOB_NIGHT_WATCH;
  6603. case MAPID_SHINKIRO_SHIRANUI: return sex?JOB_SHINKIRO:JOB_SHIRANUI;
  6604. //4-2 Jobs
  6605. case MAPID_IMPERIAL_GUARD: return JOB_IMPERIAL_GUARD;
  6606. case MAPID_ELEMENTAL_MASTER: return JOB_ELEMENTAL_MASTER;
  6607. case MAPID_INQUISITOR: return JOB_INQUISITOR;
  6608. case MAPID_TROUBADOURTROUVERE: return sex?JOB_TROUBADOUR:JOB_TROUVERE;
  6609. case MAPID_BIOLO: return JOB_BIOLO;
  6610. case MAPID_ABYSS_CHASER: return JOB_ABYSS_CHASER;
  6611. case MAPID_SOUL_ASCETIC: return JOB_SOUL_ASCETIC;
  6612. //Unknown
  6613. default:
  6614. return -1;
  6615. }
  6616. }
  6617. /*====================================================
  6618. * This function return the name of the job (by [Yor])
  6619. *----------------------------------------------------*/
  6620. const char* job_name(int class_)
  6621. {
  6622. switch (class_) {
  6623. case JOB_NOVICE:
  6624. case JOB_SWORDMAN:
  6625. case JOB_MAGE:
  6626. case JOB_ARCHER:
  6627. case JOB_ACOLYTE:
  6628. case JOB_MERCHANT:
  6629. case JOB_THIEF:
  6630. return msg_txt(NULL,550 - JOB_NOVICE+class_);
  6631. case JOB_KNIGHT:
  6632. case JOB_PRIEST:
  6633. case JOB_WIZARD:
  6634. case JOB_BLACKSMITH:
  6635. case JOB_HUNTER:
  6636. case JOB_ASSASSIN:
  6637. return msg_txt(NULL,557 - JOB_KNIGHT+class_);
  6638. case JOB_KNIGHT2:
  6639. return msg_txt(NULL,557);
  6640. case JOB_CRUSADER:
  6641. case JOB_MONK:
  6642. case JOB_SAGE:
  6643. case JOB_ROGUE:
  6644. case JOB_ALCHEMIST:
  6645. case JOB_BARD:
  6646. case JOB_DANCER:
  6647. return msg_txt(NULL,563 - JOB_CRUSADER+class_);
  6648. case JOB_CRUSADER2:
  6649. return msg_txt(NULL,563);
  6650. case JOB_WEDDING:
  6651. case JOB_SUPER_NOVICE:
  6652. case JOB_GUNSLINGER:
  6653. case JOB_NINJA:
  6654. case JOB_XMAS:
  6655. return msg_txt(NULL,570 - JOB_WEDDING+class_);
  6656. case JOB_SUMMER:
  6657. case JOB_SUMMER2:
  6658. return msg_txt(NULL,621);
  6659. case JOB_HANBOK:
  6660. return msg_txt(NULL,694);
  6661. case JOB_OKTOBERFEST:
  6662. return msg_txt(NULL,696);
  6663. case JOB_NOVICE_HIGH:
  6664. case JOB_SWORDMAN_HIGH:
  6665. case JOB_MAGE_HIGH:
  6666. case JOB_ARCHER_HIGH:
  6667. case JOB_ACOLYTE_HIGH:
  6668. case JOB_MERCHANT_HIGH:
  6669. case JOB_THIEF_HIGH:
  6670. return msg_txt(NULL,575 - JOB_NOVICE_HIGH+class_);
  6671. case JOB_LORD_KNIGHT:
  6672. case JOB_HIGH_PRIEST:
  6673. case JOB_HIGH_WIZARD:
  6674. case JOB_WHITESMITH:
  6675. case JOB_SNIPER:
  6676. case JOB_ASSASSIN_CROSS:
  6677. return msg_txt(NULL,582 - JOB_LORD_KNIGHT+class_);
  6678. case JOB_LORD_KNIGHT2:
  6679. return msg_txt(NULL,582);
  6680. case JOB_PALADIN:
  6681. case JOB_CHAMPION:
  6682. case JOB_PROFESSOR:
  6683. case JOB_STALKER:
  6684. case JOB_CREATOR:
  6685. case JOB_CLOWN:
  6686. case JOB_GYPSY:
  6687. return msg_txt(NULL,588 - JOB_PALADIN + class_);
  6688. case JOB_PALADIN2:
  6689. return msg_txt(NULL,588);
  6690. case JOB_BABY:
  6691. case JOB_BABY_SWORDMAN:
  6692. case JOB_BABY_MAGE:
  6693. case JOB_BABY_ARCHER:
  6694. case JOB_BABY_ACOLYTE:
  6695. case JOB_BABY_MERCHANT:
  6696. case JOB_BABY_THIEF:
  6697. return msg_txt(NULL,595 - JOB_BABY + class_);
  6698. case JOB_BABY_KNIGHT:
  6699. case JOB_BABY_PRIEST:
  6700. case JOB_BABY_WIZARD:
  6701. case JOB_BABY_BLACKSMITH:
  6702. case JOB_BABY_HUNTER:
  6703. case JOB_BABY_ASSASSIN:
  6704. return msg_txt(NULL,602 - JOB_BABY_KNIGHT + class_);
  6705. case JOB_BABY_KNIGHT2:
  6706. return msg_txt(NULL,602);
  6707. case JOB_BABY_CRUSADER:
  6708. case JOB_BABY_MONK:
  6709. case JOB_BABY_SAGE:
  6710. case JOB_BABY_ROGUE:
  6711. case JOB_BABY_ALCHEMIST:
  6712. case JOB_BABY_BARD:
  6713. case JOB_BABY_DANCER:
  6714. return msg_txt(NULL,608 - JOB_BABY_CRUSADER + class_);
  6715. case JOB_BABY_CRUSADER2:
  6716. return msg_txt(NULL,608);
  6717. case JOB_SUPER_BABY:
  6718. return msg_txt(NULL,615);
  6719. case JOB_TAEKWON:
  6720. return msg_txt(NULL,616);
  6721. case JOB_STAR_GLADIATOR:
  6722. case JOB_STAR_GLADIATOR2:
  6723. return msg_txt(NULL,617);
  6724. case JOB_SOUL_LINKER:
  6725. return msg_txt(NULL,618);
  6726. case JOB_GANGSI:
  6727. case JOB_DEATH_KNIGHT:
  6728. case JOB_DARK_COLLECTOR:
  6729. return msg_txt(NULL,622 - JOB_GANGSI+class_);
  6730. case JOB_RUNE_KNIGHT:
  6731. case JOB_WARLOCK:
  6732. case JOB_RANGER:
  6733. case JOB_ARCH_BISHOP:
  6734. case JOB_MECHANIC:
  6735. case JOB_GUILLOTINE_CROSS:
  6736. return msg_txt(NULL,625 - JOB_RUNE_KNIGHT+class_);
  6737. case JOB_RUNE_KNIGHT_T:
  6738. case JOB_WARLOCK_T:
  6739. case JOB_RANGER_T:
  6740. case JOB_ARCH_BISHOP_T:
  6741. case JOB_MECHANIC_T:
  6742. case JOB_GUILLOTINE_CROSS_T:
  6743. return msg_txt(NULL,681 - JOB_RUNE_KNIGHT_T+class_);
  6744. case JOB_ROYAL_GUARD:
  6745. case JOB_SORCERER:
  6746. case JOB_MINSTREL:
  6747. case JOB_WANDERER:
  6748. case JOB_SURA:
  6749. case JOB_GENETIC:
  6750. case JOB_SHADOW_CHASER:
  6751. return msg_txt(NULL,631 - JOB_ROYAL_GUARD+class_);
  6752. case JOB_ROYAL_GUARD_T:
  6753. case JOB_SORCERER_T:
  6754. case JOB_MINSTREL_T:
  6755. case JOB_WANDERER_T:
  6756. case JOB_SURA_T:
  6757. case JOB_GENETIC_T:
  6758. case JOB_SHADOW_CHASER_T:
  6759. return msg_txt(NULL,687 - JOB_ROYAL_GUARD_T+class_);
  6760. case JOB_RUNE_KNIGHT2:
  6761. case JOB_RUNE_KNIGHT_T2:
  6762. return msg_txt(NULL,625);
  6763. case JOB_ROYAL_GUARD2:
  6764. case JOB_ROYAL_GUARD_T2:
  6765. return msg_txt(NULL,631);
  6766. case JOB_RANGER2:
  6767. case JOB_RANGER_T2:
  6768. return msg_txt(NULL,627);
  6769. case JOB_MECHANIC2:
  6770. case JOB_MECHANIC_T2:
  6771. return msg_txt(NULL,629);
  6772. case JOB_BABY_RUNE_KNIGHT:
  6773. case JOB_BABY_WARLOCK:
  6774. case JOB_BABY_RANGER:
  6775. case JOB_BABY_ARCH_BISHOP:
  6776. case JOB_BABY_MECHANIC:
  6777. case JOB_BABY_GUILLOTINE_CROSS:
  6778. case JOB_BABY_ROYAL_GUARD:
  6779. case JOB_BABY_SORCERER:
  6780. case JOB_BABY_MINSTREL:
  6781. case JOB_BABY_WANDERER:
  6782. case JOB_BABY_SURA:
  6783. case JOB_BABY_GENETIC:
  6784. case JOB_BABY_SHADOW_CHASER:
  6785. return msg_txt(NULL,638 - JOB_BABY_RUNE_KNIGHT+class_);
  6786. case JOB_BABY_RUNE_KNIGHT2:
  6787. return msg_txt(NULL,638);
  6788. case JOB_BABY_ROYAL_GUARD2:
  6789. return msg_txt(NULL,644);
  6790. case JOB_BABY_RANGER2:
  6791. return msg_txt(NULL,640);
  6792. case JOB_BABY_MECHANIC2:
  6793. return msg_txt(NULL,642);
  6794. case JOB_SUPER_NOVICE_E:
  6795. case JOB_SUPER_BABY_E:
  6796. return msg_txt(NULL,651 - JOB_SUPER_NOVICE_E+class_);
  6797. case JOB_KAGEROU:
  6798. case JOB_OBORO:
  6799. return msg_txt(NULL,653 - JOB_KAGEROU+class_);
  6800. case JOB_REBELLION:
  6801. return msg_txt(NULL,695);
  6802. case JOB_SUMMONER:
  6803. return msg_txt(NULL,697);
  6804. case JOB_BABY_SUMMONER:
  6805. return msg_txt(NULL,698);
  6806. case JOB_BABY_NINJA:
  6807. return msg_txt(NULL,699);
  6808. case JOB_BABY_KAGEROU:
  6809. case JOB_BABY_OBORO:
  6810. case JOB_BABY_TAEKWON:
  6811. case JOB_BABY_STAR_GLADIATOR:
  6812. case JOB_BABY_SOUL_LINKER:
  6813. case JOB_BABY_GUNSLINGER:
  6814. case JOB_BABY_REBELLION:
  6815. return msg_txt(NULL,753 - JOB_BABY_KAGEROU+class_);
  6816. case JOB_BABY_STAR_GLADIATOR2:
  6817. return msg_txt(NULL,756);
  6818. case JOB_STAR_EMPEROR:
  6819. case JOB_SOUL_REAPER:
  6820. case JOB_BABY_STAR_EMPEROR:
  6821. case JOB_BABY_SOUL_REAPER:
  6822. return msg_txt(NULL,782 - JOB_STAR_EMPEROR + class_);
  6823. case JOB_STAR_EMPEROR2:
  6824. return msg_txt(NULL,782);
  6825. case JOB_BABY_STAR_EMPEROR2:
  6826. return msg_txt(NULL,784);
  6827. case JOB_DRAGON_KNIGHT:
  6828. case JOB_MEISTER:
  6829. case JOB_SHADOW_CROSS:
  6830. case JOB_ARCH_MAGE:
  6831. case JOB_CARDINAL:
  6832. case JOB_WINDHAWK:
  6833. case JOB_IMPERIAL_GUARD:
  6834. case JOB_BIOLO:
  6835. case JOB_ABYSS_CHASER:
  6836. case JOB_ELEMENTAL_MASTER:
  6837. case JOB_INQUISITOR:
  6838. case JOB_TROUBADOUR:
  6839. case JOB_TROUVERE:
  6840. return msg_txt( nullptr, 800 - JOB_DRAGON_KNIGHT + class_ );
  6841. case JOB_WINDHAWK2:
  6842. return msg_txt( nullptr, 805);
  6843. case JOB_MEISTER2:
  6844. return msg_txt( nullptr, 801 );
  6845. case JOB_DRAGON_KNIGHT2:
  6846. return msg_txt( nullptr, 800 );
  6847. case JOB_IMPERIAL_GUARD2:
  6848. return msg_txt( nullptr, 806 );
  6849. case JOB_SKY_EMPEROR:
  6850. case JOB_SOUL_ASCETIC:
  6851. case JOB_SHINKIRO:
  6852. case JOB_SHIRANUI:
  6853. case JOB_NIGHT_WATCH:
  6854. case JOB_HYPER_NOVICE:
  6855. case JOB_SPIRIT_HANDLER:
  6856. return msg_txt( nullptr, 813 - JOB_SKY_EMPEROR + class_ );
  6857. case JOB_SKY_EMPEROR2:
  6858. return msg_txt( nullptr, 813 );
  6859. default:
  6860. return msg_txt(NULL,655);
  6861. }
  6862. }
  6863. /*====================================================
  6864. * Timered function to make id follow a target.
  6865. * @id = bl.id (player only atm)
  6866. * target is define in sd->followtarget (bl.id)
  6867. * used by pc_follow
  6868. *----------------------------------------------------*/
  6869. TIMER_FUNC(pc_follow_timer){
  6870. map_session_data *sd;
  6871. struct block_list *tbl;
  6872. sd = map_id2sd(id);
  6873. nullpo_ret(sd);
  6874. if (sd->followtimer != tid){
  6875. ShowError("pc_follow_timer %d != %d\n",sd->followtimer,tid);
  6876. sd->followtimer = INVALID_TIMER;
  6877. return 0;
  6878. }
  6879. sd->followtimer = INVALID_TIMER;
  6880. tbl = map_id2bl(sd->followtarget);
  6881. if (tbl == NULL || pc_isdead(sd))
  6882. {
  6883. pc_stop_following(sd);
  6884. return 0;
  6885. }
  6886. // either player or target is currently detached from map blocks (could be teleporting),
  6887. // but still connected to this map, so we'll just increment the timer and check back later
  6888. if (sd->bl.prev != NULL && tbl->prev != NULL &&
  6889. sd->ud.skilltimer == INVALID_TIMER && sd->ud.attacktimer == INVALID_TIMER && sd->ud.walktimer == INVALID_TIMER)
  6890. {
  6891. if((sd->bl.m == tbl->m) && unit_can_reach_bl(&sd->bl,tbl, AREA_SIZE, 0, NULL, NULL)) {
  6892. if (!check_distance_bl(&sd->bl, tbl, 5))
  6893. unit_walktobl(&sd->bl, tbl, 5, 0);
  6894. } else
  6895. pc_setpos(sd, map_id2index(tbl->m), tbl->x, tbl->y, CLR_TELEPORT);
  6896. }
  6897. sd->followtimer = add_timer(
  6898. tick + 1000, // increase time a bit to loosen up map's load
  6899. pc_follow_timer, sd->bl.id, 0);
  6900. return 0;
  6901. }
  6902. int pc_stop_following (map_session_data *sd)
  6903. {
  6904. nullpo_ret(sd);
  6905. if (sd->followtimer != INVALID_TIMER) {
  6906. delete_timer(sd->followtimer,pc_follow_timer);
  6907. sd->followtimer = INVALID_TIMER;
  6908. }
  6909. sd->followtarget = -1;
  6910. sd->ud.target_to = 0;
  6911. unit_stop_walking(&sd->bl, 1);
  6912. return 0;
  6913. }
  6914. int pc_follow(map_session_data *sd,int target_id)
  6915. {
  6916. struct block_list *bl = map_id2bl(target_id);
  6917. if (bl == NULL /*|| bl->type != BL_PC*/)
  6918. return 1;
  6919. if (sd->followtimer != INVALID_TIMER)
  6920. pc_stop_following(sd);
  6921. sd->followtarget = target_id;
  6922. pc_follow_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  6923. return 0;
  6924. }
  6925. int pc_checkbaselevelup(map_session_data *sd) {
  6926. t_exp next = pc_nextbaseexp(sd);
  6927. if (!next || sd->status.base_exp < next || pc_is_maxbaselv(sd))
  6928. return 0;
  6929. uint32 base_level = sd->status.base_level;
  6930. do {
  6931. sd->status.base_exp -= next;
  6932. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6933. 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 )
  6934. sd->status.base_exp = next-1;
  6935. sd->status.status_point += statpoint_db.pc_gets_status_point(sd->status.base_level);
  6936. sd->status.trait_point += statpoint_db.pc_gets_trait_point(sd->status.base_level);
  6937. sd->status.base_level++;
  6938. if( pc_is_maxbaselv(sd) ){
  6939. sd->status.base_exp = u64min(sd->status.base_exp,MAX_LEVEL_BASE_EXP);
  6940. break;
  6941. }
  6942. } while ((next=pc_nextbaseexp(sd)) > 0 && sd->status.base_exp >= next);
  6943. if (battle_config.pet_lv_rate && sd->pd) //<Skotlex> update pet's level
  6944. status_calc_pet(sd->pd,SCO_NONE);
  6945. clif_updatestatus(sd,SP_STATUSPOINT);
  6946. clif_updatestatus(sd,SP_TRAITPOINT);
  6947. clif_updatestatus(sd,SP_BASELEVEL);
  6948. clif_updatestatus(sd,SP_BASEEXP);
  6949. clif_updatestatus(sd,SP_NEXTBASEEXP);
  6950. status_calc_pc(sd,SCO_FORCE);
  6951. status_percent_heal(&sd->bl,100,100);
  6952. if ((sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  6953. for (const auto &status : status_db) {
  6954. if (status.second->flag[SCF_SUPERNOVICEANGEL])
  6955. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 1, skill_get_time(status.second->skill_id, 1));
  6956. }
  6957. if (sd->state.snovice_dead_flag)
  6958. sd->state.snovice_dead_flag = 0; //Reenable steelbody resurrection on dead.
  6959. } else if( (sd->class_&MAPID_BASEMASK) == MAPID_TAEKWON ) {
  6960. for (const auto &status : status_db) {
  6961. if (status.second->flag[SCF_TAEKWONANGEL])
  6962. sc_start(&sd->bl, &sd->bl, status.second->type, 100, 10, 600000);
  6963. }
  6964. }
  6965. clif_misceffect(&sd->bl,0);
  6966. npc_script_event(sd, NPCE_BASELVUP); //LORDALFA - LVLUPEVENT
  6967. if(sd->status.party_id)
  6968. party_send_levelup(sd);
  6969. pc_baselevelchanged(sd);
  6970. for (; base_level <= sd->status.base_level; base_level++) {
  6971. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, base_level);
  6972. achievement_update_objective(sd, AG_GOAL_STATUS, 2, base_level, sd->status.class_);
  6973. }
  6974. return 1;
  6975. }
  6976. void pc_baselevelchanged(map_session_data *sd) {
  6977. uint8 i;
  6978. for( i = 0; i < EQI_MAX; i++ ) {
  6979. if( sd->equip_index[i] >= 0 && sd->inventory_data[sd->equip_index[i]] ) {
  6980. if( sd->inventory_data[ sd->equip_index[i] ]->elvmax && sd->status.base_level > (unsigned int)sd->inventory_data[ sd->equip_index[i] ]->elvmax )
  6981. pc_unequipitem(sd, sd->equip_index[i], 3);
  6982. }
  6983. }
  6984. pc_show_questinfo(sd);
  6985. }
  6986. int pc_checkjoblevelup(map_session_data *sd)
  6987. {
  6988. t_exp next = pc_nextjobexp(sd);
  6989. nullpo_ret(sd);
  6990. if(!next || sd->status.job_exp < next || pc_is_maxjoblv(sd))
  6991. return 0;
  6992. uint32 job_level = sd->status.job_level;
  6993. do {
  6994. sd->status.job_exp -= next;
  6995. //Kyoki pointed out that the max overcarry exp is the exp needed for the previous level -1. [Skotlex]
  6996. 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 )
  6997. sd->status.job_exp = next-1;
  6998. sd->status.job_level ++;
  6999. sd->status.skill_point ++;
  7000. if( pc_is_maxjoblv(sd) ){
  7001. sd->status.job_exp = u64min(sd->status.job_exp,MAX_LEVEL_JOB_EXP);
  7002. break;
  7003. }
  7004. } while ((next=pc_nextjobexp(sd)) > 0 && sd->status.job_exp >= next);
  7005. clif_updatestatus(sd,SP_JOBLEVEL);
  7006. clif_updatestatus(sd,SP_JOBEXP);
  7007. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7008. clif_updatestatus(sd,SP_SKILLPOINT);
  7009. status_calc_pc(sd,SCO_FORCE);
  7010. clif_misceffect(&sd->bl,1);
  7011. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7012. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7013. npc_script_event(sd, NPCE_JOBLVUP);
  7014. for (; job_level <= sd->status.job_level; job_level++)
  7015. achievement_update_objective(sd, AG_GOAL_LEVEL, 1, job_level);
  7016. pc_show_questinfo(sd);
  7017. return 1;
  7018. }
  7019. /** Alters experiences calculation based on self bonuses that do not get even shared to the party.
  7020. * @param sd Player
  7021. * @param base_exp Base EXP before peronal bonuses
  7022. * @param job_exp Job EXP before peronal bonuses
  7023. * @param src Block list that affecting the exp calculation
  7024. */
  7025. static void pc_calcexp(map_session_data *sd, t_exp *base_exp, t_exp *job_exp, struct block_list *src)
  7026. {
  7027. int bonus = 0, vip_bonus_base = 0, vip_bonus_job = 0;
  7028. if (src) {
  7029. struct status_data *status = status_get_status_data(src);
  7030. if( sd->indexed_bonus.expaddrace[status->race] )
  7031. bonus += sd->indexed_bonus.expaddrace[status->race];
  7032. if( sd->indexed_bonus.expaddrace[RC_ALL] )
  7033. bonus += sd->indexed_bonus.expaddrace[RC_ALL];
  7034. if( sd->indexed_bonus.expaddclass[status->class_] )
  7035. bonus += sd->indexed_bonus.expaddclass[status->class_];
  7036. if( sd->indexed_bonus.expaddclass[CLASS_ALL] )
  7037. bonus += sd->indexed_bonus.expaddclass[CLASS_ALL];
  7038. if (battle_config.pk_mode &&
  7039. (int)(status_get_lv(src) - sd->status.base_level) >= 20)
  7040. bonus += 15; // pk_mode additional exp if monster >20 levels [Valaris]
  7041. if (src && src->type == BL_MOB && pc_isvip(sd)) { // EXP bonus for VIP player
  7042. vip_bonus_base = battle_config.vip_base_exp_increase;
  7043. vip_bonus_job = battle_config.vip_job_exp_increase;
  7044. }
  7045. }
  7046. // Give EXPBOOST for quests even if src is NULL.
  7047. if (sd->sc.getSCE(SC_EXPBOOST)) {
  7048. bonus += sd->sc.getSCE(SC_EXPBOOST)->val1;
  7049. if (battle_config.vip_bm_increase && pc_isvip(sd)) // Increase Battle Manual EXP rate for VIP
  7050. bonus += (sd->sc.getSCE(SC_EXPBOOST)->val1 / battle_config.vip_bm_increase);
  7051. }
  7052. if (*base_exp) {
  7053. t_exp exp = (t_exp)(*base_exp + ((double)*base_exp * ((bonus + vip_bonus_base) / 100.)));
  7054. *base_exp = cap_value(exp, 1, MAX_EXP);
  7055. }
  7056. // Give JEXPBOOST for quests even if src is NULL.
  7057. if (sd->sc.getSCE(SC_JEXPBOOST))
  7058. bonus += sd->sc.getSCE(SC_JEXPBOOST)->val1;
  7059. if (*job_exp) {
  7060. t_exp exp = (t_exp)(*job_exp + ((double)*job_exp * ((bonus + vip_bonus_job) / 100.)));
  7061. *job_exp = cap_value(exp, 1, MAX_EXP);
  7062. }
  7063. return;
  7064. }
  7065. /**
  7066. * Show EXP gained by player in percentage by @showexp
  7067. * @param sd Player
  7068. * @param base_exp Base EXP gained/loss
  7069. * @param next_base_exp Base EXP needed for next base level
  7070. * @param job_exp Job EXP gained/loss
  7071. * @param next_job_exp Job EXP needed for next job level
  7072. * @param lost True:EXP penalty, lose EXP
  7073. **/
  7074. 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) {
  7075. char output[CHAT_SIZE_MAX];
  7076. nullpo_retv(sd);
  7077. sprintf(output, msg_txt(sd,743), // Experience %s Base:%ld (%0.2f%%) Job:%ld (%0.2f%%)
  7078. (lost) ? msg_txt(sd,742) : msg_txt(sd,741),
  7079. (long)base_exp * (lost ? -1 : 1), (base_exp / (float)next_base_exp * 100 * (lost ? -1 : 1)),
  7080. (long)job_exp * (lost ? -1 : 1), (job_exp / (float)next_job_exp * 100 * (lost ? -1 : 1)));
  7081. clif_messagecolor(&sd->bl, color_table[COLOR_LIGHT_GREEN], output, false, SELF);
  7082. }
  7083. /**
  7084. * Give Base or Job EXP to player, then calculate remaining exp for next lvl
  7085. * @param sd Player
  7086. * @param src EXP source
  7087. * @param base_exp Base EXP gained
  7088. * @param base_exp Job EXP gained
  7089. * @param exp_flag 1: Quest EXP; 2: Param Exp (Ignore Guild EXP tax, EXP adjustments)
  7090. * @return
  7091. **/
  7092. void pc_gainexp(map_session_data *sd, struct block_list *src, t_exp base_exp, t_exp job_exp, uint8 exp_flag)
  7093. {
  7094. t_exp nextb = 0, nextj = 0;
  7095. uint8 flag = 0; ///< 1: Base EXP given, 2: Job EXP given, 4: Max Base level, 8: Max Job Level
  7096. nullpo_retv(sd);
  7097. if(sd->bl.prev == NULL || pc_isdead(sd))
  7098. return;
  7099. if (!(exp_flag&2)) {
  7100. if (!battle_config.pvp_exp && map_getmapflag(sd->bl.m, MF_PVP)) // [MouseJstr]
  7101. return; // no exp on pvp maps
  7102. if (sd->status.guild_id>0)
  7103. base_exp -= guild_payexp(sd,base_exp);
  7104. }
  7105. flag = ((base_exp) ? 1 : 0) |
  7106. ((job_exp) ? 2 : 0) |
  7107. ((pc_is_maxbaselv(sd)) ? 4 : 0) |
  7108. ((pc_is_maxjoblv(sd)) ? 8 : 0);
  7109. if (!(exp_flag&2))
  7110. pc_calcexp(sd, &base_exp, &job_exp, src);
  7111. nextb = pc_nextbaseexp(sd);
  7112. nextj = pc_nextjobexp(sd);
  7113. if (flag&4){
  7114. if( sd->status.base_exp >= MAX_LEVEL_BASE_EXP )
  7115. base_exp = 0;
  7116. else if( sd->status.base_exp + base_exp >= MAX_LEVEL_BASE_EXP )
  7117. base_exp = MAX_LEVEL_BASE_EXP - sd->status.base_exp;
  7118. }
  7119. if (flag&8){
  7120. if( sd->status.job_exp >= MAX_LEVEL_JOB_EXP )
  7121. job_exp = 0;
  7122. else if( sd->status.job_exp + job_exp >= MAX_LEVEL_JOB_EXP )
  7123. job_exp = MAX_LEVEL_JOB_EXP - sd->status.job_exp;
  7124. }
  7125. if (!(exp_flag&2) && battle_config.max_exp_gain_rate && (base_exp || job_exp)) {
  7126. //Note that this value should never be greater than the original
  7127. //therefore no overflow checks are needed. [Skotlex]
  7128. if (nextb > 0) {
  7129. float nextbp = (float) base_exp / (float) nextb;
  7130. if (nextbp > battle_config.max_exp_gain_rate/1000.)
  7131. base_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextb);
  7132. }
  7133. if (nextj > 0) {
  7134. float nextjp = (float) job_exp / (float) nextj;
  7135. if (nextjp > battle_config.max_exp_gain_rate/1000.)
  7136. job_exp = (unsigned int)(battle_config.max_exp_gain_rate/1000.*nextj);
  7137. }
  7138. }
  7139. // Give EXP for Base Level
  7140. if (base_exp) {
  7141. sd->status.base_exp = util::safe_addition_cap(sd->status.base_exp, base_exp, MAX_EXP);
  7142. if (!pc_checkbaselevelup(sd))
  7143. clif_updatestatus(sd,SP_BASEEXP);
  7144. }
  7145. // Give EXP for Job Level
  7146. if (job_exp) {
  7147. sd->status.job_exp = util::safe_addition_cap(sd->status.job_exp, job_exp, MAX_EXP);
  7148. if (!pc_checkjoblevelup(sd))
  7149. clif_updatestatus(sd,SP_JOBEXP);
  7150. }
  7151. if (flag&1)
  7152. clif_displayexp(sd, (flag&4) ? 0 : base_exp, SP_BASEEXP, exp_flag&1, false);
  7153. if (flag&2)
  7154. clif_displayexp(sd, (flag&8) ? 0 : job_exp, SP_JOBEXP, exp_flag&1, false);
  7155. if (sd->state.showexp && (base_exp || job_exp))
  7156. pc_gainexp_disp(sd, base_exp, nextb, job_exp, nextj, false);
  7157. }
  7158. /**
  7159. * Lost Base/Job EXP from a player
  7160. * @param sd Player
  7161. * @param base_exp Base EXP lost
  7162. * @param job_exp Job EXP lost
  7163. **/
  7164. void pc_lostexp(map_session_data *sd, t_exp base_exp, t_exp job_exp) {
  7165. nullpo_retv(sd);
  7166. if (base_exp) {
  7167. base_exp = u64min(sd->status.base_exp, base_exp);
  7168. sd->status.base_exp -= base_exp;
  7169. clif_displayexp(sd, base_exp, SP_BASEEXP, false, true);
  7170. clif_updatestatus(sd, SP_BASEEXP);
  7171. }
  7172. if (job_exp) {
  7173. job_exp = u64min(sd->status.job_exp, job_exp);
  7174. sd->status.job_exp -= job_exp;
  7175. clif_displayexp(sd, job_exp, SP_JOBEXP, false, true);
  7176. clif_updatestatus(sd, SP_JOBEXP);
  7177. }
  7178. if (sd->state.showexp && (base_exp || job_exp))
  7179. pc_gainexp_disp(sd, base_exp, pc_nextbaseexp(sd), job_exp, pc_nextjobexp(sd), true);
  7180. }
  7181. /**
  7182. * Returns max base level for this character's class.
  7183. * @param job_id: Player's class
  7184. * @return Max Base Level
  7185. */
  7186. uint32 JobDatabase::get_maxBaseLv(uint16 job_id) {
  7187. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7188. return job ? job->max_base_level : 0;
  7189. }
  7190. /**
  7191. * Returns max base level for this character.
  7192. * @param sd Player
  7193. * @return Max Base Level
  7194. **/
  7195. unsigned int pc_maxbaselv(map_session_data *sd){
  7196. return job_db.get_maxBaseLv(sd->status.class_);
  7197. }
  7198. /**
  7199. * Returns max job level for this character's class.
  7200. * @param job_id: Player's class
  7201. * @return Max Job Level
  7202. */
  7203. uint32 JobDatabase::get_maxJobLv(uint16 job_id) {
  7204. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7205. return job ? job->max_job_level : 0;
  7206. }
  7207. /**
  7208. * Returns max job level for this character.
  7209. * @param sd Player
  7210. * @return Max Job Level
  7211. **/
  7212. unsigned int pc_maxjoblv(map_session_data *sd){
  7213. return job_db.get_maxJobLv(sd->status.class_);
  7214. }
  7215. /**
  7216. * Check if player is reached max base level
  7217. * @param sd
  7218. * @return True if reached max level
  7219. **/
  7220. bool pc_is_maxbaselv(map_session_data *sd) {
  7221. nullpo_retr(false, sd);
  7222. return (sd->status.base_level >= pc_maxbaselv(sd));
  7223. }
  7224. /**
  7225. * Check if player is reached max base level
  7226. * @param sd
  7227. * @return True if reached max level
  7228. **/
  7229. bool pc_is_maxjoblv(map_session_data *sd) {
  7230. nullpo_retr(false, sd);
  7231. return (sd->status.job_level >= pc_maxjoblv(sd));
  7232. }
  7233. /**
  7234. * Returns base experience for this character's class.
  7235. * @param job_id: Player's class
  7236. * @param level: Player's level
  7237. * @return Base EXP
  7238. */
  7239. t_exp JobDatabase::get_baseExp(uint16 job_id, uint32 level) {
  7240. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7241. return job ? job->base_exp[level - 1] : 0;
  7242. }
  7243. /**
  7244. * Base exp needed for player to level up.
  7245. * @param sd
  7246. * @return Base EXP needed for next base level
  7247. **/
  7248. t_exp pc_nextbaseexp(map_session_data *sd){
  7249. nullpo_ret(sd);
  7250. if (sd->status.base_level == 0) // Is this something that possible?
  7251. return 0;
  7252. if (pc_is_maxbaselv(sd))
  7253. return MAX_LEVEL_BASE_EXP; // On max level, player's base EXP limit is 99,999,999
  7254. return static_cast<t_exp>(job_db.get_baseExp(sd->status.class_, sd->status.base_level));
  7255. }
  7256. /**
  7257. * Returns job experience for this character's class.
  7258. * @param job_id: Player's class
  7259. * @param level: Player's level
  7260. * @return Job EXP
  7261. */
  7262. t_exp JobDatabase::get_jobExp(uint16 job_id, uint32 level) {
  7263. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7264. return job ? job->job_exp[level - 1] : 0;
  7265. }
  7266. /**
  7267. * Job exp needed for player to level up.
  7268. * @param sd
  7269. * @return Job EXP needed for next job level
  7270. **/
  7271. t_exp pc_nextjobexp(map_session_data *sd){
  7272. nullpo_ret(sd);
  7273. if (sd->status.job_level == 0) // Is this something that possible?
  7274. return 0;
  7275. if (pc_is_maxjoblv(sd))
  7276. return MAX_LEVEL_JOB_EXP; // On max level, player's job EXP limit is 999,999,999
  7277. return static_cast<t_exp>(job_db.get_jobExp(sd->status.class_, sd->status.job_level));
  7278. }
  7279. /**
  7280. * Returns max weight base for this character's class.
  7281. * @param job_id: Player's class
  7282. * @return Max weight base
  7283. */
  7284. int32 JobDatabase::get_maxWeight(uint16 job_id) {
  7285. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  7286. return job ? job->max_weight_base : 0;
  7287. }
  7288. /// Returns the value of the specified stat.
  7289. int pc_getstat(map_session_data *sd, int type)
  7290. {
  7291. nullpo_retr(-1, sd);
  7292. switch( type ) {
  7293. case SP_STR: return sd->status.str;
  7294. case SP_AGI: return sd->status.agi;
  7295. case SP_VIT: return sd->status.vit;
  7296. case SP_INT: return sd->status.int_;
  7297. case SP_DEX: return sd->status.dex;
  7298. case SP_LUK: return sd->status.luk;
  7299. case SP_POW: return sd->status.pow;
  7300. case SP_STA: return sd->status.sta;
  7301. case SP_WIS: return sd->status.wis;
  7302. case SP_SPL: return sd->status.spl;
  7303. case SP_CON: return sd->status.con;
  7304. case SP_CRT: return sd->status.crt;
  7305. default:
  7306. return -1;
  7307. }
  7308. }
  7309. /// Sets the specified stat to the specified value.
  7310. /// Returns the new value.
  7311. int pc_setstat(map_session_data* sd, int type, int val)
  7312. {
  7313. nullpo_retr(-1, sd);
  7314. switch( type ) {
  7315. case SP_STR: sd->status.str = val; break;
  7316. case SP_AGI: sd->status.agi = val; break;
  7317. case SP_VIT: sd->status.vit = val; break;
  7318. case SP_INT: sd->status.int_ = val; break;
  7319. case SP_DEX: sd->status.dex = val; break;
  7320. case SP_LUK: sd->status.luk = val; break;
  7321. case SP_POW: sd->status.pow = val; break;
  7322. case SP_STA: sd->status.sta = val; break;
  7323. case SP_WIS: sd->status.wis = val; break;
  7324. case SP_SPL: sd->status.spl = val; break;
  7325. case SP_CON: sd->status.con = val; break;
  7326. case SP_CRT: sd->status.crt = val; break;
  7327. default:
  7328. return -1;
  7329. }
  7330. return val;
  7331. }
  7332. /**
  7333. * Gets the total number of status points at the provided level.
  7334. * @param level: Player base level.
  7335. * @return Total number of status points at specific base level.
  7336. */
  7337. uint32 PlayerStatPointDatabase::get_table_point(uint16 level) {
  7338. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7339. if( entry != nullptr ){
  7340. return entry->statpoints;
  7341. }else{
  7342. return 0;
  7343. }
  7344. }
  7345. /**
  7346. * Calculates the number of status points PC gets when leveling up (from level to level+1)
  7347. * @param level: Player base level.
  7348. * @param table: Use table value or formula.
  7349. * @return Status points at specific base level.
  7350. */
  7351. uint32 PlayerStatPointDatabase::pc_gets_status_point(uint16 level) {
  7352. uint32 next_level = this->get_table_point( level + 1 );
  7353. uint32 current_level = this->get_table_point( level );
  7354. if( next_level > current_level ){
  7355. return next_level - current_level;
  7356. }else{
  7357. return 0;
  7358. }
  7359. }
  7360. /**
  7361. * Gets the total number of trait points at the provided level.
  7362. * @param level: Player base level.
  7363. * @return Total number of trait points at specific base level.
  7364. */
  7365. uint32 PlayerStatPointDatabase::get_trait_table_point(uint16 level) {
  7366. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  7367. if( entry != nullptr ){
  7368. return entry->traitpoints;
  7369. }else{
  7370. return 0;
  7371. }
  7372. }
  7373. /**
  7374. * Calculates the number of trait points PC gets when leveling up (from level to level+1)
  7375. * @param level: Player base level.
  7376. * @param table: Use table value or formula.
  7377. * @return Trait points at specific base level.
  7378. */
  7379. uint32 PlayerStatPointDatabase::pc_gets_trait_point(uint16 level) {
  7380. uint32 next_level = this->get_trait_table_point( level + 1 );
  7381. uint32 current_level = this->get_trait_table_point( level );
  7382. if( next_level > current_level ){
  7383. return next_level - current_level;
  7384. }else{
  7385. return 0;
  7386. }
  7387. }
  7388. #ifdef RENEWAL_STAT
  7389. /// Renewal status point cost formula
  7390. #define PC_STATUS_POINT_COST(low) (((low) < 100) ? (2 + ((low) - 1) / 10) : (16 + 4 * (((low) - 100) / 5)))
  7391. #else
  7392. /// Pre-Renewal status point cost formula
  7393. #define PC_STATUS_POINT_COST(low) (( 1 + ((low) + 9) / 10 ))
  7394. #endif
  7395. /// Returns the number of stat points needed to change the specified stat by val.
  7396. /// If val is negative, returns the number of stat points that would be needed to
  7397. /// raise the specified stat from (current value - val) to current value.
  7398. int pc_need_status_point(map_session_data* sd, int type, int val)
  7399. {
  7400. int low, high, sp = 0, max = 0;
  7401. if ( val == 0 )
  7402. return 0;
  7403. low = pc_getstat(sd,type);
  7404. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR));
  7405. if ( low >= max && val > 0 )
  7406. return 0; // Official servers show '0' when max is reached
  7407. high = low + val;
  7408. if ( val < 0 )
  7409. SWAP(low, high);
  7410. for ( ; low < high; low++ )
  7411. sp += PC_STATUS_POINT_COST(low);
  7412. return sp;
  7413. }
  7414. /**
  7415. * Returns the value the specified stat can be increased by with the current
  7416. * amount of available status points for the current character's class.
  7417. *
  7418. * @param sd The target character.
  7419. * @param type Stat to verify.
  7420. * @return Maximum value the stat could grow by.
  7421. */
  7422. int pc_maxparameterincrease(map_session_data* sd, int type)
  7423. {
  7424. int base, final_val, status_points, max_param;
  7425. nullpo_ret(sd);
  7426. base = final_val = pc_getstat(sd, type);
  7427. status_points = sd->status.status_point;
  7428. max_param = pc_maxparameter(sd, (enum e_params)(type-SP_STR));
  7429. while (final_val <= max_param && status_points >= 0) {
  7430. status_points -= PC_STATUS_POINT_COST(final_val);
  7431. final_val++;
  7432. }
  7433. final_val--;
  7434. return (final_val > base ? final_val-base : 0);
  7435. }
  7436. /**
  7437. * Raises a stat by the specified amount.
  7438. *
  7439. * Obeys max_parameter limits.
  7440. * Subtracts status points according to the cost of the increased stat points.
  7441. *
  7442. * @param sd The target character.
  7443. * @param type The stat to change (see enum _sp)
  7444. * @param increase The stat increase (strictly positive) amount.
  7445. * @retval true if the stat was increased by any amount.
  7446. * @retval false if there were no changes.
  7447. */
  7448. bool pc_statusup(map_session_data* sd, int type, int increase)
  7449. {
  7450. int max_increase = 0, current = 0, needed_points = 0, final_value = 0;
  7451. nullpo_ret(sd);
  7452. // check conditions
  7453. if (type < SP_STR || type > SP_LUK || increase <= 0) {
  7454. clif_statusupack(sd, type, 0, 0);
  7455. return false;
  7456. }
  7457. // check limits
  7458. current = pc_getstat(sd, type);
  7459. max_increase = pc_maxparameterincrease(sd, type);
  7460. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7461. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(type-SP_STR))) {
  7462. clif_statusupack(sd, type, 0, 0);
  7463. return false;
  7464. }
  7465. // check status points
  7466. needed_points = pc_need_status_point(sd, type, increase);
  7467. if (needed_points < 0 || needed_points > sd->status.status_point) { // Sanity check
  7468. clif_statusupack(sd, type, 0, 0);
  7469. return false;
  7470. }
  7471. // set new values
  7472. final_value = pc_setstat(sd, type, current + increase);
  7473. sd->status.status_point -= needed_points;
  7474. status_calc_pc(sd,SCO_NONE);
  7475. // update increase cost indicator
  7476. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7477. // update statpoint count
  7478. clif_updatestatus(sd, SP_STATUSPOINT);
  7479. // update stat value
  7480. clif_statusupack(sd, type, 1, final_value); // required
  7481. if( final_value > 255 )
  7482. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7483. achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7484. return true;
  7485. }
  7486. /**
  7487. * Raises a stat by the specified amount.
  7488. *
  7489. * Obeys max_parameter limits.
  7490. * Does not subtract status points for the cost of the modified stat points.
  7491. *
  7492. * @param sd The target character.
  7493. * @param type The stat to change (see enum _sp)
  7494. * @param val The stat increase (or decrease) amount.
  7495. * @return the stat increase amount.
  7496. * @retval 0 if no changes were made.
  7497. */
  7498. int pc_statusup2(map_session_data* sd, int type, int val)
  7499. {
  7500. int max, need;
  7501. nullpo_ret(sd);
  7502. if( type < SP_STR || type > SP_LUK )
  7503. {
  7504. clif_statusupack(sd,type,0,0);
  7505. return 0;
  7506. }
  7507. need = pc_need_status_point(sd,type,1);
  7508. max = pc_maxparameter(sd,(enum e_params)(type-SP_STR)); // set new value
  7509. val = pc_setstat(sd, type, cap_value(pc_getstat(sd,type) + val, 1, max));
  7510. status_calc_pc(sd,SCO_NONE);
  7511. // update increase cost indicator
  7512. if( need != pc_need_status_point(sd,type,1) )
  7513. clif_updatestatus(sd, SP_USTR + type-SP_STR);
  7514. // update stat value
  7515. clif_statusupack(sd,type,1,val); // required
  7516. if( val > 255 )
  7517. clif_updatestatus(sd,type); // send after the 'ack' to override the truncated value
  7518. return val;
  7519. }
  7520. /// Returns the number of trait stat points needed to change the specified trait stat by val.
  7521. /// If val is negative, returns the number of trait stat points that would be needed to
  7522. /// raise the specified trait stat from (current value - val) to current value.
  7523. int pc_need_trait_point(map_session_data* sd, int type, int val)
  7524. {
  7525. nullpo_retr(0, sd);
  7526. if (val == 0 || type < SP_POW || type > SP_CRT)
  7527. return 0;
  7528. int low = pc_getstat(sd, type);
  7529. int max = pc_maxparameter(sd, (e_params)(PARAM_POW + type - SP_POW));
  7530. if (low >= max && val > 0)
  7531. return 0; // Official servers show '0' when max is reached
  7532. int high = low + val, sp = 0;
  7533. if (val < 0)
  7534. SWAP(low, high);
  7535. for (; low < high; low++)
  7536. sp += 1;
  7537. return sp;
  7538. }
  7539. /**
  7540. * Returns the value the specified trait stat can be increased by with the current
  7541. * amount of available trait status points for the current character's class.
  7542. *
  7543. * @param sd The target character.
  7544. * @param type Trait stat to verify.
  7545. * @return Maximum value the stat could grow by.
  7546. */
  7547. int pc_maxtraitparameterincrease(map_session_data* sd, int type)
  7548. {
  7549. nullpo_ret(sd);
  7550. if( type < SP_POW || type > SP_CRT ){
  7551. return 0;
  7552. }
  7553. int base, final_val = pc_getstat(sd, type);
  7554. int trait_points = sd->status.trait_point;
  7555. int max_param = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW));
  7556. base = final_val;
  7557. while (final_val <= max_param && trait_points >= 0) {
  7558. trait_points -= 1;
  7559. final_val++;
  7560. }
  7561. final_val--;
  7562. return (final_val > base ? final_val - base : 0);
  7563. }
  7564. /**
  7565. * Raises a trait stat by the specified amount.
  7566. *
  7567. * Obeys max_traitparameter limits.
  7568. * Subtracts trait status points according to the cost of the increased trait stat points.
  7569. *
  7570. * @param sd The target character.
  7571. * @param type The stat to change (see enum _sp)
  7572. * @param increase The stat increase (strictly positive) amount.
  7573. * @retval true if the trait stat was increased by any amount.
  7574. * @retval false if there were no changes.
  7575. */
  7576. bool pc_traitstatusup(map_session_data* sd, int type, int increase)
  7577. {
  7578. nullpo_ret(sd);
  7579. // check conditions
  7580. if (type < SP_POW || type > SP_CRT || increase <= 0) {
  7581. clif_statusupack(sd, type, 0, 0);
  7582. return false;
  7583. }
  7584. // check limits
  7585. int current = pc_getstat(sd, type);
  7586. int max_increase = pc_maxtraitparameterincrease(sd, type);
  7587. increase = cap_value(increase, 0, max_increase); // cap to the maximum status points available
  7588. if (increase <= 0 || current + increase > pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW))) {
  7589. clif_statusupack(sd, type, 0, 0);
  7590. return false;
  7591. }
  7592. // check status points
  7593. int needed_points = pc_need_trait_point(sd, type, increase);
  7594. if (needed_points < 0 || needed_points > sd->status.trait_point) { // Sanity check
  7595. clif_statusupack(sd, type, 0, 0);
  7596. return false;
  7597. }
  7598. // set new values
  7599. int final_value = pc_setstat(sd, type, current + increase);
  7600. sd->status.trait_point -= needed_points;
  7601. status_calc_pc(sd, SCO_NONE);
  7602. // update increase cost indicator
  7603. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7604. // update statpoint count
  7605. clif_updatestatus(sd, SP_TRAITPOINT);
  7606. // update stat value
  7607. clif_statusupack(sd, type, 1, final_value); // required
  7608. if (final_value > 255)
  7609. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7610. //achievement_update_objective(sd, AG_GOAL_STATUS, 1, final_value);
  7611. return true;
  7612. }
  7613. /**
  7614. * Raises a trait stat by the specified amount.
  7615. *
  7616. * Obeys max_trait_parameter limits.
  7617. * Does not subtract status points for the cost of the modified stat points.
  7618. *
  7619. * @param sd The target character.
  7620. * @param type The stat to change (see enum _sp)
  7621. * @param val The stat increase (or decrease) amount.
  7622. * @return the stat increase amount.
  7623. * @retval 0 if no changes were made.
  7624. */
  7625. int pc_traitstatusup2(map_session_data* sd, int type, int val)
  7626. {
  7627. nullpo_ret(sd);
  7628. if (type < SP_POW || type > SP_CRT) {
  7629. clif_statusupack(sd, type, 0, 0);
  7630. return 0;
  7631. }
  7632. int need = pc_need_trait_point(sd, type, 1);
  7633. int max = pc_maxparameter(sd, (enum e_params)(PARAM_POW + type - SP_POW)); // set new value
  7634. val = pc_setstat(sd, type, cap_value(pc_getstat(sd, type) + val, 0, max));
  7635. status_calc_pc(sd, SCO_NONE);
  7636. // update increase cost indicator
  7637. if (need != pc_need_trait_point(sd, type, 1))
  7638. clif_updatestatus(sd, SP_UPOW + type - SP_POW);
  7639. // update stat value
  7640. clif_statusupack(sd, type, 1, val); // required
  7641. if (val > 255)
  7642. clif_updatestatus(sd, type); // send after the 'ack' to override the truncated value
  7643. return val;
  7644. }
  7645. /*==========================================
  7646. * Update skill_lv for player sd
  7647. * Skill point allocation
  7648. *------------------------------------------*/
  7649. void pc_skillup(map_session_data *sd,uint16 skill_id)
  7650. {
  7651. uint16 idx = skill_get_index(skill_id);
  7652. nullpo_retv(sd);
  7653. if (!idx) {
  7654. if (skill_id)
  7655. ShowError("pc_skillup: Player attempts to level up invalid skill '%d'\n", skill_id);
  7656. return;
  7657. }
  7658. // Level up guild skill
  7659. if (SKILL_CHK_GUILD(skill_id)) {
  7660. guild_skillup(sd, skill_id);
  7661. return;
  7662. }
  7663. // Level up homunculus skill
  7664. else if (sd->hd && SKILL_CHK_HOMUN(skill_id)) {
  7665. hom_skillup(sd->hd, skill_id);
  7666. return;
  7667. }
  7668. else {
  7669. if( sd->status.skill_point > 0 &&
  7670. sd->status.skill[idx].id &&
  7671. sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT && //Don't allow raising while you have granted skills. [Skotlex]
  7672. sd->status.skill[idx].lv < skill_tree_get_max(skill_id, sd->status.class_) )
  7673. {
  7674. int lv, range, upgradable;
  7675. sd->status.skill[idx].lv++;
  7676. sd->status.skill_point--;
  7677. if( !skill_get_inf(skill_id) || pc_checkskill_summoner(sd, SUMMONER_POWER_LAND) >= 20 || pc_checkskill_summoner(sd, SUMMONER_POWER_SEA) >= 20 )
  7678. status_calc_pc(sd,SCO_NONE); // Only recalculate for passive skills.
  7679. else if( sd->status.skill_point == 0 && pc_is_taekwon_ranker(sd) )
  7680. pc_calc_skilltree(sd); // Required to grant all TK Ranker skills.
  7681. else
  7682. pc_check_skilltree(sd); // Check if a new skill can Lvlup
  7683. lv = sd->status.skill[idx].lv;
  7684. range = skill_get_range2(&sd->bl, skill_id, lv, false);
  7685. upgradable = (lv < skill_tree_get_max(sd->status.skill[idx].id, sd->status.class_)) ? 1 : 0;
  7686. clif_skillup(sd,skill_id,lv,range,upgradable);
  7687. clif_updatestatus(sd,SP_SKILLPOINT);
  7688. if( skill_id == GN_REMODELING_CART ) /* cart weight info was updated by status_calc_pc */
  7689. clif_updatestatus(sd,SP_CARTINFO);
  7690. if (pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)))
  7691. clif_status_change(&sd->bl, EFST_DEVIL1, 1, 0, 0, 0, 1); //Permanent blind effect from SG_DEVIL.
  7692. if (!pc_has_permission(sd, PC_PERM_ALL_SKILL)) // may skill everything at any time anyways, and this would cause a huge slowdown
  7693. clif_skillinfoblock(sd);
  7694. }
  7695. //else
  7696. // 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);
  7697. }
  7698. }
  7699. /*==========================================
  7700. * /allskill
  7701. *------------------------------------------*/
  7702. int pc_allskillup(map_session_data *sd)
  7703. {
  7704. int i;
  7705. nullpo_ret(sd);
  7706. for (i = 0; i < MAX_SKILL; i++) {
  7707. 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) {
  7708. sd->status.skill[i].lv = (sd->status.skill[i].flag == SKILL_FLAG_TEMPORARY) ? 0 : sd->status.skill[i].flag - SKILL_FLAG_REPLACED_LV_0;
  7709. sd->status.skill[i].flag = SKILL_FLAG_PERMANENT;
  7710. if (sd->status.skill[i].lv == 0)
  7711. sd->status.skill[i].id = 0;
  7712. }
  7713. }
  7714. if (!pc_grant_allskills(sd, true)) {
  7715. uint16 sk_id;
  7716. std::shared_ptr<s_skill_tree> tree = skill_tree_db.find(sd->status.class_);
  7717. if (tree != nullptr && !tree->skills.empty()) {
  7718. for (const auto &skillsit : tree->skills) {
  7719. sk_id = skillsit.first;
  7720. uint16 sk_idx = skill_get_index(sk_id);
  7721. if (sk_idx == 0)
  7722. continue;
  7723. std::shared_ptr<s_skill_db> skill = skill_db.find(sk_id);
  7724. if (
  7725. (skill->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn) ||
  7726. ((skill->inf2[INF2_ISWEDDING] || skill->inf2[INF2_ISSPIRIT])) ||
  7727. sk_id == SG_DEVIL
  7728. )
  7729. continue; //Cannot be learned normally.
  7730. sd->status.skill[sk_idx].id = sk_id;
  7731. sd->status.skill[sk_idx].lv = skill_tree_get_max(sk_id, sd->status.class_); // celest
  7732. }
  7733. }
  7734. }
  7735. status_calc_pc(sd,SCO_NONE);
  7736. //Required because if you could level up all skills previously,
  7737. //the update will not be sent as only the lv variable changes.
  7738. clif_skillinfoblock(sd);
  7739. return 0;
  7740. }
  7741. /*==========================================
  7742. * /resetlvl
  7743. *------------------------------------------*/
  7744. int pc_resetlvl(map_session_data* sd,int type)
  7745. {
  7746. int i;
  7747. nullpo_ret(sd);
  7748. if (type != 3) //Also reset skills
  7749. pc_resetskill(sd, 0);
  7750. if(type == 1){
  7751. sd->status.skill_point=0;
  7752. sd->status.trait_point = 0;
  7753. sd->status.base_level=1;
  7754. sd->status.job_level=1;
  7755. sd->status.base_exp=0;
  7756. sd->status.job_exp=0;
  7757. if(sd->sc.option !=0)
  7758. sd->sc.option = 0;
  7759. sd->status.str=1;
  7760. sd->status.agi=1;
  7761. sd->status.vit=1;
  7762. sd->status.int_=1;
  7763. sd->status.dex=1;
  7764. sd->status.luk=1;
  7765. sd->status.pow=0;
  7766. sd->status.sta=0;
  7767. sd->status.wis=0;
  7768. sd->status.spl=0;
  7769. sd->status.con=0;
  7770. sd->status.crt=0;
  7771. if(sd->status.class_ == JOB_NOVICE_HIGH) {
  7772. sd->status.status_point=100; // not 88 [celest]
  7773. // give platinum skills upon changing
  7774. pc_skill(sd,NV_FIRSTAID,1,ADDSKILL_PERMANENT);
  7775. pc_skill(sd,NV_TRICKDEAD,1,ADDSKILL_PERMANENT);
  7776. }
  7777. }
  7778. if(type == 2){
  7779. sd->status.skill_point=0;
  7780. sd->status.base_level=1;
  7781. sd->status.job_level=1;
  7782. sd->status.base_exp=0;
  7783. sd->status.job_exp=0;
  7784. }
  7785. if(type == 3){
  7786. sd->status.base_level=1;
  7787. sd->status.base_exp=0;
  7788. }
  7789. if(type == 4){
  7790. sd->status.job_level=1;
  7791. sd->status.job_exp=0;
  7792. }
  7793. clif_updatestatus(sd,SP_STATUSPOINT);
  7794. clif_updatestatus(sd,SP_TRAITPOINT);
  7795. clif_updatestatus(sd,SP_STR);
  7796. clif_updatestatus(sd,SP_AGI);
  7797. clif_updatestatus(sd,SP_VIT);
  7798. clif_updatestatus(sd,SP_INT);
  7799. clif_updatestatus(sd,SP_DEX);
  7800. clif_updatestatus(sd,SP_LUK);
  7801. clif_updatestatus(sd,SP_POW);
  7802. clif_updatestatus(sd,SP_STA);
  7803. clif_updatestatus(sd,SP_WIS);
  7804. clif_updatestatus(sd,SP_SPL);
  7805. clif_updatestatus(sd,SP_CON);
  7806. clif_updatestatus(sd,SP_CRT);
  7807. clif_updatestatus(sd,SP_BASELEVEL);
  7808. clif_updatestatus(sd,SP_JOBLEVEL);
  7809. clif_updatestatus(sd,SP_STATUSPOINT);
  7810. clif_updatestatus(sd,SP_BASEEXP);
  7811. clif_updatestatus(sd,SP_JOBEXP);
  7812. clif_updatestatus(sd,SP_NEXTBASEEXP);
  7813. clif_updatestatus(sd,SP_NEXTJOBEXP);
  7814. clif_updatestatus(sd,SP_SKILLPOINT);
  7815. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7816. clif_updatestatus(sd,SP_UAGI);
  7817. clif_updatestatus(sd,SP_UVIT);
  7818. clif_updatestatus(sd,SP_UINT);
  7819. clif_updatestatus(sd,SP_UDEX);
  7820. clif_updatestatus(sd,SP_ULUK); // End Addition
  7821. clif_updatestatus(sd,SP_UPOW);
  7822. clif_updatestatus(sd,SP_USTA);
  7823. clif_updatestatus(sd,SP_UWIS);
  7824. clif_updatestatus(sd,SP_USPL);
  7825. clif_updatestatus(sd,SP_UCON);
  7826. clif_updatestatus(sd,SP_UCRT);
  7827. for(i=0;i<EQI_MAX;i++) { // unequip items that can't be equipped by base 1 [Valaris]
  7828. if(sd->equip_index[i] >= 0)
  7829. if(pc_isequip(sd,sd->equip_index[i]))
  7830. pc_unequipitem(sd,sd->equip_index[i],2);
  7831. }
  7832. if ((type == 1 || type == 2 || type == 3) && sd->status.party_id)
  7833. party_send_levelup(sd);
  7834. status_calc_pc(sd, SCO_FORCE);
  7835. clif_skillinfoblock(sd);
  7836. return 0;
  7837. }
  7838. /*==========================================
  7839. * /resetstate
  7840. *------------------------------------------*/
  7841. int pc_resetstate(map_session_data* sd)
  7842. {
  7843. nullpo_ret(sd);
  7844. if( sd->status.base_level > pc_maxbaselv( sd ) ){
  7845. 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",
  7846. pc_maxbaselv( sd ), sd->status.account_id, sd->status.char_id, sd->status.base_level );
  7847. sd->status.base_level = pc_maxbaselv( sd );
  7848. clif_updatestatus( sd, SP_BASELEVEL );
  7849. }
  7850. sd->status.status_point = statpoint_db.get_table_point( sd->status.base_level );
  7851. sd->status.trait_point = statpoint_db.get_trait_table_point(sd->status.base_level);
  7852. if( ( sd->class_&JOBL_UPPER ) != 0 ){
  7853. sd->status.status_point += battle_config.transcendent_status_points;
  7854. }
  7855. if ((sd->class_&JOBL_FOURTH) != 0) {
  7856. sd->status.trait_point += battle_config.trait_points_job_change;
  7857. }
  7858. pc_setstat(sd, SP_STR, 1);
  7859. pc_setstat(sd, SP_AGI, 1);
  7860. pc_setstat(sd, SP_VIT, 1);
  7861. pc_setstat(sd, SP_INT, 1);
  7862. pc_setstat(sd, SP_DEX, 1);
  7863. pc_setstat(sd, SP_LUK, 1);
  7864. pc_setstat(sd, SP_POW, 0);
  7865. pc_setstat(sd, SP_STA, 0);
  7866. pc_setstat(sd, SP_WIS, 0);
  7867. pc_setstat(sd, SP_SPL, 0);
  7868. pc_setstat(sd, SP_CON, 0);
  7869. pc_setstat(sd, SP_CRT, 0);
  7870. clif_updatestatus(sd,SP_STR);
  7871. clif_updatestatus(sd,SP_AGI);
  7872. clif_updatestatus(sd,SP_VIT);
  7873. clif_updatestatus(sd,SP_INT);
  7874. clif_updatestatus(sd,SP_DEX);
  7875. clif_updatestatus(sd,SP_LUK);
  7876. clif_updatestatus(sd,SP_POW);
  7877. clif_updatestatus(sd,SP_STA);
  7878. clif_updatestatus(sd,SP_WIS);
  7879. clif_updatestatus(sd,SP_SPL);
  7880. clif_updatestatus(sd,SP_CON);
  7881. clif_updatestatus(sd,SP_CRT);
  7882. clif_updatestatus(sd,SP_USTR); // Updates needed stat points - Valaris
  7883. clif_updatestatus(sd,SP_UAGI);
  7884. clif_updatestatus(sd,SP_UVIT);
  7885. clif_updatestatus(sd,SP_UINT);
  7886. clif_updatestatus(sd,SP_UDEX);
  7887. clif_updatestatus(sd,SP_ULUK); // End Addition
  7888. clif_updatestatus(sd,SP_UPOW);
  7889. clif_updatestatus(sd,SP_USTA);
  7890. clif_updatestatus(sd,SP_UWIS);
  7891. clif_updatestatus(sd,SP_USPL);
  7892. clif_updatestatus(sd,SP_UCON);
  7893. clif_updatestatus(sd,SP_UCRT);
  7894. clif_updatestatus(sd,SP_STATUSPOINT);
  7895. clif_updatestatus(sd,SP_TRAITPOINT);
  7896. if( sd->mission_mobid ) { //bugreport:2200
  7897. sd->mission_mobid = 0;
  7898. sd->mission_count = 0;
  7899. pc_setglobalreg(sd, add_str(TKMISSIONID_VAR), 0);
  7900. }
  7901. status_calc_pc(sd, SCO_NONE);
  7902. return 1;
  7903. }
  7904. /*==========================================
  7905. * /resetskill
  7906. * if flag&1, perform block resync and status_calc call.
  7907. * if flag&2, just count total amount of skill points used by player, do not really reset.
  7908. * if flag&4, just reset the skills if the player class is a bard/dancer type (for changesex.)
  7909. *------------------------------------------*/
  7910. int pc_resetskill(map_session_data* sd, int flag)
  7911. {
  7912. int i, skill_point=0;
  7913. nullpo_ret(sd);
  7914. if( flag&4 && (sd->class_&MAPID_UPPERMASK) != MAPID_BARDDANCER )
  7915. return 0;
  7916. if( !(flag&2) ) { //Remove stuff lost when resetting skills.
  7917. /**
  7918. * 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)
  7919. **/
  7920. if( pc_is_taekwon_ranker(sd) )
  7921. return 0;
  7922. if( pc_checkskill(sd, SG_DEVIL) && ((sd->class_&MAPID_THIRDMASK) == MAPID_STAR_EMPEROR || pc_is_maxjoblv(sd)) )
  7923. clif_status_load(&sd->bl, EFST_DEVIL1, 0); //Remove perma blindness due to skill-reset. [Skotlex]
  7924. i = sd->sc.option;
  7925. if( i&OPTION_RIDING && pc_checkskill(sd, KN_RIDING) )
  7926. i &= ~OPTION_RIDING;
  7927. if( i&OPTION_FALCON && pc_checkskill(sd, HT_FALCON) )
  7928. i &= ~OPTION_FALCON;
  7929. if( i&OPTION_DRAGON && pc_checkskill(sd, RK_DRAGONTRAINING) )
  7930. i &= ~OPTION_DRAGON;
  7931. if( i&OPTION_WUG && pc_checkskill(sd, RA_WUGMASTERY) )
  7932. i &= ~OPTION_WUG;
  7933. if( i&OPTION_WUGRIDER && pc_checkskill(sd, RA_WUGRIDER) )
  7934. i &= ~OPTION_WUGRIDER;
  7935. if( i&OPTION_MADOGEAR && ( sd->class_&MAPID_THIRDMASK ) == MAPID_MECHANIC )
  7936. i &= ~OPTION_MADOGEAR;
  7937. #ifndef NEW_CARTS
  7938. if( i&OPTION_CART && pc_checkskill(sd, MC_PUSHCART) )
  7939. i &= ~OPTION_CART;
  7940. #else
  7941. if( sd->sc.getSCE(SC_PUSH_CART) )
  7942. pc_setcart(sd, 0);
  7943. #endif
  7944. if( i != sd->sc.option )
  7945. pc_setoption(sd, i);
  7946. if( hom_is_active(sd->hd) && pc_checkskill(sd, AM_CALLHOMUN) )
  7947. hom_vaporize(sd, HOM_ST_ACTIVE);
  7948. if (sd->sc.getSCE(SC_SPRITEMABLE) && pc_checkskill(sd, SU_SPRITEMABLE))
  7949. status_change_end(&sd->bl, SC_SPRITEMABLE);
  7950. if (sd->sc.getSCE(SC_SOULATTACK) && pc_checkskill(sd, SU_SOULATTACK))
  7951. status_change_end(&sd->bl, SC_SOULATTACK);
  7952. }
  7953. for (const auto &skill : skill_db) {
  7954. uint16 skill_id = skill.second->nameid, idx = skill_get_index(skill_id);
  7955. uint8 lv = sd->status.skill[idx].lv;
  7956. if (lv == 0 || skill_id == 0)
  7957. continue;
  7958. if( skill.second->inf2[INF2_ISWEDDING] || skill.second->inf2[INF2_ISSPIRIT] ) //Avoid reseting wedding/linker skills.
  7959. continue;
  7960. // Don't reset trick dead if not a novice/baby
  7961. if( skill_id == NV_TRICKDEAD && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7962. {
  7963. sd->status.skill[idx].lv = 0;
  7964. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7965. continue;
  7966. }
  7967. // do not reset basic skill
  7968. if (skill_id == NV_BASIC && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE )
  7969. continue;
  7970. if( sd->status.skill[idx].flag == SKILL_FLAG_PERM_GRANTED )
  7971. continue;
  7972. if( flag&4 && !skill_ischangesex(skill_id) )
  7973. continue;
  7974. if( skill.second->inf2[INF2_ISQUEST] && !battle_config.quest_skill_learn )
  7975. { //Only handle quest skills in a special way when you can't learn them manually
  7976. if( battle_config.quest_skill_reset && !(flag&2) )
  7977. { //Wipe them
  7978. sd->status.skill[idx].lv = 0;
  7979. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7980. }
  7981. continue;
  7982. }
  7983. if( sd->status.skill[idx].flag == SKILL_FLAG_PERMANENT )
  7984. skill_point += lv;
  7985. else
  7986. if( sd->status.skill[idx].flag >= SKILL_FLAG_REPLACED_LV_0 )
  7987. skill_point += (sd->status.skill[idx].flag - SKILL_FLAG_REPLACED_LV_0);
  7988. if( !(flag&2) )
  7989. {// reset
  7990. sd->status.skill[idx].lv = 0;
  7991. sd->status.skill[idx].flag = SKILL_FLAG_PERMANENT;
  7992. }
  7993. }
  7994. if( flag&2 || !skill_point ) return skill_point;
  7995. sd->status.skill_point += skill_point;
  7996. if (flag&1) {
  7997. clif_updatestatus(sd,SP_SKILLPOINT);
  7998. clif_skillinfoblock(sd);
  7999. status_calc_pc(sd, SCO_FORCE);
  8000. }
  8001. return skill_point;
  8002. }
  8003. /*==========================================
  8004. * /resetfeel [Komurka]
  8005. *------------------------------------------*/
  8006. int pc_resetfeel(map_session_data* sd)
  8007. {
  8008. int i;
  8009. nullpo_ret(sd);
  8010. for (i=0; i<MAX_PC_FEELHATE; i++)
  8011. {
  8012. sd->feel_map[i].m = -1;
  8013. sd->feel_map[i].index = 0;
  8014. pc_setglobalreg(sd, add_str(sg_info[i].feel_var), 0);
  8015. }
  8016. return 0;
  8017. }
  8018. int pc_resethate(map_session_data* sd)
  8019. {
  8020. int i;
  8021. nullpo_ret(sd);
  8022. for (i=0; i<MAX_PC_FEELHATE; i++)
  8023. {
  8024. sd->hate_mob[i] = -1;
  8025. pc_setglobalreg(sd, add_str(sg_info[i].hate_var), 0);
  8026. }
  8027. return 0;
  8028. }
  8029. int pc_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8030. {
  8031. int bonus = 0;
  8032. nullpo_ret(sd);
  8033. skill_id = skill_dummy2skill_id(skill_id);
  8034. for (auto &it : sd->skillatk) {
  8035. if (it.id == skill_id) {
  8036. bonus += it.val;
  8037. break;
  8038. }
  8039. }
  8040. return bonus;
  8041. }
  8042. int pc_sub_skillatk_bonus(map_session_data *sd, uint16 skill_id)
  8043. {
  8044. int bonus = 0;
  8045. nullpo_ret(sd);
  8046. skill_id = skill_dummy2skill_id(skill_id);
  8047. for (auto &it : sd->subskill) {
  8048. if (it.id == skill_id) {
  8049. bonus += it.val;
  8050. break;
  8051. }
  8052. }
  8053. return bonus;
  8054. }
  8055. int pc_skillheal_bonus(map_session_data *sd, uint16 skill_id) {
  8056. int bonus = sd->bonus.add_heal_rate;
  8057. nullpo_ret(sd);
  8058. skill_id = skill_dummy2skill_id(skill_id);
  8059. if( bonus ) {
  8060. switch( skill_id ) {
  8061. case AL_HEAL: if( !(battle_config.skill_add_heal_rate&1) ) bonus = 0; break;
  8062. case PR_SANCTUARY: if( !(battle_config.skill_add_heal_rate&2) ) bonus = 0; break;
  8063. case AM_POTIONPITCHER: if( !(battle_config.skill_add_heal_rate&4) ) bonus = 0; break;
  8064. case CR_SLIMPITCHER: if( !(battle_config.skill_add_heal_rate&8) ) bonus = 0; break;
  8065. case BA_APPLEIDUN: if( !(battle_config.skill_add_heal_rate&16)) bonus = 0; break;
  8066. case AB_CHEAL: if (!(battle_config.skill_add_heal_rate & 32)) bonus = 0; break;
  8067. case AB_HIGHNESSHEAL: if (!(battle_config.skill_add_heal_rate & 64)) bonus = 0; break;
  8068. case CD_MEDIALE_VOTUM: if (!(battle_config.skill_add_heal_rate & 128)) bonus = 0; break;
  8069. case CD_DILECTIO_HEAL: if (!(battle_config.skill_add_heal_rate & 256)) bonus = 0; break;
  8070. }
  8071. }
  8072. for (auto &it : sd->skillheal) {
  8073. if (it.id == skill_id) {
  8074. bonus += it.val;
  8075. break;
  8076. }
  8077. }
  8078. return bonus;
  8079. }
  8080. int pc_skillheal2_bonus(map_session_data *sd, uint16 skill_id) {
  8081. int bonus = sd->bonus.add_heal2_rate;
  8082. skill_id = skill_dummy2skill_id(skill_id);
  8083. for (auto &it : sd->skillheal2) {
  8084. if (it.id == skill_id) {
  8085. bonus += it.val;
  8086. break;
  8087. }
  8088. }
  8089. return bonus;
  8090. }
  8091. void pc_respawn(map_session_data* sd, clr_type clrtype)
  8092. {
  8093. if( !pc_isdead(sd) )
  8094. return; // not applicable
  8095. if( sd->bg_id && bg_member_respawn(sd) )
  8096. return; // member revived by battleground
  8097. pc_setstand(sd, true);
  8098. pc_setrestartvalue(sd,3);
  8099. if( pc_setpos( sd, mapindex_name2id( sd->status.save_point.map ), sd->status.save_point.x, sd->status.save_point.y, clrtype ) != SETPOS_OK ){
  8100. clif_resurrection(&sd->bl, 1); //If warping fails, send a normal stand up packet.
  8101. }
  8102. }
  8103. static TIMER_FUNC(pc_respawn_timer){
  8104. map_session_data *sd = map_id2sd(id);
  8105. if( sd != NULL )
  8106. {
  8107. sd->pvp_point=0;
  8108. sd->respawn_tid = INVALID_TIMER;
  8109. pc_respawn(sd,CLR_OUTSIGHT);
  8110. }
  8111. return 0;
  8112. }
  8113. /*==========================================
  8114. * Invoked when a player has received damage
  8115. *------------------------------------------*/
  8116. void pc_damage(map_session_data *sd,struct block_list *src,unsigned int hp, unsigned int sp, unsigned int ap)
  8117. {
  8118. if (ap) clif_updatestatus(sd,SP_AP);
  8119. if (sp) clif_updatestatus(sd,SP_SP);
  8120. if (hp) clif_updatestatus(sd,SP_HP);
  8121. else return;
  8122. if (!src)
  8123. return;
  8124. if( pc_issit(sd) ) {
  8125. pc_setstand(sd, true);
  8126. skill_sit(sd,0);
  8127. }
  8128. if (sd->progressbar.npc_id)
  8129. clif_progressbar_abort(sd);
  8130. if( sd->status.pet_id > 0 && sd->pd && battle_config.pet_damage_support )
  8131. pet_target_check(sd->pd,src,1);
  8132. if( sd->status.ele_id > 0 )
  8133. elemental_set_target(sd,src);
  8134. if(battle_config.prevent_logout_trigger&PLT_DAMAGE)
  8135. sd->canlog_tick = gettick();
  8136. }
  8137. TIMER_FUNC(pc_close_npc_timer){
  8138. TBL_PC *sd = map_id2sd(id);
  8139. if(sd) pc_close_npc(sd,data);
  8140. return 0;
  8141. }
  8142. /**
  8143. * Method to properly close a NPC for player and clear anything related.
  8144. * @param sd: Player attached
  8145. * @param flag: Method of closure
  8146. * 1: Produce a close button and end the NPC
  8147. * 2: End the NPC (best for no dialog windows)
  8148. */
  8149. void pc_close_npc(map_session_data *sd,int flag)
  8150. {
  8151. nullpo_retv(sd);
  8152. if (sd->npc_id || sd->npc_shopid) {
  8153. if (sd->state.using_fake_npc) {
  8154. clif_clearunit_single(sd->npc_id, CLR_OUTSIGHT, sd->fd);
  8155. sd->state.using_fake_npc = 0;
  8156. }
  8157. if (sd->st) {
  8158. if(sd->st->state == RUN){ //wait ending code execution
  8159. add_timer(gettick()+500,pc_close_npc_timer,sd->bl.id,flag);
  8160. return;
  8161. }
  8162. sd->st->state = ((flag==1 && sd->st->mes_active)?CLOSE:END);
  8163. sd->st->mes_active = 0;
  8164. }
  8165. sd->state.menu_or_input = 0;
  8166. sd->npc_menu = 0;
  8167. sd->npc_shopid = 0;
  8168. #ifdef SECURE_NPCTIMEOUT
  8169. if( sd->npc_idle_timer != INVALID_TIMER ){
  8170. delete_timer( sd->npc_idle_timer, npc_secure_timeout_timer );
  8171. sd->npc_idle_timer = INVALID_TIMER;
  8172. }
  8173. #endif
  8174. if (sd->st) {
  8175. if (sd->st->state == CLOSE) {
  8176. clif_scriptclose(sd, sd->npc_id);
  8177. clif_scriptclear( *sd, sd->npc_id ); // [Ind/Hercules]
  8178. sd->st->state = END; // Force to end now
  8179. }
  8180. if (sd->st->state == END) { // free attached scripts that are waiting
  8181. script_free_state(sd->st);
  8182. sd->st = NULL;
  8183. sd->npc_id = 0;
  8184. }
  8185. }
  8186. }
  8187. }
  8188. /*==========================================
  8189. * Invoked when a player has negative current hp
  8190. *------------------------------------------*/
  8191. int pc_dead(map_session_data *sd,struct block_list *src)
  8192. {
  8193. int i=0,k=0;
  8194. t_tick tick = gettick();
  8195. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  8196. // Activate Steel body if a super novice dies at 99+% exp [celest]
  8197. // Super Novices have no kill or die functions attached when saved by their angel
  8198. if (!sd->state.snovice_dead_flag && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE) {
  8199. t_exp exp = pc_nextbaseexp(sd);
  8200. if( exp && get_percentage_exp(sd->status.base_exp, exp) >= 99 ) {
  8201. sd->state.snovice_dead_flag = 1;
  8202. pc_setrestartvalue(sd,1);
  8203. status_percent_heal(&sd->bl, 100, 100);
  8204. clif_resurrection(&sd->bl, 1);
  8205. if(battle_config.pc_invincible_time)
  8206. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8207. sc_start(&sd->bl,&sd->bl,SC_STEELBODY,100,5,skill_get_time(MO_STEELBODY,5));
  8208. if(mapdata_flag_gvg2(mapdata))
  8209. pc_respawn_timer(INVALID_TIMER, gettick(), sd->bl.id, 0);
  8210. return 0;
  8211. }
  8212. }
  8213. for(k = 0; k < MAX_DEVOTION; k++) {
  8214. if (sd->devotion[k]){
  8215. map_session_data *devsd = map_id2sd(sd->devotion[k]);
  8216. if (devsd)
  8217. status_change_end(&devsd->bl, SC_DEVOTION);
  8218. sd->devotion[k] = 0;
  8219. }
  8220. }
  8221. for (k = 0; k < MAX_STELLAR_MARKS; k++) {
  8222. if (sd->stellar_mark[k]) {
  8223. map_session_data *smarksd = map_id2sd(sd->stellar_mark[k]);
  8224. if (smarksd)
  8225. status_change_end(&smarksd->bl, SC_FLASHKICK);
  8226. sd->stellar_mark[k] = 0;
  8227. }
  8228. }
  8229. for (k = 0; k < MAX_UNITED_SOULS; k++) {
  8230. if (sd->united_soul[k]) {
  8231. map_session_data *usoulsd = map_id2sd(sd->united_soul[k]);
  8232. if (usoulsd)
  8233. status_change_end(&usoulsd->bl, SC_SOULUNITY);
  8234. sd->united_soul[k] = 0;
  8235. }
  8236. }
  8237. for (k = 0; k < MAX_SERVANT_SIGN; k++) {
  8238. if (sd->servant_sign[k]) {
  8239. map_session_data *ssignsd = map_id2sd(sd->servant_sign[k]);
  8240. if (ssignsd)
  8241. status_change_end(&ssignsd->bl, SC_SERVANT_SIGN);
  8242. sd->servant_sign[k] = 0;
  8243. }
  8244. }
  8245. if(sd->shadowform_id) { //if we were target of shadowform
  8246. status_change_end(map_id2bl(sd->shadowform_id), SC__SHADOWFORM);
  8247. sd->shadowform_id = 0; //should be remove on status end anyway
  8248. }
  8249. if(sd->status.pet_id > 0 && sd->pd) {
  8250. struct pet_data *pd = sd->pd;
  8251. if( !mapdata->flag[MF_NOEXPPENALTY] ) {
  8252. pet_set_intimate(pd, pd->pet.intimate + pd->get_pet_db()->die);
  8253. if( pd->pet.intimate <= PET_INTIMATE_NONE )
  8254. pet_set_intimate(pd, PET_INTIMATE_NONE);
  8255. clif_send_petdata(sd,sd->pd,1,pd->pet.intimate);
  8256. }
  8257. if( sd->pd->target_id ) // Unlock all targets...
  8258. pet_unlocktarget(sd->pd);
  8259. }
  8260. 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)
  8261. hom_vaporize(sd, HOM_ST_ACTIVE);
  8262. if( sd->md )
  8263. mercenary_delete(sd->md, 3); // Your mercenary soldier has ran away.
  8264. if( sd->ed )
  8265. elemental_delete(sd->ed);
  8266. // Leave duel if you die [LuzZza]
  8267. if(battle_config.duel_autoleave_when_die) {
  8268. if(sd->duel_group > 0)
  8269. duel_leave(sd->duel_group, sd);
  8270. if(sd->duel_invite > 0)
  8271. duel_reject(sd->duel_invite, sd);
  8272. }
  8273. if( sd->skill_keep_using.tid != INVALID_TIMER ){
  8274. delete_timer( sd->skill_keep_using.tid, skill_keep_using );
  8275. sd->skill_keep_using.tid = INVALID_TIMER;
  8276. }
  8277. pc_close_npc(sd,2); //close npc if we were using one
  8278. /* e.g. not killed thru pc_damage */
  8279. if( pc_issit(sd) ) {
  8280. clif_status_load(&sd->bl,EFST_SIT,0);
  8281. }
  8282. pc_setdead(sd);
  8283. clif_party_dead( *sd );
  8284. pc_setparam(sd, SP_PCDIECOUNTER, sd->die_counter+1);
  8285. pc_setparam(sd, SP_KILLERRID, src?src->id:0);
  8286. if (battle_config.loose_ap_on_death == 1)
  8287. status_percent_damage( nullptr, &sd->bl, 0, 0, 100, 0 );
  8288. //Reset menu skills/item skills
  8289. if ((sd->skillitem) != 0)
  8290. sd->skillitem = sd->skillitemlv = 0;
  8291. if ((sd->menuskill_id) != 0)
  8292. sd->menuskill_id = sd->menuskill_val = 0;
  8293. //Reset ticks.
  8294. sd->hp_loss.tick = sd->sp_loss.tick = sd->hp_regen.tick = sd->sp_regen.tick = 0;
  8295. if ( sd->spiritball !=0 )
  8296. pc_delspiritball(sd,sd->spiritball,0);
  8297. if (sd->soulball != 0)
  8298. pc_delsoulball(sd, sd->soulball, false);
  8299. if (sd->servantball != 0)
  8300. pc_delservantball( *sd, sd->servantball );
  8301. if (sd->abyssball != 0)
  8302. pc_delabyssball( *sd, sd->abyssball );
  8303. if (sd->spiritcharm_type != CHARM_TYPE_NONE && sd->spiritcharm > 0)
  8304. pc_delspiritcharm(sd,sd->spiritcharm,sd->spiritcharm_type);
  8305. if (src)
  8306. switch (src->type) {
  8307. case BL_MOB:
  8308. {
  8309. struct mob_data *md=(struct mob_data *)src;
  8310. if(md->target_id==sd->bl.id)
  8311. mob_unlocktarget(md,tick);
  8312. if(battle_config.mobs_level_up && md->status.hp &&
  8313. (unsigned int)md->level < pc_maxbaselv(sd) &&
  8314. !md->guardian_data && !md->special_state.ai// Guardians/summons should not level. [Skotlex]
  8315. ) { // monster level up [Valaris]
  8316. clif_misceffect(&md->bl,0);
  8317. md->level++;
  8318. status_calc_mob(md, SCO_NONE);
  8319. status_percent_heal(src,10,0);
  8320. if( battle_config.show_mob_info&4 )
  8321. {// update name with new level
  8322. clif_name_area(&md->bl);
  8323. }
  8324. }
  8325. src = battle_get_master(src); // Maybe Player Summon
  8326. }
  8327. break;
  8328. case BL_PET: //Pass on to master...
  8329. case BL_HOM:
  8330. case BL_MER:
  8331. src = battle_get_master(src);
  8332. break;
  8333. }
  8334. if (src && src->type == BL_PC) {
  8335. map_session_data *ssd = (map_session_data *)src;
  8336. pc_setparam(ssd, SP_KILLEDRID, sd->bl.id);
  8337. npc_script_event(ssd, NPCE_KILLPC);
  8338. if (battle_config.pk_mode&2) {
  8339. ssd->status.manner -= 5;
  8340. if(ssd->status.manner < 0)
  8341. sc_start(&sd->bl,src,SC_NOCHAT,100,0,0);
  8342. #if 0
  8343. // PK/Karma system code (not enabled yet) [celest]
  8344. // originally from Kade Online, so i don't know if any of these is correct ^^;
  8345. // note: karma is measured REVERSE, so more karma = more 'evil' / less honourable,
  8346. // karma going down = more 'good' / more honourable.
  8347. // The Karma System way...
  8348. if (sd->status.karma > ssd->status.karma) { // If player killed was more evil
  8349. sd->status.karma--;
  8350. ssd->status.karma--;
  8351. }
  8352. else if (sd->status.karma < ssd->status.karma) // If player killed was more good
  8353. ssd->status.karma++;
  8354. // or the PK System way...
  8355. if (sd->status.karma > 0) // player killed is dishonourable?
  8356. ssd->status.karma--; // honour points earned
  8357. sd->status.karma++; // honour points lost
  8358. // To-do: Receive exp on certain occasions
  8359. #endif
  8360. }
  8361. }
  8362. if(battle_config.bone_drop==2
  8363. || (battle_config.bone_drop==1 && mapdata->flag[MF_PVP]))
  8364. {
  8365. struct item item_tmp;
  8366. memset(&item_tmp,0,sizeof(item_tmp));
  8367. item_tmp.nameid=ITEMID_SKULL_;
  8368. item_tmp.identify=1;
  8369. item_tmp.card[0]=CARD0_CREATE;
  8370. item_tmp.card[1]=0;
  8371. item_tmp.card[2]=GetWord(sd->status.char_id,0); // CharId
  8372. item_tmp.card[3]=GetWord(sd->status.char_id,1);
  8373. map_addflooritem(&item_tmp,1,sd->bl.m,sd->bl.x,sd->bl.y,0,0,0,0,0);
  8374. }
  8375. //Remove bonus_script when dead
  8376. pc_bonus_script_clear(sd,BSF_REM_ON_DEAD);
  8377. // changed penalty options, added death by player if pk_mode [Valaris]
  8378. if(battle_config.death_penalty_type
  8379. && (sd->class_&MAPID_UPPERMASK) != MAPID_NOVICE // only novices will receive no penalty
  8380. && !sd->sc.cant.deathpenalty
  8381. && !mapdata->flag[MF_NOEXPPENALTY] && !mapdata_flag_gvg2(mapdata))
  8382. {
  8383. t_exp base_penalty = 0;
  8384. t_exp job_penalty = 0;
  8385. uint32 zeny_penalty = 0;
  8386. if (pc_isvip(sd)) { // EXP penalty for VIP
  8387. base_penalty = battle_config.vip_exp_penalty_base;
  8388. job_penalty = battle_config.vip_exp_penalty_job;
  8389. zeny_penalty = battle_config.vip_zeny_penalty;
  8390. } else {
  8391. base_penalty = battle_config.death_penalty_base;
  8392. job_penalty = battle_config.death_penalty_job;
  8393. zeny_penalty = battle_config.zeny_penalty;
  8394. }
  8395. if ((battle_config.death_penalty_maxlv&1 || !pc_is_maxbaselv(sd)) && base_penalty > 0) {
  8396. switch (battle_config.death_penalty_type) {
  8397. case 1: base_penalty = (t_exp) ( pc_nextbaseexp(sd) * ( base_penalty / 10000. ) ); break;
  8398. case 2: base_penalty = (t_exp) ( sd->status.base_exp * ( base_penalty / 10000. ) ); break;
  8399. }
  8400. if (base_penalty){ //recheck after altering to speedup
  8401. if (battle_config.pk_mode && src && src->type==BL_PC)
  8402. base_penalty *= 2;
  8403. base_penalty = u64min(sd->status.base_exp, base_penalty);
  8404. }
  8405. }
  8406. else
  8407. base_penalty = 0;
  8408. if ((battle_config.death_penalty_maxlv&2 || !pc_is_maxjoblv(sd)) && job_penalty > 0) {
  8409. switch (battle_config.death_penalty_type) {
  8410. case 1: job_penalty = (uint32) ( pc_nextjobexp(sd) * ( job_penalty / 10000. ) ); break;
  8411. case 2: job_penalty = (uint32) ( sd->status.job_exp * ( job_penalty /10000. ) ); break;
  8412. }
  8413. if (job_penalty) {
  8414. if (battle_config.pk_mode && src && src->type==BL_PC)
  8415. job_penalty *= 2;
  8416. job_penalty = u64min(sd->status.job_exp, job_penalty);
  8417. }
  8418. }
  8419. else
  8420. job_penalty = 0;
  8421. if (base_penalty || job_penalty) {
  8422. short insurance_idx = pc_search_inventory(sd, ITEMID_NEW_INSURANCE);
  8423. if (insurance_idx < 0 || pc_delitem(sd, insurance_idx, 1, 0, 1, LOG_TYPE_CONSUME) != 0)
  8424. pc_lostexp(sd, base_penalty, job_penalty);
  8425. }
  8426. if( zeny_penalty > 0 && !mapdata->flag[MF_NOZENYPENALTY]) {
  8427. zeny_penalty = (uint32)( sd->status.zeny * ( zeny_penalty / 10000. ) );
  8428. if(zeny_penalty)
  8429. pc_payzeny(sd, zeny_penalty, LOG_TYPE_PICKDROP_PLAYER, NULL);
  8430. }
  8431. }
  8432. if( mapdata->flag[MF_PVP_NIGHTMAREDROP] ) { // Moved this outside so it works when PVP isn't enabled and during pk mode [Ancyker]
  8433. for (const auto &it : mapdata->drop_list) {
  8434. int id = it.drop_id, per = it.drop_per;
  8435. enum e_nightmare_drop_type type = it.drop_type;
  8436. if(id == 0)
  8437. continue;
  8438. if(id == -1){
  8439. int eq_num=0,eq_n[MAX_INVENTORY];
  8440. memset(eq_n,0,sizeof(eq_n));
  8441. for(i=0;i<MAX_INVENTORY;i++) {
  8442. if( (type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8443. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8444. || type&NMDT_ALL)
  8445. {
  8446. int l;
  8447. ARR_FIND( 0, MAX_INVENTORY, l, eq_n[l] <= 0 );
  8448. if( l < MAX_INVENTORY )
  8449. eq_n[l] = i;
  8450. eq_num++;
  8451. }
  8452. }
  8453. if(eq_num > 0){
  8454. int n = eq_n[rnd()%eq_num];
  8455. if(rnd()%10000 < per) {
  8456. if(sd->inventory.u.items_inventory[n].equip)
  8457. pc_unequipitem(sd,n,3);
  8458. pc_dropitem(sd,n,1);
  8459. }
  8460. }
  8461. }
  8462. else if(id > 0) {
  8463. for(i=0;i<MAX_INVENTORY;i++){
  8464. if(sd->inventory.u.items_inventory[i].nameid == id
  8465. && rnd()%10000 < per
  8466. && ((type&NMDT_INVENTORY && !sd->inventory.u.items_inventory[i].equip)
  8467. || (type&NMDT_EQUIP && sd->inventory.u.items_inventory[i].equip)
  8468. || type&NMDT_ALL) ){
  8469. if(sd->inventory.u.items_inventory[i].equip)
  8470. pc_unequipitem(sd,i,3);
  8471. pc_dropitem(sd,i,1);
  8472. break;
  8473. }
  8474. }
  8475. }
  8476. }
  8477. }
  8478. // pvp
  8479. // disable certain pvp functions on pk_mode [Valaris]
  8480. if( !battle_config.pk_mode && mapdata->flag[MF_PVP] && !mapdata->flag[MF_PVP_NOCALCRANK] ) {
  8481. sd->pvp_point -= 5;
  8482. sd->pvp_lost++;
  8483. if( src && src->type == BL_PC ) {
  8484. map_session_data *ssd = (map_session_data *)src;
  8485. ssd->pvp_point++;
  8486. ssd->pvp_won++;
  8487. }
  8488. if( sd->pvp_point < 0 ) {
  8489. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer,sd->bl.id,0);
  8490. return 1|8;
  8491. }
  8492. }
  8493. //GvG
  8494. if( mapdata_flag_gvg2(mapdata) ) {
  8495. sd->respawn_tid = add_timer(tick+1000, pc_respawn_timer, sd->bl.id, 0);
  8496. return 1|8;
  8497. }
  8498. else if( sd->bg_id ) {
  8499. std::shared_ptr<s_battleground_data> bg = util::umap_find(bg_team_db, sd->bg_id);
  8500. if (bg) {
  8501. if (bg->cemetery.map > 0) { // Respawn by BG
  8502. sd->respawn_tid = add_timer(tick + 1000, pc_respawn_timer, sd->bl.id, 0);
  8503. return 1|8;
  8504. }
  8505. }
  8506. }
  8507. //Reset "can log out" tick.
  8508. if( battle_config.prevent_logout )
  8509. sd->canlog_tick = gettick() - battle_config.prevent_logout;
  8510. return 1;
  8511. }
  8512. void pc_revive(map_session_data *sd,unsigned int hp, unsigned int sp, unsigned int ap) {
  8513. if(hp) clif_updatestatus(sd,SP_HP);
  8514. if(sp) clif_updatestatus(sd,SP_SP);
  8515. if(ap) clif_updatestatus(sd,SP_AP);
  8516. pc_setstand(sd, true);
  8517. if(battle_config.pc_invincible_time > 0)
  8518. pc_setinvincibletimer(sd, battle_config.pc_invincible_time);
  8519. if( sd->state.gmaster_flag ) {
  8520. guild_guildaura_refresh(sd,GD_LEADERSHIP,guild_checkskill(sd->guild,GD_LEADERSHIP));
  8521. guild_guildaura_refresh(sd,GD_GLORYWOUNDS,guild_checkskill(sd->guild,GD_GLORYWOUNDS));
  8522. guild_guildaura_refresh(sd,GD_SOULCOLD,guild_checkskill(sd->guild,GD_SOULCOLD));
  8523. guild_guildaura_refresh(sd,GD_HAWKEYES,guild_checkskill(sd->guild,GD_HAWKEYES));
  8524. }
  8525. }
  8526. bool pc_revive_item(map_session_data *sd) {
  8527. nullpo_retr(false, sd);
  8528. if (!pc_isdead(sd) || sd->respawn_tid != INVALID_TIMER)
  8529. return false;
  8530. if (sd->sc.getSCE(SC_HELLPOWER)) // Cannot resurrect while under the effect of SC_HELLPOWER.
  8531. return false;
  8532. int16 item_position = itemdb_group.item_exists_pc(sd, IG_TOKEN_OF_SIEGFRIED);
  8533. uint8 hp = 100, sp = 100;
  8534. if (item_position < 0) {
  8535. if (sd->sc.getSCE(SC_LIGHT_OF_REGENE)) {
  8536. hp = sd->sc.getSCE(SC_LIGHT_OF_REGENE)->val2;
  8537. sp = 0;
  8538. }
  8539. else
  8540. return false;
  8541. }
  8542. if (!status_revive(&sd->bl, hp, sp))
  8543. return false;
  8544. if (item_position < 0)
  8545. status_change_end(&sd->bl, SC_LIGHT_OF_REGENE);
  8546. else
  8547. pc_delitem(sd, item_position, 1, 0, 1, LOG_TYPE_CONSUME);
  8548. clif_skill_nodamage(&sd->bl, &sd->bl, ALL_RESURRECTION, 4, 1);
  8549. return true;
  8550. }
  8551. // script
  8552. //
  8553. /*==========================================
  8554. * script reading pc status registry
  8555. *------------------------------------------*/
  8556. int64 pc_readparam(map_session_data* sd,int64 type)
  8557. {
  8558. int64 val = 0;
  8559. nullpo_ret(sd);
  8560. switch(type) {
  8561. case SP_SKILLPOINT: val = sd->status.skill_point; break;
  8562. case SP_STATUSPOINT: val = sd->status.status_point; break;
  8563. case SP_TRAITPOINT: val = sd->status.trait_point; break;
  8564. case SP_ZENY: val = sd->status.zeny; break;
  8565. case SP_BASELEVEL: val = sd->status.base_level; break;
  8566. case SP_JOBLEVEL: val = sd->status.job_level; break;
  8567. case SP_CLASS: val = sd->status.class_; break;
  8568. case SP_BASEJOB: val = pc_mapid2jobid(sd->class_&MAPID_UPPERMASK, sd->status.sex); break; //Base job, extracting upper type.
  8569. case SP_UPPER: val = sd->class_&JOBL_UPPER?1:(sd->class_&JOBL_BABY?2:0); break;
  8570. case SP_BASECLASS: val = pc_mapid2jobid(sd->class_&MAPID_BASEMASK, sd->status.sex); break; //Extract base class tree. [Skotlex]
  8571. case SP_SEX: val = sd->status.sex; break;
  8572. case SP_WEIGHT: val = sd->weight; break;
  8573. case SP_MAXWEIGHT: val = sd->max_weight; break;
  8574. case SP_BASEEXP: val = u64min(sd->status.base_exp, MAX_EXP); break;
  8575. case SP_JOBEXP: val = u64min(sd->status.job_exp, MAX_EXP); break;
  8576. case SP_NEXTBASEEXP: val = u64min(pc_nextbaseexp(sd), MAX_EXP); break;
  8577. case SP_NEXTJOBEXP: val = u64min(pc_nextjobexp(sd), MAX_EXP); break;
  8578. case SP_HP: val = sd->battle_status.hp; break;
  8579. case SP_MAXHP: val = sd->battle_status.max_hp; break;
  8580. case SP_SP: val = sd->battle_status.sp; break;
  8581. case SP_MAXSP: val = sd->battle_status.max_sp; break;
  8582. case SP_AP: val = sd->battle_status.ap; break;
  8583. case SP_MAXAP: val = sd->battle_status.max_ap; break;
  8584. case SP_STR: val = sd->status.str; break;
  8585. case SP_AGI: val = sd->status.agi; break;
  8586. case SP_VIT: val = sd->status.vit; break;
  8587. case SP_INT: val = sd->status.int_; break;
  8588. case SP_DEX: val = sd->status.dex; break;
  8589. case SP_LUK: val = sd->status.luk; break;
  8590. case SP_POW: val = sd->status.pow; break;
  8591. case SP_STA: val = sd->status.sta; break;
  8592. case SP_WIS: val = sd->status.wis; break;
  8593. case SP_SPL: val = sd->status.spl; break;
  8594. case SP_CON: val = sd->status.con; break;
  8595. case SP_CRT: val = sd->status.crt; break;
  8596. case SP_KARMA: val = sd->status.karma; break;
  8597. case SP_MANNER: val = sd->status.manner; break;
  8598. case SP_FAME: val = sd->status.fame; break;
  8599. case SP_KILLERRID: val = sd->killerrid; break;
  8600. case SP_KILLEDRID: val = sd->killedrid; break;
  8601. case SP_KILLEDGID: val = sd->killedgid; break;
  8602. case SP_SITTING: val = pc_issit(sd)?1:0; break;
  8603. case SP_CHARMOVE: val = sd->status.character_moves; break;
  8604. case SP_CHARRENAME: val = sd->status.rename; break;
  8605. case SP_CHARFONT: val = sd->status.font; break;
  8606. case SP_BANK_VAULT: val = sd->bank_vault; break;
  8607. case SP_CASHPOINTS: val = sd->cashPoints; break;
  8608. case SP_KAFRAPOINTS: val = sd->kafraPoints; break;
  8609. case SP_ROULETTE_BRONZE: val = sd->roulette_point.bronze; break;
  8610. case SP_ROULETTE_SILVER: val = sd->roulette_point.silver; break;
  8611. case SP_ROULETTE_GOLD: val = sd->roulette_point.gold; break;
  8612. case SP_PCDIECOUNTER: val = sd->die_counter; break;
  8613. case SP_COOKMASTERY: val = sd->cook_mastery; break;
  8614. case SP_ACHIEVEMENT_LEVEL: val = sd->achievement_data.level; break;
  8615. case SP_CRITICAL: val = sd->battle_status.cri/10; break;
  8616. case SP_ASPD: val = (2000-sd->battle_status.amotion)/10; break;
  8617. case SP_BASE_ATK:
  8618. #ifdef RENEWAL
  8619. val = sd->bonus.eatk;
  8620. #else
  8621. val = sd->battle_status.batk;
  8622. #endif
  8623. break;
  8624. case SP_DEF1: val = sd->battle_status.def; break;
  8625. case SP_DEF2: val = sd->battle_status.def2; break;
  8626. case SP_MDEF1: val = sd->battle_status.mdef; break;
  8627. case SP_MDEF2: val = sd->battle_status.mdef2; break;
  8628. case SP_HIT: val = sd->battle_status.hit; break;
  8629. case SP_FLEE1: val = sd->battle_status.flee; break;
  8630. case SP_FLEE2: val = sd->battle_status.flee2; break;
  8631. case SP_PATK: val = sd->battle_status.patk; break;
  8632. case SP_SMATK: val = sd->battle_status.smatk; break;
  8633. case SP_RES: val = sd->battle_status.res; break;
  8634. case SP_MRES: val = sd->battle_status.mres; break;
  8635. case SP_HPLUS: val = sd->battle_status.hplus; break;
  8636. case SP_CRATE: val = sd->battle_status.crate; break;
  8637. case SP_DEFELE: val = sd->battle_status.def_ele; break;
  8638. case SP_MAXHPRATE: val = sd->hprate; break;
  8639. case SP_MAXSPRATE: val = sd->sprate; break;
  8640. case SP_MAXAPRATE: val = sd->aprate; break;
  8641. case SP_SPRATE: val = sd->dsprate; break;
  8642. case SP_SPEED_RATE: val = sd->bonus.speed_rate; break;
  8643. case SP_SPEED_ADDRATE: val = sd->bonus.speed_add_rate; break;
  8644. case SP_ASPD_RATE:
  8645. #ifndef RENEWAL_ASPD
  8646. val = sd->battle_status.aspd_rate;
  8647. #else
  8648. val = sd->battle_status.aspd_rate2;
  8649. #endif
  8650. break;
  8651. case SP_HP_RECOV_RATE: val = sd->hprecov_rate; break;
  8652. case SP_SP_RECOV_RATE: val = sd->sprecov_rate; break;
  8653. case SP_CRITICAL_DEF: val = sd->bonus.critical_def; break;
  8654. case SP_NEAR_ATK_DEF: val = sd->bonus.near_attack_def_rate; break;
  8655. case SP_LONG_ATK_DEF: val = sd->bonus.long_attack_def_rate; break;
  8656. case SP_DOUBLE_RATE: val = sd->bonus.double_rate; break;
  8657. case SP_DOUBLE_ADD_RATE: val = sd->bonus.double_add_rate; break;
  8658. case SP_MATK_RATE: val = sd->matk_rate; break;
  8659. case SP_ATK_RATE: val = sd->bonus.atk_rate; break;
  8660. case SP_MAGIC_ATK_DEF: val = sd->bonus.magic_def_rate; break;
  8661. case SP_MISC_ATK_DEF: val = sd->bonus.misc_def_rate; break;
  8662. case SP_PERFECT_HIT_RATE:val = sd->bonus.perfect_hit; break;
  8663. case SP_PERFECT_HIT_ADD_RATE: val = sd->bonus.perfect_hit_add; break;
  8664. case SP_CRITICAL_RATE: val = sd->critical_rate; break;
  8665. case SP_HIT_RATE: val = sd->hit_rate; break;
  8666. case SP_FLEE_RATE: val = sd->flee_rate; break;
  8667. case SP_FLEE2_RATE: val = sd->flee2_rate; break;
  8668. case SP_DEF_RATE: val = sd->def_rate; break;
  8669. case SP_DEF2_RATE: val = sd->def2_rate; break;
  8670. case SP_MDEF_RATE: val = sd->mdef_rate; break;
  8671. case SP_MDEF2_RATE: val = sd->mdef2_rate; break;
  8672. case SP_PATK_RATE: val = sd->patk_rate; break;
  8673. case SP_SMATK_RATE: val = sd->smatk_rate; break;
  8674. case SP_RES_RATE: val = sd->res_rate; break;
  8675. case SP_MRES_RATE: val = sd->mres_rate; break;
  8676. case SP_HPLUS_RATE: val = sd->hplus_rate; break;
  8677. case SP_CRATE_RATE: val = sd->crate_rate; break;
  8678. case SP_RESTART_FULL_RECOVER: val = sd->special_state.restart_full_recover?1:0; break;
  8679. case SP_NO_CASTCANCEL: val = sd->special_state.no_castcancel?1:0; break;
  8680. case SP_NO_CASTCANCEL2: val = sd->special_state.no_castcancel2?1:0; break;
  8681. case SP_NO_SIZEFIX: val = sd->special_state.no_sizefix?1:0; break;
  8682. case SP_NO_MAGIC_DAMAGE: val = sd->special_state.no_magic_damage; break;
  8683. case SP_NO_WEAPON_DAMAGE:val = sd->special_state.no_weapon_damage; break;
  8684. case SP_NO_MISC_DAMAGE: val = sd->special_state.no_misc_damage; break;
  8685. case SP_NO_GEMSTONE: val = sd->special_state.no_gemstone?1:0; break;
  8686. case SP_INTRAVISION: val = sd->special_state.intravision?1:0; break;
  8687. case SP_NO_KNOCKBACK: val = sd->special_state.no_knockback?1:0; break;
  8688. case SP_NO_MADO_FUEL: val = sd->special_state.no_mado_fuel?1:0; break;
  8689. case SP_NO_WALK_DELAY: val = sd->special_state.no_walk_delay?1:0; break;
  8690. case SP_SPLASH_RANGE: val = sd->bonus.splash_range; break;
  8691. case SP_SPLASH_ADD_RANGE:val = sd->bonus.splash_add_range; break;
  8692. case SP_SHORT_WEAPON_DAMAGE_RETURN: val = sd->bonus.short_weapon_damage_return; break;
  8693. case SP_LONG_WEAPON_DAMAGE_RETURN: val = sd->bonus.long_weapon_damage_return; break;
  8694. case SP_MAGIC_DAMAGE_RETURN: val = sd->bonus.magic_damage_return; break;
  8695. case SP_REDUCE_DAMAGE_RETURN: val = sd->bonus.reduce_damage_return; break;
  8696. case SP_PERFECT_HIDE: val = sd->special_state.perfect_hiding?1:0; break;
  8697. case SP_UNBREAKABLE: val = sd->bonus.unbreakable; break;
  8698. case SP_UNBREAKABLE_WEAPON: val = (sd->bonus.unbreakable_equip&EQP_WEAPON)?1:0; break;
  8699. case SP_UNBREAKABLE_ARMOR: val = (sd->bonus.unbreakable_equip&EQP_ARMOR)?1:0; break;
  8700. case SP_UNBREAKABLE_HELM: val = (sd->bonus.unbreakable_equip&EQP_HELM)?1:0; break;
  8701. case SP_UNBREAKABLE_SHIELD: val = (sd->bonus.unbreakable_equip&EQP_SHIELD)?1:0; break;
  8702. case SP_UNBREAKABLE_GARMENT: val = (sd->bonus.unbreakable_equip&EQP_GARMENT)?1:0; break;
  8703. case SP_UNBREAKABLE_SHOES: val = (sd->bonus.unbreakable_equip&EQP_SHOES)?1:0; break;
  8704. case SP_CLASSCHANGE: val = sd->bonus.classchange; break;
  8705. case SP_SHORT_ATK_RATE: val = sd->bonus.short_attack_atk_rate; break;
  8706. case SP_LONG_ATK_RATE: val = sd->bonus.long_attack_atk_rate; break;
  8707. case SP_BREAK_WEAPON_RATE: val = sd->bonus.break_weapon_rate; break;
  8708. case SP_BREAK_ARMOR_RATE: val = sd->bonus.break_armor_rate; break;
  8709. case SP_ADD_STEAL_RATE: val = sd->bonus.add_steal_rate; break;
  8710. case SP_DELAYRATE: val = sd->bonus.delayrate; break;
  8711. case SP_CRIT_ATK_RATE: val = sd->bonus.crit_atk_rate; break;
  8712. case SP_UNSTRIPABLE_WEAPON: val = (sd->bonus.unstripable_equip&EQP_WEAPON)?1:0; break;
  8713. case SP_UNSTRIPABLE:
  8714. case SP_UNSTRIPABLE_ARMOR:
  8715. val = (sd->bonus.unstripable_equip&EQP_ARMOR)?1:0;
  8716. break;
  8717. case SP_UNSTRIPABLE_HELM: val = (sd->bonus.unstripable_equip&EQP_HELM)?1:0; break;
  8718. case SP_UNSTRIPABLE_SHIELD: val = (sd->bonus.unstripable_equip&EQP_SHIELD)?1:0; break;
  8719. case SP_SP_GAIN_VALUE: val = sd->bonus.sp_gain_value; break;
  8720. case SP_HP_GAIN_VALUE: val = sd->bonus.hp_gain_value; break;
  8721. case SP_LONG_SP_GAIN_VALUE: val = sd->bonus.long_sp_gain_value; break;
  8722. case SP_LONG_HP_GAIN_VALUE: val = sd->bonus.long_hp_gain_value; break;
  8723. case SP_MAGIC_SP_GAIN_VALUE: val = sd->bonus.magic_sp_gain_value; break;
  8724. case SP_MAGIC_HP_GAIN_VALUE: val = sd->bonus.magic_hp_gain_value; break;
  8725. case SP_ADD_HEAL_RATE: val = sd->bonus.add_heal_rate; break;
  8726. case SP_ADD_HEAL2_RATE: val = sd->bonus.add_heal2_rate; break;
  8727. case SP_ADD_ITEM_HEAL_RATE: val = sd->bonus.itemhealrate2; break;
  8728. case SP_EMATK: val = sd->bonus.ematk; break;
  8729. case SP_FIXCASTRATE: val = sd->bonus.fixcastrate; break;
  8730. case SP_ADD_FIXEDCAST: val = sd->bonus.add_fixcast; break;
  8731. case SP_ADD_VARIABLECAST: val = sd->bonus.add_varcast; break;
  8732. case SP_CASTRATE:
  8733. case SP_VARCASTRATE:
  8734. #ifdef RENEWAL_CAST
  8735. val = sd->bonus.varcastrate; break;
  8736. #else
  8737. val = sd->castrate; break;
  8738. #endif
  8739. case SP_CRIT_DEF_RATE: val = sd->bonus.crit_def_rate; break;
  8740. case SP_ADD_ITEM_SPHEAL_RATE: val = sd->bonus.itemsphealrate2; break;
  8741. default:
  8742. ShowError("pc_readparam: Attempt to read unknown parameter '%lld'.\n", type);
  8743. return -1;
  8744. }
  8745. return val;
  8746. }
  8747. /*==========================================
  8748. * script set pc status registry
  8749. *------------------------------------------*/
  8750. bool pc_setparam(map_session_data *sd,int64 type,int64 val_tmp)
  8751. {
  8752. nullpo_retr(false,sd);
  8753. int val = static_cast<unsigned int>(val_tmp);
  8754. switch(type){
  8755. case SP_BASELEVEL:
  8756. if (val > pc_maxbaselv(sd)) //Capping to max
  8757. val = pc_maxbaselv(sd);
  8758. if (val > sd->status.base_level) {
  8759. for( int i = 0; i < (int)( val - sd->status.base_level ); i++ ){
  8760. sd->status.status_point += statpoint_db.pc_gets_status_point( sd->status.base_level + i );
  8761. sd->status.trait_point += statpoint_db.pc_gets_trait_point( sd->status.base_level + i );
  8762. }
  8763. }
  8764. sd->status.base_level = val;
  8765. sd->status.base_exp = 0;
  8766. // clif_updatestatus(sd, SP_BASELEVEL); // Gets updated at the bottom
  8767. clif_updatestatus(sd, SP_NEXTBASEEXP);
  8768. clif_updatestatus(sd, SP_STATUSPOINT);
  8769. clif_updatestatus(sd, SP_TRAITPOINT);
  8770. clif_updatestatus(sd, SP_BASEEXP);
  8771. status_calc_pc(sd, SCO_FORCE);
  8772. if(sd->status.party_id)
  8773. party_send_levelup(sd);
  8774. break;
  8775. case SP_JOBLEVEL:
  8776. if (val >= sd->status.job_level) {
  8777. if (val > pc_maxjoblv(sd)) val = pc_maxjoblv(sd);
  8778. sd->status.skill_point += val - sd->status.job_level;
  8779. clif_updatestatus(sd, SP_SKILLPOINT);
  8780. }
  8781. sd->status.job_level = val;
  8782. sd->status.job_exp = 0;
  8783. // clif_updatestatus(sd, SP_JOBLEVEL); // Gets updated at the bottom
  8784. clif_updatestatus(sd, SP_NEXTJOBEXP);
  8785. clif_updatestatus(sd, SP_JOBEXP);
  8786. status_calc_pc(sd, SCO_FORCE);
  8787. break;
  8788. case SP_SKILLPOINT:
  8789. sd->status.skill_point = val;
  8790. break;
  8791. case SP_STATUSPOINT:
  8792. sd->status.status_point = val;
  8793. break;
  8794. case SP_TRAITPOINT:
  8795. sd->status.trait_point = val;
  8796. break;
  8797. case SP_ZENY:
  8798. if( val < 0 )
  8799. return false;// can't set negative zeny
  8800. log_zeny(sd, LOG_TYPE_SCRIPT, sd, -(sd->status.zeny - cap_value(val, 0, MAX_ZENY)));
  8801. sd->status.zeny = cap_value(val, 0, MAX_ZENY);
  8802. break;
  8803. case SP_BASEEXP:
  8804. val_tmp = cap_value(val_tmp, 0, pc_is_maxbaselv(sd) ? MAX_LEVEL_BASE_EXP : MAX_EXP);
  8805. if (val_tmp < sd->status.base_exp) // Lost
  8806. pc_lostexp(sd, sd->status.base_exp - val_tmp, 0);
  8807. else // Gained
  8808. pc_gainexp(sd, NULL, val_tmp - sd->status.base_exp, 0, 2);
  8809. return true;
  8810. case SP_JOBEXP:
  8811. val_tmp = cap_value(val_tmp, 0, pc_is_maxjoblv(sd) ? MAX_LEVEL_JOB_EXP : MAX_EXP);
  8812. if (val_tmp < sd->status.job_exp) // Lost
  8813. pc_lostexp(sd, 0, sd->status.job_exp - val_tmp);
  8814. else // Gained
  8815. pc_gainexp(sd, NULL, 0, val_tmp - sd->status.job_exp, 2);
  8816. return true;
  8817. case SP_SEX:
  8818. sd->status.sex = val ? SEX_MALE : SEX_FEMALE;
  8819. break;
  8820. case SP_WEIGHT:
  8821. sd->weight = val;
  8822. break;
  8823. case SP_MAXWEIGHT:
  8824. sd->max_weight = val;
  8825. break;
  8826. case SP_HP:
  8827. sd->battle_status.hp = cap_value(val, 1, (int)sd->battle_status.max_hp);
  8828. break;
  8829. case SP_MAXHP:
  8830. if (sd->status.base_level < 100)
  8831. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv99);
  8832. else if (sd->status.base_level < 151)
  8833. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp_lv150);
  8834. else
  8835. sd->battle_status.max_hp = cap_value(val, 1, battle_config.max_hp);
  8836. if( sd->battle_status.max_hp < sd->battle_status.hp )
  8837. {
  8838. sd->battle_status.hp = sd->battle_status.max_hp;
  8839. clif_updatestatus(sd, SP_HP);
  8840. }
  8841. break;
  8842. case SP_SP:
  8843. sd->battle_status.sp = cap_value(val, 0, (int)sd->battle_status.max_sp);
  8844. break;
  8845. case SP_MAXSP:
  8846. sd->battle_status.max_sp = cap_value(val, 1, battle_config.max_sp);
  8847. if( sd->battle_status.max_sp < sd->battle_status.sp )
  8848. {
  8849. sd->battle_status.sp = sd->battle_status.max_sp;
  8850. clif_updatestatus(sd, SP_SP);
  8851. }
  8852. break;
  8853. case SP_AP:
  8854. sd->battle_status.ap = cap_value(val, 0, (int)sd->battle_status.max_ap);
  8855. break;
  8856. case SP_MAXAP:
  8857. sd->battle_status.max_ap = cap_value(val, 1, battle_config.max_ap);
  8858. if (sd->battle_status.max_ap < sd->battle_status.ap) {
  8859. sd->battle_status.ap = sd->battle_status.max_ap;
  8860. clif_updatestatus(sd, SP_AP);
  8861. }
  8862. break;
  8863. case SP_STR:
  8864. sd->status.str = cap_value(val, 1, pc_maxparameter(sd,PARAM_STR));
  8865. break;
  8866. case SP_AGI:
  8867. sd->status.agi = cap_value(val, 1, pc_maxparameter(sd,PARAM_AGI));
  8868. break;
  8869. case SP_VIT:
  8870. sd->status.vit = cap_value(val, 1, pc_maxparameter(sd,PARAM_VIT));
  8871. break;
  8872. case SP_INT:
  8873. sd->status.int_ = cap_value(val, 1, pc_maxparameter(sd,PARAM_INT));
  8874. break;
  8875. case SP_DEX:
  8876. sd->status.dex = cap_value(val, 1, pc_maxparameter(sd,PARAM_DEX));
  8877. break;
  8878. case SP_LUK:
  8879. sd->status.luk = cap_value(val, 1, pc_maxparameter(sd,PARAM_LUK));
  8880. break;
  8881. case SP_POW:
  8882. sd->status.pow = cap_value(val, 0, pc_maxparameter(sd,PARAM_POW));
  8883. break;
  8884. case SP_STA:
  8885. sd->status.sta = cap_value(val, 0, pc_maxparameter(sd,PARAM_STA));
  8886. break;
  8887. case SP_WIS:
  8888. sd->status.wis = cap_value(val, 0, pc_maxparameter(sd,PARAM_WIS));
  8889. break;
  8890. case SP_SPL:
  8891. sd->status.spl = cap_value(val, 0, pc_maxparameter(sd,PARAM_SPL));
  8892. break;
  8893. case SP_CON:
  8894. sd->status.con = cap_value(val, 0, pc_maxparameter(sd,PARAM_CON));
  8895. break;
  8896. case SP_CRT:
  8897. sd->status.crt = cap_value(val, 0, pc_maxparameter(sd,PARAM_CRT));
  8898. break;
  8899. case SP_KARMA:
  8900. sd->status.karma = val;
  8901. break;
  8902. case SP_MANNER:
  8903. sd->status.manner = val;
  8904. if( val < 0 )
  8905. sc_start(NULL, &sd->bl, SC_NOCHAT, 100, 0, 0);
  8906. else {
  8907. status_change_end(&sd->bl, SC_NOCHAT);
  8908. clif_manner_message(sd, 5);
  8909. }
  8910. return true; // status_change_start/status_change_end already sends packets warning the client
  8911. case SP_FAME:
  8912. sd->status.fame = val;
  8913. break;
  8914. case SP_KILLERRID:
  8915. sd->killerrid = val;
  8916. return true;
  8917. case SP_KILLEDRID:
  8918. sd->killedrid = val;
  8919. return true;
  8920. case SP_KILLEDGID:
  8921. sd->killedgid = val;
  8922. return true;
  8923. case SP_CHARMOVE:
  8924. sd->status.character_moves = val;
  8925. return true;
  8926. case SP_CHARRENAME:
  8927. sd->status.rename = val;
  8928. return true;
  8929. case SP_CHARFONT:
  8930. sd->status.font = val;
  8931. clif_font(sd);
  8932. return true;
  8933. case SP_BANK_VAULT:
  8934. if (val < 0)
  8935. return false;
  8936. log_zeny(sd, LOG_TYPE_BANK, sd, -(sd->bank_vault - cap_value(val, 0, MAX_BANK_ZENY)));
  8937. sd->bank_vault = cap_value(val, 0, MAX_BANK_ZENY);
  8938. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  8939. return true;
  8940. case SP_ROULETTE_BRONZE:
  8941. sd->roulette_point.bronze = val;
  8942. pc_setreg2(sd, ROULETTE_BRONZE_VAR, sd->roulette_point.bronze);
  8943. return true;
  8944. case SP_ROULETTE_SILVER:
  8945. sd->roulette_point.silver = val;
  8946. pc_setreg2(sd, ROULETTE_SILVER_VAR, sd->roulette_point.silver);
  8947. return true;
  8948. case SP_ROULETTE_GOLD:
  8949. sd->roulette_point.gold = val;
  8950. pc_setreg2(sd, ROULETTE_GOLD_VAR, sd->roulette_point.gold);
  8951. return true;
  8952. case SP_CASHPOINTS:
  8953. if (val < 0)
  8954. return false;
  8955. if (!sd->state.connect_new)
  8956. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_CASH, -(sd->cashPoints - cap_value(val, 0, MAX_CASHPOINT)));
  8957. sd->cashPoints = cap_value(val, 0, MAX_CASHPOINT);
  8958. pc_setaccountreg(sd, add_str(CASHPOINT_VAR), sd->cashPoints);
  8959. return true;
  8960. case SP_KAFRAPOINTS:
  8961. if (val < 0)
  8962. return false;
  8963. if (!sd->state.connect_new)
  8964. log_cash(sd, LOG_TYPE_SCRIPT, LOG_CASH_TYPE_KAFRA, -(sd->kafraPoints - cap_value(val, 0, MAX_KAFRAPOINT)));
  8965. sd->kafraPoints = cap_value(val, 0, MAX_KAFRAPOINT);
  8966. pc_setaccountreg(sd, add_str(KAFRAPOINT_VAR), sd->kafraPoints);
  8967. return true;
  8968. case SP_PCDIECOUNTER:
  8969. if (val < 0)
  8970. return false;
  8971. if (sd->die_counter == val)
  8972. return true;
  8973. sd->die_counter = val;
  8974. if (!sd->state.connect_new && sd->die_counter == 1 && (sd->class_&MAPID_UPPERMASK) == MAPID_SUPER_NOVICE)
  8975. status_calc_pc(sd, SCO_NONE); // Lost the bonus.
  8976. pc_setglobalreg(sd, add_str(PCDIECOUNTER_VAR), sd->die_counter);
  8977. return true;
  8978. case SP_COOKMASTERY:
  8979. if (sd->cook_mastery == val)
  8980. return true;
  8981. val = cap_value(val, 0, 1999);
  8982. sd->cook_mastery = val;
  8983. pc_setglobalreg(sd, add_str(COOKMASTERY_VAR), sd->cook_mastery);
  8984. return true;
  8985. default:
  8986. ShowError("pc_setparam: Attempted to set unknown parameter '%lld'.\n", type);
  8987. return false;
  8988. }
  8989. clif_updatestatus(sd,static_cast<int>(type));
  8990. return true;
  8991. }
  8992. /*==========================================
  8993. * HP/SP/AP Healing. If flag is passed, the heal type is through clif_heal, otherwise update status.
  8994. *------------------------------------------*/
  8995. void pc_heal(map_session_data *sd,unsigned int hp,unsigned int sp, unsigned int ap, int type)
  8996. {// Is there going to be a effect for gaining AP soon??? [Rytech]
  8997. nullpo_retv(sd);
  8998. if (type&2) {
  8999. if (hp || type&4) {
  9000. clif_heal(sd->fd,SP_HP,hp);
  9001. clif_update_hp(*sd);
  9002. }
  9003. if (sp)
  9004. clif_heal(sd->fd,SP_SP,sp);
  9005. if (ap)
  9006. clif_heal(sd->fd,SP_AP,ap);
  9007. } else {
  9008. if(hp)
  9009. clif_updatestatus(sd,SP_HP);
  9010. if(sp)
  9011. clif_updatestatus(sd,SP_SP);
  9012. if (ap)
  9013. clif_updatestatus(sd,SP_AP);
  9014. }
  9015. return;
  9016. }
  9017. /**
  9018. * Heal player HP and/or SP linearly. Calculate any bonus based on active statuses.
  9019. * @param sd: Player data
  9020. * @param itemid: Item ID
  9021. * @param hp: HP to heal
  9022. * @param sp: SP to heal
  9023. * @return Amount healed to an object
  9024. */
  9025. int pc_itemheal(map_session_data *sd, t_itemid itemid, int hp, int sp)
  9026. {
  9027. int bonus, tmp, penalty = 0;
  9028. if (hp) {
  9029. bonus = 100 + (sd->battle_status.vit * 2) + pc_checkskill(sd, SM_RECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9030. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9031. if (potion_flag == 2) {
  9032. bonus += bonus * 50 / 100;
  9033. if (sd->sc.getSCE(SC_SPIRIT) && sd->sc.getSCE(SC_SPIRIT)->val2 == SL_ROGUE)
  9034. bonus += bonus; // Receive an additional +100% effect from ranked potions to HP only
  9035. }
  9036. //All item bonuses.
  9037. bonus += sd->bonus.itemhealrate2;
  9038. //Item Group bonuses
  9039. bonus += bonus * pc_get_itemgroup_bonus(sd, itemid, sd->itemgrouphealrate) / 100;
  9040. //Individual item bonuses.
  9041. for(const auto &it : sd->itemhealrate) {
  9042. if (it.id == itemid) {
  9043. bonus += bonus * it.val / 100;
  9044. break;
  9045. }
  9046. }
  9047. // Recovery Potion
  9048. if (sd->sc.getSCE(SC_INCHEALRATE))
  9049. bonus += bonus * sd->sc.getSCE(SC_INCHEALRATE)->val1 / 100;
  9050. // 2014 Halloween Event : Pumpkin Bonus
  9051. if (sd->sc.getSCE(SC_MTF_PUMPKIN)) {
  9052. if (itemid == ITEMID_PUMPKIN)
  9053. bonus += bonus * sd->sc.getSCE(SC_MTF_PUMPKIN)->val1 / 100;
  9054. else if (itemid == ITEMID_COOKIE_BAT)
  9055. bonus += sd->sc.getSCE(SC_MTF_PUMPKIN)->val2;
  9056. }
  9057. if (sd->sc.getSCE(SC_VITALIZE_POTION))
  9058. bonus += bonus * 10 / 100;
  9059. tmp = hp * bonus / 100; // Overflow check
  9060. if (bonus != 100 && tmp > hp)
  9061. hp = tmp;
  9062. }
  9063. if (sp) {
  9064. bonus = 100 + (sd->battle_status.int_ * 2) + pc_checkskill(sd, MG_SRECOVERY) * 10 + pc_checkskill(sd, AM_LEARNINGPOTION) * 5;
  9065. // A potion produced by an Alchemist in the Fame Top 10 gets +50% effect [DracoRPG]
  9066. if (potion_flag == 2)
  9067. bonus += bonus * 50 / 100;
  9068. // All item bonuses.
  9069. bonus += sd->bonus.itemsphealrate2;
  9070. // Item Group bonuses
  9071. bonus += bonus * pc_get_itemgroup_bonus( sd, itemid, sd->itemgroupsphealrate ) / 100;
  9072. // Individual item bonuses.
  9073. for( const auto &it : sd->itemsphealrate ){
  9074. if( it.id == itemid ){
  9075. bonus += bonus * it.val / 100;
  9076. break;
  9077. }
  9078. }
  9079. tmp = sp * bonus / 100; // Overflow check
  9080. if (bonus != 100 && tmp > sp)
  9081. sp = tmp;
  9082. }
  9083. if (sd->sc.count) {
  9084. // Critical Wound and Death Hurt stack
  9085. if (sd->sc.getSCE(SC_CRITICALWOUND))
  9086. penalty += sd->sc.getSCE(SC_CRITICALWOUND)->val2;
  9087. if (sd->sc.getSCE(SC_DEATHHURT) && sd->sc.getSCE(SC_DEATHHURT)->val3 == 1)
  9088. penalty += 20;
  9089. if (sd->sc.getSCE(SC_NORECOVER_STATE))
  9090. penalty = 100;
  9091. if (sd->sc.getSCE(SC_VITALITYACTIVATION))
  9092. hp += hp / 2; // 1.5 times
  9093. if (sd->sc.getSCE(SC_WATER_INSIGNIA) && sd->sc.getSCE(SC_WATER_INSIGNIA)->val1 == 2) {
  9094. hp += hp / 10;
  9095. sp += sp / 10;
  9096. }
  9097. #ifdef RENEWAL
  9098. if (sd->sc.getSCE(SC_APPLEIDUN))
  9099. hp += sd->sc.getSCE(SC_APPLEIDUN)->val3 / 100;
  9100. #endif
  9101. if (penalty > 0) {
  9102. hp -= hp * penalty / 100;
  9103. sp -= sp * penalty / 100;
  9104. }
  9105. #ifdef RENEWAL
  9106. if (sd->sc.getSCE(SC_EXTREMITYFIST2))
  9107. sp = 0;
  9108. #endif
  9109. if (sd->sc.getSCE(SC_BITESCAR))
  9110. hp = 0;
  9111. }
  9112. return status_heal(&sd->bl, hp, sp, 1);
  9113. }
  9114. /*==========================================
  9115. * HP/SP Recovery
  9116. * Heal player hp nad/or sp by rate
  9117. *------------------------------------------*/
  9118. int pc_percentheal(map_session_data *sd,int hp,int sp)
  9119. {
  9120. nullpo_ret(sd);
  9121. if (hp > 100) hp = 100;
  9122. else if (hp <-100) hp = -100;
  9123. if (sp > 100) sp = 100;
  9124. else if (sp <-100) sp = -100;
  9125. if(hp >= 0 && sp >= 0) //Heal
  9126. return status_percent_heal(&sd->bl, hp, sp);
  9127. if(hp <= 0 && sp <= 0) //Damage (negative rates indicate % of max rather than current), and only kill target IF the specified amount is 100%
  9128. return status_percent_damage(NULL, &sd->bl, hp, sp, hp==-100);
  9129. //Crossed signs
  9130. if(hp) {
  9131. if(hp > 0)
  9132. status_percent_heal(&sd->bl, hp, 0);
  9133. else
  9134. status_percent_damage(NULL, &sd->bl, hp, 0, hp==-100);
  9135. }
  9136. if(sp) {
  9137. if(sp > 0)
  9138. status_percent_heal(&sd->bl, 0, sp);
  9139. else
  9140. status_percent_damage(NULL, &sd->bl, 0, sp, false);
  9141. }
  9142. return 0;
  9143. }
  9144. static int jobchange_killclone(struct block_list *bl, va_list ap)
  9145. {
  9146. struct mob_data *md;
  9147. int flag;
  9148. md = (struct mob_data *)bl;
  9149. nullpo_ret(md);
  9150. flag = va_arg(ap, int);
  9151. if (md->master_id && md->special_state.clone && md->master_id == flag)
  9152. status_kill(&md->bl);
  9153. return 1;
  9154. }
  9155. /**
  9156. * Called when player changes job
  9157. * Rewrote to make it tidider [Celest]
  9158. * @param sd
  9159. * @param job JOB ID. See enum e_job
  9160. * @param upper 1 - JOBL_UPPER; 2 - JOBL_BABY
  9161. * @return True if success, false if failed
  9162. **/
  9163. bool pc_jobchange(map_session_data *sd,int job, char upper)
  9164. {
  9165. int i, fame_flag = 0;
  9166. nullpo_retr(false,sd);
  9167. if (job < 0)
  9168. return false;
  9169. //Normalize job.
  9170. uint64 b_class = pc_jobid2mapid(job);
  9171. if (b_class == -1)
  9172. return false;
  9173. switch (upper) {
  9174. case 1:
  9175. b_class|= JOBL_UPPER;
  9176. break;
  9177. case 2:
  9178. b_class|= JOBL_BABY;
  9179. break;
  9180. }
  9181. //This will automatically adjust bard/dancer classes to the correct gender
  9182. //That is, if you try to jobchange into dancer, it will turn you to bard.
  9183. job = pc_mapid2jobid(b_class, sd->status.sex);
  9184. if (job == -1)
  9185. return false;
  9186. if ((unsigned short)b_class == sd->class_)
  9187. return false; //Nothing to change.
  9188. // If the job does not exist in the job db, dont allow changing to it
  9189. if( !job_db.exists( job ) ){
  9190. return false;
  9191. }
  9192. if( ( b_class&JOBL_FOURTH ) && !( sd->class_&JOBL_FOURTH ) ){
  9193. // Changing to 4th job
  9194. sd->change_level_4th = sd->status.job_level;
  9195. pc_setglobalreg( sd, add_str( JOBCHANGE4TH_VAR ) , sd->change_level_4th );
  9196. }else if( ( b_class&JOBL_THIRD ) && !( sd->class_&JOBL_THIRD ) ){
  9197. // changing from 2nd to 3rd job
  9198. sd->change_level_3rd = sd->status.job_level;
  9199. pc_setglobalreg( sd, add_str( JOBCHANGE3RD_VAR ), sd->change_level_3rd );
  9200. }else if( ( b_class&JOBL_2 ) && !( sd->class_&JOBL_2 ) ){
  9201. // changing from 1st to 2nd job
  9202. sd->change_level_2nd = sd->status.job_level;
  9203. pc_setglobalreg( sd, add_str( JOBCHANGE2ND_VAR ), sd->change_level_2nd );
  9204. }
  9205. if(sd->cloneskill_idx > 0) {
  9206. pc_skill_plagiarism_reset(*sd, 1);
  9207. }
  9208. if(sd->reproduceskill_idx > 0) {
  9209. pc_skill_plagiarism_reset(*sd, 2);
  9210. }
  9211. if ( (b_class&MAPID_UPPERMASK) != (sd->class_&MAPID_UPPERMASK) ) { //Things to remove when changing class tree.
  9212. status_db.changeSkillTree(sd);
  9213. }
  9214. if( (sd->class_&MAPID_UPPERMASK) == MAPID_STAR_GLADIATOR && (b_class&MAPID_UPPERMASK) != MAPID_STAR_GLADIATOR) {
  9215. /* going off star glad lineage, reset feel and hate to not store no-longer-used vars in the database */
  9216. pc_resetfeel(sd);
  9217. pc_resethate(sd);
  9218. }
  9219. // Reset body style to 0 before changing job to avoid
  9220. // errors since not every job has a alternate outfit.
  9221. sd->status.body = 0;
  9222. clif_changelook(&sd->bl,LOOK_BODY2,0);
  9223. sd->status.class_ = job;
  9224. fame_flag = pc_famerank(sd->status.char_id,sd->class_&MAPID_UPPERMASK);
  9225. uint64 previous_class = sd->class_;
  9226. sd->class_ = (unsigned short)b_class;
  9227. sd->status.job_level=1;
  9228. sd->status.job_exp=0;
  9229. if (sd->status.base_level > pc_maxbaselv(sd)) {
  9230. sd->status.base_level = pc_maxbaselv(sd);
  9231. sd->status.base_exp=0;
  9232. pc_resetstate(sd);
  9233. clif_updatestatus(sd,SP_STATUSPOINT);
  9234. clif_updatestatus(sd,SP_TRAITPOINT);
  9235. clif_updatestatus(sd,SP_BASELEVEL);
  9236. clif_updatestatus(sd,SP_BASEEXP);
  9237. clif_updatestatus(sd,SP_NEXTBASEEXP);
  9238. }
  9239. // Give or reduce transcendent status points
  9240. if( (b_class&JOBL_UPPER) && !(previous_class&JOBL_UPPER) ){ // Change from a non t class to a t class -> give points
  9241. sd->status.status_point += battle_config.transcendent_status_points;
  9242. clif_updatestatus(sd,SP_STATUSPOINT);
  9243. }else if( !(b_class&JOBL_UPPER) && (previous_class&JOBL_UPPER) ){ // Change from a t class to a non t class -> remove points
  9244. if( sd->status.status_point < battle_config.transcendent_status_points ){
  9245. // The player already used his bonus points, so we have to reset his status points
  9246. pc_resetstate(sd);
  9247. }else{
  9248. sd->status.status_point -= battle_config.transcendent_status_points;
  9249. clif_updatestatus(sd,SP_STATUSPOINT);
  9250. }
  9251. }
  9252. // Give or reduce trait status points
  9253. if ((b_class & JOBL_FOURTH) && !(previous_class & JOBL_FOURTH)) {// Change to a 4th job.
  9254. sd->status.trait_point += battle_config.trait_points_job_change;
  9255. clif_updatestatus(sd, SP_TRAITPOINT);
  9256. clif_updatestatus(sd, SP_UPOW);
  9257. clif_updatestatus(sd, SP_USTA);
  9258. clif_updatestatus(sd, SP_UWIS);
  9259. clif_updatestatus(sd, SP_USPL);
  9260. clif_updatestatus(sd, SP_UCON);
  9261. clif_updatestatus(sd, SP_UCRT);
  9262. } else if (!(b_class & JOBL_FOURTH) && (previous_class & JOBL_FOURTH)) {// Change to a non 4th job.
  9263. if (sd->status.trait_point < battle_config.trait_points_job_change) {
  9264. // Player may have already used the trait status points. Force a reset.
  9265. pc_resetstate(sd);
  9266. } else {
  9267. sd->status.trait_point = 0;
  9268. clif_updatestatus(sd, SP_TRAITPOINT);
  9269. clif_updatestatus(sd, SP_UPOW);
  9270. clif_updatestatus(sd, SP_USTA);
  9271. clif_updatestatus(sd, SP_UWIS);
  9272. clif_updatestatus(sd, SP_USPL);
  9273. clif_updatestatus(sd, SP_UCON);
  9274. clif_updatestatus(sd, SP_UCRT);
  9275. }
  9276. }
  9277. clif_updatestatus(sd,SP_JOBLEVEL);
  9278. clif_updatestatus(sd,SP_JOBEXP);
  9279. clif_updatestatus(sd,SP_NEXTJOBEXP);
  9280. for(i=0;i<EQI_MAX;i++) {
  9281. if(sd->equip_index[i] >= 0)
  9282. if(pc_isequip(sd,sd->equip_index[i]))
  9283. pc_unequipitem(sd,sd->equip_index[i],2); // unequip invalid item for class
  9284. }
  9285. //Change look, if disguised, you need to undisguise
  9286. //to correctly calculate new job sprite without
  9287. if (sd->disguise)
  9288. pc_disguise(sd, 0);
  9289. status_set_viewdata(&sd->bl, job);
  9290. clif_changelook(&sd->bl,LOOK_BASE,sd->vd.class_); // move sprite update to prevent client crashes with incompatible equipment [Valaris]
  9291. #if PACKETVER >= 20151001
  9292. clif_changelook(&sd->bl, LOOK_HAIR, sd->vd.hair_style); // Update player's head (only matters when switching to or from Doram)
  9293. #endif
  9294. if(sd->vd.cloth_color)
  9295. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9296. /*
  9297. if(sd->vd.body_style)
  9298. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9299. */
  9300. //Update skill tree.
  9301. pc_calc_skilltree(sd);
  9302. clif_skillinfoblock(sd);
  9303. if (sd->ed)
  9304. elemental_delete(sd->ed);
  9305. if (sd->state.vending)
  9306. vending_closevending(sd);
  9307. if (sd->state.buyingstore)
  9308. buyingstore_close(sd);
  9309. map_foreachinmap(jobchange_killclone, sd->bl.m, BL_MOB, sd->bl.id);
  9310. //Remove peco/cart/falcon
  9311. i = sd->sc.option;
  9312. if( i&OPTION_RIDING && !pc_checkskill(sd, KN_RIDING) )
  9313. i&=~OPTION_RIDING;
  9314. if( i&OPTION_FALCON && !pc_checkskill(sd, HT_FALCON) )
  9315. i&=~OPTION_FALCON;
  9316. if( i&OPTION_DRAGON && !pc_checkskill(sd,RK_DRAGONTRAINING) )
  9317. i&=~OPTION_DRAGON;
  9318. if( i&OPTION_WUGRIDER && !pc_checkskill(sd,RA_WUGMASTERY) )
  9319. i&=~OPTION_WUGRIDER;
  9320. if( i&OPTION_WUG && !pc_checkskill(sd,RA_WUGMASTERY) )
  9321. i&=~OPTION_WUG;
  9322. if( i&OPTION_MADOGEAR ) //You do not need a skill for this.
  9323. i&=~OPTION_MADOGEAR;
  9324. #ifndef NEW_CARTS
  9325. if( i&OPTION_CART && !pc_checkskill(sd, MC_PUSHCART) )
  9326. i&=~OPTION_CART;
  9327. #else
  9328. if( sd->sc.getSCE(SC_PUSH_CART) && !pc_checkskill(sd, MC_PUSHCART) )
  9329. pc_setcart(sd, 0);
  9330. #endif
  9331. if(i != sd->sc.option)
  9332. pc_setoption(sd, i);
  9333. if(hom_is_active(sd->hd) && !pc_checkskill(sd, AM_CALLHOMUN))
  9334. hom_vaporize(sd, HOM_ST_ACTIVE);
  9335. if (sd->sc.getSCE(SC_SPRITEMABLE) && !pc_checkskill(sd, SU_SPRITEMABLE))
  9336. status_change_end(&sd->bl, SC_SPRITEMABLE);
  9337. if (sd->sc.getSCE(SC_SOULATTACK) && !pc_checkskill(sd, SU_SOULATTACK))
  9338. status_change_end(&sd->bl, SC_SOULATTACK);
  9339. if( sd->sc.getSCE(SC_SPIRIT) ){
  9340. status_change_end( &sd->bl, SC_SPIRIT );
  9341. }
  9342. if(sd->status.manner < 0)
  9343. clif_changestatus(sd,SP_MANNER,sd->status.manner);
  9344. status_calc_pc(sd,SCO_FORCE);
  9345. pc_checkallowskill(sd);
  9346. pc_equiplookall(sd);
  9347. pc_show_questinfo(sd);
  9348. achievement_update_objective(sd, AG_JOB_CHANGE, 2, sd->status.base_level, job);
  9349. if( sd->status.party_id ){
  9350. struct party_data* p;
  9351. if( ( p = party_search( sd->status.party_id ) ) != NULL ){
  9352. ARR_FIND(0, MAX_PARTY, i, p->party.member[i].char_id == sd->status.char_id);
  9353. if( i < MAX_PARTY ){
  9354. p->party.member[i].class_ = sd->status.class_;
  9355. clif_party_job_and_level( *sd );
  9356. }
  9357. }
  9358. }
  9359. chrif_save(sd, CSAVE_NORMAL);
  9360. //if you were previously famous, not anymore.
  9361. if (fame_flag)
  9362. chrif_buildfamelist();
  9363. else if (sd->status.fame > 0) {
  9364. //It may be that now they are famous?
  9365. switch (sd->class_&MAPID_UPPERMASK) {
  9366. case MAPID_BLACKSMITH:
  9367. case MAPID_ALCHEMIST:
  9368. case MAPID_TAEKWON:
  9369. chrif_buildfamelist();
  9370. break;
  9371. }
  9372. }
  9373. return true;
  9374. }
  9375. /*==========================================
  9376. * Tell client player sd has change equipement
  9377. *------------------------------------------*/
  9378. void pc_equiplookall(map_session_data *sd)
  9379. {
  9380. nullpo_retv(sd);
  9381. clif_changelook(&sd->bl,LOOK_WEAPON,0);
  9382. clif_changelook(&sd->bl,LOOK_SHOES,0);
  9383. clif_changelook(&sd->bl,LOOK_HEAD_BOTTOM,sd->status.head_bottom);
  9384. clif_changelook(&sd->bl,LOOK_HEAD_TOP,sd->status.head_top);
  9385. clif_changelook(&sd->bl,LOOK_HEAD_MID,sd->status.head_mid);
  9386. clif_changelook(&sd->bl,LOOK_ROBE, sd->status.robe);
  9387. }
  9388. /*==========================================
  9389. * Tell client player sd has change look (hair,equip...)
  9390. *------------------------------------------*/
  9391. void pc_changelook(map_session_data *sd,int type,int val) {
  9392. nullpo_retv(sd);
  9393. switch(type) {
  9394. case LOOK_HAIR: //Use the battle_config limits! [Skotlex]
  9395. val = cap_value(val, MIN_HAIR_STYLE, MAX_HAIR_STYLE);
  9396. if (sd->status.hair != val) {
  9397. sd->status.hair = val;
  9398. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9399. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9400. GMI_HAIR, &sd->status.hair, sizeof(sd->status.hair));
  9401. }
  9402. break;
  9403. case LOOK_WEAPON:
  9404. sd->status.weapon = val;
  9405. break;
  9406. case LOOK_HEAD_BOTTOM:
  9407. sd->status.head_bottom = val;
  9408. sd->setlook_head_bottom = val;
  9409. break;
  9410. case LOOK_HEAD_TOP:
  9411. sd->status.head_top = val;
  9412. sd->setlook_head_top = val;
  9413. break;
  9414. case LOOK_HEAD_MID:
  9415. sd->status.head_mid = val;
  9416. sd->setlook_head_mid = val;
  9417. break;
  9418. case LOOK_HAIR_COLOR: //Use the battle_config limits! [Skotlex]
  9419. val = cap_value(val, MIN_HAIR_COLOR, MAX_HAIR_COLOR);
  9420. if (sd->status.hair_color != val) {
  9421. sd->status.hair_color = val;
  9422. if (sd->status.guild_id) //Update Guild Window. [Skotlex]
  9423. intif_guild_change_memberinfo(sd->status.guild_id, sd->status.account_id, sd->status.char_id,
  9424. GMI_HAIR_COLOR, &sd->status.hair_color, sizeof(sd->status.hair_color));
  9425. }
  9426. break;
  9427. case LOOK_CLOTHES_COLOR: //Use the battle_config limits! [Skotlex]
  9428. val = cap_value(val, MIN_CLOTH_COLOR, MAX_CLOTH_COLOR);
  9429. sd->status.clothes_color = val;
  9430. break;
  9431. case LOOK_SHIELD:
  9432. sd->status.shield = val;
  9433. break;
  9434. case LOOK_SHOES:
  9435. break;
  9436. case LOOK_ROBE:
  9437. sd->status.robe = val;
  9438. sd->setlook_robe = val;
  9439. break;
  9440. case LOOK_BODY2:
  9441. val = cap_value(val, MIN_BODY_STYLE, MAX_BODY_STYLE);
  9442. sd->status.body = val;
  9443. break;
  9444. }
  9445. clif_changelook(&sd->bl, type, val);
  9446. }
  9447. /*==========================================
  9448. * Give an option (type) to player (sd) and display it to client
  9449. *------------------------------------------*/
  9450. void pc_setoption(map_session_data *sd,int type, int subtype)
  9451. {
  9452. int p_type, new_look=0;
  9453. nullpo_retv(sd);
  9454. p_type = sd->sc.option;
  9455. //Option has to be changed client-side before the class sprite or it won't always work (eg: Wedding sprite) [Skotlex]
  9456. sd->sc.option=type;
  9457. clif_changeoption(&sd->bl);
  9458. if( (type&OPTION_RIDING && !(p_type&OPTION_RIDING)) || (type&OPTION_DRAGON && !(p_type&OPTION_DRAGON) && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9459. { // Mounting
  9460. clif_status_load(&sd->bl,EFST_RIDING,1);
  9461. status_calc_pc(sd,SCO_NONE);
  9462. }
  9463. else if( (!(type&OPTION_RIDING) && p_type&OPTION_RIDING) || (!(type&OPTION_DRAGON) && p_type&OPTION_DRAGON && pc_checkskill(sd,RK_DRAGONTRAINING) > 0) )
  9464. { // Dismount
  9465. clif_status_load(&sd->bl,EFST_RIDING,0);
  9466. status_calc_pc(sd,SCO_NONE);
  9467. }
  9468. #ifndef NEW_CARTS
  9469. if( type&OPTION_CART && !( p_type&OPTION_CART ) ) { //Cart On
  9470. clif_cartlist(sd);
  9471. clif_updatestatus(sd, SP_CARTINFO);
  9472. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9473. status_calc_pc(sd,SCO_NONE); //Apply speed penalty.
  9474. } else if( !( type&OPTION_CART ) && p_type&OPTION_CART ){ //Cart Off
  9475. clif_clearcart(sd->fd);
  9476. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9477. status_calc_pc(sd,SCO_NONE); //Remove speed penalty.
  9478. }
  9479. #endif
  9480. if (type&OPTION_FALCON && !(p_type&OPTION_FALCON)) //Falcon ON
  9481. clif_status_load(&sd->bl,EFST_FALCON,1);
  9482. else if (!(type&OPTION_FALCON) && p_type&OPTION_FALCON) //Falcon OFF
  9483. clif_status_load(&sd->bl,EFST_FALCON,0);
  9484. if( type&OPTION_WUGRIDER && !(p_type&OPTION_WUGRIDER) ) { // Mounting
  9485. clif_status_load(&sd->bl,EFST_WUGRIDER,1);
  9486. status_calc_pc(sd,SCO_NONE);
  9487. } else if( !(type&OPTION_WUGRIDER) && p_type&OPTION_WUGRIDER ) { // Dismount
  9488. clif_status_load(&sd->bl,EFST_WUGRIDER,0);
  9489. status_calc_pc(sd,SCO_NONE);
  9490. }
  9491. if( type&OPTION_MADOGEAR && !(p_type&OPTION_MADOGEAR) ) {
  9492. sc_start(&sd->bl, &sd->bl, SC_MADOGEAR, 100, subtype, INFINITE_TICK);
  9493. } else if( !(type&OPTION_MADOGEAR) && p_type&OPTION_MADOGEAR ) {
  9494. status_change_end(&sd->bl, SC_MADOGEAR);
  9495. }
  9496. if (type&OPTION_FLYING && !(p_type&OPTION_FLYING))
  9497. new_look = JOB_STAR_GLADIATOR2;
  9498. else if (!(type&OPTION_FLYING) && p_type&OPTION_FLYING)
  9499. new_look = -1;
  9500. if (sd->disguise || !new_look)
  9501. return; //Disguises break sprite changes
  9502. if (new_look < 0) { //Restore normal look.
  9503. status_set_viewdata(&sd->bl, sd->status.class_);
  9504. new_look = sd->vd.class_;
  9505. }
  9506. pc_stop_attack(sd); //Stop attacking on new view change (to prevent wedding/santa attacks.
  9507. clif_changelook(&sd->bl,LOOK_BASE,new_look);
  9508. if (sd->vd.cloth_color)
  9509. clif_changelook(&sd->bl,LOOK_CLOTHES_COLOR,sd->vd.cloth_color);
  9510. if( sd->vd.body_style )
  9511. clif_changelook(&sd->bl,LOOK_BODY2,sd->vd.body_style);
  9512. clif_skillinfoblock(sd); // Skill list needs to be updated after base change.
  9513. }
  9514. /**
  9515. * Give player a cart
  9516. * @param sd Player
  9517. * @param type 0:Remove cart, 1 ~ MAX_CARTS: Cart type
  9518. **/
  9519. bool pc_setcart(map_session_data *sd,int type) {
  9520. #ifndef NEW_CARTS
  9521. int cart[6] = {0x0000,OPTION_CART1,OPTION_CART2,OPTION_CART3,OPTION_CART4,OPTION_CART5};
  9522. int option;
  9523. #endif
  9524. nullpo_retr(false,sd);
  9525. if( type < 0 || type > MAX_CARTS )
  9526. return false;// Never trust the values sent by the client! [Skotlex]
  9527. if( pc_checkskill(sd,MC_PUSHCART) <= 0 && type != 0 )
  9528. return false;// Push cart is required
  9529. #ifdef NEW_CARTS
  9530. switch( type ) {
  9531. case 0:
  9532. if( !sd->sc.getSCE(SC_PUSH_CART) )
  9533. return 0;
  9534. status_change_end(&sd->bl,SC_PUSH_CART);
  9535. clif_clearcart(sd->fd);
  9536. break;
  9537. default:/* everything else is an allowed ID so we can move on */
  9538. if( !sd->sc.getSCE(SC_PUSH_CART) ) { /* first time, so fill cart data */
  9539. clif_cartlist(sd);
  9540. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  9541. }
  9542. clif_updatestatus(sd, SP_CARTINFO);
  9543. sc_start(&sd->bl, &sd->bl, SC_PUSH_CART, 100, type, 0);
  9544. break;
  9545. }
  9546. if(pc_checkskill(sd, MC_PUSHCART) < 10)
  9547. status_calc_pc(sd,SCO_NONE); //Recalc speed penalty.
  9548. #else
  9549. // Update option
  9550. option = sd->sc.option;
  9551. option &= ~OPTION_CART;// clear cart bits
  9552. option |= cart[type]; // set cart
  9553. pc_setoption(sd, option);
  9554. #endif
  9555. return true;
  9556. }
  9557. /*==========================================
  9558. * Give player a falcon
  9559. *------------------------------------------*/
  9560. void pc_setfalcon(map_session_data* sd, int flag)
  9561. {
  9562. if( flag ){
  9563. if( pc_checkskill(sd,HT_FALCON)>0 ) // add falcon if he have the skill
  9564. pc_setoption(sd,sd->sc.option|OPTION_FALCON);
  9565. } else if( pc_isfalcon(sd) ){
  9566. pc_setoption(sd,sd->sc.option&~OPTION_FALCON); // remove falcon
  9567. }
  9568. }
  9569. /*==========================================
  9570. * Set player riding
  9571. *------------------------------------------*/
  9572. void pc_setriding(map_session_data* sd, int flag)
  9573. {
  9574. if( sd->sc.getSCE(SC_ALL_RIDING) )
  9575. return;
  9576. if( flag ){
  9577. if( pc_checkskill(sd,KN_RIDING) > 0 ) // add peco
  9578. pc_setoption(sd, sd->sc.option|OPTION_RIDING);
  9579. } else if( pc_isriding(sd) ){
  9580. pc_setoption(sd, sd->sc.option&~OPTION_RIDING);
  9581. }
  9582. }
  9583. /**
  9584. * Give player a mado
  9585. * @param sd: Player
  9586. * @param flag: Enable or disable mado
  9587. * @param type: See pc.hpp::e_mado_type (Default is MADO_ROBOT)
  9588. */
  9589. void pc_setmadogear(map_session_data *sd, bool flag, e_mado_type type)
  9590. {
  9591. if ((sd->class_ & MAPID_THIRDMASK) != MAPID_MECHANIC)
  9592. return;
  9593. if (flag) {
  9594. if (pc_checkskill(sd, NC_MADOLICENCE) > 0)
  9595. pc_setoption(sd, sd->sc.option | OPTION_MADOGEAR, type);
  9596. } else if (pc_ismadogear(sd)) {
  9597. pc_setoption(sd, sd->sc.option & ~OPTION_MADOGEAR);
  9598. }
  9599. }
  9600. /*==========================================
  9601. * Check if player can drop an item
  9602. *------------------------------------------*/
  9603. bool pc_candrop(map_session_data *sd, struct item *item)
  9604. {
  9605. if( item && ((item->expire_time || (item->bound && !pc_can_give_bounded_items(sd))) || (itemdb_ishatched_egg(item))) )
  9606. return false;
  9607. if( !pc_can_give_items(sd) || sd->sc.cant.drop) //check if this GM level can drop items
  9608. return false;
  9609. return (itemdb_isdropable(item, pc_get_group_level(sd)));
  9610. }
  9611. /*==========================================
  9612. * Read '@type' variables (temporary numeric char reg)
  9613. *------------------------------------------*/
  9614. int64 pc_readreg(map_session_data* sd, int64 reg)
  9615. {
  9616. return i64db_i64get(sd->regs.vars, reg);
  9617. }
  9618. /*==========================================
  9619. * Set '@type' variables (temporary numeric char reg)
  9620. *------------------------------------------*/
  9621. bool pc_setreg(map_session_data* sd, int64 reg, int64 val)
  9622. {
  9623. uint32 index = script_getvaridx(reg);
  9624. nullpo_retr(false, sd);
  9625. if( val ) {
  9626. i64db_i64put(sd->regs.vars, reg, val);
  9627. if( index )
  9628. script_array_update(&sd->regs, reg, false);
  9629. } else {
  9630. i64db_remove(sd->regs.vars, reg);
  9631. if( index )
  9632. script_array_update(&sd->regs, reg, true);
  9633. }
  9634. return true;
  9635. }
  9636. /*==========================================
  9637. * Read '@type$' variables (temporary string char reg)
  9638. *------------------------------------------*/
  9639. char* pc_readregstr(map_session_data* sd, int64 reg)
  9640. {
  9641. struct script_reg_str *p = NULL;
  9642. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9643. return p ? p->value : NULL;
  9644. }
  9645. /*==========================================
  9646. * Set '@type$' variables (temporary string char reg)
  9647. *------------------------------------------*/
  9648. bool pc_setregstr(map_session_data* sd, int64 reg, const char* str)
  9649. {
  9650. struct script_reg_str *p = NULL;
  9651. unsigned int index = script_getvaridx(reg);
  9652. DBData prev;
  9653. nullpo_retr(false, sd);
  9654. if( str[0] ) {
  9655. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9656. p->value = aStrdup(str);
  9657. p->flag.type = 1;
  9658. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9659. p = (struct script_reg_str *)db_data2ptr(&prev);
  9660. if( p->value )
  9661. aFree(p->value);
  9662. ers_free(str_reg_ers, p);
  9663. } else {
  9664. if( index )
  9665. script_array_update(&sd->regs, reg, false);
  9666. }
  9667. } else {
  9668. if (sd->regs.vars->remove(sd->regs.vars, db_i642key(reg), &prev)) {
  9669. p = (struct script_reg_str *)db_data2ptr(&prev);
  9670. if( p->value )
  9671. aFree(p->value);
  9672. ers_free(str_reg_ers, p);
  9673. if( index )
  9674. script_array_update(&sd->regs, reg, true);
  9675. }
  9676. }
  9677. return true;
  9678. }
  9679. /**
  9680. * Serves the following variable types:
  9681. * - 'type' (permanent numeric char reg)
  9682. * - '#type' (permanent numeric account reg)
  9683. * - '##type' (permanent numeric account reg2)
  9684. **/
  9685. int64 pc_readregistry(map_session_data *sd, int64 reg)
  9686. {
  9687. struct script_reg_num *p = NULL;
  9688. if (!sd->vars_ok) {
  9689. ShowError("pc_readregistry: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9690. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9691. //intif->request_registry(sd,type==3?4:type);
  9692. set_eof(sd->fd);
  9693. return 0;
  9694. }
  9695. p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg);
  9696. return p ? p->value : 0;
  9697. }
  9698. /**
  9699. * Serves the following variable types:
  9700. * - 'type$' (permanent str char reg)
  9701. * - '#type$' (permanent str account reg)
  9702. * - '##type$' (permanent str account reg2)
  9703. **/
  9704. char* pc_readregistry_str(map_session_data *sd, int64 reg)
  9705. {
  9706. struct script_reg_str *p = NULL;
  9707. if (!sd->vars_ok) {
  9708. ShowError("pc_readregistry_str: Trying to read reg %s before it's been loaded!\n", get_str(script_getvarid(reg)));
  9709. //This really shouldn't happen, so it's possible the data was lost somewhere, we should request it again.
  9710. //intif->request_registry(sd,type==3?4:type);
  9711. set_eof(sd->fd);
  9712. return NULL;
  9713. }
  9714. p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg);
  9715. return p ? p->value : NULL;
  9716. }
  9717. /**
  9718. * Serves the following variable types:
  9719. * - 'type' (permanent numeric char reg)
  9720. * - '#type' (permanent numeric account reg)
  9721. * - '##type' (permanent numeric account reg2)
  9722. **/
  9723. bool pc_setregistry(map_session_data *sd, int64 reg, int64 val)
  9724. {
  9725. struct script_reg_num *p = NULL;
  9726. const char *regname = get_str(script_getvarid(reg));
  9727. uint32 index = script_getvaridx(reg);
  9728. if ( !reg_load && !sd->vars_ok ) {
  9729. ShowError("pc_setregistry : refusing to set %s until vars are received.\n", regname);
  9730. return false;
  9731. }
  9732. if ((p = (struct script_reg_num *)i64db_get(sd->regs.vars, reg))) {
  9733. if( val ) {
  9734. if( !p->value && index ) /* its a entry that was deleted, so we reset array */
  9735. script_array_update(&sd->regs, reg, false);
  9736. p->value = val;
  9737. } else {
  9738. p->value = 0;
  9739. if( index )
  9740. script_array_update(&sd->regs, reg, true);
  9741. }
  9742. if (!reg_load)
  9743. p->flag.update = 1;/* either way, it will require either delete or replace */
  9744. } else if( val ) {
  9745. DBData prev;
  9746. if( index )
  9747. script_array_update(&sd->regs, reg, false);
  9748. p = ers_alloc(num_reg_ers, struct script_reg_num);
  9749. p->value = val;
  9750. if (!reg_load)
  9751. p->flag.update = 1;
  9752. if (sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev)) {
  9753. p = (struct script_reg_num *)db_data2ptr(&prev);
  9754. ers_free(num_reg_ers, p);
  9755. }
  9756. }
  9757. if (!reg_load && p)
  9758. sd->vars_dirty = true;
  9759. return true;
  9760. }
  9761. /**
  9762. * Serves the following variable types:
  9763. * - 'type$' (permanent str char reg)
  9764. * - '#type$' (permanent str account reg)
  9765. * - '##type$' (permanent str account reg2)
  9766. **/
  9767. bool pc_setregistry_str(map_session_data *sd, int64 reg, const char *val)
  9768. {
  9769. struct script_reg_str *p = NULL;
  9770. const char *regname = get_str(script_getvarid(reg));
  9771. unsigned int index = script_getvaridx(reg);
  9772. size_t vlen = 0;
  9773. if (!reg_load && !sd->vars_ok) {
  9774. ShowError("pc_setregistry_str : refusing to set %s until vars are received.\n", regname);
  9775. return false;
  9776. }
  9777. if ( !script_check_RegistryVariableLength(1, val, &vlen ) )
  9778. {
  9779. 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);
  9780. return false;
  9781. }
  9782. if( (p = (struct script_reg_str *)i64db_get(sd->regs.vars, reg) ) ) {
  9783. if( val[0] ) {
  9784. if( p->value )
  9785. aFree(p->value);
  9786. else if ( index ) // an entry that was deleted, so we reset
  9787. script_array_update(&sd->regs, reg, false);
  9788. p->value = aStrdup(val);
  9789. } else {
  9790. if (p->value)
  9791. aFree(p->value);
  9792. p->value = NULL;
  9793. if( index )
  9794. script_array_update(&sd->regs, reg, true);
  9795. }
  9796. if( !reg_load )
  9797. p->flag.update = 1; // either way, it will require either delete or replace
  9798. } else if( val[0] ) {
  9799. DBData prev;
  9800. if( index )
  9801. script_array_update(&sd->regs, reg, false);
  9802. p = ers_alloc(str_reg_ers, struct script_reg_str);
  9803. p->value = aStrdup(val);
  9804. if( !reg_load )
  9805. p->flag.update = 1;
  9806. p->flag.type = 1;
  9807. if( sd->regs.vars->put(sd->regs.vars, db_i642key(reg), db_ptr2data(p), &prev) ) {
  9808. p = (struct script_reg_str *)db_data2ptr(&prev);
  9809. if( p->value )
  9810. aFree(p->value);
  9811. ers_free(str_reg_ers, p);
  9812. }
  9813. }
  9814. if( !reg_load && p )
  9815. sd->vars_dirty = true;
  9816. return true;
  9817. }
  9818. /**
  9819. * Set value of player variable
  9820. * @param sd Player
  9821. * @param reg Variable name
  9822. * @param value
  9823. * @return True if success, false if failed.
  9824. **/
  9825. bool pc_setreg2(map_session_data *sd, const char *reg, int64 val) {
  9826. char prefix = reg[0];
  9827. nullpo_retr(false, sd);
  9828. if (reg[strlen(reg)-1] == '$') {
  9829. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9830. return false;
  9831. }
  9832. val = cap_value(val, INT_MIN, INT_MAX);
  9833. switch (prefix) {
  9834. case '.':
  9835. case '\'':
  9836. case '$':
  9837. ShowError("pc_setreg2: Invalid variable scope '%s'\n", reg);
  9838. return false;
  9839. case '@':
  9840. return pc_setreg(sd, add_str(reg), val);
  9841. case '#':
  9842. return (reg[1] == '#') ? pc_setaccountreg2(sd, add_str(reg), val) : pc_setaccountreg(sd, add_str(reg), val);
  9843. default:
  9844. return pc_setglobalreg(sd, add_str(reg), val);
  9845. }
  9846. return false;
  9847. }
  9848. /**
  9849. * Get value of player variable
  9850. * @param sd Player
  9851. * @param reg Variable name
  9852. * @return Variable value or 0 if failed.
  9853. **/
  9854. int64 pc_readreg2(map_session_data *sd, const char *reg) {
  9855. char prefix = reg[0];
  9856. nullpo_ret(sd);
  9857. if (reg[strlen(reg)-1] == '$') {
  9858. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9859. return 0;
  9860. }
  9861. switch (prefix) {
  9862. case '.':
  9863. case '\'':
  9864. case '$':
  9865. ShowError("pc_readreg2: Invalid variable scope '%s'\n", reg);
  9866. return 0;
  9867. case '@':
  9868. return pc_readreg(sd, add_str(reg));
  9869. case '#':
  9870. return (reg[1] == '#') ? pc_readaccountreg2(sd, add_str(reg)) : pc_readaccountreg(sd, add_str(reg));
  9871. default:
  9872. return pc_readglobalreg(sd, add_str(reg));
  9873. }
  9874. return 0;
  9875. }
  9876. /*==========================================
  9877. * Exec eventtimer for player sd (retrieved from map_session (id))
  9878. *------------------------------------------*/
  9879. static TIMER_FUNC(pc_eventtimer){
  9880. map_session_data *sd=map_id2sd(id);
  9881. char *p = (char *)data;
  9882. int i;
  9883. if(sd==NULL)
  9884. return 0;
  9885. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == tid );
  9886. if( i < MAX_EVENTTIMER )
  9887. {
  9888. sd->eventtimer[i] = INVALID_TIMER;
  9889. sd->eventcount--;
  9890. npc_event(sd,p,0);
  9891. }
  9892. else
  9893. ShowError("pc_eventtimer: no such event timer\n");
  9894. if (p) aFree(p);
  9895. return 0;
  9896. }
  9897. /*==========================================
  9898. * Add eventtimer for player sd ?
  9899. *------------------------------------------*/
  9900. bool pc_addeventtimer(map_session_data *sd,int tick,const char *name)
  9901. {
  9902. int i;
  9903. nullpo_retr(false,sd);
  9904. ARR_FIND( 0, MAX_EVENTTIMER, i, sd->eventtimer[i] == INVALID_TIMER );
  9905. if( i == MAX_EVENTTIMER )
  9906. return false;
  9907. sd->eventtimer[i] = add_timer(gettick()+tick, pc_eventtimer, sd->bl.id, (intptr_t)aStrdup(name));
  9908. sd->eventcount++;
  9909. return true;
  9910. }
  9911. /*==========================================
  9912. * Del eventtimer for player sd ?
  9913. *------------------------------------------*/
  9914. bool pc_deleventtimer(map_session_data *sd,const char *name)
  9915. {
  9916. char* p = NULL;
  9917. int i;
  9918. nullpo_retr(false,sd);
  9919. if (sd->eventcount == 0)
  9920. return false;
  9921. // find the named event timer
  9922. ARR_FIND( 0, MAX_EVENTTIMER, i,
  9923. sd->eventtimer[i] != INVALID_TIMER &&
  9924. (p = (char *)(get_timer(sd->eventtimer[i])->data)) != NULL &&
  9925. strcmp(p, name) == 0
  9926. );
  9927. if( i == MAX_EVENTTIMER )
  9928. return false; // not found
  9929. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9930. sd->eventtimer[i] = INVALID_TIMER;
  9931. if(sd->eventcount > 0)
  9932. sd->eventcount--;
  9933. aFree(p);
  9934. return true;
  9935. }
  9936. /*==========================================
  9937. * Update eventtimer count for player sd
  9938. *------------------------------------------*/
  9939. void pc_addeventtimercount(map_session_data *sd,const char *name,int tick)
  9940. {
  9941. int i;
  9942. nullpo_retv(sd);
  9943. for(i=0;i<MAX_EVENTTIMER;i++)
  9944. if( sd->eventtimer[i] != INVALID_TIMER && strcmp(
  9945. (char *)(get_timer(sd->eventtimer[i])->data), name)==0 ){
  9946. addtick_timer(sd->eventtimer[i],tick);
  9947. break;
  9948. }
  9949. }
  9950. /*==========================================
  9951. * Remove all eventtimer for player sd
  9952. *------------------------------------------*/
  9953. void pc_cleareventtimer(map_session_data *sd)
  9954. {
  9955. int i;
  9956. nullpo_retv(sd);
  9957. if (sd->eventcount == 0)
  9958. return;
  9959. for(i=0;i<MAX_EVENTTIMER;i++){
  9960. if( sd->eventtimer[i] != INVALID_TIMER ){
  9961. char *p = (char *)(get_timer(sd->eventtimer[i])->data);
  9962. delete_timer(sd->eventtimer[i],pc_eventtimer);
  9963. sd->eventtimer[i] = INVALID_TIMER;
  9964. if(sd->eventcount > 0) //avoid looping to max val
  9965. sd->eventcount--;
  9966. if (p) aFree(p);
  9967. }
  9968. }
  9969. }
  9970. /**
  9971. * Called when an item with combo is worn
  9972. * @param sd: Player data
  9973. * @param data: Item data
  9974. * @return Number of succeeded combo(s)
  9975. */
  9976. static int pc_checkcombo(map_session_data *sd, item_data *data) {
  9977. int success = 0;
  9978. for (const auto &item_combo : data->combos) {
  9979. bool do_continue = false;
  9980. // Ensure this isn't a duplicate combo
  9981. for (const auto player_combo : sd->combos) {
  9982. if (player_combo->id == item_combo->id) {
  9983. do_continue = true;
  9984. break;
  9985. }
  9986. }
  9987. // Combo already equipped
  9988. if (do_continue)
  9989. continue;
  9990. size_t nb_itemCombo = item_combo->nameid.size();
  9991. if (nb_itemCombo < 2) // A combo with less then 2 item?
  9992. continue;
  9993. struct s_itemchk {
  9994. int idx;
  9995. t_itemid nameid, card[MAX_SLOTS];
  9996. s_itemchk() : idx(0), nameid(0), card() {};
  9997. };
  9998. std::vector<s_itemchk> combo_idx(nb_itemCombo);
  9999. size_t j;
  10000. unsigned int pos = 0;
  10001. for (j = 0; j < nb_itemCombo; j++) {
  10002. t_itemid id = item_combo->nameid[j];
  10003. bool found = false;
  10004. for (int16 k = 0; k < EQI_MAX; k++) {
  10005. short index = sd->equip_index[k];
  10006. if (index < 0)
  10007. continue;
  10008. if (pc_is_same_equip_index((equip_index)k, sd->equip_index, index))
  10009. continue;
  10010. if (!sd->inventory_data[index])
  10011. continue;
  10012. if (itemdb_type(id) != IT_CARD) {
  10013. if (sd->inventory_data[index]->nameid != id)
  10014. continue;
  10015. if (j > 0) { // Check if this item not already used
  10016. do_continue = false;
  10017. for (size_t z = 0; z < nb_itemCombo - 1; z++) {
  10018. if (combo_idx[z].idx == index && combo_idx[z].nameid == id) { // Index already recorded
  10019. do_continue = true;
  10020. break;
  10021. }
  10022. }
  10023. if (do_continue)
  10024. continue;
  10025. }
  10026. combo_idx[j].nameid = id;
  10027. combo_idx[j].idx = index;
  10028. pos |= sd->inventory.u.items_inventory[index].equip;
  10029. found = true;
  10030. break;
  10031. } else { // Cards and enchants
  10032. if (itemdb_isspecial(sd->inventory.u.items_inventory[index].card[0]))
  10033. continue;
  10034. for (uint8 z = 0; z < MAX_SLOTS; z++) {
  10035. do_continue = false;
  10036. if (sd->inventory.u.items_inventory[index].card[z] != id)
  10037. continue;
  10038. if (j > 0) {
  10039. for (size_t c1 = 0; c1 < nb_itemCombo - 1; c1++) {
  10040. if (combo_idx[c1].idx == index && combo_idx[c1].nameid == id) {
  10041. for (uint8 c2 = 0; c2 < MAX_SLOTS; c2++) {
  10042. if (combo_idx[c1].card[c2] == id) { // Card already recorded (at this same idx)
  10043. do_continue = true;
  10044. break;
  10045. }
  10046. }
  10047. }
  10048. }
  10049. }
  10050. if (do_continue)
  10051. continue;
  10052. combo_idx[j].nameid = id;
  10053. combo_idx[j].idx = index;
  10054. combo_idx[j].card[z] = id;
  10055. pos |= sd->inventory.u.items_inventory[index].equip;
  10056. found = true;
  10057. break;
  10058. }
  10059. }
  10060. }
  10061. if (!found)
  10062. break; // Unable to found all the IDs for this combo, return
  10063. }
  10064. // Broke out of the count loop without finding all IDs, move to the next combo
  10065. if (j < nb_itemCombo)
  10066. continue;
  10067. // All items in the combo are matching
  10068. auto entry = std::make_shared<s_combos>();
  10069. entry->bonus = item_combo->script;
  10070. entry->id = item_combo->id;
  10071. entry->pos = pos;
  10072. sd->combos.push_back(entry);
  10073. combo_idx.clear();
  10074. success++;
  10075. }
  10076. return success;
  10077. }
  10078. /**
  10079. * Called when an item with combo is removed
  10080. * @param sd: Player data
  10081. * @param data: Item data
  10082. * @return Number of removed combo(s)
  10083. */
  10084. static int pc_removecombo(map_session_data *sd, item_data *data ) {
  10085. if (sd->combos.empty())
  10086. return 0; // Nothing to do here, player has no combos
  10087. int retval = 0;
  10088. for (const auto &item_combo : data->combos) {
  10089. std::shared_ptr<s_combos> del_combo = nullptr;
  10090. // Check if this combo exists on this player
  10091. for (const auto &player_combo : sd->combos) {
  10092. if (player_combo->id == item_combo->id) {
  10093. del_combo = player_combo;
  10094. break;
  10095. }
  10096. }
  10097. // No match, skip this combo
  10098. if (del_combo == nullptr)
  10099. continue;
  10100. util::vector_erase_if_exists(sd->combos, del_combo);
  10101. retval++;
  10102. // Check if combo requirements still fit
  10103. if (pc_checkcombo(sd, data))
  10104. continue;
  10105. // It's empty, clear all the memory
  10106. if (sd->combos.empty()) {
  10107. sd->combos.clear();
  10108. return retval; // Return at this point as there are no more combos to check
  10109. }
  10110. }
  10111. return retval;
  10112. }
  10113. /**
  10114. * Load combo data(s) of player
  10115. * @param sd: Player data
  10116. * @return ret numbers of succeed combo
  10117. */
  10118. int pc_load_combo(map_session_data *sd) {
  10119. int ret = 0;
  10120. for (int16 i = 0; i < EQI_MAX; i++) {
  10121. item_data *id;
  10122. short idx = sd->equip_index[i];
  10123. if (idx < 0 || !(id = sd->inventory_data[idx]))
  10124. continue;
  10125. if (!id->combos.empty())
  10126. ret += pc_checkcombo(sd, id);
  10127. if (!itemdb_isspecial(sd->inventory.u.items_inventory[idx].card[0])) {
  10128. for (uint8 j = 0; j < MAX_SLOTS; j++) {
  10129. if (!sd->inventory.u.items_inventory[idx].card[j])
  10130. continue;
  10131. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[idx].card[j]);
  10132. if (data != nullptr) {
  10133. if (!data->combos.empty())
  10134. ret += pc_checkcombo(sd, data.get());
  10135. }
  10136. }
  10137. }
  10138. }
  10139. return ret;
  10140. }
  10141. /*==========================================
  10142. * Equip item on player sd at req_pos from inventory index n
  10143. * return: false - fail; true - success
  10144. *------------------------------------------*/
  10145. bool pc_equipitem(map_session_data *sd,short n,int req_pos,bool equipswitch)
  10146. {
  10147. int i, pos, flag = 0, iflag;
  10148. struct item_data *id;
  10149. uint8 res = ITEM_EQUIP_ACK_OK;
  10150. short* equip_index;
  10151. nullpo_retr(false,sd);
  10152. if( n < 0 || n >= MAX_INVENTORY ) {
  10153. if( equipswitch ){
  10154. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10155. }else{
  10156. // Does not deserve an answer... [Lemongrass]
  10157. //clif_equipitemack( sd, ITEM_EQUIP_ACK_FAIL, n );
  10158. }
  10159. return false;
  10160. }
  10161. if( DIFF_TICK(sd->canequip_tick,gettick()) > 0 ) {
  10162. if( equipswitch ){
  10163. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10164. }else{
  10165. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n );
  10166. }
  10167. return false;
  10168. }
  10169. if (!(id = sd->inventory_data[n]))
  10170. return false;
  10171. pos = pc_equippoint(sd,n); //With a few exceptions, item should go in all specified slots.
  10172. if(battle_config.battle_log && !equipswitch)
  10173. ShowInfo("equip %u (%d) %x:%x\n",sd->inventory.u.items_inventory[n].nameid,n,id?id->equip:0,req_pos);
  10174. if((res = pc_isequip(sd,n))) {
  10175. if( equipswitch ){
  10176. clif_equipswitch_add( sd, n, req_pos, res );
  10177. }else{
  10178. clif_equipitemack( *sd, res, n ); // fail
  10179. }
  10180. return false;
  10181. }
  10182. if( equipswitch && id->type == IT_AMMO ){
  10183. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10184. return false;
  10185. }
  10186. if (!(pos&req_pos) || sd->inventory.u.items_inventory[n].equip != 0 || sd->inventory.u.items_inventory[n].attribute==1 ) { // [Valaris]
  10187. if( equipswitch ){
  10188. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10189. }else{
  10190. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); // fail
  10191. }
  10192. return false;
  10193. }
  10194. if( sd->sc.count && (sd->sc.cant.equip || (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON)) ) {
  10195. if( equipswitch ){
  10196. clif_equipswitch_add( sd, n, req_pos, ITEM_EQUIP_ACK_FAIL );
  10197. }else{
  10198. clif_equipitemack( *sd, ITEM_EQUIP_ACK_FAIL, n ); //Fail
  10199. }
  10200. return false;
  10201. }
  10202. equip_index = equipswitch ? sd->equip_switch_index : sd->equip_index;
  10203. if ( !equipswitch && id->flag.bindOnEquip && !sd->inventory.u.items_inventory[n].bound) {
  10204. sd->inventory.u.items_inventory[n].bound = (char)battle_config.default_bind_on_equip;
  10205. clif_notify_bindOnEquip( *sd, n );
  10206. }
  10207. if(pos == EQP_ACC) { //Accessories should only go in one of the two.
  10208. pos = req_pos&EQP_ACC;
  10209. if (pos == EQP_ACC) //User specified both slots.
  10210. pos = (equip_index[EQI_ACC_R] >= 0 && equip_index[EQI_ACC_L] < 0) ? EQP_ACC_L : EQP_ACC_R;
  10211. for (i = 0; i < sd->inventory_data[n]->slots; i++) { // Accessories that have cards that force equip location
  10212. if (!sd->inventory.u.items_inventory[n].card[i])
  10213. continue;
  10214. std::shared_ptr<item_data> card_data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10215. if (card_data) {
  10216. int card_pos = card_data->equip;
  10217. if (card_pos == EQP_ACC_L || card_pos == EQP_ACC_R) {
  10218. pos = card_pos; // Use the card's equip position
  10219. break;
  10220. }
  10221. }
  10222. }
  10223. } else if(pos == EQP_ARMS && id->equip == EQP_HAND_R) { //Dual wield capable weapon.
  10224. pos = (req_pos&EQP_ARMS);
  10225. if (pos == EQP_ARMS) //User specified both slots, pick one for them.
  10226. #ifdef RENEWAL
  10227. pos = (equip_index[EQI_HAND_R] >= 0 && equip_index[EQI_HAND_L] < 0) ? EQP_HAND_L : EQP_HAND_R;
  10228. #else
  10229. pos = equip_index[EQI_HAND_R] >= 0 ? EQP_HAND_L : EQP_HAND_R;
  10230. #endif
  10231. } else if(pos == EQP_SHADOW_ACC) { // Shadow System
  10232. pos = req_pos&EQP_SHADOW_ACC;
  10233. if (pos == EQP_SHADOW_ACC)
  10234. pos = (equip_index[EQI_SHADOW_ACC_L] >= 0 && equip_index[EQI_SHADOW_ACC_R] < 0) ? EQP_SHADOW_ACC_R : EQP_SHADOW_ACC_L;
  10235. } else if(pos == EQP_SHADOW_ARMS && id->equip == EQP_SHADOW_WEAPON) {
  10236. pos = (req_pos&EQP_SHADOW_ARMS);
  10237. if( pos == EQP_SHADOW_ARMS )
  10238. pos = (equip_index[EQI_SHADOW_WEAPON] >= 0 ? EQP_SHADOW_SHIELD : EQP_SHADOW_WEAPON);
  10239. }
  10240. if (pos&EQP_HAND_R && battle_config.use_weapon_skill_range&BL_PC) {
  10241. //Update skill-block range database when weapon range changes. [Skotlex]
  10242. i = equip_index[EQI_HAND_R];
  10243. if (i < 0 || !sd->inventory_data[i]) //No data, or no weapon equipped
  10244. flag = 1;
  10245. else
  10246. flag = id->range != sd->inventory_data[i]->range;
  10247. }
  10248. if( equipswitch ){
  10249. for( i = 0; i < EQI_MAX; i++ ){
  10250. if( pos&equip_bitmask[i] ){
  10251. // If there was already an item assigned to this slot
  10252. if( sd->equip_switch_index[i] >= 0 ){
  10253. pc_equipswitch_remove( sd, sd->equip_switch_index[i] );
  10254. }
  10255. // Assign the new index to it
  10256. sd->equip_switch_index[i] = n;
  10257. }
  10258. }
  10259. sd->inventory.u.items_inventory[n].equipSwitch = pos;
  10260. clif_equipswitch_add( sd, n, pos, ITEM_EQUIP_ACK_OK );
  10261. return true;
  10262. }else{
  10263. for(i=0;i<EQI_MAX;i++) {
  10264. if(pos & equip_bitmask[i]) {
  10265. if(sd->equip_index[i] >= 0) //Slot taken, remove item from there.
  10266. pc_unequipitem(sd,sd->equip_index[i], 1 | 2 | 4);
  10267. sd->equip_index[i] = n;
  10268. }
  10269. }
  10270. pc_equipswitch_remove(sd, n);
  10271. if(pos==EQP_AMMO) {
  10272. clif_arrowequip(sd,n);
  10273. clif_arrow_fail(sd,3);
  10274. }
  10275. else
  10276. clif_equipitemack( *sd, ITEM_EQUIP_ACK_OK, n, pos );
  10277. sd->inventory.u.items_inventory[n].equip = pos;
  10278. }
  10279. if(pos & EQP_HAND_R) {
  10280. if(id)
  10281. sd->weapontype1 = id->subtype;
  10282. else
  10283. sd->weapontype1 = W_FIST;
  10284. pc_calcweapontype(sd);
  10285. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10286. }
  10287. if(pos & EQP_HAND_L) {
  10288. if(id) {
  10289. if(id->type == IT_WEAPON) {
  10290. sd->status.shield = W_FIST;
  10291. sd->weapontype2 = id->subtype;
  10292. }
  10293. else
  10294. if(id->type == IT_ARMOR) {
  10295. sd->status.shield = id->look;
  10296. sd->weapontype2 = W_FIST;
  10297. }
  10298. }
  10299. else
  10300. sd->status.shield = sd->weapontype2 = W_FIST;
  10301. pc_calcweapontype(sd);
  10302. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10303. }
  10304. if(pos & EQP_SHOES)
  10305. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10306. if (battle_config.ammo_unequip && (pos&EQP_ARMS) && id->type == IT_WEAPON) {
  10307. short idx = sd->equip_index[EQI_AMMO];
  10308. if (idx >= 0) {
  10309. switch (sd->inventory_data[idx]->subtype) {
  10310. case AMMO_ARROW:
  10311. if (id->subtype != W_BOW && id->subtype != W_MUSICAL && id->subtype != W_WHIP)
  10312. pc_unequipitem(sd, idx, 2 | 4);
  10313. break;
  10314. case AMMO_BULLET:
  10315. case AMMO_SHELL:
  10316. if (id->subtype != W_REVOLVER && id->subtype != W_RIFLE && id->subtype != W_GATLING && id->subtype != W_SHOTGUN
  10317. #ifdef RENEWAL
  10318. && id->subtype != W_GRENADE
  10319. #endif
  10320. )
  10321. pc_unequipitem(sd, idx, 2 | 4);
  10322. break;
  10323. #ifndef RENEWAL
  10324. case AMMO_GRENADE:
  10325. if (id->subtype != W_GRENADE)
  10326. pc_unequipitem(sd, idx, 2 | 4);
  10327. break;
  10328. #endif
  10329. }
  10330. }
  10331. }
  10332. pc_set_costume_view(sd);
  10333. pc_checkallowskill(sd); //Check if status changes should be halted.
  10334. iflag = sd->npc_item_flag;
  10335. // Check for combos (MUST be before status_calc_pc)
  10336. if (!id->combos.empty())
  10337. pc_checkcombo(sd, id);
  10338. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10339. ; // No cards
  10340. else {
  10341. for (i = 0; i < MAX_SLOTS; i++) {
  10342. if (!sd->inventory.u.items_inventory[n].card[i])
  10343. continue;
  10344. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10345. if (data != nullptr) {
  10346. if (!data->combos.empty())
  10347. pc_checkcombo(sd, data.get());
  10348. }
  10349. }
  10350. }
  10351. status_calc_pc(sd,SCO_NONE);
  10352. if (flag) //Update skill data
  10353. clif_skillinfoblock(sd);
  10354. //OnEquip script [Skotlex]
  10355. if (id) {
  10356. //only run the script if item isn't restricted
  10357. if (id->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(id,sd->bl.m)))
  10358. run_script(id->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10359. if(itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10360. ; //No cards
  10361. else {
  10362. for( i = 0; i < MAX_SLOTS; i++ ) {
  10363. if (!sd->inventory.u.items_inventory[n].card[i])
  10364. continue;
  10365. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10366. if ( data != nullptr ) {
  10367. if (data->equip_script && (pc_has_permission(sd,PC_PERM_USE_ALL_EQUIPMENT) || !itemdb_isNoEquip(data.get(), sd->bl.m)))
  10368. run_script(data->equip_script,0,sd->bl.id,fake_nd->bl.id);
  10369. }
  10370. }
  10371. }
  10372. }
  10373. sd->npc_item_flag = iflag;
  10374. return true;
  10375. }
  10376. static void pc_deleteautobonus( std::vector<std::shared_ptr<s_autobonus>>& bonus, int position ){
  10377. std::vector<std::shared_ptr<s_autobonus>>::iterator it = bonus.begin();
  10378. while( it != bonus.end() ){
  10379. std::shared_ptr<s_autobonus> b = *it;
  10380. if( ( b->pos & position ) != b->pos ){
  10381. it++;
  10382. continue;
  10383. }
  10384. it = bonus.erase( it );
  10385. }
  10386. }
  10387. /**
  10388. * Recalculate player status on unequip
  10389. * @param sd: Player data
  10390. * @param n: Item inventory index
  10391. * @param flag: Whether to recalculate a player's status or not
  10392. * @return True on success or false on failure
  10393. */
  10394. static void pc_unequipitem_sub(map_session_data *sd, int n, int flag) {
  10395. int i, iflag;
  10396. bool status_calc = false;
  10397. pc_deleteautobonus( sd->autobonus, sd->inventory.u.items_inventory[n].equip );
  10398. pc_deleteautobonus( sd->autobonus2, sd->inventory.u.items_inventory[n].equip );
  10399. pc_deleteautobonus( sd->autobonus3, sd->inventory.u.items_inventory[n].equip );
  10400. sd->inventory.u.items_inventory[n].equip = 0;
  10401. if (!(flag & 4))
  10402. pc_checkallowskill(sd);
  10403. iflag = sd->npc_item_flag;
  10404. // Check for combos (MUST be before status_calc_pc)
  10405. if (sd->inventory_data[n]) {
  10406. if (!sd->inventory_data[n]->combos.empty()) {
  10407. if (pc_removecombo(sd, sd->inventory_data[n]))
  10408. status_calc = true;
  10409. }
  10410. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10411. ; // No cards
  10412. else {
  10413. for (i = 0; i < MAX_SLOTS; i++) {
  10414. if (!sd->inventory.u.items_inventory[n].card[i])
  10415. continue;
  10416. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10417. if (data != nullptr) {
  10418. if (!data->combos.empty()) {
  10419. if (pc_removecombo(sd, data.get()))
  10420. status_calc = true;
  10421. }
  10422. }
  10423. }
  10424. }
  10425. }
  10426. if (flag & 1 || status_calc) {
  10427. pc_checkallowskill(sd);
  10428. status_calc_pc(sd, SCO_FORCE);
  10429. }
  10430. if (sd->sc.getSCE(SC_SIGNUMCRUCIS) && !battle_check_undead(sd->battle_status.race, sd->battle_status.def_ele))
  10431. status_change_end(&sd->bl, SC_SIGNUMCRUCIS);
  10432. //OnUnEquip script [Skotlex]
  10433. if (sd->inventory_data[n]) {
  10434. if (sd->inventory_data[n]->unequip_script)
  10435. run_script(sd->inventory_data[n]->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10436. if (itemdb_isspecial(sd->inventory.u.items_inventory[n].card[0]))
  10437. ; //No cards
  10438. else {
  10439. for (i = 0; i < MAX_SLOTS; i++) {
  10440. if (!sd->inventory.u.items_inventory[n].card[i])
  10441. continue;
  10442. std::shared_ptr<item_data> data = item_db.find(sd->inventory.u.items_inventory[n].card[i]);
  10443. if (data != nullptr) {
  10444. if (data->unequip_script)
  10445. run_script(data->unequip_script, 0, sd->bl.id, fake_nd->bl.id);
  10446. }
  10447. }
  10448. }
  10449. }
  10450. sd->npc_item_flag = iflag;
  10451. }
  10452. /**
  10453. * Called when attempting to unequip an item from a player
  10454. * @param sd: Player data
  10455. * @param n: Item inventory index
  10456. * @param flag: Type of unequip
  10457. * 0 - only unequip
  10458. * 1 - calculate status after unequipping
  10459. * 2 - force unequip
  10460. * 4 - unequip by switching equipment
  10461. * @return True on success or false on failure
  10462. */
  10463. bool pc_unequipitem(map_session_data *sd, int n, int flag) {
  10464. int i, pos;
  10465. nullpo_retr(false,sd);
  10466. if (n < 0 || n >= MAX_INVENTORY) {
  10467. clif_unequipitemack(sd,0,0,0);
  10468. return false;
  10469. }
  10470. if (!(pos = sd->inventory.u.items_inventory[n].equip)) {
  10471. clif_unequipitemack(sd,n,0,0);
  10472. return false; //Nothing to unequip
  10473. }
  10474. // status change that makes player cannot unequip equipment
  10475. if (!(flag&2) && sd->sc.count &&( sd->sc.cant.unequip ||
  10476. (sd->sc.getSCE(SC_PYROCLASTIC) && sd->inventory_data[n]->type == IT_WEAPON))) // can't switch weapon
  10477. {
  10478. clif_unequipitemack(sd,n,0,0);
  10479. return false;
  10480. }
  10481. if (battle_config.battle_log)
  10482. ShowInfo("unequip %d %x:%x\n",n,pc_equippoint(sd,n),pos);
  10483. for(i = 0; i < EQI_MAX; i++) {
  10484. if (pos & equip_bitmask[i])
  10485. sd->equip_index[i] = -1;
  10486. }
  10487. if(pos & EQP_HAND_R) {
  10488. sd->weapontype1 = W_FIST;
  10489. sd->status.weapon = sd->weapontype2;
  10490. pc_calcweapontype(sd);
  10491. clif_changelook(&sd->bl,LOOK_WEAPON,sd->status.weapon);
  10492. if( !battle_config.dancing_weaponswitch_fix )
  10493. status_change_end(&sd->bl, SC_DANCING); // Unequipping => stop dancing.
  10494. #ifdef RENEWAL
  10495. if (battle_config.switch_remove_edp&2) {
  10496. #else
  10497. if (battle_config.switch_remove_edp&1) {
  10498. #endif
  10499. status_change_end(&sd->bl, SC_EDP);
  10500. }
  10501. }
  10502. if(pos & EQP_HAND_L) {
  10503. if (sd->status.shield && battle_getcurrentskill(&sd->bl) == LG_SHIELDSPELL)
  10504. unit_skillcastcancel(&sd->bl, 0); // Cancel Shield Spell if player swaps shields.
  10505. sd->status.shield = sd->weapontype2 = W_FIST;
  10506. pc_calcweapontype(sd);
  10507. clif_changelook(&sd->bl,LOOK_SHIELD,sd->status.shield);
  10508. }
  10509. if(pos & EQP_SHOES)
  10510. clif_changelook(&sd->bl,LOOK_SHOES,0);
  10511. clif_unequipitemack(sd,n,pos,1);
  10512. pc_set_costume_view(sd);
  10513. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIP });
  10514. // On weapon change (right and left hand)
  10515. if ((pos & EQP_ARMS) && sd->inventory_data[n]->type == IT_WEAPON) {
  10516. if (battle_config.ammo_unequip && !(flag & 4)) {
  10517. switch (sd->inventory_data[n]->subtype) {
  10518. case W_BOW:
  10519. case W_MUSICAL:
  10520. case W_WHIP:
  10521. case W_REVOLVER:
  10522. case W_RIFLE:
  10523. case W_GATLING:
  10524. case W_SHOTGUN:
  10525. case W_GRENADE: {
  10526. short idx = sd->equip_index[EQI_AMMO];
  10527. if (idx >= 0) {
  10528. sd->equip_index[EQI_AMMO] = -1;
  10529. clif_unequipitemack(sd, idx, sd->inventory.u.items_inventory[idx].equip, 1);
  10530. pc_unequipitem_sub(sd, idx, 0);
  10531. }
  10532. }
  10533. break;
  10534. }
  10535. }
  10536. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPWEAPON });
  10537. }
  10538. // On armor change
  10539. if (pos & EQP_ARMOR) {
  10540. status_db.removeByStatusFlag(&sd->bl, { SCF_REMOVEONUNEQUIPARMOR });
  10541. }
  10542. // On equipment change
  10543. #ifndef RENEWAL
  10544. if (!(flag&4))
  10545. status_change_end(&sd->bl, SC_CONCENTRATION);
  10546. #endif
  10547. // On ammo change
  10548. 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_))
  10549. status_change_end(&sd->bl, SC_P_ALTER);
  10550. pc_unequipitem_sub(sd, n, flag);
  10551. return true;
  10552. }
  10553. int pc_equipswitch( map_session_data* sd, int index ){
  10554. // Get the target equip mask
  10555. int position = sd->inventory.u.items_inventory[index].equipSwitch;
  10556. // Get the currently equipped item
  10557. short equippedItem = pc_checkequip( sd, position, true );
  10558. // No item equipped at the target
  10559. if( equippedItem == -1 ){
  10560. // Remove it from the equip switch
  10561. pc_equipswitch_remove( sd, index );
  10562. pc_equipitem( sd, index, position );
  10563. return position;
  10564. }else{
  10565. std::map<int, int> unequipped;
  10566. int unequipped_position = 0;
  10567. // Unequip all items that interfere
  10568. for( int i = 0; i < EQI_MAX; i++ ){
  10569. int unequip_index = sd->equip_index[i];
  10570. if( unequip_index >= 0 && position & equip_bitmask[i] ){
  10571. struct item* unequip_item = &sd->inventory.u.items_inventory[unequip_index];
  10572. // Store the unequipped index and position mask for later
  10573. unequipped[unequip_index] = unequip_item->equip;
  10574. // Keep the position for later
  10575. unequipped_position |= unequip_item->equip;
  10576. // Unequip the item
  10577. pc_unequipitem( sd, unequip_index, 0 );
  10578. }
  10579. }
  10580. int all_position = position | unequipped_position;
  10581. // Equip everything that is hit by the mask
  10582. for( int i = 0; i < EQI_MAX; i++ ){
  10583. int exchange_index = sd->equip_switch_index[i];
  10584. if( exchange_index >= 0 && all_position & equip_bitmask[i] ){
  10585. struct item* exchange_item = &sd->inventory.u.items_inventory[exchange_index];
  10586. // Store the target position
  10587. int exchange_position = exchange_item->equipSwitch;
  10588. // Remove the item from equip switch
  10589. pc_equipswitch_remove( sd, exchange_index );
  10590. // Equip the item at the destinated position
  10591. pc_equipitem( sd, exchange_index, exchange_position );
  10592. }
  10593. }
  10594. // Place all unequipped items into the equip switch window
  10595. for( std::pair<int, int> pair : unequipped ){
  10596. int unequipped_index = pair.first;
  10597. int unequipped_position = pair.second;
  10598. // Rebuild the index cache
  10599. for( int i = 0; i < EQI_MAX; i++ ){
  10600. if( unequipped_position & equip_bitmask[i] ){
  10601. sd->equip_switch_index[i] = unequipped_index;
  10602. }
  10603. }
  10604. // Set the correct position mask
  10605. sd->inventory.u.items_inventory[unequipped_index].equipSwitch = unequipped_position;
  10606. // Notify the client
  10607. clif_equipswitch_add( sd, unequipped_index, unequipped_position, ITEM_EQUIP_ACK_OK );
  10608. }
  10609. return all_position;
  10610. }
  10611. }
  10612. void pc_equipswitch_remove( map_session_data* sd, int index ){
  10613. struct item* item = &sd->inventory.u.items_inventory[index];
  10614. if( !item->equipSwitch ){
  10615. return;
  10616. }
  10617. for( int i = 0; i < EQI_MAX; i++ ){
  10618. // If a match is found
  10619. if( sd->equip_switch_index[i] == index ){
  10620. // Remove it from the slot
  10621. sd->equip_switch_index[i] = -1;
  10622. }
  10623. }
  10624. // Send out one packet for all slots using the current item's mask
  10625. clif_equipswitch_remove( sd, index, item->equipSwitch, false );
  10626. item->equipSwitch = 0;
  10627. }
  10628. /*==========================================
  10629. * Checking if player (sd) has an invalid item
  10630. * and is unequiped on map load (item_noequip)
  10631. *------------------------------------------*/
  10632. void pc_checkitem(map_session_data *sd) {
  10633. int i, calc_flag = 0;
  10634. struct item* it;
  10635. nullpo_retv(sd);
  10636. if( sd->state.vending ) //Avoid reorganizing items when we are vending, as that leads to exploits (pointed out by End of Exam)
  10637. return;
  10638. pc_check_available_item(sd, ITMCHK_NONE); // Check for invalid(ated) items.
  10639. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10640. it = &sd->inventory.u.items_inventory[i];
  10641. if( it->nameid == 0 )
  10642. continue;
  10643. if( !it->equip )
  10644. continue;
  10645. if( it->equip&~pc_equippoint(sd,i) ) {
  10646. pc_unequipitem(sd, i, 2);
  10647. calc_flag = 1;
  10648. continue;
  10649. }
  10650. 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) ) {
  10651. pc_unequipitem(sd, i, 2);
  10652. calc_flag = 1;
  10653. continue;
  10654. }
  10655. }
  10656. for( i = 0; i < MAX_INVENTORY; i++ ) {
  10657. it = &sd->inventory.u.items_inventory[i];
  10658. if( it->nameid == 0 )
  10659. continue;
  10660. if( !it->equipSwitch )
  10661. continue;
  10662. if( it->equipSwitch&~pc_equippoint(sd,i) ||
  10663. ( !pc_has_permission(sd, PC_PERM_USE_ALL_EQUIPMENT) && !battle_config.allow_equip_restricted_item && itemdb_isNoEquip(sd->inventory_data[i], sd->bl.m) ) ){
  10664. for( int j = 0; j < EQI_MAX; j++ ){
  10665. if( sd->equip_switch_index[j] == i ){
  10666. sd->equip_switch_index[j] = -1;
  10667. }
  10668. }
  10669. sd->inventory.u.items_inventory[i].equipSwitch = 0;
  10670. continue;
  10671. }
  10672. }
  10673. if( calc_flag && sd->state.active ) {
  10674. pc_checkallowskill(sd);
  10675. status_calc_pc(sd,SCO_NONE);
  10676. }
  10677. }
  10678. /*==========================================
  10679. * Checks for unavailable items and removes them.
  10680. * @param sd: Player data
  10681. * @param type Forced check:
  10682. * 1 - Inventory
  10683. * 2 - Cart
  10684. * 4 - Storage
  10685. *------------------------------------------*/
  10686. void pc_check_available_item(map_session_data *sd, uint8 type)
  10687. {
  10688. int i;
  10689. t_itemid nameid;
  10690. char output[256];
  10691. nullpo_retv(sd);
  10692. if (battle_config.item_check&ITMCHK_INVENTORY && type&ITMCHK_INVENTORY) { // Check for invalid(ated) items in inventory.
  10693. for(i = 0; i < MAX_INVENTORY; i++) {
  10694. nameid = sd->inventory.u.items_inventory[i].nameid;
  10695. if (!nameid)
  10696. continue;
  10697. if (!itemdb_available(nameid)) {
  10698. sprintf(output, msg_txt(sd, 709), nameid); // Item %u has been removed from your inventory.
  10699. clif_displaymessage(sd->fd, output);
  10700. 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);
  10701. pc_delitem(sd, i, sd->inventory.u.items_inventory[i].amount, 4, 0, LOG_TYPE_OTHER);
  10702. continue;
  10703. }
  10704. if (!sd->inventory.u.items_inventory[i].unique_id && !itemdb_isstackable(nameid))
  10705. sd->inventory.u.items_inventory[i].unique_id = pc_generate_unique_id(sd);
  10706. }
  10707. }
  10708. if (battle_config.item_check&ITMCHK_CART && type&ITMCHK_CART) { // Check for invalid(ated) items in cart.
  10709. for(i = 0; i < MAX_CART; i++) {
  10710. nameid = sd->cart.u.items_cart[i].nameid;
  10711. if (!nameid)
  10712. continue;
  10713. if (!itemdb_available(nameid)) {
  10714. sprintf(output, msg_txt(sd, 710), nameid); // Item %u has been removed from your cart.
  10715. clif_displaymessage(sd->fd, output);
  10716. 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);
  10717. pc_cart_delitem(sd, i, sd->cart.u.items_cart[i].amount, 0, LOG_TYPE_OTHER);
  10718. continue;
  10719. }
  10720. if (!sd->cart.u.items_cart[i].unique_id && !itemdb_isstackable(nameid))
  10721. sd->cart.u.items_cart[i].unique_id = pc_generate_unique_id(sd);
  10722. }
  10723. }
  10724. if (battle_config.item_check&ITMCHK_STORAGE && type&ITMCHK_STORAGE) { // Check for invalid(ated) items in storage.
  10725. for(i = 0; i < sd->storage.max_amount; i++) {
  10726. nameid = sd->storage.u.items_storage[i].nameid;
  10727. if (!nameid)
  10728. continue;
  10729. if (!itemdb_available(nameid)) {
  10730. sprintf(output, msg_txt(sd, 711), nameid); // Item %u has been removed from your storage.
  10731. clif_displaymessage(sd->fd, output);
  10732. 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);
  10733. storage_delitem(sd, &sd->storage, i, sd->storage.u.items_storage[i].amount);
  10734. continue;
  10735. }
  10736. if (!sd->storage.u.items_storage[i].unique_id && !itemdb_isstackable(nameid))
  10737. sd->storage.u.items_storage[i].unique_id = pc_generate_unique_id(sd);
  10738. }
  10739. }
  10740. }
  10741. /*==========================================
  10742. * Update PVP rank for sd1 in cmp to sd2
  10743. *------------------------------------------*/
  10744. static int pc_calc_pvprank_sub(struct block_list *bl,va_list ap)
  10745. {
  10746. map_session_data *sd1,*sd2;
  10747. sd1=(map_session_data *)bl;
  10748. sd2=va_arg(ap,map_session_data *);
  10749. if( pc_isinvisible(sd1) || pc_isinvisible(sd2) )
  10750. {// cannot register pvp rank for hidden GMs
  10751. return 0;
  10752. }
  10753. if( sd1->pvp_point > sd2->pvp_point )
  10754. sd2->pvp_rank++;
  10755. return 0;
  10756. }
  10757. /*==========================================
  10758. * Calculate new rank beetween all present players (map_foreachinallarea)
  10759. * and display result
  10760. *------------------------------------------*/
  10761. int pc_calc_pvprank(map_session_data *sd)
  10762. {
  10763. int old = sd->pvp_rank;
  10764. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  10765. sd->pvp_rank=1;
  10766. map_foreachinmap(pc_calc_pvprank_sub,sd->bl.m,BL_PC,sd);
  10767. if(old!=sd->pvp_rank || sd->pvp_lastusers!=mapdata->users_pvp)
  10768. clif_pvpset(sd,sd->pvp_rank,sd->pvp_lastusers=mapdata->users_pvp,0);
  10769. return sd->pvp_rank;
  10770. }
  10771. /*==========================================
  10772. * Calculate next sd ranking calculation from config
  10773. *------------------------------------------*/
  10774. TIMER_FUNC(pc_calc_pvprank_timer){
  10775. map_session_data *sd;
  10776. sd=map_id2sd(id);
  10777. if(sd==NULL)
  10778. return 0;
  10779. sd->pvp_timer = INVALID_TIMER;
  10780. if( pc_isinvisible(sd) )
  10781. {// do not calculate the pvp rank for a hidden GM
  10782. return 0;
  10783. }
  10784. if( pc_calc_pvprank(sd) > 0 )
  10785. sd->pvp_timer = add_timer(gettick()+PVP_CALCRANK_INTERVAL,pc_calc_pvprank_timer,id,data);
  10786. return 0;
  10787. }
  10788. /*==========================================
  10789. * Checking if sd is married
  10790. * Return:
  10791. * partner_id = yes
  10792. * 0 = no
  10793. *------------------------------------------*/
  10794. int pc_ismarried(map_session_data *sd)
  10795. {
  10796. if(sd == NULL)
  10797. return -1;
  10798. if(sd->status.partner_id > 0)
  10799. return sd->status.partner_id;
  10800. else
  10801. return 0;
  10802. }
  10803. /*==========================================
  10804. * Marry player sd to player dstsd
  10805. * Return:
  10806. * false = fail
  10807. * true = success
  10808. *------------------------------------------*/
  10809. bool pc_marriage(map_session_data *sd,map_session_data *dstsd)
  10810. {
  10811. if(sd == NULL || dstsd == NULL ||
  10812. sd->status.partner_id > 0 || dstsd->status.partner_id > 0 ||
  10813. (sd->class_&JOBL_BABY) || (dstsd->class_&JOBL_BABY))
  10814. return false;
  10815. sd->status.partner_id = dstsd->status.char_id;
  10816. dstsd->status.partner_id = sd->status.char_id;
  10817. achievement_update_objective(sd, AG_MARRY, 1, 1);
  10818. achievement_update_objective(dstsd, AG_MARRY, 1, 1);
  10819. return true;
  10820. }
  10821. /*==========================================
  10822. * Divorce sd from its partner
  10823. * Return:
  10824. * false = fail
  10825. * true = success
  10826. *------------------------------------------*/
  10827. bool pc_divorce(map_session_data *sd)
  10828. {
  10829. map_session_data *p_sd;
  10830. int i;
  10831. if( sd == NULL || !pc_ismarried(sd) )
  10832. return false;
  10833. if( !sd->status.partner_id )
  10834. return false; // Char is not married
  10835. if( (p_sd = map_charid2sd(sd->status.partner_id)) == NULL )
  10836. { // Lets char server do the divorce
  10837. if( chrif_divorce(sd->status.char_id, sd->status.partner_id) )
  10838. return false; // No char server connected
  10839. return true;
  10840. }
  10841. // Both players online, lets do the divorce manually
  10842. sd->status.partner_id = 0;
  10843. p_sd->status.partner_id = 0;
  10844. for( i = 0; i < MAX_INVENTORY; i++ )
  10845. {
  10846. if( sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10847. pc_delitem(sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10848. if( p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_M || p_sd->inventory.u.items_inventory[i].nameid == WEDDING_RING_F )
  10849. pc_delitem(p_sd, i, 1, 0, 0, LOG_TYPE_OTHER);
  10850. }
  10851. clif_divorced(sd, p_sd->status.name);
  10852. clif_divorced(p_sd, sd->status.name);
  10853. return true;
  10854. }
  10855. /**
  10856. * Get the partner map_session_data of a player
  10857. * @param sd : the husband|wife session
  10858. * @return partner session or NULL
  10859. */
  10860. map_session_data *pc_get_partner(map_session_data *sd){
  10861. if (!sd || !pc_ismarried(sd))
  10862. return NULL;
  10863. return map_charid2sd(sd->status.partner_id);
  10864. }
  10865. /**
  10866. * Get the father map_session_data of a player
  10867. * @param sd : the baby session
  10868. * @return father session or NULL
  10869. */
  10870. map_session_data *pc_get_father (map_session_data *sd){
  10871. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.father)
  10872. return NULL;
  10873. return map_charid2sd(sd->status.father);
  10874. }
  10875. /**
  10876. * Get the mother map_session_data of a player
  10877. * @param sd : the baby session
  10878. * @return mother session or NULL
  10879. */
  10880. map_session_data *pc_get_mother (map_session_data *sd){
  10881. if (!sd || !(sd->class_&JOBL_BABY) || !sd->status.mother)
  10882. return NULL;
  10883. return map_charid2sd(sd->status.mother);
  10884. }
  10885. /*==========================================
  10886. * Get sd children charid. (Need to be married)
  10887. *------------------------------------------*/
  10888. map_session_data *pc_get_child (map_session_data *sd)
  10889. {
  10890. if (!sd || !pc_ismarried(sd) || !sd->status.child)
  10891. // charid2sd returns NULL if not found
  10892. return NULL;
  10893. return map_charid2sd(sd->status.child);
  10894. }
  10895. /*==========================================
  10896. * Set player sd to bleed. (losing hp and/or sp each diff_tick)
  10897. *------------------------------------------*/
  10898. void pc_bleeding (map_session_data *sd, t_tick diff_tick)
  10899. {
  10900. int hp = 0, sp = 0;
  10901. if( pc_isdead(sd) )
  10902. return;
  10903. if (sd->hp_loss.value) {
  10904. sd->hp_loss.tick += diff_tick;
  10905. while (sd->hp_loss.tick >= sd->hp_loss.rate) {
  10906. hp += sd->hp_loss.value;
  10907. sd->hp_loss.tick -= sd->hp_loss.rate;
  10908. }
  10909. if(hp >= sd->battle_status.hp)
  10910. hp = sd->battle_status.hp-1; //Script drains cannot kill you.
  10911. }
  10912. if (sd->sp_loss.value) {
  10913. sd->sp_loss.tick += diff_tick;
  10914. while (sd->sp_loss.tick >= sd->sp_loss.rate) {
  10915. sp += sd->sp_loss.value;
  10916. sd->sp_loss.tick -= sd->sp_loss.rate;
  10917. }
  10918. }
  10919. if (hp > 0 || sp > 0)
  10920. status_zap(&sd->bl, hp, sp);
  10921. }
  10922. //Character regen. Flag is used to know which types of regen can take place.
  10923. //&1: HP regen
  10924. //&2: SP regen
  10925. void pc_regen (map_session_data *sd, t_tick diff_tick)
  10926. {
  10927. int hp = 0, sp = 0;
  10928. if (sd->hp_regen.value) {
  10929. sd->hp_regen.tick += diff_tick;
  10930. while (sd->hp_regen.tick >= sd->hp_regen.rate) {
  10931. hp += sd->hp_regen.value;
  10932. sd->hp_regen.tick -= sd->hp_regen.rate;
  10933. }
  10934. }
  10935. if (sd->sp_regen.value) {
  10936. sd->sp_regen.tick += diff_tick;
  10937. while (sd->sp_regen.tick >= sd->sp_regen.rate) {
  10938. sp += sd->sp_regen.value;
  10939. sd->sp_regen.tick -= sd->sp_regen.rate;
  10940. }
  10941. }
  10942. if (sd->percent_hp_regen.value) {
  10943. sd->percent_hp_regen.tick += diff_tick;
  10944. while (sd->percent_hp_regen.tick >= sd->percent_hp_regen.rate) {
  10945. hp += sd->status.max_hp * sd->percent_hp_regen.value / 100;
  10946. sd->percent_hp_regen.tick -= sd->percent_hp_regen.rate;
  10947. }
  10948. }
  10949. if (sd->percent_sp_regen.value) {
  10950. sd->percent_sp_regen.tick += diff_tick;
  10951. while (sd->percent_sp_regen.tick >= sd->percent_sp_regen.rate) {
  10952. sp += sd->status.max_sp * sd->percent_sp_regen.value / 100;
  10953. sd->percent_sp_regen.tick -= sd->percent_sp_regen.rate;
  10954. }
  10955. }
  10956. if (hp > 0 || sp > 0)
  10957. status_heal(&sd->bl, hp, sp, 0);
  10958. }
  10959. /*==========================================
  10960. * Memo player sd savepoint. (map,x,y)
  10961. *------------------------------------------*/
  10962. void pc_setsavepoint(map_session_data *sd, short mapindex,int x,int y)
  10963. {
  10964. nullpo_retv(sd);
  10965. safestrncpy( sd->status.save_point.map, mapindex_id2name( mapindex ), sizeof( sd->status.save_point.map ) );
  10966. sd->status.save_point.x = x;
  10967. sd->status.save_point.y = y;
  10968. }
  10969. /*==========================================
  10970. * Save 1 player data at autosave interval
  10971. *------------------------------------------*/
  10972. static TIMER_FUNC(pc_autosave){
  10973. int interval;
  10974. struct s_mapiterator* iter;
  10975. map_session_data* sd;
  10976. static int last_save_id = 0, save_flag = 0;
  10977. if(save_flag == 2) //Someone was saved on last call, normal cycle
  10978. save_flag = 0;
  10979. else
  10980. save_flag = 1; //Noone was saved, so save first found char.
  10981. iter = mapit_getallusers();
  10982. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  10983. {
  10984. if (!sd->state.pc_loaded) // Player data hasn't fully loaded
  10985. continue;
  10986. if(sd->bl.id == last_save_id && save_flag != 1) {
  10987. save_flag = 1;
  10988. continue;
  10989. }
  10990. if(save_flag != 1) //Not our turn to save yet.
  10991. continue;
  10992. //Save char.
  10993. last_save_id = sd->bl.id;
  10994. save_flag = 2;
  10995. if (pc_isvip(sd)) // Check if we're still VIP
  10996. chrif_req_login_operation(1, sd->status.name, CHRIF_OP_LOGIN_VIP, 0, 1, 0);
  10997. chrif_save(sd, CSAVE_INVENTORY|CSAVE_CART);
  10998. break;
  10999. }
  11000. mapit_free(iter);
  11001. interval = autosave_interval/(map_usercount()+1);
  11002. if(interval < minsave_interval)
  11003. interval = minsave_interval;
  11004. add_timer(gettick()+interval,pc_autosave,0,0);
  11005. return 0;
  11006. }
  11007. static int pc_daynight_timer_sub(map_session_data *sd,va_list ap)
  11008. {
  11009. if (sd->state.night != night_flag && map_getmapflag(sd->bl.m, MF_NIGHTENABLED))
  11010. { //Night/day state does not match.
  11011. clif_status_load(&sd->bl, EFST_SKE, night_flag); //New night effect by dynamix [Skotlex]
  11012. sd->state.night = night_flag;
  11013. return 1;
  11014. }
  11015. return 0;
  11016. }
  11017. /*================================================
  11018. * timer to do the day [Yor]
  11019. * data: 0 = called by timer, 1 = gmcommand/script
  11020. *------------------------------------------------*/
  11021. TIMER_FUNC(map_day_timer){
  11022. char tmp_soutput[1024];
  11023. if (data == 0 && battle_config.day_duration <= 0) // if we want a day
  11024. return 0;
  11025. if (!night_flag)
  11026. return 0; //Already day.
  11027. night_flag = 0; // 0=day, 1=night [Yor]
  11028. map_foreachpc(pc_daynight_timer_sub);
  11029. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,502) : msg_txt(NULL,60)); // The day has arrived!
  11030. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11031. return 0;
  11032. }
  11033. /*================================================
  11034. * timer to do the night [Yor]
  11035. * data: 0 = called by timer, 1 = gmcommand/script
  11036. *------------------------------------------------*/
  11037. TIMER_FUNC(map_night_timer){
  11038. char tmp_soutput[1024];
  11039. if (data == 0 && battle_config.night_duration <= 0) // if we want a night
  11040. return 0;
  11041. if (night_flag)
  11042. return 0; //Already nigth.
  11043. night_flag = 1; // 0=day, 1=night [Yor]
  11044. map_foreachpc(pc_daynight_timer_sub);
  11045. strcpy(tmp_soutput, (data == 0) ? msg_txt(NULL,503) : msg_txt(NULL,59)); // The night has fallen...
  11046. intif_broadcast(tmp_soutput, strlen(tmp_soutput) + 1, BC_DEFAULT);
  11047. return 0;
  11048. }
  11049. /**
  11050. * Attempt to stand up a player
  11051. * @param sd
  11052. * @param force Ignore the check, ask player to stand up. Used in some cases like pc_damage(), pc_revive(), etc
  11053. * @return True if success, Fals if failed
  11054. */
  11055. bool pc_setstand(map_session_data *sd, bool force){
  11056. nullpo_ret(sd);
  11057. // Cannot stand yet
  11058. // TODO: Move to SCS_NOSTAND [Cydh]
  11059. if (!force && (sd->sc.getSCE(SC_SITDOWN_FORCE) || sd->sc.getSCE(SC_BANANA_BOMB_SITDOWN)))
  11060. return false;
  11061. status_change_end(&sd->bl, SC_TENSIONRELAX);
  11062. clif_status_load(&sd->bl,EFST_SIT,0);
  11063. clif_standing(&sd->bl); //Inform area PC is standing
  11064. //Reset sitting tick.
  11065. sd->ssregen.tick.hp = sd->ssregen.tick.sp = 0;
  11066. if( pc_isdead( sd ) ){
  11067. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11068. clif_party_dead( *sd );
  11069. }else{
  11070. sd->state.dead_sit = sd->vd.dead_sit = 0;
  11071. }
  11072. return true;
  11073. }
  11074. /**
  11075. * Calculate Overheat value
  11076. * @param sd: Player data
  11077. * @param heat: Amount of Heat to adjust
  11078. **/
  11079. void pc_overheat(map_session_data &sd, int16 heat) {
  11080. status_change_entry *sce = sd.sc.getSCE(SC_OVERHEAT_LIMITPOINT);
  11081. if (sce) {
  11082. sce->val1 += heat;
  11083. sce->val1 = cap_value(sce->val1, 0, 1000);
  11084. if (heat < 0 && sce->val1 == 0) { // Cooling device used.
  11085. status_change_end(&sd.bl, SC_OVERHEAT_LIMITPOINT);
  11086. status_change_end(&sd.bl, SC_OVERHEAT);
  11087. }
  11088. } else if (heat > 0)
  11089. sc_start(&sd.bl, &sd.bl, SC_OVERHEAT_LIMITPOINT, 100, heat, 1000);
  11090. }
  11091. /**
  11092. * Check if player is autolooting given itemID.
  11093. */
  11094. bool pc_isautolooting(map_session_data *sd, t_itemid nameid)
  11095. {
  11096. uint8 i = 0;
  11097. if (sd->state.autoloottype && sd->state.autoloottype&(1<<itemdb_type(nameid)))
  11098. return true;
  11099. if (!sd->state.autolooting)
  11100. return false;
  11101. if (sd->state.autolooting)
  11102. ARR_FIND(0, AUTOLOOTITEM_SIZE, i, sd->state.autolootid[i] == nameid);
  11103. return (i != AUTOLOOTITEM_SIZE);
  11104. }
  11105. /**
  11106. * Checks if player can use @/#command
  11107. * @param sd Player map session data
  11108. * @param command Command name without @/# and params
  11109. * @param type is it atcommand or charcommand
  11110. */
  11111. bool pc_can_use_command( map_session_data *sd, const char *command, AtCommandType type ){
  11112. return sd->group->can_use_command( command, type );
  11113. }
  11114. bool pc_has_permission( map_session_data* sd, e_pc_permission permission ){
  11115. return sd->permissions.test( permission );
  11116. }
  11117. /**
  11118. * Checks if commands used by a player should be logged
  11119. * according to their group setting.
  11120. * @param sd Player map session data
  11121. */
  11122. bool pc_should_log_commands( map_session_data *sd ){
  11123. return sd->group->log_commands;
  11124. }
  11125. /**
  11126. * Spirit Charm expiration timer.
  11127. * @see TimerFunc
  11128. */
  11129. static TIMER_FUNC(pc_spiritcharm_timer){
  11130. map_session_data *sd;
  11131. int i;
  11132. if ((sd = (map_session_data *)map_id2sd(id)) == NULL || sd->bl.type != BL_PC)
  11133. return 1;
  11134. if (sd->spiritcharm <= 0) {
  11135. 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);
  11136. sd->spiritcharm = 0;
  11137. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11138. return 0;
  11139. }
  11140. ARR_FIND(0, sd->spiritcharm, i, sd->spiritcharm_timer[i] == tid);
  11141. if (i == sd->spiritcharm) {
  11142. ShowError("pc_spiritcharm_timer: timer not found (aid=%d cid=%d tid=%d)\n", sd->status.account_id, sd->status.char_id, tid);
  11143. return 0;
  11144. }
  11145. sd->spiritcharm--;
  11146. if (i != sd->spiritcharm)
  11147. memmove(sd->spiritcharm_timer + i, sd->spiritcharm_timer + i + 1, (sd->spiritcharm - i) * sizeof(int));
  11148. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11149. if (sd->spiritcharm <= 0)
  11150. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11151. clif_spiritcharm(sd);
  11152. return 0;
  11153. }
  11154. /**
  11155. * Adds a spirit charm.
  11156. * @param sd: Target character
  11157. * @param interval: Duration
  11158. * @param max: Maximum amount of charms to add
  11159. * @param type: Charm type (@see spirit_charm_types)
  11160. */
  11161. void pc_addspiritcharm(map_session_data *sd, int interval, int max, int type)
  11162. {
  11163. int tid, i;
  11164. nullpo_retv(sd);
  11165. if (sd->spiritcharm_type != CHARM_TYPE_NONE && type != sd->spiritcharm_type)
  11166. pc_delspiritcharm(sd, sd->spiritcharm, sd->spiritcharm_type);
  11167. if (max > MAX_SPIRITCHARM)
  11168. max = MAX_SPIRITCHARM;
  11169. if (sd->spiritcharm < 0)
  11170. sd->spiritcharm = 0;
  11171. if (sd->spiritcharm && sd->spiritcharm >= max) {
  11172. if (sd->spiritcharm_timer[0] != INVALID_TIMER)
  11173. delete_timer(sd->spiritcharm_timer[0], pc_spiritcharm_timer);
  11174. sd->spiritcharm--;
  11175. if (sd->spiritcharm != 0)
  11176. memmove(sd->spiritcharm_timer + 0, sd->spiritcharm_timer + 1, (sd->spiritcharm) * sizeof(int));
  11177. sd->spiritcharm_timer[sd->spiritcharm] = INVALID_TIMER;
  11178. }
  11179. tid = add_timer(gettick() + interval, pc_spiritcharm_timer, sd->bl.id, 0);
  11180. 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);
  11181. if (i != sd->spiritcharm)
  11182. memmove(sd->spiritcharm_timer + i + 1, sd->spiritcharm_timer + i, (sd->spiritcharm - i) * sizeof(int));
  11183. sd->spiritcharm_timer[i] = tid;
  11184. sd->spiritcharm++;
  11185. sd->spiritcharm_type = type;
  11186. clif_spiritcharm(sd);
  11187. }
  11188. /**
  11189. * Removes one or more spirit charms.
  11190. * @param sd: The target character
  11191. * @param count: Amount of charms to remove
  11192. * @param type: Type of charm to remove
  11193. */
  11194. void pc_delspiritcharm(map_session_data *sd, int count, int type)
  11195. {
  11196. int i;
  11197. nullpo_retv(sd);
  11198. if (sd->spiritcharm_type != type)
  11199. return;
  11200. if (sd->spiritcharm <= 0) {
  11201. sd->spiritcharm = 0;
  11202. return;
  11203. }
  11204. if (count <= 0)
  11205. return;
  11206. if (count > sd->spiritcharm)
  11207. count = sd->spiritcharm;
  11208. sd->spiritcharm -= count;
  11209. if (count > MAX_SPIRITCHARM)
  11210. count = MAX_SPIRITCHARM;
  11211. for (i = 0; i < count; i++) {
  11212. if (sd->spiritcharm_timer[i] != INVALID_TIMER) {
  11213. delete_timer(sd->spiritcharm_timer[i], pc_spiritcharm_timer);
  11214. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11215. }
  11216. }
  11217. for (i = count; i < MAX_SPIRITCHARM; i++) {
  11218. sd->spiritcharm_timer[i - count] = sd->spiritcharm_timer[i];
  11219. sd->spiritcharm_timer[i] = INVALID_TIMER;
  11220. }
  11221. if (sd->spiritcharm <= 0)
  11222. sd->spiritcharm_type = CHARM_TYPE_NONE;
  11223. clif_spiritcharm(sd);
  11224. }
  11225. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  11226. /**
  11227. * Renewal EXP/Item Drop rate modifier based on level penalty
  11228. * @param level_diff: Monster and Player level difference
  11229. * @param mob_class: Monster class
  11230. * @param mode: Monster mode
  11231. * @param type: 1 - EXP, 2 - Item Drop
  11232. * @return Penalty rate
  11233. */
  11234. uint16 pc_level_penalty_mod( map_session_data* sd, e_penalty_type type, std::shared_ptr<s_mob_db> mob, mob_data* md ){
  11235. // No player was attached, we don't use any modifier (100 = rates are not touched)
  11236. if( sd == nullptr ){
  11237. return 100;
  11238. }
  11239. if ((type == PENALTY_DROP && map_getmapflag(sd->bl.m, MF_NORENEWALDROPPENALTY)) || (type == PENALTY_EXP && map_getmapflag(sd->bl.m, MF_NORENEWALEXPPENALTY))) {
  11240. return 100;
  11241. }
  11242. int monster_level;
  11243. if( md != nullptr ){
  11244. monster_level = md->level;
  11245. mob = md->db;
  11246. }else if( mob != nullptr ){
  11247. monster_level = mob->lv;
  11248. }else{
  11249. return 100;
  11250. }
  11251. if( ( type == PENALTY_DROP || type == PENALTY_MVP_DROP ) && status_has_mode( &mob->status, MD_FIXEDITEMDROP ) ){
  11252. return 100;
  11253. }
  11254. int level_difference = monster_level - sd->status.base_level;
  11255. std::shared_ptr<s_penalty> penalty = penalty_db.find( type );
  11256. if( penalty != nullptr ){
  11257. return penalty->rate[ level_difference + MAX_LEVEL - 1 ];
  11258. }else{
  11259. return 100;
  11260. }
  11261. }
  11262. #endif
  11263. int pc_split_str(char *str,char **val,int num)
  11264. {
  11265. int i;
  11266. for (i=0; i<num && str; i++){
  11267. val[i] = str;
  11268. str = strchr(str,',');
  11269. if (str && i<num-1) //Do not remove a trailing comma.
  11270. *str++=0;
  11271. }
  11272. return i;
  11273. }
  11274. int pc_split_atoui(char* str, unsigned int* val, char sep, int max)
  11275. {
  11276. static int warning=0;
  11277. int i,j;
  11278. for (i=0; i<max; i++) {
  11279. double f;
  11280. if (!str) break;
  11281. f = atof(str);
  11282. if (f < 0)
  11283. val[i] = 0;
  11284. else if (f > UINT_MAX) {
  11285. val[i] = UINT_MAX;
  11286. if (!warning) {
  11287. warning = 1;
  11288. ShowWarning("pc_readdb (exp.txt): Required exp per level is capped to %u\n", UINT_MAX);
  11289. }
  11290. } else
  11291. val[i] = (unsigned int)f;
  11292. str = strchr(str,sep);
  11293. if (str)
  11294. *str++=0;
  11295. }
  11296. //Zero up the remaining.
  11297. for(j=i; j < max; j++)
  11298. val[j] = 0;
  11299. return i;
  11300. }
  11301. std::shared_ptr<s_skill_tree_entry> SkillTreeDatabase::get_skill_data(int class_, uint16 skill_id) {
  11302. std::shared_ptr<s_skill_tree> tree = this->find(class_);
  11303. if (tree != nullptr)
  11304. return util::umap_find(tree->skills, skill_id);
  11305. return nullptr;
  11306. }
  11307. const std::string SkillTreeDatabase::getDefaultLocation() {
  11308. return std::string(db_path) + "/skill_tree.yml";
  11309. }
  11310. /**
  11311. * Reads and parses an entry from the skill_tree.
  11312. * @param node: YAML node containing the entry.
  11313. * @return count of successfully parsed rows
  11314. */
  11315. uint64 SkillTreeDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11316. std::string job_name;
  11317. if (!this->asString(node, "Job", job_name))
  11318. return 0;
  11319. int64 constant;
  11320. std::string job_name_constant = "JOB_" + job_name;
  11321. if (!script_get_constant(job_name_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11322. this->invalidWarning(node["Job"], "Invalid job %s.\n", job_name.c_str());
  11323. return 0;
  11324. }
  11325. uint16 job_id = static_cast<uint16>(constant);
  11326. std::shared_ptr<s_skill_tree> tree = this->find(job_id);
  11327. bool exists = tree != nullptr;
  11328. if (!exists)
  11329. tree = std::make_shared<s_skill_tree>();
  11330. if (this->nodeExists(node, "Inherit")) {
  11331. const ryml::NodeRef& InheritNode = node["Inherit"];
  11332. for (const auto &Inheritit : InheritNode) {
  11333. std::string inheritname;
  11334. c4::from_chars(Inheritit.key(), &inheritname);
  11335. std::string inheritname_constant = "JOB_" + inheritname;
  11336. if (!script_get_constant(inheritname_constant.c_str(), &constant) || !pcdb_checkid(constant)) {
  11337. this->invalidWarning(InheritNode[Inheritit.key()], "Invalid job %s.\n", inheritname.c_str());
  11338. return 0;
  11339. }
  11340. bool active;
  11341. if (!this->asBool(InheritNode, inheritname, active))
  11342. return 0;
  11343. uint16 inherit_job = static_cast<uint16>(constant);
  11344. if (!active) {
  11345. if (exists)
  11346. util::vector_erase_if_exists(tree->inherit_job, inherit_job);
  11347. }
  11348. else {
  11349. if (!util::vector_exists(tree->inherit_job, inherit_job))
  11350. tree->inherit_job.push_back(inherit_job);
  11351. }
  11352. }
  11353. }
  11354. if (this->nodeExists(node, "Tree")) {
  11355. for (const auto &it : node["Tree"]) {
  11356. std::string skill_name;
  11357. if (!this->asString(it, "Name", skill_name))
  11358. return 0;
  11359. uint16 skill_id = skill_name2id(skill_name.c_str());
  11360. if (skill_id == 0) {
  11361. this->invalidWarning(it["Name"], "Invalid skill name \"%s\".\n", skill_name.c_str());
  11362. return 0;
  11363. }
  11364. if (!skill_get_index(skill_id)) {
  11365. this->invalidWarning(it["Name"], "Unable to load skill %s into job %hu's tree.\n", skill_name.c_str(), job_id);
  11366. return 0;
  11367. }
  11368. std::shared_ptr<s_skill_tree_entry> entry;
  11369. bool skill_exists = tree->skills.count(skill_id) > 0;
  11370. if (skill_exists)
  11371. entry = tree->skills[skill_id];
  11372. else
  11373. entry = std::make_shared<s_skill_tree_entry>();
  11374. entry->skill_id = skill_id;
  11375. uint16 max_lv;
  11376. if (!this->asUInt16(it, "MaxLevel", max_lv))
  11377. return 0;
  11378. if (max_lv > MAX_SKILL_LEVEL) {
  11379. this->invalidWarning(it["MaxLevel"], "MaxLevel exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11380. return 0;
  11381. }
  11382. uint16 skill_lv_max = skill_get_max(skill_id);
  11383. if (max_lv > skill_lv_max) {
  11384. 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);
  11385. max_lv = skill_lv_max;
  11386. }
  11387. // if (max_lv == 0) { // skill lvl 0 removed on loadingFinished (because of inherit)
  11388. // if (!skill_exists || entry->skill_id.erase(skill_id) == 0)
  11389. // 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());
  11390. // continue;
  11391. // }
  11392. entry->max_lv = max_lv;
  11393. if (this->nodeExists(it, "BaseLevel")) {
  11394. uint32 baselv;
  11395. if (!this->asUInt32(it, "BaseLevel", baselv))
  11396. return 0;
  11397. uint32 baselv_max = job_db.get_maxBaseLv(job_id);
  11398. if (baselv > baselv_max) {
  11399. this->invalidWarning(it["BaseLevel"], "Skill %hu's base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11400. skill_id, baselv, job_name.c_str(), baselv_max);
  11401. baselv = baselv_max;
  11402. }
  11403. entry->baselv = baselv;
  11404. } else {
  11405. if (!skill_exists)
  11406. entry->baselv = 0;
  11407. }
  11408. if (this->nodeExists(it, "JobLevel")) {
  11409. uint32 joblv;
  11410. if (!this->asUInt32(it, "JobLevel", joblv))
  11411. return 0;
  11412. uint32 joblv_max = job_db.get_maxJobLv(job_id);
  11413. if (joblv > joblv_max) {
  11414. this->invalidWarning(it["JobLevel"], "Skill %hu's job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11415. skill_id, joblv, job_name.c_str(), joblv_max);
  11416. joblv = joblv_max;
  11417. }
  11418. entry->joblv = joblv;
  11419. } else {
  11420. if (!skill_exists)
  11421. entry->joblv = 0;
  11422. }
  11423. if (this->nodeExists(it, "Requires")) {
  11424. for (const auto &Requiresit : it["Requires"]) {
  11425. if (!this->nodesExist(Requiresit, { "Name" }))
  11426. return 0;
  11427. std::string skill_name_req;
  11428. if (!this->asString(Requiresit, "Name", skill_name_req))
  11429. return 0;
  11430. uint16 skill_id_req = skill_name2id(skill_name_req.c_str());
  11431. if (skill_id_req == 0) {
  11432. this->invalidWarning(Requiresit["Name"], "Invalid skill name \"%s\".\n", skill_name_req.c_str());
  11433. return 0;
  11434. }
  11435. uint16 lv_req;
  11436. if (!this->asUInt16(Requiresit, "Level", lv_req))
  11437. return 0;
  11438. if (lv_req > MAX_SKILL_LEVEL) {
  11439. this->invalidWarning(Requiresit["Level"], "Level exceeds the maximum skill level of %d, skipping.\n", MAX_SKILL_LEVEL);
  11440. return 0;
  11441. }
  11442. uint16 lv_req_max = skill_get_max(skill_id_req);
  11443. if (lv_req > lv_req_max) {
  11444. 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);
  11445. lv_req = lv_req_max;
  11446. }
  11447. if (lv_req == 0) {
  11448. if (entry->need.erase(skill_id_req) == 0)
  11449. 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());
  11450. continue;
  11451. }
  11452. entry->need[skill_id_req] = lv_req;
  11453. }
  11454. }
  11455. if (this->nodeExists(it, "Exclude")) {
  11456. bool exclude;
  11457. if (!this->asBool(it, "Exclude", exclude))
  11458. return 0;
  11459. entry->exclude_inherit = exclude;
  11460. } else {
  11461. if (!skill_exists)
  11462. entry->exclude_inherit = false;
  11463. }
  11464. if (!skill_exists)
  11465. tree->skills.insert({ skill_id, entry });
  11466. }
  11467. }
  11468. if (!exists)
  11469. this->put(job_id, tree);
  11470. return true;
  11471. }
  11472. void SkillTreeDatabase::loadingFinished() {
  11473. std::unordered_map<uint16, std::shared_ptr<s_skill_tree>> job_tree; // get the data from skill_tree_db before populate it
  11474. for (auto &data : *this) {
  11475. if (data.second->inherit_job.empty())
  11476. continue;
  11477. std::shared_ptr<s_skill_tree> skill_tree = std::make_shared<s_skill_tree>();
  11478. uint32 baselv_max = job_db.get_maxBaseLv(data.first);
  11479. uint32 joblv_max = job_db.get_maxJobLv(data.first);
  11480. for (const auto &inherit_job : data.second->inherit_job) {
  11481. std::shared_ptr<s_skill_tree> tree = this->find(inherit_job);
  11482. if (tree == nullptr || tree->skills.empty())
  11483. continue;
  11484. for (const auto &it : tree->skills) {
  11485. if (it.second->exclude_inherit)
  11486. continue;
  11487. if (data.second->skills.count(it.first) > 0) // skill already in the skill tree
  11488. continue;
  11489. if (skill_tree->skills.count(it.first) > 0) // replaced by the last inheritance
  11490. skill_tree->skills[it.first] = it.second;
  11491. else
  11492. skill_tree->skills.insert({ it.first, it.second });
  11493. std::shared_ptr<s_skill_tree_entry> skill = skill_tree->skills[it.first];
  11494. if (skill->baselv > baselv_max) {
  11495. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s base level requirement %hu exceeds job %s's max base level %d. Capping skill base level.\n",
  11496. skill_get_name(skill->skill_id), skill->skill_id, skill->baselv, job_name(data.first), baselv_max);
  11497. skill->baselv = baselv_max;
  11498. }
  11499. if (skill->joblv > joblv_max) {
  11500. ShowWarning("SkillTreeDatabase: Skill %s (%hu)'s job level requirement %hu exceeds job %s's max job level %d. Capping skill job level.\n",
  11501. skill_get_name(skill->skill_id), skill->skill_id, skill->joblv, job_name(data.first), joblv_max);
  11502. skill->joblv = joblv_max;
  11503. }
  11504. }
  11505. }
  11506. if (skill_tree != nullptr && !skill_tree->skills.empty())
  11507. job_tree.insert({ data.first, skill_tree });
  11508. }
  11509. if (!job_tree.empty()) {
  11510. for (auto &data : *this) {
  11511. if (job_tree.count(data.first) == 0)
  11512. continue;
  11513. data.second->skills.insert(job_tree[data.first]->skills.begin(), job_tree[data.first]->skills.end());
  11514. }
  11515. }
  11516. // remove skills with max_lv = 0
  11517. for (const auto &job : *this) {
  11518. if (job.second->skills.empty())
  11519. continue;
  11520. auto it = job.second->skills.begin();
  11521. while( it != job.second->skills.end() ){
  11522. if( it->second->max_lv == 0 ){
  11523. it = job.second->skills.erase( it );
  11524. }else{
  11525. it++;
  11526. }
  11527. }
  11528. }
  11529. TypesafeYamlDatabase::loadingFinished();
  11530. }
  11531. /**
  11532. * Calculates base hp of player. Reference: http://irowiki.org/wiki/Max_HP
  11533. * @param level: Base level of player
  11534. * @param job_id: Job ID @see enum e_job
  11535. * @return base_hp
  11536. * @author [Cydh]
  11537. */
  11538. static unsigned int pc_calc_basehp(uint16 level, uint16 job_id) {
  11539. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11540. double base_hp = 35 + level * (job->hp_increase / 100.);
  11541. #ifndef RENEWAL
  11542. if (level >= 10 && (job_id == JOB_NINJA || job_id == JOB_GUNSLINGER))
  11543. base_hp += 90;
  11544. #endif
  11545. for (uint16 i = 2; i <= level; i++)
  11546. base_hp += floor(((job->hp_factor / 100.) * i) + 0.5); //Don't have round()
  11547. if (job_id == JOB_SUMMONER || job_id == JOB_SPIRIT_HANDLER)
  11548. base_hp += floor((base_hp / 2) + 0.5);
  11549. return (unsigned int)base_hp;
  11550. }
  11551. /**
  11552. * Calculates base sp of player.
  11553. * @param level: Base level of player
  11554. * @param job_id: Job ID @see enum e_job
  11555. * @return base_sp
  11556. * @author [Playtester]
  11557. */
  11558. static unsigned int pc_calc_basesp(uint16 level, uint16 job_id) {
  11559. std::shared_ptr<s_job_info> job = job_db.find(job_id);
  11560. double base_sp = 10 + floor(level * (job->sp_increase / 100.));
  11561. switch (job_id) {
  11562. case JOB_NINJA:
  11563. if (level >= 10)
  11564. base_sp -= 22;
  11565. else
  11566. base_sp = 11 + 3*level;
  11567. break;
  11568. case JOB_GUNSLINGER:
  11569. if (level > 10)
  11570. base_sp -= 18;
  11571. else
  11572. base_sp = 9 + 3*level;
  11573. break;
  11574. case JOB_SUMMONER:
  11575. case JOB_SPIRIT_HANDLER:
  11576. base_sp -= floor(base_sp / 2);
  11577. break;
  11578. }
  11579. return (unsigned int)base_sp;
  11580. }
  11581. const std::string JobDatabase::getDefaultLocation() {
  11582. return std::string(db_path) + "/job_stats.yml";
  11583. }
  11584. /**
  11585. * Reads and parses an entry from the job_db.
  11586. * @param node: YAML node containing the entry.
  11587. * @return count of successfully parsed rows
  11588. */
  11589. uint64 JobDatabase::parseBodyNode(const ryml::NodeRef& node) {
  11590. if (this->nodeExists(node, "Jobs")) {
  11591. const ryml::NodeRef& jobsNode = node["Jobs"];
  11592. for (const auto &jobit : jobsNode) {
  11593. std::string job_name;
  11594. c4::from_chars(jobit.key(), &job_name);
  11595. std::string job_name_constant = "JOB_" + job_name;
  11596. int64 job_id;
  11597. if (!script_get_constant(job_name_constant.c_str(), &job_id)) {
  11598. this->invalidWarning(node["Job"], "Job %s does not exist.\n", job_name.c_str());
  11599. return 0;
  11600. }
  11601. std::shared_ptr<s_job_info> job = job_db.find(static_cast<uint16>(job_id));
  11602. bool exists = job != nullptr;
  11603. if (!exists) {
  11604. job = std::make_shared<s_job_info>();
  11605. job->job_bonus.resize(MAX_LEVEL);
  11606. std::fill(job->job_bonus.begin(), job->job_bonus.end(), std::array<uint16, PARAM_MAX> { 0 });
  11607. job->base_hp.resize(MAX_LEVEL);
  11608. std::fill(job->base_hp.begin(), job->base_hp.end(), 0);
  11609. job->base_sp.resize(MAX_LEVEL);
  11610. std::fill(job->base_sp.begin(), job->base_sp.end(), 0);
  11611. job->base_ap.resize(MAX_LEVEL);
  11612. std::fill(job->base_ap.begin(), job->base_ap.end(), 0);
  11613. }
  11614. if (this->nodeExists(node, "MaxWeight")) {
  11615. uint32 weight;
  11616. if (!this->asUInt32(node, "MaxWeight", weight))
  11617. return 0;
  11618. job->max_weight_base = weight;
  11619. } else {
  11620. if (!exists)
  11621. job->max_weight_base = 20000;
  11622. }
  11623. if (this->nodeExists(node, "HpFactor")) {
  11624. uint32 hp;
  11625. if (!this->asUInt32(node, "HpFactor", hp))
  11626. return 0;
  11627. job->hp_factor = hp;
  11628. } else {
  11629. if (!exists)
  11630. job->hp_factor = 0;
  11631. }
  11632. if (this->nodeExists(node, "HpIncrease")) {
  11633. uint32 hp;
  11634. if (!this->asUInt32(node, "HpIncrease", hp))
  11635. return 0;
  11636. job->hp_increase = hp;
  11637. } else {
  11638. if (!exists)
  11639. job->hp_increase = 500;
  11640. }
  11641. if (this->nodeExists(node, "SpIncrease")) {
  11642. uint32 sp;
  11643. if (!this->asUInt32(node, "SpIncrease", sp))
  11644. return 0;
  11645. job->sp_increase = sp;
  11646. } else {
  11647. if (!exists)
  11648. job->sp_increase = 100;
  11649. }
  11650. if (this->nodeExists(node, "BaseASPD")) {
  11651. const ryml::NodeRef& aspdNode = node["BaseASPD"];
  11652. uint8 max = MAX_WEAPON_TYPE;
  11653. #ifdef RENEWAL // Renewal adds an extra column for shields
  11654. max += 1;
  11655. #endif
  11656. if (!exists) {
  11657. job->aspd_base.resize(max);
  11658. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11659. }
  11660. for (const auto &aspdit : aspdNode) {
  11661. std::string weapon;
  11662. c4::from_chars(aspdit.key(), &weapon);
  11663. std::string weapon_constant = "W_" + weapon;
  11664. int64 constant;
  11665. if (!script_get_constant(weapon_constant.c_str(), &constant)) {
  11666. this->invalidWarning(aspdNode["BaseASPD"], "Unknown weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11667. continue;
  11668. }
  11669. if (constant < W_FIST || constant > max) {
  11670. this->invalidWarning(aspdNode["BaseASPD"], "Invalid weapon type %s specified for %s, skipping.\n", weapon.c_str(), job_name.c_str());
  11671. continue;
  11672. }
  11673. int16 aspd;
  11674. if (!this->asInt16(aspdNode, weapon.c_str(), aspd))
  11675. return 0;
  11676. job->aspd_base[static_cast<int16>(constant)] = aspd;
  11677. }
  11678. } else {
  11679. if (!exists) {
  11680. uint8 max = MAX_WEAPON_TYPE;
  11681. #ifdef RENEWAL // Renewal adds an extra column for shields
  11682. max += 1;
  11683. #endif
  11684. job->aspd_base.resize(max);
  11685. std::fill(job->aspd_base.begin(), job->aspd_base.end(), 2000);
  11686. }
  11687. }
  11688. if (this->nodeExists(node, "MaxStats")) {
  11689. const ryml::NodeRef& statNode = node["MaxStats"];
  11690. for (const auto &statit : statNode) {
  11691. std::string stat;
  11692. c4::from_chars(statit.key(), &stat);
  11693. std::string stat_constant = "PARAM_" + stat;
  11694. int64 constant;
  11695. if (!script_get_constant(stat_constant.c_str(), &constant)) {
  11696. this->invalidWarning(statNode["Bonus"], "Unknown max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11697. continue;
  11698. }
  11699. if (constant < PARAM_STR || constant >= PARAM_MAX) {
  11700. this->invalidWarning(statNode["Bonus"], "Invalid max stat %s specified for %s, skipping.\n", stat.c_str(), job_name.c_str());
  11701. continue;
  11702. }
  11703. uint16 max;
  11704. if (!this->asUInt16(statNode, stat.c_str(), max))
  11705. return 0;
  11706. job->max_param[constant] = max;
  11707. }
  11708. }
  11709. if (this->nodeExists(node, "MaxBaseLevel")) {
  11710. uint16 level;
  11711. if (!this->asUInt16(node, "MaxBaseLevel", level))
  11712. return 0;
  11713. if (level == 0 || level > MAX_LEVEL) {
  11714. this->invalidWarning(node["MaxBaseLevel"], "MaxBaseLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11715. level = MAX_LEVEL;
  11716. }
  11717. job->max_base_level = level;
  11718. } else {
  11719. if (!exists)
  11720. job->max_base_level = MAX_LEVEL;
  11721. }
  11722. if (this->nodeExists(node, "BaseExp")) {
  11723. for (const ryml::NodeRef& bexpNode : node["BaseExp"]) {
  11724. uint16 level;
  11725. if (!this->asUInt16(bexpNode, "Level", level))
  11726. return 0;
  11727. if (level > job->max_base_level)
  11728. continue;
  11729. if (level == 0 || level > MAX_LEVEL) {
  11730. this->invalidWarning(bexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11731. return 0;
  11732. }
  11733. if (this->nodeExists(bexpNode, "Exp")) {
  11734. t_exp exp;
  11735. if (!this->asUInt64(bexpNode, "Exp", exp))
  11736. return 0;
  11737. job->base_exp[level - 1] = exp;
  11738. }
  11739. }
  11740. }
  11741. if (this->nodeExists(node, "MaxJobLevel")) {
  11742. uint16 level;
  11743. if (!this->asUInt16(node, "MaxJobLevel", level))
  11744. return 0;
  11745. if (level == 0 || level > MAX_LEVEL) {
  11746. this->invalidWarning(node["MaxJobLevel"], "MaxJobLevel must be between 1~MAX_LEVEL for %s, capping to MAX_LEVEL.\n", job_name.c_str());
  11747. level = MAX_LEVEL;
  11748. }
  11749. job->max_job_level = level;
  11750. } else {
  11751. if (!exists)
  11752. job->max_job_level = MAX_LEVEL;
  11753. }
  11754. if (this->nodeExists(node, "JobExp")) {
  11755. for (const ryml::NodeRef& jexpNode : node["JobExp"]) {
  11756. uint16 level;
  11757. if (!this->asUInt16(jexpNode, "Level", level))
  11758. return 0;
  11759. if (level > job->max_job_level)
  11760. continue;
  11761. if (level == 0 || level > MAX_LEVEL) {
  11762. this->invalidWarning(jexpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11763. return 0;
  11764. }
  11765. if (this->nodeExists(jexpNode, "Exp")) {
  11766. t_exp exp;
  11767. if (!this->asUInt64(jexpNode, "Exp", exp))
  11768. return 0;
  11769. job->job_exp[level - 1] = exp;
  11770. }
  11771. }
  11772. }
  11773. if (this->nodeExists(node, "BonusStats")) {
  11774. const ryml::NodeRef& bonusNode = node["BonusStats"];
  11775. for (const ryml::NodeRef& levelNode : bonusNode) {
  11776. uint16 level;
  11777. if (!this->asUInt16(levelNode, "Level", level))
  11778. return 0;
  11779. if (level == 0 || level > MAX_LEVEL) {
  11780. this->invalidWarning(levelNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11781. return 0;
  11782. }
  11783. for (uint8 idx = PARAM_STR; idx < PARAM_MAX; idx++) {
  11784. if (this->nodeExists(levelNode, parameter_names[idx])) {
  11785. int16 change;
  11786. if (!this->asInt16(levelNode, parameter_names[idx], change))
  11787. return 0;
  11788. job->job_bonus[level - 1][idx] = change;
  11789. }
  11790. }
  11791. }
  11792. }
  11793. #ifdef HP_SP_TABLES
  11794. if (this->nodeExists(node, "BaseHp")) {
  11795. for (const ryml::NodeRef& bhpNode : node["BaseHp"]) {
  11796. uint16 level;
  11797. if (!this->asUInt16(bhpNode, "Level", level))
  11798. return 0;
  11799. if (level > job->max_base_level)
  11800. continue;
  11801. if (level == 0 || level > MAX_LEVEL) {
  11802. this->invalidWarning(bhpNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11803. return 0;
  11804. }
  11805. if (this->nodeExists(bhpNode, "Hp")) {
  11806. uint32 points;
  11807. if (!this->asUInt32(bhpNode, "Hp", points))
  11808. return 0;
  11809. job->base_hp[level - 1] = points;
  11810. }
  11811. }
  11812. }
  11813. if (this->nodeExists(node, "BaseSp")) {
  11814. for (const ryml::NodeRef& bspNode : node["BaseSp"]) {
  11815. uint16 level;
  11816. if (!this->asUInt16(bspNode, "Level", level))
  11817. return 0;
  11818. if (level > job->max_base_level)
  11819. continue;
  11820. if (level == 0 || level > MAX_LEVEL) {
  11821. this->invalidWarning(bspNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11822. return 0;
  11823. }
  11824. if (this->nodeExists(bspNode, "Sp")) {
  11825. uint32 points;
  11826. if (!this->asUInt32(bspNode, "Sp", points))
  11827. return 0;
  11828. job->base_sp[level - 1] = points;
  11829. }
  11830. }
  11831. }
  11832. if (this->nodeExists(node, "BaseAp")) {
  11833. for (const ryml::NodeRef& bapNode : node["BaseAp"]) {
  11834. uint16 level;
  11835. if (!this->asUInt16(bapNode, "Level", level))
  11836. return 0;
  11837. if (level > job->max_base_level)
  11838. continue;
  11839. if (level == 0 || level > MAX_LEVEL) {
  11840. this->invalidWarning(bapNode["Level"], "Level must be between 1~MAX_LEVEL for %s.\n", job_name.c_str());
  11841. return 0;
  11842. }
  11843. if (this->nodeExists(bapNode, "Ap")) {
  11844. uint32 points;
  11845. if (!this->asUInt32(bapNode, "Ap", points))
  11846. return 0;
  11847. job->base_ap[level - 1] = points;
  11848. }
  11849. }
  11850. }
  11851. #endif
  11852. if (!exists)
  11853. this->put(static_cast<uint16>(job_id), job);
  11854. }
  11855. }
  11856. return 1;
  11857. }
  11858. void JobDatabase::loadingFinished() {
  11859. // Checking if all class have their data
  11860. for (auto &jobIt : *this) {
  11861. uint16 job_id = jobIt.first;
  11862. if (!pcdb_checkid(job_id))
  11863. continue;
  11864. 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)
  11865. continue; // Classes that do not need exp tables.
  11866. std::shared_ptr<s_job_info> job = jobIt.second;
  11867. uint16 maxBaseLv = job->max_base_level, maxJobLv = job->max_job_level;
  11868. if (!maxBaseLv)
  11869. ShowWarning("Class %s (%d) does not have a base exp table.\n", job_name(job_id), job_id);
  11870. if (!maxJobLv)
  11871. ShowWarning("Class %s (%d) does not have a job exp table.\n", job_name(job_id), job_id);
  11872. // Init and checking the empty value of Base HP/SP [Cydh]
  11873. if (job->base_hp.empty())
  11874. job->base_hp.resize(maxBaseLv);
  11875. for (uint16 j = 0; j < maxBaseLv; j++) {
  11876. if (job->base_hp[j] == 0)
  11877. job->base_hp[j] = pc_calc_basehp(j + 1, job_id);
  11878. }
  11879. if (job->base_sp.empty())
  11880. job->base_sp.resize(maxBaseLv);
  11881. for (uint16 j = 0; j < maxBaseLv; j++) {
  11882. if (job->base_sp[j] == 0)
  11883. job->base_sp[j] = pc_calc_basesp(j + 1, job_id);
  11884. }
  11885. // Resize to the maximum base level
  11886. if (job->base_hp.capacity() > maxBaseLv)
  11887. job->base_hp.erase(job->base_hp.begin() + maxBaseLv, job->base_hp.end());
  11888. if (job->base_sp.capacity() > maxBaseLv)
  11889. job->base_sp.erase(job->base_sp.begin() + maxBaseLv, job->base_sp.end());
  11890. if (job->base_ap.capacity() > maxBaseLv)
  11891. job->base_ap.erase(job->base_ap.begin() + maxBaseLv, job->base_ap.end());
  11892. // Resize to the maximum job level
  11893. if (job->job_bonus.capacity() > maxJobLv)
  11894. job->job_bonus.erase(job->job_bonus.begin() + maxJobLv, job->job_bonus.end());
  11895. for (uint16 parameter = PARAM_STR; parameter < PARAM_MAX; parameter++) {
  11896. // Store total
  11897. int16 current = 0;
  11898. for (uint16 job_level = 0; job_level < maxJobLv; job_level++) {
  11899. // Add the bonus from this job level
  11900. current += job->job_bonus[job_level][parameter];
  11901. // Set the new total on this job level
  11902. job->job_bonus[job_level][parameter] = current;
  11903. }
  11904. }
  11905. uint64 class_ = pc_jobid2mapid( job_id );
  11906. // Set normal status limits
  11907. uint16 max = battle_config.max_parameter;
  11908. do{
  11909. // Always check babies first
  11910. if( class_ & JOBL_BABY ){
  11911. if( class_ & JOBL_THIRD ){
  11912. max = battle_config.max_baby_third_parameter;
  11913. break;
  11914. }else{
  11915. max = battle_config.max_baby_parameter;
  11916. break;
  11917. }
  11918. }
  11919. // Summoner
  11920. if( ( class_ & MAPID_BASEMASK ) == MAPID_SUMMONER ){
  11921. max = battle_config.max_summoner_parameter;
  11922. break;
  11923. }
  11924. // Extended classes
  11925. if( ( class_ & MAPID_UPPERMASK ) == MAPID_KAGEROUOBORO || ( class_ & MAPID_UPPERMASK ) == MAPID_REBELLION ){
  11926. max = battle_config.max_extended_parameter;
  11927. break;
  11928. }
  11929. if( class_ & JOBL_FOURTH ){
  11930. max = battle_config.max_fourth_parameter;
  11931. break;
  11932. }
  11933. // 3rd class
  11934. if( class_ & JOBL_THIRD ){
  11935. // Transcendent
  11936. if( class_ & JOBL_UPPER ){
  11937. max = battle_config.max_third_trans_parameter;
  11938. break;
  11939. }else{
  11940. max = battle_config.max_third_parameter;
  11941. break;
  11942. }
  11943. }
  11944. // Transcendent
  11945. if( class_ & JOBL_UPPER ){
  11946. max = battle_config.max_trans_parameter;
  11947. break;
  11948. }
  11949. }while( false );
  11950. for( uint16 parameter = PARAM_STR; parameter < PARAM_POW; parameter++ ){
  11951. // If it is not explicitly set in the database file
  11952. if( job->max_param[parameter] == 0 ){
  11953. job->max_param[parameter] = max;
  11954. }
  11955. }
  11956. // Set trait status limit
  11957. if( class_ & JOBL_FOURTH ){
  11958. max = battle_config.max_trait_parameter;
  11959. }else{
  11960. max = 0;
  11961. }
  11962. for( uint16 parameter = PARAM_POW; parameter < PARAM_MAX; parameter++ ){
  11963. // If it is not explicitly set in the database file
  11964. if( job->max_param[parameter] == 0 ){
  11965. job->max_param[parameter] = max;
  11966. }
  11967. }
  11968. }
  11969. TypesafeCachedYamlDatabase::loadingFinished();
  11970. }
  11971. /**
  11972. * Read job_noenter_map.txt
  11973. **/
  11974. static bool pc_readdb_job_noenter_map(char *str[], int columns, int current) {
  11975. int class_ = -1;
  11976. int64 class_tmp;
  11977. if (ISDIGIT(str[0][0])) {
  11978. class_ = atoi(str[0]);
  11979. } else {
  11980. if (!script_get_constant(str[0], &class_tmp)) {
  11981. ShowError("pc_readdb_job_noenter_map: Invalid job %s specified.\n", str[0]);
  11982. return false;
  11983. }
  11984. class_ = static_cast<int>(class_tmp);
  11985. }
  11986. if (!pcdb_checkid(class_)) {
  11987. ShowError("pc_readdb_job_noenter_map: Invalid job %d specified.\n", class_);
  11988. return false;
  11989. }
  11990. std::shared_ptr<s_job_info> job = job_db.find(class_);
  11991. if (job == nullptr) {
  11992. ShowError("pc_readdb_job_noenter_map: Job %d data not initialized.\n", class_);
  11993. return false;
  11994. }
  11995. job->noenter_map.zone = atoi(str[1]);
  11996. job->noenter_map.group_lv = atoi(str[2]);
  11997. return true;
  11998. }
  11999. const std::string PlayerStatPointDatabase::getDefaultLocation() {
  12000. return std::string(db_path) + "/statpoint.yml";
  12001. }
  12002. uint64 PlayerStatPointDatabase::parseBodyNode(const ryml::NodeRef& node) {
  12003. if (!this->nodesExist(node, { "Level", "Points" })) {
  12004. return 0;
  12005. }
  12006. uint16 level;
  12007. if (!this->asUInt16(node, "Level", level))
  12008. return 0;
  12009. uint32 point;
  12010. if (!this->asUInt32(node, "Points", point))
  12011. return 0;
  12012. if (level == 0) {
  12013. this->invalidWarning(node["Level"], "The minimum level is 1.\n");
  12014. return 0;
  12015. }
  12016. if (level > MAX_LEVEL) {
  12017. this->invalidWarning(node["Level"], "Level %d exceeds maximum BaseLevel %d, skipping.\n", level, MAX_LEVEL);
  12018. return 0;
  12019. }
  12020. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12021. bool exists = entry != nullptr;
  12022. if( !exists ){
  12023. entry = std::make_shared<s_statpoint_entry>();
  12024. entry->level = level;
  12025. entry->statpoints = point;
  12026. }
  12027. if( this->nodeExists( node, "TraitPoints" ) ){
  12028. uint32 traitpoints;
  12029. if( !this->asUInt32( node, "TraitPoints", traitpoints ) ){
  12030. return 0;
  12031. }
  12032. entry->traitpoints = traitpoints;
  12033. }else{
  12034. if( !exists ){
  12035. entry->traitpoints = 0;
  12036. }
  12037. }
  12038. if( !exists ){
  12039. this->put( level, entry );
  12040. }
  12041. return 1;
  12042. }
  12043. /**
  12044. * Generate the remaining parts of the db if necessary.
  12045. */
  12046. void PlayerStatPointDatabase::loadingFinished(){
  12047. const uint16 trait_start_level = 200;
  12048. std::shared_ptr<s_statpoint_entry> level_one = this->find( 1 );
  12049. if( level_one == nullptr ){
  12050. if( battle_config.use_statpoint_table ){
  12051. ShowError( "Missing status points for Level 1\n" );
  12052. }
  12053. level_one = std::make_shared<s_statpoint_entry>();
  12054. level_one->level = 1;
  12055. level_one->statpoints = inter_config.start_status_points;
  12056. level_one->traitpoints = 0;
  12057. this->put( 1, level_one );
  12058. }else if( battle_config.use_statpoint_table ){
  12059. if( level_one->statpoints != inter_config.start_status_points ){
  12060. ShowError( "Status points for Level 1 (=%u) do not match inter_athena.conf value (=%u).\n", level_one->statpoints, inter_config.start_status_points );
  12061. level_one->statpoints = inter_config.start_status_points;
  12062. }
  12063. }else{
  12064. level_one->statpoints = inter_config.start_status_points;
  12065. level_one->traitpoints = 0;
  12066. }
  12067. std::shared_ptr<s_statpoint_entry> last_level = level_one;
  12068. for( uint16 level = 2; level <= MAX_LEVEL; level++ ){
  12069. std::shared_ptr<s_statpoint_entry> entry = this->find( level );
  12070. bool exists = entry != nullptr;
  12071. if( !exists ){
  12072. entry = std::make_shared<s_statpoint_entry>();
  12073. entry->level = level;
  12074. this->put( level, entry );
  12075. }
  12076. if( !battle_config.use_statpoint_table || !exists ){
  12077. if( battle_config.use_statpoint_table ){
  12078. ShowError("Missing status points for Level %hu\n", level);
  12079. }
  12080. if( level <= trait_start_level ){
  12081. entry->statpoints = last_level->statpoints + ( ( level - 1 + 15 ) / 5 );
  12082. }else{
  12083. entry->statpoints = last_level->statpoints;
  12084. }
  12085. }
  12086. if( !battle_config.use_traitpoint_table || !exists ){
  12087. if( battle_config.use_traitpoint_table && level > trait_start_level ){
  12088. ShowError( "Missing trait points for Level %hu\n", level );
  12089. }
  12090. if( level > trait_start_level ){
  12091. entry->traitpoints = ( level - trait_start_level ) * 3 + ( level - trait_start_level ) / 5 * 4;
  12092. }else{
  12093. entry->traitpoints = 0;
  12094. }
  12095. }
  12096. // Store it for next iteration
  12097. last_level = entry;
  12098. }
  12099. TypesafeCachedYamlDatabase::loadingFinished();
  12100. }
  12101. /*==========================================
  12102. * pc DB reading.
  12103. * job_stats.yml - Job values
  12104. * skill_tree.txt - skill tree for every class
  12105. * attr_fix.yml - elemental adjustment table
  12106. *------------------------------------------*/
  12107. void pc_readdb(void) {
  12108. int i, s = 1;
  12109. const char* dbsubpath[] = {
  12110. "",
  12111. "/" DBIMPORT,
  12112. //add other path here
  12113. };
  12114. //reset
  12115. job_db.clear(); // job_info table
  12116. #if defined(RENEWAL_DROP) || defined(RENEWAL_EXP)
  12117. penalty_db.load();
  12118. #endif
  12119. statpoint_db.clear();
  12120. job_db.load();
  12121. for(i=0; i<ARRAYLENGTH(dbsubpath); i++){
  12122. uint8 n1 = (uint8)(strlen(db_path)+strlen(dbsubpath[i])+1);
  12123. uint8 n2 = (uint8)(strlen(db_path)+strlen(DBPATH)+strlen(dbsubpath[i])+1);
  12124. char* dbsubpath1 = (char*)aMalloc(n1+1);
  12125. char* dbsubpath2 = (char*)aMalloc(n2+1);
  12126. if(i==0) {
  12127. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12128. safesnprintf(dbsubpath2,n2,"%s/%s%s",db_path,DBPATH,dbsubpath[i]);
  12129. }
  12130. else {
  12131. safesnprintf(dbsubpath1,n1,"%s%s",db_path,dbsubpath[i]);
  12132. safesnprintf(dbsubpath2,n1,"%s%s",db_path,dbsubpath[i]);
  12133. }
  12134. sv_readdb(dbsubpath2, "job_noenter_map.txt", ',', 3, 3, CLASS_COUNT, &pc_readdb_job_noenter_map, i > 0);
  12135. aFree(dbsubpath1);
  12136. aFree(dbsubpath2);
  12137. }
  12138. // Reset and read skilltree - needs to be read after pc_readdb_job_exp to get max base and job levels
  12139. skill_tree_db.reload();
  12140. statpoint_db.load();
  12141. }
  12142. // Read MOTD on startup. [Valaris]
  12143. int pc_read_motd(void)
  12144. {
  12145. FILE* fp;
  12146. // clear old MOTD
  12147. memset(motd_text, 0, sizeof(motd_text));
  12148. // read current MOTD
  12149. if( ( fp = fopen(motd_txt, "r") ) != NULL )
  12150. {
  12151. unsigned int entries = 0;
  12152. char buf[CHAT_SIZE_MAX];
  12153. while( entries < MOTD_LINE_SIZE && fgets(buf, CHAT_SIZE_MAX, fp) )
  12154. {
  12155. unsigned int lines = 0;
  12156. size_t len;
  12157. lines++;
  12158. if( buf[0] == '/' && buf[1] == '/' )
  12159. continue;
  12160. len = strlen(buf);
  12161. while( len && ( buf[len-1] == '\r' || buf[len-1] == '\n' ) ) // strip trailing EOL characters
  12162. len--;
  12163. if( len ) {
  12164. char * ptr;
  12165. buf[len] = 0;
  12166. if( ( ptr = strstr(buf, " :") ) != NULL && ptr-buf >= NAME_LENGTH ) // crashes newer clients
  12167. 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);
  12168. }
  12169. else {// empty line
  12170. buf[0] = ' ';
  12171. buf[1] = 0;
  12172. }
  12173. safestrncpy(motd_text[entries], buf, CHAT_SIZE_MAX);
  12174. entries++;
  12175. }
  12176. fclose(fp);
  12177. ShowStatus("Done reading '" CL_WHITE "%u" CL_RESET "' entries in '" CL_WHITE "%s" CL_RESET "'.\n", entries, motd_txt);
  12178. }
  12179. else
  12180. ShowWarning("File '" CL_WHITE "%s" CL_RESET "' not found.\n", motd_txt);
  12181. return 0;
  12182. }
  12183. void pc_itemcd_do(map_session_data *sd, bool load) {
  12184. int i,cursor = 0;
  12185. struct item_cd* cd = NULL;
  12186. if( load ) {
  12187. if( !(cd = (struct item_cd*)idb_get(itemcd_db, sd->status.char_id)) ) {
  12188. // no item cooldown is associated with this character
  12189. return;
  12190. }
  12191. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12192. if( cd->nameid[i] && DIFF_TICK(gettick(),cd->tick[i]) < 0 ) {
  12193. sd->item_delay[cursor].tick = cd->tick[i];
  12194. sd->item_delay[cursor].nameid = cd->nameid[i];
  12195. cursor++;
  12196. }
  12197. }
  12198. idb_remove(itemcd_db,sd->status.char_id);
  12199. } else {
  12200. if( !(cd = (struct item_cd*)idb_get(itemcd_db,sd->status.char_id)) ) {
  12201. // create a new skill cooldown object for map storage
  12202. CREATE( cd, struct item_cd, 1 );
  12203. idb_put( itemcd_db, sd->status.char_id, cd );
  12204. }
  12205. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12206. if( sd->item_delay[i].nameid && DIFF_TICK(gettick(),sd->item_delay[i].tick) < 0 ) {
  12207. cd->tick[cursor] = sd->item_delay[i].tick;
  12208. cd->nameid[cursor] = sd->item_delay[i].nameid;
  12209. cursor++;
  12210. }
  12211. }
  12212. }
  12213. return;
  12214. }
  12215. /**
  12216. * Add item delay to player's item delay data
  12217. * @param sd Player
  12218. * @param id Item data
  12219. * @param tick Current tick
  12220. * @param n Item index in inventory
  12221. * @return 0: No delay, can consume item.
  12222. * 1: Has delay, cancel consumption.
  12223. **/
  12224. uint8 pc_itemcd_add(map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12225. int i;
  12226. ARR_FIND(0, MAX_ITEMDELAYS, i, sd->item_delay[i].nameid == id->nameid );
  12227. if( i == MAX_ITEMDELAYS ) /* item not found. try first empty now */
  12228. ARR_FIND(0, MAX_ITEMDELAYS, i, !sd->item_delay[i].nameid );
  12229. if( i < MAX_ITEMDELAYS ) {
  12230. if( sd->item_delay[i].nameid ) {// found
  12231. if( DIFF_TICK(sd->item_delay[i].tick, tick) > 0 ) {
  12232. t_tick e_tick = DIFF_TICK(sd->item_delay[i].tick, tick)/1000;
  12233. char e_msg[CHAT_SIZE_MAX];
  12234. if( e_tick > 99 )
  12235. sprintf(e_msg,msg_txt(sd,379), // Item Failed. [%s] is cooling down. Wait %.1f minutes.
  12236. itemdb_ename(sd->item_delay[i].nameid), (double)e_tick / 60);
  12237. else
  12238. sprintf(e_msg,msg_txt(sd,380), // Item Failed. [%s] is cooling down. Wait %d seconds.
  12239. itemdb_ename(sd->item_delay[i].nameid), e_tick+1);
  12240. clif_messagecolor(&sd->bl,color_table[COLOR_YELLOW],e_msg,false,SELF);
  12241. return 1; // Delay has not expired yet
  12242. }
  12243. } else {// not yet used item (all slots are initially empty)
  12244. sd->item_delay[i].nameid = id->nameid;
  12245. }
  12246. if( !(id->nameid == ITEMID_REINS_OF_MOUNT && sd->sc.option&(OPTION_WUGRIDER|OPTION_RIDING|OPTION_DRAGON|OPTION_MADOGEAR)) )
  12247. sd->item_delay[i].tick = tick + sd->inventory_data[n]->delay.duration;
  12248. } else {// should not happen
  12249. ShowError("pc_itemcd_add: Exceeded item delay array capacity! (nameid=%u, char_id=%d)\n", id->nameid, sd->status.char_id);
  12250. }
  12251. //clean up used delays so we can give room for more
  12252. for(i = 0; i < MAX_ITEMDELAYS; i++) {
  12253. if( DIFF_TICK(sd->item_delay[i].tick, tick) <= 0 ) {
  12254. sd->item_delay[i].tick = 0;
  12255. sd->item_delay[i].nameid = 0;
  12256. }
  12257. }
  12258. return 0;
  12259. }
  12260. /**
  12261. * Check if player has delay to reuse item
  12262. * @param sd Player
  12263. * @param id Item data
  12264. * @param tick Current tick
  12265. * @param n Item index in inventory
  12266. * @return 0: No delay, can consume item.
  12267. * 1: Has delay, cancel consumption.
  12268. **/
  12269. uint8 pc_itemcd_check(map_session_data *sd, struct item_data *id, t_tick tick, unsigned short n) {
  12270. status_change *sc = NULL;
  12271. nullpo_retr(0, sd);
  12272. nullpo_retr(0, id);
  12273. // Do normal delay assignment
  12274. if (id->delay.sc <= SC_NONE || id->delay.sc >= SC_MAX || !(sc = &sd->sc))
  12275. return pc_itemcd_add(sd, id, tick, n);
  12276. // Send reply of delay remains
  12277. if (sc->getSCE(id->delay.sc)) {
  12278. const struct TimerData *timer = get_timer(sc->getSCE(id->delay.sc)->timer);
  12279. clif_msg_value(sd, ITEM_REUSE_LIMIT, (int)(timer ? DIFF_TICK(timer->tick, tick) / 1000 : 99));
  12280. return 1;
  12281. }
  12282. sc_start(&sd->bl, &sd->bl, id->delay.sc, 100, id->nameid, id->delay.duration);
  12283. return 0;
  12284. }
  12285. /**
  12286. * Clear the dmglog data from player
  12287. * @param sd
  12288. * @param md
  12289. **/
  12290. static void pc_clear_log_damage_sub(uint32 char_id, struct mob_data *md)
  12291. {
  12292. uint8 i;
  12293. ARR_FIND(0,DAMAGELOG_SIZE,i,md->dmglog[i].id == char_id);
  12294. if (i < DAMAGELOG_SIZE) {
  12295. md->dmglog[i].id = 0;
  12296. md->dmglog[i].dmg = 0;
  12297. md->dmglog[i].flag = 0;
  12298. }
  12299. }
  12300. /**
  12301. * Add log to player's dmglog
  12302. * @param sd
  12303. * @param id Monster's GID
  12304. **/
  12305. void pc_damage_log_add(map_session_data *sd, int id)
  12306. {
  12307. uint8 i = 0;
  12308. if (!sd || !id)
  12309. return;
  12310. //Only store new data, don't need to renew the old one with same id
  12311. ARR_FIND(0, DAMAGELOG_SIZE_PC, i, sd->dmglog[i] == id);
  12312. if (i < DAMAGELOG_SIZE_PC)
  12313. return;
  12314. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12315. if (sd->dmglog[i] == 0) {
  12316. sd->dmglog[i] = id;
  12317. return;
  12318. }
  12319. }
  12320. }
  12321. /**
  12322. * Clear dmglog data from player
  12323. * @param sd
  12324. * @param id Monster's id
  12325. **/
  12326. void pc_damage_log_clear(map_session_data *sd, int id)
  12327. {
  12328. uint8 i;
  12329. struct mob_data *md = NULL;
  12330. if (!sd)
  12331. return;
  12332. if (!id) {
  12333. for (i = 0; i < DAMAGELOG_SIZE_PC; i++) {
  12334. if( !sd->dmglog[i] ) //skip the empty value
  12335. continue;
  12336. if ((md = map_id2md(sd->dmglog[i])))
  12337. pc_clear_log_damage_sub(sd->status.char_id,md);
  12338. sd->dmglog[i] = 0;
  12339. }
  12340. }
  12341. else {
  12342. if ((md = map_id2md(id)))
  12343. pc_clear_log_damage_sub(sd->status.char_id,md);
  12344. ARR_FIND(0,DAMAGELOG_SIZE_PC,i,sd->dmglog[i] == id); // find the id position
  12345. if (i < DAMAGELOG_SIZE_PC)
  12346. sd->dmglog[i] = 0;
  12347. }
  12348. }
  12349. /**
  12350. * Status change data arrived from char-server
  12351. * @param sd: Player data
  12352. */
  12353. void pc_scdata_received(map_session_data *sd) {
  12354. pc_inventory_rentals(sd); // Needed here to remove rentals that have Status Changes after chrif_load_scdata has finished
  12355. clif_weight_limit( sd );
  12356. if( pc_has_permission( sd, PC_PERM_ATTENDANCE ) && pc_attendance_enabled() && !pc_attendance_rewarded_today( sd ) && pc_attendance_counter(sd) < 200 ){
  12357. clif_ui_open( *sd, OUT_UI_ATTENDANCE, pc_attendance_counter( sd ) );
  12358. }
  12359. sd->state.pc_loaded = true;
  12360. if (sd->state.connect_new == 0 && sd->fd) { // Character already loaded map! Gotta trigger LoadEndAck manually.
  12361. sd->state.connect_new = 1;
  12362. clif_parse_LoadEndAck(sd->fd, sd);
  12363. }
  12364. if (pc_iscarton(sd)) {
  12365. sd->cart_weight_max = 0; // Force a client refesh
  12366. status_calc_cart_weight(sd, (e_status_calc_weight_opt)(CALCWT_ITEM|CALCWT_MAXBONUS|CALCWT_CARTSTATE));
  12367. }
  12368. if (sd->sc.getSCE(SC_SOULENERGY))
  12369. sd->soulball = sd->sc.getSCE(SC_SOULENERGY)->val1;
  12370. }
  12371. /**
  12372. * Check player account expiration time and rental item expirations
  12373. * @param sd: Player data
  12374. */
  12375. void pc_check_expiration(map_session_data *sd) {
  12376. #ifndef ENABLE_SC_SAVING
  12377. pc_inventory_rentals(sd); // Check here if Status Change saving is disabled
  12378. #endif
  12379. if (sd->expiration_time != 0) { //Don't display if it's unlimited or unknow value
  12380. time_t exp_time = sd->expiration_time;
  12381. char tmpstr[1024];
  12382. strftime(tmpstr,sizeof(tmpstr) - 1,msg_txt(sd,501),localtime(&exp_time)); // "Your account time limit is: %d-%m-%Y %H:%M:%S."
  12383. clif_wis_message(sd,wisp_server_name,tmpstr,strlen(tmpstr) + 1,0);
  12384. pc_expire_check(sd);
  12385. }
  12386. }
  12387. TIMER_FUNC(pc_expiration_timer){
  12388. map_session_data *sd = map_id2sd(id);
  12389. if( !sd ) return 0;
  12390. sd->expiration_tid = INVALID_TIMER;
  12391. if( sd->fd )
  12392. clif_authfail_fd(sd->fd,10);
  12393. map_quit(sd);
  12394. return 0;
  12395. }
  12396. TIMER_FUNC(pc_autotrade_timer){
  12397. map_session_data *sd = map_id2sd(id);
  12398. if (!sd)
  12399. return 0;
  12400. sd->autotrade_tid = INVALID_TIMER;
  12401. if (sd->state.autotrade&2)
  12402. vending_reopen(sd);
  12403. if (sd->state.autotrade&4)
  12404. buyingstore_reopen(sd);
  12405. if (!sd->vender_id && !sd->buyer_id) {
  12406. sd->state.autotrade = 0;
  12407. map_quit(sd);
  12408. }
  12409. return 0;
  12410. }
  12411. /* this timer exists only when a character with a expire timer > 24h is online */
  12412. /* it loops thru online players once an hour to check whether a new < 24h is available */
  12413. TIMER_FUNC(pc_global_expiration_timer){
  12414. struct s_mapiterator* iter;
  12415. map_session_data* sd;
  12416. iter = mapit_getallusers();
  12417. for( sd = (TBL_PC*)mapit_first(iter); mapit_exists(iter); sd = (TBL_PC*)mapit_next(iter) )
  12418. if( sd->expiration_time )
  12419. pc_expire_check(sd);
  12420. mapit_free(iter);
  12421. return 0;
  12422. }
  12423. void pc_expire_check(map_session_data *sd) {
  12424. /* ongoing timer */
  12425. if( sd->expiration_tid != INVALID_TIMER )
  12426. return;
  12427. /* not within the next 24h, enable the global check */
  12428. if( sd->expiration_time > (time(NULL) + ((60 * 60) * 24)) ) {
  12429. /* global check not running, enable */
  12430. if( pc_expiration_tid == INVALID_TIMER ) /* Starts in 1h, repeats every hour */
  12431. pc_expiration_tid = add_timer_interval(gettick() + ((1000 * 60) * 60), pc_global_expiration_timer, 0, 0, ((1000 * 60) * 60));
  12432. return;
  12433. }
  12434. sd->expiration_tid = add_timer(gettick() + (unsigned int)(sd->expiration_time - time(NULL)) * 1000, pc_expiration_timer, sd->bl.id, 0);
  12435. }
  12436. /**
  12437. * Deposit some money to bank
  12438. * @param sd
  12439. * @param money Amount of money to deposit
  12440. **/
  12441. enum e_BANKING_DEPOSIT_ACK pc_bank_deposit(map_session_data *sd, int money) {
  12442. unsigned int limit_check = money + sd->bank_vault;
  12443. if( money <= 0 || limit_check > MAX_BANK_ZENY ) {
  12444. return BDA_OVERFLOW;
  12445. } else if ( money > sd->status.zeny ) {
  12446. return BDA_NO_MONEY;
  12447. }
  12448. if( pc_payzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12449. return BDA_NO_MONEY;
  12450. sd->bank_vault += money;
  12451. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12452. if( save_settings&CHARSAVE_BANK )
  12453. chrif_save(sd, CSAVE_NORMAL);
  12454. return BDA_SUCCESS;
  12455. }
  12456. /**
  12457. * Withdraw money from bank
  12458. * @param sd
  12459. * @param money Amount of money that will be withdrawn
  12460. **/
  12461. enum e_BANKING_WITHDRAW_ACK pc_bank_withdraw(map_session_data *sd, int money) {
  12462. unsigned int limit_check = money + sd->status.zeny;
  12463. if( money <= 0 ) {
  12464. return BWA_UNKNOWN_ERROR;
  12465. } else if ( money > sd->bank_vault ) {
  12466. return BWA_NO_MONEY;
  12467. } else if ( limit_check > MAX_ZENY ) {
  12468. /* no official response for this scenario exists. */
  12469. clif_messagecolor(&sd->bl,color_table[COLOR_RED],msg_txt(sd,1495),false,SELF); //You can't withdraw that much money
  12470. return BWA_UNKNOWN_ERROR;
  12471. }
  12472. if( pc_getzeny(sd,money, LOG_TYPE_BANK, NULL) )
  12473. return BWA_NO_MONEY;
  12474. sd->bank_vault -= money;
  12475. pc_setreg2(sd, BANK_VAULT_VAR, sd->bank_vault);
  12476. if( save_settings&CHARSAVE_BANK )
  12477. chrif_save(sd, CSAVE_NORMAL);
  12478. return BWA_SUCCESS;
  12479. }
  12480. /**
  12481. * Clear Crimson Marker data from caster
  12482. * @param sd: Player
  12483. **/
  12484. void pc_crimson_marker_clear(map_session_data *sd) {
  12485. uint8 i;
  12486. if (!sd)
  12487. return;
  12488. for (i = 0; i < MAX_SKILL_CRIMSON_MARKER; i++) {
  12489. struct block_list *bl = NULL;
  12490. if (sd->c_marker[i] && (bl = map_id2bl(sd->c_marker[i])))
  12491. status_change_end(bl,SC_C_MARKER);
  12492. sd->c_marker[i] = 0;
  12493. }
  12494. }
  12495. /**
  12496. * Show version to player
  12497. * @param sd: Player
  12498. **/
  12499. void pc_show_version(map_session_data *sd) {
  12500. const char* svn = get_svn_revision();
  12501. char buf[CHAT_SIZE_MAX];
  12502. if( svn[0] != UNKNOWN_VERSION )
  12503. sprintf(buf,msg_txt(sd,1295),"SVN: r",svn); //rAthena Version SVN: r%s
  12504. else {
  12505. const char* git = get_git_hash();
  12506. if( git[0] != UNKNOWN_VERSION )
  12507. sprintf(buf,msg_txt(sd,1295),"Git Hash: ",git); //rAthena Version Git Hash: %s
  12508. else
  12509. sprintf(buf,"%s",msg_txt(sd,1296)); //Cannot determine SVN/Git version.
  12510. }
  12511. clif_displaymessage(sd->fd,buf);
  12512. }
  12513. /**
  12514. * Run bonus_script on player
  12515. * @param sd
  12516. * @author [Cydh]
  12517. **/
  12518. void pc_bonus_script(map_session_data *sd) {
  12519. t_tick now = gettick();
  12520. struct linkdb_node *node = NULL, *next = NULL;
  12521. if (!sd || !(node = sd->bonus_script.head))
  12522. return;
  12523. while (node) {
  12524. struct s_bonus_script_entry *entry = NULL;
  12525. next = node->next;
  12526. if ((entry = (struct s_bonus_script_entry *)node->data)) {
  12527. // Only start timer for new bonus_script
  12528. if (entry->tid == INVALID_TIMER) {
  12529. if (entry->icon != EFST_BLANK) // Gives status icon if exist
  12530. clif_status_change(&sd->bl, entry->icon, 1, entry->tick, 1, 0, 0);
  12531. entry->tick += now;
  12532. entry->tid = add_timer(entry->tick, pc_bonus_script_timer, sd->bl.id, (intptr_t)entry);
  12533. }
  12534. if (entry->script)
  12535. run_script(entry->script, 0, sd->bl.id, 0);
  12536. else
  12537. ShowError("pc_bonus_script: The script has been removed somewhere. \"%s\"\n", StringBuf_Value(entry->script_buf));
  12538. }
  12539. node = next;
  12540. }
  12541. }
  12542. /**
  12543. * Add bonus_script to player
  12544. * @param sd Player
  12545. * @param script_str Script string
  12546. * @param dur Duration in ms
  12547. * @param icon EFST
  12548. * @param flag Flags @see enum e_bonus_script_flags
  12549. * @param type 0 - None, 1 - Buff, 2 - Debuff
  12550. * @return New created entry pointer or NULL if failed or NULL if duplicate fail
  12551. * @author [Cydh]
  12552. **/
  12553. 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) {
  12554. struct script_code *script = NULL;
  12555. struct linkdb_node *node = NULL;
  12556. struct s_bonus_script_entry *entry = NULL;
  12557. if (!sd)
  12558. return NULL;
  12559. if (!(script = parse_script(script_str, "bonus_script", 0, SCRIPT_IGNORE_EXTERNAL_BRACKETS))) {
  12560. ShowError("pc_bonus_script_add: Failed to parse script '%s' (CID:%d).\n", script_str, sd->status.char_id);
  12561. return NULL;
  12562. }
  12563. // Duplication checks
  12564. if ((node = sd->bonus_script.head)) {
  12565. while (node) {
  12566. entry = (struct s_bonus_script_entry *)node->data;
  12567. if (strcmpi(script_str, StringBuf_Value(entry->script_buf)) == 0) {
  12568. t_tick newdur = gettick() + dur;
  12569. if (flag&BSF_FORCE_REPLACE && entry->tick < newdur) { // Change duration
  12570. settick_timer(entry->tid, newdur);
  12571. script_free_code(script);
  12572. return NULL;
  12573. }
  12574. else if (flag&BSF_FORCE_DUPLICATE) // Allow duplicate
  12575. break;
  12576. else { // No duplicate bonus
  12577. script_free_code(script);
  12578. return NULL;
  12579. }
  12580. }
  12581. node = node->next;
  12582. }
  12583. }
  12584. CREATE(entry, struct s_bonus_script_entry, 1);
  12585. entry->script_buf = StringBuf_Malloc();
  12586. StringBuf_AppendStr(entry->script_buf, script_str);
  12587. entry->tid = INVALID_TIMER;
  12588. entry->flag = flag;
  12589. entry->icon = icon;
  12590. entry->tick = dur; // Use duration first, on run change to expire time
  12591. entry->type = type;
  12592. entry->script = script;
  12593. sd->bonus_script.count++;
  12594. return entry;
  12595. }
  12596. /**
  12597. * Remove bonus_script data from player
  12598. * @param sd: Target player
  12599. * @param list: Bonus script entry from player
  12600. * @author [Cydh]
  12601. **/
  12602. void pc_bonus_script_free_entry(map_session_data *sd, struct s_bonus_script_entry *entry) {
  12603. if (entry->tid != INVALID_TIMER)
  12604. delete_timer(entry->tid, pc_bonus_script_timer);
  12605. if (entry->script)
  12606. script_free_code(entry->script);
  12607. if (entry->script_buf)
  12608. StringBuf_Free(entry->script_buf);
  12609. if (sd) {
  12610. if (entry->icon != EFST_BLANK)
  12611. clif_status_load(&sd->bl, entry->icon, 0);
  12612. if (sd->bonus_script.count > 0)
  12613. sd->bonus_script.count--;
  12614. }
  12615. aFree(entry);
  12616. }
  12617. /**
  12618. * Do final process if no entry left
  12619. * @param sd
  12620. **/
  12621. static void inline pc_bonus_script_check_final(map_session_data *sd) {
  12622. if (sd->bonus_script.count == 0) {
  12623. if (sd->bonus_script.head && sd->bonus_script.head->data)
  12624. pc_bonus_script_free_entry(sd, (struct s_bonus_script_entry *)sd->bonus_script.head->data);
  12625. linkdb_final(&sd->bonus_script.head);
  12626. }
  12627. }
  12628. /**
  12629. * Timer for bonus_script
  12630. * @param tid
  12631. * @param tick
  12632. * @param id
  12633. * @param data
  12634. * @author [Cydh]
  12635. **/
  12636. TIMER_FUNC(pc_bonus_script_timer){
  12637. map_session_data *sd;
  12638. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)data;
  12639. sd = map_id2sd(id);
  12640. if (!sd) {
  12641. ShowError("pc_bonus_script_timer: Null pointer id: %d tid: %d\n", id, tid);
  12642. return 0;
  12643. }
  12644. if (tid == INVALID_TIMER)
  12645. return 0;
  12646. if (!sd->bonus_script.head || entry == NULL) {
  12647. ShowError("pc_bonus_script_timer: Invalid entry pointer %p!\n", entry);
  12648. return 0;
  12649. }
  12650. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12651. pc_bonus_script_free_entry(sd, entry);
  12652. pc_bonus_script_check_final(sd);
  12653. status_calc_pc(sd,SCO_NONE);
  12654. return 0;
  12655. }
  12656. /**
  12657. * Check then clear all active timer(s) of bonus_script data from player based on reason
  12658. * @param sd: Target player
  12659. * @param flag: Reason to remove the bonus_script. e_bonus_script_flags or e_bonus_script_types
  12660. * @author [Cydh]
  12661. **/
  12662. void pc_bonus_script_clear(map_session_data *sd, uint32 flag) {
  12663. struct linkdb_node *node = NULL;
  12664. uint16 count = 0;
  12665. if (!sd || !(node = sd->bonus_script.head))
  12666. return;
  12667. while (node) {
  12668. struct linkdb_node *next = node->next;
  12669. struct s_bonus_script_entry *entry = (struct s_bonus_script_entry *)node->data;
  12670. if (entry && (
  12671. (flag == BSF_PERMANENT) || // Remove all with permanent bonus
  12672. (!flag && !(entry->flag&BSF_PERMANENT)) || // Remove all WITHOUT permanent bonus
  12673. (flag&entry->flag) || // Matched flag
  12674. (flag&BSF_REM_BUFF && entry->type == 1) || // Remove buff
  12675. (flag&BSF_REM_DEBUFF && entry->type == 2) // Remove debuff
  12676. )
  12677. )
  12678. {
  12679. linkdb_erase(&sd->bonus_script.head, (void *)((intptr_t)entry));
  12680. pc_bonus_script_free_entry(sd, entry);
  12681. count++;
  12682. }
  12683. node = next;
  12684. }
  12685. pc_bonus_script_check_final(sd);
  12686. if (count && !(flag&BSF_REM_ON_LOGOUT)) //Don't need to do this if log out
  12687. status_calc_pc(sd,SCO_NONE);
  12688. }
  12689. /** [Cydh]
  12690. * Gives/removes SC_BASILICA when player steps in/out the cell with 'cell_basilica'
  12691. * @param sd: Target player
  12692. */
  12693. void pc_cell_basilica(map_session_data *sd) {
  12694. nullpo_retv(sd);
  12695. #ifdef RENEWAL
  12696. enum sc_type type = SC_BASILICA_CELL;
  12697. #else
  12698. enum sc_type type = SC_BASILICA;
  12699. #endif
  12700. if (!map_getcell(sd->bl.m,sd->bl.x,sd->bl.y,CELL_CHKBASILICA)) {
  12701. if (sd->sc.getSCE(type))
  12702. status_change_end(&sd->bl, type);
  12703. }
  12704. else if (!sd->sc.getSCE(type))
  12705. sc_start(&sd->bl,&sd->bl, type,100,0,INFINITE_TICK);
  12706. }
  12707. /** [Cydh]
  12708. * Get maximum specified parameter for specified class
  12709. * @param sd: Player data
  12710. * @param param: Max parameter to check
  12711. * @return max_param
  12712. */
  12713. uint16 pc_maxparameter(map_session_data *sd, e_params param) {
  12714. nullpo_retr(0, sd);
  12715. std::shared_ptr<s_job_info> job = job_db.find(pc_mapid2jobid(sd->class_,sd->status.sex));
  12716. if( job == nullptr || param == PARAM_MAX ){
  12717. return 0;
  12718. }
  12719. return job->max_param[param];
  12720. }
  12721. /**
  12722. * Get max ASPD for player based on Class
  12723. * @param sd Player
  12724. * @return ASPD
  12725. */
  12726. short pc_maxaspd(map_session_data *sd) {
  12727. nullpo_ret(sd);
  12728. return (( sd->class_&JOBL_THIRD) ? battle_config.max_third_aspd : (
  12729. ((sd->class_&MAPID_UPPERMASK) == MAPID_KAGEROUOBORO || (sd->class_&MAPID_UPPERMASK) == MAPID_REBELLION) ? battle_config.max_extended_aspd : (
  12730. (sd->class_&MAPID_BASEMASK) == MAPID_SUMMONER) ? battle_config.max_summoner_aspd :
  12731. battle_config.max_aspd ));
  12732. }
  12733. /**
  12734. * Calculates total item-group related bonuses for the given item
  12735. * @param sd Player
  12736. * @param nameid Item ID
  12737. * @return Heal rate
  12738. **/
  12739. short pc_get_itemgroup_bonus(map_session_data* sd, t_itemid nameid, std::vector<s_item_bonus>& bonuses) {
  12740. if (bonuses.empty())
  12741. return 0;
  12742. short bonus = 0;
  12743. for (const auto &it : bonuses) {
  12744. uint16 group_id = it.id;
  12745. if (group_id == 0)
  12746. continue;
  12747. if (itemdb_group.item_exists(group_id, nameid))
  12748. bonus += it.val;
  12749. }
  12750. return bonus;
  12751. }
  12752. /**
  12753. * Calculates total item-group related bonuses for the given item group
  12754. * @param sd Player
  12755. * @param group_id Item Group ID
  12756. * @return Heal rate
  12757. **/
  12758. short pc_get_itemgroup_bonus_group(map_session_data* sd, uint16 group_id, std::vector<s_item_bonus>& bonuses) {
  12759. if (bonuses.empty())
  12760. return 0;
  12761. for (const auto &it : bonuses) {
  12762. if (it.id == group_id)
  12763. return it.val;
  12764. }
  12765. return 0;
  12766. }
  12767. /**
  12768. * Check if player's equip index in same specified position, like for 2-Handed weapon & Heagdear (inc. costume)
  12769. * @param eqi Item EQI of enum equip_index
  12770. * @param *equip_index Player's equip_index[]
  12771. * @param index Known index item in inventory from sd->equip_index[] to compare with specified EQI in *equip_index
  12772. * @return True if item in same inventory index, False if doesn't
  12773. */
  12774. bool pc_is_same_equip_index(enum equip_index eqi, short *equip_index, short index) {
  12775. if (index < 0 || index >= MAX_INVENTORY)
  12776. return true;
  12777. // Dual weapon checks
  12778. if (eqi == EQI_HAND_R && equip_index[EQI_HAND_L] == index)
  12779. return true;
  12780. // Headgear with Mid & Low location
  12781. if (eqi == EQI_HEAD_MID && equip_index[EQI_HEAD_LOW] == index)
  12782. return true;
  12783. // Headgear with Top & Mid or Low location
  12784. if (eqi == EQI_HEAD_TOP && (equip_index[EQI_HEAD_MID] == index || equip_index[EQI_HEAD_LOW] == index))
  12785. return true;
  12786. // Headgear with Mid & Low location
  12787. if (eqi == EQI_COSTUME_HEAD_MID && equip_index[EQI_COSTUME_HEAD_LOW] == index)
  12788. return true;
  12789. // Headgear with Top & Mid or Low location
  12790. if (eqi == EQI_COSTUME_HEAD_TOP && (equip_index[EQI_COSTUME_HEAD_MID] == index || equip_index[EQI_COSTUME_HEAD_LOW] == index))
  12791. return true;
  12792. return false;
  12793. }
  12794. /**
  12795. * Generate Unique item ID for player
  12796. * @param sd : Player
  12797. * @return A generated Unique item ID
  12798. */
  12799. uint64 pc_generate_unique_id(map_session_data *sd) {
  12800. nullpo_ret(sd);
  12801. return ((uint64)sd->status.char_id << 32) | sd->status.uniqueitem_counter++;
  12802. }
  12803. /**
  12804. * Validating skill from player after logged on
  12805. * @param sd
  12806. **/
  12807. void pc_validate_skill(map_session_data *sd) {
  12808. if (sd) {
  12809. uint16 i = 0, count = 0;
  12810. struct s_skill tmp_skills[MAX_SKILL] = {{ 0 }};
  12811. memcpy(tmp_skills, sd->status.skill, sizeof(sd->status.skill));
  12812. memset(sd->status.skill, 0, sizeof(sd->status.skill));
  12813. for (i = 0; i < MAX_SKILL; i++) {
  12814. uint16 idx = 0;
  12815. if (tmp_skills[i].id == 0 || tmp_skills[i].lv == 0)
  12816. continue;
  12817. if ((idx = skill_get_index(tmp_skills[i].id))) {
  12818. memcpy(&sd->status.skill[idx], &tmp_skills[i], sizeof(tmp_skills[i]));
  12819. count++;
  12820. }
  12821. else
  12822. 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);
  12823. }
  12824. }
  12825. }
  12826. /**
  12827. * Show available NPC Quest / Event Icon Check [Kisuka]
  12828. * @param sd Player
  12829. **/
  12830. void pc_show_questinfo(map_session_data *sd) {
  12831. #if PACKETVER >= 20090218
  12832. nullpo_retv(sd);
  12833. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12834. return;
  12835. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12836. nullpo_retv(mapdata);
  12837. if (mapdata->qi_npc.empty())
  12838. return;
  12839. if (mapdata->qi_npc.size() != sd->qi_display.size())
  12840. return; // init was not called yet
  12841. for (int i = 0; i < mapdata->qi_npc.size(); i++) {
  12842. struct npc_data *nd = map_id2nd(mapdata->qi_npc[i]);
  12843. if (!nd || nd->qi_data.empty())
  12844. continue;
  12845. bool show = false;
  12846. for (auto &qi : nd->qi_data) {
  12847. if (!qi->condition || achievement_check_condition(qi->condition, sd)) {
  12848. show = true;
  12849. // Check if need to be displayed
  12850. if (!sd->qi_display[i].is_active || qi->icon != sd->qi_display[i].icon || qi->color != sd->qi_display[i].color) {
  12851. sd->qi_display[i].is_active = true;
  12852. sd->qi_display[i].icon = qi->icon;
  12853. sd->qi_display[i].color = qi->color;
  12854. clif_quest_show_event(sd, &nd->bl, qi->icon, qi->color);
  12855. }
  12856. break;
  12857. }
  12858. }
  12859. if (show == false) {
  12860. // Check if need to be hide
  12861. if (sd->qi_display[i].is_active) {
  12862. sd->qi_display[i].is_active = false;
  12863. sd->qi_display[i].icon = QTYPE_NONE;
  12864. sd->qi_display[i].color = QMARK_NONE;
  12865. #if PACKETVER >= 20120410
  12866. clif_quest_show_event(sd, &nd->bl, QTYPE_NONE, QMARK_NONE);
  12867. #else
  12868. clif_quest_show_event(sd, &nd->bl, QTYPE_QUEST, QMARK_NONE);
  12869. #endif
  12870. }
  12871. }
  12872. }
  12873. #endif
  12874. }
  12875. /**
  12876. * Reinit the questinfo for player when changing map
  12877. * @param sd Player
  12878. **/
  12879. void pc_show_questinfo_reinit(map_session_data *sd) {
  12880. #if PACKETVER >= 20090218
  12881. nullpo_retv(sd);
  12882. sd->qi_display.clear();
  12883. if (sd->bl.m < 0 || sd->bl.m >= MAX_MAPINDEX)
  12884. return;
  12885. struct map_data *mapdata = map_getmapdata(sd->bl.m);
  12886. nullpo_retv(mapdata);
  12887. if (mapdata->qi_npc.empty())
  12888. return;
  12889. sd->qi_display.reserve( mapdata->qi_npc.size() );
  12890. for( int i = 0; i < mapdata->qi_npc.size(); i++ ){
  12891. sd->qi_display.push_back( s_qi_display() );
  12892. }
  12893. #endif
  12894. }
  12895. /**
  12896. * Check if a job is allowed to enter the map
  12897. * @param jobid Job ID see enum e_job or sd->status.class_
  12898. * @param m ID -an index- for direct indexing map[] array
  12899. * @return 1 if job is allowed, 0 otherwise
  12900. **/
  12901. bool pc_job_can_entermap(enum e_job jobid, int m, int group_lv) {
  12902. // Map is other map server.
  12903. // !FIXME: Currently, a map-server doesn't recognized map's attributes on other server, so we assume it's fine to warp.
  12904. if (m < 0)
  12905. return true;
  12906. struct map_data *mapdata = map_getmapdata(m);
  12907. if (!mapdata->cell)
  12908. return false;
  12909. if (!pcdb_checkid(jobid))
  12910. return false;
  12911. std::shared_ptr<s_job_info> job = job_db.find(jobid);
  12912. if (!job->noenter_map.zone || group_lv > job->noenter_map.group_lv)
  12913. return true;
  12914. if ((job->noenter_map.zone&1 && !mapdata_flag_vs2(mapdata)) || // Normal
  12915. (job->noenter_map.zone&2 && mapdata->flag[MF_PVP]) || // PVP
  12916. (job->noenter_map.zone&4 && mapdata_flag_gvg2_no_te(mapdata)) || // GVG
  12917. (job->noenter_map.zone&8 && mapdata->flag[MF_BATTLEGROUND]) || // Battleground
  12918. (job->noenter_map.zone&16 && mapdata_flag_gvg2_te(mapdata)) || // WOE:TE
  12919. (job->noenter_map.zone&(mapdata->zone) && mapdata->flag[MF_RESTRICTED]) // Zone restriction
  12920. )
  12921. return false;
  12922. return true;
  12923. }
  12924. /**
  12925. * Tells client about player's costume view on mapchange for checking 'nocostume' mapflag.
  12926. * @param sd
  12927. **/
  12928. void pc_set_costume_view(map_session_data *sd) {
  12929. int i = -1, head_low = 0, head_mid = 0, head_top = 0, robe = 0;
  12930. struct item_data *id = NULL;
  12931. nullpo_retv(sd);
  12932. head_low = sd->status.head_bottom;
  12933. head_mid = sd->status.head_mid;
  12934. head_top = sd->status.head_top;
  12935. robe = sd->status.robe;
  12936. sd->status.head_bottom = sd->status.head_mid = sd->status.head_top = sd->status.robe = 0;
  12937. //Added check to prevent sending the same look on multiple slots ->
  12938. //causes client to redraw item on top of itself. (suggested by Lupus)
  12939. // Normal headgear checks
  12940. if ((i = sd->equip_index[EQI_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12941. if (!(id->equip&(EQP_HEAD_MID|EQP_HEAD_TOP)))
  12942. sd->status.head_bottom = id->look;
  12943. else
  12944. sd->status.head_bottom = 0;
  12945. }
  12946. if ((i = sd->equip_index[EQI_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12947. if (!(id->equip&(EQP_HEAD_TOP)))
  12948. sd->status.head_mid = id->look;
  12949. else
  12950. sd->status.head_mid = 0;
  12951. }
  12952. if ((i = sd->equip_index[EQI_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12953. sd->status.head_top = id->look;
  12954. if ((i = sd->equip_index[EQI_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12955. sd->status.robe = id->look;
  12956. // Costumes check
  12957. if (!map_getmapflag(sd->bl.m, MF_NOCOSTUME)) {
  12958. if ((i = sd->equip_index[EQI_COSTUME_HEAD_LOW]) != -1 && (id = sd->inventory_data[i])) {
  12959. if (!(id->equip&(EQP_COSTUME_HEAD_MID|EQP_COSTUME_HEAD_TOP)))
  12960. sd->status.head_bottom = id->look;
  12961. else
  12962. sd->status.head_bottom = 0;
  12963. }
  12964. if ((i = sd->equip_index[EQI_COSTUME_HEAD_MID]) != -1 && (id = sd->inventory_data[i])) {
  12965. if (!(id->equip&EQP_COSTUME_HEAD_TOP))
  12966. sd->status.head_mid = id->look;
  12967. else
  12968. sd->status.head_mid = 0;
  12969. }
  12970. if ((i = sd->equip_index[EQI_COSTUME_HEAD_TOP]) != -1 && (id = sd->inventory_data[i]))
  12971. sd->status.head_top = id->look;
  12972. if ((i = sd->equip_index[EQI_COSTUME_GARMENT]) != -1 && (id = sd->inventory_data[i]))
  12973. sd->status.robe = id->look;
  12974. }
  12975. if (sd->setlook_head_bottom)
  12976. sd->status.head_bottom = sd->setlook_head_bottom;
  12977. if (sd->setlook_head_mid)
  12978. sd->status.head_mid = sd->setlook_head_mid;
  12979. if (sd->setlook_head_top)
  12980. sd->status.head_top = sd->setlook_head_top;
  12981. if (sd->setlook_robe)
  12982. sd->status.robe = sd->setlook_robe;
  12983. if (head_low != sd->status.head_bottom)
  12984. clif_changelook(&sd->bl, LOOK_HEAD_BOTTOM, sd->status.head_bottom);
  12985. if (head_mid != sd->status.head_mid)
  12986. clif_changelook(&sd->bl, LOOK_HEAD_MID, sd->status.head_mid);
  12987. if (head_top != sd->status.head_top)
  12988. clif_changelook(&sd->bl, LOOK_HEAD_TOP, sd->status.head_top);
  12989. if (robe != sd->status.robe)
  12990. clif_changelook(&sd->bl, LOOK_ROBE, sd->status.robe);
  12991. }
  12992. std::shared_ptr<s_attendance_period> pc_attendance_period(){
  12993. uint32 date = date_get(DT_YYYYMMDD);
  12994. for( std::pair<const uint32,std::shared_ptr<s_attendance_period>>& pair : attendance_db ){
  12995. std::shared_ptr<s_attendance_period> period = pair.second;
  12996. if( period->start <= date && period->end >= date ){
  12997. return period;
  12998. }
  12999. }
  13000. return nullptr;
  13001. }
  13002. bool pc_attendance_enabled(){
  13003. // Check if the attendance feature is disabled
  13004. if( !battle_config.feature_attendance ){
  13005. return false;
  13006. }
  13007. // Check if there is a running attendance period
  13008. return pc_attendance_period() != nullptr;
  13009. }
  13010. static inline bool pc_attendance_rewarded_today( map_session_data* sd ){
  13011. return pc_readreg2( sd, ATTENDANCE_DATE_VAR ) >= date_get(DT_YYYYMMDD);
  13012. }
  13013. int32 pc_attendance_counter( map_session_data* sd ){
  13014. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13015. // No running attendance period
  13016. if( period == nullptr ){
  13017. return 0;
  13018. }
  13019. // Get the counter for the current period
  13020. int counter = static_cast<int>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13021. // Check if we have a remaining counter from a previous period
  13022. if( counter > 0 && pc_readreg2( sd, ATTENDANCE_DATE_VAR ) < period->start ){
  13023. // Reset the counter to zero
  13024. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, 0 );
  13025. return 0;
  13026. }
  13027. return 10 * counter + ( ( pc_attendance_rewarded_today(sd) ) ? 1 : 0 );
  13028. }
  13029. void pc_attendance_claim_reward( map_session_data* sd ){
  13030. // If the user's group does not have the permission
  13031. if( !pc_has_permission( sd, PC_PERM_ATTENDANCE ) ){
  13032. return;
  13033. }
  13034. // Check if the attendance feature is disabled
  13035. if( !pc_attendance_enabled() ){
  13036. return;
  13037. }
  13038. // Check if the user already got his reward today
  13039. if( pc_attendance_rewarded_today( sd ) ){
  13040. return;
  13041. }
  13042. int32 attendance_counter = static_cast<int32>(pc_readreg2( sd, ATTENDANCE_COUNT_VAR ));
  13043. attendance_counter += 1;
  13044. std::shared_ptr<s_attendance_period> period = pc_attendance_period();
  13045. if( period == nullptr ){
  13046. return;
  13047. }
  13048. if( period->rewards.size() < attendance_counter ){
  13049. return;
  13050. }
  13051. pc_setreg2( sd, ATTENDANCE_DATE_VAR, date_get(DT_YYYYMMDD) );
  13052. pc_setreg2( sd, ATTENDANCE_COUNT_VAR, attendance_counter );
  13053. if( save_settings&CHARSAVE_ATTENDANCE )
  13054. chrif_save(sd, CSAVE_NORMAL);
  13055. std::shared_ptr<s_attendance_reward> reward = period->rewards[attendance_counter - 1];
  13056. struct mail_message msg;
  13057. memset( &msg, 0, sizeof( struct mail_message ) );
  13058. msg.dest_id = sd->status.char_id;
  13059. safestrncpy( msg.send_name, msg_txt( sd, 788 ), NAME_LENGTH );
  13060. safesnprintf( msg.title, MAIL_TITLE_LENGTH, msg_txt( sd, 789 ), attendance_counter );
  13061. safesnprintf( msg.body, MAIL_BODY_LENGTH, msg_txt( sd, 790 ), attendance_counter );
  13062. msg.item[0].nameid = reward->item_id;
  13063. msg.item[0].amount = reward->amount;
  13064. msg.item[0].identify = 1;
  13065. msg.status = MAIL_NEW;
  13066. msg.type = MAIL_INBOX_NORMAL;
  13067. msg.timestamp = time(NULL);
  13068. intif_Mail_send(0, &msg);
  13069. clif_attendence_response( sd, attendance_counter );
  13070. }
  13071. /**
  13072. * Send a player to jail and determine the location to send in jail.
  13073. * @param sd: Player data
  13074. * @param duration: Duration in minutes (default INT_MAX = infinite)
  13075. */
  13076. void pc_jail(map_session_data &sd, int32 duration) {
  13077. uint16 m_index = mapindex_name2id(MAP_JAIL);
  13078. int16 x, y;
  13079. switch (rnd() % 2) { // Jail Locations
  13080. case 0: // Jail #1
  13081. x = 49;
  13082. y = 75;
  13083. break;
  13084. default: // Default Jail
  13085. x = 24;
  13086. y = 75;
  13087. break;
  13088. }
  13089. duration = i32max(0, duration); // Can't be less than 0 seconds.
  13090. // If duration > 0 then triggered via jailfor which checks every minute.
  13091. // If duration == INT_MAX then triggered via jail for infinite duration.
  13092. // If duration == 0 then triggered via unjail and end status.
  13093. if (duration > 0)
  13094. sc_start4(nullptr, &sd.bl, SC_JAILED, 100, duration, m_index, x, y, 60000);
  13095. else
  13096. status_change_end(&sd.bl, SC_JAILED);
  13097. }
  13098. /**
  13099. * Determine the punishment type when failing macro checks.
  13100. * @param sd: Player data
  13101. * @param stype: Macro detection status type (for banning)
  13102. */
  13103. static void pc_macro_punishment(map_session_data &sd, e_macro_detect_status stype) {
  13104. int32 duration = 0;
  13105. // Determine if there's a unique duration
  13106. if (battle_config.macro_detection_punishment_time > 0) {
  13107. char time[13];
  13108. safesnprintf(time, 13, "+%dnm", battle_config.macro_detection_punishment_time);
  13109. duration = static_cast<int32>(solve_time(time));
  13110. }
  13111. // Delete the timer
  13112. if (sd.macro_detect.timer != INVALID_TIMER)
  13113. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13114. // Clear the macro detect data
  13115. sd.macro_detect = {};
  13116. sd.macro_detect.timer = INVALID_TIMER;
  13117. // Unblock all actions for the player
  13118. sd.state.block_action &= ~PCBLOCK_ALL;
  13119. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13120. if (battle_config.macro_detection_punishment == 0) { // Ban
  13121. clif_macro_detector_status(sd, stype);
  13122. 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);
  13123. } else { // Jail
  13124. // Send success to close the window without closing the client
  13125. clif_macro_detector_status(sd, MCD_GOOD);
  13126. pc_jail(sd, (duration == 0 ? INT_MAX : duration / 60));
  13127. }
  13128. }
  13129. /**
  13130. * Save a captcha image to memory via /macro_register.
  13131. * @param sd: Player data
  13132. * @param image_size: Captcha image size
  13133. * @param captcha_answer: Answer to captcha
  13134. */
  13135. void pc_macro_captcha_register(map_session_data &sd, uint16 image_size, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13136. nullpo_retv(captcha_answer);
  13137. sd.captcha_upload.cd = nullptr;
  13138. sd.captcha_upload.upload_size = 0;
  13139. if (strlen(captcha_answer) < 4 || image_size == 0 || image_size > CAPTCHA_BMP_SIZE) {
  13140. clif_captcha_upload_request(sd); // Notify client of failure.
  13141. return;
  13142. }
  13143. std::shared_ptr<s_captcha_data> cd = std::make_shared<s_captcha_data>();
  13144. sd.captcha_upload.cd = cd;
  13145. cd->image_size = image_size;
  13146. safestrncpy(cd->captcha_answer, captcha_answer, sizeof(cd->captcha_answer));
  13147. memset(cd->image_data, 0, sizeof(cd->image_data));
  13148. // Request the image data from the client.
  13149. clif_captcha_upload_request(sd);
  13150. }
  13151. /**
  13152. * Save captcha image to server.
  13153. * @param sd: Player data
  13154. * @param captcha_key: Captcha ID
  13155. * @param upload_size: Captcha size
  13156. * @param upload_data: Image data
  13157. */
  13158. void pc_macro_captcha_register_upload(map_session_data &sd, uint16 upload_size, char *upload_data) {
  13159. nullpo_retv(upload_data);
  13160. memcpy(&sd.captcha_upload.cd->image_data[sd.captcha_upload.upload_size], upload_data, upload_size);
  13161. sd.captcha_upload.upload_size += upload_size;
  13162. // Notify that the image finished uploading.
  13163. if (sd.captcha_upload.upload_size == sd.captcha_upload.cd->image_size) {
  13164. // Tell the client that the upload was finished
  13165. clif_captcha_upload_end(sd);
  13166. // Look for a free key
  13167. uint16 index;
  13168. for (index = 0; index < UINT16_MAX; index++) {
  13169. if (!captcha_db.exists(index)) {
  13170. break;
  13171. }
  13172. }
  13173. if (index == UINT16_MAX) {
  13174. // no free key found...
  13175. sd.captcha_upload.cd = nullptr;
  13176. sd.captcha_upload.upload_size = 0;
  13177. return;
  13178. }
  13179. captcha_db.put(index, sd.captcha_upload.cd);
  13180. sd.captcha_upload.cd = nullptr;
  13181. sd.captcha_upload.upload_size = 0;
  13182. // TODO: write YAML and BMP file?
  13183. }
  13184. }
  13185. /**
  13186. * Timer attached to target player with attempts to confirm captcha.
  13187. */
  13188. TIMER_FUNC(pc_macro_detector_timeout) {
  13189. map_session_data *sd = map_id2sd(id);
  13190. nullpo_ret(sd);
  13191. // Remove the current timer
  13192. sd->macro_detect.timer = INVALID_TIMER;
  13193. // Deduct an answering attempt
  13194. sd->macro_detect.retry -= 1;
  13195. if (sd->macro_detect.retry == 0) {
  13196. // All attempts have been exhausted, punish the user
  13197. pc_macro_punishment(*sd, MCD_TIMEOUT);
  13198. } else {
  13199. // Update the client
  13200. clif_macro_detector_request_show(*sd);
  13201. // Start a new timer
  13202. sd->macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd->bl.id, 0);
  13203. }
  13204. return 0;
  13205. }
  13206. /**
  13207. * Check player's captcha answer.
  13208. * @param sd: Player data
  13209. * @param captcha_answer: Captcha answer entered by player
  13210. */
  13211. void pc_macro_detector_process_answer(map_session_data &sd, char captcha_answer[CAPTCHA_ANSWER_SIZE]) {
  13212. nullpo_retv(captcha_answer);
  13213. const std::shared_ptr<s_captcha_data> cd = sd.macro_detect.cd;
  13214. // Has no captcha request
  13215. if (cd == nullptr) {
  13216. return;
  13217. }
  13218. // Correct answer
  13219. if (strcmp(captcha_answer, cd->captcha_answer) == 0) {
  13220. // Delete the timer
  13221. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13222. // Clear the macro detect data
  13223. sd.macro_detect = {};
  13224. sd.macro_detect.timer = INVALID_TIMER;
  13225. // Unblock all actions for the player
  13226. sd.state.block_action &= ~PCBLOCK_ALL;
  13227. sd.state.block_action &= ~PCBLOCK_IMMUNE;
  13228. // Assign temporary macro variable to check failures
  13229. pc_setreg(&sd, add_str("@captcha_retries"), battle_config.macro_detection_retry - sd.macro_detect.retry);
  13230. // Grant bonuses via script
  13231. run_script(cd->bonus_script, 0, sd.bl.id, fake_nd->bl.id);
  13232. // Notify the client
  13233. clif_macro_detector_status(sd, MCD_GOOD);
  13234. } else {
  13235. // Deduct an answering attempt
  13236. sd.macro_detect.retry -= 1;
  13237. // All attempts have been exhausted, punish the user
  13238. if (sd.macro_detect.retry <= 0) {
  13239. pc_macro_punishment(sd, MCD_INCORRECT);
  13240. return;
  13241. }
  13242. // Incorrect response, update the client
  13243. clif_macro_detector_request_show(sd);
  13244. // Reset the timer
  13245. addtick_timer(sd.macro_detect.timer, gettick() + battle_config.macro_detection_timeout);
  13246. }
  13247. }
  13248. /**
  13249. * Determine if a player tries to log out during a captcha check.
  13250. * @param sd: Player data
  13251. */
  13252. void pc_macro_detector_disconnect(map_session_data &sd) {
  13253. // Delete the timeout timer
  13254. if (sd.macro_detect.timer != INVALID_TIMER) {
  13255. delete_timer(sd.macro_detect.timer, pc_macro_detector_timeout);
  13256. sd.macro_detect.timer = INVALID_TIMER;
  13257. }
  13258. // If the player disconnects before clearing the challenge the player is punished.
  13259. if (sd.macro_detect.retry != 0)
  13260. pc_macro_punishment(sd, MCD_TIMEOUT);
  13261. }
  13262. /**
  13263. * Save a list of players from an area select via /macro_detector.
  13264. */
  13265. int pc_macro_reporter_area_select_sub(block_list *bl, va_list ap) {
  13266. nullpo_retr(0, bl);
  13267. if (bl->type != BL_PC)
  13268. return 0;
  13269. std::vector<uint32> *aid_list = va_arg(ap, std::vector<uint32> *);
  13270. nullpo_ret(aid_list);
  13271. aid_list->push_back(bl->id);
  13272. return 0;
  13273. }
  13274. /**
  13275. * Area select via /macro_detector.
  13276. * @param sd: Player data
  13277. * @param x: X location
  13278. * @param y: Y location
  13279. * @param radius: Area
  13280. */
  13281. void pc_macro_reporter_area_select(map_session_data &sd, const int16 x, const int16 y, const int8 radius) {
  13282. std::vector<uint32> aid_list;
  13283. map_foreachinarea(pc_macro_reporter_area_select_sub, sd.bl.m, x - radius, y - radius, x + radius, y + radius, BL_PC, &aid_list);
  13284. clif_macro_reporter_select(sd, aid_list);
  13285. }
  13286. /**
  13287. * Send out captcha check to player.
  13288. * @param sd: Target player data
  13289. * @param reporter_account_id: Account ID of reporter
  13290. */
  13291. void pc_macro_reporter_process(map_session_data &sd, int32 reporter_account_id) {
  13292. if (captcha_db.empty())
  13293. return;
  13294. // Pick a random image from the database.
  13295. const std::shared_ptr<s_captcha_data> cd = captcha_db.random();
  13296. // Set macro detection data.
  13297. sd.macro_detect.cd = cd;
  13298. sd.macro_detect.reporter_aid = reporter_account_id;
  13299. sd.macro_detect.retry = battle_config.macro_detection_retry;
  13300. // Block all actions for the target player.
  13301. sd.state.block_action |= (PCBLOCK_ALL | PCBLOCK_IMMUNE);
  13302. // Open macro detect client side.
  13303. clif_macro_detector_request(sd);
  13304. // Start the timeout timer.
  13305. sd.macro_detect.timer = add_timer(gettick() + battle_config.macro_detection_timeout, pc_macro_detector_timeout, sd.bl.id, 0);
  13306. }
  13307. /**
  13308. * Parse a BMP image to memory.
  13309. * @param filepath: Image file location
  13310. * @param cd: Captcha data
  13311. */
  13312. bool pc_macro_read_captcha_db_loadbmp(const std::string &filepath, std::shared_ptr<s_captcha_data> cd) {
  13313. if (cd == nullptr)
  13314. return false;
  13315. FILE *fp = fopen(filepath.c_str(), "rb");
  13316. if (fp == nullptr) {
  13317. ShowError("%s: Failed to open file \"%s\"\n", __func__, filepath.c_str());
  13318. return false;
  13319. }
  13320. // Load the file data and verify magic
  13321. char bmp_data[CAPTCHA_BMP_SIZE];
  13322. if (fread(bmp_data, CAPTCHA_BMP_SIZE, 1, fp) != 1) {
  13323. ShowError("%s: Failed to read data from \"%s\"\n", __func__, filepath.c_str());
  13324. fclose(fp);
  13325. return false;
  13326. }
  13327. fclose(fp);
  13328. if (bmp_data[0] != 'B' || bmp_data[1] != 'M') {
  13329. ShowError("%s: Invalid BMP file header given at \"%s\"\n", __func__, filepath.c_str());
  13330. return false;
  13331. }
  13332. // Compress the data into the destination
  13333. unsigned long com_size = sizeof(cd->image_data);
  13334. encode_zip(cd->image_data, &com_size, bmp_data, CAPTCHA_BMP_SIZE);
  13335. cd->image_size = static_cast<int16>(com_size);
  13336. return true;
  13337. }
  13338. const std::string CaptchaDatabase::getDefaultLocation() {
  13339. return std::string(db_path) + "/captcha_db.yml";
  13340. }
  13341. /**
  13342. * Reads and parses an entry from the captcha_db.
  13343. * @param node: YAML node containing the entry.
  13344. * @return count of successfully parsed rows
  13345. */
  13346. uint64 CaptchaDatabase::parseBodyNode(const ryml::NodeRef &node) {
  13347. uint16 index;
  13348. if (!this->asUInt16(node, "Id", index))
  13349. return 0;
  13350. std::shared_ptr<s_captcha_data> cd = captcha_db.find(index);
  13351. bool exists = cd != nullptr;
  13352. if (!exists) {
  13353. if (!this->nodesExist(node, { "Filename", "Answer" }))
  13354. return 0;
  13355. cd = std::make_shared<s_captcha_data>();
  13356. cd->index = index;
  13357. }
  13358. if (this->nodeExists(node, "Filename")) {
  13359. std::string filename;
  13360. if (!this->asString(node, "Filename", filename))
  13361. return 0;
  13362. if (!pc_macro_read_captcha_db_loadbmp(filename, cd)) {
  13363. this->invalidWarning(node["Filename"], "Failed to parse BMP image, skipping...\n");
  13364. return 0;
  13365. }
  13366. }
  13367. if (this->nodeExists(node, "Answer")) {
  13368. std::string answer;
  13369. if (!this->asString(node, "Answer", answer))
  13370. return 0;
  13371. if (answer.length() < 4 || answer.length() > CAPTCHA_ANSWER_SIZE) {
  13372. this->invalidWarning(node["Answer"], "The captcha answer must be between 4~%d characters, skipping...", CAPTCHA_ANSWER_SIZE);
  13373. return 0;
  13374. }
  13375. safestrncpy(cd->captcha_answer, answer.c_str(), sizeof(cd->captcha_answer));
  13376. }
  13377. if (this->nodeExists(node, "Bonus")) {
  13378. std::string script;
  13379. if (!this->asString(node, "Bonus", script)) {
  13380. return 0;
  13381. }
  13382. if (cd->bonus_script) {
  13383. script_free_code(cd->bonus_script);
  13384. cd->bonus_script = nullptr;
  13385. }
  13386. cd->bonus_script = parse_script(script.c_str(), this->getCurrentFile().c_str(), this->getLineNumber(node["Bonus"]), SCRIPT_IGNORE_EXTERNAL_BRACKETS);
  13387. } else {
  13388. if (!exists)
  13389. 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);
  13390. }
  13391. if (!exists)
  13392. captcha_db.put(index, cd);
  13393. return 1;
  13394. }
  13395. /*==========================================
  13396. * pc Init/Terminate
  13397. *------------------------------------------*/
  13398. void do_final_pc(void) {
  13399. db_destroy(itemcd_db);
  13400. do_final_pc_groups();
  13401. ers_destroy(pc_sc_display_ers);
  13402. ers_destroy(num_reg_ers);
  13403. ers_destroy(str_reg_ers);
  13404. attendance_db.clear();
  13405. reputation_db.clear();
  13406. #ifdef MAP_GENERATOR
  13407. reputationgroup_db.clear();
  13408. #endif
  13409. penalty_db.clear();
  13410. captcha_db.clear();
  13411. }
  13412. void do_init_pc(void) {
  13413. itemcd_db = idb_alloc(DB_OPT_RELEASE_DATA);
  13414. pc_readdb();
  13415. pc_read_motd(); // Read MOTD [Valaris]
  13416. attendance_db.load();
  13417. reputation_db.load();
  13418. #ifdef MAP_GENERATOR
  13419. reputationgroup_db.load();
  13420. #endif
  13421. captcha_db.load();
  13422. add_timer_func_list(pc_invincible_timer, "pc_invincible_timer");
  13423. add_timer_func_list(pc_eventtimer, "pc_eventtimer");
  13424. add_timer_func_list(pc_inventory_rental_end, "pc_inventory_rental_end");
  13425. add_timer_func_list(pc_calc_pvprank_timer, "pc_calc_pvprank_timer");
  13426. add_timer_func_list(pc_autosave, "pc_autosave");
  13427. add_timer_func_list(pc_spiritball_timer, "pc_spiritball_timer");
  13428. add_timer_func_list(pc_follow_timer, "pc_follow_timer");
  13429. add_timer_func_list(pc_endautobonus, "pc_endautobonus");
  13430. add_timer_func_list(pc_spiritcharm_timer, "pc_spiritcharm_timer");
  13431. add_timer_func_list(pc_global_expiration_timer, "pc_global_expiration_timer");
  13432. add_timer_func_list(pc_expiration_timer, "pc_expiration_timer");
  13433. add_timer_func_list(pc_autotrade_timer, "pc_autotrade_timer");
  13434. add_timer_func_list(pc_on_expire_active, "pc_on_expire_active");
  13435. add_timer_func_list(pc_macro_detector_timeout, "pc_macro_detector_timeout");
  13436. add_timer(gettick() + autosave_interval, pc_autosave, 0, 0);
  13437. // 0=day, 1=night [Yor]
  13438. night_flag = battle_config.night_at_start ? 1 : 0;
  13439. if (battle_config.day_duration > 0 && battle_config.night_duration > 0) {
  13440. int day_duration = battle_config.day_duration;
  13441. int night_duration = battle_config.night_duration;
  13442. // add night/day timer [Yor]
  13443. add_timer_func_list(map_day_timer, "map_day_timer");
  13444. add_timer_func_list(map_night_timer, "map_night_timer");
  13445. day_timer_tid = add_timer_interval(gettick() + (night_flag ? 0 : day_duration) + night_duration, map_day_timer, 0, 0, day_duration + night_duration);
  13446. night_timer_tid = add_timer_interval(gettick() + day_duration + (night_flag ? night_duration : 0), map_night_timer, 0, 0, day_duration + night_duration);
  13447. }
  13448. do_init_pc_groups();
  13449. pc_sc_display_ers = ers_new(sizeof(struct sc_display_entry), "pc.cpp:pc_sc_display_ers", ERS_OPT_FLEX_CHUNK);
  13450. num_reg_ers = ers_new(sizeof(struct script_reg_num), "pc.cpp:num_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13451. str_reg_ers = ers_new(sizeof(struct script_reg_str), "pc.cpp:str_reg_ers", (ERSOptions)(ERS_OPT_CLEAN|ERS_OPT_FLEX_CHUNK));
  13452. ers_chunk_size(pc_sc_display_ers, 150);
  13453. ers_chunk_size(num_reg_ers, 300);
  13454. ers_chunk_size(str_reg_ers, 50);
  13455. }